This is an archive of the discontinued LLVM Phabricator instance.

InstructionSimplify: Canonicalize shuffle operands. NFC-ish.
ClosedPublic

Authored by zvi on Apr 20 2017, 10:22 PM.

Details

Summary

Apply canonicalization rules:

  1. Input vectors with no elements selected from can be replaced with undef.
  2. If only one input vector is constant it shall be the second one.

This allows constant-folding to cover more ad-hoc simplifications that
were in place and avoid duplication for RHS and LHS checks.

There are more rules we may want to add in the future when we see a
justification. e.g. mask elements that select undef elements can be
replaced with undef.

Diff Detail

Repository
rL LLVM

Event Timeline

RKSimon added inline comments.Apr 21 2017, 2:09 AM
lib/Analysis/InstructionSimplify.cpp
4150–4151

You are calling ShuffleVectorInst::getMaskValue multiple times per element - better to call ShuffleVectorInst::getShuffleMask instead?

4171

constant

4172

Add tests for this?

4197

Add an assert here to check that NewIndices has MaskNumElts elements

zvi added inline comments.Apr 21 2017, 9:26 AM
lib/Analysis/InstructionSimplify.cpp
4150–4151

Right, this is the case even without this patch (see lines 4158 and 4188). Will add a parent patch to address that.

4172

The tests are already there and this is supposed to be a NFC patch.
This canonoicalization allowed the removal of lines 4169-4171 which are a duplication of lines 4166-4168 (or lines 4188-4190 after the patch is applied).
One test-case that should give coverage is the function 'splat_operand3' in test/Transforms/InstSimplify/shufflevector.ll

4197

Thanks for pointing this out. I didn't really intend for the newly created mask to be illegal in the case it contains an undef. Will fix this.

zvi updated this revision to Diff 96275.Apr 22 2017, 12:23 AM
zvi edited the summary of this revision. (Show Details)

Addressing some of Simon's comments.

zvi marked 5 inline comments as done.Apr 22 2017, 12:25 AM
zvi added inline comments.
lib/Analysis/InstructionSimplify.cpp
4150–4151

This is done in a parent patch, D32388.

zvi marked 2 inline comments as done.Apr 22 2017, 12:26 AM
spatel added inline comments.Apr 27 2017, 8:43 AM
lib/Analysis/InstructionSimplify.cpp
4140–4142

I got confused by the order of the patches/dependencies. This patch is proposing to remove this check that would be added by D32293. That's because the code in this patch will tell us that MaskSelects0 && MaskSelects1 are false, and that will then fall into ConstantFoldShuffleVectorInstruction and get folded?

I would've just left this check here as an early exit for the easy case.

FWIW, this may just be a case of too many cooks in the kitchen. :)
I think the end result of these 2 patches covers everything that we want in the current set of regression tests.

zvi added inline comments.Apr 27 2017, 9:59 AM
lib/Analysis/InstructionSimplify.cpp
4140–4142

Sorry about the excessive patches, will try to structure them better next time :)

The answer to your question is yes.

I thought it would be best to remove this early exit from fear of premature optimization as i don't have any data on how often this pattern occurs in real-life workloads (though from looking at the lit tests it would appear to happen quite often - but these are engineered corner cases).
I have no problem with leaving this early-exit in place..

zvi updated this revision to Diff 96949.Apr 27 2017, 11:11 AM

Keep the check for undef mask + early exit in place.

zvi marked 2 inline comments as done.Apr 27 2017, 11:12 AM
spatel accepted this revision.Apr 27 2017, 12:59 PM

LGTM. @RKSimon / @davide , any other comments?

This revision is now accepted and ready to land.Apr 27 2017, 12:59 PM
RKSimon accepted this revision.Apr 27 2017, 2:08 PM

LGTM

zvi updated this revision to Diff 97212.Apr 29 2017, 11:32 PM

Rebase on top of trunk

This revision was automatically updated to reflect the committed changes.