Skip to content

Commit 83a55a2

Browse files
committed
[PGO] Gracefully handle zero entry-count
With sampled instrumentation (#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 83a55a2

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1624,8 +1624,12 @@ void PGOUseFunc::populateCounters() {
16241624
FuncMaxCount = std::max(FuncMaxCount, *BI->Count);
16251625
}
16261626

1627-
// Fix the obviously inconsistent entry count.
1628-
if (FuncMaxCount > 0 && FuncEntryCount == 0)
1627+
// Fix the obviously inconsistent entry count. A function that has all zero
1628+
// counters will skip populating the counters so a minimum entry count of 1
1629+
// makes sense. Note that the presence of a non-zero counter does not imply
1630+
// FuncMaxCount is greater than zero because the only non-zero counts could
1631+
// be for select instrumentation which is handled later.
1632+
if (FuncEntryCount == 0)
16291633
FuncEntryCount = 1;
16301634
F.setEntryCount(ProfileCount(FuncEntryCount, Function::PCT_Real));
16311635
markFunctionAttributes(FuncEntryCount, FuncMaxCount);

0 commit comments

Comments
 (0)