Skip to content

Commit d7a43a0

Browse files
authored
[RISCV][TTI] Scale the cost of trunc/fptrunc/fpext with LMUL (#87101)
Use the destination data type to measure the LMUL size for latency/throughput cost
1 parent 30fd099 commit d7a43a0

File tree

5 files changed

+298
-279
lines changed

5 files changed

+298
-279
lines changed

llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -927,6 +927,7 @@ InstructionCost RISCVTTIImpl::getCastInstrCost(unsigned Opcode, Type *Dst,
927927
if (!IsTypeLegal)
928928
return BaseT::getCastInstrCost(Opcode, Dst, Src, CCH, CostKind, I);
929929

930+
std::pair<InstructionCost, MVT> SrcLT = getTypeLegalizationCost(Src);
930931
std::pair<InstructionCost, MVT> DstLT = getTypeLegalizationCost(Dst);
931932

932933
int ISD = TLI->InstructionOpcodeToISD(Opcode);
@@ -961,13 +962,31 @@ InstructionCost RISCVTTIImpl::getCastInstrCost(unsigned Opcode, Type *Dst,
961962
// Instead we use the following instructions to truncate to mask vector:
962963
// vand.vi v8, v8, 1
963964
// vmsne.vi v0, v8, 0
964-
return 2;
965+
return getRISCVInstructionCost({RISCV::VAND_VI, RISCV::VMSNE_VI},
966+
SrcLT.second, CostKind);
965967
}
966968
[[fallthrough]];
967969
case ISD::FP_EXTEND:
968-
case ISD::FP_ROUND:
970+
case ISD::FP_ROUND: {
969971
// Counts of narrow/widen instructions.
970-
return std::abs(PowDiff);
972+
unsigned SrcEltSize = Src->getScalarSizeInBits();
973+
unsigned DstEltSize = Dst->getScalarSizeInBits();
974+
975+
unsigned Op = (ISD == ISD::TRUNCATE) ? RISCV::VNSRL_WI
976+
: (ISD == ISD::FP_EXTEND) ? RISCV::VFWCVT_F_F_V
977+
: RISCV::VFNCVT_F_F_W;
978+
InstructionCost Cost = 0;
979+
for (; SrcEltSize != DstEltSize;) {
980+
MVT ElementMVT = (ISD == ISD::TRUNCATE)
981+
? MVT::getIntegerVT(DstEltSize)
982+
: MVT::getFloatingPointVT(DstEltSize);
983+
MVT DstMVT = DstLT.second.changeVectorElementType(ElementMVT);
984+
DstEltSize =
985+
(DstEltSize > SrcEltSize) ? DstEltSize >> 1 : DstEltSize << 1;
986+
Cost += getRISCVInstructionCost(Op, DstMVT, CostKind);
987+
}
988+
return Cost;
989+
}
971990
case ISD::FP_TO_SINT:
972991
case ISD::FP_TO_UINT:
973992
case ISD::SINT_TO_FP:

0 commit comments

Comments
 (0)