-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
[WIP] Re-enable the early otherwise branch optimization #121397
Conversation
Some changes occurred to MIR optimizations cc @rust-lang/wg-mir-opt |
r? mir-opt |
☔ The latest upstream changes (presumably #121370) made this pull request unmergeable. Please resolve the merge conflicts. |
4613221
to
f65946e
Compare
@bors try @rust-timer queue |
This comment has been minimized.
This comment has been minimized.
… r=<try> Re-enable the early otherwise branch optimization Fixes rust-lang#95162. Fixes rust-lang#119014. Fixes rust-lang#117970. An invalid enum discriminant can come from anywhere. We have to check to see if all successors contain the discriminant statement. It should not be UB that we pass in an invalid enum discriminant when calling a function, this is more like LLVM's poison value. UB only when used. Although miri would consider the following code to be UB. (It's fine for miri.) https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=18602870aaeb07cbdf7dfcd2c28961a2 I extended the scenario with scalars and the same target values. r? compiler
☀️ Try build successful - checks-actions |
This comment has been minimized.
This comment has been minimized.
Finished benchmarking commit (7a8b62c): comparison URL. Overall result: ❌✅ regressions and improvements - ACTION NEEDEDBenchmarking this pull request likely means that it is perf-sensitive, so we're automatically marking it as not fit for rolling up. While you can manually mark this PR as fit for rollup, we strongly recommend not doing so since this PR may lead to changes in compiler perf. Next Steps: If you can justify the regressions found in this try perf run, please indicate this with @bors rollup=never Instruction countThis is a highly reliable metric that was used to determine the overall result at the top of this comment.
Max RSS (memory usage)ResultsThis is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.
CyclesResultsThis is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.
Binary sizeResultsThis is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.
Bootstrap: 650.923s -> 650.175s (-0.11%) |
… target values are the same
d5c6f73
to
8d0f254
Compare
… r=<try> Re-enable the early otherwise branch optimization Fixes rust-lang#95162. Fixes rust-lang#119014. Fixes rust-lang#117970. An invalid enum discriminant can come from anywhere. We have to check to see if all successors contain the discriminant statement. It should not be UB that we pass in an invalid enum discriminant when calling a function, this is more like LLVM's poison value. UB only when used. Although miri would consider the following code to be UB. (It's fine for miri.) https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=18602870aaeb07cbdf7dfcd2c28961a2 I extended the scenario with scalars and the same target values. r? compiler
☀️ Try build successful - checks-actions |
This comment has been minimized.
This comment has been minimized.
Finished benchmarking commit (a4a739a): comparison URL. Overall result: ❌ regressions - no action neededBenchmarking this pull request likely means that it is perf-sensitive, so we're automatically marking it as not fit for rolling up. While you can manually mark this PR as fit for rollup, we strongly recommend not doing so since this PR may lead to changes in compiler perf. @bors rollup=never Instruction countThis is a highly reliable metric that was used to determine the overall result at the top of this comment.
Max RSS (memory usage)ResultsThis is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.
CyclesResultsThis is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.
Binary sizeResultsThis is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.
Bootstrap: 649.358s -> 650.059s (0.11%) |
r? cjgillot |
Could not assign reviewer from: |
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.
I have made a first pass over the implementation. This pass is very hard to review, so I may need to come back several times to it.
// invalid value, which is UB. | ||
// In order to fix this, we would either need to show that the discriminant computation of | ||
// `place` is computed in all branches. | ||
// So we need the `otherwise` branch has no statements and an unreachable terminator. |
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.
The opt would be correct if we compute the discriminant in the otherwise branch like all other ones, wouldn't it?
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.
Yes. But I think it could make this pass become complicated. So I decided to keep it simple first. This way, at least the pass can start to do work, and iterate over it in follow-on updates.
#[derive(Debug)] | ||
struct OptimizationData<'tcx> { | ||
destination: BasicBlock, | ||
child_place: Place<'tcx>, | ||
child_ty: Ty<'tcx>, | ||
child_source: SourceInfo, | ||
hoist_discriminant: bool, | ||
same_target_value: Option<u128>, |
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.
Could you add docs for those fields?
if child_targets.otherwise() != destination { | ||
return None; | ||
} | ||
// Make sure there are only two branches. |
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.
Should this comment be moved?
#[derive(Debug)] | ||
struct OptimizationData<'tcx> { | ||
destination: BasicBlock, | ||
child_place: Place<'tcx>, | ||
child_ty: Ty<'tcx>, | ||
child_source: SourceInfo, | ||
hoist_discriminant: bool, | ||
same_target_value: Option<u128>, | ||
} | ||
|
||
fn evaluate_candidate<'tcx>( |
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.
I have a lot of trouble understanding how this function is structured and what it evaluates exactly.
if destination != targets.otherwise() { | ||
return false; | ||
if hoist_discriminant { | ||
// ...assign the discriminant of `place` in that statement |
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.
Nit: the ...
pattern is lost since you moved code into evaluate_candidate
.
I don't think it's an issue for this PR's implementation (since it's conservative), but passing an invalid value to a function is UB. This is very explicit in the reference:
The unsoundness described in #95162 occurs for exactly this reason--the old implementation would hoist the |
Sorry for that, it's probably because I have new findings and update to this PR. I can split this up into multiple PRs. The current commit history should reflect that as well. Major changes (PRs that can be split):
|
Yes. I plan to submit a series of PRs with these reviews. |
@erikdesjardins Thanks for the explanation. I will document this in a new PR. |
Very interesting, I broke this PR with #120268. See https://play.rust-lang.org/?version=nightly&mode=release&edition=2021&gist=7606620b8e2ab84fe0c6e06b4ec3e7c4. Sometimes we fail to make better with better facts. :( |
#122387 is the first PR. |
…nch, r=<try> Re-enable the early otherwise branch optimization Closes rust-lang#95162. Fixes rust-lang#119014. This is the first part of rust-lang#121397. An invalid enum discriminant can come from anywhere. We have to check to see if all successors contain the discriminant statement. This should have a pass to hoist instructions. r? cjgillot
…nch, r=cjgillot Re-enable the early otherwise branch optimization Closes rust-lang#95162. Fixes rust-lang#119014. This is the first part of rust-lang#121397. An invalid enum discriminant can come from anywhere. We have to check to see if all successors contain the discriminant statement. This should have a pass to hoist instructions. r? cjgillot
…nch, r=cjgillot Re-enable the early otherwise branch optimization Closes rust-lang#95162. Fixes rust-lang#119014. This is the first part of rust-lang#121397. An invalid enum discriminant can come from anywhere. We have to check to see if all successors contain the discriminant statement. This should have a pass to hoist instructions. r? cjgillot
Closing this as it's being split into multiple prs with #122387 being the first part |
Part 2: #129047. |
Fixes #95162. Fixes #119014. Fixes #117970.
An invalid enum discriminant can come from anywhere. We have to check to see if all successors contain the discriminant statement.
It should not be UB that we pass in an invalid enum discriminant when calling a function, this is more like LLVM's poison value. UB only when used. Although miri would consider the following code to be UB. (It's fine for miri.)
https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=18602870aaeb07cbdf7dfcd2c28961a2
I extended the scenario with scalars and the same target values.
r? compiler