Skip to content

Commit 896037c

Browse files
authored
[LoopRotate] Set loop back edge weight to not less than exit weight (#86496)
Branch weight from sample-based PGO may be not inaccurate due to sampling. If the loop body must be executed, then origin loop back edge weight must be not less than exit weight.
1 parent daa755b commit 896037c

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

llvm/lib/Transforms/Utils/LoopRotationUtils.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,9 +347,19 @@ static void updateBranchWeights(BranchInst &PreHeaderBI, BranchInst &LoopBI,
347347
// probabilities as if there are only 0-trip and 1-trip cases.
348348
ExitWeight0 = OrigLoopExitWeight - OrigLoopBackedgeWeight;
349349
}
350+
} else {
351+
// Theoretically, if the loop body must be executed at least once, the
352+
// backedge count must be not less than exit count. However the branch
353+
// weight collected by sampling-based PGO may be not very accurate due to
354+
// sampling. Therefore this workaround is required here to avoid underflow
355+
// of unsigned in following update of branch weight.
356+
if (OrigLoopExitWeight > OrigLoopBackedgeWeight)
357+
OrigLoopBackedgeWeight = OrigLoopExitWeight;
350358
}
359+
assert(OrigLoopExitWeight >= ExitWeight0 && "Bad branch weight");
351360
ExitWeight1 = OrigLoopExitWeight - ExitWeight0;
352361
EnterWeight = ExitWeight1;
362+
assert(OrigLoopBackedgeWeight >= EnterWeight && "Bad branch weight");
353363
LoopBackWeight = OrigLoopBackedgeWeight - EnterWeight;
354364
} else if (OrigLoopExitWeight == 0) {
355365
if (OrigLoopBackedgeWeight == 0) {

llvm/test/Transforms/LoopRotate/update-branch-weights.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ loop_exit:
240240

241241
; BFI_AFTER-LABEL: block-frequency-info: func6_inaccurate_branch_weight
242242
; BFI_AFTER: - entry: {{.*}} count = 1024
243-
; BFI_AFTER: - loop_body: {{.*}} count = 4294967296
243+
; BFI_AFTER: - loop_body: {{.*}} count = 1024
244244
; BFI_AFTER: - loop_exit: {{.*}} count = 1024
245245

246246
; IR-LABEL: define void @func6_inaccurate_branch_weight(
@@ -292,4 +292,4 @@ loop_exit:
292292
; IR: [[PROF_FUNC3_0]] = !{!"branch_weights", i32 0, i32 1}
293293
; IR: [[PROF_FUNC4_0]] = !{!"branch_weights", i32 1, i32 0}
294294
; IR: [[PROF_FUNC5_0]] = !{!"branch_weights", i32 0, i32 0}
295-
; IR: [[PROF_FUNC6_0]] = !{!"branch_weights", i32 -1, i32 1024}
295+
; IR: [[PROF_FUNC6_0]] = !{!"branch_weights", i32 0, i32 1024}

0 commit comments

Comments
 (0)