Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9132,6 +9132,10 @@ collectUsersInExitBlocks(Loop *OrigLoop, VPRecipeBuilder &Builder,
VPlan &Plan) {
SetVector<VPIRInstruction *> ExitUsersToFix;
for (VPIRBasicBlock *ExitVPBB : Plan.getExitBlocks()) {
// Nothing to do for unreachable exit blocks.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All exit blocks are expected to be reachable, in the long run.
The exit blocks of Plan include only exit.block and optionally latch.exit, as depicted in
https://llvm.org/docs/Vectorizers.html#epilogue-vectorization and https://llvm.org/docs/Vectorizers.html#early-exit-vectorization, where the scalar header is expected to eventually reach the above exit blocks.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, once we connect the exits also to the scalar loop

if (ExitVPBB->getNumPredecessors() == 0)
continue;

for (VPRecipeBase &R : *ExitVPBB) {
auto *ExitIRI = dyn_cast<VPIRInstruction>(&R);
if (!ExitIRI)
Expand Down
15 changes: 14 additions & 1 deletion llvm/lib/Transforms/Vectorize/VPlan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -850,6 +850,11 @@ void VPRegionBlock::print(raw_ostream &O, const Twine &Indent,
VPlan::VPlan(Loop *L) {
setEntry(createVPIRBasicBlock(L->getLoopPreheader()));
ScalarHeader = createVPIRBasicBlock(L->getHeader());

SmallVector<BasicBlock *> IRExitBlocks;
L->getExitBlocks(IRExitBlocks);
for (BasicBlock *EB : IRExitBlocks)
ExitBlocks.push_back(createVPIRBasicBlock(EB));
}

VPlan::~VPlan() {
Expand Down Expand Up @@ -931,7 +936,7 @@ VPlanPtr VPlan::createInitialVPlan(Type *InductionTy,
// we unconditionally branch to the scalar preheader. Do nothing.
// 3) Otherwise, construct a runtime check.
BasicBlock *IRExitBlock = TheLoop->getUniqueLatchExitBlock();
auto *VPExitBlock = Plan->createVPIRBasicBlock(IRExitBlock);
VPIRBasicBlock *VPExitBlock = Plan->getExitBlock(IRExitBlock);
// The connection order corresponds to the operands of the conditional branch.
VPBlockUtils::insertBlockAfter(VPExitBlock, MiddleVPBB);
VPBlockUtils::connectBlocks(MiddleVPBB, ScalarPH);
Expand Down Expand Up @@ -983,6 +988,14 @@ void VPlan::prepareToExecute(Value *TripCountV, Value *VectorTripCountV,
}
}

VPIRBasicBlock *VPlan::getExitBlock(BasicBlock *IRBB) const {
auto Iter = find_if(getExitBlocks(), [IRBB](const VPIRBasicBlock *VPIRBB) {
return VPIRBB->getIRBasicBlock() == IRBB;
});
assert(Iter != getExitBlocks().end() && "no exit block found");
return *Iter;
}

bool VPlan::isExitBlock(VPBlockBase *VPBB) {
return isa<VPIRBasicBlock>(VPBB) && VPBB->getNumSuccessors() == 0;
}
Expand Down
17 changes: 12 additions & 5 deletions llvm/lib/Transforms/Vectorize/VPlan.h
Original file line number Diff line number Diff line change
Expand Up @@ -3426,6 +3426,11 @@ class VPlan {
/// VPIRBasicBlock wrapping the header of the original scalar loop.
VPIRBasicBlock *ScalarHeader;

/// Immutable list of VPIRBasicBlocks wrapping the exit blocks of the original
/// scalar loop. Note that some exit blocks may be unreachable at the moment,
/// e.g. if the scalar epilogue always executes.
SmallVector<VPIRBasicBlock *, 2> ExitBlocks;

/// Holds the VFs applicable to this VPlan.
SmallSetVector<ElementCount, 2> VFs;

Expand Down Expand Up @@ -3559,11 +3564,13 @@ class VPlan {
/// Return the VPIRBasicBlock wrapping the header of the scalar loop.
VPIRBasicBlock *getScalarHeader() const { return ScalarHeader; }

/// Return an iterator range over the VPIRBasicBlock wrapping the exit blocks
/// of the VPlan, that is leaf nodes except the scalar header. Defined in
/// VPlanHCFG, as the definition of the type needs access to the definitions
/// of VPBlockShallowTraversalWrapper.
auto getExitBlocks();
/// Return an ArrayRef containing VPIRBasicBlocks wrapping the exit blocks of
/// the original scalar loop.
ArrayRef<VPIRBasicBlock *> getExitBlocks() const { return ExitBlocks; }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This admittedly may be more involved if ExitBlocks are heald as an IRBBToVPExitBlock map.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left as vector for now, as getExitBlock should not be needed soonish and be replaced by connecting the blocks during initial VPlan construction.


/// Return the VPIRBasicBlock corresponding to \p IRBB. \p IRBB must be an
/// exit block.
Comment on lines +3571 to +3572
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"must" - if IRBB is not an exit block, an assert is triggered, or null is returned?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added an assert, rather than just relying on dereferenceing the end iterator.

VPIRBasicBlock *getExitBlock(BasicBlock *IRBB) const;

/// Returns true if \p VPBB is an exit block.
bool isExitBlock(VPBlockBase *VPBB);
Expand Down
9 changes: 0 additions & 9 deletions llvm/lib/Transforms/Vectorize/VPlanCFG.h
Original file line number Diff line number Diff line change
Expand Up @@ -307,15 +307,6 @@ template <> struct GraphTraits<VPlan *> {
}
};

inline auto VPlan::getExitBlocks() {
VPBlockBase *ScalarHeader = getScalarHeader();
return make_filter_range(
VPBlockUtils::blocksOnly<VPIRBasicBlock>(
vp_depth_first_shallow(getVectorLoopRegion()->getSingleSuccessor())),
[ScalarHeader](VPIRBasicBlock *VPIRBB) {
return VPIRBB != ScalarHeader && VPIRBB->getNumSuccessors() == 0;
});
}
} // namespace llvm

#endif // LLVM_TRANSFORMS_VECTORIZE_VPLANCFG_H
15 changes: 3 additions & 12 deletions llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2049,18 +2049,9 @@ void VPlanTransforms::handleUncountableEarlyExit(
cast<BranchInst>(UncountableExitingBlock->getTerminator());
BasicBlock *TrueSucc = EarlyExitingBranch->getSuccessor(0);
BasicBlock *FalseSucc = EarlyExitingBranch->getSuccessor(1);

// The early exit block may or may not be the same as the "countable" exit
// block. Creates a new VPIRBB for the early exit block in case it is distinct
// from the countable exit block.
// TODO: Introduce both exit blocks during VPlan skeleton construction.
VPIRBasicBlock *VPEarlyExitBlock;
if (OrigLoop->getUniqueExitBlock()) {
VPEarlyExitBlock = cast<VPIRBasicBlock>(MiddleVPBB->getSuccessors()[0]);
} else {
VPEarlyExitBlock = Plan.createVPIRBasicBlock(
!OrigLoop->contains(TrueSucc) ? TrueSucc : FalseSucc);
}
BasicBlock *EarlyExitIRBB =
!OrigLoop->contains(TrueSucc) ? TrueSucc : FalseSucc;
VPIRBasicBlock *VPEarlyExitBlock = Plan.getExitBlock(EarlyExitIRBB);

VPValue *EarlyExitNotTakenCond = RecipeBuilder.getBlockInMask(
OrigLoop->contains(TrueSucc) ? TrueSucc : FalseSucc);
Expand Down