Skip to content

Commit 43b69c2

Browse files
nikomatsakisDylan-DPC
authored andcommitted
make liveness generic over set of local variables
We used to hardcode that we wanted the liveness of *all* variables. This can now be configured by selecting an alternative index type V and providing a (partial) map from locals to that new type V.
1 parent 4b5f0ba commit 43b69c2

File tree

4 files changed

+170
-102
lines changed

4 files changed

+170
-102
lines changed

src/librustc_mir/borrow_check/nll/mod.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ use rustc::infer::InferCtxt;
2222
use rustc::mir::{ClosureOutlivesSubject, ClosureRegionRequirements, Mir, Local};
2323
use rustc::ty::{self, RegionKind, RegionVid};
2424
use rustc::util::nodemap::FxHashMap;
25-
use rustc_data_structures::indexed_vec::Idx;
2625
use std::collections::BTreeSet;
2726
use std::fmt::Debug;
2827
use std::env;
@@ -31,7 +30,7 @@ use std::path::PathBuf;
3130
use std::rc::Rc;
3231
use std::str::FromStr;
3332
use transform::MirSource;
34-
use util::liveness::{LivenessResults, LocalSet};
33+
use util::liveness::{IdentityMap, LivenessResults, LocalSet};
3534

3635
use self::mir_util::PassWhere;
3736
use polonius_engine::{Algorithm, Output};
@@ -104,7 +103,7 @@ pub(in borrow_check) fn compute_regions<'cx, 'gcx, 'tcx>(
104103
let elements = &Rc::new(RegionValueElements::new(mir, universal_regions.len()));
105104

106105
// Run the MIR type-checker.
107-
let liveness = &LivenessResults::compute(mir);
106+
let liveness = &LivenessResults::compute(mir, &IdentityMap::new(mir));
108107
let constraint_sets = type_check::type_check(
109108
infcx,
110109
param_env,
@@ -220,14 +219,16 @@ fn dump_mir_results<'a, 'gcx, 'tcx>(
220219
return;
221220
}
222221

222+
let map = &IdentityMap::new(mir);
223+
223224
let regular_liveness_per_location: FxHashMap<_, _> = mir
224225
.basic_blocks()
225226
.indices()
226227
.flat_map(|bb| {
227228
let mut results = vec![];
228229
liveness
229230
.regular
230-
.simulate_block(&mir, bb, |location, local_set| {
231+
.simulate_block(&mir, bb, map, |location, local_set| {
231232
results.push((location, local_set.clone()));
232233
});
233234
results
@@ -241,7 +242,7 @@ fn dump_mir_results<'a, 'gcx, 'tcx>(
241242
let mut results = vec![];
242243
liveness
243244
.drop
244-
.simulate_block(&mir, bb, |location, local_set| {
245+
.simulate_block(&mir, bb, map, |location, local_set| {
245246
results.push((location, local_set.clone()));
246247
});
247248
results

src/librustc_mir/borrow_check/nll/type_check/liveness.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,8 @@ use rustc::traits::query::type_op::outlives::DropckOutlives;
2020
use rustc::traits::query::type_op::TypeOp;
2121
use rustc::ty::{Ty, TypeFoldable};
2222
use rustc_data_structures::fx::FxHashMap;
23-
use rustc_data_structures::indexed_vec::Idx;
2423
use std::rc::Rc;
25-
use util::liveness::{LivenessResults, LiveVariableMap};
24+
use util::liveness::{IdentityMap, LivenessResults};
2625

2726
use super::TypeChecker;
2827

@@ -48,6 +47,7 @@ pub(super) fn generate<'gcx, 'tcx>(
4847
flow_inits,
4948
move_data,
5049
drop_data: FxHashMap(),
50+
map: &IdentityMap::new(mir),
5151
};
5252

5353
for bb in mir.basic_blocks().indices() {
@@ -61,22 +61,22 @@ where
6161
'flow: 'gen,
6262
'tcx: 'typeck + 'flow,
6363
'gcx: 'tcx,
64-
V: 'gen,
6564
{
6665
cx: &'gen mut TypeChecker<'typeck, 'gcx, 'tcx>,
6766
mir: &'gen Mir<'tcx>,
6867
liveness: &'gen LivenessResults<Local>,
6968
flow_inits: &'gen mut FlowAtLocation<MaybeInitializedPlaces<'flow, 'gcx, 'tcx>>,
7069
move_data: &'gen MoveData<'tcx>,
7170
drop_data: FxHashMap<Ty<'tcx>, DropData<'tcx>>,
71+
map: &'gen IdentityMap<'gen, 'tcx>,
7272
}
7373

7474
struct DropData<'tcx> {
7575
dropck_result: DropckOutlivesResult<'tcx>,
7676
region_constraint_data: Option<Rc<Vec<QueryRegionConstraint<'tcx>>>>,
7777
}
7878

79-
impl<'gen, 'typeck, 'flow, 'gcx, 'tcx, V:LiveVariableMap> TypeLivenessGenerator<'gen, 'typeck, 'flow, 'gcx, 'tcx, V> {
79+
impl<'gen, 'typeck, 'flow, 'gcx, 'tcx> TypeLivenessGenerator<'gen, 'typeck, 'flow, 'gcx, 'tcx> {
8080
/// Liveness constraints:
8181
///
8282
/// > If a variable V is live at point P, then all regions R in the type of V
@@ -86,17 +86,17 @@ impl<'gen, 'typeck, 'flow, 'gcx, 'tcx, V:LiveVariableMap> TypeLivenessGenerator<
8686

8787
self.liveness
8888
.regular
89-
.simulate_block(self.mir, bb, |location, live_locals| {
89+
.simulate_block(self.mir, bb, self.map, |location, live_locals| {
9090
for live_local in live_locals.iter() {
9191
let live_local_ty = self.mir.local_decls[live_local].ty;
9292
Self::push_type_live_constraint(&mut self.cx, live_local_ty, location);
9393
}
9494
});
9595

96-
let mut all_live_locals: Vec<(Location, Vec<V::LiveVar>)> = vec![];
96+
let mut all_live_locals: Vec<(Location, Vec<Local>)> = vec![];
9797
self.liveness
9898
.drop
99-
.simulate_block(self.mir, bb, |location, live_locals| {
99+
.simulate_block(self.mir, bb, self.map, |location, live_locals| {
100100
all_live_locals.push((location, live_locals.iter().collect()));
101101
});
102102
debug!(

src/librustc_mir/transform/generator.rs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ use rustc::mir::visit::{PlaceContext, Visitor, MutVisitor};
6666
use rustc::ty::{self, TyCtxt, AdtDef, Ty};
6767
use rustc::ty::subst::Substs;
6868
use util::dump_mir;
69-
use util::liveness::{self, LivenessMode};
69+
use util::liveness::{self, IdentityMap, LivenessMode};
7070
use rustc_data_structures::indexed_vec::Idx;
7171
use rustc_data_structures::indexed_set::IdxSetBuf;
7272
use std::collections::HashMap;
@@ -334,7 +334,7 @@ impl<'tcx> Visitor<'tcx> for StorageIgnored {
334334

335335
struct BorrowedLocals(liveness::LocalSet<Local>);
336336

337-
fn mark_as_borrowed<'tcx, V: Idx>(place: &Place<'tcx>, locals: &mut BorrowedLocals) {
337+
fn mark_as_borrowed<'tcx>(place: &Place<'tcx>, locals: &mut BorrowedLocals) {
338338
match *place {
339339
Place::Local(l) => { locals.0.add(&l); },
340340
Place::Static(..) => (),
@@ -397,11 +397,22 @@ fn locals_live_across_suspend_points<'a, 'tcx,>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
397397

398398
// Calculate the liveness of MIR locals ignoring borrows.
399399
let mut set = liveness::LocalSet::new_empty(mir.local_decls.len());
400-
let mut liveness = liveness::liveness_of_locals(mir, LivenessMode {
401-
include_regular_use: true,
402-
include_drops: true,
403-
});
404-
liveness::dump_mir(tcx, "generator_liveness", source, mir, &liveness);
400+
let mut liveness = liveness::liveness_of_locals(
401+
mir,
402+
LivenessMode {
403+
include_regular_use: true,
404+
include_drops: true,
405+
},
406+
&IdentityMap::new(mir),
407+
);
408+
liveness::dump_mir(
409+
tcx,
410+
"generator_liveness",
411+
source,
412+
mir,
413+
&IdentityMap::new(mir),
414+
&liveness,
415+
);
405416

406417
let mut storage_liveness_map = HashMap::new();
407418

0 commit comments

Comments
 (0)