[llvm] TTI: Fix special casing vectorization costs of saturating add/sub (PR #97463)

David Green via llvm-commits llvm-commits at lists.llvm.org
Fri Jul 26 08:26:37 PDT 2024


davemgreen wrote:

I think I agree for custom, but AFAIU this isn't about that exactly (I should probably have picked a better word). It's about `getTypeLegalizationCost(i64) == {i32, 2}`, and whether it is sensible to use an i32 cost for a i64 operation.

We would hit the same problem on an i64 system if `getTypeLegalizationCost(i128) == {i64, 2}`. The same is true for any other larger-than-legal type and I'm not sure if the best answer is to try and get ever target to add the correct costs for every type that isn't legal. It's OK to override custom costs for custom operations but for these it feels like there should be a more generic way to handle them.

I haven't tried it with this patch (and it probably deserves to be a separate revision), but something like this might work, even if it is still not perfect.
```
    const TargetLoweringBase *TLI = getTLI();
    std::pair<InstructionCost, MVT> LT = getTypeLegalizationCost(RetTy);

    if (LT.second.getScalarSizeInBits() >= RetTy->getScalarSizeInBits() &&
        TLI->isOperationLegalOrPromote(ISD, LT.second)) {
      if (IID == Intrinsic::fabs && LT.second.isFloatingPoint() &&
          TLI->isFAbsFree(LT.second)) {
        return 0;
      }

      // The operation is legal. Assume it costs 1.
      return LT.first;
    }

    // If we can't lower fmuladd into an FMA estimate the cost as a floating
    // point mul followed by an add.
    if (IID == Intrinsic::fmuladd)
      return thisT()->getArithmeticInstrCost(BinaryOperator::FMul, RetTy,
                                             CostKind) +
             thisT()->getArithmeticInstrCost(BinaryOperator::FAdd, RetTy,
                                             CostKind);
    if (IID == Intrinsic::experimental_constrained_fmuladd) {
      IntrinsicCostAttributes FMulAttrs(
        Intrinsic::experimental_constrained_fmul, RetTy, Tys);
      IntrinsicCostAttributes FAddAttrs(
        Intrinsic::experimental_constrained_fadd, RetTy, Tys);
      return thisT()->getIntrinsicInstrCost(FMulAttrs, CostKind) +
             thisT()->getIntrinsicInstrCost(FAddAttrs, CostKind);
    }

    // More of these..

    if (!TLI->isOperationExpand(ISD, LT.second)) {
      // If the operation is custom lowered or .. then assume
      // that the code is twice as expensive.
      return (LT.first * 2);
    }

    // Else, assume that we...
```

https://github.com/llvm/llvm-project/pull/97463


More information about the llvm-commits mailing list