1
+ use std:: collections:: { BTreeMap , BTreeSet } ;
2
+
1
3
use either:: Either ;
2
4
use rustc_data_structures:: fx:: { FxHashMap , FxHashSet , FxIndexSet } ;
3
- use rustc_middle:: mir:: Body ;
5
+ use rustc_middle:: mir:: visit:: Visitor ;
6
+ use rustc_middle:: mir:: {
7
+ Body , Local , Location , Place , Rvalue , Statement , StatementKind , Terminator , TerminatorKind ,
8
+ } ;
4
9
use rustc_middle:: ty:: { RegionVid , TyCtxt } ;
5
10
use rustc_mir_dataflow:: points:: PointIndex ;
6
11
7
12
use super :: { LiveLoans , LocalizedOutlivesConstraintSet } ;
8
- use crate :: BorrowSet ;
13
+ use crate :: dataflow :: BorrowIndex ;
9
14
use crate :: region_infer:: values:: LivenessValues ;
15
+ use crate :: { BorrowSet , PlaceConflictBias , places_conflict} ;
10
16
11
- /// With the full graph of constraints, we can compute loan reachability, and trace loan liveness
12
- /// throughout the CFG.
17
+ /// With the full graph of constraints, we can compute loan reachability, stop at kills, and trace
18
+ /// loan liveness throughout the CFG.
13
19
pub ( super ) fn compute_loan_liveness < ' tcx > (
14
- _tcx : TyCtxt < ' tcx > ,
15
- _body : & Body < ' tcx > ,
20
+ tcx : TyCtxt < ' tcx > ,
21
+ body : & Body < ' tcx > ,
16
22
liveness : & LivenessValues ,
17
23
borrow_set : & BorrowSet < ' tcx > ,
18
24
localized_outlives_constraints : & LocalizedOutlivesConstraintSet ,
19
25
) -> LiveLoans {
20
26
let mut live_loans = LiveLoans :: new ( borrow_set. len ( ) ) ;
27
+
28
+ // FIXME: it may be preferable for kills to be encoded in the edges themselves, to simplify and
29
+ // likely make traversal (and constraint generation) more efficient. We also display kills on
30
+ // edges when visualizing the constraint graph anyways.
31
+ let kills = collect_kills ( body, tcx, borrow_set) ;
32
+
21
33
let graph = index_constraints ( & localized_outlives_constraints) ;
22
34
let mut visited = FxHashSet :: default ( ) ;
23
35
let mut stack = Vec :: new ( ) ;
@@ -42,7 +54,16 @@ pub(super) fn compute_loan_liveness<'tcx>(
42
54
// Record the loan as being live on entry to this point.
43
55
live_loans. insert ( node. point , loan_idx) ;
44
56
57
+ // Continuing traversal will depend on whether the loan is killed at this point.
58
+ let current_location = liveness. location_from_point ( node. point ) ;
59
+ let is_loan_killed =
60
+ kills. get ( & current_location) . is_some_and ( |kills| kills. contains ( & loan_idx) ) ;
61
+
45
62
for succ in outgoing_edges ( & graph, node) {
63
+ // If the loan is killed at this point, it is killed _on exit_.
64
+ if is_loan_killed {
65
+ continue ;
66
+ }
46
67
stack. push ( succ) ;
47
68
}
48
69
}
@@ -62,7 +83,7 @@ struct LocalizedNode {
62
83
point : PointIndex ,
63
84
}
64
85
65
- /// Index the outlives constraints into a graph of edges per node.
86
+ /// Traverses the constraints and returns the indexable graph of edges per node.
66
87
fn index_constraints ( constraints : & LocalizedOutlivesConstraintSet ) -> LocalizedConstraintGraph {
67
88
let mut edges = LocalizedConstraintGraph :: default ( ) ;
68
89
for constraint in & constraints. outlives {
@@ -85,3 +106,101 @@ fn outgoing_edges(
85
106
Either :: Right ( std:: iter:: empty ( ) )
86
107
}
87
108
}
109
+
110
+ /// Traverses the MIR and collects kills.
111
+ fn collect_kills < ' tcx > (
112
+ body : & Body < ' tcx > ,
113
+ tcx : TyCtxt < ' tcx > ,
114
+ borrow_set : & BorrowSet < ' tcx > ,
115
+ ) -> BTreeMap < Location , BTreeSet < BorrowIndex > > {
116
+ let mut collector = KillsCollector { borrow_set, tcx, body, kills : BTreeMap :: default ( ) } ;
117
+ for ( block, data) in body. basic_blocks . iter_enumerated ( ) {
118
+ collector. visit_basic_block_data ( block, data) ;
119
+ }
120
+ collector. kills
121
+ }
122
+
123
+ struct KillsCollector < ' a , ' tcx > {
124
+ body : & ' a Body < ' tcx > ,
125
+ tcx : TyCtxt < ' tcx > ,
126
+ borrow_set : & ' a BorrowSet < ' tcx > ,
127
+
128
+ /// The set of loans killed at each location.
129
+ kills : BTreeMap < Location , BTreeSet < BorrowIndex > > ,
130
+ }
131
+
132
+ // This visitor has a similar structure to the `Borrows` dataflow computation with respect to kills,
133
+ // and the datalog polonius fact generation for the `loan_killed_at` relation.
134
+ impl < ' tcx > KillsCollector < ' _ , ' tcx > {
135
+ /// Records the borrows on the specified place as `killed`. For example, when assigning to a
136
+ /// local, or on a call's return destination.
137
+ fn record_killed_borrows_for_place ( & mut self , place : Place < ' tcx > , location : Location ) {
138
+ let other_borrows_of_local = self
139
+ . borrow_set
140
+ . local_map
141
+ . get ( & place. local )
142
+ . into_iter ( )
143
+ . flat_map ( |bs| bs. iter ( ) )
144
+ . copied ( ) ;
145
+
146
+ // If the borrowed place is a local with no projections, all other borrows of this
147
+ // local must conflict. This is purely an optimization so we don't have to call
148
+ // `places_conflict` for every borrow.
149
+ if place. projection . is_empty ( ) {
150
+ if !self . body . local_decls [ place. local ] . is_ref_to_static ( ) {
151
+ self . kills . entry ( location) . or_default ( ) . extend ( other_borrows_of_local) ;
152
+ }
153
+ return ;
154
+ }
155
+
156
+ // By passing `PlaceConflictBias::NoOverlap`, we conservatively assume that any given
157
+ // pair of array indices are not equal, so that when `places_conflict` returns true, we
158
+ // will be assured that two places being compared definitely denotes the same sets of
159
+ // locations.
160
+ let definitely_conflicting_borrows = other_borrows_of_local. filter ( |& i| {
161
+ places_conflict (
162
+ self . tcx ,
163
+ self . body ,
164
+ self . borrow_set [ i] . borrowed_place ,
165
+ place,
166
+ PlaceConflictBias :: NoOverlap ,
167
+ )
168
+ } ) ;
169
+
170
+ self . kills . entry ( location) . or_default ( ) . extend ( definitely_conflicting_borrows) ;
171
+ }
172
+
173
+ /// Records the borrows on the specified local as `killed`.
174
+ fn record_killed_borrows_for_local ( & mut self , local : Local , location : Location ) {
175
+ if let Some ( borrow_indices) = self . borrow_set . local_map . get ( & local) {
176
+ self . kills . entry ( location) . or_default ( ) . extend ( borrow_indices. iter ( ) ) ;
177
+ }
178
+ }
179
+ }
180
+
181
+ impl < ' tcx > Visitor < ' tcx > for KillsCollector < ' _ , ' tcx > {
182
+ fn visit_statement ( & mut self , statement : & Statement < ' tcx > , location : Location ) {
183
+ // Make sure there are no remaining borrows for locals that have gone out of scope.
184
+ if let StatementKind :: StorageDead ( local) = statement. kind {
185
+ self . record_killed_borrows_for_local ( local, location) ;
186
+ }
187
+
188
+ self . super_statement ( statement, location) ;
189
+ }
190
+
191
+ fn visit_assign ( & mut self , place : & Place < ' tcx > , rvalue : & Rvalue < ' tcx > , location : Location ) {
192
+ // When we see `X = ...`, then kill borrows of `(*X).foo` and so forth.
193
+ self . record_killed_borrows_for_place ( * place, location) ;
194
+ self . super_assign ( place, rvalue, location) ;
195
+ }
196
+
197
+ fn visit_terminator ( & mut self , terminator : & Terminator < ' tcx > , location : Location ) {
198
+ // A `Call` terminator's return value can be a local which has borrows, so we need to record
199
+ // those as killed as well.
200
+ if let TerminatorKind :: Call { destination, .. } = terminator. kind {
201
+ self . record_killed_borrows_for_place ( destination, location) ;
202
+ }
203
+
204
+ self . super_terminator ( terminator, location) ;
205
+ }
206
+ }
0 commit comments