Skip to content

Commit 6ced27d

Browse files
committed
!fixup address comments, thanks
1 parent 9233672 commit 6ced27d

File tree

5 files changed

+55
-42
lines changed

5 files changed

+55
-42
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2380,10 +2380,14 @@ void InnerLoopVectorizer::introduceCheckBlockInVPlan(BasicBlock *CheckIRBB) {
23802380

23812381
// We just connected a new block to the scalar preheader. Update all
23822382
// VPPhis by adding an incoming value for it, replicating the last value.
2383+
unsigned NumPredecessors = ScalarPH->getNumPredecessors();
2384+
(void)NumPredecessors;
23832385
for (VPRecipeBase &R : cast<VPBasicBlock>(ScalarPH)->phis()) {
23842386
auto *ResumePhi = cast<VPPhi>(&R);
23852387
ResumePhi->addOperand(
23862388
ResumePhi->getOperand(ResumePhi->getNumOperands() - 1));
2389+
assert(ResumePhi->getNumIncoming() == NumPredecessors &&
2390+
"must have incoming values for all operands");
23872391
}
23882392
}
23892393

llvm/lib/Transforms/Vectorize/VPlan.h

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,7 +1122,9 @@ class VPPhiAccessors {
11221122
return getAsRecipe()->getNumOperands();
11231123
}
11241124

1125-
void removeIncomingValue(VPBlockBase *VPB) const;
1125+
/// Removes the incoming value corresponding to \p IncomingBlock, which must
1126+
/// be a predecessor.
1127+
void removeIncomingValue(VPBlockBase *IncomingBlock) const;
11261128

11271129
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
11281130
/// Print the recipe.
@@ -3717,15 +3719,6 @@ VPPhiAccessors::getIncomingBlock(unsigned Idx) const {
37173719
return getAsRecipe()->getParent()->getCFGPredecessor(Idx);
37183720
}
37193721

3720-
inline void VPPhiAccessors::removeIncomingValue(VPBlockBase *VPB) const {
3721-
VPRecipeBase *R = const_cast<VPRecipeBase *>(getAsRecipe());
3722-
const VPBasicBlock *Parent = R->getParent();
3723-
assert(R->getNumOperands() == Parent->getNumPredecessors());
3724-
auto I = find(Parent->getPredecessors(), VPB);
3725-
R->getOperand(I - Parent->getPredecessors().begin())->removeUser(*R);
3726-
R->removeOperand(I - Parent->getPredecessors().begin());
3727-
}
3728-
37293722
/// A special type of VPBasicBlock that wraps an existing IR basic block.
37303723
/// Recipes of the block get added before the first non-phi instruction in the
37313724
/// wrapped block.

llvm/lib/Transforms/Vectorize/VPlanConstruction.cpp

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -502,10 +502,8 @@ void VPlanTransforms::prepareForVectorization(
502502
cast<VPBasicBlock>(LatchVPB), Range);
503503
HandledUncountableEarlyExit = true;
504504
} else {
505-
for (VPRecipeBase &R : cast<VPIRBasicBlock>(EB)->phis()) {
506-
if (auto *PhiR = dyn_cast<VPIRPhi>(&R))
507-
PhiR->removeIncomingValue(Pred);
508-
}
505+
for (VPRecipeBase &R : EB->phis())
506+
cast<VPIRPhi>(&R)->removeIncomingValue(Pred);
509507
}
510508
cast<VPBasicBlock>(Pred)->getTerminator()->eraseFromParent();
511509
VPBlockUtils::disconnectBlocks(Pred, EB);
@@ -530,44 +528,51 @@ void VPlanTransforms::prepareForVectorization(
530528
VPBasicBlock *ScalarPH = Plan.createVPBasicBlock("scalar.ph");
531529
VPBlockUtils::connectBlocks(ScalarPH, Plan.getScalarHeader());
532530

533-
// If needed, add a check in the middle block to see if we have completed
534-
// all of the iterations in the first vector loop. Three cases:
535-
// 1) If we require a scalar epilogue, there is no conditional branch as
536-
// we unconditionally branch to the scalar preheader. Remove the recipes
537-
// from the exit blocks.
538-
// 2) If (N - N%VF) == N, then we *don't* need to run the remainder.
539-
// Thus if tail is to be folded, we know we don't need to run the
540-
// remainder and we can set the condition to true.
541-
// 3) Otherwise, construct a runtime check.
542531
VPBlockUtils::connectBlocks(MiddleVPBB, ScalarPH);
543532
// Also connect the entry block to the scalar preheader.
544533
// TODO: Also introduce a branch recipe together with the minimum trip count
545534
// check.
546535
VPBlockUtils::connectBlocks(Plan.getEntry(), ScalarPH);
547536
Plan.getEntry()->swapSuccessors();
548537

549-
if (MiddleVPBB->getNumSuccessors() != 2)
538+
// If MiddleVPBB has a single successor the original loop exits via the latch
539+
// and the single successor must be the scalar preheader. There's no need to
540+
// add a runtime check to MiddleVPBB.
541+
if (MiddleVPBB->getNumSuccessors() == 1) {
542+
assert(MiddleVPBB->getSingleSuccessor() == ScalarPH &&
543+
"must have ScalarPH as single successor");
550544
return;
545+
}
546+
547+
assert(MiddleVPBB->getNumSuccessors() == 2 && "must have 2 successors");
548+
549+
// If needed, add a check in the middle block to see if we have completed
550+
// all of the iterations in the first vector loop.
551+
// Three cases:
552+
// 1) If we require a scalar epilogue, the scalar ph must execute. Set the
553+
// condition to false.
554+
// 2) If (N - N%VF) == N, then we *don't* need to run the
555+
// remainder. Thus if tail is to be folded, we know we don't need to run
556+
// the remainder and we can set the condition to true.
557+
// 3) Otherwise, construct a runtime check.
551558

552-
auto *ScalarLatchTerm = TheLoop->getLoopLatch()->getTerminator();
553-
// Here we use the same DebugLoc as the scalar loop latch terminator instead
554-
// of the corresponding compare because they may have ended up with
555-
// different line numbers and we want to avoid awkward line stepping while
556-
// debugging. Eg. if the compare has got a line number inside the loop.
559+
// We use the same DebugLoc as the scalar loop latch terminator instead of
560+
// the corresponding compare because they may have ended up with different
561+
// line numbers and we want to avoid awkward line stepping while debugging.
562+
// E.g., if the compare has got a line number inside the loop.
563+
DebugLoc LatchDL = TheLoop->getLoopLatch()->getTerminator()->getDebugLoc();
557564
VPBuilder Builder(MiddleVPBB);
558565
VPValue *Cmp;
559-
if (TailFolded)
560-
Cmp = Plan.getOrAddLiveIn(ConstantInt::getTrue(
561-
IntegerType::getInt1Ty(TripCount->getType()->getContext())));
562-
else if (!RequiresScalarEpilogueCheck)
566+
if (!RequiresScalarEpilogueCheck)
563567
Cmp = Plan.getOrAddLiveIn(ConstantInt::getFalse(
564568
IntegerType::getInt1Ty(TripCount->getType()->getContext())));
569+
else if (TailFolded)
570+
Cmp = Plan.getOrAddLiveIn(ConstantInt::getTrue(
571+
IntegerType::getInt1Ty(TripCount->getType()->getContext())));
565572
else
566573
Cmp = Builder.createICmp(CmpInst::ICMP_EQ, Plan.getTripCount(),
567-
&Plan.getVectorTripCount(),
568-
ScalarLatchTerm->getDebugLoc(), "cmp.n");
569-
Builder.createNaryOp(VPInstruction::BranchOnCond, {Cmp},
570-
ScalarLatchTerm->getDebugLoc());
574+
&Plan.getVectorTripCount(), LatchDL, "cmp.n");
575+
Builder.createNaryOp(VPInstruction::BranchOnCond, {Cmp}, LatchDL);
571576
}
572577

573578
void VPlanTransforms::createLoopRegions(VPlan &Plan) {

llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1185,6 +1185,16 @@ void VPIRPhi::execute(VPTransformState &State) {
11851185
State.Builder.SetInsertPoint(Phi->getParent(), std::next(Phi->getIterator()));
11861186
}
11871187

1188+
void VPPhiAccessors::removeIncomingValue(VPBlockBase *IncomingBlock) const {
1189+
VPRecipeBase *R = const_cast<VPRecipeBase *>(getAsRecipe());
1190+
auto &Preds = R->getParent()->getPredecessors();
1191+
assert(R->getNumOperands() == Preds.size() &&
1192+
"Number of phi operands must match number of predecessors");
1193+
unsigned Position = std::distance(Preds.begin(), find(Preds, IncomingBlock));
1194+
R->getOperand(Position)->removeUser(*R);
1195+
R->removeOperand(Position);
1196+
}
1197+
11881198
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
11891199
void VPPhiAccessors::printPhiOperands(raw_ostream &O,
11901200
VPSlotTracker &SlotTracker) const {

llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1841,9 +1841,9 @@ void VPlanTransforms::truncateToMinimalBitwidths(
18411841
}
18421842
}
18431843

1844-
/// Remove BranchOnCond recipes with true conditions together with removing
1845-
/// dead edges to their successors.
1846-
static void removeBranchOnCondTrue(VPlan &Plan) {
1844+
/// Remove BranchOnCond recipes with true or false conditions together with
1845+
/// removing dead edges to their successors.
1846+
static void removeBranchOnConst(VPlan &Plan) {
18471847
using namespace llvm::VPlanPatternMatch;
18481848
for (VPBasicBlock *VPBB : VPBlockUtils::blocksOnly<VPBasicBlock>(
18491849
vp_depth_first_shallow(Plan.getEntry()))) {
@@ -1871,7 +1871,8 @@ static void removeBranchOnCondTrue(VPlan &Plan) {
18711871
// from these recipes.
18721872
for (VPRecipeBase &R : make_early_inc_range(*RemovedSucc)) {
18731873
if (isa<VPIRPhi>(&R)) {
1874-
assert(RemovedSucc->getNumPredecessors() == 1);
1874+
assert(RemovedSucc->getNumPredecessors() == 1 &&
1875+
"VPIRPhis must have a single predecessor");
18751876
cast<VPIRPhi>(&R)->removeIncomingValue(VPBB);
18761877
continue;
18771878
}
@@ -1909,7 +1910,7 @@ void VPlanTransforms::optimize(VPlan &Plan) {
19091910
runPass(legalizeAndOptimizeInductions, Plan);
19101911
runPass(removeRedundantExpandSCEVRecipes, Plan);
19111912
runPass(simplifyRecipes, Plan, *Plan.getCanonicalIV()->getScalarType());
1912-
runPass(removeBranchOnCondTrue, Plan);
1913+
runPass(removeBranchOnConst, Plan);
19131914
runPass(removeDeadRecipes, Plan);
19141915

19151916
runPass(createAndOptimizeReplicateRegions, Plan);

0 commit comments

Comments
 (0)