[llvm] [VPlan] Introduce vputils::mustReplicate (NFC) (PR #199047)
Ramkumar Ramachandra via llvm-commits
llvm-commits at lists.llvm.org
Mon Jun 15 05:42:10 PDT 2026
https://github.com/artagnon updated https://github.com/llvm/llvm-project/pull/199047
>From 849dbf978ae164e85df96bdc6b0cc788a61f65af Mon Sep 17 00:00:00 2001
From: Ramkumar Ramachandra <artagnon at tenstorrent.com>
Date: Thu, 21 May 2026 10:42:46 -0400
Subject: [PATCH 1/2] [VPlan] Introduce vputils::mustReplicate (NFC)
Introduce a query for recipes that must replicate for each part,
that returns true for non-single-scalar replicates, SIVSteps, and a
couple of VPInstructions.
---
llvm/lib/Transforms/Vectorize/VPlan.h | 16 +++++++++-------
llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp | 7 ++++---
.../lib/Transforms/Vectorize/VPlanTransforms.cpp | 9 ++-------
llvm/lib/Transforms/Vectorize/VPlanUnroll.cpp | 7 +------
llvm/lib/Transforms/Vectorize/VPlanUtils.cpp | 8 ++++++++
llvm/lib/Transforms/Vectorize/VPlanUtils.h | 4 ++++
6 files changed, 28 insertions(+), 23 deletions(-)
diff --git a/llvm/lib/Transforms/Vectorize/VPlan.h b/llvm/lib/Transforms/Vectorize/VPlan.h
index 71eeb02a74c69..f208e2a3ff137 100644
--- a/llvm/lib/Transforms/Vectorize/VPlan.h
+++ b/llvm/lib/Transforms/Vectorize/VPlan.h
@@ -1350,13 +1350,10 @@ class LLVM_ABI_FOR_TEST VPInstruction : public VPRecipeWithIRFlags,
OpsEnd = VScale,
};
- /// Returns true if this VPInstruction generates scalar values for all lanes.
- /// Most VPInstructions generate a single value per part, either vector or
- /// scalar. VPReplicateRecipe takes care of generating multiple (scalar)
- /// values per all lanes, stemming from an original ingredient. This method
- /// identifies the (rare) cases of VPInstructions that do so as well, w/o an
- /// underlying ingredient.
- bool doesGeneratePerAllLanes() const;
+ /// Returns true if this VPInstruction replicates for each part, and generates
+ /// scalar values for all lanes; i.e. if it behaves like a non-single-scalar
+ /// VPReplicateRecipe (see also VPReplicateRecipe::mustReplicate).
+ bool mustReplicate() const;
/// Return the number of operands determined by the opcode of the
/// VPInstruction, excluding mask. Returns -1u if the number of operands
@@ -3453,8 +3450,13 @@ class LLVM_ABI_FOR_TEST VPReplicateRecipe : public VPRecipeWithIRFlags,
bool IsSingleScalar, ElementCount VF,
VPCostContext &Ctx);
+ /// Returns true if the recipe produces a single scalar value, with scalar
+ /// operands.
bool isSingleScalar() const { return IsSingleScalar; }
+ /// Returns true if the recipe produces scalar values for all lanes.
+ bool mustReplicate() const { return !IsSingleScalar; }
+
bool isPredicated() const { return IsPredicated; }
/// Returns true if the recipe only uses the first lane of operand \p Op.
diff --git a/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp b/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
index 17b90c733a817..f216489e6b83c 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
@@ -626,8 +626,9 @@ unsigned VPInstruction::getNumOperandsForOpcode() const {
llvm_unreachable("all cases should be handled above");
}
-bool VPInstruction::doesGeneratePerAllLanes() const {
- return Opcode == VPInstruction::PtrAdd && !vputils::onlyFirstLaneUsed(this);
+bool VPInstruction::mustReplicate() const {
+ return Opcode == VPInstruction::Unpack ||
+ (Opcode == VPInstruction::PtrAdd && !vputils::onlyFirstLaneUsed(this));
}
bool VPInstruction::canGenerateScalarForFirstLane() const {
@@ -1262,7 +1263,7 @@ InstructionCost VPInstruction::computeCost(ElementCount VF,
return 0;
}
- assert(!doesGeneratePerAllLanes() &&
+ assert(!mustReplicate() &&
"Should only generate a vector value or single scalar, not scalars "
"for all lanes.");
return getCostForRecipeWithOpcode(
diff --git a/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp b/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
index c03a0efda9528..fdb73c8ee9efb 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
@@ -5196,19 +5196,14 @@ void VPlanTransforms::materializePacksAndUnpacks(VPlan &Plan) {
for (VPBasicBlock *VPBB :
concat<VPBasicBlock *>(VPBBsOutsideLoopRegion, VPBBsInsideLoopRegion)) {
for (VPRecipeBase &R : make_early_inc_range(*VPBB)) {
- if (!isa<VPScalarIVStepsRecipe, VPReplicateRecipe, VPInstruction>(&R))
+ if (!vputils::mustReplicate(&R))
continue;
auto *DefR = cast<VPSingleDefRecipe>(&R);
auto UsesVectorOrInsideReplicateRegion = [DefR, LoopRegion](VPUser *U) {
VPRegionBlock *ParentRegion = cast<VPRecipeBase>(U)->getRegion();
return !U->usesScalars(DefR) || ParentRegion != LoopRegion;
};
- if ((isa<VPReplicateRecipe>(DefR) &&
- cast<VPReplicateRecipe>(DefR)->isSingleScalar()) ||
- (isa<VPInstruction>(DefR) &&
- (vputils::onlyFirstLaneUsed(DefR) ||
- !cast<VPInstruction>(DefR)->doesGeneratePerAllLanes())) ||
- none_of(DefR->users(), UsesVectorOrInsideReplicateRegion))
+ if (none_of(DefR->users(), UsesVectorOrInsideReplicateRegion))
continue;
Type *ScalarTy = DefR->getScalarType();
diff --git a/llvm/lib/Transforms/Vectorize/VPlanUnroll.cpp b/llvm/lib/Transforms/Vectorize/VPlanUnroll.cpp
index 65509bd775a71..33ba64c7e4456 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanUnroll.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanUnroll.cpp
@@ -936,12 +936,7 @@ void VPlanTransforms::replicateByVF(VPlan &Plan, ElementCount VF) {
SmallVector<VPRecipeBase *> ToRemove;
for (VPBasicBlock *VPBB : VPBBsToUnroll) {
for (VPRecipeBase &R : make_early_inc_range(*VPBB)) {
- if (!isa<VPInstruction, VPReplicateRecipe, VPScalarIVStepsRecipe>(&R) ||
- (isa<VPReplicateRecipe>(&R) &&
- cast<VPReplicateRecipe>(&R)->isSingleScalar()) ||
- (isa<VPInstruction>(&R) &&
- !cast<VPInstruction>(&R)->doesGeneratePerAllLanes() &&
- cast<VPInstruction>(&R)->getOpcode() != VPInstruction::Unpack))
+ if (!vputils::mustReplicate(&R))
continue;
auto *DefR = cast<VPSingleDefRecipe>(&R);
diff --git a/llvm/lib/Transforms/Vectorize/VPlanUtils.cpp b/llvm/lib/Transforms/Vectorize/VPlanUtils.cpp
index 442ddbdde66ca..23d73af21a59f 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanUtils.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanUtils.cpp
@@ -490,6 +490,14 @@ bool vputils::isUniformAcrossVFsAndUFs(const VPValue *V) {
});
}
+bool vputils::mustReplicate(const VPRecipeBase *R) {
+ if (auto *RepR = dyn_cast<VPReplicateRecipe>(R))
+ return RepR->mustReplicate();
+ if (auto *VPI = dyn_cast<VPInstruction>(R))
+ return VPI->mustReplicate();
+ return isa<VPScalarIVStepsRecipe>(R);
+}
+
VPBasicBlock *vputils::getFirstLoopHeader(VPlan &Plan, VPDominatorTree &VPDT) {
auto DepthFirst = vp_depth_first_shallow(Plan.getEntry());
auto I = find_if(DepthFirst, [&VPDT](VPBlockBase *VPB) {
diff --git a/llvm/lib/Transforms/Vectorize/VPlanUtils.h b/llvm/lib/Transforms/Vectorize/VPlanUtils.h
index deb025992c718..c3a64b60a4139 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanUtils.h
+++ b/llvm/lib/Transforms/Vectorize/VPlanUtils.h
@@ -64,6 +64,10 @@ bool isHeaderMask(const VPValue *V, const VPlan &Plan);
/// VPDerivedIV or the canonical IV).
bool isUniformAcrossVFsAndUFs(const VPValue *V);
+/// Returns true if \p R must be replicated, to produce scalar values for all
+/// lanes.
+bool mustReplicate(const VPRecipeBase *R);
+
/// Returns the header block of the first, top-level loop, or null if none
/// exist.
VPBasicBlock *getFirstLoopHeader(VPlan &Plan, VPDominatorTree &VPDT);
>From ab3e11d9ccbedfaac204fba9c6f91ac799fdaaa6 Mon Sep 17 00:00:00 2001
From: Ramkumar Ramachandra <artagnon at tenstorrent.com>
Date: Mon, 15 Jun 2026 13:40:26 +0100
Subject: [PATCH 2/2] [VPlan] Tweak comment
---
llvm/lib/Transforms/Vectorize/VPlan.h | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/llvm/lib/Transforms/Vectorize/VPlan.h b/llvm/lib/Transforms/Vectorize/VPlan.h
index f208e2a3ff137..0a601d1aa6d09 100644
--- a/llvm/lib/Transforms/Vectorize/VPlan.h
+++ b/llvm/lib/Transforms/Vectorize/VPlan.h
@@ -1350,9 +1350,9 @@ class LLVM_ABI_FOR_TEST VPInstruction : public VPRecipeWithIRFlags,
OpsEnd = VScale,
};
- /// Returns true if this VPInstruction replicates for each part, and generates
- /// scalar values for all lanes; i.e. if it behaves like a non-single-scalar
- /// VPReplicateRecipe (see also VPReplicateRecipe::mustReplicate).
+ /// Returns true if this VPInstruction generates scalar values for all lanes;
+ /// i.e. if it behaves like a non-single-scalar VPReplicateRecipe (see also
+ /// VPReplicateRecipe::mustReplicate).
bool mustReplicate() const;
/// Return the number of operands determined by the opcode of the
More information about the llvm-commits
mailing list