Skip to content

Commit d704a98

Browse files
committed
[VPlan] Run recipe removal and simplification after optimizeForVFAndUF.
Run recipe simplification and dead recipe removal after VPlan-based unrolling and optimizeForVFAndUF, to clean up any redundant or dead recipes introduced by them. Currently this is NFC, as it removes the corresponding removeDeadRecipes run in optimizeForVFAndUF and no additional simplifications kick in after unrolling yet. That is changing with #123655. Note that with this change, pattern-matching is now applied after EVL-based recipes have been introduced. Trying to match VPWidenEVLRecipe when not explicitly requested might apply a pattern with 2 operands to one with 3 due to the extra EVL operand and VPWidenEVLRecipe being a subclass of VPWidenRecipe. To prevent this, update Recipe_match::match to only match VPWidenEVLRecipe if it is in the requested recipe types (RecipeTy).
1 parent 585b75e commit d704a98

File tree

4 files changed

+15
-4
lines changed

4 files changed

+15
-4
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7685,6 +7685,8 @@ DenseMap<const SCEV *, Value *> LoopVectorizationPlanner::executePlan(
76857685
VPlanTransforms::runPass(VPlanTransforms::unrollByUF, BestVPlan, BestUF,
76867686
OrigLoop->getHeader()->getContext());
76877687
VPlanTransforms::optimizeForVFAndUF(BestVPlan, BestVF, BestUF, PSE);
7688+
VPlanTransforms::simplifyRecipes(BestVPlan, *Legal->getWidestInductionType());
7689+
VPlanTransforms::removeDeadRecipes(BestVPlan);
76887690
VPlanTransforms::convertToConcreteRecipes(BestVPlan);
76897691

76907692
// Perform the actual loop transformation.

llvm/lib/Transforms/Vectorize/VPlanPatternMatch.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,14 @@ struct Recipe_match {
170170
if ((!matchRecipeAndOpcode<RecipeTys>(R) && ...))
171171
return false;
172172

173+
if (!(std::is_same_v<VPWidenEVLRecipe, RecipeTys> || ...) &&
174+
isa<VPWidenEVLRecipe>(R)) {
175+
// Don't match VPWidenEVLRecipe if it is not explicitly part of RecipeTys.
176+
// Otherwise we might match it unexpectedly when trying to match
177+
// VPWidenRecipe, of which VPWidenEVLRecipe is a subclass of.
178+
return false;
179+
}
180+
173181
assert(R->getNumOperands() == std::tuple_size<Ops_t>::value &&
174182
"recipe with matched opcode the expected number of operands");
175183

llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -964,9 +964,7 @@ static void simplifyRecipe(VPRecipeBase &R, VPTypeAnalysis &TypeInfo) {
964964
return R.getVPSingleValue()->replaceAllUsesWith(R.getOperand(1));
965965
}
966966

967-
/// Try to simplify the recipes in \p Plan. Use \p CanonicalIVTy as type for all
968-
/// un-typed live-ins in VPTypeAnalysis.
969-
static void simplifyRecipes(VPlan &Plan, Type &CanonicalIVTy) {
967+
void VPlanTransforms::simplifyRecipes(VPlan &Plan, Type &CanonicalIVTy) {
970968
ReversePostOrderTraversal<VPBlockDeepTraversalWrapper<VPBlockBase *>> RPOT(
971969
Plan.getEntry());
972970
VPTypeAnalysis TypeInfo(&CanonicalIVTy);
@@ -1043,7 +1041,6 @@ void VPlanTransforms::optimizeForVFAndUF(VPlan &Plan, ElementCount BestVF,
10431041
}
10441042

10451043
Term->eraseFromParent();
1046-
VPlanTransforms::removeDeadRecipes(Plan);
10471044

10481045
Plan.setVF(BestVF);
10491046
Plan.setUF(BestUF);

llvm/lib/Transforms/Vectorize/VPlanTransforms.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,10 @@ struct VPlanTransforms {
163163
/// Lower abstract recipes to concrete ones, that can be codegen'd.
164164
static void convertToConcreteRecipes(VPlan &Plan);
165165

166+
/// Perform instcombine-like simplifications on recipes in \p Plan. Use \p
167+
/// CanonicalIVTy as type for all un-typed live-ins in VPTypeAnalysis.
168+
static void simplifyRecipes(VPlan &Plan, Type &CanonicalIVTy);
169+
166170
/// If there's a single exit block, optimize its phi recipes that use exiting
167171
/// IV values by feeding them precomputed end values instead, possibly taken
168172
/// one step backwards.

0 commit comments

Comments
 (0)