This is an archive of the discontinued LLVM Phabricator instance.

[SVE] Fix bug in simplification of scalable vector instructions
ClosedPublic

Authored by ctetreau on Jan 28 2020, 7:27 AM.

Details

Summary
  • Most of the simplifications in SimplifyShuffleVectorInst depend on the

concrete value of, or the length of the mask vector. For scalable
vectors, this cannot be known at compile time.

  • for these tests, detect if the vector is scalable before attempting

the transformation

  • The functions ShuffleVectorInst::getMaskValue and

ShuffleVectorInst::getShuffleMask access the value of the constant mask.
However, since the length of the mask is unknown at compile time, these
function do not work for scalable vectors. Add asserts to ensure that
the input mask is not scalable

Event Timeline

ctetreau created this revision.Jan 28 2020, 7:27 AM
Herald added a project: Restricted Project. · View Herald Transcript
ctetreau updated this revision to Diff 240884.Jan 28 2020, 7:30 AM

Fixed permissions on test file

efriedma added inline comments.Jan 28 2020, 4:14 PM
llvm/lib/Analysis/InstructionSimplify.cpp
4079

Missing testcase?

4463

Redundant "if"?

4488

ConstantFoldShuffleVectorInstruction should be fine? Or is this temporary to work around some other issue?

4529

Can you rearrange this code so you can put the early return earlier?

ctetreau marked 3 inline comments as done.Jan 29 2020, 2:01 PM
ctetreau added inline comments.
llvm/lib/Analysis/InstructionSimplify.cpp
4463

The inner computation walks the indices array, so we do need to test it. I suppose I could have put the !Scalable check inside the match conditional, but I don't think that'd be any clearer. I guess it'd make the diff smaller?

4488

It could be made to be fine with the current implementation that uses a Constant*; The beginning of this function checks if (isa<UndefValue>(Mask)), and returns an undef. This currently contains a bug that I just now realized that I failed to address, but it's fixable. However, once https://reviews.llvm.org/D72467 is merged, it will be unsalvageable due to the fact that the mask is passed as an array of int, and to determine if it's an undef, you must walk the array. I wonder if it's worth fixing, since your patch seems destined to land soon.

4529

I did not rearrange the code because the current implementation forms an ordering of which simplification should be taken, and I assumed that this order was selected on purpose.

ctetreau marked an inline comment as done.Jan 29 2020, 2:16 PM
ctetreau added inline comments.
llvm/lib/Analysis/InstructionSimplify.cpp
4463

Actually, I mistakenly thought we were discussing the conditional at 4502. I believe that this is not redundant; this will walk from 0 .. MaskEltCount.Min, and will call operator[](i) on Indices, which (assuming SmallVector behaves like std::vector) will result in an out of bounds array access.

efriedma added inline comments.Jan 29 2020, 3:04 PM
llvm/lib/Analysis/InstructionSimplify.cpp
4488

With new shuffles, we'll eventually want this to work, but sure, we can leave it out for now.

4529

It didn't look like the transforms overlapped, but maybe it's more complicated than I thought at first glance?

ctetreau updated this revision to Diff 241781.Jan 31 2020, 11:27 AM

Address review issues. Resolve additional issue discovered in the IR parser

This revision is now accepted and ready to land.Jan 31 2020, 11:38 AM
ctetreau closed this revision.Feb 3 2020, 10:20 AM
rnk added a subscriber: rnk.Feb 3 2020, 11:14 AM

This test failed for me locally, so I reverted this change in rGa05441038a3a4. Here is the stack trace I got:
https://reviews.llvm.org/P8190

ctetreau reopened this revision.Feb 3 2020, 12:16 PM
This revision is now accepted and ready to land.Feb 3 2020, 12:16 PM
ctetreau updated this revision to Diff 242150.Feb 3 2020, 12:17 PM

[SVE] Fix bug in simplification of scalable vector instructions

  • Most of the simplifications in SimplifyShuffleVectorInst depend on the

concrete value of, or the length of the mask vector. For scalable
vectors, this cannot be known at compile time.

  • for these tests, detect if the vector is scalable before attempting

the transformation
-The functions ShuffleVectorInst::getMaskValue and
ShuffleVectorInst::getShuffleMask access the value of the constant mask.
However, since the length of the mask is unknown at compile time, these
function do not work for scalable vectors. Add asserts to ensure that
the input mask is not scalable

Fixed test failure caused by new patch https://reviews.llvm.org/D73435 that was merged after this patch was approved.

ctetreau updated this revision to Diff 242157.Feb 3 2020, 12:22 PM

Removed added extra whitespace in otherwise untouched code

ctetreau marked an inline comment as done.Feb 3 2020, 12:36 PM
ctetreau added inline comments.
llvm/lib/Analysis/ValueTracking.cpp
169

@RKSimon This is the new code added. It modifies your recent patch, so I figured you'd like to review it.

RKSimon added inline comments.Feb 3 2020, 12:46 PM
llvm/lib/Analysis/ValueTracking.cpp
169

Would it be viable to set all DemandedLHS/DemandedRHS instead so we can still handle all element cases?

ctetreau marked an inline comment as done.Feb 3 2020, 12:51 PM
ctetreau added inline comments.
llvm/lib/Analysis/ValueTracking.cpp
169

The problem is that for scalable vectors, we don't know how many elements there are. For a scalable vector, getVectorNumElements() returns the minimum number of elements there can be. If we have <vscale x 4 x i32>, it will return 4, which is the number of elements that would be in the vector if vscale = 1. But the value of vscale isn't known until runtime, so this doesn't really help us.

RKSimon added inline comments.Feb 3 2020, 1:21 PM
llvm/lib/Analysis/ValueTracking.cpp
169

OK, it's a shame we can't do anything in this case. No more comments.

ctetreau marked an inline comment as done.Feb 3 2020, 1:32 PM
ctetreau added inline comments.
llvm/lib/Analysis/ValueTracking.cpp
169

Scalable shufflevector is still kind of a work in progress. There's an RFC up (http://lists.llvm.org/pipermail/llvm-dev/2020-January/138762.html) to expand it, so hopefully this can be revisited after that.

apazos added inline comments.Feb 4 2020, 11:10 AM
llvm/lib/Analysis/ValueTracking.cpp
172

I see this line changed because of the conflict with https://reviews.llvm.org/D73435. Thanks for fixing it. LGTM.

ctetreau closed this revision.Feb 5 2020, 10:49 AM