Skip to content

Commit f76c132

Browse files
authored
[SimplifyCFG] Fix weight calculation for simplifySwitchOfPowersOfTwo (#165956)
Continued from #165804 This maintains the BFI of the default branch. Originally `10/63`​, post-pass, it ends up being `5/63 + 58/63 * 5/58`​(first term is from `PROF`​, second is the probability of going to the switch lookup times the probability, there, of taking the default branch) Issue #147390
1 parent b0ae054 commit f76c132

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7731,19 +7731,24 @@ static bool simplifySwitchOfPowersOfTwo(SwitchInst *SI, IRBuilder<> &Builder,
77317731
// label. The other is those powers of 2 that don't appear in the case
77327732
// statement. We don't know the distribution of the values coming in, so
77337733
// the safest is to split 50-50 the original probability to `default`.
7734-
uint64_t OrigDenominator = sum_of(map_range(
7735-
Weights, [](const auto &V) { return static_cast<uint64_t>(V); }));
7734+
uint64_t OrigDenominator =
7735+
sum_of(map_range(Weights, StaticCastTo<uint64_t>));
77367736
SmallVector<uint64_t> NewWeights(2);
77377737
NewWeights[1] = Weights[0] / 2;
77387738
NewWeights[0] = OrigDenominator - NewWeights[1];
77397739
setFittedBranchWeights(*BI, NewWeights, /*IsExpected=*/false);
7740-
7741-
// For the original switch, we reduce the weight of the default by the
7742-
// amount by which the previous branch contributes to getting to default,
7743-
// and then make sure the remaining weights have the same relative ratio
7744-
// wrt eachother.
7740+
// The probability of executing the default block stays constant. It was
7741+
// p_d = Weights[0] / OrigDenominator
7742+
// we rewrite as W/D
7743+
// We want to find the probability of the default branch of the switch
7744+
// statement. Let's call it X. We have W/D = W/2D + X * (1-W/2D)
7745+
// i.e. the original probability is the probability we go to the default
7746+
// branch from the BI branch, or we take the default branch on the SI.
7747+
// Meaning X = W / (2D - W), or (W/2) / (D - W/2)
7748+
// This matches using W/2 for the default branch probability numerator and
7749+
// D-W/2 as the denominator.
7750+
Weights[0] = NewWeights[1];
77457751
uint64_t CasesDenominator = OrigDenominator - Weights[0];
7746-
Weights[0] /= 2;
77477752
for (auto &W : drop_begin(Weights))
77487753
W = NewWeights[0] * static_cast<double>(W) / CasesDenominator;
77497754

llvm/test/Transforms/SimplifyCFG/X86/switch-of-powers-of-two.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,5 +141,5 @@ return:
141141
;.
142142
; CHECK: [[PROF0]] = !{!"function_entry_count", i32 10}
143143
; CHECK: [[PROF1]] = !{!"branch_weights", i32 58, i32 5}
144-
; CHECK: [[PROF2]] = !{!"branch_weights", i32 56, i32 5}
144+
; CHECK: [[PROF2]] = !{!"branch_weights", i32 53, i32 5}
145145
;.

0 commit comments

Comments
 (0)