[llvm] [SimplifyCFG] Update profile metadata regardless of weight count (PR #190982)

via llvm-commits llvm-commits at lists.llvm.org
Wed Apr 8 07:45:57 PDT 2026


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-ir

Author: Aiden Grossman (boomanaiden154)

<details>
<summary>Changes</summary>

Otherwise we miss updating in cases where we should be updating which causes assertion failures later due to the fact that the number of cases no longer matches the number of branch weights.

It looks like this was last touched in #<!-- -->160629, but it doesn't seem like there is anything there specifically motivating this diff hunk.

Fixes #<!-- -->190901.

---
Full diff: https://github.com/llvm/llvm-project/pull/190982.diff


2 Files Affected:

- (modified) llvm/include/llvm/IR/Instructions.h (+1-1) 
- (added) llvm/test/Transforms/SimplifyCFG/profile-update-unreachable.ll (+39) 


``````````diff
diff --git a/llvm/include/llvm/IR/Instructions.h b/llvm/include/llvm/IR/Instructions.h
index bdc6d5bd2f5e5..9f347a39e6671 100644
--- a/llvm/include/llvm/IR/Instructions.h
+++ b/llvm/include/llvm/IR/Instructions.h
@@ -3699,7 +3699,7 @@ class SwitchInstProfUpdateWrapper {
   SwitchInstProfUpdateWrapper(SwitchInst &SI) : SI(SI) { init(); }
 
   ~SwitchInstProfUpdateWrapper() {
-    if (Changed && Weights.has_value() && Weights->size() >= 2)
+    if (Changed && Weights.has_value())
       setBranchWeights(SI, Weights.value(), /*IsExpected=*/false);
   }
 
diff --git a/llvm/test/Transforms/SimplifyCFG/profile-update-unreachable.ll b/llvm/test/Transforms/SimplifyCFG/profile-update-unreachable.ll
new file mode 100644
index 0000000000000..fcafb4a716c87
--- /dev/null
+++ b/llvm/test/Transforms/SimplifyCFG/profile-update-unreachable.ll
@@ -0,0 +1,39 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
+; RUN: opt < %s -passes="simplifycfg" -S | FileCheck %s
+
+define i32 @f1(i1 %cond1, i8 %cond2) {
+; CHECK-LABEL: define i32 @f1(
+; CHECK-SAME: i1 [[COND1:%.*]], i8 [[COND2:%.*]]) {
+; CHECK-NEXT:  [[ENTRY:.*:]]
+; CHECK-NEXT:    call void @llvm.assume(i1 [[COND1]])
+; CHECK-NEXT:    ret i32 0
+;
+entry:
+  br i1 %cond1, label %swbb1, label %swbb2
+
+swbb1.case1:
+  ret i32 0
+
+swbb2.case1:
+  unreachable
+
+sw.case2:
+  unreachable
+
+sw.default:
+  unreachable
+
+swbb1:
+  switch i8 %cond2, label %sw.default [
+  i8 0, label %swbb1.case1
+  i8 1, label %sw.case2
+  ]
+
+swbb2:
+  switch i8 %cond2, label %sw.default [
+  i8 0, label %swbb2.case1
+  i8 1, label %sw.case2
+  ], !prof !0
+}
+
+!0 = !{!"branch_weights", i32 3, i32 5, i32 7}

``````````

</details>


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


More information about the llvm-commits mailing list