Skip to content

Commit d2ce88a

Browse files
committed
[VPlan] Create initial skeleton before creating regions. (NFC)
Move out the logic to prepare for vectorization to a separate transform, before creating loop regions. This was discussed as follow-up in #136455. This just moves the existing code around slightly and will simplify follow-up patches to include the exiting edges during initial VPlan construction.
1 parent e12ff33 commit d2ce88a

File tree

4 files changed

+54
-37
lines changed

4 files changed

+54
-37
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9457,9 +9457,10 @@ LoopVectorizationPlanner::tryToBuildVPlanWithVPRecipes(VFRange &Range) {
94579457
Range);
94589458
DenseMap<VPBlockBase *, BasicBlock *> VPB2IRBB;
94599459
auto Plan = VPlanTransforms::buildPlainCFG(OrigLoop, *LI, VPB2IRBB);
9460-
VPlanTransforms::createLoopRegions(*Plan, Legal->getWidestInductionType(),
9461-
PSE, RequiresScalarEpilogueCheck,
9462-
CM.foldTailByMasking(), OrigLoop);
9460+
VPlanTransforms::prepareForVectorization(
9461+
*Plan, Legal->getWidestInductionType(), PSE, RequiresScalarEpilogueCheck,
9462+
CM.foldTailByMasking(), OrigLoop);
9463+
VPlanTransforms::createLoopRegions(*Plan);
94639464

94649465
// Don't use getDecisionAndClampRange here, because we don't know the UF
94659466
// so this function is better to be conservative, rather than to split
@@ -9749,8 +9750,9 @@ VPlanPtr LoopVectorizationPlanner::tryToBuildVPlan(VFRange &Range) {
97499750

97509751
DenseMap<VPBlockBase *, BasicBlock *> VPB2IRBB;
97519752
auto Plan = VPlanTransforms::buildPlainCFG(OrigLoop, *LI, VPB2IRBB);
9752-
VPlanTransforms::createLoopRegions(*Plan, Legal->getWidestInductionType(),
9753-
PSE, true, false, OrigLoop);
9753+
VPlanTransforms::prepareForVectorization(
9754+
*Plan, Legal->getWidestInductionType(), PSE, true, false, OrigLoop);
9755+
VPlanTransforms::createLoopRegions(*Plan);
97549756

97559757
for (ElementCount VF : Range)
97569758
Plan->addVF(VF);

llvm/lib/Transforms/Vectorize/VPlanConstruction.cpp

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -461,19 +461,23 @@ static void createLoopRegion(VPlan &Plan, VPBlockBase *HeaderVPB) {
461461
VPBlockUtils::connectBlocks(R, Succ);
462462
}
463463

464-
void VPlanTransforms::createLoopRegions(VPlan &Plan, Type *InductionTy,
465-
PredicatedScalarEvolution &PSE,
466-
bool RequiresScalarEpilogueCheck,
467-
bool TailFolded, Loop *TheLoop) {
464+
void VPlanTransforms::prepareForVectorization(VPlan &Plan, Type *InductionTy,
465+
PredicatedScalarEvolution &PSE,
466+
bool RequiresScalarEpilogueCheck,
467+
bool TailFolded, Loop *TheLoop) {
468468
VPDominatorTree VPDT;
469469
VPDT.recalculate(Plan);
470-
for (VPBlockBase *HeaderVPB : vp_depth_first_shallow(Plan.getEntry()))
471-
if (canonicalHeaderAndLatch(HeaderVPB, VPDT))
472-
createLoopRegion(Plan, HeaderVPB);
473470

474-
VPRegionBlock *TopRegion = Plan.getVectorLoopRegion();
475-
TopRegion->setName("vector loop");
476-
TopRegion->getEntryBasicBlock()->setName("vector.body");
471+
VPBlockBase *HeaderVPB = Plan.getEntry()->getSingleSuccessor();
472+
canonicalHeaderAndLatch(HeaderVPB, VPDT);
473+
VPBlockBase *LatchVPB = HeaderVPB->getPredecessors()[1];
474+
475+
VPBasicBlock *VecPreheader = Plan.createVPBasicBlock("vector.ph");
476+
VPBlockUtils::insertBlockAfter(VecPreheader, Plan.getEntry());
477+
478+
VPBasicBlock *MiddleVPBB = Plan.createVPBasicBlock("middle.block");
479+
VPBlockUtils::connectBlocks(LatchVPB, MiddleVPBB);
480+
LatchVPB->swapSuccessors();
477481

478482
// Create SCEV and VPValue for the trip count.
479483
// We use the symbolic max backedge-taken-count, which works also when
@@ -487,11 +491,6 @@ void VPlanTransforms::createLoopRegions(VPlan &Plan, Type *InductionTy,
487491
Plan.setTripCount(
488492
vputils::getOrCreateVPValueForSCEVExpr(Plan, TripCount, SE));
489493

490-
VPBasicBlock *VecPreheader = Plan.createVPBasicBlock("vector.ph");
491-
VPBlockUtils::insertBlockAfter(VecPreheader, Plan.getEntry());
492-
VPBasicBlock *MiddleVPBB = Plan.createVPBasicBlock("middle.block");
493-
VPBlockUtils::insertBlockAfter(MiddleVPBB, TopRegion);
494-
495494
VPBasicBlock *ScalarPH = Plan.createVPBasicBlock("scalar.ph");
496495
VPBlockUtils::connectBlocks(ScalarPH, Plan.getScalarHeader());
497496

@@ -516,10 +515,10 @@ void VPlanTransforms::createLoopRegions(VPlan &Plan, Type *InductionTy,
516515
return;
517516
}
518517

518+
// The connection order corresponds to the operands of the conditional branch.
519519
BasicBlock *IRExitBlock = TheLoop->getUniqueLatchExitBlock();
520520
auto *VPExitBlock = Plan.getExitBlock(IRExitBlock);
521-
// The connection order corresponds to the operands of the conditional branch.
522-
VPBlockUtils::insertBlockAfter(VPExitBlock, MiddleVPBB);
521+
VPBlockUtils::connectBlocks(MiddleVPBB, VPExitBlock);
523522
VPBlockUtils::connectBlocks(MiddleVPBB, ScalarPH);
524523

525524
auto *ScalarLatchTerm = TheLoop->getLoopLatch()->getTerminator();
@@ -538,3 +537,15 @@ void VPlanTransforms::createLoopRegions(VPlan &Plan, Type *InductionTy,
538537
Builder.createNaryOp(VPInstruction::BranchOnCond, {Cmp},
539538
ScalarLatchTerm->getDebugLoc());
540539
}
540+
541+
void VPlanTransforms::createLoopRegions(VPlan &Plan) {
542+
VPDominatorTree VPDT;
543+
VPDT.recalculate(Plan);
544+
for (VPBlockBase *HeaderVPB : vp_depth_first_shallow(Plan.getEntry()))
545+
if (canonicalHeaderAndLatch(HeaderVPB, VPDT))
546+
createLoopRegion(Plan, HeaderVPB);
547+
548+
VPRegionBlock *TopRegion = Plan.getVectorLoopRegion();
549+
TopRegion->setName("vector loop");
550+
TopRegion->getEntryBasicBlock()->setName("vector.body");
551+
}

llvm/lib/Transforms/Vectorize/VPlanTransforms.h

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -57,19 +57,22 @@ struct VPlanTransforms {
5757
buildPlainCFG(Loop *TheLoop, LoopInfo &LI,
5858
DenseMap<VPBlockBase *, BasicBlock *> &VPB2IRBB);
5959

60-
/// Replace loops in \p Plan's flat CFG with VPRegionBlocks, turing \p Plan's
61-
/// flat CFG into a hierarchical CFG. It also creates a VPValue expression for
62-
/// the original trip count. It will also introduce a dedicated VPBasicBlock
63-
/// for the vector pre-header as well a VPBasicBlock as exit block of the
64-
/// region (middle.block). If a check is needed to guard executing the scalar
65-
/// epilogue loop, it will be added to the middle block, together with
66-
/// VPBasicBlocks for the scalar preheader and exit blocks. \p InductionTy is
67-
/// the type of the canonical induction and used for related values, like the
68-
/// trip count expression.
69-
static void createLoopRegions(VPlan &Plan, Type *InductionTy,
70-
PredicatedScalarEvolution &PSE,
71-
bool RequiresScalarEpilogueCheck,
72-
bool TailFolded, Loop *TheLoop);
60+
/// Prepare the plan for vectorization. It will introduce a dedicated
61+
/// VPBasicBlock for the vector pre-header as well as a VPBasicBlock as exit
62+
/// block of the main vector loop (middle.block). If a check is needed to
63+
/// guard executing the scalar epilogue loop, it will be added to the middle
64+
/// block, together with VPBasicBlocks for the scalar preheader and exit
65+
/// blocks. \p InductionTy is the type of the canonical induction and used for
66+
/// related values, like the trip count expression. It also creates a VPValue
67+
/// expression for the original trip count.
68+
static void prepareForVectorization(VPlan &Plan, Type *InductionTy,
69+
PredicatedScalarEvolution &PSE,
70+
bool RequiresScalarEpilogueCheck,
71+
bool TailFolded, Loop *TheLoop);
72+
73+
/// Replace loops in \p Plan's flat CFG with VPRegionBlocks, turning \p Plan's
74+
/// flat CFG into a hierarchical CFG.
75+
static void createLoopRegions(VPlan &Plan);
7376

7477
/// Replaces the VPInstructions in \p Plan with corresponding
7578
/// widen recipes. Returns false if any VPInstructions could not be converted

llvm/unittests/Transforms/Vectorize/VPlanTestBase.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,9 @@ class VPlanTestIRBase : public testing::Test {
7272
PredicatedScalarEvolution PSE(*SE, *L);
7373
DenseMap<VPBlockBase *, BasicBlock *> VPB2IRBB;
7474
auto Plan = VPlanTransforms::buildPlainCFG(L, *LI, VPB2IRBB);
75-
VPlanTransforms::createLoopRegions(*Plan, IntegerType::get(*Ctx, 64), PSE,
76-
true, false, L);
75+
VPlanTransforms::prepareForVectorization(*Plan, IntegerType::get(*Ctx, 64),
76+
PSE, true, false, L);
77+
VPlanTransforms::createLoopRegions(*Plan);
7778
return Plan;
7879
}
7980
};

0 commit comments

Comments
 (0)