Skip to content

Commit f4e7bbe

Browse files
committed
add something for mir opt
1 parent 9106ae1 commit f4e7bbe

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

src/mir/optimizations.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,45 @@
11
# 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 make the final
5+
generated executable code better, and second, it means that LLVM has less work
6+
to do, so compilation is faster.
7+
8+
[mir]: https://rust-lang.github.io/rustc-guide/mir/index.html
9+
10+
MIR optimizations run after borrow checking. We run a series of optimization
11+
passes over the MIR to improve it. Some passes are required to run on all code,
12+
some passes don't actually do optimizations but only check stuff, and some
13+
passes are only turned on in `release` mode.
14+
15+
The [`optimized_mir`][optmir] [query] is called to produce the optimized MIR
16+
for a given [`DefId`][defid]. This query makes sure that the borrow checker has
17+
run and that some validation has occured. Then, it [steals][steal] the MIR
18+
optimizes it, and returns the improved MIR.
19+
20+
[optmir]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_mir/transform/fn.optimized_mir.html
21+
[query]: https://rust-lang.github.io/rustc-guide/query.html
22+
[defid]: https://rust-lang.github.io/rustc-guide/appendix/glossary.html?highlight=DefId#appendix-c-glossary
23+
[steal]: https://rust-lang.github.io/rustc-guide/mir/passes.html?highlight=steal#stealing
24+
25+
## Defining optimization passes
26+
27+
The list of passes run and the order in which they are run is defined by the
28+
[`run_optimization_passes`][rop] function. It contains an array of passes to
29+
run. Each pass in the array is a struct that implements the [`MirPass`] trait.
30+
The array is an array of `&dyn MirPass` trait objects. Typically, a pass is
31+
implemented in its own submodule of the [`rustc_mir::transform`][trans] module.
32+
33+
[rop]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_mir/transform/fn.run_optimization_passes.html
34+
[`MirPass`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_mir/transform/trait.MirPass.html
35+
[trans]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_mir/transform/index.html
36+
37+
Some examples of passes are:
38+
- `CleanupNonCodegenStatements`: remove some of the info that is only need for
39+
analyses, rather than codegen.
40+
- `ConstProp`: Does [constant propagation][constprop]
41+
42+
You can see the ["Implementors" section of the `MirPass` rustdocs][impl] for more examples.
43+
44+
[impl]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_mir/transform/trait.MirPass.html#implementors
45+
[constprop]: https://en.wikipedia.org/wiki/Constant_folding#Constant_propagation

0 commit comments

Comments
 (0)