[WIP] Re-enable the early otherwise branch optimization
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
Some changes occurred to MIR optimizations
cc @rust-lang/wg-mir-opt
r? mir-opt
:umbrella: The latest upstream changes (presumably #121370) made this pull request unmergeable. Please resolve the merge conflicts.
@bors try @rust-timer queue
Awaiting bors try build completion.
@rustbot label: +S-waiting-on-perf
:hourglass: Trying commit d5c6f730ebb6284aafbf63a215a86ea9493bb907 with merge 7a8b62c30d4f0ad6b88fa2bffff86fa70e668cd4...
:sunny: Try build successful - checks-actions
Build commit: 7a8b62c30d4f0ad6b88fa2bffff86fa70e668cd4 (7a8b62c30d4f0ad6b88fa2bffff86fa70e668cd4)
Queued 7a8b62c30d4f0ad6b88fa2bffff86fa70e668cd4 with parent d3d145ea1cae47ad392173f890577788117da3d9, future comparison URL. There is currently 1 preceding artifact in the queue. It will probably take at least ~1.5 hours until the benchmark run finishes.
Finished benchmarking commit (7a8b62c30d4f0ad6b88fa2bffff86fa70e668cd4): comparison URL.
Overall result: ❌✅ regressions and improvements - ACTION NEEDED
Benchmarking 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 @rustbot label: +perf-regression-triaged along with sufficient written justification. If you cannot justify the regressions please fix the regressions and do another perf run. If the next run shows neutral or positive results, the label will be automatically removed.
@bors rollup=never @rustbot label: -S-waiting-on-perf +perf-regression
Instruction count
This is a highly reliable metric that was used to determine the overall result at the top of this comment.
| mean | range | count | |
|---|---|---|---|
| Regressions ❌ (primary) |
1.5% | [0.3%, 2.6%] | 2 |
| Regressions ❌ (secondary) |
1.2% | [0.9%, 2.6%] | 7 |
| Improvements ✅ (primary) |
- | - | 0 |
| Improvements ✅ (secondary) |
-1.7% | [-2.0%, -1.4%] | 2 |
| All ❌✅ (primary) | 1.5% | [0.3%, 2.6%] | 2 |
Max RSS (memory usage)
Results
This 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.
| mean | range | count | |
|---|---|---|---|
| Regressions ❌ (primary) |
3.8% | [3.8%, 3.8%] | 1 |
| Regressions ❌ (secondary) |
- | - | 0 |
| Improvements ✅ (primary) |
-0.0% | [-0.0%, -0.0%] | 1 |
| Improvements ✅ (secondary) |
- | - | 0 |
| All ❌✅ (primary) | 1.9% | [-0.0%, 3.8%] | 2 |
Cycles
Results
This 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.
| mean | range | count | |
|---|---|---|---|
| Regressions ❌ (primary) |
2.5% | [2.5%, 2.5%] | 1 |
| Regressions ❌ (secondary) |
3.2% | [3.2%, 3.2%] | 1 |
| Improvements ✅ (primary) |
- | - | 0 |
| Improvements ✅ (secondary) |
- | - | 0 |
| All ❌✅ (primary) | 2.5% | [2.5%, 2.5%] | 1 |
Binary size
Results
This 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.
| mean | range | count | |
|---|---|---|---|
| Regressions ❌ (primary) |
0.3% | [0.3%, 0.3%] | 1 |
| Regressions ❌ (secondary) |
- | - | 0 |
| Improvements ✅ (primary) |
- | - | 0 |
| Improvements ✅ (secondary) |
- | - | 0 |
| All ❌✅ (primary) | 0.3% | [0.3%, 0.3%] | 1 |
Bootstrap: 650.923s -> 650.175s (-0.11%) Artifact size: 311.19 MiB -> 311.09 MiB (-0.03%)
@bors try @rust-timer queue
Awaiting bors try build completion.
@rustbot label: +S-waiting-on-perf
:hourglass: Trying commit 73ef0c07156f39adf8d01eb1ae60a96ec547f866 with merge a4a739ad3555a822cf38daebb07f963e3cf8a3c2...
:sunny: Try build successful - checks-actions
Build commit: a4a739ad3555a822cf38daebb07f963e3cf8a3c2 (a4a739ad3555a822cf38daebb07f963e3cf8a3c2)
Queued a4a739ad3555a822cf38daebb07f963e3cf8a3c2 with parent 279c9ba260389ac92c4827a721d3163c4e519f66, future comparison URL. There is currently 1 preceding artifact in the queue. It will probably take at least ~1.3 hours until the benchmark run finishes.
Finished benchmarking commit (a4a739ad3555a822cf38daebb07f963e3cf8a3c2): comparison URL.
Overall result: ❌ regressions - no action needed
Benchmarking 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 @rustbot label: -S-waiting-on-perf -perf-regression
Instruction count
This is a highly reliable metric that was used to determine the overall result at the top of this comment.
| mean | range | count | |
|---|---|---|---|
| Regressions ❌ (primary) |
2.7% | [2.7%, 2.7%] | 1 |
| Regressions ❌ (secondary) |
- | - | 0 |
| Improvements ✅ (primary) |
- | - | 0 |
| Improvements ✅ (secondary) |
- | - | 0 |
| All ❌✅ (primary) | 2.7% | [2.7%, 2.7%] | 1 |
Max RSS (memory usage)
Results
This 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.
| mean | range | count | |
|---|---|---|---|
| Regressions ❌ (primary) |
- | - | 0 |
| Regressions ❌ (secondary) |
- | - | 0 |
| Improvements ✅ (primary) |
-2.1% | [-2.6%, -1.6%] | 2 |
| Improvements ✅ (secondary) |
-2.7% | [-4.4%, -2.0%] | 7 |
| All ❌✅ (primary) | -2.1% | [-2.6%, -1.6%] | 2 |
Cycles
Results
This 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.
| mean | range | count | |
|---|---|---|---|
| Regressions ❌ (primary) |
2.7% | [2.7%, 2.7%] | 1 |
| Regressions ❌ (secondary) |
- | - | 0 |
| Improvements ✅ (primary) |
- | - | 0 |
| Improvements ✅ (secondary) |
- | - | 0 |
| All ❌✅ (primary) | 2.7% | [2.7%, 2.7%] | 1 |
Binary size
Results
This 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.
| mean | range | count | |
|---|---|---|---|
| Regressions ❌ (primary) |
- | - | 0 |
| Regressions ❌ (secondary) |
- | - | 0 |
| Improvements ✅ (primary) |
-0.1% | [-0.1%, -0.1%] | 1 |
| Improvements ✅ (secondary) |
- | - | 0 |
| All ❌✅ (primary) | -0.1% | [-0.1%, -0.1%] | 1 |
Bootstrap: 649.358s -> 650.059s (0.11%) Artifact size: 175.04 MiB -> 175.07 MiB (0.02%)
r? cjgillot
Could not assign reviewer from: cjgillot.
User(s) cjgillot are either the PR author, already assigned, or on vacation, and there are no other candidates.
Use r? to specify someone else to assign.
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.
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:
Producing an invalid value, even in private fields and locals. "Producing" a value happens any time a value is assigned to or read from a place, passed to a function/primitive operation or returned from a function/primitive operation. The following values are invalid (at their respective type):
- [...]
- A discriminant in an enum not included in the type definition.
The unsoundness described in #95162 occurs for exactly this reason--the old implementation would hoist the discriminant(Q) operation, causing it to execute on code paths where it wouldn't before, introducing new instances of this kind of UB. If the discriminant(Q) operation merely returned something poison-like, instead of invoking UB when you pass an invalid value to it, that wouldn't be an issue.
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.
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):
- Fix the unsound issue and re-enable this pass.
- Add scalar support.
- Add pattern with the same target value.
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.
I want to leave a FIXME and re-enable it. Rather than disable it and making it difficult to iterate.
Very interesting, I broke this PR with #120268. Since we haven't enabled this pass yet, I don't think this is a regression.
See https://play.rust-lang.org/?version=nightly&mode=release&edition=2021&gist=7606620b8e2ab84fe0c6e06b4ec3e7c4.
We change switchInt(move _3) -> [0: bb5, otherwise: bb1] to switchInt(move _3) -> [0: bb5, 1: bb1, otherwise: bb7];. This is not an if else statement now.
Sometimes we fail to make better with better facts. :( But I can fix it and let me refine this pass step by step. :3
#122387 is the first PR.
Closing this as it's being split into multiple prs with #122387 being the first part
Part 2: https://github.com/rust-lang/rust/pull/129047.