Skip to content

Commit

Permalink
compute liveness later
Browse files Browse the repository at this point in the history
  • Loading branch information
nikomatsakis committed Aug 7, 2018
1 parent ccb550f commit fb1702f
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 40 deletions.
22 changes: 12 additions & 10 deletions src/librustc_mir/borrow_check/nll/mod.rs
Expand Up @@ -11,7 +11,7 @@
use borrow_check::borrow_set::BorrowSet;
use borrow_check::location::{LocationIndex, LocationTable};
use borrow_check::nll::facts::AllFactsExt;
use borrow_check::nll::type_check::MirTypeckRegionConstraints;
use borrow_check::nll::type_check::{MirTypeckResults, MirTypeckRegionConstraints};
use borrow_check::nll::region_infer::values::RegionValueElements;
use borrow_check::nll::liveness_map::{NllLivenessMap, LocalWithRegion};
use dataflow::indexes::BorrowIndex;
Expand Down Expand Up @@ -109,17 +109,19 @@ pub(in borrow_check) fn compute_regions<'cx, 'gcx, 'tcx>(
let elements = &Rc::new(RegionValueElements::new(mir));

// Run the MIR type-checker.
let liveness_map = NllLivenessMap::compute(&mir);
let liveness = LivenessResults::compute(mir, &liveness_map);
let (constraint_sets, universal_region_relations) = type_check::type_check(
let MirTypeckResults {
constraints,
universal_region_relations,
liveness,
liveness_map,
} = type_check::type_check(
infcx,
param_env,
mir,
def_id,
&universal_regions,
location_table,
borrow_set,
&liveness,
&mut all_facts,
flow_inits,
move_data,
Expand All @@ -141,7 +143,7 @@ pub(in borrow_check) fn compute_regions<'cx, 'gcx, 'tcx>(
mut liveness_constraints,
outlives_constraints,
type_tests,
} = constraint_sets;
} = constraints;

constraint_generation::generate_constraints(
infcx,
Expand Down Expand Up @@ -205,6 +207,7 @@ pub(in borrow_check) fn compute_regions<'cx, 'gcx, 'tcx>(
dump_mir_results(
infcx,
&liveness,
&liveness_map,
MirSource::item(def_id),
&mir,
&regioncx,
Expand All @@ -221,6 +224,7 @@ pub(in borrow_check) fn compute_regions<'cx, 'gcx, 'tcx>(
fn dump_mir_results<'a, 'gcx, 'tcx>(
infcx: &InferCtxt<'a, 'gcx, 'tcx>,
liveness: &LivenessResults<LocalWithRegion>,
liveness_map: &NllLivenessMap,
source: MirSource,
mir: &Mir<'tcx>,
regioncx: &RegionInferenceContext,
Expand All @@ -230,16 +234,14 @@ fn dump_mir_results<'a, 'gcx, 'tcx>(
return;
}

let map = &NllLivenessMap::compute(mir);

let regular_liveness_per_location: FxHashMap<_, _> = mir
.basic_blocks()
.indices()
.flat_map(|bb| {
let mut results = vec![];
liveness
.regular
.simulate_block(&mir, bb, map, |location, local_set| {
.simulate_block(&mir, bb, liveness_map, |location, local_set| {
results.push((location, local_set.clone()));
});
results
Expand All @@ -253,7 +255,7 @@ fn dump_mir_results<'a, 'gcx, 'tcx>(
let mut results = vec![];
liveness
.drop
.simulate_block(&mir, bb, map, |location, local_set| {
.simulate_block(&mir, bb, liveness_map, |location, local_set| {
results.push((location, local_set.clone()));
});
results
Expand Down
34 changes: 20 additions & 14 deletions src/librustc_mir/borrow_check/nll/type_check/liveness.rs
Expand Up @@ -36,23 +36,29 @@ use super::TypeChecker;
pub(super) fn generate<'gcx, 'tcx>(
cx: &mut TypeChecker<'_, 'gcx, 'tcx>,
mir: &Mir<'tcx>,
liveness: &LivenessResults<LocalWithRegion>,
flow_inits: &mut FlowAtLocation<MaybeInitializedPlaces<'_, 'gcx, 'tcx>>,
move_data: &MoveData<'tcx>,
) {
let mut generator = TypeLivenessGenerator {
cx,
mir,
liveness,
flow_inits,
move_data,
drop_data: FxHashMap(),
map: &NllLivenessMap::compute(mir),
};

for bb in mir.basic_blocks().indices() {
generator.add_liveness_constraints(bb);
) -> (LivenessResults<LocalWithRegion>, NllLivenessMap) {
let liveness_map = NllLivenessMap::compute(&mir);
let liveness = LivenessResults::compute(mir, &liveness_map);

{
let mut generator = TypeLivenessGenerator {
cx,
mir,
liveness: &liveness,
flow_inits,
move_data,
drop_data: FxHashMap(),
map: &liveness_map,
};

for bb in mir.basic_blocks().indices() {
generator.add_liveness_constraints(bb);
}
}

(liveness, liveness_map)
}

struct TypeLivenessGenerator<'gen, 'typeck, 'flow, 'gcx, 'tcx>
Expand Down
39 changes: 23 additions & 16 deletions src/librustc_mir/borrow_check/nll/type_check/mod.rs
Expand Up @@ -15,6 +15,7 @@ use borrow_check::borrow_set::BorrowSet;
use borrow_check::location::LocationTable;
use borrow_check::nll::constraints::{ConstraintSet, OutlivesConstraint};
use borrow_check::nll::facts::AllFacts;
use borrow_check::nll::liveness_map::NllLivenessMap;
use borrow_check::nll::region_infer::values::{RegionValueElements, LivenessValues};
use borrow_check::nll::region_infer::{ClosureRegionRequirementsExt, TypeTest};
use borrow_check::nll::type_check::free_region_relations::{CreateResult, UniversalRegionRelations};
Expand Down Expand Up @@ -115,16 +116,12 @@ pub(crate) fn type_check<'gcx, 'tcx>(
universal_regions: &Rc<UniversalRegions<'tcx>>,
location_table: &LocationTable,
borrow_set: &BorrowSet<'tcx>,
liveness: &LivenessResults<LocalWithRegion>,
all_facts: &mut Option<AllFacts>,
flow_inits: &mut FlowAtLocation<MaybeInitializedPlaces<'_, 'gcx, 'tcx>>,
move_data: &MoveData<'tcx>,
elements: &Rc<RegionValueElements>,
errors_buffer: &mut Vec<Diagnostic>,
) -> (
MirTypeckRegionConstraints<'tcx>,
Rc<UniversalRegionRelations<'tcx>>,
) {
) -> MirTypeckResults<'tcx> {
let implicit_region_bound = infcx.tcx.mk_region(ty::ReVar(universal_regions.fr_fn_body));
let mut constraints = MirTypeckRegionConstraints {
liveness_constraints: LivenessValues::new(elements),
Expand All @@ -147,7 +144,7 @@ pub(crate) fn type_check<'gcx, 'tcx>(
all_facts,
);

{
let (liveness, liveness_map) = {
let mut borrowck_context = BorrowCheckContext {
universal_regions,
location_table,
Expand All @@ -166,22 +163,27 @@ pub(crate) fn type_check<'gcx, 'tcx>(
Some(&mut borrowck_context),
Some(errors_buffer),
|cx| {
liveness::generate(cx, mir, liveness, flow_inits, move_data);
cx.equate_inputs_and_outputs(
mir,
mir_def_id,
universal_regions,
&universal_region_relations,
&normalized_inputs_and_output,
);
liveness::generate(cx, mir, flow_inits, move_data)
},
);
}
)
};

(constraints, universal_region_relations)
MirTypeckResults {
constraints,
universal_region_relations,
liveness,
liveness_map,
}
}

fn type_check_internal<'a, 'gcx, 'tcx, F>(
fn type_check_internal<'a, 'gcx, 'tcx, R>(
infcx: &'a InferCtxt<'a, 'gcx, 'tcx>,
mir_def_id: DefId,
param_env: ty::ParamEnv<'gcx>,
Expand All @@ -190,10 +192,8 @@ fn type_check_internal<'a, 'gcx, 'tcx, F>(
implicit_region_bound: Option<ty::Region<'tcx>>,
borrowck_context: Option<&'a mut BorrowCheckContext<'a, 'tcx>>,
errors_buffer: Option<&mut Vec<Diagnostic>>,
mut extra: F,
) where
F: FnMut(&mut TypeChecker<'a, 'gcx, 'tcx>),
{
mut extra: impl FnMut(&mut TypeChecker<'a, 'gcx, 'tcx>) -> R,
) -> R where {
let mut checker = TypeChecker::new(
infcx,
mir,
Expand All @@ -214,7 +214,7 @@ fn type_check_internal<'a, 'gcx, 'tcx, F>(
checker.typeck_mir(mir, errors_buffer);
}

extra(&mut checker);
extra(&mut checker)
}

fn mirbug(tcx: TyCtxt, span: Span, msg: &str) {
Expand Down Expand Up @@ -655,6 +655,13 @@ struct BorrowCheckContext<'a, 'tcx: 'a> {
constraints: &'a mut MirTypeckRegionConstraints<'tcx>,
}

crate struct MirTypeckResults<'tcx> {
crate constraints: MirTypeckRegionConstraints<'tcx>,
crate universal_region_relations: Rc<UniversalRegionRelations<'tcx>>,
crate liveness: LivenessResults<LocalWithRegion>,
crate liveness_map: NllLivenessMap,
}

/// A collection of region constraints that must be satisfied for the
/// program to be considered well-typed.
crate struct MirTypeckRegionConstraints<'tcx> {
Expand Down

0 comments on commit fb1702f

Please sign in to comment.