Skip to content

Commit 976b006

Browse files
authored
[RISCV] Disable combineToVCPOP for illegal scalable vector types. (#140195)
This transform creates target specific instructions which must have legal types. We were checking this for fixed vectors, but not scalable vectors. This caused a crash with <vscale x 1 x i1> which isn't legal for Zve32x.
1 parent 0de8ff6 commit 976b006

File tree

2 files changed

+47
-14
lines changed

2 files changed

+47
-14
lines changed

llvm/lib/Target/RISCV/RISCVISelLowering.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19031,6 +19031,10 @@ static SDValue combineToVCPOP(SDNode *N, SelectionDAG &DAG,
1903119031
if (!SrcMVT.isVector() || SrcMVT.getVectorElementType() != MVT::i1)
1903219032
return SDValue();
1903319033

19034+
const TargetLowering &TLI = DAG.getTargetLoweringInfo();
19035+
if (!TLI.isTypeLegal(SrcMVT))
19036+
return SDValue();
19037+
1903419038
// Check that destination type is large enough to hold result without
1903519039
// overflow.
1903619040
if (Opc == ISD::VECREDUCE_ADD) {
@@ -19047,9 +19051,6 @@ static SDValue combineToVCPOP(SDNode *N, SelectionDAG &DAG,
1904719051

1904819052
MVT ContainerVT = SrcMVT;
1904919053
if (SrcMVT.isFixedLengthVector()) {
19050-
if (!useRVVForFixedLengthVectorVT(SrcMVT, Subtarget))
19051-
return SDValue();
19052-
1905319054
ContainerVT = getContainerForFixedLengthVector(DAG, SrcMVT, Subtarget);
1905419055
Src = convertToScalableVector(ContainerVT, Src, DAG, Subtarget);
1905519056
}

llvm/test/CodeGen/RISCV/rvv/combine-reduce-add-to-vcpop.ll

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 5
2-
; RUN: llc < %s -mtriple=riscv32 -mattr=+v,+zbb | FileCheck %s --check-prefixes=CHECK,RV32
3-
; RUN: llc < %s -mtriple=riscv64 -mattr=+v,+zbb | FileCheck %s --check-prefixes=CHECK,RV64
2+
; RUN: llc < %s -mtriple=riscv32 -mattr=+v,+zbb | FileCheck %s --check-prefixes=CHECK,V
3+
; RUN: llc < %s -mtriple=riscv64 -mattr=+v,+zbb | FileCheck %s --check-prefixes=CHECK,V
4+
; RUN: llc < %s -mtriple=riscv64 -mattr=+zve32x,+zvl128b,+zbb | FileCheck %s --check-prefixes=CHECK,ZVE
45

56
define i32 @test_v2i1(<2 x i1> %x) {
6-
; CHECK-LABEL: test_v2i1:
7-
; CHECK: # %bb.0:
8-
; CHECK-NEXT: vsetivli zero, 2, e8, mf8, ta, ma
9-
; CHECK-NEXT: vcpop.m a0, v0
10-
; CHECK-NEXT: ret
7+
; V-LABEL: test_v2i1:
8+
; V: # %bb.0:
9+
; V-NEXT: vsetivli zero, 2, e8, mf8, ta, ma
10+
; V-NEXT: vcpop.m a0, v0
11+
; V-NEXT: ret
12+
;
13+
; ZVE-LABEL: test_v2i1:
14+
; ZVE: # %bb.0:
15+
; ZVE-NEXT: vsetivli zero, 2, e8, mf4, ta, ma
16+
; ZVE-NEXT: vcpop.m a0, v0
17+
; ZVE-NEXT: ret
1118
%a = zext <2 x i1> %x to <2 x i32>
1219
%b = call i32 @llvm.vector.reduce.add.v2i32(<2 x i32> %a)
1320
ret i32 %b
@@ -173,6 +180,35 @@ define i32 @test_v256i1(<256 x i1> %x) {
173180
ret i32 %b
174181
}
175182

183+
; FIXME: Optimize this case with Zve32x. We have to use mf4 and set the VL to
184+
; VLEN/64.
185+
define i32 @test_nxv1i1(<vscale x 1 x i1> %x) {
186+
; V-LABEL: test_nxv1i1:
187+
; V: # %bb.0: # %entry
188+
; V-NEXT: vsetvli a0, zero, e8, mf8, ta, ma
189+
; V-NEXT: vcpop.m a0, v0
190+
; V-NEXT: ret
191+
;
192+
; ZVE-LABEL: test_nxv1i1:
193+
; ZVE: # %bb.0: # %entry
194+
; ZVE-NEXT: vsetvli a0, zero, e32, m1, ta, ma
195+
; ZVE-NEXT: vmv.v.i v8, 0
196+
; ZVE-NEXT: csrr a0, vlenb
197+
; ZVE-NEXT: srli a0, a0, 3
198+
; ZVE-NEXT: vsetvli zero, a0, e32, m1, ta, ma
199+
; ZVE-NEXT: vmerge.vim v8, v8, 1, v0
200+
; ZVE-NEXT: vsetivli zero, 1, e32, m1, ta, ma
201+
; ZVE-NEXT: vmv.s.x v9, zero
202+
; ZVE-NEXT: vsetvli zero, a0, e32, m1, ta, ma
203+
; ZVE-NEXT: vredsum.vs v9, v8, v9
204+
; ZVE-NEXT: vmv.x.s a0, v9
205+
; ZVE-NEXT: ret
206+
entry:
207+
%a = zext <vscale x 1 x i1> %x to <vscale x 1 x i32>
208+
%b = call i32 @llvm.vector.reduce.add.nxv1i32(<vscale x 1 x i32> %a)
209+
ret i32 %b
210+
}
211+
176212
define i32 @test_nxv2i1(<vscale x 2 x i1> %x) {
177213
; CHECK-LABEL: test_nxv2i1:
178214
; CHECK: # %bb.0: # %entry
@@ -520,7 +556,3 @@ entry:
520556
%b = call i16 @llvm.vector.reduce.add.nxv64i16(<vscale x 64 x i16> %a)
521557
ret i16 %b
522558
}
523-
524-
;; NOTE: These prefixes are unused and the list is autogenerated. Do not add tests below this line:
525-
; RV32: {{.*}}
526-
; RV64: {{.*}}

0 commit comments

Comments
 (0)