[llvm] [LICM] Only check for provenance captures (PR #141731)
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Wed May 28 01:55:13 PDT 2025
https://github.com/nikic created https://github.com/llvm/llvm-project/pull/141731
When performing scalar promotions, only consider provenance captures, which may lead to non-thread-safe accesses. Address captures can be ignored.
>From 1041b5e2b36b50f8a3c1cfef7e884c93a9dfeb4f Mon Sep 17 00:00:00 2001
From: Nikita Popov <npopov at redhat.com>
Date: Wed, 28 May 2025 10:47:12 +0200
Subject: [PATCH 1/2] [LICM] Add test with address-only capture
---
llvm/test/Transforms/LICM/promote-capture.ll | 51 ++++++++++++++++++++
1 file changed, 51 insertions(+)
diff --git a/llvm/test/Transforms/LICM/promote-capture.ll b/llvm/test/Transforms/LICM/promote-capture.ll
index 5cbe158e47a3c..e4921e5009196 100644
--- a/llvm/test/Transforms/LICM/promote-capture.ll
+++ b/llvm/test/Transforms/LICM/promote-capture.ll
@@ -158,6 +158,57 @@ exit:
ret void
}
+define void @test_captured_before_loop_address_only(i32 %len) {
+; CHECK-LABEL: @test_captured_before_loop_address_only(
+; CHECK-NEXT: entry:
+; CHECK-NEXT: [[COUNT:%.*]] = alloca i32, align 4
+; CHECK-NEXT: store i32 0, ptr [[COUNT]], align 4
+; CHECK-NEXT: call void @capture(ptr captures(address) [[COUNT]])
+; CHECK-NEXT: [[COUNT_PROMOTED:%.*]] = load i32, ptr [[COUNT]], align 4
+; CHECK-NEXT: br label [[LOOP:%.*]]
+; CHECK: loop:
+; CHECK-NEXT: [[C_INC2:%.*]] = phi i32 [ [[COUNT_PROMOTED]], [[ENTRY:%.*]] ], [ [[C_INC1:%.*]], [[LATCH:%.*]] ]
+; CHECK-NEXT: [[I:%.*]] = phi i32 [ 0, [[ENTRY]] ], [ [[I_NEXT:%.*]], [[LATCH]] ]
+; CHECK-NEXT: [[COND:%.*]] = call i1 @cond(i32 [[I]])
+; CHECK-NEXT: br i1 [[COND]], label [[IF:%.*]], label [[LATCH]]
+; CHECK: if:
+; CHECK-NEXT: [[C_INC:%.*]] = add i32 [[C_INC2]], 1
+; CHECK-NEXT: store i32 [[C_INC]], ptr [[COUNT]], align 4
+; CHECK-NEXT: br label [[LATCH]]
+; CHECK: latch:
+; CHECK-NEXT: [[C_INC1]] = phi i32 [ [[C_INC]], [[IF]] ], [ [[C_INC2]], [[LOOP]] ]
+; CHECK-NEXT: [[I_NEXT]] = add nuw i32 [[I]], 1
+; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[I_NEXT]], [[LEN:%.*]]
+; CHECK-NEXT: br i1 [[CMP]], label [[EXIT:%.*]], label [[LOOP]]
+; CHECK: exit:
+; CHECK-NEXT: ret void
+;
+entry:
+ %count = alloca i32
+ store i32 0, ptr %count
+ call void @capture(ptr captures(address) %count)
+ br label %loop
+
+loop:
+ %i = phi i32 [ 0, %entry ], [ %i.next, %latch ]
+ %cond = call i1 @cond(i32 %i)
+ br i1 %cond, label %if, label %latch
+
+if:
+ %c = load i32, ptr %count
+ %c.inc = add i32 %c, 1
+ store i32 %c.inc, ptr %count
+ br label %latch
+
+latch:
+ %i.next = add nuw i32 %i, 1
+ %cmp = icmp eq i32 %i.next, %len
+ br i1 %cmp, label %exit, label %loop
+
+exit:
+ ret void
+}
+
; Should not get promoted, because the pointer is captured and may not
; be thread-local.
define void @test_captured_before_loop_byval(ptr byval(i32) align 4 %count, i32 %len) {
>From 3ac28c6825100ddf2d4b4a26303090644467c2a0 Mon Sep 17 00:00:00 2001
From: Nikita Popov <npopov at redhat.com>
Date: Wed, 28 May 2025 10:49:04 +0200
Subject: [PATCH 2/2] [LICM] Only check for provenance captures
When performing scalar promotions, only consider provenance
escapes, which may lead to non-thread-safe accesses. Address captures
can be ignored.
---
llvm/lib/Transforms/Scalar/LICM.cpp | 5 +++--
llvm/test/Transforms/LICM/promote-capture.ll | 3 ++-
2 files changed, 5 insertions(+), 3 deletions(-)
diff --git a/llvm/lib/Transforms/Scalar/LICM.cpp b/llvm/lib/Transforms/Scalar/LICM.cpp
index 006a09b38bc71..7965ed76a81b7 100644
--- a/llvm/lib/Transforms/Scalar/LICM.cpp
+++ b/llvm/lib/Transforms/Scalar/LICM.cpp
@@ -1928,8 +1928,9 @@ bool isNotCapturedBeforeOrInLoop(const Value *V, const Loop *L,
// loop header, as the loop header is reachable from any instruction inside
// the loop.
// TODO: ReturnCaptures=true shouldn't be necessary here.
- return !PointerMayBeCapturedBefore(V, /* ReturnCaptures */ true,
- L->getHeader()->getTerminator(), DT);
+ return capturesNothing(PointerMayBeCapturedBefore(
+ V, /*ReturnCaptures=*/true, L->getHeader()->getTerminator(), DT,
+ /*IncludeI=*/false, CaptureComponents::Provenance));
}
/// Return true if we can prove that a caller cannot inspect the object if an
diff --git a/llvm/test/Transforms/LICM/promote-capture.ll b/llvm/test/Transforms/LICM/promote-capture.ll
index e4921e5009196..eac670e6f3edd 100644
--- a/llvm/test/Transforms/LICM/promote-capture.ll
+++ b/llvm/test/Transforms/LICM/promote-capture.ll
@@ -173,7 +173,6 @@ define void @test_captured_before_loop_address_only(i32 %len) {
; CHECK-NEXT: br i1 [[COND]], label [[IF:%.*]], label [[LATCH]]
; CHECK: if:
; CHECK-NEXT: [[C_INC:%.*]] = add i32 [[C_INC2]], 1
-; CHECK-NEXT: store i32 [[C_INC]], ptr [[COUNT]], align 4
; CHECK-NEXT: br label [[LATCH]]
; CHECK: latch:
; CHECK-NEXT: [[C_INC1]] = phi i32 [ [[C_INC]], [[IF]] ], [ [[C_INC2]], [[LOOP]] ]
@@ -181,6 +180,8 @@ define void @test_captured_before_loop_address_only(i32 %len) {
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[I_NEXT]], [[LEN:%.*]]
; CHECK-NEXT: br i1 [[CMP]], label [[EXIT:%.*]], label [[LOOP]]
; CHECK: exit:
+; CHECK-NEXT: [[C_INC1_LCSSA:%.*]] = phi i32 [ [[C_INC1]], [[LATCH]] ]
+; CHECK-NEXT: store i32 [[C_INC1_LCSSA]], ptr [[COUNT]], align 4
; CHECK-NEXT: ret void
;
entry:
More information about the llvm-commits
mailing list