about summary refs log tree commit diff
path: root/src/doc/rustc-dev-guide
diff options
context:
space:
mode:
authorMark Mansi <markm@cs.wisc.edu>2019-11-12 10:25:55 -0600
committerWho? Me?! <mark-i-m@users.noreply.github.com>2019-11-14 13:08:20 -0600
commit7ecacb4af5e3de2386a97ad406ab98e73b8881b2 (patch)
tree322c32a5d64469947091d1b79f5d0a3fbcc9d710 /src/doc/rustc-dev-guide
parent99ba3e67d6a00b5e0945cf7f7e5fee5504ace6ea (diff)
downloadrust-7ecacb4af5e3de2386a97ad406ab98e73b8881b2.tar.gz
rust-7ecacb4af5e3de2386a97ad406ab98e73b8881b2.zip
add something for mir opt
Diffstat (limited to 'src/doc/rustc-dev-guide')
-rw-r--r--src/doc/rustc-dev-guide/src/mir/optimizations.md44
1 files changed, 44 insertions, 0 deletions
diff --git a/src/doc/rustc-dev-guide/src/mir/optimizations.md b/src/doc/rustc-dev-guide/src/mir/optimizations.md
index ddafa0c99c3..fb550af4cd7 100644
--- a/src/doc/rustc-dev-guide/src/mir/optimizations.md
+++ b/src/doc/rustc-dev-guide/src/mir/optimizations.md
@@ -1 +1,45 @@
 # MIR optimizations
+
+MIR optimizations are optimizations run on the [MIR][mir] to produce better MIR
+before codegen. This is important for two reasons: first, it make the final
+generated executable code better, and second, it means that LLVM has less work
+to do, so compilation is faster.
+
+[mir]: https://rust-lang.github.io/rustc-guide/mir/index.html
+
+MIR optimizations run after borrow checking. We run a series of optimization
+passes over the MIR to improve it. Some passes are required to run on all code,
+some passes don't actually do optimizations but only check stuff, and some
+passes are only turned on in `release` mode.
+
+The [`optimized_mir`][optmir] [query] is called to produce the optimized MIR
+for a given [`DefId`][defid]. This query makes sure that the borrow checker has
+run and that some validation has occured. Then, it [steals][steal] the MIR
+optimizes it, and returns the improved MIR.
+
+[optmir]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_mir/transform/fn.optimized_mir.html
+[query]: https://rust-lang.github.io/rustc-guide/query.html
+[defid]: https://rust-lang.github.io/rustc-guide/appendix/glossary.html?highlight=DefId#appendix-c-glossary
+[steal]: https://rust-lang.github.io/rustc-guide/mir/passes.html?highlight=steal#stealing
+
+## Defining optimization passes
+
+The list of passes run and the order in which they are run is defined by the
+[`run_optimization_passes`][rop] function. It contains an array of passes to
+run.  Each pass in the array is a struct that implements the [`MirPass`] trait.
+The array is an array of `&dyn MirPass` trait objects. Typically, a pass is
+implemented in its own submodule of the [`rustc_mir::transform`][trans] module.
+
+[rop]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_mir/transform/fn.run_optimization_passes.html
+[`MirPass`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_mir/transform/trait.MirPass.html
+[trans]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_mir/transform/index.html 
+
+Some examples of passes are:
+- `CleanupNonCodegenStatements`: remove some of the info that is only need for
+  analyses, rather than codegen.
+- `ConstProp`: Does [constant propagation][constprop]
+
+You can see the ["Implementors" section of the `MirPass` rustdocs][impl] for more examples.
+
+[impl]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_mir/transform/trait.MirPass.html#implementors
+[constprop]: https://en.wikipedia.org/wiki/Constant_folding#Constant_propagation