Skip to content

[VPlan] Remove loop region in optimizeForVFAndUF. #108378

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
d3614bc
[VPlan] Use ResumePhi to create reduction resume phis.
fhahn Sep 25, 2024
5f8fabe
[VPlan] Remove loop region in optimizeForVFAndUF.
fhahn Sep 25, 2024
c68ddd1
Merge remote-tracking branch 'origin/main' into vplan-remove-loop-reg…
fhahn Nov 2, 2024
f0421c6
!fixup after merge
fhahn Nov 4, 2024
4a0eb12
Merge remote-tracking branch 'origin/main' into vplan-remove-loop-reg…
fhahn Nov 4, 2024
9499aaa
Merge remote-tracking branch 'origin/main' into vplan-remove-loop-reg…
fhahn Nov 4, 2024
a4843b5
!fixup address latest comments, thanks!
fhahn Nov 4, 2024
f5d2bc6
Merge remote-tracking branch 'origin/main' into vplan-remove-loop-reg…
fhahn Nov 9, 2024
1222e23
Merge remote-tracking branch 'origin/main' into vplan-remove-loop-reg…
fhahn Nov 24, 2024
706b681
!fixup address latest comments, thanks!
fhahn Nov 24, 2024
71436fc
!fixup fix formatting
fhahn Nov 24, 2024
38cbdf6
Merge remote-tracking branch 'origin/main' into vplan-remove-loop-reg…
fhahn Dec 7, 2024
cc43362
!fixup update after merge.
fhahn Dec 7, 2024
e758945
!fixup remove unrelated changes.
fhahn Dec 7, 2024
d4e8c7e
Merge remote-tracking branch 'origin/main' into vplan-remove-loop-reg…
fhahn Dec 17, 2024
1d4b2e6
!fixup partial fixup after merging main.
fhahn Dec 17, 2024
98529f3
Merge remote-tracking branch 'origin/main' into vplan-remove-loop-reg…
fhahn Dec 23, 2024
0c76e9d
!fixup update on top of current main, iterate to find vector.ph
fhahn Dec 23, 2024
dd45cad
[VPlan] Manage created blocks directly in VPlan. (NFC)
fhahn Dec 18, 2024
e72a71f
!fixup address comments, add comments
fhahn Dec 24, 2024
407dbc1
[VPlan] Funnel
fhahn Dec 27, 2024
af48fcc
[VPlan] Funnel
fhahn Dec 27, 2024
f51412a
Merge remote-tracking branch 'origin/main' into manage-blocks-in-vplan
fhahn Dec 27, 2024
f2b5e53
Merge remote-tracking branch 'origin/main' into manage-blocks-in-vplan
fhahn Dec 28, 2024
1f4febc
Merge branch 'manage-blocks-in-vplan' into vplan-remove-loop-region-i…
fhahn Dec 28, 2024
3d412a1
Merge remote-tracking branch 'origin/main' into vplan-remove-loop-reg…
fhahn Dec 31, 2024
56a4181
!fixup update after merge
fhahn Dec 31, 2024
88fcf60
Merge remote-tracking branch 'origin/main' into vplan-remove-loop-reg…
fhahn Dec 31, 2024
303ce93
!fixup adjust assert
fhahn Dec 31, 2024
3f016cb
Merge remote-tracking branch 'origin/main' into vplan-remove-loop-reg…
fhahn Dec 31, 2024
430c369
Merge remote-tracking branch 'origin/main' into vplan-remove-loop-reg…
fhahn Dec 31, 2024
f9db2d0
!fixup update extra tests.
fhahn Dec 31, 2024
cabc591
!fixup fix formatting
fhahn Dec 31, 2024
057f2e9
[VPlan] Replace VPBBs with VPIRBBs during skeleton creation (NFC).
fhahn Jan 1, 2025
60a046a
Merge remote-tracking branch 'origin/main' into vplan-remove-loop-reg…
fhahn Jan 1, 2025
fe2c3a5
[VPlan] Track VectorPH during skeleton creation.
fhahn Jan 1, 2025
26c94b1
!fixup restore original getVectorLoopRegion impl
fhahn Jan 1, 2025
92c0ccc
Merge remote-tracking branch 'origin/main' into vplan-remove-loop-reg…
fhahn Jan 2, 2025
05b2e4e
Merge remote-tracking branch 'origin/main' into vplan-remove-loop-reg…
fhahn Jan 3, 2025
df67f2e
Merge remote-tracking branch 'origin/main' into vplan-remove-loop-reg…
fhahn Jan 3, 2025
e17003f
Merge remote-tracking branch 'origin/main' into vplan-remove-loop-reg…
fhahn Jan 3, 2025
f180edb
!fixup address latest comments, thanks
fhahn Jan 3, 2025
71ff80a
!fixup fix formatting
fhahn Jan 3, 2025
911c50a
Merge remote-tracking branch 'origin/main' into vplan-remove-loop-reg…
fhahn Jan 4, 2025
4f74827
!fixup restore newline and move comment
fhahn Jan 4, 2025
d17571d
!fixup move erase of term just before removing dead recipes.
fhahn Jan 4, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 38 additions & 27 deletions llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2471,6 +2471,8 @@ InnerLoopVectorizer::getOrCreateVectorTripCount(BasicBlock *InsertBlock) {
/// scalar preheader.
static void introduceCheckBlockInVPlan(VPlan &Plan, BasicBlock *CheckIRBB) {
VPBlockBase *ScalarPH = Plan.getScalarPreheader();
// FIXME: Cannot get the vector preheader at the moment if the vector loop
// region has been removed.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Passing in VectorPH seems fine, still need "FIXME"? In fact, can pass both VectorDestination and ScalarDestination, i.e., the two block that the new check block is introduced to select between.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixme is gone, thanks

VPBlockBase *VectorPH = Plan.getVectorPreheader();
VPBlockBase *PreVectorPH = VectorPH->getSinglePredecessor();
if (PreVectorPH->getNumSuccessors() != 1) {
Expand Down Expand Up @@ -3066,6 +3068,9 @@ void InnerLoopVectorizer::fixVectorizedLoop(VPTransformState &State) {
getOrCreateVectorTripCount(nullptr), LoopMiddleBlock, State);
}

if (!State.Plan->getVectorLoopRegion())
return;

Comment on lines +3017 to +3019
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment why this is placed here, i.e., why all above should work even if vector loop region was removed, and all below should not.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added thanks

for (Instruction *PI : PredicatedInstructions)
sinkScalarOperands(&*PI);

Expand Down Expand Up @@ -7859,24 +7864,25 @@ DenseMap<const SCEV *, Value *> LoopVectorizationPlanner::executePlan(
makeFollowupLoopID(OrigLoopID, {LLVMLoopVectorizeFollowupAll,
LLVMLoopVectorizeFollowupVectorized});

VPBasicBlock *HeaderVPBB =
BestVPlan.getVectorLoopRegion()->getEntryBasicBlock();
Loop *L = LI->getLoopFor(State.CFG.VPBB2IRBB[HeaderVPBB]);
if (VectorizedLoopID)
L->setLoopID(*VectorizedLoopID);
else {
// Keep all loop hints from the original loop on the vector loop (we'll
// replace the vectorizer-specific hints below).
if (MDNode *LID = OrigLoop->getLoopID())
L->setLoopID(LID);

LoopVectorizeHints Hints(L, true, *ORE);
Hints.setAlreadyVectorized();
if (auto *R = BestVPlan.getVectorLoopRegion()) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: fold all of 2.6 under this condition, including setting OrigLoopID and VectorizedLoopID.

Perhaps worth spitting out a debug note when hints are being dropped.

Update comment above that all loop hints are kept on the vector loop - provided there is one.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, thanks

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (auto *R = BestVPlan.getVectorLoopRegion()) {
if (auto *LoopRegion = BestVPlan.getVectorLoopRegion()) {

(R is often used to denote Recipe)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done thanks

VPBasicBlock *HeaderVPBB = R->getEntryBasicBlock();
Loop *L = LI->getLoopFor(State.CFG.VPBB2IRBB[HeaderVPBB]);
if (VectorizedLoopID) {
L->setLoopID(*VectorizedLoopID);
} else {
// Keep all loop hints from the original loop on the vector loop (we'll
// replace the vectorizer-specific hints below).
if (MDNode *LID = OrigLoop->getLoopID())
L->setLoopID(LID);

LoopVectorizeHints Hints(L, true, *ORE);
Hints.setAlreadyVectorized();
}
TargetTransformInfo::UnrollingPreferences UP;
TTI.getUnrollingPreferences(L, *PSE.getSE(), UP, ORE);
if (!UP.UnrollVectorizedLoop || VectorizingEpilogue)
addRuntimeUnrollDisableMetaData(L);
}
TargetTransformInfo::UnrollingPreferences UP;
TTI.getUnrollingPreferences(L, *PSE.getSE(), UP, ORE);
if (!UP.UnrollVectorizedLoop || VectorizingEpilogue)
addRuntimeUnrollDisableMetaData(L);

// 3. Fix the vectorized code: take care of header phi's, live-outs,
// predication, updating analyses.
Expand All @@ -7885,15 +7891,19 @@ DenseMap<const SCEV *, Value *> LoopVectorizationPlanner::executePlan(
ILV.printDebugTracesAtEnd();

// 4. Adjust branch weight of the branch in the middle block.
auto *MiddleTerm =
cast<BranchInst>(State.CFG.VPBB2IRBB[ExitVPBB]->getTerminator());
if (MiddleTerm->isConditional() &&
hasBranchWeightMD(*OrigLoop->getLoopLatch()->getTerminator())) {
// Assume that `Count % VectorTripCount` is equally distributed.
unsigned TripCount = BestVPlan.getUF() * State.VF.getKnownMinValue();
assert(TripCount > 0 && "trip count should not be zero");
const uint32_t Weights[] = {1, TripCount - 1};
setBranchWeights(*MiddleTerm, Weights, /*IsExpected=*/false);
if (auto *R = BestVPlan.getVectorLoopRegion()) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (auto *R = BestVPlan.getVectorLoopRegion()) {
if (auto *LoopRegion = BestVPlan.getVectorLoopRegion()) {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adjusted, thanks

auto *ExitVPBB = cast<VPBasicBlock>(R->getSingleSuccessor());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overwrite ExitVPBB set to middle block above?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Name always refers to the middle block, fixed in 11c6af6, thanks


auto *MiddleTerm =
cast<BranchInst>(State.CFG.VPBB2IRBB[ExitVPBB]->getTerminator());
if (MiddleTerm->isConditional() &&
hasBranchWeightMD(*OrigLoop->getLoopLatch()->getTerminator())) {
// Assume that `Count % VectorTripCount` is equally distributed.
unsigned TripCount = BestVPlan.getUF() * State.VF.getKnownMinValue();
assert(TripCount > 0 && "trip count should not be zero");
const uint32_t Weights[] = {1, TripCount - 1};
setBranchWeights(*MiddleTerm, Weights, /*IsExpected=*/false);
}
}

return State.ExpandedSCEVs;
Expand Down Expand Up @@ -9735,7 +9745,8 @@ void VPDerivedIVRecipe::execute(VPTransformState &State) {
State.Builder, CanonicalIV, getStartValue()->getLiveInIRValue(), Step,
Kind, cast_if_present<BinaryOperator>(FPBinOp));
DerivedIV->setName(Name);
assert(DerivedIV != CanonicalIV && "IV didn't need transforming?");
assert((isa<Constant>(CanonicalIV) || DerivedIV != CanonicalIV) &&
"IV didn't need transforming?");

State.set(this, DerivedIV, VPLane(0));
}
Expand Down
38 changes: 19 additions & 19 deletions llvm/lib/Transforms/Vectorize/VPlan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,6 @@ void VPBasicBlock::connectToPredecessors(VPTransformState::CFGState &CFG) {
CFG.DTU.applyUpdates({{DominatorTree::Insert, PredBB, NewBB}});
}
}

void VPIRBasicBlock::execute(VPTransformState *State) {
assert(getHierarchicalSuccessors().size() <= 2 &&
"VPIRBasicBlock can have at most two successors at the moment!");
Expand Down Expand Up @@ -484,7 +483,7 @@ void VPBasicBlock::execute(VPTransformState *State) {
};

// 1. Create an IR basic block.
if (this == getPlan()->getVectorPreheader() ||
if (this == getPlan()->getEntry() ||
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be committed independently?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually this check can be removed completely, as the pre-header and entry are modeled as VPIRBasicBlocks.

Split off to 20d491b

(Replica && this == getParent()->getEntry()) ||
IsReplicateRegion(getSingleHierarchicalPredecessor())) {
// Reuse the previous basic block if the current VPBB is either
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update comment below: "// * the vector preheader," >> "// * the entry to VPlan,"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dropped, thanks

Expand Down Expand Up @@ -939,7 +938,6 @@ void VPlan::prepareToExecute(Value *TripCountV, Value *VectorTripCountV,

IRBuilder<> Builder(State.CFG.PrevBB->getTerminator());
// FIXME: Model VF * UF computation completely in VPlan.
assert(VFxUF.getNumUsers() && "VFxUF expected to always have users");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this related?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We may remove the users (the canonical IV increment). Updated to account for that in assertion

unsigned UF = getUF();
if (VF.getNumUsers()) {
Value *RuntimeVF = getRuntimeVF(Builder, TCTy, State.VF);
Expand Down Expand Up @@ -1024,16 +1022,17 @@ void VPlan::execute(VPTransformState *State) {
for (VPBlockBase *Block : RPOT)
Block->execute(State);

VPBasicBlock *LatchVPBB = getVectorLoopRegion()->getExitingBasicBlock();
BasicBlock *VectorLatchBB = State->CFG.VPBB2IRBB[LatchVPBB];
if (auto *LoopRegion = getVectorLoopRegion()) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Better than R ;-))

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: early-exit would reduce troubled diff, but requires duplicating State->CFG.DTU.flush();

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated, thanks! DTU.flush can be pulled up

VPBasicBlock *LatchVPBB = LoopRegion->getExitingBasicBlock();
BasicBlock *VectorLatchBB = State->CFG.VPBB2IRBB[LatchVPBB];

// Fix the latch value of canonical, reduction and first-order recurrences
// phis in the vector loop.
VPBasicBlock *Header = getVectorLoopRegion()->getEntryBasicBlock();
for (VPRecipeBase &R : Header->phis()) {
// Skip phi-like recipes that generate their backedege values themselves.
if (isa<VPWidenPHIRecipe>(&R))
continue;
// Fix the latch value of canonical, reduction and first-order recurrences
// phis in the vector loop.
VPBasicBlock *Header = LoopRegion->getEntryBasicBlock();
for (VPRecipeBase &R : Header->phis()) {
// Skip phi-like recipes that generate their backedege values themselves.
if (isa<VPWidenPHIRecipe>(&R))
continue;

if (isa<VPWidenInductionRecipe>(&R)) {
PHINode *Phi = nullptr;
Expand All @@ -1060,13 +1059,14 @@ void VPlan::execute(VPTransformState *State) {
continue;
}

auto *PhiR = cast<VPHeaderPHIRecipe>(&R);
bool NeedsScalar = isa<VPScalarPHIRecipe>(PhiR) ||
(isa<VPReductionPHIRecipe>(PhiR) &&
cast<VPReductionPHIRecipe>(PhiR)->isInLoop());
Value *Phi = State->get(PhiR, NeedsScalar);
Value *Val = State->get(PhiR->getBackedgeValue(), NeedsScalar);
cast<PHINode>(Phi)->addIncoming(Val, VectorLatchBB);
auto *PhiR = cast<VPHeaderPHIRecipe>(&R);
bool NeedsScalar = isa<VPScalarPHIRecipe>(PhiR) ||
(isa<VPReductionPHIRecipe>(PhiR) &&
cast<VPReductionPHIRecipe>(PhiR)->isInLoop());
Value *Phi = State->get(PhiR, NeedsScalar);
Value *Val = State->get(PhiR->getBackedgeValue(), NeedsScalar);
cast<PHINode>(Phi)->addIncoming(Val, VectorLatchBB);
}
}

State->CFG.DTU.flush();
Expand Down
6 changes: 5 additions & 1 deletion llvm/lib/Transforms/Vectorize/VPlan.h
Original file line number Diff line number Diff line change
Expand Up @@ -3722,6 +3722,7 @@ class VPRegionBlock : public VPBlockBase {
assert(!isReplicator() && "should only get pre-header of loop regions");
return getSinglePredecessor()->getExitingBasicBlock();
}
void clearEntry() { Entry = nullptr; }

/// An indicator whether this region is to generate multiple replicated
/// instances of output IR corresponding to its VPBlockBases.
Expand Down Expand Up @@ -3866,7 +3867,10 @@ class VPlan {

/// Returns the preheader of the vector loop region.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// Returns the preheader of the vector loop region.
/// Returns the preheader of the vector loop region, if one exists, or null otherwise.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated, thanks!

VPBasicBlock *getVectorPreheader() {
return cast<VPBasicBlock>(getVectorLoopRegion()->getSinglePredecessor());
auto *LoopRegion = getVectorLoopRegion();
if (!LoopRegion)
return nullptr;
return dyn_cast<VPBasicBlock>(LoopRegion->getSinglePredecessor());
}

/// Returns the VPRegionBlock of the vector loop.
Expand Down
4 changes: 3 additions & 1 deletion llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2378,7 +2378,9 @@ void VPBranchOnMaskRecipe::execute(VPTransformState &State) {
// Replace the temporary unreachable terminator with a new conditional branch,
// whose two destinations will be set later when they are created.
auto *CurrentTerminator = State.CFG.PrevBB->getTerminator();
assert(isa<UnreachableInst>(CurrentTerminator) &&
assert((isa<UnreachableInst>(CurrentTerminator) ||
(isa<BranchInst>(CurrentTerminator) &&
!CurrentTerminator->getOperand(0))) &&
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not needed with the latest version, removed, thanks

"Expected to replace unreachable terminator with conditional branch.");
auto *CondBr = BranchInst::Create(State.CFG.PrevBB, nullptr, ConditionBit);
CondBr->setSuccessor(0, nullptr);
Expand Down
40 changes: 34 additions & 6 deletions llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -692,16 +692,44 @@ void VPlanTransforms::optimizeForVFAndUF(VPlan &Plan, ElementCount BestVF,
!SE.isKnownPredicate(CmpInst::ICMP_ULE, TripCount, C))
return;

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Say something about what is about to happen now.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added, thanks

LLVMContext &Ctx = SE.getContext();
auto *BOC = new VPInstruction(
VPInstruction::BranchOnCond,
{Plan.getOrAddLiveIn(ConstantInt::getTrue(Ctx))}, Term->getDebugLoc());

SmallVector<VPValue *> PossiblyDead(Term->operands());
Term->eraseFromParent();
auto *Header = cast<VPBasicBlock>(Plan.getVectorLoopRegion()->getEntry());
if (any_of(Header->phis(),
IsaPred<VPWidenIntOrFpInductionRecipe, VPReductionPHIRecipe>)) {
LLVMContext &Ctx = SE.getContext();
auto *BOC = new VPInstruction(
VPInstruction::BranchOnCond,
{Plan.getOrAddLiveIn(ConstantInt::getTrue(Ctx))}, Term->getDebugLoc());
ExitingVPBB->appendRecipe(BOC);
} else {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Say something about what is about to happen now.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added, thanks

for (VPRecipeBase &R : make_early_inc_range(Header->phis())) {
auto *P = cast<VPHeaderPHIRecipe>(&R);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
auto *P = cast<VPHeaderPHIRecipe>(&R);
auto *HeaderPhiR = cast<VPHeaderPHIRecipe>(&R);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done thanks

P->replaceAllUsesWith(P->getStartValue());
P->eraseFromParent();
}

VPBlockBase *Preheader = Plan.getVectorLoopRegion()->getSinglePredecessor();
auto *Exiting =
cast<VPBasicBlock>(Plan.getVectorLoopRegion()->getExiting());

auto *LoopRegion = Plan.getVectorLoopRegion();
VPBlockBase *Middle = LoopRegion->getSingleSuccessor();
VPBlockUtils::disconnectBlocks(Preheader, LoopRegion);
VPBlockUtils::disconnectBlocks(LoopRegion, Middle);

Header->setParent(nullptr);
Exiting->setParent(nullptr);
VPBlockUtils::connectBlocks(Preheader, Header);

VPBlockUtils::connectBlocks(Exiting, Middle);
// Set LoopRegion's Entry to nullptr, as the CFG from LoopRegion shouldn't
// be deleted when the region is deleted.
LoopRegion->clearEntry();
delete LoopRegion;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Potentially unsafe, may leak if the programmer forgets about it. Maybe, use Destructor or something like RAII?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if there is a nice way to do so currently. The whole replace-and-erase logic in the block needs to happen atomically at the moment. To avoid having to do manual delete's, VPlan could track all blocks added to it and delete them all when the VPlan is deleted (at the moment it only deletes reachable blocks). But that would require some extra logic to make sure all blocks are added properly.

}
for (VPValue *Op : PossiblyDead)
recursivelyDeleteDeadRecipes(Op);
ExitingVPBB->appendRecipe(BOC);
Plan.setVF(BestVF);
Plan.setUF(BestUF);
// TODO: Further simplifications are possible
Expand Down
15 changes: 6 additions & 9 deletions llvm/test/Transforms/LoopVectorize/AArch64/call-costs.ll
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,13 @@ define void @powi_call(ptr %P) {
; CHECK: [[VECTOR_PH]]:
; CHECK-NEXT: br label %[[VECTOR_BODY:.*]]
; CHECK: [[VECTOR_BODY]]:
; CHECK-NEXT: [[INDEX:%.*]] = phi i64 [ 0, %[[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], %[[VECTOR_BODY]] ]
; CHECK-NEXT: [[TMP0:%.*]] = add i64 [[INDEX]], 0
; CHECK-NEXT: [[TMP1:%.*]] = getelementptr inbounds double, ptr [[P]], i64 [[TMP0]]
; CHECK-NEXT: [[TMP1:%.*]] = getelementptr inbounds double, ptr [[P]], i64 0
; CHECK-NEXT: [[TMP2:%.*]] = getelementptr inbounds double, ptr [[TMP1]], i32 0
; CHECK-NEXT: [[WIDE_LOAD:%.*]] = load <2 x double>, ptr [[TMP2]], align 8
; CHECK-NEXT: [[TMP3:%.*]] = call <2 x double> @llvm.powi.v2f64.i32(<2 x double> [[WIDE_LOAD]], i32 3)
; CHECK-NEXT: store <2 x double> [[TMP3]], ptr [[TMP2]], align 8
; CHECK-NEXT: [[INDEX_NEXT]] = add nuw i64 [[INDEX]], 2
; CHECK-NEXT: br i1 true, label %[[MIDDLE_BLOCK:.*]], label %[[VECTOR_BODY]], !llvm.loop [[LOOP4:![0-9]+]]
; CHECK-NEXT: [[TMP4:%.*]] = getelementptr inbounds double, ptr [[TMP1]], i32 0
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better (continue to) reuse TMP2 instead of replicating it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this is a fall-out from not running non-VPlan-based simple CSE in fixVectorizedLoop

; CHECK-NEXT: store <2 x double> [[TMP3]], ptr [[TMP4]], align 8
; CHECK-NEXT: br label %[[MIDDLE_BLOCK:.*]]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Potential for merging original latch block with middle block?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep

; CHECK: [[MIDDLE_BLOCK]]:
; CHECK-NEXT: br i1 true, label %[[EXIT:.*]], label %[[SCALAR_PH]]
; CHECK: [[SCALAR_PH]]:
Expand All @@ -102,7 +100,7 @@ define void @powi_call(ptr %P) {
; CHECK-NEXT: store double [[POWI]], ptr [[GEP]], align 8
; CHECK-NEXT: [[IV_NEXT]] = add i64 [[IV]], 1
; CHECK-NEXT: [[EC:%.*]] = icmp eq i64 [[IV]], 1
; CHECK-NEXT: br i1 [[EC]], label %[[EXIT]], label %[[LOOP]], !llvm.loop [[LOOP5:![0-9]+]]
; CHECK-NEXT: br i1 [[EC]], label %[[EXIT]], label %[[LOOP]], !llvm.loop [[LOOP4:![0-9]+]]
; CHECK: [[EXIT]]:
; CHECK-NEXT: ret void
;
Expand Down Expand Up @@ -233,6 +231,5 @@ declare i64 @llvm.fshl.i64(i64, i64, i64)
; CHECK: [[META1]] = !{!"llvm.loop.isvectorized", i32 1}
; CHECK: [[META2]] = !{!"llvm.loop.unroll.runtime.disable"}
; CHECK: [[LOOP3]] = distinct !{[[LOOP3]], [[META2]], [[META1]]}
; CHECK: [[LOOP4]] = distinct !{[[LOOP4]], [[META1]], [[META2]]}
; CHECK: [[LOOP5]] = distinct !{[[LOOP5]], [[META2]], [[META1]]}
; CHECK: [[LOOP4]] = distinct !{[[LOOP4]], [[META2]], [[META1]]}
;.
Loading
Loading