Skip to content

Commit e849195

Browse files
committed
!fixup address more comments.
1 parent 5cb0851 commit e849195

File tree

9 files changed

+218
-504
lines changed

9 files changed

+218
-504
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8839,11 +8839,7 @@ static SetVector<VPIRInstruction *> collectUsersInExitBlocks(
88398839
Loop *OrigLoop, VPRecipeBuilder &Builder, VPlan &Plan,
88408840
const MapVector<PHINode *, InductionDescriptor> &Inductions) {
88418841
SetVector<VPIRInstruction *> ExitUsersToFix;
8842-
for (VPBlockBase *VPB : vp_depth_first_shallow(
8843-
Plan.getVectorLoopRegion()->getSingleSuccessor())) {
8844-
if (VPB->getNumSuccessors() != 0 || VPB == Plan.getScalarHeader())
8845-
continue;
8846-
auto *ExitVPBB = cast<VPIRBasicBlock>(VPB);
8842+
for (VPIRBasicBlock *ExitVPBB : Plan.getExitBlocks()) {
88478843
BasicBlock *ExitBB = ExitVPBB->getIRBasicBlock();
88488844
for (VPRecipeBase &R : *ExitVPBB) {
88498845
auto *ExitIRI = dyn_cast<VPIRInstruction>(&R);
@@ -9178,14 +9174,31 @@ LoopVectorizationPlanner::tryToBuildVPlanWithVPRecipes(VFRange &Range) {
91789174
RecipeBuilder.fixHeaderPhis();
91799175

91809176
if (Legal->hasUncountableEarlyExit()) {
9181-
VPlanTransforms::handleUncountableEarlyExit(*Plan, *PSE.getSE(), OrigLoop,
9182-
RecipeBuilder);
9177+
VPlanTransforms::handleUncountableEarlyExit(
9178+
*Plan, *PSE.getSE(), OrigLoop, Legal->getUncountableExitingBlocks(),
9179+
RecipeBuilder);
91839180
}
91849181
addScalarResumePhis(RecipeBuilder, *Plan);
91859182
SetVector<VPIRInstruction *> ExitUsersToFix = collectUsersInExitBlocks(
91869183
OrigLoop, RecipeBuilder, *Plan, Legal->getInductionVars());
91879184
addExitUsersForFirstOrderRecurrences(*Plan, ExitUsersToFix);
91889185
addUsersInExitBlocks(*Plan, ExitUsersToFix);
9186+
9187+
// Currently only live-ins can be used by exit values. We also bail out if any
9188+
// exit value isn't handled in VPlan yet, i.e. a VPIRInstruction in the exit
9189+
// without any operands.
9190+
if (Legal->hasUncountableEarlyExit()) {
9191+
if (any_of(Plan->getExitBlocks(), [](VPIRBasicBlock *ExitBB) {
9192+
return any_of(*ExitBB, [](VPRecipeBase &R) {
9193+
auto VPIRI = cast<VPIRInstruction>(&R);
9194+
return VPIRI->getNumOperands() == 0 ||
9195+
any_of(VPIRI->operands(),
9196+
[](VPValue *Op) { return !Op->isLiveIn(); });
9197+
});
9198+
}))
9199+
return nullptr;
9200+
}
9201+
91899202
// ---------------------------------------------------------------------------
91909203
// Transform initial VPlan: Apply previously taken decisions, in order, to
91919204
// bring the VPlan to its final state.

llvm/lib/Transforms/Vectorize/VPlan.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3832,6 +3832,10 @@ class VPlan {
38323832
return cast<VPBasicBlock>(getScalarPreheader()->getSinglePredecessor());
38333833
}
38343834

3835+
/// Return the exit blocks of the VPlan, that is leaf nodes except the scalar
3836+
/// header.
3837+
auto getExitBlocks();
3838+
38353839
/// The trip count of the original loop.
38363840
VPValue *getTripCount() const {
38373841
assert(TripCount && "trip count needs to be set before accessing it");

llvm/lib/Transforms/Vectorize/VPlanCFG.h

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

309+
inline auto VPlan::getExitBlocks() {
310+
VPBlockBase *ScalarHeader = getScalarHeader();
311+
return make_filter_range(
312+
VPBlockUtils::blocksOnly<VPIRBasicBlock>(
313+
vp_depth_first_shallow(getVectorLoopRegion()->getSingleSuccessor())),
314+
[ScalarHeader](VPIRBasicBlock *VPIRBB) {
315+
return VPIRBB != ScalarHeader && VPIRBB->getNumSuccessors() == 0;
316+
});
317+
}
309318
} // namespace llvm
310319

311320
#endif // LLVM_TRANSFORMS_VECTORIZE_VPLANCFG_H

llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -872,7 +872,6 @@ void VPIRInstruction::print(raw_ostream &O, const Twine &Indent,
872872
O << Indent << "IR " << I;
873873

874874
if (getNumOperands() != 0) {
875-
// assert(getNumOperands() == 1 && "can have at most 1 operand");
876875
O << " (extra operand: ";
877876
printOperands(O, SlotTracker);
878877
O << ")";

llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1793,31 +1793,26 @@ void VPlanTransforms::createInterleaveGroups(
17931793

17941794
void VPlanTransforms::handleUncountableEarlyExit(
17951795
VPlan &Plan, ScalarEvolution &SE, Loop *OrigLoop,
1796+
ArrayRef<BasicBlock *> UncountableExitingBlocks,
17961797
VPRecipeBuilder &RecipeBuilder) {
17971798
auto *LatchVPBB =
17981799
cast<VPBasicBlock>(Plan.getVectorLoopRegion()->getExiting());
17991800
VPBuilder Builder(LatchVPBB->getTerminator());
18001801
auto *MiddleVPBB = Plan.getMiddleBlock();
18011802
VPRegionBlock *LoopRegion = Plan.getVectorLoopRegion();
1802-
18031803
VPValue *EarlyExitTaken = nullptr;
1804-
SmallVector<BasicBlock *> ExitingBBs;
1805-
OrigLoop->getExitingBlocks(ExitingBBs);
18061804

18071805
// Process all uncountable exiting blocks. For each exiting block, update the
18081806
// EarlyExitTaken, which tracks if any uncountable early exit has been taken.
18091807
// Also split the middle block and branch to the exit block for the early exit
18101808
// if it has been taken.
1811-
for (BasicBlock *Exiting : ExitingBBs) {
1812-
if (Exiting == OrigLoop->getLoopLatch())
1813-
continue;
1814-
1809+
for (BasicBlock *Exiting : UncountableExitingBlocks) {
18151810
auto *ExitingTerm = cast<BranchInst>(Exiting->getTerminator());
18161811
BasicBlock *TrueSucc = ExitingTerm->getSuccessor(0);
18171812
BasicBlock *FalseSucc = ExitingTerm->getSuccessor(1);
1818-
VPBasicBlock *VPExitBlock;
1813+
VPIRBasicBlock *VPExitBlock;
18191814
if (OrigLoop->getUniqueExitBlock()) {
1820-
VPExitBlock = cast<VPBasicBlock>(MiddleVPBB->getSuccessors()[0]);
1815+
VPExitBlock = cast<VPIRBasicBlock>(MiddleVPBB->getSuccessors()[0]);
18211816
} else {
18221817
VPExitBlock = VPIRBasicBlock::fromBasicBlock(
18231818
!OrigLoop->contains(TrueSucc) ? TrueSucc : FalseSucc);

llvm/lib/Transforms/Vectorize/VPlanTransforms.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,16 @@ struct VPlanTransforms {
124124
/// Remove dead recipes from \p Plan.
125125
static void removeDeadRecipes(VPlan &Plan);
126126

127-
static void handleUncountableEarlyExit(VPlan &Plan, ScalarEvolution &SE,
128-
Loop *OrigLoop,
129-
VPRecipeBuilder &RecipeBuilder);
127+
/// Update \p Plan to account for uncountable exit blocks in \p
128+
/// UncountableExitingBlocks by
129+
/// * updating the condition to exit the vector loop to include the early
130+
/// exit conditions
131+
/// * splitting the original middle block to branch to the early exit blocks
132+
/// if taken. Returns false if the transformation wasn't successful.
133+
static void
134+
handleUncountableEarlyExit(VPlan &Plan, ScalarEvolution &SE, Loop *OrigLoop,
135+
ArrayRef<BasicBlock *> UncountableExitingBlocks,
136+
VPRecipeBuilder &RecipeBuilder);
130137
};
131138

132139
} // namespace llvm

llvm/test/Transforms/LoopVectorize/X86/multi-exit-codegen.ll

Lines changed: 0 additions & 240 deletions
This file was deleted.

0 commit comments

Comments
 (0)