1 //===- Parsing, selection, and construction of pass pipelines --*- 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 /// \file 9 /// 10 /// Interfaces for registering analysis passes, producing common pass manager 11 /// configurations, and parsing of pass pipelines. 12 /// 13 //===----------------------------------------------------------------------===// 14 15 #ifndef LLVM_PASSES_PASSBUILDER_H 16 #define LLVM_PASSES_PASSBUILDER_H 17 18 #include "llvm/Analysis/CGSCCPassManager.h" 19 #include "llvm/CodeGen/MachinePassManager.h" 20 #include "llvm/CodeGen/RegAllocCommon.h" 21 #include "llvm/IR/PassManager.h" 22 #include "llvm/Passes/OptimizationLevel.h" 23 #include "llvm/Support/Error.h" 24 #include "llvm/Support/PGOOptions.h" 25 #include "llvm/Support/raw_ostream.h" 26 #include "llvm/Transforms/IPO/Inliner.h" 27 #include "llvm/Transforms/IPO/ModuleInliner.h" 28 #include "llvm/Transforms/Scalar/LoopPassManager.h" 29 #include <optional> 30 #include <vector> 31 32 namespace llvm { 33 class StringRef; 34 class AAManager; 35 class TargetMachine; 36 class ModuleSummaryIndex; 37 template <typename T> class IntrusiveRefCntPtr; 38 namespace vfs { 39 class FileSystem; 40 } // namespace vfs 41 42 /// Tunable parameters for passes in the default pipelines. 43 class PipelineTuningOptions { 44 public: 45 /// Constructor sets pipeline tuning defaults based on cl::opts. Each option 46 /// can be set in the PassBuilder when using a LLVM as a library. 47 PipelineTuningOptions(); 48 49 /// Tuning option to set loop interleaving on/off, set based on opt level. 50 bool LoopInterleaving; 51 52 /// Tuning option to enable/disable loop vectorization, set based on opt 53 /// level. 54 bool LoopVectorization; 55 56 /// Tuning option to enable/disable slp loop vectorization, set based on opt 57 /// level. 58 bool SLPVectorization; 59 60 /// Tuning option to enable/disable loop unrolling. Its default value is true. 61 bool LoopUnrolling; 62 63 /// Tuning option to forget all SCEV loops in LoopUnroll. Its default value 64 /// is that of the flag: `-forget-scev-loop-unroll`. 65 bool ForgetAllSCEVInLoopUnroll; 66 67 /// Tuning option to cap the number of calls to retrive clobbering accesses in 68 /// MemorySSA, in LICM. 69 unsigned LicmMssaOptCap; 70 71 /// Tuning option to disable promotion to scalars in LICM with MemorySSA, if 72 /// the number of access is too large. 73 unsigned LicmMssaNoAccForPromotionCap; 74 75 /// Tuning option to enable/disable call graph profile. Its default value is 76 /// that of the flag: `-enable-npm-call-graph-profile`. 77 bool CallGraphProfile; 78 79 // Add LTO pipeline tuning option to enable the unified LTO pipeline. 80 bool UnifiedLTO; 81 82 /// Tuning option to enable/disable function merging. Its default value is 83 /// false. 84 bool MergeFunctions; 85 86 /// Tuning option to override the default inliner threshold. 87 int InlinerThreshold; 88 89 // Experimental option to eagerly invalidate more analyses. This has the 90 // potential to decrease max memory usage in exchange for more compile time. 91 // This may affect codegen due to either passes using analyses only when 92 // cached, or invalidating and recalculating an analysis that was 93 // stale/imprecise but still valid. Currently this invalidates all function 94 // analyses after various module->function or cgscc->function adaptors in the 95 // default pipelines. 96 bool EagerlyInvalidateAnalyses; 97 }; 98 99 /// This class provides access to building LLVM's passes. 100 /// 101 /// Its members provide the baseline state available to passes during their 102 /// construction. The \c PassRegistry.def file specifies how to construct all 103 /// of the built-in passes, and those may reference these members during 104 /// construction. 105 class PassBuilder { 106 TargetMachine *TM; 107 PipelineTuningOptions PTO; 108 std::optional<PGOOptions> PGOOpt; 109 PassInstrumentationCallbacks *PIC; 110 111 public: 112 /// A struct to capture parsed pass pipeline names. 113 /// 114 /// A pipeline is defined as a series of names, each of which may in itself 115 /// recursively contain a nested pipeline. A name is either the name of a pass 116 /// (e.g. "instcombine") or the name of a pipeline type (e.g. "cgscc"). If the 117 /// name is the name of a pass, the InnerPipeline is empty, since passes 118 /// cannot contain inner pipelines. See parsePassPipeline() for a more 119 /// detailed description of the textual pipeline format. 120 struct PipelineElement { 121 StringRef Name; 122 std::vector<PipelineElement> InnerPipeline; 123 }; 124 125 explicit PassBuilder(TargetMachine *TM = nullptr, 126 PipelineTuningOptions PTO = PipelineTuningOptions(), 127 std::optional<PGOOptions> PGOOpt = std::nullopt, 128 PassInstrumentationCallbacks *PIC = nullptr); 129 130 /// Cross register the analysis managers through their proxies. 131 /// 132 /// This is an interface that can be used to cross register each 133 /// AnalysisManager with all the others analysis managers. 134 void crossRegisterProxies(LoopAnalysisManager &LAM, 135 FunctionAnalysisManager &FAM, 136 CGSCCAnalysisManager &CGAM, 137 ModuleAnalysisManager &MAM, 138 MachineFunctionAnalysisManager *MFAM = nullptr); 139 140 /// Registers all available module analysis passes. 141 /// 142 /// This is an interface that can be used to populate a \c 143 /// ModuleAnalysisManager with all registered module analyses. Callers can 144 /// still manually register any additional analyses. Callers can also 145 /// pre-register analyses and this will not override those. 146 void registerModuleAnalyses(ModuleAnalysisManager &MAM); 147 148 /// Registers all available CGSCC analysis passes. 149 /// 150 /// This is an interface that can be used to populate a \c CGSCCAnalysisManager 151 /// with all registered CGSCC analyses. Callers can still manually register any 152 /// additional analyses. Callers can also pre-register analyses and this will 153 /// not override those. 154 void registerCGSCCAnalyses(CGSCCAnalysisManager &CGAM); 155 156 /// Registers all available function analysis passes. 157 /// 158 /// This is an interface that can be used to populate a \c 159 /// FunctionAnalysisManager with all registered function analyses. Callers can 160 /// still manually register any additional analyses. Callers can also 161 /// pre-register analyses and this will not override those. 162 void registerFunctionAnalyses(FunctionAnalysisManager &FAM); 163 164 /// Registers all available loop analysis passes. 165 /// 166 /// This is an interface that can be used to populate a \c LoopAnalysisManager 167 /// with all registered loop analyses. Callers can still manually register any 168 /// additional analyses. 169 void registerLoopAnalyses(LoopAnalysisManager &LAM); 170 171 /// Registers all available machine function analysis passes. 172 /// 173 /// This is an interface that can be used to populate a \c 174 /// MachineFunctionAnalysisManager with all registered function analyses. 175 /// Callers can still manually register any additional analyses. Callers can 176 /// also pre-register analyses and this will not override those. 177 void registerMachineFunctionAnalyses(MachineFunctionAnalysisManager &MFAM); 178 179 /// Construct the core LLVM function canonicalization and simplification 180 /// pipeline. 181 /// 182 /// This is a long pipeline and uses most of the per-function optimization 183 /// passes in LLVM to canonicalize and simplify the IR. It is suitable to run 184 /// repeatedly over the IR and is not expected to destroy important 185 /// information about the semantics of the IR. 186 /// 187 /// Note that \p Level cannot be `O0` here. The pipelines produced are 188 /// only intended for use when attempting to optimize code. If frontends 189 /// require some transformations for semantic reasons, they should explicitly 190 /// build them. 191 /// 192 /// \p Phase indicates the current ThinLTO phase. 193 FunctionPassManager 194 buildFunctionSimplificationPipeline(OptimizationLevel Level, 195 ThinOrFullLTOPhase Phase); 196 197 /// Construct the core LLVM module canonicalization and simplification 198 /// pipeline. 199 /// 200 /// This pipeline focuses on canonicalizing and simplifying the entire module 201 /// of IR. Much like the function simplification pipeline above, it is 202 /// suitable to run repeatedly over the IR and is not expected to destroy 203 /// important information. It does, however, perform inlining and other 204 /// heuristic based simplifications that are not strictly reversible. 205 /// 206 /// Note that \p Level cannot be `O0` here. The pipelines produced are 207 /// only intended for use when attempting to optimize code. If frontends 208 /// require some transformations for semantic reasons, they should explicitly 209 /// build them. 210 /// 211 /// \p Phase indicates the current ThinLTO phase. 212 ModulePassManager buildModuleSimplificationPipeline(OptimizationLevel Level, 213 ThinOrFullLTOPhase Phase); 214 215 /// Construct the module pipeline that performs inlining as well as 216 /// the inlining-driven cleanups. 217 ModuleInlinerWrapperPass buildInlinerPipeline(OptimizationLevel Level, 218 ThinOrFullLTOPhase Phase); 219 220 /// Construct the module pipeline that performs inlining with 221 /// module inliner pass. 222 ModulePassManager buildModuleInlinerPipeline(OptimizationLevel Level, 223 ThinOrFullLTOPhase Phase); 224 225 /// Construct the core LLVM module optimization pipeline. 226 /// 227 /// This pipeline focuses on optimizing the execution speed of the IR. It 228 /// uses cost modeling and thresholds to balance code growth against runtime 229 /// improvements. It includes vectorization and other information destroying 230 /// transformations. It also cannot generally be run repeatedly on a module 231 /// without potentially seriously regressing either runtime performance of 232 /// the code or serious code size growth. 233 /// 234 /// Note that \p Level cannot be `O0` here. The pipelines produced are 235 /// only intended for use when attempting to optimize code. If frontends 236 /// require some transformations for semantic reasons, they should explicitly 237 /// build them. 238 ModulePassManager 239 buildModuleOptimizationPipeline(OptimizationLevel Level, 240 ThinOrFullLTOPhase LTOPhase); 241 242 /// Build a per-module default optimization pipeline. 243 /// 244 /// This provides a good default optimization pipeline for per-module 245 /// optimization and code generation without any link-time optimization. It 246 /// typically correspond to frontend "-O[123]" options for optimization 247 /// levels \c O1, \c O2 and \c O3 resp. 248 ModulePassManager buildPerModuleDefaultPipeline( 249 OptimizationLevel Level, 250 ThinOrFullLTOPhase Phase = ThinOrFullLTOPhase::None); 251 252 /// Build a fat object default optimization pipeline. 253 /// 254 /// This builds a pipeline that runs the LTO/ThinLTO pre-link pipeline, and 255 /// emits a section containing the pre-link bitcode along side the object code 256 /// generated in non-LTO compilation. 257 ModulePassManager buildFatLTODefaultPipeline(OptimizationLevel Level, 258 bool ThinLTO, bool EmitSummary); 259 260 /// Build a pre-link, ThinLTO-targeting default optimization pipeline to 261 /// a pass manager. 262 /// 263 /// This adds the pre-link optimizations tuned to prepare a module for 264 /// a ThinLTO run. It works to minimize the IR which needs to be analyzed 265 /// without making irreversible decisions which could be made better during 266 /// the LTO run. 267 ModulePassManager buildThinLTOPreLinkDefaultPipeline(OptimizationLevel Level); 268 269 /// Build a ThinLTO default optimization pipeline to a pass manager. 270 /// 271 /// This provides a good default optimization pipeline for link-time 272 /// optimization and code generation. It is particularly tuned to fit well 273 /// when IR coming into the LTO phase was first run through \c 274 /// buildThinLTOPreLinkDefaultPipeline, and the two coordinate closely. 275 ModulePassManager 276 buildThinLTODefaultPipeline(OptimizationLevel Level, 277 const ModuleSummaryIndex *ImportSummary); 278 279 /// Build a pre-link, LTO-targeting default optimization pipeline to a pass 280 /// manager. 281 /// 282 /// This adds the pre-link optimizations tuned to work well with a later LTO 283 /// run. It works to minimize the IR which needs to be analyzed without 284 /// making irreversible decisions which could be made better during the LTO 285 /// run. 286 ModulePassManager buildLTOPreLinkDefaultPipeline(OptimizationLevel Level); 287 288 /// Build an LTO default optimization pipeline to a pass manager. 289 /// 290 /// This provides a good default optimization pipeline for link-time 291 /// optimization and code generation. It is particularly tuned to fit well 292 /// when IR coming into the LTO phase was first run through \c 293 /// buildLTOPreLinkDefaultPipeline, and the two coordinate closely. 294 ModulePassManager buildLTODefaultPipeline(OptimizationLevel Level, 295 ModuleSummaryIndex *ExportSummary); 296 297 /// Build an O0 pipeline with the minimal semantically required passes. 298 /// 299 /// This should only be used for non-LTO and LTO pre-link pipelines. 300 ModulePassManager 301 buildO0DefaultPipeline(OptimizationLevel Level, 302 ThinOrFullLTOPhase Phase = ThinOrFullLTOPhase::None); 303 304 /// Build the default `AAManager` with the default alias analysis pipeline 305 /// registered. 306 /// 307 /// This also adds target-specific alias analyses registered via 308 /// TargetMachine::registerDefaultAliasAnalyses(). 309 AAManager buildDefaultAAPipeline(); 310 311 /// Parse a textual pass pipeline description into a \c 312 /// ModulePassManager. 313 /// 314 /// The format of the textual pass pipeline description looks something like: 315 /// 316 /// module(function(instcombine,sroa),dce,cgscc(inliner,function(...)),...) 317 /// 318 /// Pass managers have ()s describing the nest structure of passes. All passes 319 /// are comma separated. As a special shortcut, if the very first pass is not 320 /// a module pass (as a module pass manager is), this will automatically form 321 /// the shortest stack of pass managers that allow inserting that first pass. 322 /// So, assuming function passes 'fpassN', CGSCC passes 'cgpassN', and loop 323 /// passes 'lpassN', all of these are valid: 324 /// 325 /// fpass1,fpass2,fpass3 326 /// cgpass1,cgpass2,cgpass3 327 /// lpass1,lpass2,lpass3 328 /// 329 /// And they are equivalent to the following (resp.): 330 /// 331 /// module(function(fpass1,fpass2,fpass3)) 332 /// module(cgscc(cgpass1,cgpass2,cgpass3)) 333 /// module(function(loop(lpass1,lpass2,lpass3))) 334 /// 335 /// This shortcut is especially useful for debugging and testing small pass 336 /// combinations. 337 /// 338 /// The sequence of passes aren't necessarily the exact same kind of pass. 339 /// You can mix different levels implicitly if adaptor passes are defined to 340 /// make them work. For example, 341 /// 342 /// mpass1,fpass1,fpass2,mpass2,lpass1 343 /// 344 /// This pipeline uses only one pass manager: the top-level module manager. 345 /// fpass1,fpass2 and lpass1 are added into the top-level module manager 346 /// using only adaptor passes. No nested function/loop pass managers are 347 /// added. The purpose is to allow easy pass testing when the user 348 /// specifically want the pass to run under a adaptor directly. This is 349 /// preferred when a pipeline is largely of one type, but one or just a few 350 /// passes are of different types(See PassBuilder.cpp for examples). 351 Error parsePassPipeline(ModulePassManager &MPM, StringRef PipelineText); 352 353 /// {{@ Parse a textual pass pipeline description into a specific PassManager 354 /// 355 /// Automatic deduction of an appropriate pass manager stack is not supported. 356 /// For example, to insert a loop pass 'lpass' into a FunctionPassManager, 357 /// this is the valid pipeline text: 358 /// 359 /// function(lpass) 360 Error parsePassPipeline(CGSCCPassManager &CGPM, StringRef PipelineText); 361 Error parsePassPipeline(FunctionPassManager &FPM, StringRef PipelineText); 362 Error parsePassPipeline(LoopPassManager &LPM, StringRef PipelineText); 363 /// @}} 364 365 /// Parse a textual MIR pipeline into the provided \c MachineFunctionPass 366 /// manager. 367 /// The format of the textual machine pipeline is a comma separated list of 368 /// machine pass names: 369 /// 370 /// machine-funciton-pass,machine-module-pass,... 371 /// 372 /// There is no need to specify the pass nesting, and this function 373 /// currently cannot handle the pass nesting. 374 Error parsePassPipeline(MachineFunctionPassManager &MFPM, 375 StringRef PipelineText); 376 377 /// Parse a textual alias analysis pipeline into the provided AA manager. 378 /// 379 /// The format of the textual AA pipeline is a comma separated list of AA 380 /// pass names: 381 /// 382 /// basic-aa,globals-aa,... 383 /// 384 /// The AA manager is set up such that the provided alias analyses are tried 385 /// in the order specified. See the \c AAManaager documentation for details 386 /// about the logic used. This routine just provides the textual mapping 387 /// between AA names and the analyses to register with the manager. 388 /// 389 /// Returns false if the text cannot be parsed cleanly. The specific state of 390 /// the \p AA manager is unspecified if such an error is encountered and this 391 /// returns false. 392 Error parseAAPipeline(AAManager &AA, StringRef PipelineText); 393 394 /// Parse RegAllocFilterName to get RegAllocFilterFunc. 395 std::optional<RegAllocFilterFunc> 396 parseRegAllocFilter(StringRef RegAllocFilterName); 397 398 /// Print pass names. 399 void printPassNames(raw_ostream &OS); 400 401 /// Register a callback for a default optimizer pipeline extension 402 /// point 403 /// 404 /// This extension point allows adding passes that perform peephole 405 /// optimizations similar to the instruction combiner. These passes will be 406 /// inserted after each instance of the instruction combiner pass. 407 void registerPeepholeEPCallback( 408 const std::function<void(FunctionPassManager &, OptimizationLevel)> &C) { 409 PeepholeEPCallbacks.push_back(C); 410 } 411 412 /// Register a callback for a default optimizer pipeline extension 413 /// point 414 /// 415 /// This extension point allows adding late loop canonicalization and 416 /// simplification passes. This is the last point in the loop optimization 417 /// pipeline before loop deletion. Each pass added 418 /// here must be an instance of LoopPass. 419 /// This is the place to add passes that can remove loops, such as target- 420 /// specific loop idiom recognition. 421 void registerLateLoopOptimizationsEPCallback( 422 const std::function<void(LoopPassManager &, OptimizationLevel)> &C) { 423 LateLoopOptimizationsEPCallbacks.push_back(C); 424 } 425 426 /// Register a callback for a default optimizer pipeline extension 427 /// point 428 /// 429 /// This extension point allows adding loop passes to the end of the loop 430 /// optimizer. 431 void registerLoopOptimizerEndEPCallback( 432 const std::function<void(LoopPassManager &, OptimizationLevel)> &C) { 433 LoopOptimizerEndEPCallbacks.push_back(C); 434 } 435 436 /// Register a callback for a default optimizer pipeline extension 437 /// point 438 /// 439 /// This extension point allows adding optimization passes after most of the 440 /// main optimizations, but before the last cleanup-ish optimizations. 441 void registerScalarOptimizerLateEPCallback( 442 const std::function<void(FunctionPassManager &, OptimizationLevel)> &C) { 443 ScalarOptimizerLateEPCallbacks.push_back(C); 444 } 445 446 /// Register a callback for a default optimizer pipeline extension 447 /// point 448 /// 449 /// This extension point allows adding CallGraphSCC passes at the end of the 450 /// main CallGraphSCC passes and before any function simplification passes run 451 /// by CGPassManager. 452 void registerCGSCCOptimizerLateEPCallback( 453 const std::function<void(CGSCCPassManager &, OptimizationLevel)> &C) { 454 CGSCCOptimizerLateEPCallbacks.push_back(C); 455 } 456 457 /// Register a callback for a default optimizer pipeline extension 458 /// point 459 /// 460 /// This extension point allows adding optimization passes before the 461 /// vectorizer and other highly target specific optimization passes are 462 /// executed. 463 void registerVectorizerStartEPCallback( 464 const std::function<void(FunctionPassManager &, OptimizationLevel)> &C) { 465 VectorizerStartEPCallbacks.push_back(C); 466 } 467 468 /// Register a callback for a default optimizer pipeline extension 469 /// point 470 /// 471 /// This extension point allows adding optimization passes after the 472 /// vectorizer and other highly target specific optimization passes are 473 /// executed. 474 void registerVectorizerEndEPCallback( 475 const std::function<void(FunctionPassManager &, OptimizationLevel)> &C) { 476 VectorizerEndEPCallbacks.push_back(C); 477 } 478 479 /// Register a callback for a default optimizer pipeline extension point. 480 /// 481 /// This extension point allows adding optimization once at the start of the 482 /// pipeline. This does not apply to 'backend' compiles (LTO and ThinLTO 483 /// link-time pipelines). 484 void registerPipelineStartEPCallback( 485 const std::function<void(ModulePassManager &, OptimizationLevel)> &C) { 486 PipelineStartEPCallbacks.push_back(C); 487 } 488 489 /// Register a callback for a default optimizer pipeline extension point. 490 /// 491 /// This extension point allows adding optimization right after passes that do 492 /// basic simplification of the input IR. 493 void registerPipelineEarlySimplificationEPCallback( 494 const std::function<void(ModulePassManager &, OptimizationLevel, 495 ThinOrFullLTOPhase)> &C) { 496 PipelineEarlySimplificationEPCallbacks.push_back(C); 497 } 498 499 /// Register a callback for a default optimizer pipeline extension point 500 /// 501 /// This extension point allows adding optimizations before the function 502 /// optimization pipeline. 503 void registerOptimizerEarlyEPCallback( 504 const std::function<void(ModulePassManager &, OptimizationLevel, 505 ThinOrFullLTOPhase Phase)> &C) { 506 OptimizerEarlyEPCallbacks.push_back(C); 507 } 508 509 /// Register a callback for a default optimizer pipeline extension point 510 /// 511 /// This extension point allows adding optimizations at the very end of the 512 /// function optimization pipeline. 513 void registerOptimizerLastEPCallback( 514 const std::function<void(ModulePassManager &, OptimizationLevel, 515 ThinOrFullLTOPhase)> &C) { 516 OptimizerLastEPCallbacks.push_back(C); 517 } 518 519 /// Register a callback for a default optimizer pipeline extension point 520 /// 521 /// This extension point allows adding optimizations at the start of the full 522 /// LTO pipeline. 523 void registerFullLinkTimeOptimizationEarlyEPCallback( 524 const std::function<void(ModulePassManager &, OptimizationLevel)> &C) { 525 FullLinkTimeOptimizationEarlyEPCallbacks.push_back(C); 526 } 527 528 /// Register a callback for a default optimizer pipeline extension point 529 /// 530 /// This extension point allows adding optimizations at the end of the full 531 /// LTO pipeline. 532 void registerFullLinkTimeOptimizationLastEPCallback( 533 const std::function<void(ModulePassManager &, OptimizationLevel)> &C) { 534 FullLinkTimeOptimizationLastEPCallbacks.push_back(C); 535 } 536 537 /// Register a callback for parsing an AliasAnalysis Name to populate 538 /// the given AAManager \p AA 539 void registerParseAACallback( 540 const std::function<bool(StringRef Name, AAManager &AA)> &C) { 541 AAParsingCallbacks.push_back(C); 542 } 543 544 /// {{@ Register callbacks for analysis registration with this PassBuilder 545 /// instance. 546 /// Callees register their analyses with the given AnalysisManager objects. 547 void registerAnalysisRegistrationCallback( 548 const std::function<void(CGSCCAnalysisManager &)> &C) { 549 CGSCCAnalysisRegistrationCallbacks.push_back(C); 550 } 551 void registerAnalysisRegistrationCallback( 552 const std::function<void(FunctionAnalysisManager &)> &C) { 553 FunctionAnalysisRegistrationCallbacks.push_back(C); 554 } 555 void registerAnalysisRegistrationCallback( 556 const std::function<void(LoopAnalysisManager &)> &C) { 557 LoopAnalysisRegistrationCallbacks.push_back(C); 558 } 559 void registerAnalysisRegistrationCallback( 560 const std::function<void(ModuleAnalysisManager &)> &C) { 561 ModuleAnalysisRegistrationCallbacks.push_back(C); 562 } 563 void registerAnalysisRegistrationCallback( 564 const std::function<void(MachineFunctionAnalysisManager &)> &C) { 565 MachineFunctionAnalysisRegistrationCallbacks.push_back(C); 566 } 567 /// @}} 568 569 /// {{@ Register pipeline parsing callbacks with this pass builder instance. 570 /// Using these callbacks, callers can parse both a single pass name, as well 571 /// as entire sub-pipelines, and populate the PassManager instance 572 /// accordingly. 573 void registerPipelineParsingCallback( 574 const std::function<bool(StringRef Name, CGSCCPassManager &, 575 ArrayRef<PipelineElement>)> &C) { 576 CGSCCPipelineParsingCallbacks.push_back(C); 577 } 578 void registerPipelineParsingCallback( 579 const std::function<bool(StringRef Name, FunctionPassManager &, 580 ArrayRef<PipelineElement>)> &C) { 581 FunctionPipelineParsingCallbacks.push_back(C); 582 } 583 void registerPipelineParsingCallback( 584 const std::function<bool(StringRef Name, LoopPassManager &, 585 ArrayRef<PipelineElement>)> &C) { 586 LoopPipelineParsingCallbacks.push_back(C); 587 } 588 void registerPipelineParsingCallback( 589 const std::function<bool(StringRef Name, ModulePassManager &, 590 ArrayRef<PipelineElement>)> &C) { 591 ModulePipelineParsingCallbacks.push_back(C); 592 } 593 void registerPipelineParsingCallback( 594 const std::function<bool(StringRef Name, MachineFunctionPassManager &, 595 ArrayRef<PipelineElement>)> &C) { 596 MachineFunctionPipelineParsingCallbacks.push_back(C); 597 } 598 /// @}} 599 600 /// Register callbacks to parse target specific filter field if regalloc pass 601 /// needs it. E.g. AMDGPU requires regalloc passes can handle sgpr and vgpr 602 /// separately. 603 void registerRegClassFilterParsingCallback( 604 const std::function<RegAllocFilterFunc(StringRef)> &C) { 605 RegClassFilterParsingCallbacks.push_back(C); 606 } 607 608 /// Register a callback for a top-level pipeline entry. 609 /// 610 /// If the PassManager type is not given at the top level of the pipeline 611 /// text, this Callback should be used to determine the appropriate stack of 612 /// PassManagers and populate the passed ModulePassManager. 613 void registerParseTopLevelPipelineCallback( 614 const std::function<bool(ModulePassManager &, ArrayRef<PipelineElement>)> 615 &C); 616 617 /// Add PGOInstrumenation passes for O0 only. 618 void addPGOInstrPassesForO0(ModulePassManager &MPM, bool RunProfileGen, 619 bool IsCS, bool AtomicCounterUpdate, 620 std::string ProfileFile, 621 std::string ProfileRemappingFile, 622 IntrusiveRefCntPtr<vfs::FileSystem> FS); 623 624 /// Returns PIC. External libraries can use this to register pass 625 /// instrumentation callbacks. 626 PassInstrumentationCallbacks *getPassInstrumentationCallbacks() const { 627 return PIC; 628 } 629 630 // Invoke the callbacks registered for the various extension points. 631 // Custom pipelines should use these to invoke the callbacks registered 632 // by TargetMachines and other clients. 633 void invokePeepholeEPCallbacks(FunctionPassManager &FPM, 634 OptimizationLevel Level); 635 void invokeLateLoopOptimizationsEPCallbacks(LoopPassManager &LPM, 636 OptimizationLevel Level); 637 void invokeLoopOptimizerEndEPCallbacks(LoopPassManager &LPM, 638 OptimizationLevel Level); 639 void invokeScalarOptimizerLateEPCallbacks(FunctionPassManager &FPM, 640 OptimizationLevel Level); 641 void invokeCGSCCOptimizerLateEPCallbacks(CGSCCPassManager &CGPM, 642 OptimizationLevel Level); 643 void invokeVectorizerStartEPCallbacks(FunctionPassManager &FPM, 644 OptimizationLevel Level); 645 void invokeVectorizerEndEPCallbacks(FunctionPassManager &FPM, 646 OptimizationLevel Level); 647 void invokeOptimizerEarlyEPCallbacks(ModulePassManager &MPM, 648 OptimizationLevel Level, 649 ThinOrFullLTOPhase Phase); 650 void invokeOptimizerLastEPCallbacks(ModulePassManager &MPM, 651 OptimizationLevel Level, 652 ThinOrFullLTOPhase Phase); 653 void invokeFullLinkTimeOptimizationEarlyEPCallbacks(ModulePassManager &MPM, 654 OptimizationLevel Level); 655 void invokeFullLinkTimeOptimizationLastEPCallbacks(ModulePassManager &MPM, 656 OptimizationLevel Level); 657 void invokePipelineStartEPCallbacks(ModulePassManager &MPM, 658 OptimizationLevel Level); 659 void invokePipelineEarlySimplificationEPCallbacks(ModulePassManager &MPM, 660 OptimizationLevel Level, 661 ThinOrFullLTOPhase Phase); 662 663 static bool checkParametrizedPassName(StringRef Name, StringRef PassName) { 664 if (!Name.consume_front(PassName)) 665 return false; 666 // normal pass name w/o parameters == default parameters 667 if (Name.empty()) 668 return true; 669 return Name.starts_with("<") && Name.ends_with(">"); 670 } 671 672 /// This performs customized parsing of pass name with parameters. 673 /// 674 /// We do not need parametrization of passes in textual pipeline very often, 675 /// yet on a rare occasion ability to specify parameters right there can be 676 /// useful. 677 /// 678 /// \p Name - parameterized specification of a pass from a textual pipeline 679 /// is a string in a form of : 680 /// PassName '<' parameter-list '>' 681 /// 682 /// Parameter list is being parsed by the parser callable argument, \p Parser, 683 /// It takes a string-ref of parameters and returns either StringError or a 684 /// parameter list in a form of a custom parameters type, all wrapped into 685 /// Expected<> template class. 686 /// 687 template <typename ParametersParseCallableT> 688 static auto parsePassParameters(ParametersParseCallableT &&Parser, 689 StringRef Name, StringRef PassName) 690 -> decltype(Parser(StringRef{})) { 691 using ParametersT = typename decltype(Parser(StringRef{}))::value_type; 692 693 StringRef Params = Name; 694 if (!Params.consume_front(PassName)) { 695 llvm_unreachable( 696 "unable to strip pass name from parametrized pass specification"); 697 } 698 if (!Params.empty() && 699 (!Params.consume_front("<") || !Params.consume_back(">"))) { 700 llvm_unreachable("invalid format for parametrized pass name"); 701 } 702 703 Expected<ParametersT> Result = Parser(Params); 704 assert((Result || Result.template errorIsA<StringError>()) && 705 "Pass parameter parser can only return StringErrors."); 706 return Result; 707 } 708 709 /// Handle passes only accept one bool-valued parameter. 710 /// 711 /// \return false when Params is empty. 712 static Expected<bool> parseSinglePassOption(StringRef Params, 713 StringRef OptionName, 714 StringRef PassName); 715 716 private: 717 // O1 pass pipeline 718 FunctionPassManager 719 buildO1FunctionSimplificationPipeline(OptimizationLevel Level, 720 ThinOrFullLTOPhase Phase); 721 722 void addRequiredLTOPreLinkPasses(ModulePassManager &MPM); 723 724 void addVectorPasses(OptimizationLevel Level, FunctionPassManager &FPM, 725 bool IsFullLTO); 726 727 static std::optional<std::vector<PipelineElement>> 728 parsePipelineText(StringRef Text); 729 730 Error parseModulePass(ModulePassManager &MPM, const PipelineElement &E); 731 Error parseCGSCCPass(CGSCCPassManager &CGPM, const PipelineElement &E); 732 Error parseFunctionPass(FunctionPassManager &FPM, const PipelineElement &E); 733 Error parseLoopPass(LoopPassManager &LPM, const PipelineElement &E); 734 Error parseMachinePass(MachineFunctionPassManager &MFPM, 735 const PipelineElement &E); 736 bool parseAAPassName(AAManager &AA, StringRef Name); 737 738 Error parseMachinePassPipeline(MachineFunctionPassManager &MFPM, 739 ArrayRef<PipelineElement> Pipeline); 740 Error parseLoopPassPipeline(LoopPassManager &LPM, 741 ArrayRef<PipelineElement> Pipeline); 742 Error parseFunctionPassPipeline(FunctionPassManager &FPM, 743 ArrayRef<PipelineElement> Pipeline); 744 Error parseCGSCCPassPipeline(CGSCCPassManager &CGPM, 745 ArrayRef<PipelineElement> Pipeline); 746 Error parseModulePassPipeline(ModulePassManager &MPM, 747 ArrayRef<PipelineElement> Pipeline); 748 749 // Adds passes to do pre-inlining and related cleanup passes before 750 // profile instrumentation/matching (to enable better context sensitivity), 751 // and for memprof to enable better matching with missing debug frames. 752 void addPreInlinerPasses(ModulePassManager &MPM, OptimizationLevel Level, 753 ThinOrFullLTOPhase LTOPhase); 754 755 void addPGOInstrPasses(ModulePassManager &MPM, OptimizationLevel Level, 756 bool RunProfileGen, bool IsCS, 757 bool AtomicCounterUpdate, std::string ProfileFile, 758 std::string ProfileRemappingFile, 759 IntrusiveRefCntPtr<vfs::FileSystem> FS); 760 void addPostPGOLoopRotation(ModulePassManager &MPM, OptimizationLevel Level); 761 762 // Extension Point callbacks 763 SmallVector<std::function<void(FunctionPassManager &, OptimizationLevel)>, 2> 764 PeepholeEPCallbacks; 765 SmallVector<std::function<void(LoopPassManager &, OptimizationLevel)>, 2> 766 LateLoopOptimizationsEPCallbacks; 767 SmallVector<std::function<void(LoopPassManager &, OptimizationLevel)>, 2> 768 LoopOptimizerEndEPCallbacks; 769 SmallVector<std::function<void(FunctionPassManager &, OptimizationLevel)>, 2> 770 ScalarOptimizerLateEPCallbacks; 771 SmallVector<std::function<void(CGSCCPassManager &, OptimizationLevel)>, 2> 772 CGSCCOptimizerLateEPCallbacks; 773 SmallVector<std::function<void(FunctionPassManager &, OptimizationLevel)>, 2> 774 VectorizerStartEPCallbacks; 775 SmallVector<std::function<void(FunctionPassManager &, OptimizationLevel)>, 2> 776 VectorizerEndEPCallbacks; 777 // Module callbacks 778 SmallVector<std::function<void(ModulePassManager &, OptimizationLevel, 779 ThinOrFullLTOPhase)>, 780 2> 781 OptimizerEarlyEPCallbacks; 782 SmallVector<std::function<void(ModulePassManager &, OptimizationLevel, 783 ThinOrFullLTOPhase)>, 784 2> 785 OptimizerLastEPCallbacks; 786 SmallVector<std::function<void(ModulePassManager &, OptimizationLevel)>, 2> 787 FullLinkTimeOptimizationEarlyEPCallbacks; 788 SmallVector<std::function<void(ModulePassManager &, OptimizationLevel)>, 2> 789 FullLinkTimeOptimizationLastEPCallbacks; 790 SmallVector<std::function<void(ModulePassManager &, OptimizationLevel)>, 2> 791 PipelineStartEPCallbacks; 792 SmallVector<std::function<void(ModulePassManager &, OptimizationLevel, 793 ThinOrFullLTOPhase)>, 794 2> 795 PipelineEarlySimplificationEPCallbacks; 796 797 SmallVector<std::function<void(ModuleAnalysisManager &)>, 2> 798 ModuleAnalysisRegistrationCallbacks; 799 SmallVector<std::function<bool(StringRef, ModulePassManager &, 800 ArrayRef<PipelineElement>)>, 801 2> 802 ModulePipelineParsingCallbacks; 803 SmallVector< 804 std::function<bool(ModulePassManager &, ArrayRef<PipelineElement>)>, 2> 805 TopLevelPipelineParsingCallbacks; 806 // CGSCC callbacks 807 SmallVector<std::function<void(CGSCCAnalysisManager &)>, 2> 808 CGSCCAnalysisRegistrationCallbacks; 809 SmallVector<std::function<bool(StringRef, CGSCCPassManager &, 810 ArrayRef<PipelineElement>)>, 811 2> 812 CGSCCPipelineParsingCallbacks; 813 // Function callbacks 814 SmallVector<std::function<void(FunctionAnalysisManager &)>, 2> 815 FunctionAnalysisRegistrationCallbacks; 816 SmallVector<std::function<bool(StringRef, FunctionPassManager &, 817 ArrayRef<PipelineElement>)>, 818 2> 819 FunctionPipelineParsingCallbacks; 820 // Loop callbacks 821 SmallVector<std::function<void(LoopAnalysisManager &)>, 2> 822 LoopAnalysisRegistrationCallbacks; 823 SmallVector<std::function<bool(StringRef, LoopPassManager &, 824 ArrayRef<PipelineElement>)>, 825 2> 826 LoopPipelineParsingCallbacks; 827 // AA callbacks 828 SmallVector<std::function<bool(StringRef Name, AAManager &AA)>, 2> 829 AAParsingCallbacks; 830 // Machine pass callbackcs 831 SmallVector<std::function<void(MachineFunctionAnalysisManager &)>, 2> 832 MachineFunctionAnalysisRegistrationCallbacks; 833 SmallVector<std::function<bool(StringRef, MachineFunctionPassManager &, 834 ArrayRef<PipelineElement>)>, 835 2> 836 MachineFunctionPipelineParsingCallbacks; 837 // Callbacks to parse `filter` parameter in register allocation passes 838 SmallVector<std::function<RegAllocFilterFunc(StringRef)>, 2> 839 RegClassFilterParsingCallbacks; 840 }; 841 842 /// This utility template takes care of adding require<> and invalidate<> 843 /// passes for an analysis to a given \c PassManager. It is intended to be used 844 /// during parsing of a pass pipeline when parsing a single PipelineName. 845 /// When registering a new function analysis FancyAnalysis with the pass 846 /// pipeline name "fancy-analysis", a matching ParsePipelineCallback could look 847 /// like this: 848 /// 849 /// static bool parseFunctionPipeline(StringRef Name, FunctionPassManager &FPM, 850 /// ArrayRef<PipelineElement> P) { 851 /// if (parseAnalysisUtilityPasses<FancyAnalysis>("fancy-analysis", Name, 852 /// FPM)) 853 /// return true; 854 /// return false; 855 /// } 856 template <typename AnalysisT, typename IRUnitT, typename AnalysisManagerT, 857 typename... ExtraArgTs> 858 bool parseAnalysisUtilityPasses( 859 StringRef AnalysisName, StringRef PipelineName, 860 PassManager<IRUnitT, AnalysisManagerT, ExtraArgTs...> &PM) { 861 if (!PipelineName.ends_with(">")) 862 return false; 863 // See if this is an invalidate<> pass name 864 if (PipelineName.starts_with("invalidate<")) { 865 PipelineName = PipelineName.substr(11, PipelineName.size() - 12); 866 if (PipelineName != AnalysisName) 867 return false; 868 PM.addPass(InvalidateAnalysisPass<AnalysisT>()); 869 return true; 870 } 871 872 // See if this is a require<> pass name 873 if (PipelineName.starts_with("require<")) { 874 PipelineName = PipelineName.substr(8, PipelineName.size() - 9); 875 if (PipelineName != AnalysisName) 876 return false; 877 PM.addPass(RequireAnalysisPass<AnalysisT, IRUnitT, AnalysisManagerT, 878 ExtraArgTs...>()); 879 return true; 880 } 881 882 return false; 883 } 884 885 // These are special since they are only for testing purposes. 886 887 /// No-op module pass which does nothing. 888 struct NoOpModulePass : PassInfoMixin<NoOpModulePass> { 889 PreservedAnalyses run(Module &M, ModuleAnalysisManager &) { 890 return PreservedAnalyses::all(); 891 } 892 }; 893 894 /// No-op module analysis. 895 class NoOpModuleAnalysis : public AnalysisInfoMixin<NoOpModuleAnalysis> { 896 friend AnalysisInfoMixin<NoOpModuleAnalysis>; 897 static AnalysisKey Key; 898 899 public: 900 struct Result {}; 901 Result run(Module &, ModuleAnalysisManager &) { return Result(); } 902 }; 903 904 /// No-op CGSCC pass which does nothing. 905 struct NoOpCGSCCPass : PassInfoMixin<NoOpCGSCCPass> { 906 PreservedAnalyses run(LazyCallGraph::SCC &C, CGSCCAnalysisManager &, 907 LazyCallGraph &, CGSCCUpdateResult &UR) { 908 return PreservedAnalyses::all(); 909 } 910 }; 911 912 /// No-op CGSCC analysis. 913 class NoOpCGSCCAnalysis : public AnalysisInfoMixin<NoOpCGSCCAnalysis> { 914 friend AnalysisInfoMixin<NoOpCGSCCAnalysis>; 915 static AnalysisKey Key; 916 917 public: 918 struct Result {}; 919 Result run(LazyCallGraph::SCC &, CGSCCAnalysisManager &, LazyCallGraph &G) { 920 return Result(); 921 } 922 }; 923 924 /// No-op function pass which does nothing. 925 struct NoOpFunctionPass : PassInfoMixin<NoOpFunctionPass> { 926 PreservedAnalyses run(Function &F, FunctionAnalysisManager &) { 927 return PreservedAnalyses::all(); 928 } 929 }; 930 931 /// No-op function analysis. 932 class NoOpFunctionAnalysis : public AnalysisInfoMixin<NoOpFunctionAnalysis> { 933 friend AnalysisInfoMixin<NoOpFunctionAnalysis>; 934 static AnalysisKey Key; 935 936 public: 937 struct Result {}; 938 Result run(Function &, FunctionAnalysisManager &) { return Result(); } 939 }; 940 941 /// No-op loop nest pass which does nothing. 942 struct NoOpLoopNestPass : PassInfoMixin<NoOpLoopNestPass> { 943 PreservedAnalyses run(LoopNest &L, LoopAnalysisManager &, 944 LoopStandardAnalysisResults &, LPMUpdater &) { 945 return PreservedAnalyses::all(); 946 } 947 }; 948 949 /// No-op loop pass which does nothing. 950 struct NoOpLoopPass : PassInfoMixin<NoOpLoopPass> { 951 PreservedAnalyses run(Loop &L, LoopAnalysisManager &, 952 LoopStandardAnalysisResults &, LPMUpdater &) { 953 return PreservedAnalyses::all(); 954 } 955 }; 956 957 /// No-op machine function pass which does nothing. 958 struct NoOpMachineFunctionPass : public PassInfoMixin<NoOpMachineFunctionPass> { 959 PreservedAnalyses run(MachineFunction &, MachineFunctionAnalysisManager &) { 960 return PreservedAnalyses::all(); 961 } 962 }; 963 964 /// No-op loop analysis. 965 class NoOpLoopAnalysis : public AnalysisInfoMixin<NoOpLoopAnalysis> { 966 friend AnalysisInfoMixin<NoOpLoopAnalysis>; 967 static AnalysisKey Key; 968 969 public: 970 struct Result {}; 971 Result run(Loop &, LoopAnalysisManager &, LoopStandardAnalysisResults &) { 972 return Result(); 973 } 974 }; 975 976 /// Common option used by multiple tools to print pipeline passes 977 extern cl::opt<bool> PrintPipelinePasses; 978 979 } 980 981 #endif 982