Skip to content

Convert moves of references to copies in ReferencePropagation #142185

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

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 31 additions & 3 deletions compiler/rustc_mir_transform/src/ref_prop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ impl<'tcx> crate::MirPass<'tcx> for ReferencePropagation {
#[instrument(level = "trace", skip(self, tcx, body))]
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
debug!(def_id = ?body.source.def_id());
move_to_copy_pointers(tcx, body);
while propagate_ssa(tcx, body) {}
}

Expand All @@ -87,11 +88,38 @@ impl<'tcx> crate::MirPass<'tcx> for ReferencePropagation {
}
}

fn move_to_copy_pointers<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
let mut visitor = MoveToCopyVisitor { tcx, local_decls: &body.local_decls };
for (bb, data) in body.basic_blocks.as_mut_preserves_cfg().iter_enumerated_mut() {
visitor.visit_basic_block_data(bb, data);
}

struct MoveToCopyVisitor<'a, 'tcx> {
tcx: TyCtxt<'tcx>,
local_decls: &'a IndexVec<Local, LocalDecl<'tcx>>,
}

impl<'a, 'tcx> MutVisitor<'tcx> for MoveToCopyVisitor<'a, 'tcx> {
fn tcx(&self) -> TyCtxt<'tcx> {
self.tcx
}

fn visit_operand(&mut self, operand: &mut Operand<'tcx>, loc: Location) {
if let Operand::Move(place) = *operand {
if place.ty(self.local_decls, self.tcx).ty.is_any_ptr() {
*operand = Operand::Copy(place);
}
}
self.super_operand(operand, loc);
}
}
}

fn propagate_ssa<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) -> bool {
let typing_env = body.typing_env(tcx);
let ssa = SsaLocals::new(tcx, body, typing_env);

let mut replacer = compute_replacement(tcx, body, &ssa);
let mut replacer = compute_replacement(tcx, body, ssa);
debug!(?replacer.targets);
debug!(?replacer.allowed_replacements);
debug!(?replacer.storage_to_remove);
Expand Down Expand Up @@ -119,7 +147,7 @@ enum Value<'tcx> {
fn compute_replacement<'tcx>(
tcx: TyCtxt<'tcx>,
body: &Body<'tcx>,
ssa: &SsaLocals,
ssa: SsaLocals,
) -> Replacer<'tcx> {
let always_live_locals = always_storage_live_locals(body);

Expand All @@ -138,7 +166,7 @@ fn compute_replacement<'tcx>(
// reborrowed references.
let mut storage_to_remove = DenseBitSet::new_empty(body.local_decls.len());

let fully_replacable_locals = fully_replacable_locals(ssa);
let fully_replacable_locals = fully_replacable_locals(&ssa);

// Returns true iff we can use `place` as a pointee.
//
Expand Down
2 changes: 1 addition & 1 deletion tests/mir-opt/building/index_array_and_slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ struct WithSliceTail(f64, [i32]);
fn index_custom(custom: &WithSliceTail, index: usize) -> &i32 {
// CHECK: bb0:
// CHECK: [[PTR:_.+]] = &raw const (fake) ((*_1).1: [i32]);
// CHECK: [[LEN:_.+]] = PtrMetadata(move [[PTR]]);
// CHECK: [[LEN:_.+]] = PtrMetadata(copy [[PTR]]);
// CHECK: [[LT:_.+]] = Lt(copy _2, copy [[LEN]]);
// CHECK: assert(move [[LT]], "index out of bounds{{.+}}", move [[LEN]], copy _2) -> [success: bb1,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
StorageDead(_6);
_4 = copy _5 as *mut [u8] (Transmute);
StorageDead(_5);
_3 = move _4 as *mut u8 (PtrToPtr);
_3 = copy _4 as *mut u8 (PtrToPtr);
StorageDead(_4);
StorageDead(_3);
- StorageDead(_1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ fn slice_get_mut_usize(_1: &mut [u32], _2: usize) -> Option<&mut u32> {
StorageDead(_3);
StorageLive(_5);
_5 = &mut (*_1)[_2];
_0 = Option::<&mut u32>::Some(move _5);
_0 = Option::<&mut u32>::Some(copy _5);
StorageDead(_5);
goto -> bb3;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@
_13 = &(*_26);
StorageLive(_15);
_15 = RangeFull;
_12 = <[i32; 10] as Index<RangeFull>>::index(move _13, move _15) -> [return: bb5, unwind continue];
- _12 = <[i32; 10] as Index<RangeFull>>::index(move _13, move _15) -> [return: bb5, unwind continue];
+ _12 = <[i32; 10] as Index<RangeFull>>::index(copy _13, move _15) -> [return: bb5, unwind continue];
}

bb5: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,9 @@
- StorageLive(_14);
- _14 = &_11;
- _13 = &(*_14);
- _12 = move _13;
+ _13 = &_11;
_12 = move _13;
+ _12 = copy _13;
StorageDead(_13);
- StorageDead(_14);
StorageLive(_15);
Expand Down Expand Up @@ -252,7 +253,8 @@
StorageLive(_23);
StorageLive(_24);
_24 = copy _21;
_23 = opaque::<&&usize>(move _24) -> [return: bb3, unwind continue];
- _23 = opaque::<&&usize>(move _24) -> [return: bb3, unwind continue];
+ _23 = opaque::<&&usize>(copy _24) -> [return: bb3, unwind continue];
}

bb3: {
Expand All @@ -276,7 +278,8 @@
StorageLive(_30);
StorageLive(_31);
_31 = copy _28;
_30 = opaque::<*mut &usize>(move _31) -> [return: bb4, unwind continue];
- _30 = opaque::<*mut &usize>(move _31) -> [return: bb4, unwind continue];
+ _30 = opaque::<*mut &usize>(copy _31) -> [return: bb4, unwind continue];
}

bb4: {
Expand All @@ -299,7 +302,8 @@
StorageLive(_36);
StorageLive(_37);
_37 = copy _34;
_36 = opaque::<&usize>(move _37) -> [return: bb5, unwind continue];
- _36 = opaque::<&usize>(move _37) -> [return: bb5, unwind continue];
+ _36 = opaque::<&usize>(copy _37) -> [return: bb5, unwind continue];
}

bb5: {
Expand Down Expand Up @@ -328,7 +332,8 @@
StorageLive(_45);
StorageLive(_46);
_46 = copy _44;
_45 = opaque::<&usize>(move _46) -> [return: bb6, unwind continue];
- _45 = opaque::<&usize>(move _46) -> [return: bb6, unwind continue];
+ _45 = opaque::<&usize>(copy _46) -> [return: bb6, unwind continue];
}

bb6: {
Expand Down Expand Up @@ -368,8 +373,9 @@
- StorageLive(_55);
- _55 = &(*_1);
- _54 = &(*_55);
- _2 = move _54;
+ _54 = &(*_1);
_2 = move _54;
+ _2 = copy _54;
StorageDead(_54);
- StorageDead(_55);
StorageLive(_56);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,8 @@
_12 = &raw const _10;
StorageLive(_13);
_13 = &raw const _11;
_12 = move _13;
- _12 = move _13;
+ _12 = copy _13;
StorageDead(_13);
StorageLive(_14);
_14 = copy (*_12);
Expand Down Expand Up @@ -265,7 +266,8 @@
StorageLive(_22);
StorageLive(_23);
_23 = copy _20;
_22 = opaque::<&*const usize>(move _23) -> [return: bb3, unwind continue];
- _22 = opaque::<&*const usize>(move _23) -> [return: bb3, unwind continue];
+ _22 = opaque::<&*const usize>(copy _23) -> [return: bb3, unwind continue];
}

bb3: {
Expand All @@ -289,7 +291,8 @@
StorageLive(_29);
StorageLive(_30);
_30 = copy _27;
_29 = opaque::<*mut *const usize>(move _30) -> [return: bb4, unwind continue];
- _29 = opaque::<*mut *const usize>(move _30) -> [return: bb4, unwind continue];
+ _29 = opaque::<*mut *const usize>(copy _30) -> [return: bb4, unwind continue];
}

bb4: {
Expand All @@ -312,7 +315,8 @@
StorageLive(_35);
StorageLive(_36);
_36 = copy _33;
_35 = opaque::<*const usize>(move _36) -> [return: bb5, unwind continue];
- _35 = opaque::<*const usize>(move _36) -> [return: bb5, unwind continue];
+ _35 = opaque::<*const usize>(copy _36) -> [return: bb5, unwind continue];
}

bb5: {
Expand Down Expand Up @@ -341,7 +345,8 @@
StorageLive(_44);
StorageLive(_45);
_45 = copy _43;
_44 = opaque::<*const usize>(move _45) -> [return: bb6, unwind continue];
- _44 = opaque::<*const usize>(move _45) -> [return: bb6, unwind continue];
+ _44 = opaque::<*const usize>(copy _45) -> [return: bb6, unwind continue];
}

bb6: {
Expand Down Expand Up @@ -379,7 +384,8 @@
_52 = &raw const (*_2);
StorageLive(_53);
_53 = &raw const (*_1);
_2 = move _53;
- _2 = move _53;
+ _2 = copy _53;
StorageDead(_53);
StorageLive(_54);
_54 = copy (*_52);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,9 @@
- StorageLive(_14);
- _14 = &mut _11;
- _13 = &mut (*_14);
- _12 = move _13;
+ _13 = &mut _11;
_12 = move _13;
+ _12 = copy _13;
StorageDead(_13);
- StorageDead(_14);
StorageLive(_15);
Expand Down Expand Up @@ -251,7 +252,8 @@
StorageLive(_23);
StorageLive(_24);
_24 = copy _21;
_23 = opaque::<&&mut usize>(move _24) -> [return: bb3, unwind continue];
- _23 = opaque::<&&mut usize>(move _24) -> [return: bb3, unwind continue];
+ _23 = opaque::<&&mut usize>(copy _24) -> [return: bb3, unwind continue];
}

bb3: {
Expand All @@ -275,7 +277,8 @@
StorageLive(_30);
StorageLive(_31);
_31 = copy _28;
_30 = opaque::<*mut &mut usize>(move _31) -> [return: bb4, unwind continue];
- _30 = opaque::<*mut &mut usize>(move _31) -> [return: bb4, unwind continue];
+ _30 = opaque::<*mut &mut usize>(copy _31) -> [return: bb4, unwind continue];
}

bb4: {
Expand All @@ -296,8 +299,10 @@
_35 = copy (*_34);
StorageLive(_36);
StorageLive(_37);
_37 = move _34;
_36 = opaque::<&mut usize>(move _37) -> [return: bb5, unwind continue];
- _37 = move _34;
- _36 = opaque::<&mut usize>(move _37) -> [return: bb5, unwind continue];
+ _37 = copy _34;
+ _36 = opaque::<&mut usize>(copy _37) -> [return: bb5, unwind continue];
}

bb5: {
Expand All @@ -316,15 +321,19 @@
StorageLive(_41);
_41 = copy (*_40);
StorageLive(_42);
_42 = move _40;
- _42 = move _40;
+ _42 = copy _40;
StorageLive(_43);
_43 = copy (*_42);
StorageLive(_44);
_44 = move _42;
- _44 = move _42;
+ _44 = copy _42;
StorageLive(_45);
StorageLive(_46);
_46 = move _44;
_45 = opaque::<&mut usize>(move _46) -> [return: bb6, unwind continue];
- _46 = move _44;
- _45 = opaque::<&mut usize>(move _46) -> [return: bb6, unwind continue];
+ _46 = copy _44;
+ _45 = opaque::<&mut usize>(copy _46) -> [return: bb6, unwind continue];
}

bb6: {
Expand Down Expand Up @@ -364,8 +373,9 @@
- StorageLive(_55);
- _55 = &mut (*_1);
- _54 = &mut (*_55);
- _2 = move _54;
+ _54 = &mut (*_1);
_2 = move _54;
+ _2 = copy _54;
StorageDead(_54);
- StorageDead(_55);
StorageLive(_56);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,8 @@
_12 = &raw mut _10;
StorageLive(_13);
_13 = &raw mut _11;
_12 = move _13;
- _12 = move _13;
+ _12 = copy _13;
StorageDead(_13);
StorageLive(_14);
_14 = copy (*_12);
Expand Down Expand Up @@ -245,7 +246,8 @@
StorageLive(_22);
StorageLive(_23);
_23 = copy _20;
_22 = opaque::<&*mut usize>(move _23) -> [return: bb3, unwind continue];
- _22 = opaque::<&*mut usize>(move _23) -> [return: bb3, unwind continue];
+ _22 = opaque::<&*mut usize>(copy _23) -> [return: bb3, unwind continue];
}

bb3: {
Expand All @@ -269,7 +271,8 @@
StorageLive(_29);
StorageLive(_30);
_30 = copy _27;
_29 = opaque::<*mut *mut usize>(move _30) -> [return: bb4, unwind continue];
- _29 = opaque::<*mut *mut usize>(move _30) -> [return: bb4, unwind continue];
+ _29 = opaque::<*mut *mut usize>(copy _30) -> [return: bb4, unwind continue];
}

bb4: {
Expand All @@ -291,7 +294,8 @@
StorageLive(_35);
StorageLive(_36);
_36 = copy _33;
_35 = opaque::<*mut usize>(move _36) -> [return: bb5, unwind continue];
- _35 = opaque::<*mut usize>(move _36) -> [return: bb5, unwind continue];
+ _35 = opaque::<*mut usize>(copy _36) -> [return: bb5, unwind continue];
}

bb5: {
Expand All @@ -318,7 +322,8 @@
StorageLive(_44);
StorageLive(_45);
_45 = copy _43;
_44 = opaque::<*mut usize>(move _45) -> [return: bb6, unwind continue];
- _44 = opaque::<*mut usize>(move _45) -> [return: bb6, unwind continue];
+ _44 = opaque::<*mut usize>(copy _45) -> [return: bb6, unwind continue];
}

bb6: {
Expand Down Expand Up @@ -356,7 +361,8 @@
_52 = &raw mut (*_2);
StorageLive(_53);
_53 = &raw mut (*_1);
_2 = move _53;
- _2 = move _53;
+ _2 = copy _53;
StorageDead(_53);
StorageLive(_54);
_54 = copy (*_52);
Expand Down
Loading
Loading