PassBuilder.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- Parsing, selection, and construction of pass pipelines --*- C++ -*--===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. /// \file
  14. ///
  15. /// Interfaces for registering analysis passes, producing common pass manager
  16. /// configurations, and parsing of pass pipelines.
  17. ///
  18. //===----------------------------------------------------------------------===//
  19. #ifndef LLVM_PASSES_PASSBUILDER_H
  20. #define LLVM_PASSES_PASSBUILDER_H
  21. #include "llvm/Analysis/CGSCCPassManager.h"
  22. #include "llvm/IR/PassManager.h"
  23. #include "llvm/Passes/OptimizationLevel.h"
  24. #include "llvm/Support/Error.h"
  25. #include "llvm/Support/PGOOptions.h"
  26. #include "llvm/Support/raw_ostream.h"
  27. #include "llvm/Transforms/IPO/Inliner.h"
  28. #include "llvm/Transforms/IPO/ModuleInliner.h"
  29. #include "llvm/Transforms/Instrumentation.h"
  30. #include "llvm/Transforms/Scalar/LoopPassManager.h"
  31. #include <vector>
  32. namespace llvm {
  33. class StringRef;
  34. class AAManager;
  35. class TargetMachine;
  36. class ModuleSummaryIndex;
  37. /// Tunable parameters for passes in the default pipelines.
  38. class PipelineTuningOptions {
  39. public:
  40. /// Constructor sets pipeline tuning defaults based on cl::opts. Each option
  41. /// can be set in the PassBuilder when using a LLVM as a library.
  42. PipelineTuningOptions();
  43. /// Tuning option to set loop interleaving on/off, set based on opt level.
  44. bool LoopInterleaving;
  45. /// Tuning option to enable/disable loop vectorization, set based on opt
  46. /// level.
  47. bool LoopVectorization;
  48. /// Tuning option to enable/disable slp loop vectorization, set based on opt
  49. /// level.
  50. bool SLPVectorization;
  51. /// Tuning option to enable/disable loop unrolling. Its default value is true.
  52. bool LoopUnrolling;
  53. /// Tuning option to forget all SCEV loops in LoopUnroll. Its default value
  54. /// is that of the flag: `-forget-scev-loop-unroll`.
  55. bool ForgetAllSCEVInLoopUnroll;
  56. /// Tuning option to cap the number of calls to retrive clobbering accesses in
  57. /// MemorySSA, in LICM.
  58. unsigned LicmMssaOptCap;
  59. /// Tuning option to disable promotion to scalars in LICM with MemorySSA, if
  60. /// the number of access is too large.
  61. unsigned LicmMssaNoAccForPromotionCap;
  62. /// Tuning option to enable/disable call graph profile. Its default value is
  63. /// that of the flag: `-enable-npm-call-graph-profile`.
  64. bool CallGraphProfile;
  65. /// Tuning option to enable/disable function merging. Its default value is
  66. /// false.
  67. bool MergeFunctions;
  68. /// Tuning option to override the default inliner threshold.
  69. int InlinerThreshold;
  70. // Experimental option to eagerly invalidate more analyses. This has the
  71. // potential to decrease max memory usage in exchange for more compile time.
  72. // This may affect codegen due to either passes using analyses only when
  73. // cached, or invalidating and recalculating an analysis that was
  74. // stale/imprecise but still valid. Currently this invalidates all function
  75. // analyses after various module->function or cgscc->function adaptors in the
  76. // default pipelines.
  77. bool EagerlyInvalidateAnalyses;
  78. };
  79. /// This class provides access to building LLVM's passes.
  80. ///
  81. /// Its members provide the baseline state available to passes during their
  82. /// construction. The \c PassRegistry.def file specifies how to construct all
  83. /// of the built-in passes, and those may reference these members during
  84. /// construction.
  85. class PassBuilder {
  86. TargetMachine *TM;
  87. PipelineTuningOptions PTO;
  88. std::optional<PGOOptions> PGOOpt;
  89. PassInstrumentationCallbacks *PIC;
  90. public:
  91. /// A struct to capture parsed pass pipeline names.
  92. ///
  93. /// A pipeline is defined as a series of names, each of which may in itself
  94. /// recursively contain a nested pipeline. A name is either the name of a pass
  95. /// (e.g. "instcombine") or the name of a pipeline type (e.g. "cgscc"). If the
  96. /// name is the name of a pass, the InnerPipeline is empty, since passes
  97. /// cannot contain inner pipelines. See parsePassPipeline() for a more
  98. /// detailed description of the textual pipeline format.
  99. struct PipelineElement {
  100. StringRef Name;
  101. std::vector<PipelineElement> InnerPipeline;
  102. };
  103. explicit PassBuilder(TargetMachine *TM = nullptr,
  104. PipelineTuningOptions PTO = PipelineTuningOptions(),
  105. std::optional<PGOOptions> PGOOpt = std::nullopt,
  106. PassInstrumentationCallbacks *PIC = nullptr);
  107. /// Cross register the analysis managers through their proxies.
  108. ///
  109. /// This is an interface that can be used to cross register each
  110. /// AnalysisManager with all the others analysis managers.
  111. void crossRegisterProxies(LoopAnalysisManager &LAM,
  112. FunctionAnalysisManager &FAM,
  113. CGSCCAnalysisManager &CGAM,
  114. ModuleAnalysisManager &MAM);
  115. /// Registers all available module analysis passes.
  116. ///
  117. /// This is an interface that can be used to populate a \c
  118. /// ModuleAnalysisManager with all registered module analyses. Callers can
  119. /// still manually register any additional analyses. Callers can also
  120. /// pre-register analyses and this will not override those.
  121. void registerModuleAnalyses(ModuleAnalysisManager &MAM);
  122. /// Registers all available CGSCC analysis passes.
  123. ///
  124. /// This is an interface that can be used to populate a \c CGSCCAnalysisManager
  125. /// with all registered CGSCC analyses. Callers can still manually register any
  126. /// additional analyses. Callers can also pre-register analyses and this will
  127. /// not override those.
  128. void registerCGSCCAnalyses(CGSCCAnalysisManager &CGAM);
  129. /// Registers all available function analysis passes.
  130. ///
  131. /// This is an interface that can be used to populate a \c
  132. /// FunctionAnalysisManager with all registered function analyses. Callers can
  133. /// still manually register any additional analyses. Callers can also
  134. /// pre-register analyses and this will not override those.
  135. void registerFunctionAnalyses(FunctionAnalysisManager &FAM);
  136. /// Registers all available loop analysis passes.
  137. ///
  138. /// This is an interface that can be used to populate a \c LoopAnalysisManager
  139. /// with all registered loop analyses. Callers can still manually register any
  140. /// additional analyses.
  141. void registerLoopAnalyses(LoopAnalysisManager &LAM);
  142. /// Construct the core LLVM function canonicalization and simplification
  143. /// pipeline.
  144. ///
  145. /// This is a long pipeline and uses most of the per-function optimization
  146. /// passes in LLVM to canonicalize and simplify the IR. It is suitable to run
  147. /// repeatedly over the IR and is not expected to destroy important
  148. /// information about the semantics of the IR.
  149. ///
  150. /// Note that \p Level cannot be `O0` here. The pipelines produced are
  151. /// only intended for use when attempting to optimize code. If frontends
  152. /// require some transformations for semantic reasons, they should explicitly
  153. /// build them.
  154. ///
  155. /// \p Phase indicates the current ThinLTO phase.
  156. FunctionPassManager
  157. buildFunctionSimplificationPipeline(OptimizationLevel Level,
  158. ThinOrFullLTOPhase Phase);
  159. /// Construct the core LLVM module canonicalization and simplification
  160. /// pipeline.
  161. ///
  162. /// This pipeline focuses on canonicalizing and simplifying the entire module
  163. /// of IR. Much like the function simplification pipeline above, it is
  164. /// suitable to run repeatedly over the IR and is not expected to destroy
  165. /// important information. It does, however, perform inlining and other
  166. /// heuristic based simplifications that are not strictly reversible.
  167. ///
  168. /// Note that \p Level cannot be `O0` here. The pipelines produced are
  169. /// only intended for use when attempting to optimize code. If frontends
  170. /// require some transformations for semantic reasons, they should explicitly
  171. /// build them.
  172. ///
  173. /// \p Phase indicates the current ThinLTO phase.
  174. ModulePassManager buildModuleSimplificationPipeline(OptimizationLevel Level,
  175. ThinOrFullLTOPhase Phase);
  176. /// Construct the module pipeline that performs inlining as well as
  177. /// the inlining-driven cleanups.
  178. ModuleInlinerWrapperPass buildInlinerPipeline(OptimizationLevel Level,
  179. ThinOrFullLTOPhase Phase);
  180. /// Construct the module pipeline that performs inlining with
  181. /// module inliner pass.
  182. ModulePassManager buildModuleInlinerPipeline(OptimizationLevel Level,
  183. ThinOrFullLTOPhase Phase);
  184. /// Construct the core LLVM module optimization pipeline.
  185. ///
  186. /// This pipeline focuses on optimizing the execution speed of the IR. It
  187. /// uses cost modeling and thresholds to balance code growth against runtime
  188. /// improvements. It includes vectorization and other information destroying
  189. /// transformations. It also cannot generally be run repeatedly on a module
  190. /// without potentially seriously regressing either runtime performance of
  191. /// the code or serious code size growth.
  192. ///
  193. /// Note that \p Level cannot be `O0` here. The pipelines produced are
  194. /// only intended for use when attempting to optimize code. If frontends
  195. /// require some transformations for semantic reasons, they should explicitly
  196. /// build them.
  197. ModulePassManager
  198. buildModuleOptimizationPipeline(OptimizationLevel Level,
  199. ThinOrFullLTOPhase LTOPhase);
  200. /// Build a per-module default optimization pipeline.
  201. ///
  202. /// This provides a good default optimization pipeline for per-module
  203. /// optimization and code generation without any link-time optimization. It
  204. /// typically correspond to frontend "-O[123]" options for optimization
  205. /// levels \c O1, \c O2 and \c O3 resp.
  206. ///
  207. /// Note that \p Level cannot be `O0` here. The pipelines produced are
  208. /// only intended for use when attempting to optimize code. If frontends
  209. /// require some transformations for semantic reasons, they should explicitly
  210. /// build them.
  211. ModulePassManager buildPerModuleDefaultPipeline(OptimizationLevel Level,
  212. bool LTOPreLink = false);
  213. /// Build a pre-link, ThinLTO-targeting default optimization pipeline to
  214. /// a pass manager.
  215. ///
  216. /// This adds the pre-link optimizations tuned to prepare a module for
  217. /// a ThinLTO run. It works to minimize the IR which needs to be analyzed
  218. /// without making irreversible decisions which could be made better during
  219. /// the LTO run.
  220. ///
  221. /// Note that \p Level cannot be `O0` here. The pipelines produced are
  222. /// only intended for use when attempting to optimize code. If frontends
  223. /// require some transformations for semantic reasons, they should explicitly
  224. /// build them.
  225. ModulePassManager buildThinLTOPreLinkDefaultPipeline(OptimizationLevel Level);
  226. /// Build an ThinLTO default optimization pipeline to a pass manager.
  227. ///
  228. /// This provides a good default optimization pipeline for link-time
  229. /// optimization and code generation. It is particularly tuned to fit well
  230. /// when IR coming into the LTO phase was first run through \c
  231. /// addPreLinkLTODefaultPipeline, and the two coordinate closely.
  232. ///
  233. /// Note that \p Level cannot be `O0` here. The pipelines produced are
  234. /// only intended for use when attempting to optimize code. If frontends
  235. /// require some transformations for semantic reasons, they should explicitly
  236. /// build them.
  237. ModulePassManager
  238. buildThinLTODefaultPipeline(OptimizationLevel Level,
  239. const ModuleSummaryIndex *ImportSummary);
  240. /// Build a pre-link, LTO-targeting default optimization pipeline to a pass
  241. /// manager.
  242. ///
  243. /// This adds the pre-link optimizations tuned to work well with a later LTO
  244. /// run. It works to minimize the IR which needs to be analyzed without
  245. /// making irreversible decisions which could be made better during the LTO
  246. /// run.
  247. ///
  248. /// Note that \p Level cannot be `O0` here. The pipelines produced are
  249. /// only intended for use when attempting to optimize code. If frontends
  250. /// require some transformations for semantic reasons, they should explicitly
  251. /// build them.
  252. ModulePassManager buildLTOPreLinkDefaultPipeline(OptimizationLevel Level);
  253. /// Build an LTO default optimization pipeline to a pass manager.
  254. ///
  255. /// This provides a good default optimization pipeline for link-time
  256. /// optimization and code generation. It is particularly tuned to fit well
  257. /// when IR coming into the LTO phase was first run through \c
  258. /// addPreLinkLTODefaultPipeline, and the two coordinate closely.
  259. ///
  260. /// Note that \p Level cannot be `O0` here. The pipelines produced are
  261. /// only intended for use when attempting to optimize code. If frontends
  262. /// require some transformations for semantic reasons, they should explicitly
  263. /// build them.
  264. ModulePassManager buildLTODefaultPipeline(OptimizationLevel Level,
  265. ModuleSummaryIndex *ExportSummary);
  266. /// Build an O0 pipeline with the minimal semantically required passes.
  267. ///
  268. /// This should only be used for non-LTO and LTO pre-link pipelines.
  269. ModulePassManager buildO0DefaultPipeline(OptimizationLevel Level,
  270. bool LTOPreLink = false);
  271. /// Build the default `AAManager` with the default alias analysis pipeline
  272. /// registered.
  273. ///
  274. /// This also adds target-specific alias analyses registered via
  275. /// TargetMachine::registerDefaultAliasAnalyses().
  276. AAManager buildDefaultAAPipeline();
  277. /// Parse a textual pass pipeline description into a \c
  278. /// ModulePassManager.
  279. ///
  280. /// The format of the textual pass pipeline description looks something like:
  281. ///
  282. /// module(function(instcombine,sroa),dce,cgscc(inliner,function(...)),...)
  283. ///
  284. /// Pass managers have ()s describing the nest structure of passes. All passes
  285. /// are comma separated. As a special shortcut, if the very first pass is not
  286. /// a module pass (as a module pass manager is), this will automatically form
  287. /// the shortest stack of pass managers that allow inserting that first pass.
  288. /// So, assuming function passes 'fpassN', CGSCC passes 'cgpassN', and loop
  289. /// passes 'lpassN', all of these are valid:
  290. ///
  291. /// fpass1,fpass2,fpass3
  292. /// cgpass1,cgpass2,cgpass3
  293. /// lpass1,lpass2,lpass3
  294. ///
  295. /// And they are equivalent to the following (resp.):
  296. ///
  297. /// module(function(fpass1,fpass2,fpass3))
  298. /// module(cgscc(cgpass1,cgpass2,cgpass3))
  299. /// module(function(loop(lpass1,lpass2,lpass3)))
  300. ///
  301. /// This shortcut is especially useful for debugging and testing small pass
  302. /// combinations.
  303. ///
  304. /// The sequence of passes aren't necessarily the exact same kind of pass.
  305. /// You can mix different levels implicitly if adaptor passes are defined to
  306. /// make them work. For example,
  307. ///
  308. /// mpass1,fpass1,fpass2,mpass2,lpass1
  309. ///
  310. /// This pipeline uses only one pass manager: the top-level module manager.
  311. /// fpass1,fpass2 and lpass1 are added into the the top-level module manager
  312. /// using only adaptor passes. No nested function/loop pass managers are
  313. /// added. The purpose is to allow easy pass testing when the user
  314. /// specifically want the pass to run under a adaptor directly. This is
  315. /// preferred when a pipeline is largely of one type, but one or just a few
  316. /// passes are of different types(See PassBuilder.cpp for examples).
  317. Error parsePassPipeline(ModulePassManager &MPM, StringRef PipelineText);
  318. /// {{@ Parse a textual pass pipeline description into a specific PassManager
  319. ///
  320. /// Automatic deduction of an appropriate pass manager stack is not supported.
  321. /// For example, to insert a loop pass 'lpass' into a FunctionPassManager,
  322. /// this is the valid pipeline text:
  323. ///
  324. /// function(lpass)
  325. Error parsePassPipeline(CGSCCPassManager &CGPM, StringRef PipelineText);
  326. Error parsePassPipeline(FunctionPassManager &FPM, StringRef PipelineText);
  327. Error parsePassPipeline(LoopPassManager &LPM, StringRef PipelineText);
  328. /// @}}
  329. /// Parse a textual alias analysis pipeline into the provided AA manager.
  330. ///
  331. /// The format of the textual AA pipeline is a comma separated list of AA
  332. /// pass names:
  333. ///
  334. /// basic-aa,globals-aa,...
  335. ///
  336. /// The AA manager is set up such that the provided alias analyses are tried
  337. /// in the order specified. See the \c AAManaager documentation for details
  338. /// about the logic used. This routine just provides the textual mapping
  339. /// between AA names and the analyses to register with the manager.
  340. ///
  341. /// Returns false if the text cannot be parsed cleanly. The specific state of
  342. /// the \p AA manager is unspecified if such an error is encountered and this
  343. /// returns false.
  344. Error parseAAPipeline(AAManager &AA, StringRef PipelineText);
  345. /// Print pass names.
  346. void printPassNames(raw_ostream &OS);
  347. /// Register a callback for a default optimizer pipeline extension
  348. /// point
  349. ///
  350. /// This extension point allows adding passes that perform peephole
  351. /// optimizations similar to the instruction combiner. These passes will be
  352. /// inserted after each instance of the instruction combiner pass.
  353. void registerPeepholeEPCallback(
  354. const std::function<void(FunctionPassManager &, OptimizationLevel)> &C) {
  355. PeepholeEPCallbacks.push_back(C);
  356. }
  357. /// Register a callback for a default optimizer pipeline extension
  358. /// point
  359. ///
  360. /// This extension point allows adding late loop canonicalization and
  361. /// simplification passes. This is the last point in the loop optimization
  362. /// pipeline before loop deletion. Each pass added
  363. /// here must be an instance of LoopPass.
  364. /// This is the place to add passes that can remove loops, such as target-
  365. /// specific loop idiom recognition.
  366. void registerLateLoopOptimizationsEPCallback(
  367. const std::function<void(LoopPassManager &, OptimizationLevel)> &C) {
  368. LateLoopOptimizationsEPCallbacks.push_back(C);
  369. }
  370. /// Register a callback for a default optimizer pipeline extension
  371. /// point
  372. ///
  373. /// This extension point allows adding loop passes to the end of the loop
  374. /// optimizer.
  375. void registerLoopOptimizerEndEPCallback(
  376. const std::function<void(LoopPassManager &, OptimizationLevel)> &C) {
  377. LoopOptimizerEndEPCallbacks.push_back(C);
  378. }
  379. /// Register a callback for a default optimizer pipeline extension
  380. /// point
  381. ///
  382. /// This extension point allows adding optimization passes after most of the
  383. /// main optimizations, but before the last cleanup-ish optimizations.
  384. void registerScalarOptimizerLateEPCallback(
  385. const std::function<void(FunctionPassManager &, OptimizationLevel)> &C) {
  386. ScalarOptimizerLateEPCallbacks.push_back(C);
  387. }
  388. /// Register a callback for a default optimizer pipeline extension
  389. /// point
  390. ///
  391. /// This extension point allows adding CallGraphSCC passes at the end of the
  392. /// main CallGraphSCC passes and before any function simplification passes run
  393. /// by CGPassManager.
  394. void registerCGSCCOptimizerLateEPCallback(
  395. const std::function<void(CGSCCPassManager &, OptimizationLevel)> &C) {
  396. CGSCCOptimizerLateEPCallbacks.push_back(C);
  397. }
  398. /// Register a callback for a default optimizer pipeline extension
  399. /// point
  400. ///
  401. /// This extension point allows adding optimization passes before the
  402. /// vectorizer and other highly target specific optimization passes are
  403. /// executed.
  404. void registerVectorizerStartEPCallback(
  405. const std::function<void(FunctionPassManager &, OptimizationLevel)> &C) {
  406. VectorizerStartEPCallbacks.push_back(C);
  407. }
  408. /// Register a callback for a default optimizer pipeline extension point.
  409. ///
  410. /// This extension point allows adding optimization once at the start of the
  411. /// pipeline. This does not apply to 'backend' compiles (LTO and ThinLTO
  412. /// link-time pipelines).
  413. void registerPipelineStartEPCallback(
  414. const std::function<void(ModulePassManager &, OptimizationLevel)> &C) {
  415. PipelineStartEPCallbacks.push_back(C);
  416. }
  417. /// Register a callback for a default optimizer pipeline extension point.
  418. ///
  419. /// This extension point allows adding optimization right after passes that do
  420. /// basic simplification of the input IR.
  421. void registerPipelineEarlySimplificationEPCallback(
  422. const std::function<void(ModulePassManager &, OptimizationLevel)> &C) {
  423. PipelineEarlySimplificationEPCallbacks.push_back(C);
  424. }
  425. /// Register a callback for a default optimizer pipeline extension point
  426. ///
  427. /// This extension point allows adding optimizations before the function
  428. /// optimization pipeline.
  429. void registerOptimizerEarlyEPCallback(
  430. const std::function<void(ModulePassManager &, OptimizationLevel)> &C) {
  431. OptimizerEarlyEPCallbacks.push_back(C);
  432. }
  433. /// Register a callback for a default optimizer pipeline extension point
  434. ///
  435. /// This extension point allows adding optimizations at the very end of the
  436. /// function optimization pipeline.
  437. void registerOptimizerLastEPCallback(
  438. const std::function<void(ModulePassManager &, OptimizationLevel)> &C) {
  439. OptimizerLastEPCallbacks.push_back(C);
  440. }
  441. /// Register a callback for a default optimizer pipeline extension point
  442. ///
  443. /// This extension point allows adding optimizations at the start of the full
  444. /// LTO pipeline.
  445. void registerFullLinkTimeOptimizationEarlyEPCallback(
  446. const std::function<void(ModulePassManager &, OptimizationLevel)> &C) {
  447. FullLinkTimeOptimizationEarlyEPCallbacks.push_back(C);
  448. }
  449. /// Register a callback for a default optimizer pipeline extension point
  450. ///
  451. /// This extension point allows adding optimizations at the end of the full
  452. /// LTO pipeline.
  453. void registerFullLinkTimeOptimizationLastEPCallback(
  454. const std::function<void(ModulePassManager &, OptimizationLevel)> &C) {
  455. FullLinkTimeOptimizationLastEPCallbacks.push_back(C);
  456. }
  457. /// Register a callback for parsing an AliasAnalysis Name to populate
  458. /// the given AAManager \p AA
  459. void registerParseAACallback(
  460. const std::function<bool(StringRef Name, AAManager &AA)> &C) {
  461. AAParsingCallbacks.push_back(C);
  462. }
  463. /// {{@ Register callbacks for analysis registration with this PassBuilder
  464. /// instance.
  465. /// Callees register their analyses with the given AnalysisManager objects.
  466. void registerAnalysisRegistrationCallback(
  467. const std::function<void(CGSCCAnalysisManager &)> &C) {
  468. CGSCCAnalysisRegistrationCallbacks.push_back(C);
  469. }
  470. void registerAnalysisRegistrationCallback(
  471. const std::function<void(FunctionAnalysisManager &)> &C) {
  472. FunctionAnalysisRegistrationCallbacks.push_back(C);
  473. }
  474. void registerAnalysisRegistrationCallback(
  475. const std::function<void(LoopAnalysisManager &)> &C) {
  476. LoopAnalysisRegistrationCallbacks.push_back(C);
  477. }
  478. void registerAnalysisRegistrationCallback(
  479. const std::function<void(ModuleAnalysisManager &)> &C) {
  480. ModuleAnalysisRegistrationCallbacks.push_back(C);
  481. }
  482. /// @}}
  483. /// {{@ Register pipeline parsing callbacks with this pass builder instance.
  484. /// Using these callbacks, callers can parse both a single pass name, as well
  485. /// as entire sub-pipelines, and populate the PassManager instance
  486. /// accordingly.
  487. void registerPipelineParsingCallback(
  488. const std::function<bool(StringRef Name, CGSCCPassManager &,
  489. ArrayRef<PipelineElement>)> &C) {
  490. CGSCCPipelineParsingCallbacks.push_back(C);
  491. }
  492. void registerPipelineParsingCallback(
  493. const std::function<bool(StringRef Name, FunctionPassManager &,
  494. ArrayRef<PipelineElement>)> &C) {
  495. FunctionPipelineParsingCallbacks.push_back(C);
  496. }
  497. void registerPipelineParsingCallback(
  498. const std::function<bool(StringRef Name, LoopPassManager &,
  499. ArrayRef<PipelineElement>)> &C) {
  500. LoopPipelineParsingCallbacks.push_back(C);
  501. }
  502. void registerPipelineParsingCallback(
  503. const std::function<bool(StringRef Name, ModulePassManager &,
  504. ArrayRef<PipelineElement>)> &C) {
  505. ModulePipelineParsingCallbacks.push_back(C);
  506. }
  507. /// @}}
  508. /// Register a callback for a top-level pipeline entry.
  509. ///
  510. /// If the PassManager type is not given at the top level of the pipeline
  511. /// text, this Callback should be used to determine the appropriate stack of
  512. /// PassManagers and populate the passed ModulePassManager.
  513. void registerParseTopLevelPipelineCallback(
  514. const std::function<bool(ModulePassManager &, ArrayRef<PipelineElement>)>
  515. &C);
  516. /// Add PGOInstrumenation passes for O0 only.
  517. void addPGOInstrPassesForO0(ModulePassManager &MPM, bool RunProfileGen,
  518. bool IsCS, std::string ProfileFile,
  519. std::string ProfileRemappingFile);
  520. /// Returns PIC. External libraries can use this to register pass
  521. /// instrumentation callbacks.
  522. PassInstrumentationCallbacks *getPassInstrumentationCallbacks() const {
  523. return PIC;
  524. }
  525. private:
  526. // O1 pass pipeline
  527. FunctionPassManager
  528. buildO1FunctionSimplificationPipeline(OptimizationLevel Level,
  529. ThinOrFullLTOPhase Phase);
  530. void addRequiredLTOPreLinkPasses(ModulePassManager &MPM);
  531. void addVectorPasses(OptimizationLevel Level, FunctionPassManager &FPM,
  532. bool IsFullLTO);
  533. static std::optional<std::vector<PipelineElement>>
  534. parsePipelineText(StringRef Text);
  535. Error parseModulePass(ModulePassManager &MPM, const PipelineElement &E);
  536. Error parseCGSCCPass(CGSCCPassManager &CGPM, const PipelineElement &E);
  537. Error parseFunctionPass(FunctionPassManager &FPM, const PipelineElement &E);
  538. Error parseLoopPass(LoopPassManager &LPM, const PipelineElement &E);
  539. bool parseAAPassName(AAManager &AA, StringRef Name);
  540. Error parseLoopPassPipeline(LoopPassManager &LPM,
  541. ArrayRef<PipelineElement> Pipeline);
  542. Error parseFunctionPassPipeline(FunctionPassManager &FPM,
  543. ArrayRef<PipelineElement> Pipeline);
  544. Error parseCGSCCPassPipeline(CGSCCPassManager &CGPM,
  545. ArrayRef<PipelineElement> Pipeline);
  546. Error parseModulePassPipeline(ModulePassManager &MPM,
  547. ArrayRef<PipelineElement> Pipeline);
  548. void addPGOInstrPasses(ModulePassManager &MPM, OptimizationLevel Level,
  549. bool RunProfileGen, bool IsCS, std::string ProfileFile,
  550. std::string ProfileRemappingFile,
  551. ThinOrFullLTOPhase LTOPhase);
  552. void invokePeepholeEPCallbacks(FunctionPassManager &, OptimizationLevel);
  553. // Extension Point callbacks
  554. SmallVector<std::function<void(FunctionPassManager &, OptimizationLevel)>, 2>
  555. PeepholeEPCallbacks;
  556. SmallVector<std::function<void(LoopPassManager &, OptimizationLevel)>, 2>
  557. LateLoopOptimizationsEPCallbacks;
  558. SmallVector<std::function<void(LoopPassManager &, OptimizationLevel)>, 2>
  559. LoopOptimizerEndEPCallbacks;
  560. SmallVector<std::function<void(FunctionPassManager &, OptimizationLevel)>, 2>
  561. ScalarOptimizerLateEPCallbacks;
  562. SmallVector<std::function<void(CGSCCPassManager &, OptimizationLevel)>, 2>
  563. CGSCCOptimizerLateEPCallbacks;
  564. SmallVector<std::function<void(FunctionPassManager &, OptimizationLevel)>, 2>
  565. VectorizerStartEPCallbacks;
  566. // Module callbacks
  567. SmallVector<std::function<void(ModulePassManager &, OptimizationLevel)>, 2>
  568. OptimizerEarlyEPCallbacks;
  569. SmallVector<std::function<void(ModulePassManager &, OptimizationLevel)>, 2>
  570. OptimizerLastEPCallbacks;
  571. SmallVector<std::function<void(ModulePassManager &, OptimizationLevel)>, 2>
  572. FullLinkTimeOptimizationEarlyEPCallbacks;
  573. SmallVector<std::function<void(ModulePassManager &, OptimizationLevel)>, 2>
  574. FullLinkTimeOptimizationLastEPCallbacks;
  575. SmallVector<std::function<void(ModulePassManager &, OptimizationLevel)>, 2>
  576. PipelineStartEPCallbacks;
  577. SmallVector<std::function<void(ModulePassManager &, OptimizationLevel)>, 2>
  578. PipelineEarlySimplificationEPCallbacks;
  579. SmallVector<std::function<void(ModuleAnalysisManager &)>, 2>
  580. ModuleAnalysisRegistrationCallbacks;
  581. SmallVector<std::function<bool(StringRef, ModulePassManager &,
  582. ArrayRef<PipelineElement>)>,
  583. 2>
  584. ModulePipelineParsingCallbacks;
  585. SmallVector<
  586. std::function<bool(ModulePassManager &, ArrayRef<PipelineElement>)>, 2>
  587. TopLevelPipelineParsingCallbacks;
  588. // CGSCC callbacks
  589. SmallVector<std::function<void(CGSCCAnalysisManager &)>, 2>
  590. CGSCCAnalysisRegistrationCallbacks;
  591. SmallVector<std::function<bool(StringRef, CGSCCPassManager &,
  592. ArrayRef<PipelineElement>)>,
  593. 2>
  594. CGSCCPipelineParsingCallbacks;
  595. // Function callbacks
  596. SmallVector<std::function<void(FunctionAnalysisManager &)>, 2>
  597. FunctionAnalysisRegistrationCallbacks;
  598. SmallVector<std::function<bool(StringRef, FunctionPassManager &,
  599. ArrayRef<PipelineElement>)>,
  600. 2>
  601. FunctionPipelineParsingCallbacks;
  602. // Loop callbacks
  603. SmallVector<std::function<void(LoopAnalysisManager &)>, 2>
  604. LoopAnalysisRegistrationCallbacks;
  605. SmallVector<std::function<bool(StringRef, LoopPassManager &,
  606. ArrayRef<PipelineElement>)>,
  607. 2>
  608. LoopPipelineParsingCallbacks;
  609. // AA callbacks
  610. SmallVector<std::function<bool(StringRef Name, AAManager &AA)>, 2>
  611. AAParsingCallbacks;
  612. };
  613. /// This utility template takes care of adding require<> and invalidate<>
  614. /// passes for an analysis to a given \c PassManager. It is intended to be used
  615. /// during parsing of a pass pipeline when parsing a single PipelineName.
  616. /// When registering a new function analysis FancyAnalysis with the pass
  617. /// pipeline name "fancy-analysis", a matching ParsePipelineCallback could look
  618. /// like this:
  619. ///
  620. /// static bool parseFunctionPipeline(StringRef Name, FunctionPassManager &FPM,
  621. /// ArrayRef<PipelineElement> P) {
  622. /// if (parseAnalysisUtilityPasses<FancyAnalysis>("fancy-analysis", Name,
  623. /// FPM))
  624. /// return true;
  625. /// return false;
  626. /// }
  627. template <typename AnalysisT, typename IRUnitT, typename AnalysisManagerT,
  628. typename... ExtraArgTs>
  629. bool parseAnalysisUtilityPasses(
  630. StringRef AnalysisName, StringRef PipelineName,
  631. PassManager<IRUnitT, AnalysisManagerT, ExtraArgTs...> &PM) {
  632. if (!PipelineName.endswith(">"))
  633. return false;
  634. // See if this is an invalidate<> pass name
  635. if (PipelineName.startswith("invalidate<")) {
  636. PipelineName = PipelineName.substr(11, PipelineName.size() - 12);
  637. if (PipelineName != AnalysisName)
  638. return false;
  639. PM.addPass(InvalidateAnalysisPass<AnalysisT>());
  640. return true;
  641. }
  642. // See if this is a require<> pass name
  643. if (PipelineName.startswith("require<")) {
  644. PipelineName = PipelineName.substr(8, PipelineName.size() - 9);
  645. if (PipelineName != AnalysisName)
  646. return false;
  647. PM.addPass(RequireAnalysisPass<AnalysisT, IRUnitT, AnalysisManagerT,
  648. ExtraArgTs...>());
  649. return true;
  650. }
  651. return false;
  652. }
  653. }
  654. #endif
  655. #ifdef __GNUC__
  656. #pragma GCC diagnostic pop
  657. #endif