Skip to content

Commit 2b54b69

Browse files
committed
[VPlan][NFC] Add new getMiddleBlock interface to VPlan
This work is in preparation for PRs #112138 and #88385 where the middle block is not guaranteed to be the immediate successor to the region block. I've simply add new getMiddleBlock() interfaces to VPlan that for now just return cast<VPBasicBlock>(VectorRegion->getSingleSuccessor()) Once PR #112138 lands we'll need to do more work to discover the middle block.
1 parent c72a751 commit 2b54b69

File tree

3 files changed

+17
-13
lines changed

3 files changed

+17
-13
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7703,8 +7703,7 @@ DenseMap<const SCEV *, Value *> LoopVectorizationPlanner::executePlan(
77037703
BestVPlan.execute(&State);
77047704

77057705
// 2.5 Collect reduction resume values.
7706-
auto *ExitVPBB =
7707-
cast<VPBasicBlock>(BestVPlan.getVectorLoopRegion()->getSingleSuccessor());
7706+
auto *ExitVPBB = BestVPlan.getMiddleBlock();
77087707
if (VectorizingEpilogue)
77097708
for (VPRecipeBase &R : *ExitVPBB) {
77107709
fixReductionScalarResumeWhenVectorizingEpilog(
@@ -8830,8 +8829,7 @@ static void addScalarResumePhis(VPRecipeBuilder &Builder, VPlan &Plan) {
88308829
static SetVector<VPIRInstruction *> collectUsersInExitBlock(
88318830
Loop *OrigLoop, VPRecipeBuilder &Builder, VPlan &Plan,
88328831
const MapVector<PHINode *, InductionDescriptor> &Inductions) {
8833-
auto *MiddleVPBB =
8834-
cast<VPBasicBlock>(Plan.getVectorLoopRegion()->getSingleSuccessor());
8832+
auto *MiddleVPBB = Plan.getMiddleBlock();
88358833
// No edge from the middle block to the unique exit block has been inserted
88368834
// and there is nothing to fix from vector loop; phis should have incoming
88378835
// from scalar loop only.
@@ -8876,8 +8874,7 @@ addUsersInExitBlock(VPlan &Plan,
88768874
if (ExitUsersToFix.empty())
88778875
return;
88788876

8879-
auto *MiddleVPBB =
8880-
cast<VPBasicBlock>(Plan.getVectorLoopRegion()->getSingleSuccessor());
8877+
auto *MiddleVPBB = Plan.getMiddleBlock();
88818878
VPBuilder B(MiddleVPBB, MiddleVPBB->getFirstNonPhi());
88828879

88838880
// Introduce extract for exiting values and update the VPIRInstructions
@@ -8905,7 +8902,7 @@ static void addExitUsersForFirstOrderRecurrences(
89058902
VPlan &Plan, SetVector<VPIRInstruction *> &ExitUsersToFix) {
89068903
VPRegionBlock *VectorRegion = Plan.getVectorLoopRegion();
89078904
auto *ScalarPHVPBB = Plan.getScalarPreheader();
8908-
auto *MiddleVPBB = cast<VPBasicBlock>(VectorRegion->getSingleSuccessor());
8905+
auto *MiddleVPBB = Plan.getMiddleBlock();
89098906
VPBuilder ScalarPHBuilder(ScalarPHVPBB);
89108907
VPBuilder MiddleBuilder(MiddleVPBB, MiddleVPBB->getFirstNonPhi());
89118908
VPValue *TwoVPV = Plan.getOrAddLiveIn(
@@ -9085,8 +9082,7 @@ LoopVectorizationPlanner::tryToBuildVPlanWithVPRecipes(VFRange &Range) {
90859082
bool NeedsBlends = BB != HeaderBB && !BB->phis().empty();
90869083
return Legal->blockNeedsPredication(BB) || NeedsBlends;
90879084
});
9088-
auto *MiddleVPBB =
9089-
cast<VPBasicBlock>(Plan->getVectorLoopRegion()->getSingleSuccessor());
9085+
auto *MiddleVPBB = Plan->getMiddleBlock();
90909086
VPBasicBlock::iterator MBIP = MiddleVPBB->getFirstNonPhi();
90919087
for (BasicBlock *BB : make_range(DFS.beginRPO(), DFS.endRPO())) {
90929088
// Relevant instructions from basic block BB will be grouped into VPRecipe
@@ -9303,8 +9299,7 @@ void LoopVectorizationPlanner::adjustRecipesForReductions(
93039299
using namespace VPlanPatternMatch;
93049300
VPRegionBlock *VectorLoopRegion = Plan->getVectorLoopRegion();
93059301
VPBasicBlock *Header = VectorLoopRegion->getEntryBasicBlock();
9306-
VPBasicBlock *MiddleVPBB =
9307-
cast<VPBasicBlock>(VectorLoopRegion->getSingleSuccessor());
9302+
VPBasicBlock *MiddleVPBB = Plan->getMiddleBlock();
93089303
for (VPRecipeBase &R : Header->phis()) {
93099304
auto *PhiR = dyn_cast<VPReductionPHIRecipe>(&R);
93109305
if (!PhiR || !PhiR->isInLoop() || (MinVF.isScalar() && !PhiR->isOrdered()))

llvm/lib/Transforms/Vectorize/VPlan.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,8 +1034,7 @@ void VPlan::execute(VPTransformState *State) {
10341034
// skeleton creation, so we can only create the VPIRBasicBlocks now during
10351035
// VPlan execution rather than earlier during VPlan construction.
10361036
BasicBlock *MiddleBB = State->CFG.ExitBB;
1037-
VPBasicBlock *MiddleVPBB =
1038-
cast<VPBasicBlock>(getVectorLoopRegion()->getSingleSuccessor());
1037+
VPBasicBlock *MiddleVPBB = getMiddleBlock();
10391038
BasicBlock *ScalarPh = MiddleBB->getSingleSuccessor();
10401039
replaceVPBBWithIRVPBB(getScalarPreheader(), ScalarPh);
10411040
replaceVPBBWithIRVPBB(MiddleVPBB, MiddleBB);

llvm/lib/Transforms/Vectorize/VPlan.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3748,6 +3748,16 @@ class VPlan {
37483748
return cast<VPBasicBlock>(ScalarHeader->getSinglePredecessor());
37493749
}
37503750

3751+
/// Returns the 'middle' block of the plan, that is the block that selects
3752+
/// whether to execute the scalar tail loop or the exit block from the loop
3753+
/// latch.
3754+
const VPBasicBlock *getMiddleBlock() const {
3755+
return cast<VPBasicBlock>(getVectorLoopRegion()->getSingleSuccessor());
3756+
}
3757+
VPBasicBlock *getMiddleBlock() {
3758+
return cast<VPBasicBlock>(getVectorLoopRegion()->getSingleSuccessor());
3759+
}
3760+
37513761
/// The trip count of the original loop.
37523762
VPValue *getTripCount() const {
37533763
assert(TripCount && "trip count needs to be set before accessing it");

0 commit comments

Comments
 (0)