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/LlvmState.h" |
| 20 | #include "lib/PerfHelper.h" |
Clement Courbet | 4860b98 | 2018-06-26 08:49:30 +0000 | [diff] [blame] | 21 | #include "lib/Target.h" |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/StringExtras.h" |
| 23 | #include "llvm/ADT/Twine.h" |
| 24 | #include "llvm/MC/MCInstBuilder.h" |
| 25 | #include "llvm/MC/MCRegisterInfo.h" |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 26 | #include "llvm/MC/MCSubtargetInfo.h" |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 27 | #include "llvm/Support/CommandLine.h" |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 28 | #include "llvm/Support/Format.h" |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 29 | #include "llvm/Support/Path.h" |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 30 | #include "llvm/Support/TargetRegistry.h" |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 31 | #include "llvm/Support/TargetSelect.h" |
| 32 | #include <algorithm> |
| 33 | #include <random> |
| 34 | #include <string> |
| 35 | #include <unordered_map> |
| 36 | |
| 37 | static llvm::cl::opt<unsigned> |
| 38 | OpcodeIndex("opcode-index", llvm::cl::desc("opcode to measure, by index"), |
| 39 | llvm::cl::init(0)); |
| 40 | |
| 41 | static llvm::cl::opt<std::string> |
| 42 | OpcodeName("opcode-name", llvm::cl::desc("opcode to measure, by name"), |
| 43 | llvm::cl::init("")); |
| 44 | |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 45 | static llvm::cl::opt<std::string> |
Guillaume Chatelet | 8c91d4c | 2018-06-07 07:51:16 +0000 | [diff] [blame] | 46 | BenchmarkFile("benchmarks-file", llvm::cl::desc(""), llvm::cl::init("")); |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 47 | |
Clement Courbet | 4860b98 | 2018-06-26 08:49:30 +0000 | [diff] [blame] | 48 | static llvm::cl::opt<exegesis::InstructionBenchmark::ModeE> BenchmarkMode( |
Clement Courbet | 5ec03cd | 2018-05-18 12:33:57 +0000 | [diff] [blame] | 49 | "mode", llvm::cl::desc("the mode to run"), |
Clement Courbet | 4860b98 | 2018-06-26 08:49:30 +0000 | [diff] [blame] | 50 | llvm::cl::values(clEnumValN(exegesis::InstructionBenchmark::Latency, |
| 51 | "latency", "Instruction Latency"), |
| 52 | clEnumValN(exegesis::InstructionBenchmark::Uops, "uops", |
| 53 | "Uop Decomposition"), |
| 54 | // When not asking for a specific benchmark mode, we'll |
| 55 | // analyse the results. |
| 56 | clEnumValN(exegesis::InstructionBenchmark::Unknown, |
| 57 | "analysis", "Analysis"))); |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 58 | |
| 59 | static llvm::cl::opt<unsigned> |
| 60 | NumRepetitions("num-repetitions", |
| 61 | llvm::cl::desc("number of time to repeat the asm snippet"), |
| 62 | llvm::cl::init(10000)); |
| 63 | |
Clement Courbet | e752fd6 | 2018-06-18 11:27:47 +0000 | [diff] [blame] | 64 | static llvm::cl::opt<bool> IgnoreInvalidSchedClass( |
| 65 | "ignore-invalid-sched-class", |
| 66 | llvm::cl::desc("ignore instructions that do not define a sched class"), |
| 67 | llvm::cl::init(false)); |
| 68 | |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 69 | static llvm::cl::opt<unsigned> AnalysisNumPoints( |
| 70 | "analysis-numpoints", |
| 71 | llvm::cl::desc("minimum number of points in an analysis cluster"), |
| 72 | llvm::cl::init(3)); |
| 73 | |
| 74 | static llvm::cl::opt<float> |
| 75 | AnalysisEpsilon("analysis-epsilon", |
| 76 | llvm::cl::desc("dbscan epsilon for analysis clustering"), |
| 77 | llvm::cl::init(0.1)); |
| 78 | |
Clement Courbet | cf21074 | 2018-05-17 13:41:28 +0000 | [diff] [blame] | 79 | static llvm::cl::opt<std::string> |
| 80 | AnalysisClustersOutputFile("analysis-clusters-output-file", |
| 81 | llvm::cl::desc(""), llvm::cl::init("-")); |
| 82 | static llvm::cl::opt<std::string> |
| 83 | AnalysisInconsistenciesOutputFile("analysis-inconsistencies-output-file", |
| 84 | llvm::cl::desc(""), llvm::cl::init("-")); |
Clement Courbet | caa163e | 2018-05-16 09:50:04 +0000 | [diff] [blame] | 85 | |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 86 | namespace exegesis { |
| 87 | |
Guillaume Chatelet | 8c91d4c | 2018-06-07 07:51:16 +0000 | [diff] [blame] | 88 | static llvm::ExitOnError ExitOnErr; |
| 89 | |
Clement Courbet | 44b4c54 | 2018-06-19 11:28:59 +0000 | [diff] [blame] | 90 | #ifdef LLVM_EXEGESIS_INITIALIZE_NATIVE_TARGET |
| 91 | void LLVM_EXEGESIS_INITIALIZE_NATIVE_TARGET(); |
| 92 | #endif |
| 93 | |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 94 | static unsigned GetOpcodeOrDie(const llvm::MCInstrInfo &MCInstrInfo) { |
| 95 | if (OpcodeName.empty() && (OpcodeIndex == 0)) |
| 96 | llvm::report_fatal_error( |
| 97 | "please provide one and only one of 'opcode-index' or 'opcode-name'"); |
| 98 | if (OpcodeIndex > 0) |
| 99 | return OpcodeIndex; |
| 100 | // Resolve opcode name -> opcode. |
| 101 | for (unsigned I = 0, E = MCInstrInfo.getNumOpcodes(); I < E; ++I) |
| 102 | if (MCInstrInfo.getName(I) == OpcodeName) |
| 103 | return I; |
| 104 | llvm::report_fatal_error(llvm::Twine("unknown opcode ").concat(OpcodeName)); |
| 105 | } |
| 106 | |
Clement Courbet | 53d35d2 | 2018-06-05 10:56:19 +0000 | [diff] [blame] | 107 | static BenchmarkResultContext |
| 108 | getBenchmarkResultContext(const LLVMState &State) { |
| 109 | BenchmarkResultContext Ctx; |
| 110 | |
| 111 | const llvm::MCInstrInfo &InstrInfo = State.getInstrInfo(); |
| 112 | for (unsigned E = InstrInfo.getNumOpcodes(), I = 0; I < E; ++I) |
| 113 | Ctx.addInstrEntry(I, InstrInfo.getName(I).data()); |
| 114 | |
| 115 | const llvm::MCRegisterInfo &RegInfo = State.getRegInfo(); |
| 116 | for (unsigned E = RegInfo.getNumRegs(), I = 0; I < E; ++I) |
| 117 | Ctx.addRegEntry(I, RegInfo.getName(I)); |
| 118 | |
| 119 | return Ctx; |
| 120 | } |
| 121 | |
Clement Courbet | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame] | 122 | // Generates code snippets for opcode `Opcode`. |
| 123 | llvm::Expected<std::vector<BenchmarkCode>> |
Clement Courbet | 7958735 | 2018-09-13 08:06:29 +0000 | [diff] [blame^] | 124 | generateSnippets(const LLVMState &State, unsigned Opcode) { |
Clement Courbet | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame] | 125 | const std::unique_ptr<SnippetGenerator> Generator = |
| 126 | State.getExegesisTarget().createSnippetGenerator(BenchmarkMode, State); |
| 127 | if (!Generator) { |
| 128 | llvm::report_fatal_error("cannot create snippet generator"); |
| 129 | } |
| 130 | |
| 131 | const llvm::MCInstrDesc &InstrDesc = State.getInstrInfo().get(Opcode); |
| 132 | // Ignore instructions that we cannot run. |
| 133 | if (InstrDesc.isPseudo()) |
| 134 | return llvm::make_error<BenchmarkFailure>("Unsupported opcode: isPseudo"); |
| 135 | if (InstrDesc.isBranch() || InstrDesc.isIndirectBranch()) |
| 136 | return llvm::make_error<BenchmarkFailure>( |
| 137 | "Unsupported opcode: isBranch/isIndirectBranch"); |
| 138 | if (InstrDesc.isCall() || InstrDesc.isReturn()) |
| 139 | return llvm::make_error<BenchmarkFailure>( |
| 140 | "Unsupported opcode: isCall/isReturn"); |
| 141 | |
| 142 | return Generator->generateConfigurations(Opcode); |
| 143 | } |
| 144 | |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 145 | void benchmarkMain() { |
| 146 | if (exegesis::pfm::pfmInitialize()) |
| 147 | llvm::report_fatal_error("cannot initialize libpfm"); |
| 148 | |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 149 | llvm::InitializeNativeTarget(); |
| 150 | llvm::InitializeNativeTargetAsmPrinter(); |
Clement Courbet | 44b4c54 | 2018-06-19 11:28:59 +0000 | [diff] [blame] | 151 | #ifdef LLVM_EXEGESIS_INITIALIZE_NATIVE_TARGET |
| 152 | LLVM_EXEGESIS_INITIALIZE_NATIVE_TARGET(); |
| 153 | #endif |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 154 | |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 155 | const LLVMState State; |
Clement Courbet | e752fd6 | 2018-06-18 11:27:47 +0000 | [diff] [blame] | 156 | const auto Opcode = GetOpcodeOrDie(State.getInstrInfo()); |
| 157 | |
| 158 | // Ignore instructions without a sched class if -ignore-invalid-sched-class is |
| 159 | // passed. |
| 160 | if (IgnoreInvalidSchedClass && |
| 161 | State.getInstrInfo().get(Opcode).getSchedClass() == 0) { |
| 162 | llvm::errs() << "ignoring instruction without sched class\n"; |
| 163 | return; |
| 164 | } |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 165 | |
Clement Courbet | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame] | 166 | // FIXME: Allow arbitrary code. |
| 167 | const std::vector<BenchmarkCode> Configurations = |
Clement Courbet | 7958735 | 2018-09-13 08:06:29 +0000 | [diff] [blame^] | 168 | ExitOnErr(generateSnippets(State, Opcode)); |
Clement Courbet | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame] | 169 | |
Clement Courbet | 4860b98 | 2018-06-26 08:49:30 +0000 | [diff] [blame] | 170 | const std::unique_ptr<BenchmarkRunner> Runner = |
| 171 | State.getExegesisTarget().createBenchmarkRunner(BenchmarkMode, State); |
| 172 | if (!Runner) { |
| 173 | llvm::report_fatal_error("cannot create benchmark runner"); |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 176 | if (NumRepetitions == 0) |
| 177 | llvm::report_fatal_error("--num-repetitions must be greater than zero"); |
| 178 | |
Guillaume Chatelet | 8c91d4c | 2018-06-07 07:51:16 +0000 | [diff] [blame] | 179 | // Write to standard output if file is not set. |
| 180 | if (BenchmarkFile.empty()) |
| 181 | BenchmarkFile = "-"; |
| 182 | |
Guillaume Chatelet | b4f1582 | 2018-06-07 14:00:29 +0000 | [diff] [blame] | 183 | const BenchmarkResultContext Context = getBenchmarkResultContext(State); |
Guillaume Chatelet | b4f1582 | 2018-06-07 14:00:29 +0000 | [diff] [blame] | 184 | |
Clement Courbet | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame] | 185 | for (const BenchmarkCode &Conf : Configurations) { |
| 186 | InstructionBenchmark Result = |
| 187 | Runner->runConfiguration(Conf, NumRepetitions); |
| 188 | ExitOnErr(Result.writeYaml(Context, BenchmarkFile)); |
| 189 | } |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 190 | exegesis::pfm::pfmTerminate(); |
| 191 | } |
| 192 | |
Clement Courbet | cf21074 | 2018-05-17 13:41:28 +0000 | [diff] [blame] | 193 | // Prints the results of running analysis pass `Pass` to file `OutputFilename` |
| 194 | // if OutputFilename is non-empty. |
| 195 | template <typename Pass> |
| 196 | static void maybeRunAnalysis(const Analysis &Analyzer, const std::string &Name, |
Clement Courbet | 53d35d2 | 2018-06-05 10:56:19 +0000 | [diff] [blame] | 197 | const std::string &OutputFilename) { |
Clement Courbet | cf21074 | 2018-05-17 13:41:28 +0000 | [diff] [blame] | 198 | if (OutputFilename.empty()) |
| 199 | return; |
| 200 | if (OutputFilename != "-") { |
| 201 | llvm::errs() << "Printing " << Name << " results to file '" |
| 202 | << OutputFilename << "'\n"; |
| 203 | } |
| 204 | std::error_code ErrorCode; |
| 205 | llvm::raw_fd_ostream ClustersOS(OutputFilename, ErrorCode, |
Zachary Turner | 1f67a3c | 2018-06-07 19:58:58 +0000 | [diff] [blame] | 206 | llvm::sys::fs::FA_Read | |
| 207 | llvm::sys::fs::FA_Write); |
| 208 | if (ErrorCode) |
| 209 | llvm::report_fatal_error("cannot open out file: " + OutputFilename); |
| 210 | if (auto Err = Analyzer.run<Pass>(ClustersOS)) |
| 211 | llvm::report_fatal_error(std::move(Err)); |
Clement Courbet | cf21074 | 2018-05-17 13:41:28 +0000 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | static void analysisMain() { |
Guillaume Chatelet | 8c91d4c | 2018-06-07 07:51:16 +0000 | [diff] [blame] | 215 | if (BenchmarkFile.empty()) |
| 216 | llvm::report_fatal_error("--benchmarks-file must be set."); |
| 217 | |
Clement Courbet | 53d35d2 | 2018-06-05 10:56:19 +0000 | [diff] [blame] | 218 | llvm::InitializeNativeTarget(); |
| 219 | llvm::InitializeNativeTargetAsmPrinter(); |
Clement Courbet | 4273e1e | 2018-06-15 07:30:45 +0000 | [diff] [blame] | 220 | llvm::InitializeNativeTargetDisassembler(); |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 221 | // Read benchmarks. |
Clement Courbet | 53d35d2 | 2018-06-05 10:56:19 +0000 | [diff] [blame] | 222 | const LLVMState State; |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 223 | const std::vector<InstructionBenchmark> Points = |
Guillaume Chatelet | 8c91d4c | 2018-06-07 07:51:16 +0000 | [diff] [blame] | 224 | ExitOnErr(InstructionBenchmark::readYamls( |
| 225 | getBenchmarkResultContext(State), BenchmarkFile)); |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 226 | llvm::outs() << "Parsed " << Points.size() << " benchmark points\n"; |
| 227 | if (Points.empty()) { |
| 228 | llvm::errs() << "no benchmarks to analyze\n"; |
| 229 | return; |
| 230 | } |
| 231 | // FIXME: Check that all points have the same triple/cpu. |
| 232 | // FIXME: Merge points from several runs (latency and uops). |
| 233 | |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 234 | std::string Error; |
| 235 | const auto *TheTarget = |
| 236 | llvm::TargetRegistry::lookupTarget(Points[0].LLVMTriple, Error); |
| 237 | if (!TheTarget) { |
| 238 | llvm::errs() << "unknown target '" << Points[0].LLVMTriple << "'\n"; |
| 239 | return; |
| 240 | } |
Guillaume Chatelet | 6416592 | 2018-06-11 09:18:01 +0000 | [diff] [blame] | 241 | const auto Clustering = ExitOnErr(InstructionBenchmarkClustering::create( |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 242 | Points, AnalysisNumPoints, AnalysisEpsilon)); |
Clement Courbet | 6d6c1a9 | 2018-05-16 08:47:21 +0000 | [diff] [blame] | 243 | |
| 244 | const Analysis Analyzer(*TheTarget, Clustering); |
| 245 | |
Clement Courbet | cf21074 | 2018-05-17 13:41:28 +0000 | [diff] [blame] | 246 | maybeRunAnalysis<Analysis::PrintClusters>(Analyzer, "analysis clusters", |
| 247 | AnalysisClustersOutputFile); |
| 248 | maybeRunAnalysis<Analysis::PrintSchedClassInconsistencies>( |
| 249 | Analyzer, "sched class consistency analysis", |
| 250 | AnalysisInconsistenciesOutputFile); |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | } // namespace exegesis |
| 254 | |
| 255 | int main(int Argc, char **Argv) { |
| 256 | llvm::cl::ParseCommandLineOptions(Argc, Argv, ""); |
| 257 | |
Guillaume Chatelet | 6416592 | 2018-06-11 09:18:01 +0000 | [diff] [blame] | 258 | exegesis::ExitOnErr.setExitCodeMapper([](const llvm::Error &Err) { |
| 259 | if (Err.isA<llvm::StringError>()) |
| 260 | return EXIT_SUCCESS; |
| 261 | return EXIT_FAILURE; |
| 262 | }); |
| 263 | |
Clement Courbet | 4860b98 | 2018-06-26 08:49:30 +0000 | [diff] [blame] | 264 | if (BenchmarkMode == exegesis::InstructionBenchmark::Unknown) { |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 265 | exegesis::analysisMain(); |
| 266 | } else { |
| 267 | exegesis::benchmarkMain(); |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 268 | } |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 269 | return EXIT_SUCCESS; |
| 270 | } |