]> git.proxmox.com Git - rustc.git/blame - 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
CommitLineData
a1dfa0c6 1# MIR optimizations
60c5eb7d
XL
2
3MIR optimizations are optimizations run on the [MIR][mir] to produce better MIR
4before codegen. This is important for two reasons: first, it makes the final
5generated executable code better, and second, it means that LLVM has less work
6to do, so compilation is faster. Note that since MIR is generic (not
7[monomorphized][monomorph] yet), these optimizations are particularly
8effective; we can optimize the generic version, so all of the monomorphizations
9are 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
14MIR optimizations run after borrow checking. We run a series of optimization
15passes over the MIR to improve it. Some passes are required to run on all code,
16some passes don't actually do optimizations but only check stuff, and some
17passes are only turned on in `release` mode.
18
19The [`optimized_mir`][optmir] [query] is called to produce the optimized MIR
20for a given [`DefId`][defid]. This query makes sure that the borrow checker has
21run and that some validation has occurred. Then, it [steals][steal] the MIR
22optimizes 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
31The 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
33run. Each pass in the array is a struct that implements the [`MirPass`] trait.
34The array is an array of `&dyn MirPass` trait objects. Typically, a pass is
35implemented 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
41Some 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
46You 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