Skip to content
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

Closed
wants to merge 10 commits into from

Conversation

DianQK
Copy link
Member

@DianQK DianQK commented Feb 21, 2024

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

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Feb 21, 2024
@rustbot
Copy link
Collaborator

rustbot commented Feb 21, 2024

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

@michaelwoerister
Copy link
Member

r? mir-opt

@bors
Copy link
Contributor

bors commented Feb 22, 2024

☔ The latest upstream changes (presumably #121370) made this pull request unmergeable. Please resolve the merge conflicts.

@DianQK
Copy link
Member Author

DianQK commented Feb 29, 2024

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Feb 29, 2024
@bors
Copy link
Contributor

bors commented Feb 29, 2024

⌛ Trying commit d5c6f73 with merge 7a8b62c...

bors added a commit to rust-lang-ci/rust that referenced this pull request Feb 29, 2024
… 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
@bors
Copy link
Contributor

bors commented Feb 29, 2024

☀️ Try build successful - checks-actions
Build commit: 7a8b62c (7a8b62c30d4f0ad6b88fa2bffff86fa70e668cd4)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (7a8b62c): 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%)

@rustbot rustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Feb 29, 2024
@DianQK DianQK force-pushed the early_otherwise_branch_sound branch from d5c6f73 to 8d0f254 Compare March 3, 2024 13:53
@bors
Copy link
Contributor

bors commented Mar 3, 2024

⌛ Trying commit 73ef0c0 with merge a4a739a...

bors added a commit to rust-lang-ci/rust that referenced this pull request Mar 3, 2024
… 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
@bors
Copy link
Contributor

bors commented Mar 3, 2024

☀️ Try build successful - checks-actions
Build commit: a4a739a (a4a739ad3555a822cf38daebb07f963e3cf8a3c2)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (a4a739a): 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%)

@rustbot rustbot removed S-waiting-on-perf Status: Waiting on a perf run to be completed. perf-regression Performance regression. labels Mar 4, 2024
@DianQK
Copy link
Member Author

DianQK commented Mar 4, 2024

r? cjgillot

@rustbot
Copy link
Collaborator

rustbot commented Mar 4, 2024

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.

Copy link
Contributor

@cjgillot cjgillot left a 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.
Copy link
Contributor

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?

Copy link
Member Author

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>,
Copy link
Contributor

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.
Copy link
Contributor

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>(
Copy link
Contributor

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
Copy link
Contributor

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.

@cjgillot cjgillot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Mar 10, 2024
@erikdesjardins
Copy link
Contributor

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.

@DianQK
Copy link
Member Author

DianQK commented Mar 11, 2024

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.

@DianQK
Copy link
Member Author

DianQK commented Mar 11, 2024

Yes. I plan to submit a series of PRs with these reviews.

@DianQK DianQK marked this pull request as draft March 11, 2024 01:26
@DianQK
Copy link
Member Author

DianQK commented Mar 11, 2024

@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.

@DianQK
Copy link
Member Author

DianQK commented Mar 12, 2024

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

@DianQK DianQK changed the title Re-enable the early otherwise branch optimization [WIP] Re-enable the early otherwise branch optimization Mar 12, 2024
@DianQK
Copy link
Member Author

DianQK commented Mar 12, 2024

#122387 is the first PR.

bors added a commit to rust-lang-ci/rust that referenced this pull request Mar 27, 2024
…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
bors added a commit to rust-lang-ci/rust that referenced this pull request Apr 7, 2024
…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
bors added a commit to rust-lang-ci/rust that referenced this pull request Apr 9, 2024
…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
@Dylan-DPC
Copy link
Member

Closing this as it's being split into multiple prs with #122387 being the first part

@Dylan-DPC Dylan-DPC closed this Aug 8, 2024
@DianQK DianQK deleted the early_otherwise_branch_sound branch August 12, 2024 01:28
@DianQK
Copy link
Member Author

DianQK commented Aug 13, 2024

Part 2: #129047.

@apiraino apiraino removed the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Sep 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
10 participants