[llvm-branch-commits] [llvm] [RISCV][P-ext] Lower RV32 insert_subvector to extract_subector+concat_vectors. (PR #241139)

Craig Topper via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Wed Nov 11 12:57:26 PST 2026


https://github.com/topperc updated https://github.com/llvm/llvm-project/pull/241139

>From 84eef92477874821e1b6827b795e5965e1cf6ae6 Mon Sep 17 00:00:00 2001
From: Craig Topper <craig.topper at sifive.com>
Date: Tue, 10 Nov 2026 21:43:50 -0800
Subject: [PATCH 1/2] Pre-commit tests

---
 .../CodeGen/RISCV/rvp-insert-subvector.ll     | 154 ++++++++++++++++++
 1 file changed, 154 insertions(+)

diff --git a/llvm/test/CodeGen/RISCV/rvp-insert-subvector.ll b/llvm/test/CodeGen/RISCV/rvp-insert-subvector.ll
index 6ba26ca595835d..de3474740a275a 100644
--- a/llvm/test/CodeGen/RISCV/rvp-insert-subvector.ll
+++ b/llvm/test/CodeGen/RISCV/rvp-insert-subvector.ll
@@ -43,3 +43,157 @@ define <2 x i32> @insert_zero_v1i32(<1 x i32> %v) {
   %r = call <2 x i32> @llvm.vector.insert.v2i32.v1i32(<2 x i32> zeroinitializer, <1 x i32> %v, i64 0)
   ret <2 x i32> %r
 }
+
+define <8 x i8> @insert_lo_v4i8(<8 x i8> %v, <4 x i8> %s) {
+; RV32-LABEL: insert_lo_v4i8:
+; RV32:       # %bb.0:
+; RV32-NEXT:    addi sp, sp, -16
+; RV32-NEXT:    .cfi_def_cfa_offset 16
+; RV32-NEXT:    mv a0, a2
+; RV32-NEXT:    addi sp, sp, 16
+; RV32-NEXT:    .cfi_def_cfa_offset 0
+; RV32-NEXT:    ret
+;
+; RV64-LABEL: insert_lo_v4i8:
+; RV64:       # %bb.0:
+; RV64-NEXT:    li a2, 255
+; RV64-NEXT:    mvm a0, a1, a2
+; RV64-NEXT:    lui a3, 16
+; RV64-NEXT:    andi a4, a1, -256
+; RV64-NEXT:    addi a3, a3, -256
+; RV64-NEXT:    mvm a0, a4, a3
+; RV64-NEXT:    lui a3, 1048560
+; RV64-NEXT:    and a3, a1, a3
+; RV64-NEXT:    lui a4, 4080
+; RV64-NEXT:    mvm a0, a3, a4
+; RV64-NEXT:    lui a3, 1044480
+; RV64-NEXT:    and a1, a1, a3
+; RV64-NEXT:    slli a2, a2, 24
+; RV64-NEXT:    mvm a0, a1, a2
+; RV64-NEXT:    ret
+  %r = call <8 x i8> @llvm.vector.insert.v8i8.v4i8(<8 x i8> %v, <4 x i8> %s, i64 0)
+  ret <8 x i8> %r
+}
+
+define <8 x i8> @insert_hi_v4i8(<8 x i8> %v, <4 x i8> %s) {
+; RV32-LABEL: insert_hi_v4i8:
+; RV32:       # %bb.0:
+; RV32-NEXT:    addi sp, sp, -16
+; RV32-NEXT:    .cfi_def_cfa_offset 16
+; RV32-NEXT:    mv a1, a2
+; RV32-NEXT:    addi sp, sp, 16
+; RV32-NEXT:    .cfi_def_cfa_offset 0
+; RV32-NEXT:    ret
+;
+; RV64-LABEL: insert_hi_v4i8:
+; RV64:       # %bb.0:
+; RV64-NEXT:    li a2, 255
+; RV64-NEXT:    slli a1, a1, 32
+; RV64-NEXT:    slli a3, a2, 32
+; RV64-NEXT:    mvm a0, a1, a3
+; RV64-NEXT:    srli a3, a1, 40
+; RV64-NEXT:    slli a3, a3, 40
+; RV64-NEXT:    slli a4, a2, 40
+; RV64-NEXT:    mvm a0, a3, a4
+; RV64-NEXT:    srli a3, a1, 48
+; RV64-NEXT:    slli a3, a3, 48
+; RV64-NEXT:    li a4, -1
+; RV64-NEXT:    slli a2, a2, 48
+; RV64-NEXT:    slli a4, a4, 56
+; RV64-NEXT:    mvm a0, a3, a2
+; RV64-NEXT:    and a1, a1, a4
+; RV64-NEXT:    mvm a0, a1, a4
+; RV64-NEXT:    ret
+  %r = call <8 x i8> @llvm.vector.insert.v8i8.v4i8(<8 x i8> %v, <4 x i8> %s, i64 4)
+  ret <8 x i8> %r
+}
+
+define <4 x i16> @insert_lo_v2i16(<4 x i16> %v, <2 x i16> %s) {
+; RV32-LABEL: insert_lo_v2i16:
+; RV32:       # %bb.0:
+; RV32-NEXT:    addi sp, sp, -16
+; RV32-NEXT:    .cfi_def_cfa_offset 16
+; RV32-NEXT:    mv a0, a2
+; RV32-NEXT:    addi sp, sp, 16
+; RV32-NEXT:    .cfi_def_cfa_offset 0
+; RV32-NEXT:    ret
+;
+; RV64-LABEL: insert_lo_v2i16:
+; RV64:       # %bb.0:
+; RV64-NEXT:    lui a2, 16
+; RV64-NEXT:    addi a2, a2, -1
+; RV64-NEXT:    lui a3, 1048560
+; RV64-NEXT:    mvm a0, a1, a2
+; RV64-NEXT:    lui a2, 65535
+; RV64-NEXT:    and a1, a1, a3
+; RV64-NEXT:    slli a2, a2, 4
+; RV64-NEXT:    mvm a0, a1, a2
+; RV64-NEXT:    ret
+  %r = call <4 x i16> @llvm.vector.insert.v4i16.v2i16(<4 x i16> %v, <2 x i16> %s, i64 0)
+  ret <4 x i16> %r
+}
+
+define <4 x i16> @insert_hi_v2i16(<4 x i16> %v, <2 x i16> %s) {
+; RV32-LABEL: insert_hi_v2i16:
+; RV32:       # %bb.0:
+; RV32-NEXT:    addi sp, sp, -16
+; RV32-NEXT:    .cfi_def_cfa_offset 16
+; RV32-NEXT:    mv a1, a2
+; RV32-NEXT:    addi sp, sp, 16
+; RV32-NEXT:    .cfi_def_cfa_offset 0
+; RV32-NEXT:    ret
+;
+; RV64-LABEL: insert_hi_v2i16:
+; RV64:       # %bb.0:
+; RV64-NEXT:    lui a2, 65535
+; RV64-NEXT:    slli a1, a1, 32
+; RV64-NEXT:    li a3, -1
+; RV64-NEXT:    slli a2, a2, 20
+; RV64-NEXT:    slli a3, a3, 48
+; RV64-NEXT:    mvm a0, a1, a2
+; RV64-NEXT:    and a1, a1, a3
+; RV64-NEXT:    mvm a0, a1, a3
+; RV64-NEXT:    ret
+  %r = call <4 x i16> @llvm.vector.insert.v4i16.v2i16(<4 x i16> %v, <2 x i16> %s, i64 2)
+  ret <4 x i16> %r
+}
+
+define <8 x i8> @insert_hi_undef_v4i8(<4 x i8> %s) {
+; RV32-LABEL: insert_hi_undef_v4i8:
+; RV32:       # %bb.0:
+; RV32-NEXT:    addi sp, sp, -16
+; RV32-NEXT:    .cfi_def_cfa_offset 16
+; RV32-NEXT:    lw a1, 8(sp)
+; RV32-NEXT:    mv a2, a0
+; RV32-NEXT:    mv a0, a1
+; RV32-NEXT:    mv a1, a2
+; RV32-NEXT:    addi sp, sp, 16
+; RV32-NEXT:    .cfi_def_cfa_offset 0
+; RV32-NEXT:    ret
+;
+; RV64-LABEL: insert_hi_undef_v4i8:
+; RV64:       # %bb.0:
+; RV64-NEXT:    ppairoe.w a0, a0, a0
+; RV64-NEXT:    ret
+  %r = call <8 x i8> @llvm.vector.insert.v8i8.v4i8(<8 x i8> poison, <4 x i8> %s, i64 4)
+  ret <8 x i8> %r
+}
+
+define <4 x i16> @insert_hi_zero_v2i16(<2 x i16> %s) {
+; RV32-LABEL: insert_hi_zero_v2i16:
+; RV32:       # %bb.0:
+; RV32-NEXT:    addi sp, sp, -16
+; RV32-NEXT:    .cfi_def_cfa_offset 16
+; RV32-NEXT:    mv a1, a0
+; RV32-NEXT:    li a0, 0
+; RV32-NEXT:    addi sp, sp, 16
+; RV32-NEXT:    .cfi_def_cfa_offset 0
+; RV32-NEXT:    ret
+;
+; RV64-LABEL: insert_hi_zero_v2i16:
+; RV64:       # %bb.0:
+; RV64-NEXT:    pack a0, zero, a0
+; RV64-NEXT:    ret
+  %r = call <4 x i16> @llvm.vector.insert.v4i16.v2i16(<4 x i16> zeroinitializer, <2 x i16> %s, i64 2)
+  ret <4 x i16> %r
+}

>From 3e8c2bb32f0daa3ca388cd0c914b9f686b2cf49e Mon Sep 17 00:00:00 2001
From: Craig Topper <craig.topper at sifive.com>
Date: Tue, 10 Nov 2026 21:46:42 -0800
Subject: [PATCH 2/2] [RISCV][P-ext] Lower RV32 insert_subvector to
 extract_subector+concat_vectors.

Assisted-by: Claude
---
 llvm/lib/Target/RISCV/RISCVISelLowering.cpp   | 24 +++++++--------
 .../CodeGen/RISCV/rvp-insert-subvector.ll     | 29 +------------------
 2 files changed, 12 insertions(+), 41 deletions(-)

diff --git a/llvm/lib/Target/RISCV/RISCVISelLowering.cpp b/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
index b79f6594baaed3..01d9b55ea105e4 100644
--- a/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
+++ b/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
@@ -14762,20 +14762,18 @@ SDValue RISCVTargetLowering::lowerINSERT_SUBVECTOR(SDValue Op,
   unsigned OrigIdx = Op.getConstantOperandVal(2);
   const RISCVRegisterInfo *TRI = Subtarget.getRegisterInfo();
 
-  bool IsPExtInsert =
-      Subtarget.hasStdExtP() &&
-      (!Subtarget.is64Bit() && (VecVT == MVT::v4i16 || VecVT == MVT::v8i8));
-
-  // Fold insert of a 32-bit packed type into a zero-filled 64-bit packed vector
-  // at index 0 (a zero-extend) to avoid scalarizing it into a byte-wise repack.
-  if (IsPExtInsert) {
-    if ((VecVT != MVT::v4i16 && VecVT != MVT::v8i8) ||
-        SubVecVT.getSizeInBits() != 32 || OrigIdx != 0 ||
-        !ISD::isConstantSplatVectorAllZeros(Vec.getNode()))
+  // Lower a 32-bit insert into a 64-bit packed vector on RV32 to an
+  // extract_subvector of the other half and a concat_vectors.
+  if (Subtarget.hasStdExtP() && !Subtarget.is64Bit() &&
+      (VecVT == MVT::v4i16 || VecVT == MVT::v8i8)) {
+    unsigned HalfElts = VecVT.getVectorNumElements() / 2;
+    if (SubVecVT.getSizeInBits() != 32 || (OrigIdx != 0 && OrigIdx != HalfElts))
       return SDValue();
-
-    SDValue Zero = DAG.getConstant(0, DL, SubVecVT);
-    return DAG.getNode(ISD::CONCAT_VECTORS, DL, VecVT, SubVec, Zero);
+    SDValue Other =
+        DAG.getExtractSubvector(DL, SubVecVT, Vec, OrigIdx == 0 ? HalfElts : 0);
+    return OrigIdx == 0
+               ? DAG.getNode(ISD::CONCAT_VECTORS, DL, VecVT, SubVec, Other)
+               : DAG.getNode(ISD::CONCAT_VECTORS, DL, VecVT, Other, SubVec);
   }
 
   if (OrigIdx == 0 && Vec.isUndef())
diff --git a/llvm/test/CodeGen/RISCV/rvp-insert-subvector.ll b/llvm/test/CodeGen/RISCV/rvp-insert-subvector.ll
index de3474740a275a..88368d8496ce3b 100644
--- a/llvm/test/CodeGen/RISCV/rvp-insert-subvector.ll
+++ b/llvm/test/CodeGen/RISCV/rvp-insert-subvector.ll
@@ -47,11 +47,7 @@ define <2 x i32> @insert_zero_v1i32(<1 x i32> %v) {
 define <8 x i8> @insert_lo_v4i8(<8 x i8> %v, <4 x i8> %s) {
 ; RV32-LABEL: insert_lo_v4i8:
 ; RV32:       # %bb.0:
-; RV32-NEXT:    addi sp, sp, -16
-; RV32-NEXT:    .cfi_def_cfa_offset 16
 ; RV32-NEXT:    mv a0, a2
-; RV32-NEXT:    addi sp, sp, 16
-; RV32-NEXT:    .cfi_def_cfa_offset 0
 ; RV32-NEXT:    ret
 ;
 ; RV64-LABEL: insert_lo_v4i8:
@@ -78,11 +74,7 @@ define <8 x i8> @insert_lo_v4i8(<8 x i8> %v, <4 x i8> %s) {
 define <8 x i8> @insert_hi_v4i8(<8 x i8> %v, <4 x i8> %s) {
 ; RV32-LABEL: insert_hi_v4i8:
 ; RV32:       # %bb.0:
-; RV32-NEXT:    addi sp, sp, -16
-; RV32-NEXT:    .cfi_def_cfa_offset 16
 ; RV32-NEXT:    mv a1, a2
-; RV32-NEXT:    addi sp, sp, 16
-; RV32-NEXT:    .cfi_def_cfa_offset 0
 ; RV32-NEXT:    ret
 ;
 ; RV64-LABEL: insert_hi_v4i8:
@@ -111,11 +103,7 @@ define <8 x i8> @insert_hi_v4i8(<8 x i8> %v, <4 x i8> %s) {
 define <4 x i16> @insert_lo_v2i16(<4 x i16> %v, <2 x i16> %s) {
 ; RV32-LABEL: insert_lo_v2i16:
 ; RV32:       # %bb.0:
-; RV32-NEXT:    addi sp, sp, -16
-; RV32-NEXT:    .cfi_def_cfa_offset 16
 ; RV32-NEXT:    mv a0, a2
-; RV32-NEXT:    addi sp, sp, 16
-; RV32-NEXT:    .cfi_def_cfa_offset 0
 ; RV32-NEXT:    ret
 ;
 ; RV64-LABEL: insert_lo_v2i16:
@@ -136,11 +124,7 @@ define <4 x i16> @insert_lo_v2i16(<4 x i16> %v, <2 x i16> %s) {
 define <4 x i16> @insert_hi_v2i16(<4 x i16> %v, <2 x i16> %s) {
 ; RV32-LABEL: insert_hi_v2i16:
 ; RV32:       # %bb.0:
-; RV32-NEXT:    addi sp, sp, -16
-; RV32-NEXT:    .cfi_def_cfa_offset 16
 ; RV32-NEXT:    mv a1, a2
-; RV32-NEXT:    addi sp, sp, 16
-; RV32-NEXT:    .cfi_def_cfa_offset 0
 ; RV32-NEXT:    ret
 ;
 ; RV64-LABEL: insert_hi_v2i16:
@@ -161,14 +145,7 @@ define <4 x i16> @insert_hi_v2i16(<4 x i16> %v, <2 x i16> %s) {
 define <8 x i8> @insert_hi_undef_v4i8(<4 x i8> %s) {
 ; RV32-LABEL: insert_hi_undef_v4i8:
 ; RV32:       # %bb.0:
-; RV32-NEXT:    addi sp, sp, -16
-; RV32-NEXT:    .cfi_def_cfa_offset 16
-; RV32-NEXT:    lw a1, 8(sp)
-; RV32-NEXT:    mv a2, a0
-; RV32-NEXT:    mv a0, a1
-; RV32-NEXT:    mv a1, a2
-; RV32-NEXT:    addi sp, sp, 16
-; RV32-NEXT:    .cfi_def_cfa_offset 0
+; RV32-NEXT:    mv a1, a0
 ; RV32-NEXT:    ret
 ;
 ; RV64-LABEL: insert_hi_undef_v4i8:
@@ -182,12 +159,8 @@ define <8 x i8> @insert_hi_undef_v4i8(<4 x i8> %s) {
 define <4 x i16> @insert_hi_zero_v2i16(<2 x i16> %s) {
 ; RV32-LABEL: insert_hi_zero_v2i16:
 ; RV32:       # %bb.0:
-; RV32-NEXT:    addi sp, sp, -16
-; RV32-NEXT:    .cfi_def_cfa_offset 16
 ; RV32-NEXT:    mv a1, a0
 ; RV32-NEXT:    li a0, 0
-; RV32-NEXT:    addi sp, sp, 16
-; RV32-NEXT:    .cfi_def_cfa_offset 0
 ; RV32-NEXT:    ret
 ;
 ; RV64-LABEL: insert_hi_zero_v2i16:



More information about the llvm-branch-commits mailing list