Skip to content

Commit a900c63

Browse files
committed
!fixup address latest comments, thanks
1 parent ea95862 commit a900c63

File tree

3 files changed

+33
-26
lines changed

3 files changed

+33
-26
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -9370,8 +9370,8 @@ static void addScalarResumePhis(VPRecipeBuilder &Builder, VPlan &Plan,
93709370

93719371
// Collect VPIRInstructions for phis in the exit block from the latch only.
93729372
static SetVector<VPIRInstruction *>
9373-
collectUsersInExitBlocks(Loop *OrigLoop, VPRecipeBuilder &Builder,
9374-
VPlan &Plan) {
9373+
collectUsersInLatchExitBlock(Loop *OrigLoop, VPRecipeBuilder &Builder,
9374+
VPlan &Plan) {
93759375
SetVector<VPIRInstruction *> ExitUsersToFix;
93769376
for (VPIRBasicBlock *ExitVPBB : Plan.getExitBlocks()) {
93779377
// Nothing to do for unreachable exit blocks.
@@ -9758,7 +9758,7 @@ LoopVectorizationPlanner::tryToBuildVPlanWithVPRecipes(VFRange &Range) {
97589758
DenseMap<VPValue *, VPValue *> IVEndValues;
97599759
addScalarResumePhis(RecipeBuilder, *Plan, IVEndValues);
97609760
SetVector<VPIRInstruction *> ExitUsersToFix =
9761-
collectUsersInExitBlocks(OrigLoop, RecipeBuilder, *Plan);
9761+
collectUsersInLatchExitBlock(OrigLoop, RecipeBuilder, *Plan);
97629762
addExitUsersForFirstOrderRecurrences(*Plan, ExitUsersToFix);
97639763
addUsersInExitBlocks(*Plan, ExitUsersToFix);
97649764

llvm/lib/Transforms/Vectorize/VPlanConstruction.cpp

+14-9
Original file line numberDiff line numberDiff line change
@@ -352,8 +352,10 @@ std::unique_ptr<VPlan> PlainCFGBuilder::buildPlainCFG(
352352
Plan->getEntry()->setOneSuccessor(getOrCreateVPBB(TheLoop->getHeader()));
353353
Plan->getEntry()->setPlan(&*Plan);
354354

355-
// Fix VPlan loop-closed-ssa exit phi's by add incoming operands to the
355+
// Fix VPlan loop-closed-ssa exit phi's by adding incoming operands to the
356356
// VPIRInstructions wrapping them.
357+
// Note that the operand order may need adjusting when predecessors are added,
358+
// if an exit block has multiple predecessor.
357359
for (auto *EB : Plan->getExitBlocks()) {
358360
for (VPRecipeBase &R : EB->phis()) {
359361
auto *PhiR = cast<VPIRPhi>(&R);
@@ -476,6 +478,17 @@ void VPlanTransforms::createLoopRegions(VPlan &Plan, Type *InductionTy,
476478

477479
VPBasicBlock *ScalarPH = Plan.createVPBasicBlock("scalar.ph");
478480
VPBlockUtils::connectBlocks(ScalarPH, Plan.getScalarHeader());
481+
482+
// If needed, add a check in the middle block to see if we have completed
483+
// all of the iterations in the first vector loop. Three cases:
484+
// 1) If we require a scalar epilogue, there is no conditional branch as
485+
// we unconditionally branch to the scalar preheader. Remove the recipes
486+
// from the exit blocks.
487+
// 2) If (N - N%VF) == N, then we *don't* need to run the remainder.
488+
// Thus if tail is to be folded, we know we don't need to run the
489+
// remainder and we can set the condition to true.
490+
// 3) Otherwise, construct a runtime check.
491+
479492
if (!RequiresScalarEpilogueCheck) {
480493
VPBlockUtils::connectBlocks(MiddleVPBB, ScalarPH);
481494
// The exit blocks are unreachable, remove their recipes to make sure no
@@ -487,14 +500,6 @@ void VPlanTransforms::createLoopRegions(VPlan &Plan, Type *InductionTy,
487500
return;
488501
}
489502

490-
// If needed, add a check in the middle block to see if we have completed
491-
// all of the iterations in the first vector loop. Three cases:
492-
// 1) If (N - N%VF) == N, then we *don't* need to run the remainder.
493-
// Thus if tail is to be folded, we know we don't need to run the
494-
// remainder and we can set the condition to true.
495-
// 2) If we require a scalar epilogue, there is no conditional branch as
496-
// we unconditionally branch to the scalar preheader. Do nothing.
497-
// 3) Otherwise, construct a runtime check.
498503
BasicBlock *IRExitBlock = TheLoop->getUniqueLatchExitBlock();
499504
auto *VPExitBlock = Plan.getExitBlock(IRExitBlock);
500505
// The connection order corresponds to the operands of the conditional branch.

llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp

+16-14
Original file line numberDiff line numberDiff line change
@@ -2505,23 +2505,25 @@ void VPlanTransforms::handleUncountableEarlyExit(
25052505
VPBuilder EarlyExitB(VectorEarlyExitVPBB);
25062506
for (VPRecipeBase &R : VPEarlyExitBlock->phis()) {
25072507
auto *ExitIRI = cast<VPIRPhi>(&R);
2508-
// By default, assume early exit operand is first, e.g., when the two exit
2509-
// blocks are distinct - VPEarlyExitBlock has a single predecessor.
2510-
unsigned EarlyExitIdx = 0;
2508+
// Early exit operand should always be last, i.e., 0 if VPEarlyExitBlock has
2509+
// a single predecessor and 1 if it has two.
2510+
unsigned EarlyExitIdx = ExitIRI->getNumOperands() - 1;
25112511
if (OrigLoop->getUniqueExitBlock()) {
2512-
// The incoming values currently correspond to the original IR
2513-
// predecessors. After the transform, the first incoming value is coming
2514-
// from the original loop latch, while the second operand is from the
2515-
// early exit. Swap the phi operands, if the first predecessor in the
2516-
// original IR is not the loop latch.
2517-
if (*pred_begin(VPEarlyExitBlock->getIRBasicBlock()) !=
2518-
OrigLoop->getLoopLatch())
2512+
// If VPEarlyExitBlock has two predecessors, they are already ordered such
2513+
// that early exit is second (and latch exit is first), by construction.
2514+
// But its underlying IRBB (EarlyExitIRBB) may have its predecessors
2515+
// ordered the other way around, and it is the order of the latter which
2516+
// corresponds to the order of operands of VPEarlyExitBlock's phi recipes.
2517+
// Therefore, if early exit (UncountableExitingBlock) is the first
2518+
// predecessor of EarlyExitIRBB, we swap the operands of phi recipes,
2519+
// thereby bringing them to match VPEarlyExitBlock's predecessor order,
2520+
// with early exit being last (second). Otherwise they already match.
2521+
if (*pred_begin(VPEarlyExitBlock->getIRBasicBlock()) ==
2522+
UncountableExitingBlock)
25192523
ExitIRI->swapOperands();
25202524

2521-
EarlyExitIdx = 1;
2522-
// If there's a unique exit block, VPEarlyExitBlock has 2 predecessors
2523-
// (MiddleVPBB and NewMiddle). Extract the last lane of the incoming value
2524-
// from MiddleVPBB which is coming from the original latch.
2525+
// The first of two operands corresponds to the latch exit, via MiddleVPBB
2526+
// predecessor. Extract its last lane.
25252527
ExitIRI->extractLastLaneOfFirstOperand(MiddleBuilder);
25262528
}
25272529

0 commit comments

Comments
 (0)