[llvm] Reapply "[SimplifyCFG] Fix profcheck failure from #213302" (#220932) (PR #220939)
Yingwei Zheng via llvm-commits
llvm-commits at lists.llvm.org
Sun Sep 6 05:34:41 PDT 2026
================
@@ -5117,6 +5115,56 @@ bool SimplifyCFGOpt::simplifySwitchOnSelectRemap(SwitchInst *SI,
if (DTU && !OldDestStillTargeted)
DTU->applyUpdates({{DominatorTree::Delete, BB, OldDest}});
}
+
+ // Update the profile information on the switch if we had a profile
+ // for both it and the select instruction.
+ SmallVector<uint32_t> SwitchWeights;
+ bool SwitchHasBranchWeights = extractBranchWeights(*SI, SwitchWeights);
+ uint64_t SelectTrueWeight;
+ uint64_t SelectFalseWeight;
+ bool SelectHasBranchWeights =
+ extractBranchWeights(*Select, SelectTrueWeight, SelectFalseWeight);
+ if (Negate)
+ std::swap(SelectTrueWeight, SelectFalseWeight);
+ if (SwitchHasBranchWeights && SelectHasBranchWeights &&
+ !ProfcheckDisableMetadataFixes) {
+ uint64_t SwitchTotalWeight = sum_of(SwitchWeights, uint64_t{0});
+ SmallVector<uint64_t> NewSwitchWeights;
+ if (IsDefault) {
+ // Update the branch weights of the switch by multiplying all of them by
+ // the weight of the false branch of the select. Then add the new switch
+ // case at the end by multiplying the weight of the true branch of the
+ // select by the total weight of the switch.
+ NewSwitchWeights.reserve(SwitchWeights.size() + 1);
+ for (uint32_t SwitchWeight : SwitchWeights)
+ NewSwitchWeights.push_back(SwitchWeight * SelectFalseWeight);
----------------
dtcxzyw wrote:
This overestimates the branch weight for `x = K` because the original weight for `sel = K` also includes the contribution from `x = C`. We can correct this if we have an existing case for `sel = K`. But we cannot get the precise weight when `sel = K` falls into the default case.
Maybe it is not worth the complexity. But we can document the approximation here
https://github.com/llvm/llvm-project/pull/220939
More information about the llvm-commits
mailing list