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