Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PGO] Ensure non-zero entry-count after populateCounters #112029

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

mofarrell
Copy link

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.

Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added PGO Profile Guided Optimizations llvm:transforms labels Oct 11, 2024
@llvmbot
Copy link
Collaborator

llvmbot commented Oct 11, 2024

@llvm/pr-subscribers-llvm-transforms

Author: Michael O'Farrell (mofarrell)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/112029.diff

1 Files Affected:

  • (modified) llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp (+2-4)
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) {

@llvmbot
Copy link
Collaborator

llvmbot commented Oct 11, 2024

@llvm/pr-subscribers-pgo

Author: Michael O'Farrell (mofarrell)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/112029.diff

1 Files Affected:

  • (modified) llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp (+2-4)
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) {

Copy link
Contributor

@MatzeB MatzeB left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Changes makes sense to me
  • Please provide a test if possible

Copy link
Member

@WenleiHe WenleiHe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Beyond the point fix here, I'm wondering if we want to consider profile inference to smooth the counts for sampled IRPGO just like HW sample PGO. @xur-llvm wdyt?

llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp Outdated Show resolved Hide resolved
@mofarrell mofarrell changed the title [PGO] Gracefully handle zero entry-count in fixFuncEntryCount [PGO] Ensure non-zero entry-count after populateCounters Oct 17, 2024
@mofarrell
Copy link
Author

Changed the fix to guarantee the function entry count is non zero after populateCounters.

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.
This crashed before making fixFuncEntryCount gracefully handle zero
counts.
Copy link
Member

@WenleiHe WenleiHe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
llvm:transforms PGO Profile Guided Optimizations
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants