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 | e752fd6 | 2018-06-18 11:27:47 +0000 | [diff] [blame^] | 63 | static llvm::cl::opt<bool> IgnoreInvalidSchedClass( |
| 64 | "ignore-invalid-sched-class", |
| 65 | llvm::cl::desc("ignore instructions that do not define a sched class"), |
| 66 | llvm::cl::init(false)); |
| 67 | |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 68 | static llvm::cl::opt<unsigned> AnalysisNumPoints( |
| 69 | "analysis-numpoints", |
| 70 | llvm::cl::desc("minimum number of points in an analysis cluster"), |
| 71 | llvm::cl::init(3)); |
| 72 | |
| 73 | static llvm::cl::opt<float> |
| 74 | AnalysisEpsilon("analysis-epsilon", |
| 75 | llvm::cl::desc("dbscan epsilon for analysis clustering"), |
| 76 | llvm::cl::init(0.1)); |
| 77 | |
Clement Courbet | cf21074 | 2018-05-17 13:41:28 +0000 | [diff] [blame] | 78 | static llvm::cl::opt<std::string> |
| 79 | AnalysisClustersOutputFile("analysis-clusters-output-file", |
| 80 | llvm::cl::desc(""), llvm::cl::init("-")); |
| 81 | static llvm::cl::opt<std::string> |
| 82 | AnalysisInconsistenciesOutputFile("analysis-inconsistencies-output-file", |
| 83 | llvm::cl::desc(""), llvm::cl::init("-")); |
Clement Courbet | caa163e | 2018-05-16 09:50:04 +0000 | [diff] [blame] | 84 | |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 85 | namespace exegesis { |
| 86 | |
Guillaume Chatelet | 8c91d4c | 2018-06-07 07:51:16 +0000 | [diff] [blame] | 87 | static llvm::ExitOnError ExitOnErr; |
| 88 | |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 89 | static unsigned GetOpcodeOrDie(const llvm::MCInstrInfo &MCInstrInfo) { |
| 90 | if (OpcodeName.empty() && (OpcodeIndex == 0)) |
| 91 | llvm::report_fatal_error( |
| 92 | "please provide one and only one of 'opcode-index' or 'opcode-name'"); |
| 93 | if (OpcodeIndex > 0) |
| 94 | return OpcodeIndex; |
| 95 | // Resolve opcode name -> opcode. |
| 96 | for (unsigned I = 0, E = MCInstrInfo.getNumOpcodes(); I < E; ++I) |
| 97 | if (MCInstrInfo.getName(I) == OpcodeName) |
| 98 | return I; |
| 99 | llvm::report_fatal_error(llvm::Twine("unknown opcode ").concat(OpcodeName)); |
| 100 | } |
| 101 | |
Clement Courbet | 53d35d2 | 2018-06-05 10:56:19 +0000 | [diff] [blame] | 102 | static BenchmarkResultContext |
| 103 | getBenchmarkResultContext(const LLVMState &State) { |
| 104 | BenchmarkResultContext Ctx; |
| 105 | |
| 106 | const llvm::MCInstrInfo &InstrInfo = State.getInstrInfo(); |
| 107 | for (unsigned E = InstrInfo.getNumOpcodes(), I = 0; I < E; ++I) |
| 108 | Ctx.addInstrEntry(I, InstrInfo.getName(I).data()); |
| 109 | |
| 110 | const llvm::MCRegisterInfo &RegInfo = State.getRegInfo(); |
| 111 | for (unsigned E = RegInfo.getNumRegs(), I = 0; I < E; ++I) |
| 112 | Ctx.addRegEntry(I, RegInfo.getName(I)); |
| 113 | |
| 114 | return Ctx; |
| 115 | } |
| 116 | |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 117 | void benchmarkMain() { |
| 118 | if (exegesis::pfm::pfmInitialize()) |
| 119 | llvm::report_fatal_error("cannot initialize libpfm"); |
| 120 | |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 121 | llvm::InitializeNativeTarget(); |
| 122 | llvm::InitializeNativeTargetAsmPrinter(); |
| 123 | |
| 124 | // FIXME: Target-specific filter. |
| 125 | X86Filter Filter; |
| 126 | |
| 127 | const LLVMState State; |
Clement Courbet | e752fd6 | 2018-06-18 11:27:47 +0000 | [diff] [blame^] | 128 | const auto Opcode = GetOpcodeOrDie(State.getInstrInfo()); |
| 129 | |
| 130 | // Ignore instructions without a sched class if -ignore-invalid-sched-class is |
| 131 | // passed. |
| 132 | if (IgnoreInvalidSchedClass && |
| 133 | State.getInstrInfo().get(Opcode).getSchedClass() == 0) { |
| 134 | llvm::errs() << "ignoring instruction without sched class\n"; |
| 135 | return; |
| 136 | } |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 137 | |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 138 | // FIXME: Do not require SchedModel for latency. |
Simon Pilgrim | 656444b | 2018-04-18 14:46:54 +0000 | [diff] [blame] | 139 | if (!State.getSubtargetInfo().getSchedModel().hasExtraProcessorInfo()) |
| 140 | llvm::report_fatal_error("sched model is missing extra processor info!"); |
| 141 | |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 142 | std::unique_ptr<BenchmarkRunner> Runner; |
| 143 | switch (BenchmarkMode) { |
| 144 | case BenchmarkModeE::Latency: |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 145 | Runner = llvm::make_unique<LatencyBenchmarkRunner>(State); |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 146 | break; |
| 147 | case BenchmarkModeE::Uops: |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 148 | Runner = llvm::make_unique<UopsBenchmarkRunner>(State); |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 149 | break; |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 150 | case BenchmarkModeE::Analysis: |
| 151 | llvm_unreachable("not a benchmark"); |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 152 | } |
| 153 | |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 154 | if (NumRepetitions == 0) |
| 155 | llvm::report_fatal_error("--num-repetitions must be greater than zero"); |
| 156 | |
Guillaume Chatelet | 8c91d4c | 2018-06-07 07:51:16 +0000 | [diff] [blame] | 157 | // Write to standard output if file is not set. |
| 158 | if (BenchmarkFile.empty()) |
| 159 | BenchmarkFile = "-"; |
| 160 | |
Guillaume Chatelet | b4f1582 | 2018-06-07 14:00:29 +0000 | [diff] [blame] | 161 | const BenchmarkResultContext Context = getBenchmarkResultContext(State); |
Clement Courbet | e752fd6 | 2018-06-18 11:27:47 +0000 | [diff] [blame^] | 162 | std::vector<InstructionBenchmark> Results = |
| 163 | ExitOnErr(Runner->run(Opcode, Filter, NumRepetitions)); |
Guillaume Chatelet | b4f1582 | 2018-06-07 14:00:29 +0000 | [diff] [blame] | 164 | for (InstructionBenchmark &Result : Results) |
Guillaume Chatelet | 015b3e5 | 2018-06-11 14:10:10 +0000 | [diff] [blame] | 165 | ExitOnErr(Result.writeYaml(Context, BenchmarkFile)); |
Guillaume Chatelet | b4f1582 | 2018-06-07 14:00:29 +0000 | [diff] [blame] | 166 | |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 167 | exegesis::pfm::pfmTerminate(); |
| 168 | } |
| 169 | |
Clement Courbet | cf21074 | 2018-05-17 13:41:28 +0000 | [diff] [blame] | 170 | // Prints the results of running analysis pass `Pass` to file `OutputFilename` |
| 171 | // if OutputFilename is non-empty. |
| 172 | template <typename Pass> |
| 173 | static void maybeRunAnalysis(const Analysis &Analyzer, const std::string &Name, |
Clement Courbet | 53d35d2 | 2018-06-05 10:56:19 +0000 | [diff] [blame] | 174 | const std::string &OutputFilename) { |
Clement Courbet | cf21074 | 2018-05-17 13:41:28 +0000 | [diff] [blame] | 175 | if (OutputFilename.empty()) |
| 176 | return; |
| 177 | if (OutputFilename != "-") { |
| 178 | llvm::errs() << "Printing " << Name << " results to file '" |
| 179 | << OutputFilename << "'\n"; |
| 180 | } |
| 181 | std::error_code ErrorCode; |
| 182 | llvm::raw_fd_ostream ClustersOS(OutputFilename, ErrorCode, |
Zachary Turner | 1f67a3c | 2018-06-07 19:58:58 +0000 | [diff] [blame] | 183 | llvm::sys::fs::FA_Read | |
| 184 | llvm::sys::fs::FA_Write); |
| 185 | if (ErrorCode) |
| 186 | llvm::report_fatal_error("cannot open out file: " + OutputFilename); |
| 187 | if (auto Err = Analyzer.run<Pass>(ClustersOS)) |
| 188 | llvm::report_fatal_error(std::move(Err)); |
Clement Courbet | cf21074 | 2018-05-17 13:41:28 +0000 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | static void analysisMain() { |
Guillaume Chatelet | 8c91d4c | 2018-06-07 07:51:16 +0000 | [diff] [blame] | 192 | if (BenchmarkFile.empty()) |
| 193 | llvm::report_fatal_error("--benchmarks-file must be set."); |
| 194 | |
Clement Courbet | 53d35d2 | 2018-06-05 10:56:19 +0000 | [diff] [blame] | 195 | llvm::InitializeNativeTarget(); |
| 196 | llvm::InitializeNativeTargetAsmPrinter(); |
Clement Courbet | 4273e1e | 2018-06-15 07:30:45 +0000 | [diff] [blame] | 197 | llvm::InitializeNativeTargetDisassembler(); |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 198 | // Read benchmarks. |
Clement Courbet | 53d35d2 | 2018-06-05 10:56:19 +0000 | [diff] [blame] | 199 | const LLVMState State; |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 200 | const std::vector<InstructionBenchmark> Points = |
Guillaume Chatelet | 8c91d4c | 2018-06-07 07:51:16 +0000 | [diff] [blame] | 201 | ExitOnErr(InstructionBenchmark::readYamls( |
| 202 | getBenchmarkResultContext(State), BenchmarkFile)); |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 203 | llvm::outs() << "Parsed " << Points.size() << " benchmark points\n"; |
| 204 | if (Points.empty()) { |
| 205 | llvm::errs() << "no benchmarks to analyze\n"; |
| 206 | return; |
| 207 | } |
| 208 | // FIXME: Check that all points have the same triple/cpu. |
| 209 | // FIXME: Merge points from several runs (latency and uops). |
| 210 | |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 211 | std::string Error; |
| 212 | const auto *TheTarget = |
| 213 | llvm::TargetRegistry::lookupTarget(Points[0].LLVMTriple, Error); |
| 214 | if (!TheTarget) { |
| 215 | llvm::errs() << "unknown target '" << Points[0].LLVMTriple << "'\n"; |
| 216 | return; |
| 217 | } |
Guillaume Chatelet | 6416592 | 2018-06-11 09:18:01 +0000 | [diff] [blame] | 218 | const auto Clustering = ExitOnErr(InstructionBenchmarkClustering::create( |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 219 | Points, AnalysisNumPoints, AnalysisEpsilon)); |
Clement Courbet | 6d6c1a9 | 2018-05-16 08:47:21 +0000 | [diff] [blame] | 220 | |
| 221 | const Analysis Analyzer(*TheTarget, Clustering); |
| 222 | |
Clement Courbet | cf21074 | 2018-05-17 13:41:28 +0000 | [diff] [blame] | 223 | maybeRunAnalysis<Analysis::PrintClusters>(Analyzer, "analysis clusters", |
| 224 | AnalysisClustersOutputFile); |
| 225 | maybeRunAnalysis<Analysis::PrintSchedClassInconsistencies>( |
| 226 | Analyzer, "sched class consistency analysis", |
| 227 | AnalysisInconsistenciesOutputFile); |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | } // namespace exegesis |
| 231 | |
| 232 | int main(int Argc, char **Argv) { |
| 233 | llvm::cl::ParseCommandLineOptions(Argc, Argv, ""); |
| 234 | |
Guillaume Chatelet | 6416592 | 2018-06-11 09:18:01 +0000 | [diff] [blame] | 235 | exegesis::ExitOnErr.setExitCodeMapper([](const llvm::Error &Err) { |
| 236 | if (Err.isA<llvm::StringError>()) |
| 237 | return EXIT_SUCCESS; |
| 238 | return EXIT_FAILURE; |
| 239 | }); |
| 240 | |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 241 | if (BenchmarkMode == BenchmarkModeE::Analysis) { |
| 242 | exegesis::analysisMain(); |
| 243 | } else { |
| 244 | exegesis::benchmarkMain(); |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 245 | } |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 246 | return EXIT_SUCCESS; |
| 247 | } |