Skip to content

Commit 41958f7

Browse files
committed
[Costmodel] Add "type-based-intrinsic-cost" cli option
This patch adds a command line flag to be able to test the type based cost-model analysis for Intrinsics. Differential Revision: https://reviews.llvm.org/D129109
1 parent 600172a commit 41958f7

File tree

4 files changed

+333
-5
lines changed

4 files changed

+333
-5
lines changed

llvm/include/llvm/Analysis/TargetTransformInfo.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,8 @@ class IntrinsicCostAttributes {
130130
public:
131131
IntrinsicCostAttributes(
132132
Intrinsic::ID Id, const CallBase &CI,
133-
InstructionCost ScalarCost = InstructionCost::getInvalid());
133+
InstructionCost ScalarCost = InstructionCost::getInvalid(),
134+
bool TypeBasedOnly = false);
134135

135136
IntrinsicCostAttributes(
136137
Intrinsic::ID Id, Type *RTy, ArrayRef<Type *> Tys,

llvm/lib/Analysis/CostModel.cpp

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "llvm/Pass.h"
2626
#include "llvm/Support/CommandLine.h"
2727
#include "llvm/Support/raw_ostream.h"
28+
#include "llvm/IR/IntrinsicInst.h"
2829
using namespace llvm;
2930

3031
static cl::opt<TargetTransformInfo::TargetCostKind> CostKind(
@@ -39,6 +40,9 @@ static cl::opt<TargetTransformInfo::TargetCostKind> CostKind(
3940
clEnumValN(TargetTransformInfo::TCK_SizeAndLatency,
4041
"size-latency", "Code size and latency")));
4142

43+
static cl::opt<bool> TypeBasedIntrinsicCost("type-based-intrinsic-cost",
44+
cl::desc("Calculate intrinsics cost based only on argument types"),
45+
cl::init(false));
4246

4347
#define CM_NAME "cost-model"
4448
#define DEBUG_TYPE CM_NAME
@@ -103,7 +107,16 @@ void CostModelAnalysis::print(raw_ostream &OS, const Module*) const {
103107

104108
for (BasicBlock &B : *F) {
105109
for (Instruction &Inst : B) {
106-
InstructionCost Cost = TTI->getInstructionCost(&Inst, CostKind);
110+
InstructionCost Cost;
111+
if (TypeBasedIntrinsicCost && isa<IntrinsicInst>(&Inst)) {
112+
auto *II = dyn_cast<IntrinsicInst>(&Inst);
113+
IntrinsicCostAttributes ICA(II->getIntrinsicID(), *II,
114+
InstructionCost::getInvalid(), true);
115+
Cost = TTI->getIntrinsicInstrCost(ICA, CostKind);
116+
}
117+
else {
118+
Cost = TTI->getInstructionCost(&Inst, CostKind);
119+
}
107120
if (auto CostVal = Cost.getValue())
108121
OS << "Cost Model: Found an estimated cost of " << *CostVal;
109122
else
@@ -122,7 +135,16 @@ PreservedAnalyses CostModelPrinterPass::run(Function &F,
122135
for (Instruction &Inst : B) {
123136
// TODO: Use a pass parameter instead of cl::opt CostKind to determine
124137
// which cost kind to print.
125-
InstructionCost Cost = TTI.getInstructionCost(&Inst, CostKind);
138+
InstructionCost Cost;
139+
if (TypeBasedIntrinsicCost && isa<IntrinsicInst>(&Inst)) {
140+
auto *II = dyn_cast<IntrinsicInst>(&Inst);
141+
IntrinsicCostAttributes ICA(II->getIntrinsicID(), *II,
142+
InstructionCost::getInvalid(), true);
143+
Cost = TTI.getIntrinsicInstrCost(ICA, CostKind);
144+
}
145+
else {
146+
Cost = TTI.getInstructionCost(&Inst, CostKind);
147+
}
126148
if (auto CostVal = Cost.getValue())
127149
OS << "Cost Model: Found an estimated cost of " << *CostVal;
128150
else

llvm/lib/Analysis/TargetTransformInfo.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,16 @@ bool HardwareLoopInfo::canAnalyze(LoopInfo &LI) {
5858
}
5959

6060
IntrinsicCostAttributes::IntrinsicCostAttributes(
61-
Intrinsic::ID Id, const CallBase &CI, InstructionCost ScalarizationCost)
61+
Intrinsic::ID Id, const CallBase &CI, InstructionCost ScalarizationCost,
62+
bool TypeBasedOnly)
6263
: II(dyn_cast<IntrinsicInst>(&CI)), RetTy(CI.getType()), IID(Id),
6364
ScalarizationCost(ScalarizationCost) {
6465

6566
if (const auto *FPMO = dyn_cast<FPMathOperator>(&CI))
6667
FMF = FPMO->getFastMathFlags();
6768

68-
Arguments.insert(Arguments.begin(), CI.arg_begin(), CI.arg_end());
69+
if (!TypeBasedOnly)
70+
Arguments.insert(Arguments.begin(), CI.arg_begin(), CI.arg_end());
6971
FunctionType *FTy = CI.getCalledFunction()->getFunctionType();
7072
ParamTys.insert(ParamTys.begin(), FTy->param_begin(), FTy->param_end());
7173
}

0 commit comments

Comments
 (0)