This is an archive of the discontinued LLVM Phabricator instance.

InstructionSimplify: Simplify a shuffle with a undef mask to undef
ClosedPublic

Authored by zvi on Apr 20 2017, 7:15 AM.

Details

Diff Detail

Repository
rL LLVM

Event Timeline

zvi created this revision.Apr 20 2017, 7:15 AM
davide requested changes to this revision.Apr 20 2017, 7:27 AM
davide added a subscriber: davide.
davide added inline comments.
lib/Analysis/InstructionSimplify.cpp
4147–4149 ↗(On Diff #95955)

microopt: you can move this up to the beginning of the function.

test/Transforms/InstSimplify/shufflevector.ll
113–119 ↗(On Diff #95955)

We have a similar case here, maybe it makes sense to handle them together?

This revision now requires changes to proceed.Apr 20 2017, 7:27 AM

We are not allowed to fold (select undef x y) -> undef, but we can fold (select undef x y) to x or to y. Likewise, I think this transformation needs to be (shufflevector x y undef) -> x.

We are not allowed to fold (select undef x y) -> undef, but we can fold (select undef x y) to x or to y. Likewise, I think this transformation needs to be (shufflevector x y undef) -> x.

Wait, never mind, LangRef says: "If the shuffle mask is undef, the result vector is undef"

zvi added inline comments.Apr 20 2017, 9:07 AM
test/Transforms/InstSimplify/shufflevector.ll
113–119 ↗(On Diff #95955)

Sorry i don't follow you. What do you mean by handling them together?
This case you are pointing out is handled by a more generic combine starting from DAGCombine.cpp:4154

Uhm, I don't follow you. Maybe DAGCombiner has a similar transformation, but instsimplify is powerful enough to handle the case I pointed out.

[davide@cupiditate bin]$ ./opt -instsimplify tinkywinky.ll -S
; ModuleID = 'tinkywinky.ll'
source_filename = "tinkywinky.ll"

define <4 x i32> @undef_mask(<4 x i32> %x) {
  ret <4 x i32> undef
}
[davide@cupiditate bin]$ cat tinkywinky.ll
define <4 x i32> @undef_mask(<4 x i32> %x) {
  %shuf = shufflevector <4 x i32> %x, <4 x i32> undef, <4 x i32> undef
  ret <4 x i32> %shuf
}
zvi added a comment.Apr 20 2017, 9:27 AM

Uhm, I don't follow you. Maybe DAGCombiner has a similar transformation, but instsimplify is powerful enough to handle the case I pointed out.

Sorry, what i really meant was InstructionSimplify.cpp:4154

test/Transforms/InstSimplify/shufflevector.ll
113–119 ↗(On Diff #95955)

I actually meant InstructionSimplify.cpp:4154 :)

zvi updated this revision to Diff 95969.Apr 20 2017, 9:30 AM
zvi edited edge metadata.

Moving added code to top of function

zvi marked an inline comment as done.Apr 20 2017, 9:30 AM
davide added inline comments.Apr 20 2017, 9:30 AM
test/Transforms/InstSimplify/shufflevector.ll
113–119 ↗(On Diff #95955)

I was wondering how hard it would be to extend that combine to handle also this case.

zvi added inline comments.Apr 20 2017, 9:43 AM
test/Transforms/InstSimplify/shufflevector.ll
113–119 ↗(On Diff #95955)

I plan in a future patch to do a refactoring/NFC-ish canonicalization of the shuffle input vectors and the mask. I believe that will make the special handling of the "single constant vector" case unnecessary, because the unreferenced variable vector will be canonicalized to an undef. Similarly, it is possible that canonicalization will cover the case of an undef mask, because it will replace both vector operands to undefs and then trivial constant-folding will handle that case.

davide added inline comments.Apr 20 2017, 9:46 AM
test/Transforms/InstSimplify/shufflevector.ll
113–119 ↗(On Diff #95955)

I'd appreciate if you can propose a refactoring before committing this patch (if possible)

zvi added inline comments.Apr 20 2017, 10:25 PM
test/Transforms/InstSimplify/shufflevector.ll
113–119 ↗(On Diff #95955)

D32338 adds a couple of canonicalization rules which are sufficient for what we cover so far.

This revision now requires changes to proceed.Apr 25 2017, 2:30 PM
spatel accepted this revision.Apr 27 2017, 10:24 AM

This patch is trivially LGTM based on the LangRef and discussion in PR32486:
https://bugs.llvm.org//show_bug.cgi?id=32486

zvi added a comment.Apr 27 2017, 10:29 AM

@davide, do you have any further comments?

No further comments from me.

Note, I don't qualify to LGTM as I'm not particularly familiar with vector code in LLVM.

zvi updated this revision to Diff 97209.Apr 29 2017, 11:18 PM

Rebase on top of trunk

This revision was automatically updated to reflect the committed changes.