1 //===-- llvm-mca.cpp - Machine Code Analyzer -------------------*- 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 // This utility is a simple driver that allows static performance analysis on
10 // machine code similarly to how IACA (Intel Architecture Code Analyzer) works.
11 //
12 //   llvm-mca [options] <file-name>
13 //      -march <type>
14 //      -mcpu <cpu>
15 //      -o <file>
16 //
17 // The target defaults to the host target.
18 // The cpu defaults to the 'native' host cpu.
19 // The output defaults to standard output.
20 //
21 //===----------------------------------------------------------------------===//
22 
23 #include "CodeRegion.h"
24 #include "CodeRegionGenerator.h"
25 #include "PipelinePrinter.h"
26 #include "Views/BottleneckAnalysis.h"
27 #include "Views/DispatchStatistics.h"
28 #include "Views/InstructionInfoView.h"
29 #include "Views/RegisterFileStatistics.h"
30 #include "Views/ResourcePressureView.h"
31 #include "Views/RetireControlUnitStatistics.h"
32 #include "Views/SchedulerStatistics.h"
33 #include "Views/SummaryView.h"
34 #include "Views/TimelineView.h"
35 #include "llvm/MC/MCAsmInfo.h"
36 #include "llvm/MC/MCContext.h"
37 #include "llvm/MC/MCObjectFileInfo.h"
38 #include "llvm/MC/MCRegisterInfo.h"
39 #include "llvm/MCA/Context.h"
40 #include "llvm/MCA/Pipeline.h"
41 #include "llvm/MCA/Stages/EntryStage.h"
42 #include "llvm/MCA/Stages/InstructionTables.h"
43 #include "llvm/MCA/Support.h"
44 #include "llvm/Support/CommandLine.h"
45 #include "llvm/Support/ErrorHandling.h"
46 #include "llvm/Support/ErrorOr.h"
47 #include "llvm/Support/FileSystem.h"
48 #include "llvm/Support/Host.h"
49 #include "llvm/Support/InitLLVM.h"
50 #include "llvm/Support/MemoryBuffer.h"
51 #include "llvm/Support/SourceMgr.h"
52 #include "llvm/Support/TargetRegistry.h"
53 #include "llvm/Support/TargetSelect.h"
54 #include "llvm/Support/ToolOutputFile.h"
55 #include "llvm/Support/WithColor.h"
56 
57 using namespace llvm;
58 
59 static cl::OptionCategory ToolOptions("Tool Options");
60 static cl::OptionCategory ViewOptions("View Options");
61 
62 static cl::opt<std::string> InputFilename(cl::Positional,
63                                           cl::desc("<input file>"),
64                                           cl::cat(ToolOptions), cl::init("-"));
65 
66 static cl::opt<std::string> OutputFilename("o", cl::desc("Output filename"),
67                                            cl::init("-"), cl::cat(ToolOptions),
68                                            cl::value_desc("filename"));
69 
70 static cl::opt<std::string>
71     ArchName("march",
72              cl::desc("Target architecture. "
73                       "See -version for available targets"),
74              cl::cat(ToolOptions));
75 
76 static cl::opt<std::string>
77     TripleName("mtriple",
78                cl::desc("Target triple. See -version for available targets"),
79                cl::cat(ToolOptions));
80 
81 static cl::opt<std::string>
82     MCPU("mcpu",
83          cl::desc("Target a specific cpu type (-mcpu=help for details)"),
84          cl::value_desc("cpu-name"), cl::cat(ToolOptions), cl::init("native"));
85 
86 static cl::opt<int>
87     OutputAsmVariant("output-asm-variant",
88                      cl::desc("Syntax variant to use for output printing"),
89                      cl::cat(ToolOptions), cl::init(-1));
90 
91 static cl::opt<bool>
92     PrintImmHex("print-imm-hex", cl::cat(ToolOptions), cl::init(false),
93                 cl::desc("Prefer hex format when printing immediate values"));
94 
95 static cl::opt<unsigned> Iterations("iterations",
96                                     cl::desc("Number of iterations to run"),
97                                     cl::cat(ToolOptions), cl::init(0));
98 
99 static cl::opt<unsigned>
100     DispatchWidth("dispatch", cl::desc("Override the processor dispatch width"),
101                   cl::cat(ToolOptions), cl::init(0));
102 
103 static cl::opt<unsigned>
104     RegisterFileSize("register-file-size",
105                      cl::desc("Maximum number of physical registers which can "
106                               "be used for register mappings"),
107                      cl::cat(ToolOptions), cl::init(0));
108 
109 static cl::opt<unsigned>
110     MicroOpQueue("micro-op-queue-size", cl::Hidden,
111                  cl::desc("Number of entries in the micro-op queue"),
112                  cl::cat(ToolOptions), cl::init(0));
113 
114 static cl::opt<unsigned>
115     DecoderThroughput("decoder-throughput", cl::Hidden,
116                       cl::desc("Maximum throughput from the decoders "
117                                "(instructions per cycle)"),
118                       cl::cat(ToolOptions), cl::init(0));
119 
120 static cl::opt<bool>
121     PrintRegisterFileStats("register-file-stats",
122                            cl::desc("Print register file statistics"),
123                            cl::cat(ViewOptions), cl::init(false));
124 
125 static cl::opt<bool> PrintDispatchStats("dispatch-stats",
126                                         cl::desc("Print dispatch statistics"),
127                                         cl::cat(ViewOptions), cl::init(false));
128 
129 static cl::opt<bool>
130     PrintSummaryView("summary-view", cl::Hidden,
131                      cl::desc("Print summary view (enabled by default)"),
132                      cl::cat(ViewOptions), cl::init(true));
133 
134 static cl::opt<bool> PrintSchedulerStats("scheduler-stats",
135                                          cl::desc("Print scheduler statistics"),
136                                          cl::cat(ViewOptions), cl::init(false));
137 
138 static cl::opt<bool>
139     PrintRetireStats("retire-stats",
140                      cl::desc("Print retire control unit statistics"),
141                      cl::cat(ViewOptions), cl::init(false));
142 
143 static cl::opt<bool> PrintResourcePressureView(
144     "resource-pressure",
145     cl::desc("Print the resource pressure view (enabled by default)"),
146     cl::cat(ViewOptions), cl::init(true));
147 
148 static cl::opt<bool> PrintTimelineView("timeline",
149                                        cl::desc("Print the timeline view"),
150                                        cl::cat(ViewOptions), cl::init(false));
151 
152 static cl::opt<unsigned> TimelineMaxIterations(
153     "timeline-max-iterations",
154     cl::desc("Maximum number of iterations to print in timeline view"),
155     cl::cat(ViewOptions), cl::init(0));
156 
157 static cl::opt<unsigned> TimelineMaxCycles(
158     "timeline-max-cycles",
159     cl::desc(
160         "Maximum number of cycles in the timeline view. Defaults to 80 cycles"),
161     cl::cat(ViewOptions), cl::init(80));
162 
163 static cl::opt<bool>
164     AssumeNoAlias("noalias",
165                   cl::desc("If set, assume that loads and stores do not alias"),
166                   cl::cat(ToolOptions), cl::init(true));
167 
168 static cl::opt<unsigned> LoadQueueSize("lqueue",
169                                        cl::desc("Size of the load queue"),
170                                        cl::cat(ToolOptions), cl::init(0));
171 
172 static cl::opt<unsigned> StoreQueueSize("squeue",
173                                         cl::desc("Size of the store queue"),
174                                         cl::cat(ToolOptions), cl::init(0));
175 
176 static cl::opt<bool>
177     PrintInstructionTables("instruction-tables",
178                            cl::desc("Print instruction tables"),
179                            cl::cat(ToolOptions), cl::init(false));
180 
181 static cl::opt<bool> PrintInstructionInfoView(
182     "instruction-info",
183     cl::desc("Print the instruction info view (enabled by default)"),
184     cl::cat(ViewOptions), cl::init(true));
185 
186 static cl::opt<bool> EnableAllStats("all-stats",
187                                     cl::desc("Print all hardware statistics"),
188                                     cl::cat(ViewOptions), cl::init(false));
189 
190 static cl::opt<bool>
191     EnableAllViews("all-views",
192                    cl::desc("Print all views including hardware statistics"),
193                    cl::cat(ViewOptions), cl::init(false));
194 
195 static cl::opt<bool> EnableBottleneckAnalysis(
196     "bottleneck-analysis",
197     cl::desc("Enable bottleneck analysis (disabled by default)"),
198     cl::cat(ViewOptions), cl::init(false));
199 
200 namespace {
201 
202 const Target *getTarget(const char *ProgName) {
203   if (TripleName.empty())
204     TripleName = Triple::normalize(sys::getDefaultTargetTriple());
205   Triple TheTriple(TripleName);
206 
207   // Get the target specific parser.
208   std::string Error;
209   const Target *TheTarget =
210       TargetRegistry::lookupTarget(ArchName, TheTriple, Error);
211   if (!TheTarget) {
212     errs() << ProgName << ": " << Error;
213     return nullptr;
214   }
215 
216   // Return the found target.
217   return TheTarget;
218 }
219 
220 ErrorOr<std::unique_ptr<ToolOutputFile>> getOutputStream() {
221   if (OutputFilename == "")
222     OutputFilename = "-";
223   std::error_code EC;
224   auto Out =
225       llvm::make_unique<ToolOutputFile>(OutputFilename, EC, sys::fs::OF_None);
226   if (!EC)
227     return std::move(Out);
228   return EC;
229 }
230 } // end of anonymous namespace
231 
232 static void processOptionImpl(cl::opt<bool> &O, const cl::opt<bool> &Default) {
233   if (!O.getNumOccurrences() || O.getPosition() < Default.getPosition())
234     O = Default.getValue();
235 }
236 
237 static void processViewOptions() {
238   if (!EnableAllViews.getNumOccurrences() &&
239       !EnableAllStats.getNumOccurrences())
240     return;
241 
242   if (EnableAllViews.getNumOccurrences()) {
243     processOptionImpl(PrintSummaryView, EnableAllViews);
244     processOptionImpl(EnableBottleneckAnalysis, EnableAllViews);
245     processOptionImpl(PrintResourcePressureView, EnableAllViews);
246     processOptionImpl(PrintTimelineView, EnableAllViews);
247     processOptionImpl(PrintInstructionInfoView, EnableAllViews);
248   }
249 
250   const cl::opt<bool> &Default =
251       EnableAllViews.getPosition() < EnableAllStats.getPosition()
252           ? EnableAllStats
253           : EnableAllViews;
254   processOptionImpl(PrintRegisterFileStats, Default);
255   processOptionImpl(PrintDispatchStats, Default);
256   processOptionImpl(PrintSchedulerStats, Default);
257   processOptionImpl(PrintRetireStats, Default);
258 }
259 
260 // Returns true on success.
261 static bool runPipeline(mca::Pipeline &P) {
262   // Handle pipeline errors here.
263   Expected<unsigned> Cycles = P.run();
264   if (!Cycles) {
265     WithColor::error() << toString(Cycles.takeError());
266     return false;
267   }
268   return true;
269 }
270 
271 int main(int argc, char **argv) {
272   InitLLVM X(argc, argv);
273 
274   // Initialize targets and assembly parsers.
275   InitializeAllTargetInfos();
276   InitializeAllTargetMCs();
277   InitializeAllAsmParsers();
278 
279   // Enable printing of available targets when flag --version is specified.
280   cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
281 
282   cl::HideUnrelatedOptions({&ToolOptions, &ViewOptions});
283 
284   // Parse flags and initialize target options.
285   cl::ParseCommandLineOptions(argc, argv,
286                               "llvm machine code performance analyzer.\n");
287 
288   // Get the target from the triple. If a triple is not specified, then select
289   // the default triple for the host. If the triple doesn't correspond to any
290   // registered target, then exit with an error message.
291   const char *ProgName = argv[0];
292   const Target *TheTarget = getTarget(ProgName);
293   if (!TheTarget)
294     return 1;
295 
296   // GetTarget() may replaced TripleName with a default triple.
297   // For safety, reconstruct the Triple object.
298   Triple TheTriple(TripleName);
299 
300   ErrorOr<std::unique_ptr<MemoryBuffer>> BufferPtr =
301       MemoryBuffer::getFileOrSTDIN(InputFilename);
302   if (std::error_code EC = BufferPtr.getError()) {
303     WithColor::error() << InputFilename << ": " << EC.message() << '\n';
304     return 1;
305   }
306 
307   // Apply overrides to llvm-mca specific options.
308   processViewOptions();
309 
310   SourceMgr SrcMgr;
311 
312   // Tell SrcMgr about this buffer, which is what the parser will pick up.
313   SrcMgr.AddNewSourceBuffer(std::move(*BufferPtr), SMLoc());
314 
315   std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName));
316   assert(MRI && "Unable to create target register info!");
317 
318   std::unique_ptr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(*MRI, TripleName));
319   assert(MAI && "Unable to create target asm info!");
320 
321   MCObjectFileInfo MOFI;
322   MCContext Ctx(MAI.get(), MRI.get(), &MOFI, &SrcMgr);
323   MOFI.InitMCObjectFileInfo(TheTriple, /* PIC= */ false, Ctx);
324 
325   std::unique_ptr<buffer_ostream> BOS;
326 
327   std::unique_ptr<MCInstrInfo> MCII(TheTarget->createMCInstrInfo());
328 
329   std::unique_ptr<MCInstrAnalysis> MCIA(
330       TheTarget->createMCInstrAnalysis(MCII.get()));
331 
332   if (!MCPU.compare("native"))
333     MCPU = llvm::sys::getHostCPUName();
334 
335   std::unique_ptr<MCSubtargetInfo> STI(
336       TheTarget->createMCSubtargetInfo(TripleName, MCPU, /* FeaturesStr */ ""));
337   if (!STI->isCPUStringValid(MCPU))
338     return 1;
339 
340   if (!PrintInstructionTables && !STI->getSchedModel().isOutOfOrder()) {
341     WithColor::error() << "please specify an out-of-order cpu. '" << MCPU
342                        << "' is an in-order cpu.\n";
343     return 1;
344   }
345 
346   if (!STI->getSchedModel().hasInstrSchedModel()) {
347     WithColor::error()
348         << "unable to find instruction-level scheduling information for"
349         << " target triple '" << TheTriple.normalize() << "' and cpu '" << MCPU
350         << "'.\n";
351 
352     if (STI->getSchedModel().InstrItineraries)
353       WithColor::note()
354           << "cpu '" << MCPU << "' provides itineraries. However, "
355           << "instruction itineraries are currently unsupported.\n";
356     return 1;
357   }
358 
359   // Parse the input and create CodeRegions that llvm-mca can analyze.
360   mca::AsmCodeRegionGenerator CRG(*TheTarget, SrcMgr, Ctx, *MAI, *STI, *MCII);
361   Expected<const mca::CodeRegions &> RegionsOrErr = CRG.parseCodeRegions();
362   if (!RegionsOrErr) {
363     if (auto Err =
364             handleErrors(RegionsOrErr.takeError(), [](const StringError &E) {
365               WithColor::error() << E.getMessage() << '\n';
366             })) {
367       // Default case.
368       WithColor::error() << toString(std::move(Err)) << '\n';
369     }
370     return 1;
371   }
372   const mca::CodeRegions &Regions = *RegionsOrErr;
373 
374   // Early exit if errors were found by the code region parsing logic.
375   if (!Regions.isValid())
376     return 1;
377 
378   if (Regions.empty()) {
379     WithColor::error() << "no assembly instructions found.\n";
380     return 1;
381   }
382 
383   // Now initialize the output file.
384   auto OF = getOutputStream();
385   if (std::error_code EC = OF.getError()) {
386     WithColor::error() << EC.message() << '\n';
387     return 1;
388   }
389 
390   unsigned AssemblerDialect = CRG.getAssemblerDialect();
391   if (OutputAsmVariant >= 0)
392     AssemblerDialect = static_cast<unsigned>(OutputAsmVariant);
393   std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
394       Triple(TripleName), AssemblerDialect, *MAI, *MCII, *MRI));
395   if (!IP) {
396     WithColor::error()
397         << "unable to create instruction printer for target triple '"
398         << TheTriple.normalize() << "' with assembly variant "
399         << AssemblerDialect << ".\n";
400     return 1;
401   }
402 
403   // Set the display preference for hex vs. decimal immediates.
404   IP->setPrintImmHex(PrintImmHex);
405 
406   std::unique_ptr<ToolOutputFile> TOF = std::move(*OF);
407 
408   const MCSchedModel &SM = STI->getSchedModel();
409 
410   // Create an instruction builder.
411   mca::InstrBuilder IB(*STI, *MCII, *MRI, MCIA.get());
412 
413   // Create a context to control ownership of the pipeline hardware.
414   mca::Context MCA(*MRI, *STI);
415 
416   mca::PipelineOptions PO(MicroOpQueue, DecoderThroughput, DispatchWidth,
417                           RegisterFileSize, LoadQueueSize, StoreQueueSize,
418                           AssumeNoAlias, EnableBottleneckAnalysis);
419 
420   // Number each region in the sequence.
421   unsigned RegionIdx = 0;
422 
423   for (const std::unique_ptr<mca::CodeRegion> &Region : Regions) {
424     // Skip empty code regions.
425     if (Region->empty())
426       continue;
427 
428     // Don't print the header of this region if it is the default region, and
429     // it doesn't have an end location.
430     if (Region->startLoc().isValid() || Region->endLoc().isValid()) {
431       TOF->os() << "\n[" << RegionIdx++ << "] Code Region";
432       StringRef Desc = Region->getDescription();
433       if (!Desc.empty())
434         TOF->os() << " - " << Desc;
435       TOF->os() << "\n\n";
436     }
437 
438     // Lower the MCInst sequence into an mca::Instruction sequence.
439     ArrayRef<MCInst> Insts = Region->getInstructions();
440     std::vector<std::unique_ptr<mca::Instruction>> LoweredSequence;
441     for (const MCInst &MCI : Insts) {
442       Expected<std::unique_ptr<mca::Instruction>> Inst =
443           IB.createInstruction(MCI);
444       if (!Inst) {
445         if (auto NewE = handleErrors(
446                 Inst.takeError(),
447                 [&IP, &STI](const mca::InstructionError<MCInst> &IE) {
448                   std::string InstructionStr;
449                   raw_string_ostream SS(InstructionStr);
450                   WithColor::error() << IE.Message << '\n';
451                   IP->printInst(&IE.Inst, SS, "", *STI);
452                   SS.flush();
453                   WithColor::note()
454                       << "instruction: " << InstructionStr << '\n';
455                 })) {
456           // Default case.
457           WithColor::error() << toString(std::move(NewE));
458         }
459         return 1;
460       }
461 
462       LoweredSequence.emplace_back(std::move(Inst.get()));
463     }
464 
465     mca::SourceMgr S(LoweredSequence, PrintInstructionTables ? 1 : Iterations);
466 
467     if (PrintInstructionTables) {
468       //  Create a pipeline, stages, and a printer.
469       auto P = llvm::make_unique<mca::Pipeline>();
470       P->appendStage(llvm::make_unique<mca::EntryStage>(S));
471       P->appendStage(llvm::make_unique<mca::InstructionTables>(SM));
472       mca::PipelinePrinter Printer(*P);
473 
474       // Create the views for this pipeline, execute, and emit a report.
475       if (PrintInstructionInfoView) {
476         Printer.addView(llvm::make_unique<mca::InstructionInfoView>(
477             *STI, *MCII, Insts, *IP));
478       }
479       Printer.addView(
480           llvm::make_unique<mca::ResourcePressureView>(*STI, *IP, Insts));
481 
482       if (!runPipeline(*P))
483         return 1;
484 
485       Printer.printReport(TOF->os());
486       continue;
487     }
488 
489     // Create a basic pipeline simulating an out-of-order backend.
490     auto P = MCA.createDefaultPipeline(PO, IB, S);
491     mca::PipelinePrinter Printer(*P);
492 
493     if (PrintSummaryView)
494       Printer.addView(
495           llvm::make_unique<mca::SummaryView>(SM, Insts, DispatchWidth));
496 
497     if (EnableBottleneckAnalysis) {
498       Printer.addView(llvm::make_unique<mca::BottleneckAnalysis>(
499           *STI, *IP, Insts, S.getNumIterations()));
500     }
501 
502     if (PrintInstructionInfoView)
503       Printer.addView(
504           llvm::make_unique<mca::InstructionInfoView>(*STI, *MCII, Insts, *IP));
505 
506     if (PrintDispatchStats)
507       Printer.addView(llvm::make_unique<mca::DispatchStatistics>());
508 
509     if (PrintSchedulerStats)
510       Printer.addView(llvm::make_unique<mca::SchedulerStatistics>(*STI));
511 
512     if (PrintRetireStats)
513       Printer.addView(llvm::make_unique<mca::RetireControlUnitStatistics>(SM));
514 
515     if (PrintRegisterFileStats)
516       Printer.addView(llvm::make_unique<mca::RegisterFileStatistics>(*STI));
517 
518     if (PrintResourcePressureView)
519       Printer.addView(
520           llvm::make_unique<mca::ResourcePressureView>(*STI, *IP, Insts));
521 
522     if (PrintTimelineView) {
523       unsigned TimelineIterations =
524           TimelineMaxIterations ? TimelineMaxIterations : 10;
525       Printer.addView(llvm::make_unique<mca::TimelineView>(
526           *STI, *IP, Insts, std::min(TimelineIterations, S.getNumIterations()),
527           TimelineMaxCycles));
528     }
529 
530     if (!runPipeline(*P))
531       return 1;
532 
533     Printer.printReport(TOF->os());
534 
535     // Clear the InstrBuilder internal state in preparation for another round.
536     IB.clear();
537   }
538 
539   TOF->keep();
540   return 0;
541 }
542