Skip to content
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

Merged
merged 3 commits into from
Apr 12, 2024

Conversation

fzou1
Copy link
Contributor

@fzou1 fzou1 commented Apr 10, 2024

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.

…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.
Copy link

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 @ followed by their GitHub username.

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.

@llvmbot llvmbot added backend:X86 llvm:SelectionDAG SelectionDAGISel as well labels Apr 10, 2024
@llvmbot
Copy link
Collaborator

llvmbot commented Apr 10, 2024

@llvm/pr-subscribers-backend-x86

@llvm/pr-subscribers-llvm-selectiondag

Author: Feng Zou (fzou1)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/88284.diff

2 Files Affected:

  • (modified) llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp (+16-11)
  • (added) llvm/test/CodeGen/X86/buildvec-bitcast.ll (+24)
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
Copy link
Collaborator

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

Copy link
Contributor Author

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()) {
Copy link
Collaborator

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())

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@KanRobert KanRobert self-requested a review April 11, 2024 02:44
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
Copy link
Contributor

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.

Copy link
Contributor Author

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) {
Copy link
Contributor

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.

Copy link
Contributor Author

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()) {
Copy link
Contributor

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.

Copy link
Contributor Author

@fzou1 fzou1 Apr 11, 2024

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().

Copy link
Contributor

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.

Copy link
Contributor

@KanRobert KanRobert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Collaborator

@RKSimon RKSimon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@fzou1
Copy link
Contributor Author

fzou1 commented Apr 12, 2024

Thanks. Is it okay to merge? If so, can someone help merge this?

@KanRobert
Copy link
Contributor

Thanks. Is it okay to merge? If so, can someone help merge this?

Waiting for result of pre-commit tests.

@KanRobert KanRobert merged commit 6b6f272 into llvm:main Apr 12, 2024
4 checks passed
Copy link

@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
by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as
the builds can include changes from many authors. It is not uncommon for your
change to be included in a build that fails due to someone else's changes, or
infrastructure issues.

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.
This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

@fzou1 fzou1 deleted the fzou1-dagcombiner branch April 12, 2024 02:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend:X86 llvm:SelectionDAG SelectionDAGISel as well
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants