Skip to content

Commit 3c43b61

Browse files
committed
Do not consider borrowed Freeze locals as SSA.
1 parent 3490375 commit 3c43b61

10 files changed

+69
-43
lines changed

compiler/rustc_mir_transform/src/copy_prop.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,8 @@ impl<'tcx> MirPass<'tcx> for CopyProp {
3333
}
3434

3535
fn propagate_ssa<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
36-
let param_env = tcx.param_env_reveal_all_normalized(body.source.def_id());
3736
let borrowed_locals = borrowed_locals(body);
38-
let ssa = SsaLocals::new(tcx, param_env, body, &borrowed_locals);
37+
let ssa = SsaLocals::new(body);
3938

4039
let fully_moved = fully_moved_locals(&ssa, body);
4140
debug!(?fully_moved);

compiler/rustc_mir_transform/src/normalize_array_len.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use rustc_index::IndexVec;
77
use rustc_middle::mir::visit::*;
88
use rustc_middle::mir::*;
99
use rustc_middle::ty::{self, TyCtxt};
10-
use rustc_mir_dataflow::impls::borrowed_locals;
1110

1211
pub struct NormalizeArrayLen;
1312

@@ -24,9 +23,7 @@ impl<'tcx> MirPass<'tcx> for NormalizeArrayLen {
2423
}
2524

2625
fn normalize_array_len_calls<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
27-
let param_env = tcx.param_env_reveal_all_normalized(body.source.def_id());
28-
let borrowed_locals = borrowed_locals(body);
29-
let ssa = SsaLocals::new(tcx, param_env, body, &borrowed_locals);
26+
let ssa = SsaLocals::new(body);
3027

3128
let slice_lengths = compute_slice_length(tcx, &ssa, body);
3229
debug!(?slice_lengths);

compiler/rustc_mir_transform/src/ref_prop.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use rustc_index::IndexVec;
44
use rustc_middle::mir::visit::*;
55
use rustc_middle::mir::*;
66
use rustc_middle::ty::TyCtxt;
7-
use rustc_mir_dataflow::impls::{borrowed_locals, MaybeStorageDead};
7+
use rustc_mir_dataflow::impls::MaybeStorageDead;
88
use rustc_mir_dataflow::storage::always_storage_live_locals;
99
use rustc_mir_dataflow::Analysis;
1010

@@ -82,9 +82,7 @@ impl<'tcx> MirPass<'tcx> for ReferencePropagation {
8282
}
8383

8484
fn propagate_ssa<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
85-
let param_env = tcx.param_env_reveal_all_normalized(body.source.def_id());
86-
let borrowed_locals = borrowed_locals(body);
87-
let ssa = SsaLocals::new(tcx, param_env, body, &borrowed_locals);
85+
let ssa = SsaLocals::new(body);
8886

8987
let mut replacer = compute_replacement(tcx, body, &ssa);
9088
debug!(?replacer.targets, ?replacer.allowed_replacements, ?replacer.storage_to_remove);

compiler/rustc_mir_transform/src/ssa.rs

+30-19
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
1+
//! We denote as "SSA" the set of locals that verify the following properties:
2+
//! 1/ They are only assigned-to once, either as a function parameter, or in an assign statement;
3+
//! 2/ This single assignment dominates all uses;
4+
//!
5+
//! As a consequence of rule 2, we consider that borrowed locals are not SSA, even if they are
6+
//! `Freeze`, as we do not track that the assignment dominates all uses of the borrow.
7+
//!
8+
//! We say a local has a stable address if its address has SSA-like properties:
9+
//! 1/ It has a single `StorageLive` statement, or none at all (always-live);
10+
//! 2/ All its uses dominate this `StorageLive` statement.
11+
//!
12+
//! We do not discard borrowed locals from this analysis, as we cannot take their address' address.
13+
114
use either::Either;
215
use rustc_data_structures::graph::dominators::Dominators;
316
use rustc_index::bit_set::BitSet;
417
use rustc_index::{IndexSlice, IndexVec};
518
use rustc_middle::middle::resolve_bound_vars::Set1;
619
use rustc_middle::mir::visit::*;
720
use rustc_middle::mir::*;
8-
use rustc_middle::ty::{ParamEnv, TyCtxt};
921
use rustc_mir_dataflow::storage::always_storage_live_locals;
1022

1123
#[derive(Debug)]
@@ -62,12 +74,7 @@ impl SmallDominators {
6274
}
6375

6476
impl SsaLocals {
65-
pub fn new<'tcx>(
66-
tcx: TyCtxt<'tcx>,
67-
param_env: ParamEnv<'tcx>,
68-
body: &Body<'tcx>,
69-
borrowed_locals: &BitSet<Local>,
70-
) -> SsaLocals {
77+
pub fn new<'tcx>(body: &Body<'tcx>) -> SsaLocals {
7178
let assignment_order = Vec::with_capacity(body.local_decls.len());
7279

7380
let assignments = IndexVec::from_elem(Set1::Empty, &body.local_decls);
@@ -80,13 +87,8 @@ impl SsaLocals {
8087
let mut visitor =
8188
SsaVisitor { assignments, assignment_order, dominators, direct_uses, storage_live };
8289

83-
for (local, decl) in body.local_decls.iter_enumerated() {
84-
if matches!(body.local_kind(local), LocalKind::Arg) {
85-
visitor.assignments[local] = Set1::One(LocationExtended::Arg);
86-
}
87-
if borrowed_locals.contains(local) && !decl.ty.is_freeze(tcx, param_env) {
88-
visitor.assignments[local] = Set1::Many;
89-
}
90+
for local in body.args_iter() {
91+
visitor.assignments[local] = Set1::One(LocationExtended::Arg);
9092
}
9193

9294
for local in always_storage_live_locals(body).iter() {
@@ -237,6 +239,8 @@ struct SsaVisitor {
237239
impl<'tcx> Visitor<'tcx> for SsaVisitor {
238240
fn visit_local(&mut self, local: Local, ctxt: PlaceContext, loc: Location) {
239241
match ctxt {
242+
PlaceContext::MutatingUse(MutatingUseContext::Projection)
243+
| PlaceContext::NonMutatingUse(NonMutatingUseContext::Projection) => bug!(),
240244
PlaceContext::MutatingUse(MutatingUseContext::Store) => {
241245
self.assignments[local].insert(LocationExtended::Plain(loc));
242246
if let Set1::One(_) = self.assignments[local] {
@@ -246,13 +250,18 @@ impl<'tcx> Visitor<'tcx> for SsaVisitor {
246250
self.dominators.check_dominates(&mut self.storage_live[local], loc);
247251
}
248252
// Anything can happen with raw pointers, so remove them.
249-
PlaceContext::NonMutatingUse(NonMutatingUseContext::AddressOf)
253+
// We do not verify that all uses of the borrow dominate the assignment to `local`,
254+
// so we have to remove them too.
255+
PlaceContext::NonMutatingUse(
256+
NonMutatingUseContext::SharedBorrow
257+
| NonMutatingUseContext::ShallowBorrow
258+
| NonMutatingUseContext::UniqueBorrow
259+
| NonMutatingUseContext::AddressOf,
260+
)
250261
| PlaceContext::MutatingUse(_) => {
251262
self.assignments[local] = Set1::Many;
252263
self.dominators.check_dominates(&mut self.storage_live[local], loc);
253264
}
254-
// Immutable borrows are taken into account in `SsaLocals::new` by
255-
// removing non-freeze locals.
256265
PlaceContext::NonMutatingUse(_) => {
257266
self.dominators.check_dominates(&mut self.assignments[local], loc);
258267
self.dominators.check_dominates(&mut self.storage_live[local], loc);
@@ -270,15 +279,17 @@ impl<'tcx> Visitor<'tcx> for SsaVisitor {
270279
// Do not do anything for storage statements and debuginfo.
271280
if ctxt.is_use() {
272281
// Only change the context if it is a real use, not a "use" in debuginfo.
273-
let new_ctxt = PlaceContext::NonMutatingUse(NonMutatingUseContext::Projection);
282+
let new_ctxt = PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy);
274283

275284
self.visit_projection(place.as_ref(), new_ctxt, loc);
276285
self.dominators.check_dominates(&mut self.assignments[place.local], loc);
277286
self.dominators.check_dominates(&mut self.storage_live[place.local], loc);
278287
}
279288
return;
289+
} else {
290+
self.visit_projection(place.as_ref(), ctxt, loc);
291+
self.visit_local(place.local, ctxt, loc);
280292
}
281-
self.super_place(place, ctxt, loc);
282293
}
283294
}
284295

tests/mir-opt/copy-prop/borrowed_local.f.CopyProp.diff

+1-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@
2020
}
2121

2222
bb1: {
23-
- _0 = opaque::<u8>(_3) -> bb2; // scope 0 at $DIR/borrowed_local.rs:+12:13: +12:38
24-
+ _0 = opaque::<u8>(_1) -> bb2; // scope 0 at $DIR/borrowed_local.rs:+12:13: +12:38
23+
_0 = opaque::<u8>(_3) -> bb2; // scope 0 at $DIR/borrowed_local.rs:+12:13: +12:38
2524
// mir::Constant
2625
// + span: $DIR/borrowed_local.rs:28:28: 28:34
2726
// + literal: Const { ty: fn(u8) -> bool {opaque::<u8>}, val: Value(<ZST>) }

tests/mir-opt/reference_prop.reference_propagation.ReferencePropagation.diff

+1-2
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,8 @@
178178
StorageLive(_17); // scope 9 at $DIR/reference_prop.rs:+22:13: +22:14
179179
_17 = &_16; // scope 9 at $DIR/reference_prop.rs:+22:17: +22:19
180180
StorageLive(_18); // scope 10 at $DIR/reference_prop.rs:+23:13: +23:14
181-
- _18 = (*_16); // scope 10 at $DIR/reference_prop.rs:+23:17: +23:19
181+
_18 = (*_16); // scope 10 at $DIR/reference_prop.rs:+23:17: +23:19
182182
- _14 = const (); // scope 0 at $DIR/reference_prop.rs:+19:5: +24:6
183-
+ _18 = _15; // scope 10 at $DIR/reference_prop.rs:+23:17: +23:19
184183
StorageDead(_18); // scope 10 at $DIR/reference_prop.rs:+24:5: +24:6
185184
StorageDead(_17); // scope 9 at $DIR/reference_prop.rs:+24:5: +24:6
186185
StorageDead(_16); // scope 8 at $DIR/reference_prop.rs:+24:5: +24:6

tests/mir-opt/reference_prop.reference_propagation_const_ptr.ReferencePropagation.diff

+1-2
Original file line numberDiff line numberDiff line change
@@ -207,9 +207,8 @@
207207
StorageLive(_16); // scope 12 at $DIR/reference_prop.rs:+22:13: +22:14
208208
_16 = &_15; // scope 12 at $DIR/reference_prop.rs:+22:17: +22:19
209209
StorageLive(_17); // scope 13 at $DIR/reference_prop.rs:+23:13: +23:14
210-
- _17 = (*_15); // scope 13 at $DIR/reference_prop.rs:+23:17: +23:19
210+
_17 = (*_15); // scope 13 at $DIR/reference_prop.rs:+23:17: +23:19
211211
- _13 = const (); // scope 10 at $DIR/reference_prop.rs:+19:5: +24:6
212-
+ _17 = _14; // scope 13 at $DIR/reference_prop.rs:+23:17: +23:19
213212
StorageDead(_17); // scope 13 at $DIR/reference_prop.rs:+24:5: +24:6
214213
StorageDead(_16); // scope 12 at $DIR/reference_prop.rs:+24:5: +24:6
215214
StorageDead(_15); // scope 11 at $DIR/reference_prop.rs:+24:5: +24:6

tests/mir-opt/slice_filter.variant_a-{closure#0}.CopyProp.diff

+8-8
Original file line numberDiff line numberDiff line change
@@ -101,16 +101,16 @@
101101
}
102102

103103
bb0: {
104-
- StorageLive(_3); // scope 0 at $DIR/slice_filter.rs:+0:27: +0:28
104+
StorageLive(_3); // scope 0 at $DIR/slice_filter.rs:+0:27: +0:28
105105
_25 = deref_copy (*_2); // scope 0 at $DIR/slice_filter.rs:+0:27: +0:28
106106
_3 = &((*_25).0: usize); // scope 0 at $DIR/slice_filter.rs:+0:27: +0:28
107-
- StorageLive(_4); // scope 0 at $DIR/slice_filter.rs:+0:30: +0:31
107+
StorageLive(_4); // scope 0 at $DIR/slice_filter.rs:+0:30: +0:31
108108
_26 = deref_copy (*_2); // scope 0 at $DIR/slice_filter.rs:+0:30: +0:31
109109
_4 = &((*_26).1: usize); // scope 0 at $DIR/slice_filter.rs:+0:30: +0:31
110-
- StorageLive(_5); // scope 0 at $DIR/slice_filter.rs:+0:33: +0:34
110+
StorageLive(_5); // scope 0 at $DIR/slice_filter.rs:+0:33: +0:34
111111
_27 = deref_copy (*_2); // scope 0 at $DIR/slice_filter.rs:+0:33: +0:34
112112
_5 = &((*_27).2: usize); // scope 0 at $DIR/slice_filter.rs:+0:33: +0:34
113-
- StorageLive(_6); // scope 0 at $DIR/slice_filter.rs:+0:36: +0:37
113+
StorageLive(_6); // scope 0 at $DIR/slice_filter.rs:+0:36: +0:37
114114
_28 = deref_copy (*_2); // scope 0 at $DIR/slice_filter.rs:+0:36: +0:37
115115
_6 = &((*_28).3: usize); // scope 0 at $DIR/slice_filter.rs:+0:36: +0:37
116116
StorageLive(_7); // scope 1 at $DIR/slice_filter.rs:+0:40: +0:56
@@ -184,10 +184,10 @@
184184
bb3: {
185185
StorageDead(_16); // scope 1 at $DIR/slice_filter.rs:+0:75: +0:76
186186
StorageDead(_7); // scope 1 at $DIR/slice_filter.rs:+0:75: +0:76
187-
- StorageDead(_6); // scope 0 at $DIR/slice_filter.rs:+0:75: +0:76
188-
- StorageDead(_5); // scope 0 at $DIR/slice_filter.rs:+0:75: +0:76
189-
- StorageDead(_4); // scope 0 at $DIR/slice_filter.rs:+0:75: +0:76
190-
- StorageDead(_3); // scope 0 at $DIR/slice_filter.rs:+0:75: +0:76
187+
StorageDead(_6); // scope 0 at $DIR/slice_filter.rs:+0:75: +0:76
188+
StorageDead(_5); // scope 0 at $DIR/slice_filter.rs:+0:75: +0:76
189+
StorageDead(_4); // scope 0 at $DIR/slice_filter.rs:+0:75: +0:76
190+
StorageDead(_3); // scope 0 at $DIR/slice_filter.rs:+0:75: +0:76
191191
return; // scope 0 at $DIR/slice_filter.rs:+0:76: +0:76
192192
}
193193

tests/mir-opt/slice_filter.variant_a-{closure#0}.DestinationPropagation.diff

+16
Original file line numberDiff line numberDiff line change
@@ -89,15 +89,23 @@
8989
}
9090

9191
bb0: {
92+
- StorageLive(_3); // scope 0 at $DIR/slice_filter.rs:+0:27: +0:28
93+
+ nop; // scope 0 at $DIR/slice_filter.rs:+0:27: +0:28
9294
_25 = deref_copy (*_2); // scope 0 at $DIR/slice_filter.rs:+0:27: +0:28
9395
- _3 = &((*_25).0: usize); // scope 0 at $DIR/slice_filter.rs:+0:27: +0:28
96+
- StorageLive(_4); // scope 0 at $DIR/slice_filter.rs:+0:30: +0:31
9497
+ _20 = &((*_25).0: usize); // scope 0 at $DIR/slice_filter.rs:+0:27: +0:28
98+
+ nop; // scope 0 at $DIR/slice_filter.rs:+0:30: +0:31
9599
_26 = deref_copy (*_2); // scope 0 at $DIR/slice_filter.rs:+0:30: +0:31
96100
- _4 = &((*_26).1: usize); // scope 0 at $DIR/slice_filter.rs:+0:30: +0:31
101+
- StorageLive(_5); // scope 0 at $DIR/slice_filter.rs:+0:33: +0:34
97102
+ _15 = &((*_26).1: usize); // scope 0 at $DIR/slice_filter.rs:+0:30: +0:31
103+
+ nop; // scope 0 at $DIR/slice_filter.rs:+0:33: +0:34
98104
_27 = deref_copy (*_2); // scope 0 at $DIR/slice_filter.rs:+0:33: +0:34
99105
- _5 = &((*_27).2: usize); // scope 0 at $DIR/slice_filter.rs:+0:33: +0:34
106+
- StorageLive(_6); // scope 0 at $DIR/slice_filter.rs:+0:36: +0:37
100107
+ _11 = &((*_27).2: usize); // scope 0 at $DIR/slice_filter.rs:+0:33: +0:34
108+
+ nop; // scope 0 at $DIR/slice_filter.rs:+0:36: +0:37
101109
_28 = deref_copy (*_2); // scope 0 at $DIR/slice_filter.rs:+0:36: +0:37
102110
- _6 = &((*_28).3: usize); // scope 0 at $DIR/slice_filter.rs:+0:36: +0:37
103111
- StorageLive(_7); // scope 1 at $DIR/slice_filter.rs:+0:40: +0:56
@@ -162,8 +170,16 @@
162170
bb3: {
163171
- StorageDead(_16); // scope 1 at $DIR/slice_filter.rs:+0:75: +0:76
164172
- StorageDead(_7); // scope 1 at $DIR/slice_filter.rs:+0:75: +0:76
173+
- StorageDead(_6); // scope 0 at $DIR/slice_filter.rs:+0:75: +0:76
174+
- StorageDead(_5); // scope 0 at $DIR/slice_filter.rs:+0:75: +0:76
175+
- StorageDead(_4); // scope 0 at $DIR/slice_filter.rs:+0:75: +0:76
176+
- StorageDead(_3); // scope 0 at $DIR/slice_filter.rs:+0:75: +0:76
165177
+ nop; // scope 1 at $DIR/slice_filter.rs:+0:75: +0:76
166178
+ nop; // scope 1 at $DIR/slice_filter.rs:+0:75: +0:76
179+
+ nop; // scope 0 at $DIR/slice_filter.rs:+0:75: +0:76
180+
+ nop; // scope 0 at $DIR/slice_filter.rs:+0:75: +0:76
181+
+ nop; // scope 0 at $DIR/slice_filter.rs:+0:75: +0:76
182+
+ nop; // scope 0 at $DIR/slice_filter.rs:+0:75: +0:76
167183
return; // scope 0 at $DIR/slice_filter.rs:+0:76: +0:76
168184
}
169185

tests/mir-opt/slice_filter.variant_a-{closure#0}.ReferencePropagation.diff

+8
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,16 @@
9393
}
9494

9595
bb0: {
96+
StorageLive(_3); // scope 0 at $DIR/slice_filter.rs:+0:27: +0:28
9697
_25 = deref_copy (*_2); // scope 0 at $DIR/slice_filter.rs:+0:27: +0:28
9798
_3 = &((*_25).0: usize); // scope 0 at $DIR/slice_filter.rs:+0:27: +0:28
99+
StorageLive(_4); // scope 0 at $DIR/slice_filter.rs:+0:30: +0:31
98100
_26 = deref_copy (*_2); // scope 0 at $DIR/slice_filter.rs:+0:30: +0:31
99101
_4 = &((*_26).1: usize); // scope 0 at $DIR/slice_filter.rs:+0:30: +0:31
102+
StorageLive(_5); // scope 0 at $DIR/slice_filter.rs:+0:33: +0:34
100103
_27 = deref_copy (*_2); // scope 0 at $DIR/slice_filter.rs:+0:33: +0:34
101104
_5 = &((*_27).2: usize); // scope 0 at $DIR/slice_filter.rs:+0:33: +0:34
105+
StorageLive(_6); // scope 0 at $DIR/slice_filter.rs:+0:36: +0:37
102106
_28 = deref_copy (*_2); // scope 0 at $DIR/slice_filter.rs:+0:36: +0:37
103107
_6 = &((*_28).3: usize); // scope 0 at $DIR/slice_filter.rs:+0:36: +0:37
104108
StorageLive(_7); // scope 1 at $DIR/slice_filter.rs:+0:40: +0:56
@@ -160,6 +164,10 @@
160164
bb3: {
161165
StorageDead(_16); // scope 1 at $DIR/slice_filter.rs:+0:75: +0:76
162166
StorageDead(_7); // scope 1 at $DIR/slice_filter.rs:+0:75: +0:76
167+
StorageDead(_6); // scope 0 at $DIR/slice_filter.rs:+0:75: +0:76
168+
StorageDead(_5); // scope 0 at $DIR/slice_filter.rs:+0:75: +0:76
169+
StorageDead(_4); // scope 0 at $DIR/slice_filter.rs:+0:75: +0:76
170+
StorageDead(_3); // scope 0 at $DIR/slice_filter.rs:+0:75: +0:76
163171
return; // scope 0 at $DIR/slice_filter.rs:+0:76: +0:76
164172
}
165173

0 commit comments

Comments
 (0)