-
Notifications
You must be signed in to change notification settings - Fork 15.4k
[RISCV][CostModel] Add cost for fabs/fsqrt of type bf16/f16 #118608
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 6 commits
48dfe67
e682e00
86bd2f7
0b5f44e
1fe4655
b7c046f
3bb8859
1a8c67e
5d2432f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,6 +13,7 @@ | |
| #include "llvm/CodeGen/BasicTTIImpl.h" | ||
| #include "llvm/CodeGen/CostTable.h" | ||
| #include "llvm/CodeGen/TargetLowering.h" | ||
| #include "llvm/CodeGen/ValueTypes.h" | ||
| #include "llvm/IR/Instructions.h" | ||
| #include "llvm/IR/PatternMatch.h" | ||
| #include <cmath> | ||
|
|
@@ -1035,21 +1036,61 @@ RISCVTTIImpl::getIntrinsicInstrCost(const IntrinsicCostAttributes &ICA, | |
| } | ||
| break; | ||
| } | ||
| case Intrinsic::fabs: | ||
| case Intrinsic::fabs: { | ||
| auto LT = getTypeLegalizationCost(RetTy); | ||
| if (ST->hasVInstructions() && LT.second.isVector()) { | ||
| // lui a0, 8 | ||
| // addi a0, a0, -1 | ||
| // vsetvli a1, zero, e16, m1, ta, ma | ||
| // vand.vx v8, v8, a0 | ||
| // f16 with zvfhmin and bf16 with zvfhbmin | ||
| if (LT.second.getVectorElementType() == MVT::bf16 || | ||
| (LT.second.getVectorElementType() == MVT::f16 && | ||
| !ST->hasVInstructionsF16())) | ||
| return LT.first * getRISCVInstructionCost(RISCV::VAND_VX, LT.second, | ||
| CostKind) + | ||
| 2; | ||
| else | ||
| return LT.first * | ||
| getRISCVInstructionCost(RISCV::VFSGNJX_VV, LT.second, CostKind); | ||
| } | ||
| break; | ||
| } | ||
| case Intrinsic::sqrt: { | ||
| auto LT = getTypeLegalizationCost(RetTy); | ||
| // TODO: add f16/bf16, bf16 with zvfbfmin && f16 with zvfhmin | ||
| auto NVT = LT.second; | ||
| if (ST->hasVInstructions() && LT.second.isVector()) { | ||
| unsigned Op; | ||
| switch (ICA.getID()) { | ||
| case Intrinsic::fabs: | ||
| Op = RISCV::VFSGNJX_VV; | ||
| break; | ||
| case Intrinsic::sqrt: | ||
| Op = RISCV::VFSQRT_V; | ||
| break; | ||
| SmallVector<unsigned, 3> Opcodes; | ||
| // f16 with zvfhmin and bf16 with zvfbfmin and the type of nxv32[b]f16 | ||
| // will be spilt. | ||
| if (LT.second.getVectorElementType() == MVT::bf16) { | ||
| if (LT.second == MVT::nxv32bf16) { | ||
| Opcodes = {RISCV::VFWCVTBF16_F_F_V, RISCV::VFWCVTBF16_F_F_V, | ||
| RISCV::VFSQRT_V, RISCV::VFSQRT_V, | ||
| RISCV::VFNCVTBF16_F_F_W, RISCV::VFNCVTBF16_F_F_W}; | ||
| NVT = TLI->getTypeToPromoteTo(ISD::FSQRT, | ||
| NVT.getHalfNumVectorElementsVT()); | ||
| } else { | ||
| Opcodes = {RISCV::VFWCVTBF16_F_F_V, RISCV::VFSQRT_V, | ||
| RISCV::VFNCVTBF16_F_F_W}; | ||
| NVT = TLI->getTypeToPromoteTo(ISD::FSQRT, NVT); | ||
| } | ||
| } else if (LT.second.getVectorElementType() == MVT::f16 && | ||
| !ST->hasVInstructionsF16()) { | ||
| if (LT.second == MVT::nxv32f16) { | ||
| Opcodes = {RISCV::VFWCVT_F_F_V, RISCV::VFWCVT_F_F_V, | ||
| RISCV::VFSQRT_V, RISCV::VFSQRT_V, | ||
| RISCV::VFNCVT_F_F_W, RISCV::VFNCVT_F_F_W}; | ||
| NVT = TLI->getTypeToPromoteTo(ISD::FSQRT, | ||
|
||
| NVT.getHalfNumVectorElementsVT()); | ||
| } else { | ||
| Opcodes = {RISCV::VFWCVT_F_F_V, RISCV::VFSQRT_V, RISCV::VFNCVT_F_F_W}; | ||
| NVT = TLI->getTypeToPromoteTo(ISD::FSQRT, NVT); | ||
| } | ||
| } else { | ||
| Opcodes = {RISCV::VFSQRT_V}; | ||
| } | ||
| return LT.first * getRISCVInstructionCost(Op, LT.second, CostKind); | ||
| return LT.first * getRISCVInstructionCost(Opcodes, NVT, CostKind); | ||
| } | ||
| break; | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NVT needs to be changed to v16f32.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you help me see the code check? I feel that there is no problem with the code, but the code check fails.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@LiqinWeng Are you trying to compute the cost for ISAs without f16 support as (vfwcvt, nxvf16) + (vfsqrt, nxvf32) + (vfncvt, nxv*f16)? From the current code, it doesn’t seem like that’s what’s being implemented.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Under what circumstances will it occur: (vfwcvt, nxvf16) + (vfsqrt, nxvf32) + (vfncvt, nxv*f16)???I don't quite understand ‘ISAs without f16’? do you means the mattr only support zvfh???
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a vfwcvt.f.f.v from EEW=16 EMUL=m1 to EEW=32 EMUL=m2; Shouldn't the type of vfwcvt be nxvf32, the dest eew is 32
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@LiqinWeng vfwcvt is a widening arithmetic instruction. Based on the spec description, the destination EEW is
2 * SEW, so the vtype is nxvf16 and the destination is nxvf32.On the other hand, for narrowing arithmetic instruction such as
vfncvt, the destination EEW isSEW, so both the vtype and the destination are nxvf16.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this the case with this instruction(vfwcvtbf16.f.f.v )?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm.. I think vfwcvtbf16.f.f.v is the same with vfwcvt.f.f.v.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed