-
Notifications
You must be signed in to change notification settings - Fork 10.8k
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
[DAGCombiner] Require same type of splat & element for build_vector #88284
Conversation
…ector Only allow to change build_vector to concat_vector when the splat type and vector element type is same. It's to fix assertion of failing to bitcast types of different sizes.
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be 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 If you have received no comments on your PR for a week, you can request a review 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-backend-x86 @llvm/pr-subscribers-llvm-selectiondag Author: Feng Zou (fzou1) ChangesOnly allow to change build_vector to concat_vector when the splat type and vector element type is same. It's to fix assertion of failing to bitcast types of different sizes. Full diff: https://github.com/llvm/llvm-project/pull/88284.diff 2 Files Affected:
diff --git a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
index 8fe074666a3dc9..ee90ca8eaa7d7f 100644
--- a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
@@ -23429,17 +23429,22 @@ SDValue DAGCombiner::visitBUILD_VECTOR(SDNode *N) {
// TODO: Maybe this is useful for non-splat too?
if (!LegalOperations) {
if (SDValue Splat = cast<BuildVectorSDNode>(N)->getSplatValue()) {
- Splat = peekThroughBitcasts(Splat);
- EVT SrcVT = Splat.getValueType();
- if (SrcVT.isVector()) {
- unsigned NumElts = N->getNumOperands() * SrcVT.getVectorNumElements();
- EVT NewVT = EVT::getVectorVT(*DAG.getContext(),
- SrcVT.getVectorElementType(), NumElts);
- if (!LegalTypes || TLI.isTypeLegal(NewVT)) {
- SmallVector<SDValue, 8> Ops(N->getNumOperands(), Splat);
- SDValue Concat = DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(N),
- NewVT, Ops);
- return DAG.getBitcast(VT, Concat);
+ EVT SplatVT = Splat.getValueType();
+ // Only change build_vector to a concat_vector if the splat value type is
+ // same as the vector element type.
+ if (SplatVT == VT.getVectorElementType()) {
+ Splat = peekThroughBitcasts(Splat);
+ EVT SrcVT = Splat.getValueType();
+ if (SrcVT.isVector()) {
+ unsigned NumElts = N->getNumOperands() * SrcVT.getVectorNumElements();
+ EVT NewVT = EVT::getVectorVT(*DAG.getContext(),
+ SrcVT.getVectorElementType(), NumElts);
+ if (!LegalTypes || TLI.isTypeLegal(NewVT)) {
+ SmallVector<SDValue, 8> Ops(N->getNumOperands(), Splat);
+ SDValue Concat =
+ DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(N), NewVT, Ops);
+ return DAG.getBitcast(VT, Concat);
+ }
}
}
}
diff --git a/llvm/test/CodeGen/X86/buildvec-bitcast.ll b/llvm/test/CodeGen/X86/buildvec-bitcast.ll
new file mode 100644
index 00000000000000..9f7167f2946bf9
--- /dev/null
+++ b/llvm/test/CodeGen/X86/buildvec-bitcast.ll
@@ -0,0 +1,24 @@
+; RUN: llc < %s -mtriple=x86_64 -mattr=avx512bw | FileCheck %s
+
+; Verify that the DAGCombiner doesn't change build_vector to concat_vectors if
+; the vector element type is different than splat type. The example here:
+; v8i1 = build_vector (i8 (bitcast (v8i1 X))), ..., (i8 (bitcast (v8i1 X))))
+
+; CHECK: foo:
+; CHECK: # %bb.0: # %entry
+; CHECK-NEXT: retq
+
+define void @foo(<8 x i1> %mask.i1) {
+entry:
+ %0 = and <8 x i1> %mask.i1, <i1 true, i1 false, i1 false, i1 false, i1 false, i1 false, i1 false, i1 false>
+ %1 = bitcast <8 x i1> %0 to i8
+ %2 = icmp ne i8 %1, 0
+ %insert54 = insertelement <8 x i1> zeroinitializer, i1 %2, i64 0
+ %splat55 = shufflevector <8 x i1> %insert54, <8 x i1> zeroinitializer, <8 x i32> zeroinitializer
+ %3 = and <8 x i1> %0, %splat55
+ br label %end
+
+end: ; preds = %entry
+ %4 = select <8 x i1> %3, <8 x i1> zeroinitializer, <8 x i1> zeroinitializer
+ ret void
+}
|
@@ -0,0 +1,24 @@ | |||
; RUN: llc < %s -mtriple=x86_64 -mattr=avx512bw | FileCheck %s |
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.
Regenerate with utils/update_llc_test_checks.py
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. Thanks.
EVT SplatVT = Splat.getValueType(); | ||
// Only change build_vector to a concat_vector if the splat value type is | ||
// same as the vector element type. | ||
if (SplatVT == VT.getVectorElementType()) { |
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.
SplatVT is only used once - move into if (Splat.getValueType() == VT.getVectorElementType())
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
SDValue Concat = DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(N), | ||
NewVT, Ops); | ||
return DAG.getBitcast(VT, Concat); | ||
// Only change build_vector to a concat_vector if the splat value type is |
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.
Minor suggestion.
SDValue Splat = cast<BuildVectorSDNode>(N)->getSplatValue();
if (Splat && Splat.getValueType() == VT.getVectorElementType())
to eliminate nested braces.
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.
Right. Updated.
; the vector element type is different than splat type. The example here: | ||
; v8i1 = build_vector (i8 (bitcast (v8i1 X))), ..., (i8 (bitcast (v8i1 X)))) | ||
|
||
define void @foo(<8 x i1> %mask.i1) { |
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.
Should we change the function prototype to <8 x i1> @foo (<8 x i1> %mask.i1)
and return %4
at line 23?
In the current variant, the function has no return value and has no side effects. I doubt it may be optimized to
define void @foo(<8 x i1> %mask.i1) {
entry:
return void
}
before ISEL in the future, then it checks nothing for DAG.
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.
Updated.
@@ -23429,17 +23429,21 @@ SDValue DAGCombiner::visitBUILD_VECTOR(SDNode *N) { | |||
// TODO: Maybe this is useful for non-splat too? | |||
if (!LegalOperations) { | |||
if (SDValue Splat = cast<BuildVectorSDNode>(N)->getSplatValue()) { |
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.
The old code seems problematic, if we use cast
, we don't need to use if
.
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.
It's correct, I think. cast
is used for casting N to BuildeVectorSDNode
type and if
is to check the return value of getSplatValue()
.
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.
Sorry, my mistake. The check is for getSplatValue()
not cast
. We can't change it to dyn_cast
.
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
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
Thanks. Is it okay to merge? If so, can someone help merge this? |
Waiting for result of pre-commit tests. |
@fzou1 Congratulations on having your first Pull Request (PR) merged into the LLVM Project! Your changes will be combined with recent changes from other authors, then tested Please check whether problems have been caused by your change specifically, as How to do this, and the rest of the post-merge process, is covered in detail here. If your change does cause a problem, it may be reverted, or you can revert it yourself. If you don't get any reports, no action is required from you. Your changes are working as expected, well done! |
Only allow to change build_vector to concat_vector when the splat type and vector element type is same. It's to fix assertion of failing to bitcast types of different sizes.