5 Passes represent the basic infrastructure for transformation and optimization.
6 This document provides an overview of the pass infrastructure in MLIR and how to
9 See [MLIR specification](LangRef.md) for more information about MLIR and its
10 core aspects, such as the IR structure and operations.
12 See [MLIR Rewrites](Tutorials/QuickstartRewrites.md) for a quick start on graph
13 rewriting in MLIR. If a transformation involves pattern matching operation DAGs,
14 this is a great place to start.
18 In MLIR, the main unit of abstraction and transformation is an
19 [operation](LangRef.md/#operations). As such, the pass manager is designed to
20 work on instances of operations at different levels of nesting. The structure of
21 the [pass manager](#pass-manager), and the concept of nesting, is detailed
22 further below. All passes in MLIR derive from `OperationPass` and adhere to the
23 following restrictions; any noncompliance will lead to problematic behavior in
24 multithreaded and other advanced scenarios:
26 * Must not modify any state referenced or relied upon outside the current
27 operation being operated on. This includes adding or removing operations
28 from the parent block, changing the attributes(depending on the contract
29 of the current operation)/operands/results/successors of the current operation.
30 * Must not modify the state of another operation not nested within the current
31 operation being operated on.
32 * Other threads may be operating on these operations simultaneously.
33 * Must not inspect the state of sibling operations.
34 * Other threads may be modifying these operations in parallel.
35 * Inspecting the state of ancestor/parent operations is permitted.
36 * Must not maintain mutable pass state across invocations of `runOnOperation`.
37 A pass may be run on many different operations with no guarantee of
39 * When multithreading, a specific pass instance may not even execute on
40 all operations within the IR. As such, a pass should not rely on running
42 * Must not maintain any global mutable state, e.g. static variables within the
43 source file. All mutable state should be maintained by an instance of the
45 * Must be copy-constructible
46 * Multiple instances of the pass may be created by the pass manager to
47 process operations in parallel.
49 ### Op-Agnostic Operation Passes
51 By default, an operation pass is `op-agnostic`, meaning that it operates on the
52 operation type of the pass manager that it is added to. This means a pass may operate
53 on many different types of operations. Agnostic passes should be written such that
54 they do not make assumptions on the operation they run on. Examples of this type of pass are
55 [Canonicalization](Pass.md/-canonicalize-canonicalize-operations)
56 [Common Sub-Expression Elimination](Passes.md/#-cse-eliminate-common-sub-expressions).
58 To create an agnostic operation pass, a derived class must adhere to the following:
60 * Inherit from the CRTP class `OperationPass`.
61 * Override the virtual `void runOnOperation()` method.
63 A simple pass may look like:
66 /// Here we utilize the CRTP `PassWrapper` utility class to provide some
67 /// necessary utility hooks. This is only necessary for passes defined directly
68 /// in C++. Passes defined declaratively use a cleaner mechanism for providing
70 struct MyOperationPass : public PassWrapper<MyOperationPass, OperationPass<>> {
71 void runOnOperation() override {
72 // Get the current operation being operated on.
73 Operation *op = getOperation();
79 ### Filtered Operation Pass
81 If a pass needs to constrain its execution to specific types or classes of operations,
82 additional filtering may be applied on top. This transforms a once `agnostic` pass into
83 one more specific to a certain context. There are various ways in which to filter the
84 execution of a pass, and different contexts in which filtering may apply:
86 ### Operation Pass: Static Schedule Filtering
88 Static filtering allows for applying additional constraints on the operation types a
89 pass may be scheduled on. This type of filtering generally allows for building more
90 constrained passes that can only be scheduled on operations that satisfy the necessary
91 constraints. For example, this allows for specifying passes that only run on operations
92 of a certain, those that provide a certain interface, trait, or some other constraint that
93 applies to all instances of that operation type. Below is an example of a pass that only
94 permits scheduling on operations that implement `FunctionOpInterface`:
97 struct MyFunctionPass : ... {
98 /// This method is used to provide additional static filtering, and returns if the
99 /// pass may be scheduled on the given operation type.
100 bool canScheduleOn(RegisteredOperationName opInfo) const override {
101 return opInfo.hasInterface<FunctionOpInterface>();
104 void runOnOperation() {
105 // Here we can freely cast to FunctionOpInterface, because our `canScheduleOn` ensures
106 // that our pass is only executed on operations implementing that interface.
107 FunctionOpInterface op = cast<FunctionOpInterface>(getOperation());
112 When a pass with static filtering is added to an [`op-specific` pass manager](#oppassmanager),
113 it asserts that the operation type of the pass manager satisfies the static constraints of the
114 pass. When added to an [`op-agnostic` pass manager](#oppassmanager), that pass manager, and all
115 passes contained within, inherits the static constraints of the pass. For example, if the pass
116 filters on `FunctionOpInterface`, as in the `MyFunctionPass` example above, only operations that
117 implement `FunctionOpInterface` will be considered when executing **any** passes within the pass
118 manager. This invariant is important to keep in mind, as each pass added to an `op-agnostic` pass
119 manager further constrains the operations that may be scheduled on it. Consider the following example:
132 If we were to apply the op-agnostic pipeline, `any(cse,my-function-pass)`, to the above MLIR snippet
133 it would only run on the `foo` function operation. This is because the `my-function-pass` has a
134 static filtering constraint to only schedule on operations implementing `FunctionOpInterface`. Remember
135 that this constraint is inherited by the entire pass manager, so we never consider `someModule` for
136 any of the passes, including `cse` which normally can be scheduled on any operation.
138 #### Operation Pass: Static Filtering By Op Type
140 In the above section, we detailed a general mechanism for statically filtering the types of operations
141 that a pass may be scheduled on. Sugar is provided on top of that mechanism to simplify the definition
142 of passes that are restricted to scheduling on a single operation type. In these cases, a pass simply
143 needs to provide the type of operation to the `OperationPass` base class. This will automatically
144 instill filtering on that operation type:
147 /// Here we utilize the CRTP `PassWrapper` utility class to provide some
148 /// necessary utility hooks. This is only necessary for passes defined directly
149 /// in C++. Passes defined declaratively use a cleaner mechanism for providing
151 struct MyFunctionPass : public PassWrapper<MyOperationPass, OperationPass<func::FuncOp>> {
152 void runOnOperation() {
153 // Get the current operation being operated on.
154 func::FuncOp op = getOperation();
159 #### Operation Pass: Static Filtering By Interface
161 In the above section, we detailed a general mechanism for statically filtering the types of operations
162 that a pass may be scheduled on. Sugar is provided on top of that mechanism to simplify the definition
163 of passes that are restricted to scheduling on a specific operation interface. In these cases, a pass
164 simply needs to inherit from the `InterfacePass` base class. This class is similar to `OperationPass`,
165 but expects the type of interface to operate on. This will automatically instill filtering on that
169 /// Here we utilize the CRTP `PassWrapper` utility class to provide some
170 /// necessary utility hooks. This is only necessary for passes defined directly
171 /// in C++. Passes defined declaratively use a cleaner mechanism for providing
173 struct MyFunctionPass : public PassWrapper<MyOperationPass, InterfacePass<FunctionOpInterface>> {
174 void runOnOperation() {
175 // Get the current operation being operated on.
176 FunctionOpInterface op = getOperation();
181 ### Dependent Dialects
183 Dialects must be loaded in the MLIRContext before entities from these dialects
184 (operations, types, attributes, ...) can be created. Dialects must also be
185 loaded before starting the execution of a multi-threaded pass pipeline. To this
186 end, a pass that may create an entity from a dialect that isn't guaranteed to
187 already be loaded must express this by overriding the `getDependentDialects()`
188 method and declare this list of Dialects explicitly.
189 See also the `dependentDialects` field in the
190 [TableGen Specification](#tablegen-specification).
194 In certain situations, a Pass may contain state that is constructed dynamically,
195 but is potentially expensive to recompute in successive runs of the Pass. One
196 such example is when using [`PDL`-based](Dialects/PDLOps.md)
197 [patterns](PatternRewriter.md), which are compiled into a bytecode during
198 runtime. In these situations, a pass may override the following hook to
199 initialize this heavy state:
201 * `LogicalResult initialize(MLIRContext *context)`
203 This hook is executed once per run of a full pass pipeline, meaning that it does
204 not have access to the state available during a `runOnOperation` call. More
205 concretely, all necessary accesses to an `MLIRContext` should be driven via the
206 provided `context` parameter, and methods that utilize "per-run" state such as
207 `getContext`/`getOperation`/`getAnalysis`/etc. must not be used.
208 In case of an error during initialization, the pass is expected to emit an error
209 diagnostic and return a `failure()` which will abort the pass pipeline execution.
211 ## Analysis Management
213 An important concept, along with transformation passes, are analyses. These are
214 conceptually similar to transformation passes, except that they compute
215 information on a specific operation without modifying it. In MLIR, analyses are
216 not passes but free-standing classes that are computed lazily on-demand and
217 cached to avoid unnecessary recomputation. An analysis in MLIR must adhere to
220 * Provide a valid constructor taking either an `Operation*` or `Operation*`
221 and `AnalysisManager &`.
222 * The provided `AnalysisManager &` should be used to query any necessary
223 analysis dependencies.
224 * Must not modify the given operation.
226 An analysis may provide additional hooks to control various behavior:
228 * `bool isInvalidated(const AnalysisManager::PreservedAnalyses &)`
230 Given a preserved analysis set, the analysis returns true if it should truly be
231 invalidated. This allows for more fine-tuned invalidation in cases where an
232 analysis wasn't explicitly marked preserved, but may be preserved (or
233 invalidated) based upon other properties such as analyses sets. If the analysis
234 uses any other analysis as a dependency, it must also check if the dependency
237 ### Querying Analyses
239 The base `OperationPass` class provides utilities for querying and preserving
240 analyses for the current operation being processed.
242 * OperationPass automatically provides the following utilities for querying
245 - Get an analysis for the current operation, constructing it if
247 * `getCachedAnalysis<>`
248 - Get an analysis for the current operation, if it already exists.
249 * `getCachedParentAnalysis<>`
250 - Get an analysis for a given parent operation, if it exists.
251 * `getCachedChildAnalysis<>`
252 - Get an analysis for a given child operation, if it exists.
253 * `getChildAnalysis<>`
254 - Get an analysis for a given child operation, constructing it if
257 Using the example passes defined above, let's see some examples:
260 /// An interesting analysis.
261 struct MyOperationAnalysis {
262 // Compute this analysis with the provided operation.
263 MyOperationAnalysis(Operation *op);
266 struct MyOperationAnalysisWithDependency {
267 MyOperationAnalysisWithDependency(Operation *op, AnalysisManager &am) {
268 // Request other analysis as dependency
269 MyOperationAnalysis &otherAnalysis = am.getAnalysis<MyOperationAnalysis>();
273 bool isInvalidated(const AnalysisManager::PreservedAnalyses &pa) {
274 // Check if analysis or its dependency were invalidated
275 return !pa.isPreserved<MyOperationAnalysisWithDependency>() ||
276 !pa.isPreserved<MyOperationAnalysis>();
280 void MyOperationPass::runOnOperation() {
281 // Query MyOperationAnalysis for the current operation.
282 MyOperationAnalysis &myAnalysis = getAnalysis<MyOperationAnalysis>();
284 // Query a cached instance of MyOperationAnalysis for the current operation.
285 // It will not be computed if it doesn't exist.
286 auto optionalAnalysis = getCachedAnalysis<MyOperationAnalysis>();
287 if (optionalAnalysis)
290 // Query a cached instance of MyOperationAnalysis for the parent operation of
291 // the current operation. It will not be computed if it doesn't exist.
292 auto optionalAnalysis = getCachedParentAnalysis<MyOperationAnalysis>();
293 if (optionalAnalysis)
298 ### Preserving Analyses
300 Analyses that are constructed after being queried by a pass are cached to avoid
301 unnecessary computation if they are requested again later. To avoid stale
302 analyses, all analyses are assumed to be invalidated by a pass. To avoid
303 invalidation, a pass must specifically mark analyses that are known to be
306 * All Pass classes automatically provide the following utilities for
308 * `markAllAnalysesPreserved`
309 * `markAnalysesPreserved<>`
312 void MyOperationPass::runOnOperation() {
313 // Mark all analyses as preserved. This is useful if a pass can guarantee
314 // that no transformation was performed.
315 markAllAnalysesPreserved();
317 // Mark specific analyses as preserved. This is used if some transformation
318 // was performed, but some analyses were either unaffected or explicitly
320 markAnalysesPreserved<MyAnalysis, MyAnalyses...>();
326 Passes in MLIR are allowed to gracefully fail. This may happen if some invariant
327 of the pass was broken, potentially leaving the IR in some invalid state. If
328 such a situation occurs, the pass can directly signal a failure to the pass
329 manager via the `signalPassFailure` method. If a pass signaled a failure when
330 executing, no other passes in the pipeline will execute and the top-level call
331 to `PassManager::run` will return `failure`.
334 void MyOperationPass::runOnOperation() {
335 // Signal failure on a broken invariant.
336 if (some_broken_invariant)
337 return signalPassFailure();
343 The above sections introduced the different types of passes and their
344 invariants. This section introduces the concept of a PassManager, and how it can
345 be used to configure and schedule a pass pipeline. There are two main classes
346 related to pass management, the `PassManager` and the `OpPassManager`. The
347 `PassManager` class acts as the top-level entry point, and contains various
348 configurations used for the entire pass pipeline. The `OpPassManager` class is
349 used to schedule passes to run at a specific level of nesting. The top-level
350 `PassManager` also functions as an `OpPassManager`.
354 An `OpPassManager` is essentially a collection of passes anchored to execute on
355 operations at a given level of nesting. A pass manager may be `op-specific`
356 (anchored on a specific operation type), or `op-agnostic` (not restricted to any
357 specific operation, and executed on any viable operation type). Operation types that
358 anchor pass managers must adhere to the following requirement:
360 * Must be registered and marked
361 [`IsolatedFromAbove`](Traits.md/#isolatedfromabove).
363 * Passes are expected not to modify operations at or above the current
364 operation being processed. If the operation is not isolated, it may
365 inadvertently modify or traverse the SSA use-list of an operation it is
368 Passes can be added to a pass manager via `addPass`.
370 An `OpPassManager` is generally created by explicitly nesting a pipeline within
371 another existing `OpPassManager` via the `nest<OpT>` or `nestAny` methods. The
372 former method takes the operation type that the nested pass manager will operate on.
373 The latter method nests an `op-agnostic` pass manager, that may run on any viable
374 operation type. Nesting in this sense, corresponds to the
375 [structural](Tutorials/UnderstandingTheIRStructure.md) nesting within
376 [Regions](LangRef.md/#regions) of the IR.
378 For example, the following `.mlir`:
382 spirv.module "Logical" "GLSL450" {
390 Has the nesting structure of:
398 Below is an example of constructing a pipeline that operates on the above
402 // Create a top-level `PassManager` class.
403 auto pm = PassManager::on<ModuleOp>(ctx);
405 // Add a pass on the top-level module operation.
406 pm.addPass(std::make_unique<MyModulePass>());
408 // Nest a pass manager that operates on `spirv.module` operations nested
409 // directly under the top-level module.
410 OpPassManager &nestedModulePM = pm.nest<spirv::ModuleOp>();
411 nestedModulePM.addPass(std::make_unique<MySPIRVModulePass>());
413 // Nest a pass manager that operates on functions within the nested SPIRV
415 OpPassManager &nestedFunctionPM = nestedModulePM.nest<func::FuncOp>();
416 nestedFunctionPM.addPass(std::make_unique<MyFunctionPass>());
418 // Nest an op-agnostic pass manager. This will operate on any viable
419 // operation, e.g. func.func, spirv.func, spirv.module, builtin.module, etc.
420 OpPassManager &nestedAnyPM = nestedModulePM.nestAny();
421 nestedAnyPM.addPass(createCanonicalizePass());
422 nestedAnyPM.addPass(createCSEPass());
424 // Run the pass manager on the top-level module.
426 if (failed(pm.run(m)))
427 ... // One of the passes signaled a failure.
430 The above pass manager contains the following pipeline structure:
433 OpPassManager<ModuleOp>
435 OpPassManager<spirv::ModuleOp>
437 OpPassManager<func::FuncOp>
444 These pipelines are then run over a single operation at a time. This means that,
445 for example, given a series of consecutive passes on func::FuncOp, it will execute all
446 on the first function, then all on the second function, etc. until the entire
447 program has been run through the passes. This provides several benefits:
449 * This improves the cache behavior of the compiler, because it is only
450 touching a single function at a time, instead of traversing the entire
452 * This improves multi-threading performance by reducing the number of jobs
453 that need to be scheduled, as well as increasing the efficiency of each job.
454 An entire function pipeline can be run on each function asynchronously.
456 ## Dynamic Pass Pipelines
458 In some situations it may be useful to run a pass pipeline within another pass,
459 to allow configuring or filtering based on some invariants of the current
460 operation being operated on. For example, the
461 [Inliner Pass](Passes.md/#-inline) may want to run
462 intraprocedural simplification passes while it is inlining to produce a better
463 cost model, and provide more optimal inlining. To enable this, passes may run an
464 arbitrary `OpPassManager` on the current operation being operated on or any
465 operation nested within the current operation via the `LogicalResult
466 Pass::runPipeline(OpPassManager &, Operation *)` method. This method returns
467 whether the dynamic pipeline succeeded or failed, similarly to the result of the
468 top-level `PassManager::run` method. A simple example is shown below:
471 void MyModulePass::runOnOperation() {
472 ModuleOp module = getOperation();
473 if (hasSomeSpecificProperty(module)) {
474 OpPassManager dynamicPM("builtin.module");
475 ...; // Build the dynamic pipeline.
476 if (failed(runPipeline(dynamicPM, module)))
477 return signalPassFailure();
482 Note: though above the dynamic pipeline was constructed within the
483 `runOnOperation` method, this is not necessary and pipelines should be cached
484 when possible as the `OpPassManager` class can be safely copy constructed.
486 The mechanism described in this section should be used whenever a pass pipeline
487 should run in a nested fashion, i.e. when the nested pipeline cannot be
488 scheduled statically along with the rest of the main pass pipeline. More
489 specifically, a `PassManager` should generally never need to be constructed
490 within a `Pass`. Using `runPipeline` also ensures that all analyses,
491 [instrumentations](#pass-instrumentation), and other pass manager related
492 components are integrated with the dynamic pipeline being executed.
494 ## Instance Specific Pass Options
496 MLIR provides a builtin mechanism for passes to specify options that configure
497 its behavior. These options are parsed at pass construction time independently
498 for each instance of the pass. Options are defined using the `Option<>` and
499 `ListOption<>` classes, and generally follow the
500 [LLVM command line](https://llvm.org/docs/CommandLine.html) flag definition
501 rules. One major distinction from the LLVM command line functionality is that
502 all `ListOption`s are comma-separated, and delimited sub-ranges within individual
503 elements of the list may contain commas that are not treated as separators for the
508 /// Make sure that we have a valid default constructor and copy constructor to
509 /// ensure that the options are initialized properly.
511 MyPass(const MyPass& pass) {}
513 /// Any parameters after the description are forwarded to llvm::cl::list and
514 /// llvm::cl::opt respectively.
515 Option<int> exampleOption{*this, "flag-name", llvm::cl::desc("...")};
516 ListOption<int> exampleListOption{*this, "list-flag-name", llvm::cl::desc("...")};
520 For pass pipelines, the `PassPipelineRegistration` templates take an additional
521 template parameter for an optional `Option` struct definition. This struct
522 should inherit from `mlir::PassPipelineOptions` and contain the desired pipeline
523 options. When using `PassPipelineRegistration`, the constructor now takes a
524 function with the signature `void (OpPassManager &pm, const MyPipelineOptions&)`
525 which should construct the passes from the options and pass them to the pm:
528 struct MyPipelineOptions : public PassPipelineOptions {
529 // The structure of these options is the same as those for pass options.
530 Option<int> exampleOption{*this, "flag-name", llvm::cl::desc("...")};
531 ListOption<int> exampleListOption{*this, "list-flag-name",
532 llvm::cl::desc("...")};
535 void registerMyPasses() {
536 PassPipelineRegistration<MyPipelineOptions>(
537 "example-pipeline", "Run an example pipeline.",
538 [](OpPassManager &pm, const MyPipelineOptions &pipelineOptions) {
539 // Initialize the pass manager.
546 Statistics are a way to keep track of what the compiler is doing and how
547 effective various transformations are. It is often useful to see what effect
548 specific transformations have on a particular input, and how often they trigger.
549 Pass statistics are specific to each pass instance, which allow for seeing the
550 effect of placing a particular transformation at specific places within the pass
551 pipeline. For example, they help answer questions like "What happens if I run
554 Statistics can be added to a pass by using the 'Pass::Statistic' class. This
555 class takes as a constructor arguments: the parent pass, a name, and a
556 description. This class acts like an atomic unsigned integer, and may be
557 incremented and updated accordingly. These statistics rely on the same
559 [`llvm::Statistic`](http://llvm.org/docs/ProgrammersManual.html#the-statistic-class-stats-option)
560 and thus have similar usage constraints. Collected statistics can be dumped by
561 the [pass manager](#pass-manager) programmatically via
562 `PassManager::enableStatistics`; or via `-mlir-pass-statistics` and
563 `-mlir-pass-statistics-display` on the command line.
565 An example is shown below:
569 /// Make sure that we have a valid default constructor and copy constructor to
570 /// ensure that the options are initialized properly.
572 MyPass(const MyPass& pass) {}
573 StringRef getArgument() const final {
574 // This is the argument used to refer to the pass in
575 // the textual format (on the commandline for example).
578 StringRef getDescription() const final {
579 // This is a brief description of the pass.
580 return "description";
582 /// Define the statistic to track during the execution of MyPass.
583 Statistic exampleStat{this, "exampleStat", "An example statistic"};
585 void runOnOperation() {
588 // Update the statistic after some invariant was hit.
596 The collected statistics may be aggregated in two types of views:
598 A pipeline view that models the structure of the pass manager, this is the
602 $ mlir-opt -pass-pipeline='any(func.func(my-pass,my-pass))' foo.mlir -mlir-pass-statistics
604 ===-------------------------------------------------------------------------===
605 ... Pass statistics report ...
606 ===-------------------------------------------------------------------------===
609 (S) 15 exampleStat - An example statistic
612 (S) 6 exampleStat - An example statistic
617 A list view that aggregates the statistics of all instances of a specific pass
621 $ mlir-opt -pass-pipeline='any(func.func(my-pass,my-pass))' foo.mlir -mlir-pass-statistics -mlir-pass-statistics-display=list
623 ===-------------------------------------------------------------------------===
624 ... Pass statistics report ...
625 ===-------------------------------------------------------------------------===
627 (S) 21 exampleStat - An example statistic
632 Briefly shown in the example definitions of the various pass types is the
633 `PassRegistration` class. This mechanism allows for registering pass classes so
634 that they may be created within a
635 [textual pass pipeline description](#textual-pass-pipeline-specification). An
636 example registration is shown below:
639 void registerMyPass() {
640 PassRegistration<MyPass>();
644 * `MyPass` is the name of the derived pass class.
645 * The pass `getArgument()` method is used to get the identifier that will be
646 used to refer to the pass.
647 * The pass `getDescription()` method provides a short summary describing the
650 For passes that cannot be default-constructed, `PassRegistration` accepts an
651 optional argument that takes a callback to create the pass:
654 void registerMyPass() {
655 PassRegistration<MyParametricPass>(
656 []() -> std::unique_ptr<Pass> {
657 std::unique_ptr<Pass> p = std::make_unique<MyParametricPass>(/*options*/);
658 /*... non-trivial-logic to configure the pass ...*/;
664 This variant of registration can be used, for example, to accept the
665 configuration of a pass from command-line arguments and pass it to the pass
668 Note: Make sure that the pass is copy-constructible in a way that does not share
669 data as the [pass manager](#pass-manager) may create copies of the pass to run
672 ### Pass Pipeline Registration
674 Described above is the mechanism used for registering a specific derived pass
675 class. On top of that, MLIR allows for registering custom pass pipelines in a
676 similar fashion. This allows for custom pipelines to be available to tools like
677 mlir-opt in the same way that passes are, which is useful for encapsulating
678 common pipelines like the "-O1" series of passes. Pipelines are registered via a
679 similar mechanism to passes in the form of `PassPipelineRegistration`. Compared
680 to `PassRegistration`, this class takes an additional parameter in the form of a
681 pipeline builder that modifies a provided `OpPassManager`.
684 void pipelineBuilder(OpPassManager &pm) {
685 pm.addPass(std::make_unique<MyPass>());
686 pm.addPass(std::make_unique<MyOtherPass>());
689 void registerMyPasses() {
690 // Register an existing pipeline builder function.
691 PassPipelineRegistration<>(
692 "argument", "description", pipelineBuilder);
694 // Register an inline pipeline builder.
695 PassPipelineRegistration<>(
696 "argument", "description", [](OpPassManager &pm) {
697 pm.addPass(std::make_unique<MyPass>());
698 pm.addPass(std::make_unique<MyOtherPass>());
703 ### Textual Pass Pipeline Specification
705 The previous sections detailed how to register passes and pass pipelines with a
706 specific argument and description. Once registered, these can be used to
707 configure a pass manager from a string description. This is especially useful
708 for tools like `mlir-opt`, that configure pass managers from the command line,
709 or as options to passes that utilize
710 [dynamic pass pipelines](#dynamic-pass-pipelines).
712 To support the ability to describe the full structure of pass pipelines, MLIR
713 supports a custom textual description of pass pipelines. The textual description
714 includes the nesting structure, the arguments of the passes and pass pipelines
715 to run, and any options for those passes and pipelines. A textual pipeline is
716 defined as a series of names, each of which may in itself recursively contain a
717 nested pipeline description. The syntax for this specification is as follows:
720 pipeline ::= op-anchor `(` pipeline-element (`,` pipeline-element)* `)`
721 pipeline-element ::= pipeline | (pass-name | pass-pipeline-name) options?
722 options ::= '{' (key ('=' value)?)+ '}'
726 * This corresponds to the mnemonic name that anchors the execution of the
727 pass manager. This is either the name of an operation to run passes on,
728 e.g. `func.func` or `builtin.module`, or `any`, for op-agnostic pass
729 managers that execute on any viable operation (i.e. any operation that
730 can be used to anchor a pass manager).
731 * `pass-name` | `pass-pipeline-name`
732 * This corresponds to the argument of a registered pass or pass pipeline,
733 e.g. `cse` or `canonicalize`.
735 * Options are specific key value pairs representing options defined by a
736 pass or pass pipeline, as described in the
737 ["Instance Specific Pass Options"](#instance-specific-pass-options)
738 section. See this section for an example usage in a textual pipeline.
740 For example, the following pipeline:
743 $ mlir-opt foo.mlir -cse -canonicalize -convert-func-to-llvm='use-bare-ptr-memref-call-conv=1'
746 Can also be specified as (via the `-pass-pipeline` flag):
749 # Anchor the cse and canonicalize passes on the `func.func` operation.
750 $ mlir-opt foo.mlir -pass-pipeline='builtin.module(func.func(cse,canonicalize),convert-func-to-llvm{use-bare-ptr-memref-call-conv=1})'
752 # Anchor the cse and canonicalize passes on "any" viable root operation.
753 $ mlir-opt foo.mlir -pass-pipeline='builtin.module(any(cse,canonicalize),convert-func-to-llvm{use-bare-ptr-memref-call-conv=1})'
756 In order to support round-tripping a pass to the textual representation using
757 `OpPassManager::printAsTextualPipeline(raw_ostream&)`, override `StringRef
758 Pass::getArgument()` to specify the argument used when registering a pass.
760 ## Declarative Pass Specification
762 Some aspects of a Pass may be specified declaratively, in a form similar to
763 [operations](DefiningDialects/Operations.md). This specification simplifies several mechanisms
764 used when defining passes. It can be used for generating pass registration
765 calls, defining boilerplate pass utilities, and generating pass documentation.
767 Consider the following pass specified in C++:
770 struct MyPass : PassWrapper<MyPass, OperationPass<ModuleOp>> {
772 MyPass(const MyPass &) {}
776 // Specify any options.
778 *this, "example-option",
779 llvm::cl::desc("An example option"), llvm::cl::init(true)};
780 ListOption<int64_t> listOption{
781 *this, "example-list",
782 llvm::cl::desc("An example list option")};
784 // Specify any statistics.
785 Statistic statistic{this, "example-statistic", "An example statistic"};
788 /// Expose this pass to the outside world.
789 std::unique_ptr<Pass> foo::createMyPass() {
790 return std::make_unique<MyPass>();
793 /// Register this pass.
794 void foo::registerMyPass() {
795 PassRegistration<MyPass>();
799 This pass may be specified declaratively as so:
802 def MyPass : Pass<"my-pass", "ModuleOp"> {
803 let summary = "My Pass Summary";
805 Here we can now give a much larger description of `MyPass`, including all of
806 its various constraints and behavior.
809 // A constructor must be provided to specify how to create a default instance
810 // of MyPass. It can be skipped for this specific example, because both the
811 // constructor and the registration methods live in the same namespace.
812 let constructor = "foo::createMyPass()";
814 // Specify any options.
816 Option<"option", "example-option", "bool", /*default=*/"true",
817 "An example option">,
818 ListOption<"listOption", "example-list", "int64_t",
819 "An example list option">
822 // Specify any statistics.
824 Statistic<"statistic", "example-statistic", "An example statistic">
829 Using the `gen-pass-decls` generator, we can generate most of the boilerplate
830 above automatically. This generator takes as an input a `-name` parameter, that
831 provides a tag for the group of passes that are being generated. This generator
832 produces code with multiple purposes:
834 The first is to register the declared passes with the global registry. For
835 each pass, the generator produces a `registerPassName` where
836 `PassName` is the name of the definition specified in tablegen. It also
837 generates a `registerGroupPasses`, where `Group` is the tag provided via the
838 `-name` input parameter, that registers all of the passes present.
841 // Tablegen options: -gen-pass-decls -name="Example"
846 #define GEN_PASS_REGISTRATION
847 #include "Passes.h.inc"
850 void registerMyPasses() {
851 // Register all of the passes.
852 foo::registerExamplePasses();
856 // Register `MyPass` specifically.
857 foo::registerMyPass();
861 The second is to provide a way to configure the pass options. These classes are
862 named in the form of `MyPassOptions`, where `MyPass` is the name of the pass
863 definition in tablegen. The configurable parameters reflect the options declared
864 in the tablegen file. These declarations can be enabled for the whole group of
865 passes by defining the `GEN_PASS_DECL` macro, or on a per-pass basis by defining
866 `GEN_PASS_DECL_PASSNAME` where `PASSNAME` is the uppercase version of the name
867 specified in tablegen.
872 #ifdef GEN_PASS_DECL_MYPASS
874 struct MyPassOptions {
876 ::llvm::ArrayRef<int64_t> listOption;
879 #undef GEN_PASS_DECL_MYPASS
880 #endif // GEN_PASS_DECL_MYPASS
883 If the `constructor` field has not been specified in the tablegen declaration,
884 then autogenerated file will also contain the declarations of the default
890 #ifdef GEN_PASS_DECL_MYPASS
893 std::unique_ptr<::mlir::Pass> createMyPass();
894 std::unique_ptr<::mlir::Pass> createMyPass(const MyPassOptions &options);
896 #undef GEN_PASS_DECL_MYPASS
897 #endif // GEN_PASS_DECL_MYPASS
900 The last purpose of this generator is to emit a base class for each of the
901 passes, containing most of the boiler plate related to pass definitions. These
902 classes are named in the form of `MyPassBase` and are declared inside the
903 `impl` namespace, where `MyPass` is the name of the pass definition in
904 tablegen. We can update the original C++ pass definition as so:
909 /// Include the generated base pass class definitions.
911 #define GEN_PASS_DEF_MYPASS
912 #include "Passes.h.inc"
915 /// Define the main class as deriving from the generated base class.
916 struct MyPass : foo::impl::MyPassBase<MyPass> {
917 using MyPassBase::MyPassBase;
919 /// The definitions of the options and statistics are now generated within
920 /// the base class, but are accessible in the same way.
924 These definitions can be enabled on a per-pass basis by defining the appropriate
925 preprocessor `GEN_PASS_DEF_PASSNAME` macro, with `PASSNAME` equal to the
926 uppercase version of the name of the pass definition in tablegen.
927 If the `constructor` field has not been specified in tablegen, then the default
928 constructors are also defined and expect the name of the actual pass class to
929 be equal to the name defined in tablegen.
931 Using the `gen-pass-doc` generator, markdown documentation for each of the
932 passes can be generated. See [Passes.md](Passes.md) for example output of real
935 ### Tablegen Specification
937 The `Pass` class is used to begin a new pass definition. This class takes as an
938 argument the registry argument to attribute to the pass, as well as an optional
939 string corresponding to the operation type that the pass operates on. The class
940 contains the following fields:
943 - A short one-line summary of the pass, used as the description when
944 registering the pass.
946 - A longer, more detailed description of the pass. This is used when
947 generating pass documentation.
948 * `dependentDialects`
949 - A list of strings representing the `Dialect` classes this pass may
950 introduce entities, Attributes/Operations/Types/etc., of.
952 - A code block used to create a default instance of the pass.
954 - A list of pass options used by the pass.
956 - A list of pass statistics used by the pass.
960 Options may be specified via the `Option` and `ListOption` classes. The `Option`
961 class takes the following template parameters:
964 - A name to use for the generated option variable.
966 - The argument name of the option.
968 - The C++ type of the option.
970 - The default option value.
972 - A one-line description of the option.
973 * additional option flags
974 - A string containing any additional options necessary to construct the
978 def MyPass : Pass<"my-pass"> {
980 Option<"option", "example-option", "bool", /*default=*/"true",
981 "An example option">,
986 The `ListOption` class takes the following fields:
989 - A name to use for the generated option variable.
991 - The argument name of the option.
993 - The C++ type of the list element.
995 - A one-line description of the option.
996 * additional option flags
997 - A string containing any additional options necessary to construct the
1001 def MyPass : Pass<"my-pass"> {
1003 ListOption<"listOption", "example-list", "int64_t",
1004 "An example list option">
1011 Statistics may be specified via the `Statistic`, which takes the following
1012 template parameters:
1015 - A name to use for the generated statistic variable.
1017 - The name used when displaying the statistic.
1019 - A one-line description of the statistic.
1022 def MyPass : Pass<"my-pass"> {
1024 Statistic<"statistic", "example-statistic", "An example statistic">
1029 ## Pass Instrumentation
1031 MLIR provides a customizable framework to instrument pass execution and analysis
1032 computation, via the `PassInstrumentation` class. This class provides hooks into
1033 the PassManager that observe various events:
1035 * `runBeforePipeline`
1036 * This callback is run just before a pass pipeline, i.e. pass manager, is
1038 * `runAfterPipeline`
1039 * This callback is run right after a pass pipeline has been executed,
1040 successfully or not.
1042 * This callback is run just before a pass is executed.
1044 * This callback is run right after a pass has been successfully executed.
1045 If this hook is executed, `runAfterPassFailed` will *not* be.
1046 * `runAfterPassFailed`
1047 * This callback is run right after a pass execution fails. If this hook is
1048 executed, `runAfterPass` will *not* be.
1049 * `runBeforeAnalysis`
1050 * This callback is run just before an analysis is computed.
1051 * If the analysis requested another analysis as a dependency, the
1052 `runBeforeAnalysis`/`runAfterAnalysis` pair for the dependency can be
1053 called from inside of the current `runBeforeAnalysis`/`runAfterAnalysis`
1055 * `runAfterAnalysis`
1056 * This callback is run right after an analysis is computed.
1058 PassInstrumentation instances may be registered directly with a
1059 [PassManager](#pass-manager) instance via the `addInstrumentation` method.
1060 Instrumentations added to the PassManager are run in a stack like fashion, i.e.
1061 the last instrumentation to execute a `runBefore*` hook will be the first to
1062 execute the respective `runAfter*` hook. The hooks of a `PassInstrumentation`
1063 class are guaranteed to be executed in a thread-safe fashion, so additional
1064 synchronization is not necessary. Below in an example instrumentation that
1065 counts the number of times the `DominanceInfo` analysis is computed:
1068 struct DominanceCounterInstrumentation : public PassInstrumentation {
1069 /// The cumulative count of how many times dominance has been calculated.
1072 DominanceCounterInstrumentation(unsigned &count) : count(count) {}
1073 void runAfterAnalysis(llvm::StringRef, TypeID id, Operation *) override {
1074 if (id == TypeID::get<DominanceInfo>())
1079 MLIRContext *ctx = ...;
1080 PassManager pm(ctx);
1082 // Add the instrumentation to the pass manager.
1083 unsigned domInfoCount;
1084 pm.addInstrumentation(
1085 std::make_unique<DominanceCounterInstrumentation>(domInfoCount));
1087 // Run the pass manager on a module operation.
1089 if (failed(pm.run(m)))
1092 llvm::errs() << "DominanceInfo was computed " << domInfoCount << " times!\n";
1095 ### Standard Instrumentations
1097 MLIR utilizes the pass instrumentation framework to provide a few useful
1098 developer tools and utilities. Each of these instrumentations are directly
1099 available to all users of the MLIR pass framework.
1103 The PassTiming instrumentation provides timing information about the execution
1104 of passes and computation of analyses. This provides a quick glimpse into what
1105 passes are taking the most time to execute, as well as how much of an effect a
1106 pass has on the total execution time of the pipeline. Users can enable this
1107 instrumentation directly on the PassManager via `enableTiming`. This
1108 instrumentation is also made available in mlir-opt via the `-mlir-timing` flag.
1109 The PassTiming instrumentation provides several different display modes for the
1110 timing results, each of which is described below:
1112 ##### List Display Mode
1114 In this mode, the results are displayed in a list sorted by total time with each
1115 pass/analysis instance aggregated into one unique result. This view is useful
1116 for getting an overview of what analyses/passes are taking the most time in a
1117 pipeline. This display mode is available in mlir-opt via
1118 `-mlir-timing-display=list`.
1121 $ mlir-opt foo.mlir -mlir-disable-threading -pass-pipeline='builtin.module(func.func(cse,canonicalize),convert-func-to-llvm)' -mlir-timing -mlir-timing-display=list
1123 ===-------------------------------------------------------------------------===
1124 ... Pass execution timing report ...
1125 ===-------------------------------------------------------------------------===
1126 Total Execution Time: 0.0203 seconds
1128 ---Wall Time--- --- Name ---
1129 0.0047 ( 55.9%) Canonicalizer
1130 0.0019 ( 22.2%) VerifierPass
1131 0.0016 ( 18.5%) LLVMLoweringPass
1133 0.0002 ( 1.9%) (A) DominanceInfo
1134 0.0084 (100.0%) Total
1137 ##### Tree Display Mode
1139 In this mode, the results are displayed in a nested pipeline view that mirrors
1140 the internal pass pipeline that is being executed in the pass manager. This view
1141 is useful for understanding specifically which parts of the pipeline are taking
1142 the most time, and can also be used to identify when analyses are being
1143 invalidated and recomputed. This is the default display mode.
1146 $ mlir-opt foo.mlir -mlir-disable-threading -pass-pipeline='builtin.module(func.func(cse,canonicalize),convert-func-to-llvm)' -mlir-timing
1148 ===-------------------------------------------------------------------------===
1149 ... Pass execution timing report ...
1150 ===-------------------------------------------------------------------------===
1151 Total Execution Time: 0.0249 seconds
1153 ---Wall Time--- --- Name ---
1154 0.0058 ( 70.8%) 'func.func' Pipeline
1156 0.0002 ( 2.6%) (A) DominanceInfo
1157 0.0004 ( 4.8%) VerifierPass
1158 0.0046 ( 55.4%) Canonicalizer
1159 0.0005 ( 6.2%) VerifierPass
1160 0.0005 ( 5.8%) VerifierPass
1161 0.0014 ( 17.2%) LLVMLoweringPass
1162 0.0005 ( 6.2%) VerifierPass
1163 0.0082 (100.0%) Total
1166 ##### Multi-threaded Pass Timing
1168 When multi-threading is enabled in the pass manager the meaning of the display
1169 slightly changes. First, a new timing column is added, `User Time`, that
1170 displays the total time spent across all threads. Secondly, the `Wall Time`
1171 column displays the longest individual time spent amongst all of the threads.
1172 This means that the `Wall Time` column will continue to give an indicator on the
1173 perceived time, or clock time, whereas the `User Time` will display the total
1177 $ mlir-opt foo.mlir -pass-pipeline='builtin.module(func.func(cse,canonicalize),convert-func-to-llvm)' -mlir-timing
1179 ===-------------------------------------------------------------------------===
1180 ... Pass execution timing report ...
1181 ===-------------------------------------------------------------------------===
1182 Total Execution Time: 0.0078 seconds
1184 ---User Time--- ---Wall Time--- --- Name ---
1185 0.0177 ( 88.5%) 0.0057 ( 71.3%) 'func.func' Pipeline
1186 0.0044 ( 22.0%) 0.0015 ( 18.9%) CSE
1187 0.0029 ( 14.5%) 0.0012 ( 15.2%) (A) DominanceInfo
1188 0.0038 ( 18.9%) 0.0015 ( 18.7%) VerifierPass
1189 0.0089 ( 44.6%) 0.0025 ( 31.1%) Canonicalizer
1190 0.0006 ( 3.0%) 0.0002 ( 2.6%) VerifierPass
1191 0.0004 ( 2.2%) 0.0004 ( 5.4%) VerifierPass
1192 0.0013 ( 6.5%) 0.0013 ( 16.3%) LLVMLoweringPass
1193 0.0006 ( 2.8%) 0.0006 ( 7.0%) VerifierPass
1194 0.0200 (100.0%) 0.0081 (100.0%) Total
1199 When debugging it is often useful to dump the IR at various stages of a pass
1200 pipeline. This is where the IR printing instrumentation comes into play. This
1201 instrumentation allows for conditionally printing the IR before and after pass
1202 execution by optionally filtering on the pass being executed. This
1203 instrumentation can be added directly to the PassManager via the
1204 `enableIRPrinting` method. `mlir-opt` provides a few useful flags for utilizing
1205 this instrumentation:
1207 * `mlir-print-ir-before=(comma-separated-pass-list)`
1208 * Print the IR before each of the passes provided within the pass list.
1209 * `mlir-print-ir-before-all`
1210 * Print the IR before every pass in the pipeline.
1213 $ mlir-opt foo.mlir -pass-pipeline='func.func(cse)' -mlir-print-ir-before=cse
1215 *** IR Dump Before CSE ***
1216 func.func @simple_constant() -> (i32, i32) {
1217 %c1_i32 = arith.constant 1 : i32
1218 %c1_i32_0 = arith.constant 1 : i32
1219 return %c1_i32, %c1_i32_0 : i32, i32
1223 * `mlir-print-ir-after=(comma-separated-pass-list)`
1224 * Print the IR after each of the passes provided within the pass list.
1225 * `mlir-print-ir-after-all`
1226 * Print the IR after every pass in the pipeline.
1229 $ mlir-opt foo.mlir -pass-pipeline='func.func(cse)' -mlir-print-ir-after=cse
1231 *** IR Dump After CSE ***
1232 func.func @simple_constant() -> (i32, i32) {
1233 %c1_i32 = arith.constant 1 : i32
1234 return %c1_i32, %c1_i32 : i32, i32
1238 * `mlir-print-ir-after-change`
1239 * Only print the IR after a pass if the pass mutated the IR. This helps to
1240 reduce the number of IR dumps for "uninteresting" passes.
1241 * Note: Changes are detected by comparing a hash of the operation before
1242 and after the pass. This adds additional run-time to compute the hash of
1243 the IR, and in some rare cases may result in false-positives depending
1244 on the collision rate of the hash algorithm used.
1245 * Note: This option should be used in unison with one of the other
1246 'mlir-print-ir-after' options above, as this option alone does not enable
1250 $ mlir-opt foo.mlir -pass-pipeline='func.func(cse,cse)' -mlir-print-ir-after=cse -mlir-print-ir-after-change
1252 *** IR Dump After CSE ***
1253 func.func @simple_constant() -> (i32, i32) {
1254 %c1_i32 = arith.constant 1 : i32
1255 return %c1_i32, %c1_i32 : i32, i32
1259 * `mlir-print-ir-after-failure`
1260 * Only print IR after a pass failure.
1261 * This option should *not* be used with the other `mlir-print-ir-after` flags
1265 $ mlir-opt foo.mlir -pass-pipeline='func.func(cse,bad-pass)' -mlir-print-ir-after-failure
1267 *** IR Dump After BadPass Failed ***
1268 func.func @simple_constant() -> (i32, i32) {
1269 %c1_i32 = arith.constant 1 : i32
1270 return %c1_i32, %c1_i32 : i32, i32
1274 * `mlir-print-ir-module-scope`
1275 * Always print the top-level module operation, regardless of pass type or
1276 operation nesting level.
1277 * Note: Printing at module scope should only be used when multi-threading
1278 is disabled(`-mlir-disable-threading`)
1281 $ mlir-opt foo.mlir -mlir-disable-threading -pass-pipeline='func.func(cse)' -mlir-print-ir-after=cse -mlir-print-ir-module-scope
1283 *** IR Dump After CSE *** ('func.func' operation: @bar)
1284 func.func @bar(%arg0: f32, %arg1: f32) -> f32 {
1288 func.func @simple_constant() -> (i32, i32) {
1289 %c1_i32 = arith.constant 1 : i32
1290 %c1_i32_0 = arith.constant 1 : i32
1291 return %c1_i32, %c1_i32_0 : i32, i32
1294 *** IR Dump After CSE *** ('func.func' operation: @simple_constant)
1295 func.func @bar(%arg0: f32, %arg1: f32) -> f32 {
1299 func.func @simple_constant() -> (i32, i32) {
1300 %c1_i32 = arith.constant 1 : i32
1301 return %c1_i32, %c1_i32 : i32, i32
1305 ## Crash and Failure Reproduction
1307 The [pass manager](#pass-manager) in MLIR contains a builtin mechanism to
1308 generate reproducibles in the event of a crash, or a
1309 [pass failure](#pass-failure). This functionality can be enabled via
1310 `PassManager::enableCrashReproducerGeneration` or via the command line flag
1311 `mlir-pass-pipeline-crash-reproducer`. In either case, an argument is provided that
1312 corresponds to the output `.mlir` file name that the reproducible should be
1313 written to. The reproducible contains the configuration of the pass manager that
1314 was executing, as well as the initial IR before any passes were run. The reproducer
1315 is stored within the assembly format as an external resource. A potential reproducible
1326 external_resources: {
1328 pipeline: "builtin.module(func.func(cse,canonicalize),inline)",
1329 disable_threading: true,
1336 The configuration dumped can be passed to `mlir-opt` by specifying
1337 `-run-reproducer` flag. This will result in parsing the configuration of the reproducer
1338 and adjusting the necessary opt state, e.g. configuring the pass manager, context, etc.
1340 Beyond specifying a filename, one can also register a `ReproducerStreamFactory`
1341 function that would be invoked in the case of a crash and the reproducer written
1344 ### Local Reproducer Generation
1346 An additional flag may be passed to
1347 `PassManager::enableCrashReproducerGeneration`, and specified via
1348 `mlir-pass-pipeline-local-reproducer` on the command line, that signals that the pass
1349 manager should attempt to generate a "local" reproducer. This will attempt to
1350 generate a reproducer containing IR right before the pass that fails. This is
1351 useful for situations where the crash is known to be within a specific pass, or
1352 when the original input relies on components (like dialects or passes) that may
1353 not always be available.
1355 Note: Local reproducer generation requires that multi-threading is
1356 disabled(`-mlir-disable-threading`)
1358 For example, if the failure in the previous example came from the `canonicalize` pass,
1359 the following reproducer would be generated:
1369 external_resources: {
1371 pipeline: "builtin.module(func.func(canonicalize))",
1372 disable_threading: true,