Skip to content

Commit e0453a8

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 6bac414 commit e0453a8

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
@@ -7692,8 +7692,7 @@ DenseMap<const SCEV *, Value *> LoopVectorizationPlanner::executePlan(
76927692
BestVPlan.execute(&State);
76937693

76947694
// 2.5 Collect reduction resume values.
7695-
auto *ExitVPBB =
7696-
cast<VPBasicBlock>(BestVPlan.getVectorLoopRegion()->getSingleSuccessor());
7695+
auto *ExitVPBB = BestVPlan.getMiddleBlock();
76977696
for (VPRecipeBase &R : *ExitVPBB) {
76987697
createAndCollectMergePhiForReduction(
76997698
dyn_cast<VPInstruction>(&R), State, OrigLoop,
@@ -8778,8 +8777,7 @@ static void addCanonicalIVRecipes(VPlan &Plan, Type *IdxTy, bool HasNUW,
87788777
static SetVector<VPIRInstruction *> collectUsersInExitBlock(
87798778
Loop *OrigLoop, VPRecipeBuilder &Builder, VPlan &Plan,
87808779
const MapVector<PHINode *, InductionDescriptor> &Inductions) {
8781-
auto *MiddleVPBB =
8782-
cast<VPBasicBlock>(Plan.getVectorLoopRegion()->getSingleSuccessor());
8780+
auto *MiddleVPBB = Plan.getMiddleBlock();
87838781
// No edge from the middle block to the unique exit block has been inserted
87848782
// and there is nothing to fix from vector loop; phis should have incoming
87858783
// from scalar loop only.
@@ -8825,8 +8823,7 @@ addUsersInExitBlock(VPlan &Plan,
88258823
if (ExitUsersToFix.empty())
88268824
return;
88278825

8828-
auto *MiddleVPBB =
8829-
cast<VPBasicBlock>(Plan.getVectorLoopRegion()->getSingleSuccessor());
8826+
auto *MiddleVPBB = Plan.getMiddleBlock();
88308827
VPBuilder B(MiddleVPBB, MiddleVPBB->getFirstNonPhi());
88318828

88328829
// Introduce extract for exiting values and update the VPIRInstructions
@@ -8863,7 +8860,7 @@ static void addLiveOutsForFirstOrderRecurrences(
88638860
// TODO: Should be replaced by
88648861
// Plan->getScalarLoopRegion()->getSinglePredecessor() in the future once the
88658862
// scalar region is modeled as well.
8866-
auto *MiddleVPBB = cast<VPBasicBlock>(VectorRegion->getSingleSuccessor());
8863+
auto *MiddleVPBB = Plan.getMiddleBlock();
88678864
VPBasicBlock *ScalarPHVPBB = nullptr;
88688865
if (MiddleVPBB->getNumSuccessors() == 2) {
88698866
// Order is strict: first is the exit block, second is the scalar preheader.
@@ -9065,8 +9062,7 @@ LoopVectorizationPlanner::tryToBuildVPlanWithVPRecipes(VFRange &Range) {
90659062
bool NeedsBlends = BB != HeaderBB && !BB->phis().empty();
90669063
return Legal->blockNeedsPredication(BB) || NeedsBlends;
90679064
});
9068-
auto *MiddleVPBB =
9069-
cast<VPBasicBlock>(Plan->getVectorLoopRegion()->getSingleSuccessor());
9065+
auto *MiddleVPBB = Plan->getMiddleBlock();
90709066
VPBasicBlock::iterator MBIP = MiddleVPBB->getFirstNonPhi();
90719067
for (BasicBlock *BB : make_range(DFS.beginRPO(), DFS.endRPO())) {
90729068
// Relevant instructions from basic block BB will be grouped into VPRecipe
@@ -9277,8 +9273,7 @@ void LoopVectorizationPlanner::adjustRecipesForReductions(
92779273
using namespace VPlanPatternMatch;
92789274
VPRegionBlock *VectorLoopRegion = Plan->getVectorLoopRegion();
92799275
VPBasicBlock *Header = VectorLoopRegion->getEntryBasicBlock();
9280-
VPBasicBlock *MiddleVPBB =
9281-
cast<VPBasicBlock>(VectorLoopRegion->getSingleSuccessor());
9276+
VPBasicBlock *MiddleVPBB = Plan->getMiddleBlock();
92829277
for (VPRecipeBase &R : Header->phis()) {
92839278
auto *PhiR = dyn_cast<VPReductionPHIRecipe>(&R);
92849279
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
@@ -3689,6 +3689,14 @@ class VPlan {
36893689
VPBasicBlock *getEntry() { return Entry; }
36903690
const VPBasicBlock *getEntry() const { return Entry; }
36913691

3692+
/// Methods to support access to the middle block.
3693+
const VPBasicBlock *getMiddleBlock() const {
3694+
return cast<VPBasicBlock>(getVectorLoopRegion()->getSingleSuccessor());
3695+
}
3696+
VPBasicBlock *getMiddleBlock() {
3697+
return cast<VPBasicBlock>(getVectorLoopRegion()->getSingleSuccessor());
3698+
}
3699+
36923700
/// The trip count of the original loop.
36933701
VPValue *getTripCount() const {
36943702
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
@@ -212,8 +212,7 @@ bool VPRecipeBase::mayHaveSideEffects() const {
212212

213213
void VPLiveOut::fixPhi(VPlan &Plan, VPTransformState &State) {
214214
VPValue *ExitValue = getOperand(0);
215-
VPBasicBlock *MiddleVPBB =
216-
cast<VPBasicBlock>(Plan.getVectorLoopRegion()->getSingleSuccessor());
215+
VPBasicBlock *MiddleVPBB = Plan.getMiddleBlock();
217216
VPRecipeBase *ExitingRecipe = ExitValue->getDefiningRecipe();
218217
auto *ExitingVPBB = ExitingRecipe ? ExitingRecipe->getParent() : nullptr;
219218
// 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)