Skip to content

Commit 25c5bad

Browse files
authored
[RISCV] Check the legality of source vector types in matchSplatAsGather (#133028)
When we're trying to lower `extractelement + splat` with vrgather.vi/.vx, we should also check the legality of source vector type from `extractelement`, as the entire transformation assumes legal types. Fixes #133020
1 parent 1876a89 commit 25c5bad

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

llvm/lib/Target/RISCV/RISCVISelLowering.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3566,7 +3566,8 @@ static SDValue matchSplatAsGather(SDValue SplatVal, MVT VT, const SDLoc &DL,
35663566
// FIXME: Support i1 vectors, maybe by promoting to i8?
35673567
MVT EltTy = VT.getVectorElementType();
35683568
MVT SrcVT = Src.getSimpleValueType();
3569-
if (EltTy == MVT::i1 || EltTy != SrcVT.getVectorElementType())
3569+
if (EltTy == MVT::i1 || EltTy != SrcVT.getVectorElementType() ||
3570+
!DAG.getTargetLoweringInfo().isTypeLegal(SrcVT))
35703571
return SDValue();
35713572
SDValue Idx = SplatVal.getOperand(1);
35723573
// The index must be a legal type.

llvm/test/CodeGen/RISCV/rvv/splat-vectors.ll

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,21 @@ define <8 x float> @splat_idx_nxv4f32_v8f32_constant_7(<vscale x 4 x float> %v)
230230
ret <8 x float> %splat
231231
}
232232

233+
; This test shouldn't crash.
234+
define <vscale x 2 x float> @splat_idx_illegal_type(<3 x float> %v) {
235+
; CHECK-LABEL: splat_idx_illegal_type:
236+
; CHECK: # %bb.0: # %entry
237+
; CHECK-NEXT: vsetvli a0, zero, e32, m1, ta, ma
238+
; CHECK-NEXT: vrgather.vi v9, v8, 0
239+
; CHECK-NEXT: vmv.v.v v8, v9
240+
; CHECK-NEXT: ret
241+
entry:
242+
%x = extractelement <3 x float> %v, i64 0
243+
%ins = insertelement <vscale x 2 x float> poison, float %x, i64 0
244+
%splat = shufflevector <vscale x 2 x float> %ins, <vscale x 2 x float> poison, <vscale x 2 x i32> zeroinitializer
245+
ret <vscale x 2 x float> %splat
246+
}
247+
233248
; Negative test, vscale might be 4
234249
define <8 x float> @splat_idx_nxv4f32_v8f32_constant_8(<vscale x 4 x float> %v) {
235250
; CHECK-LABEL: splat_idx_nxv4f32_v8f32_constant_8:

0 commit comments

Comments
 (0)