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

Implement MIR opt unit tests #96090

Merged
merged 3 commits into from
Apr 25, 2022
Merged

Implement MIR opt unit tests #96090

merged 3 commits into from
Apr 25, 2022

Conversation

JakobDegen
Copy link
Contributor

This implements rust-lang/compiler-team#502 .

There's not much to say here, this implementation does everything as proposed. I also added the flag to a bunch of existing tests (mostly those to which I could add it without causing huge diffs due to changes in line numbers). Summarizing the changes to test outputs:

  • Every time an MirPatch is created, it adds a cleanup block to the body if it did not exist already. If this block is unused (as is usually the case), it usually gets removed soon after by some pass calling SimplifyCFG for unrelated reasons (in many cases this cycle happens quite a few times for a single body). We now run SimplifyCFG less often, so those blocks end up in some of our outputs. I looked at changing MirPatch to not do this, but that seemed too complicated for this PR. I may still do that in a follow-up.
  • The InstCombine test had set -C opt-level=0 in its flags and so there were no storage markers. I don't really see a good motivation for doing this, so bringing it back in line with what everything else does seems correct.
  • One of the EarlyOtherwiseBranch tests had UnreachableProp running on it. Preventing that kind of thing is the goal of this feature, so this seems fine.

For the remaining tests for which this feature might be useful, we can gradually migrate them as opportunities present themselves.

In terms of documentation, I plan on submitting a PR to the rustc dev guide in the near future documenting this and other recent changes to MIR. If there's any other places to update, do let me know

r? @nagisa

@rustbot rustbot added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Apr 15, 2022
@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Apr 15, 2022
Copy link
Member

@nagisa nagisa left a comment

Choose a reason for hiding this comment

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

Looks great! See some nits inline, otherwise r=me

compiler/rustc_mir_transform/src/pass_manager.rs Outdated Show resolved Hide resolved
compiler/rustc_mir_transform/src/pass_manager.rs Outdated Show resolved Hide resolved
compiler/rustc_session/src/options.rs Outdated Show resolved Hide resolved
let name = pass.name();

if let Some((_, polarity)) = overridden_passes.iter().rev().find(|(s, _)| s == &*name) {
Copy link
Member

Choose a reason for hiding this comment

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

I wonder if we should use a StableMap for overridden_passes. This looks awfully like a n^2 situation, although it'll probably never end up with enough values to make it matter much? One other reason to use a StableMap would be to reduce rebuilds from this option being changed in a ways that don't actually matter (e.g. -Zmir-enable-passes=+InstCombine,-InstCombine has no functional difference compoared to -Zmir-enable-passes=-InstCombine)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I had considered this but actually intentionally avoided it - my reasoning was that the most important scenario for perf is the one where overridden_passes is empty (since that's what essentially every user will be using), and Vec is a data structure that I can be very certain is very fast in that case.

@@ -189,6 +191,7 @@ mod directives {
pub const STDERR_PER_BITWIDTH: &'static str = "stderr-per-bitwidth";
pub const INCREMENTAL: &'static str = "incremental";
pub const KNOWN_BUG: &'static str = "known-bug";
pub const MIR_UNIT_TEST: &'static str = "unit-test";
Copy link
Member

Choose a reason for hiding this comment

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

Hm, adding this seems useful, but I'm torn over this effectively duplicating compile-flags option. On one hand I can see how compile-flags: -Zmir-opt-level=0 -Zmir-enable-passes=+ThePass can get boring quickly, on another it never really was a huge problem with -Cno-prepopulate-passes in LLVM tests so far.

I think there's might be some value in having a somewhat uniform mechanism to achieve similar things across tests exercising different parts of the codebase.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, this is valid. The one thing that I'm keeping in the back of my mind as motivation for this is the potential to turn on some canonicalization passes by default in the future. At that point we will certainly want this header, since we don't want tests getting out of sync.

Besides that, I don't have an opinion either way. It totally might make sense to delay this until we have a need to turn on passes by default.

@nagisa
Copy link
Member

nagisa commented Apr 24, 2022

@bors r+

@bors
Copy link
Contributor

bors commented Apr 24, 2022

📌 Commit 4534188 has been approved by nagisa

@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 24, 2022
bors added a commit to rust-lang-ci/rust that referenced this pull request Apr 24, 2022
…askrgr

Rollup of 6 pull requests

Successful merges:

 - rust-lang#95395 (Better error message for `_` in function signature in `impl Trait for Ty`)
 - rust-lang#96090 (Implement MIR opt unit tests)
 - rust-lang#96107 ([test] Add test cases for untested functions for VecDeque)
 - rust-lang#96212 (Use revisions instead of nll compare mode for `/regions/` ui tests)
 - rust-lang#96215 (Drop support for legacy PM with LLVM 15)
 - rust-lang#96366 (bootstrap: Remove dead code in rustdoc shim)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit ddbeda1 into rust-lang:master Apr 25, 2022
@rustbot rustbot added this to the 1.62.0 milestone Apr 25, 2022
@JakobDegen JakobDegen deleted the mir-tests branch April 25, 2022 18:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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.

5 participants