[llvm] Reland "[SimplifyCFG] Fix branch-weight overflow when folding switch case into default (#212520)" (PR #212867)

Alok Kumar Sharma via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 29 13:50:05 PDT 2026


https://github.com/alokkrsharma created https://github.com/llvm/llvm-project/pull/212867

Relands of #212520.

The original change was reverted in PR #212540 due to a profcheck build
failure triggered by a newly added test.

The test intentionally uses branch weights larger than 32 bits to exercise the uint64_t overflow guard in ConstantFoldTerminator. During profcheck processing, the branch weight metadata is analyzed before SimplifyCFG runs, which results in an assertion for this test configuration.

This reland:
- Restores the original change from PR #212520.
- Marks the affected test UNSUPPORTED under profcheck.

Original PR: #212520
Revert PR: #212540

>From a15dec7ab806666f6b9d5bdaf384b39dfd4738b9 Mon Sep 17 00:00:00 2001
From: Alok Kumar Sharma <AlokKumar.Sharma at amd.com>
Date: Tue, 28 Jul 2026 21:58:34 +0530
Subject: [PATCH 1/2] [SimplifyCFG] Fix branch-weight overflow when folding
 switch case into default (#212520)

The overflow guard added in #178964 for merging a switch case's weight
into the default's calls fitWeights() to downscale the weights, but the
returned value was never assigned back to Weights, so the downscale
didn't take effect. As a result, a default+case sum that overflows
uint64_t would wrap around, turning a dominant default weight into a
small, incorrect one.

Assign fitWeights()'s result back to Weights before the addition so the
downscale takes effect as intended.
---
 llvm/lib/Transforms/Utils/Local.cpp           |  6 ++-
 .../switch-default-fold-weight-overflow.ll    | 45 +++++++++++++++++++
 2 files changed, 49 insertions(+), 2 deletions(-)
 create mode 100644 llvm/test/Transforms/SimplifyCFG/switch-default-fold-weight-overflow.ll

diff --git a/llvm/lib/Transforms/Utils/Local.cpp b/llvm/lib/Transforms/Utils/Local.cpp
index b17740c0bc192..643ad0fb24267 100644
--- a/llvm/lib/Transforms/Utils/Local.cpp
+++ b/llvm/lib/Transforms/Utils/Local.cpp
@@ -228,8 +228,10 @@ 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])
-            fitWeights(Weights);
+          if (Weights[0] > UINT64_MAX - Weights[Idx + 1]) {
+            SmallVector<uint32_t> Fitted = fitWeights(Weights);
+            Weights.assign(Fitted.begin(), Fitted.end());
+          }
 
           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
new file mode 100644
index 0000000000000..91311343e5572
--- /dev/null
+++ b/llvm/test/Transforms/SimplifyCFG/switch-default-fold-weight-overflow.ll
@@ -0,0 +1,45 @@
+; 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}
+;.

>From fbe491d71b10df6af9c276f16317e6588343397d Mon Sep 17 00:00:00 2001
From: Alok Kumar Sharma <AlokKumar.Sharma at amd.com>
Date: Thu, 30 Jul 2026 01:46:23 +0530
Subject: [PATCH 2/2] [SimplifyCFG] Skip switch-default-fold-weight-overflow.ll
 under profcheck

The test uses branch weights larger than 32 bits on purpose, to trigger
the uint64_t overflow guard in ConstantFoldTerminator. profcheck's
prof-inject pass analyzes that metadata before SimplifyCFG runs, using
an API that only supports 32-bit weights, so it asserts. Not a real bug,
just mark the test UNSUPPORTED there.
---
 .../SimplifyCFG/switch-default-fold-weight-overflow.ll   | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/llvm/test/Transforms/SimplifyCFG/switch-default-fold-weight-overflow.ll b/llvm/test/Transforms/SimplifyCFG/switch-default-fold-weight-overflow.ll
index 91311343e5572..b6b261a14ea9c 100644
--- a/llvm/test/Transforms/SimplifyCFG/switch-default-fold-weight-overflow.ll
+++ b/llvm/test/Transforms/SimplifyCFG/switch-default-fold-weight-overflow.ll
@@ -1,6 +1,15 @@
 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
 ; RUN: opt < %s -passes=simplifycfg -S | FileCheck %s
 
+; This test's branch-weight metadata deliberately exceeds 32 bits (see the
+; comment below) to exercise the uint64_t overflow guard in SimplifyCFG. That
+; makes it incompatible with the profcheck configuration: its prof-inject
+; pass runs BranchProbabilityAnalysis on the switch before SimplifyCFG even
+; runs, and that analysis reads branch_weights via the 32-bit-only
+; extractBranchWeights() API, which asserts on values that don't fit in
+; 32 bits.
+; UNSUPPORTED: profcheck
+
 ; 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



More information about the llvm-commits mailing list