Skip to content

Commit 9f14698

Browse files
committed
extract polonius "constraint generation"
to help review, this duplicates the existing NLL + polonius constraint generation component, before splitting them up to only do what they individually need.
1 parent 4901083 commit 9f14698

File tree

3 files changed

+280
-2
lines changed

3 files changed

+280
-2
lines changed

compiler/rustc_borrowck/src/nll.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,14 @@ pub(crate) fn compute_regions<'cx, 'tcx>(
157157
body,
158158
borrow_set,
159159
);
160+
polonius::emit_cfg_and_loan_kills_facts(
161+
infcx,
162+
&mut liveness_constraints,
163+
&mut all_facts,
164+
location_table,
165+
body,
166+
borrow_set,
167+
);
160168

161169
let mut regioncx = RegionInferenceContext::new(
162170
infcx,
Lines changed: 248 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,248 @@
1+
#![deny(rustc::untranslatable_diagnostic)]
2+
#![deny(rustc::diagnostic_outside_of_impl)]
3+
use rustc_infer::infer::InferCtxt;
4+
use rustc_middle::mir::visit::TyContext;
5+
use rustc_middle::mir::visit::Visitor;
6+
use rustc_middle::mir::{
7+
Body, Local, Location, Place, PlaceRef, ProjectionElem, Rvalue, SourceInfo, Statement,
8+
StatementKind, Terminator, TerminatorKind, UserTypeProjection,
9+
};
10+
use rustc_middle::ty::visit::TypeVisitable;
11+
use rustc_middle::ty::GenericArgsRef;
12+
use rustc_middle::ty::{self, RegionVid, Ty, TyCtxt};
13+
14+
use crate::{
15+
borrow_set::BorrowSet, facts::AllFacts, location::LocationTable, places_conflict,
16+
region_infer::values::LivenessValues,
17+
};
18+
19+
pub(super) fn generate_constraints<'tcx>(
20+
infcx: &InferCtxt<'tcx>,
21+
liveness_constraints: &mut LivenessValues<RegionVid>,
22+
all_facts: &mut Option<AllFacts>,
23+
location_table: &LocationTable,
24+
body: &Body<'tcx>,
25+
borrow_set: &BorrowSet<'tcx>,
26+
) {
27+
let mut cg = ConstraintGeneration {
28+
borrow_set,
29+
infcx,
30+
liveness_constraints,
31+
location_table,
32+
all_facts,
33+
body,
34+
};
35+
36+
for (bb, data) in body.basic_blocks.iter_enumerated() {
37+
cg.visit_basic_block_data(bb, data);
38+
}
39+
}
40+
41+
/// 'cg = the duration of the constraint generation process itself.
42+
struct ConstraintGeneration<'cg, 'tcx> {
43+
infcx: &'cg InferCtxt<'tcx>,
44+
all_facts: &'cg mut Option<AllFacts>,
45+
location_table: &'cg LocationTable,
46+
liveness_constraints: &'cg mut LivenessValues<RegionVid>,
47+
borrow_set: &'cg BorrowSet<'tcx>,
48+
body: &'cg Body<'tcx>,
49+
}
50+
51+
impl<'cg, 'tcx> Visitor<'tcx> for ConstraintGeneration<'cg, 'tcx> {
52+
/// We sometimes have `args` within an rvalue, or within a
53+
/// call. Make them live at the location where they appear.
54+
fn visit_args(&mut self, args: &GenericArgsRef<'tcx>, location: Location) {
55+
self.add_regular_live_constraint(*args, location);
56+
self.super_args(args);
57+
}
58+
59+
/// We sometimes have `region` within an rvalue, or within a
60+
/// call. Make them live at the location where they appear.
61+
fn visit_region(&mut self, region: ty::Region<'tcx>, location: Location) {
62+
self.add_regular_live_constraint(region, location);
63+
self.super_region(region);
64+
}
65+
66+
/// We sometimes have `ty` within an rvalue, or within a
67+
/// call. Make them live at the location where they appear.
68+
fn visit_ty(&mut self, ty: Ty<'tcx>, ty_context: TyContext) {
69+
match ty_context {
70+
TyContext::ReturnTy(SourceInfo { span, .. })
71+
| TyContext::YieldTy(SourceInfo { span, .. })
72+
| TyContext::UserTy(span)
73+
| TyContext::LocalDecl { source_info: SourceInfo { span, .. }, .. } => {
74+
span_bug!(span, "should not be visiting outside of the CFG: {:?}", ty_context);
75+
}
76+
TyContext::Location(location) => {
77+
self.add_regular_live_constraint(ty, location);
78+
}
79+
}
80+
81+
self.super_ty(ty);
82+
}
83+
84+
fn visit_statement(&mut self, statement: &Statement<'tcx>, location: Location) {
85+
if let Some(all_facts) = self.all_facts {
86+
let _prof_timer = self.infcx.tcx.prof.generic_activity("polonius_fact_generation");
87+
all_facts.cfg_edge.push((
88+
self.location_table.start_index(location),
89+
self.location_table.mid_index(location),
90+
));
91+
92+
all_facts.cfg_edge.push((
93+
self.location_table.mid_index(location),
94+
self.location_table.start_index(location.successor_within_block()),
95+
));
96+
97+
// If there are borrows on this now dead local, we need to record them as `killed`.
98+
if let StatementKind::StorageDead(local) = statement.kind {
99+
record_killed_borrows_for_local(
100+
all_facts,
101+
self.borrow_set,
102+
self.location_table,
103+
local,
104+
location,
105+
);
106+
}
107+
}
108+
109+
self.super_statement(statement, location);
110+
}
111+
112+
fn visit_assign(&mut self, place: &Place<'tcx>, rvalue: &Rvalue<'tcx>, location: Location) {
113+
// When we see `X = ...`, then kill borrows of
114+
// `(*X).foo` and so forth.
115+
self.record_killed_borrows_for_place(*place, location);
116+
117+
self.super_assign(place, rvalue, location);
118+
}
119+
120+
fn visit_terminator(&mut self, terminator: &Terminator<'tcx>, location: Location) {
121+
if let Some(all_facts) = self.all_facts {
122+
let _prof_timer = self.infcx.tcx.prof.generic_activity("polonius_fact_generation");
123+
all_facts.cfg_edge.push((
124+
self.location_table.start_index(location),
125+
self.location_table.mid_index(location),
126+
));
127+
128+
let successor_blocks = terminator.successors();
129+
all_facts.cfg_edge.reserve(successor_blocks.size_hint().0);
130+
for successor_block in successor_blocks {
131+
all_facts.cfg_edge.push((
132+
self.location_table.mid_index(location),
133+
self.location_table.start_index(successor_block.start_location()),
134+
));
135+
}
136+
}
137+
138+
// A `Call` terminator's return value can be a local which has borrows,
139+
// so we need to record those as `killed` as well.
140+
if let TerminatorKind::Call { destination, .. } = terminator.kind {
141+
self.record_killed_borrows_for_place(destination, location);
142+
}
143+
144+
self.super_terminator(terminator, location);
145+
}
146+
147+
fn visit_ascribe_user_ty(
148+
&mut self,
149+
_place: &Place<'tcx>,
150+
_variance: ty::Variance,
151+
_user_ty: &UserTypeProjection,
152+
_location: Location,
153+
) {
154+
}
155+
}
156+
157+
impl<'cx, 'tcx> ConstraintGeneration<'cx, 'tcx> {
158+
/// Some variable with type `live_ty` is "regular live" at
159+
/// `location` -- i.e., it may be used later. This means that all
160+
/// regions appearing in the type `live_ty` must be live at
161+
/// `location`.
162+
fn add_regular_live_constraint<T>(&mut self, live_ty: T, location: Location)
163+
where
164+
T: TypeVisitable<TyCtxt<'tcx>>,
165+
{
166+
debug!("add_regular_live_constraint(live_ty={:?}, location={:?})", live_ty, location);
167+
168+
self.infcx.tcx.for_each_free_region(&live_ty, |live_region| {
169+
let vid = live_region.as_var();
170+
self.liveness_constraints.add_element(vid, location);
171+
});
172+
}
173+
174+
/// When recording facts for Polonius, records the borrows on the specified place
175+
/// as `killed`. For example, when assigning to a local, or on a call's return destination.
176+
fn record_killed_borrows_for_place(&mut self, place: Place<'tcx>, location: Location) {
177+
if let Some(all_facts) = self.all_facts {
178+
let _prof_timer = self.infcx.tcx.prof.generic_activity("polonius_fact_generation");
179+
180+
// Depending on the `Place` we're killing:
181+
// - if it's a local, or a single deref of a local,
182+
// we kill all the borrows on the local.
183+
// - if it's a deeper projection, we have to filter which
184+
// of the borrows are killed: the ones whose `borrowed_place`
185+
// conflicts with the `place`.
186+
match place.as_ref() {
187+
PlaceRef { local, projection: &[] }
188+
| PlaceRef { local, projection: &[ProjectionElem::Deref] } => {
189+
debug!(
190+
"Recording `killed` facts for borrows of local={:?} at location={:?}",
191+
local, location
192+
);
193+
194+
record_killed_borrows_for_local(
195+
all_facts,
196+
self.borrow_set,
197+
self.location_table,
198+
local,
199+
location,
200+
);
201+
}
202+
203+
PlaceRef { local, projection: &[.., _] } => {
204+
// Kill conflicting borrows of the innermost local.
205+
debug!(
206+
"Recording `killed` facts for borrows of \
207+
innermost projected local={:?} at location={:?}",
208+
local, location
209+
);
210+
211+
if let Some(borrow_indices) = self.borrow_set.local_map.get(&local) {
212+
for &borrow_index in borrow_indices {
213+
let places_conflict = places_conflict::places_conflict(
214+
self.infcx.tcx,
215+
self.body,
216+
self.borrow_set[borrow_index].borrowed_place,
217+
place,
218+
places_conflict::PlaceConflictBias::NoOverlap,
219+
);
220+
221+
if places_conflict {
222+
let location_index = self.location_table.mid_index(location);
223+
all_facts.loan_killed_at.push((borrow_index, location_index));
224+
}
225+
}
226+
}
227+
}
228+
}
229+
}
230+
}
231+
}
232+
233+
/// When recording facts for Polonius, records the borrows on the specified local as `killed`.
234+
fn record_killed_borrows_for_local(
235+
all_facts: &mut AllFacts,
236+
borrow_set: &BorrowSet<'_>,
237+
location_table: &LocationTable,
238+
local: Local,
239+
location: Location,
240+
) {
241+
if let Some(borrow_indices) = borrow_set.local_map.get(&local) {
242+
all_facts.loan_killed_at.reserve(borrow_indices.len());
243+
for &borrow_index in borrow_indices {
244+
let location_index = location_table.mid_index(location);
245+
all_facts.loan_killed_at.push((borrow_index, location_index));
246+
}
247+
}
248+
}

compiler/rustc_borrowck/src/polonius/mod.rs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,19 @@
33
//! Will be removed in the future, once the in-tree `-Zpolonius=next` implementation reaches feature
44
//! parity.
55
6+
use rustc_infer::infer::InferCtxt;
67
use rustc_middle::mir::{Body, LocalKind, Location, START_BLOCK};
7-
use rustc_middle::ty::TyCtxt;
8+
use rustc_middle::ty::{RegionVid, TyCtxt};
89
use rustc_mir_dataflow::move_paths::{InitKind, InitLocation, MoveData};
910

1011
use crate::borrow_set::BorrowSet;
1112
use crate::facts::AllFacts;
1213
use crate::location::LocationTable;
14+
use crate::region_infer::values::LivenessValues;
1315
use crate::type_check::free_region_relations::UniversalRegionRelations;
1416
use crate::universal_regions::UniversalRegions;
1517

18+
mod constraint_generation;
1619
mod invalidation;
1720

1821
/// Emit facts needed for move/init analysis: moves and assignments.
@@ -130,7 +133,7 @@ pub(crate) fn emit_universal_region_facts(
130133
}
131134
}
132135

133-
/// Emit facts about loan invalidations
136+
/// Emit facts about loan invalidations.
134137
pub(crate) fn emit_loan_invalidations_facts<'tcx>(
135138
tcx: TyCtxt<'tcx>,
136139
all_facts: &mut Option<AllFacts>,
@@ -145,3 +148,22 @@ pub(crate) fn emit_loan_invalidations_facts<'tcx>(
145148

146149
invalidation::emit_loan_invalidations(tcx, all_facts, location_table, body, borrow_set);
147150
}
151+
152+
/// Emit facts about CFG points and edges, as well as locations where loans are killed.
153+
pub(crate) fn emit_cfg_and_loan_kills_facts<'tcx>(
154+
infcx: &InferCtxt<'tcx>,
155+
liveness_constraints: &mut LivenessValues<RegionVid>,
156+
all_facts: &mut Option<AllFacts>,
157+
location_table: &LocationTable,
158+
body: &Body<'tcx>,
159+
borrow_set: &BorrowSet<'tcx>,
160+
) {
161+
constraint_generation::generate_constraints(
162+
infcx,
163+
liveness_constraints,
164+
all_facts,
165+
location_table,
166+
body,
167+
borrow_set,
168+
);
169+
}

0 commit comments

Comments
 (0)