Skip to content

Commit 5121d24

Browse files
committed
Add quickstart for adding a new optimization
1 parent 9a676ee commit 5121d24

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

src/mir/optimizations.md

+42
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,48 @@ 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.
42+
43+
4. Implement a new optimization in `compiler/rustc_mir/transform`. The fastest
44+
and easiest way to do this is to
45+
46+
1. pick a small optimization (such as [this one][no_landing_pads]) and copy it
47+
to a new file,
48+
2. add your optimization to one of the lists in the
49+
[`run_optimization_passes()`] function,
50+
3. and then start modifying the copied optimization.
51+
52+
5. Rerun `./x.py test --bless src/test/mir-opt/<your-test>.rs` to regenerate the
53+
MIR dumps. Look at the diffs to see if they are what you expect.
54+
55+
6. Run `./x.py test src/test/ui` to see if your optimization broke anything.
56+
57+
7. If there are issues with your optimization, experiment with it a bit and
58+
repeat steps 5 and 6.
59+
60+
8. Commit and open a PR. You can do this at any point, even if things aren't
61+
working yet, so that you can ask for feedback on the PR. Open a "WIP" PR
62+
(just prefix your PR title with `[WIP]` or otherwise note that it is a
63+
work in progress) in that case.
64+
65+
If you have any questions along the way, feel free to ask in `#t-compiler/help`
66+
or `#t-compiler/wg-mir-opt` on Zulip.
67+
68+
[mir-opt-test-readme]: https://github.com/rust-lang/rust/blob/master/src/test/mir-opt/README.md
69+
[`run_optimization_passes()`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_mir/transform/fn.run_optimization_passes.html
70+
2971
## Defining optimization passes
3072

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

0 commit comments

Comments
 (0)