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

Deduplicate unreachable blocks, for real this time #110569

Merged
merged 2 commits into from
Apr 21, 2023

Conversation

saethlin
Copy link
Member

@saethlin saethlin commented Apr 20, 2023

In #106428 (in particular 41eda69) we noticed that inlining unreachable_unchecked can produce duplicate unreachable blocks. So we improved two MIR optimizations: SimplifyCfg was given a simplify to deduplicate unreachable blocks, then InstCombine was given a combiner to deduplicate switch targets that point at the same block. The problem is that change doesn't actually work.

Our current pass order is

SimplifyCfg (does nothing relevant to this situation)
Inline (produces multiple unreachable blocks)
InstCombine (doesn't do anything here, oops)
SimplifyCfg (produces the duplicate SwitchTargets that InstCombine is looking for)

So in here, I have factored out the specific function from InstCombine and placed it inside the simplify that produces the case it is looking for. This should ensure that it runs in the scenario it was designed for.

Fixes #110551
r? @cjgillot

@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 Apr 20, 2023
@rustbot
Copy link
Collaborator

rustbot commented Apr 20, 2023

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

@rust-log-analyzer

This comment has been minimized.

@@ -0,0 +1,16 @@
// compile-flags: -Zmir-opt-level=2 -Zinline-mir
// ignore-debug: standard library debug assertions add a panic that breaks this optimization
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blessing the ignore-debug tests requires to recompile everything, which is tedious.
Can you use the intrinsic directly and cook your own unreachable_unchecked?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm really wary of doing things like that. Modifying the mir-opt test for the convenience of testing is what caused this regression in the first place, and this test is already somewhat removed from the code that was reported to not optimize.

*target = self.duplicates[0];
}
}

if optimization_was_applied {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this heavy enough to warrant the short circuit?
Could this cause us to miss cases where combining would be useful, but the branches are already deduplicated?
match { A => {}, B | C => unreachable }

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or do you rely on the call in instcombine?

Copy link
Member Author

@saethlin saethlin Apr 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I wrote this I think I didn't realize how rare this code should be, since we already only get here this if there are multiple unreachable blocks in the Body. And I'm quite sure I do not want to be relying on InstCombine.

compiler/rustc_mir_transform/src/instcombine.rs Outdated Show resolved Hide resolved
@@ -0,0 +1,16 @@
// compile-flags: -Zmir-opt-level=2 -Zinline-mir
// ignore-debug: standard library debug assertions add a panic that breaks this optimization
#![crate_type = "lib"]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated: should we make mir-opt tests "lib" by default, to avoid to list all the cases in a fn main everywhere?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we can. The codegen tests could do with the same treatment; I pulled this style of testing from them. There's a #![crate_type = "lib"] in nearly every single codegen test file.

@saethlin
Copy link
Member Author

It seems prudent to
@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 Apr 21, 2023
@bors
Copy link
Contributor

bors commented Apr 21, 2023

⌛ Trying commit 8ec49ad with merge 31ffe61ee8b251efc5bd2f3561d22d594d5a0cab...

@bors
Copy link
Contributor

bors commented Apr 21, 2023

☀️ Try build successful - checks-actions
Build commit: 31ffe61ee8b251efc5bd2f3561d22d594d5a0cab (31ffe61ee8b251efc5bd2f3561d22d594d5a0cab)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (31ffe61ee8b251efc5bd2f3561d22d594d5a0cab): comparison URL.

Overall result: no relevant changes - 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 benchmark run did not return any relevant results for this metric.

Max RSS (memory usage)

This benchmark run did not return any relevant results for this metric.

Cycles

This benchmark run did not return any relevant results for this metric.

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Apr 21, 2023
@cjgillot
Copy link
Contributor

@bors r+

@bors
Copy link
Contributor

bors commented Apr 21, 2023

📌 Commit 8ec49ad has been approved by cjgillot

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 21, 2023
@bors
Copy link
Contributor

bors commented Apr 21, 2023

⌛ Testing commit 8ec49ad with merge 4a03f14...

@bors
Copy link
Contributor

bors commented Apr 21, 2023

☀️ Test successful - checks-actions
Approved by: cjgillot
Pushing 4a03f14 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Apr 21, 2023
@bors bors merged commit 4a03f14 into rust-lang:master Apr 21, 2023
@rustbot rustbot added this to the 1.71.0 milestone Apr 21, 2023
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (4a03f14): comparison URL.

Overall result: no relevant changes - no action needed

@rustbot label: -perf-regression

Instruction count

This benchmark run did not return any relevant results for this metric.

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)
3.5% [3.5%, 3.5%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-1.1% [-1.1%, -1.1%] 1
All ❌✅ (primary) - - 0

Cycles

This benchmark run did not return any relevant results for this metric.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Codegen regression involving assume/unreachable_unchecked
6 participants