[llvm] [AArch64] Improve pow(x, y) cost model for some constant values of y (PR #185607)
Paul Walker via llvm-commits
llvm-commits at lists.llvm.org
Thu Mar 12 07:26:41 PDT 2026
================
@@ -1109,10 +1109,53 @@ AArch64TTIImpl::getIntrinsicInstrCost(const IntrinsicCostAttributes &ICA,
}
break;
case Intrinsic::pow: {
- EVT VT = getTLI()->getValueType(DL, RetTy);
- RTLIB::Libcall LC = RTLIB::getPOW(VT);
- if (getTLI()->getLibcallImpl(LC) != RTLIB::Unsupported)
- return getCallInstrCost(nullptr, RetTy, ICA.getArgTypes(), CostKind);
+ // For scalar calls we know the target has the libcall, and for fixed-width
+ // vectors we know for the worst case it can be scalarised.
+ bool CanLowerWithLibcalls = true;
+ bool UsesSingleLibcall = true;
+ if (isa<VectorType>(RetTy)) {
+ EVT VT = getTLI()->getValueType(DL, RetTy);
+ RTLIB::Libcall LC = RTLIB::getPOW(VT);
+ bool HasVectorLibcall =
+ getTLI()->getLibcallImpl(LC) != RTLIB::Unsupported;
+ CanLowerWithLibcalls = isa<FixedVectorType>(RetTy) || HasVectorLibcall;
+ UsesSingleLibcall = HasVectorLibcall;
+ }
+ // If we know that the call can be lowered with libcalls then it's safe to
+ // reduce the costs in some cases. This is important for scalable vectors,
+ // since we cannot scalarize the call in the absence of a vector math
+ // library.
+ if (CanLowerWithLibcalls && ICA.getInst() && !ICA.getArgs().empty()) {
+ // If we know the fast math flags and the exponent is a constant then the
+ // cost may be less for some exponents like 0.25 and 0.75.
+ const Constant *ExpC = dyn_cast<Constant>(ICA.getArgs()[1]);
+ if (ExpC && isa<VectorType>(ExpC->getType()))
+ ExpC = ExpC->getSplatValue();
+ if (ExpC) {
+ // The argument must be a FP constant.
+ const ConstantFP *ExpF = cast<ConstantFP>(ExpC);
+ bool Is025 = ExpF->getValueAPF().isExactlyValue(0.25);
+ bool Is075 = ExpF->getValueAPF().isExactlyValue(0.75);
+ FastMathFlags FMF = ICA.getInst()->getFastMathFlags();
+ if ((Is025 || Is075) && FMF.noInfs() && FMF.approxFunc() &&
+ (!Is025 || FMF.noSignedZeros())) {
+ IntrinsicCostAttributes Attrs(Intrinsic::sqrt, RetTy, {RetTy}, FMF);
+ InstructionCost Sqrt = getIntrinsicInstrCost(Attrs, CostKind);
+ if (Is025)
+ return 2 * Sqrt;
+ InstructionCost FMul =
+ getArithmeticInstrCost(Instruction::FMul, RetTy, CostKind);
+ return (Sqrt * 2) + FMul;
+ }
+ // TODO: For 1/3 exponents we expect the cbrt call to be slightly
+ // cheaper than pow.
+ }
+ }
+ if (UsesSingleLibcall)
+ return CostKind == TTI::TCK_CodeSize
+ ? 1
+ : getCallInstrCost(nullptr, RetTy, ICA.getArgTypes(),
+ CostKind);
----------------
paulwalker-arm wrote:
Is this change intentional? Previously we'd return `getCallInstrCost` when a lib call is available and follow through otherwise. Now we always return `getCallInstrCost` for the scalar case?
Shouldn't `getCallInstrCost` be returning the correct value of the `CostKind == TTI::TCK_CodeSize` case?
https://github.com/llvm/llvm-project/pull/185607
More information about the llvm-commits
mailing list