[llvm] Revert "[SimplifyCFG] Fix branch-weight overflow when folding switch case into default" (PR #212540)
via llvm-commits
llvm-commits at lists.llvm.org
Tue Jul 28 09:49:09 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-transforms
Author: Aiden Grossman (boomanaiden154)
<details>
<summary>Changes</summary>
Reverts llvm/llvm-project#<!-- -->212520
This broke the profcheck builder, which given this is a profile info fix probably should not be happening.
---
Full diff: https://github.com/llvm/llvm-project/pull/212540.diff
2 Files Affected:
- (modified) llvm/lib/Transforms/Utils/Local.cpp (+2-4)
- (removed) llvm/test/Transforms/SimplifyCFG/switch-default-fold-weight-overflow.ll (-45)
``````````diff
diff --git a/llvm/lib/Transforms/Utils/Local.cpp b/llvm/lib/Transforms/Utils/Local.cpp
index 643ad0fb24267..b17740c0bc192 100644
--- a/llvm/lib/Transforms/Utils/Local.cpp
+++ b/llvm/lib/Transforms/Utils/Local.cpp
@@ -228,10 +228,8 @@ bool llvm::ConstantFoldTerminator(BasicBlock *BB, bool DeleteDeadConditions,
unsigned Idx = It->getCaseIndex();
// Check for and prevent uint64_t overflow by reducing branch weights.
- if (Weights[0] > UINT64_MAX - Weights[Idx + 1]) {
- SmallVector<uint32_t> Fitted = fitWeights(Weights);
- Weights.assign(Fitted.begin(), Fitted.end());
- }
+ if (Weights[0] > UINT64_MAX - Weights[Idx + 1])
+ fitWeights(Weights);
Weights[0] += Weights[Idx + 1];
// Remove weight for this case.
diff --git a/llvm/test/Transforms/SimplifyCFG/switch-default-fold-weight-overflow.ll b/llvm/test/Transforms/SimplifyCFG/switch-default-fold-weight-overflow.ll
deleted file mode 100644
index 91311343e5572..0000000000000
--- a/llvm/test/Transforms/SimplifyCFG/switch-default-fold-weight-overflow.ll
+++ /dev/null
@@ -1,45 +0,0 @@
-; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
-; RUN: opt < %s -passes=simplifycfg -S | FileCheck %s
-
-; A case whose destination is the same as the default gets folded into the
-; default, adding its weight to the default's. If default + case overflows
-; uint64_t, the weights must be downscaled via fitWeights() before the
-; addition; otherwise it wraps around and the default's huge weight turns
-; into a tiny, wrong one.
-;
-; default = 2^64 - 100, folded case = 200 (default + case wraps around to
-; 100 as a uint64_t), surviving case = 1000. Correctly scaled, default stays
-; by far the biggest weight. If the downscale isn't applied, it collapses to
-; 100 -- smaller than the surviving case's 1000, which is the wrong relative
-; order.
-
-define void @switch_default_fold_overflow(i32 %x, ptr %p) {
-; CHECK-LABEL: define void @switch_default_fold_overflow(
-; CHECK-SAME: i32 [[X:%.*]], ptr [[P:%.*]]) {
-; CHECK-NEXT: [[ENTRY:.*:]]
-; CHECK-NEXT: [[COND:%.*]] = icmp eq i32 [[X]], 1
-; CHECK-NEXT: br i1 [[COND]], label %[[OTHER:.*]], label %[[DEFAULT:.*]], !prof [[PROF0:![0-9]+]]
-; CHECK: [[DEFAULT]]:
-; CHECK-NEXT: ret void
-; CHECK: [[OTHER]]:
-; CHECK-NEXT: store i32 0, ptr [[P]], align 4
-; CHECK-NEXT: br label %[[DEFAULT]]
-;
-entry:
- switch i32 %x, label %default [
- i32 0, label %default
- i32 1, label %other
- ], !prof !0
-
-default:
- ret void
-
-other:
- store i32 0, ptr %p
- br label %default
-}
-
-!0 = !{!"branch_weights", i64 18446744073709551516, i64 200, i64 1000}
-;.
-; CHECK: [[PROF0]] = !{!"branch_weights", i32 0, i32 -1}
-;.
``````````
</details>
https://github.com/llvm/llvm-project/pull/212540
More information about the llvm-commits
mailing list