[llvm-exegesis] Fix off by one error
[llvm-complete.git] / tools / llvm-exegesis / llvm-exegesis.cpp
blobbbc1c9ba28cfe7c60d1716ac806aa83a8ee9a1af
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 //===----------------------------------------------------------------------===//
15 #include "lib/Analysis.h"
16 #include "lib/BenchmarkResult.h"
17 #include "lib/BenchmarkRunner.h"
18 #include "lib/Clustering.h"
19 #include "lib/LlvmState.h"
20 #include "lib/PerfHelper.h"
21 #include "lib/Target.h"
22 #include "llvm/ADT/StringExtras.h"
23 #include "llvm/ADT/Twine.h"
24 #include "llvm/MC/MCInstBuilder.h"
25 #include "llvm/MC/MCObjectFileInfo.h"
26 #include "llvm/MC/MCParser/MCAsmParser.h"
27 #include "llvm/MC/MCParser/MCTargetAsmParser.h"
28 #include "llvm/MC/MCRegisterInfo.h"
29 #include "llvm/MC/MCStreamer.h"
30 #include "llvm/MC/MCSubtargetInfo.h"
31 #include "llvm/Object/ObjectFile.h"
32 #include "llvm/Support/CommandLine.h"
33 #include "llvm/Support/Format.h"
34 #include "llvm/Support/Path.h"
35 #include "llvm/Support/SourceMgr.h"
36 #include "llvm/Support/TargetRegistry.h"
37 #include "llvm/Support/TargetSelect.h"
38 #include <algorithm>
39 #include <string>
41 static llvm::cl::opt<int>
42 OpcodeIndex("opcode-index", llvm::cl::desc("opcode to measure, by index"),
43 llvm::cl::init(0));
45 static llvm::cl::opt<std::string> OpcodeNames(
46 "opcode-name",
47 llvm::cl::desc("comma-separated list of opcodes to measure, by name"),
48 llvm::cl::init(""));
50 static llvm::cl::opt<std::string>
51 SnippetsFile("snippets-file", llvm::cl::desc("code snippets to measure"),
52 llvm::cl::init(""));
54 static llvm::cl::opt<std::string>
55 BenchmarkFile("benchmarks-file", llvm::cl::desc(""), llvm::cl::init(""));
57 static llvm::cl::opt<exegesis::InstructionBenchmark::ModeE> BenchmarkMode(
58 "mode", llvm::cl::desc("the mode to run"),
59 llvm::cl::values(clEnumValN(exegesis::InstructionBenchmark::Latency,
60 "latency", "Instruction Latency"),
61 clEnumValN(exegesis::InstructionBenchmark::Uops, "uops",
62 "Uop Decomposition"),
63 // When not asking for a specific benchmark mode, we'll
64 // analyse the results.
65 clEnumValN(exegesis::InstructionBenchmark::Unknown,
66 "analysis", "Analysis")));
68 static llvm::cl::opt<unsigned>
69 NumRepetitions("num-repetitions",
70 llvm::cl::desc("number of time to repeat the asm snippet"),
71 llvm::cl::init(10000));
73 static llvm::cl::opt<bool> IgnoreInvalidSchedClass(
74 "ignore-invalid-sched-class",
75 llvm::cl::desc("ignore instructions that do not define a sched class"),
76 llvm::cl::init(false));
78 static llvm::cl::opt<unsigned> AnalysisNumPoints(
79 "analysis-numpoints",
80 llvm::cl::desc("minimum number of points in an analysis cluster"),
81 llvm::cl::init(3));
83 static llvm::cl::opt<float>
84 AnalysisEpsilon("analysis-epsilon",
85 llvm::cl::desc("dbscan epsilon for analysis clustering"),
86 llvm::cl::init(0.1));
88 static llvm::cl::opt<std::string>
89 AnalysisClustersOutputFile("analysis-clusters-output-file",
90 llvm::cl::desc(""), llvm::cl::init("-"));
91 static llvm::cl::opt<std::string>
92 AnalysisInconsistenciesOutputFile("analysis-inconsistencies-output-file",
93 llvm::cl::desc(""), llvm::cl::init("-"));
95 namespace exegesis {
97 static llvm::ExitOnError ExitOnErr;
99 #ifdef LLVM_EXEGESIS_INITIALIZE_NATIVE_TARGET
100 void LLVM_EXEGESIS_INITIALIZE_NATIVE_TARGET();
101 #endif
103 // Checks that only one of OpcodeNames, OpcodeIndex or SnippetsFile is provided,
104 // and returns the opcode indices or {} if snippets should be read from
105 // `SnippetsFile`.
106 static std::vector<unsigned>
107 getOpcodesOrDie(const llvm::MCInstrInfo &MCInstrInfo) {
108 const size_t NumSetFlags = (OpcodeNames.empty() ? 0 : 1) +
109 (OpcodeIndex == 0 ? 0 : 1) +
110 (SnippetsFile.empty() ? 0 : 1);
111 if (NumSetFlags != 1)
112 llvm::report_fatal_error(
113 "please provide one and only one of 'opcode-index', 'opcode-name' or "
114 "'snippets-file'");
115 if (!SnippetsFile.empty())
116 return {};
117 if (OpcodeIndex > 0)
118 return {static_cast<unsigned>(OpcodeIndex)};
119 if (OpcodeIndex < 0) {
120 std::vector<unsigned> Result;
121 for (unsigned I = 1, E = MCInstrInfo.getNumOpcodes(); I < E; ++I)
122 Result.push_back(I);
123 return Result;
125 // Resolve opcode name -> opcode.
126 const auto ResolveName =
127 [&MCInstrInfo](llvm::StringRef OpcodeName) -> unsigned {
128 for (unsigned I = 1, E = MCInstrInfo.getNumOpcodes(); I < E; ++I)
129 if (MCInstrInfo.getName(I) == OpcodeName)
130 return I;
131 return 0u;
133 llvm::SmallVector<llvm::StringRef, 2> Pieces;
134 llvm::StringRef(OpcodeNames.getValue())
135 .split(Pieces, ",", /* MaxSplit */ -1, /* KeepEmpty */ false);
136 std::vector<unsigned> Result;
137 for (const llvm::StringRef OpcodeName : Pieces) {
138 if (unsigned Opcode = ResolveName(OpcodeName))
139 Result.push_back(Opcode);
140 else
141 llvm::report_fatal_error(
142 llvm::Twine("unknown opcode ").concat(OpcodeName));
144 return Result;
147 // Generates code snippets for opcode `Opcode`.
148 static llvm::Expected<std::vector<BenchmarkCode>>
149 generateSnippets(const LLVMState &State, unsigned Opcode) {
150 const Instruction Instr(State, Opcode);
151 const llvm::MCInstrDesc &InstrDesc = *Instr.Description;
152 // Ignore instructions that we cannot run.
153 if (InstrDesc.isPseudo())
154 return llvm::make_error<BenchmarkFailure>("Unsupported opcode: isPseudo");
155 if (InstrDesc.isBranch() || InstrDesc.isIndirectBranch())
156 return llvm::make_error<BenchmarkFailure>(
157 "Unsupported opcode: isBranch/isIndirectBranch");
158 if (InstrDesc.isCall() || InstrDesc.isReturn())
159 return llvm::make_error<BenchmarkFailure>(
160 "Unsupported opcode: isCall/isReturn");
162 const std::unique_ptr<SnippetGenerator> Generator =
163 State.getExegesisTarget().createSnippetGenerator(BenchmarkMode, State);
164 if (!Generator)
165 llvm::report_fatal_error("cannot create snippet generator");
166 return Generator->generateConfigurations(Instr);
169 namespace {
171 // An MCStreamer that reads a BenchmarkCode definition from a file.
172 // The BenchmarkCode definition is just an asm file, with additional comments to
173 // specify which registers should be defined or are live on entry.
174 class BenchmarkCodeStreamer : public llvm::MCStreamer,
175 public llvm::AsmCommentConsumer {
176 public:
177 explicit BenchmarkCodeStreamer(llvm::MCContext *Context,
178 const llvm::MCRegisterInfo *TheRegInfo,
179 BenchmarkCode *Result)
180 : llvm::MCStreamer(*Context), RegInfo(TheRegInfo), Result(Result) {}
182 // Implementation of the llvm::MCStreamer interface. We only care about
183 // instructions.
184 void EmitInstruction(const llvm::MCInst &Instruction,
185 const llvm::MCSubtargetInfo &STI,
186 bool PrintSchedInfo) override {
187 Result->Instructions.push_back(Instruction);
190 // Implementation of the llvm::AsmCommentConsumer.
191 void HandleComment(llvm::SMLoc Loc, llvm::StringRef CommentText) override {
192 CommentText = CommentText.trim();
193 if (!CommentText.consume_front("LLVM-EXEGESIS-"))
194 return;
195 if (CommentText.consume_front("DEFREG")) {
196 // LLVM-EXEGESIS-DEFREF <reg> <hex_value>
197 RegisterValue RegVal;
198 llvm::SmallVector<llvm::StringRef, 2> Parts;
199 CommentText.split(Parts, ' ', /*unlimited splits*/ -1,
200 /*do not keep empty strings*/ false);
201 if (Parts.size() != 2) {
202 llvm::errs() << "invalid comment 'LLVM-EXEGESIS-DEFREG " << CommentText
203 << "\n";
204 ++InvalidComments;
206 if (!(RegVal.Register = findRegisterByName(Parts[0].trim()))) {
207 llvm::errs() << "unknown register in 'LLVM-EXEGESIS-DEFREG "
208 << CommentText << "\n";
209 ++InvalidComments;
210 return;
212 const llvm::StringRef HexValue = Parts[1].trim();
213 RegVal.Value = llvm::APInt(
214 /* each hex digit is 4 bits */ HexValue.size() * 4, HexValue, 16);
215 Result->RegisterInitialValues.push_back(std::move(RegVal));
216 return;
218 if (CommentText.consume_front("LIVEIN")) {
219 // LLVM-EXEGESIS-LIVEIN <reg>
220 if (unsigned Reg = findRegisterByName(CommentText.ltrim()))
221 Result->LiveIns.push_back(Reg);
222 else {
223 llvm::errs() << "unknown register in 'LLVM-EXEGESIS-LIVEIN "
224 << CommentText << "\n";
225 ++InvalidComments;
227 return;
231 unsigned numInvalidComments() const { return InvalidComments; }
233 private:
234 // We only care about instructions, we don't implement this part of the API.
235 void EmitCommonSymbol(llvm::MCSymbol *Symbol, uint64_t Size,
236 unsigned ByteAlignment) override {}
237 bool EmitSymbolAttribute(llvm::MCSymbol *Symbol,
238 llvm::MCSymbolAttr Attribute) override {
239 return false;
241 void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
242 unsigned ValueSize,
243 unsigned MaxBytesToEmit) override {}
244 void EmitZerofill(llvm::MCSection *Section, llvm::MCSymbol *Symbol,
245 uint64_t Size, unsigned ByteAlignment,
246 llvm::SMLoc Loc) override {}
248 unsigned findRegisterByName(const llvm::StringRef RegName) const {
249 // FIXME: Can we do better than this ?
250 for (unsigned I = 0, E = RegInfo->getNumRegs(); I < E; ++I) {
251 if (RegName == RegInfo->getName(I))
252 return I;
254 llvm::errs() << "'" << RegName
255 << "' is not a valid register name for the target\n";
256 return 0;
259 const llvm::MCRegisterInfo *const RegInfo;
260 BenchmarkCode *const Result;
261 unsigned InvalidComments = 0;
264 } // namespace
266 // Reads code snippets from file `Filename`.
267 static llvm::Expected<std::vector<BenchmarkCode>>
268 readSnippets(const LLVMState &State, llvm::StringRef Filename) {
269 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> BufferPtr =
270 llvm::MemoryBuffer::getFileOrSTDIN(Filename);
271 if (std::error_code EC = BufferPtr.getError()) {
272 return llvm::make_error<BenchmarkFailure>(
273 "cannot read snippet: " + Filename + ": " + EC.message());
275 llvm::SourceMgr SM;
276 SM.AddNewSourceBuffer(std::move(BufferPtr.get()), llvm::SMLoc());
278 BenchmarkCode Result;
280 llvm::MCObjectFileInfo ObjectFileInfo;
281 const llvm::TargetMachine &TM = State.getTargetMachine();
282 llvm::MCContext Context(TM.getMCAsmInfo(), TM.getMCRegisterInfo(),
283 &ObjectFileInfo);
284 ObjectFileInfo.InitMCObjectFileInfo(TM.getTargetTriple(), /*PIC*/ false,
285 Context);
286 BenchmarkCodeStreamer Streamer(&Context, TM.getMCRegisterInfo(), &Result);
287 const std::unique_ptr<llvm::MCAsmParser> AsmParser(
288 llvm::createMCAsmParser(SM, Context, Streamer, *TM.getMCAsmInfo()));
289 if (!AsmParser)
290 return llvm::make_error<BenchmarkFailure>("cannot create asm parser");
291 AsmParser->getLexer().setCommentConsumer(&Streamer);
293 const std::unique_ptr<llvm::MCTargetAsmParser> TargetAsmParser(
294 TM.getTarget().createMCAsmParser(*TM.getMCSubtargetInfo(), *AsmParser,
295 *TM.getMCInstrInfo(),
296 llvm::MCTargetOptions()));
298 if (!TargetAsmParser)
299 return llvm::make_error<BenchmarkFailure>(
300 "cannot create target asm parser");
301 AsmParser->setTargetParser(*TargetAsmParser);
303 if (AsmParser->Run(false))
304 return llvm::make_error<BenchmarkFailure>("cannot parse asm file");
305 if (Streamer.numInvalidComments())
306 return llvm::make_error<BenchmarkFailure>(
307 llvm::Twine("found ")
308 .concat(llvm::Twine(Streamer.numInvalidComments()))
309 .concat(" invalid LLVM-EXEGESIS comments"));
310 return std::vector<BenchmarkCode>{std::move(Result)};
313 void benchmarkMain() {
314 if (exegesis::pfm::pfmInitialize())
315 llvm::report_fatal_error("cannot initialize libpfm");
317 llvm::InitializeNativeTarget();
318 llvm::InitializeNativeTargetAsmPrinter();
319 llvm::InitializeNativeTargetAsmParser();
320 #ifdef LLVM_EXEGESIS_INITIALIZE_NATIVE_TARGET
321 LLVM_EXEGESIS_INITIALIZE_NATIVE_TARGET();
322 #endif
324 const LLVMState State;
325 const auto Opcodes = getOpcodesOrDie(State.getInstrInfo());
327 std::vector<BenchmarkCode> Configurations;
328 if (!Opcodes.empty()) {
329 for (const unsigned Opcode : Opcodes) {
330 // Ignore instructions without a sched class if
331 // -ignore-invalid-sched-class is passed.
332 if (IgnoreInvalidSchedClass &&
333 State.getInstrInfo().get(Opcode).getSchedClass() == 0) {
334 llvm::errs() << State.getInstrInfo().getName(Opcode)
335 << ": ignoring instruction without sched class\n";
336 continue;
338 auto ConfigsForInstr = generateSnippets(State, Opcode);
339 if (!ConfigsForInstr) {
340 llvm::logAllUnhandledErrors(
341 ConfigsForInstr.takeError(), llvm::errs(),
342 llvm::Twine(State.getInstrInfo().getName(Opcode)).concat(": "));
343 continue;
345 std::move(ConfigsForInstr->begin(), ConfigsForInstr->end(),
346 std::back_inserter(Configurations));
348 } else {
349 Configurations = ExitOnErr(readSnippets(State, SnippetsFile));
352 const std::unique_ptr<BenchmarkRunner> Runner =
353 State.getExegesisTarget().createBenchmarkRunner(BenchmarkMode, State);
354 if (!Runner) {
355 llvm::report_fatal_error("cannot create benchmark runner");
358 if (NumRepetitions == 0)
359 llvm::report_fatal_error("--num-repetitions must be greater than zero");
361 // Write to standard output if file is not set.
362 if (BenchmarkFile.empty())
363 BenchmarkFile = "-";
365 for (const BenchmarkCode &Conf : Configurations) {
366 InstructionBenchmark Result =
367 Runner->runConfiguration(Conf, NumRepetitions);
368 ExitOnErr(Result.writeYaml(State, BenchmarkFile));
370 exegesis::pfm::pfmTerminate();
373 // Prints the results of running analysis pass `Pass` to file `OutputFilename`
374 // if OutputFilename is non-empty.
375 template <typename Pass>
376 static void maybeRunAnalysis(const Analysis &Analyzer, const std::string &Name,
377 const std::string &OutputFilename) {
378 if (OutputFilename.empty())
379 return;
380 if (OutputFilename != "-") {
381 llvm::errs() << "Printing " << Name << " results to file '"
382 << OutputFilename << "'\n";
384 std::error_code ErrorCode;
385 llvm::raw_fd_ostream ClustersOS(OutputFilename, ErrorCode,
386 llvm::sys::fs::FA_Read |
387 llvm::sys::fs::FA_Write);
388 if (ErrorCode)
389 llvm::report_fatal_error("cannot open out file: " + OutputFilename);
390 if (auto Err = Analyzer.run<Pass>(ClustersOS))
391 llvm::report_fatal_error(std::move(Err));
394 static void analysisMain() {
395 if (BenchmarkFile.empty())
396 llvm::report_fatal_error("--benchmarks-file must be set.");
398 llvm::InitializeNativeTarget();
399 llvm::InitializeNativeTargetAsmPrinter();
400 llvm::InitializeNativeTargetDisassembler();
401 // Read benchmarks.
402 const LLVMState State;
403 const std::vector<InstructionBenchmark> Points =
404 ExitOnErr(InstructionBenchmark::readYamls(State, BenchmarkFile));
405 llvm::outs() << "Parsed " << Points.size() << " benchmark points\n";
406 if (Points.empty()) {
407 llvm::errs() << "no benchmarks to analyze\n";
408 return;
410 // FIXME: Check that all points have the same triple/cpu.
411 // FIXME: Merge points from several runs (latency and uops).
413 std::string Error;
414 const auto *TheTarget =
415 llvm::TargetRegistry::lookupTarget(Points[0].LLVMTriple, Error);
416 if (!TheTarget) {
417 llvm::errs() << "unknown target '" << Points[0].LLVMTriple << "'\n";
418 return;
420 const auto Clustering = ExitOnErr(InstructionBenchmarkClustering::create(
421 Points, AnalysisNumPoints, AnalysisEpsilon));
423 const Analysis Analyzer(*TheTarget, Clustering);
425 maybeRunAnalysis<Analysis::PrintClusters>(Analyzer, "analysis clusters",
426 AnalysisClustersOutputFile);
427 maybeRunAnalysis<Analysis::PrintSchedClassInconsistencies>(
428 Analyzer, "sched class consistency analysis",
429 AnalysisInconsistenciesOutputFile);
432 } // namespace exegesis
434 int main(int Argc, char **Argv) {
435 llvm::cl::ParseCommandLineOptions(Argc, Argv, "");
437 exegesis::ExitOnErr.setExitCodeMapper([](const llvm::Error &Err) {
438 if (Err.isA<llvm::StringError>())
439 return EXIT_SUCCESS;
440 return EXIT_FAILURE;
443 if (BenchmarkMode == exegesis::InstructionBenchmark::Unknown) {
444 exegesis::analysisMain();
445 } else {
446 exegesis::benchmarkMain();
448 return EXIT_SUCCESS;