diff --git a/llvm/lib/CodeGen/SelectionDAG/LegalizeVectorTypes.cpp b/llvm/lib/CodeGen/SelectionDAG/LegalizeVectorTypes.cpp --- a/llvm/lib/CodeGen/SelectionDAG/LegalizeVectorTypes.cpp +++ b/llvm/lib/CodeGen/SelectionDAG/LegalizeVectorTypes.cpp @@ -4015,7 +4015,7 @@ EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0)); EVT WidenEltVT = WidenVT.getVectorElementType(); EVT VT = WidenVT; - unsigned NumElts = VT.getVectorNumElements(); + unsigned NumElts = VT.getVectorMinNumElements(); const SDNodeFlags Flags = N->getFlags(); while (!TLI.isTypeLegal(VT) && NumElts != 1) { NumElts = NumElts / 2; @@ -4029,6 +4029,9 @@ return DAG.getNode(N->getOpcode(), dl, WidenVT, InOp1, InOp2, Flags); } + // FIXME: Improve support for scalable vectors. + assert(!VT.isScalableVector() && "Scalable vectors not handled yet."); + // No legal vector version so unroll the vector operation and then widen. if (NumElts == 1) return DAG.UnrollVectorOp(N, WidenVT.getVectorNumElements()); diff --git a/llvm/lib/Target/RISCV/RISCVISelLowering.cpp b/llvm/lib/Target/RISCV/RISCVISelLowering.cpp --- a/llvm/lib/Target/RISCV/RISCVISelLowering.cpp +++ b/llvm/lib/Target/RISCV/RISCVISelLowering.cpp @@ -112,6 +112,12 @@ if (Subtarget.hasVInstructions()) { auto addRegClassForRVV = [this](MVT VT) { + // Disable the smallest fractional LMUL types if ELEN is less than + // RVVBitsPerBlock. + unsigned MinElts = RISCV::RVVBitsPerBlock / Subtarget.getELEN(); + if (VT.getVectorMinNumElements() < MinElts) + return; + unsigned Size = VT.getSizeInBits().getKnownMinValue(); const TargetRegisterClass *RC; if (Size <= RISCV::RVVBitsPerBlock) @@ -472,6 +478,9 @@ } for (MVT VT : BoolVecVTs) { + if (!isTypeLegal(VT)) + continue; + setOperationAction(ISD::SPLAT_VECTOR, VT, Custom); // Mask VTs are custom-expanded into a series of standard nodes @@ -519,8 +528,7 @@ } for (MVT VT : IntVecVTs) { - if (VT.getVectorElementType() == MVT::i64 && - !Subtarget.hasVInstructionsI64()) + if (!isTypeLegal(VT)) continue; setOperationAction(ISD::SPLAT_VECTOR, VT, Legal); @@ -704,21 +712,31 @@ } }; - if (Subtarget.hasVInstructionsF16()) - for (MVT VT : F16VecVTs) + if (Subtarget.hasVInstructionsF16()) { + for (MVT VT : F16VecVTs) { + if (!isTypeLegal(VT)) + continue; SetCommonVFPActions(VT); + } + } - for (MVT VT : F32VecVTs) { - if (Subtarget.hasVInstructionsF32()) + if (Subtarget.hasVInstructionsF32()) { + for (MVT VT : F32VecVTs) { + if (!isTypeLegal(VT)) + continue; SetCommonVFPActions(VT); - SetCommonVFPExtLoadTruncStoreActions(VT, F16VecVTs); + SetCommonVFPExtLoadTruncStoreActions(VT, F16VecVTs); + } } - for (MVT VT : F64VecVTs) { - if (Subtarget.hasVInstructionsF64()) + if (Subtarget.hasVInstructionsF64()) { + for (MVT VT : F64VecVTs) { + if (!isTypeLegal(VT)) + continue; SetCommonVFPActions(VT); - SetCommonVFPExtLoadTruncStoreActions(VT, F16VecVTs); - SetCommonVFPExtLoadTruncStoreActions(VT, F32VecVTs); + SetCommonVFPExtLoadTruncStoreActions(VT, F16VecVTs); + SetCommonVFPExtLoadTruncStoreActions(VT, F32VecVTs); + } } if (Subtarget.useRVVForFixedLengthVectors()) { diff --git a/llvm/test/CodeGen/RISCV/rvv/zve32-types.ll b/llvm/test/CodeGen/RISCV/rvv/zve32-types.ll new file mode 100644 --- /dev/null +++ b/llvm/test/CodeGen/RISCV/rvv/zve32-types.ll @@ -0,0 +1,91 @@ +; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py +; RUN: sed 's/iXLen/i32/g' %s | llc -mtriple=riscv32 -mattr=+zve32f,+f,+zvl64b \ +; RUN: -verify-machineinstrs | FileCheck %s --check-prefixes=CHECK +; RUN: sed 's/iXLen/i64/g' %s | llc -mtriple=riscv64 -mattr=+zve32f,+f,+zvl64b \ +; RUN: -verify-machineinstrs | FileCheck %s --check-prefixes=CHECK + +; Sanity check that type legalization kicks in for vscale x 1 types with Zve32. + +; NOTE: The load and store are widened by using VP_LOAD/STORE. The add/fadd are +; widened by using the next larger LMUL and operating on the whole vector. This +; isn't optimal, but doesn't crash. + +define void @vadd_vv_nxv1i8(* %pa, * %pb) { +; CHECK-LABEL: vadd_vv_nxv1i8: +; CHECK: # %bb.0: +; CHECK-NEXT: csrr a2, vlenb +; CHECK-NEXT: srli a2, a2, 3 +; CHECK-NEXT: vsetvli zero, a2, e8, mf4, ta, mu +; CHECK-NEXT: vle8.v v8, (a0) +; CHECK-NEXT: vle8.v v9, (a1) +; CHECK-NEXT: vsetvli a1, zero, e8, mf4, ta, mu +; CHECK-NEXT: vadd.vv v8, v8, v9 +; CHECK-NEXT: vsetvli zero, a2, e8, mf4, ta, mu +; CHECK-NEXT: vse8.v v8, (a0) +; CHECK-NEXT: ret + %va = load , * %pa + %vb = load , * %pb + %vc = add %va, %vb + store %vc, * %pa + ret void +} + +define void @vadd_vv_nxv1i16(* %pa, * %pb) { +; CHECK-LABEL: vadd_vv_nxv1i16: +; CHECK: # %bb.0: +; CHECK-NEXT: csrr a2, vlenb +; CHECK-NEXT: srli a2, a2, 3 +; CHECK-NEXT: vsetvli zero, a2, e16, mf2, ta, mu +; CHECK-NEXT: vle16.v v8, (a0) +; CHECK-NEXT: vle16.v v9, (a1) +; CHECK-NEXT: vsetvli a1, zero, e16, mf2, ta, mu +; CHECK-NEXT: vadd.vv v8, v8, v9 +; CHECK-NEXT: vsetvli zero, a2, e16, mf2, ta, mu +; CHECK-NEXT: vse16.v v8, (a0) +; CHECK-NEXT: ret + %va = load , * %pa + %vb = load , * %pb + %vc = add %va, %vb + store %vc, * %pa + ret void +} + +define void @vadd_vv_nxv1i32(* %pa, * %pb) { +; CHECK-LABEL: vadd_vv_nxv1i32: +; CHECK: # %bb.0: +; CHECK-NEXT: csrr a2, vlenb +; CHECK-NEXT: srli a2, a2, 3 +; CHECK-NEXT: vsetvli zero, a2, e32, m1, ta, mu +; CHECK-NEXT: vle32.v v8, (a0) +; CHECK-NEXT: vle32.v v9, (a1) +; CHECK-NEXT: vsetvli a1, zero, e32, m1, ta, mu +; CHECK-NEXT: vadd.vv v8, v8, v9 +; CHECK-NEXT: vsetvli zero, a2, e32, m1, ta, mu +; CHECK-NEXT: vse32.v v8, (a0) +; CHECK-NEXT: ret + %va = load , * %pa + %vb = load , * %pb + %vc = add %va, %vb + store %vc, * %pa + ret void +} + +define void @vfadd_vv_nxv1f32(* %pa, * %pb) { +; CHECK-LABEL: vfadd_vv_nxv1f32: +; CHECK: # %bb.0: +; CHECK-NEXT: csrr a2, vlenb +; CHECK-NEXT: srli a2, a2, 3 +; CHECK-NEXT: vsetvli zero, a2, e32, m1, ta, mu +; CHECK-NEXT: vle32.v v8, (a0) +; CHECK-NEXT: vle32.v v9, (a1) +; CHECK-NEXT: vsetvli a1, zero, e32, m1, ta, mu +; CHECK-NEXT: vfadd.vv v8, v8, v9 +; CHECK-NEXT: vsetvli zero, a2, e32, m1, ta, mu +; CHECK-NEXT: vse32.v v8, (a0) +; CHECK-NEXT: ret + %va = load , * %pa + %vb = load , * %pb + %vc = fadd %va, %vb + store %vc, * %pa + ret void +}