Skip to content

Conversation

dlee992
Copy link

@dlee992 dlee992 commented Jul 14, 2025

In visitEXTRACT_VECTOR_ELT, we should use ScalarVT instead of LVT as the result VT for return and method call.

If the DAG has these two nodes (this is generated from llvm-project/llvm/test/CodeGen/Generic/extractelement-shuffle.ll):

        t22: v4i32,ch = load<(load (s128) from %stack.0)> t43, FrameIndex:i32<0>, undef:i32
      t24: i64 = extract_vector_elt t22, Constant:i32<3>

It could apply this combiner rule (i.e., (vextract (v4i32 load $addr), c) -> (i32 load $addr+c*size)) then hit a failure:

Combining: t24: i64 = extract_vector_elt t22, Constant:i32<3>
Creating constant: t47: i32 = Constant<63>
Creating constant: t48: i32 = Constant<4>
Creating constant: t49: i32 = Constant<12>
Creating new node: t50: i32 = add FrameIndex:i32<0>, Constant:i32<12>
Creating new node: t51: i32,ch = load<(load (s32) from %stack.0 + 12)> t43, t50, undef:i32
 ... into: t51: i32,ch = load<(load (s32) from %stack.0 + 12)> t43, t50, undef:i32
llc: ${HOME}/llvm-project/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp:1830: void (anonymous namespace)::DAGCombiner::Run(CombineLevel): Assertion `N->getValueType(0) == RV.getValueType() && N->getNumValues() == 1 && "Type mismatch"' failed.

This PR will fix this issue.

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 the llvm:SelectionDAG SelectionDAGISel as well label Jul 14, 2025
@llvmbot
Copy link
Member

llvmbot commented Jul 14, 2025

@llvm/pr-subscribers-llvm-selectiondag

Author: Da Li (李达) (dlee992)

Changes

In visitEXTRACT_VECTOR_ELT, we should use ScalarVT instead of LVT as the result VT for return and method call.

If the DAG has these two nodes (this is generated from llvm-project/llvm/test/CodeGen/Generic/extractelement-shuffle.ll with a private backend implementation):

        t22: v64i32,ch = load&lt;(load (s2048) from %stack.0, align 64)&gt; t43, FrameIndex:i64&lt;0&gt;, undef:i64
      t24: i64 = extract_vector_elt t22, Constant:i64&lt;3&gt;

It could apply this combiner rule (i.e., (vextract (v4f32 load $addr), c) -&gt; (f32 load $addr+c*size)) then hit a failure:

Combining: t24: i64 = extract_vector_elt t22, Constant:i64&lt;3&gt;
Creating constant: t47: i64 = Constant&lt;63&gt;
Creating constant: t48: i64 = Constant&lt;4&gt;
Creating constant: t49: i64 = Constant&lt;12&gt;
Creating new node: t50: i64 = add FrameIndex:i64&lt;0&gt;, Constant:i64&lt;12&gt;
Creating new node: t51: i32,ch = load&lt;(load (s32) from %stack.0 + 12)&gt; t43, t50, undef:i64
 ... into: t51: i32,ch = load&lt;(load (s32) from %stack.0 + 12)&gt; t43, t50, undef:i64
llc: ${HOME}/llvm-project/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp:1830: void (anonymous namespace)::DAGCombiner::Run(CombineLevel): Assertion `N-&gt;getValueType(0) == RV.getValueType() &amp;&amp; N-&gt;getNumValues() == 1 &amp;&amp; "Type mismatch"' failed.

This PR will fix this issue.


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

1 Files Affected:

  • (modified) llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp (+3-3)
diff --git a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
index 8136f1794775e..f26ee0ace6919 100644
--- a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
@@ -23440,10 +23440,10 @@ SDValue DAGCombiner::visitEXTRACT_VECTOR_ELT(SDNode *N) {
 
   // If Idx was -1 above, Elt is going to be -1, so just return undef.
   if (Elt == -1)
-    return DAG.getUNDEF(LVT);
+    return DAG.getUNDEF(ScalarVT);
 
-  if (SDValue Scalarized =
-          TLI.scalarizeExtractedVectorLoad(LVT, DL, VecVT, Index, LN0, DAG)) {
+  if (SDValue Scalarized = TLI.scalarizeExtractedVectorLoad(ScalarVT, DL, VecVT,
+                                                            Index, LN0, DAG)) {
     ++OpsNarrowed;
     return Scalarized;
   }

@topperc
Copy link
Collaborator

topperc commented Jul 14, 2025

It could apply this combiner rule (i.e., (vextract (v4f32 load $addr), c) -> (f32 load $addr+c*size)) then hit a failure:

Was that supposed to be i32 instead of f32?

@topperc
Copy link
Collaborator

topperc commented Jul 14, 2025

Do you have a test case?

@dlee992
Copy link
Author

dlee992 commented Jul 14, 2025

Do you have a test case?

I don't have one right now. I need to make up one with public targets, but I'm unfamiliar with public targets yet. (or anyone can help? Thanks!)

This PR is extremely similar with this regression fix, so also cc @arsenm .

@arsenm
Copy link
Contributor

arsenm commented Oct 11, 2025

LGTM but should be tested. I think ARM makes the most use of the illegal-scalar-type vectors

@RKSimon RKSimon requested review from RKSimon, arsenm and topperc October 11, 2025 11:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

llvm:SelectionDAG SelectionDAGISel as well

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants