[llvm] [LICM] Recompute insertion point for promotion when necessary (PR #229581)
Eli Friedman via llvm-commits
llvm-commits at lists.llvm.org
Tue Oct 6 15:05:56 PDT 2026
https://github.com/efriedma-quic created https://github.com/llvm/llvm-project/pull/229581
Each time we call collectPromotionCandidates, it considers the loop from
scratch, without considering any operations which have been previously
promoted. This is generally fine, but that also means we need recompute
all the relevant state.
Without this patch, we would use a stale insertion point: a point after
any promoted stores. This meant promotion would sink the newly inserted
store past previously promoted stores, without checking aliasing.
The way the code is currently structured, I think this issue can only
show up with aliasing metadata.
Fixes https://github.com/llvm/llvm-project/issues/226391 .
>From ca1e88e3b8ecec8958ce309bb6c9f65b267dabb4 Mon Sep 17 00:00:00 2001
From: Eli Friedman <efriedma at qti.qualcomm.com>
Date: Tue, 6 Oct 2026 14:52:32 -0700
Subject: [PATCH 1/2] Precommit test for #226391
---
.../Transforms/LICM/scalar-promote-aa-tags.ll | 56 +++++++++++++++++++
1 file changed, 56 insertions(+)
diff --git a/llvm/test/Transforms/LICM/scalar-promote-aa-tags.ll b/llvm/test/Transforms/LICM/scalar-promote-aa-tags.ll
index c7bd7265f1986..3ac6d70c51616 100644
--- a/llvm/test/Transforms/LICM/scalar-promote-aa-tags.ll
+++ b/llvm/test/Transforms/LICM/scalar-promote-aa-tags.ll
@@ -629,6 +629,62 @@ exit:
ret i32 %res
}
+; All the stores in the loop are promoted. Make sure the promoted stores
+; are emitted in the right order.
+define void @promote_multiple_iterations() {
+; CHECK-LABEL: define void @promote_multiple_iterations() {
+; CHECK-NEXT: [[ENTRY:.*]]:
+; CHECK-NEXT: [[ADDR:%.*]] = alloca ptr, align 8
+; CHECK-NEXT: store ptr null, ptr [[ADDR]], align 8
+; CHECK-NEXT: [[ADDR_PROMOTED:%.*]] = load i64, ptr [[ADDR]], align 1
+; CHECK-NEXT: br label %[[BB_1:.*]]
+; CHECK: [[BB_1_LOOPEXIT:.*]]:
+; CHECK-NEXT: [[DOTLCSSA1:%.*]] = phi i64 [ [[TMP1:%.*]], %[[BB_2:.*]] ]
+; CHECK-NEXT: br label %[[BB_1]]
+; CHECK: [[BB_1]]:
+; CHECK-NEXT: [[TMP0:%.*]] = phi i64 [ [[DOTLCSSA1]], %[[BB_1_LOOPEXIT]] ], [ [[ADDR_PROMOTED]], %[[ENTRY]] ]
+; CHECK-NEXT: br i1 true, label %[[EXIT:.*]], label %[[BB_2_PREHEADER:.*]]
+; CHECK: [[BB_2_PREHEADER]]:
+; CHECK-NEXT: br label %[[BB_2]]
+; CHECK: [[BB_2]]:
+; CHECK-NEXT: [[TMP1]] = phi i64 [ [[TMP0]], %[[BB_2_PREHEADER]] ], [ 0, %[[UNREACHABLE:.*]] ]
+; CHECK-NEXT: br i1 false, label %[[UNREACHABLE]], label %[[BB_1_LOOPEXIT]]
+; CHECK: [[EXIT]]:
+; CHECK-NEXT: [[DOTLCSSA:%.*]] = phi i64 [ [[TMP0]], %[[BB_1]] ]
+; CHECK-NEXT: store ptr @promote_multiple_iterations, ptr [[ADDR]], align 8, !tbaa [[INT_TBAA0]]
+; CHECK-NEXT: store i64 [[DOTLCSSA]], ptr [[ADDR]], align 1
+; CHECK-NEXT: [[VAL:%.*]] = load ptr, ptr [[ADDR]], align 8
+; CHECK-NEXT: [[CMP:%.*]] = icmp ne ptr [[VAL]], null
+; CHECK-NEXT: call void @llvm.assume(i1 [[CMP]])
+; CHECK-NEXT: ret void
+; CHECK: [[UNREACHABLE]]:
+; CHECK-NEXT: br label %[[BB_2]]
+;
+entry:
+ %addr = alloca ptr, align 8
+ store ptr null, ptr %addr, align 8
+ br label %bb.1
+
+bb.1:
+ store ptr @promote_multiple_iterations, ptr %addr, align 8, !tbaa !0
+ br i1 true, label %exit, label %bb.2
+
+bb.2:
+ br i1 false, label %unreachable, label %bb.1
+
+exit:
+ %val = load ptr, ptr %addr, align 8
+ %cmp = icmp ne ptr %val, null
+ call void @llvm.assume(i1 %cmp)
+ ret void
+
+unreachable:
+ store ptr %addr, ptr %addr, align 8, !tbaa !0
+ %1 = load ptr, ptr %addr, align 8, !tbaa !0
+ store i64 0, ptr %1, align 8, !tbaa !3
+ br label %bb.2
+}
+
!0 = !{!4, !4, i64 0}
!1 = !{!"omnipotent char", !2}
!2 = !{!"Simple C/C++ TBAA"}
>From 7d80c1eab7c2d4f02fa0513fed392f58eec2221d Mon Sep 17 00:00:00 2001
From: Eli Friedman <efriedma at qti.qualcomm.com>
Date: Tue, 6 Oct 2026 14:53:31 -0700
Subject: [PATCH 2/2] [LICM] Recompute insertion point for promotion when
necessary.
Each time we call collectPromotionCandidates, it considers the loop from
scratch, without considering any operations which have been previously
promoted. This is generally fine, but that also means we need recompute
all the relevant state.
Without this patch, we would use a stale insertion point: a point after
any promoted stores. This meant promotion would sink the newly inserted
store past previosly promoted stores, without checking aliasing.
The way the code is currently structured, I think this issue can only
show up with aliasing metadata.
Fixes #226391.
---
llvm/lib/Transforms/Scalar/LICM.cpp | 16 +++++++++++-----
.../Transforms/LICM/scalar-promote-aa-tags.ll | 2 +-
2 files changed, 12 insertions(+), 6 deletions(-)
diff --git a/llvm/lib/Transforms/Scalar/LICM.cpp b/llvm/lib/Transforms/Scalar/LICM.cpp
index aa8bba03f5ce5..73fa553e3a3b1 100644
--- a/llvm/lib/Transforms/Scalar/LICM.cpp
+++ b/llvm/lib/Transforms/Scalar/LICM.cpp
@@ -505,11 +505,6 @@ bool LoopInvariantCodeMotion::runOnLoop(Loop *L, AAResults *AA, LoopInfo *LI,
SmallVector<MemoryAccess *, 8> MSSAInsertPts;
InsertPts.reserve(ExitBlocks.size());
MSSAInsertPts.reserve(ExitBlocks.size());
- for (BasicBlock *ExitBlock : ExitBlocks) {
- InsertPts.push_back(ExitBlock->getFirstInsertionPt());
- MSSAInsertPts.push_back(nullptr);
- }
-
PredIteratorCache PIC;
// Promoting one set of accesses may make the pointers for another set
@@ -518,6 +513,17 @@ bool LoopInvariantCodeMotion::runOnLoop(Loop *L, AAResults *AA, LoopInfo *LI,
bool LocalPromoted;
do {
LocalPromoted = false;
+
+ // Recompute the insertion points each time we compute the promotion
+ // candidates, so we don't sink past a store which was promoted in a
+ // previous iteration.
+ InsertPts.clear();
+ MSSAInsertPts.clear();
+ for (BasicBlock *ExitBlock : ExitBlocks) {
+ InsertPts.push_back(ExitBlock->getFirstInsertionPt());
+ MSSAInsertPts.push_back(nullptr);
+ }
+
for (auto [PointerMustAliases, HasReadsOutsideSet] :
collectPromotionCandidates(MSSA, AA, DT, &SafetyInfo,
LoopLocalAliasScopes, L)) {
diff --git a/llvm/test/Transforms/LICM/scalar-promote-aa-tags.ll b/llvm/test/Transforms/LICM/scalar-promote-aa-tags.ll
index 3ac6d70c51616..d4c1c7228a339 100644
--- a/llvm/test/Transforms/LICM/scalar-promote-aa-tags.ll
+++ b/llvm/test/Transforms/LICM/scalar-promote-aa-tags.ll
@@ -651,8 +651,8 @@ define void @promote_multiple_iterations() {
; CHECK-NEXT: br i1 false, label %[[UNREACHABLE]], label %[[BB_1_LOOPEXIT]]
; CHECK: [[EXIT]]:
; CHECK-NEXT: [[DOTLCSSA:%.*]] = phi i64 [ [[TMP0]], %[[BB_1]] ]
-; CHECK-NEXT: store ptr @promote_multiple_iterations, ptr [[ADDR]], align 8, !tbaa [[INT_TBAA0]]
; CHECK-NEXT: store i64 [[DOTLCSSA]], ptr [[ADDR]], align 1
+; CHECK-NEXT: store ptr @promote_multiple_iterations, ptr [[ADDR]], align 8, !tbaa [[INT_TBAA0]]
; CHECK-NEXT: [[VAL:%.*]] = load ptr, ptr [[ADDR]], align 8
; CHECK-NEXT: [[CMP:%.*]] = icmp ne ptr [[VAL]], null
; CHECK-NEXT: call void @llvm.assume(i1 [[CMP]])
More information about the llvm-commits
mailing list