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/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/Host.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 64 using namespace llvm; 65 66 static mc::RegisterMCTargetOptionsFlags MOF; 67 68 static cl::OptionCategory ToolOptions("Tool Options"); 69 static cl::OptionCategory ViewOptions("View Options"); 70 71 static cl::opt<std::string> InputFilename(cl::Positional, 72 cl::desc("<input file>"), 73 cl::cat(ToolOptions), cl::init("-")); 74 75 static cl::opt<std::string> OutputFilename("o", cl::desc("Output filename"), 76 cl::init("-"), cl::cat(ToolOptions), 77 cl::value_desc("filename")); 78 79 static cl::opt<std::string> 80 ArchName("march", 81 cl::desc("Target architecture. " 82 "See -version for available targets"), 83 cl::cat(ToolOptions)); 84 85 static cl::opt<std::string> 86 TripleName("mtriple", 87 cl::desc("Target triple. See -version for available targets"), 88 cl::cat(ToolOptions)); 89 90 static cl::opt<std::string> 91 MCPU("mcpu", 92 cl::desc("Target a specific cpu type (-mcpu=help for details)"), 93 cl::value_desc("cpu-name"), cl::cat(ToolOptions), cl::init("native")); 94 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)); 99 100 static cl::opt<bool> 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> 176 TimelineMaxCycles("timeline-max-cycles", 177 cl::desc("Maximum number of cycles in the timeline view, " 178 "or 0 for unlimited. 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 static cl::opt<bool> ShowBarriers( 224 "show-barriers", 225 cl::desc("Print memory barrier information in the instruction info view"), 226 cl::cat(ViewOptions), cl::init(false)); 227 228 static cl::opt<bool> DisableCustomBehaviour( 229 "disable-cb", 230 cl::desc( 231 "Disable custom behaviour (use the default class which does nothing)."), 232 cl::cat(ViewOptions), cl::init(false)); 233 234 static cl::opt<bool> DisableInstrumentManager( 235 "disable-im", 236 cl::desc("Disable instrumentation manager (use the default class which " 237 "ignores instruments.)."), 238 cl::cat(ViewOptions), cl::init(false)); 239 240 namespace { 241 242 const Target *getTarget(const char *ProgName) { 243 if (TripleName.empty()) 244 TripleName = Triple::normalize(sys::getDefaultTargetTriple()); 245 Triple TheTriple(TripleName); 246 247 // Get the target specific parser. 248 std::string Error; 249 const Target *TheTarget = 250 TargetRegistry::lookupTarget(ArchName, TheTriple, Error); 251 if (!TheTarget) { 252 errs() << ProgName << ": " << Error; 253 return nullptr; 254 } 255 256 // Update TripleName with the updated triple from the target lookup. 257 TripleName = TheTriple.str(); 258 259 // Return the found target. 260 return TheTarget; 261 } 262 263 ErrorOr<std::unique_ptr<ToolOutputFile>> getOutputStream() { 264 if (OutputFilename == "") 265 OutputFilename = "-"; 266 std::error_code EC; 267 auto Out = std::make_unique<ToolOutputFile>(OutputFilename, EC, 268 sys::fs::OF_TextWithCRLF); 269 if (!EC) 270 return std::move(Out); 271 return EC; 272 } 273 } // end of anonymous namespace 274 275 static void processOptionImpl(cl::opt<bool> &O, const cl::opt<bool> &Default) { 276 if (!O.getNumOccurrences() || O.getPosition() < Default.getPosition()) 277 O = Default.getValue(); 278 } 279 280 static void processViewOptions(bool IsOutOfOrder) { 281 if (!EnableAllViews.getNumOccurrences() && 282 !EnableAllStats.getNumOccurrences()) 283 return; 284 285 if (EnableAllViews.getNumOccurrences()) { 286 processOptionImpl(PrintSummaryView, EnableAllViews); 287 if (IsOutOfOrder) 288 processOptionImpl(EnableBottleneckAnalysis, EnableAllViews); 289 processOptionImpl(PrintResourcePressureView, EnableAllViews); 290 processOptionImpl(PrintTimelineView, EnableAllViews); 291 processOptionImpl(PrintInstructionInfoView, EnableAllViews); 292 } 293 294 const cl::opt<bool> &Default = 295 EnableAllViews.getPosition() < EnableAllStats.getPosition() 296 ? EnableAllStats 297 : EnableAllViews; 298 processOptionImpl(PrintRegisterFileStats, Default); 299 processOptionImpl(PrintDispatchStats, Default); 300 processOptionImpl(PrintSchedulerStats, Default); 301 if (IsOutOfOrder) 302 processOptionImpl(PrintRetireStats, Default); 303 } 304 305 // Returns true on success. 306 static bool runPipeline(mca::Pipeline &P) { 307 // Handle pipeline errors here. 308 Expected<unsigned> Cycles = P.run(); 309 if (!Cycles) { 310 WithColor::error() << toString(Cycles.takeError()); 311 return false; 312 } 313 return true; 314 } 315 316 int main(int argc, char **argv) { 317 InitLLVM X(argc, argv); 318 319 // Initialize targets and assembly parsers. 320 InitializeAllTargetInfos(); 321 InitializeAllTargetMCs(); 322 InitializeAllAsmParsers(); 323 InitializeAllTargetMCAs(); 324 325 // Register the Target and CPU printer for --version. 326 cl::AddExtraVersionPrinter(sys::printDefaultTargetAndDetectedCPU); 327 328 // Enable printing of available targets when flag --version is specified. 329 cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion); 330 331 cl::HideUnrelatedOptions({&ToolOptions, &ViewOptions}); 332 333 // Parse flags and initialize target options. 334 cl::ParseCommandLineOptions(argc, argv, 335 "llvm machine code performance analyzer.\n"); 336 337 // Get the target from the triple. If a triple is not specified, then select 338 // the default triple for the host. If the triple doesn't correspond to any 339 // registered target, then exit with an error message. 340 const char *ProgName = argv[0]; 341 const Target *TheTarget = getTarget(ProgName); 342 if (!TheTarget) 343 return 1; 344 345 // GetTarget() may replaced TripleName with a default triple. 346 // For safety, reconstruct the Triple object. 347 Triple TheTriple(TripleName); 348 349 ErrorOr<std::unique_ptr<MemoryBuffer>> BufferPtr = 350 MemoryBuffer::getFileOrSTDIN(InputFilename); 351 if (std::error_code EC = BufferPtr.getError()) { 352 WithColor::error() << InputFilename << ": " << EC.message() << '\n'; 353 return 1; 354 } 355 356 if (MCPU == "native") 357 MCPU = std::string(llvm::sys::getHostCPUName()); 358 359 // Package up features to be passed to target/subtarget 360 std::string FeaturesStr; 361 if (MATTRS.size()) { 362 SubtargetFeatures Features; 363 for (std::string &MAttr : MATTRS) 364 Features.AddFeature(MAttr); 365 FeaturesStr = Features.getString(); 366 } 367 368 std::unique_ptr<MCSubtargetInfo> STI( 369 TheTarget->createMCSubtargetInfo(TripleName, MCPU, FeaturesStr)); 370 assert(STI && "Unable to create subtarget info!"); 371 if (!STI->isCPUStringValid(MCPU)) 372 return 1; 373 374 if (!STI->getSchedModel().hasInstrSchedModel()) { 375 WithColor::error() 376 << "unable to find instruction-level scheduling information for" 377 << " target triple '" << TheTriple.normalize() << "' and cpu '" << MCPU 378 << "'.\n"; 379 380 if (STI->getSchedModel().InstrItineraries) 381 WithColor::note() 382 << "cpu '" << MCPU << "' provides itineraries. However, " 383 << "instruction itineraries are currently unsupported.\n"; 384 return 1; 385 } 386 387 // Apply overrides to llvm-mca specific options. 388 bool IsOutOfOrder = STI->getSchedModel().isOutOfOrder(); 389 processViewOptions(IsOutOfOrder); 390 391 std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName)); 392 assert(MRI && "Unable to create target register info!"); 393 394 MCTargetOptions MCOptions = mc::InitMCTargetOptionsFromFlags(); 395 std::unique_ptr<MCAsmInfo> MAI( 396 TheTarget->createMCAsmInfo(*MRI, TripleName, MCOptions)); 397 assert(MAI && "Unable to create target asm info!"); 398 399 SourceMgr SrcMgr; 400 401 // Tell SrcMgr about this buffer, which is what the parser will pick up. 402 SrcMgr.AddNewSourceBuffer(std::move(*BufferPtr), SMLoc()); 403 404 MCContext Ctx(TheTriple, MAI.get(), MRI.get(), STI.get(), &SrcMgr); 405 std::unique_ptr<MCObjectFileInfo> MOFI( 406 TheTarget->createMCObjectFileInfo(Ctx, /*PIC=*/false)); 407 Ctx.setObjectFileInfo(MOFI.get()); 408 409 std::unique_ptr<buffer_ostream> BOS; 410 411 std::unique_ptr<MCInstrInfo> MCII(TheTarget->createMCInstrInfo()); 412 assert(MCII && "Unable to create instruction info!"); 413 414 std::unique_ptr<MCInstrAnalysis> MCIA( 415 TheTarget->createMCInstrAnalysis(MCII.get())); 416 417 // Need to initialize an MCInstPrinter as it is 418 // required for initializing the MCTargetStreamer 419 // which needs to happen within the CRG.parseAnalysisRegions() call below. 420 // Without an MCTargetStreamer, certain assembly directives can trigger a 421 // segfault. (For example, the .cv_fpo_proc directive on x86 will segfault if 422 // we don't initialize the MCTargetStreamer.) 423 unsigned IPtempOutputAsmVariant = 424 OutputAsmVariant == -1 ? 0 : OutputAsmVariant; 425 std::unique_ptr<MCInstPrinter> IPtemp(TheTarget->createMCInstPrinter( 426 Triple(TripleName), IPtempOutputAsmVariant, *MAI, *MCII, *MRI)); 427 if (!IPtemp) { 428 WithColor::error() 429 << "unable to create instruction printer for target triple '" 430 << TheTriple.normalize() << "' with assembly variant " 431 << IPtempOutputAsmVariant << ".\n"; 432 return 1; 433 } 434 435 // Parse the input and create CodeRegions that llvm-mca can analyze. 436 mca::AsmAnalysisRegionGenerator CRG(*TheTarget, SrcMgr, Ctx, *MAI, *STI, 437 *MCII); 438 Expected<const mca::AnalysisRegions &> RegionsOrErr = 439 CRG.parseAnalysisRegions(std::move(IPtemp)); 440 if (!RegionsOrErr) { 441 if (auto Err = 442 handleErrors(RegionsOrErr.takeError(), [](const StringError &E) { 443 WithColor::error() << E.getMessage() << '\n'; 444 })) { 445 // Default case. 446 WithColor::error() << toString(std::move(Err)) << '\n'; 447 } 448 return 1; 449 } 450 const mca::AnalysisRegions &Regions = *RegionsOrErr; 451 452 // Early exit if errors were found by the code region parsing logic. 453 if (!Regions.isValid()) 454 return 1; 455 456 if (Regions.empty()) { 457 WithColor::error() << "no assembly instructions found.\n"; 458 return 1; 459 } 460 461 std::unique_ptr<mca::InstrumentManager> IM; 462 if (!DisableInstrumentManager) { 463 IM = std::unique_ptr<mca::InstrumentManager>( 464 TheTarget->createInstrumentManager(*STI, *MCII)); 465 } 466 if (!IM) { 467 // If the target doesn't have its own IM implemented (or the -disable-cb 468 // flag is set) then we use the base class (which does nothing). 469 IM = std::make_unique<mca::InstrumentManager>(*STI, *MCII); 470 } 471 472 // Parse the input and create InstrumentRegion that llvm-mca 473 // can use to improve analysis. 474 mca::AsmInstrumentRegionGenerator IRG(*TheTarget, SrcMgr, Ctx, *MAI, *STI, 475 *MCII, *IM); 476 Expected<const mca::InstrumentRegions &> InstrumentRegionsOrErr = 477 IRG.parseInstrumentRegions(std::move(IPtemp)); 478 if (!InstrumentRegionsOrErr) { 479 if (auto Err = handleErrors(InstrumentRegionsOrErr.takeError(), 480 [](const StringError &E) { 481 WithColor::error() << E.getMessage() << '\n'; 482 })) { 483 // Default case. 484 WithColor::error() << toString(std::move(Err)) << '\n'; 485 } 486 return 1; 487 } 488 const mca::InstrumentRegions &InstrumentRegions = *InstrumentRegionsOrErr; 489 490 // Early exit if errors were found by the instrumentation parsing logic. 491 if (!InstrumentRegions.isValid()) 492 return 1; 493 494 // Now initialize the output file. 495 auto OF = getOutputStream(); 496 if (std::error_code EC = OF.getError()) { 497 WithColor::error() << EC.message() << '\n'; 498 return 1; 499 } 500 501 unsigned AssemblerDialect = CRG.getAssemblerDialect(); 502 if (OutputAsmVariant >= 0) 503 AssemblerDialect = static_cast<unsigned>(OutputAsmVariant); 504 std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter( 505 Triple(TripleName), AssemblerDialect, *MAI, *MCII, *MRI)); 506 if (!IP) { 507 WithColor::error() 508 << "unable to create instruction printer for target triple '" 509 << TheTriple.normalize() << "' with assembly variant " 510 << AssemblerDialect << ".\n"; 511 return 1; 512 } 513 514 // Set the display preference for hex vs. decimal immediates. 515 IP->setPrintImmHex(PrintImmHex); 516 517 std::unique_ptr<ToolOutputFile> TOF = std::move(*OF); 518 519 const MCSchedModel &SM = STI->getSchedModel(); 520 521 std::unique_ptr<mca::InstrPostProcess> IPP; 522 if (!DisableCustomBehaviour) { 523 // TODO: It may be a good idea to separate CB and IPP so that they can 524 // be used independently of each other. What I mean by this is to add 525 // an extra command-line arg --disable-ipp so that CB and IPP can be 526 // toggled without needing to toggle both of them together. 527 IPP = std::unique_ptr<mca::InstrPostProcess>( 528 TheTarget->createInstrPostProcess(*STI, *MCII)); 529 } 530 if (!IPP) { 531 // If the target doesn't have its own IPP implemented (or the -disable-cb 532 // flag is set) then we use the base class (which does nothing). 533 IPP = std::make_unique<mca::InstrPostProcess>(*STI, *MCII); 534 } 535 536 // Create an instruction builder. 537 mca::InstrBuilder IB(*STI, *MCII, *MRI, MCIA.get(), *IM); 538 539 // Create a context to control ownership of the pipeline hardware. 540 mca::Context MCA(*MRI, *STI); 541 542 mca::PipelineOptions PO(MicroOpQueue, DecoderThroughput, DispatchWidth, 543 RegisterFileSize, LoadQueueSize, StoreQueueSize, 544 AssumeNoAlias, EnableBottleneckAnalysis); 545 546 // Number each region in the sequence. 547 unsigned RegionIdx = 0; 548 549 std::unique_ptr<MCCodeEmitter> MCE( 550 TheTarget->createMCCodeEmitter(*MCII, Ctx)); 551 assert(MCE && "Unable to create code emitter!"); 552 553 std::unique_ptr<MCAsmBackend> MAB(TheTarget->createMCAsmBackend( 554 *STI, *MRI, mc::InitMCTargetOptionsFromFlags())); 555 assert(MAB && "Unable to create asm backend!"); 556 557 json::Object JSONOutput; 558 for (const std::unique_ptr<mca::AnalysisRegion> &Region : Regions) { 559 // Skip empty code regions. 560 if (Region->empty()) 561 continue; 562 563 IB.clear(); 564 565 // Lower the MCInst sequence into an mca::Instruction sequence. 566 ArrayRef<MCInst> Insts = Region->getInstructions(); 567 mca::CodeEmitter CE(*STI, *MAB, *MCE, Insts); 568 569 IPP->resetState(); 570 571 SmallVector<std::unique_ptr<mca::Instruction>> LoweredSequence; 572 for (const MCInst &MCI : Insts) { 573 SMLoc Loc = MCI.getLoc(); 574 const SmallVector<mca::SharedInstrument> Instruments = 575 InstrumentRegions.getActiveInstruments(Loc); 576 577 Expected<std::unique_ptr<mca::Instruction>> Inst = 578 IB.createInstruction(MCI, Instruments); 579 if (!Inst) { 580 if (auto NewE = handleErrors( 581 Inst.takeError(), 582 [&IP, &STI](const mca::InstructionError<MCInst> &IE) { 583 std::string InstructionStr; 584 raw_string_ostream SS(InstructionStr); 585 WithColor::error() << IE.Message << '\n'; 586 IP->printInst(&IE.Inst, 0, "", *STI, SS); 587 SS.flush(); 588 WithColor::note() 589 << "instruction: " << InstructionStr << '\n'; 590 })) { 591 // Default case. 592 WithColor::error() << toString(std::move(NewE)); 593 } 594 return 1; 595 } 596 597 IPP->postProcessInstruction(Inst.get(), MCI); 598 599 LoweredSequence.emplace_back(std::move(Inst.get())); 600 } 601 602 mca::CircularSourceMgr S(LoweredSequence, 603 PrintInstructionTables ? 1 : Iterations); 604 605 if (PrintInstructionTables) { 606 // Create a pipeline, stages, and a printer. 607 auto P = std::make_unique<mca::Pipeline>(); 608 P->appendStage(std::make_unique<mca::EntryStage>(S)); 609 P->appendStage(std::make_unique<mca::InstructionTables>(SM)); 610 611 mca::PipelinePrinter Printer(*P, *Region, RegionIdx, *STI, PO); 612 if (PrintJson) { 613 Printer.addView( 614 std::make_unique<mca::InstructionView>(*STI, *IP, Insts)); 615 } 616 617 // Create the views for this pipeline, execute, and emit a report. 618 if (PrintInstructionInfoView) { 619 Printer.addView(std::make_unique<mca::InstructionInfoView>( 620 *STI, *MCII, CE, ShowEncoding, Insts, *IP, LoweredSequence, 621 ShowBarriers)); 622 } 623 Printer.addView( 624 std::make_unique<mca::ResourcePressureView>(*STI, *IP, Insts)); 625 626 if (!runPipeline(*P)) 627 return 1; 628 629 if (PrintJson) { 630 Printer.printReport(JSONOutput); 631 } else { 632 Printer.printReport(TOF->os()); 633 } 634 635 ++RegionIdx; 636 continue; 637 } 638 639 // Create the CustomBehaviour object for enforcing Target Specific 640 // behaviours and dependencies that aren't expressed well enough 641 // in the tablegen. CB cannot depend on the list of MCInst or 642 // the source code (but it can depend on the list of 643 // mca::Instruction or any objects that can be reconstructed 644 // from the target information). 645 std::unique_ptr<mca::CustomBehaviour> CB; 646 if (!DisableCustomBehaviour) 647 CB = std::unique_ptr<mca::CustomBehaviour>( 648 TheTarget->createCustomBehaviour(*STI, S, *MCII)); 649 if (!CB) 650 // If the target doesn't have its own CB implemented (or the -disable-cb 651 // flag is set) then we use the base class (which does nothing). 652 CB = std::make_unique<mca::CustomBehaviour>(*STI, S, *MCII); 653 654 // Create a basic pipeline simulating an out-of-order backend. 655 auto P = MCA.createDefaultPipeline(PO, S, *CB); 656 657 mca::PipelinePrinter Printer(*P, *Region, RegionIdx, *STI, PO); 658 659 // Targets can define their own custom Views that exist within their 660 // /lib/Target/ directory so that the View can utilize their CustomBehaviour 661 // or other backend symbols / functionality that are not already exposed 662 // through one of the MC-layer classes. These Views will be initialized 663 // using the CustomBehaviour::getViews() variants. 664 // If a target makes a custom View that does not depend on their target 665 // CB or their backend, they should put the View within 666 // /tools/llvm-mca/Views/ instead. 667 if (!DisableCustomBehaviour) { 668 std::vector<std::unique_ptr<mca::View>> CBViews = 669 CB->getStartViews(*IP, Insts); 670 for (auto &CBView : CBViews) 671 Printer.addView(std::move(CBView)); 672 } 673 674 // When we output JSON, we add a view that contains the instructions 675 // and CPU resource information. 676 if (PrintJson) { 677 auto IV = std::make_unique<mca::InstructionView>(*STI, *IP, Insts); 678 Printer.addView(std::move(IV)); 679 } 680 681 if (PrintSummaryView) 682 Printer.addView( 683 std::make_unique<mca::SummaryView>(SM, Insts, DispatchWidth)); 684 685 if (EnableBottleneckAnalysis) { 686 if (!IsOutOfOrder) { 687 WithColor::warning() 688 << "bottleneck analysis is not supported for in-order CPU '" << MCPU 689 << "'.\n"; 690 } 691 Printer.addView(std::make_unique<mca::BottleneckAnalysis>( 692 *STI, *IP, Insts, S.getNumIterations())); 693 } 694 695 if (PrintInstructionInfoView) 696 Printer.addView(std::make_unique<mca::InstructionInfoView>( 697 *STI, *MCII, CE, ShowEncoding, Insts, *IP, LoweredSequence, 698 ShowBarriers)); 699 700 // Fetch custom Views that are to be placed after the InstructionInfoView. 701 // Refer to the comment paired with the CB->getStartViews(*IP, Insts); line 702 // for more info. 703 if (!DisableCustomBehaviour) { 704 std::vector<std::unique_ptr<mca::View>> CBViews = 705 CB->getPostInstrInfoViews(*IP, Insts); 706 for (auto &CBView : CBViews) 707 Printer.addView(std::move(CBView)); 708 } 709 710 if (PrintDispatchStats) 711 Printer.addView(std::make_unique<mca::DispatchStatistics>()); 712 713 if (PrintSchedulerStats) 714 Printer.addView(std::make_unique<mca::SchedulerStatistics>(*STI)); 715 716 if (PrintRetireStats) 717 Printer.addView(std::make_unique<mca::RetireControlUnitStatistics>(SM)); 718 719 if (PrintRegisterFileStats) 720 Printer.addView(std::make_unique<mca::RegisterFileStatistics>(*STI)); 721 722 if (PrintResourcePressureView) 723 Printer.addView( 724 std::make_unique<mca::ResourcePressureView>(*STI, *IP, Insts)); 725 726 if (PrintTimelineView) { 727 unsigned TimelineIterations = 728 TimelineMaxIterations ? TimelineMaxIterations : 10; 729 Printer.addView(std::make_unique<mca::TimelineView>( 730 *STI, *IP, Insts, std::min(TimelineIterations, S.getNumIterations()), 731 TimelineMaxCycles)); 732 } 733 734 // Fetch custom Views that are to be placed after all other Views. 735 // Refer to the comment paired with the CB->getStartViews(*IP, Insts); line 736 // for more info. 737 if (!DisableCustomBehaviour) { 738 std::vector<std::unique_ptr<mca::View>> CBViews = 739 CB->getEndViews(*IP, Insts); 740 for (auto &CBView : CBViews) 741 Printer.addView(std::move(CBView)); 742 } 743 744 if (!runPipeline(*P)) 745 return 1; 746 747 if (PrintJson) { 748 Printer.printReport(JSONOutput); 749 } else { 750 Printer.printReport(TOF->os()); 751 } 752 753 ++RegionIdx; 754 } 755 756 if (PrintJson) 757 TOF->os() << formatv("{0:2}", json::Value(std::move(JSONOutput))) << "\n"; 758 759 TOF->keep(); 760 return 0; 761 } 762