Skip to content

Commit 528e463

Browse files
committed
!fixup address latest comments,t hanks!
1 parent 17fe80c commit 528e463

File tree

3 files changed

+53
-204
lines changed

3 files changed

+53
-204
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2573,7 +2573,7 @@ void InnerLoopVectorizer::fixVectorizedLoop(VPTransformState &State) {
25732573
// Remove redundant induction instructions.
25742574
cse(HeaderBB);
25752575

2576-
if (Plan.getScalarPreheader()->hasPredecessors())
2576+
if (!Plan.getScalarPreheader()->hasPredecessors())
25772577
return;
25782578

25792579
// Set/update profile weights for the vector and remainder loops as original
@@ -7230,7 +7230,13 @@ DenseMap<const SCEV *, Value *> LoopVectorizationPlanner::executePlan(
72307230
VPlanTransforms::removeBranchOnConst(BestVPlan);
72317231
if (BestVPlan.getEntry()->getSingleSuccessor() ==
72327232
BestVPlan.getScalarPreheader()) {
7233-
// TODO: Should not even try to vectorize.
7233+
// TODO: The vector loop would be dead, should not even try to vectorize.
7234+
ORE->emit([&]() {
7235+
return OptimizationRemarkAnalysis(DEBUG_TYPE, "VectorizationDead",
7236+
OrigLoop->getStartLoc(),
7237+
OrigLoop->getHeader())
7238+
<< "Created vector loop never executes.";
7239+
});
72347240
return DenseMap<const SCEV *, Value *>();
72357241
}
72367242

@@ -7315,6 +7321,10 @@ DenseMap<const SCEV *, Value *> LoopVectorizationPlanner::executePlan(
73157321
if (BasicBlock *SCEVCheckBlock = ILV.RTChecks.getSCEVChecks().second)
73167322
SCEVCheckBlock->moveAfter(EntryBB);
73177323

7324+
std::optional<MDNode *> VectorizedLoopID = makeFollowupLoopID(
7325+
OrigLoop->getLoopID(),
7326+
{LLVMLoopVectorizeFollowupAll, LLVMLoopVectorizeFollowupVectorized});
7327+
73187328
BestVPlan.execute(&State);
73197329

73207330
// 2.5 When vectorizing the epilogue, fix reduction resume values from the
@@ -7347,21 +7357,16 @@ DenseMap<const SCEV *, Value *> LoopVectorizationPlanner::executePlan(
73477357
// replace the vectorizer-specific hints below).
73487358
VPBasicBlock *HeaderVPBB = vputils::getFirstLoopHeader(BestVPlan, State.VPDT);
73497359
if (HeaderVPBB) {
7350-
MDNode *OrigLoopID = OrigLoop->getLoopID();
7351-
7352-
std::optional<MDNode *> VectorizedLoopID =
7353-
makeFollowupLoopID(OrigLoopID, {LLVMLoopVectorizeFollowupAll,
7354-
LLVMLoopVectorizeFollowupVectorized});
7355-
73567360
Loop *L = LI->getLoopFor(State.CFG.VPBB2IRBB[HeaderVPBB]);
73577361
if (VectorizedLoopID) {
73587362
L->setLoopID(*VectorizedLoopID);
73597363
} else {
73607364
// Keep all loop hints from the original loop on the vector loop (we'll
73617365
// replace the vectorizer-specific hints below).
7362-
if (BestVPlan.getScalarPreheader()->getNumPredecessors() > 0)
7366+
if (BestVPlan.getScalarPreheader()->hasPredecessors()) {
73637367
if (MDNode *LID = OrigLoop->getLoopID())
73647368
L->setLoopID(LID);
7369+
}
73657370

73667371
LoopVectorizeHints Hints(L, true, *ORE);
73677372
Hints.setAlreadyVectorized();
@@ -7392,16 +7397,6 @@ DenseMap<const SCEV *, Value *> LoopVectorizationPlanner::executePlan(
73927397
addRuntimeUnrollDisableMetaData(L);
73937398
}
73947399

7395-
if (BestVPlan.getScalarPreheader()->getNumPredecessors() == 0) {
7396-
// If the original loop became unreachable, we need to delete it.
7397-
auto Blocks = OrigLoop->getBlocksVector();
7398-
Blocks.push_back(cast<VPIRBasicBlock>(BestVPlan.getScalarPreheader())
7399-
->getIRBasicBlock());
7400-
for (auto *BB : Blocks)
7401-
LI->removeBlock(BB);
7402-
LI->erase(OrigLoop);
7403-
}
7404-
74057400
// 3. Fix the vectorized code: take care of header phi's, live-outs,
74067401
// predication, updating analyses.
74077402
ILV.fixVectorizedLoop(State);
@@ -10322,7 +10317,9 @@ bool LoopVectorizePass::processLoop(Loop *L) {
1032210317

1032310318
assert(DT->verify(DominatorTree::VerificationLevel::Fast) &&
1032410319
"DT not preserved correctly");
10320+
assert(!verifyFunction(*F, &dbgs()));
1032510321

10322+
// The original loop has been removed, no need to update its metadata.
1032610323
if (LoopRemoved)
1032710324
return true;
1032810325

@@ -10339,7 +10336,6 @@ bool LoopVectorizePass::processLoop(Loop *L) {
1033910336
Hints.setAlreadyVectorized();
1034010337
}
1034110338

10342-
assert(!verifyFunction(*L->getHeader()->getParent(), &dbgs()));
1034310339
return true;
1034410340
}
1034510341

llvm/lib/Transforms/Vectorize/VPlan.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -973,12 +973,23 @@ void VPlan::execute(VPTransformState *State) {
973973
LLVM_DEBUG(dump());
974974

975975
BasicBlock *ScalarPh = State->CFG.ExitBB;
976-
if (getScalarPreheader()->getNumPredecessors() > 0) {
976+
if (getScalarPreheader()->hasPredecessors()) {
977977
// Disconnect scalar preheader and scalar header, as the dominator tree edge
978978
// will be updated as part of VPlan execution. This allows keeping the DTU
979979
// logic generic during VPlan execution.
980980
State->CFG.DTU.applyUpdates(
981981
{{DominatorTree::Delete, ScalarPh, ScalarPh->getSingleSuccessor()}});
982+
} else {
983+
Loop *OrigLoop = State->LI->getLoopFor(
984+
cast<VPIRBasicBlock>(getScalarPreheader()->getSingleSuccessor())
985+
->getIRBasicBlock());
986+
// If the original loop is unreachable, we need to delete it.
987+
auto Blocks = OrigLoop->getBlocksVector();
988+
Blocks.push_back(
989+
cast<VPIRBasicBlock>(getScalarPreheader())->getIRBasicBlock());
990+
for (auto *BB : Blocks)
991+
State->LI->removeBlock(BB);
992+
State->LI->erase(OrigLoop);
982993
}
983994

984995
ReversePostOrderTraversal<VPBlockShallowTraversalWrapper<VPBlockBase *>> RPOT(

0 commit comments

Comments
 (0)