[llvm] 0f3e448 - [SimplifyCFG] Update profile metadata regardless of weight count (#190982)

via llvm-commits llvm-commits at lists.llvm.org
Wed Apr 8 09:15:06 PDT 2026


Author: Aiden Grossman
Date: 2026-04-08T16:15:01Z
New Revision: 0f3e448c980f083b97ad4c2b93525dd44e7ba743

URL: https://github.com/llvm/llvm-project/commit/0f3e448c980f083b97ad4c2b93525dd44e7ba743
DIFF: https://github.com/llvm/llvm-project/commit/0f3e448c980f083b97ad4c2b93525dd44e7ba743.diff

LOG: [SimplifyCFG] Update profile metadata regardless of weight count (#190982)

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.

Added: 
    llvm/test/Transforms/SimplifyCFG/profile-update-unreachable.ll

Modified: 
    llvm/include/llvm/IR/Instructions.h

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/IR/Instructions.h b/llvm/include/llvm/IR/Instructions.h
index bdc6d5bd2f5e5..f4fa59ada9d05 100644
--- a/llvm/include/llvm/IR/Instructions.h
+++ b/llvm/include/llvm/IR/Instructions.h
@@ -3699,8 +3699,17 @@ class SwitchInstProfUpdateWrapper {
   SwitchInstProfUpdateWrapper(SwitchInst &SI) : SI(SI) { init(); }
 
   ~SwitchInstProfUpdateWrapper() {
-    if (Changed && Weights.has_value() && Weights->size() >= 2)
-      setBranchWeights(SI, Weights.value(), /*IsExpected=*/false);
+    if (Changed && Weights.has_value()) {
+      if (Weights->size() >= 2) {
+        setBranchWeights(SI, Weights.value(), /*IsExpected=*/false);
+        return;
+      }
+      // In some cases while simplifying switch instructions, we end up with
+      // degenerate switch instructions (e.g., only contains the default case).
+      // We drop profile metadata in such cases rather than updating given it
+      // does not convey anything.
+      SI.setMetadata(LLVMContext::MD_prof, nullptr);
+    }
   }
 
   /// Delegate the call to the underlying SwitchInst::removeCase() and remove

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}


        


More information about the llvm-commits mailing list