[llvm] [PGO] Gracefully handle zero entry-count in `fixFuncEntryCount` (PR #112029)

Michael O'Farrell via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 11 10:56:47 PDT 2024


https://github.com/mofarrell created https://github.com/llvm/llvm-project/pull/112029

With sampled instrumentation (#69535), profile counts may appear corrupt and `fixFuncEntryCount` may assert.  In particular a function can have a 0 block count for its entry, while later blocks are non zero.  This is only likely to happen for colder functions, so it is reasonable to take any action that does not crash.  Here we simply bail from fixing the entry count.

>From d4695485d892c190ac77468238de5e900e47bcaf Mon Sep 17 00:00:00 2001
From: Michael O'Farrell <micpof at gmail.com>
Date: Tue, 8 Oct 2024 10:41:11 -0700
Subject: [PATCH] [PGO] Gracefully handle zero entry-count

With sampled instrumentation (#69535), profile counts can appear
corrupt.  In particular a function can have a 0 block count for its
entry, while later blocks are non zero.  This is only likely to happen
for colder functions, so it is reasonable to take any action that does
not crash.
---
 llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)

diff --git a/llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp b/llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp
index e6e474ed376069..23d34016173165 100644
--- a/llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp
+++ b/llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp
@@ -1970,11 +1970,9 @@ static void fixFuncEntryCount(PGOUseFunc &Func, LoopInfo &LI,
                               BranchProbabilityInfo &NBPI) {
   Function &F = Func.getFunc();
   BlockFrequencyInfo NBFI(F, NBPI, LI);
-#ifndef NDEBUG
   auto BFIEntryCount = F.getEntryCount();
-  assert(BFIEntryCount && (BFIEntryCount->getCount() > 0) &&
-         "Invalid BFI Entrycount");
-#endif
+  if (!BFIEntryCount || BFIEntryCount->getCount() == 0)
+    return;
   auto SumCount = APFloat::getZero(APFloat::IEEEdouble());
   auto SumBFICount = APFloat::getZero(APFloat::IEEEdouble());
   for (auto &BBI : F) {



More information about the llvm-commits mailing list