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

rustc_mir_transform cleanups, round 2 #129929

Merged
merged 10 commits into from
Sep 9, 2024

Conversation

nnethercote
Copy link
Contributor

More cleanups in the style of #129738.

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 Sep 3, 2024
@rustbot
Copy link
Collaborator

rustbot commented Sep 3, 2024

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

Some changes occurred in coverage instrumentation.

cc @Zalathar

let mut calls_to_terminate = Vec::new();
let mut cleanups_to_remove = Vec::new();
for (id, block) in body.basic_blocks.iter_enumerated() {
for block in body.basic_blocks.as_mut() {
Copy link
Contributor

Choose a reason for hiding this comment

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

This changes behaviour: basic_block.as_mut() clears the cfg caches.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch. I will change it to as_mut_preserves_cfg.

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 have switched to as_mut_preserves_cfg, but I'm not certain if it's correct.

When you say "changes behaviour", clearing the caches might hurt perf, but won't have any visibile functional effect, right? But switching to as_mut_preserves_cfg could have a visible effect. I can just remove this commit if necessary.

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 could just go back to as_mut() and let the cache be cleared, and measure the perf effect. I suspect it will be negligible.

Copy link
Contributor

@cjgillot cjgillot Sep 6, 2024

Choose a reason for hiding this comment

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

Using this is actually unsound, as you are modifying the CFG.
You need to call body.basic_blocks.invalidate_cfg_cache() explicitly if you use this.
3 options:

  • keep as-is;
  • use as_mut() and potentially clear the cache -> probably not an issue, as this pass runs just after ElaborateDrops which changes the CFG a lot, so we may have nothing in cache;
  • use as_mut_preserves_cfg() and call invalidate_cfg_cache() explicitly -> I'm not super fond, as that's a footgun.

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 measured and found that the cache is always totally empty when this code path is hit. So I will convert back to as_mut and I think that should be good enough.

compiler/rustc_mir_transform/src/early_otherwise_branch.rs Outdated Show resolved Hide resolved
});
terminator.kind = TerminatorKind::Goto { target };
}
let target = target.unwrap();
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm a bit split on this commit. This could backfire in case we have a pathological input where MIR builder concluded that we don't need a target block, although we need one.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The existing code uses a mix of fallible and infallible. Can you see a reason for this split? To me it wasn't clear, it looked arbitrary, so I tried making it all infallible and the test suite passed...

Copy link
Contributor

Choose a reason for hiding this comment

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

The only reason I see is personal taste of the person writing the code.
target can only be None if the returned type is uninhabited. If the type is known, which happens a lot for intrinsics, we can unwrap without any issue. If the type depends on the user code, we can't.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

All the intrinsics have a non-! return type: forget, {add,sub,mul}_with_overflow, size_of, min_align_of, and discriminant_value. So I think this change is safe.

@nnethercote
Copy link
Contributor Author

I have updated: I switched to as_mut_preserves_cfg, but I'm not sure about it's correctness. I replaced the len == 0 comparison. I haven't change the target unwrapping. Let me know what you think. Thanks!

@bors
Copy link
Contributor

bors commented Sep 5, 2024

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

@bors
Copy link
Contributor

bors commented Sep 6, 2024

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

Lots of unnecessary `pub`s in this crate. Most are downgraded to
`pub(super)`, though some don't need any visibility.
Currently it constructs two vectors `calls_to_terminated` and
`cleanups_to_remove` in the main loop, and then processes them after the
main loop. But the processing can be done in the main loop, avoiding the
need for the vectors.
Let chains are perfect for this kind of function.
Nothing comes after it within the loop.
It doesn't need to be, and it's 72 bytes on 64-bit platforms, which is
fairly large.
It's nicer than the `IndexVec` type.
In some cases `target` and `arg` are obtained fallibly, and in some
cases they are obtained infallibly. This commit changes them all to
infallible.
@nnethercote
Copy link
Contributor Author

I rebased. Now that I've gone back to as_mut, I think this should be ready to merge.

@cjgillot
Copy link
Contributor

cjgillot commented Sep 9, 2024

@bors r+

@bors
Copy link
Contributor

bors commented Sep 9, 2024

📌 Commit 5445953 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 Sep 9, 2024
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Sep 9, 2024
…eanups-2, r=cjgillot

`rustc_mir_transform` cleanups, round 2

More cleanups in the style of rust-lang#129738.

r? `@cjgillot`
bors added a commit to rust-lang-ci/rust that referenced this pull request Sep 9, 2024
…iaskrgr

Rollup of 9 pull requests

Successful merges:

 - rust-lang#129929 (`rustc_mir_transform` cleanups, round 2)
 - rust-lang#130022 (Dataflow/borrowck lifetime cleanups)
 - rust-lang#130064 (fix ICE in CMSE type validation)
 - rust-lang#130067 (Remove redundant check in `symlink_hard_link` test)
 - rust-lang#130131 (Print a helpful message if any tests were skipped for being up-to-date)
 - rust-lang#130137 (Fix ICE caused by missing span in a region error)
 - rust-lang#130153 (use verbose flag as a default value for `rust.verbose-tests`)
 - rust-lang#130154 (Stabilize `char::MIN`)
 - rust-lang#130158 (Update books)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 3e3b148 into rust-lang:master Sep 9, 2024
6 checks passed
@rustbot rustbot added this to the 1.83.0 milestone Sep 9, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Sep 9, 2024
Rollup merge of rust-lang#129929 - nnethercote:rustc_mir_transform-cleanups-2, r=cjgillot

`rustc_mir_transform` cleanups, round 2

More cleanups in the style of rust-lang#129738.

r? ``@cjgillot``
@nnethercote nnethercote deleted the rustc_mir_transform-cleanups-2 branch September 9, 2024 22:37
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Sep 10, 2024
…eanups-3, r=saethlin

`rustc_mir_transform` cleanups 3

More cleanups in the style of rust-lang#129929.

r? `@saethlin`
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Sep 10, 2024
Rollup merge of rust-lang#130175 - nnethercote:rustc_mir_transform-cleanups-3, r=saethlin

`rustc_mir_transform` cleanups 3

More cleanups in the style of rust-lang#129929.

r? `@saethlin`
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.

4 participants