Skip to content

Commit 6ff8a06

Browse files
authored
[VPlan] Run recipe removal and simplification after optimizeForVFAndUF. (#125926)
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). PR: #125926
1 parent df2e8ee commit 6ff8a06

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
@@ -7672,6 +7672,8 @@ DenseMap<const SCEV *, Value *> LoopVectorizationPlanner::executePlan(
76727672
VPlanTransforms::runPass(VPlanTransforms::unrollByUF, BestVPlan, BestUF,
76737673
OrigLoop->getHeader()->getContext());
76747674
VPlanTransforms::optimizeForVFAndUF(BestVPlan, BestVF, BestUF, PSE);
7675+
VPlanTransforms::simplifyRecipes(BestVPlan, *Legal->getWidestInductionType());
7676+
VPlanTransforms::removeDeadRecipes(BestVPlan);
76757677
VPlanTransforms::convertToConcreteRecipes(BestVPlan);
76767678

76777679
// 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
@@ -155,6 +155,14 @@ struct Recipe_match {
155155
if ((!matchRecipeAndOpcode<RecipeTys>(R) && ...))
156156
return false;
157157

158+
if (!(std::is_same_v<VPWidenEVLRecipe, RecipeTys> || ...) &&
159+
isa<VPWidenEVLRecipe>(R)) {
160+
// Don't match VPWidenEVLRecipe if it is not explicitly part of RecipeTys.
161+
// Otherwise we might match it unexpectedly when trying to match
162+
// VPWidenRecipe, of which VPWidenEVLRecipe is a subclass of.
163+
return false;
164+
}
165+
158166
assert(R->getNumOperands() == std::tuple_size<Ops_t>::value &&
159167
"recipe with matched opcode the expected number of operands");
160168

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)