From 23c0334fd3549c1e3a701c2669babbe272504251 Mon Sep 17 00:00:00 2001 From: Aman Arora Date: Sun, 27 Jun 2021 21:19:39 -0400 Subject: [PATCH 1/3] 2229: Reduce the size of closures with `capture_disjoint_fields` One key observation while going over the closure size profile of rustc was that we are disjointly capturing one or more fields starting at an immutable reference. Disjoint capture over immutable reference doesn't add too much value because the fields can either be borrowed immutably or copied. One possible edge case of the optimization is when a fields of a struct have a longer lifetime than the structure, therefore we can't completely get rid of all the accesses on top of sharef refs, only the rightmost one. Here is a possible example: ```rust struct MyStruct<'a> { a: &'static A, b: B, c: C<'a>, } fn foo<'a, 'b>(m: &'a MyStruct<'b>) -> impl FnMut() + 'static { let c = || drop(&*m.a.field_of_a); // Here we really do want to capture `*m.a` because that outlives `'static` // If we capture `m`, then the closure no longer outlives `'static' // it is constrained to `'a` } ``` --- compiler/rustc_typeck/src/check/upvar.rs | 50 ++++++++++++++++++- .../diagnostics/cant-mutate-imm-borrow.rs | 4 +- .../diagnostics/cant-mutate-imm-borrow.stderr | 4 +- .../2229_closure_analysis/move_closure.rs | 6 +-- .../2229_closure_analysis/move_closure.stderr | 6 +-- .../optimization/edge_case.rs | 37 ++++++++++++++ .../optimization/edge_case.stderr | 45 +++++++++++++++++ .../optimization/edge_case_run_pass.rs | 31 ++++++++++++ .../optimization/edge_case_run_pass.stderr | 11 ++++ 9 files changed, 182 insertions(+), 12 deletions(-) create mode 100644 src/test/ui/closures/2229_closure_analysis/optimization/edge_case.rs create mode 100644 src/test/ui/closures/2229_closure_analysis/optimization/edge_case.stderr create mode 100644 src/test/ui/closures/2229_closure_analysis/optimization/edge_case_run_pass.rs create mode 100644 src/test/ui/closures/2229_closure_analysis/optimization/edge_case_run_pass.stderr diff --git a/compiler/rustc_typeck/src/check/upvar.rs b/compiler/rustc_typeck/src/check/upvar.rs index 7b5b14ae6c8..8418626b8e6 100644 --- a/compiler/rustc_typeck/src/check/upvar.rs +++ b/compiler/rustc_typeck/src/check/upvar.rs @@ -1609,11 +1609,17 @@ fn consume( "consume(place_with_id={:?}, diag_expr_id={:?}, mode={:?})", place_with_id, diag_expr_id, mode ); + + let place_with_id = PlaceWithHirId { + place: truncate_capture_for_optimization(&place_with_id.place), + ..*place_with_id + }; + if !self.capture_information.contains_key(&place_with_id.place) { - self.init_capture_info_for_place(place_with_id, diag_expr_id); + self.init_capture_info_for_place(&place_with_id, diag_expr_id); } - self.adjust_upvar_borrow_kind_for_consume(place_with_id, diag_expr_id, mode); + self.adjust_upvar_borrow_kind_for_consume(&place_with_id, diag_expr_id, mode); } fn borrow( @@ -1636,6 +1642,8 @@ fn borrow( &place_with_id.place, ); + let place = truncate_capture_for_optimization(&place); + let place_with_id = PlaceWithHirId { place, ..*place_with_id }; if !self.capture_information.contains_key(&place_with_id.place) { @@ -1967,6 +1975,44 @@ fn determine_place_ancestry_relation( } } +/// Reduces the precision of the captured place when the precision doesn't yeild any benefit from +/// borrow checking prespective, allowing us to save us on the size of the capture. +/// +/// +/// Fields that are read through a shared reference will always be read via a shared ref or a copy, +/// and therefore capturing precise paths yields no benefit. This optimization truncates the +/// rightmost deref of the capture if the deref is applied to a shared ref. +/// +/// Reason we only drop the last deref is because of the following edge case: +/// +/// ```rust +/// struct MyStruct<'a> { +/// a: &'static A, +/// b: B, +/// c: C<'a>, +/// } +/// +/// fn foo<'a, 'b>(m: &'a MyStruct<'b>) -> impl FnMut() + 'static { +/// let c = || drop(&*m.a.field_of_a); +/// // Here we really do want to capture `*m.a` because that outlives `'static` +/// +/// // If we capture `m`, then the closure no longer outlives `'static' +/// // it is constrained to `'a` +/// } +/// ``` +fn truncate_capture_for_optimization<'tcx>(place: &Place<'tcx>) -> Place<'tcx> { + let is_shared_ref = |ty: Ty<'_>| matches!(ty.kind(), ty::Ref(.., hir::Mutability::Not)); + + let idx = place.projections.iter().rposition(|proj| ProjectionKind::Deref == proj.kind); + + match idx { + Some(idx) if is_shared_ref(place.ty_before_projection(idx)) => { + Place { projections: place.projections[0..=idx].to_vec(), ..place.clone() } + } + None | Some(_) => place.clone(), + } +} + /// Precise capture is enabled if the feature gate `capture_disjoint_fields` is enabled or if /// user is using Rust Edition 2021 or higher. /// diff --git a/src/test/ui/closures/2229_closure_analysis/diagnostics/cant-mutate-imm-borrow.rs b/src/test/ui/closures/2229_closure_analysis/diagnostics/cant-mutate-imm-borrow.rs index a5b4a19d8c3..77effcb0065 100644 --- a/src/test/ui/closures/2229_closure_analysis/diagnostics/cant-mutate-imm-borrow.rs +++ b/src/test/ui/closures/2229_closure_analysis/diagnostics/cant-mutate-imm-borrow.rs @@ -8,10 +8,10 @@ fn main() { let mut y = (&x, "Y"); let z = (&mut y, "Z"); - // `x.0` is mutable but we access `x` via `z.0.0`, which is an immutable reference and + // `x.0` is mutable but we access `x` via `*z.0.0`, which is an immutable reference and // therefore can't be mutated. let mut c = || { - //~^ ERROR: cannot borrow `z.0.0.0` as mutable, as it is behind a `&` reference + //~^ ERROR: cannot borrow `*z.0.0` as mutable, as it is behind a `&` reference z.0.0.0 = format!("X1"); }; diff --git a/src/test/ui/closures/2229_closure_analysis/diagnostics/cant-mutate-imm-borrow.stderr b/src/test/ui/closures/2229_closure_analysis/diagnostics/cant-mutate-imm-borrow.stderr index cfe531e17d3..38c530b809a 100644 --- a/src/test/ui/closures/2229_closure_analysis/diagnostics/cant-mutate-imm-borrow.stderr +++ b/src/test/ui/closures/2229_closure_analysis/diagnostics/cant-mutate-imm-borrow.stderr @@ -1,11 +1,11 @@ -error[E0596]: cannot borrow `z.0.0.0` as mutable, as it is behind a `&` reference +error[E0596]: cannot borrow `*z.0.0` as mutable, as it is behind a `&` reference --> $DIR/cant-mutate-imm-borrow.rs:13:17 | LL | let mut c = || { | ^^ cannot borrow as mutable LL | LL | z.0.0.0 = format!("X1"); - | ------- mutable borrow occurs due to use of `z.0.0.0` in closure + | ------- mutable borrow occurs due to use of `*z.0.0` in closure error: aborting due to previous error diff --git a/src/test/ui/closures/2229_closure_analysis/move_closure.rs b/src/test/ui/closures/2229_closure_analysis/move_closure.rs index 06db19974eb..76874e03dc0 100644 --- a/src/test/ui/closures/2229_closure_analysis/move_closure.rs +++ b/src/test/ui/closures/2229_closure_analysis/move_closure.rs @@ -78,8 +78,8 @@ fn struct_contains_ref_to_another_struct_2() { //~^ ERROR: First Pass analysis includes: //~| ERROR: Min Capture analysis includes: let _t = t.0.0; - //~^ NOTE: Capturing t[(0, 0),Deref,(0, 0)] -> ImmBorrow - //~| NOTE: Min Capture t[(0, 0),Deref,(0, 0)] -> ImmBorrow + //~^ NOTE: Capturing t[(0, 0),Deref] -> ImmBorrow + //~| NOTE: Min Capture t[(0, 0),Deref] -> ImmBorrow }; c(); @@ -100,7 +100,7 @@ fn struct_contains_ref_to_another_struct_3() { //~^ ERROR: First Pass analysis includes: //~| ERROR: Min Capture analysis includes: let _t = t.0.0; - //~^ NOTE: Capturing t[(0, 0),Deref,(0, 0)] -> ImmBorrow + //~^ NOTE: Capturing t[(0, 0),Deref] -> ImmBorrow //~| NOTE: Capturing t[(0, 0)] -> ByValue //~| NOTE: Min Capture t[(0, 0)] -> ByValue }; diff --git a/src/test/ui/closures/2229_closure_analysis/move_closure.stderr b/src/test/ui/closures/2229_closure_analysis/move_closure.stderr index 013cacfb9f2..b35aadfcbd4 100644 --- a/src/test/ui/closures/2229_closure_analysis/move_closure.stderr +++ b/src/test/ui/closures/2229_closure_analysis/move_closure.stderr @@ -190,7 +190,7 @@ LL | | LL | | }; | |_____^ | -note: Capturing t[(0, 0),Deref,(0, 0)] -> ImmBorrow +note: Capturing t[(0, 0),Deref] -> ImmBorrow --> $DIR/move_closure.rs:80:18 | LL | let _t = t.0.0; @@ -208,7 +208,7 @@ LL | | LL | | }; | |_____^ | -note: Min Capture t[(0, 0),Deref,(0, 0)] -> ImmBorrow +note: Min Capture t[(0, 0),Deref] -> ImmBorrow --> $DIR/move_closure.rs:80:18 | LL | let _t = t.0.0; @@ -226,7 +226,7 @@ LL | | LL | | }; | |_____^ | -note: Capturing t[(0, 0),Deref,(0, 0)] -> ImmBorrow +note: Capturing t[(0, 0),Deref] -> ImmBorrow --> $DIR/move_closure.rs:102:18 | LL | let _t = t.0.0; diff --git a/src/test/ui/closures/2229_closure_analysis/optimization/edge_case.rs b/src/test/ui/closures/2229_closure_analysis/optimization/edge_case.rs new file mode 100644 index 00000000000..960c61ccaef --- /dev/null +++ b/src/test/ui/closures/2229_closure_analysis/optimization/edge_case.rs @@ -0,0 +1,37 @@ +#![feature(capture_disjoint_fields)] +//~^ WARNING: the feature `capture_disjoint_fields` is incomplete +//~| NOTE: `#[warn(incomplete_features)]` on by default +//~| NOTE: see issue #53488 + +#![feature(rustc_attrs)] +#![allow(unused)] +#![allow(dead_code)] + +struct Int(i32); +struct B<'a>(&'a i32); + +const I : Int = Int(0); +const REF_I : &'static Int = &I; + + +struct MyStruct<'a> { + a: &'static Int, + b: B<'a>, +} + +fn foo<'a, 'b>(m: &'a MyStruct<'b>) -> impl FnMut() + 'static { + let c = #[rustc_capture_analysis] || drop(&m.a.0); + //~^ ERROR: attributes on expressions are experimental + //~| NOTE: see issue #15701 + //~| ERROR: First Pass analysis includes: + //~| ERROR: Min Capture analysis includes: + //~| NOTE: Capturing m[Deref,(0, 0),Deref] -> ImmBorrow + //~| NOTE: Min Capture m[Deref,(0, 0),Deref] -> ImmBorrow + c +} + +fn main() { + let t = 0; + let s = MyStruct { a: REF_I, b: B(&t) }; + let _ = foo(&s); +} diff --git a/src/test/ui/closures/2229_closure_analysis/optimization/edge_case.stderr b/src/test/ui/closures/2229_closure_analysis/optimization/edge_case.stderr new file mode 100644 index 00000000000..c9f9386b9aa --- /dev/null +++ b/src/test/ui/closures/2229_closure_analysis/optimization/edge_case.stderr @@ -0,0 +1,45 @@ +error[E0658]: attributes on expressions are experimental + --> $DIR/edge_case.rs:23:13 + | +LL | let c = #[rustc_capture_analysis] || drop(&m.a.0); + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: see issue #15701 for more information + = help: add `#![feature(stmt_expr_attributes)]` to the crate attributes to enable + +warning: the feature `capture_disjoint_fields` is incomplete and may not be safe to use and/or cause compiler crashes + --> $DIR/edge_case.rs:1:12 + | +LL | #![feature(capture_disjoint_fields)] + | ^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `#[warn(incomplete_features)]` on by default + = note: see issue #53488 for more information + +error: First Pass analysis includes: + --> $DIR/edge_case.rs:23:39 + | +LL | let c = #[rustc_capture_analysis] || drop(&m.a.0); + | ^^^^^^^^^^^^^^^ + | +note: Capturing m[Deref,(0, 0),Deref] -> ImmBorrow + --> $DIR/edge_case.rs:23:48 + | +LL | let c = #[rustc_capture_analysis] || drop(&m.a.0); + | ^^^^^ + +error: Min Capture analysis includes: + --> $DIR/edge_case.rs:23:39 + | +LL | let c = #[rustc_capture_analysis] || drop(&m.a.0); + | ^^^^^^^^^^^^^^^ + | +note: Min Capture m[Deref,(0, 0),Deref] -> ImmBorrow + --> $DIR/edge_case.rs:23:48 + | +LL | let c = #[rustc_capture_analysis] || drop(&m.a.0); + | ^^^^^ + +error: aborting due to 3 previous errors; 1 warning emitted + +For more information about this error, try `rustc --explain E0658`. diff --git a/src/test/ui/closures/2229_closure_analysis/optimization/edge_case_run_pass.rs b/src/test/ui/closures/2229_closure_analysis/optimization/edge_case_run_pass.rs new file mode 100644 index 00000000000..2c4ba0c91a6 --- /dev/null +++ b/src/test/ui/closures/2229_closure_analysis/optimization/edge_case_run_pass.rs @@ -0,0 +1,31 @@ +#![feature(capture_disjoint_fields)] +//~^ WARNING: the feature `capture_disjoint_fields` is incomplete +//~| NOTE: `#[warn(incomplete_features)]` on by default +//~| NOTE: see issue #53488 + +// run-pass + +#![allow(unused)] +#![allow(dead_code)] + +struct Int(i32); +struct B<'a>(&'a i32); + +const I : Int = Int(0); +const REF_I : &'static Int = &I; + +struct MyStruct<'a> { + a: &'static Int, + b: B<'a>, +} + +fn foo<'a, 'b>(m: &'a MyStruct<'b>) -> impl FnMut() + 'static { + let c = || drop(&m.a.0); + c +} + +fn main() { + let t = 0; + let s = MyStruct { a: REF_I, b: B(&t) }; + let _ = foo(&s); +} diff --git a/src/test/ui/closures/2229_closure_analysis/optimization/edge_case_run_pass.stderr b/src/test/ui/closures/2229_closure_analysis/optimization/edge_case_run_pass.stderr new file mode 100644 index 00000000000..36906d1685c --- /dev/null +++ b/src/test/ui/closures/2229_closure_analysis/optimization/edge_case_run_pass.stderr @@ -0,0 +1,11 @@ +warning: the feature `capture_disjoint_fields` is incomplete and may not be safe to use and/or cause compiler crashes + --> $DIR/edge_case_run_pass.rs:1:12 + | +LL | #![feature(capture_disjoint_fields)] + | ^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `#[warn(incomplete_features)]` on by default + = note: see issue #53488 for more information + +warning: 1 warning emitted + From 32fa96486a5a9969d6151cf436dca4a31f8da292 Mon Sep 17 00:00:00 2001 From: Aman Arora Date: Tue, 29 Jun 2021 03:33:24 -0400 Subject: [PATCH 2/3] Edition 2021 for tests --- .../optimization/edge_case.rs | 5 +---- .../optimization/edge_case.stderr | 21 ++++++------------- .../optimization/edge_case_run_pass.rs | 6 +----- .../optimization/edge_case_run_pass.stderr | 11 ---------- 4 files changed, 8 insertions(+), 35 deletions(-) delete mode 100644 src/test/ui/closures/2229_closure_analysis/optimization/edge_case_run_pass.stderr diff --git a/src/test/ui/closures/2229_closure_analysis/optimization/edge_case.rs b/src/test/ui/closures/2229_closure_analysis/optimization/edge_case.rs index 960c61ccaef..37a2a97d442 100644 --- a/src/test/ui/closures/2229_closure_analysis/optimization/edge_case.rs +++ b/src/test/ui/closures/2229_closure_analysis/optimization/edge_case.rs @@ -1,7 +1,4 @@ -#![feature(capture_disjoint_fields)] -//~^ WARNING: the feature `capture_disjoint_fields` is incomplete -//~| NOTE: `#[warn(incomplete_features)]` on by default -//~| NOTE: see issue #53488 +// edition:2021 #![feature(rustc_attrs)] #![allow(unused)] diff --git a/src/test/ui/closures/2229_closure_analysis/optimization/edge_case.stderr b/src/test/ui/closures/2229_closure_analysis/optimization/edge_case.stderr index c9f9386b9aa..b727c06d952 100644 --- a/src/test/ui/closures/2229_closure_analysis/optimization/edge_case.stderr +++ b/src/test/ui/closures/2229_closure_analysis/optimization/edge_case.stderr @@ -1,5 +1,5 @@ error[E0658]: attributes on expressions are experimental - --> $DIR/edge_case.rs:23:13 + --> $DIR/edge_case.rs:20:13 | LL | let c = #[rustc_capture_analysis] || drop(&m.a.0); | ^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -7,39 +7,30 @@ LL | let c = #[rustc_capture_analysis] || drop(&m.a.0); = note: see issue #15701 for more information = help: add `#![feature(stmt_expr_attributes)]` to the crate attributes to enable -warning: the feature `capture_disjoint_fields` is incomplete and may not be safe to use and/or cause compiler crashes - --> $DIR/edge_case.rs:1:12 - | -LL | #![feature(capture_disjoint_fields)] - | ^^^^^^^^^^^^^^^^^^^^^^^ - | - = note: `#[warn(incomplete_features)]` on by default - = note: see issue #53488 for more information - error: First Pass analysis includes: - --> $DIR/edge_case.rs:23:39 + --> $DIR/edge_case.rs:20:39 | LL | let c = #[rustc_capture_analysis] || drop(&m.a.0); | ^^^^^^^^^^^^^^^ | note: Capturing m[Deref,(0, 0),Deref] -> ImmBorrow - --> $DIR/edge_case.rs:23:48 + --> $DIR/edge_case.rs:20:48 | LL | let c = #[rustc_capture_analysis] || drop(&m.a.0); | ^^^^^ error: Min Capture analysis includes: - --> $DIR/edge_case.rs:23:39 + --> $DIR/edge_case.rs:20:39 | LL | let c = #[rustc_capture_analysis] || drop(&m.a.0); | ^^^^^^^^^^^^^^^ | note: Min Capture m[Deref,(0, 0),Deref] -> ImmBorrow - --> $DIR/edge_case.rs:23:48 + --> $DIR/edge_case.rs:20:48 | LL | let c = #[rustc_capture_analysis] || drop(&m.a.0); | ^^^^^ -error: aborting due to 3 previous errors; 1 warning emitted +error: aborting due to 3 previous errors For more information about this error, try `rustc --explain E0658`. diff --git a/src/test/ui/closures/2229_closure_analysis/optimization/edge_case_run_pass.rs b/src/test/ui/closures/2229_closure_analysis/optimization/edge_case_run_pass.rs index 2c4ba0c91a6..033fd6f1775 100644 --- a/src/test/ui/closures/2229_closure_analysis/optimization/edge_case_run_pass.rs +++ b/src/test/ui/closures/2229_closure_analysis/optimization/edge_case_run_pass.rs @@ -1,8 +1,4 @@ -#![feature(capture_disjoint_fields)] -//~^ WARNING: the feature `capture_disjoint_fields` is incomplete -//~| NOTE: `#[warn(incomplete_features)]` on by default -//~| NOTE: see issue #53488 - +// edition:2021 // run-pass #![allow(unused)] diff --git a/src/test/ui/closures/2229_closure_analysis/optimization/edge_case_run_pass.stderr b/src/test/ui/closures/2229_closure_analysis/optimization/edge_case_run_pass.stderr deleted file mode 100644 index 36906d1685c..00000000000 --- a/src/test/ui/closures/2229_closure_analysis/optimization/edge_case_run_pass.stderr +++ /dev/null @@ -1,11 +0,0 @@ -warning: the feature `capture_disjoint_fields` is incomplete and may not be safe to use and/or cause compiler crashes - --> $DIR/edge_case_run_pass.rs:1:12 - | -LL | #![feature(capture_disjoint_fields)] - | ^^^^^^^^^^^^^^^^^^^^^^^ - | - = note: `#[warn(incomplete_features)]` on by default - = note: see issue #53488 for more information - -warning: 1 warning emitted - From 38dcae2cda85916d91d727ffe08a944e69ee3162 Mon Sep 17 00:00:00 2001 From: Niko Matsakis Date: Tue, 6 Jul 2021 20:52:53 -0400 Subject: [PATCH 3/3] Apply suggestions from code review --- compiler/rustc_typeck/src/check/upvar.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/compiler/rustc_typeck/src/check/upvar.rs b/compiler/rustc_typeck/src/check/upvar.rs index 8418626b8e6..86d978718dd 100644 --- a/compiler/rustc_typeck/src/check/upvar.rs +++ b/compiler/rustc_typeck/src/check/upvar.rs @@ -2003,9 +2003,13 @@ fn determine_place_ancestry_relation( fn truncate_capture_for_optimization<'tcx>(place: &Place<'tcx>) -> Place<'tcx> { let is_shared_ref = |ty: Ty<'_>| matches!(ty.kind(), ty::Ref(.., hir::Mutability::Not)); + // Find the right-most deref (if any). All the projections that come after this + // are fields or other "in-place pointer adjustments"; these refer therefore to + // data owned by whatever pointer is being dereferenced here. let idx = place.projections.iter().rposition(|proj| ProjectionKind::Deref == proj.kind); match idx { + // If that pointer is a shared reference, then we don't need those fields. Some(idx) if is_shared_ref(place.ty_before_projection(idx)) => { Place { projections: place.projections[0..=idx].to_vec(), ..place.clone() } }