Skip to content

Commit 6dba5f6

Browse files
authored
[TTI] Align optional FMFs in getExtendedReductionCost() to getArithmeticReductionCost(). (#131968)
In the implementation of the getExtendedReductionCost(), it ofter calls getArithmeticReductionCost() with FMFs. But we shouldn't call getArithmeticReductionCost() with FMFs for non-floating-point reductions which will return the wrong cost. This patch makes FMFs in getExtendedReductionCost() optional and align to the getArithmeticReductionCost(). So the TTI will return the correct cost for non-FP extended-reductions query without FMFs. This patch is not quite NFC but it's hard to test from the CostModel side. Split from #113903.
1 parent ee8a759 commit 6dba5f6

10 files changed

+14
-13
lines changed

llvm/include/llvm/Analysis/TargetTransformInfo.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1622,7 +1622,7 @@ class TargetTransformInfo {
16221622
/// ResTy vecreduce.opcode(ext(Ty A)).
16231623
InstructionCost getExtendedReductionCost(
16241624
unsigned Opcode, bool IsUnsigned, Type *ResTy, VectorType *Ty,
1625-
FastMathFlags FMF,
1625+
std::optional<FastMathFlags> FMF,
16261626
TTI::TargetCostKind CostKind = TTI::TCK_RecipThroughput) const;
16271627

16281628
/// \returns The cost of Intrinsic instructions. Analyses the real arguments.
@@ -2267,7 +2267,7 @@ class TargetTransformInfo::Concept {
22672267
TTI::TargetCostKind CostKind) = 0;
22682268
virtual InstructionCost getExtendedReductionCost(
22692269
unsigned Opcode, bool IsUnsigned, Type *ResTy, VectorType *Ty,
2270-
FastMathFlags FMF,
2270+
std::optional<FastMathFlags> FMF,
22712271
TTI::TargetCostKind CostKind = TTI::TCK_RecipThroughput) = 0;
22722272
virtual InstructionCost getMulAccReductionCost(
22732273
bool IsUnsigned, Type *ResTy, VectorType *Ty,
@@ -3023,7 +3023,7 @@ class TargetTransformInfo::Model final : public TargetTransformInfo::Concept {
30233023
}
30243024
InstructionCost
30253025
getExtendedReductionCost(unsigned Opcode, bool IsUnsigned, Type *ResTy,
3026-
VectorType *Ty, FastMathFlags FMF,
3026+
VectorType *Ty, std::optional<FastMathFlags> FMF,
30273027
TTI::TargetCostKind CostKind) override {
30283028
return Impl.getExtendedReductionCost(Opcode, IsUnsigned, ResTy, Ty, FMF,
30293029
CostKind);

llvm/include/llvm/Analysis/TargetTransformInfoImpl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -885,7 +885,7 @@ class TargetTransformInfoImplBase {
885885

886886
InstructionCost getExtendedReductionCost(unsigned Opcode, bool IsUnsigned,
887887
Type *ResTy, VectorType *Ty,
888-
FastMathFlags FMF,
888+
std::optional<FastMathFlags> FMF,
889889
TTI::TargetCostKind CostKind) const {
890890
return 1;
891891
}

llvm/include/llvm/CodeGen/BasicTTIImpl.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3041,7 +3041,7 @@ class BasicTTIImplBase : public TargetTransformInfoImplCRTPBase<T> {
30413041

30423042
InstructionCost getExtendedReductionCost(unsigned Opcode, bool IsUnsigned,
30433043
Type *ResTy, VectorType *Ty,
3044-
FastMathFlags FMF,
3044+
std::optional<FastMathFlags> FMF,
30453045
TTI::TargetCostKind CostKind) {
30463046
if (auto *FTy = dyn_cast<FixedVectorType>(Ty);
30473047
FTy && IsUnsigned && Opcode == Instruction::Add &&
@@ -3050,7 +3050,8 @@ class BasicTTIImplBase : public TargetTransformInfoImplCRTPBase<T> {
30503050
// ZExtOrTrunc(ctpop(bitcast <n x i1> to in)).
30513051
auto *IntTy =
30523052
IntegerType::get(ResTy->getContext(), FTy->getNumElements());
3053-
IntrinsicCostAttributes ICA(Intrinsic::ctpop, IntTy, {IntTy}, FMF);
3053+
IntrinsicCostAttributes ICA(Intrinsic::ctpop, IntTy, {IntTy},
3054+
FMF ? *FMF : FastMathFlags());
30543055
return thisT()->getCastInstrCost(Instruction::BitCast, IntTy, FTy,
30553056
TTI::CastContextHint::None, CostKind) +
30563057
thisT()->getIntrinsicInstrCost(ICA, CostKind);

llvm/lib/Analysis/TargetTransformInfo.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1251,7 +1251,7 @@ InstructionCost TargetTransformInfo::getMinMaxReductionCost(
12511251

12521252
InstructionCost TargetTransformInfo::getExtendedReductionCost(
12531253
unsigned Opcode, bool IsUnsigned, Type *ResTy, VectorType *Ty,
1254-
FastMathFlags FMF, TTI::TargetCostKind CostKind) const {
1254+
std::optional<FastMathFlags> FMF, TTI::TargetCostKind CostKind) const {
12551255
return TTIImpl->getExtendedReductionCost(Opcode, IsUnsigned, ResTy, Ty, FMF,
12561256
CostKind);
12571257
}

llvm/lib/Target/AArch64/AArch64TargetTransformInfo.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4774,7 +4774,7 @@ AArch64TTIImpl::getArithmeticReductionCost(unsigned Opcode, VectorType *ValTy,
47744774

47754775
InstructionCost AArch64TTIImpl::getExtendedReductionCost(
47764776
unsigned Opcode, bool IsUnsigned, Type *ResTy, VectorType *VecTy,
4777-
FastMathFlags FMF, TTI::TargetCostKind CostKind) {
4777+
std::optional<FastMathFlags> FMF, TTI::TargetCostKind CostKind) {
47784778
EVT VecVT = TLI->getValueType(DL, VecTy);
47794779
EVT ResVT = TLI->getValueType(DL, ResTy);
47804780

llvm/lib/Target/AArch64/AArch64TargetTransformInfo.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ class AArch64TTIImpl : public BasicTTIImplBase<AArch64TTIImpl> {
426426

427427
InstructionCost getExtendedReductionCost(unsigned Opcode, bool IsUnsigned,
428428
Type *ResTy, VectorType *ValTy,
429-
FastMathFlags FMF,
429+
std::optional<FastMathFlags> FMF,
430430
TTI::TargetCostKind CostKind);
431431

432432
InstructionCost getMulAccReductionCost(

llvm/lib/Target/ARM/ARMTargetTransformInfo.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1784,7 +1784,7 @@ ARMTTIImpl::getArithmeticReductionCost(unsigned Opcode, VectorType *ValTy,
17841784

17851785
InstructionCost ARMTTIImpl::getExtendedReductionCost(
17861786
unsigned Opcode, bool IsUnsigned, Type *ResTy, VectorType *ValTy,
1787-
FastMathFlags FMF, TTI::TargetCostKind CostKind) {
1787+
std::optional<FastMathFlags> FMF, TTI::TargetCostKind CostKind) {
17881788
EVT ValVT = TLI->getValueType(DL, ValTy);
17891789
EVT ResVT = TLI->getValueType(DL, ResTy);
17901790

llvm/lib/Target/ARM/ARMTargetTransformInfo.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ class ARMTTIImpl : public BasicTTIImplBase<ARMTTIImpl> {
284284
TTI::TargetCostKind CostKind);
285285
InstructionCost getExtendedReductionCost(unsigned Opcode, bool IsUnsigned,
286286
Type *ResTy, VectorType *ValTy,
287-
FastMathFlags FMF,
287+
std::optional<FastMathFlags> FMF,
288288
TTI::TargetCostKind CostKind);
289289
InstructionCost getMulAccReductionCost(bool IsUnsigned, Type *ResTy,
290290
VectorType *ValTy,

llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1889,7 +1889,7 @@ RISCVTTIImpl::getArithmeticReductionCost(unsigned Opcode, VectorType *Ty,
18891889

18901890
InstructionCost RISCVTTIImpl::getExtendedReductionCost(
18911891
unsigned Opcode, bool IsUnsigned, Type *ResTy, VectorType *ValTy,
1892-
FastMathFlags FMF, TTI::TargetCostKind CostKind) {
1892+
std::optional<FastMathFlags> FMF, TTI::TargetCostKind CostKind) {
18931893
if (isa<FixedVectorType>(ValTy) && !ST->useRVVForFixedLengthVectors())
18941894
return BaseT::getExtendedReductionCost(Opcode, IsUnsigned, ResTy, ValTy,
18951895
FMF, CostKind);

llvm/lib/Target/RISCV/RISCVTargetTransformInfo.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ class RISCVTTIImpl : public BasicTTIImplBase<RISCVTTIImpl> {
211211

212212
InstructionCost getExtendedReductionCost(unsigned Opcode, bool IsUnsigned,
213213
Type *ResTy, VectorType *ValTy,
214-
FastMathFlags FMF,
214+
std::optional<FastMathFlags> FMF,
215215
TTI::TargetCostKind CostKind);
216216

217217
InstructionCost

0 commit comments

Comments
 (0)