This is an archive of the discontinued LLVM Phabricator instance.

[VPlan] Model pre-header explicitly.
ClosedPublic

Authored by fhahn on Mar 14 2022, 10:18 AM.

Details

Summary

This patch extends the scope of VPlan to also model the pre-header.
The pre-header can be used to place recipes that should be code-gen'd
outside the loop, like SCEV expansion.

Depends on D121623.

Diff Detail

Event Timeline

fhahn created this revision.Mar 14 2022, 10:18 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 14 2022, 10:18 AM
fhahn requested review of this revision.Mar 14 2022, 10:18 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 14 2022, 10:18 AM
Herald added a subscriber: vkmr. · View Herald Transcript
fhahn updated this revision to Diff 415801.Mar 16 2022, 6:09 AM
fhahn added a comment.Mar 16 2022, 6:09 AM

Fix VPlan-native path. Prevously, VPlan-native VPlans had an empty pre-header as part of the main vector loop region. That has been fixed now and all code dealing with native VPlans has been updated. This removes a few places where we had to distinguish between inner loop and VPlan-native plans until now.

All tests are now updated as well and the patch is ready for review.

fhahn retitled this revision from [VPlan] Model pre-header explicitly (WIP). to [VPlan] Model pre-header explicitly..Mar 16 2022, 6:11 AM
fhahn edited the summary of this revision. (Show Details)
Ayal added inline comments.Mar 27 2022, 11:37 PM
llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
8751–8752

Comment deserves updating.

Start by creating Preheader, then Plan, and later insert TopRegion after Preheader?

9564

Worth caching in State.CFG.VectorPreHeader?

llvm/lib/Transforms/Vectorize/VPlan.cpp
330

"the loop header BB" >> "the loop preheader BB" ?

A2 should be a refinement of B: fold {single succ, single pred} pairs, provided they do not belong to distinct rotating regions?

499

nit: brackets can be removed

503–504

nit: if's can be fused

946–947

"between Header and Latch" >> between PreHeader and its successor/Header ?

946–951

"loop body" >> "loop preheader and body"

llvm/lib/Transforms/Vectorize/VPlanHCFGBuilder.cpp
77 ↗(On Diff #415801)

Can continue to return its Top Region, with the understanding that it succeeds a newly introduced preheader VPBB.

83 ↗(On Diff #415801)

Comment that/why \p HeaderBB is excluded?

259 ↗(On Diff #415801)

Still the case?

309 ↗(On Diff #415801)

Explain that we avoid representing backedges from latch to header blocks - a loop is instead represented by a parenting region?

344 ↗(On Diff #415801)

update comment

llvm/lib/Transforms/Vectorize/VPlanHCFGBuilder.h
58 ↗(On Diff #415801)

Update comment.

llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
30 ↗(On Diff #415801)

update comment?

fhahn updated this revision to Diff 419367.Mar 31 2022, 1:57 AM
fhahn marked 12 inline comments as done.

Address latest comments, thanks!

fhahn added inline comments.Mar 31 2022, 6:11 AM
llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
8751–8752

Added comment with up-to-date info about the initial skeleton.

9564

I moved the logic to VPTransformState::CFGState::getPreheaderBBFor. Not sure about caching it, as it may require a bit more tracking/invalidation for nested vector-loop regions with pre-headers, whereas the current code should handle them properly.

llvm/lib/Transforms/Vectorize/VPlan.cpp
330

"the loop header BB" >> "the loop preheader BB" ?

Done, thanks!

A2 should be a refinement of B: fold {single succ, single pred} pairs, provided they do not belong to distinct rotating regions?

Sounds good, I adjusted the code to require blocks to share the same (non-replicate) region.

499

Done, thanks!

503–504

Done, thanks!

946–951

Updated, thanks!

llvm/lib/Transforms/Vectorize/VPlanHCFGBuilder.cpp
77 ↗(On Diff #415801)

buildPlainCFG also creates the pre-header, so it seems more direct to return it directly, rather than requiring the callers to possibly get the predecessor?

I updated the comment but kept the return value the same.

83 ↗(On Diff #415801)

Added

259 ↗(On Diff #415801)

Yes I think so. Nothing should have changed with respect to LoopBlockDFS not visiting the pre-header.

309 ↗(On Diff #415801)

Added the comment, thanks!

344 ↗(On Diff #415801)

Updated, thanks!

llvm/lib/Transforms/Vectorize/VPlanHCFGBuilder.h
58 ↗(On Diff #415801)

Updated, thanks!

fhahn updated this revision to Diff 419433.Mar 31 2022, 6:53 AM

Rebased on top of 8378a71b6cce611e0, which needed to pull in the changes for creating the loop during VPlan.

Also updated to use getPreheaderForBB in more places.

fhahn updated this revision to Diff 420086.Apr 3 2022, 1:37 PM

Moved VPlan native path changes to separate patch D123005.

Ayal added inline comments.Apr 3 2022, 2:49 PM
llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
3104–3105

Worth commenting, say in the summary, that the skeleton is left creating only the preheader and the middle-block (and the scalar loop preheader), with the "vector.body" header-block now being created by VPlan::execute?

7602–7603

Comment deserves updating?

8751–8752

Start by creating Preheader, then Plan, and later insert TopRegion after Preheader?

suggestion was, i.e., to have

VPBasicBlock *Preheader = new VPBasicBlock("vector.ph");
auto Plan = std::make_unique<VPlan>(Preheader);

VPBasicBlock *HeaderVPBB = new VPBasicBlock();
VPBasicBlock *LatchVPBB = new VPBasicBlock("vector.latch");
VPBlockUtils::insertBlockAfter(LatchVPBB, HeaderVPBB);
auto *TopRegion = new VPRegionBlock(HeaderVPBB, LatchVPBB, "vector loop");
VPBlockUtils::insertBlockAfter(TopRegion, Preheader);
llvm/lib/Transforms/Vectorize/VPlan.cpp
249

This assumes VPBB is the header.
Can alternatively climb from VPBB to its enclosing loop region (ensure there is one), and take its predecessor; that will only assume VPBB belongs to a loop, which is implied by asking for its preheader.
All uses actually have a recipe to provide, can take it's parent VPBB here.
Perhaps a loop region should provide its preheader VPBB?

493

VPBB2IRBB[getPreheaderVPBB()]? (suggested above)

1457

getPreheaderBBFor()?

fhahn updated this revision to Diff 420131.Apr 4 2022, 3:42 AM
fhahn marked 7 inline comments as done.

Address latest comments, thanks!

Ayal accepted this revision.Apr 4 2022, 6:56 AM

This looks fine! Adding a couple of nits and future thoughts:

  • refactor recipes that reset a builder to getPreHeaderBBFor(), placing newly introduced recipes in the preheader VPBB instead?
  • refactor VPlan::prepareToExecute() into preheader recipes?
llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
3106

typo: heaader

8752–8758

(nit: could we initialize HeaderVPBB's name to "vector.body" here, next to vector.latch and vector.ph, instead of overwriting it below, provided we guard setting VPBB's name to BB->getName() if VPBB != HeaderVPBB?)

llvm/lib/Transforms/Vectorize/VPlan.cpp
339–340

getEnclosingLoopRegion() assumes/asserts we're in a loop; perhaps have it return null if we're not?

430

Add error message to the assert.

936–937

Update comment.
"inside the body" >> "inside the preheader and body"
"Assumes a single LoopVectorBody basic-block was created for this" - a single preheader basic-block?

1028

place below inside the 'if'?

1477

Do we need to getPreheaderBBFor(this), given that the recipe should reside in the preheader VPBB?

This revision is now accepted and ready to land.Apr 4 2022, 6:56 AM
fhahn updated this revision to Diff 420474.Apr 5 2022, 5:54 AM
fhahn marked 7 inline comments as done.

Thanks Ayal! I addressed the latest comments and also managed to make the patch independent of the VPlan native patches. To do so, the native VPlan is made compatible with the inner loop vplan after construction and predication.

fhahn updated this revision to Diff 421601.Apr 8 2022, 11:40 AM

Rebase after recent changes, I plan to land this soon.

Ayal added inline comments.Apr 8 2022, 2:11 PM
llvm/lib/Transforms/Vectorize/VPlan.cpp
1477

We do, at this time, until D122095 moves the recipe (from the header) to the preheader VPBB.

This revision was landed with ongoing or failed builds.Apr 9 2022, 5:25 AM
This revision was automatically updated to reflect the committed changes.
fhahn added a comment.Apr 9 2022, 5:51 AM

I just realized I forgot to submit my responses to the latest comments. Sorry about that and here they are :)

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
3104–3105

Update, thanks!

3106

Fixed, thanks!

7602–7603

Updated, thanks!

8751–8752

Done, thanks!

8752–8758

updated, thanks!

llvm/lib/Transforms/Vectorize/VPlan.cpp
249

Good points, updated to take a recipe, and use getEnclosingLoopRegion helper.

339–340

I think it should return null if the block doesn't have parent region. The assertion at the moment ensures we do not have nested replicate region.

430

Added!

493

Done, thanks!

936–937

Updated, thanks!

946–947

I think this stale comment is gone now.

1028

Moved thanks!

1457

Updated, thanks!

1477

This patch doesn't yet move any recipes. The linked follow-up patches will perform the moves, including D122095 and children.