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 | 37f0ca0 | 2018-05-15 12:08:00 +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 | 37f0ca0 | 2018-05-15 12:08:00 +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 | 37f0ca0 | 2018-05-15 12:08:00 +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 | 37f0ca0 | 2018-05-15 12:08:00 +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 | 37f0ca0 | 2018-05-15 12:08:00 +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 | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 47 | static llvm::cl::opt<std::string> |
Guillaume Chatelet | 8c91d4c | 2018-06-07 07:51:16 +0000 | [diff] [blame^] | 48 | BenchmarkFile("benchmarks-file", llvm::cl::desc(""), llvm::cl::init("")); |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 49 | |
| 50 | enum class BenchmarkModeE { Latency, Uops, Analysis }; |
| 51 | static llvm::cl::opt<BenchmarkModeE> BenchmarkMode( |
Clement Courbet | 5ec03cd | 2018-05-18 12:33:57 +0000 | [diff] [blame] | 52 | "mode", llvm::cl::desc("the mode to run"), |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 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 | 37f0ca0 | 2018-05-15 12:08:00 +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 | cf21074 | 2018-05-17 13:41:28 +0000 | [diff] [blame] | 73 | static llvm::cl::opt<std::string> |
| 74 | AnalysisClustersOutputFile("analysis-clusters-output-file", |
| 75 | llvm::cl::desc(""), llvm::cl::init("-")); |
| 76 | static llvm::cl::opt<std::string> |
| 77 | AnalysisInconsistenciesOutputFile("analysis-inconsistencies-output-file", |
| 78 | llvm::cl::desc(""), llvm::cl::init("-")); |
Clement Courbet | caa163e | 2018-05-16 09:50:04 +0000 | [diff] [blame] | 79 | |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 80 | namespace exegesis { |
| 81 | |
Guillaume Chatelet | 8c91d4c | 2018-06-07 07:51:16 +0000 | [diff] [blame^] | 82 | static llvm::ExitOnError ExitOnErr; |
| 83 | |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 84 | static unsigned GetOpcodeOrDie(const llvm::MCInstrInfo &MCInstrInfo) { |
| 85 | if (OpcodeName.empty() && (OpcodeIndex == 0)) |
| 86 | llvm::report_fatal_error( |
| 87 | "please provide one and only one of 'opcode-index' or 'opcode-name'"); |
| 88 | if (OpcodeIndex > 0) |
| 89 | return OpcodeIndex; |
| 90 | // Resolve opcode name -> opcode. |
| 91 | for (unsigned I = 0, E = MCInstrInfo.getNumOpcodes(); I < E; ++I) |
| 92 | if (MCInstrInfo.getName(I) == OpcodeName) |
| 93 | return I; |
| 94 | llvm::report_fatal_error(llvm::Twine("unknown opcode ").concat(OpcodeName)); |
| 95 | } |
| 96 | |
Clement Courbet | 53d35d2 | 2018-06-05 10:56:19 +0000 | [diff] [blame] | 97 | static BenchmarkResultContext |
| 98 | getBenchmarkResultContext(const LLVMState &State) { |
| 99 | BenchmarkResultContext Ctx; |
| 100 | |
| 101 | const llvm::MCInstrInfo &InstrInfo = State.getInstrInfo(); |
| 102 | for (unsigned E = InstrInfo.getNumOpcodes(), I = 0; I < E; ++I) |
| 103 | Ctx.addInstrEntry(I, InstrInfo.getName(I).data()); |
| 104 | |
| 105 | const llvm::MCRegisterInfo &RegInfo = State.getRegInfo(); |
| 106 | for (unsigned E = RegInfo.getNumRegs(), I = 0; I < E; ++I) |
| 107 | Ctx.addRegEntry(I, RegInfo.getName(I)); |
| 108 | |
| 109 | return Ctx; |
| 110 | } |
| 111 | |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 112 | void benchmarkMain() { |
| 113 | if (exegesis::pfm::pfmInitialize()) |
| 114 | llvm::report_fatal_error("cannot initialize libpfm"); |
| 115 | |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 116 | llvm::InitializeNativeTarget(); |
| 117 | llvm::InitializeNativeTargetAsmPrinter(); |
| 118 | |
| 119 | // FIXME: Target-specific filter. |
| 120 | X86Filter Filter; |
| 121 | |
| 122 | const LLVMState State; |
| 123 | |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 124 | // FIXME: Do not require SchedModel for latency. |
Simon Pilgrim | 656444b | 2018-04-18 14:46:54 +0000 | [diff] [blame] | 125 | if (!State.getSubtargetInfo().getSchedModel().hasExtraProcessorInfo()) |
| 126 | llvm::report_fatal_error("sched model is missing extra processor info!"); |
| 127 | |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 128 | std::unique_ptr<BenchmarkRunner> Runner; |
| 129 | switch (BenchmarkMode) { |
| 130 | case BenchmarkModeE::Latency: |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 131 | Runner = llvm::make_unique<LatencyBenchmarkRunner>(State); |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 132 | break; |
| 133 | case BenchmarkModeE::Uops: |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 134 | Runner = llvm::make_unique<UopsBenchmarkRunner>(State); |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 135 | break; |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 136 | case BenchmarkModeE::Analysis: |
| 137 | llvm_unreachable("not a benchmark"); |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 140 | if (NumRepetitions == 0) |
| 141 | llvm::report_fatal_error("--num-repetitions must be greater than zero"); |
| 142 | |
Guillaume Chatelet | 8c91d4c | 2018-06-07 07:51:16 +0000 | [diff] [blame^] | 143 | // Write to standard output if file is not set. |
| 144 | if (BenchmarkFile.empty()) |
| 145 | BenchmarkFile = "-"; |
| 146 | |
| 147 | ExitOnErr( |
| 148 | Runner->run(GetOpcodeOrDie(State.getInstrInfo()), Filter, NumRepetitions) |
| 149 | .writeYaml(getBenchmarkResultContext(State), BenchmarkFile)); |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 150 | exegesis::pfm::pfmTerminate(); |
| 151 | } |
| 152 | |
Clement Courbet | cf21074 | 2018-05-17 13:41:28 +0000 | [diff] [blame] | 153 | // Prints the results of running analysis pass `Pass` to file `OutputFilename` |
| 154 | // if OutputFilename is non-empty. |
| 155 | template <typename Pass> |
| 156 | static void maybeRunAnalysis(const Analysis &Analyzer, const std::string &Name, |
Clement Courbet | 53d35d2 | 2018-06-05 10:56:19 +0000 | [diff] [blame] | 157 | const std::string &OutputFilename) { |
Clement Courbet | cf21074 | 2018-05-17 13:41:28 +0000 | [diff] [blame] | 158 | if (OutputFilename.empty()) |
| 159 | return; |
| 160 | if (OutputFilename != "-") { |
| 161 | llvm::errs() << "Printing " << Name << " results to file '" |
| 162 | << OutputFilename << "'\n"; |
| 163 | } |
| 164 | std::error_code ErrorCode; |
| 165 | llvm::raw_fd_ostream ClustersOS(OutputFilename, ErrorCode, |
| 166 | llvm::sys::fs::F_RW); |
Guillaume Chatelet | 8c91d4c | 2018-06-07 07:51:16 +0000 | [diff] [blame^] | 167 | ExitOnErr(llvm::errorCodeToError(ErrorCode)); |
| 168 | ExitOnErr(Analyzer.run<Pass>(ClustersOS)); |
Clement Courbet | cf21074 | 2018-05-17 13:41:28 +0000 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | static void analysisMain() { |
Guillaume Chatelet | 8c91d4c | 2018-06-07 07:51:16 +0000 | [diff] [blame^] | 172 | if (BenchmarkFile.empty()) |
| 173 | llvm::report_fatal_error("--benchmarks-file must be set."); |
| 174 | |
Clement Courbet | 53d35d2 | 2018-06-05 10:56:19 +0000 | [diff] [blame] | 175 | llvm::InitializeNativeTarget(); |
| 176 | llvm::InitializeNativeTargetAsmPrinter(); |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 177 | // Read benchmarks. |
Clement Courbet | 53d35d2 | 2018-06-05 10:56:19 +0000 | [diff] [blame] | 178 | const LLVMState State; |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 179 | const std::vector<InstructionBenchmark> Points = |
Guillaume Chatelet | 8c91d4c | 2018-06-07 07:51:16 +0000 | [diff] [blame^] | 180 | ExitOnErr(InstructionBenchmark::readYamls( |
| 181 | getBenchmarkResultContext(State), BenchmarkFile)); |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 182 | llvm::outs() << "Parsed " << Points.size() << " benchmark points\n"; |
| 183 | if (Points.empty()) { |
| 184 | llvm::errs() << "no benchmarks to analyze\n"; |
| 185 | return; |
| 186 | } |
| 187 | // FIXME: Check that all points have the same triple/cpu. |
| 188 | // FIXME: Merge points from several runs (latency and uops). |
| 189 | |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 190 | std::string Error; |
| 191 | const auto *TheTarget = |
| 192 | llvm::TargetRegistry::lookupTarget(Points[0].LLVMTriple, Error); |
| 193 | if (!TheTarget) { |
| 194 | llvm::errs() << "unknown target '" << Points[0].LLVMTriple << "'\n"; |
| 195 | return; |
| 196 | } |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 197 | const auto Clustering = llvm::cantFail(InstructionBenchmarkClustering::create( |
| 198 | Points, AnalysisNumPoints, AnalysisEpsilon)); |
Clement Courbet | 6d6c1a9 | 2018-05-16 08:47:21 +0000 | [diff] [blame] | 199 | |
| 200 | const Analysis Analyzer(*TheTarget, Clustering); |
| 201 | |
Clement Courbet | cf21074 | 2018-05-17 13:41:28 +0000 | [diff] [blame] | 202 | maybeRunAnalysis<Analysis::PrintClusters>(Analyzer, "analysis clusters", |
| 203 | AnalysisClustersOutputFile); |
| 204 | maybeRunAnalysis<Analysis::PrintSchedClassInconsistencies>( |
| 205 | Analyzer, "sched class consistency analysis", |
| 206 | AnalysisInconsistenciesOutputFile); |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | } // namespace exegesis |
| 210 | |
| 211 | int main(int Argc, char **Argv) { |
| 212 | llvm::cl::ParseCommandLineOptions(Argc, Argv, ""); |
| 213 | |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 214 | if (BenchmarkMode == BenchmarkModeE::Analysis) { |
| 215 | exegesis::analysisMain(); |
| 216 | } else { |
| 217 | exegesis::benchmarkMain(); |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 218 | } |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 219 | return EXIT_SUCCESS; |
| 220 | } |