Skip to content

Commit 4d796c7

Browse files
committed
[NFC] [hwasan] factor out selective instrumentation logic
sanitizeFunction is long enough already. Pull Request: llvm#84408
1 parent 9d3bf9b commit 4d796c7

File tree

1 file changed

+31
-22
lines changed

1 file changed

+31
-22
lines changed

llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,8 @@ class HWAddressSanitizer {
318318
};
319319
void setSSI(const StackSafetyGlobalInfo *S) { SSI = S; }
320320

321+
bool selectiveInstrumentationShouldSkip(Function &F,
322+
FunctionAnalysisManager &FAM);
321323
void initializeModule();
322324
void createHwasanCtorComdat();
323325

@@ -1524,6 +1526,31 @@ bool HWAddressSanitizer::instrumentStack(memtag::StackInfo &SInfo,
15241526
return true;
15251527
}
15261528

1529+
bool HWAddressSanitizer::selectiveInstrumentationShouldSkip(
1530+
Function &F, FunctionAnalysisManager &FAM) {
1531+
if (ClRandomSkipRate.getNumOccurrences()) {
1532+
std::bernoulli_distribution D(ClRandomSkipRate);
1533+
if (D(*Rng))
1534+
return true;
1535+
} else {
1536+
auto &MAMProxy = FAM.getResult<ModuleAnalysisManagerFunctionProxy>(F);
1537+
ProfileSummaryInfo *PSI =
1538+
MAMProxy.getCachedResult<ProfileSummaryAnalysis>(*F.getParent());
1539+
if (PSI && PSI->hasProfileSummary()) {
1540+
auto &BFI = FAM.getResult<BlockFrequencyAnalysis>(F);
1541+
if ((ClHotPercentileCutoff.getNumOccurrences() &&
1542+
ClHotPercentileCutoff >= 0)
1543+
? PSI->isFunctionHotInCallGraphNthPercentile(
1544+
ClHotPercentileCutoff, &F, BFI)
1545+
: PSI->isFunctionHotInCallGraph(&F, BFI))
1546+
return true;
1547+
} else {
1548+
++NumNoProfileSummaryFuncs;
1549+
}
1550+
}
1551+
return false;
1552+
}
1553+
15271554
void HWAddressSanitizer::sanitizeFunction(Function &F,
15281555
FunctionAnalysisManager &FAM) {
15291556
if (&F == HwasanCtorFunction)
@@ -1536,28 +1563,10 @@ void HWAddressSanitizer::sanitizeFunction(Function &F,
15361563
return;
15371564

15381565
NumTotalFuncs++;
1539-
if (CSelectiveInstrumentation) {
1540-
if (ClRandomSkipRate.getNumOccurrences()) {
1541-
std::bernoulli_distribution D(ClRandomSkipRate);
1542-
if (D(*Rng))
1543-
return;
1544-
} else {
1545-
auto &MAMProxy = FAM.getResult<ModuleAnalysisManagerFunctionProxy>(F);
1546-
ProfileSummaryInfo *PSI =
1547-
MAMProxy.getCachedResult<ProfileSummaryAnalysis>(*F.getParent());
1548-
if (PSI && PSI->hasProfileSummary()) {
1549-
auto &BFI = FAM.getResult<BlockFrequencyAnalysis>(F);
1550-
if ((ClHotPercentileCutoff.getNumOccurrences() &&
1551-
ClHotPercentileCutoff >= 0)
1552-
? PSI->isFunctionHotInCallGraphNthPercentile(
1553-
ClHotPercentileCutoff, &F, BFI)
1554-
: PSI->isFunctionHotInCallGraph(&F, BFI))
1555-
return;
1556-
} else {
1557-
++NumNoProfileSummaryFuncs;
1558-
}
1559-
}
1560-
}
1566+
1567+
if (CSelectiveInstrumentation && selectiveInstrumentationShouldSkip(F, FAM))
1568+
return;
1569+
15611570
NumInstrumentedFuncs++;
15621571

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

0 commit comments

Comments
 (0)