Skip to content

Commit b09bd29

Browse files
committed
Auto merge of rust-lang#132527 - DianQK:gvn-stmt-iter, r=<try>
[WIP] Invalidate all dereferences when encountering non-local assignments Fixes rust-lang#132353. r? ghost
2 parents 733616f + be36b65 commit b09bd29

File tree

7 files changed

+109
-130
lines changed

7 files changed

+109
-130
lines changed

compiler/rustc_data_structures/src/fx.rs

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ pub type FxIndexSet<V> = indexmap::IndexSet<V, BuildHasherDefault<FxHasher>>;
99
pub type IndexEntry<'a, K, V> = indexmap::map::Entry<'a, K, V>;
1010
pub type IndexOccupiedEntry<'a, K, V> = indexmap::map::OccupiedEntry<'a, K, V>;
1111

12+
pub use indexmap::set::MutableValues;
13+
1214
#[macro_export]
1315
macro_rules! define_id_collections {
1416
($map_name:ident, $set_name:ident, $entry_name:ident, $key:ty) => {

compiler/rustc_mir_transform/src/gvn.rs

+96-82
Large diffs are not rendered by default.

compiler/rustc_mir_transform/src/ssa.rs

-37
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,6 @@ pub(super) struct SsaLocals {
3232
borrowed_locals: BitSet<Local>,
3333
}
3434

35-
pub(super) enum AssignedValue<'a, 'tcx> {
36-
Arg,
37-
Rvalue(&'a mut Rvalue<'tcx>),
38-
Terminator,
39-
}
40-
4135
impl SsaLocals {
4236
pub(super) fn new<'tcx>(
4337
tcx: TyCtxt<'tcx>,
@@ -152,37 +146,6 @@ impl SsaLocals {
152146
})
153147
}
154148

155-
pub(super) fn for_each_assignment_mut<'tcx>(
156-
&self,
157-
basic_blocks: &mut IndexSlice<BasicBlock, BasicBlockData<'tcx>>,
158-
mut f: impl FnMut(Local, AssignedValue<'_, 'tcx>, Location),
159-
) {
160-
for &local in &self.assignment_order {
161-
match self.assignments[local] {
162-
Set1::One(DefLocation::Argument) => f(local, AssignedValue::Arg, Location {
163-
block: START_BLOCK,
164-
statement_index: 0,
165-
}),
166-
Set1::One(DefLocation::Assignment(loc)) => {
167-
let bb = &mut basic_blocks[loc.block];
168-
// `loc` must point to a direct assignment to `local`.
169-
let stmt = &mut bb.statements[loc.statement_index];
170-
let StatementKind::Assign(box (target, ref mut rvalue)) = stmt.kind else {
171-
bug!()
172-
};
173-
assert_eq!(target.as_local(), Some(local));
174-
f(local, AssignedValue::Rvalue(rvalue), loc)
175-
}
176-
Set1::One(DefLocation::CallReturn { call, .. }) => {
177-
let bb = &mut basic_blocks[call];
178-
let loc = Location { block: call, statement_index: bb.statements.len() };
179-
f(local, AssignedValue::Terminator, loc)
180-
}
181-
_ => {}
182-
}
183-
}
184-
}
185-
186149
/// Compute the equivalence classes for locals, based on copy statements.
187150
///
188151
/// The returned vector maps each local to the one it copies. In the following case:

tests/mir-opt/const_prop/control_flow_simplification.hello.GVN.panic-abort.diff

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88

99
bb0: {
1010
StorageLive(_1);
11-
_1 = const <bool as NeedsDrop>::NEEDS;
11+
- _1 = const <bool as NeedsDrop>::NEEDS;
1212
- switchInt(move _1) -> [0: bb2, otherwise: bb1];
13+
+ _1 = const false;
1314
+ switchInt(const false) -> [0: bb2, otherwise: bb1];
1415
}
1516

tests/mir-opt/const_prop/control_flow_simplification.hello.GVN.panic-unwind.diff

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88

99
bb0: {
1010
StorageLive(_1);
11-
_1 = const <bool as NeedsDrop>::NEEDS;
11+
- _1 = const <bool as NeedsDrop>::NEEDS;
1212
- switchInt(move _1) -> [0: bb2, otherwise: bb1];
13+
+ _1 = const false;
1314
+ switchInt(const false) -> [0: bb2, otherwise: bb1];
1415
}
1516

tests/mir-opt/pre-codegen/try_identity.old.PreCodegen.after.mir

+4-4
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ fn old(_1: Result<T, E>) -> Result<T, E> {
1919
}
2020

2121
bb1: {
22-
_3 = move ((_1 as Ok).0: T);
23-
_0 = Result::<T, E>::Ok(copy _3);
22+
_3 = copy ((_1 as Ok).0: T);
23+
_0 = copy _1;
2424
goto -> bb3;
2525
}
2626

2727
bb2: {
28-
_4 = move ((_1 as Err).0: E);
29-
_0 = Result::<T, E>::Err(copy _4);
28+
_4 = copy ((_1 as Err).0: E);
29+
_0 = copy _1;
3030
goto -> bb3;
3131
}
3232

tests/mir-opt/simplify_aggregate_to_copy_miscompile.foo.GVN.diff

+3-5
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@
1919
}
2020

2121
bb0: {
22-
- StorageLive(_2);
23-
+ nop;
22+
StorageLive(_2);
2423
StorageLive(_3);
2524
StorageLive(_4);
2625
_4 = &_1;
@@ -50,13 +49,12 @@
5049
StorageLive(_9);
5150
_9 = copy _6;
5251
- _0 = Option::<i32>::Some(move _9);
53-
+ _0 = copy (*_2);
52+
+ _0 = Option::<i32>::Some(copy _6);
5453
StorageDead(_9);
5554
- StorageDead(_6);
5655
+ nop;
5756
StorageDead(_4);
58-
- StorageDead(_2);
59-
+ nop;
57+
StorageDead(_2);
6058
return;
6159
}
6260

0 commit comments

Comments
 (0)