Skip to content

Commit

Permalink
Auto merge of #132527 - DianQK:gvn-stmt-iter, r=<try>
Browse files Browse the repository at this point in the history
[WIP] Invalidate all dereferences when encountering non-local assignments

Fixes #132353.

r? ghost
  • Loading branch information
bors committed Nov 2, 2024
2 parents 588a420 + 64d0f48 commit 0011660
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 173 deletions.
2 changes: 2 additions & 0 deletions compiler/rustc_data_structures/src/fx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ pub type FxIndexSet<V> = indexmap::IndexSet<V, BuildHasherDefault<FxHasher>>;
pub type IndexEntry<'a, K, V> = indexmap::map::Entry<'a, K, V>;
pub type IndexOccupiedEntry<'a, K, V> = indexmap::map::OccupiedEntry<'a, K, V>;

pub use indexmap::set::MutableValues;

#[macro_export]
macro_rules! define_id_collections {
($map_name:ident, $set_name:ident, $entry_name:ident, $key:ty) => {
Expand Down
178 changes: 96 additions & 82 deletions compiler/rustc_mir_transform/src/gvn.rs

Large diffs are not rendered by default.

37 changes: 0 additions & 37 deletions compiler/rustc_mir_transform/src/ssa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,6 @@ pub(super) struct SsaLocals {
borrowed_locals: BitSet<Local>,
}

pub(super) enum AssignedValue<'a, 'tcx> {
Arg,
Rvalue(&'a mut Rvalue<'tcx>),
Terminator,
}

impl SsaLocals {
pub(super) fn new<'tcx>(
tcx: TyCtxt<'tcx>,
Expand Down Expand Up @@ -152,37 +146,6 @@ impl SsaLocals {
})
}

pub(super) fn for_each_assignment_mut<'tcx>(
&self,
basic_blocks: &mut IndexSlice<BasicBlock, BasicBlockData<'tcx>>,
mut f: impl FnMut(Local, AssignedValue<'_, 'tcx>, Location),
) {
for &local in &self.assignment_order {
match self.assignments[local] {
Set1::One(DefLocation::Argument) => f(local, AssignedValue::Arg, Location {
block: START_BLOCK,
statement_index: 0,
}),
Set1::One(DefLocation::Assignment(loc)) => {
let bb = &mut basic_blocks[loc.block];
// `loc` must point to a direct assignment to `local`.
let stmt = &mut bb.statements[loc.statement_index];
let StatementKind::Assign(box (target, ref mut rvalue)) = stmt.kind else {
bug!()
};
assert_eq!(target.as_local(), Some(local));
f(local, AssignedValue::Rvalue(rvalue), loc)
}
Set1::One(DefLocation::CallReturn { call, .. }) => {
let bb = &mut basic_blocks[call];
let loc = Location { block: call, statement_index: bb.statements.len() };
f(local, AssignedValue::Terminator, loc)
}
_ => {}
}
}
}

/// Compute the equivalence classes for locals, based on copy statements.
///
/// The returned vector maps each local to the one it copies. In the following case:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@

bb0: {
StorageLive(_1);
_1 = const <bool as NeedsDrop>::NEEDS;
- _1 = const <bool as NeedsDrop>::NEEDS;
- switchInt(move _1) -> [0: bb2, otherwise: bb1];
+ _1 = const false;
+ switchInt(const false) -> [0: bb2, otherwise: bb1];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@

bb0: {
StorageLive(_1);
_1 = const <bool as NeedsDrop>::NEEDS;
- _1 = const <bool as NeedsDrop>::NEEDS;
- switchInt(move _1) -> [0: bb2, otherwise: bb1];
+ _1 = const false;
+ switchInt(const false) -> [0: bb2, otherwise: bb1];
}

Expand Down
30 changes: 10 additions & 20 deletions tests/mir-opt/gvn.dereferences.GVN.panic-unwind.diff
Original file line number Diff line number Diff line change
Expand Up @@ -107,23 +107,18 @@
StorageLive(_18);
_18 = &(*_1);
StorageLive(_19);
- StorageLive(_20);
+ nop;
StorageLive(_20);
_20 = copy (*_18);
- _19 = opaque::<u32>(move _20) -> [return: bb7, unwind continue];
+ _19 = opaque::<u32>(copy _20) -> [return: bb7, unwind continue];
_19 = opaque::<u32>(move _20) -> [return: bb7, unwind continue];
}

bb7: {
- StorageDead(_20);
+ nop;
StorageDead(_20);
StorageDead(_19);
StorageLive(_21);
StorageLive(_22);
- _22 = copy (*_18);
- _21 = opaque::<u32>(move _22) -> [return: bb8, unwind continue];
+ _22 = copy _20;
+ _21 = opaque::<u32>(copy _20) -> [return: bb8, unwind continue];
_22 = copy (*_18);
_21 = opaque::<u32>(move _22) -> [return: bb8, unwind continue];
}

bb8: {
Expand Down Expand Up @@ -157,23 +152,18 @@
StorageDead(_28);
StorageDead(_27);
StorageLive(_29);
- StorageLive(_30);
+ nop;
StorageLive(_30);
_30 = copy ((*_3).0: u32);
- _29 = opaque::<u32>(move _30) -> [return: bb12, unwind continue];
+ _29 = opaque::<u32>(copy _30) -> [return: bb12, unwind continue];
_29 = opaque::<u32>(move _30) -> [return: bb12, unwind continue];
}

bb12: {
- StorageDead(_30);
+ nop;
StorageDead(_30);
StorageDead(_29);
StorageLive(_31);
StorageLive(_32);
- _32 = copy ((*_3).0: u32);
- _31 = opaque::<u32>(move _32) -> [return: bb13, unwind continue];
+ _32 = copy _30;
+ _31 = opaque::<u32>(copy _30) -> [return: bb13, unwind continue];
_32 = copy ((*_3).0: u32);
_31 = opaque::<u32>(move _32) -> [return: bb13, unwind continue];
}

bb13: {
Expand Down
50 changes: 18 additions & 32 deletions tests/mir-opt/gvn.subexpression_elimination.GVN.panic-unwind.diff
Original file line number Diff line number Diff line change
Expand Up @@ -758,39 +758,32 @@
StorageLive(_126);
_126 = &_3;
StorageLive(_127);
- StorageLive(_128);
- StorageLive(_129);
+ nop;
+ nop;
StorageLive(_128);
StorageLive(_129);
_129 = copy (*_126);
StorageLive(_130);
_130 = copy _1;
- _128 = Add(move _129, move _130);
+ _128 = Add(copy _129, copy _1);
+ _128 = Add(move _129, copy _1);
StorageDead(_130);
- StorageDead(_129);
- _127 = opaque::<u64>(move _128) -> [return: bb35, unwind continue];
+ nop;
+ _127 = opaque::<u64>(copy _128) -> [return: bb35, unwind continue];
StorageDead(_129);
_127 = opaque::<u64>(move _128) -> [return: bb35, unwind continue];
}

bb35: {
- StorageDead(_128);
+ nop;
StorageDead(_128);
StorageDead(_127);
StorageLive(_131);
StorageLive(_132);
StorageLive(_133);
- _133 = copy (*_126);
+ _133 = copy _129;
_133 = copy (*_126);
StorageLive(_134);
_134 = copy _1;
- _132 = Add(move _133, move _134);
+ _132 = copy _128;
+ _132 = Add(move _133, copy _1);
StorageDead(_134);
StorageDead(_133);
- _131 = opaque::<u64>(move _132) -> [return: bb36, unwind continue];
+ _131 = opaque::<u64>(copy _128) -> [return: bb36, unwind continue];
_131 = opaque::<u64>(move _132) -> [return: bb36, unwind continue];
}

bb36: {
Expand Down Expand Up @@ -906,39 +899,32 @@
StorageLive(_163);
_163 = &_3;
StorageLive(_164);
- StorageLive(_165);
- StorageLive(_166);
+ nop;
+ nop;
StorageLive(_165);
StorageLive(_166);
_166 = copy (*_163);
StorageLive(_167);
_167 = copy _1;
- _165 = Add(move _166, move _167);
+ _165 = Add(copy _166, copy _1);
+ _165 = Add(move _166, copy _1);
StorageDead(_167);
- StorageDead(_166);
- _164 = opaque::<u64>(move _165) -> [return: bb43, unwind continue];
+ nop;
+ _164 = opaque::<u64>(copy _165) -> [return: bb43, unwind continue];
StorageDead(_166);
_164 = opaque::<u64>(move _165) -> [return: bb43, unwind continue];
}

bb43: {
- StorageDead(_165);
+ nop;
StorageDead(_165);
StorageDead(_164);
StorageLive(_168);
StorageLive(_169);
StorageLive(_170);
- _170 = copy (*_163);
+ _170 = copy _166;
_170 = copy (*_163);
StorageLive(_171);
_171 = copy _1;
- _169 = Add(move _170, move _171);
+ _169 = copy _165;
+ _169 = Add(move _170, copy _1);
StorageDead(_171);
StorageDead(_170);
- _168 = opaque::<u64>(move _169) -> [return: bb44, unwind continue];
+ _168 = opaque::<u64>(copy _165) -> [return: bb44, unwind continue];
_168 = opaque::<u64>(move _169) -> [return: bb44, unwind continue];
}

bb44: {
Expand Down

0 comments on commit 0011660

Please sign in to comment.