Repository navigation
Don't emit no-op &T → &T reborrows - #164014
Jules-Bertholet wants to merge 2 commits into
Conversation
|
cc @rust-lang/clippy |
| where | ||
| T: TrivialClone, | ||
| { | ||
| #[inline] |
There was a problem hiding this comment.
To make tests/codegen-llvm/lib-optimizations/append-elements.rs happy. That test is really fragile…
|
@bors try @rust-timer queue |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Don't emit no-op `&T` → `&T` reborrows
This comment has been minimized.
This comment has been minimized.
|
Finished benchmarking commit (d77c4b4): comparison URL. Overall result: ✅ improvements - no action neededBenchmarking means the PR may be perf-sensitive. It's automatically marked not fit for rolling up. Overriding is possible but disadvised: it risks changing compiler perf. @bors rollup=never rustc-perf Instruction countOur most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.
Max RSS (memory usage)Results (primary -2.1%, secondary -2.9%)A less reliable metric. May be of interest, but not used to determine the overall result above.
CyclesResults (primary -2.7%, secondary -4.6%)A less reliable metric. May be of interest, but not used to determine the overall result above.
Binary sizeResults (primary 0.1%, secondary -0.2%)A less reliable metric. May be of interest, but not used to determine the overall result above.
Bootstrap: 486.533s -> 486.402s (-0.03%) |
|
Does this have implications on what Miri can catch? |
It shouldn't. Miri does retags on every typed copy. |
This comment has been minimized.
This comment has been minimized.
9190d54 to
2cbbc14
Compare
This comment has been minimized.
This comment has been minimized.
2cbbc14 to
258e369
Compare
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
258e369 to
e8df7c9
Compare
This comment has been minimized.
This comment has been minimized.
e8df7c9 to
2912de4
Compare
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
2912de4 to
b6cc682
Compare
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
| Adjust::Borrow(AutoBorrow::Ref(m)) => { | ||
| // Don't emit no-op &T -> &T reborrows | ||
| if let AutoBorrowMutability::Not = m | ||
| && let ExprKind::Deref { arg: derefed_expr } = expr.kind | ||
| && let ty::Ref(_, _, ty::Mutability::Not) = | ||
| self.thir.exprs[derefed_expr].ty.kind() | ||
| // FIXME: removing these reborrows may prevent closure upvars from resolving | ||
| && !self.tcx.is_closure_like(self.body_owner) | ||
| { | ||
| assert_eq!(self.thir.exprs.last_index(), Some(derefed_expr)); | ||
| return self.thir.exprs.pop().unwrap(); | ||
| } else { |
There was a problem hiding this comment.
This covers the skipping in coercion, right? It's more general, since it removes all redundant adjustments, whereas the coercion skipping only removes one autoborrow + deref pair?
It also skips adjustments from method confirmation 🤔
vibe: how about we only keep this skipping in THIR building and drop those in coercion?
This shouldn't affect the major perf win in borrowck, and it reduces duplicated logic for this optimization.
There was a problem hiding this comment.
Not doing HIR skipping might lose some of the Clippy and diagnostics improvements. I will check
There was a problem hiding this comment.
Can confirm, we lose the Clippy wins. Which is quite minor in this PR, but a bigger loss with #163717 (lose all the redundant_closure improvements)
There was a problem hiding this comment.
Could you link some examples? I didn't find tests mentioning redundant_closure or redundant_clone in #163717. Only found one redundant_closure_for_method_call.
Curious about why not doing HIR skipping affects them.
There was a problem hiding this comment.
Look at the clippy UI test diffs of 3fe31e3#diff-1a22849d98bd10df7627f6e869d3aac2a384b02629660ab698b7bcb0fe2ca452
There was a problem hiding this comment.
These lints look at a closure's HIR to determine "does it do anything other than a function call", and coercion counts as "anything"
15dc8af to
145a6dd
Compare
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
145a6dd to
a21e76a
Compare
This comment has been minimized.
This comment has been minimized.
a21e76a to
3762a2c
Compare
This comment has been minimized.
This comment has been minimized.
3762a2c to
1348290
Compare
This comment has been minimized.
This comment has been minimized.
|
I just realized—this does have an effect on borrowcheck. The following goes from compile fail → pass: fn main() {
let x: &'static ();
let _: &_ = /* there used to be an implicit reborrow here */ x;
}Personally, I think we should accept this, but worth discussing. Note that the following compiles today: fn main() {
let x: &'static ();
let _ = x;
}What semantics do we want here? @rustbot label I-opsem-nominated T-opsem T-types |
This comment has been minimized.
This comment has been minimized.
Except inside closures, where we need to keep capture inference stable. Gets rid of MIR bloat, and improves diagnostics.
1348290 to
7a4a1ea
Compare
|
This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
View all comments
Except inside closures, where we need to keep capture inference stable. Gets rid of MIR bloat, and improves diagnostics.
See also #163717 (would allow doing this even inside closures), #163494 (would allow doing this for mutable references outside closures).
r? compiler
@rustbot label A-coercions