[llvm] [LV] Only collect interleave strides without predicates under OptForSize (PR #205793)
via llvm-commits
llvm-commits at lists.llvm.org
Thu Jun 25 05:02:50 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-transforms
Author: Mel Chen (Mel-Chen)
<details>
<summary>Changes</summary>
During interleaved access analysis, certain addresses require a no-wrap predicate to form an add recurrence and obtain the stride. However, when optimizing for size, generating SCEV runtime checks is disallowed.
This patch modifies the constant stride collection when optimizing for size to only collect strides that do not require predicates. This ensures that vectorization will not blocked by disallowed predicates.
---
Full diff: https://github.com/llvm/llvm-project/pull/205793.diff
4 Files Affected:
- (modified) llvm/include/llvm/Analysis/VectorUtils.h (+6-4)
- (modified) llvm/lib/Analysis/VectorUtils.cpp (+4-3)
- (modified) llvm/lib/Transforms/Vectorize/LoopVectorize.cpp (+1-1)
- (modified) llvm/test/Transforms/LoopVectorize/AArch64/discarded-interleave-group.ll (+27-14)
``````````diff
diff --git a/llvm/include/llvm/Analysis/VectorUtils.h b/llvm/include/llvm/Analysis/VectorUtils.h
index e088371be9464..f70ae3ff57f88 100644
--- a/llvm/include/llvm/Analysis/VectorUtils.h
+++ b/llvm/include/llvm/Analysis/VectorUtils.h
@@ -665,8 +665,9 @@ class InterleavedAccessInfo {
public:
InterleavedAccessInfo(PredicatedScalarEvolution &PSE, Loop *L,
DominatorTree *DT, LoopInfo *LI,
- const LoopAccessInfo *LAI)
- : PSE(PSE), TheLoop(L), DT(DT), LI(LI), LAI(LAI) {}
+ const LoopAccessInfo *LAI, bool OptForSize)
+ : PSE(PSE), TheLoop(L), DT(DT), LI(LI), LAI(LAI), OptForSize(OptForSize) {
+ }
~InterleavedAccessInfo() { invalidateGroups(); }
@@ -738,6 +739,7 @@ class InterleavedAccessInfo {
DominatorTree *DT;
LoopInfo *LI;
const LoopAccessInfo *LAI;
+ bool OptForSize;
/// True if the loop may contain non-reversed interleaved groups with
/// out-of-bounds accesses. We ensure we don't speculatively access memory
@@ -807,11 +809,11 @@ class InterleavedAccessInfo {
/// Collect all the accesses with a constant stride in program order. Any
/// SCEV predicates needed to compute the strides are added to \p
- /// Predicates.
+ /// Predicates if it is not nullptr.
void collectConstStrideAccesses(
MapVector<Instruction *, StrideDescriptor> &AccessStrideInfo,
const DenseMap<Value *, const SCEV *> &Strides,
- SmallVectorImpl<const SCEVPredicate *> &Predicates);
+ SmallVectorImpl<const SCEVPredicate *> *Predicates);
/// Returns true if \p Stride is allowed in an interleaved group.
LLVM_ABI static bool isStrided(int Stride);
diff --git a/llvm/lib/Analysis/VectorUtils.cpp b/llvm/lib/Analysis/VectorUtils.cpp
index 884229e09c6be..bcdb6df47d757 100644
--- a/llvm/lib/Analysis/VectorUtils.cpp
+++ b/llvm/lib/Analysis/VectorUtils.cpp
@@ -1310,7 +1310,7 @@ bool InterleavedAccessInfo::isStrided(int Stride) {
void InterleavedAccessInfo::collectConstStrideAccesses(
MapVector<Instruction *, StrideDescriptor> &AccessStrideInfo,
const DenseMap<Value *, const SCEV *> &Strides,
- SmallVectorImpl<const SCEVPredicate *> &Predicates) {
+ SmallVectorImpl<const SCEVPredicate *> *Predicates) {
auto &DL = TheLoop->getHeader()->getDataLayout();
// Since it's desired that the load/store instructions be maintained in
@@ -1342,7 +1342,7 @@ void InterleavedAccessInfo::collectConstStrideAccesses(
// even without the transformation. The wrapping checks are therefore
// deferred until after we've formed the interleaved groups.
int64_t Stride = getPtrStride(PSE, ElementTy, Ptr, TheLoop, *DT, Strides,
- /*ShouldCheckWrap=*/false, &Predicates)
+ /*ShouldCheckWrap=*/false, Predicates)
.value_or(0);
const SCEV *Scev = replaceSymbolicStrideSCEV(PSE, Strides, Ptr);
@@ -1395,7 +1395,8 @@ void InterleavedAccessInfo::analyzeInterleaving(
// Holds all accesses with a constant stride.
MapVector<Instruction *, StrideDescriptor> AccessStrideInfo;
SmallVector<const SCEVPredicate *> Predicates;
- collectConstStrideAccesses(AccessStrideInfo, Strides, Predicates);
+ collectConstStrideAccesses(AccessStrideInfo, Strides,
+ OptForSize ? nullptr : &Predicates);
if (AccessStrideInfo.empty())
return;
diff --git a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
index 545fcba65d228..0b9a1e781e169 100644
--- a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
+++ b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
@@ -7924,7 +7924,7 @@ bool LoopVectorizePass::processLoop(Loop *L) {
}
}
- InterleavedAccessInfo IAI(PSE, L, DT, LI, LVL.getLAI());
+ InterleavedAccessInfo IAI(PSE, L, DT, LI, LVL.getLAI(), OptForSize);
bool UseInterleaved =
IsInnerLoop && TTI->enableInterleavedAccessVectorization();
diff --git a/llvm/test/Transforms/LoopVectorize/AArch64/discarded-interleave-group.ll b/llvm/test/Transforms/LoopVectorize/AArch64/discarded-interleave-group.ll
index 1c79629ac1754..1b5a7ced7ca03 100644
--- a/llvm/test/Transforms/LoopVectorize/AArch64/discarded-interleave-group.ll
+++ b/llvm/test/Transforms/LoopVectorize/AArch64/discarded-interleave-group.ll
@@ -65,28 +65,41 @@ exit:
}
; The stride-2 interleave group on %B survives, no predicate should be needed.
-; FIXME: only predicates required by surviving group members should be added.
define void @over_conservative_merge(ptr noalias %A, ptr noalias %B, i64 %N) #0 {
; CHECK-LABEL: define void @over_conservative_merge(
; CHECK-SAME: ptr noalias [[A:%.*]], ptr noalias [[B:%.*]], i64 [[N:%.*]]) #[[ATTR0]] {
-; CHECK-NEXT: [[ENTRY:.*]]:
+; CHECK-NEXT: [[ENTRY:.*:]]
; CHECK-NEXT: br label %[[LOOP:.*]]
; CHECK: [[LOOP]]:
-; CHECK-NEXT: [[INDEX:%.*]] = phi i64 [ 0, %[[ENTRY]] ], [ [[IV_NEXT:%.*]], %[[LOOP]] ]
-; CHECK-NEXT: [[CLAMPED:%.*]] = urem i64 [[INDEX]], 4
-; CHECK-NEXT: [[GEP_A:%.*]] = getelementptr float, ptr [[A]], i64 [[CLAMPED]]
-; CHECK-NEXT: [[LA:%.*]] = load float, ptr [[GEP_A]], align 4
-; CHECK-NEXT: [[M7:%.*]] = fmul float [[LA]], [[LA]]
+; CHECK-NEXT: [[TMP0:%.*]] = call i64 @llvm.vscale.i64()
+; CHECK-NEXT: [[TMP1:%.*]] = shl nuw i64 [[TMP0]], 2
+; CHECK-NEXT: [[ACTIVE_LANE_MASK_ENTRY:%.*]] = call <vscale x 4 x i1> @llvm.get.active.lane.mask.nxv4i1.i64(i64 0, i64 [[N]])
+; CHECK-NEXT: [[TMP2:%.*]] = call <vscale x 4 x i64> @llvm.stepvector.nxv4i64()
+; CHECK-NEXT: [[BROADCAST_SPLATINSERT:%.*]] = insertelement <vscale x 4 x i64> poison, i64 [[TMP1]], i64 0
+; CHECK-NEXT: [[BROADCAST_SPLAT:%.*]] = shufflevector <vscale x 4 x i64> [[BROADCAST_SPLATINSERT]], <vscale x 4 x i64> poison, <vscale x 4 x i32> zeroinitializer
+; CHECK-NEXT: br label %[[VECTOR_BODY:.*]]
+; CHECK: [[VECTOR_BODY]]:
+; CHECK-NEXT: [[INDEX:%.*]] = phi i64 [ 0, %[[LOOP]] ], [ [[INDEX_NEXT:%.*]], %[[VECTOR_BODY]] ]
+; CHECK-NEXT: [[ACTIVE_LANE_MASK:%.*]] = phi <vscale x 4 x i1> [ [[ACTIVE_LANE_MASK_ENTRY]], %[[LOOP]] ], [ [[ACTIVE_LANE_MASK_NEXT:%.*]], %[[VECTOR_BODY]] ]
+; CHECK-NEXT: [[VEC_IND:%.*]] = phi <vscale x 4 x i64> [ [[TMP2]], %[[LOOP]] ], [ [[VEC_IND_NEXT:%.*]], %[[VECTOR_BODY]] ]
+; CHECK-NEXT: [[TMP3:%.*]] = urem <vscale x 4 x i64> [[VEC_IND]], splat (i64 4)
+; CHECK-NEXT: [[WIDE_GEP:%.*]] = getelementptr float, ptr [[A]], <vscale x 4 x i64> [[TMP3]]
+; CHECK-NEXT: [[WIDE_MASKED_GATHER:%.*]] = call <vscale x 4 x float> @llvm.masked.gather.nxv4f32.nxv4p0(<vscale x 4 x ptr> align 4 [[WIDE_GEP]], <vscale x 4 x i1> [[ACTIVE_LANE_MASK]], <vscale x 4 x float> poison)
+; CHECK-NEXT: [[TMP4:%.*]] = fmul <vscale x 4 x float> [[WIDE_MASKED_GATHER]], [[WIDE_MASKED_GATHER]]
; CHECK-NEXT: [[TMP12:%.*]] = shl nuw nsw i64 [[INDEX]], 1
; CHECK-NEXT: [[TMP13:%.*]] = getelementptr inbounds float, ptr [[B]], i64 [[TMP12]]
-; CHECK-NEXT: store float [[M7]], ptr [[TMP13]], align 4
-; CHECK-NEXT: [[IDX1:%.*]] = add nuw nsw i64 [[TMP12]], 1
-; CHECK-NEXT: [[GEP_B1:%.*]] = getelementptr inbounds float, ptr [[B]], i64 [[IDX1]]
-; CHECK-NEXT: store float [[M7]], ptr [[GEP_B1]], align 4
-; CHECK-NEXT: [[IV_NEXT]] = add nuw nsw i64 [[INDEX]], 1
-; CHECK-NEXT: [[COND:%.*]] = icmp eq i64 [[IV_NEXT]], [[N]]
-; CHECK-NEXT: br i1 [[COND]], label %[[EXIT:.*]], label %[[LOOP]]
+; CHECK-NEXT: [[INTERLEAVED_VEC:%.*]] = call <vscale x 8 x float> @llvm.vector.interleave2.nxv8f32(<vscale x 4 x float> [[TMP4]], <vscale x 4 x float> [[TMP4]])
+; CHECK-NEXT: [[INTERLEAVED_MASK:%.*]] = call <vscale x 8 x i1> @llvm.vector.interleave2.nxv8i1(<vscale x 4 x i1> [[ACTIVE_LANE_MASK]], <vscale x 4 x i1> [[ACTIVE_LANE_MASK]])
+; CHECK-NEXT: call void @llvm.masked.store.nxv8f32.p0(<vscale x 8 x float> [[INTERLEAVED_VEC]], ptr align 4 [[TMP13]], <vscale x 8 x i1> [[INTERLEAVED_MASK]])
+; CHECK-NEXT: [[INDEX_NEXT]] = add i64 [[INDEX]], [[TMP1]]
+; CHECK-NEXT: [[ACTIVE_LANE_MASK_NEXT]] = call <vscale x 4 x i1> @llvm.get.active.lane.mask.nxv4i1.i64(i64 [[INDEX_NEXT]], i64 [[N]])
+; CHECK-NEXT: [[TMP7:%.*]] = extractelement <vscale x 4 x i1> [[ACTIVE_LANE_MASK_NEXT]], i64 0
+; CHECK-NEXT: [[TMP8:%.*]] = xor i1 [[TMP7]], true
+; CHECK-NEXT: [[VEC_IND_NEXT]] = add nuw nsw <vscale x 4 x i64> [[VEC_IND]], [[BROADCAST_SPLAT]]
+; CHECK-NEXT: br i1 [[TMP8]], label %[[EXIT:.*]], label %[[VECTOR_BODY]], !llvm.loop [[LOOP3:![0-9]+]]
; CHECK: [[EXIT]]:
+; CHECK-NEXT: br label %[[EXIT1:.*]]
+; CHECK: [[EXIT1]]:
; CHECK-NEXT: ret void
;
entry:
``````````
</details>
https://github.com/llvm/llvm-project/pull/205793
More information about the llvm-commits
mailing list