Skip to content

Commit 320a89e

Browse files
committed
[PGO] Gracefully handle zero entry-count
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.
1 parent 58bf78c commit 320a89e

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1615,6 +1615,10 @@ void PGOUseFunc::populateCounters() {
16151615
assert(BI->Count && "BB count is not valid");
16161616
}
16171617
#endif
1618+
// Now annotate select instructions. This may fixup impossible block counts.
1619+
FuncInfo.SIVisitor.annotateSelects(this, &CountPosition);
1620+
assert(CountPosition == ProfileCountSize);
1621+
16181622
uint64_t FuncEntryCount = *getBBInfo(&*F.begin()).Count;
16191623
uint64_t FuncMaxCount = FuncEntryCount;
16201624
for (auto &BB : F) {
@@ -1630,10 +1634,6 @@ void PGOUseFunc::populateCounters() {
16301634
F.setEntryCount(ProfileCount(FuncEntryCount, Function::PCT_Real));
16311635
markFunctionAttributes(FuncEntryCount, FuncMaxCount);
16321636

1633-
// Now annotate select instructions
1634-
FuncInfo.SIVisitor.annotateSelects(this, &CountPosition);
1635-
assert(CountPosition == ProfileCountSize);
1636-
16371637
LLVM_DEBUG(FuncInfo.dumpInfo("after reading profile."));
16381638
}
16391639

@@ -1742,8 +1742,13 @@ void SelectInstVisitor::annotateOneSelectInst(SelectInst &SI) {
17421742
++(*CurCtrIdx);
17431743
uint64_t TotalCount = 0;
17441744
auto BI = UseFunc->findBBInfo(SI.getParent());
1745-
if (BI != nullptr)
1745+
if (BI != nullptr) {
17461746
TotalCount = *BI->Count;
1747+
1748+
// Fix the block count if it is impossible.
1749+
if (TotalCount < SCounts[0])
1750+
BI->Count = SCounts[0];
1751+
}
17471752
// False Count
17481753
SCounts[1] = (TotalCount > SCounts[0] ? TotalCount - SCounts[0] : 0);
17491754
uint64_t MaxCount = std::max(SCounts[0], SCounts[1]);

0 commit comments

Comments
 (0)