From ccfa589183770311048388378517b869b0086add Mon Sep 17 00:00:00 2001 From: Camelid Date: Fri, 26 Mar 2021 18:03:37 -0700 Subject: [PATCH 1/4] Add quickstart for adding a new optimization --- src/mir/optimizations.md | 44 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/src/mir/optimizations.md b/src/mir/optimizations.md index 080c8fd3f..d1be44cb3 100644 --- a/src/mir/optimizations.md +++ b/src/mir/optimizations.md @@ -26,6 +26,50 @@ optimizes it, and returns the improved MIR. [defid]: https://rustc-dev-guide.rust-lang.org/appendix/glossary.html#def-id [steal]: https://rustc-dev-guide.rust-lang.org/mir/passes.html?highlight=steal#stealing +## Quickstart for adding a new optimization + +1. Make a Rust source file in `src/test/mir-opt` that shows the code you want to + optimize. This should be kept simple, so avoid `println!` or other formatting + code if it's not necessary for the optimization. The reason for this is that + `println!`, `format!`, etc. generate a lot of MIR that can make it harder to + understand what the optimization does to the test. + +2. Run `./x.py test --bless src/test/mir-opt/.rs` to generate a MIR + dump. Read [this README][mir-opt-test-readme] for instructions on how to dump + things. + +3. Commit the current working directory state. + +4. Implement a new optimization in [`compiler/rustc_mir/src/transform`]. + The fastest and easiest way to do this is to + + 1. pick a small optimization (such as [`no_landing_pads`]) and copy it + to a new file, + 2. add your optimization to one of the lists in the + [`run_optimization_passes()`] function, + 3. and then start modifying the copied optimization. + +5. Rerun `./x.py test --bless src/test/mir-opt/.rs` to regenerate the + MIR dumps. Look at the diffs to see if they are what you expect. + +6. Run `./x.py test src/test/ui` to see if your optimization broke anything. + +7. If there are issues with your optimization, experiment with it a bit and + repeat steps 5 and 6. + +8. Commit and open a PR. You can do this at any point, even if things aren't + working yet, so that you can ask for feedback on the PR. Open a "WIP" PR + (just prefix your PR title with `[WIP]` or otherwise note that it is a + work in progress) in that case. + +If you have any questions along the way, feel free to ask in `#t-compiler/help` +or `#t-compiler/wg-mir-opt` on Zulip. + +[mir-opt-test-readme]: https://github.com/rust-lang/rust/blob/master/src/test/mir-opt/README.md +[`compiler/rustc_mir/src/transform`]: https://github.com/rust-lang/rust/tree/master/compiler/rustc_mir/src/transform +[`no_landing_pads`]: https://github.com/rust-lang/rust/blob/master/compiler/rustc_mir/src/transform/no_landing_pads.rs +[`run_optimization_passes()`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_mir/transform/fn.run_optimization_passes.html + ## Defining optimization passes The list of passes run and the order in which they are run is defined by the From 6d52d04b89fa64bb28acda6ecbf3cb238fbf22d2 Mon Sep 17 00:00:00 2001 From: Camelid Date: Tue, 30 Mar 2021 13:18:33 -0700 Subject: [PATCH 2/4] Only point people to `#t-compiler/wg-mir-opt` --- src/mir/optimizations.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/mir/optimizations.md b/src/mir/optimizations.md index d1be44cb3..2ec37d463 100644 --- a/src/mir/optimizations.md +++ b/src/mir/optimizations.md @@ -62,8 +62,8 @@ optimizes it, and returns the improved MIR. (just prefix your PR title with `[WIP]` or otherwise note that it is a work in progress) in that case. -If you have any questions along the way, feel free to ask in `#t-compiler/help` -or `#t-compiler/wg-mir-opt` on Zulip. +If you have any questions along the way, feel free to ask in +`#t-compiler/wg-mir-opt` on Zulip. [mir-opt-test-readme]: https://github.com/rust-lang/rust/blob/master/src/test/mir-opt/README.md [`compiler/rustc_mir/src/transform`]: https://github.com/rust-lang/rust/tree/master/compiler/rustc_mir/src/transform From 34a1f1d088e328f1bb200bd468b05fc53f96fa8e Mon Sep 17 00:00:00 2001 From: Camelid Date: Tue, 30 Mar 2021 13:20:53 -0700 Subject: [PATCH 3/4] Note that blessed test output should be included in PRs --- src/mir/optimizations.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/mir/optimizations.md b/src/mir/optimizations.md index 2ec37d463..b9958f31b 100644 --- a/src/mir/optimizations.md +++ b/src/mir/optimizations.md @@ -62,6 +62,9 @@ optimizes it, and returns the improved MIR. (just prefix your PR title with `[WIP]` or otherwise note that it is a work in progress) in that case. + Make sure to commit the blessed test output as well! It's necessary for CI to + pass and it's very helpful to reviewers. + If you have any questions along the way, feel free to ask in `#t-compiler/wg-mir-opt` on Zulip. From e92be9059300aae2fe89776e7f023aeb7a85155b Mon Sep 17 00:00:00 2001 From: Camelid Date: Thu, 1 Apr 2021 10:05:52 -0700 Subject: [PATCH 4/4] Add reason for step 3 --- src/mir/optimizations.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/mir/optimizations.md b/src/mir/optimizations.md index b9958f31b..50e06960e 100644 --- a/src/mir/optimizations.md +++ b/src/mir/optimizations.md @@ -38,7 +38,9 @@ optimizes it, and returns the improved MIR. dump. Read [this README][mir-opt-test-readme] for instructions on how to dump things. -3. Commit the current working directory state. +3. Commit the current working directory state. The reason you should commit the + test output before you implement the optimization is so that you (and your + reviewers) can see a before/after diff of what the optimization changed. 4. Implement a new optimization in [`compiler/rustc_mir/src/transform`]. The fastest and easiest way to do this is to