PassBuilder.h 30 KB

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