[llvm] [VPlan] Consolidate (ActiveLaneMask|Widen)PHI recipes (NFC) (PR #169481)
Ramkumar Ramachandra via llvm-commits
llvm-commits at lists.llvm.org
Tue Nov 25 06:20:35 PST 2025
https://github.com/artagnon updated https://github.com/llvm/llvm-project/pull/169481
>From 4ae1b0f408ec66c76c8652f43144e317775ce982 Mon Sep 17 00:00:00 2001
From: Ramkumar Ramachandra <ramkumar.ramachandra at codasip.com>
Date: Tue, 25 Nov 2025 10:00:34 +0000
Subject: [PATCH 1/2] [VPlan] Consolidate (ActiveLaneMask|Widen)PHI recipes
(NFC)
Make the WidenPHI recipe a HeaderPHI recipe, and make the
ActiveLaneMaskPHI recipe a special case of the WidenPHI recipe,
consolidating VP(ActiveLaneMask|Widen)PHI::execute.
---
.../Transforms/Vectorize/LoopVectorize.cpp | 3 +-
llvm/lib/Transforms/Vectorize/VPlan.h | 45 ++++++++++---------
.../lib/Transforms/Vectorize/VPlanRecipes.cpp | 17 ++-----
3 files changed, 29 insertions(+), 36 deletions(-)
diff --git a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
index a63956c0cba6b..3a12b1e6a97ad 100644
--- a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
+++ b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
@@ -2597,7 +2597,8 @@ void InnerLoopVectorizer::fixNonInductionPHIs(VPTransformState &State) {
PHINode *NewPhi = cast<PHINode>(State.get(VPPhi));
// Make sure the builder has a valid insert point.
Builder.SetInsertPoint(NewPhi);
- for (const auto &[Inc, VPBB] : VPPhi->incoming_values_and_blocks())
+ for (const auto &[Inc, VPBB] :
+ drop_begin(VPPhi->incoming_values_and_blocks()))
NewPhi->addIncoming(State.get(Inc), State.CFG.VPBB2IRBB[VPBB]);
}
}
diff --git a/llvm/lib/Transforms/Vectorize/VPlan.h b/llvm/lib/Transforms/Vectorize/VPlan.h
index 0c7d9c0193a03..fe12c46767900 100644
--- a/llvm/lib/Transforms/Vectorize/VPlan.h
+++ b/llvm/lib/Transforms/Vectorize/VPlan.h
@@ -2048,8 +2048,10 @@ class LLVM_ABI_FOR_TEST VPHeaderPHIRecipe : public VPSingleDefRecipe,
protected:
VPHeaderPHIRecipe(unsigned char VPDefID, Instruction *UnderlyingInstr,
VPValue *Start, DebugLoc DL = DebugLoc::getUnknown())
- : VPSingleDefRecipe(VPDefID, ArrayRef<VPValue *>({Start}),
- UnderlyingInstr, DL) {}
+ : VPSingleDefRecipe(VPDefID, {}, UnderlyingInstr, DL) {
+ if (Start)
+ addOperand(Start);
+ }
const VPRecipeBase *getAsRecipe() const override { return this; }
@@ -2317,24 +2319,28 @@ class VPWidenPointerInductionRecipe : public VPWidenInductionRecipe {
/// recipe is placed in an entry block to a (non-replicate) region, it must have
/// exactly 2 incoming values, the first from the predecessor of the region and
/// the second from the exiting block of the region.
-class LLVM_ABI_FOR_TEST VPWidenPHIRecipe : public VPSingleDefRecipe,
- public VPPhiAccessors {
+class LLVM_ABI_FOR_TEST VPWidenPHIRecipe : public VPHeaderPHIRecipe {
/// Name to use for the generated IR instruction for the widened phi.
std::string Name;
+protected:
+ /// Constructor for the VPActiveLaneMaskPHIRecipe subclass.
+ VPWidenPHIRecipe(unsigned SC, VPValue *Start,
+ DebugLoc DL = DebugLoc::getUnknown(), const Twine &Name = "")
+ : VPHeaderPHIRecipe(SC, nullptr, Start, DL), Name(Name.str()) {}
+
public:
/// Create a new VPWidenPHIRecipe for \p Phi with start value \p Start and
/// debug location \p DL.
VPWidenPHIRecipe(PHINode *Phi, VPValue *Start = nullptr,
DebugLoc DL = DebugLoc::getUnknown(), const Twine &Name = "")
- : VPSingleDefRecipe(VPDef::VPWidenPHISC, {}, Phi, DL), Name(Name.str()) {
- if (Start)
- addOperand(Start);
- }
+ : VPHeaderPHIRecipe(VPDef::VPWidenPHISC, Phi, Start, DL),
+ Name(Name.str()) {}
VPWidenPHIRecipe *clone() override {
- auto *C = new VPWidenPHIRecipe(cast<PHINode>(getUnderlyingValue()),
- getOperand(0), getDebugLoc(), Name);
+ auto *C =
+ new VPWidenPHIRecipe(cast_if_present<PHINode>(getUnderlyingValue()),
+ getOperand(0), getDebugLoc(), Name);
for (VPValue *Op : llvm::drop_begin(operands()))
C->addOperand(Op);
return C;
@@ -3609,28 +3615,23 @@ class VPCanonicalIVPHIRecipe : public VPHeaderPHIRecipe {
/// A recipe for generating the active lane mask for the vector loop that is
/// used to predicate the vector operations.
-/// TODO: It would be good to use the existing VPWidenPHIRecipe instead and
-/// remove VPActiveLaneMaskPHIRecipe.
-class VPActiveLaneMaskPHIRecipe : public VPHeaderPHIRecipe {
+class VPActiveLaneMaskPHIRecipe : public VPWidenPHIRecipe {
public:
VPActiveLaneMaskPHIRecipe(VPValue *StartMask, DebugLoc DL)
- : VPHeaderPHIRecipe(VPDef::VPActiveLaneMaskPHISC, nullptr, StartMask,
- DL) {}
+ : VPWidenPHIRecipe(VPDef::VPActiveLaneMaskPHISC, StartMask, DL,
+ "active.lane.mask") {}
~VPActiveLaneMaskPHIRecipe() override = default;
VPActiveLaneMaskPHIRecipe *clone() override {
- auto *R = new VPActiveLaneMaskPHIRecipe(getOperand(0), getDebugLoc());
- if (getNumOperands() == 2)
- R->addOperand(getOperand(1));
- return R;
+ auto *C = new VPActiveLaneMaskPHIRecipe(getOperand(0), getDebugLoc());
+ for (VPValue *Op : llvm::drop_begin(operands()))
+ C->addOperand(Op);
+ return C;
}
VP_CLASSOF_IMPL(VPDef::VPActiveLaneMaskPHISC)
- /// Generate the active lane mask phi of the vector loop.
- void execute(VPTransformState &State) override;
-
protected:
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
/// Print the recipe.
diff --git a/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp b/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
index 54fdec3bcf4a1..1d071c6b4b238 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
@@ -4472,9 +4472,12 @@ void VPReductionPHIRecipe::printRecipe(raw_ostream &O, const Twine &Indent,
#endif
void VPWidenPHIRecipe::execute(VPTransformState &State) {
+ BasicBlock *VectorPH =
+ State.CFG.VPBB2IRBB.at(getParent()->getCFGPredecessor(0));
Value *Op0 = State.get(getOperand(0));
Type *VecTy = Op0->getType();
- Instruction *VecPhi = State.Builder.CreatePHI(VecTy, 2, Name);
+ PHINode *VecPhi = State.Builder.CreatePHI(VecTy, 2, Name);
+ VecPhi->addIncoming(Op0, VectorPH);
State.set(this, VecPhi);
}
@@ -4489,18 +4492,6 @@ void VPWidenPHIRecipe::printRecipe(raw_ostream &O, const Twine &Indent,
}
#endif
-// TODO: It would be good to use the existing VPWidenPHIRecipe instead and
-// remove VPActiveLaneMaskPHIRecipe.
-void VPActiveLaneMaskPHIRecipe::execute(VPTransformState &State) {
- BasicBlock *VectorPH =
- State.CFG.VPBB2IRBB.at(getParent()->getCFGPredecessor(0));
- Value *StartMask = State.get(getOperand(0));
- PHINode *Phi =
- State.Builder.CreatePHI(StartMask->getType(), 2, "active.lane.mask");
- Phi->addIncoming(StartMask, VectorPH);
- State.set(this, Phi);
-}
-
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
void VPActiveLaneMaskPHIRecipe::printRecipe(raw_ostream &O, const Twine &Indent,
VPSlotTracker &SlotTracker) const {
>From 60a235bd671f72522f0b7cc0c59d7f8f540fca24 Mon Sep 17 00:00:00 2001
From: Ramkumar Ramachandra <ramkumar.ramachandra at codasip.com>
Date: Tue, 25 Nov 2025 14:19:06 +0000
Subject: [PATCH 2/2] [VPlan] Fix classof
---
llvm/lib/Transforms/Vectorize/VPlan.h | 24 +++++++++++++-----------
1 file changed, 13 insertions(+), 11 deletions(-)
diff --git a/llvm/lib/Transforms/Vectorize/VPlan.h b/llvm/lib/Transforms/Vectorize/VPlan.h
index fe12c46767900..7718db3b96f49 100644
--- a/llvm/lib/Transforms/Vectorize/VPlan.h
+++ b/llvm/lib/Transforms/Vectorize/VPlan.h
@@ -2325,9 +2325,9 @@ class LLVM_ABI_FOR_TEST VPWidenPHIRecipe : public VPHeaderPHIRecipe {
protected:
/// Constructor for the VPActiveLaneMaskPHIRecipe subclass.
- VPWidenPHIRecipe(unsigned SC, VPValue *Start,
- DebugLoc DL = DebugLoc::getUnknown(), const Twine &Name = "")
- : VPHeaderPHIRecipe(SC, nullptr, Start, DL), Name(Name.str()) {}
+ VPWidenPHIRecipe(unsigned char VPDefID, VPValue *Start, DebugLoc DL,
+ const Twine &Name)
+ : VPHeaderPHIRecipe(VPDefID, nullptr, Start, DL), Name(Name.str()) {}
public:
/// Create a new VPWidenPHIRecipe for \p Phi with start value \p Start and
@@ -2338,9 +2338,8 @@ class LLVM_ABI_FOR_TEST VPWidenPHIRecipe : public VPHeaderPHIRecipe {
Name(Name.str()) {}
VPWidenPHIRecipe *clone() override {
- auto *C =
- new VPWidenPHIRecipe(cast_if_present<PHINode>(getUnderlyingValue()),
- getOperand(0), getDebugLoc(), Name);
+ auto *C = new VPWidenPHIRecipe(cast<PHINode>(getUnderlyingValue()),
+ getOperand(0), getDebugLoc(), Name);
for (VPValue *Op : llvm::drop_begin(operands()))
C->addOperand(Op);
return C;
@@ -2348,7 +2347,10 @@ class LLVM_ABI_FOR_TEST VPWidenPHIRecipe : public VPHeaderPHIRecipe {
~VPWidenPHIRecipe() override = default;
- VP_CLASSOF_IMPL(VPDef::VPWidenPHISC)
+ static inline bool classof(const VPRecipeBase *R) {
+ return R->getVPDefID() == VPDef::VPWidenPHISC ||
+ R->getVPDefID() == VPDef::VPActiveLaneMaskPHISC;
+ }
/// Generate the phi/select nodes.
void execute(VPTransformState &State) override;
@@ -3624,10 +3626,10 @@ class VPActiveLaneMaskPHIRecipe : public VPWidenPHIRecipe {
~VPActiveLaneMaskPHIRecipe() override = default;
VPActiveLaneMaskPHIRecipe *clone() override {
- auto *C = new VPActiveLaneMaskPHIRecipe(getOperand(0), getDebugLoc());
- for (VPValue *Op : llvm::drop_begin(operands()))
- C->addOperand(Op);
- return C;
+ auto *R = new VPActiveLaneMaskPHIRecipe(getOperand(0), getDebugLoc());
+ for (VPValue *Op : drop_begin(operands()))
+ R->addOperand(Op);
+ return R;
}
VP_CLASSOF_IMPL(VPDef::VPActiveLaneMaskPHISC)
More information about the llvm-commits
mailing list