Skip to content

Commit 31fbf2b

Browse files
preamescjdb
authored andcommitted
[RISCV][TTI] Use legalized element types when costing casts (llvm#105723)
This fixes a crash introduced by my ac6e1fd. I had failed to consider the case where a vector is truncated to an illegal element type. The resulting intermediate VT wasn't an MVT and we'd fail an assertion. Surprisingly, SLP does query illegal element types in some cases.
1 parent c263078 commit 31fbf2b

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,13 +1055,12 @@ InstructionCost RISCVTTIImpl::getCastInstrCost(unsigned Opcode, Type *Dst,
10551055
int ISD = TLI->InstructionOpcodeToISD(Opcode);
10561056
assert(ISD && "Invalid opcode");
10571057

1058-
int PowDiff = (int)Log2_32(Dst->getScalarSizeInBits()) -
1059-
(int)Log2_32(Src->getScalarSizeInBits());
1058+
int PowDiff = (int)Log2_32(DstLT.second.getScalarSizeInBits()) -
1059+
(int)Log2_32(SrcLT.second.getScalarSizeInBits());
10601060
switch (ISD) {
10611061
case ISD::SIGN_EXTEND:
10621062
case ISD::ZERO_EXTEND: {
1063-
const unsigned SrcEltSize = Src->getScalarSizeInBits();
1064-
if (SrcEltSize == 1) {
1063+
if (Src->getScalarSizeInBits() == 1) {
10651064
// We do not use vsext/vzext to extend from mask vector.
10661065
// Instead we use the following instructions to extend from mask vector:
10671066
// vmv.v.i v8, 0
@@ -1091,8 +1090,8 @@ InstructionCost RISCVTTIImpl::getCastInstrCost(unsigned Opcode, Type *Dst,
10911090
case ISD::FP_EXTEND:
10921091
case ISD::FP_ROUND: {
10931092
// Counts of narrow/widen instructions.
1094-
unsigned SrcEltSize = Src->getScalarSizeInBits();
1095-
unsigned DstEltSize = Dst->getScalarSizeInBits();
1093+
unsigned SrcEltSize = SrcLT.second.getScalarSizeInBits();
1094+
unsigned DstEltSize = DstLT.second.getScalarSizeInBits();
10961095

10971096
unsigned Op = (ISD == ISD::TRUNCATE) ? RISCV::VNSRL_WI
10981097
: (ISD == ISD::FP_EXTEND) ? RISCV::VFWCVT_F_F_V

llvm/test/Analysis/CostModel/RISCV/cast.ll

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,6 +1016,11 @@ define void @zext() {
10161016

10171017
define void @trunc() {
10181018
; RV32-LABEL: 'trunc'
1019+
; RV32-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %v2i16_v2i2 = trunc <2 x i16> undef to <2 x i2>
1020+
; RV32-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %v2i16_v2i4 = trunc <2 x i16> undef to <2 x i4>
1021+
; RV32-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %v2i16_v2i6 = trunc <2 x i16> undef to <2 x i6>
1022+
; RV32-NEXT: Cost Model: Found an estimated cost of 0 for instruction: %v2i4_v2i2 = trunc <2 x i4> undef to <2 x i2>
1023+
; RV32-NEXT: Cost Model: Found an estimated cost of 0 for instruction: %v2i6_v2i4 = trunc <2 x i6> undef to <2 x i4>
10191024
; RV32-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %v2i16_v2i8 = trunc <2 x i16> undef to <2 x i8>
10201025
; RV32-NEXT: Cost Model: Found an estimated cost of 2 for instruction: %v2i32_v2i8 = trunc <2 x i32> undef to <2 x i8>
10211026
; RV32-NEXT: Cost Model: Found an estimated cost of 3 for instruction: %v2i64_v2i8 = trunc <2 x i64> undef to <2 x i8>
@@ -1169,6 +1174,11 @@ define void @trunc() {
11691174
; RV32-NEXT: Cost Model: Found an estimated cost of 0 for instruction: ret void
11701175
;
11711176
; RV64-LABEL: 'trunc'
1177+
; RV64-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %v2i16_v2i2 = trunc <2 x i16> undef to <2 x i2>
1178+
; RV64-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %v2i16_v2i4 = trunc <2 x i16> undef to <2 x i4>
1179+
; RV64-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %v2i16_v2i6 = trunc <2 x i16> undef to <2 x i6>
1180+
; RV64-NEXT: Cost Model: Found an estimated cost of 0 for instruction: %v2i4_v2i2 = trunc <2 x i4> undef to <2 x i2>
1181+
; RV64-NEXT: Cost Model: Found an estimated cost of 0 for instruction: %v2i6_v2i4 = trunc <2 x i6> undef to <2 x i4>
11721182
; RV64-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %v2i16_v2i8 = trunc <2 x i16> undef to <2 x i8>
11731183
; RV64-NEXT: Cost Model: Found an estimated cost of 2 for instruction: %v2i32_v2i8 = trunc <2 x i32> undef to <2 x i8>
11741184
; RV64-NEXT: Cost Model: Found an estimated cost of 3 for instruction: %v2i64_v2i8 = trunc <2 x i64> undef to <2 x i8>
@@ -1321,6 +1331,13 @@ define void @trunc() {
13211331
; RV64-NEXT: Cost Model: Found an estimated cost of 135 for instruction: %nxv64i64_nxv64i1 = trunc <vscale x 64 x i64> undef to <vscale x 64 x i1>
13221332
; RV64-NEXT: Cost Model: Found an estimated cost of 0 for instruction: ret void
13231333
;
1334+
1335+
%v2i16_v2i2 = trunc <2 x i16> undef to <2 x i2>
1336+
%v2i16_v2i4 = trunc <2 x i16> undef to <2 x i4>
1337+
%v2i16_v2i6 = trunc <2 x i16> undef to <2 x i6>
1338+
%v2i4_v2i2 = trunc <2 x i4> undef to <2 x i2>
1339+
%v2i6_v2i4 = trunc <2 x i6> undef to <2 x i4>
1340+
13241341
%v2i16_v2i8 = trunc <2 x i16> undef to <2 x i8>
13251342
%v2i32_v2i8 = trunc <2 x i32> undef to <2 x i8>
13261343
%v2i64_v2i8 = trunc <2 x i64> undef to <2 x i8>

0 commit comments

Comments
 (0)