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" |
Clement Courbet | 78b2e73 | 2018-09-25 07:31:44 +0000 | [diff] [blame^] | 25 | #include "llvm/MC/MCObjectFileInfo.h" |
| 26 | #include "llvm/MC/MCParser/MCAsmParser.h" |
| 27 | #include "llvm/MC/MCParser/MCTargetAsmParser.h" |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 28 | #include "llvm/MC/MCRegisterInfo.h" |
Clement Courbet | 78b2e73 | 2018-09-25 07:31:44 +0000 | [diff] [blame^] | 29 | #include "llvm/MC/MCStreamer.h" |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 30 | #include "llvm/MC/MCSubtargetInfo.h" |
Clement Courbet | 78b2e73 | 2018-09-25 07:31:44 +0000 | [diff] [blame^] | 31 | #include "llvm/Object/ObjectFile.h" |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 32 | #include "llvm/Support/CommandLine.h" |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 33 | #include "llvm/Support/Format.h" |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 34 | #include "llvm/Support/Path.h" |
Clement Courbet | 78b2e73 | 2018-09-25 07:31:44 +0000 | [diff] [blame^] | 35 | #include "llvm/Support/SourceMgr.h" |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 36 | #include "llvm/Support/TargetRegistry.h" |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 37 | #include "llvm/Support/TargetSelect.h" |
| 38 | #include <algorithm> |
| 39 | #include <random> |
| 40 | #include <string> |
| 41 | #include <unordered_map> |
| 42 | |
| 43 | static llvm::cl::opt<unsigned> |
| 44 | OpcodeIndex("opcode-index", llvm::cl::desc("opcode to measure, by index"), |
| 45 | llvm::cl::init(0)); |
| 46 | |
| 47 | static llvm::cl::opt<std::string> |
| 48 | OpcodeName("opcode-name", llvm::cl::desc("opcode to measure, by name"), |
| 49 | llvm::cl::init("")); |
| 50 | |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 51 | static llvm::cl::opt<std::string> |
Clement Courbet | 78b2e73 | 2018-09-25 07:31:44 +0000 | [diff] [blame^] | 52 | SnippetsFile("snippets-file", llvm::cl::desc("code snippets to measure"), |
| 53 | llvm::cl::init("")); |
| 54 | |
| 55 | static llvm::cl::opt<std::string> |
Guillaume Chatelet | 8c91d4c | 2018-06-07 07:51:16 +0000 | [diff] [blame] | 56 | BenchmarkFile("benchmarks-file", llvm::cl::desc(""), llvm::cl::init("")); |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 57 | |
Clement Courbet | 4860b98 | 2018-06-26 08:49:30 +0000 | [diff] [blame] | 58 | static llvm::cl::opt<exegesis::InstructionBenchmark::ModeE> BenchmarkMode( |
Clement Courbet | 5ec03cd | 2018-05-18 12:33:57 +0000 | [diff] [blame] | 59 | "mode", llvm::cl::desc("the mode to run"), |
Clement Courbet | 4860b98 | 2018-06-26 08:49:30 +0000 | [diff] [blame] | 60 | llvm::cl::values(clEnumValN(exegesis::InstructionBenchmark::Latency, |
| 61 | "latency", "Instruction Latency"), |
| 62 | clEnumValN(exegesis::InstructionBenchmark::Uops, "uops", |
| 63 | "Uop Decomposition"), |
| 64 | // When not asking for a specific benchmark mode, we'll |
| 65 | // analyse the results. |
| 66 | clEnumValN(exegesis::InstructionBenchmark::Unknown, |
| 67 | "analysis", "Analysis"))); |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 68 | |
| 69 | static llvm::cl::opt<unsigned> |
| 70 | NumRepetitions("num-repetitions", |
| 71 | llvm::cl::desc("number of time to repeat the asm snippet"), |
| 72 | llvm::cl::init(10000)); |
| 73 | |
Clement Courbet | e752fd6 | 2018-06-18 11:27:47 +0000 | [diff] [blame] | 74 | static llvm::cl::opt<bool> IgnoreInvalidSchedClass( |
| 75 | "ignore-invalid-sched-class", |
| 76 | llvm::cl::desc("ignore instructions that do not define a sched class"), |
| 77 | llvm::cl::init(false)); |
| 78 | |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 79 | static llvm::cl::opt<unsigned> AnalysisNumPoints( |
| 80 | "analysis-numpoints", |
| 81 | llvm::cl::desc("minimum number of points in an analysis cluster"), |
| 82 | llvm::cl::init(3)); |
| 83 | |
| 84 | static llvm::cl::opt<float> |
| 85 | AnalysisEpsilon("analysis-epsilon", |
| 86 | llvm::cl::desc("dbscan epsilon for analysis clustering"), |
| 87 | llvm::cl::init(0.1)); |
| 88 | |
Clement Courbet | cf21074 | 2018-05-17 13:41:28 +0000 | [diff] [blame] | 89 | static llvm::cl::opt<std::string> |
| 90 | AnalysisClustersOutputFile("analysis-clusters-output-file", |
| 91 | llvm::cl::desc(""), llvm::cl::init("-")); |
| 92 | static llvm::cl::opt<std::string> |
| 93 | AnalysisInconsistenciesOutputFile("analysis-inconsistencies-output-file", |
| 94 | llvm::cl::desc(""), llvm::cl::init("-")); |
Clement Courbet | caa163e | 2018-05-16 09:50:04 +0000 | [diff] [blame] | 95 | |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 96 | namespace exegesis { |
| 97 | |
Guillaume Chatelet | 8c91d4c | 2018-06-07 07:51:16 +0000 | [diff] [blame] | 98 | static llvm::ExitOnError ExitOnErr; |
| 99 | |
Clement Courbet | 44b4c54 | 2018-06-19 11:28:59 +0000 | [diff] [blame] | 100 | #ifdef LLVM_EXEGESIS_INITIALIZE_NATIVE_TARGET |
| 101 | void LLVM_EXEGESIS_INITIALIZE_NATIVE_TARGET(); |
| 102 | #endif |
| 103 | |
Clement Courbet | 78b2e73 | 2018-09-25 07:31:44 +0000 | [diff] [blame^] | 104 | // Checks that only one of OpcodeName, OpcodeIndex or SnippetsFile is provided, |
| 105 | // and returns the opcode index or 0 if snippets should be read from |
| 106 | // `SnippetsFile`. |
| 107 | static unsigned getOpcodeOrDie(const llvm::MCInstrInfo &MCInstrInfo) { |
| 108 | const size_t NumSetFlags = (OpcodeName.empty() ? 0 : 1) + |
| 109 | (OpcodeIndex == 0 ? 0 : 1) + |
| 110 | (SnippetsFile.empty() ? 0 : 1); |
| 111 | if (NumSetFlags != 1) |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 112 | llvm::report_fatal_error( |
Clement Courbet | 78b2e73 | 2018-09-25 07:31:44 +0000 | [diff] [blame^] | 113 | "please provide one and only one of 'opcode-index', 'opcode-name' or " |
| 114 | "'snippets-file'"); |
| 115 | if (!SnippetsFile.empty()) |
| 116 | return 0; |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 117 | if (OpcodeIndex > 0) |
| 118 | return OpcodeIndex; |
| 119 | // Resolve opcode name -> opcode. |
| 120 | for (unsigned I = 0, E = MCInstrInfo.getNumOpcodes(); I < E; ++I) |
| 121 | if (MCInstrInfo.getName(I) == OpcodeName) |
| 122 | return I; |
| 123 | llvm::report_fatal_error(llvm::Twine("unknown opcode ").concat(OpcodeName)); |
| 124 | } |
| 125 | |
Clement Courbet | 53d35d2 | 2018-06-05 10:56:19 +0000 | [diff] [blame] | 126 | static BenchmarkResultContext |
| 127 | getBenchmarkResultContext(const LLVMState &State) { |
| 128 | BenchmarkResultContext Ctx; |
| 129 | |
| 130 | const llvm::MCInstrInfo &InstrInfo = State.getInstrInfo(); |
| 131 | for (unsigned E = InstrInfo.getNumOpcodes(), I = 0; I < E; ++I) |
| 132 | Ctx.addInstrEntry(I, InstrInfo.getName(I).data()); |
| 133 | |
| 134 | const llvm::MCRegisterInfo &RegInfo = State.getRegInfo(); |
| 135 | for (unsigned E = RegInfo.getNumRegs(), I = 0; I < E; ++I) |
| 136 | Ctx.addRegEntry(I, RegInfo.getName(I)); |
| 137 | |
| 138 | return Ctx; |
| 139 | } |
| 140 | |
Clement Courbet | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame] | 141 | // Generates code snippets for opcode `Opcode`. |
Clement Courbet | 78b2e73 | 2018-09-25 07:31:44 +0000 | [diff] [blame^] | 142 | static llvm::Expected<std::vector<BenchmarkCode>> |
Clement Courbet | 7958735 | 2018-09-13 08:06:29 +0000 | [diff] [blame] | 143 | generateSnippets(const LLVMState &State, unsigned Opcode) { |
Clement Courbet | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame] | 144 | const std::unique_ptr<SnippetGenerator> Generator = |
| 145 | State.getExegesisTarget().createSnippetGenerator(BenchmarkMode, State); |
Clement Courbet | 78b2e73 | 2018-09-25 07:31:44 +0000 | [diff] [blame^] | 146 | if (!Generator) |
Clement Courbet | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame] | 147 | llvm::report_fatal_error("cannot create snippet generator"); |
Clement Courbet | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame] | 148 | |
| 149 | const llvm::MCInstrDesc &InstrDesc = State.getInstrInfo().get(Opcode); |
| 150 | // Ignore instructions that we cannot run. |
| 151 | if (InstrDesc.isPseudo()) |
| 152 | return llvm::make_error<BenchmarkFailure>("Unsupported opcode: isPseudo"); |
| 153 | if (InstrDesc.isBranch() || InstrDesc.isIndirectBranch()) |
| 154 | return llvm::make_error<BenchmarkFailure>( |
| 155 | "Unsupported opcode: isBranch/isIndirectBranch"); |
| 156 | if (InstrDesc.isCall() || InstrDesc.isReturn()) |
| 157 | return llvm::make_error<BenchmarkFailure>( |
| 158 | "Unsupported opcode: isCall/isReturn"); |
| 159 | |
| 160 | return Generator->generateConfigurations(Opcode); |
| 161 | } |
| 162 | |
Clement Courbet | 78b2e73 | 2018-09-25 07:31:44 +0000 | [diff] [blame^] | 163 | namespace { |
| 164 | |
| 165 | // An MCStreamer that reads a BenchmarkCode definition from a file. |
| 166 | // The BenchmarkCode definition is just an asm file, with additional comments to |
| 167 | // specify which registers should be defined or are live on entry. |
| 168 | class BenchmarkCodeStreamer : public llvm::MCStreamer, |
| 169 | public llvm::AsmCommentConsumer { |
| 170 | public: |
| 171 | explicit BenchmarkCodeStreamer(llvm::MCContext *Context, |
| 172 | const llvm::MCRegisterInfo *TheRegInfo, |
| 173 | BenchmarkCode *Result) |
| 174 | : llvm::MCStreamer(*Context), RegInfo(TheRegInfo), Result(Result) {} |
| 175 | |
| 176 | // Implementation of the llvm::MCStreamer interface. We only care about |
| 177 | // instructions. |
| 178 | void EmitInstruction(const llvm::MCInst &instruction, |
| 179 | const llvm::MCSubtargetInfo &mc_subtarget_info, |
| 180 | bool PrintSchedInfo) override { |
| 181 | Result->Instructions.push_back(instruction); |
| 182 | } |
| 183 | |
| 184 | // Implementation of the llvm::AsmCommentConsumer. |
| 185 | void HandleComment(llvm::SMLoc Loc, llvm::StringRef CommentText) override { |
| 186 | CommentText = CommentText.trim(); |
| 187 | if (!CommentText.consume_front("LLVM-EXEGESIS-")) |
| 188 | return; |
| 189 | if (CommentText.consume_front("DEFREG")) { |
| 190 | // LLVM-EXEGESIS-DEFREF <reg> <hex_value> |
| 191 | RegisterValue RegVal; |
| 192 | llvm::SmallVector<llvm::StringRef, 2> Parts; |
| 193 | CommentText.split(Parts, ' ', /*unlimited splits*/ -1, |
| 194 | /*do not keep empty strings*/ false); |
| 195 | if (Parts.size() != 2) { |
| 196 | llvm::errs() << "invalid comment 'LLVM-EXEGESIS-DEFREG " << CommentText |
| 197 | << "\n"; |
| 198 | ++InvalidComments; |
| 199 | } |
| 200 | if (!(RegVal.Register = findRegisterByName(Parts[0].trim()))) { |
| 201 | llvm::errs() << "unknown register in 'LLVM-EXEGESIS-DEFREG " |
| 202 | << CommentText << "\n"; |
| 203 | ++InvalidComments; |
| 204 | return; |
| 205 | } |
| 206 | const llvm::StringRef HexValue = Parts[1].trim(); |
| 207 | RegVal.Value = llvm::APInt( |
| 208 | /* each hex digit is 4 bits */ HexValue.size() * 4, HexValue, 16); |
| 209 | Result->RegisterInitialValues.push_back(std::move(RegVal)); |
| 210 | return; |
| 211 | } |
| 212 | if (CommentText.consume_front("LIVEIN")) { |
| 213 | // LLVM-EXEGESIS-LIVEIN <reg> |
| 214 | if (unsigned Reg = findRegisterByName(CommentText.ltrim())) |
| 215 | Result->LiveIns.push_back(Reg); |
| 216 | else { |
| 217 | llvm::errs() << "unknown register in 'LLVM-EXEGESIS-LIVEIN " |
| 218 | << CommentText << "\n"; |
| 219 | ++InvalidComments; |
| 220 | } |
| 221 | return; |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | unsigned numInvalidComments() const { return InvalidComments; } |
| 226 | |
| 227 | private: |
| 228 | // We only care about instructions, we don't implement this part of the API. |
| 229 | void EmitCommonSymbol(llvm::MCSymbol *symbol, uint64_t size, |
| 230 | unsigned byte_alignment) override {} |
| 231 | bool EmitSymbolAttribute(llvm::MCSymbol *symbol, |
| 232 | llvm::MCSymbolAttr attribute) override { |
| 233 | return false; |
| 234 | } |
| 235 | void EmitValueToAlignment(unsigned byte_alignment, int64_t value, |
| 236 | unsigned value_size, |
| 237 | unsigned max_bytes_to_emit) override {} |
| 238 | void EmitZerofill(llvm::MCSection *section, llvm::MCSymbol *symbol, |
| 239 | uint64_t size, unsigned byte_alignment, |
| 240 | llvm::SMLoc Loc) override {} |
| 241 | |
| 242 | unsigned findRegisterByName(const llvm::StringRef RegName) const { |
| 243 | // FIXME: Can we do better than this ? |
| 244 | for (unsigned I = 0, E = RegInfo->getNumRegs(); I < E; ++I) { |
| 245 | if (RegName == RegInfo->getName(I)) |
| 246 | return I; |
| 247 | } |
| 248 | llvm::errs() << "'" << RegName |
| 249 | << "' is not a valid register name for the target\n"; |
| 250 | return 0; |
| 251 | } |
| 252 | |
| 253 | const llvm::MCRegisterInfo *const RegInfo; |
| 254 | BenchmarkCode *const Result; |
| 255 | unsigned InvalidComments = 0; |
| 256 | }; |
| 257 | |
| 258 | } // namespace |
| 259 | |
| 260 | // Reads code snippets from file `Filename`. |
| 261 | static llvm::Expected<std::vector<BenchmarkCode>> |
| 262 | readSnippets(const LLVMState &State, llvm::StringRef Filename) { |
| 263 | llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> BufferPtr = |
| 264 | llvm::MemoryBuffer::getFileOrSTDIN(Filename); |
| 265 | if (std::error_code EC = BufferPtr.getError()) { |
| 266 | return llvm::make_error<BenchmarkFailure>( |
| 267 | "cannot read snippet: " + Filename + ": " + EC.message()); |
| 268 | } |
| 269 | llvm::SourceMgr SM; |
| 270 | SM.AddNewSourceBuffer(std::move(BufferPtr.get()), llvm::SMLoc()); |
| 271 | |
| 272 | BenchmarkCode Result; |
| 273 | |
| 274 | llvm::MCObjectFileInfo ObjectFileInfo; |
| 275 | const llvm::TargetMachine &TM = State.getTargetMachine(); |
| 276 | llvm::MCContext Context(TM.getMCAsmInfo(), TM.getMCRegisterInfo(), |
| 277 | &ObjectFileInfo); |
| 278 | ObjectFileInfo.InitMCObjectFileInfo(TM.getTargetTriple(), /*PIC*/ false, |
| 279 | Context); |
| 280 | BenchmarkCodeStreamer Streamer(&Context, TM.getMCRegisterInfo(), &Result); |
| 281 | const std::unique_ptr<llvm::MCAsmParser> AsmParser( |
| 282 | llvm::createMCAsmParser(SM, Context, Streamer, *TM.getMCAsmInfo())); |
| 283 | if (!AsmParser) |
| 284 | return llvm::make_error<BenchmarkFailure>("cannot create asm parser"); |
| 285 | AsmParser->getLexer().setCommentConsumer(&Streamer); |
| 286 | |
| 287 | const std::unique_ptr<llvm::MCTargetAsmParser> TargetAsmParser( |
| 288 | TM.getTarget().createMCAsmParser(*TM.getMCSubtargetInfo(), *AsmParser, |
| 289 | *TM.getMCInstrInfo(), |
| 290 | llvm::MCTargetOptions())); |
| 291 | |
| 292 | if (!TargetAsmParser) |
| 293 | return llvm::make_error<BenchmarkFailure>( |
| 294 | "cannot create target asm parser"); |
| 295 | AsmParser->setTargetParser(*TargetAsmParser); |
| 296 | |
| 297 | if (AsmParser->Run(false)) |
| 298 | return llvm::make_error<BenchmarkFailure>("cannot parse asm file"); |
| 299 | if (Streamer.numInvalidComments()) |
| 300 | return llvm::make_error<BenchmarkFailure>( |
| 301 | llvm::Twine("found ") |
| 302 | .concat(llvm::Twine(Streamer.numInvalidComments())) |
| 303 | .concat(" invalid LLVM-EXEGESIS comments")); |
| 304 | return std::vector<BenchmarkCode>{std::move(Result)}; |
| 305 | } |
| 306 | |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 307 | void benchmarkMain() { |
| 308 | if (exegesis::pfm::pfmInitialize()) |
| 309 | llvm::report_fatal_error("cannot initialize libpfm"); |
| 310 | |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 311 | llvm::InitializeNativeTarget(); |
| 312 | llvm::InitializeNativeTargetAsmPrinter(); |
Clement Courbet | 78b2e73 | 2018-09-25 07:31:44 +0000 | [diff] [blame^] | 313 | llvm::InitializeNativeTargetAsmParser(); |
Clement Courbet | 44b4c54 | 2018-06-19 11:28:59 +0000 | [diff] [blame] | 314 | #ifdef LLVM_EXEGESIS_INITIALIZE_NATIVE_TARGET |
| 315 | LLVM_EXEGESIS_INITIALIZE_NATIVE_TARGET(); |
| 316 | #endif |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 317 | |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 318 | const LLVMState State; |
Clement Courbet | 78b2e73 | 2018-09-25 07:31:44 +0000 | [diff] [blame^] | 319 | const auto Opcode = getOpcodeOrDie(State.getInstrInfo()); |
Clement Courbet | e752fd6 | 2018-06-18 11:27:47 +0000 | [diff] [blame] | 320 | |
Clement Courbet | 78b2e73 | 2018-09-25 07:31:44 +0000 | [diff] [blame^] | 321 | std::vector<BenchmarkCode> Configurations; |
| 322 | if (Opcode > 0) { |
| 323 | // Ignore instructions without a sched class if -ignore-invalid-sched-class |
| 324 | // is passed. |
| 325 | if (IgnoreInvalidSchedClass && |
| 326 | State.getInstrInfo().get(Opcode).getSchedClass() == 0) { |
| 327 | llvm::errs() << "ignoring instruction without sched class\n"; |
| 328 | return; |
| 329 | } |
| 330 | Configurations = ExitOnErr(generateSnippets(State, Opcode)); |
| 331 | } else { |
| 332 | Configurations = ExitOnErr(readSnippets(State, SnippetsFile)); |
Clement Courbet | e752fd6 | 2018-06-18 11:27:47 +0000 | [diff] [blame] | 333 | } |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 334 | |
Clement Courbet | 4860b98 | 2018-06-26 08:49:30 +0000 | [diff] [blame] | 335 | const std::unique_ptr<BenchmarkRunner> Runner = |
| 336 | State.getExegesisTarget().createBenchmarkRunner(BenchmarkMode, State); |
| 337 | if (!Runner) { |
| 338 | llvm::report_fatal_error("cannot create benchmark runner"); |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 339 | } |
| 340 | |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 341 | if (NumRepetitions == 0) |
| 342 | llvm::report_fatal_error("--num-repetitions must be greater than zero"); |
| 343 | |
Guillaume Chatelet | 8c91d4c | 2018-06-07 07:51:16 +0000 | [diff] [blame] | 344 | // Write to standard output if file is not set. |
| 345 | if (BenchmarkFile.empty()) |
| 346 | BenchmarkFile = "-"; |
| 347 | |
Guillaume Chatelet | b4f1582 | 2018-06-07 14:00:29 +0000 | [diff] [blame] | 348 | const BenchmarkResultContext Context = getBenchmarkResultContext(State); |
Guillaume Chatelet | b4f1582 | 2018-06-07 14:00:29 +0000 | [diff] [blame] | 349 | |
Clement Courbet | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame] | 350 | for (const BenchmarkCode &Conf : Configurations) { |
| 351 | InstructionBenchmark Result = |
| 352 | Runner->runConfiguration(Conf, NumRepetitions); |
| 353 | ExitOnErr(Result.writeYaml(Context, BenchmarkFile)); |
| 354 | } |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 355 | exegesis::pfm::pfmTerminate(); |
| 356 | } |
| 357 | |
Clement Courbet | cf21074 | 2018-05-17 13:41:28 +0000 | [diff] [blame] | 358 | // Prints the results of running analysis pass `Pass` to file `OutputFilename` |
| 359 | // if OutputFilename is non-empty. |
| 360 | template <typename Pass> |
| 361 | static void maybeRunAnalysis(const Analysis &Analyzer, const std::string &Name, |
Clement Courbet | 53d35d2 | 2018-06-05 10:56:19 +0000 | [diff] [blame] | 362 | const std::string &OutputFilename) { |
Clement Courbet | cf21074 | 2018-05-17 13:41:28 +0000 | [diff] [blame] | 363 | if (OutputFilename.empty()) |
| 364 | return; |
| 365 | if (OutputFilename != "-") { |
| 366 | llvm::errs() << "Printing " << Name << " results to file '" |
| 367 | << OutputFilename << "'\n"; |
| 368 | } |
| 369 | std::error_code ErrorCode; |
| 370 | llvm::raw_fd_ostream ClustersOS(OutputFilename, ErrorCode, |
Zachary Turner | 1f67a3c | 2018-06-07 19:58:58 +0000 | [diff] [blame] | 371 | llvm::sys::fs::FA_Read | |
| 372 | llvm::sys::fs::FA_Write); |
| 373 | if (ErrorCode) |
| 374 | llvm::report_fatal_error("cannot open out file: " + OutputFilename); |
| 375 | if (auto Err = Analyzer.run<Pass>(ClustersOS)) |
| 376 | llvm::report_fatal_error(std::move(Err)); |
Clement Courbet | cf21074 | 2018-05-17 13:41:28 +0000 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | static void analysisMain() { |
Guillaume Chatelet | 8c91d4c | 2018-06-07 07:51:16 +0000 | [diff] [blame] | 380 | if (BenchmarkFile.empty()) |
| 381 | llvm::report_fatal_error("--benchmarks-file must be set."); |
| 382 | |
Clement Courbet | 53d35d2 | 2018-06-05 10:56:19 +0000 | [diff] [blame] | 383 | llvm::InitializeNativeTarget(); |
| 384 | llvm::InitializeNativeTargetAsmPrinter(); |
Clement Courbet | 4273e1e | 2018-06-15 07:30:45 +0000 | [diff] [blame] | 385 | llvm::InitializeNativeTargetDisassembler(); |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 386 | // Read benchmarks. |
Clement Courbet | 53d35d2 | 2018-06-05 10:56:19 +0000 | [diff] [blame] | 387 | const LLVMState State; |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 388 | const std::vector<InstructionBenchmark> Points = |
Guillaume Chatelet | 8c91d4c | 2018-06-07 07:51:16 +0000 | [diff] [blame] | 389 | ExitOnErr(InstructionBenchmark::readYamls( |
| 390 | getBenchmarkResultContext(State), BenchmarkFile)); |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 391 | llvm::outs() << "Parsed " << Points.size() << " benchmark points\n"; |
| 392 | if (Points.empty()) { |
| 393 | llvm::errs() << "no benchmarks to analyze\n"; |
| 394 | return; |
| 395 | } |
| 396 | // FIXME: Check that all points have the same triple/cpu. |
| 397 | // FIXME: Merge points from several runs (latency and uops). |
| 398 | |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 399 | std::string Error; |
| 400 | const auto *TheTarget = |
| 401 | llvm::TargetRegistry::lookupTarget(Points[0].LLVMTriple, Error); |
| 402 | if (!TheTarget) { |
| 403 | llvm::errs() << "unknown target '" << Points[0].LLVMTriple << "'\n"; |
| 404 | return; |
| 405 | } |
Guillaume Chatelet | 6416592 | 2018-06-11 09:18:01 +0000 | [diff] [blame] | 406 | const auto Clustering = ExitOnErr(InstructionBenchmarkClustering::create( |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 407 | Points, AnalysisNumPoints, AnalysisEpsilon)); |
Clement Courbet | 6d6c1a9 | 2018-05-16 08:47:21 +0000 | [diff] [blame] | 408 | |
| 409 | const Analysis Analyzer(*TheTarget, Clustering); |
| 410 | |
Clement Courbet | cf21074 | 2018-05-17 13:41:28 +0000 | [diff] [blame] | 411 | maybeRunAnalysis<Analysis::PrintClusters>(Analyzer, "analysis clusters", |
| 412 | AnalysisClustersOutputFile); |
| 413 | maybeRunAnalysis<Analysis::PrintSchedClassInconsistencies>( |
| 414 | Analyzer, "sched class consistency analysis", |
| 415 | AnalysisInconsistenciesOutputFile); |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | } // namespace exegesis |
| 419 | |
| 420 | int main(int Argc, char **Argv) { |
| 421 | llvm::cl::ParseCommandLineOptions(Argc, Argv, ""); |
| 422 | |
Guillaume Chatelet | 6416592 | 2018-06-11 09:18:01 +0000 | [diff] [blame] | 423 | exegesis::ExitOnErr.setExitCodeMapper([](const llvm::Error &Err) { |
| 424 | if (Err.isA<llvm::StringError>()) |
| 425 | return EXIT_SUCCESS; |
| 426 | return EXIT_FAILURE; |
| 427 | }); |
| 428 | |
Clement Courbet | 4860b98 | 2018-06-26 08:49:30 +0000 | [diff] [blame] | 429 | if (BenchmarkMode == exegesis::InstructionBenchmark::Unknown) { |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 430 | exegesis::analysisMain(); |
| 431 | } else { |
| 432 | exegesis::benchmarkMain(); |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 433 | } |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 434 | return EXIT_SUCCESS; |
| 435 | } |