-
Notifications
You must be signed in to change notification settings - Fork 14.4k
[llvm][misexpect] Update MisExpect to use provenance tracking metadata #86610
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
[llvm][misexpect] Update MisExpect to use provenance tracking metadata #86610
Conversation
Created using spr 1.3.4 [skip ci]
Created using spr 1.3.4
@llvm/pr-subscribers-llvm-transforms Author: Paul Kirth (ilovepi) ChangesWith the IR extension added to MD_prof branch weights, we can now easily Full diff: https://github.com/llvm/llvm-project/pull/86610.diff 1 Files Affected:
diff --git a/llvm/lib/Transforms/Utils/MisExpect.cpp b/llvm/lib/Transforms/Utils/MisExpect.cpp
index 759289384ee06d..6d1c26b6cedcb5 100644
--- a/llvm/lib/Transforms/Utils/MisExpect.cpp
+++ b/llvm/lib/Transforms/Utils/MisExpect.cpp
@@ -151,15 +151,9 @@ void verifyMisExpect(Instruction &I, ArrayRef<uint32_t> RealWeights,
uint64_t TotalBranchWeight =
LikelyBranchWeight + (UnlikelyBranchWeight * NumUnlikelyTargets);
- // FIXME: When we've addressed sample profiling, restore the assertion
- //
- // We cannot calculate branch probability if either of these invariants aren't
- // met. However, MisExpect diagnostics should not prevent code from compiling,
- // so we simply forgo emitting diagnostics here, and return early.
- // assert((TotalBranchWeight >= LikelyBranchWeight) && (TotalBranchWeight > 0)
- // && "TotalBranchWeight is less than the Likely branch weight");
- if ((TotalBranchWeight == 0) || (TotalBranchWeight <= LikelyBranchWeight))
- return;
+ // Failing this assert means that we have corrupted metadata.
+ assert((TotalBranchWeight >= LikelyBranchWeight) && (TotalBranchWeight > 0)
+ && "TotalBranchWeight is less than the Likely branch weight");
// To determine our threshold value we need to obtain the branch probability
// for the weights added by llvm.expect and use that proportion to calculate
@@ -186,6 +180,13 @@ void verifyMisExpect(Instruction &I, ArrayRef<uint32_t> RealWeights,
void checkBackendInstrumentation(Instruction &I,
const ArrayRef<uint32_t> RealWeights) {
+ // Backend checking assumes any existing weight comes from an `llvm.expect`
+ // intrinsic. However, SampleProfiling + ThinLTO add branch weights multiple
+ // times, leading to an invalid assumption in our checking. Backend checks
+ // should only operate on branch weights that carry the "!expected" field,
+ // since they are guaranteed to be added by the LowerExpectIntrinsic pass.
+ if(!hasExpectedProvenance(I))
+ return;
SmallVector<uint32_t> ExpectedWeights;
if (!extractBranchWeights(I, ExpectedWeights))
return;
|
Created using spr 1.3.4 [skip ci]
Created using spr 1.3.4
With the IR extension added to MD_prof branch weights, we can now easily destinguish between weights added by `llvm.expect*` intrinsics and weights from other sources. This patch re-enables the assert checking for malformed metadata, which should never happen using the `llvm.expect*` family of intrinsics. Pull Request: llvm#86610
Created using spr 1.3.4 [skip ci]
Created using spr 1.3.4
Created using spr 1.3.4 [skip ci]
Created using spr 1.3.4 [skip ci]
Created using spr 1.3.4 [skip ci]
Created using spr 1.3.4
Created using spr 1.3.4 [skip ci]
Created using spr 1.3.4
Created using spr 1.3.4 [skip ci]
Created using spr 1.3.4 [skip ci]
Created using spr 1.3.4 [skip ci]
Created using spr 1.3.4 [skip ci]
Ping. Now that the metadata patches have landed, I think we can start discussing using that in MisExpect. |
ping |
if ((TotalBranchWeight == 0) || (TotalBranchWeight <= LikelyBranchWeight)) | ||
return; | ||
// Failing this assert means that we have corrupted metadata. | ||
assert((TotalBranchWeight >= LikelyBranchWeight) && (TotalBranchWeight > 0) && |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should it be an assert or disagnostic?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IIRC many of the places where we check metadata for corruption or format incompatibility just use asserts, but I'm fine if its a diagnostic. Let me know if you have a preference, as I'm fine either way.
That said, if we're going to make this a diagnostic, we should probably move it out of MisExpect and put it on some other code path where we're examining branch weights, since MisExpect is one of the least used compilation modes. WDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we have examples of this assert firing? If it is something user do not have control with, leave it as an assert is fine. If not, exposing it as diagnostic is better.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it was introduced after we found an issue w/ thinLTO, since sample profiling can run multiple times. We then introduced an early return to avoid division by zero, but now it should be safe to just use the assert, since we should only ever process branches w/ the provenance tag.
for now, I guess let's leave it as an assert, since I don't expect it to fire, and users don't have control of it.
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/3/builds/1343 Here is the relevant piece of the build log for the reference:
|
llvm#86610) With the IR extension added to MD_prof branch weights, we can now easily distinguish between weights added by `llvm.expect*` intrinsics and weights from other sources. This patch re-enables the assert checking for malformed metadata, which should never happen using the `llvm.expect*` family of intrinsics.
With the IR extension added to MD_prof branch weights, we can now easily
destinguish between weights added by
llvm.expect*
intrinsics andweights from other sources. This patch re-enables the assert checking
for malformed metadata, which should never happen using the
llvm.expect*
family of intrinsics.