[llvm] [SimplifyCFG][PGO] Add missing overflow check to ConstantFoldTerminator (PR #178964)

Paul Kirth via llvm-commits llvm-commits at lists.llvm.org
Thu Feb 5 09:07:27 PST 2026


================
@@ -0,0 +1,27 @@
+; RUN: opt < %s -passes=simplifycfg -S | FileCheck %s
+
+; Ensure branch weight does not overflow when folding block 3 into block 0.
+; This IR test is reduced from an optimization that occurs during the Jump
+; Threading pass, which creates a branch prediction of 0% and 100%. This is
+; reduced in SimplifyCFG into the IR shown in the
+; switch-branch-weight-overflow.ll test.
+
+define void @foo(ptr %Overflow) {
+  %1 = extractvalue { i32, i1 } zeroinitializer, 0
+  %2 = icmp eq i32 %1, 0
+  br i1 %2, label %6, label %3
+
+3:                                                ; preds = %0
+  %4 = icmp eq i32 %1, 1
+  br i1 %4, label %5, label %6, !prof !0
+
+5:                                                ; preds = %3
+  store i32 0, ptr %Overflow, align 4
+  br label %6
+
+6:                                                ; preds = %5, %3, %0
+  ret void
+}
+
+; CHECK: branch_weights{{.*}} 0, {{.*}} -2147483648
----------------
ilovepi wrote:

When we implemented it, anything that reweighed or merged weights would drop it, since the weights wren't set by. I wasn't aware of a case where we'd end back at the original weights, so maybe we didn't consider that. At any rate, I suppose since it went through a switch transform, we'd naturally expect it to get stripped. This is probably something we want to consider more in the future, but for now what's happening doesn't conflict with my mental model. 

Since there's no `!"expected"` metadata field, lets be explicit in your CHECK line. In fact, I don't think you need any regex for this, given that the output would be deterministic, and the types will always be `i32`. Plus if `!"expected" ever stops getting stripped, we'll have notice. 

https://github.com/llvm/llvm-project/pull/178964


More information about the llvm-commits mailing list