1 //===-- llvm-exegesis.cpp ---------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 ///
9 /// \file
10 /// Measures execution properties (latencies/uops) of an instruction.
11 ///
12 //===----------------------------------------------------------------------===//
13 
14 #include "lib/Analysis.h"
15 #include "lib/BenchmarkResult.h"
16 #include "lib/BenchmarkRunner.h"
17 #include "lib/Clustering.h"
18 #include "lib/Error.h"
19 #include "lib/LlvmState.h"
20 #include "lib/PerfHelper.h"
21 #include "lib/SnippetFile.h"
22 #include "lib/SnippetRepetitor.h"
23 #include "lib/Target.h"
24 #include "lib/TargetSelect.h"
25 #include "llvm/ADT/StringExtras.h"
26 #include "llvm/ADT/Twine.h"
27 #include "llvm/MC/MCInstBuilder.h"
28 #include "llvm/MC/MCObjectFileInfo.h"
29 #include "llvm/MC/MCParser/MCAsmParser.h"
30 #include "llvm/MC/MCParser/MCTargetAsmParser.h"
31 #include "llvm/MC/MCRegisterInfo.h"
32 #include "llvm/MC/MCSubtargetInfo.h"
33 #include "llvm/Object/ObjectFile.h"
34 #include "llvm/Support/CommandLine.h"
35 #include "llvm/Support/FileSystem.h"
36 #include "llvm/Support/Format.h"
37 #include "llvm/Support/Path.h"
38 #include "llvm/Support/SourceMgr.h"
39 #include "llvm/Support/TargetRegistry.h"
40 #include "llvm/Support/TargetSelect.h"
41 #include <algorithm>
42 #include <string>
43 
44 namespace llvm {
45 namespace exegesis {
46 
47 static cl::OptionCategory Options("llvm-exegesis options");
48 static cl::OptionCategory BenchmarkOptions("llvm-exegesis benchmark options");
49 static cl::OptionCategory AnalysisOptions("llvm-exegesis analysis options");
50 
51 static cl::opt<int> OpcodeIndex(
52     "opcode-index",
53     cl::desc("opcode to measure, by index, or -1 to measure all opcodes"),
54     cl::cat(BenchmarkOptions), cl::init(0));
55 
56 static cl::opt<std::string>
57     OpcodeNames("opcode-name",
58                 cl::desc("comma-separated list of opcodes to measure, by name"),
59                 cl::cat(BenchmarkOptions), cl::init(""));
60 
61 static cl::opt<std::string> SnippetsFile("snippets-file",
62                                          cl::desc("code snippets to measure"),
63                                          cl::cat(BenchmarkOptions),
64                                          cl::init(""));
65 
66 static cl::opt<std::string>
67     BenchmarkFile("benchmarks-file",
68                   cl::desc("File to read (analysis mode) or write "
69                            "(latency/uops/inverse_throughput modes) benchmark "
70                            "results. “-” uses stdin/stdout."),
71                   cl::cat(Options), cl::init(""));
72 
73 static cl::opt<exegesis::InstructionBenchmark::ModeE> BenchmarkMode(
74     "mode", cl::desc("the mode to run"), cl::cat(Options),
75     cl::values(clEnumValN(exegesis::InstructionBenchmark::Latency, "latency",
76                           "Instruction Latency"),
77                clEnumValN(exegesis::InstructionBenchmark::InverseThroughput,
78                           "inverse_throughput",
79                           "Instruction Inverse Throughput"),
80                clEnumValN(exegesis::InstructionBenchmark::Uops, "uops",
81                           "Uop Decomposition"),
82                // When not asking for a specific benchmark mode,
83                // we'll analyse the results.
84                clEnumValN(exegesis::InstructionBenchmark::Unknown, "analysis",
85                           "Analysis")));
86 
87 static cl::opt<exegesis::InstructionBenchmark::ResultAggregationModeE>
88     ResultAggMode(
89         "result-aggregation-mode",
90         cl::desc("How to aggregate multi-values result"), cl::cat(Options),
91         cl::values(clEnumValN(exegesis::InstructionBenchmark::Min, "min",
92                               "Keep min reading"),
93                    clEnumValN(exegesis::InstructionBenchmark::Max, "max",
94                               "Keep max reading"),
95                    clEnumValN(exegesis::InstructionBenchmark::Mean, "mean",
96                               "Compute mean of all readings"),
97                    clEnumValN(exegesis::InstructionBenchmark::MinVariance,
98                               "min-variance",
99                               "Keep readings set with min-variance")),
100         cl::init(exegesis::InstructionBenchmark::Min));
101 
102 static cl::opt<exegesis::InstructionBenchmark::RepetitionModeE> RepetitionMode(
103     "repetition-mode", cl::desc("how to repeat the instruction snippet"),
104     cl::cat(BenchmarkOptions),
105     cl::values(
106         clEnumValN(exegesis::InstructionBenchmark::Duplicate, "duplicate",
107                    "Duplicate the snippet"),
108         clEnumValN(exegesis::InstructionBenchmark::Loop, "loop",
109                    "Loop over the snippet"),
110         clEnumValN(exegesis::InstructionBenchmark::AggregateMin, "min",
111                    "All of the above and take the minimum of measurements")),
112     cl::init(exegesis::InstructionBenchmark::Duplicate));
113 
114 static cl::opt<unsigned>
115     NumRepetitions("num-repetitions",
116                    cl::desc("number of time to repeat the asm snippet"),
117                    cl::cat(BenchmarkOptions), cl::init(10000));
118 
119 static cl::opt<unsigned> MaxConfigsPerOpcode(
120     "max-configs-per-opcode",
121     cl::desc(
122         "allow to snippet generator to generate at most that many configs"),
123     cl::cat(BenchmarkOptions), cl::init(1));
124 
125 static cl::opt<bool> IgnoreInvalidSchedClass(
126     "ignore-invalid-sched-class",
127     cl::desc("ignore instructions that do not define a sched class"),
128     cl::cat(BenchmarkOptions), cl::init(false));
129 
130 static cl::opt<exegesis::InstructionBenchmarkClustering::ModeE>
131     AnalysisClusteringAlgorithm(
132         "analysis-clustering", cl::desc("the clustering algorithm to use"),
133         cl::cat(AnalysisOptions),
134         cl::values(clEnumValN(exegesis::InstructionBenchmarkClustering::Dbscan,
135                               "dbscan", "use DBSCAN/OPTICS algorithm"),
136                    clEnumValN(exegesis::InstructionBenchmarkClustering::Naive,
137                               "naive", "one cluster per opcode")),
138         cl::init(exegesis::InstructionBenchmarkClustering::Dbscan));
139 
140 static cl::opt<unsigned> AnalysisDbscanNumPoints(
141     "analysis-numpoints",
142     cl::desc("minimum number of points in an analysis cluster (dbscan only)"),
143     cl::cat(AnalysisOptions), cl::init(3));
144 
145 static cl::opt<float> AnalysisClusteringEpsilon(
146     "analysis-clustering-epsilon",
147     cl::desc("epsilon for benchmark point clustering"),
148     cl::cat(AnalysisOptions), cl::init(0.1));
149 
150 static cl::opt<float> AnalysisInconsistencyEpsilon(
151     "analysis-inconsistency-epsilon",
152     cl::desc("epsilon for detection of when the cluster is different from the "
153              "LLVM schedule profile values"),
154     cl::cat(AnalysisOptions), cl::init(0.1));
155 
156 static cl::opt<std::string>
157     AnalysisClustersOutputFile("analysis-clusters-output-file", cl::desc(""),
158                                cl::cat(AnalysisOptions), cl::init(""));
159 static cl::opt<std::string>
160     AnalysisInconsistenciesOutputFile("analysis-inconsistencies-output-file",
161                                       cl::desc(""), cl::cat(AnalysisOptions),
162                                       cl::init(""));
163 
164 static cl::opt<bool> AnalysisDisplayUnstableOpcodes(
165     "analysis-display-unstable-clusters",
166     cl::desc("if there is more than one benchmark for an opcode, said "
167              "benchmarks may end up not being clustered into the same cluster "
168              "if the measured performance characteristics are different. by "
169              "default all such opcodes are filtered out. this flag will "
170              "instead show only such unstable opcodes"),
171     cl::cat(AnalysisOptions), cl::init(false));
172 
173 static cl::opt<std::string> CpuName(
174     "mcpu",
175     cl::desc("cpu name to use for pfm counters, leave empty to autodetect"),
176     cl::cat(Options), cl::init(""));
177 
178 static cl::opt<bool>
179     DumpObjectToDisk("dump-object-to-disk",
180                      cl::desc("dumps the generated benchmark object to disk "
181                               "and prints a message to access it"),
182                      cl::cat(BenchmarkOptions), cl::init(true));
183 
184 static ExitOnError ExitOnErr("llvm-exegesis error: ");
185 
186 // Helper function that logs the error(s) and exits.
ExitWithError(ArgTs &&...Args)187 template <typename... ArgTs> static void ExitWithError(ArgTs &&... Args) {
188   ExitOnErr(make_error<Failure>(std::forward<ArgTs>(Args)...));
189 }
190 
191 // Check Err. If it's in a failure state log the file error(s) and exit.
ExitOnFileError(const Twine & FileName,Error Err)192 static void ExitOnFileError(const Twine &FileName, Error Err) {
193   if (Err) {
194     ExitOnErr(createFileError(FileName, std::move(Err)));
195   }
196 }
197 
198 // Check E. If it's in a success state then return the contained value.
199 // If it's in a failure state log the file error(s) and exit.
200 template <typename T>
ExitOnFileError(const Twine & FileName,Expected<T> && E)201 T ExitOnFileError(const Twine &FileName, Expected<T> &&E) {
202   ExitOnFileError(FileName, E.takeError());
203   return std::move(*E);
204 }
205 
206 // Checks that only one of OpcodeNames, OpcodeIndex or SnippetsFile is provided,
207 // and returns the opcode indices or {} if snippets should be read from
208 // `SnippetsFile`.
getOpcodesOrDie(const MCInstrInfo & MCInstrInfo)209 static std::vector<unsigned> getOpcodesOrDie(const MCInstrInfo &MCInstrInfo) {
210   const size_t NumSetFlags = (OpcodeNames.empty() ? 0 : 1) +
211                              (OpcodeIndex == 0 ? 0 : 1) +
212                              (SnippetsFile.empty() ? 0 : 1);
213   if (NumSetFlags != 1) {
214     ExitOnErr.setBanner("llvm-exegesis: ");
215     ExitWithError("please provide one and only one of 'opcode-index', "
216                   "'opcode-name' or 'snippets-file'");
217   }
218   if (!SnippetsFile.empty())
219     return {};
220   if (OpcodeIndex > 0)
221     return {static_cast<unsigned>(OpcodeIndex)};
222   if (OpcodeIndex < 0) {
223     std::vector<unsigned> Result;
224     for (unsigned I = 1, E = MCInstrInfo.getNumOpcodes(); I < E; ++I)
225       Result.push_back(I);
226     return Result;
227   }
228   // Resolve opcode name -> opcode.
229   const auto ResolveName = [&MCInstrInfo](StringRef OpcodeName) -> unsigned {
230     for (unsigned I = 1, E = MCInstrInfo.getNumOpcodes(); I < E; ++I)
231       if (MCInstrInfo.getName(I) == OpcodeName)
232         return I;
233     return 0u;
234   };
235   SmallVector<StringRef, 2> Pieces;
236   StringRef(OpcodeNames.getValue())
237       .split(Pieces, ",", /* MaxSplit */ -1, /* KeepEmpty */ false);
238   std::vector<unsigned> Result;
239   for (const StringRef &OpcodeName : Pieces) {
240     if (unsigned Opcode = ResolveName(OpcodeName))
241       Result.push_back(Opcode);
242     else
243       ExitWithError(Twine("unknown opcode ").concat(OpcodeName));
244   }
245   return Result;
246 }
247 
248 // Generates code snippets for opcode `Opcode`.
249 static Expected<std::vector<BenchmarkCode>>
generateSnippets(const LLVMState & State,unsigned Opcode,const BitVector & ForbiddenRegs)250 generateSnippets(const LLVMState &State, unsigned Opcode,
251                  const BitVector &ForbiddenRegs) {
252   const Instruction &Instr = State.getIC().getInstr(Opcode);
253   const MCInstrDesc &InstrDesc = Instr.Description;
254   // Ignore instructions that we cannot run.
255   if (InstrDesc.isPseudo() || InstrDesc.usesCustomInsertionHook())
256     return make_error<Failure>(
257         "Unsupported opcode: isPseudo/usesCustomInserter");
258   if (InstrDesc.isBranch() || InstrDesc.isIndirectBranch())
259     return make_error<Failure>("Unsupported opcode: isBranch/isIndirectBranch");
260   if (InstrDesc.isCall() || InstrDesc.isReturn())
261     return make_error<Failure>("Unsupported opcode: isCall/isReturn");
262 
263   const std::vector<InstructionTemplate> InstructionVariants =
264       State.getExegesisTarget().generateInstructionVariants(
265           Instr, MaxConfigsPerOpcode);
266 
267   SnippetGenerator::Options SnippetOptions;
268   SnippetOptions.MaxConfigsPerOpcode = MaxConfigsPerOpcode;
269   const std::unique_ptr<SnippetGenerator> Generator =
270       State.getExegesisTarget().createSnippetGenerator(BenchmarkMode, State,
271                                                        SnippetOptions);
272   if (!Generator)
273     ExitWithError("cannot create snippet generator");
274 
275   std::vector<BenchmarkCode> Benchmarks;
276   for (const InstructionTemplate &Variant : InstructionVariants) {
277     if (Benchmarks.size() >= MaxConfigsPerOpcode)
278       break;
279     if (auto Err = Generator->generateConfigurations(Variant, Benchmarks,
280                                                      ForbiddenRegs))
281       return std::move(Err);
282   }
283   return Benchmarks;
284 }
285 
benchmarkMain()286 void benchmarkMain() {
287 #ifndef HAVE_LIBPFM
288   ExitWithError("benchmarking unavailable, LLVM was built without libpfm.");
289 #endif
290 
291   if (exegesis::pfm::pfmInitialize())
292     ExitWithError("cannot initialize libpfm");
293 
294   InitializeNativeTarget();
295   InitializeNativeTargetAsmPrinter();
296   InitializeNativeTargetAsmParser();
297   InitializeNativeExegesisTarget();
298 
299   const LLVMState State(CpuName);
300 
301   // Preliminary check to ensure features needed for requested
302   // benchmark mode are present on target CPU and/or OS.
303   ExitOnErr(State.getExegesisTarget().checkFeatureSupport());
304 
305   const std::unique_ptr<BenchmarkRunner> Runner =
306       ExitOnErr(State.getExegesisTarget().createBenchmarkRunner(
307           BenchmarkMode, State, ResultAggMode));
308   if (!Runner) {
309     ExitWithError("cannot create benchmark runner");
310   }
311 
312   const auto Opcodes = getOpcodesOrDie(State.getInstrInfo());
313 
314   SmallVector<std::unique_ptr<const SnippetRepetitor>, 2> Repetitors;
315   if (RepetitionMode != InstructionBenchmark::RepetitionModeE::AggregateMin)
316     Repetitors.emplace_back(SnippetRepetitor::Create(RepetitionMode, State));
317   else {
318     for (InstructionBenchmark::RepetitionModeE RepMode :
319          {InstructionBenchmark::RepetitionModeE::Duplicate,
320           InstructionBenchmark::RepetitionModeE::Loop})
321       Repetitors.emplace_back(SnippetRepetitor::Create(RepMode, State));
322   }
323 
324   BitVector AllReservedRegs;
325   llvm::for_each(Repetitors,
326                  [&AllReservedRegs](
327                      const std::unique_ptr<const SnippetRepetitor> &Repetitor) {
328                    AllReservedRegs |= Repetitor->getReservedRegs();
329                  });
330 
331   std::vector<BenchmarkCode> Configurations;
332   if (!Opcodes.empty()) {
333     for (const unsigned Opcode : Opcodes) {
334       // Ignore instructions without a sched class if
335       // -ignore-invalid-sched-class is passed.
336       if (IgnoreInvalidSchedClass &&
337           State.getInstrInfo().get(Opcode).getSchedClass() == 0) {
338         errs() << State.getInstrInfo().getName(Opcode)
339                << ": ignoring instruction without sched class\n";
340         continue;
341       }
342 
343       auto ConfigsForInstr = generateSnippets(State, Opcode, AllReservedRegs);
344       if (!ConfigsForInstr) {
345         logAllUnhandledErrors(
346             ConfigsForInstr.takeError(), errs(),
347             Twine(State.getInstrInfo().getName(Opcode)).concat(": "));
348         continue;
349       }
350       std::move(ConfigsForInstr->begin(), ConfigsForInstr->end(),
351                 std::back_inserter(Configurations));
352     }
353   } else {
354     Configurations = ExitOnErr(readSnippets(State, SnippetsFile));
355   }
356 
357   if (NumRepetitions == 0) {
358     ExitOnErr.setBanner("llvm-exegesis: ");
359     ExitWithError("--num-repetitions must be greater than zero");
360   }
361 
362   // Write to standard output if file is not set.
363   if (BenchmarkFile.empty())
364     BenchmarkFile = "-";
365 
366   for (const BenchmarkCode &Conf : Configurations) {
367     InstructionBenchmark Result = ExitOnErr(Runner->runConfiguration(
368         Conf, NumRepetitions, Repetitors, DumpObjectToDisk));
369     ExitOnFileError(BenchmarkFile, Result.writeYaml(State, BenchmarkFile));
370   }
371   exegesis::pfm::pfmTerminate();
372 }
373 
374 // Prints the results of running analysis pass `Pass` to file `OutputFilename`
375 // if OutputFilename is non-empty.
376 template <typename Pass>
maybeRunAnalysis(const Analysis & Analyzer,const std::string & Name,const std::string & OutputFilename)377 static void maybeRunAnalysis(const Analysis &Analyzer, const std::string &Name,
378                              const std::string &OutputFilename) {
379   if (OutputFilename.empty())
380     return;
381   if (OutputFilename != "-") {
382     errs() << "Printing " << Name << " results to file '" << OutputFilename
383            << "'\n";
384   }
385   std::error_code ErrorCode;
386   raw_fd_ostream ClustersOS(OutputFilename, ErrorCode,
387                             sys::fs::FA_Read | sys::fs::FA_Write);
388   if (ErrorCode)
389     ExitOnFileError(OutputFilename, errorCodeToError(ErrorCode));
390   if (auto Err = Analyzer.run<Pass>(ClustersOS))
391     ExitOnFileError(OutputFilename, std::move(Err));
392 }
393 
analysisMain()394 static void analysisMain() {
395   ExitOnErr.setBanner("llvm-exegesis: ");
396   if (BenchmarkFile.empty())
397     ExitWithError("--benchmarks-file must be set");
398 
399   if (AnalysisClustersOutputFile.empty() &&
400       AnalysisInconsistenciesOutputFile.empty()) {
401     ExitWithError(
402         "for --mode=analysis: At least one of --analysis-clusters-output-file "
403         "and --analysis-inconsistencies-output-file must be specified");
404   }
405 
406   InitializeNativeTarget();
407   InitializeNativeTargetAsmPrinter();
408   InitializeNativeTargetDisassembler();
409 
410   // Read benchmarks.
411   const LLVMState State("");
412   const std::vector<InstructionBenchmark> Points = ExitOnFileError(
413       BenchmarkFile, InstructionBenchmark::readYamls(State, BenchmarkFile));
414 
415   outs() << "Parsed " << Points.size() << " benchmark points\n";
416   if (Points.empty()) {
417     errs() << "no benchmarks to analyze\n";
418     return;
419   }
420   // FIXME: Check that all points have the same triple/cpu.
421   // FIXME: Merge points from several runs (latency and uops).
422 
423   std::string Error;
424   const auto *TheTarget =
425       TargetRegistry::lookupTarget(Points[0].LLVMTriple, Error);
426   if (!TheTarget) {
427     errs() << "unknown target '" << Points[0].LLVMTriple << "'\n";
428     return;
429   }
430 
431   std::unique_ptr<MCInstrInfo> InstrInfo(TheTarget->createMCInstrInfo());
432   assert(InstrInfo && "Unable to create instruction info!");
433 
434   const auto Clustering = ExitOnErr(InstructionBenchmarkClustering::create(
435       Points, AnalysisClusteringAlgorithm, AnalysisDbscanNumPoints,
436       AnalysisClusteringEpsilon, InstrInfo->getNumOpcodes()));
437 
438   const Analysis Analyzer(*TheTarget, std::move(InstrInfo), Clustering,
439                           AnalysisInconsistencyEpsilon,
440                           AnalysisDisplayUnstableOpcodes, CpuName);
441 
442   maybeRunAnalysis<Analysis::PrintClusters>(Analyzer, "analysis clusters",
443                                             AnalysisClustersOutputFile);
444   maybeRunAnalysis<Analysis::PrintSchedClassInconsistencies>(
445       Analyzer, "sched class consistency analysis",
446       AnalysisInconsistenciesOutputFile);
447 }
448 
449 } // namespace exegesis
450 } // namespace llvm
451 
main(int Argc,char ** Argv)452 int main(int Argc, char **Argv) {
453   using namespace llvm;
454   cl::ParseCommandLineOptions(Argc, Argv, "");
455 
456   exegesis::ExitOnErr.setExitCodeMapper([](const Error &Err) {
457     if (Err.isA<exegesis::ClusteringError>())
458       return EXIT_SUCCESS;
459     return EXIT_FAILURE;
460   });
461 
462   if (exegesis::BenchmarkMode == exegesis::InstructionBenchmark::Unknown) {
463     exegesis::analysisMain();
464   } else {
465     exegesis::benchmarkMain();
466   }
467   return EXIT_SUCCESS;
468 }
469