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