-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
rustc_mir_transform
cleanups, round 2
#129929
Conversation
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() { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 afterElaborateDrops
which changes the CFG a lot, so we may have nothing in cache; - use
as_mut_preserves_cfg()
and callinvalidate_cfg_cache()
explicitly -> I'm not super fond, as that's a footgun.
There was a problem hiding this comment.
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.
}); | ||
terminator.kind = TerminatorKind::Goto { target }; | ||
} | ||
let target = target.unwrap(); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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...
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
6c39576
to
713a465
Compare
I have updated: I switched to |
713a465
to
63376c6
Compare
☔ The latest upstream changes (presumably #129936) made this pull request unmergeable. Please resolve the merge conflicts. |
63376c6
to
a297ee8
Compare
☔ 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.
a297ee8
to
5445953
Compare
I rebased. Now that I've gone back to |
@bors r+ |
…eanups-2, r=cjgillot `rustc_mir_transform` cleanups, round 2 More cleanups in the style of rust-lang#129738. r? `@cjgillot`
…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
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``
…eanups-3, r=saethlin `rustc_mir_transform` cleanups 3 More cleanups in the style of rust-lang#129929. r? `@saethlin`
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`
More cleanups in the style of #129738.
r? @cjgillot