-
Notifications
You must be signed in to change notification settings - Fork 12.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New MIR optimization pass to reduce branches on match of tuples of enums #75119
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @varkor (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
This looks great, but unfortunately I don't have time to review something of this size at the moment. r? @eddyb @bors try @rust-timer queue |
Awaiting bors try build completion |
⌛ Trying commit c6982e36a893aeb5647d1c7773bff287acc43a38 with merge e42444c82f1bdc51f3bd1abadcf3f7b2a9e87159... |
☀️ Try build successful - checks-actions, checks-azure |
Queued e42444c82f1bdc51f3bd1abadcf3f7b2a9e87159 with parent 829d69b, future comparison URL. |
Finished benchmarking try commit (e42444c82f1bdc51f3bd1abadcf3f7b2a9e87159): comparison url. Benchmarking this pull request likely means that it is perf-sensitive, so we're automatically marking it as not fit for rolling up. Please note that if the perf results are neutral, you should likely undo the rollup=never given below by specifying Importantly, though, if the results of this run are non-neutral do not roll this PR up -- it will mask other regressions or improvements in the roll up. @bors rollup=never |
The pass is gated on Mir opt level 3 right now, so I don't think a perf run makes sense without removing that check. @eddyb do you want me to remove it so we can check the impact of perf? |
c6982e3
to
8ef3624
Compare
I don't quite understand how to solve the tests failing.
Running bless locally does not change anything |
You likely need to edit the 32-bit test outputs ( |
You also have to add
|
☔ The latest upstream changes (presumably #73656) made this pull request unmergeable. Please resolve the merge conflicts. |
cc @rust-lang/wg-mir-opt |
61056c8
to
e083d97
Compare
Rebased on master and squashed |
r? @oli-obk |
+ StorageLive(_11); // scope 0 at $DIR/early_otherwise_branch.rs:7:10: 7:17 | ||
+ _11 = Ne(_10, _7); // scope 0 at $DIR/early_otherwise_branch.rs:7:10: 7:17 | ||
+ StorageDead(_10); // scope 0 at $DIR/early_otherwise_branch.rs:7:10: 7:17 | ||
+ switchInt(move _11) -> [false: bb6, otherwise: bb1]; // scope 0 at $DIR/early_otherwise_branch.rs:7:10: 7:17 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
instead of doing a switch and going to a block that does another switch, you can also compute another comparison:
_42 = Eq(_7, 1_isize)
_43 = BitAnd(_42, _11)
and then switch on _43
to directly go to bb3
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@oli-obk Hmm that works when we have only have 2 targets in the switch, but what if we have 3 targets in the switch like match (x,y,z)
? See the 3_element_tuple test. Correct me if I am wrong though
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That test makes bb2
dead code, and doesn't affect the match part on z
at all, so it all seems to work out to me? You aren't optimizing chains longer than 2 elements with your optimization, and even if the optimization is extended to support that, we'd just end up with more of these BitAnd
s I think.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's been a while, but I finally looked at it again. I don't think it is that easy to use BitAnd to avoid the extra bb. See https://github.com/rust-lang/rust/pull/75119/files#diff-37f80fe71480f2b22fb8fd1fc103fda4R88 where we need to check both 0 and 1.
☔ The latest upstream changes (presumably #75370) made this pull request unmergeable. Please resolve the merge conflicts. |
6e45568
to
048c233
Compare
048c233
to
13e8e20
Compare
@bors r+ |
📌 Commit 13e8e20df3307d8eaf8b6f86e73f313fcaab39b2 has been approved by |
@bors r- |
13e8e20
to
0363694
Compare
@bors r+ |
📌 Commit 0363694 has been approved by |
☀️ Test successful - checks-actions, checks-azure |
Hi! This PR showed up in the weekly perf triage report. It showed mixed results in instruction counts. While It's possible that there's some improvement in the generated code, although I'd be a bit surprised if LLVM couldn't manage this. If there's no runtime speedup, I think we should consider disabling this by default. |
Hi @ecstatic-morse |
It sounds like this might be a good fit for mir-opt-level=2 then as (eventually) we want to make that the default for optimized builds while mir-opt-level=1 will remain the default for non optimized builds. |
@wesleywiser @simonvandel Was this ever moved to |
Hi @ecstatic-morse, no this pass is still running at opt-level=1. Should I create a pr to gate it at 2? |
…ise-branch, r=wesleywiser Move `EarlyOtherwiseBranch` to mir-opt-level 2 cc rust-lang#75119 This didn't have an [effect in most cases](https://perf.rust-lang.org/compare.html?start=81e02708f1f4760244756548981277d5199baa9a&end=2e0edc0f28c5647141bedba02e7a222d3a5dc9c3&stat=instructions:u), and is not trivially sound. Let it bake at `mir-opt-level=2` for a while. Also, this missed the cutoff for beta, so we'll have to backport this. r? @wesleywiser
Fixes #68867 by adding a new pass that turns something like
into something like
The opt-diffs still have the old basic blocks like
These do get removed on later passes. I'm not sure if I should include those passes in the test to make it clear?