Skip to content

Commit 048cf6d

Browse files
committed
Address review comments
1 parent 7d354a6 commit 048cf6d

File tree

5 files changed

+35
-19
lines changed

5 files changed

+35
-19
lines changed

llvm/docs/Vectorizers.rst

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -406,8 +406,10 @@ When vectorizing a loop with a single early exit, the loop blocks following the
406406
early exit are predicated and the vector loop will always exit via the latch.
407407
If the early exit has been taken, the vector loop's successor block
408408
(``middle.split`` below) branches to the early exit block via an intermediate
409-
block (``vector.early.exit`` below). Otherwise ``middle.block`` selects between
410-
the exit block from the latch or the scalar remainder loop.
409+
block (``vector.early.exit`` below). This intermediate block is responsible for
410+
calculating any exit values of loop-defined variables that are used in the
411+
early exit block. Otherwise, ``middle.block`` selects between the exit block
412+
from the latch or the scalar remainder loop.
411413

412414
.. image:: vplan-early-exit.png
413415

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7818,9 +7818,6 @@ DenseMap<const SCEV *, Value *> LoopVectorizationPlanner::executePlan(
78187818
State.LVer->prepareNoAliasMetadata();
78197819
}
78207820

7821-
// Set the uncountable early exit block in the VPTransformState.
7822-
State.CFG.UncountableEarlyExitBB = ILV.Legal->getUncountableEarlyExitBlock();
7823-
78247821
ILV.printDebugTracesAtStart();
78257822

78267823
//===------------------------------------------------===//
@@ -9278,7 +9275,7 @@ addUsersInExitBlocks(VPlan &Plan,
92789275
if (PredVPBB != MiddleVPBB) {
92799276
assert(ExitIRI->getParent()->getNumPredecessors() <= 2);
92809277

9281-
// Cache the early exit mask
9278+
// Lookup and cache the early exit mask.
92829279
if (!EarlyExitMask) {
92839280
VPBasicBlock *MiddleSplitVPBB =
92849281
cast<VPBasicBlock>(VectorEarlyExitVPBB->getSinglePredecessor());

llvm/lib/Transforms/Vectorize/VPlan.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -501,16 +501,15 @@ void VPBasicBlock::execute(VPTransformState *State) {
501501
UnreachableInst *Terminator = State->Builder.CreateUnreachable();
502502
// Register NewBB in its loop. In innermost loops its the same for all
503503
// BB's.
504-
if (this == State->Plan->getEarlyExit()) {
505-
// If this is the vector early exit block then it has a single successor,
506-
// which is the uncountable early exit block of the original loop. The
507-
// parent loop for the exit block may not be the same as the parent loop
508-
// of the vectorised loop, so we have to treat this differently.
509-
Loop *EEL = State->LI->getLoopFor(State->CFG.UncountableEarlyExitBB);
510-
if (EEL)
511-
EEL->addBasicBlockToLoop(NewBB, *State->LI);
512-
} else if (State->CurrentParentLoop)
513-
State->CurrentParentLoop->addBasicBlockToLoop(NewBB, *State->LI);
504+
Loop *ParentLoop = State->CurrentParentLoop;
505+
// If this block has a sole successor that is an exit block then it needs
506+
// adding to the same parent loop as the exit block.
507+
VPBlockBase *SuccVPBB = getSingleSuccessor();
508+
if (SuccVPBB && State->Plan->isExitBlock(SuccVPBB))
509+
ParentLoop = State->LI->getLoopFor(
510+
cast<VPIRBasicBlock>(SuccVPBB)->getIRBasicBlock());
511+
if (ParentLoop)
512+
ParentLoop->addBasicBlockToLoop(NewBB, *State->LI);
514513
State->Builder.SetInsertPoint(Terminator);
515514

516515
State->CFG.PrevBB = NewBB;

llvm/lib/Transforms/Vectorize/VPlan.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -347,9 +347,6 @@ struct VPTransformState {
347347
/// vector loop.
348348
BasicBlock *ExitBB = nullptr;
349349

350-
/// The uncountable early exit block in the original scalar loop.
351-
BasicBlock *UncountableEarlyExitBB = nullptr;
352-
353350
/// A mapping of each VPBasicBlock to the corresponding BasicBlock. In case
354351
/// of replication, maps the BasicBlock of the last replica created.
355352
SmallDenseMap<VPBasicBlock *, BasicBlock *> VPBB2IRBB;
@@ -3976,6 +3973,9 @@ class VPlan {
39763973
/// of VPBlockShallowTraversalWrapper.
39773974
auto getExitBlocks();
39783975

3976+
/// Returns true if \p VPBB is an exit block.
3977+
bool isExitBlock(VPBlockBase *VPBB);
3978+
39793979
/// The trip count of the original loop.
39803980
VPValue *getTripCount() const {
39813981
assert(TripCount && "trip count needs to be set before accessing it");

llvm/lib/Transforms/Vectorize/VPlanCFG.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,24 @@ template <> struct GraphTraits<VPlan *> {
306306
}
307307
};
308308

309+
inline bool VPlan::isExitBlock(VPBlockBase *VPBB) {
310+
if (!isa<VPIRBasicBlock>(VPBB) || VPBB->getNumSuccessors() ||
311+
VPBB == getScalarHeader())
312+
return false;
313+
314+
VPRegionBlock *RegionBlock = getVectorLoopRegion();
315+
if (!RegionBlock)
316+
return false;
317+
318+
// The block must be a successor of the region block.
319+
for (auto *OtherVPBB :
320+
vp_depth_first_shallow(RegionBlock->getSingleSuccessor()))
321+
if (OtherVPBB == VPBB)
322+
return true;
323+
324+
return false;
325+
}
326+
309327
inline auto VPlan::getExitBlocks() {
310328
VPBlockBase *ScalarHeader = getScalarHeader();
311329
return make_filter_range(

0 commit comments

Comments
 (0)