Skip to content

[AArch64] Generalize costing for FP16 instructions #150033

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

Merged
merged 2 commits into from
Aug 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 48 additions & 24 deletions llvm/lib/Target/AArch64/AArch64TargetTransformInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3975,6 +3975,27 @@ InstructionCost AArch64TTIImpl::getScalarizationOverhead(
return DemandedElts.popcount() * (Insert + Extract) * VecInstCost;
}

std::optional<InstructionCost> AArch64TTIImpl::getFP16BF16PromoteCost(
Type *Ty, TTI::TargetCostKind CostKind, TTI::OperandValueInfo Op1Info,
TTI::OperandValueInfo Op2Info, bool IncludeTrunc,
std::function<InstructionCost(Type *)> InstCost) const {
if (!Ty->getScalarType()->isHalfTy() && !Ty->getScalarType()->isBFloatTy())
return std::nullopt;
if (Ty->getScalarType()->isHalfTy() && ST->hasFullFP16())
return std::nullopt;

Type *PromotedTy = Ty->getWithNewType(Type::getFloatTy(Ty->getContext()));
InstructionCost Cost = getCastInstrCost(Instruction::FPExt, PromotedTy, Ty,
TTI::CastContextHint::None, CostKind);
if (!Op1Info.isConstant() && !Op2Info.isConstant())
Cost *= 2;
Cost += InstCost(PromotedTy);
if (IncludeTrunc)
Cost += getCastInstrCost(Instruction::FPTrunc, Ty, PromotedTy,
TTI::CastContextHint::None, CostKind);
return Cost;
}

InstructionCost AArch64TTIImpl::getArithmeticInstrCost(
unsigned Opcode, Type *Ty, TTI::TargetCostKind CostKind,
TTI::OperandValueInfo Op1Info, TTI::OperandValueInfo Op2Info,
Expand All @@ -3997,6 +4018,18 @@ InstructionCost AArch64TTIImpl::getArithmeticInstrCost(
std::pair<InstructionCost, MVT> LT = getTypeLegalizationCost(Ty);
int ISD = TLI->InstructionOpcodeToISD(Opcode);

// Increase the cost for half and bfloat types if not architecturally
// supported.
if (ISD == ISD::FADD || ISD == ISD::FSUB || ISD == ISD::FMUL ||
ISD == ISD::FDIV || ISD == ISD::FREM)
if (auto PromotedCost = getFP16BF16PromoteCost(
Ty, CostKind, Op1Info, Op2Info, /*IncludeTrunc=*/true,
[&](Type *PromotedTy) {
return getArithmeticInstrCost(Opcode, PromotedTy, CostKind,
Op1Info, Op2Info);
}))
return *PromotedCost;

switch (ISD) {
default:
return BaseT::getArithmeticInstrCost(Opcode, Ty, CostKind, Op1Info,
Expand Down Expand Up @@ -4265,11 +4298,6 @@ InstructionCost AArch64TTIImpl::getArithmeticInstrCost(
[[fallthrough]];
case ISD::FADD:
case ISD::FSUB:
// Increase the cost for half and bfloat types if not architecturally
// supported.
if ((Ty->getScalarType()->isHalfTy() && !ST->hasFullFP16()) ||
(Ty->getScalarType()->isBFloatTy() && !ST->hasBF16()))
return 2 * LT.first;
if (!Ty->getScalarType()->isFP128Ty())
return LT.first;
[[fallthrough]];
Expand Down Expand Up @@ -4371,25 +4399,21 @@ InstructionCost AArch64TTIImpl::getCmpSelInstrCost(
}

if (Opcode == Instruction::FCmp) {
// Without dedicated instructions we promote f16 + bf16 compares to f32.
if ((!ST->hasFullFP16() && ValTy->getScalarType()->isHalfTy()) ||
ValTy->getScalarType()->isBFloatTy()) {
Type *PromotedTy =
ValTy->getWithNewType(Type::getFloatTy(ValTy->getContext()));
InstructionCost Cost =
getCastInstrCost(Instruction::FPExt, PromotedTy, ValTy,
TTI::CastContextHint::None, CostKind);
if (!Op1Info.isConstant() && !Op2Info.isConstant())
Cost *= 2;
Cost += getCmpSelInstrCost(Opcode, PromotedTy, CondTy, VecPred, CostKind,
Op1Info, Op2Info);
if (ValTy->isVectorTy())
Cost += getCastInstrCost(
Instruction::Trunc, VectorType::getInteger(cast<VectorType>(ValTy)),
VectorType::getInteger(cast<VectorType>(PromotedTy)),
TTI::CastContextHint::None, CostKind);
return Cost;
}
if (auto Cost = getFP16BF16PromoteCost(
ValTy, CostKind, Op1Info, Op2Info, /*IncludeTrunc=*/false,
[&](Type *PromotedTy) {
InstructionCost Cost =
getCmpSelInstrCost(Opcode, PromotedTy, CondTy, VecPred,
CostKind, Op1Info, Op2Info);
if (isa<VectorType>(PromotedTy))
Cost += getCastInstrCost(
Instruction::Trunc,
VectorType::getInteger(cast<VectorType>(ValTy)),
VectorType::getInteger(cast<VectorType>(PromotedTy)),
TTI::CastContextHint::None, CostKind);
return Cost;
}))
return *Cost;

auto LT = getTypeLegalizationCost(ValTy);
// Model unknown fp compares as a libcall.
Expand Down
8 changes: 8 additions & 0 deletions llvm/lib/Target/AArch64/AArch64TargetTransformInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,14 @@ class AArch64TTIImpl final : public BasicTTIImplBase<AArch64TTIImpl> {

bool preferPredicatedReductionSelect() const override { return ST->hasSVE(); }

/// FP16 and BF16 operations are lowered to fptrunc(op(fpext, fpext) if the
/// architecture features are not present.
std::optional<InstructionCost>
getFP16BF16PromoteCost(Type *Ty, TTI::TargetCostKind CostKind,
TTI::OperandValueInfo Op1Info,
TTI::OperandValueInfo Op2Info, bool IncludeTrunc,
std::function<InstructionCost(Type *)> InstCost) const;

InstructionCost
getArithmeticReductionCost(unsigned Opcode, VectorType *Ty,
std::optional<FastMathFlags> FMF,
Expand Down
Loading
Loading