[llvm] [LV] isScalarWithPredication to check for interleaved accesses (PR #194579)
Gaƫtan Bossu via llvm-commits
llvm-commits at lists.llvm.org
Tue Apr 28 02:52:56 PDT 2026
https://github.com/gbossu created https://github.com/llvm/llvm-project/pull/194579
isScalarWithPredication used to assume that predicated interleaved accesses were supported iff the target supports predicated gather/scatters.
The change is mostly NFC as targets enabling masked interleaved accesses (AArch64, RISCV) also enable gather/scatter.
However, there are subtle problems with isScalarWithPredication not considering masked interleaved ld/st:
- Instructions can get marked as isProfitableToScalarize (for fixed VFs) even though they will actually be widened because CM_Interleave takes precedence over isProfitableToScalarize. See tryToWidenMemory. This means we add the wrong cost: precomputeCosts adds the scalarization cost, whereas the widen recipe actually gets executed.
- Having any instruction return true for isScalarWithPredication means that its parent BB gets added to PredicatedBBsAfterVectorization. The consequence is that this disables if-conversion for scalable VFs: precomputeCosts() thinks the conditional branch is still there, which gives Invalid costs for scalable VFs.
>From deff88eae0d8fb555de0c0d21a19e816c0b8a9aa Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ga=C3=ABtan=20Bossu?= <gaetan.bossu at arm.com>
Date: Mon, 27 Apr 2026 12:03:51 +0000
Subject: [PATCH] [LV] isScalarWithPredication to check for interleaved
accesses
isScalarWithPredication used to assume that predicated interleaved
accesses were supported iff the target supports predicated
gather/scatters.
The change is mostly NFC as targets enabling masked interleaved accesses
(AArch64, RISCV) also enable gather/scatter.
However, there are subtle problems with isScalarWithPredication not considering masked interleaved ld/st:
- Instructions can get marked as isProfitableToScalarize (for fixed VFs) even though they will actually be widened because CM_Interleave takes precedence over isProfitableToScalarize. See tryToWidenMemory. This means we add the wrong cost: precomputeCosts adds the scalarization cost, whereas the widen recipe actually gets executed.
- Having any instruction trigger isScalarWithPredication means its parent BB gets added to PredicatedBBsAfterVectorization. The consequence is that this disables if-conversion for scalable VFs: precomputeCosts() thinks the conditional branch is still there, which gives Invalid costs for scalable VFs.
---
.../Vectorize/LoopVectorizationPlanner.cpp | 28 ++++++++++---------
.../Vectorize/LoopVectorizationPlanner.h | 16 ++++-------
.../Transforms/Vectorize/LoopVectorize.cpp | 26 +++++++++--------
.../CostModel/masked-interleaved-store-i16.ll | 6 ++--
4 files changed, 39 insertions(+), 37 deletions(-)
diff --git a/llvm/lib/Transforms/Vectorize/LoopVectorizationPlanner.cpp b/llvm/lib/Transforms/Vectorize/LoopVectorizationPlanner.cpp
index 0e847f4767a8b..c81e113309223 100644
--- a/llvm/lib/Transforms/Vectorize/LoopVectorizationPlanner.cpp
+++ b/llvm/lib/Transforms/Vectorize/LoopVectorizationPlanner.cpp
@@ -59,20 +59,22 @@ static cl::opt<bool> ForceTargetSupportsMaskedMemoryOps(
cl::desc("Assume the target supports masked memory operations (used for "
"testing)."));
-bool VFSelectionContext::isLegalMaskedStore(Type *DataType, Value *Ptr,
- Align Alignment,
- unsigned AddressSpace) const {
- return Legal->isConsecutivePtr(DataType, Ptr) &&
- (ForceTargetSupportsMaskedMemoryOps ||
- TTI.isLegalMaskedStore(DataType, Alignment, AddressSpace));
-}
+bool VFSelectionContext::isLegalMaskedLoadOrStore(Instruction *I,
+ ElementCount VF) const {
+ assert(isa<LoadInst>(I) || isa<StoreInst>(I));
+ auto *Ptr = getLoadStorePointerOperand(I);
+ auto *Ty = getLoadStoreType(I);
+ const unsigned AS = getLoadStoreAddressSpace(I);
+ const Align Alignment = getLoadStoreAlignment(I);
+
+ bool LegalAccessPattern =
+ Legal->isConsecutivePtr(Ty, Ptr) || IAI.isInterleaved(I);
+ if (!LegalAccessPattern)
+ return false;
-bool VFSelectionContext::isLegalMaskedLoad(Type *DataType, Value *Ptr,
- Align Alignment,
- unsigned AddressSpace) const {
- return Legal->isConsecutivePtr(DataType, Ptr) &&
- (ForceTargetSupportsMaskedMemoryOps ||
- TTI.isLegalMaskedLoad(DataType, Alignment, AddressSpace));
+ return ForceTargetSupportsMaskedMemoryOps ||
+ (isa<LoadInst>(I) ? TTI.isLegalMaskedLoad(Ty, Alignment, AS)
+ : TTI.isLegalMaskedStore(Ty, Alignment, AS));
}
bool VFSelectionContext::isLegalGatherOrScatter(Value *V,
diff --git a/llvm/lib/Transforms/Vectorize/LoopVectorizationPlanner.h b/llvm/lib/Transforms/Vectorize/LoopVectorizationPlanner.h
index b07a17f2d8baa..c4bf13d6e1cbc 100644
--- a/llvm/lib/Transforms/Vectorize/LoopVectorizationPlanner.h
+++ b/llvm/lib/Transforms/Vectorize/LoopVectorizationPlanner.h
@@ -555,6 +555,7 @@ class VFSelectionContext {
PredicatedScalarEvolution &PSE;
OptimizationRemarkEmitter *ORE;
const LoopVectorizeHints *Hints;
+ const InterleavedAccessInfo &IAI;
/// Cached result of isScalableVectorizationAllowed.
std::optional<bool> IsScalableVectorizationAllowed;
@@ -599,9 +600,10 @@ class VFSelectionContext {
const Loop *TheLoop, const Function &F,
PredicatedScalarEvolution &PSE,
OptimizationRemarkEmitter *ORE,
- const LoopVectorizeHints *Hints, bool OptForSize)
+ const LoopVectorizeHints *Hints,
+ const InterleavedAccessInfo &IAI, bool OptForSize)
: TTI(TTI), Legal(Legal), TheLoop(TheLoop), F(F), PSE(PSE), ORE(ORE),
- Hints(Hints),
+ Hints(Hints), IAI(IAI),
CostKind(F.hasMinSize() ? TTI::TCK_CodeSize : TTI::TCK_RecipThroughput),
OptForSize(OptForSize) {
initializeVScaleForTuning();
@@ -650,15 +652,9 @@ class VFSelectionContext {
/// of FP operations.
bool useOrderedReductions(const RecurrenceDescriptor &RdxDesc) const;
- /// Returns true if the target machine supports masked store operation
+ /// Returns true if the target machine supports masked load or store operation
/// for the given \p DataType and kind of access to \p Ptr.
- bool isLegalMaskedStore(Type *DataType, Value *Ptr, Align Alignment,
- unsigned AddressSpace) const;
-
- /// Returns true if the target machine supports masked load operation
- /// for the given \p DataType and kind of access to \p Ptr.
- bool isLegalMaskedLoad(Type *DataType, Value *Ptr, Align Alignment,
- unsigned AddressSpace) const;
+ bool isLegalMaskedLoadOrStore(Instruction *I, ElementCount VF) const;
/// Returns true if the target machine can represent \p V as a masked gather
/// or scatter operation.
diff --git a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
index 6e7a17c8b59d5..c5a45a9e9ca3a 100644
--- a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
+++ b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
@@ -2465,18 +2465,18 @@ bool LoopVectorizationCostModel::isScalarWithPredication(Instruction *I,
return getCallWideningDecision(cast<CallInst>(I), VF).Kind == CM_Scalarize;
case Instruction::Load:
case Instruction::Store: {
+ // We can use a masked ld/st for consecutive accesses or those that have
+ // been decided to use interleaving.
+ LoopVectorizationCostModel::InstWidening WidenKind =
+ getWideningDecision(I, VF);
auto *Ptr = getLoadStorePointerOperand(I);
auto *Ty = getLoadStoreType(I);
- unsigned AS = getLoadStoreAddressSpace(I);
- Type *VTy = Ty;
- if (VF.isVector())
- VTy = VectorType::get(Ty, VF);
- const Align Alignment = getLoadStoreAlignment(I);
- return isa<LoadInst>(I)
- ? !(Config.isLegalMaskedLoad(Ty, Ptr, Alignment, AS) ||
- TTI.isLegalMaskedGather(VTy, Alignment))
- : !(Config.isLegalMaskedStore(Ty, Ptr, Alignment, AS) ||
- TTI.isLegalMaskedScatter(VTy, Alignment));
+ bool CanUseLdSt =
+ Legal->isConsecutivePtr(Ty, Ptr) || WidenKind == CM_Interleave;
+ // TODO: It might actually make sense to take InstWidening out of
+ // LoopVectorizationCostModel and just pass it to isLegalMaskedLoadOrStore?
+ return !(CanUseLdSt && Config.isLegalMaskedLoadOrStore(I, VF)) &&
+ !Config.isLegalGatherOrScatter(I, VF);
}
case Instruction::UDiv:
case Instruction::SDiv:
@@ -7582,7 +7582,8 @@ static bool processLoopInVPlanNativePath(
EpilogueLowering SEL =
getEpilogueLowering(F, L, Hints, OptForSize, TTI, TLI, *LVL, &IAI);
- VFSelectionContext Config(*TTI, LVL, L, *F, PSE, ORE, &Hints, OptForSize);
+ VFSelectionContext Config(*TTI, LVL, L, *F, PSE, ORE, &Hints, IAI,
+ OptForSize);
LoopVectorizationCostModel CM(SEL, L, PSE, LI, LVL, *TTI, TLI, DB, AC, ORE,
GetBFI, F, &Hints, IAI, Config);
// Use the planner for outer loop vectorization.
@@ -8415,7 +8416,8 @@ bool LoopVectorizePass::processLoop(Loop *L) {
}
// Use the cost model.
- VFSelectionContext Config(*TTI, &LVL, L, *F, PSE, ORE, &Hints, OptForSize);
+ VFSelectionContext Config(*TTI, &LVL, L, *F, PSE, ORE, &Hints, IAI,
+ OptForSize);
LoopVectorizationCostModel CM(SEL, L, PSE, LI, &LVL, *TTI, TLI, DB, AC, ORE,
GetBFI, F, &Hints, IAI, Config);
// Use the planner for vectorization.
diff --git a/llvm/test/Transforms/LoopVectorize/X86/CostModel/masked-interleaved-store-i16.ll b/llvm/test/Transforms/LoopVectorize/X86/CostModel/masked-interleaved-store-i16.ll
index 398f553059297..14a68cef22e2d 100644
--- a/llvm/test/Transforms/LoopVectorize/X86/CostModel/masked-interleaved-store-i16.ll
+++ b/llvm/test/Transforms/LoopVectorize/X86/CostModel/masked-interleaved-store-i16.ll
@@ -161,8 +161,10 @@ define void @test(ptr noalias nocapture %points, ptr noalias nocapture readonly
; ENABLED_MASKED_STRIDED: LV: Found an estimated cost of 1 for VF 1 For instruction: store i16 %0, ptr %arrayidx6, align 2
; ENABLED_MASKED_STRIDED: Cost of 2 for VF 2: profitable to scalarize store i16 %0, ptr %arrayidx6, align 2
; ENABLED_MASKED_STRIDED: Cost of 4 for VF 4: profitable to scalarize store i16 %0, ptr %arrayidx6, align 2
-; ENABLED_MASKED_STRIDED: Cost of 8 for VF 8: profitable to scalarize store i16 %0, ptr %arrayidx6, align 2
-; ENABLED_MASKED_STRIDED: Cost of 16 for VF 16: profitable to scalarize store i16 %0, ptr %arrayidx6, align 2
+; ENABLED_MASKED_STRIDED: Cost of 12 for VF 8: INTERLEAVE-GROUP with factor 3 at <badref>, ir<%arrayidx6>, ir<%cmp1>
+; ENABLED_MASKED_STRIDED: store ir<%0> to index 0
+; ENABLED_MASKED_STRIDED: Cost of 22 for VF 16: INTERLEAVE-GROUP with factor 3 at <badref>, ir<%arrayidx6>, ir<%cmp1>
+; ENABLED_MASKED_STRIDED: store ir<%0> to index 0
;
entry:
br label %for.body
More information about the llvm-commits
mailing list