Skip to content

Commit

Permalink
Do not grow assignment_order needlessly.
Browse files Browse the repository at this point in the history
  • Loading branch information
cjgillot committed Feb 27, 2023
1 parent 2a32a2b commit 209eb8a
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 22 deletions.
7 changes: 5 additions & 2 deletions compiler/rustc_mir_transform/src/ssa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl SsaLocals {
body: &Body<'tcx>,
borrowed_locals: &BitSet<Local>,
) -> SsaLocals {
let assignment_order = Vec::new();
let assignment_order = Vec::with_capacity(body.local_decls.len());

let assignments = IndexVec::from_elem(Set1::Empty, &body.local_decls);
let dominators =
Expand Down Expand Up @@ -203,7 +203,10 @@ impl<'tcx> Visitor<'tcx> for SsaVisitor {
match ctxt {
PlaceContext::MutatingUse(MutatingUseContext::Store) => {
self.assignments[local].insert(LocationExtended::Plain(loc));
self.assignment_order.push(local);
if let Set1::One(_) = self.assignments[local] {
// Only record if SSA-like, to avoid growing the vector needlessly.
self.assignment_order.push(local);
}
}
// Anything can happen with raw pointers, so remove them.
PlaceContext::NonMutatingUse(NonMutatingUseContext::AddressOf)
Expand Down
25 changes: 20 additions & 5 deletions tests/mir-opt/copy-prop/reborrow.demiraw.CopyProp.diff
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
let mut _0: (); // return place in scope 0 at $DIR/reborrow.rs:+0:23: +0:23
let _2: *mut u8; // in scope 0 at $DIR/reborrow.rs:+1:9: +1:10
let mut _4: &mut u8; // in scope 0 at $DIR/reborrow.rs:+2:22: +2:29
let _6: (); // in scope 0 at $DIR/reborrow.rs:+4:5: +4:14
let mut _7: *mut u8; // in scope 0 at $DIR/reborrow.rs:+4:12: +4:13
scope 1 {
debug a => _2; // in scope 1 at $DIR/reborrow.rs:+1:9: +1:10
let _3: &mut u8; // in scope 1 at $DIR/reborrow.rs:+2:9: +2:10
Expand All @@ -31,11 +33,24 @@
StorageDead(_4); // scope 1 at $DIR/reborrow.rs:+2:31: +2:32
- StorageLive(_5); // scope 2 at $DIR/reborrow.rs:+3:9: +3:10
- _5 = _2; // scope 2 at $DIR/reborrow.rs:+3:13: +3:14
_0 = const (); // scope 0 at $DIR/reborrow.rs:+0:23: +4:2
- StorageDead(_5); // scope 2 at $DIR/reborrow.rs:+4:1: +4:2
StorageDead(_3); // scope 1 at $DIR/reborrow.rs:+4:1: +4:2
- StorageDead(_2); // scope 0 at $DIR/reborrow.rs:+4:1: +4:2
return; // scope 0 at $DIR/reborrow.rs:+4:2: +4:2
StorageLive(_6); // scope 4 at $DIR/reborrow.rs:+4:5: +4:14
- StorageLive(_7); // scope 4 at $DIR/reborrow.rs:+4:12: +4:13
- _7 = _5; // scope 4 at $DIR/reborrow.rs:+4:12: +4:13
- _6 = opaque::<*mut u8>(move _7) -> bb1; // scope 4 at $DIR/reborrow.rs:+4:5: +4:14
+ _6 = opaque::<*mut u8>(_2) -> bb1; // scope 4 at $DIR/reborrow.rs:+4:5: +4:14
// mir::Constant
// + span: $DIR/reborrow.rs:38:5: 38:11
// + literal: Const { ty: fn(*mut u8) {opaque::<*mut u8>}, val: Value(<ZST>) }
}

bb1: {
- StorageDead(_7); // scope 4 at $DIR/reborrow.rs:+4:13: +4:14
StorageDead(_6); // scope 4 at $DIR/reborrow.rs:+4:14: +4:15
_0 = const (); // scope 0 at $DIR/reborrow.rs:+0:23: +5:2
- StorageDead(_5); // scope 2 at $DIR/reborrow.rs:+5:1: +5:2
StorageDead(_3); // scope 1 at $DIR/reborrow.rs:+5:1: +5:2
- StorageDead(_2); // scope 0 at $DIR/reborrow.rs:+5:1: +5:2
return; // scope 0 at $DIR/reborrow.rs:+5:2: +5:2
}
}

25 changes: 20 additions & 5 deletions tests/mir-opt/copy-prop/reborrow.miraw.CopyProp.diff
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
debug x => _1; // in scope 0 at $DIR/reborrow.rs:+0:10: +0:15
let mut _0: (); // return place in scope 0 at $DIR/reborrow.rs:+0:21: +0:21
let _2: *mut u8; // in scope 0 at $DIR/reborrow.rs:+1:9: +1:10
let _5: (); // in scope 0 at $DIR/reborrow.rs:+4:5: +4:14
let mut _6: *mut u8; // in scope 0 at $DIR/reborrow.rs:+4:12: +4:13
scope 1 {
debug a => _2; // in scope 1 at $DIR/reborrow.rs:+1:9: +1:10
let _3: *mut u8; // in scope 1 at $DIR/reborrow.rs:+2:9: +2:10
Expand All @@ -27,11 +29,24 @@
_3 = &raw mut (*_2); // scope 3 at $DIR/reborrow.rs:+2:22: +2:33
- StorageLive(_4); // scope 2 at $DIR/reborrow.rs:+3:9: +3:10
- _4 = _2; // scope 2 at $DIR/reborrow.rs:+3:13: +3:14
_0 = const (); // scope 0 at $DIR/reborrow.rs:+0:21: +4:2
- StorageDead(_4); // scope 2 at $DIR/reborrow.rs:+4:1: +4:2
StorageDead(_3); // scope 1 at $DIR/reborrow.rs:+4:1: +4:2
- StorageDead(_2); // scope 0 at $DIR/reborrow.rs:+4:1: +4:2
return; // scope 0 at $DIR/reborrow.rs:+4:2: +4:2
StorageLive(_5); // scope 4 at $DIR/reborrow.rs:+4:5: +4:14
- StorageLive(_6); // scope 4 at $DIR/reborrow.rs:+4:12: +4:13
- _6 = _4; // scope 4 at $DIR/reborrow.rs:+4:12: +4:13
- _5 = opaque::<*mut u8>(move _6) -> bb1; // scope 4 at $DIR/reborrow.rs:+4:5: +4:14
+ _5 = opaque::<*mut u8>(_2) -> bb1; // scope 4 at $DIR/reborrow.rs:+4:5: +4:14
// mir::Constant
// + span: $DIR/reborrow.rs:30:5: 30:11
// + literal: Const { ty: fn(*mut u8) {opaque::<*mut u8>}, val: Value(<ZST>) }
}

bb1: {
- StorageDead(_6); // scope 4 at $DIR/reborrow.rs:+4:13: +4:14
StorageDead(_5); // scope 4 at $DIR/reborrow.rs:+4:14: +4:15
_0 = const (); // scope 0 at $DIR/reborrow.rs:+0:21: +5:2
- StorageDead(_4); // scope 2 at $DIR/reborrow.rs:+5:1: +5:2
StorageDead(_3); // scope 1 at $DIR/reborrow.rs:+5:1: +5:2
- StorageDead(_2); // scope 0 at $DIR/reborrow.rs:+5:1: +5:2
return; // scope 0 at $DIR/reborrow.rs:+5:2: +5:2
}
}

25 changes: 20 additions & 5 deletions tests/mir-opt/copy-prop/reborrow.remut.CopyProp.diff
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
debug x => _1; // in scope 0 at $DIR/reborrow.rs:+0:10: +0:15
let mut _0: (); // return place in scope 0 at $DIR/reborrow.rs:+0:21: +0:21
let _2: &mut u8; // in scope 0 at $DIR/reborrow.rs:+1:9: +1:10
let _5: (); // in scope 0 at $DIR/reborrow.rs:+4:5: +4:14
let mut _6: &mut u8; // in scope 0 at $DIR/reborrow.rs:+4:12: +4:13
scope 1 {
debug a => _2; // in scope 1 at $DIR/reborrow.rs:+1:9: +1:10
let _3: &mut u8; // in scope 1 at $DIR/reborrow.rs:+2:9: +2:10
Expand All @@ -25,11 +27,24 @@
_3 = &mut (*_2); // scope 1 at $DIR/reborrow.rs:+2:13: +2:20
- StorageLive(_4); // scope 2 at $DIR/reborrow.rs:+3:9: +3:10
- _4 = move _2; // scope 2 at $DIR/reborrow.rs:+3:13: +3:14
_0 = const (); // scope 0 at $DIR/reborrow.rs:+0:21: +4:2
- StorageDead(_4); // scope 2 at $DIR/reborrow.rs:+4:1: +4:2
StorageDead(_3); // scope 1 at $DIR/reborrow.rs:+4:1: +4:2
- StorageDead(_2); // scope 0 at $DIR/reborrow.rs:+4:1: +4:2
return; // scope 0 at $DIR/reborrow.rs:+4:2: +4:2
StorageLive(_5); // scope 3 at $DIR/reborrow.rs:+4:5: +4:14
- StorageLive(_6); // scope 3 at $DIR/reborrow.rs:+4:12: +4:13
- _6 = move _4; // scope 3 at $DIR/reborrow.rs:+4:12: +4:13
- _5 = opaque::<&mut u8>(move _6) -> bb1; // scope 3 at $DIR/reborrow.rs:+4:5: +4:14
+ _5 = opaque::<&mut u8>(move _2) -> bb1; // scope 3 at $DIR/reborrow.rs:+4:5: +4:14
// mir::Constant
// + span: $DIR/reborrow.rs:14:5: 14:11
// + literal: Const { ty: fn(&mut u8) {opaque::<&mut u8>}, val: Value(<ZST>) }
}

bb1: {
- StorageDead(_6); // scope 3 at $DIR/reborrow.rs:+4:13: +4:14
StorageDead(_5); // scope 3 at $DIR/reborrow.rs:+4:14: +4:15
_0 = const (); // scope 0 at $DIR/reborrow.rs:+0:21: +5:2
- StorageDead(_4); // scope 2 at $DIR/reborrow.rs:+5:1: +5:2
StorageDead(_3); // scope 1 at $DIR/reborrow.rs:+5:1: +5:2
- StorageDead(_2); // scope 0 at $DIR/reborrow.rs:+5:1: +5:2
return; // scope 0 at $DIR/reborrow.rs:+5:2: +5:2
}
}

25 changes: 20 additions & 5 deletions tests/mir-opt/copy-prop/reborrow.reraw.CopyProp.diff
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
debug x => _1; // in scope 0 at $DIR/reborrow.rs:+0:10: +0:15
let mut _0: (); // return place in scope 0 at $DIR/reborrow.rs:+0:21: +0:21
let _2: &mut u8; // in scope 0 at $DIR/reborrow.rs:+1:9: +1:10
let _5: (); // in scope 0 at $DIR/reborrow.rs:+4:5: +4:14
let mut _6: &mut u8; // in scope 0 at $DIR/reborrow.rs:+4:12: +4:13
scope 1 {
debug a => _2; // in scope 1 at $DIR/reborrow.rs:+1:9: +1:10
let _3: *mut u8; // in scope 1 at $DIR/reborrow.rs:+2:9: +2:10
Expand All @@ -25,11 +27,24 @@
_3 = &raw mut (*_2); // scope 1 at $DIR/reborrow.rs:+2:13: +2:24
- StorageLive(_4); // scope 2 at $DIR/reborrow.rs:+3:9: +3:10
- _4 = move _2; // scope 2 at $DIR/reborrow.rs:+3:13: +3:14
_0 = const (); // scope 0 at $DIR/reborrow.rs:+0:21: +4:2
- StorageDead(_4); // scope 2 at $DIR/reborrow.rs:+4:1: +4:2
StorageDead(_3); // scope 1 at $DIR/reborrow.rs:+4:1: +4:2
- StorageDead(_2); // scope 0 at $DIR/reborrow.rs:+4:1: +4:2
return; // scope 0 at $DIR/reborrow.rs:+4:2: +4:2
StorageLive(_5); // scope 3 at $DIR/reborrow.rs:+4:5: +4:14
- StorageLive(_6); // scope 3 at $DIR/reborrow.rs:+4:12: +4:13
- _6 = move _4; // scope 3 at $DIR/reborrow.rs:+4:12: +4:13
- _5 = opaque::<&mut u8>(move _6) -> bb1; // scope 3 at $DIR/reborrow.rs:+4:5: +4:14
+ _5 = opaque::<&mut u8>(move _2) -> bb1; // scope 3 at $DIR/reborrow.rs:+4:5: +4:14
// mir::Constant
// + span: $DIR/reborrow.rs:22:5: 22:11
// + literal: Const { ty: fn(&mut u8) {opaque::<&mut u8>}, val: Value(<ZST>) }
}

bb1: {
- StorageDead(_6); // scope 3 at $DIR/reborrow.rs:+4:13: +4:14
StorageDead(_5); // scope 3 at $DIR/reborrow.rs:+4:14: +4:15
_0 = const (); // scope 0 at $DIR/reborrow.rs:+0:21: +5:2
- StorageDead(_4); // scope 2 at $DIR/reborrow.rs:+5:1: +5:2
StorageDead(_3); // scope 1 at $DIR/reborrow.rs:+5:1: +5:2
- StorageDead(_2); // scope 0 at $DIR/reborrow.rs:+5:1: +5:2
return; // scope 0 at $DIR/reborrow.rs:+5:2: +5:2
}
}

7 changes: 7 additions & 0 deletions tests/mir-opt/copy-prop/reborrow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,39 @@

#![feature(raw_ref_op)]

#[inline(never)]
fn opaque(_: impl Sized) {}

// EMIT_MIR reborrow.remut.CopyProp.diff
fn remut(mut x: u8) {
let a = &mut x;
let b = &mut *a; //< this cannot mutate a.
let c = a; //< so `c` and `a` can be merged.
opaque(c);
}

// EMIT_MIR reborrow.reraw.CopyProp.diff
fn reraw(mut x: u8) {
let a = &mut x;
let b = &raw mut *a; //< this cannot mutate a.
let c = a; //< so `c` and `a` can be merged.
opaque(c);
}

// EMIT_MIR reborrow.miraw.CopyProp.diff
fn miraw(mut x: u8) {
let a = &raw mut x;
let b = unsafe { &raw mut *a }; //< this cannot mutate a.
let c = a; //< so `c` and `a` can be merged.
opaque(c);
}

// EMIT_MIR reborrow.demiraw.CopyProp.diff
fn demiraw(mut x: u8) {
let a = &raw mut x;
let b = unsafe { &mut *a }; //< this cannot mutate a.
let c = a; //< so `c` and `a` can be merged.
opaque(c);
}

fn main() {
Expand Down

0 comments on commit 209eb8a

Please sign in to comment.