Skip to content

Commit a00aafc

Browse files
committed
[VPlan] Iterate over phi recipes to detect reductions to fix.
After refactoring the phi recipes, we can now iterate over all header phis in a VPlan to detect reductions when it comes to fixing them up when tail folding. This reduces the coupling with the cost model & legal by using the information directly available in VPlan. It also removes a call to getOrAddVPValue, which references the original IR value which may become outdated after VPlan transformations. Reviewed By: Ayal Differential Revision: https://reviews.llvm.org/D100102
1 parent 45685a1 commit a00aafc

File tree

2 files changed

+33
-29
lines changed

2 files changed

+33
-29
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorizationPlanner.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -351,13 +351,14 @@ class LoopVectorizationPlanner {
351351
/// legal to vectorize the loop. This method creates VPlans using VPRecipes.
352352
void buildVPlansWithVPRecipes(ElementCount MinVF, ElementCount MaxVF);
353353

354-
/// Adjust the recipes for any inloop reductions. The chain of instructions
355-
/// leading from the loop exit instr to the phi need to be converted to
356-
/// reductions, with one operand being vector and the other being the scalar
357-
/// reduction chain.
358-
void adjustRecipesForInLoopReductions(VPlanPtr &Plan,
359-
VPRecipeBuilder &RecipeBuilder,
360-
ElementCount MinVF);
354+
// Adjust the recipes for reductions. For in-loop reductions the chain of
355+
// instructions leading from the loop exit instr to the phi need to be
356+
// converted to reductions, with one operand being vector and the other being
357+
// the scalar reduction chain. For other reductions, a select is introduced
358+
// between the phi and live-out recipes when folding the tail.
359+
void adjustRecipesForReductions(VPBasicBlock *LatchVPBB, VPlanPtr &Plan,
360+
VPRecipeBuilder &RecipeBuilder,
361+
ElementCount MinVF);
361362
};
362363

363364
} // namespace llvm

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4295,7 +4295,7 @@ void InnerLoopVectorizer::fixReduction(VPReductionPHIRecipe *PhiR,
42954295
Instruction *LoopExitInst = RdxDesc.getLoopExitInstr();
42964296
setDebugLocFromInst(ReductionStartValue);
42974297

4298-
VPValue *LoopExitInstDef = State.Plan->getVPValue(LoopExitInst);
4298+
VPValue *LoopExitInstDef = PhiR->getBackedgeValue();
42994299
// This is the vector-clone of the value that leaves the loop.
43004300
Type *VecTy = State.get(LoopExitInstDef, 0)->getType();
43014301

@@ -9438,21 +9438,7 @@ VPlanPtr LoopVectorizationPlanner::buildVPlanWithVPRecipes(
94389438
}
94399439

94409440
// Adjust the recipes for any inloop reductions.
9441-
adjustRecipesForInLoopReductions(Plan, RecipeBuilder, Range.Start);
9442-
9443-
// Finally, if tail is folded by masking, introduce selects between the phi
9444-
// and the live-out instruction of each reduction, at the end of the latch.
9445-
if (CM.foldTailByMasking() && !Legal->getReductionVars().empty()) {
9446-
Builder.setInsertPoint(VPBB);
9447-
auto *Cond = RecipeBuilder.createBlockInMask(OrigLoop->getHeader(), Plan);
9448-
for (auto &Reduction : Legal->getReductionVars()) {
9449-
if (CM.isInLoopReduction(Reduction.first))
9450-
continue;
9451-
VPValue *Phi = Plan->getOrAddVPValue(Reduction.first);
9452-
VPValue *Red = Plan->getOrAddVPValue(Reduction.second.getLoopExitInstr());
9453-
Builder.createNaryOp(Instruction::Select, {Cond, Red, Phi});
9454-
}
9455-
}
9441+
adjustRecipesForReductions(VPBB, Plan, RecipeBuilder, Range.Start);
94569442

94579443
VPlanTransforms::sinkScalarOperands(*Plan);
94589444
VPlanTransforms::mergeReplicateRegions(*Plan);
@@ -9508,12 +9494,14 @@ VPlanPtr LoopVectorizationPlanner::buildVPlan(VFRange &Range) {
95089494
return Plan;
95099495
}
95109496

9511-
// Adjust the recipes for any inloop reductions. The chain of instructions
9512-
// leading from the loop exit instr to the phi need to be converted to
9513-
// reductions, with one operand being vector and the other being the scalar
9514-
// reduction chain.
9515-
void LoopVectorizationPlanner::adjustRecipesForInLoopReductions(
9516-
VPlanPtr &Plan, VPRecipeBuilder &RecipeBuilder, ElementCount MinVF) {
9497+
// Adjust the recipes for reductions. For in-loop reductions the chain of
9498+
// instructions leading from the loop exit instr to the phi need to be converted
9499+
// to reductions, with one operand being vector and the other being the scalar
9500+
// reduction chain. For other reductions, a select is introduced between the phi
9501+
// and live-out recipes when folding the tail.
9502+
void LoopVectorizationPlanner::adjustRecipesForReductions(
9503+
VPBasicBlock *LatchVPBB, VPlanPtr &Plan, VPRecipeBuilder &RecipeBuilder,
9504+
ElementCount MinVF) {
95179505
for (auto &Reduction : CM.getInLoopReductionChains()) {
95189506
PHINode *Phi = Reduction.first;
95199507
RecurrenceDescriptor &RdxDesc = Legal->getReductionVars()[Phi];
@@ -9570,6 +9558,21 @@ void LoopVectorizationPlanner::adjustRecipesForInLoopReductions(
95709558
Chain = R;
95719559
}
95729560
}
9561+
9562+
// If tail is folded by masking, introduce selects between the phi
9563+
// and the live-out instruction of each reduction, at the end of the latch.
9564+
if (CM.foldTailByMasking()) {
9565+
for (VPRecipeBase &R : Plan->getEntry()->getEntryBasicBlock()->phis()) {
9566+
VPReductionPHIRecipe *PhiR = dyn_cast<VPReductionPHIRecipe>(&R);
9567+
if (!PhiR || PhiR->isInLoop())
9568+
continue;
9569+
Builder.setInsertPoint(LatchVPBB);
9570+
VPValue *Cond =
9571+
RecipeBuilder.createBlockInMask(OrigLoop->getHeader(), Plan);
9572+
VPValue *Red = PhiR->getBackedgeValue();
9573+
Builder.createNaryOp(Instruction::Select, {Cond, Red, PhiR});
9574+
}
9575+
}
95739576
}
95749577

95759578
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)

0 commit comments

Comments
 (0)