-
Notifications
You must be signed in to change notification settings - Fork 14.5k
[InstCombine] Let shrinkSplatShuffle act on vectors of different lengths #148593
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be notified. If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers. If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
@llvm/pr-subscribers-llvm-transforms Author: Adar Dagan (Adar-Dagan) ChangesshrinkSplatShuffle in InstCombine would only move truncs up through shuffles if those shuffles inputs had the exact same type as their output, this PR weakens this constraint to only requiring that the scalar type of the input and output match. Full diff: https://github.com/llvm/llvm-project/pull/148593.diff 3 Files Affected:
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
index 033ef8be700eb..8a98fd3235915 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
@@ -708,10 +708,14 @@ static Instruction *shrinkSplatShuffle(TruncInst &Trunc,
auto *Shuf = dyn_cast<ShuffleVectorInst>(Trunc.getOperand(0));
if (Shuf && Shuf->hasOneUse() && match(Shuf->getOperand(1), m_Undef()) &&
all_equal(Shuf->getShuffleMask()) &&
- Shuf->getType() == Shuf->getOperand(0)->getType()) {
+ Shuf->getType()->getScalarType() ==
+ Shuf->getOperand(0)->getType()->getScalarType()) {
// trunc (shuf X, Undef, SplatMask) --> shuf (trunc X), Poison, SplatMask
// trunc (shuf X, Poison, SplatMask) --> shuf (trunc X), Poison, SplatMask
- Value *NarrowOp = Builder.CreateTrunc(Shuf->getOperand(0), Trunc.getType());
+ auto *const NewTruncTy =
+ VectorType::get(Trunc.getType()->getScalarType(),
+ cast<VectorType>(Shuf->getOperand(0)->getType())->getElementCount());
+ Value *NarrowOp = Builder.CreateTrunc(Shuf->getOperand(0), NewTruncTy);
return new ShuffleVectorInst(NarrowOp, Shuf->getShuffleMask());
}
diff --git a/llvm/test/Transforms/InstCombine/trunc-inseltpoison.ll b/llvm/test/Transforms/InstCombine/trunc-inseltpoison.ll
index 33fa2c375f1ec..f83352c94ad89 100644
--- a/llvm/test/Transforms/InstCombine/trunc-inseltpoison.ll
+++ b/llvm/test/Transforms/InstCombine/trunc-inseltpoison.ll
@@ -959,8 +959,8 @@ define <3 x i31> @wide_splat3(<3 x i33> %x) {
define <8 x i8> @wide_lengthening_splat(<4 x i16> %v) {
; CHECK-LABEL: @wide_lengthening_splat(
-; CHECK-NEXT: [[SHUF:%.*]] = shufflevector <4 x i16> [[V:%.*]], <4 x i16> poison, <8 x i32> zeroinitializer
-; CHECK-NEXT: [[TR:%.*]] = trunc <8 x i16> [[SHUF]] to <8 x i8>
+; CHECK-NEXT: [[TMP1:%.*]] = trunc <4 x i16> [[V:%.*]] to <4 x i8>
+; CHECK-NEXT: [[TR:%.*]] = shufflevector <4 x i8> [[TMP1]], <4 x i8> poison, <8 x i32> zeroinitializer
; CHECK-NEXT: ret <8 x i8> [[TR]]
;
%shuf = shufflevector <4 x i16> %v, <4 x i16> %v, <8 x i32> zeroinitializer
diff --git a/llvm/test/Transforms/InstCombine/trunc.ll b/llvm/test/Transforms/InstCombine/trunc.ll
index a85ce716fbdfa..8f727e365e88e 100644
--- a/llvm/test/Transforms/InstCombine/trunc.ll
+++ b/llvm/test/Transforms/InstCombine/trunc.ll
@@ -960,8 +960,8 @@ define <3 x i31> @wide_splat3(<3 x i33> %x) {
define <8 x i8> @wide_lengthening_splat(<4 x i16> %v) {
; CHECK-LABEL: @wide_lengthening_splat(
-; CHECK-NEXT: [[SHUF:%.*]] = shufflevector <4 x i16> [[V:%.*]], <4 x i16> poison, <8 x i32> zeroinitializer
-; CHECK-NEXT: [[TR:%.*]] = trunc <8 x i16> [[SHUF]] to <8 x i8>
+; CHECK-NEXT: [[TMP1:%.*]] = trunc <4 x i16> [[V:%.*]] to <4 x i8>
+; CHECK-NEXT: [[TR:%.*]] = shufflevector <4 x i8> [[TMP1]], <4 x i8> poison, <8 x i32> zeroinitializer
; CHECK-NEXT: ret <8 x i8> [[TR]]
;
%shuf = shufflevector <4 x i16> %v, <4 x i16> %v, <8 x i32> zeroinitializer
|
✅ With the latest revision this PR passed the C/C++ code formatter. |
return new ShuffleVectorInst(NarrowOp, Shuf->getShuffleMask()); | ||
auto *const NewTruncTy = VectorType::get( | ||
Trunc.getType()->getScalarType(), | ||
cast<VectorType>(Shuf->getOperand(0)->getType())->getElementCount()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is Shuf->getOperand(0)->getType()->getWithNewType(Trunc.getType()->getScalarType())
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, done
; CHECK-NEXT: [[SHUF:%.*]] = shufflevector <4 x i16> [[V:%.*]], <4 x i16> poison, <8 x i32> zeroinitializer | ||
; CHECK-NEXT: [[TR:%.*]] = trunc <8 x i16> [[SHUF]] to <8 x i8> | ||
; CHECK-NEXT: [[TMP1:%.*]] = trunc <4 x i16> [[V:%.*]] to <4 x i8> | ||
; CHECK-NEXT: [[TR:%.*]] = shufflevector <4 x i8> [[TMP1]], <4 x i8> poison, <8 x i32> zeroinitializer |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also test shortening splat? I think in that case the profitability is less clear.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I think we want to restrict it to only when Shuf->getOperand(0) is shorter than Shuf?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand why it wouldn't be profitable in that case, could you please elaborate?
Also added test
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We're now performing trunc on a wider type than before, and that can be slower on some targets. E.g. a <8 x i16> trunc may take twice as many uops as a <4 x i16> trunc. There is definitely at least hardware on RISC-V where this is the case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! changed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is getting very close to needing to be cost based, which suggests we should move it the entire fold to VectorCombine - we already have VectorCombine::foldShuffleOfCastops for a shuffle of 2 matching casts, relaxing this to handle a single cast wouldn't be a huge amount of work.
@RKSimon I think the transformation I am adding to here fits in InstCombine because we generally try to move up truncations and then act on smaller types. |
; CHECK-NEXT: ret <8 x i8> [[TR]] | ||
; | ||
%shuf = shufflevector <4 x i16> %v, <4 x i16> %v, <8 x i32> zeroinitializer | ||
%tr = trunc <8 x i16> %shuf to <8 x i8> | ||
ret <8 x i8> %tr | ||
} | ||
|
||
define <4 x i8> @wide_shortening_splat(<8 x i16> %v) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit, add a comment above explaining that this is a negative test?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
// trunc (shuf X, Undef, SplatMask) --> shuf (trunc X), Poison, SplatMask | ||
// trunc (shuf X, Poison, SplatMask) --> shuf (trunc X), Poison, SplatMask | ||
Value *NarrowOp = Builder.CreateTrunc(Shuf->getOperand(0), Trunc.getType()); | ||
return new ShuffleVectorInst(NarrowOp, Shuf->getShuffleMask()); | ||
auto *const NewTruncTy = Shuf->getOperand(0)->getType()->getWithNewType( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Style nit, avoid using auto when it doesn't make the type clearer https://llvm.org/docs/CodingStandards.html#id29
auto *const NewTruncTy = Shuf->getOperand(0)->getType()->getWithNewType( | |
Type *NewTruncTy = Shuf->getOperand(0)->getType()->getWithNewType( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
Value *NarrowOp = | ||
Builder.CreateTrunc(Shuf->getOperand(0), NewTruncTy, Trunc.getName()); | ||
return new ShuffleVectorInst(NarrowOp, Shuf->getShuffleMask(), | ||
Shuf->getName()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think many places in InstCombine preserve the names, should these changes be left out and posted as a separate NFC?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do think it would be nicer if the names where preserved but changed to be consistent with the rest of the pass
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, but agree with @RKSimon that we should look at doing these type of transforms in VectorCombine
// trunc (shuf X, Undef, SplatMask) --> shuf (trunc X), Poison, SplatMask | ||
// trunc (shuf X, Poison, SplatMask) --> shuf (trunc X), Poison, SplatMask | ||
Value *NarrowOp = Builder.CreateTrunc(Shuf->getOperand(0), Trunc.getType()); | ||
Type *const NewTruncTy = Shuf->getOperand(0)->getType()->getWithNewType( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think const pointers are very common in LLVM, it can probably be dropped
shrinkSplatShuffle in InstCombine would only move truncs up through shuffles if those shuffles inputs had the exact same type as their output, this PR weakens this constraint to only requiring that the scalar type of the input and output match.