Skip to content

Commit a1f8aa6

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 89f2d50 commit a1f8aa6

File tree

5 files changed

+18
-17
lines changed

5 files changed

+18
-17
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

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

77087708
// 2.5 Collect reduction resume values.
7709-
auto *ExitVPBB =
7710-
cast<VPBasicBlock>(BestVPlan.getVectorLoopRegion()->getSingleSuccessor());
7709+
auto *ExitVPBB = BestVPlan.getMiddleBlock();
77117710
if (VectorizingEpilogue)
77127711
for (VPRecipeBase &R : *ExitVPBB) {
77137712
fixReductionScalarResumeWhenVectorizingEpilog(
@@ -8798,8 +8797,7 @@ static void addCanonicalIVRecipes(VPlan &Plan, Type *IdxTy, bool HasNUW,
87988797
static SetVector<VPIRInstruction *> collectUsersInExitBlock(
87998798
Loop *OrigLoop, VPRecipeBuilder &Builder, VPlan &Plan,
88008799
const MapVector<PHINode *, InductionDescriptor> &Inductions) {
8801-
auto *MiddleVPBB =
8802-
cast<VPBasicBlock>(Plan.getVectorLoopRegion()->getSingleSuccessor());
8800+
auto *MiddleVPBB = Plan.getMiddleBlock();
88038801
// No edge from the middle block to the unique exit block has been inserted
88048802
// and there is nothing to fix from vector loop; phis should have incoming
88058803
// from scalar loop only.
@@ -8845,8 +8843,7 @@ addUsersInExitBlock(VPlan &Plan,
88458843
if (ExitUsersToFix.empty())
88468844
return;
88478845

8848-
auto *MiddleVPBB =
8849-
cast<VPBasicBlock>(Plan.getVectorLoopRegion()->getSingleSuccessor());
8846+
auto *MiddleVPBB = Plan.getMiddleBlock();
88508847
VPBuilder B(MiddleVPBB, MiddleVPBB->getFirstNonPhi());
88518848

88528849
// Introduce extract for exiting values and update the VPIRInstructions
@@ -8883,7 +8880,7 @@ static void addLiveOutsForFirstOrderRecurrences(
88838880
// TODO: Should be replaced by
88848881
// Plan->getScalarLoopRegion()->getSinglePredecessor() in the future once the
88858882
// scalar region is modeled as well.
8886-
auto *MiddleVPBB = cast<VPBasicBlock>(VectorRegion->getSingleSuccessor());
8883+
auto *MiddleVPBB = Plan.getMiddleBlock();
88878884
VPBasicBlock *ScalarPHVPBB = nullptr;
88888885
if (MiddleVPBB->getNumSuccessors() == 2) {
88898886
// Order is strict: first is the exit block, second is the scalar preheader.
@@ -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
@@ -9305,8 +9301,7 @@ void LoopVectorizationPlanner::adjustRecipesForReductions(
93059301
using namespace VPlanPatternMatch;
93069302
VPRegionBlock *VectorLoopRegion = Plan->getVectorLoopRegion();
93079303
VPBasicBlock *Header = VectorLoopRegion->getEntryBasicBlock();
9308-
VPBasicBlock *MiddleVPBB =
9309-
cast<VPBasicBlock>(VectorLoopRegion->getSingleSuccessor());
9304+
VPBasicBlock *MiddleVPBB = Plan->getMiddleBlock();
93109305
for (VPRecipeBase &R : Header->phis()) {
93119306
auto *PhiR = dyn_cast<VPReductionPHIRecipe>(&R);
93129307
if (!PhiR || !PhiR->isInLoop() || (MinVF.isScalar() && !PhiR->isOrdered()))

llvm/lib/Transforms/Vectorize/VPlan.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,8 +1030,8 @@ void VPlan::execute(VPTransformState *State) {
10301030
// skeleton creation, so we can only create the VPIRBasicBlocks now during
10311031
// VPlan execution rather than earlier during VPlan construction.
10321032
BasicBlock *MiddleBB = State->CFG.ExitBB;
1033-
VPBasicBlock *MiddleVPBB =
1034-
cast<VPBasicBlock>(getVectorLoopRegion()->getSingleSuccessor());
1033+
VPBasicBlock *MiddleVPBB = getMiddleBlock();
1034+
10351035
// Find the VPBB for the scalar preheader, relying on the current structure
10361036
// when creating the middle block and its successrs: if there's a single
10371037
// predecessor, it must be the scalar preheader. Otherwise, the second

llvm/lib/Transforms/Vectorize/VPlan.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3773,6 +3773,14 @@ class VPlan {
37733773
VPBasicBlock *getEntry() { return Entry; }
37743774
const VPBasicBlock *getEntry() const { return Entry; }
37753775

3776+
/// Methods to support access to the middle block.
3777+
const VPBasicBlock *getMiddleBlock() const {
3778+
return cast<VPBasicBlock>(getVectorLoopRegion()->getSingleSuccessor());
3779+
}
3780+
VPBasicBlock *getMiddleBlock() {
3781+
return cast<VPBasicBlock>(getVectorLoopRegion()->getSingleSuccessor());
3782+
}
3783+
37763784
/// The trip count of the original loop.
37773785
VPValue *getTripCount() const {
37783786
assert(TripCount && "trip count needs to be set before accessing it");

llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,7 @@ bool VPRecipeBase::mayHaveSideEffects() const {
216216

217217
void VPLiveOut::fixPhi(VPlan &Plan, VPTransformState &State) {
218218
VPValue *ExitValue = getOperand(0);
219-
VPBasicBlock *MiddleVPBB =
220-
cast<VPBasicBlock>(Plan.getVectorLoopRegion()->getSingleSuccessor());
219+
VPBasicBlock *MiddleVPBB = Plan.getMiddleBlock();
221220
VPRecipeBase *ExitingRecipe = ExitValue->getDefiningRecipe();
222221
auto *ExitingVPBB = ExitingRecipe ? ExitingRecipe->getParent() : nullptr;
223222
// Values leaving the vector loop reach live out phi's in the exiting block

llvm/lib/Transforms/Vectorize/VPlanVerifier.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,7 @@ bool VPlanVerifier::verifyVPBasicBlock(const VPBasicBlock *VPBB) {
248248
return false;
249249
}
250250

251-
VPBlockBase *MiddleBB =
252-
IRBB->getPlan()->getVectorLoopRegion()->getSingleSuccessor();
251+
const VPBasicBlock *MiddleBB = IRBB->getPlan()->getMiddleBlock();
253252
if (IRBB != IRBB->getPlan()->getPreheader() &&
254253
IRBB->getSinglePredecessor() != MiddleBB) {
255254
errs() << "VPIRBasicBlock can only be used as pre-header or a successor of "

0 commit comments

Comments
 (0)