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

Merged
merged 2 commits into from
Oct 22, 2024

Conversation

mofarrell
Copy link
Contributor

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
Member

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
Member

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) {

@dcci dcci requested review from mtrofin, WenleiHe and MatzeB October 14, 2024 22:00
@MatzeB MatzeB requested a review from xur-llvm October 15, 2024 21:31
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
Contributor Author

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

@mofarrell mofarrell requested review from WenleiHe and MatzeB October 17, 2024 18:00
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.
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.

@@ -0,0 +1,28 @@
; RUN: llvm-profdata merge %S/Inputs/fix_entry_count_sampled.proftext -o %t.profdata
Copy link
Contributor

Choose a reason for hiding this comment

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

Since this input .proftext file is only used for this test, can we use split-file and inline it here?

https://llvm.org/docs/TestingGuide.html#extra-files

Copy link
Contributor Author

Choose a reason for hiding this comment

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

These testing oddities were mimicking the approach taken in other nearby tests covering the same functionality. In particular fix_entry_count.ll was used as a base.

In llvm/test/Transforms/PGOProfile/ no other test makes use of split-file for proftext data, does this make it reasonable to follow this localized convention despite the proftext only being used by one test?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think split-file was introduced relatively recently and fix_entry_count.ll was last updated 2 years ago. More recent proftext tests do use split-file.

; RUN: rm -rf %t && split-file %s %t
; RUN: llvm-profdata merge %t/a.proftext -o %t/a.profdata
; RUN: opt < %t/a.ll --passes=pgo-instr-use -pgo-test-profile-file=%t/a.profdata
;--- a.ll

I like to use split-file because otherwise we have lots of similar .proftext files in Inputs/ and it is slightly inconvenient to find their uses. It also means only one file needs to be updated if we want to change the test.

@@ -0,0 +1,28 @@
; RUN: llvm-profdata merge %S/Inputs/fix_entry_count_sampled.proftext -o %t.profdata
; RUN: opt < %s -passes=pgo-instr-use -pgo-test-profile-file=%t.profdata -S | FileCheck %s --check-prefix=USE
Copy link
Contributor

Choose a reason for hiding this comment

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

Since this is the only FileCheck command, can we use the default check prefix?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Certainly. This followed the neighboring tests fix_entry_count.ll's style, which is testing similar functionality.

This crashed before making fixFuncEntryCount gracefully handle zero
counts.
Copy link
Contributor

@ellishg ellishg left a comment

Choose a reason for hiding this comment

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

Thanks! LGTM

@rafaelauler rafaelauler merged commit 10f0c1a into llvm:main Oct 22, 2024
7 checks passed
Copy link

@mofarrell Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

@mofarrell mofarrell deleted the zero-prof-counts branch October 29, 2024 23:44
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.

6 participants