Skip to content

Commit

Permalink
[PGO] Gracefully handle zero entry-count
Browse files Browse the repository at this point in the history
With sampled instrumentation (llvm#69535), profile counts can appear
corrupt.  In particular a function can have a 0 block counts for all its
blocks, while having some non-zero counters for select instrumentation.
This is only possible for colder functions, and a reasonable
modification to ensure the entry is non-zero (required by
`fixFuncEntryCounts`) is to set the counter to one.  This is only likely
to happen for colder functions, so it is reasonable to take any action
that does not crash.
  • Loading branch information
mofarrell committed Oct 18, 2024
1 parent 58bf78c commit 320a89e
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1615,6 +1615,10 @@ void PGOUseFunc::populateCounters() {
assert(BI->Count && "BB count is not valid");
}
#endif
// Now annotate select instructions. This may fixup impossible block counts.
FuncInfo.SIVisitor.annotateSelects(this, &CountPosition);
assert(CountPosition == ProfileCountSize);

uint64_t FuncEntryCount = *getBBInfo(&*F.begin()).Count;
uint64_t FuncMaxCount = FuncEntryCount;
for (auto &BB : F) {
Expand All @@ -1630,10 +1634,6 @@ void PGOUseFunc::populateCounters() {
F.setEntryCount(ProfileCount(FuncEntryCount, Function::PCT_Real));
markFunctionAttributes(FuncEntryCount, FuncMaxCount);

// Now annotate select instructions
FuncInfo.SIVisitor.annotateSelects(this, &CountPosition);
assert(CountPosition == ProfileCountSize);

LLVM_DEBUG(FuncInfo.dumpInfo("after reading profile."));
}

Expand Down Expand Up @@ -1742,8 +1742,13 @@ void SelectInstVisitor::annotateOneSelectInst(SelectInst &SI) {
++(*CurCtrIdx);
uint64_t TotalCount = 0;
auto BI = UseFunc->findBBInfo(SI.getParent());
if (BI != nullptr)
if (BI != nullptr) {
TotalCount = *BI->Count;

// Fix the block count if it is impossible.
if (TotalCount < SCounts[0])
BI->Count = SCounts[0];
}
// False Count
SCounts[1] = (TotalCount > SCounts[0] ? TotalCount - SCounts[0] : 0);
uint64_t MaxCount = std::max(SCounts[0], SCounts[1]);
Expand Down

0 comments on commit 320a89e

Please sign in to comment.