[llvm] e4ef651 - [VPlan] Simplify VPReductionPHIRecipe::execute (NFC).

Florian Hahn via llvm-commits llvm-commits at lists.llvm.org
Fri May 30 07:57:32 PDT 2025


Author: Florian Hahn
Date: 2025-05-30T15:56:44+01:00
New Revision: e4ef651695e5274df0abc0f596b3404007094052

URL: https://github.com/llvm/llvm-project/commit/e4ef651695e5274df0abc0f596b3404007094052
DIFF: https://github.com/llvm/llvm-project/commit/e4ef651695e5274df0abc0f596b3404007094052.diff

LOG: [VPlan] Simplify VPReductionPHIRecipe::execute (NFC).

Simplify VPReductionPHIRecipe::execute by handling the simple cases
first, by directly using State.get() to the appropriate start value.

Added: 
    

Modified: 
    llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp b/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
index 5565c4b8aac1e..4ee2fdecf2813 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
@@ -3835,24 +3835,17 @@ void VPFirstOrderRecurrencePHIRecipe::print(raw_ostream &O, const Twine &Indent,
 #endif
 
 void VPReductionPHIRecipe::execute(VPTransformState &State) {
-  auto &Builder = State.Builder;
-
   // If this phi is fed by a scaled reduction then it should output a
   // vector with fewer elements than the VF.
   ElementCount VF = State.VF.divideCoefficientBy(VFScaleFactor);
 
-  // Reductions do not have to start at zero. They can start with
-  // any loop invariant values.
-  VPValue *StartVPV = getStartValue();
-  Value *StartV = StartVPV->getLiveInIRValue();
-
   // In order to support recurrences we need to be able to vectorize Phi nodes.
   // Phi nodes have cycles, so we need to vectorize them in two stages. This is
   // stage #1: We create a new vector PHI node with no incoming edges. We'll use
   // this value when we vectorize all of the instructions that use the PHI.
+  auto *ScalarTy = State.TypeAnalysis.inferScalarType(this);
   bool ScalarPHI = State.VF.isScalar() || IsInLoop;
-  Type *VecTy =
-      ScalarPHI ? StartV->getType() : VectorType::get(StartV->getType(), VF);
+  Type *VecTy = ScalarPHI ? ScalarTy : VectorType::get(ScalarTy, VF);
 
   BasicBlock *HeaderBB = State.CFG.PrevBB;
   assert(State.CurrentParentLoop->getHeader() == HeaderBB &&
@@ -3863,22 +3856,18 @@ void VPReductionPHIRecipe::execute(VPTransformState &State) {
 
   BasicBlock *VectorPH =
       State.CFG.VPBB2IRBB.at(getParent()->getCFGPredecessor(0));
+  // Create start and identity vector values for the reduction in the preheader.
+  // TODO: Introduce recipes in VPlan preheader to create initial values.
+  IRBuilderBase::InsertPointGuard IPBuilder(State.Builder);
+  State.Builder.SetInsertPoint(VectorPH->getTerminator());
 
-  Value *Iden = nullptr;
+  // Reductions do not have to start at zero. They can start with
+  // any loop invariant values.
+  VPValue *StartVPV = getStartValue();
   RecurKind RK = RdxDesc.getRecurrenceKind();
-  unsigned CurrentPart = getUnrollPart(*this);
-
   if (RecurrenceDescriptor::isMinMaxRecurrenceKind(RK) ||
-      RecurrenceDescriptor::isAnyOfRecurrenceKind(RK)) {
-    // MinMax and AnyOf reductions have the start value as their identity.
-    if (ScalarPHI) {
-      Iden = StartV;
-    } else {
-      IRBuilderBase::InsertPointGuard IPBuilder(Builder);
-      Builder.SetInsertPoint(VectorPH->getTerminator());
-      StartV = Iden = State.get(StartVPV);
-    }
-  } else if (RecurrenceDescriptor::isFindLastIVRecurrenceKind(RK)) {
+      RecurrenceDescriptor::isAnyOfRecurrenceKind(RK) ||
+      RecurrenceDescriptor::isFindLastIVRecurrenceKind(RK)) {
     // [I|F]FindLastIV will use a sentinel value to initialize the reduction
     // phi or the resume value from the main vector loop when vectorizing the
     // epilogue loop. In the exit block, ComputeReductionResult will generate
@@ -3888,33 +3877,24 @@ void VPReductionPHIRecipe::execute(VPTransformState &State) {
     // TODO: The sentinel value is not always necessary. When the start value is
     // a constant, and smaller than the start value of the induction variable,
     // the start value can be directly used to initialize the reduction phi.
-    Iden = StartV;
-    if (!ScalarPHI) {
-      IRBuilderBase::InsertPointGuard IPBuilder(Builder);
-      Builder.SetInsertPoint(VectorPH->getTerminator());
-      StartV = Iden = Builder.CreateVectorSplat(State.VF, Iden);
-    }
-  } else {
-    Iden = llvm::getRecurrenceIdentity(RK, VecTy->getScalarType(),
-                                       RdxDesc.getFastMathFlags());
-
-    if (!ScalarPHI) {
-      if (CurrentPart == 0) {
-        // Create start and identity vector values for the reduction in the
-        // preheader.
-        // TODO: Introduce recipes in VPlan preheader to create initial values.
-        Iden = Builder.CreateVectorSplat(VF, Iden);
-        IRBuilderBase::InsertPointGuard IPBuilder(Builder);
-        Builder.SetInsertPoint(VectorPH->getTerminator());
-        Constant *Zero = Builder.getInt32(0);
-        StartV = Builder.CreateInsertElement(Iden, StartV, Zero);
-      } else {
-        Iden = Builder.CreateVectorSplat(VF, Iden);
-      }
+    Phi->addIncoming(State.get(StartVPV, ScalarPHI), VectorPH);
+    return;
+  }
+
+  Value *Iden = getRecurrenceIdentity(RK, VecTy->getScalarType(),
+                                      RdxDesc.getFastMathFlags());
+  unsigned CurrentPart = getUnrollPart(*this);
+  Value *StartV = StartVPV->getLiveInIRValue();
+  if (!ScalarPHI) {
+    if (CurrentPart == 0) {
+      Iden = State.Builder.CreateVectorSplat(VF, Iden);
+      Constant *Zero = State.Builder.getInt32(0);
+      StartV = State.Builder.CreateInsertElement(Iden, StartV, Zero);
+    } else {
+      Iden = State.Builder.CreateVectorSplat(VF, Iden);
     }
   }
 
-  Phi = cast<PHINode>(State.get(this, IsInLoop));
   Value *StartVal = (CurrentPart == 0) ? StartV : Iden;
   Phi->addIncoming(StartVal, VectorPH);
 }


        


More information about the llvm-commits mailing list