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/MCSchedule.h"
42#include "llvm/MC/MCSubtargetInfo.h"
43#include "llvm/MC/MCTargetOptionsCommandFlags.h"
44#include "llvm/MC/TargetRegistry.h"
45#include "llvm/MCA/CodeEmitter.h"
46#include "llvm/MCA/Context.h"
47#include "llvm/MCA/CustomBehaviour.h"
48#include "llvm/MCA/InstrBuilder.h"
49#include "llvm/MCA/Pipeline.h"
50#include "llvm/MCA/Stages/EntryStage.h"
51#include "llvm/MCA/Stages/InstructionTables.h"
52#include "llvm/MCA/Support.h"
53#include "llvm/Support/CommandLine.h"
54#include "llvm/Support/ErrorHandling.h"
55#include "llvm/Support/ErrorOr.h"
56#include "llvm/Support/FileSystem.h"
57#include "llvm/Support/InitLLVM.h"
58#include "llvm/Support/MemoryBuffer.h"
59#include "llvm/Support/SourceMgr.h"
60#include "llvm/Support/TargetSelect.h"
61#include "llvm/Support/ToolOutputFile.h"
62#include "llvm/Support/WithColor.h"
63#include "llvm/TargetParser/Host.h"
64
65using namespace llvm;
66
67static mc::RegisterMCTargetOptionsFlags MOF;
68
69static cl::OptionCategory ToolOptions("Tool Options");
70static cl::OptionCategory ViewOptions("View Options");
71
72static cl::opt<std::string> InputFilename(cl::Positional,
73 cl::desc("<input file>"),
74 cl::cat(ToolOptions), cl::init(Val: "-"));
75
76static cl::opt<std::string> OutputFilename("o", cl::desc("Output filename"),
77 cl::init(Val: "-"), cl::cat(ToolOptions),
78 cl::value_desc("filename"));
79
80static cl::opt<std::string>
81 ArchName("march",
82 cl::desc("Target architecture. "
83 "See -version for available targets"),
84 cl::cat(ToolOptions));
85
86static cl::opt<std::string>
87 TripleNameOpt("mtriple",
88 cl::desc("Target triple. See -version for available targets"),
89 cl::cat(ToolOptions));
90
91static cl::opt<std::string>
92 MCPU("mcpu",
93 cl::desc("Target a specific cpu type (-mcpu=help for details)"),
94 cl::value_desc("cpu-name"), cl::cat(ToolOptions), cl::init(Val: "native"));
95
96static cl::list<std::string>
97 MATTRS("mattr", cl::CommaSeparated,
98 cl::desc("Target specific attributes (-mattr=help for details)"),
99 cl::value_desc("a1,+a2,-a3,..."), cl::cat(ToolOptions));
100
101static cl::opt<bool> PrintJson("json",
102 cl::desc("Print the output in json format"),
103 cl::cat(ToolOptions), cl::init(Val: false));
104
105static cl::opt<int>
106 OutputAsmVariant("output-asm-variant",
107 cl::desc("Syntax variant to use for output printing"),
108 cl::cat(ToolOptions), cl::init(Val: -1));
109
110static cl::opt<bool>
111 PrintImmHex("print-imm-hex", cl::cat(ToolOptions), cl::init(Val: false),
112 cl::desc("Prefer hex format when printing immediate values"));
113
114static cl::opt<unsigned> Iterations("iterations",
115 cl::desc("Number of iterations to run"),
116 cl::cat(ToolOptions), cl::init(Val: 0));
117
118static cl::opt<unsigned>
119 DispatchWidth("dispatch", cl::desc("Override the processor dispatch width"),
120 cl::cat(ToolOptions), cl::init(Val: 0));
121
122static cl::opt<unsigned>
123 RegisterFileSize("register-file-size",
124 cl::desc("Maximum number of physical registers which can "
125 "be used for register mappings"),
126 cl::cat(ToolOptions), cl::init(Val: 0));
127
128static cl::opt<unsigned>
129 MicroOpQueue("micro-op-queue-size", cl::Hidden,
130 cl::desc("Number of entries in the micro-op queue"),
131 cl::cat(ToolOptions), cl::init(Val: 0));
132
133static cl::opt<unsigned>
134 DecoderThroughput("decoder-throughput", cl::Hidden,
135 cl::desc("Maximum throughput from the decoders "
136 "(instructions per cycle)"),
137 cl::cat(ToolOptions), cl::init(Val: 0));
138
139static cl::opt<unsigned>
140 CallLatency("call-latency", cl::Hidden,
141 cl::desc("Number of cycles to assume for a call instruction"),
142 cl::cat(ToolOptions), cl::init(Val: 100U));
143
144enum class SkipType { NONE, LACK_SCHED, PARSE_FAILURE, ANY_FAILURE };
145
146static cl::opt<enum SkipType> SkipUnsupportedInstructions(
147 "skip-unsupported-instructions",
148 cl::desc("Force analysis to continue in the presence of unsupported "
149 "instructions"),
150 cl::values(
151 clEnumValN(SkipType::NONE, "none",
152 "Exit with an error when an instruction is unsupported for "
153 "any reason (default)"),
154 clEnumValN(
155 SkipType::LACK_SCHED, "lack-sched",
156 "Skip instructions on input which lack scheduling information"),
157 clEnumValN(
158 SkipType::PARSE_FAILURE, "parse-failure",
159 "Skip lines on the input which fail to parse for any reason"),
160 clEnumValN(SkipType::ANY_FAILURE, "any",
161 "Skip instructions or lines on input which are unsupported "
162 "for any reason")),
163 cl::init(Val: SkipType::NONE), cl::cat(ViewOptions));
164
165bool shouldSkip(enum SkipType skipType) {
166 if (SkipUnsupportedInstructions == SkipType::NONE)
167 return false;
168 if (SkipUnsupportedInstructions == SkipType::ANY_FAILURE)
169 return true;
170 return skipType == SkipUnsupportedInstructions;
171}
172
173static cl::opt<bool>
174 PrintRegisterFileStats("register-file-stats",
175 cl::desc("Print register file statistics"),
176 cl::cat(ViewOptions), cl::init(Val: false));
177
178static cl::opt<bool> PrintDispatchStats("dispatch-stats",
179 cl::desc("Print dispatch statistics"),
180 cl::cat(ViewOptions), cl::init(Val: false));
181
182static cl::opt<bool>
183 PrintSummaryView("summary-view", cl::Hidden,
184 cl::desc("Print summary view (enabled by default)"),
185 cl::cat(ViewOptions), cl::init(Val: true));
186
187static cl::opt<bool> PrintSchedulerStats("scheduler-stats",
188 cl::desc("Print scheduler statistics"),
189 cl::cat(ViewOptions), cl::init(Val: false));
190
191static cl::opt<bool>
192 PrintRetireStats("retire-stats",
193 cl::desc("Print retire control unit statistics"),
194 cl::cat(ViewOptions), cl::init(Val: false));
195
196static cl::opt<bool> PrintResourcePressureView(
197 "resource-pressure",
198 cl::desc("Print the resource pressure view (enabled by default)"),
199 cl::cat(ViewOptions), cl::init(Val: true));
200
201static cl::opt<bool> PrintTimelineView("timeline",
202 cl::desc("Print the timeline view"),
203 cl::cat(ViewOptions), cl::init(Val: false));
204
205static cl::opt<unsigned> TimelineMaxIterations(
206 "timeline-max-iterations",
207 cl::desc("Maximum number of iterations to print in timeline view"),
208 cl::cat(ViewOptions), cl::init(Val: 0));
209
210static cl::opt<unsigned>
211 TimelineMaxCycles("timeline-max-cycles",
212 cl::desc("Maximum number of cycles in the timeline view, "
213 "or 0 for unlimited. Defaults to 80 cycles"),
214 cl::cat(ViewOptions), cl::init(Val: 80));
215
216static cl::opt<bool>
217 AssumeNoAlias("noalias",
218 cl::desc("If set, assume that loads and stores do not alias"),
219 cl::cat(ToolOptions), cl::init(Val: true));
220
221static cl::opt<unsigned> LoadQueueSize("lqueue",
222 cl::desc("Size of the load queue"),
223 cl::cat(ToolOptions), cl::init(Val: 0));
224
225static cl::opt<unsigned> StoreQueueSize("squeue",
226 cl::desc("Size of the store queue"),
227 cl::cat(ToolOptions), cl::init(Val: 0));
228
229enum class InstructionTablesType { NONE, NORMAL, FULL };
230
231static cl::opt<enum InstructionTablesType> InstructionTablesOption(
232 "instruction-tables", cl::desc("Print instruction tables"),
233 cl::values(clEnumValN(InstructionTablesType::NONE, "none",
234 "Do not print instruction tables"),
235 clEnumValN(InstructionTablesType::NORMAL, "normal",
236 "Print instruction tables"),
237 clEnumValN(InstructionTablesType::NORMAL, "", ""),
238 clEnumValN(InstructionTablesType::FULL, "full",
239 "Print instruction tables with additional"
240 " information: bypass latency, LLVM opcode,"
241 " used resources")),
242 cl::cat(ToolOptions), cl::init(Val: InstructionTablesType::NONE),
243 cl::ValueOptional);
244
245static bool shouldPrintInstructionTables(enum InstructionTablesType ITType) {
246 return InstructionTablesOption == ITType;
247}
248
249static bool shouldPrintInstructionTables() {
250 return !shouldPrintInstructionTables(ITType: InstructionTablesType::NONE);
251}
252
253static cl::opt<bool> PrintInstructionInfoView(
254 "instruction-info",
255 cl::desc("Print the instruction info view (enabled by default)"),
256 cl::cat(ViewOptions), cl::init(Val: true));
257
258static cl::opt<bool> EnableAllStats("all-stats",
259 cl::desc("Print all hardware statistics"),
260 cl::cat(ViewOptions), cl::init(Val: false));
261
262static cl::opt<bool>
263 EnableAllViews("all-views",
264 cl::desc("Print all views including hardware statistics"),
265 cl::cat(ViewOptions), cl::init(Val: false));
266
267static cl::opt<bool> EnableBottleneckAnalysis(
268 "bottleneck-analysis",
269 cl::desc("Enable bottleneck analysis (disabled by default)"),
270 cl::cat(ViewOptions), cl::init(Val: false));
271
272static cl::opt<bool> ShowEncoding(
273 "show-encoding",
274 cl::desc("Print encoding information in the instruction info view"),
275 cl::cat(ViewOptions), cl::init(Val: false));
276
277static cl::opt<bool> ShowBarriers(
278 "show-barriers",
279 cl::desc("Print memory barrier information in the instruction info view"),
280 cl::cat(ViewOptions), cl::init(Val: false));
281
282static cl::opt<bool> DisableCustomBehaviour(
283 "disable-cb",
284 cl::desc(
285 "Disable custom behaviour (use the default class which does nothing)."),
286 cl::cat(ViewOptions), cl::init(Val: false));
287
288static cl::opt<bool> DisableInstrumentManager(
289 "disable-im",
290 cl::desc("Disable instrumentation manager (use the default class which "
291 "ignores instruments.)."),
292 cl::cat(ViewOptions), cl::init(Val: false));
293
294namespace {
295
296const Target *getTarget(Triple &TheTriple, const char *ProgName) {
297 // Get the target specific parser.
298 std::string Error;
299 const Target *TheTarget =
300 TargetRegistry::lookupTarget(ArchName, TheTriple, Error);
301 if (!TheTarget) {
302 errs() << ProgName << ": " << Error;
303 return nullptr;
304 }
305
306 // Return the found target.
307 return TheTarget;
308}
309
310ErrorOr<std::unique_ptr<ToolOutputFile>> getOutputStream() {
311 if (OutputFilename == "")
312 OutputFilename = "-";
313 std::error_code EC;
314 auto Out = std::make_unique<ToolOutputFile>(args&: OutputFilename, args&: EC,
315 args: sys::fs::OF_TextWithCRLF);
316 if (!EC)
317 return std::move(Out);
318 return EC;
319}
320} // end of anonymous namespace
321
322static void processOptionImpl(cl::opt<bool> &O, const cl::opt<bool> &Default) {
323 if (!O.getNumOccurrences() || O.getPosition() < Default.getPosition())
324 O = Default.getValue();
325}
326
327static void processViewOptions(bool IsOutOfOrder) {
328 if (!EnableAllViews.getNumOccurrences() &&
329 !EnableAllStats.getNumOccurrences())
330 return;
331
332 if (EnableAllViews.getNumOccurrences()) {
333 processOptionImpl(O&: PrintSummaryView, Default: EnableAllViews);
334 if (IsOutOfOrder)
335 processOptionImpl(O&: EnableBottleneckAnalysis, Default: EnableAllViews);
336 processOptionImpl(O&: PrintResourcePressureView, Default: EnableAllViews);
337 processOptionImpl(O&: PrintTimelineView, Default: EnableAllViews);
338 processOptionImpl(O&: PrintInstructionInfoView, Default: EnableAllViews);
339 }
340
341 const cl::opt<bool> &Default =
342 EnableAllViews.getPosition() < EnableAllStats.getPosition()
343 ? EnableAllStats
344 : EnableAllViews;
345 processOptionImpl(O&: PrintRegisterFileStats, Default);
346 processOptionImpl(O&: PrintDispatchStats, Default);
347 processOptionImpl(O&: PrintSchedulerStats, Default);
348 if (IsOutOfOrder)
349 processOptionImpl(O&: PrintRetireStats, Default);
350}
351
352// Returns true on success.
353static bool runPipeline(mca::Pipeline &P) {
354 // Handle pipeline errors here.
355 Expected<unsigned> Cycles = P.run();
356 if (!Cycles) {
357 WithColor::error() << toString(E: Cycles.takeError());
358 return false;
359 }
360 return true;
361}
362
363int main(int argc, char **argv) {
364 InitLLVM X(argc, argv);
365
366 // Initialize targets and assembly parsers.
367 InitializeAllTargetInfos();
368 InitializeAllTargetMCs();
369 InitializeAllAsmParsers();
370 InitializeAllTargetMCAs();
371
372 // Register the Target and CPU printer for --version.
373 cl::AddExtraVersionPrinter(func: sys::printDefaultTargetAndDetectedCPU);
374
375 // Enable printing of available targets when flag --version is specified.
376 cl::AddExtraVersionPrinter(func: TargetRegistry::printRegisteredTargetsForVersion);
377
378 cl::HideUnrelatedOptions(Categories: {&ToolOptions, &ViewOptions, &MCScheduleOptions});
379
380 // Parse flags and initialize target options.
381 cl::ParseCommandLineOptions(argc, argv,
382 Overview: "llvm machine code performance analyzer.\n");
383
384 Triple TheTriple(TripleNameOpt.empty()
385 ? Triple::normalize(Str: sys::getDefaultTargetTriple())
386 : TripleNameOpt);
387
388 // Get the target from the triple. If a triple is not specified, then select
389 // the default triple for the host. If the triple doesn't correspond to any
390 // registered target, then exit with an error message.
391 const char *ProgName = argv[0];
392 const Target *TheTarget = getTarget(TheTriple, ProgName);
393 if (!TheTarget)
394 return 1;
395
396 const bool WantsCPUHelp = MCPU == "help";
397
398 std::unique_ptr<MemoryBuffer> InputBuffer;
399 if (!WantsCPUHelp) {
400 ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
401 MemoryBuffer::getFileOrSTDIN(Filename: InputFilename);
402 if (!BufferOrErr) {
403 std::error_code EC = BufferOrErr.getError();
404 WithColor::error() << InputFilename << ": " << EC.message() << '\n';
405 return 1;
406 }
407 InputBuffer = std::move(*BufferOrErr);
408 }
409
410 if (MCPU == "native")
411 MCPU = std::string(llvm::sys::getHostCPUName());
412
413 // Package up features to be passed to target/subtarget
414 std::string FeaturesStr;
415 if (MATTRS.size()) {
416 SubtargetFeatures Features;
417 for (std::string &MAttr : MATTRS)
418 Features.AddFeature(String: MAttr);
419 FeaturesStr = Features.getString();
420 }
421
422 std::unique_ptr<MCSubtargetInfo> STI(
423 TheTarget->createMCSubtargetInfo(TheTriple, CPU: MCPU, Features: FeaturesStr));
424 if (!STI) {
425 WithColor::error() << "unable to create subtarget info\n";
426 return 1;
427 }
428
429 if (WantsCPUHelp)
430 return 0;
431
432 if (!STI->isCPUStringValid(CPU: MCPU))
433 return 1;
434
435 if (!STI->getSchedModel().hasInstrSchedModel()) {
436 WithColor::error()
437 << "unable to find instruction-level scheduling information for"
438 << " target triple '" << TheTriple.normalize() << "' and cpu '" << MCPU
439 << "'.\n";
440
441 if (STI->getSchedModel().InstrItineraries)
442 WithColor::note()
443 << "cpu '" << MCPU << "' provides itineraries. However, "
444 << "instruction itineraries are currently unsupported.\n";
445 return 1;
446 }
447
448 // Apply overrides to llvm-mca specific options.
449 bool IsOutOfOrder = STI->getSchedModel().isOutOfOrder();
450 processViewOptions(IsOutOfOrder);
451
452 std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TT: TheTriple));
453 assert(MRI && "Unable to create target register info!");
454
455 MCTargetOptions MCOptions = mc::InitMCTargetOptionsFromFlags();
456 std::unique_ptr<MCAsmInfo> MAI(
457 TheTarget->createMCAsmInfo(MRI: *MRI, TheTriple, Options: MCOptions));
458 assert(MAI && "Unable to create target asm info!");
459
460 SourceMgr SrcMgr;
461
462 // Tell SrcMgr about this buffer, which is what the parser will pick up.
463 SrcMgr.AddNewSourceBuffer(F: std::move(InputBuffer), IncludeLoc: SMLoc());
464
465 std::unique_ptr<MCInstrInfo> MCII(TheTarget->createMCInstrInfo());
466 assert(MCII && "Unable to create instruction info!");
467
468 std::unique_ptr<MCInstrAnalysis> MCIA(
469 TheTarget->createMCInstrAnalysis(Info: MCII.get()));
470
471 // Need to initialize an MCInstPrinter as it is
472 // required for initializing the MCTargetStreamer
473 // which needs to happen within the CRG.parseAnalysisRegions() call below.
474 // Without an MCTargetStreamer, certain assembly directives can trigger a
475 // segfault. (For example, the .cv_fpo_proc directive on x86 will segfault if
476 // we don't initialize the MCTargetStreamer.)
477 unsigned IPtempOutputAsmVariant =
478 OutputAsmVariant == -1 ? 0 : OutputAsmVariant;
479 std::unique_ptr<MCInstPrinter> IPtemp(TheTarget->createMCInstPrinter(
480 T: TheTriple, SyntaxVariant: IPtempOutputAsmVariant, MAI: *MAI, MII: *MCII, MRI: *MRI));
481 if (!IPtemp) {
482 WithColor::error()
483 << "unable to create instruction printer for target triple '"
484 << TheTriple.normalize() << "' with assembly variant "
485 << IPtempOutputAsmVariant << ".\n";
486 return 1;
487 }
488
489 // Parse the input and create CodeRegions that llvm-mca can analyze.
490 MCContext ACtx(TheTriple, *MAI, *MRI, *STI, &SrcMgr);
491 std::unique_ptr<MCObjectFileInfo> AMOFI(
492 TheTarget->createMCObjectFileInfo(Ctx&: ACtx, /*PIC=*/false));
493 ACtx.setObjectFileInfo(AMOFI.get());
494 mca::AsmAnalysisRegionGenerator CRG(*TheTarget, SrcMgr, ACtx, *MAI, *STI,
495 *MCII);
496 Expected<const mca::AnalysisRegions &> RegionsOrErr =
497 CRG.parseAnalysisRegions(IP: std::move(IPtemp),
498 SkipFailures: shouldSkip(skipType: SkipType::PARSE_FAILURE));
499 if (!RegionsOrErr) {
500 if (auto Err =
501 handleErrors(E: RegionsOrErr.takeError(), Hs: [](const StringError &E) {
502 WithColor::error() << E.getMessage() << '\n';
503 })) {
504 // Default case.
505 WithColor::error() << toString(E: std::move(Err)) << '\n';
506 }
507 return 1;
508 }
509 const mca::AnalysisRegions &Regions = *RegionsOrErr;
510
511 // Early exit if errors were found by the code region parsing logic.
512 if (!Regions.isValid())
513 return 1;
514
515 if (Regions.empty()) {
516 WithColor::error() << "no assembly instructions found.\n";
517 return 1;
518 }
519
520 std::unique_ptr<mca::InstrumentManager> IM;
521 if (!DisableInstrumentManager) {
522 IM = std::unique_ptr<mca::InstrumentManager>(
523 TheTarget->createInstrumentManager(STI: *STI, MCII: *MCII));
524 if (!IM) {
525 // If the target doesn't have its own IM implemented we use base class
526 // with instruments enabled.
527 IM = std::make_unique<mca::InstrumentManager>(args&: *STI, args&: *MCII);
528 }
529 } else {
530 // If the -disable-im flag is set then we use the default base class
531 // implementation and disable the instruments.
532 IM = std::make_unique<mca::InstrumentManager>(args&: *STI, args&: *MCII,
533 /*EnableInstruments=*/args: false);
534 }
535
536 // Parse the input and create InstrumentRegion that llvm-mca
537 // can use to improve analysis.
538 MCContext ICtx(TheTriple, *MAI, *MRI, *STI, &SrcMgr);
539 std::unique_ptr<MCObjectFileInfo> IMOFI(
540 TheTarget->createMCObjectFileInfo(Ctx&: ICtx, /*PIC=*/false));
541 ICtx.setObjectFileInfo(IMOFI.get());
542 mca::AsmInstrumentRegionGenerator IRG(*TheTarget, SrcMgr, ICtx, *MAI, *STI,
543 *MCII, *IM);
544 Expected<const mca::InstrumentRegions &> InstrumentRegionsOrErr =
545 IRG.parseInstrumentRegions(IP: std::move(IPtemp),
546 SkipFailures: shouldSkip(skipType: SkipType::PARSE_FAILURE));
547 if (!InstrumentRegionsOrErr) {
548 if (auto Err = handleErrors(E: InstrumentRegionsOrErr.takeError(),
549 Hs: [](const StringError &E) {
550 WithColor::error() << E.getMessage() << '\n';
551 })) {
552 // Default case.
553 WithColor::error() << toString(E: std::move(Err)) << '\n';
554 }
555 return 1;
556 }
557 const mca::InstrumentRegions &InstrumentRegions = *InstrumentRegionsOrErr;
558
559 // Early exit if errors were found by the instrumentation parsing logic.
560 if (!InstrumentRegions.isValid())
561 return 1;
562
563 // Now initialize the output file.
564 auto OF = getOutputStream();
565 if (std::error_code EC = OF.getError()) {
566 WithColor::error() << EC.message() << '\n';
567 return 1;
568 }
569
570 unsigned AssemblerDialect = CRG.getAssemblerDialect();
571 if (OutputAsmVariant >= 0)
572 AssemblerDialect = static_cast<unsigned>(OutputAsmVariant);
573 std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
574 T: TheTriple, SyntaxVariant: AssemblerDialect, MAI: *MAI, MII: *MCII, MRI: *MRI));
575 if (!IP) {
576 WithColor::error()
577 << "unable to create instruction printer for target triple '"
578 << TheTriple.normalize() << "' with assembly variant "
579 << AssemblerDialect << ".\n";
580 return 1;
581 }
582
583 // Set the display preference for hex vs. decimal immediates.
584 IP->setPrintImmHex(PrintImmHex);
585
586 std::unique_ptr<ToolOutputFile> TOF = std::move(*OF);
587
588 const MCSchedModel &SM = STI->getSchedModel();
589
590 std::unique_ptr<mca::InstrPostProcess> IPP;
591 if (!DisableCustomBehaviour) {
592 // TODO: It may be a good idea to separate CB and IPP so that they can
593 // be used independently of each other. What I mean by this is to add
594 // an extra command-line arg --disable-ipp so that CB and IPP can be
595 // toggled without needing to toggle both of them together.
596 IPP = std::unique_ptr<mca::InstrPostProcess>(
597 TheTarget->createInstrPostProcess(STI: *STI, MCII: *MCII));
598 }
599 if (!IPP) {
600 // If the target doesn't have its own IPP implemented (or the -disable-cb
601 // flag is set) then we use the base class (which does nothing).
602 IPP = std::make_unique<mca::InstrPostProcess>(args&: *STI, args&: *MCII);
603 }
604
605 // Create an instruction builder.
606 mca::InstrBuilder IB(*STI, *MCII, *MRI, MCIA.get(), *IM, CallLatency);
607
608 // Create a context to control ownership of the pipeline hardware.
609 mca::Context MCA(*MRI, *STI);
610
611 mca::PipelineOptions PO(MicroOpQueue, DecoderThroughput, DispatchWidth,
612 RegisterFileSize, LoadQueueSize, StoreQueueSize,
613 AssumeNoAlias, EnableBottleneckAnalysis);
614
615 // Number each region in the sequence.
616 unsigned RegionIdx = 0;
617
618 std::unique_ptr<MCCodeEmitter> MCE(
619 TheTarget->createMCCodeEmitter(II: *MCII, Ctx&: ACtx));
620 assert(MCE && "Unable to create code emitter!");
621
622 std::unique_ptr<MCAsmBackend> MAB(TheTarget->createMCAsmBackend(
623 STI: *STI, MRI: *MRI, Options: mc::InitMCTargetOptionsFromFlags()));
624 assert(MAB && "Unable to create asm backend!");
625
626 json::Object JSONOutput;
627 int NonEmptyRegions = 0;
628 for (const std::unique_ptr<mca::AnalysisRegion> &Region : Regions) {
629 // Skip empty code regions.
630 if (Region->empty())
631 continue;
632
633 IB.clear();
634
635 // Lower the MCInst sequence into an mca::Instruction sequence.
636 ArrayRef<MCInst> Insts = Region->getInstructions();
637 mca::CodeEmitter CE(*STI, *MAB, *MCE, Insts);
638
639 IPP->resetState();
640
641 DenseMap<const MCInst *, SmallVector<mca::Instrument *>> InstToInstruments;
642 SmallVector<std::unique_ptr<mca::Instruction>> LoweredSequence;
643 SmallPtrSet<const MCInst *, 16> DroppedInsts;
644 for (const MCInst &MCI : Insts) {
645 SMLoc Loc = MCI.getLoc();
646 const SmallVector<mca::Instrument *> Instruments =
647 InstrumentRegions.getActiveInstruments(Loc);
648
649 Expected<std::unique_ptr<mca::Instruction>> Inst =
650 IB.createInstruction(MCI, IVec: Instruments);
651 if (!Inst) {
652 if (auto NewE = handleErrors(
653 E: Inst.takeError(),
654 Hs: [&IP, &STI](const mca::InstructionError<MCInst> &IE) {
655 std::string InstructionStr;
656 raw_string_ostream SS(InstructionStr);
657 if (shouldSkip(skipType: SkipType::LACK_SCHED))
658 WithColor::warning()
659 << IE.Message
660 << ", skipping with -skip-unsupported-instructions, "
661 "note accuracy will be impacted:\n";
662 else
663 WithColor::error()
664 << IE.Message
665 << ", use -skip-unsupported-instructions=lack-sched to "
666 "ignore these on the input.\n";
667 IP->printInst(MI: &IE.Inst, Address: 0, Annot: "", STI: *STI, OS&: SS);
668 WithColor::note()
669 << "instruction: " << InstructionStr << '\n';
670 })) {
671 // Default case.
672 WithColor::error() << toString(E: std::move(NewE));
673 }
674 if (shouldSkip(skipType: SkipType::LACK_SCHED)) {
675 DroppedInsts.insert(Ptr: &MCI);
676 continue;
677 }
678 return 1;
679 }
680
681 IPP->postProcessInstruction(Inst&: *Inst.get(), MCI);
682 InstToInstruments.insert(KV: {&MCI, Instruments});
683 LoweredSequence.emplace_back(Args: std::move(Inst.get()));
684 }
685
686 Insts = Region->dropInstructions(Insts: DroppedInsts);
687
688 // Skip empty regions.
689 if (Insts.empty())
690 continue;
691 NonEmptyRegions++;
692
693 mca::CircularSourceMgr S(LoweredSequence,
694 shouldPrintInstructionTables() ? 1 : Iterations);
695
696 if (shouldPrintInstructionTables()) {
697 // Create a pipeline, stages, and a printer.
698 auto P = std::make_unique<mca::Pipeline>();
699 P->appendStage(S: std::make_unique<mca::EntryStage>(args&: S));
700 P->appendStage(S: std::make_unique<mca::InstructionTables>(args: SM));
701
702 mca::PipelinePrinter Printer(*P, *Region, RegionIdx, *STI, PO);
703 if (PrintJson) {
704 Printer.addView(
705 V: std::make_unique<mca::InstructionView>(args&: *STI, args&: *IP, args&: Insts));
706 }
707
708 // Create the views for this pipeline, execute, and emit a report.
709 if (PrintInstructionInfoView) {
710 Printer.addView(V: std::make_unique<mca::InstructionInfoView>(
711 args&: *STI, args&: *MCII, args&: CE, args&: ShowEncoding, args&: Insts, args&: *IP, args&: LoweredSequence,
712 args&: ShowBarriers,
713 args: shouldPrintInstructionTables(ITType: InstructionTablesType::FULL), args&: *IM,
714 args&: InstToInstruments));
715 }
716
717 if (PrintResourcePressureView)
718 Printer.addView(
719 V: std::make_unique<mca::ResourcePressureView>(args&: *STI, args&: *IP, args&: Insts));
720
721 if (!runPipeline(P&: *P))
722 return 1;
723
724 if (PrintJson) {
725 Printer.printReport(JO&: JSONOutput);
726 } else {
727 Printer.printReport(OS&: TOF->os());
728 }
729
730 ++RegionIdx;
731 continue;
732 }
733
734 // Create the CustomBehaviour object for enforcing Target Specific
735 // behaviours and dependencies that aren't expressed well enough
736 // in the tablegen. CB cannot depend on the list of MCInst or
737 // the source code (but it can depend on the list of
738 // mca::Instruction or any objects that can be reconstructed
739 // from the target information).
740 std::unique_ptr<mca::CustomBehaviour> CB;
741 if (!DisableCustomBehaviour)
742 CB = std::unique_ptr<mca::CustomBehaviour>(
743 TheTarget->createCustomBehaviour(STI: *STI, SrcMgr: S, MCII: *MCII));
744 if (!CB)
745 // If the target doesn't have its own CB implemented (or the -disable-cb
746 // flag is set) then we use the base class (which does nothing).
747 CB = std::make_unique<mca::CustomBehaviour>(args&: *STI, args&: S, args&: *MCII);
748
749 // Create a basic pipeline simulating an out-of-order backend.
750 auto P = MCA.createDefaultPipeline(Opts: PO, SrcMgr&: S, CB&: *CB);
751
752 mca::PipelinePrinter Printer(*P, *Region, RegionIdx, *STI, PO);
753
754 // Targets can define their own custom Views that exist within their
755 // /lib/Target/ directory so that the View can utilize their CustomBehaviour
756 // or other backend symbols / functionality that are not already exposed
757 // through one of the MC-layer classes. These Views will be initialized
758 // using the CustomBehaviour::getViews() variants.
759 // If a target makes a custom View that does not depend on their target
760 // CB or their backend, they should put the View within
761 // /tools/llvm-mca/Views/ instead.
762 if (!DisableCustomBehaviour) {
763 std::vector<std::unique_ptr<mca::View>> CBViews =
764 CB->getStartViews(IP&: *IP, Insts);
765 for (auto &CBView : CBViews)
766 Printer.addView(V: std::move(CBView));
767 }
768
769 // When we output JSON, we add a view that contains the instructions
770 // and CPU resource information.
771 if (PrintJson) {
772 auto IV = std::make_unique<mca::InstructionView>(args&: *STI, args&: *IP, args&: Insts);
773 Printer.addView(V: std::move(IV));
774 }
775
776 if (PrintSummaryView)
777 Printer.addView(
778 V: std::make_unique<mca::SummaryView>(args: SM, args&: Insts, args&: DispatchWidth));
779
780 if (EnableBottleneckAnalysis) {
781 if (!IsOutOfOrder) {
782 WithColor::warning()
783 << "bottleneck analysis is not supported for in-order CPU '" << MCPU
784 << "'.\n";
785 }
786 Printer.addView(V: std::make_unique<mca::BottleneckAnalysis>(
787 args&: *STI, args&: *IP, args&: Insts, args: S.getNumIterations()));
788 }
789
790 if (PrintInstructionInfoView)
791 Printer.addView(V: std::make_unique<mca::InstructionInfoView>(
792 args&: *STI, args&: *MCII, args&: CE, args&: ShowEncoding, args&: Insts, args&: *IP, args&: LoweredSequence,
793 args&: ShowBarriers, /*ShouldPrintFullInfo=*/args: false, args&: *IM, args&: InstToInstruments));
794
795 // Fetch custom Views that are to be placed after the InstructionInfoView.
796 // Refer to the comment paired with the CB->getStartViews(*IP, Insts); line
797 // for more info.
798 if (!DisableCustomBehaviour) {
799 std::vector<std::unique_ptr<mca::View>> CBViews =
800 CB->getPostInstrInfoViews(IP&: *IP, Insts);
801 for (auto &CBView : CBViews)
802 Printer.addView(V: std::move(CBView));
803 }
804
805 if (PrintDispatchStats)
806 Printer.addView(V: std::make_unique<mca::DispatchStatistics>());
807
808 if (PrintSchedulerStats)
809 Printer.addView(V: std::make_unique<mca::SchedulerStatistics>(args&: *STI));
810
811 if (PrintRetireStats)
812 Printer.addView(V: std::make_unique<mca::RetireControlUnitStatistics>(args: SM));
813
814 if (PrintRegisterFileStats)
815 Printer.addView(V: std::make_unique<mca::RegisterFileStatistics>(args&: *STI));
816
817 if (PrintResourcePressureView)
818 Printer.addView(
819 V: std::make_unique<mca::ResourcePressureView>(args&: *STI, args&: *IP, args&: Insts));
820
821 if (PrintTimelineView) {
822 unsigned TimelineIterations =
823 TimelineMaxIterations ? TimelineMaxIterations : 10;
824 Printer.addView(V: std::make_unique<mca::TimelineView>(
825 args&: *STI, args&: *IP, args&: Insts, args: std::min(a: TimelineIterations, b: S.getNumIterations()),
826 args&: TimelineMaxCycles));
827 }
828
829 // Fetch custom Views that are to be placed after all other Views.
830 // Refer to the comment paired with the CB->getStartViews(*IP, Insts); line
831 // for more info.
832 if (!DisableCustomBehaviour) {
833 std::vector<std::unique_ptr<mca::View>> CBViews =
834 CB->getEndViews(IP&: *IP, Insts);
835 for (auto &CBView : CBViews)
836 Printer.addView(V: std::move(CBView));
837 }
838
839 if (!runPipeline(P&: *P))
840 return 1;
841
842 if (PrintJson) {
843 Printer.printReport(JO&: JSONOutput);
844 } else {
845 Printer.printReport(OS&: TOF->os());
846 }
847
848 ++RegionIdx;
849 }
850
851 if (NonEmptyRegions == 0) {
852 WithColor::error() << "no assembly instructions found.\n";
853 return 1;
854 }
855
856 if (PrintJson)
857 TOF->os() << formatv(Fmt: "{0:2}", Vals: json::Value(std::move(JSONOutput))) << "\n";
858
859 TOF->keep();
860 return 0;
861}
862