[AArch64] Loop Vectorizer fails to form interleaved store group (st2) when pointers are outer-loop inductions
Summary
The Loop Vectorizer fails to form a factor-2 interleaved store group when two stride-2 store pointers are carried across an outer loop. The cost model then sees the stores as expensive scalar scatters and rejects vectorization entirely as "not beneficial."
The same logical store pattern in a single (non-nested) loop correctly forms the interleave group, vectorizes, and generates st2.
Reproduction
// WORKS: vectorized with st2
void single_loop(const float* __restrict src, float* __restrict dst, unsigned n) {
float* pA = dst;
float* pB = dst + 1;
for (unsigned i = 0; i < n; i++) {
*pA = src[i] * 2.0f;
*pB = src[i] * 3.0f;
pA += 2;
pB += 2;
}
}
// FAILS: not vectorized ("not beneficial")
void nested_loop(const float* __restrict src, float* __restrict dst,
unsigned width, unsigned height, unsigned srcStride) {
float* pA = dst;
float* pB = dst + 1;
for (unsigned y = 0; y < height; y++) {
const float* row = src + y * srcStride;
for (unsigned x = 0; x < width; x++) {
*pA = row[x] * 2.0f;
*pB = row[x] * 3.0f;
pA += 2;
pB += 2;
}
}
}
Compile with:
clang --target=aarch64-linux-gnu -O2 -S test.cpp -Rpass=loop-vectorize -Rpass-missed=loop-vectorize
Observed Behavior
single_loop: vectorized (width 4, interleaved count 2), generates optimal st2:
st2 { v5.4s, v6.4s }, [x11]
st2 { v3.4s, v4.4s }, [x14]
nested_loop: not vectorized at all:
remark: the cost-model indicates that vectorization is not beneficial
remark: the cost-model indicates that interleaving is not beneficial
If vectorization is forced with #pragma clang loop vectorize(enable) vectorize_width(4), the computation is vectorized but stores are emitted as scalar lane extracts:
st1 { v3.s }[1], [x7]
st1 { v3.s }[3], [x7]
st1 { v3.s }[2], [x1]
st1 { v2.s }[1], [x7]
st1 { v2.s }[2], [x5]
st1 { v2.s }[3], [x7]
Expected Behavior
nested_loop should vectorize and generate st2 instructions, since the inner loop store pattern is identical to single_loop (two pointers offset by 4 bytes, both advancing by stride 8 bytes per iteration).
Impact
This pattern is common in image processing code that computes two (or more) output channels stored interleaved in memory, with a 2D loop nest (rows x columns).
The performance impact is significant: st2 writes 32 bytes in one instruction vs 6+ st1 lane stores for the same data.
@fhahn @david-arm What would be the preferred fix direction for this? Should the interleave group analysis be extended to handle this case (e.g., by looking through outer-loop PHIs to determine the constant offset), or should an earlier pass canonicalize the IR (something like.. the second pointer is expressed as a GEP off the first) or something different all together.
Please provide your thoughts.
Had a look at this. Reproduced on trunk. Sharing some datapoints:
Root cause:
for.body4: ; preds = %for.cond2
%idxprom = zext i32 %x.0 to i64
%arrayidx = getelementptr inbounds nuw float, ptr %add.ptr1, i64 %idxprom
%0 = load float, ptr %arrayidx, align 4
%mul5 = fmul float %0, 2.000000e+00
store float %mul5, ptr %pA.1, align 4
%idxprom6 = zext i32 %x.0 to i64
%arrayidx7 = getelementptr inbounds nuw float, ptr %add.ptr1, i64 %idxprom6
%1 = load float, ptr %arrayidx7, align 4
%mul8 = fmul float %1, 3.000000e+00
store float %mul8, ptr %pB.1, align 4
%add.ptr9 = getelementptr inbounds float, ptr %pA.1, i64 2
%add.ptr10 = getelementptr inbounds float, ptr %pB.1, i64 2
br label %for.inc
Run -passes='loop-vectorize' with custom logging:
A: {%pA.0,+,8}<nuw><%for.cond2>
B: {%pB.0,+,8}<nuw><%for.cond2>
A-B = ***COULDNOTCOMPUTE***
members: 1 factors: 2
LV: Invalidate candidate interleaved store group due to gaps.
The two stores never merged into one interleave group. InterleavedAccessInfo::analyzeInterleaving requires the A→B distance to be a compile-time constant.
getMinusSCEV doesn't return a symbolic difference here, because the two pointers have no common base:
single_loop:
%pA.0 → {%dst,+,8}
%pB.0 → {(4 + %dst),+,8}
getPointerBase: %dst == %dst → -4
nested_loop:
%pA.1 → {%pA.0,+,8}
%pB.1 → {%pB.0,+,8}
getPointerBase: %pA.0 != %pB.0 → getCouldNotCompute()
The part that I think reframes this issue is, an outer-loop-carried pointer induction is fine, example: https://godbolt.org/z/Tvz9P67rd
// SCEV same pointer base
void nested_one_iv(const float* __restrict src, float* __restrict dst,
unsigned width, unsigned height, unsigned srcStride) {
float* pA = dst;
for (unsigned y = 0; y < height; y++) {
const float* row = src + y * srcStride;
for (unsigned x = 0; x < width; x++) {
pA[0] = row[x] * 2.0f;
pA[1] = row[x] * 3.0f;
pA += 2;
}
}
}
^ This vectorizes to st2.
st2 { v6.4s, v7.4s }, [x1]
...
st2 { v4.4s, v5.4s }, [x6]
Crucially, %pA.0 is still an opaque SCEVUnknown, so SCEV understands the outer loop no better than before. The only thing changed is the shared pointer base. In this case fix direction is probably canonicalize the IR (%pB.1 → gep(%pA.1, 1)) like suggested above? Other directions may include Teaching createAddRecFromPHI to build something like {%dst,+,8*width}<outer>... may not worth the effort.
P.S.: happy to draft a patch using the gep solution if we believe that's the correct solution : )
Thanks for taking the time to look into this @kuroyukiasuna I am looking for acceptable fix direction as well. Please feel free to add people who might be interested in this.
@Mel-Chen @lukel97 Please provide if you have any feedback on this. Ping @fhahn
@fhahn @david-arm What would be the preferred fix direction for this? Should the interleave group analysis be extended to handle this case (e.g., by looking through outer-loop PHIs to determine the constant offset), or should an earlier pass canonicalize the IR (something like.. the second pointer is expressed as a GEP off the first) or something different all together.
Please provide your thoughts.
Ping @fhahn @david-arm @Mel-Chen @luke957
@kartcq I attempted a fix in the direction of expressing second ptr as GEP of the first. The replace happens in IndVarSimplify, here's the PR: https://github.com/llvm/llvm-project/pull/215584
Proven to have the effect wanted:
running only -passes=indvars -stats
for.body4: ; preds = %for.body4, %for.body
%indvars.iv = phi i64 [ %indvars.iv.next, %for.body4 ], [ 0, %for.body ]
%pA.112 = phi ptr [ %pA.018, %for.body ], [ %add.ptr9, %for.body4 ]
%pB.111.off = getelementptr i8, ptr %pA.112, i64 4
...
1 indvars - Number of offset-congruent pointer IVs eliminated
running full opt with -O2
vector.body: ; preds = %vector.body, %vector.ph
%index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
%9 = shl i64 %index, 3
%next.gep = getelementptr i8, ptr %pA.018, i64 %9
%10 = getelementptr i8, ptr %pA.018, i64 %9
%next.gep10 = getelementptr i8, ptr %10, i64 32
%11 = getelementptr inbounds nuw [4 x i8], ptr %add.ptr1, i64 %index
%12 = getelementptr inbounds nuw i8, ptr %11, i64 16
%wide.load = load <4 x float>, ptr %11, align 4, !alias.scope !11
%wide.load11 = load <4 x float>, ptr %12, align 4, !alias.scope !11
%13 = fmul <4 x float> %wide.load, splat (float 2.000000e+00)
%14 = fmul <4 x float> %wide.load11, splat (float 2.000000e+00)
%15 = fmul <4 x float> %wide.load, splat (float 3.000000e+00)
%16 = fmul <4 x float> %wide.load11, splat (float 3.000000e+00)
%interleaved.vec = shufflevector <4 x float> %13, <4 x float> %15, <8 x i32> <i32 0, i32 4, i32 1, i32 5, i32 2, i32 6, i32 3, i32 7>
store <8 x float> %interleaved.vec, ptr %next.gep, align 4, !alias.scope !14, !noalias !11
%interleaved.vec12 = shufflevector <4 x float> %14, <4 x float> %16, <8 x i32> <i32 0, i32 4, i32 1, i32 5, i32 2, i32 6, i32 3, i32 7>
store <8 x float> %interleaved.vec12, ptr %next.gep10, align 4, !alias.scope !14, !noalias !11
%index.next = add nuw i64 %index, 8
%17 = icmp eq i64 %index.next, %n.vec
br i1 %17, label %middle.block, label %vector.body, !llvm.loop !16
assembly
.LBB0_7: // %vector.body
// Parent Loop BB0_4 Depth=1
// => This Inner Loop Header: Depth=2
ldp q2, q3, [x3, #-16]
sub x6, x1, #32
subs x5, x5, #8
add x3, x3, #32
fadd v6.4s, v3.4s, v3.4s
fadd v4.4s, v2.4s, v2.4s
fmul v7.4s, v3.4s, v0.4s
fmul v5.4s, v2.4s, v0.4s
st2 { v6.4s, v7.4s }, [x1]
add x1, x1, #64
st2 { v4.4s, v5.4s }, [x6]
b.ne .LBB0_7
Happy to discuss other directions if we don't like this one - let me know what you think, thanks!
nested_loop: %pA.1 → {%pA.0,+,8} %pB.1 → {%pB.0,+,8} getPointerBase: %pA.0 != %pB.0 → getCouldNotCompute()
This looks weird. Why is SCEV not looking through pA.0? Is something explicitly calling ScalarEvolution::getUnknown()?
@efriedma-quic yes, pA.0 is a SCEVUnknown created by an explicit getUnknown(PN) call: https://github.com/llvm/llvm-project/blob/dbde22497768e692c402e8b1c7ad1dfe63db5c69/llvm/lib/Analysis/ScalarEvolution.cpp#L6155
SCEV tried four recognizers, all declined, and the fallback interns it as an opaque symbol.
What does it look like with https://github.com/llvm/llvm-project/pull/215658 ?
Tried this against ~#211229~ #215658 (@efriedma-quic) (two pointer IVs in a nested loop, 4 bytes apart interleaved). The patch does form the AddRecs I need, but only when the SCEVs happen to be computed in program order first:
# cold
jerry@20:07:10 build (jerry/fix-211229) > ./bin/opt -passes='loop-vectorize' ./st2/st2_nested_pre_lv.ll -S -o - 2>/dev/null | grep shuffle
jerry@20:13:51 build (jerry/fix-211229) >
# warm, if print<scalar-evolution> runs before loop-vectorize
jerry@20:16:27 build (jerry/fix-211229) > ./bin/opt -passes='print<scalar-evolution>,loop-vectorize' ./st2/st2_nested_pre_lv.ll -S -o - 2>/dev/null | grep shuffle
%27 = shufflevector <4 x float> %25, <4 x float> %26, <8 x i32> <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7>
%interleaved.vec = shufflevector <8 x float> %27, <8 x float> poison, <8 x i32> <i32 0, i32 4, i32 1, i32 5, i32 2, i32 6, i32 3, i32 7>
st2_nested_pre_lv.ll:
; Function Attrs: mustprogress noinline nounwind uwtable
define dso_local void @_Z11nested_loopPKfPfjjj(ptr noalias noundef %src, ptr noalias noundef %dst, i32 noundef %width, i32 noundef %height, i32 noundef %srcStride) #0 {
entry:
%add.ptr = getelementptr inbounds float, ptr %dst, i64 1
br label %for.cond
for.cond: ; preds = %for.inc11, %entry
%pA.0 = phi ptr [ %dst, %entry ], [ %pA.1, %for.inc11 ]
%pB.0 = phi ptr [ %add.ptr, %entry ], [ %pB.1, %for.inc11 ]
%y.0 = phi i32 [ 0, %entry ], [ %inc12, %for.inc11 ]
%cmp = icmp ult i32 %y.0, %height
br i1 %cmp, label %for.body, label %for.end13
for.body: ; preds = %for.cond
%mul = mul i32 %y.0, %srcStride
%idx.ext = zext i32 %mul to i64
%add.ptr1 = getelementptr inbounds nuw float, ptr %src, i64 %idx.ext
br label %for.cond2
for.cond2: ; preds = %for.inc, %for.body
%pA.1 = phi ptr [ %pA.0, %for.body ], [ %add.ptr9, %for.inc ]
%pB.1 = phi ptr [ %pB.0, %for.body ], [ %add.ptr10, %for.inc ]
%x.0 = phi i32 [ 0, %for.body ], [ %inc, %for.inc ]
%cmp3 = icmp ult i32 %x.0, %width
br i1 %cmp3, label %for.body4, label %for.end
for.body4: ; preds = %for.cond2
%idxprom = zext i32 %x.0 to i64
%arrayidx = getelementptr inbounds nuw float, ptr %add.ptr1, i64 %idxprom
%0 = load float, ptr %arrayidx, align 4
%mul5 = fmul float %0, 2.000000e+00
store float %mul5, ptr %pA.1, align 4
%idxprom6 = zext i32 %x.0 to i64
%arrayidx7 = getelementptr inbounds nuw float, ptr %add.ptr1, i64 %idxprom6
%1 = load float, ptr %arrayidx7, align 4
%mul8 = fmul float %1, 3.000000e+00
store float %mul8, ptr %pB.1, align 4
%add.ptr9 = getelementptr inbounds float, ptr %pA.1, i64 2
%add.ptr10 = getelementptr inbounds float, ptr %pB.1, i64 2
br label %for.inc
for.inc: ; preds = %for.body4
%inc = add i32 %x.0, 1
br label %for.cond2, !llvm.loop !11
for.end: ; preds = %for.cond2
br label %for.inc11
for.inc11: ; preds = %for.end
%inc12 = add i32 %y.0, 1
br label %for.cond, !llvm.loop !13
for.end13: ; preds = %for.cond
ret void
}
https://github.com/llvm/llvm-project/pull/215658 and https://github.com/llvm/llvm-project/pull/215910 combined seems to work.
@efriedma-quic you are right! The two PRs combined has fixed the issue:
jerry@10:05:11 build (jerry/fix-211229) > ./bin/opt -passes='loop-vectorize' ./st2/st2_nested_pre_lv.ll -S -o - 2>/dev/null | grep shuffle
%27 = shufflevector <4 x float> %25, <4 x float> %26, <8 x i32> <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7>
%interleaved.vec = shufflevector <8 x float> %27, <8 x float> poison, <8 x i32> <i32 0, i32 4, i32 1, i32 5, i32 2, i32 6, i32 3, i32 7>
Fixing the SCEV itself is a much better approach. I'll close my PR.