Skip to content

Commit 3a064e7

Browse files
committed
!fixup address latest comments, thanks!
1 parent 9c652ec commit 3a064e7

File tree

5 files changed

+35
-20
lines changed

5 files changed

+35
-20
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2781,7 +2781,7 @@ void InnerLoopVectorizer::fixVectorizedLoop(VPTransformState &State) {
27812781
// Don't apply optimizations below when no (vector) loop remains, as they all
27822782
// require one at the moment.
27832783
VPBasicBlock *HeaderVPBB =
2784-
vputils::getTopLevelVectorLoopHeader(*State.Plan, State.VPDT);
2784+
vputils::getFirstLoopHeader(*State.Plan, State.VPDT);
27852785
if (!HeaderVPBB)
27862786
return;
27872787

@@ -7801,6 +7801,9 @@ DenseMap<const SCEV *, Value *> LoopVectorizationPlanner::executePlan(
78017801
TTI.getRegisterBitWidth(TargetTransformInfo::RGK_FixedWidthVector));
78027802
VPlanTransforms::removeDeadRecipes(BestVPlan);
78037803

7804+
// Retrieve and store the middle block before dissolving regions. Regions are
7805+
// dissolved after optimizing for VF and UF, which completely removes unneeded
7806+
// loop regions first.
78047807
VPBasicBlock *MiddleVPBB =
78057808
BestVPlan.getVectorLoopRegion() ? BestVPlan.getMiddleBlock() : nullptr;
78067809
VPlanTransforms::disolveLoopRegions(BestVPlan);
@@ -7899,8 +7902,7 @@ DenseMap<const SCEV *, Value *> LoopVectorizationPlanner::executePlan(
78997902
// 2.6. Maintain Loop Hints
79007903
// Keep all loop hints from the original loop on the vector loop (we'll
79017904
// replace the vectorizer-specific hints below).
7902-
VPBasicBlock *HeaderVPBB =
7903-
vputils::getTopLevelVectorLoopHeader(BestVPlan, State.VPDT);
7905+
VPBasicBlock *HeaderVPBB = vputils::getFirstLoopHeader(BestVPlan, State.VPDT);
79047906
if (HeaderVPBB) {
79057907
MDNode *OrigLoopID = OrigLoop->getLoopID();
79067908

llvm/lib/Transforms/Vectorize/VPlan.cpp

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -212,14 +212,24 @@ bool VPBlockUtils::isHeader(const VPBlockBase *VPB,
212212
auto *VPBB = dyn_cast<VPBasicBlock>(VPB);
213213
if (!VPBB)
214214
return false;
215+
216+
// If VPBB is in a region R, VPBB is a loop header if R is a loop region with
217+
// VPBB as its entry, i.e., free of predecessors.
215218
if (auto *R = VPBB->getParent())
216219
return !R->isReplicator() && VPBB->getNumPredecessors() == 0;
217220

218-
assert(!VPB->getParent() && "checking blocks in regions not implemented yet");
221+
// A header dominates its second predecessor (the latch), with the other
222+
// predecessor being the preheader
219223
return VPB->getPredecessors().size() == 2 &&
220224
VPDT.dominates(VPB, VPB->getPredecessors()[1]);
221225
}
222226

227+
bool VPBlockUtils::isLatch(const VPBlockBase *VPB,
228+
const VPDominatorTree &VPDT) {
229+
return VPB->getNumSuccessors() == 2 &&
230+
VPBlockUtils::isHeader(VPB->getSuccessors()[1], VPDT);
231+
}
232+
223233
VPBasicBlock::iterator VPBasicBlock::getFirstNonPhi() {
224234
iterator It = begin();
225235
while (It != end() && It->isPhi())
@@ -435,16 +445,17 @@ void VPBasicBlock::connectToPredecessors(VPTransformState &State) {
435445

436446
auto Preds = to_vector(getHierarchicalPredecessors());
437447
if (VPBlockUtils::isHeader(this, State.VPDT)) {
438-
// There's no block yet for the latch, don't try to connect it yet.
448+
// There's no block for the latch yet, connect to the preheader only.
439449
Preds = {Preds[0]};
440450
}
441451

442452
// Hook up the new basic block to its predecessors.
443453
for (VPBlockBase *PredVPBlock : Preds) {
444454
VPBasicBlock *PredVPBB = PredVPBlock->getExitingBasicBlock();
445455
auto &PredVPSuccessors = PredVPBB->getHierarchicalSuccessors();
456+
assert(CFG.VPBB2IRBB.contains(PredVPBB) &&
457+
"Predecessor basic-block not found building successor.");
446458
BasicBlock *PredBB = CFG.VPBB2IRBB.lookup(PredVPBB);
447-
assert(PredBB && "Predecessor basic-block not found building successor.");
448459
auto *PredBBTerminator = PredBB->getTerminator();
449460
LLVM_DEBUG(dbgs() << "LV: draw edge from" << PredBB->getName() << '\n');
450461

@@ -548,8 +559,7 @@ void VPBasicBlock::execute(VPTransformState *State) {
548559
executeRecipes(State, NewBB);
549560

550561
// If this block is a latch, update CurrentParentLoop.
551-
if (getNumSuccessors() == 2 &&
552-
VPBlockUtils::isHeader(getSuccessors()[1], State->VPDT))
562+
if (VPBlockUtils::isLatch(this, State->VPDT))
553563
State->CurrentParentLoop = State->CurrentParentLoop->getParentLoop();
554564
}
555565

@@ -988,8 +998,7 @@ void VPlan::execute(VPTransformState *State) {
988998
for (VPBlockBase *Block : RPOT)
989999
Block->execute(State);
9901000

991-
VPBasicBlock *Header =
992-
vputils::getTopLevelVectorLoopHeader(*this, State->VPDT);
1001+
VPBasicBlock *Header = vputils::getFirstLoopHeader(*this, State->VPDT);
9931002
if (!Header)
9941003
return;
9951004

llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,7 @@ Value *VPInstruction::generate(VPTransformState &State) {
577577
Value *Cond = State.get(getOperand(0), VPLane(0));
578578
// Replace the temporary unreachable terminator with a new conditional
579579
// branch, hooking it up to backward destination (header) for latch blocks
580-
// now to forward destination(s) later when they are created.
580+
// now, and to forward destination(s) later when they are created.
581581
BranchInst *CondBr =
582582
Builder.CreateCondBr(Cond, Builder.GetInsertBlock(), nullptr);
583583
CondBr->setSuccessor(0, nullptr);
@@ -601,9 +601,9 @@ Value *VPInstruction::generate(VPTransformState &State) {
601601

602602
// Replace the temporary unreachable terminator with a new conditional
603603
// branch, hooking it up to backward destination (the header) for latch
604-
// blocks now forward destination (the exit/middle block) later when it is
605-
// created. Note that CreateCondBr expects a valid BB as first argument, so
606-
// we need to set it to nullptr later.
604+
// blocks now, and to forward destination (the exit/middle block) later when
605+
// it is created. Note that CreateCondBr expects a valid BB as first
606+
// argument, so we need to set it to nullptr later.
607607
BranchInst *CondBr = Builder.CreateCondBr(Cond, Builder.GetInsertBlock(),
608608
State.CFG.VPBB2IRBB[Header]);
609609
CondBr->setSuccessor(0, nullptr);

llvm/lib/Transforms/Vectorize/VPlanUtils.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,7 @@ bool vputils::isUniformAcrossVFsAndUFs(VPValue *V) {
126126
});
127127
}
128128

129-
VPBasicBlock *vputils::getTopLevelVectorLoopHeader(VPlan &Plan,
130-
VPDominatorTree &VPDT) {
129+
VPBasicBlock *vputils::getFirstLoopHeader(VPlan &Plan, VPDominatorTree &VPDT) {
131130
auto DepthFirst = vp_depth_first_shallow(Plan.getEntry());
132131
auto I = find_if(DepthFirst, [&VPDT](VPBlockBase *VPB) {
133132
return VPBlockUtils::isHeader(VPB, VPDT);

llvm/lib/Transforms/Vectorize/VPlanUtils.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,9 @@ bool isHeaderMask(const VPValue *V, VPlan &Plan);
9494
/// VPDerivedIV or VPCanonicalIVPHI).
9595
bool isUniformAcrossVFsAndUFs(VPValue *V);
9696

97-
/// Returns the header block of the top-level vector loop, if one exists.
98-
VPBasicBlock *getTopLevelVectorLoopHeader(VPlan &Plan, VPDominatorTree &VPDT);
97+
/// Returns the header block of the first, top-level loop, or null if none
98+
/// exist.
99+
VPBasicBlock *getFirstLoopHeader(VPlan &Plan, VPDominatorTree &VPDT);
99100
} // namespace vputils
100101

101102
//===----------------------------------------------------------------------===//
@@ -243,8 +244,12 @@ class VPBlockUtils {
243244
VPBlockUtils::connectBlocks(BlockPtr, To, PredIx, -1);
244245
}
245246

246-
/// Returns true if \p VPB is a loop header.
247-
static bool isHeader(const VPBlockBase *VPBB, const VPDominatorTree &VPDT);
247+
/// Returns true if \p VPB is a loop header, based on regions or \p VPDT in
248+
/// their absence.
249+
static bool isHeader(const VPBlockBase *VPB, const VPDominatorTree &VPDT);
250+
251+
/// Returns true if \p VPB is a loop latch, using isHeader().
252+
static bool isLatch(const VPBlockBase *VPB, const VPDominatorTree &VPDT);
248253
};
249254

250255
} // namespace llvm

0 commit comments

Comments
 (0)