This is an archive of the discontinued LLVM Phabricator instance.

[VPlan] Model masked assumes as replicate recipes, drop them (NFCI).
ClosedPublic

Authored by fhahn on Aug 3 2023, 2:10 PM.

Details

Summary

Replace ConditionalAssume set by treating conditional assumes like other
predicated instructions (i.e. create a VPReplicateRecipe with a mask)
and later remove any assume recipes with masks during VPlan cleanup.

This reduces coupling of VPlan construction and Legal by removing a
shared set between the 2 and results in a cleaner code structure
overall.

Diff Detail

Event Timeline

fhahn created this revision.Aug 3 2023, 2:10 PM
Herald added a project: Restricted Project. · View Herald TranscriptAug 3 2023, 2:10 PM
fhahn requested review of this revision.Aug 3 2023, 2:10 PM
Herald added a project: Restricted Project. · View Herald TranscriptAug 3 2023, 2:10 PM
Herald added subscribers: wangpc, vkmr. · View Herald Transcript
Ayal added a comment.Aug 4 2023, 3:21 AM

Neat cleanup!

llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
14–18
484

Worth adding a comment about dropping conditional assumes, for conditions destined to be flattened.

484–485

(Other, unpredicated) Assumes are not removed because they are considered to have side effects or have droppable users?

485–487

Would a single if look better?

fhahn updated this revision to Diff 547177.Aug 4 2023, 5:54 AM

Address latest comments, thanks!

fhahn marked 3 inline comments as done.Aug 4 2023, 5:55 AM
fhahn added inline comments.
llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
14–18

undid the unrelated changes here.

484

Added comment, thanks!

484–485

Presumably they are retained as they may add info helping later optimizations.

485–487

Updated, thanks!

Ayal added inline comments.Aug 4 2023, 2:22 PM
llvm/include/llvm/Transforms/Vectorize/LoopVectorizationLegality.h
446–447
534–535

/// or drop them in case of conditional assumes.

llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp
1560

nit (independent of this patch): pass MaskedOp instead of filling TmpMaskedOp and copying it over?

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
8926–8927

nit: if Instr is a branch there's not much left to continue.

llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
484–485

(Other, unpredicated) Assumes are not removed because they are considered to have side effects or have droppable users?

It is the former - assumes are considered to have side effects (return void, have no users).
Would it be better to exclude conditional assumes from the mayHaveSideEffect waivor, e.g., something like:

// A user keeps R alive:
if (any_of(R.definedValues(), [](VPValue *V) { return V->getNumUsers(); }))
  continue;

// Having side effects keeps R alive, but do remove conditional assume
// instructions as their conditions may be flattened.
auto *RepR = dyn_cast<VPReplicateRecipe>(&R);
bool IsConditionalAssume = RepR && RepR->isPredicated() &&
    match(RepR->getUnderlyingInstr(), m_Intrinsic<Intrinsic::assume>());
if (R.mayHaveSideEffects() && !IsConditionalAssume)
  continue;

R.eraseFromParent();

?

fhahn updated this revision to Diff 547480.Aug 5 2023, 6:08 AM
fhahn marked 7 inline comments as done.

Address latest comments, thanks!

fhahn marked an inline comment as done.Aug 5 2023, 6:22 AM
fhahn added inline comments.
llvm/include/llvm/Transforms/Vectorize/LoopVectorizationLegality.h
446–447

updated, thanks!

534–535

Updated thanks!

llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp
1560

Hmm, this might be intentional, to avoid populating MaskedOp partially if any block cannot be predicated.

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
8926–8927

Updated to drop the terminator when iterating over the block.

llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
484–485

Updated, thanks!

Ayal accepted this revision.Aug 5 2023, 2:03 PM

Looks good to me! Adding a couple of last nits.

llvm/include/llvm/Transforms/Vectorize/LoopVectorizationLegality.h
534–535

"drop" got dropped

llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp
1346–1347

nit: combine into a single if

1560

Ahh, right, returning false may still end up vectorizing. Worth a comment next to TmpMaskedOp.

This revision is now accepted and ready to land.Aug 5 2023, 2:03 PM
This revision was automatically updated to reflect the committed changes.
fhahn marked 4 inline comments as done.
fhahn added inline comments.Aug 6 2023, 12:57 PM
llvm/include/llvm/Transforms/Vectorize/LoopVectorizationLegality.h
534–535

Adjusted in the committed version, thanks!

llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp
1346–1347

Adjusted in the committed version ,thanks!

1560

Added in the committed version, thanks!