Skip to content

Commit b06a45c

Browse files
committed
[VPlan] Add all blocks to outer loop if present during ::execute (NFCI).
This ensures that all blocks created during VPlan execution are properly added to an enclosing loop, if present. Split off from #108378 and also needed once more of the skeleton blocks are created directly via VPlan. This also allows removing the custom logic for early-exit loop vectorization added as part of #117008.
1 parent 71d6b0b commit b06a45c

File tree

4 files changed

+18
-30
lines changed

4 files changed

+18
-30
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2961,22 +2961,6 @@ void InnerLoopVectorizer::fixVectorizedLoop(VPTransformState &State) {
29612961
PSE.getSE()->forgetLoop(OrigLoop);
29622962
PSE.getSE()->forgetBlockAndLoopDispositions();
29632963

2964-
// When dealing with uncountable early exits we create middle.split blocks
2965-
// between the vector loop region and the exit block. These blocks need
2966-
// adding to any outer loop.
2967-
VPRegionBlock *VectorRegion = State.Plan->getVectorLoopRegion();
2968-
Loop *OuterLoop = OrigLoop->getParentLoop();
2969-
if (Legal->hasUncountableEarlyExit() && OuterLoop) {
2970-
VPBasicBlock *MiddleVPBB = State.Plan->getMiddleBlock();
2971-
VPBlockBase *PredVPBB = MiddleVPBB->getSinglePredecessor();
2972-
while (PredVPBB && PredVPBB != VectorRegion) {
2973-
BasicBlock *MiddleSplitBB =
2974-
State.CFG.VPBB2IRBB[cast<VPBasicBlock>(PredVPBB)];
2975-
OuterLoop->addBasicBlockToLoop(MiddleSplitBB, *LI);
2976-
PredVPBB = PredVPBB->getSinglePredecessor();
2977-
}
2978-
}
2979-
29802964
// After vectorization, the exit blocks of the original loop will have
29812965
// additional predecessors. Invalidate SCEVs for the exit phis in case SE
29822966
// looked through single-entry phis.
@@ -3007,6 +2991,7 @@ void InnerLoopVectorizer::fixVectorizedLoop(VPTransformState &State) {
30072991
for (Instruction *PI : PredicatedInstructions)
30082992
sinkScalarOperands(&*PI);
30092993

2994+
VPRegionBlock *VectorRegion = State.Plan->getVectorLoopRegion();
30102995
VPBasicBlock *HeaderVPBB = VectorRegion->getEntryBasicBlock();
30112996
BasicBlock *HeaderBB = State.CFG.VPBB2IRBB[HeaderVPBB];
30122997

@@ -7715,7 +7700,8 @@ DenseMap<const SCEV *, Value *> LoopVectorizationPlanner::executePlan(
77157700

77167701
// Perform the actual loop transformation.
77177702
VPTransformState State(&TTI, BestVF, BestUF, LI, DT, ILV.Builder, &ILV,
7718-
&BestVPlan, Legal->getWidestInductionType());
7703+
&BestVPlan, OrigLoop->getParentLoop(),
7704+
Legal->getWidestInductionType());
77197705

77207706
#ifdef EXPENSIVE_CHECKS
77217707
assert(DT->verify(DominatorTree::VerificationLevel::Fast));

llvm/lib/Transforms/Vectorize/VPlan.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -216,9 +216,10 @@ VPTransformState::VPTransformState(const TargetTransformInfo *TTI,
216216
ElementCount VF, unsigned UF, LoopInfo *LI,
217217
DominatorTree *DT, IRBuilderBase &Builder,
218218
InnerLoopVectorizer *ILV, VPlan *Plan,
219-
Type *CanonicalIVTy)
219+
Loop *CurrentParentLoop, Type *CanonicalIVTy)
220220
: TTI(TTI), VF(VF), CFG(DT), LI(LI), Builder(Builder), ILV(ILV), Plan(Plan),
221-
LVer(nullptr), TypeAnalysis(CanonicalIVTy) {}
221+
CurrentParentLoop(CurrentParentLoop), LVer(nullptr),
222+
TypeAnalysis(CanonicalIVTy) {}
222223

223224
Value *VPTransformState::get(VPValue *Def, const VPLane &Lane) {
224225
if (Def->isLiveIn())
@@ -502,8 +503,8 @@ void VPBasicBlock::execute(VPTransformState *State) {
502503
UnreachableInst *Terminator = State->Builder.CreateUnreachable();
503504
// Register NewBB in its loop. In innermost loops its the same for all
504505
// BB's.
505-
if (State->CurrentVectorLoop)
506-
State->CurrentVectorLoop->addBasicBlockToLoop(NewBB, *State->LI);
506+
if (State->CurrentParentLoop)
507+
State->CurrentParentLoop->addBasicBlockToLoop(NewBB, *State->LI);
507508
State->Builder.SetInsertPoint(Terminator);
508509

509510
State->CFG.PrevBB = NewBB;
@@ -713,25 +714,25 @@ void VPRegionBlock::execute(VPTransformState *State) {
713714

714715
if (!isReplicator()) {
715716
// Create and register the new vector loop.
716-
Loop *PrevLoop = State->CurrentVectorLoop;
717-
State->CurrentVectorLoop = State->LI->AllocateLoop();
717+
Loop *PrevLoop = State->CurrentParentLoop;
718+
State->CurrentParentLoop = State->LI->AllocateLoop();
718719
BasicBlock *VectorPH = State->CFG.VPBB2IRBB[getPreheaderVPBB()];
719720
Loop *ParentLoop = State->LI->getLoopFor(VectorPH);
720721

721722
// Insert the new loop into the loop nest and register the new basic blocks
722723
// before calling any utilities such as SCEV that require valid LoopInfo.
723724
if (ParentLoop)
724-
ParentLoop->addChildLoop(State->CurrentVectorLoop);
725+
ParentLoop->addChildLoop(State->CurrentParentLoop);
725726
else
726-
State->LI->addTopLevelLoop(State->CurrentVectorLoop);
727+
State->LI->addTopLevelLoop(State->CurrentParentLoop);
727728

728729
// Visit the VPBlocks connected to "this", starting from it.
729730
for (VPBlockBase *Block : RPOT) {
730731
LLVM_DEBUG(dbgs() << "LV: VPBlock in RPO " << Block->getName() << '\n');
731732
Block->execute(State);
732733
}
733734

734-
State->CurrentVectorLoop = PrevLoop;
735+
State->CurrentParentLoop = PrevLoop;
735736
return;
736737
}
737738

llvm/lib/Transforms/Vectorize/VPlan.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,8 @@ class VPLane {
236236
struct VPTransformState {
237237
VPTransformState(const TargetTransformInfo *TTI, ElementCount VF, unsigned UF,
238238
LoopInfo *LI, DominatorTree *DT, IRBuilderBase &Builder,
239-
InnerLoopVectorizer *ILV, VPlan *Plan, Type *CanonicalIVTy);
239+
InnerLoopVectorizer *ILV, VPlan *Plan,
240+
Loop *CurrentParentLoop, Type *CanonicalIVTy);
240241
/// Target Transform Info.
241242
const TargetTransformInfo *TTI;
242243

@@ -373,8 +374,8 @@ struct VPTransformState {
373374
/// Pointer to the VPlan code is generated for.
374375
VPlan *Plan;
375376

376-
/// The loop object for the current parent region, or nullptr.
377-
Loop *CurrentVectorLoop = nullptr;
377+
/// The parent loop object for the current scope, or nullptr.
378+
Loop *CurrentParentLoop = nullptr;
378379

379380
/// LoopVersioning. It's only set up (non-null) if memchecks were
380381
/// used.

llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3365,7 +3365,7 @@ void VPReductionPHIRecipe::execute(VPTransformState &State) {
33653365
: VectorType::get(StartV->getType(), State.VF);
33663366

33673367
BasicBlock *HeaderBB = State.CFG.PrevBB;
3368-
assert(State.CurrentVectorLoop->getHeader() == HeaderBB &&
3368+
assert(State.CurrentParentLoop->getHeader() == HeaderBB &&
33693369
"recipe must be in the vector loop header");
33703370
auto *Phi = PHINode::Create(VecTy, 2, "vec.phi");
33713371
Phi->insertBefore(HeaderBB->getFirstInsertionPt());

0 commit comments

Comments
 (0)