Skip to content

Commit b61cfc9

Browse files
jacquesguanjacquesguan
authored andcommitted
[RISCV] Add cost modelling for vector widenning reduction.
In RVV, we use vwredsum.vs and vwredsumu.vs for vecreduce.add(ext(Ty A)) if the result type's width is twice of the input vector's SEW-width. In this situation, the cost of extended add reduction should be same as single-width add reduction. So as the vector float widenning reduction. Differential Revision: https://reviews.llvm.org/D129994
1 parent 6f867f9 commit b61cfc9

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,32 @@ RISCVTTIImpl::getArithmeticReductionCost(unsigned Opcode, VectorType *Ty,
377377
return (LT.first - 1) + BaseCost + Log2_32_Ceil(VL);
378378
}
379379

380+
InstructionCost RISCVTTIImpl::getExtendedReductionCost(
381+
unsigned Opcode, bool IsUnsigned, Type *ResTy, VectorType *ValTy,
382+
Optional<FastMathFlags> FMF, TTI::TargetCostKind CostKind) {
383+
if (isa<FixedVectorType>(ValTy) && !ST->useRVVForFixedLengthVectors())
384+
return BaseT::getExtendedReductionCost(Opcode, IsUnsigned, ResTy, ValTy,
385+
FMF, CostKind);
386+
387+
// Skip if scalar size of ResTy is bigger than ELEN.
388+
if (ResTy->getScalarSizeInBits() > ST->getELEN())
389+
return BaseT::getExtendedReductionCost(Opcode, IsUnsigned, ResTy, ValTy,
390+
FMF, CostKind);
391+
392+
if (Opcode != Instruction::Add && Opcode != Instruction::FAdd)
393+
return BaseT::getExtendedReductionCost(Opcode, IsUnsigned, ResTy, ValTy,
394+
FMF, CostKind);
395+
396+
std::pair<InstructionCost, MVT> LT = TLI->getTypeLegalizationCost(DL, ValTy);
397+
398+
if (ResTy->getScalarSizeInBits() != 2 * LT.second.getScalarSizeInBits())
399+
return BaseT::getExtendedReductionCost(Opcode, IsUnsigned, ResTy, ValTy,
400+
FMF, CostKind);
401+
402+
return (LT.first - 1) +
403+
getArithmeticReductionCost(Opcode, ValTy, FMF, CostKind);
404+
}
405+
380406
void RISCVTTIImpl::getUnrollingPreferences(Loop *L, ScalarEvolution &SE,
381407
TTI::UnrollingPreferences &UP,
382408
OptimizationRemarkEmitter *ORE) {

llvm/lib/Target/RISCV/RISCVTargetTransformInfo.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,11 @@ class RISCVTTIImpl : public BasicTTIImplBase<RISCVTTIImpl> {
112112
Optional<FastMathFlags> FMF,
113113
TTI::TargetCostKind CostKind);
114114

115+
InstructionCost getExtendedReductionCost(unsigned Opcode, bool IsUnsigned,
116+
Type *ResTy, VectorType *ValTy,
117+
Optional<FastMathFlags> FMF,
118+
TTI::TargetCostKind CostKind);
119+
115120
bool isElementTypeLegalForScalableVector(Type *Ty) const {
116121
return TLI->isLegalElementTypeForRVV(Ty);
117122
}

0 commit comments

Comments
 (0)