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 17, 2024
1 parent 58bf78c commit 83a55a2
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1624,8 +1624,12 @@ void PGOUseFunc::populateCounters() {
FuncMaxCount = std::max(FuncMaxCount, *BI->Count);
}

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

0 comments on commit 83a55a2

Please sign in to comment.