This is an archive of the discontinued LLVM Phabricator instance.

[VPlan] Add predicate to VPReplicateRecipe, expand region later.
ClosedPublic

Authored by fhahn on Feb 12 2023, 2:45 PM.

Details

Summary

This patch adds the predicate as additional operand to VPReplicateRecipe
during initial construction. The predicated recipes are later moved into
replicate regions. This simplifies constructions and some VPlan
transformations, like fixed-order recurrence handling.

Diff Detail

Event Timeline

fhahn created this revision.Feb 12 2023, 2:45 PM
Herald added a project: Restricted Project. · View Herald TranscriptFeb 12 2023, 2:45 PM
fhahn requested review of this revision.Feb 12 2023, 2:45 PM
Herald added a project: Restricted Project. · View Herald TranscriptFeb 12 2023, 2:45 PM
fhahn updated this revision to Diff 498841.Feb 20 2023, 6:54 AM

Rebase after recent changes.

Ayal added a comment.Feb 20 2023, 3:30 PM

Nice clean-up!

(Inspired by "All You Need Is Superword-Level Parallelism: Systematic Control-Flow Vectorization with SLP", Yishen Chen
, Charith Mendis and Saman Amarasinghe, PLDI'22.)

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
2920

Could the last redundant operand of RepRecipe be removed once it is no longer needed (after wrapping it inside a replicate region) instead of silently ignoring it here?

8672

Worth clarifying that these instructions are first represented as a replicate recipe with a mask (facilitating their motion etc.) and are wrapping within the if-then masked region later?

9136

comment?

Is this the best position - must be placed before sinkScalarOperands()/mergeReplicateRegionsIntoSuccessors(), possibly right before them?

llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
622

Is this comment and code dealing with PredInstPHI still needed here?

689

Future thought: would it be better to handle intervals of RepR's that share the same mask together here, placing them in a common region, rather than fusing regions later?

llvm/lib/Transforms/Vectorize/VPlanTransforms.h
87

Documentation?

fhahn updated this revision to Diff 500768.Feb 27 2023, 6:37 AM
fhahn marked 5 inline comments as done.

Address comments, thanks!

fhahn added inline comments.Feb 27 2023, 6:39 AM
llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
2920

Updated the code to remove the mask, by creating a new VPReplicateRecipe without the mask.

8672

Adjusted, thanks!

9136

Added a comment and moved just before sinkScalarOperands/region merging.

llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
622

Not any longer, removed!

689

that would be a good follow-up change; general merging & sinking will probably still be useful, as sinking can enable new merging opportunities.

fhahn updated this revision to Diff 500772.Feb 27 2023, 6:42 AM
fhahn marked an inline comment as done.

Add missing doc-comment

fhahn added a comment.Mar 5 2023, 3:13 AM

Ping :)

llvm/lib/Transforms/Vectorize/VPlanTransforms.h
87

Should be added, thanks!

Ayal added inline comments.Mar 5 2023, 6:23 AM
llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
8675

nit: worth recording as Mask to be returned by getMask(), initially set to null, rather than having the latter return the last operand blindly below?

8689

nit: worth adding a comment that the masked replicate recipe is replaced by a non-masked one?

llvm/test/Transforms/LoopVectorize/X86/consecutive-ptr-uniforms.ll
94–95

nit: name of label fixed? Worth fixing them PRED_LOAD* names as well for consistency?

llvm/test/Transforms/LoopVectorize/first-order-recurrence-sink-replicate-region.ll
369

Second operand vp<[[STEPS]]> dropped intentionally?

371

ditto

llvm/test/Transforms/LoopVectorize/reduction-inloop-cond.ll
411

Two independent replicating regions with a reduce.add between them that were kept separate are now being merged? This patch seems conceptually an NFC, according to its current commit message.

fhahn marked 4 inline comments as done.Mar 5 2023, 11:35 AM
fhahn added inline comments.
llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
8675

I updated the code to add the mask in the constructer, iff IsPredicated is set.

8689

Added a comment, thanks!

llvm/test/Transforms/LoopVectorize/X86/consecutive-ptr-uniforms.ll
94–95

Fixed, thanks!

llvm/test/Transforms/LoopVectorize/reduction-inloop-cond.ll
411

In this case, all recipes are in the same VPBasicBlock when adjustRecipesForReductions is called, which will add the reduction recipes at the end of the block containing the original widen recipes, hence the change.

fhahn updated this revision to Diff 502453.Mar 5 2023, 11:53 AM
fhahn marked 4 inline comments as done.

Address latest comments, thanks! This now depends on D145322. Also add getMask helper.

Ayal added inline comments.Mar 7 2023, 2:28 PM
llvm/lib/Transforms/Vectorize/VPlan.h
1514

Passing IsPredicated is now redundant - can be inferred by checking if given Mask is null?

Ayal accepted this revision.Mar 8 2023, 1:13 AM

Looks good to me, thanks!
Adding a couple of final nits.

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
8701

nit: now that this is a VPlan2VPlan transformation, check if PredRecipe has users instead of Instr's type?

While we're here, could perhaps be simplified into (independent of this patch):

VPRecipeBase *PHIRecipe = nullptr;
if (PredRecipe has users outside its block) {
  PHIRecipe = new VPPredInstPHIRecipe(RecipeWithoutMask);
  RecipeWithoutMask->replaceAllUsesWith(PHIRecipe);
  PHIRecipe->setOperand(0, RecipeWithoutMask);
}
9188

nit: the above creation and iterative optimization of replicate regions could be folded into some VPlanTransforms::createAndOptimizeReplicateRegions(*Plan, RecipeBuilder);

llvm/lib/Transforms/Vectorize/VPlanTransforms.h
87

nit: perhaps better place addReplicateRegions() above right before sinkScalarOperand() - merge - merge?

llvm/test/Transforms/LoopVectorize/reduction-inloop-cond.ll
411

Wonderful!
Worth clarifying at commit message that this construction simplification may yield better optimized code?

This revision is now accepted and ready to land.Mar 8 2023, 1:13 AM
fhahn updated this revision to Diff 503283.Mar 8 2023, 2:41 AM
fhahn marked 4 inline comments as done.

Address latest comments & rebase before landing, thanks!

fhahn added inline comments.Mar 8 2023, 3:30 AM
llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
8701

Updated to check the users, I'll do the other simplification separately.

9188

Will do separately!

llvm/lib/Transforms/Vectorize/VPlan.h
1514

yes, I removed the option, thanks!

fhahn added inline comments.Mar 16 2023, 10:09 AM
llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
8701

Done in 2db71c9851e5, thanks!

9188

Done in eca14a810e59