Skip to content

Commit 0011660

Browse files
committed
Auto merge of #132527 - DianQK:gvn-stmt-iter, r=<try>
[WIP] Invalidate all dereferences when encountering non-local assignments Fixes #132353. r? ghost
2 parents 588a420 + 64d0f48 commit 0011660

7 files changed

+130
-173
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/gvn.dereferences.GVN.panic-unwind.diff

+10-20
Original file line numberDiff line numberDiff line change
@@ -107,23 +107,18 @@
107107
StorageLive(_18);
108108
_18 = &(*_1);
109109
StorageLive(_19);
110-
- StorageLive(_20);
111-
+ nop;
110+
StorageLive(_20);
112111
_20 = copy (*_18);
113-
- _19 = opaque::<u32>(move _20) -> [return: bb7, unwind continue];
114-
+ _19 = opaque::<u32>(copy _20) -> [return: bb7, unwind continue];
112+
_19 = opaque::<u32>(move _20) -> [return: bb7, unwind continue];
115113
}
116114

117115
bb7: {
118-
- StorageDead(_20);
119-
+ nop;
116+
StorageDead(_20);
120117
StorageDead(_19);
121118
StorageLive(_21);
122119
StorageLive(_22);
123-
- _22 = copy (*_18);
124-
- _21 = opaque::<u32>(move _22) -> [return: bb8, unwind continue];
125-
+ _22 = copy _20;
126-
+ _21 = opaque::<u32>(copy _20) -> [return: bb8, unwind continue];
120+
_22 = copy (*_18);
121+
_21 = opaque::<u32>(move _22) -> [return: bb8, unwind continue];
127122
}
128123

129124
bb8: {
@@ -157,23 +152,18 @@
157152
StorageDead(_28);
158153
StorageDead(_27);
159154
StorageLive(_29);
160-
- StorageLive(_30);
161-
+ nop;
155+
StorageLive(_30);
162156
_30 = copy ((*_3).0: u32);
163-
- _29 = opaque::<u32>(move _30) -> [return: bb12, unwind continue];
164-
+ _29 = opaque::<u32>(copy _30) -> [return: bb12, unwind continue];
157+
_29 = opaque::<u32>(move _30) -> [return: bb12, unwind continue];
165158
}
166159

167160
bb12: {
168-
- StorageDead(_30);
169-
+ nop;
161+
StorageDead(_30);
170162
StorageDead(_29);
171163
StorageLive(_31);
172164
StorageLive(_32);
173-
- _32 = copy ((*_3).0: u32);
174-
- _31 = opaque::<u32>(move _32) -> [return: bb13, unwind continue];
175-
+ _32 = copy _30;
176-
+ _31 = opaque::<u32>(copy _30) -> [return: bb13, unwind continue];
165+
_32 = copy ((*_3).0: u32);
166+
_31 = opaque::<u32>(move _32) -> [return: bb13, unwind continue];
177167
}
178168

179169
bb13: {

tests/mir-opt/gvn.subexpression_elimination.GVN.panic-unwind.diff

+18-32
Original file line numberDiff line numberDiff line change
@@ -758,39 +758,32 @@
758758
StorageLive(_126);
759759
_126 = &_3;
760760
StorageLive(_127);
761-
- StorageLive(_128);
762-
- StorageLive(_129);
763-
+ nop;
764-
+ nop;
761+
StorageLive(_128);
762+
StorageLive(_129);
765763
_129 = copy (*_126);
766764
StorageLive(_130);
767765
_130 = copy _1;
768766
- _128 = Add(move _129, move _130);
769-
+ _128 = Add(copy _129, copy _1);
767+
+ _128 = Add(move _129, copy _1);
770768
StorageDead(_130);
771-
- StorageDead(_129);
772-
- _127 = opaque::<u64>(move _128) -> [return: bb35, unwind continue];
773-
+ nop;
774-
+ _127 = opaque::<u64>(copy _128) -> [return: bb35, unwind continue];
769+
StorageDead(_129);
770+
_127 = opaque::<u64>(move _128) -> [return: bb35, unwind continue];
775771
}
776772

777773
bb35: {
778-
- StorageDead(_128);
779-
+ nop;
774+
StorageDead(_128);
780775
StorageDead(_127);
781776
StorageLive(_131);
782777
StorageLive(_132);
783778
StorageLive(_133);
784-
- _133 = copy (*_126);
785-
+ _133 = copy _129;
779+
_133 = copy (*_126);
786780
StorageLive(_134);
787781
_134 = copy _1;
788782
- _132 = Add(move _133, move _134);
789-
+ _132 = copy _128;
783+
+ _132 = Add(move _133, copy _1);
790784
StorageDead(_134);
791785
StorageDead(_133);
792-
- _131 = opaque::<u64>(move _132) -> [return: bb36, unwind continue];
793-
+ _131 = opaque::<u64>(copy _128) -> [return: bb36, unwind continue];
786+
_131 = opaque::<u64>(move _132) -> [return: bb36, unwind continue];
794787
}
795788

796789
bb36: {
@@ -906,39 +899,32 @@
906899
StorageLive(_163);
907900
_163 = &_3;
908901
StorageLive(_164);
909-
- StorageLive(_165);
910-
- StorageLive(_166);
911-
+ nop;
912-
+ nop;
902+
StorageLive(_165);
903+
StorageLive(_166);
913904
_166 = copy (*_163);
914905
StorageLive(_167);
915906
_167 = copy _1;
916907
- _165 = Add(move _166, move _167);
917-
+ _165 = Add(copy _166, copy _1);
908+
+ _165 = Add(move _166, copy _1);
918909
StorageDead(_167);
919-
- StorageDead(_166);
920-
- _164 = opaque::<u64>(move _165) -> [return: bb43, unwind continue];
921-
+ nop;
922-
+ _164 = opaque::<u64>(copy _165) -> [return: bb43, unwind continue];
910+
StorageDead(_166);
911+
_164 = opaque::<u64>(move _165) -> [return: bb43, unwind continue];
923912
}
924913

925914
bb43: {
926-
- StorageDead(_165);
927-
+ nop;
915+
StorageDead(_165);
928916
StorageDead(_164);
929917
StorageLive(_168);
930918
StorageLive(_169);
931919
StorageLive(_170);
932-
- _170 = copy (*_163);
933-
+ _170 = copy _166;
920+
_170 = copy (*_163);
934921
StorageLive(_171);
935922
_171 = copy _1;
936923
- _169 = Add(move _170, move _171);
937-
+ _169 = copy _165;
924+
+ _169 = Add(move _170, copy _1);
938925
StorageDead(_171);
939926
StorageDead(_170);
940-
- _168 = opaque::<u64>(move _169) -> [return: bb44, unwind continue];
941-
+ _168 = opaque::<u64>(copy _165) -> [return: bb44, unwind continue];
927+
_168 = opaque::<u64>(move _169) -> [return: bb44, unwind continue];
942928
}
943929

944930
bb44: {

0 commit comments

Comments
 (0)