Skip to content

Commit 9a75a80

Browse files
committed
[RISCV] Fix a codegen crash in getSetCCResultType
This patch fixes some crashes coming from `RISCVISelLowering::getSetCCResultType`, which would occasionally return an EVT constructed from an invalid MVT, which has a null Type pointer. The attached test shows this happening currently for some fixed-length vectors, which hit this issue when the V extension was enabled, even though they're not legal types under the V extension. The fix was also pre-emptively extended to scalable vectors which can't be represented as an MVT, even though a test case couldn't be found for them. Reviewed By: craig.topper Differential Revision: https://reviews.llvm.org/D95434
1 parent a8f51ea commit 9a75a80

File tree

2 files changed

+96
-3
lines changed

2 files changed

+96
-3
lines changed

llvm/lib/Target/RISCV/RISCVISelLowering.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -481,12 +481,13 @@ RISCVTargetLowering::RISCVTargetLowering(const TargetMachine &TM,
481481
}
482482
}
483483

484-
EVT RISCVTargetLowering::getSetCCResultType(const DataLayout &DL, LLVMContext &,
484+
EVT RISCVTargetLowering::getSetCCResultType(const DataLayout &DL,
485+
LLVMContext &Context,
485486
EVT VT) const {
486487
if (!VT.isVector())
487488
return getPointerTy(DL);
488-
if (Subtarget.hasStdExtV())
489-
return MVT::getVectorVT(MVT::i1, VT.getVectorElementCount());
489+
if (Subtarget.hasStdExtV() && VT.isScalableVector())
490+
return EVT::getVectorVT(Context, MVT::i1, VT.getVectorElementCount());
490491
return VT.changeVectorElementTypeToInteger();
491492
}
492493

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
2+
; RUN: llc -mtriple=riscv32 -mattr=+experimental-v -verify-machineinstrs < %s \
3+
; RUN: | FileCheck %s --check-prefix=RV32
4+
; RUN: llc -mtriple=riscv64 -mattr=+experimental-v -verify-machineinstrs < %s \
5+
; RUN: | FileCheck %s --check-prefix=RV64
6+
7+
; This test would lead one of the DAGCombiner's visitVSELECT optimizations to
8+
; call getSetCCResultType, from which we'd return an invalid MVT (<3 x i1>)
9+
; upon seeing that the V extension is enabled. The invalid MVT has a null
10+
; Type*, which then segfaulted when accessed (as an EVT).
11+
define void @vec3_setcc_crash(<3 x i8>* %in, <3 x i8>* %out) {
12+
; RV32-LABEL: vec3_setcc_crash:
13+
; RV32: # %bb.0:
14+
; RV32-NEXT: lw a0, 0(a0)
15+
; RV32-NEXT: lui a2, 16
16+
; RV32-NEXT: addi a2, a2, -256
17+
; RV32-NEXT: and a2, a0, a2
18+
; RV32-NEXT: slli a3, a2, 16
19+
; RV32-NEXT: srai a6, a3, 24
20+
; RV32-NEXT: slli a4, a0, 24
21+
; RV32-NEXT: srai a3, a4, 24
22+
; RV32-NEXT: slli a4, a0, 8
23+
; RV32-NEXT: mv a5, a0
24+
; RV32-NEXT: bgtz a3, .LBB0_2
25+
; RV32-NEXT: # %bb.1:
26+
; RV32-NEXT: mv a5, zero
27+
; RV32-NEXT: .LBB0_2:
28+
; RV32-NEXT: srai a4, a4, 24
29+
; RV32-NEXT: andi a5, a5, 255
30+
; RV32-NEXT: bgtz a6, .LBB0_4
31+
; RV32-NEXT: # %bb.3:
32+
; RV32-NEXT: mv a2, zero
33+
; RV32-NEXT: j .LBB0_5
34+
; RV32-NEXT: .LBB0_4:
35+
; RV32-NEXT: srli a2, a2, 8
36+
; RV32-NEXT: .LBB0_5:
37+
; RV32-NEXT: slli a2, a2, 8
38+
; RV32-NEXT: or a2, a5, a2
39+
; RV32-NEXT: bgtz a4, .LBB0_7
40+
; RV32-NEXT: # %bb.6:
41+
; RV32-NEXT: mv a0, zero
42+
; RV32-NEXT: j .LBB0_8
43+
; RV32-NEXT: .LBB0_7:
44+
; RV32-NEXT: srli a0, a0, 16
45+
; RV32-NEXT: .LBB0_8:
46+
; RV32-NEXT: sb a0, 2(a1)
47+
; RV32-NEXT: sh a2, 0(a1)
48+
; RV32-NEXT: ret
49+
;
50+
; RV64-LABEL: vec3_setcc_crash:
51+
; RV64: # %bb.0:
52+
; RV64-NEXT: lwu a0, 0(a0)
53+
; RV64-NEXT: lui a2, 16
54+
; RV64-NEXT: addiw a2, a2, -256
55+
; RV64-NEXT: and a2, a0, a2
56+
; RV64-NEXT: slli a3, a2, 48
57+
; RV64-NEXT: srai a6, a3, 56
58+
; RV64-NEXT: slli a4, a0, 56
59+
; RV64-NEXT: srai a3, a4, 56
60+
; RV64-NEXT: slli a4, a0, 40
61+
; RV64-NEXT: mv a5, a0
62+
; RV64-NEXT: bgtz a3, .LBB0_2
63+
; RV64-NEXT: # %bb.1:
64+
; RV64-NEXT: mv a5, zero
65+
; RV64-NEXT: .LBB0_2:
66+
; RV64-NEXT: srai a4, a4, 56
67+
; RV64-NEXT: andi a5, a5, 255
68+
; RV64-NEXT: bgtz a6, .LBB0_4
69+
; RV64-NEXT: # %bb.3:
70+
; RV64-NEXT: mv a2, zero
71+
; RV64-NEXT: j .LBB0_5
72+
; RV64-NEXT: .LBB0_4:
73+
; RV64-NEXT: srli a2, a2, 8
74+
; RV64-NEXT: .LBB0_5:
75+
; RV64-NEXT: slli a2, a2, 8
76+
; RV64-NEXT: or a2, a5, a2
77+
; RV64-NEXT: bgtz a4, .LBB0_7
78+
; RV64-NEXT: # %bb.6:
79+
; RV64-NEXT: mv a0, zero
80+
; RV64-NEXT: j .LBB0_8
81+
; RV64-NEXT: .LBB0_7:
82+
; RV64-NEXT: srli a0, a0, 16
83+
; RV64-NEXT: .LBB0_8:
84+
; RV64-NEXT: sb a0, 2(a1)
85+
; RV64-NEXT: sh a2, 0(a1)
86+
; RV64-NEXT: ret
87+
%a = load <3 x i8>, <3 x i8>* %in
88+
%cmp = icmp sgt <3 x i8> %a, zeroinitializer
89+
%c = select <3 x i1> %cmp, <3 x i8> %a, <3 x i8> zeroinitializer
90+
store <3 x i8> %c, <3 x i8>* %out
91+
ret void
92+
}

0 commit comments

Comments
 (0)