PassBuilder.h 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  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/Support/Error.h"
  25. #include "llvm/Transforms/IPO/Inliner.h"
  26. #include "llvm/Transforms/Instrumentation.h"
  27. #include "llvm/Transforms/Scalar/LoopPassManager.h"
  28. #include <vector>
  29. namespace llvm {
  30. class StringRef;
  31. class AAManager;
  32. class TargetMachine;
  33. class ModuleSummaryIndex;
  34. /// A struct capturing PGO tunables.
  35. struct PGOOptions {
  36. enum PGOAction { NoAction, IRInstr, IRUse, SampleUse };
  37. enum CSPGOAction { NoCSAction, CSIRInstr, CSIRUse };
  38. PGOOptions(std::string ProfileFile = "", std::string CSProfileGenFile = "",
  39. std::string ProfileRemappingFile = "", PGOAction Action = NoAction,
  40. CSPGOAction CSAction = NoCSAction,
  41. bool DebugInfoForProfiling = false,
  42. bool PseudoProbeForProfiling = false)
  43. : ProfileFile(ProfileFile), CSProfileGenFile(CSProfileGenFile),
  44. ProfileRemappingFile(ProfileRemappingFile), Action(Action),
  45. CSAction(CSAction), DebugInfoForProfiling(DebugInfoForProfiling ||
  46. (Action == SampleUse &&
  47. !PseudoProbeForProfiling)),
  48. PseudoProbeForProfiling(PseudoProbeForProfiling) {
  49. // Note, we do allow ProfileFile.empty() for Action=IRUse LTO can
  50. // callback with IRUse action without ProfileFile.
  51. // If there is a CSAction, PGOAction cannot be IRInstr or SampleUse.
  52. assert(this->CSAction == NoCSAction ||
  53. (this->Action != IRInstr && this->Action != SampleUse));
  54. // For CSIRInstr, CSProfileGenFile also needs to be nonempty.
  55. assert(this->CSAction != CSIRInstr || !this->CSProfileGenFile.empty());
  56. // If CSAction is CSIRUse, PGOAction needs to be IRUse as they share
  57. // a profile.
  58. assert(this->CSAction != CSIRUse || this->Action == IRUse);
  59. // If neither Action nor CSAction, DebugInfoForProfiling or
  60. // PseudoProbeForProfiling needs to be true.
  61. assert(this->Action != NoAction || this->CSAction != NoCSAction ||
  62. this->DebugInfoForProfiling || this->PseudoProbeForProfiling);
  63. // Pseudo probe emission does not work with -fdebug-info-for-profiling since
  64. // they both use the discriminator field of debug lines but for different
  65. // purposes.
  66. if (this->DebugInfoForProfiling && this->PseudoProbeForProfiling) {
  67. report_fatal_error(
  68. "Pseudo probes cannot be used with -debug-info-for-profiling", false);
  69. }
  70. }
  71. std::string ProfileFile;
  72. std::string CSProfileGenFile;
  73. std::string ProfileRemappingFile;
  74. PGOAction Action;
  75. CSPGOAction CSAction;
  76. bool DebugInfoForProfiling;
  77. bool PseudoProbeForProfiling;
  78. };
  79. /// Tunable parameters for passes in the default pipelines.
  80. class PipelineTuningOptions {
  81. public:
  82. /// Constructor sets pipeline tuning defaults based on cl::opts. Each option
  83. /// can be set in the PassBuilder when using a LLVM as a library.
  84. PipelineTuningOptions();
  85. /// Tuning option to set loop interleaving on/off, set based on opt level.
  86. bool LoopInterleaving;
  87. /// Tuning option to enable/disable loop vectorization, set based on opt
  88. /// level.
  89. bool LoopVectorization;
  90. /// Tuning option to enable/disable slp loop vectorization, set based on opt
  91. /// level.
  92. bool SLPVectorization;
  93. /// Tuning option to enable/disable loop unrolling. Its default value is true.
  94. bool LoopUnrolling;
  95. /// Tuning option to forget all SCEV loops in LoopUnroll. Its default value
  96. /// is that of the flag: `-forget-scev-loop-unroll`.
  97. bool ForgetAllSCEVInLoopUnroll;
  98. /// Tuning option to enable/disable coroutine intrinsic lowering. Its default
  99. /// value is false. Frontends such as Clang may enable this conditionally. For
  100. /// example, Clang enables this option if the flags `-std=c++2a` or above, or
  101. /// `-fcoroutines-ts`, have been specified.
  102. bool Coroutines;
  103. /// Tuning option to cap the number of calls to retrive clobbering accesses in
  104. /// MemorySSA, in LICM.
  105. unsigned LicmMssaOptCap;
  106. /// Tuning option to disable promotion to scalars in LICM with MemorySSA, if
  107. /// the number of access is too large.
  108. unsigned LicmMssaNoAccForPromotionCap;
  109. /// Tuning option to enable/disable call graph profile. Its default value is
  110. /// that of the flag: `-enable-npm-call-graph-profile`.
  111. bool CallGraphProfile;
  112. /// Tuning option to enable/disable function merging. Its default value is
  113. /// false.
  114. bool MergeFunctions;
  115. /// Uniquefy function linkage name. Its default value is false.
  116. bool UniqueLinkageNames;
  117. };
  118. /// This class provides access to building LLVM's passes.
  119. ///
  120. /// Its members provide the baseline state available to passes during their
  121. /// construction. The \c PassRegistry.def file specifies how to construct all
  122. /// of the built-in passes, and those may reference these members during
  123. /// construction.
  124. class PassBuilder {
  125. bool DebugLogging;
  126. TargetMachine *TM;
  127. PipelineTuningOptions PTO;
  128. Optional<PGOOptions> PGOOpt;
  129. PassInstrumentationCallbacks *PIC;
  130. public:
  131. /// A struct to capture parsed pass pipeline names.
  132. ///
  133. /// A pipeline is defined as a series of names, each of which may in itself
  134. /// recursively contain a nested pipeline. A name is either the name of a pass
  135. /// (e.g. "instcombine") or the name of a pipeline type (e.g. "cgscc"). If the
  136. /// name is the name of a pass, the InnerPipeline is empty, since passes
  137. /// cannot contain inner pipelines. See parsePassPipeline() for a more
  138. /// detailed description of the textual pipeline format.
  139. struct PipelineElement {
  140. StringRef Name;
  141. std::vector<PipelineElement> InnerPipeline;
  142. };
  143. /// LLVM-provided high-level optimization levels.
  144. ///
  145. /// This enumerates the LLVM-provided high-level optimization levels. Each
  146. /// level has a specific goal and rationale.
  147. class OptimizationLevel final {
  148. unsigned SpeedLevel = 2;
  149. unsigned SizeLevel = 0;
  150. OptimizationLevel(unsigned SpeedLevel, unsigned SizeLevel)
  151. : SpeedLevel(SpeedLevel), SizeLevel(SizeLevel) {
  152. // Check that only valid combinations are passed.
  153. assert(SpeedLevel <= 3 &&
  154. "Optimization level for speed should be 0, 1, 2, or 3");
  155. assert(SizeLevel <= 2 &&
  156. "Optimization level for size should be 0, 1, or 2");
  157. assert((SizeLevel == 0 || SpeedLevel == 2) &&
  158. "Optimize for size should be encoded with speedup level == 2");
  159. }
  160. public:
  161. OptimizationLevel() = default;
  162. /// Disable as many optimizations as possible. This doesn't completely
  163. /// disable the optimizer in all cases, for example always_inline functions
  164. /// can be required to be inlined for correctness.
  165. static const OptimizationLevel O0;
  166. /// Optimize quickly without destroying debuggability.
  167. ///
  168. /// This level is tuned to produce a result from the optimizer as quickly
  169. /// as possible and to avoid destroying debuggability. This tends to result
  170. /// in a very good development mode where the compiled code will be
  171. /// immediately executed as part of testing. As a consequence, where
  172. /// possible, we would like to produce efficient-to-execute code, but not
  173. /// if it significantly slows down compilation or would prevent even basic
  174. /// debugging of the resulting binary.
  175. ///
  176. /// As an example, complex loop transformations such as versioning,
  177. /// vectorization, or fusion don't make sense here due to the degree to
  178. /// which the executed code differs from the source code, and the compile
  179. /// time cost.
  180. static const OptimizationLevel O1;
  181. /// Optimize for fast execution as much as possible without triggering
  182. /// significant incremental compile time or code size growth.
  183. ///
  184. /// The key idea is that optimizations at this level should "pay for
  185. /// themselves". So if an optimization increases compile time by 5% or
  186. /// increases code size by 5% for a particular benchmark, that benchmark
  187. /// should also be one which sees a 5% runtime improvement. If the compile
  188. /// time or code size penalties happen on average across a diverse range of
  189. /// LLVM users' benchmarks, then the improvements should as well.
  190. ///
  191. /// And no matter what, the compile time needs to not grow superlinearly
  192. /// with the size of input to LLVM so that users can control the runtime of
  193. /// the optimizer in this mode.
  194. ///
  195. /// This is expected to be a good default optimization level for the vast
  196. /// majority of users.
  197. static const OptimizationLevel O2;
  198. /// Optimize for fast execution as much as possible.
  199. ///
  200. /// This mode is significantly more aggressive in trading off compile time
  201. /// and code size to get execution time improvements. The core idea is that
  202. /// this mode should include any optimization that helps execution time on
  203. /// balance across a diverse collection of benchmarks, even if it increases
  204. /// code size or compile time for some benchmarks without corresponding
  205. /// improvements to execution time.
  206. ///
  207. /// Despite being willing to trade more compile time off to get improved
  208. /// execution time, this mode still tries to avoid superlinear growth in
  209. /// order to make even significantly slower compile times at least scale
  210. /// reasonably. This does not preclude very substantial constant factor
  211. /// costs though.
  212. static const OptimizationLevel O3;
  213. /// Similar to \c O2 but tries to optimize for small code size instead of
  214. /// fast execution without triggering significant incremental execution
  215. /// time slowdowns.
  216. ///
  217. /// The logic here is exactly the same as \c O2, but with code size and
  218. /// execution time metrics swapped.
  219. ///
  220. /// A consequence of the different core goal is that this should in general
  221. /// produce substantially smaller executables that still run in
  222. /// a reasonable amount of time.
  223. static const OptimizationLevel Os;
  224. /// A very specialized mode that will optimize for code size at any and all
  225. /// costs.
  226. ///
  227. /// This is useful primarily when there are absolute size limitations and
  228. /// any effort taken to reduce the size is worth it regardless of the
  229. /// execution time impact. You should expect this level to produce rather
  230. /// slow, but very small, code.
  231. static const OptimizationLevel Oz;
  232. bool isOptimizingForSpeed() const {
  233. return SizeLevel == 0 && SpeedLevel > 0;
  234. }
  235. bool isOptimizingForSize() const { return SizeLevel > 0; }
  236. bool operator==(const OptimizationLevel &Other) const {
  237. return SizeLevel == Other.SizeLevel && SpeedLevel == Other.SpeedLevel;
  238. }
  239. bool operator!=(const OptimizationLevel &Other) const {
  240. return SizeLevel != Other.SizeLevel || SpeedLevel != Other.SpeedLevel;
  241. }
  242. unsigned getSpeedupLevel() const { return SpeedLevel; }
  243. unsigned getSizeLevel() const { return SizeLevel; }
  244. };
  245. explicit PassBuilder(bool DebugLogging = false, TargetMachine *TM = nullptr,
  246. PipelineTuningOptions PTO = PipelineTuningOptions(),
  247. Optional<PGOOptions> PGOOpt = None,
  248. PassInstrumentationCallbacks *PIC = nullptr);
  249. /// Cross register the analysis managers through their proxies.
  250. ///
  251. /// This is an interface that can be used to cross register each
  252. /// AnalysisManager with all the others analysis managers.
  253. void crossRegisterProxies(LoopAnalysisManager &LAM,
  254. FunctionAnalysisManager &FAM,
  255. CGSCCAnalysisManager &CGAM,
  256. ModuleAnalysisManager &MAM);
  257. /// Registers all available module analysis passes.
  258. ///
  259. /// This is an interface that can be used to populate a \c
  260. /// ModuleAnalysisManager with all registered module analyses. Callers can
  261. /// still manually register any additional analyses. Callers can also
  262. /// pre-register analyses and this will not override those.
  263. void registerModuleAnalyses(ModuleAnalysisManager &MAM);
  264. /// Registers all available CGSCC analysis passes.
  265. ///
  266. /// This is an interface that can be used to populate a \c CGSCCAnalysisManager
  267. /// with all registered CGSCC analyses. Callers can still manually register any
  268. /// additional analyses. Callers can also pre-register analyses and this will
  269. /// not override those.
  270. void registerCGSCCAnalyses(CGSCCAnalysisManager &CGAM);
  271. /// Registers all available function analysis passes.
  272. ///
  273. /// This is an interface that can be used to populate a \c
  274. /// FunctionAnalysisManager with all registered function analyses. Callers can
  275. /// still manually register any additional analyses. Callers can also
  276. /// pre-register analyses and this will not override those.
  277. void registerFunctionAnalyses(FunctionAnalysisManager &FAM);
  278. /// Registers all available loop analysis passes.
  279. ///
  280. /// This is an interface that can be used to populate a \c LoopAnalysisManager
  281. /// with all registered loop analyses. Callers can still manually register any
  282. /// additional analyses.
  283. void registerLoopAnalyses(LoopAnalysisManager &LAM);
  284. /// Construct the core LLVM function canonicalization and simplification
  285. /// pipeline.
  286. ///
  287. /// This is a long pipeline and uses most of the per-function optimization
  288. /// passes in LLVM to canonicalize and simplify the IR. It is suitable to run
  289. /// repeatedly over the IR and is not expected to destroy important
  290. /// information about the semantics of the IR.
  291. ///
  292. /// Note that \p Level cannot be `O0` here. The pipelines produced are
  293. /// only intended for use when attempting to optimize code. If frontends
  294. /// require some transformations for semantic reasons, they should explicitly
  295. /// build them.
  296. ///
  297. /// \p Phase indicates the current ThinLTO phase.
  298. FunctionPassManager
  299. buildFunctionSimplificationPipeline(OptimizationLevel Level,
  300. ThinOrFullLTOPhase Phase);
  301. /// Construct the core LLVM module canonicalization and simplification
  302. /// pipeline.
  303. ///
  304. /// This pipeline focuses on canonicalizing and simplifying the entire module
  305. /// of IR. Much like the function simplification pipeline above, it is
  306. /// suitable to run repeatedly over the IR and is not expected to destroy
  307. /// important information. It does, however, perform inlining and other
  308. /// heuristic based simplifications that are not strictly reversible.
  309. ///
  310. /// Note that \p Level cannot be `O0` here. The pipelines produced are
  311. /// only intended for use when attempting to optimize code. If frontends
  312. /// require some transformations for semantic reasons, they should explicitly
  313. /// build them.
  314. ///
  315. /// \p Phase indicates the current ThinLTO phase.
  316. ModulePassManager buildModuleSimplificationPipeline(OptimizationLevel Level,
  317. ThinOrFullLTOPhase Phase);
  318. /// Construct the module pipeline that performs inlining as well as
  319. /// the inlining-driven cleanups.
  320. ModuleInlinerWrapperPass buildInlinerPipeline(OptimizationLevel Level,
  321. ThinOrFullLTOPhase Phase);
  322. /// Construct the core LLVM module optimization pipeline.
  323. ///
  324. /// This pipeline focuses on optimizing the execution speed of the IR. It
  325. /// uses cost modeling and thresholds to balance code growth against runtime
  326. /// improvements. It includes vectorization and other information destroying
  327. /// transformations. It also cannot generally be run repeatedly on a module
  328. /// without potentially seriously regressing either runtime performance of
  329. /// the code or serious code size growth.
  330. ///
  331. /// Note that \p Level cannot be `O0` here. The pipelines produced are
  332. /// only intended for use when attempting to optimize code. If frontends
  333. /// require some transformations for semantic reasons, they should explicitly
  334. /// build them.
  335. ModulePassManager buildModuleOptimizationPipeline(OptimizationLevel Level,
  336. bool LTOPreLink = false);
  337. /// Build a per-module default optimization pipeline.
  338. ///
  339. /// This provides a good default optimization pipeline for per-module
  340. /// optimization and code generation without any link-time optimization. It
  341. /// typically correspond to frontend "-O[123]" options for optimization
  342. /// levels \c O1, \c O2 and \c O3 resp.
  343. ///
  344. /// Note that \p Level cannot be `O0` here. The pipelines produced are
  345. /// only intended for use when attempting to optimize code. If frontends
  346. /// require some transformations for semantic reasons, they should explicitly
  347. /// build them.
  348. ModulePassManager buildPerModuleDefaultPipeline(OptimizationLevel Level,
  349. bool LTOPreLink = false);
  350. /// Build a pre-link, ThinLTO-targeting default optimization pipeline to
  351. /// a pass manager.
  352. ///
  353. /// This adds the pre-link optimizations tuned to prepare a module for
  354. /// a ThinLTO run. It works to minimize the IR which needs to be analyzed
  355. /// without making irreversible decisions which could be made better during
  356. /// the LTO run.
  357. ///
  358. /// Note that \p Level cannot be `O0` here. The pipelines produced are
  359. /// only intended for use when attempting to optimize code. If frontends
  360. /// require some transformations for semantic reasons, they should explicitly
  361. /// build them.
  362. ModulePassManager buildThinLTOPreLinkDefaultPipeline(OptimizationLevel Level);
  363. /// Build an ThinLTO default optimization pipeline to a pass manager.
  364. ///
  365. /// This provides a good default optimization pipeline for link-time
  366. /// optimization and code generation. It is particularly tuned to fit well
  367. /// when IR coming into the LTO phase was first run through \c
  368. /// addPreLinkLTODefaultPipeline, and the two coordinate closely.
  369. ///
  370. /// Note that \p Level cannot be `O0` here. The pipelines produced are
  371. /// only intended for use when attempting to optimize code. If frontends
  372. /// require some transformations for semantic reasons, they should explicitly
  373. /// build them.
  374. ModulePassManager
  375. buildThinLTODefaultPipeline(OptimizationLevel Level,
  376. const ModuleSummaryIndex *ImportSummary);
  377. /// Build a pre-link, LTO-targeting default optimization pipeline to a pass
  378. /// manager.
  379. ///
  380. /// This adds the pre-link optimizations tuned to work well with a later LTO
  381. /// run. It works to minimize the IR which needs to be analyzed without
  382. /// making irreversible decisions which could be made better during the LTO
  383. /// run.
  384. ///
  385. /// Note that \p Level cannot be `O0` here. The pipelines produced are
  386. /// only intended for use when attempting to optimize code. If frontends
  387. /// require some transformations for semantic reasons, they should explicitly
  388. /// build them.
  389. ModulePassManager buildLTOPreLinkDefaultPipeline(OptimizationLevel Level);
  390. /// Build an LTO default optimization pipeline to a pass manager.
  391. ///
  392. /// This provides a good default optimization pipeline for link-time
  393. /// optimization and code generation. It is particularly tuned to fit well
  394. /// when IR coming into the LTO phase was first run through \c
  395. /// addPreLinkLTODefaultPipeline, and the two coordinate closely.
  396. ///
  397. /// Note that \p Level cannot be `O0` here. The pipelines produced are
  398. /// only intended for use when attempting to optimize code. If frontends
  399. /// require some transformations for semantic reasons, they should explicitly
  400. /// build them.
  401. ModulePassManager buildLTODefaultPipeline(OptimizationLevel Level,
  402. ModuleSummaryIndex *ExportSummary);
  403. /// Build an O0 pipeline with the minimal semantically required passes.
  404. ///
  405. /// This should only be used for non-LTO and LTO pre-link pipelines.
  406. ModulePassManager buildO0DefaultPipeline(OptimizationLevel Level,
  407. bool LTOPreLink = false);
  408. /// Build the default `AAManager` with the default alias analysis pipeline
  409. /// registered.
  410. ///
  411. /// This also adds target-specific alias analyses registered via
  412. /// TargetMachine::registerDefaultAliasAnalyses().
  413. AAManager buildDefaultAAPipeline();
  414. /// Parse a textual pass pipeline description into a \c
  415. /// ModulePassManager.
  416. ///
  417. /// The format of the textual pass pipeline description looks something like:
  418. ///
  419. /// module(function(instcombine,sroa),dce,cgscc(inliner,function(...)),...)
  420. ///
  421. /// Pass managers have ()s describing the nest structure of passes. All passes
  422. /// are comma separated. As a special shortcut, if the very first pass is not
  423. /// a module pass (as a module pass manager is), this will automatically form
  424. /// the shortest stack of pass managers that allow inserting that first pass.
  425. /// So, assuming function passes 'fpassN', CGSCC passes 'cgpassN', and loop
  426. /// passes 'lpassN', all of these are valid:
  427. ///
  428. /// fpass1,fpass2,fpass3
  429. /// cgpass1,cgpass2,cgpass3
  430. /// lpass1,lpass2,lpass3
  431. ///
  432. /// And they are equivalent to the following (resp.):
  433. ///
  434. /// module(function(fpass1,fpass2,fpass3))
  435. /// module(cgscc(cgpass1,cgpass2,cgpass3))
  436. /// module(function(loop(lpass1,lpass2,lpass3)))
  437. ///
  438. /// This shortcut is especially useful for debugging and testing small pass
  439. /// combinations.
  440. ///
  441. /// The sequence of passes aren't necessarily the exact same kind of pass.
  442. /// You can mix different levels implicitly if adaptor passes are defined to
  443. /// make them work. For example,
  444. ///
  445. /// mpass1,fpass1,fpass2,mpass2,lpass1
  446. ///
  447. /// This pipeline uses only one pass manager: the top-level module manager.
  448. /// fpass1,fpass2 and lpass1 are added into the the top-level module manager
  449. /// using only adaptor passes. No nested function/loop pass managers are
  450. /// added. The purpose is to allow easy pass testing when the user
  451. /// specifically want the pass to run under a adaptor directly. This is
  452. /// preferred when a pipeline is largely of one type, but one or just a few
  453. /// passes are of different types(See PassBuilder.cpp for examples).
  454. Error parsePassPipeline(ModulePassManager &MPM, StringRef PipelineText);
  455. /// {{@ Parse a textual pass pipeline description into a specific PassManager
  456. ///
  457. /// Automatic deduction of an appropriate pass manager stack is not supported.
  458. /// For example, to insert a loop pass 'lpass' into a FunctionPassManager,
  459. /// this is the valid pipeline text:
  460. ///
  461. /// function(lpass)
  462. Error parsePassPipeline(CGSCCPassManager &CGPM, StringRef PipelineText);
  463. Error parsePassPipeline(FunctionPassManager &FPM, StringRef PipelineText);
  464. Error parsePassPipeline(LoopPassManager &LPM, StringRef PipelineText);
  465. /// @}}
  466. /// Parse a textual alias analysis pipeline into the provided AA manager.
  467. ///
  468. /// The format of the textual AA pipeline is a comma separated list of AA
  469. /// pass names:
  470. ///
  471. /// basic-aa,globals-aa,...
  472. ///
  473. /// The AA manager is set up such that the provided alias analyses are tried
  474. /// in the order specified. See the \c AAManaager documentation for details
  475. /// about the logic used. This routine just provides the textual mapping
  476. /// between AA names and the analyses to register with the manager.
  477. ///
  478. /// Returns false if the text cannot be parsed cleanly. The specific state of
  479. /// the \p AA manager is unspecified if such an error is encountered and this
  480. /// returns false.
  481. Error parseAAPipeline(AAManager &AA, StringRef PipelineText);
  482. /// Returns true if the pass name is the name of an alias analysis pass.
  483. bool isAAPassName(StringRef PassName);
  484. /// Returns true if the pass name is the name of a (non-alias) analysis pass.
  485. bool isAnalysisPassName(StringRef PassName);
  486. /// Register a callback for a default optimizer pipeline extension
  487. /// point
  488. ///
  489. /// This extension point allows adding passes that perform peephole
  490. /// optimizations similar to the instruction combiner. These passes will be
  491. /// inserted after each instance of the instruction combiner pass.
  492. void registerPeepholeEPCallback(
  493. const std::function<void(FunctionPassManager &, OptimizationLevel)> &C) {
  494. PeepholeEPCallbacks.push_back(C);
  495. }
  496. /// Register a callback for a default optimizer pipeline extension
  497. /// point
  498. ///
  499. /// This extension point allows adding late loop canonicalization and
  500. /// simplification passes. This is the last point in the loop optimization
  501. /// pipeline before loop deletion. Each pass added
  502. /// here must be an instance of LoopPass.
  503. /// This is the place to add passes that can remove loops, such as target-
  504. /// specific loop idiom recognition.
  505. void registerLateLoopOptimizationsEPCallback(
  506. const std::function<void(LoopPassManager &, OptimizationLevel)> &C) {
  507. LateLoopOptimizationsEPCallbacks.push_back(C);
  508. }
  509. /// Register a callback for a default optimizer pipeline extension
  510. /// point
  511. ///
  512. /// This extension point allows adding loop passes to the end of the loop
  513. /// optimizer.
  514. void registerLoopOptimizerEndEPCallback(
  515. const std::function<void(LoopPassManager &, OptimizationLevel)> &C) {
  516. LoopOptimizerEndEPCallbacks.push_back(C);
  517. }
  518. /// Register a callback for a default optimizer pipeline extension
  519. /// point
  520. ///
  521. /// This extension point allows adding optimization passes after most of the
  522. /// main optimizations, but before the last cleanup-ish optimizations.
  523. void registerScalarOptimizerLateEPCallback(
  524. const std::function<void(FunctionPassManager &, OptimizationLevel)> &C) {
  525. ScalarOptimizerLateEPCallbacks.push_back(C);
  526. }
  527. /// Register a callback for a default optimizer pipeline extension
  528. /// point
  529. ///
  530. /// This extension point allows adding CallGraphSCC passes at the end of the
  531. /// main CallGraphSCC passes and before any function simplification passes run
  532. /// by CGPassManager.
  533. void registerCGSCCOptimizerLateEPCallback(
  534. const std::function<void(CGSCCPassManager &, OptimizationLevel)> &C) {
  535. CGSCCOptimizerLateEPCallbacks.push_back(C);
  536. }
  537. /// Register a callback for a default optimizer pipeline extension
  538. /// point
  539. ///
  540. /// This extension point allows adding optimization passes before the
  541. /// vectorizer and other highly target specific optimization passes are
  542. /// executed.
  543. void registerVectorizerStartEPCallback(
  544. const std::function<void(FunctionPassManager &, OptimizationLevel)> &C) {
  545. VectorizerStartEPCallbacks.push_back(C);
  546. }
  547. /// Register a callback for a default optimizer pipeline extension point.
  548. ///
  549. /// This extension point allows adding optimization once at the start of the
  550. /// pipeline. This does not apply to 'backend' compiles (LTO and ThinLTO
  551. /// link-time pipelines).
  552. void registerPipelineStartEPCallback(
  553. const std::function<void(ModulePassManager &, OptimizationLevel)> &C) {
  554. PipelineStartEPCallbacks.push_back(C);
  555. }
  556. /// Register a callback for a default optimizer pipeline extension point.
  557. ///
  558. /// This extension point allows adding optimization right after passes that do
  559. /// basic simplification of the input IR.
  560. void registerPipelineEarlySimplificationEPCallback(
  561. const std::function<void(ModulePassManager &, OptimizationLevel)> &C) {
  562. PipelineEarlySimplificationEPCallbacks.push_back(C);
  563. }
  564. /// Register a callback for a default optimizer pipeline extension point
  565. ///
  566. /// This extension point allows adding optimizations at the very end of the
  567. /// function optimization pipeline.
  568. void registerOptimizerLastEPCallback(
  569. const std::function<void(ModulePassManager &, OptimizationLevel)> &C) {
  570. OptimizerLastEPCallbacks.push_back(C);
  571. }
  572. /// Register a callback for parsing an AliasAnalysis Name to populate
  573. /// the given AAManager \p AA
  574. void registerParseAACallback(
  575. const std::function<bool(StringRef Name, AAManager &AA)> &C) {
  576. AAParsingCallbacks.push_back(C);
  577. }
  578. /// {{@ Register callbacks for analysis registration with this PassBuilder
  579. /// instance.
  580. /// Callees register their analyses with the given AnalysisManager objects.
  581. void registerAnalysisRegistrationCallback(
  582. const std::function<void(CGSCCAnalysisManager &)> &C) {
  583. CGSCCAnalysisRegistrationCallbacks.push_back(C);
  584. }
  585. void registerAnalysisRegistrationCallback(
  586. const std::function<void(FunctionAnalysisManager &)> &C) {
  587. FunctionAnalysisRegistrationCallbacks.push_back(C);
  588. }
  589. void registerAnalysisRegistrationCallback(
  590. const std::function<void(LoopAnalysisManager &)> &C) {
  591. LoopAnalysisRegistrationCallbacks.push_back(C);
  592. }
  593. void registerAnalysisRegistrationCallback(
  594. const std::function<void(ModuleAnalysisManager &)> &C) {
  595. ModuleAnalysisRegistrationCallbacks.push_back(C);
  596. }
  597. /// @}}
  598. /// {{@ Register pipeline parsing callbacks with this pass builder instance.
  599. /// Using these callbacks, callers can parse both a single pass name, as well
  600. /// as entire sub-pipelines, and populate the PassManager instance
  601. /// accordingly.
  602. void registerPipelineParsingCallback(
  603. const std::function<bool(StringRef Name, CGSCCPassManager &,
  604. ArrayRef<PipelineElement>)> &C) {
  605. CGSCCPipelineParsingCallbacks.push_back(C);
  606. }
  607. void registerPipelineParsingCallback(
  608. const std::function<bool(StringRef Name, FunctionPassManager &,
  609. ArrayRef<PipelineElement>)> &C) {
  610. FunctionPipelineParsingCallbacks.push_back(C);
  611. }
  612. void registerPipelineParsingCallback(
  613. const std::function<bool(StringRef Name, LoopPassManager &,
  614. ArrayRef<PipelineElement>)> &C) {
  615. LoopPipelineParsingCallbacks.push_back(C);
  616. }
  617. void registerPipelineParsingCallback(
  618. const std::function<bool(StringRef Name, ModulePassManager &,
  619. ArrayRef<PipelineElement>)> &C) {
  620. ModulePipelineParsingCallbacks.push_back(C);
  621. }
  622. /// @}}
  623. /// Register a callback for a top-level pipeline entry.
  624. ///
  625. /// If the PassManager type is not given at the top level of the pipeline
  626. /// text, this Callback should be used to determine the appropriate stack of
  627. /// PassManagers and populate the passed ModulePassManager.
  628. void registerParseTopLevelPipelineCallback(
  629. const std::function<bool(ModulePassManager &, ArrayRef<PipelineElement>,
  630. bool DebugLogging)> &C);
  631. /// Add PGOInstrumenation passes for O0 only.
  632. void addPGOInstrPassesForO0(ModulePassManager &MPM, bool RunProfileGen,
  633. bool IsCS, std::string ProfileFile,
  634. std::string ProfileRemappingFile);
  635. /// Returns PIC. External libraries can use this to register pass
  636. /// instrumentation callbacks.
  637. PassInstrumentationCallbacks *getPassInstrumentationCallbacks() const {
  638. return PIC;
  639. }
  640. private:
  641. // O1 pass pipeline
  642. FunctionPassManager
  643. buildO1FunctionSimplificationPipeline(OptimizationLevel Level,
  644. ThinOrFullLTOPhase Phase);
  645. void addRequiredLTOPreLinkPasses(ModulePassManager &MPM);
  646. static Optional<std::vector<PipelineElement>>
  647. parsePipelineText(StringRef Text);
  648. Error parseModulePass(ModulePassManager &MPM, const PipelineElement &E);
  649. Error parseCGSCCPass(CGSCCPassManager &CGPM, const PipelineElement &E);
  650. Error parseFunctionPass(FunctionPassManager &FPM, const PipelineElement &E);
  651. Error parseLoopPass(LoopPassManager &LPM, const PipelineElement &E);
  652. bool parseAAPassName(AAManager &AA, StringRef Name);
  653. Error parseLoopPassPipeline(LoopPassManager &LPM,
  654. ArrayRef<PipelineElement> Pipeline);
  655. Error parseFunctionPassPipeline(FunctionPassManager &FPM,
  656. ArrayRef<PipelineElement> Pipeline);
  657. Error parseCGSCCPassPipeline(CGSCCPassManager &CGPM,
  658. ArrayRef<PipelineElement> Pipeline);
  659. Error parseModulePassPipeline(ModulePassManager &MPM,
  660. ArrayRef<PipelineElement> Pipeline);
  661. void addPGOInstrPasses(ModulePassManager &MPM, OptimizationLevel Level,
  662. bool RunProfileGen, bool IsCS, std::string ProfileFile,
  663. std::string ProfileRemappingFile);
  664. void invokePeepholeEPCallbacks(FunctionPassManager &, OptimizationLevel);
  665. // Extension Point callbacks
  666. SmallVector<std::function<void(FunctionPassManager &, OptimizationLevel)>, 2>
  667. PeepholeEPCallbacks;
  668. SmallVector<std::function<void(LoopPassManager &, OptimizationLevel)>, 2>
  669. LateLoopOptimizationsEPCallbacks;
  670. SmallVector<std::function<void(LoopPassManager &, OptimizationLevel)>, 2>
  671. LoopOptimizerEndEPCallbacks;
  672. SmallVector<std::function<void(FunctionPassManager &, OptimizationLevel)>, 2>
  673. ScalarOptimizerLateEPCallbacks;
  674. SmallVector<std::function<void(CGSCCPassManager &, OptimizationLevel)>, 2>
  675. CGSCCOptimizerLateEPCallbacks;
  676. SmallVector<std::function<void(FunctionPassManager &, OptimizationLevel)>, 2>
  677. VectorizerStartEPCallbacks;
  678. SmallVector<std::function<void(ModulePassManager &, OptimizationLevel)>, 2>
  679. OptimizerLastEPCallbacks;
  680. // Module callbacks
  681. SmallVector<std::function<void(ModulePassManager &, OptimizationLevel)>, 2>
  682. PipelineStartEPCallbacks;
  683. SmallVector<std::function<void(ModulePassManager &, OptimizationLevel)>, 2>
  684. PipelineEarlySimplificationEPCallbacks;
  685. SmallVector<std::function<void(ModuleAnalysisManager &)>, 2>
  686. ModuleAnalysisRegistrationCallbacks;
  687. SmallVector<std::function<bool(StringRef, ModulePassManager &,
  688. ArrayRef<PipelineElement>)>,
  689. 2>
  690. ModulePipelineParsingCallbacks;
  691. SmallVector<std::function<bool(ModulePassManager &, ArrayRef<PipelineElement>,
  692. bool DebugLogging)>,
  693. 2>
  694. TopLevelPipelineParsingCallbacks;
  695. // CGSCC callbacks
  696. SmallVector<std::function<void(CGSCCAnalysisManager &)>, 2>
  697. CGSCCAnalysisRegistrationCallbacks;
  698. SmallVector<std::function<bool(StringRef, CGSCCPassManager &,
  699. ArrayRef<PipelineElement>)>,
  700. 2>
  701. CGSCCPipelineParsingCallbacks;
  702. // Function callbacks
  703. SmallVector<std::function<void(FunctionAnalysisManager &)>, 2>
  704. FunctionAnalysisRegistrationCallbacks;
  705. SmallVector<std::function<bool(StringRef, FunctionPassManager &,
  706. ArrayRef<PipelineElement>)>,
  707. 2>
  708. FunctionPipelineParsingCallbacks;
  709. // Loop callbacks
  710. SmallVector<std::function<void(LoopAnalysisManager &)>, 2>
  711. LoopAnalysisRegistrationCallbacks;
  712. SmallVector<std::function<bool(StringRef, LoopPassManager &,
  713. ArrayRef<PipelineElement>)>,
  714. 2>
  715. LoopPipelineParsingCallbacks;
  716. // AA callbacks
  717. SmallVector<std::function<bool(StringRef Name, AAManager &AA)>, 2>
  718. AAParsingCallbacks;
  719. };
  720. /// This utility template takes care of adding require<> and invalidate<>
  721. /// passes for an analysis to a given \c PassManager. It is intended to be used
  722. /// during parsing of a pass pipeline when parsing a single PipelineName.
  723. /// When registering a new function analysis FancyAnalysis with the pass
  724. /// pipeline name "fancy-analysis", a matching ParsePipelineCallback could look
  725. /// like this:
  726. ///
  727. /// static bool parseFunctionPipeline(StringRef Name, FunctionPassManager &FPM,
  728. /// ArrayRef<PipelineElement> P) {
  729. /// if (parseAnalysisUtilityPasses<FancyAnalysis>("fancy-analysis", Name,
  730. /// FPM))
  731. /// return true;
  732. /// return false;
  733. /// }
  734. template <typename AnalysisT, typename IRUnitT, typename AnalysisManagerT,
  735. typename... ExtraArgTs>
  736. bool parseAnalysisUtilityPasses(
  737. StringRef AnalysisName, StringRef PipelineName,
  738. PassManager<IRUnitT, AnalysisManagerT, ExtraArgTs...> &PM) {
  739. if (!PipelineName.endswith(">"))
  740. return false;
  741. // See if this is an invalidate<> pass name
  742. if (PipelineName.startswith("invalidate<")) {
  743. PipelineName = PipelineName.substr(11, PipelineName.size() - 12);
  744. if (PipelineName != AnalysisName)
  745. return false;
  746. PM.addPass(InvalidateAnalysisPass<AnalysisT>());
  747. return true;
  748. }
  749. // See if this is a require<> pass name
  750. if (PipelineName.startswith("require<")) {
  751. PipelineName = PipelineName.substr(8, PipelineName.size() - 9);
  752. if (PipelineName != AnalysisName)
  753. return false;
  754. PM.addPass(RequireAnalysisPass<AnalysisT, IRUnitT, AnalysisManagerT,
  755. ExtraArgTs...>());
  756. return true;
  757. }
  758. return false;
  759. }
  760. }
  761. #endif
  762. #ifdef __GNUC__
  763. #pragma GCC diagnostic pop
  764. #endif