Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 1 | //===-- llvm-exegesis.cpp ---------------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | /// |
| 10 | /// \file |
| 11 | /// Measures execution properties (latencies/uops) of an instruction. |
| 12 | /// |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Clement Courbet | 3d479fe | 2018-05-14 11:30:56 +0000 | [diff] [blame] | 15 | #include "lib/Analysis.h" |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 16 | #include "lib/BenchmarkResult.h" |
| 17 | #include "lib/BenchmarkRunner.h" |
Clement Courbet | 3d479fe | 2018-05-14 11:30:56 +0000 | [diff] [blame] | 18 | #include "lib/Clustering.h" |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 19 | #include "lib/Latency.h" |
| 20 | #include "lib/LlvmState.h" |
| 21 | #include "lib/PerfHelper.h" |
| 22 | #include "lib/Uops.h" |
| 23 | #include "lib/X86.h" |
| 24 | #include "llvm/ADT/StringExtras.h" |
| 25 | #include "llvm/ADT/Twine.h" |
| 26 | #include "llvm/MC/MCInstBuilder.h" |
| 27 | #include "llvm/MC/MCRegisterInfo.h" |
Clement Courbet | 3d479fe | 2018-05-14 11:30:56 +0000 | [diff] [blame] | 28 | #include "llvm/MC/MCSubtargetInfo.h" |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 29 | #include "llvm/Support/CommandLine.h" |
Clement Courbet | 3d479fe | 2018-05-14 11:30:56 +0000 | [diff] [blame] | 30 | #include "llvm/Support/Format.h" |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 31 | #include "llvm/Support/Path.h" |
Clement Courbet | 3d479fe | 2018-05-14 11:30:56 +0000 | [diff] [blame] | 32 | #include "llvm/Support/TargetRegistry.h" |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 33 | #include "llvm/Support/TargetSelect.h" |
| 34 | #include <algorithm> |
| 35 | #include <random> |
| 36 | #include <string> |
| 37 | #include <unordered_map> |
| 38 | |
| 39 | static llvm::cl::opt<unsigned> |
| 40 | OpcodeIndex("opcode-index", llvm::cl::desc("opcode to measure, by index"), |
| 41 | llvm::cl::init(0)); |
| 42 | |
| 43 | static llvm::cl::opt<std::string> |
| 44 | OpcodeName("opcode-name", llvm::cl::desc("opcode to measure, by name"), |
| 45 | llvm::cl::init("")); |
| 46 | |
Clement Courbet | 3d479fe | 2018-05-14 11:30:56 +0000 | [diff] [blame] | 47 | static llvm::cl::opt<std::string> |
| 48 | BenchmarkFile("benchmarks-file", llvm::cl::desc(""), llvm::cl::init("-")); |
| 49 | |
| 50 | enum class BenchmarkModeE { Latency, Uops, Analysis }; |
| 51 | static llvm::cl::opt<BenchmarkModeE> BenchmarkMode( |
| 52 | "benchmark-mode", llvm::cl::desc("the benchmark mode to run"), |
| 53 | llvm::cl::values( |
| 54 | clEnumValN(BenchmarkModeE::Latency, "latency", "Instruction Latency"), |
| 55 | clEnumValN(BenchmarkModeE::Uops, "uops", "Uop Decomposition"), |
| 56 | clEnumValN(BenchmarkModeE::Analysis, "analysis", "Analysis"))); |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 57 | |
| 58 | static llvm::cl::opt<unsigned> |
| 59 | NumRepetitions("num-repetitions", |
| 60 | llvm::cl::desc("number of time to repeat the asm snippet"), |
| 61 | llvm::cl::init(10000)); |
| 62 | |
Clement Courbet | 3d479fe | 2018-05-14 11:30:56 +0000 | [diff] [blame] | 63 | static llvm::cl::opt<unsigned> AnalysisNumPoints( |
| 64 | "analysis-numpoints", |
| 65 | llvm::cl::desc("minimum number of points in an analysis cluster"), |
| 66 | llvm::cl::init(3)); |
| 67 | |
| 68 | static llvm::cl::opt<float> |
| 69 | AnalysisEpsilon("analysis-epsilon", |
| 70 | llvm::cl::desc("dbscan epsilon for analysis clustering"), |
| 71 | llvm::cl::init(0.1)); |
| 72 | |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 73 | namespace exegesis { |
| 74 | |
Clement Courbet | 3d479fe | 2018-05-14 11:30:56 +0000 | [diff] [blame] | 75 | void benchmarkMain() { |
| 76 | if (exegesis::pfm::pfmInitialize()) |
| 77 | llvm::report_fatal_error("cannot initialize libpfm"); |
| 78 | |
| 79 | if (OpcodeName.empty() == (OpcodeIndex == 0)) |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 80 | llvm::report_fatal_error( |
Simon Pilgrim | 656444b | 2018-04-18 14:46:54 +0000 | [diff] [blame] | 81 | "please provide one and only one of 'opcode-index' or 'opcode-name'"); |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 82 | |
| 83 | llvm::InitializeNativeTarget(); |
| 84 | llvm::InitializeNativeTargetAsmPrinter(); |
| 85 | |
| 86 | // FIXME: Target-specific filter. |
| 87 | X86Filter Filter; |
| 88 | |
| 89 | const LLVMState State; |
| 90 | |
Simon Pilgrim | 656444b | 2018-04-18 14:46:54 +0000 | [diff] [blame] | 91 | if (!State.getSubtargetInfo().getSchedModel().hasExtraProcessorInfo()) |
| 92 | llvm::report_fatal_error("sched model is missing extra processor info!"); |
| 93 | |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 94 | unsigned Opcode = OpcodeIndex; |
| 95 | if (Opcode == 0) { |
| 96 | // Resolve opcode name -> opcode. |
| 97 | for (unsigned I = 0, E = State.getInstrInfo().getNumOpcodes(); I < E; ++I) { |
| 98 | if (State.getInstrInfo().getName(I) == OpcodeName) { |
| 99 | Opcode = I; |
| 100 | break; |
| 101 | } |
| 102 | } |
| 103 | if (Opcode == 0) { |
| 104 | llvm::report_fatal_error( |
| 105 | llvm::Twine("unknown opcode ").concat(OpcodeName)); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | std::unique_ptr<BenchmarkRunner> Runner; |
| 110 | switch (BenchmarkMode) { |
| 111 | case BenchmarkModeE::Latency: |
| 112 | Runner = llvm::make_unique<LatencyBenchmarkRunner>(); |
| 113 | break; |
| 114 | case BenchmarkModeE::Uops: |
| 115 | Runner = llvm::make_unique<UopsBenchmarkRunner>(); |
| 116 | break; |
Clement Courbet | 3d479fe | 2018-05-14 11:30:56 +0000 | [diff] [blame] | 117 | case BenchmarkModeE::Analysis: |
| 118 | llvm_unreachable("not a benchmark"); |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | Runner->run(State, Opcode, NumRepetitions > 0 ? NumRepetitions : 1, Filter) |
Clement Courbet | 3d479fe | 2018-05-14 11:30:56 +0000 | [diff] [blame] | 122 | .writeYamlOrDie(BenchmarkFile); |
| 123 | exegesis::pfm::pfmTerminate(); |
| 124 | } |
| 125 | |
| 126 | void analysisMain() { |
| 127 | // Read benchmarks. |
| 128 | const std::vector<InstructionBenchmark> Points = |
| 129 | InstructionBenchmark::readYamlsOrDie(BenchmarkFile); |
| 130 | llvm::outs() << "Parsed " << Points.size() << " benchmark points\n"; |
| 131 | if (Points.empty()) { |
| 132 | llvm::errs() << "no benchmarks to analyze\n"; |
| 133 | return; |
| 134 | } |
| 135 | // TODO: Merge points from several runs (latency and uops). |
| 136 | |
| 137 | // FIXME: Check that all points have the same triple/cpu. |
| 138 | llvm::InitializeAllTargets(); |
| 139 | std::string Error; |
| 140 | const auto *TheTarget = |
| 141 | llvm::TargetRegistry::lookupTarget(Points[0].LLVMTriple, Error); |
| 142 | if (!TheTarget) { |
| 143 | llvm::errs() << "unknown target '" << Points[0].LLVMTriple << "'\n"; |
| 144 | return; |
| 145 | } |
| 146 | std::unique_ptr<llvm::MCSubtargetInfo> STI(TheTarget->createMCSubtargetInfo( |
| 147 | Points[0].LLVMTriple, Points[0].CpuName, "")); |
| 148 | |
| 149 | const auto Clustering = llvm::cantFail(InstructionBenchmarkClustering::create( |
| 150 | Points, AnalysisNumPoints, AnalysisEpsilon)); |
| 151 | if (auto Err = printAnalysisClusters(Clustering, *STI, llvm::outs())) { |
| 152 | llvm::report_fatal_error(std::move(Err)); |
| 153 | } |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | } // namespace exegesis |
| 157 | |
| 158 | int main(int Argc, char **Argv) { |
| 159 | llvm::cl::ParseCommandLineOptions(Argc, Argv, ""); |
| 160 | |
Clement Courbet | 3d479fe | 2018-05-14 11:30:56 +0000 | [diff] [blame] | 161 | if (BenchmarkMode == BenchmarkModeE::Analysis) { |
| 162 | exegesis::analysisMain(); |
| 163 | } else { |
| 164 | exegesis::benchmarkMain(); |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 165 | } |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 166 | return EXIT_SUCCESS; |
| 167 | } |