Skip to content

[SROA] Support load-only promotion with dynamic offset loads #135609

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion llvm/lib/Transforms/Scalar/SROA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1114,8 +1114,10 @@ class AllocaSlices::SliceBuilder : public PtrUseVisitor<SliceBuilder> {
assert((!LI.isSimple() || LI.getType()->isSingleValueType()) &&
"All simple FCA loads should have been pre-split");

// If there is a load with an unknown offset, we can still perform store
// to load forwarding for other known-offset loads.
if (!IsOffsetKnown)
return PI.setAborted(&LI);
return PI.setEscapedReadOnly(&LI);

TypeSize Size = DL.getTypeStoreSize(LI.getType());
if (Size.isScalable())
Expand Down
8 changes: 6 additions & 2 deletions llvm/test/Transforms/SROA/readonlynocapture.ll
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,8 @@ define i32 @twoalloc_with_lifetimes() {

declare void @use.i32(i32)

; We can promote the %i load, even though there is an unknown offset load
; in the loop. It is sufficient that we know all stores.
define void @load_dyn_offset(ptr %ary) {
; CHECK-LABEL: @load_dyn_offset(
; CHECK-NEXT: [[A:%.*]] = alloca { i64, [4 x i32] }, align 8
Expand All @@ -509,8 +511,8 @@ define void @load_dyn_offset(ptr %ary) {
; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr [[GEP]], ptr [[ARY:%.*]], i64 16, i1 false)
; CHECK-NEXT: br label [[LOOP:%.*]]
; CHECK: loop:
; CHECK-NEXT: [[I:%.*]] = load i64, ptr [[A]], align 4
; CHECK-NEXT: [[I_NEXT:%.*]] = add i64 [[I]], 1
; CHECK-NEXT: [[I:%.*]] = phi i64 [ [[I_NEXT:%.*]], [[LOOP]] ], [ 0, [[TMP0:%.*]] ]
; CHECK-NEXT: [[I_NEXT]] = add i64 [[I]], 1
; CHECK-NEXT: store i64 [[I_NEXT]], ptr [[A]], align 4
; CHECK-NEXT: [[GEP_I:%.*]] = getelementptr i32, ptr [[GEP]], i64 [[I]]
; CHECK-NEXT: [[VAL:%.*]] = load i32, ptr [[GEP_I]], align 4
Expand Down Expand Up @@ -540,6 +542,8 @@ exit:
ret void
}

; Same as previous test, but with an unknown-offset store. We can't promote in
; that case.
define void @store_dyn_offset(ptr %ary) {
; CHECK-LABEL: @store_dyn_offset(
; CHECK-NEXT: [[A:%.*]] = alloca { i64, [4 x i32] }, align 8
Expand Down
Loading