Skip to content

Commit

Permalink
Document StackOverflowDetection flag in a warning
Browse files Browse the repository at this point in the history
Inform the user about StackOverflowDetection flag that can
be used to debug VLA related memory issues.
  • Loading branch information
fda0 authored and igcbot committed Sep 21, 2023
1 parent 501e2ed commit 27557fb
Showing 1 changed file with 18 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ namespace IGC {
unsigned int operandIndex;
};

void expandPrivateMemoryForVla(uint32_t &maxPrivateMem);
static bool testTransposedMemory(const Type* pTmpType, const Type* const pTypeOfAccessedObject, uint64_t tmpAllocaSize, const uint64_t bufferSizeLimit);

/// @brief The module level alloca information
Expand Down Expand Up @@ -121,6 +122,21 @@ void PrivateMemoryResolution::getAnalysisUsage(llvm::AnalysisUsage& AU) const
AU.addRequired<ModuleAllocaAnalysis>();
}

void PrivateMemoryResolution::expandPrivateMemoryForVla(uint32_t &maxPrivateMem)
{
// Add another 4KB if there are VLAs
maxPrivateMem += 4096;
std::string maxPrivateMemValue = std::to_string(maxPrivateMem);
std::string fullWarningMessage = "VLA has been detected, the private memory size is set to " + maxPrivateMemValue + "B. "
"You can change the size by setting flag ForcePerThreadPrivateMemorySize to a value from [1024:20480]. "
"Greater values can affect performance, and lower ones may lead to incorrect results of your program.\n"
"To make sure your program runs correctly you can set flag StackOverflowDetection to 1. "
"This flag will print \"Stack overflow detected!\" if insufficient memory value has lead to stack overflow. "
"It should be used for debugging only as it affects performance.";

getAnalysis<CodeGenContextWrapper>().getCodeGenContext()->EmitWarning(fullWarningMessage.c_str());
}

bool PrivateMemoryResolution::runOnModule(llvm::Module& M)
{
// Get the analysis
Expand Down Expand Up @@ -193,13 +209,8 @@ bool PrivateMemoryResolution::runOnModule(llvm::Module& M)
if (!FGA && hasVLA)
{
uint32_t maxPrivateMem = funcMD->second.privateMemoryPerWI;
// Add another 4KB if there are VLAs
maxPrivateMem += 4096;
std::string maxPrivateMemValue = std::to_string(maxPrivateMem);
std::string fullWarningMessage = "VLA has been detected, the private memory size is set to " + maxPrivateMemValue + "B. You can change the size by setting flag ForcePerThreadPrivateMemorySize to a value from [1024:20480]. Greater values can affect performance, and lower ones may lead to incorrect results of your program";
expandPrivateMemoryForVla(maxPrivateMem);

// Now, you can pass the concatenated warning message (const char*) to the EmitWarning function
getAnalysis<CodeGenContextWrapper>().getCodeGenContext()->EmitWarning(fullWarningMessage.c_str());
maxPrivateMem = std::max(maxPrivateMem, Ctx.getPrivateMemoryMinimalSizePerThread());
maxPrivateMem = std::max(maxPrivateMem, (uint32_t)(IGC_GET_FLAG_VALUE(ForcePerThreadPrivateMemorySize)));
modMD.PrivateMemoryPerFG[m_currFunction] = maxPrivateMem;
Expand Down Expand Up @@ -316,13 +327,7 @@ bool PrivateMemoryResolution::runOnModule(llvm::Module& M)
}
if (FG->hasVariableLengthAlloca())
{
// Add another 4KB if there are VLAs
maxPrivateMem += 4096;
std::string maxPrivateMemValue = std::to_string(maxPrivateMem);
std::string fullWarningMessage = "VLA has been detected, the private memory size is set to " + maxPrivateMemValue + "B. You can change the size by setting flag ForcePerThreadPrivateMemorySize to a value from [1024:20480]. Greater values can affect performance, and lower ones may lead to incorrect results of your program";

// Now, you can pass the concatenated warning message (const char*) to the EmitWarning function
getAnalysis<CodeGenContextWrapper>().getCodeGenContext()->EmitWarning(fullWarningMessage.c_str());
expandPrivateMemoryForVla(maxPrivateMem);
}
maxPrivateMem = std::max(maxPrivateMem, Ctx.getPrivateMemoryMinimalSizePerThread());
maxPrivateMem = std::max(maxPrivateMem, (uint32_t)(IGC_GET_FLAG_VALUE(ForcePerThreadPrivateMemorySize)));
Expand Down

0 comments on commit 27557fb

Please sign in to comment.