[llvm] [DAGCombiner][NVPTX] Avoid forming illegal-typed shuffles after type-legalization (PR #205056)
Srinivasa Ravi via llvm-commits
llvm-commits at lists.llvm.org
Mon Jun 22 03:00:35 PDT 2026
https://github.com/Wolfram70 updated https://github.com/llvm/llvm-project/pull/205056
>From 6a3ff603a5c2478de5e8b675f9283152ec553f88 Mon Sep 17 00:00:00 2001
From: Srinivasa Ravi <srinivasar at nvidia.com>
Date: Mon, 22 Jun 2026 07:56:56 +0000
Subject: [PATCH 1/2] [DAGCombiner][NVPTX] Avoid forming illegal-typed shuffles
after type legalization
Currently, `combineInsertEltToShuffle` could create a shuffle of
an illegal type after type legalization, which when reaches the
operation legalizer, asserts ("Unexpected illegal type!").
https://github.com/llvm/llvm-project/pull/198259 fixed a crash
resuulting from this in NVPTX but resulted in regressions with
some types due to the check blocking pre-legalization folds in
addition to the illegal post-legalization shuffle.
This change removes the TTI override and adds a guard in the
`combineInsertEltToShuffle` pattern to avoid forming illegal-typed
shuffles post type legalization.
---
llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp | 6 ++--
llvm/lib/Target/NVPTX/NVPTXISelLowering.h | 4 ---
.../NVPTX/insert-vector-elt-shuffle-i8.ll | 33 +++++++++++++++++++
3 files changed, 37 insertions(+), 6 deletions(-)
create mode 100644 llvm/test/CodeGen/NVPTX/insert-vector-elt-shuffle-i8.ll
diff --git a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
index 4fdef7d4afb5d..fcec01bdca33a 100644
--- a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
@@ -24638,10 +24638,12 @@ SDValue DAGCombiner::combineInsertEltToShuffle(SDNode *N, unsigned InsIndex) {
Mask[i] = i;
}
- // Bail out if the target can not handle the shuffle we want to create.
+ // Bail out if the target can not handle the shuffle we want to create, or
+ // would create an illegal-typed shuffle after type legalization.
EVT SubVecEltVT = SubVecVT.getVectorElementType();
EVT ShufVT = EVT::getVectorVT(*DAG.getContext(), SubVecEltVT, NumMaskVals);
- if (!TLI.isShuffleMaskLegal(Mask, ShufVT))
+ if (!TLI.isShuffleMaskLegal(Mask, ShufVT) ||
+ (LegalTypes && !TLI.isTypeLegal(ShufVT)))
return SDValue();
// Step 2: Create a wide vector from the inserted source vector by appending
diff --git a/llvm/lib/Target/NVPTX/NVPTXISelLowering.h b/llvm/lib/Target/NVPTX/NVPTXISelLowering.h
index 0e8dd6056af81..f2879c87dafc7 100644
--- a/llvm/lib/Target/NVPTX/NVPTXISelLowering.h
+++ b/llvm/lib/Target/NVPTX/NVPTXISelLowering.h
@@ -107,10 +107,6 @@ class NVPTXTargetLowering : public TargetLowering {
TargetLoweringBase::LegalizeTypeAction
getPreferredVectorAction(MVT VT) const override;
- bool isShuffleMaskLegal(ArrayRef<int>, EVT VT) const override {
- return isTypeLegal(VT);
- }
-
// Get the degree of precision we want from 32-bit floating point division
// operations.
NVPTX::DivPrecisionLevel getDivF32Level(const MachineFunction &MF,
diff --git a/llvm/test/CodeGen/NVPTX/insert-vector-elt-shuffle-i8.ll b/llvm/test/CodeGen/NVPTX/insert-vector-elt-shuffle-i8.ll
new file mode 100644
index 0000000000000..f34623865d3b6
--- /dev/null
+++ b/llvm/test/CodeGen/NVPTX/insert-vector-elt-shuffle-i8.ll
@@ -0,0 +1,33 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
+; RUN: llc < %s -mtriple=nvptx64 -mcpu=sm_90a -mattr=+ptx88 | FileCheck %s
+; RUN: %if ptxas-sm_90a && ptxas-isa-8.8 %{ llc < %s -mtriple=nvptx64 -mcpu=sm_90a -mattr=+ptx88 | %ptxas-verify -arch=sm_90a %}
+
+; Check the i8-pack store lowers to a single packed store, with no prmt reading
+; an uninitialized register (combineInsertEltToShuffle must fire pre-legalization).
+
+define ptx_kernel void @pack_i8(<2 x i8> %a) {
+; CHECK-LABEL: pack_i8(
+; CHECK: {
+; CHECK-NEXT: .reg .b16 %rs<2>;
+; CHECK-NEXT: .reg .b32 %r<4>;
+; CHECK-NEXT: .reg .b64 %rd<2>;
+; CHECK-EMPTY:
+; CHECK-NEXT: // %bb.0:
+; CHECK-NEXT: mov.b32 %r1, 0;
+; CHECK-NEXT: // begin inline asm
+; CHECK-NEXT: cvt.rn.satfinite.e4m3x2.f32 %rs1, %r1, %r1;
+; CHECK-EMPTY:
+; CHECK-NEXT: // end inline asm
+; CHECK-NEXT: cvt.u32.u16 %r2, %rs1;
+; CHECK-NEXT: bfi.b32 %r3, 0, %r2, 0, 8;
+; CHECK-NEXT: mov.b64 %rd1, 0;
+; CHECK-NEXT: st.shared.v4.b32 [%rd1], {%r3, 0, 0, 0};
+; CHECK-NEXT: ret;
+ %asm = tail call <2 x i8> asm "cvt.rn.satfinite.e4m3x2.f32 $0, $2, $1; \0A", "=h,r,r"(i32 0, i32 0)
+ %s = shufflevector <2 x i8> %a, <2 x i8> %asm, <4 x i32> <i32 1, i32 3, i32 poison, i32 poison>
+ %ins = insertelement <4 x i8> %s, i8 0, i64 0
+ %bc = bitcast <4 x i8> %ins to i32
+ %v = insertelement <4 x i32> zeroinitializer, i32 %bc, i64 0
+ store <4 x i32> %v, ptr addrspace(3) null, align 16
+ ret void
+}
>From 735b8f492203fa14f45681336b9a196ed67401fc Mon Sep 17 00:00:00 2001
From: Srinivasa Ravi <srinivasar at nvidia.com>
Date: Mon, 22 Jun 2026 10:00:13 +0000
Subject: [PATCH 2/2] fix test
---
llvm/test/CodeGen/NVPTX/insert-vector-elt-shuffle-i8.ll | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/llvm/test/CodeGen/NVPTX/insert-vector-elt-shuffle-i8.ll b/llvm/test/CodeGen/NVPTX/insert-vector-elt-shuffle-i8.ll
index f34623865d3b6..11ec3ff776726 100644
--- a/llvm/test/CodeGen/NVPTX/insert-vector-elt-shuffle-i8.ll
+++ b/llvm/test/CodeGen/NVPTX/insert-vector-elt-shuffle-i8.ll
@@ -2,8 +2,8 @@
; RUN: llc < %s -mtriple=nvptx64 -mcpu=sm_90a -mattr=+ptx88 | FileCheck %s
; RUN: %if ptxas-sm_90a && ptxas-isa-8.8 %{ llc < %s -mtriple=nvptx64 -mcpu=sm_90a -mattr=+ptx88 | %ptxas-verify -arch=sm_90a %}
-; Check the i8-pack store lowers to a single packed store, with no prmt reading
-; an uninitialized register (combineInsertEltToShuffle must fire pre-legalization).
+; combineInsertEltToShuffle must fire pre-type-legalization, else this i8 pack
+; lowers to prmts that read uninitialized registers.
define ptx_kernel void @pack_i8(<2 x i8> %a) {
; CHECK-LABEL: pack_i8(
@@ -19,7 +19,7 @@ define ptx_kernel void @pack_i8(<2 x i8> %a) {
; CHECK-EMPTY:
; CHECK-NEXT: // end inline asm
; CHECK-NEXT: cvt.u32.u16 %r2, %rs1;
-; CHECK-NEXT: bfi.b32 %r3, 0, %r2, 0, 8;
+; CHECK-NEXT: prmt.b32 %r3, %r2, 0, 0x14U;
; CHECK-NEXT: mov.b64 %rd1, 0;
; CHECK-NEXT: st.shared.v4.b32 [%rd1], {%r3, 0, 0, 0};
; CHECK-NEXT: ret;
More information about the llvm-commits
mailing list