[llvm] [VPlan] Consolidate (ActiveLaneMask|Widen)PHI recipes (NFC) (PR #169481)

via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 25 02:28:56 PST 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-transforms

Author: Ramkumar Ramachandra (artagnon)

<details>
<summary>Changes</summary>

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.

---
Full diff: https://github.com/llvm/llvm-project/pull/169481.diff


3 Files Affected:

- (modified) llvm/lib/Transforms/Vectorize/LoopVectorize.cpp (+2-1) 
- (modified) llvm/lib/Transforms/Vectorize/VPlan.h (+23-22) 
- (modified) llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp (+4-13) 


``````````diff
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 {

``````````

</details>


https://github.com/llvm/llvm-project/pull/169481


More information about the llvm-commits mailing list