This is an archive of the discontinued LLVM Phabricator instance.

[VPlan] Make sure optimizeInductions removes wide ind from scalar plan.
ClosedPublic

Authored by fhahn on Jun 28 2022, 1:06 PM.

Details

Summary

In some cases, there may be widened users of inductions even though the
plan includes the scalar VF. In those cases, make sure we still replace
the VPWidenIntOrFpInductionRecipe with scalar steps, as otherwise we may
try to execute a VPWidenIntOrFpInductionRecipe with a scalar VF.

Alternatively the patch could also split the range if needed.

This fixes a crash exposed by D123720.

Diff Detail

Event Timeline

fhahn created this revision.Jun 28 2022, 1:06 PM
Herald added a project: Restricted Project. · View Herald TranscriptJun 28 2022, 1:06 PM
fhahn requested review of this revision.Jun 28 2022, 1:06 PM
Herald added a project: Restricted Project. · View Herald TranscriptJun 28 2022, 1:06 PM
Herald added a subscriber: vkmr. · View Herald Transcript
bmahjour added inline comments.
llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
387–390

suggestion:

(none_of(IV->users(), [IV](VPUser *U) { return U->usesScalars(IV); })

could you please add the crashing IR from https://reviews.llvm.org/D123720 as an additional test? It still seems to crash with this patch!

Ayal added inline comments.Jun 29 2022, 12:59 AM
llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
384

How about AllUseScalars, or ScalarVF? "NeedsScalarSteps" combines the existence of a true usesScalars user together with a scalar VF.

Worth clarifying that usesScalars() assumes a VF>1 vector is given, as when VF=1 all users are provided a scalar and use it. I.e., "usesScalarsOfAVector()".

388

Worth reversing the condition, to early-continue if (!(IV && (NeedsScalarSteps || any_of(usesScalars(IV))?
(BTW, none_of() seems clearer than all_of(!)).

406

Worth reversing the condition, to early-continue if (!(NeedsScalarSteps || usesScalars(IV))?

llvm/test/Transforms/LoopVectorize/interleave-and-scalarize-only.ll
172

Explain what this test is designed to check? (Worth pre-committing it to demonstrate failure?)

fhahn updated this revision to Diff 440945.Jun 29 2022, 5:10 AM
fhahn marked 3 inline comments as done.

Address latest comments, thanks!

could you please add the crashing IR from https://reviews.llvm.org/D123720 as an additional test? It still seems to crash with this patch!

Is it still crashing with this patch applied to current main? It needs b18141a8f29f638be0602aa6ffbfc2d434b0b74a as well.

fhahn marked an inline comment as done.Jun 29 2022, 5:11 AM
fhahn added inline comments.
llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
384

How about AllUseScalars, or ScalarVF? "NeedsScalarSteps" combines the existence of a true usesScalars user together with a scalar VF.

Good point, I updated the variable name to ScalarVF.

Worth clarifying that usesScalars() assumes a VF>1 vector is given, as when VF=1 all users are provided a scalar and use it. I.e., "usesScalarsOfAVector()".

Sounds good, I can update that separately.

387–390

Thanks, I wasn't aware of none_of. Updated!

388

Worth reversing the condition, to early-continue if (!(IV && (NeedsScalarSteps || any_of(usesScalars(IV))?
(BTW, none_of() seems clearer than all_of(!)).

I moved the ScarlarVF check first, thanks! I also updated the check to use none_of and also moved out the !IV check to a separate if which hopefully should make the code a bit easier to follow.

llvm/test/Transforms/LoopVectorize/interleave-and-scalarize-only.ll
172

Thanks, I added an explanation. Unfortunately pre-committing it would mean we have to XFAIL the whole file, as it causes a crash with assertions :(

Address latest comments, thanks!

could you please add the crashing IR from https://reviews.llvm.org/D123720 as an additional test? It still seems to crash with this patch!

Is it still crashing with this patch applied to current main? It needs b18141a8f29f638be0602aa6ffbfc2d434b0b74a as well.

I was indeed missing b18141a8f29f638be0602aa6ffbfc2d434b0b74a. I've verified that the crash is gone with this patch on top of latest trunk. Thank you!

Ayal accepted this revision.Jun 29 2022, 10:58 AM

This fix is fine, thanks, adding a minor nit.

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

Ah, perhaps HasVectorVF = !Plan

388

Worth reversing the condition, to early-continue if (!(IV && (NeedsScalarSteps || any_of(usesScalars(IV))?
(BTW, none_of() seems clearer than all_of(!)).

I moved the ScarlarVF check first, thanks! I also updated the check to use none_of and also moved out the !IV check to a separate if which hopefully should make the code a bit easier to follow.

Thanks!
nit: seems logical to combine HasScalarVF || any_of(usesScalars(), because a scalar VF implies all use scalars. and then reverse it, w/o applying De Morgan, here and below. But if you prefer the current form is also ok.

This revision is now accepted and ready to land.Jun 29 2022, 10:58 AM
This revision was automatically updated to reflect the committed changes.
fhahn marked 2 inline comments as done.
fhahn added inline comments.Jun 30 2022, 1:15 AM
llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
384

I updated the name to use HasOnlyVectorVFs, thanks!