Skip to content

Commit

Permalink
Auto merge of rust-lang#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 rust-lang#132353.

r? ghost
  • Loading branch information
bors committed Dec 4, 2024
2 parents 733616f + be36b65 commit b09bd29
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 130 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
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ fn old(_1: Result<T, E>) -> Result<T, E> {
}

bb1: {
_3 = move ((_1 as Ok).0: T);
_0 = Result::<T, E>::Ok(copy _3);
_3 = copy ((_1 as Ok).0: T);
_0 = copy _1;
goto -> bb3;
}

bb2: {
_4 = move ((_1 as Err).0: E);
_0 = Result::<T, E>::Err(copy _4);
_4 = copy ((_1 as Err).0: E);
_0 = copy _1;
goto -> bb3;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
}

bb0: {
- StorageLive(_2);
+ nop;
StorageLive(_2);
StorageLive(_3);
StorageLive(_4);
_4 = &_1;
Expand Down Expand Up @@ -50,13 +49,12 @@
StorageLive(_9);
_9 = copy _6;
- _0 = Option::<i32>::Some(move _9);
+ _0 = copy (*_2);
+ _0 = Option::<i32>::Some(copy _6);
StorageDead(_9);
- StorageDead(_6);
+ nop;
StorageDead(_4);
- StorageDead(_2);
+ nop;
StorageDead(_2);
return;
}

Expand Down

0 comments on commit b09bd29

Please sign in to comment.