Skip to content

Commit 1d781ce

Browse files
ilovepiaaryanshukla
authored andcommitted
[llvm][misexpect] Update MisExpect to use provenance tracking metadata (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.
1 parent 358122f commit 1d781ce

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

llvm/lib/Transforms/Utils/MisExpect.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -151,15 +151,9 @@ void verifyMisExpect(Instruction &I, ArrayRef<uint32_t> RealWeights,
151151
uint64_t TotalBranchWeight =
152152
LikelyBranchWeight + (UnlikelyBranchWeight * NumUnlikelyTargets);
153153

154-
// FIXME: When we've addressed sample profiling, restore the assertion
155-
//
156-
// We cannot calculate branch probability if either of these invariants aren't
157-
// met. However, MisExpect diagnostics should not prevent code from compiling,
158-
// so we simply forgo emitting diagnostics here, and return early.
159-
// assert((TotalBranchWeight >= LikelyBranchWeight) && (TotalBranchWeight > 0)
160-
// && "TotalBranchWeight is less than the Likely branch weight");
161-
if ((TotalBranchWeight == 0) || (TotalBranchWeight <= LikelyBranchWeight))
162-
return;
154+
// Failing this assert means that we have corrupted metadata.
155+
assert((TotalBranchWeight >= LikelyBranchWeight) && (TotalBranchWeight > 0) &&
156+
"TotalBranchWeight is less than the Likely branch weight");
163157

164158
// To determine our threshold value we need to obtain the branch probability
165159
// 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,
186180

187181
void checkBackendInstrumentation(Instruction &I,
188182
const ArrayRef<uint32_t> RealWeights) {
183+
// Backend checking assumes any existing weight comes from an `llvm.expect`
184+
// intrinsic. However, SampleProfiling + ThinLTO add branch weights multiple
185+
// times, leading to an invalid assumption in our checking. Backend checks
186+
// should only operate on branch weights that carry the "!expected" field,
187+
// since they are guaranteed to be added by the LowerExpectIntrinsic pass.
188+
if (!hasBranchWeightOrigin(I))
189+
return;
189190
SmallVector<uint32_t> ExpectedWeights;
190191
if (!extractBranchWeights(I, ExpectedWeights))
191192
return;

0 commit comments

Comments
 (0)