Skip to content

Commit 3edeb41

Browse files
committed
[VPlan] Add helper to run VPlan passes, verify after run (NFC).
Add new runPass helpers to run a VPlan transformation. This makes it easier to add additional checks/functionality for each transform run. In this patch, an option is added to run the verifier after each VPlan transform. Follow-ups will use the same helper to also support printing VPlans after each transform.
1 parent 9da7c3b commit 3edeb41

File tree

4 files changed

+73
-29
lines changed

4 files changed

+73
-29
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,16 @@ cl::opt<bool> EnableVPlanNativePath(
355355
"enable-vplan-native-path", cl::Hidden,
356356
cl::desc("Enable VPlan-native vectorization path with "
357357
"support for outer loop vectorization."));
358+
359+
cl::opt<bool>
360+
VerifyEachVPlan("vplan-verify-each",
361+
#ifdef EXPENSIVE_CHECKS
362+
cl::init(true),
363+
#else
364+
cl::init(false),
365+
#endif
366+
cl::Hidden,
367+
cl::desc("Verfiy VPlans after VPlan transforms."));
358368
} // namespace llvm
359369

360370
// This flag enables the stress testing of the VPlan H-CFG construction in the
@@ -7649,8 +7659,8 @@ DenseMap<const SCEV *, Value *> LoopVectorizationPlanner::executePlan(
76497659

76507660
// TODO: Move to VPlan transform stage once the transition to the VPlan-based
76517661
// cost model is complete for better cost estimates.
7652-
VPlanTransforms::unrollByUF(BestVPlan, BestUF,
7653-
OrigLoop->getHeader()->getContext());
7662+
VPlanTransforms::runPass(VPlanTransforms::unrollByUF, BestVPlan, BestUF,
7663+
OrigLoop->getHeader()->getContext());
76547664
VPlanTransforms::optimizeForVFAndUF(BestVPlan, BestVF, BestUF, PSE);
76557665
VPlanTransforms::convertToConcreteRecipes(BestVPlan);
76567666

@@ -8887,13 +8897,14 @@ void LoopVectorizationPlanner::buildVPlansWithVPRecipes(ElementCount MinVF,
88878897
if (auto Plan = tryToBuildVPlanWithVPRecipes(SubRange)) {
88888898
// Now optimize the initial VPlan.
88898899
if (!Plan->hasVF(ElementCount::getFixed(1)))
8890-
VPlanTransforms::truncateToMinimalBitwidths(*Plan,
8891-
CM.getMinimalBitwidths());
8900+
VPlanTransforms::runPass(VPlanTransforms::truncateToMinimalBitwidths,
8901+
*Plan, CM.getMinimalBitwidths());
88928902
VPlanTransforms::optimize(*Plan);
88938903
// TODO: try to put it close to addActiveLaneMask().
88948904
// Discard the plan if it is not EVL-compatible
8895-
if (CM.foldTailWithEVL() && !VPlanTransforms::tryAddExplicitVectorLength(
8896-
*Plan, CM.getMaxSafeElements()))
8905+
if (CM.foldTailWithEVL() &&
8906+
!VPlanTransforms::runPass(VPlanTransforms::tryAddExplicitVectorLength,
8907+
*Plan, CM.getMaxSafeElements()))
88978908
break;
88988909
assert(verifyVPlanIsValid(*Plan) && "VPlan is invalid");
88998910
VPlans.push_back(std::move(Plan));
@@ -9393,8 +9404,9 @@ LoopVectorizationPlanner::tryToBuildVPlanWithVPRecipes(VFRange &Range) {
93939404

93949405
if (auto *UncountableExitingBlock =
93959406
Legal->getUncountableEarlyExitingBlock()) {
9396-
VPlanTransforms::handleUncountableEarlyExit(
9397-
*Plan, *PSE.getSE(), OrigLoop, UncountableExitingBlock, RecipeBuilder);
9407+
VPlanTransforms::runPass(VPlanTransforms::handleUncountableEarlyExit, *Plan,
9408+
*PSE.getSE(), OrigLoop, UncountableExitingBlock,
9409+
RecipeBuilder);
93989410
}
93999411
DenseMap<VPValue *, VPValue *> IVEndValues;
94009412
addScalarResumePhis(RecipeBuilder, *Plan, IVEndValues);
@@ -9419,8 +9431,9 @@ LoopVectorizationPlanner::tryToBuildVPlanWithVPRecipes(VFRange &Range) {
94199431
// Interleave memory: for each Interleave Group we marked earlier as relevant
94209432
// for this VPlan, replace the Recipes widening its memory instructions with a
94219433
// single VPInterleaveRecipe at its insertion point.
9422-
VPlanTransforms::createInterleaveGroups(
9423-
*Plan, InterleaveGroups, RecipeBuilder, CM.isScalarEpilogueAllowed());
9434+
VPlanTransforms::runPass(VPlanTransforms::createInterleaveGroups, *Plan,
9435+
InterleaveGroups, RecipeBuilder,
9436+
CM.isScalarEpilogueAllowed());
94249437

94259438
for (ElementCount VF : Range)
94269439
Plan->addVF(VF);
@@ -9462,13 +9475,16 @@ LoopVectorizationPlanner::tryToBuildVPlanWithVPRecipes(VFRange &Range) {
94629475
}
94639476
}
94649477

9465-
VPlanTransforms::dropPoisonGeneratingRecipes(*Plan, [this](BasicBlock *BB) {
9478+
auto BlockNeedsPredication = [this](BasicBlock *BB) {
94669479
return Legal->blockNeedsPredication(BB);
9467-
});
9480+
};
9481+
VPlanTransforms::runPass(VPlanTransforms::dropPoisonGeneratingRecipes, *Plan,
9482+
BlockNeedsPredication);
94689483

94699484
// Sink users of fixed-order recurrence past the recipe defining the previous
94709485
// value and introduce FirstOrderRecurrenceSplice VPInstructions.
9471-
if (!VPlanTransforms::adjustFixedOrderRecurrences(*Plan, Builder))
9486+
if (!VPlanTransforms::runPass(VPlanTransforms::adjustFixedOrderRecurrences,
9487+
*Plan, Builder))
94729488
return nullptr;
94739489

94749490
if (useActiveLaneMask(Style)) {
@@ -9817,10 +9833,10 @@ void LoopVectorizationPlanner::adjustRecipesForReductions(
98179833
PhiR->setOperand(0, Plan->getOrAddLiveIn(RdxDesc.getSentinelValue()));
98189834
}
98199835
}
9820-
9821-
VPlanTransforms::clearReductionWrapFlags(*Plan);
98229836
for (VPRecipeBase *R : ToDelete)
98239837
R->eraseFromParent();
9838+
9839+
VPlanTransforms::runPass(VPlanTransforms::clearReductionWrapFlags, *Plan);
98249840
}
98259841

98269842
void VPDerivedIVRecipe::execute(VPTransformState &State) {
@@ -10184,7 +10200,7 @@ static void preparePlanForMainVectorLoop(VPlan &MainPlan, VPlan &EpiPlan) {
1018410200
VPIRInst->eraseFromParent();
1018510201
ResumePhi->eraseFromParent();
1018610202
}
10187-
VPlanTransforms::removeDeadRecipes(MainPlan);
10203+
VPlanTransforms::runPass(VPlanTransforms::removeDeadRecipes, MainPlan);
1018810204

1018910205
using namespace VPlanPatternMatch;
1019010206
VPBasicBlock *MainScalarPH = MainPlan.getScalarPreheader();

llvm/lib/Transforms/Vectorize/VPlan.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ using namespace llvm::VPlanPatternMatch;
5252
namespace llvm {
5353
extern cl::opt<bool> EnableVPlanNativePath;
5454
}
55+
5556
extern cl::opt<unsigned> ForceTargetInstructionCost;
5657

5758
static cl::opt<bool> PrintVPlansInDotFormat(

llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "VPlanDominatorTree.h"
2020
#include "VPlanPatternMatch.h"
2121
#include "VPlanUtils.h"
22+
#include "VPlanVerifier.h"
2223
#include "llvm/ADT/PostOrderIterator.h"
2324
#include "llvm/ADT/STLExtras.h"
2425
#include "llvm/ADT/SetVector.h"
@@ -1439,19 +1440,19 @@ void VPlanTransforms::truncateToMinimalBitwidths(
14391440
}
14401441

14411442
void VPlanTransforms::optimize(VPlan &Plan) {
1442-
removeRedundantCanonicalIVs(Plan);
1443-
removeRedundantInductionCasts(Plan);
1443+
runPass(removeRedundantCanonicalIVs, Plan);
1444+
runPass(removeRedundantInductionCasts, Plan);
14441445

14451446
simplifyRecipes(Plan, Plan.getCanonicalIV()->getScalarType());
1446-
removeDeadRecipes(Plan);
1447-
legalizeAndOptimizeInductions(Plan);
1448-
removeRedundantExpandSCEVRecipes(Plan);
1447+
runPass(removeDeadRecipes, Plan);
1448+
runPass(legalizeAndOptimizeInductions, Plan);
1449+
runPass(removeRedundantExpandSCEVRecipes, Plan);
14491450
simplifyRecipes(Plan, Plan.getCanonicalIV()->getScalarType());
1450-
removeDeadRecipes(Plan);
1451+
runPass(removeDeadRecipes, Plan);
14511452

1452-
createAndOptimizeReplicateRegions(Plan);
1453-
mergeBlocksIntoPredecessors(Plan);
1454-
licm(Plan);
1453+
runPass(createAndOptimizeReplicateRegions, Plan);
1454+
runPass(mergeBlocksIntoPredecessors, Plan);
1455+
runPass(licm, Plan);
14551456
}
14561457

14571458
// Add a VPActiveLaneMaskPHIRecipe and related recipes to \p Plan and replace
@@ -1871,7 +1872,8 @@ bool VPlanTransforms::tryAddExplicitVectorLength(
18711872
}
18721873

18731874
void VPlanTransforms::dropPoisonGeneratingRecipes(
1874-
VPlan &Plan, function_ref<bool(BasicBlock *)> BlockNeedsPredication) {
1875+
VPlan &Plan,
1876+
const std::function<bool(BasicBlock *)> &BlockNeedsPredication) {
18751877
// Collect recipes in the backward slice of `Root` that may generate a poison
18761878
// value that is used after vectorization.
18771879
SmallPtrSet<VPRecipeBase *, 16> Visited;
@@ -1971,7 +1973,7 @@ void VPlanTransforms::createInterleaveGroups(
19711973
VPlan &Plan,
19721974
const SmallPtrSetImpl<const InterleaveGroup<Instruction> *>
19731975
&InterleaveGroups,
1974-
VPRecipeBuilder &RecipeBuilder, bool ScalarEpilogueAllowed) {
1976+
VPRecipeBuilder &RecipeBuilder, const bool &ScalarEpilogueAllowed) {
19751977
if (InterleaveGroups.empty())
19761978
return;
19771979

llvm/lib/Transforms/Vectorize/VPlanTransforms.h

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
#define LLVM_TRANSFORMS_VECTORIZE_VPLANTRANSFORMS_H
1515

1616
#include "VPlan.h"
17+
#include "VPlanVerifier.h"
1718
#include "llvm/ADT/STLFunctionalExtras.h"
19+
#include "llvm/Support/CommandLine.h"
1820

1921
namespace llvm {
2022

@@ -27,7 +29,29 @@ class TargetLibraryInfo;
2729
class VPBuilder;
2830
class VPRecipeBuilder;
2931

32+
extern cl::opt<bool> VerifyEachVPlan;
33+
3034
struct VPlanTransforms {
35+
/// Helper to run a VPlan transform \p Transform on \p VPlan, forwarding extra
36+
/// arguments to the transform. Returns the boolean returned by the transform.
37+
template <typename... ArgsTy>
38+
static bool runPass(bool (*Transform)(VPlan &, ArgsTy...), VPlan &Plan,
39+
typename std::remove_reference<ArgsTy>::type &...Args) {
40+
bool Res = Transform(Plan, Args...);
41+
if (VerifyEachVPlan)
42+
verifyVPlanIsValid(Plan);
43+
return Res;
44+
}
45+
/// Helper to run a VPlan transform \p Transform on \p VPlan, forwarding extra
46+
/// arguments to the transform.
47+
template <typename... ArgsTy>
48+
static void runPass(void (*Fn)(VPlan &, ArgsTy...), VPlan &Plan,
49+
typename std::remove_reference<ArgsTy>::type &...Args) {
50+
Fn(Plan, Args...);
51+
if (VerifyEachVPlan)
52+
verifyVPlanIsValid(Plan);
53+
}
54+
3155
/// Replaces the VPInstructions in \p Plan with corresponding
3256
/// widen recipes.
3357
static void
@@ -100,7 +124,8 @@ struct VPlanTransforms {
100124
/// TODO: Replace BlockNeedsPredication callback with retrieving info from
101125
/// VPlan directly.
102126
static void dropPoisonGeneratingRecipes(
103-
VPlan &Plan, function_ref<bool(BasicBlock *)> BlockNeedsPredication);
127+
VPlan &Plan,
128+
const std::function<bool(BasicBlock *)> &BlockNeedsPredication);
104129

105130
/// Add a VPEVLBasedIVPHIRecipe and related recipes to \p Plan and
106131
/// replaces all uses except the canonical IV increment of
@@ -119,7 +144,7 @@ struct VPlanTransforms {
119144
VPlan &Plan,
120145
const SmallPtrSetImpl<const InterleaveGroup<Instruction> *>
121146
&InterleaveGroups,
122-
VPRecipeBuilder &RecipeBuilder, bool ScalarEpilogueAllowed);
147+
VPRecipeBuilder &RecipeBuilder, const bool &ScalarEpilogueAllowed);
123148

124149
/// Remove dead recipes from \p Plan.
125150
static void removeDeadRecipes(VPlan &Plan);

0 commit comments

Comments
 (0)