[llvm] [VPlan] Handle loads feeding memops in makeMemOpWideningDecisions (NFC) (PR #196842)
Florian Hahn via llvm-commits
llvm-commits at lists.llvm.org
Tue Jun 23 12:59:01 PDT 2026
https://github.com/fhahn updated https://github.com/llvm/llvm-project/pull/196842
>From 0b025b35318518cb265a0006095ca176be8aef64 Mon Sep 17 00:00:00 2001
From: Florian Hahn <flo at fhahn.com>
Date: Fri, 1 May 2026 20:49:36 +0100
Subject: [PATCH 1/2] [VPlan] Handle loads feeding memops in
makeMemOpWideningDecisions (NFC)
The legacy cost model force-scalarizes loads feeding memory ops, unless
the target prefers vector addressing. Add the logic to
makeMemOpWideningDecisions. Currently this should match the legacy
decision as-is. Should be test-able in VPlan-printing test if legacy
decisions are done in a separate sub-pass as
https://github.com/llvm/llvm-project/pull/182593
---
.../Transforms/Vectorize/LoopVectorize.cpp | 10 ++++++-
.../Transforms/Vectorize/VPRecipeBuilder.h | 7 +++++
.../Transforms/Vectorize/VPlanTransforms.cpp | 30 +++++++++++++++++--
.../Transforms/Vectorize/VPlanTransforms.h | 4 ++-
llvm/lib/Transforms/Vectorize/VPlanUtils.cpp | 16 +++++++---
.../AArch64/vplan-memory-op-decisions.ll | 6 ++--
6 files changed, 62 insertions(+), 11 deletions(-)
diff --git a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
index 3dbee08e7d7d8..780230ebfae53 100644
--- a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
+++ b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
@@ -6117,6 +6117,14 @@ void EpilogueVectorizerEpilogueLoop::printDebugTracesAtEnd() {
});
}
+bool VPRecipeBuilder::isPredicatedInst(Instruction *I) const {
+ return CM.isPredicatedInst(I);
+}
+
+bool VPRecipeBuilder::prefersVectorizedAddressing() const {
+ return CM.TTI.prefersVectorizedAddressing();
+}
+
VPRecipeBase *VPRecipeBuilder::tryToWidenMemory(VPInstruction *VPI,
VFRange &Range) {
assert((VPI->getOpcode() == Instruction::Load ||
@@ -6711,7 +6719,7 @@ VPlanPtr LoopVectorizationPlanner::tryToBuildVPlan(VPlanPtr Plan,
OrigLoop);
RUN_VPLAN_PASS(VPlanTransforms::makeMemOpWideningDecisions, *Plan, Range,
- RecipeBuilder);
+ RecipeBuilder, CM.PSE, OrigLoop);
RUN_VPLAN_PASS(VPlanTransforms::makeScalarizationDecisions, *Plan, Range);
diff --git a/llvm/lib/Transforms/Vectorize/VPRecipeBuilder.h b/llvm/lib/Transforms/Vectorize/VPRecipeBuilder.h
index 8296d058b71c3..9a31c182bdd4b 100644
--- a/llvm/lib/Transforms/Vectorize/VPRecipeBuilder.h
+++ b/llvm/lib/Transforms/Vectorize/VPRecipeBuilder.h
@@ -53,6 +53,13 @@ class VPRecipeBuilder {
LoopVectorizationCostModel &CM, VPBuilder &Builder)
: Plan(Plan), Legal(Legal), CM(CM), Builder(Builder) {}
+ /// Returns true if \p I needs to be predicated (i.e. cannot be executed
+ /// unconditionally for all lanes) in the loop being vectorized.
+ bool isPredicatedInst(Instruction *I) const;
+
+ /// Returns true if the target prefers vectorized addressing.
+ bool prefersVectorizedAddressing() const;
+
/// Create and return a widened recipe for a non-phi recipe \p R if one can be
/// created within the given VF \p Range.
VPRecipeBase *tryToCreateWidenNonPhiRecipe(VPSingleDefRecipe *R,
diff --git a/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp b/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
index adcfe30ff9561..a4da68598212c 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
@@ -7136,8 +7136,10 @@ void VPlanTransforms::createPartialReductions(VPlan &Plan,
transformToPartialReduction(Chain, Plan, Phi);
}
-void VPlanTransforms::makeMemOpWideningDecisions(
- VPlan &Plan, VFRange &Range, VPRecipeBuilder &RecipeBuilder) {
+void VPlanTransforms::makeMemOpWideningDecisions(VPlan &Plan, VFRange &Range,
+ VPRecipeBuilder &RecipeBuilder,
+ PredicatedScalarEvolution &PSE,
+ const Loop *L) {
// Collect all loads/stores first. We will start with ones having simpler
// decisions followed by more complex ones that are potentially
// guided/dependent on the simpler ones.
@@ -7219,6 +7221,30 @@ void VPlanTransforms::makeMemOpWideningDecisions(
return false;
});
+ if (!RecipeBuilder.prefersVectorizedAddressing()) {
+ VPlanTransforms::runPass(
+ "makeVPlanMemOpDecision", ProcessSubset, Plan, [&](VPInstruction *VPI) {
+ Instruction *I = VPI->getUnderlyingInstr();
+ bool IsLoad = VPI->getOpcode() == Instruction::Load;
+ if (RecipeBuilder.isPredicatedInst(I) || !IsLoad ||
+ !vputils::isUsedByLoadStoreAddress(VPI))
+ return false;
+
+ // Scalarize loads used as addresses, matching the legacy CM. The load
+ // is single-scalar if the pointer is loop-invariant, otherwise it is
+ // replicated per-lane. No mask is needed as the load is not
+ // predicated.
+ VPValue *Ptr = VPI->getOperand(0);
+ const SCEV *PtrSCEV = vputils::getSCEVExprForVPValue(Ptr, PSE, L);
+ bool IsSingleScalarLoad = !isa<SCEVCouldNotCompute>(PtrSCEV) &&
+ PSE.getSE()->isLoopInvariant(PtrSCEV, L);
+ ReplaceWith(VPI, new VPReplicateRecipe(
+ I, {Ptr}, /*IsSingleScalar=*/IsSingleScalarLoad,
+ /*Mask=*/nullptr, {}, *VPI, VPI->getDebugLoc()));
+ return true;
+ });
+ }
+
VPlanTransforms::runPass("delegateMemOpWideningToLegacyCM", ProcessSubset,
Plan, [&](VPInstruction *VPI) {
if (VPRecipeBase *Recipe =
diff --git a/llvm/lib/Transforms/Vectorize/VPlanTransforms.h b/llvm/lib/Transforms/Vectorize/VPlanTransforms.h
index 8948d92884661..8fbf6793e90c5 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanTransforms.h
+++ b/llvm/lib/Transforms/Vectorize/VPlanTransforms.h
@@ -584,7 +584,9 @@ struct VPlanTransforms {
/// Convert load/store VPInstructions in \p Plan into widened or replicate
/// recipes. Non load/store input instructions are left unchanged.
static void makeMemOpWideningDecisions(VPlan &Plan, VFRange &Range,
- VPRecipeBuilder &RecipeBuilder);
+ VPRecipeBuilder &RecipeBuilder,
+ PredicatedScalarEvolution &PSE,
+ const Loop *L);
/// Make VPlan-based scalarization decision prior to delegating to the ones
/// made by the legacy CM. Only transforms "usesFirstLaneOnly` def-use chains
diff --git a/llvm/lib/Transforms/Vectorize/VPlanUtils.cpp b/llvm/lib/Transforms/Vectorize/VPlanUtils.cpp
index 7e0f536a63a83..8a3567691ef51 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanUtils.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanUtils.cpp
@@ -790,10 +790,18 @@ bool vputils::isUsedByLoadStoreAddress(const VPValue *V) {
continue;
// Only traverse further through users that also define a value (and can
- // thus have their own users walked).
- for (VPUser *U : Cur->users())
- if (auto *SDR = dyn_cast<VPSingleDefRecipe>(U))
- WorkList.push_back(SDR);
+ // thus have their own users walked). Skip load VPInstructions: a loaded
+ // value does not depend on the load's operands, so reaching a load here
+ // (only possible via its mask operand) would be a false positive.
+ for (VPUser *U : Cur->users()) {
+ auto *SDR = dyn_cast<VPSingleDefRecipe>(U);
+ if (!SDR)
+ continue;
+ auto *VPI = dyn_cast<VPInstruction>(U);
+ if (VPI && VPI->getOpcode() == Instruction::Load)
+ continue;
+ WorkList.push_back(SDR);
+ }
}
return false;
}
diff --git a/llvm/test/Transforms/LoopVectorize/VPlan/AArch64/vplan-memory-op-decisions.ll b/llvm/test/Transforms/LoopVectorize/VPlan/AArch64/vplan-memory-op-decisions.ll
index 7e7f66ad446f2..70a02c048cf4b 100644
--- a/llvm/test/Transforms/LoopVectorize/VPlan/AArch64/vplan-memory-op-decisions.ll
+++ b/llvm/test/Transforms/LoopVectorize/VPlan/AArch64/vplan-memory-op-decisions.ll
@@ -1,5 +1,5 @@
; NOTE: Assertions have been autogenerated by utils/update_analyze_test_checks.py UTC_ARGS: --filter-out-after "middle.block:" --version 6
-; RUN: opt -passes=loop-vectorize -force-vector-width=2 -force-vector-interleave=1 -S -disable-output -vplan-print-after=scalarizeMemOpsWithIrregularTypes %s 2>&1 | FileCheck %s
+; RUN: opt -passes=loop-vectorize -force-vector-width=2 -force-vector-interleave=1 -S -disable-output -vplan-print-after=makeVPlanMemOpDecision %s 2>&1 | FileCheck %s
target triple = "arm64-apple-macosx"
@@ -24,7 +24,7 @@ define void @replicating_load_used_as_store_addr(ptr noalias %A) {
; CHECK-NEXT: ir<%iv> = WIDEN-INDUCTION ir<0>, ir<1>, vp<[[VP0]]>
; CHECK-NEXT: EMIT ir<%iv.next> = add ir<%iv>, ir<1>
; CHECK-NEXT: EMIT ir<%gep.A> = getelementptr ir<%A>, ir<%iv>
-; CHECK-NEXT: EMIT-SCALAR ir<%l.p> = load ir<%gep.A>
+; CHECK-NEXT: REPLICATE ir<%l.p> = load ir<%gep.A>
; CHECK-NEXT: EMIT-SCALAR ir<%iv.trunc> = trunc ir<%iv.next> to i32
; CHECK-NEXT: EMIT store ir<%iv.trunc>, ir<%l.p>
; CHECK-NEXT: EMIT ir<%ec> = icmp eq ir<%iv>, ir<100>
@@ -143,7 +143,7 @@ define void @single_scalar_load_used_as_store_addr(ptr noalias %p) {
; CHECK-NEXT: vector.body:
; CHECK-NEXT: ir<%iv> = WIDEN-INDUCTION ir<0>, ir<1>, vp<[[VP0]]>
; CHECK-NEXT: EMIT ir<%iv.next> = add ir<%iv>, ir<1>
-; CHECK-NEXT: EMIT-SCALAR ir<%l.p> = load ir<%p>
+; CHECK-NEXT: CLONE ir<%l.p> = load ir<%p>
; CHECK-NEXT: EMIT ir<%gep> = getelementptr ir<%l.p>, ir<%iv>
; CHECK-NEXT: EMIT-SCALAR ir<%iv.trunc> = trunc ir<%iv.next> to i32
; CHECK-NEXT: EMIT store ir<%iv.trunc>, ir<%gep>
>From 2507bef5a7c7931f0d8afe416b83e1843d166fb0 Mon Sep 17 00:00:00 2001
From: Florian Hahn <flo at fhahn.com>
Date: Tue, 23 Jun 2026 21:58:28 +0200
Subject: [PATCH 2/2] !fixup simplify loop in isUsedByLoadStoreAddress.
---
llvm/lib/Transforms/Vectorize/VPlanUtils.cpp | 9 +++------
1 file changed, 3 insertions(+), 6 deletions(-)
diff --git a/llvm/lib/Transforms/Vectorize/VPlanUtils.cpp b/llvm/lib/Transforms/Vectorize/VPlanUtils.cpp
index 8a3567691ef51..f987d6bd1a9e2 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanUtils.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanUtils.cpp
@@ -794,13 +794,10 @@ bool vputils::isUsedByLoadStoreAddress(const VPValue *V) {
// value does not depend on the load's operands, so reaching a load here
// (only possible via its mask operand) would be a false positive.
for (VPUser *U : Cur->users()) {
- auto *SDR = dyn_cast<VPSingleDefRecipe>(U);
- if (!SDR)
+ if (match(U, m_VPInstruction<Instruction::Load>()))
continue;
- auto *VPI = dyn_cast<VPInstruction>(U);
- if (VPI && VPI->getOpcode() == Instruction::Load)
- continue;
- WorkList.push_back(SDR);
+ if (auto *SDR = dyn_cast<VPSingleDefRecipe>(U))
+ WorkList.push_back(SDR);
}
}
return false;
More information about the llvm-commits
mailing list