Skip to content

Commit

Permalink
[NFC] [hwasan] factor out selective instrumentation logic (#84408)
Browse files Browse the repository at this point in the history
sanitizeFunction is long enough already.
  • Loading branch information
fmayer authored Mar 12, 2024
1 parent 67ef4ae commit 672fc89
Showing 1 changed file with 31 additions and 22 deletions.
53 changes: 31 additions & 22 deletions llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,8 @@ class HWAddressSanitizer {
Value *MemTag = nullptr;
};

bool selectiveInstrumentationShouldSkip(Function &F,
FunctionAnalysisManager &FAM);
void initializeModule();
void createHwasanCtorComdat();

Expand Down Expand Up @@ -1523,6 +1525,31 @@ bool HWAddressSanitizer::instrumentStack(memtag::StackInfo &SInfo,
return true;
}

bool HWAddressSanitizer::selectiveInstrumentationShouldSkip(
Function &F, FunctionAnalysisManager &FAM) {
if (ClRandomSkipRate.getNumOccurrences()) {
std::bernoulli_distribution D(ClRandomSkipRate);
if (D(*Rng))
return true;
} else {
auto &MAMProxy = FAM.getResult<ModuleAnalysisManagerFunctionProxy>(F);
ProfileSummaryInfo *PSI =
MAMProxy.getCachedResult<ProfileSummaryAnalysis>(*F.getParent());
if (PSI && PSI->hasProfileSummary()) {
auto &BFI = FAM.getResult<BlockFrequencyAnalysis>(F);
if ((ClHotPercentileCutoff.getNumOccurrences() &&
ClHotPercentileCutoff >= 0)
? PSI->isFunctionHotInCallGraphNthPercentile(
ClHotPercentileCutoff, &F, BFI)
: PSI->isFunctionHotInCallGraph(&F, BFI))
return true;
} else {
++NumNoProfileSummaryFuncs;
}
}
return false;
}

void HWAddressSanitizer::sanitizeFunction(Function &F,
FunctionAnalysisManager &FAM) {
if (&F == HwasanCtorFunction)
Expand All @@ -1535,28 +1562,10 @@ void HWAddressSanitizer::sanitizeFunction(Function &F,
return;

NumTotalFuncs++;
if (CSelectiveInstrumentation) {
if (ClRandomSkipRate.getNumOccurrences()) {
std::bernoulli_distribution D(ClRandomSkipRate);
if (D(*Rng))
return;
} else {
auto &MAMProxy = FAM.getResult<ModuleAnalysisManagerFunctionProxy>(F);
ProfileSummaryInfo *PSI =
MAMProxy.getCachedResult<ProfileSummaryAnalysis>(*F.getParent());
if (PSI && PSI->hasProfileSummary()) {
auto &BFI = FAM.getResult<BlockFrequencyAnalysis>(F);
if ((ClHotPercentileCutoff.getNumOccurrences() &&
ClHotPercentileCutoff >= 0)
? PSI->isFunctionHotInCallGraphNthPercentile(
ClHotPercentileCutoff, &F, BFI)
: PSI->isFunctionHotInCallGraph(&F, BFI))
return;
} else {
++NumNoProfileSummaryFuncs;
}
}
}

if (CSelectiveInstrumentation && selectiveInstrumentationShouldSkip(F, FAM))
return;

NumInstrumentedFuncs++;

LLVM_DEBUG(dbgs() << "Function: " << F.getName() << "\n");
Expand Down

0 comments on commit 672fc89

Please sign in to comment.