Skip to content

[VPlan] Introduce all loop regions as VPlan transform. (NFC) #129402

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
merged 14 commits into from
Apr 16, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
Prev Previous commit
Next Next commit
!fixup adderss latest comments, thanks!
  • Loading branch information
fhahn committed Apr 13, 2025
commit c6903d64f99a7243cfbf80b009623c083fb2573c
2 changes: 1 addition & 1 deletion llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9561,7 +9561,7 @@ LoopVectorizationPlanner::tryToBuildVPlanWithVPRecipes(VFRange &Range) {
Range);
auto Plan = std::make_unique<VPlan>(OrigLoop);
// Build hierarchical CFG.
// TODO: Convert to VPlan-transform and consoliate all transforms for VPlan
// TODO: Convert to VPlan-transform and consolidate all transforms for VPlan
// creation.
VPlanHCFGBuilder HCFGBuilder(OrigLoop, LI, *Plan);
HCFGBuilder.buildPlainCFG();
Expand Down
10 changes: 6 additions & 4 deletions llvm/lib/Transforms/Vectorize/VPlanConstruction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@ getPreheaderAndLatch(VPBlockBase *HeaderVPB, const VPDominatorTree &VPDT) {
return std::nullopt;
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
return std::nullopt;
return false;

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!

}

/// Create a new VPRegionBlock if there is a loop starting at \p HeaderVPB.
static void createLoopRegion(VPlan &Plan, VPBlockBase *HeaderVPB,
VPDominatorTree &VPDT) {
/// Try to create a new VPRegionBlock if there is a loop starting at \p
/// HeaderVPB.
static void tryToCreateLoopRegion(VPlan &Plan, VPBlockBase *HeaderVPB,
VPDominatorTree &VPDT) {
auto Res = getPreheaderAndLatch(HeaderVPB, VPDT);
if (!Res)
return;
Expand Down Expand Up @@ -93,14 +94,15 @@ void VPlanTransforms::createLoopRegions(VPlan &Plan, Type *InductionTy,
VPDominatorTree VPDT;
VPDT.recalculate(Plan);
for (VPBlockBase *HeaderVPB : vp_depth_first_shallow(Plan.getEntry()))
createLoopRegion(Plan, HeaderVPB, VPDT);
tryToCreateLoopRegion(Plan, HeaderVPB, VPDT);
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
tryToCreateLoopRegion(Plan, HeaderVPB, VPDT);
if (canonicalHeader(HeaderVPB, VPDT)
createLoopRegion(Plan, HeaderVPB);

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!


VPRegionBlock *TopRegion = Plan.getVectorLoopRegion();
auto *OrigExiting = TopRegion->getExiting();
VPBasicBlock *LatchVPBB = Plan.createVPBasicBlock("vector.latch");
VPBlockUtils::insertBlockAfter(LatchVPBB, OrigExiting);
TopRegion->setExiting(LatchVPBB);
TopRegion->setName("vector loop");
TopRegion->getEntryBasicBlock()->setName("vector.body");

// Create SCEV and VPValue for the trip count.
// We use the symbolic max backedge-taken-count, which works also when
Expand Down
17 changes: 4 additions & 13 deletions llvm/lib/Transforms/Vectorize/VPlanHCFGBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,6 @@ void PlainCFGBuilder::fixHeaderPhis() {
assert(VPPhi->getNumOperands() == 0 &&
"Expected VPInstruction with no operands.");
assert(isHeaderBB(Phi->getParent(), LI->getLoopFor(Phi->getParent())));
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
assert(isHeaderBB(Phi->getParent(), LI->getLoopFor(Phi->getParent())));
assert(isHeaderBB(Phi->getParent(), LI->getLoopFor(Phi->getParent())) &&
"Expected Phi in header 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.

Done thanks

// For header phis, make sure the incoming value from the loop
// predecessor is the first operand of the recipe.
assert(Phi->getNumOperands() == 2 &&
"header phi must have exactly 2 operands");
for (BasicBlock *Pred : predecessors(Phi->getParent()))
Expand All @@ -109,10 +107,6 @@ void PlainCFGBuilder::fixHeaderPhis() {
}
}

static bool isHeaderVPBB(VPBasicBlock *VPBB) {
return VPBB->getParent() && VPBB->getParent()->getEntry() == VPBB;
}

// Create a new empty VPBasicBlock for an incoming BasicBlock or retrieve an
// existing one if it was already created.
VPBasicBlock *PlainCFGBuilder::getOrCreateVPBB(BasicBlock *BB) {
Expand All @@ -122,7 +116,7 @@ VPBasicBlock *PlainCFGBuilder::getOrCreateVPBB(BasicBlock *BB) {
}

// Create new VPBB.
StringRef Name = isHeaderBB(BB, TheLoop) ? "vector.body" : BB->getName();
StringRef Name = BB->getName();
LLVM_DEBUG(dbgs() << "Creating VPBasicBlock for " << Name << "\n");
VPBasicBlock *VPBB = Plan.createVPBasicBlock(Name);
BB2VPBB[BB] = VPBB;
Expand Down Expand Up @@ -325,10 +319,7 @@ void PlainCFGBuilder::buildPlainCFG(
auto *BI = cast<BranchInst>(BB->getTerminator());
unsigned NumSuccs = succ_size(BB);
if (NumSuccs == 1) {
auto *Successor = getOrCreateVPBB(BB->getSingleSuccessor());
VPBB->setOneSuccessor(isHeaderVPBB(Successor)
? Successor->getParent()
: static_cast<VPBlockBase *>(Successor));
VPBB->setOneSuccessor(getOrCreateVPBB(BB->getSingleSuccessor()));
continue;
}
assert(BI->isConditional() && NumSuccs == 2 && BI->isConditional() &&
Expand All @@ -341,8 +332,8 @@ void PlainCFGBuilder::buildPlainCFG(

// Don't connect any blocks outside the current loop except the latches for
// inner loops.
if (LoopForBB &&
(LoopForBB == TheLoop || BB != LoopForBB->getLoopLatch())) {
// TODO: Also connect exit blocks during initial VPlan construction.
if (LoopForBB == TheLoop || BB != LoopForBB->getLoopLatch()) {
if (!LoopForBB->contains(IRSucc0)) {
VPBB->setOneSuccessor(Successor1);
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ define void @foo(i64 %n) {
; CHECK-NEXT: {
; CHECK-EMPTY:
; CHECK-NEXT: ir-bb<entry>:
; CHECK-NEXT: Successor(s): vector.body
; CHECK-NEXT: Successor(s): outer.header
; CHECK-EMPTY:
; CHECK-NEXT: vector.body:
; CHECK-NEXT: outer.header:
; CHECK-NEXT: WIDEN-PHI ir<%outer.iv> = phi ir<%outer.iv.next>, ir<0>
; CHECK-NEXT: EMIT ir<%gep.1> = getelementptr ir<@arr2>, ir<0>, ir<%outer.iv>
; CHECK-NEXT: EMIT store ir<%outer.iv>, ir<%gep.1>
Expand All @@ -31,7 +31,7 @@ define void @foo(i64 %n) {
; CHECK-NEXT: outer.latch:
; CHECK-NEXT: EMIT ir<%outer.iv.next> = add ir<%outer.iv>, ir<1>
; CHECK-NEXT: EMIT ir<%outer.ec> = icmp ir<%outer.iv.next>, ir<8>
; CHECK-NEXT: Successor(s): vector.body
; CHECK-NEXT: Successor(s): outer.header
; CHECK-NEXT: }
entry:
br label %outer.header
Expand Down