Skip to content

Commit ab709d3

Browse files
committed
!fixup update after merge.
1 parent 08d066d commit ab709d3

File tree

3 files changed

+32
-24
lines changed

3 files changed

+32
-24
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2895,14 +2895,18 @@ void InnerLoopVectorizer::fixVectorizedLoop(VPTransformState &State) {
28952895

28962896
// Don't apply optimizations below when no vector region remains, as they all
28972897
// require a vector loop at the moment.
2898-
if (!State.Plan->getVectorLoopRegion())
2898+
VPBasicBlock *HeaderVPBB = find_singleton<VPBasicBlock>(
2899+
vp_depth_first_shallow(State.Plan->getEntry()),
2900+
[](VPBlockBase *VPB, bool) {
2901+
auto *VPBB = dyn_cast<VPBasicBlock>(VPB);
2902+
return VPBB && VPBB->isHeader() ? VPBB : nullptr;
2903+
});
2904+
if (!HeaderVPBB)
28992905
return;
29002906

29012907
for (Instruction *PI : PredicatedInstructions)
29022908
sinkScalarOperands(&*PI);
29032909

2904-
VPBasicBlock *HeaderVPBB = cast<VPBasicBlock>(
2905-
State.Plan->getVectorPreheader()->getSingleSuccessor());
29062910
BasicBlock *HeaderBB = State.CFG.VPBB2IRBB[HeaderVPBB];
29072911

29082912
// Remove redundant induction instructions.
@@ -7710,7 +7714,6 @@ DenseMap<const SCEV *, Value *> LoopVectorizationPlanner::executePlan(
77107714
ILV.getTripCount(),
77117715
ILV.getOrCreateVectorTripCount(ILV.LoopVectorPreHeader), State);
77127716
replaceVPBBWithIRVPBB(VectorPH, State.CFG.PrevBB);
7713-
VPlanTransforms::prepareToExecute(BestVPlan);
77147717

77157718
BestVPlan.execute(&State);
77167719

@@ -7736,15 +7739,18 @@ DenseMap<const SCEV *, Value *> LoopVectorizationPlanner::executePlan(
77367739
// 2.6. Maintain Loop Hints
77377740
// Keep all loop hints from the original loop on the vector loop (we'll
77387741
// replace the vectorizer-specific hints below).
7739-
if (auto *LoopRegion = BestVPlan.getVectorLoopRegion()) {
7742+
VPBasicBlock *HeaderVPBB = find_singleton<VPBasicBlock>(
7743+
vp_depth_first_shallow(BestVPlan.getEntry()), [](VPBlockBase *VPB, bool) {
7744+
auto *VPBB = dyn_cast<VPBasicBlock>(VPB);
7745+
return VPBB && VPBB->isHeader() ? VPBB : nullptr;
7746+
});
7747+
if (HeaderVPBB) {
77407748
MDNode *OrigLoopID = OrigLoop->getLoopID();
77417749

77427750
std::optional<MDNode *> VectorizedLoopID =
77437751
makeFollowupLoopID(OrigLoopID, {LLVMLoopVectorizeFollowupAll,
77447752
LLVMLoopVectorizeFollowupVectorized});
77457753

7746-
VPBasicBlock *HeaderVPBB =
7747-
cast<VPBasicBlock>(BestVPlan.getVectorPreheader()->getSingleSuccessor());
77487754
Loop *L = LI->getLoopFor(State.CFG.VPBB2IRBB[HeaderVPBB]);
77497755
if (VectorizedLoopID) {
77507756
L->setLoopID(*VectorizedLoopID);
@@ -7770,7 +7776,7 @@ DenseMap<const SCEV *, Value *> LoopVectorizationPlanner::executePlan(
77707776
ILV.printDebugTracesAtEnd();
77717777

77727778
// 4. Adjust branch weight of the branch in the middle block.
7773-
if (BestVPlan.getVectorLoopRegion()) {
7779+
if (HeaderVPBB) {
77747780
auto *MiddleVPBB = BestVPlan.getMiddleBlock();
77757781
auto *MiddleTerm =
77767782
cast<BranchInst>(State.CFG.VPBB2IRBB[MiddleVPBB]->getTerminator());

llvm/lib/Transforms/Vectorize/VPlan.cpp

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -487,17 +487,17 @@ void VPBasicBlock::execute(VPTransformState *State) {
487487

488488
if (isHeader()) {
489489
// Create and register the new vector loop.
490-
State->CurrentVectorLoop = State->LI->AllocateLoop();
490+
State->CurrentParentLoop = State->LI->AllocateLoop();
491491
BasicBlock *VectorPH =
492492
State->CFG.VPBB2IRBB[cast<VPBasicBlock>(getPredecessors()[0])];
493493
Loop *ParentLoop = State->LI->getLoopFor(VectorPH);
494494

495495
// Insert the new loop into the loop nest and register the new basic blocks
496496
// before calling any utilities such as SCEV that require valid LoopInfo.
497497
if (ParentLoop)
498-
ParentLoop->addChildLoop(State->CurrentVectorLoop);
498+
ParentLoop->addChildLoop(State->CurrentParentLoop);
499499
else
500-
State->LI->addTopLevelLoop(State->CurrentVectorLoop);
500+
State->LI->addTopLevelLoop(State->CurrentParentLoop);
501501
}
502502

503503
auto IsReplicateRegion = [](VPBlockBase *BB) {
@@ -823,10 +823,6 @@ void VPRegionBlock::removeRegion() {
823823

824824
VPBlockUtils::connectBlocks(Preheader, Header);
825825
VPBlockUtils::connectBlocks(Exiting, Middle);
826-
827-
// Set LoopRegion's Entry to nullptr, as the CFG from LoopRegion shouldn't
828-
// be deleted when the region is deleted.
829-
Entry = nullptr;
830826
}
831827

832828
VPlan::VPlan(Loop *L) {
@@ -1029,13 +1025,17 @@ void VPlan::execute(VPTransformState *State) {
10291025
Phi = cast<PHINode>(GEP->getPointerOperand());
10301026
}
10311027

1032-
auto *PhiR = cast<VPHeaderPHIRecipe>(&R);
1033-
bool NeedsScalar = isa<VPScalarPHIRecipe>(PhiR) ||
1034-
(isa<VPReductionPHIRecipe>(PhiR) &&
1035-
cast<VPReductionPHIRecipe>(PhiR)->isInLoop());
1036-
Value *Phi = State->get(PhiR, NeedsScalar);
1037-
Value *Val = State->get(PhiR->getBackedgeValue(), NeedsScalar);
1038-
cast<PHINode>(Phi)->addIncoming(Val, VectorLatchBB);
1028+
Phi->setIncomingBlock(1, VectorLatchBB);
1029+
1030+
// Move the last step to the end of the latch block. This ensures
1031+
// consistent placement of all induction updates.
1032+
Instruction *Inc = cast<Instruction>(Phi->getIncomingValue(1));
1033+
Inc->moveBefore(VectorLatchBB->getTerminator()->getPrevNode());
1034+
1035+
// Use the steps for the last part as backedge value for the induction.
1036+
if (auto *IV = dyn_cast<VPWidenIntOrFpInductionRecipe>(&R))
1037+
Inc->setOperand(0, State->get(IV->getLastUnrolledPartOperand()));
1038+
continue;
10391039
}
10401040

10411041
auto *PhiR = cast<VPHeaderPHIRecipe>(&R);
@@ -1045,6 +1045,7 @@ void VPlan::execute(VPTransformState *State) {
10451045
Value *Phi = State->get(PhiR, NeedsScalar);
10461046
Value *Val = State->get(PhiR->getBackedgeValue(), NeedsScalar);
10471047
cast<PHINode>(Phi)->addIncoming(Val, VectorLatchBB);
1048+
}
10481049
}
10491050
}
10501051

@@ -1419,7 +1420,9 @@ bool VPValue::isDefinedOutsideLoopRegions() const {
14191420

14201421
const VPBasicBlock *DefVPBB = DefR->getParent();
14211422
auto *Plan = DefVPBB->getPlan();
1422-
return DefVPBB == Plan->getPreheader() || DefVPBB == Plan->getEntry();
1423+
if (Plan->getVectorLoopRegion())
1424+
return !DefR->getParent()->getEnclosingLoopRegion();
1425+
return DefVPBB == Plan->getEntry();
14231426
}
14241427
void VPValue::replaceAllUsesWith(VPValue *New) {
14251428
replaceUsesWithIf(New, [](VPUser &, unsigned) { return true; });

llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2057,7 +2057,6 @@ void VPlanTransforms::convertToConcreteRecipes(VPlan &Plan) {
20572057
R->removeRegion();
20582058
// Add explicit backedge.
20592059
VPBlockUtils::connectBlocks(Latch, Header);
2060-
delete R;
20612060
}
20622061

20632062
for (VPBasicBlock *VPBB : VPBlockUtils::blocksOnly<VPBasicBlock>(

0 commit comments

Comments
 (0)