1 //===-- llvm-mca.cpp - Machine Code Analyzer -------------------*- C++ -* -===//
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
7 //===----------------------------------------------------------------------===//
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.
12 // llvm-mca [options] <file-name>
17 // The target defaults to the host target.
18 // The cpu defaults to the 'native' host cpu.
19 // The output defaults to standard output.
21 //===----------------------------------------------------------------------===//
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/MC/TargetRegistry.h"
44 #include "llvm/MCA/CodeEmitter.h"
45 #include "llvm/MCA/Context.h"
46 #include "llvm/MCA/CustomBehaviour.h"
47 #include "llvm/MCA/InstrBuilder.h"
48 #include "llvm/MCA/Pipeline.h"
49 #include "llvm/MCA/Stages/EntryStage.h"
50 #include "llvm/MCA/Stages/InstructionTables.h"
51 #include "llvm/MCA/Support.h"
52 #include "llvm/Support/CommandLine.h"
53 #include "llvm/Support/ErrorHandling.h"
54 #include "llvm/Support/ErrorOr.h"
55 #include "llvm/Support/FileSystem.h"
56 #include "llvm/Support/InitLLVM.h"
57 #include "llvm/Support/MemoryBuffer.h"
58 #include "llvm/Support/SourceMgr.h"
59 #include "llvm/Support/TargetSelect.h"
60 #include "llvm/Support/ToolOutputFile.h"
61 #include "llvm/Support/WithColor.h"
62 #include "llvm/TargetParser/Host.h"
66 static mc::RegisterMCTargetOptionsFlags MOF
;
68 static cl::OptionCategory
ToolOptions("Tool Options");
69 static cl::OptionCategory
ViewOptions("View Options");
71 static cl::opt
<std::string
> InputFilename(cl::Positional
,
72 cl::desc("<input file>"),
73 cl::cat(ToolOptions
), cl::init("-"));
75 static cl::opt
<std::string
> OutputFilename("o", cl::desc("Output filename"),
76 cl::init("-"), cl::cat(ToolOptions
),
77 cl::value_desc("filename"));
79 static cl::opt
<std::string
>
81 cl::desc("Target architecture. "
82 "See -version for available targets"),
83 cl::cat(ToolOptions
));
85 static cl::opt
<std::string
>
87 cl::desc("Target triple. See -version for available targets"),
88 cl::cat(ToolOptions
));
90 static cl::opt
<std::string
>
92 cl::desc("Target a specific cpu type (-mcpu=help for details)"),
93 cl::value_desc("cpu-name"), cl::cat(ToolOptions
), cl::init("native"));
95 static cl::list
<std::string
>
96 MATTRS("mattr", cl::CommaSeparated
,
97 cl::desc("Target specific attributes (-mattr=help for details)"),
98 cl::value_desc("a1,+a2,-a3,..."), cl::cat(ToolOptions
));
100 static cl::opt
<bool> PrintJson("json",
101 cl::desc("Print the output in json format"),
102 cl::cat(ToolOptions
), cl::init(false));
105 OutputAsmVariant("output-asm-variant",
106 cl::desc("Syntax variant to use for output printing"),
107 cl::cat(ToolOptions
), cl::init(-1));
110 PrintImmHex("print-imm-hex", cl::cat(ToolOptions
), cl::init(false),
111 cl::desc("Prefer hex format when printing immediate values"));
113 static cl::opt
<unsigned> Iterations("iterations",
114 cl::desc("Number of iterations to run"),
115 cl::cat(ToolOptions
), cl::init(0));
117 static cl::opt
<unsigned>
118 DispatchWidth("dispatch", cl::desc("Override the processor dispatch width"),
119 cl::cat(ToolOptions
), cl::init(0));
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));
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));
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));
138 static cl::opt
<unsigned>
139 CallLatency("call-latency", cl::Hidden
,
140 cl::desc("Number of cycles to assume for a call instruction"),
141 cl::cat(ToolOptions
), cl::init(100U));
143 enum class SkipType
{ NONE
, LACK_SCHED
, PARSE_FAILURE
, ANY_FAILURE
};
145 static cl::opt
<enum SkipType
> SkipUnsupportedInstructions(
146 "skip-unsupported-instructions",
147 cl::desc("Force analysis to continue in the presence of unsupported "
150 clEnumValN(SkipType::NONE
, "none",
151 "Exit with an error when an instruction is unsupported for "
152 "any reason (default)"),
154 SkipType::LACK_SCHED
, "lack-sched",
155 "Skip instructions on input which lack scheduling information"),
157 SkipType::PARSE_FAILURE
, "parse-failure",
158 "Skip lines on the input which fail to parse for any reason"),
159 clEnumValN(SkipType::ANY_FAILURE
, "any",
160 "Skip instructions or lines on input which are unsupported "
162 cl::init(SkipType::NONE
), cl::cat(ViewOptions
));
164 bool shouldSkip(enum SkipType skipType
) {
165 if (SkipUnsupportedInstructions
== SkipType::NONE
)
167 if (SkipUnsupportedInstructions
== SkipType::ANY_FAILURE
)
169 return skipType
== SkipUnsupportedInstructions
;
173 PrintRegisterFileStats("register-file-stats",
174 cl::desc("Print register file statistics"),
175 cl::cat(ViewOptions
), cl::init(false));
177 static cl::opt
<bool> PrintDispatchStats("dispatch-stats",
178 cl::desc("Print dispatch statistics"),
179 cl::cat(ViewOptions
), cl::init(false));
182 PrintSummaryView("summary-view", cl::Hidden
,
183 cl::desc("Print summary view (enabled by default)"),
184 cl::cat(ViewOptions
), cl::init(true));
186 static cl::opt
<bool> PrintSchedulerStats("scheduler-stats",
187 cl::desc("Print scheduler statistics"),
188 cl::cat(ViewOptions
), cl::init(false));
191 PrintRetireStats("retire-stats",
192 cl::desc("Print retire control unit statistics"),
193 cl::cat(ViewOptions
), cl::init(false));
195 static cl::opt
<bool> PrintResourcePressureView(
197 cl::desc("Print the resource pressure view (enabled by default)"),
198 cl::cat(ViewOptions
), cl::init(true));
200 static cl::opt
<bool> PrintTimelineView("timeline",
201 cl::desc("Print the timeline view"),
202 cl::cat(ViewOptions
), cl::init(false));
204 static cl::opt
<unsigned> TimelineMaxIterations(
205 "timeline-max-iterations",
206 cl::desc("Maximum number of iterations to print in timeline view"),
207 cl::cat(ViewOptions
), cl::init(0));
209 static cl::opt
<unsigned>
210 TimelineMaxCycles("timeline-max-cycles",
211 cl::desc("Maximum number of cycles in the timeline view, "
212 "or 0 for unlimited. Defaults to 80 cycles"),
213 cl::cat(ViewOptions
), cl::init(80));
216 AssumeNoAlias("noalias",
217 cl::desc("If set, assume that loads and stores do not alias"),
218 cl::cat(ToolOptions
), cl::init(true));
220 static cl::opt
<unsigned> LoadQueueSize("lqueue",
221 cl::desc("Size of the load queue"),
222 cl::cat(ToolOptions
), cl::init(0));
224 static cl::opt
<unsigned> StoreQueueSize("squeue",
225 cl::desc("Size of the store queue"),
226 cl::cat(ToolOptions
), cl::init(0));
229 PrintInstructionTables("instruction-tables",
230 cl::desc("Print instruction tables"),
231 cl::cat(ToolOptions
), cl::init(false));
233 static cl::opt
<bool> PrintInstructionInfoView(
235 cl::desc("Print the instruction info view (enabled by default)"),
236 cl::cat(ViewOptions
), cl::init(true));
238 static cl::opt
<bool> EnableAllStats("all-stats",
239 cl::desc("Print all hardware statistics"),
240 cl::cat(ViewOptions
), cl::init(false));
243 EnableAllViews("all-views",
244 cl::desc("Print all views including hardware statistics"),
245 cl::cat(ViewOptions
), cl::init(false));
247 static cl::opt
<bool> EnableBottleneckAnalysis(
248 "bottleneck-analysis",
249 cl::desc("Enable bottleneck analysis (disabled by default)"),
250 cl::cat(ViewOptions
), cl::init(false));
252 static cl::opt
<bool> ShowEncoding(
254 cl::desc("Print encoding information in the instruction info view"),
255 cl::cat(ViewOptions
), cl::init(false));
257 static cl::opt
<bool> ShowBarriers(
259 cl::desc("Print memory barrier information in the instruction info view"),
260 cl::cat(ViewOptions
), cl::init(false));
262 static cl::opt
<bool> DisableCustomBehaviour(
265 "Disable custom behaviour (use the default class which does nothing)."),
266 cl::cat(ViewOptions
), cl::init(false));
268 static cl::opt
<bool> DisableInstrumentManager(
270 cl::desc("Disable instrumentation manager (use the default class which "
271 "ignores instruments.)."),
272 cl::cat(ViewOptions
), cl::init(false));
276 const Target
*getTarget(const char *ProgName
) {
277 if (TripleName
.empty())
278 TripleName
= Triple::normalize(sys::getDefaultTargetTriple());
279 Triple
TheTriple(TripleName
);
281 // Get the target specific parser.
283 const Target
*TheTarget
=
284 TargetRegistry::lookupTarget(ArchName
, TheTriple
, Error
);
286 errs() << ProgName
<< ": " << Error
;
290 // Update TripleName with the updated triple from the target lookup.
291 TripleName
= TheTriple
.str();
293 // Return the found target.
297 ErrorOr
<std::unique_ptr
<ToolOutputFile
>> getOutputStream() {
298 if (OutputFilename
== "")
299 OutputFilename
= "-";
301 auto Out
= std::make_unique
<ToolOutputFile
>(OutputFilename
, EC
,
302 sys::fs::OF_TextWithCRLF
);
304 return std::move(Out
);
307 } // end of anonymous namespace
309 static void processOptionImpl(cl::opt
<bool> &O
, const cl::opt
<bool> &Default
) {
310 if (!O
.getNumOccurrences() || O
.getPosition() < Default
.getPosition())
311 O
= Default
.getValue();
314 static void processViewOptions(bool IsOutOfOrder
) {
315 if (!EnableAllViews
.getNumOccurrences() &&
316 !EnableAllStats
.getNumOccurrences())
319 if (EnableAllViews
.getNumOccurrences()) {
320 processOptionImpl(PrintSummaryView
, EnableAllViews
);
322 processOptionImpl(EnableBottleneckAnalysis
, EnableAllViews
);
323 processOptionImpl(PrintResourcePressureView
, EnableAllViews
);
324 processOptionImpl(PrintTimelineView
, EnableAllViews
);
325 processOptionImpl(PrintInstructionInfoView
, EnableAllViews
);
328 const cl::opt
<bool> &Default
=
329 EnableAllViews
.getPosition() < EnableAllStats
.getPosition()
332 processOptionImpl(PrintRegisterFileStats
, Default
);
333 processOptionImpl(PrintDispatchStats
, Default
);
334 processOptionImpl(PrintSchedulerStats
, Default
);
336 processOptionImpl(PrintRetireStats
, Default
);
339 // Returns true on success.
340 static bool runPipeline(mca::Pipeline
&P
) {
341 // Handle pipeline errors here.
342 Expected
<unsigned> Cycles
= P
.run();
344 WithColor::error() << toString(Cycles
.takeError());
350 int main(int argc
, char **argv
) {
351 InitLLVM
X(argc
, argv
);
353 // Initialize targets and assembly parsers.
354 InitializeAllTargetInfos();
355 InitializeAllTargetMCs();
356 InitializeAllAsmParsers();
357 InitializeAllTargetMCAs();
359 // Register the Target and CPU printer for --version.
360 cl::AddExtraVersionPrinter(sys::printDefaultTargetAndDetectedCPU
);
362 // Enable printing of available targets when flag --version is specified.
363 cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion
);
365 cl::HideUnrelatedOptions({&ToolOptions
, &ViewOptions
});
367 // Parse flags and initialize target options.
368 cl::ParseCommandLineOptions(argc
, argv
,
369 "llvm machine code performance analyzer.\n");
371 // Get the target from the triple. If a triple is not specified, then select
372 // the default triple for the host. If the triple doesn't correspond to any
373 // registered target, then exit with an error message.
374 const char *ProgName
= argv
[0];
375 const Target
*TheTarget
= getTarget(ProgName
);
379 // GetTarget() may replaced TripleName with a default triple.
380 // For safety, reconstruct the Triple object.
381 Triple
TheTriple(TripleName
);
383 ErrorOr
<std::unique_ptr
<MemoryBuffer
>> BufferPtr
=
384 MemoryBuffer::getFileOrSTDIN(InputFilename
);
385 if (std::error_code EC
= BufferPtr
.getError()) {
386 WithColor::error() << InputFilename
<< ": " << EC
.message() << '\n';
390 if (MCPU
== "native")
391 MCPU
= std::string(llvm::sys::getHostCPUName());
393 // Package up features to be passed to target/subtarget
394 std::string FeaturesStr
;
396 SubtargetFeatures Features
;
397 for (std::string
&MAttr
: MATTRS
)
398 Features
.AddFeature(MAttr
);
399 FeaturesStr
= Features
.getString();
402 std::unique_ptr
<MCSubtargetInfo
> STI(
403 TheTarget
->createMCSubtargetInfo(TripleName
, MCPU
, FeaturesStr
));
404 assert(STI
&& "Unable to create subtarget info!");
405 if (!STI
->isCPUStringValid(MCPU
))
408 if (!STI
->getSchedModel().hasInstrSchedModel()) {
410 << "unable to find instruction-level scheduling information for"
411 << " target triple '" << TheTriple
.normalize() << "' and cpu '" << MCPU
414 if (STI
->getSchedModel().InstrItineraries
)
416 << "cpu '" << MCPU
<< "' provides itineraries. However, "
417 << "instruction itineraries are currently unsupported.\n";
421 // Apply overrides to llvm-mca specific options.
422 bool IsOutOfOrder
= STI
->getSchedModel().isOutOfOrder();
423 processViewOptions(IsOutOfOrder
);
425 std::unique_ptr
<MCRegisterInfo
> MRI(TheTarget
->createMCRegInfo(TripleName
));
426 assert(MRI
&& "Unable to create target register info!");
428 MCTargetOptions MCOptions
= mc::InitMCTargetOptionsFromFlags();
429 std::unique_ptr
<MCAsmInfo
> MAI(
430 TheTarget
->createMCAsmInfo(*MRI
, TripleName
, MCOptions
));
431 assert(MAI
&& "Unable to create target asm info!");
435 // Tell SrcMgr about this buffer, which is what the parser will pick up.
436 SrcMgr
.AddNewSourceBuffer(std::move(*BufferPtr
), SMLoc());
438 std::unique_ptr
<buffer_ostream
> BOS
;
440 std::unique_ptr
<MCInstrInfo
> MCII(TheTarget
->createMCInstrInfo());
441 assert(MCII
&& "Unable to create instruction info!");
443 std::unique_ptr
<MCInstrAnalysis
> MCIA(
444 TheTarget
->createMCInstrAnalysis(MCII
.get()));
446 // Need to initialize an MCInstPrinter as it is
447 // required for initializing the MCTargetStreamer
448 // which needs to happen within the CRG.parseAnalysisRegions() call below.
449 // Without an MCTargetStreamer, certain assembly directives can trigger a
450 // segfault. (For example, the .cv_fpo_proc directive on x86 will segfault if
451 // we don't initialize the MCTargetStreamer.)
452 unsigned IPtempOutputAsmVariant
=
453 OutputAsmVariant
== -1 ? 0 : OutputAsmVariant
;
454 std::unique_ptr
<MCInstPrinter
> IPtemp(TheTarget
->createMCInstPrinter(
455 Triple(TripleName
), IPtempOutputAsmVariant
, *MAI
, *MCII
, *MRI
));
458 << "unable to create instruction printer for target triple '"
459 << TheTriple
.normalize() << "' with assembly variant "
460 << IPtempOutputAsmVariant
<< ".\n";
464 // Parse the input and create CodeRegions that llvm-mca can analyze.
465 MCContext
ACtx(TheTriple
, MAI
.get(), MRI
.get(), STI
.get(), &SrcMgr
);
466 std::unique_ptr
<MCObjectFileInfo
> AMOFI(
467 TheTarget
->createMCObjectFileInfo(ACtx
, /*PIC=*/false));
468 ACtx
.setObjectFileInfo(AMOFI
.get());
469 mca::AsmAnalysisRegionGenerator
CRG(*TheTarget
, SrcMgr
, ACtx
, *MAI
, *STI
,
471 Expected
<const mca::AnalysisRegions
&> RegionsOrErr
=
472 CRG
.parseAnalysisRegions(std::move(IPtemp
),
473 shouldSkip(SkipType::PARSE_FAILURE
));
476 handleErrors(RegionsOrErr
.takeError(), [](const StringError
&E
) {
477 WithColor::error() << E
.getMessage() << '\n';
480 WithColor::error() << toString(std::move(Err
)) << '\n';
484 const mca::AnalysisRegions
&Regions
= *RegionsOrErr
;
486 // Early exit if errors were found by the code region parsing logic.
487 if (!Regions
.isValid())
490 if (Regions
.empty()) {
491 WithColor::error() << "no assembly instructions found.\n";
495 std::unique_ptr
<mca::InstrumentManager
> IM
;
496 if (!DisableInstrumentManager
) {
497 IM
= std::unique_ptr
<mca::InstrumentManager
>(
498 TheTarget
->createInstrumentManager(*STI
, *MCII
));
501 // If the target doesn't have its own IM implemented (or the -disable-cb
502 // flag is set) then we use the base class (which does nothing).
503 IM
= std::make_unique
<mca::InstrumentManager
>(*STI
, *MCII
);
506 // Parse the input and create InstrumentRegion that llvm-mca
507 // can use to improve analysis.
508 MCContext
ICtx(TheTriple
, MAI
.get(), MRI
.get(), STI
.get(), &SrcMgr
);
509 std::unique_ptr
<MCObjectFileInfo
> IMOFI(
510 TheTarget
->createMCObjectFileInfo(ICtx
, /*PIC=*/false));
511 ICtx
.setObjectFileInfo(IMOFI
.get());
512 mca::AsmInstrumentRegionGenerator
IRG(*TheTarget
, SrcMgr
, ICtx
, *MAI
, *STI
,
514 Expected
<const mca::InstrumentRegions
&> InstrumentRegionsOrErr
=
515 IRG
.parseInstrumentRegions(std::move(IPtemp
),
516 shouldSkip(SkipType::PARSE_FAILURE
));
517 if (!InstrumentRegionsOrErr
) {
518 if (auto Err
= handleErrors(InstrumentRegionsOrErr
.takeError(),
519 [](const StringError
&E
) {
520 WithColor::error() << E
.getMessage() << '\n';
523 WithColor::error() << toString(std::move(Err
)) << '\n';
527 const mca::InstrumentRegions
&InstrumentRegions
= *InstrumentRegionsOrErr
;
529 // Early exit if errors were found by the instrumentation parsing logic.
530 if (!InstrumentRegions
.isValid())
533 // Now initialize the output file.
534 auto OF
= getOutputStream();
535 if (std::error_code EC
= OF
.getError()) {
536 WithColor::error() << EC
.message() << '\n';
540 unsigned AssemblerDialect
= CRG
.getAssemblerDialect();
541 if (OutputAsmVariant
>= 0)
542 AssemblerDialect
= static_cast<unsigned>(OutputAsmVariant
);
543 std::unique_ptr
<MCInstPrinter
> IP(TheTarget
->createMCInstPrinter(
544 Triple(TripleName
), AssemblerDialect
, *MAI
, *MCII
, *MRI
));
547 << "unable to create instruction printer for target triple '"
548 << TheTriple
.normalize() << "' with assembly variant "
549 << AssemblerDialect
<< ".\n";
553 // Set the display preference for hex vs. decimal immediates.
554 IP
->setPrintImmHex(PrintImmHex
);
556 std::unique_ptr
<ToolOutputFile
> TOF
= std::move(*OF
);
558 const MCSchedModel
&SM
= STI
->getSchedModel();
560 std::unique_ptr
<mca::InstrPostProcess
> IPP
;
561 if (!DisableCustomBehaviour
) {
562 // TODO: It may be a good idea to separate CB and IPP so that they can
563 // be used independently of each other. What I mean by this is to add
564 // an extra command-line arg --disable-ipp so that CB and IPP can be
565 // toggled without needing to toggle both of them together.
566 IPP
= std::unique_ptr
<mca::InstrPostProcess
>(
567 TheTarget
->createInstrPostProcess(*STI
, *MCII
));
570 // If the target doesn't have its own IPP implemented (or the -disable-cb
571 // flag is set) then we use the base class (which does nothing).
572 IPP
= std::make_unique
<mca::InstrPostProcess
>(*STI
, *MCII
);
575 // Create an instruction builder.
576 mca::InstrBuilder
IB(*STI
, *MCII
, *MRI
, MCIA
.get(), *IM
, CallLatency
);
578 // Create a context to control ownership of the pipeline hardware.
579 mca::Context
MCA(*MRI
, *STI
);
581 mca::PipelineOptions
PO(MicroOpQueue
, DecoderThroughput
, DispatchWidth
,
582 RegisterFileSize
, LoadQueueSize
, StoreQueueSize
,
583 AssumeNoAlias
, EnableBottleneckAnalysis
);
585 // Number each region in the sequence.
586 unsigned RegionIdx
= 0;
588 std::unique_ptr
<MCCodeEmitter
> MCE(
589 TheTarget
->createMCCodeEmitter(*MCII
, ACtx
));
590 assert(MCE
&& "Unable to create code emitter!");
592 std::unique_ptr
<MCAsmBackend
> MAB(TheTarget
->createMCAsmBackend(
593 *STI
, *MRI
, mc::InitMCTargetOptionsFromFlags()));
594 assert(MAB
&& "Unable to create asm backend!");
596 json::Object JSONOutput
;
597 int NonEmptyRegions
= 0;
598 for (const std::unique_ptr
<mca::AnalysisRegion
> &Region
: Regions
) {
599 // Skip empty code regions.
605 // Lower the MCInst sequence into an mca::Instruction sequence.
606 ArrayRef
<MCInst
> Insts
= Region
->getInstructions();
607 mca::CodeEmitter
CE(*STI
, *MAB
, *MCE
, Insts
);
611 DenseMap
<const MCInst
*, SmallVector
<mca::Instrument
*>> InstToInstruments
;
612 SmallVector
<std::unique_ptr
<mca::Instruction
>> LoweredSequence
;
613 SmallPtrSet
<const MCInst
*, 16> DroppedInsts
;
614 for (const MCInst
&MCI
: Insts
) {
615 SMLoc Loc
= MCI
.getLoc();
616 const SmallVector
<mca::Instrument
*> Instruments
=
617 InstrumentRegions
.getActiveInstruments(Loc
);
619 Expected
<std::unique_ptr
<mca::Instruction
>> Inst
=
620 IB
.createInstruction(MCI
, Instruments
);
622 if (auto NewE
= handleErrors(
624 [&IP
, &STI
](const mca::InstructionError
<MCInst
> &IE
) {
625 std::string InstructionStr
;
626 raw_string_ostream
SS(InstructionStr
);
627 if (shouldSkip(SkipType::LACK_SCHED
))
630 << ", skipping with -skip-unsupported-instructions, "
631 "note accuracy will be impacted:\n";
635 << ", use -skip-unsupported-instructions=lack-sched to "
636 "ignore these on the input.\n";
637 IP
->printInst(&IE
.Inst
, 0, "", *STI
, SS
);
640 << "instruction: " << InstructionStr
<< '\n';
643 WithColor::error() << toString(std::move(NewE
));
645 if (shouldSkip(SkipType::LACK_SCHED
)) {
646 DroppedInsts
.insert(&MCI
);
652 IPP
->postProcessInstruction(Inst
.get(), MCI
);
653 InstToInstruments
.insert({&MCI
, Instruments
});
654 LoweredSequence
.emplace_back(std::move(Inst
.get()));
657 Insts
= Region
->dropInstructions(DroppedInsts
);
659 // Skip empty regions.
664 mca::CircularSourceMgr
S(LoweredSequence
,
665 PrintInstructionTables
? 1 : Iterations
);
667 if (PrintInstructionTables
) {
668 // Create a pipeline, stages, and a printer.
669 auto P
= std::make_unique
<mca::Pipeline
>();
670 P
->appendStage(std::make_unique
<mca::EntryStage
>(S
));
671 P
->appendStage(std::make_unique
<mca::InstructionTables
>(SM
));
673 mca::PipelinePrinter
Printer(*P
, *Region
, RegionIdx
, *STI
, PO
);
676 std::make_unique
<mca::InstructionView
>(*STI
, *IP
, Insts
));
679 // Create the views for this pipeline, execute, and emit a report.
680 if (PrintInstructionInfoView
) {
681 Printer
.addView(std::make_unique
<mca::InstructionInfoView
>(
682 *STI
, *MCII
, CE
, ShowEncoding
, Insts
, *IP
, LoweredSequence
,
683 ShowBarriers
, *IM
, InstToInstruments
));
686 std::make_unique
<mca::ResourcePressureView
>(*STI
, *IP
, Insts
));
688 if (!runPipeline(*P
))
692 Printer
.printReport(JSONOutput
);
694 Printer
.printReport(TOF
->os());
701 // Create the CustomBehaviour object for enforcing Target Specific
702 // behaviours and dependencies that aren't expressed well enough
703 // in the tablegen. CB cannot depend on the list of MCInst or
704 // the source code (but it can depend on the list of
705 // mca::Instruction or any objects that can be reconstructed
706 // from the target information).
707 std::unique_ptr
<mca::CustomBehaviour
> CB
;
708 if (!DisableCustomBehaviour
)
709 CB
= std::unique_ptr
<mca::CustomBehaviour
>(
710 TheTarget
->createCustomBehaviour(*STI
, S
, *MCII
));
712 // If the target doesn't have its own CB implemented (or the -disable-cb
713 // flag is set) then we use the base class (which does nothing).
714 CB
= std::make_unique
<mca::CustomBehaviour
>(*STI
, S
, *MCII
);
716 // Create a basic pipeline simulating an out-of-order backend.
717 auto P
= MCA
.createDefaultPipeline(PO
, S
, *CB
);
719 mca::PipelinePrinter
Printer(*P
, *Region
, RegionIdx
, *STI
, PO
);
721 // Targets can define their own custom Views that exist within their
722 // /lib/Target/ directory so that the View can utilize their CustomBehaviour
723 // or other backend symbols / functionality that are not already exposed
724 // through one of the MC-layer classes. These Views will be initialized
725 // using the CustomBehaviour::getViews() variants.
726 // If a target makes a custom View that does not depend on their target
727 // CB or their backend, they should put the View within
728 // /tools/llvm-mca/Views/ instead.
729 if (!DisableCustomBehaviour
) {
730 std::vector
<std::unique_ptr
<mca::View
>> CBViews
=
731 CB
->getStartViews(*IP
, Insts
);
732 for (auto &CBView
: CBViews
)
733 Printer
.addView(std::move(CBView
));
736 // When we output JSON, we add a view that contains the instructions
737 // and CPU resource information.
739 auto IV
= std::make_unique
<mca::InstructionView
>(*STI
, *IP
, Insts
);
740 Printer
.addView(std::move(IV
));
743 if (PrintSummaryView
)
745 std::make_unique
<mca::SummaryView
>(SM
, Insts
, DispatchWidth
));
747 if (EnableBottleneckAnalysis
) {
750 << "bottleneck analysis is not supported for in-order CPU '" << MCPU
753 Printer
.addView(std::make_unique
<mca::BottleneckAnalysis
>(
754 *STI
, *IP
, Insts
, S
.getNumIterations()));
757 if (PrintInstructionInfoView
)
758 Printer
.addView(std::make_unique
<mca::InstructionInfoView
>(
759 *STI
, *MCII
, CE
, ShowEncoding
, Insts
, *IP
, LoweredSequence
,
760 ShowBarriers
, *IM
, InstToInstruments
));
762 // Fetch custom Views that are to be placed after the InstructionInfoView.
763 // Refer to the comment paired with the CB->getStartViews(*IP, Insts); line
765 if (!DisableCustomBehaviour
) {
766 std::vector
<std::unique_ptr
<mca::View
>> CBViews
=
767 CB
->getPostInstrInfoViews(*IP
, Insts
);
768 for (auto &CBView
: CBViews
)
769 Printer
.addView(std::move(CBView
));
772 if (PrintDispatchStats
)
773 Printer
.addView(std::make_unique
<mca::DispatchStatistics
>());
775 if (PrintSchedulerStats
)
776 Printer
.addView(std::make_unique
<mca::SchedulerStatistics
>(*STI
));
778 if (PrintRetireStats
)
779 Printer
.addView(std::make_unique
<mca::RetireControlUnitStatistics
>(SM
));
781 if (PrintRegisterFileStats
)
782 Printer
.addView(std::make_unique
<mca::RegisterFileStatistics
>(*STI
));
784 if (PrintResourcePressureView
)
786 std::make_unique
<mca::ResourcePressureView
>(*STI
, *IP
, Insts
));
788 if (PrintTimelineView
) {
789 unsigned TimelineIterations
=
790 TimelineMaxIterations
? TimelineMaxIterations
: 10;
791 Printer
.addView(std::make_unique
<mca::TimelineView
>(
792 *STI
, *IP
, Insts
, std::min(TimelineIterations
, S
.getNumIterations()),
796 // Fetch custom Views that are to be placed after all other Views.
797 // Refer to the comment paired with the CB->getStartViews(*IP, Insts); line
799 if (!DisableCustomBehaviour
) {
800 std::vector
<std::unique_ptr
<mca::View
>> CBViews
=
801 CB
->getEndViews(*IP
, Insts
);
802 for (auto &CBView
: CBViews
)
803 Printer
.addView(std::move(CBView
));
806 if (!runPipeline(*P
))
810 Printer
.printReport(JSONOutput
);
812 Printer
.printReport(TOF
->os());
818 if (NonEmptyRegions
== 0) {
819 WithColor::error() << "no assembly instructions found.\n";
824 TOF
->os() << formatv("{0:2}", json::Value(std::move(JSONOutput
))) << "\n";