Skip to content

[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

Conversation

ilovepi
Copy link
Contributor

@ilovepi ilovepi commented Mar 26, 2024

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.

ilovepi added 2 commits March 26, 2024 00:49
Created using spr 1.3.4
@llvmbot
Copy link
Member

llvmbot commented Mar 26, 2024

@llvm/pr-subscribers-llvm-transforms

Author: Paul Kirth (ilovepi)

Changes

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.


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

1 Files Affected:

  • (modified) llvm/lib/Transforms/Utils/MisExpect.cpp (+10-9)
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;

ilovepi added 2 commits March 26, 2024 00:56
Created using spr 1.3.4

[skip ci]
Created using spr 1.3.4
ilovepi added a commit to ilovepi/llvm-project that referenced this pull request Mar 26, 2024
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
ilovepi and others added 18 commits April 25, 2024 23:10
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
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
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
@ilovepi ilovepi changed the base branch from users/ilovepi/spr/main.llvmmisexpect-update-misexpect-to-use-provenance-tracking-metadata to main June 24, 2024 16:01
@ilovepi
Copy link
Contributor Author

ilovepi commented Jun 24, 2024

Ping. Now that the metadata patches have landed, I think we can start discussing using that in MisExpect.

@ilovepi
Copy link
Contributor Author

ilovepi commented Jul 8, 2024

ping

if ((TotalBranchWeight == 0) || (TotalBranchWeight <= LikelyBranchWeight))
return;
// Failing this assert means that we have corrupted metadata.
assert((TotalBranchWeight >= LikelyBranchWeight) && (TotalBranchWeight > 0) &&
Copy link
Contributor

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?

Copy link
Contributor Author

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?

Copy link
Contributor

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.

Copy link
Contributor Author

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.

@ilovepi ilovepi merged commit a8cabb6 into main Jul 10, 2024
8 checks passed
@ilovepi ilovepi deleted the users/ilovepi/spr/llvmmisexpect-update-misexpect-to-use-provenance-tracking-metadata branch July 10, 2024 19:22
@llvm-ci
Copy link
Collaborator

llvm-ci commented Jul 10, 2024

LLVM Buildbot has detected a new failure on builder arc-builder running on arc-worker while building llvm at step 5 "build-unified-tree".

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:

Step 5 (build-unified-tree) failure: build (failure)
...
27.558 [5/15/99] Linking CXX shared library lib/libclang.so.19.0.0git
27.565 [4/15/100] Creating library symlink lib/libclang.so.19.0git lib/libclang.so
27.646 [2/16/101] Linking CXX executable bin/c-arcmt-test
27.944 [2/15/102] Linking CXX executable bin/llvm-dwp
28.269 [2/14/103] Linking CXX executable bin/clang-scan-deps
28.333 [2/13/104] Linking CXX executable bin/llvm-gsymutil
28.379 [2/12/105] Linking CXX executable bin/llvm-isel-fuzzer
28.496 [2/11/106] Linking CXX executable bin/clang-check
28.644 [2/10/107] Linking CXX executable bin/llvm-exegesis
29.519 [2/9/108] Linking CXX executable bin/llvm-split
command timed out: 1200 seconds without output running [b'ninja', b'-j', b'16'], attempting to kill
process killed by signal 9
program finished with exit code -1
elapsedTime=1429.349399

aaryanshukla pushed a commit to aaryanshukla/llvm-project that referenced this pull request Jul 14, 2024
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants