]> git.proxmox.com Git - rustc.git/blob - src/doc/rustc-guide/src/mir/optimizations.md
New upstream version 1.41.1+dfsg1
[rustc.git] / src / doc / rustc-guide / src / mir / optimizations.md
1 # MIR optimizations
2
3 MIR optimizations are optimizations run on the [MIR][mir] to produce better MIR
4 before codegen. This is important for two reasons: first, it makes the final
5 generated executable code better, and second, it means that LLVM has less work
6 to do, so compilation is faster. Note that since MIR is generic (not
7 [monomorphized][monomorph] yet), these optimizations are particularly
8 effective; we can optimize the generic version, so all of the monomorphizations
9 are cheaper!
10
11 [mir]: https://rust-lang.github.io/rustc-guide/mir/index.html
12 [monomorph]: https://rust-lang.github.io/rustc-guide/appendix/glossary.html?highlight=monomorphize#appendix-c-glossary
13
14 MIR optimizations run after borrow checking. We run a series of optimization
15 passes over the MIR to improve it. Some passes are required to run on all code,
16 some passes don't actually do optimizations but only check stuff, and some
17 passes are only turned on in `release` mode.
18
19 The [`optimized_mir`][optmir] [query] is called to produce the optimized MIR
20 for a given [`DefId`][defid]. This query makes sure that the borrow checker has
21 run and that some validation has occurred. Then, it [steals][steal] the MIR
22 optimizes it, and returns the improved MIR.
23
24 [optmir]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_mir/transform/fn.optimized_mir.html
25 [query]: https://rust-lang.github.io/rustc-guide/query.html
26 [defid]: https://rust-lang.github.io/rustc-guide/appendix/glossary.html?highlight=DefId#appendix-c-glossary
27 [steal]: https://rust-lang.github.io/rustc-guide/mir/passes.html?highlight=steal#stealing
28
29 ## Defining optimization passes
30
31 The list of passes run and the order in which they are run is defined by the
32 [`run_optimization_passes`][rop] function. It contains an array of passes to
33 run. Each pass in the array is a struct that implements the [`MirPass`] trait.
34 The array is an array of `&dyn MirPass` trait objects. Typically, a pass is
35 implemented in its own submodule of the [`rustc_mir::transform`][trans] module.
36
37 [rop]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_mir/transform/fn.run_optimization_passes.html
38 [`MirPass`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_mir/transform/trait.MirPass.html
39 [trans]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_mir/transform/index.html
40
41 Some examples of passes are:
42 - `CleanupNonCodegenStatements`: remove some of the info that is only needed for
43 analyses, rather than codegen.
44 - `ConstProp`: Does [constant propagation][constprop]
45
46 You can see the ["Implementors" section of the `MirPass` rustdocs][impl] for more examples.
47
48 [impl]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_mir/transform/trait.MirPass.html#implementors
49 [constprop]: https://en.wikipedia.org/wiki/Constant_folding#Constant_propagation