[llvm] fix for miscompilation for #150120 (PR #173961)

via llvm-commits llvm-commits at lists.llvm.org
Mon Dec 29 22:35:47 PST 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-transforms

Author: Nimit Sachdeva (nimit25)

<details>
<summary>Changes</summary>

Fixes the miscompilation discussed for the PR [164882](https://github.com/llvm/llvm-project/pull/164882/commits) as part of generalizing the optimization for the issue [150120](https://github.com/llvm/llvm-project/issues/150120)

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


2 Files Affected:

- (modified) llvm/lib/Transforms/Utils/MoveAutoInit.cpp (+2-1) 
- (added) llvm/test/Transforms/MoveAutoInit/loop-store.ll (+57) 


``````````diff
diff --git a/llvm/lib/Transforms/Utils/MoveAutoInit.cpp b/llvm/lib/Transforms/Utils/MoveAutoInit.cpp
index ad105f5a57b49..9302d5e284fd3 100644
--- a/llvm/lib/Transforms/Utils/MoveAutoInit.cpp
+++ b/llvm/lib/Transforms/Utils/MoveAutoInit.cpp
@@ -164,7 +164,8 @@ static bool runMoveAutoInit(Function &F, DominatorTree &DT, MemorySSA &MSSA) {
 
         if (!DT.isReachableFromEntry(Pred))
           continue;
-
+        if (!DT.dominates(Pred, UsersDominatorHead))
+          continue;
         DominatingPredecessor =
             DominatingPredecessor
                 ? DT.findNearestCommonDominator(DominatingPredecessor, Pred)
diff --git a/llvm/test/Transforms/MoveAutoInit/loop-store.ll b/llvm/test/Transforms/MoveAutoInit/loop-store.ll
new file mode 100644
index 0000000000000..8bd0913728db8
--- /dev/null
+++ b/llvm/test/Transforms/MoveAutoInit/loop-store.ll
@@ -0,0 +1,57 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 2
+; RUN: opt -passes='move-auto-init' -verify-memoryssa -S < %s | FileCheck %s
+
+
+define i8 @test_no_update(i32 %a, i32 %b) {
+; CHECK-LABEL: define i8 @test_no_update
+; CHECK-SAME: (i32 [[A:%.*]], i32 [[B:%.*]]) {
+; CHECK-NEXT:  lbl_653:
+; CHECK-NEXT:    [[L_580:%.*]] = alloca i32, align 4
+; CHECK-NEXT:    store i32 1, ptr [[L_580]], align 4
+; CHECK-NEXT:    br i1 true, label [[LBL_629:%.*]], label [[IF_THEN:%.*]]
+; CHECK:       if.then:
+; CHECK-NEXT:    br label [[IF_END:%.*]]
+; CHECK:       lbl_629:
+; CHECK-NEXT:    br label [[IF_END]]
+; CHECK:       if.end:
+; CHECK-NEXT:    [[TMP0:%.*]] = load i32, ptr [[L_580]], align 4
+; CHECK-NEXT:    [[TOBOOL223_NOT:%.*]] = icmp eq i32 [[TMP0]], 0
+; CHECK-NEXT:    br i1 [[TOBOOL223_NOT]], label [[LOR_RHS224:%.*]], label [[LOR_END230:%.*]]
+; CHECK:       lor.rhs224:
+; CHECK-NEXT:    store i8 0, ptr null, align 1
+; CHECK-NEXT:    br label [[LOR_END230]]
+; CHECK:       lor.end230:
+; CHECK-NEXT:    br i1 true, label [[FOR_INC456_1:%.*]], label [[LBL_629]]
+; CHECK:       for.inc456.1:
+; CHECK-NEXT:    ret i8 0
+;
+
+lbl_653:
+  %l_580 = alloca i32, align 4
+  store i32 1, ptr %l_580, align 4, !annotation !0
+  br i1 true, label %lbl_629, label %if.then
+
+if.then:                                          ; preds = %lbl_653
+  br label %if.end
+
+lbl_629:                                          ; preds = %lor.end230, %lbl_653
+  br label %if.end
+
+if.end:                                           ; preds = %lbl_629, %if.then
+  %0 = load i32, ptr %l_580, align 4
+  %tobool223.not = icmp eq i32 %0, 0
+  br i1 %tobool223.not, label %lor.rhs224, label %lor.end230
+
+lor.rhs224:                                       ; preds = %if.end
+  store i8 0, ptr null, align 1
+  br label %lor.end230
+
+lor.end230:                                       ; preds = %lor.rhs224, %if.end
+  br i1 true, label %for.inc456.1, label %lbl_629
+
+for.inc456.1:                                     ; preds = %lor.end230
+  ret i8 0
+}
+
+
+!0 = !{!"auto-init"}
\ No newline at end of file

``````````

</details>


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


More information about the llvm-commits mailing list