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