Skip to content

Commit 86bd2f7

Browse files
committed
Promote the bf16/f16 to fp32 to calculate the cost
1 parent e682e00 commit 86bd2f7

File tree

2 files changed

+19
-15
lines changed

2 files changed

+19
-15
lines changed

llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,18 +1057,22 @@ RISCVTTIImpl::getIntrinsicInstrCost(const IntrinsicCostAttributes &ICA,
10571057
}
10581058
case Intrinsic::sqrt: {
10591059
auto LT = getTypeLegalizationCost(RetTy);
1060+
auto NVT = LT.second;
10601061
if (ST->hasVInstructions() && LT.second.isVector()) {
10611062
SmallVector<unsigned, 3> Opcodes;
10621063
// f16 with zvfhmin and bf16 with zvfbfmin
1063-
if (LT.second.getVectorElementType() == MVT::bf16)
1064+
if (LT.second.getVectorElementType() == MVT::bf16) {
10641065
Opcodes = {RISCV::VFWCVTBF16_F_F_V, RISCV::VFSQRT_V,
10651066
RISCV::VFNCVTBF16_F_F_W};
1066-
else if (LT.second.getVectorElementType() == MVT::f16 &&
1067-
!ST->hasVInstructionsF16())
1067+
NVT = TLI->getTypeToPromoteTo(ISD::FSQRT, NVT);
1068+
} else if (LT.second.getVectorElementType() == MVT::f16 &&
1069+
!ST->hasVInstructionsF16()) {
10681070
Opcodes = {RISCV::VFWCVT_F_F_V, RISCV::VFSQRT_V, RISCV::VFNCVT_F_F_W};
1069-
else
1071+
NVT = TLI->getTypeToPromoteTo(ISD::FSQRT, NVT);
1072+
} else {
10701073
Opcodes = {RISCV::VFSQRT_V};
1071-
return LT.first * getRISCVInstructionCost(Opcodes, LT.second, CostKind);
1074+
}
1075+
return LT.first * getRISCVInstructionCost(Opcodes, NVT, CostKind);
10721076
}
10731077
break;
10741078
}

llvm/test/Analysis/CostModel/RISCV/fp-sqrt-pow.ll

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ define void @sqrt() {
77
; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %1 = call bfloat @llvm.sqrt.bf16(bfloat undef)
88
; CHECK-NEXT: Cost Model: Found an estimated cost of 3 for instruction: %2 = call <2 x bfloat> @llvm.sqrt.v2bf16(<2 x bfloat> undef)
99
; CHECK-NEXT: Cost Model: Found an estimated cost of 3 for instruction: %3 = call <4 x bfloat> @llvm.sqrt.v4bf16(<4 x bfloat> undef)
10-
; CHECK-NEXT: Cost Model: Found an estimated cost of 3 for instruction: %4 = call <8 x bfloat> @llvm.sqrt.v8bf16(<8 x bfloat> undef)
11-
; CHECK-NEXT: Cost Model: Found an estimated cost of 6 for instruction: %5 = call <16 x bfloat> @llvm.sqrt.v16bf16(<16 x bfloat> undef)
10+
; CHECK-NEXT: Cost Model: Found an estimated cost of 6 for instruction: %4 = call <8 x bfloat> @llvm.sqrt.v8bf16(<8 x bfloat> undef)
11+
; CHECK-NEXT: Cost Model: Found an estimated cost of 12 for instruction: %5 = call <16 x bfloat> @llvm.sqrt.v16bf16(<16 x bfloat> undef)
1212
; CHECK-NEXT: Cost Model: Found an estimated cost of 3 for instruction: %6 = call <vscale x 2 x bfloat> @llvm.sqrt.nxv2bf16(<vscale x 2 x bfloat> undef)
13-
; CHECK-NEXT: Cost Model: Found an estimated cost of 3 for instruction: %7 = call <vscale x 4 x bfloat> @llvm.sqrt.nxv4bf16(<vscale x 4 x bfloat> undef)
14-
; CHECK-NEXT: Cost Model: Found an estimated cost of 6 for instruction: %8 = call <vscale x 8 x bfloat> @llvm.sqrt.nxv8bf16(<vscale x 8 x bfloat> undef)
15-
; CHECK-NEXT: Cost Model: Found an estimated cost of 12 for instruction: %9 = call <vscale x 16 x bfloat> @llvm.sqrt.nxv16bf16(<vscale x 16 x bfloat> undef)
13+
; CHECK-NEXT: Cost Model: Found an estimated cost of 6 for instruction: %7 = call <vscale x 4 x bfloat> @llvm.sqrt.nxv4bf16(<vscale x 4 x bfloat> undef)
14+
; CHECK-NEXT: Cost Model: Found an estimated cost of 12 for instruction: %8 = call <vscale x 8 x bfloat> @llvm.sqrt.nxv8bf16(<vscale x 8 x bfloat> undef)
15+
; CHECK-NEXT: Cost Model: Found an estimated cost of 24 for instruction: %9 = call <vscale x 16 x bfloat> @llvm.sqrt.nxv16bf16(<vscale x 16 x bfloat> undef)
1616
; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %10 = call float @llvm.sqrt.f32(float undef)
1717
; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %11 = call <2 x float> @llvm.sqrt.v2f32(<2 x float> undef)
1818
; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %12 = call <4 x float> @llvm.sqrt.v4f32(<4 x float> undef)
@@ -82,12 +82,12 @@ define void @sqrt_f16() {
8282
; ZVFHMIN-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %1 = call half @llvm.sqrt.f16(half undef)
8383
; ZVFHMIN-NEXT: Cost Model: Found an estimated cost of 3 for instruction: %2 = call <2 x half> @llvm.sqrt.v2f16(<2 x half> undef)
8484
; ZVFHMIN-NEXT: Cost Model: Found an estimated cost of 3 for instruction: %3 = call <4 x half> @llvm.sqrt.v4f16(<4 x half> undef)
85-
; ZVFHMIN-NEXT: Cost Model: Found an estimated cost of 3 for instruction: %4 = call <8 x half> @llvm.sqrt.v8f16(<8 x half> undef)
86-
; ZVFHMIN-NEXT: Cost Model: Found an estimated cost of 6 for instruction: %5 = call <16 x half> @llvm.sqrt.v16f16(<16 x half> undef)
85+
; ZVFHMIN-NEXT: Cost Model: Found an estimated cost of 6 for instruction: %4 = call <8 x half> @llvm.sqrt.v8f16(<8 x half> undef)
86+
; ZVFHMIN-NEXT: Cost Model: Found an estimated cost of 12 for instruction: %5 = call <16 x half> @llvm.sqrt.v16f16(<16 x half> undef)
8787
; ZVFHMIN-NEXT: Cost Model: Found an estimated cost of 3 for instruction: %6 = call <vscale x 2 x half> @llvm.sqrt.nxv2f16(<vscale x 2 x half> undef)
88-
; ZVFHMIN-NEXT: Cost Model: Found an estimated cost of 3 for instruction: %7 = call <vscale x 4 x half> @llvm.sqrt.nxv4f16(<vscale x 4 x half> undef)
89-
; ZVFHMIN-NEXT: Cost Model: Found an estimated cost of 6 for instruction: %8 = call <vscale x 8 x half> @llvm.sqrt.nxv8f16(<vscale x 8 x half> undef)
90-
; ZVFHMIN-NEXT: Cost Model: Found an estimated cost of 12 for instruction: %9 = call <vscale x 16 x half> @llvm.sqrt.nxv16f16(<vscale x 16 x half> undef)
88+
; ZVFHMIN-NEXT: Cost Model: Found an estimated cost of 6 for instruction: %7 = call <vscale x 4 x half> @llvm.sqrt.nxv4f16(<vscale x 4 x half> undef)
89+
; ZVFHMIN-NEXT: Cost Model: Found an estimated cost of 12 for instruction: %8 = call <vscale x 8 x half> @llvm.sqrt.nxv8f16(<vscale x 8 x half> undef)
90+
; ZVFHMIN-NEXT: Cost Model: Found an estimated cost of 24 for instruction: %9 = call <vscale x 16 x half> @llvm.sqrt.nxv16f16(<vscale x 16 x half> undef)
9191
; ZVFHMIN-NEXT: Cost Model: Found an estimated cost of 0 for instruction: ret void
9292
;
9393
call half @llvm.sqrt.f16(half undef)

0 commit comments

Comments
 (0)