From 0c1326748f6625b90b38ed800a1b90e77657a762 Mon Sep 17 00:00:00 2001 From: Philip Reames Date: Thu, 23 Jun 2022 09:11:24 -0700 Subject: [PATCH] [BasicTTI] Avoid crash when costing scalable select expansion If the target has chosen to expand a scalable vector type, BasicTTI tries to scalarize and we'd crash. As a minimum, we should return an invalid cost instead. The added test provide coverage for the moment, but given they show a number of gaps in RISCV costing, they're likely not to cover this code path long term. --- llvm/include/llvm/CodeGen/BasicTTIImpl.h | 3 + .../Analysis/CostModel/RISCV/rvv-select.ll | 147 ++++++++++++++++++ 2 files changed, 150 insertions(+) create mode 100644 llvm/test/Analysis/CostModel/RISCV/rvv-select.ll diff --git a/llvm/include/llvm/CodeGen/BasicTTIImpl.h b/llvm/include/llvm/CodeGen/BasicTTIImpl.h index 6fb3e1a0e5c5..afe9554defaa 100644 --- a/llvm/include/llvm/CodeGen/BasicTTIImpl.h +++ b/llvm/include/llvm/CodeGen/BasicTTIImpl.h @@ -1128,6 +1128,9 @@ public: // TODO: If one of the types get legalized by splitting, handle this // similarly to what getCastInstrCost() does. if (auto *ValVTy = dyn_cast(ValTy)) { + if (isa(ValTy)) + return InstructionCost::getInvalid(); + unsigned Num = cast(ValVTy)->getNumElements(); if (CondTy) CondTy = CondTy->getScalarType(); diff --git a/llvm/test/Analysis/CostModel/RISCV/rvv-select.ll b/llvm/test/Analysis/CostModel/RISCV/rvv-select.ll new file mode 100644 index 000000000000..b22e107ae420 --- /dev/null +++ b/llvm/test/Analysis/CostModel/RISCV/rvv-select.ll @@ -0,0 +1,147 @@ +; NOTE: Assertions have been autogenerated by utils/update_analyze_test_checks.py +; RUN: opt -passes='print' 2>&1 -disable-output -mtriple=riscv64 -mattr=+v,+f,+d,+zfh,+experimental-zvfh -riscv-v-vector-bits-min=128 -riscv-v-fixed-length-vector-lmul-max=1 < %s | FileCheck %s +; Check that we don't crash querying costs when vectors are not enabled. +; RUN: opt -passes='print' 2>&1 -disable-output -mtriple=riscv64 + +define void @select() { +; CHECK-LABEL: 'select' +; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %1 = select i1 undef, i1 undef, i1 undef +; CHECK-NEXT: Cost Model: Found an estimated cost of 2 for instruction: %2 = select <1 x i1> undef, <1 x i1> undef, <1 x i1> undef +; CHECK-NEXT: Cost Model: Found an estimated cost of 4 for instruction: %3 = select <2 x i1> undef, <2 x i1> undef, <2 x i1> undef +; CHECK-NEXT: Cost Model: Found an estimated cost of 8 for instruction: %4 = select <4 x i1> undef, <4 x i1> undef, <4 x i1> undef +; CHECK-NEXT: Cost Model: Found an estimated cost of 16 for instruction: %5 = select <8 x i1> undef, <8 x i1> undef, <8 x i1> undef +; CHECK-NEXT: Cost Model: Found an estimated cost of 32 for instruction: %6 = select <16 x i1> undef, <16 x i1> undef, <16 x i1> undef +; CHECK-NEXT: Cost Model: Found an estimated cost of 64 for instruction: %7 = select <32 x i1> undef, <32 x i1> undef, <32 x i1> undef +; CHECK-NEXT: Cost Model: Invalid cost for instruction: %8 = select undef, undef, undef +; CHECK-NEXT: Cost Model: Invalid cost for instruction: %9 = select undef, undef, undef +; CHECK-NEXT: Cost Model: Invalid cost for instruction: %10 = select undef, undef, undef +; CHECK-NEXT: Cost Model: Invalid cost for instruction: %11 = select undef, undef, undef +; CHECK-NEXT: Cost Model: Invalid cost for instruction: %12 = select undef, undef, undef +; CHECK-NEXT: Cost Model: Invalid cost for instruction: %13 = select undef, undef, undef +; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %14 = select i1 undef, i8 undef, i8 undef +; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %15 = select <1 x i1> undef, <1 x i8> undef, <1 x i8> undef +; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %16 = select <2 x i1> undef, <2 x i8> undef, <2 x i8> undef +; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %17 = select <4 x i1> undef, <4 x i8> undef, <4 x i8> undef +; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %18 = select <8 x i1> undef, <8 x i8> undef, <8 x i8> undef +; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %19 = select <16 x i1> undef, <16 x i8> undef, <16 x i8> undef +; CHECK-NEXT: Cost Model: Found an estimated cost of 2 for instruction: %20 = select <32 x i1> undef, <32 x i8> undef, <32 x i8> undef +; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %21 = select undef, undef, undef +; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %22 = select undef, undef, undef +; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %23 = select undef, undef, undef +; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %24 = select undef, undef, undef +; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %25 = select undef, undef, undef +; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %26 = select undef, undef, undef +; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %27 = select i1 undef, i16 undef, i16 undef +; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %28 = select <1 x i1> undef, <1 x i16> undef, <1 x i16> undef +; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %29 = select <2 x i1> undef, <2 x i16> undef, <2 x i16> undef +; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %30 = select <4 x i1> undef, <4 x i16> undef, <4 x i16> undef +; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %31 = select <8 x i1> undef, <8 x i16> undef, <8 x i16> undef +; CHECK-NEXT: Cost Model: Found an estimated cost of 2 for instruction: %32 = select <16 x i1> undef, <16 x i16> undef, <16 x i16> undef +; CHECK-NEXT: Cost Model: Found an estimated cost of 4 for instruction: %33 = select <32 x i1> undef, <32 x i16> undef, <32 x i16> undef +; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %34 = select undef, undef, undef +; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %35 = select undef, undef, undef +; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %36 = select undef, undef, undef +; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %37 = select undef, undef, undef +; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %38 = select undef, undef, undef +; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %39 = select undef, undef, undef +; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %40 = select i1 undef, i32 undef, i32 undef +; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %41 = select <1 x i1> undef, <1 x i32> undef, <1 x i32> undef +; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %42 = select <2 x i1> undef, <2 x i32> undef, <2 x i32> undef +; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %43 = select <4 x i1> undef, <4 x i32> undef, <4 x i32> undef +; CHECK-NEXT: Cost Model: Found an estimated cost of 2 for instruction: %44 = select <8 x i1> undef, <8 x i32> undef, <8 x i32> undef +; CHECK-NEXT: Cost Model: Found an estimated cost of 4 for instruction: %45 = select <16 x i1> undef, <16 x i32> undef, <16 x i32> undef +; CHECK-NEXT: Cost Model: Found an estimated cost of 8 for instruction: %46 = select <32 x i1> undef, <32 x i32> undef, <32 x i32> undef +; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %47 = select undef, undef, undef +; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %48 = select undef, undef, undef +; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %49 = select undef, undef, undef +; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %50 = select undef, undef, undef +; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %51 = select undef, undef, undef +; CHECK-NEXT: Cost Model: Found an estimated cost of 2 for instruction: %52 = select undef, undef, undef +; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %53 = select i1 undef, i64 undef, i64 undef +; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %54 = select <1 x i1> undef, <1 x i64> undef, <1 x i64> undef +; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %55 = select <2 x i1> undef, <2 x i64> undef, <2 x i64> undef +; CHECK-NEXT: Cost Model: Found an estimated cost of 2 for instruction: %56 = select <4 x i1> undef, <4 x i64> undef, <4 x i64> undef +; CHECK-NEXT: Cost Model: Found an estimated cost of 4 for instruction: %57 = select <8 x i1> undef, <8 x i64> undef, <8 x i64> undef +; CHECK-NEXT: Cost Model: Found an estimated cost of 8 for instruction: %58 = select <16 x i1> undef, <16 x i64> undef, <16 x i64> undef +; CHECK-NEXT: Cost Model: Found an estimated cost of 16 for instruction: %59 = select <32 x i1> undef, <32 x i64> undef, <32 x i64> undef +; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %60 = select undef, undef, undef +; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %61 = select undef, undef, undef +; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %62 = select undef, undef, undef +; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %63 = select undef, undef, undef +; CHECK-NEXT: Cost Model: Found an estimated cost of 2 for instruction: %64 = select undef, undef, undef +; CHECK-NEXT: Cost Model: Found an estimated cost of 4 for instruction: %65 = select undef, undef, undef +; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: ret void +; + select i1 undef, i1 undef, i1 undef + select <1 x i1> undef, <1 x i1> undef, <1 x i1> undef + select <2 x i1> undef, <2 x i1> undef, <2 x i1> undef + select <4 x i1> undef, <4 x i1> undef, <4 x i1> undef + select <8 x i1> undef, <8 x i1> undef, <8 x i1> undef + select <16 x i1> undef, <16 x i1> undef, <16 x i1> undef + select <32 x i1> undef, <32 x i1> undef, <32 x i1> undef + select undef, undef, undef + select undef, undef, undef + select undef, undef, undef + select undef, undef, undef + select undef, undef, undef + select undef, undef, undef + + select i1 undef, i8 undef, i8 undef + select <1 x i1> undef, <1 x i8> undef, <1 x i8> undef + select <2 x i1> undef, <2 x i8> undef, <2 x i8> undef + select <4 x i1> undef, <4 x i8> undef, <4 x i8> undef + select <8 x i1> undef, <8 x i8> undef, <8 x i8> undef + select <16 x i1> undef, <16 x i8> undef, <16 x i8> undef + select <32 x i1> undef, <32 x i8> undef, <32 x i8> undef + select undef, undef, undef + select undef, undef, undef + select undef, undef, undef + select undef, undef, undef + select undef, undef, undef + select undef, undef, undef + + select i1 undef, i16 undef, i16 undef + select <1 x i1> undef, <1 x i16> undef, <1 x i16> undef + select <2 x i1> undef, <2 x i16> undef, <2 x i16> undef + select <4 x i1> undef, <4 x i16> undef, <4 x i16> undef + select <8 x i1> undef, <8 x i16> undef, <8 x i16> undef + select <16 x i1> undef, <16 x i16> undef, <16 x i16> undef + select <32 x i1> undef, <32 x i16> undef, <32 x i16> undef + select undef, undef, undef + select undef, undef, undef + select undef, undef, undef + select undef, undef, undef + select undef, undef, undef + select undef, undef, undef + + select i1 undef, i32 undef, i32 undef + select <1 x i1> undef, <1 x i32> undef, <1 x i32> undef + select <2 x i1> undef, <2 x i32> undef, <2 x i32> undef + select <4 x i1> undef, <4 x i32> undef, <4 x i32> undef + select <8 x i1> undef, <8 x i32> undef, <8 x i32> undef + select <16 x i1> undef, <16 x i32> undef, <16 x i32> undef + select <32 x i1> undef, <32 x i32> undef, <32 x i32> undef + select undef, undef, undef + select undef, undef, undef + select undef, undef, undef + select undef, undef, undef + select undef, undef, undef + select undef, undef, undef + + + select i1 undef, i64 undef, i64 undef + select <1 x i1> undef, <1 x i64> undef, <1 x i64> undef + select <2 x i1> undef, <2 x i64> undef, <2 x i64> undef + select <4 x i1> undef, <4 x i64> undef, <4 x i64> undef + select <8 x i1> undef, <8 x i64> undef, <8 x i64> undef + select <16 x i1> undef, <16 x i64> undef, <16 x i64> undef + select <32 x i1> undef, <32 x i64> undef, <32 x i64> undef + select undef, undef, undef + select undef, undef, undef + select undef, undef, undef + select undef, undef, undef + select undef, undef, undef + select undef, undef, undef + + ret void +}