|
28 | 28 |
|
29 | 29 | using namespace llvm; |
30 | 30 |
|
31 | | -static cl::opt<TargetTransformInfo::TargetCostKind> CostKind( |
| 31 | +enum class 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, |
@@ -79,22 +87,53 @@ static InstructionCost getCost(Instruction &Inst, TTI::TargetCostKind CostKind, |
79 | 87 | return TTI.getInstructionCost(&Inst, CostKind); |
80 | 88 | } |
81 | 89 |
|
| 90 | +static TTI::TargetCostKind |
| 91 | +OutputCostKindToTargetCostKind(OutputCostKind CostKind) { |
| 92 | + switch (CostKind) { |
| 93 | + case OutputCostKind::RecipThroughput: |
| 94 | + return TTI::TCK_RecipThroughput; |
| 95 | + case OutputCostKind::Latency: |
| 96 | + return TTI::TCK_Latency; |
| 97 | + case OutputCostKind::CodeSize: |
| 98 | + return TTI::TCK_CodeSize; |
| 99 | + case OutputCostKind::SizeAndLatency: |
| 100 | + return TTI::TCK_SizeAndLatency; |
| 101 | + default: |
| 102 | + llvm_unreachable("Unexpected OutputCostKind!"); |
| 103 | + }; |
| 104 | +} |
| 105 | + |
82 | 106 | PreservedAnalyses CostModelPrinterPass::run(Function &F, |
83 | 107 | FunctionAnalysisManager &AM) { |
84 | 108 | auto &TTI = AM.getResult<TargetIRAnalysis>(F); |
85 | 109 | auto &TLI = AM.getResult<TargetLibraryAnalysis>(F); |
86 | 110 | OS << "Printing analysis 'Cost Model Analysis' for function '" << F.getName() << "':\n"; |
87 | 111 | for (BasicBlock &B : F) { |
88 | 112 | for (Instruction &Inst : B) { |
89 | | - // TODO: Use a pass parameter instead of cl::opt CostKind to determine |
90 | | - // which cost kind to print. |
91 | | - InstructionCost Cost = getCost(Inst, CostKind, TTI, TLI); |
92 | | - if (auto CostVal = Cost.getValue()) |
93 | | - OS << "Cost Model: Found an estimated cost of " << *CostVal; |
94 | | - else |
95 | | - OS << "Cost Model: Invalid cost"; |
96 | | - |
97 | | - OS << " for instruction: " << Inst << "\n"; |
| 113 | + OS << "Cost Model: "; |
| 114 | + if (CostKind == OutputCostKind::All) { |
| 115 | + OS << "Found costs of "; |
| 116 | + InstructionCost RThru = |
| 117 | + getCost(Inst, TTI::TCK_RecipThroughput, TTI, TLI); |
| 118 | + InstructionCost CodeSize = getCost(Inst, TTI::TCK_CodeSize, TTI, TLI); |
| 119 | + InstructionCost Lat = getCost(Inst, TTI::TCK_Latency, TTI, TLI); |
| 120 | + InstructionCost SizeLat = |
| 121 | + getCost(Inst, TTI::TCK_SizeAndLatency, TTI, TLI); |
| 122 | + if (RThru == CodeSize && RThru == Lat && RThru == SizeLat) |
| 123 | + OS << RThru; |
| 124 | + else |
| 125 | + OS << "RThru:" << RThru << " CodeSize:" << CodeSize << " Lat:" << Lat |
| 126 | + << " SizeLat:" << SizeLat; |
| 127 | + OS << " for: " << Inst << "\n"; |
| 128 | + } else { |
| 129 | + InstructionCost Cost = |
| 130 | + getCost(Inst, OutputCostKindToTargetCostKind(CostKind), TTI, TLI); |
| 131 | + if (auto CostVal = Cost.getValue()) |
| 132 | + OS << "Found an estimated cost of " << *CostVal; |
| 133 | + else |
| 134 | + OS << "Invalid cost"; |
| 135 | + OS << " for instruction: " << Inst << "\n"; |
| 136 | + } |
98 | 137 | } |
99 | 138 | } |
100 | 139 | return PreservedAnalyses::all(); |
|
0 commit comments