[llvm-branch-commits] [llvm] [RISCV][P-ext] Fix crash on v2i32 extract_vector_elt with a variable index on RV32. (PR #241039)
Craig Topper via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Wed Nov 11 21:14:49 PST 2026
https://github.com/topperc updated https://github.com/llvm/llvm-project/pull/241039
>From 10d414dd6612b942f7db2e1b92f56196e51fc9a1 Mon Sep 17 00:00:00 2001
From: Craig Topper <craig.topper at sifive.com>
Date: Tue, 10 Nov 2026 14:57:12 -0800
Subject: [PATCH] [RISCV][P-ext] Fix crash on v2i32 extract_vector_elt with a
variable index on RV32.
Assisted-by: Claude
---
llvm/lib/Target/RISCV/RISCVISelLowering.cpp | 9 ++--
llvm/test/CodeGen/RISCV/rvp-simd-64.ll | 60 ++++++++++++++-------
2 files changed, 48 insertions(+), 21 deletions(-)
diff --git a/llvm/lib/Target/RISCV/RISCVISelLowering.cpp b/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
index 70ef715f9e601..9d6a2efff2f46 100644
--- a/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
+++ b/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
@@ -618,10 +618,8 @@ RISCVTargetLowering::RISCVTargetLowering(const TargetMachine &TM,
setOperationAction(ISD::SPLAT_VECTOR, P64VecVTs, Legal);
setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Legal);
setOperationAction(ISD::BUILD_VECTOR, {MVT::v4i16, MVT::v8i8}, Custom);
- setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i32, Legal);
setOperationAction({ISD::EXTRACT_VECTOR_ELT, ISD::INSERT_VECTOR_ELT},
- {MVT::v4i16, MVT::v8i8}, Custom);
- setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i32, Custom);
+ P64VecVTs, Custom);
setOperationAction(ISD::CONCAT_VECTORS, {MVT::v4i16, MVT::v8i8}, Legal);
setOperationAction(ISD::EXTRACT_SUBVECTOR, {MVT::v2i16, MVT::v4i8},
Legal);
@@ -11980,6 +11978,11 @@ SDValue RISCVTargetLowering::lowerEXTRACT_VECTOR_ELT(SDValue Op,
VecVT != MVT::v4i8 && VecVT != MVT::v2i32)
return SDValue();
+ // v2i32 on RV32 lives in a GPR pair; constant index extracts are selected
+ // with EXTRACT_SUBREG.
+ if (!Subtarget.is64Bit() && VecVT == MVT::v2i32)
+ return isa<ConstantSDNode>(Idx) ? Op : SDValue();
+
if (!Subtarget.is64Bit() && (VecVT == MVT::v4i16 || VecVT == MVT::v8i8)) {
auto *IdxC = dyn_cast<ConstantSDNode>(Idx);
if (!IdxC)
diff --git a/llvm/test/CodeGen/RISCV/rvp-simd-64.ll b/llvm/test/CodeGen/RISCV/rvp-simd-64.ll
index 071bb15dbd570..7848ead1f8fcf 100644
--- a/llvm/test/CodeGen/RISCV/rvp-simd-64.ll
+++ b/llvm/test/CodeGen/RISCV/rvp-simd-64.ll
@@ -1192,6 +1192,30 @@ define i32 @test_extract_vector_32_elem1(<2 x i32> %a) {
ret i32 %extracted
}
+define i32 @test_extract_vector_32_variable(<2 x i32> %a, i32 %idx) {
+; RV32-LABEL: test_extract_vector_32_variable:
+; RV32: # %bb.0:
+; RV32-NEXT: addi sp, sp, -16
+; RV32-NEXT: .cfi_def_cfa_offset 16
+; RV32-NEXT: andi a2, a2, 1
+; RV32-NEXT: addi a3, sp, 8
+; RV32-NEXT: sh2add a2, a2, a3
+; RV32-NEXT: sw a0, 8(sp)
+; RV32-NEXT: sw a1, 12(sp)
+; RV32-NEXT: lw a0, 0(a2)
+; RV32-NEXT: addi sp, sp, 16
+; RV32-NEXT: .cfi_def_cfa_offset 0
+; RV32-NEXT: ret
+;
+; RV64-LABEL: test_extract_vector_32_variable:
+; RV64: # %bb.0:
+; RV64-NEXT: slli.uw a1, a1, 5
+; RV64-NEXT: srl a0, a0, a1
+; RV64-NEXT: ret
+ %res = extractelement <2 x i32> %a, i32 %idx
+ ret i32 %res
+}
+
define <4 x i16> @test_insert_vector_16(<4 x i16> %a, i16 %val) {
; RV32-LABEL: test_insert_vector_16:
; RV32: # %bb.0:
@@ -4971,12 +4995,12 @@ define <4 x i16> @test_select_v4i16(i1 %cond, <4 x i16> %a, <4 x i16> %b) {
; RV32-LABEL: test_select_v4i16:
; RV32: # %bb.0:
; RV32-NEXT: andi a5, a0, 1
-; RV32-NEXT: bnez a5, .LBB267_2
+; RV32-NEXT: bnez a5, .LBB268_2
; RV32-NEXT: # %bb.1:
; RV32-NEXT: mv a0, a3
; RV32-NEXT: mv a1, a4
; RV32-NEXT: ret
-; RV32-NEXT: .LBB267_2:
+; RV32-NEXT: .LBB268_2:
; RV32-NEXT: mv a0, a1
; RV32-NEXT: mv a1, a2
; RV32-NEXT: ret
@@ -4985,10 +5009,10 @@ define <4 x i16> @test_select_v4i16(i1 %cond, <4 x i16> %a, <4 x i16> %b) {
; RV64: # %bb.0:
; RV64-NEXT: andi a3, a0, 1
; RV64-NEXT: mv a0, a1
-; RV64-NEXT: bnez a3, .LBB267_2
+; RV64-NEXT: bnez a3, .LBB268_2
; RV64-NEXT: # %bb.1:
; RV64-NEXT: mv a0, a2
-; RV64-NEXT: .LBB267_2:
+; RV64-NEXT: .LBB268_2:
; RV64-NEXT: ret
%res = select i1 %cond, <4 x i16> %a, <4 x i16> %b
ret <4 x i16> %res
@@ -4998,12 +5022,12 @@ define <8 x i8> @test_select_v8i8(i1 %cond, <8 x i8> %a, <8 x i8> %b) {
; RV32-LABEL: test_select_v8i8:
; RV32: # %bb.0:
; RV32-NEXT: andi a5, a0, 1
-; RV32-NEXT: bnez a5, .LBB268_2
+; RV32-NEXT: bnez a5, .LBB269_2
; RV32-NEXT: # %bb.1:
; RV32-NEXT: mv a0, a3
; RV32-NEXT: mv a1, a4
; RV32-NEXT: ret
-; RV32-NEXT: .LBB268_2:
+; RV32-NEXT: .LBB269_2:
; RV32-NEXT: mv a0, a1
; RV32-NEXT: mv a1, a2
; RV32-NEXT: ret
@@ -5012,10 +5036,10 @@ define <8 x i8> @test_select_v8i8(i1 %cond, <8 x i8> %a, <8 x i8> %b) {
; RV64: # %bb.0:
; RV64-NEXT: andi a3, a0, 1
; RV64-NEXT: mv a0, a1
-; RV64-NEXT: bnez a3, .LBB268_2
+; RV64-NEXT: bnez a3, .LBB269_2
; RV64-NEXT: # %bb.1:
; RV64-NEXT: mv a0, a2
-; RV64-NEXT: .LBB268_2:
+; RV64-NEXT: .LBB269_2:
; RV64-NEXT: ret
%res = select i1 %cond, <8 x i8> %a, <8 x i8> %b
ret <8 x i8> %res
@@ -5025,12 +5049,12 @@ define <2 x i32> @test_select_v2i32(i1 %cond, <2 x i32> %a, <2 x i32> %b) {
; RV32-LABEL: test_select_v2i32:
; RV32: # %bb.0:
; RV32-NEXT: andi a5, a0, 1
-; RV32-NEXT: bnez a5, .LBB269_2
+; RV32-NEXT: bnez a5, .LBB270_2
; RV32-NEXT: # %bb.1:
; RV32-NEXT: mv a0, a3
; RV32-NEXT: mv a1, a4
; RV32-NEXT: ret
-; RV32-NEXT: .LBB269_2:
+; RV32-NEXT: .LBB270_2:
; RV32-NEXT: mv a0, a1
; RV32-NEXT: mv a1, a2
; RV32-NEXT: ret
@@ -5039,10 +5063,10 @@ define <2 x i32> @test_select_v2i32(i1 %cond, <2 x i32> %a, <2 x i32> %b) {
; RV64: # %bb.0:
; RV64-NEXT: andi a3, a0, 1
; RV64-NEXT: mv a0, a1
-; RV64-NEXT: bnez a3, .LBB269_2
+; RV64-NEXT: bnez a3, .LBB270_2
; RV64-NEXT: # %bb.1:
; RV64-NEXT: mv a0, a2
-; RV64-NEXT: .LBB269_2:
+; RV64-NEXT: .LBB270_2:
; RV64-NEXT: ret
%res = select i1 %cond, <2 x i32> %a, <2 x i32> %b
ret <2 x i32> %res
@@ -5090,16 +5114,16 @@ define <2 x i32> @test_vselect_v2i32(<2 x i32> %a, <2 x i32> %b, <2 x i32> %c) {
; RV32: # %bb.0:
; RV32-NEXT: pmslt.dw a6, a2, a0
; RV32-NEXT: mv a0, a4
-; RV32-NEXT: beqz a7, .LBB272_3
+; RV32-NEXT: beqz a7, .LBB273_3
; RV32-NEXT: # %bb.1:
-; RV32-NEXT: beqz a6, .LBB272_4
-; RV32-NEXT: .LBB272_2:
+; RV32-NEXT: beqz a6, .LBB273_4
+; RV32-NEXT: .LBB273_2:
; RV32-NEXT: mv a1, a5
; RV32-NEXT: ret
-; RV32-NEXT: .LBB272_3:
+; RV32-NEXT: .LBB273_3:
; RV32-NEXT: mv a5, a3
-; RV32-NEXT: bnez a6, .LBB272_2
-; RV32-NEXT: .LBB272_4:
+; RV32-NEXT: bnez a6, .LBB273_2
+; RV32-NEXT: .LBB273_4:
; RV32-NEXT: mv a0, a2
; RV32-NEXT: mv a1, a5
; RV32-NEXT: ret
More information about the llvm-branch-commits
mailing list