Skip to content

Commit 32c6d27

Browse files
authored
Add quickstart for adding a new optimization (#1094)
1 parent dda441c commit 32c6d27

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

src/mir/optimizations.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,55 @@ optimizes it, and returns the improved MIR.
2626
[defid]: https://rustc-dev-guide.rust-lang.org/appendix/glossary.html#def-id
2727
[steal]: https://rustc-dev-guide.rust-lang.org/mir/passes.html?highlight=steal#stealing
2828

29+
## Quickstart for adding a new optimization
30+
31+
1. Make a Rust source file in `src/test/mir-opt` that shows the code you want to
32+
optimize. This should be kept simple, so avoid `println!` or other formatting
33+
code if it's not necessary for the optimization. The reason for this is that
34+
`println!`, `format!`, etc. generate a lot of MIR that can make it harder to
35+
understand what the optimization does to the test.
36+
37+
2. Run `./x.py test --bless src/test/mir-opt/<your-test>.rs` to generate a MIR
38+
dump. Read [this README][mir-opt-test-readme] for instructions on how to dump
39+
things.
40+
41+
3. Commit the current working directory state. The reason you should commit the
42+
test output before you implement the optimization is so that you (and your
43+
reviewers) can see a before/after diff of what the optimization changed.
44+
45+
4. Implement a new optimization in [`compiler/rustc_mir/src/transform`].
46+
The fastest and easiest way to do this is to
47+
48+
1. pick a small optimization (such as [`no_landing_pads`]) and copy it
49+
to a new file,
50+
2. add your optimization to one of the lists in the
51+
[`run_optimization_passes()`] function,
52+
3. and then start modifying the copied optimization.
53+
54+
5. Rerun `./x.py test --bless src/test/mir-opt/<your-test>.rs` to regenerate the
55+
MIR dumps. Look at the diffs to see if they are what you expect.
56+
57+
6. Run `./x.py test src/test/ui` to see if your optimization broke anything.
58+
59+
7. If there are issues with your optimization, experiment with it a bit and
60+
repeat steps 5 and 6.
61+
62+
8. Commit and open a PR. You can do this at any point, even if things aren't
63+
working yet, so that you can ask for feedback on the PR. Open a "WIP" PR
64+
(just prefix your PR title with `[WIP]` or otherwise note that it is a
65+
work in progress) in that case.
66+
67+
Make sure to commit the blessed test output as well! It's necessary for CI to
68+
pass and it's very helpful to reviewers.
69+
70+
If you have any questions along the way, feel free to ask in
71+
`#t-compiler/wg-mir-opt` on Zulip.
72+
73+
[mir-opt-test-readme]: https://github.com/rust-lang/rust/blob/master/src/test/mir-opt/README.md
74+
[`compiler/rustc_mir/src/transform`]: https://github.com/rust-lang/rust/tree/master/compiler/rustc_mir/src/transform
75+
[`no_landing_pads`]: https://github.com/rust-lang/rust/blob/master/compiler/rustc_mir/src/transform/no_landing_pads.rs
76+
[`run_optimization_passes()`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_mir/transform/fn.run_optimization_passes.html
77+
2978
## Defining optimization passes
3079

3180
The list of passes run and the order in which they are run is defined by the

0 commit comments

Comments
 (0)