28
28
29
29
using namespace llvm ;
30
30
31
- static cl::opt<TargetTransformInfo::TargetCostKind> CostKind (
31
+ enum OutputCostKind {
32
+ RecipThroughput,
33
+ Latency,
34
+ CodeSize,
35
+ SizeAndLatency,
36
+ All,
37
+ };
38
+
39
+ static cl::opt<OutputCostKind> CostKind (
32
40
" cost-kind" , cl::desc(" Target cost kind" ),
33
- cl::init(TargetTransformInfo::TCK_RecipThroughput ),
34
- cl::values(clEnumValN(TargetTransformInfo::TCK_RecipThroughput ,
35
- " throughput " , " Reciprocal throughput" ),
36
- clEnumValN(TargetTransformInfo::TCK_Latency ,
37
- " latency " , " Instruction latency" ),
38
- clEnumValN(TargetTransformInfo::TCK_CodeSize ,
39
- " code-size " , " Code size" ) ,
40
- clEnumValN(TargetTransformInfo::TCK_SizeAndLatency ,
41
- " size-latency " , " Code size and latency " )));
41
+ cl::init(OutputCostKind::RecipThroughput ),
42
+ cl::values(clEnumValN(OutputCostKind::RecipThroughput, " throughput " ,
43
+ " Reciprocal throughput" ),
44
+ clEnumValN(OutputCostKind::Latency, " latency " ,
45
+ " Instruction latency" ),
46
+ clEnumValN(OutputCostKind::CodeSize, " code-size " , " Code size " ) ,
47
+ clEnumValN(OutputCostKind::SizeAndLatency , " size-latency " ,
48
+ " Code size and latency " ) ,
49
+ clEnumValN(OutputCostKind::All, " all " , " Print all cost kinds " )));
42
50
43
51
enum class IntrinsicCostStrategy {
44
52
InstructionCost,
@@ -63,34 +71,51 @@ static cl::opt<IntrinsicCostStrategy> IntrinsicCost(
63
71
#define CM_NAME " cost-model"
64
72
#define DEBUG_TYPE CM_NAME
65
73
74
+ static InstructionCost getCost (Instruction &Inst, TTI::TargetCostKind CostKind,
75
+ TargetTransformInfo &TTI,
76
+ TargetLibraryInfo &TLI) {
77
+ auto *II = dyn_cast<IntrinsicInst>(&Inst);
78
+ if (II && IntrinsicCost != IntrinsicCostStrategy::InstructionCost) {
79
+ IntrinsicCostAttributes ICA (
80
+ II->getIntrinsicID (), *II, InstructionCost::getInvalid (),
81
+ /* TypeBasedOnly=*/ IntrinsicCost ==
82
+ IntrinsicCostStrategy::TypeBasedIntrinsicCost,
83
+ &TLI);
84
+ return TTI.getIntrinsicInstrCost (ICA, CostKind);
85
+ }
86
+
87
+ return TTI.getInstructionCost (&Inst, CostKind);
88
+ }
89
+
66
90
PreservedAnalyses CostModelPrinterPass::run (Function &F,
67
91
FunctionAnalysisManager &AM) {
68
92
auto &TTI = AM.getResult <TargetIRAnalysis>(F);
69
93
auto &TLI = AM.getResult <TargetLibraryAnalysis>(F);
70
94
OS << " Printing analysis 'Cost Model Analysis' for function '" << F.getName () << " ':\n " ;
71
95
for (BasicBlock &B : F) {
72
96
for (Instruction &Inst : B) {
73
- // TODO: Use a pass parameter instead of cl::opt CostKind to determine
74
- // which cost kind to print.
75
- InstructionCost Cost;
76
- auto *II = dyn_cast<IntrinsicInst>(&Inst);
77
- if (II && IntrinsicCost != IntrinsicCostStrategy::InstructionCost) {
78
- IntrinsicCostAttributes ICA (
79
- II->getIntrinsicID (), *II, InstructionCost::getInvalid (),
80
- /* TypeBasedOnly=*/ IntrinsicCost ==
81
- IntrinsicCostStrategy::TypeBasedIntrinsicCost,
82
- &TLI);
83
- Cost = TTI.getIntrinsicInstrCost (ICA, CostKind);
97
+ OS << " Cost Model: " ;
98
+ if (CostKind == OutputCostKind::All) {
99
+ OS << " Found costs of " ;
100
+ InstructionCost RThru = getCost (Inst, TTI::TCK_RecipThroughput, TTI, TLI);
101
+ InstructionCost CodeSize = getCost (Inst, TTI::TCK_CodeSize, TTI, TLI);
102
+ InstructionCost Lat = getCost (Inst, TTI::TCK_Latency, TTI, TLI);
103
+ InstructionCost SizeLat = getCost (Inst, TTI::TCK_SizeAndLatency, TTI, TLI);
104
+ if (RThru == CodeSize && RThru == Lat && RThru == SizeLat)
105
+ OS << RThru;
106
+ else
107
+ OS << " RThru:" << RThru << " CodeSize:" << CodeSize << " Lat:" << Lat
108
+ << " SizeLat:" << SizeLat;
109
+ OS << " for: " << Inst << " \n " ;
84
110
} else {
85
- Cost = TTI.getInstructionCost (&Inst, CostKind);
111
+ InstructionCost Cost =
112
+ getCost (Inst, (TTI::TargetCostKind)(unsigned )CostKind, TTI, TLI);
113
+ if (auto CostVal = Cost.getValue ())
114
+ OS << " Found an estimated cost of " << *CostVal;
115
+ else
116
+ OS << " Invalid cost" ;
117
+ OS << " for instruction: " << Inst << " \n " ;
86
118
}
87
-
88
- if (auto CostVal = Cost.getValue ())
89
- OS << " Cost Model: Found an estimated cost of " << *CostVal;
90
- else
91
- OS << " Cost Model: Invalid cost" ;
92
-
93
- OS << " for instruction: " << Inst << " \n " ;
94
119
}
95
120
}
96
121
return PreservedAnalyses::all ();
0 commit comments