Skip to content

Commit

Permalink
Honor max GRF set by IGC
Browse files Browse the repository at this point in the history
  • Loading branch information
jfuentes authored and igcbot committed Feb 22, 2025
1 parent d4a5cd0 commit d613057
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 7 deletions.
21 changes: 17 additions & 4 deletions visa/G4_Kernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -635,15 +635,16 @@ bool G4_Kernel::updateKernelToLargerGRF() {
//
// Updates kernel's related structures based on register pressure
//
void G4_Kernel::updateKernelByRegPressure(unsigned regPressure) {
void G4_Kernel::updateKernelByRegPressure(unsigned regPressure,
bool forceGRFModeUp) {
unsigned largestInputReg = getLargestInputRegister();
if (m_kernelAttrs->isKernelAttrSet(Attributes::ATTR_MaxRegThreadDispatch)) {
unsigned maxRegPayloadDispatch = m_kernelAttrs->getInt32KernelAttr(
Attributes::ATTR_MaxRegThreadDispatch);
largestInputReg = std::max(largestInputReg, maxRegPayloadDispatch);
}

unsigned newGRF = grfMode.setModeByRegPressure(regPressure, largestInputReg);
unsigned newGRF = grfMode.setModeByRegPressure(regPressure, largestInputReg, forceGRFModeUp);

if (newGRF == numRegTotal)
return;
Expand Down Expand Up @@ -2167,10 +2168,15 @@ GRFMode::GRFMode(const TARGET_PLATFORM platform, Options *op) : options(op) {
unsigned maxGRF = op->getuInt32Option(vISA_MaxGRFNum);
upperBoundGRF = maxGRF > 0 ? maxGRF : configs.back().numGRF;
vISA_ASSERT(isValidNumGRFs(upperBoundGRF), "Invalid upper bound for GRF number");

// Select higher GRF
GRFModeUpValue = op->getuInt32Option(vISA_ForceGRFModeUp);
vISA_ASSERT(GRFModeUpValue >= 0 && GRFModeUpValue <= configs.size(),
"Invalid value for selecting a higher GRF mode");
}

unsigned GRFMode::setModeByRegPressure(unsigned maxRP,
unsigned largestInputReg) {
unsigned GRFMode::setModeByRegPressure(unsigned maxRP, unsigned largestInputReg,
bool forceGRFModeUp) {
unsigned size = configs.size(), i = 0;
bool spillAllowed = 0;
spillAllowed = options->getuInt32Option(vISA_SpillAllowed) > 256;
Expand All @@ -2184,6 +2190,13 @@ unsigned GRFMode::setModeByRegPressure(unsigned maxRP,
// those blocked for kernel input. This helps cases
// where an 8 GRF variable shows up in entry BB.
(largestInputReg + 8) <= configs[i].numGRF) {
if (forceGRFModeUp && GRFModeUpValue > 0) {
// Check if user is force a higher GRF mode
unsigned newGRFMode = currentMode + GRFModeUpValue;
unsigned maxGRFMode = getMaxGRFMode();
currentMode = newGRFMode < maxGRFMode ? newGRFMode : maxGRFMode;
}

if (spillAllowed && currentMode > 0)
return configs[--currentMode].numGRF;
else
Expand Down
15 changes: 13 additions & 2 deletions visa/G4_Kernel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@ class GRFMode {
return iter != configs.end();
}

unsigned setModeByRegPressure(unsigned maxRP, unsigned largestInputReg);
unsigned setModeByRegPressure(unsigned maxRP, unsigned largestInputReg,
bool forceGRFModedUp = false);
bool hasLargerGRFSameThreads() const;

unsigned getNumGRF() const { return configs[currentMode].numGRF; }
Expand Down Expand Up @@ -189,6 +190,14 @@ class GRFMode {
return found->numGRF;
}

unsigned getMaxGRFMode() const {
auto found =
std::find_if(configs.rbegin(), configs.rend(), [this](const Config &c) {
return c.VRTEnable && c.numGRF <= upperBoundGRF;
});
return configs.size() - std::distance(configs.rbegin(), found) - 1;
}

// Get GRF number for initial kernel creation
unsigned getInitalGRFNum() const {
// Max GRF number is used when GRF selection is enabled.
Expand Down Expand Up @@ -255,6 +264,7 @@ class GRFMode {
unsigned currentMode;
unsigned lowerBoundGRF;
unsigned upperBoundGRF;
unsigned GRFModeUpValue;
Options *options;
};

Expand Down Expand Up @@ -712,7 +722,8 @@ class G4_Kernel {
const char *getName() const { return name; }

bool updateKernelToLargerGRF();
void updateKernelByRegPressure(unsigned regPressure);
void updateKernelByRegPressure(unsigned regPressure,
bool forceGRFModeUp = false);
bool updateKernelFromNumGRFAttr();

void evalAddrExp();
Expand Down
2 changes: 1 addition & 1 deletion visa/LocalScheduler/G4_Sched.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -747,7 +747,7 @@ bool preRA_Scheduler::runWithGRFSelection(unsigned &KernelPressure) {
KernelPressure = rp.getMaxRP();
}

kernel.updateKernelByRegPressure(KernelPressure);
kernel.updateKernelByRegPressure(KernelPressure, true);

return Changed;
}
Expand Down
3 changes: 3 additions & 0 deletions visa/Optimizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,10 @@ class Optimizer {
unsigned KernelPressure = 0;
preRA_Scheduler Sched(kernel);
if (kernel.useAutoGRFSelection()) {
unsigned InitialGRFNumber = kernel.getNumRegTotal();
Sched.runWithGRFSelection(KernelPressure);
if (InitialGRFNumber != kernel.getNumRegTotal())
Sched.run(KernelPressure);
} else {
Sched.run(KernelPressure);
}
Expand Down
5 changes: 5 additions & 0 deletions visa/include/VISAOptionsDefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,11 @@ DEF_VISA_OPTION(
"Spill size allowed without increasing GRF number in VRT."
"0 means VRT will always bump up the GRF number to avoid spills",
256)
DEF_VISA_OPTION(vISA_ForceGRFModeUp, ET_INT32, "-forceGRFModeUp",
"USAGE: -forceGRFModeUp <k>.\n"
"Set the GRF mode k higher than the one selected by default"
"heuristics. 0 means no increase in GRF mode.",
0)
DEF_VISA_OPTION(vISA_ScalarPipe, ET_INT32, "-scalarPipe",
"USAGE: -scalarPipe <num>\n", 0)
DEF_VISA_OPTION(vISA_LVN, ET_BOOL, "-nolvn", UNUSED, true)
Expand Down

0 comments on commit d613057

Please sign in to comment.