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