Skip to content

Commit 49578c3

Browse files
committed
[VPlan] Connect Entry to scalar preheader during initial construction.
Update initial construction to connect the Plan's entry to the scalar preheader during initial construction. This moves a small part of the skeleton creation out of ILV and will also enable replacing VPInstruction::ResumePhi with regular VPPhi recipes. Resume phis need 2 incoming values to start with, the second being the bypass value from the scalar ph (and used to replicate the incoming value for other bypass blocks). Adding the extra edge ensures we incoming values for resume phis match the incoming blocks. Still needs various VPlan printing tests to be updated. vplan-printing.ll shows the changes.
1 parent f9dbfb1 commit 49578c3

File tree

5 files changed

+72
-71
lines changed

5 files changed

+72
-71
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ class InnerLoopVectorizer {
490490
MinProfitableTripCount(MinProfitableTripCount), UF(UnrollFactor),
491491
Builder(PSE.getSE()->getContext()), Cost(CM), BFI(BFI), PSI(PSI),
492492
RTChecks(RTChecks), Plan(Plan),
493-
VectorPHVPB(Plan.getEntry()->getSingleSuccessor()) {}
493+
VectorPHVPB(Plan.getEntry()->getSuccessors()[1]) {}
494494

495495
virtual ~InnerLoopVectorizer() = default;
496496

@@ -2365,14 +2365,11 @@ InnerLoopVectorizer::getOrCreateVectorTripCount(BasicBlock *InsertBlock) {
23652365
void InnerLoopVectorizer::introduceCheckBlockInVPlan(BasicBlock *CheckIRBB) {
23662366
VPBlockBase *ScalarPH = Plan.getScalarPreheader();
23672367
VPBlockBase *PreVectorPH = VectorPHVPB->getSinglePredecessor();
2368-
if (PreVectorPH->getNumSuccessors() != 1) {
2369-
assert(PreVectorPH->getNumSuccessors() == 2 && "Expected 2 successors");
2370-
assert(PreVectorPH->getSuccessors()[0] == ScalarPH &&
2371-
"Unexpected successor");
2372-
VPIRBasicBlock *CheckVPIRBB = Plan.createVPIRBasicBlock(CheckIRBB);
2373-
VPBlockUtils::insertOnEdge(PreVectorPH, VectorPHVPB, CheckVPIRBB);
2374-
PreVectorPH = CheckVPIRBB;
2375-
}
2368+
assert(PreVectorPH->getNumSuccessors() == 2 && "Expected 2 successors");
2369+
assert(PreVectorPH->getSuccessors()[0] == ScalarPH && "Unexpected successor");
2370+
VPIRBasicBlock *CheckVPIRBB = Plan.createVPIRBasicBlock(CheckIRBB);
2371+
VPBlockUtils::insertOnEdge(PreVectorPH, VectorPHVPB, CheckVPIRBB);
2372+
PreVectorPH = CheckVPIRBB;
23762373
VPBlockUtils::connectBlocks(PreVectorPH, ScalarPH);
23772374
PreVectorPH->swapSuccessors();
23782375

@@ -2463,9 +2460,6 @@ void InnerLoopVectorizer::emitIterationCountCheck(BasicBlock *Bypass) {
24632460
setBranchWeights(BI, MinItersBypassWeights, /*IsExpected=*/false);
24642461
ReplaceInstWithInst(TCCheckBlock->getTerminator(), &BI);
24652462
LoopBypassBlocks.push_back(TCCheckBlock);
2466-
2467-
// TODO: Wrap LoopVectorPreHeader in VPIRBasicBlock here.
2468-
introduceCheckBlockInVPlan(TCCheckBlock);
24692463
}
24702464

24712465
BasicBlock *InnerLoopVectorizer::emitSCEVChecks(BasicBlock *Bypass) {
@@ -7838,7 +7832,7 @@ DenseMap<const SCEV *, Value *> LoopVectorizationPlanner::executePlan(
78387832

78397833
// 1. Set up the skeleton for vectorization, including vector pre-header and
78407834
// middle block. The vector loop is created during VPlan execution.
7841-
VPBasicBlock *VectorPH = cast<VPBasicBlock>(Entry->getSingleSuccessor());
7835+
VPBasicBlock *VectorPH = cast<VPBasicBlock>(Entry->getSuccessors()[1]);
78427836
State.CFG.PrevBB = ILV.createVectorizedLoopSkeleton();
78437837
if (VectorizingEpilogue)
78447838
VPlanTransforms::removeDeadRecipes(BestVPlan);
@@ -8068,7 +8062,8 @@ EpilogueVectorizerMainLoop::emitIterationCountCheck(BasicBlock *Bypass,
80688062
setBranchWeights(BI, MinItersBypassWeights, /*IsExpected=*/false);
80698063
ReplaceInstWithInst(TCCheckBlock->getTerminator(), &BI);
80708064

8071-
introduceCheckBlockInVPlan(TCCheckBlock);
8065+
if (!ForEpilogue)
8066+
introduceCheckBlockInVPlan(TCCheckBlock);
80728067
return TCCheckBlock;
80738068
}
80748069

@@ -8198,7 +8193,6 @@ EpilogueVectorizerEpilogueLoop::emitMinimumVectorEpilogueIterCountCheck(
81988193
Plan.setEntry(NewEntry);
81998194
// OldEntry is now dead and will be cleaned up when the plan gets destroyed.
82008195

8201-
introduceCheckBlockInVPlan(Insert);
82028196
return Insert;
82038197
}
82048198

@@ -9158,7 +9152,7 @@ static void addScalarResumePhis(VPRecipeBuilder &Builder, VPlan &Plan,
91589152
DenseMap<VPValue *, VPValue *> &IVEndValues) {
91599153
VPTypeAnalysis TypeInfo(Plan.getCanonicalIV()->getScalarType());
91609154
auto *ScalarPH = Plan.getScalarPreheader();
9161-
auto *MiddleVPBB = cast<VPBasicBlock>(ScalarPH->getSinglePredecessor());
9155+
auto *MiddleVPBB = cast<VPBasicBlock>(ScalarPH->getPredecessors()[0]);
91629156
VPRegionBlock *VectorRegion = Plan.getVectorLoopRegion();
91639157
VPBuilder VectorPHBuilder(
91649158
cast<VPBasicBlock>(VectorRegion->getSinglePredecessor()));

llvm/lib/Transforms/Vectorize/VPlan.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3995,7 +3995,8 @@ class VPlan {
39953995
/// that this relies on unneeded branches to the scalar tail loop being
39963996
/// removed.
39973997
bool hasScalarTail() const {
3998-
return getScalarPreheader()->getNumPredecessors() != 0;
3998+
return getScalarPreheader()->getNumPredecessors() != 0 &&
3999+
getScalarPreheader()->getSinglePredecessor() != getEntry();
39994000
}
40004001
};
40014002

llvm/lib/Transforms/Vectorize/VPlanConstruction.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,9 @@ void VPlanTransforms::prepareForVectorization(
546546
if (auto *LatchExitVPB = MiddleVPBB->getSingleSuccessor())
547547
VPBlockUtils::disconnectBlocks(MiddleVPBB, LatchExitVPB);
548548
VPBlockUtils::connectBlocks(MiddleVPBB, ScalarPH);
549+
VPBlockUtils::connectBlocks(Plan.getEntry(), ScalarPH);
550+
Plan.getEntry()->swapSuccessors();
551+
549552
// The exit blocks are unreachable, remove their recipes to make sure no
550553
// users remain that may pessimize transforms.
551554
for (auto *EB : Plan.getExitBlocks()) {
@@ -558,6 +561,9 @@ void VPlanTransforms::prepareForVectorization(
558561
// The connection order corresponds to the operands of the conditional branch,
559562
// with the middle block already connected to the exit block.
560563
VPBlockUtils::connectBlocks(MiddleVPBB, ScalarPH);
564+
// Also connect the entry block to the scalar preheader.
565+
VPBlockUtils::connectBlocks(Plan.getEntry(), ScalarPH);
566+
Plan.getEntry()->swapSuccessors();
561567

562568
auto *ScalarLatchTerm = TheLoop->getLoopLatch()->getTerminator();
563569
// Here we use the same DebugLoc as the scalar loop latch terminator instead

llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1733,7 +1733,7 @@ static void removeBranchOnCondTrue(VPlan &Plan) {
17331733
using namespace llvm::VPlanPatternMatch;
17341734
for (VPBasicBlock *VPBB : VPBlockUtils::blocksOnly<VPBasicBlock>(
17351735
vp_depth_first_shallow(Plan.getEntry()))) {
1736-
if (VPBB->getNumSuccessors() != 2 ||
1736+
if (VPBB->getNumSuccessors() != 2 || isa<VPIRBasicBlock>(VPBB) ||
17371737
!match(&VPBB->back(), m_BranchOnCond(m_True())))
17381738
continue;
17391739

0 commit comments

Comments
 (0)