Skip to content

Commit b4a7eed

Browse files
Added 'getVecLibCallCost' in TTI.
Unfortunately TLI (TargetLibraryInfo) is not available in TTI and changing the signature of 'getArithmeticInstrCost' would cause significant changes in loads of places. As a compromise getVecLibCallCost returns a vector library exist for a given target + vector type.
1 parent 29ae086 commit b4a7eed

File tree

3 files changed

+25
-13
lines changed

3 files changed

+25
-13
lines changed

llvm/include/llvm/Analysis/TargetTransformInfo.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1255,6 +1255,13 @@ class TargetTransformInfo {
12551255
ArrayRef<const Value *> Args = ArrayRef<const Value *>(),
12561256
const Instruction *CxtI = nullptr) const;
12571257

1258+
/// Returns the cost of a call when a target has a vector library function for
1259+
/// the given \p VecTy, otherwise an invalid cost.
1260+
InstructionCost getVecLibCallCost(const int OpCode,
1261+
const TargetLibraryInfo *TLI,
1262+
VectorType *VecTy,
1263+
TTI::TargetCostKind CostKind);
1264+
12581265
/// Returns the cost estimation for alternating opcode pattern that can be
12591266
/// lowered to a single instruction on the target. In X86 this is for the
12601267
/// addsub instruction which corrsponds to a Shuffle + Fadd + FSub pattern in

llvm/lib/Analysis/TargetTransformInfo.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "llvm/Analysis/TargetTransformInfo.h"
1010
#include "llvm/Analysis/CFG.h"
1111
#include "llvm/Analysis/LoopIterator.h"
12+
#include "llvm/Analysis/TargetLibraryInfo.h"
1213
#include "llvm/Analysis/TargetTransformInfoImpl.h"
1314
#include "llvm/IR/CFG.h"
1415
#include "llvm/IR/Dominators.h"
@@ -869,6 +870,18 @@ TargetTransformInfo::getOperandInfo(const Value *V) {
869870
return {OpInfo, OpProps};
870871
}
871872

873+
InstructionCost TargetTransformInfo::getVecLibCallCost(
874+
const int OpCode, const TargetLibraryInfo *TLI, VectorType *VecTy,
875+
TTI::TargetCostKind CostKind) {
876+
Type *ScalarTy = VecTy->getScalarType();
877+
LibFunc Func;
878+
if (TLI->getLibFunc(OpCode, ScalarTy, Func) &&
879+
TLI->isFunctionVectorizable(TLI->getName(Func), VecTy->getElementCount()))
880+
return getCallInstrCost(nullptr, VecTy, {ScalarTy, ScalarTy}, CostKind);
881+
882+
return InstructionCost::getInvalid();
883+
}
884+
872885
InstructionCost TargetTransformInfo::getArithmeticInstrCost(
873886
unsigned Opcode, Type *Ty, TTI::TargetCostKind CostKind,
874887
OperandValueInfo Op1Info, OperandValueInfo Op2Info,

llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8362,20 +8362,12 @@ BoUpSLP::getEntryCost(const TreeEntry *E, ArrayRef<Value *> VectorizedVals,
83628362
unsigned OpIdx = isa<UnaryOperator>(VL0) ? 0 : 1;
83638363
TTI::OperandValueInfo Op1Info = getOperandInfo(E->getOperand(0));
83648364
TTI::OperandValueInfo Op2Info = getOperandInfo(E->getOperand(OpIdx));
8365-
auto VecCost = TTI->getArithmeticInstrCost(ShuffleOrOp, VecTy, CostKind,
8366-
Op1Info, Op2Info);
8365+
InstructionCost VecInstrCost = TTI->getArithmeticInstrCost(
8366+
ShuffleOrOp, VecTy, CostKind, Op1Info, Op2Info);
83678367
// Some targets can replace frem with vector library calls.
8368-
if (ShuffleOrOp == Instruction::FRem) {
8369-
LibFunc Func;
8370-
if (TLI->getLibFunc(ShuffleOrOp, ScalarTy, Func) &&
8371-
TLI->isFunctionVectorizable(TLI->getName(Func),
8372-
VecTy->getElementCount())) {
8373-
auto VecCallCost = TTI->getCallInstrCost(
8374-
nullptr, VecTy, {ScalarTy, ScalarTy}, CostKind);
8375-
VecCost = std::min(VecCost, VecCallCost);
8376-
}
8377-
}
8378-
return VecCost + CommonCost;
8368+
InstructionCost VecCallCost =
8369+
TTI->getVecLibCallCost(ShuffleOrOp, TLI, VecTy, CostKind);
8370+
return std::min(VecInstrCost, VecCallCost) + CommonCost;
83798371
};
83808372
return GetCostDiff(GetScalarCost, GetVectorCost);
83818373
}

0 commit comments

Comments
 (0)