2018-04-06 15:18:01 -04:00
|
|
|
use borrow_check::borrow_set::{BorrowSet, BorrowData};
|
2018-04-06 15:53:49 -04:00
|
|
|
use borrow_check::place_ext::PlaceExt;
|
2018-04-06 15:18:01 -04:00
|
|
|
|
New `ActiveBorrows` dataflow for two-phase `&mut`; not yet borrowed-checked.
High-level picture: The old `Borrows` analysis is now called
`Reservations` (implemented as a newtype wrapper around `Borrows`);
this continues to compute whether a `Rvalue::Ref` can reach a
statement without an intervening `EndRegion`. In addition, we also
track what `Place` each such `Rvalue::Ref` was immediately assigned
to in a given borrow (yay for MIR-structural properties!).
The new `ActiveBorrows` analysis then tracks the initial use of any of
those assigned `Places` for a given borrow. I.e. a borrow becomes
"active" immediately after it starts being "used" in some way. (This
is conservative in the sense that we will treat a copy `x = y;` as a
use of `y`; in principle one might further delay activation in such
cases.)
The new `ActiveBorrows` analysis needs to take the `Reservations`
results as an initial input, because the reservation state influences
the gen/kill sets for `ActiveBorrows`. In particular, a use of `a`
activates a borrow `a = &b` if and only if there exists a path (in the
control flow graph) from the borrow to that use. So we need to know if
the borrow reaches a given use to know if it really gets a gen-bit or
not.
* Incorporating the output from one dataflow analysis into the input
of another required more changes to the infrastructure than I had
expected, and even after those changes, the resulting code is still
a bit subtle.
* In particular, Since we need to know the intrablock reservation
state, we need to dynamically update a bitvector for the
reservations as we are also trying to compute the gen/kills
bitvector for the active borrows.
* The way I ended up deciding to do this (after also toying with at
least two other designs) is to put both the reservation state and
the active borrow state into a single bitvector. That is why we now
have separate (but related) `BorrowIndex` and
`ReserveOrActivateIndex`: each borrow index maps to a pair of
neighboring reservation and activation indexes.
As noted above, these changes are solely adding the active borrows
dataflow analysis (and updating the existing code to cope with the
switch from `Borrows` to `Reservations`). The code to process the
bitvector in the borrow checker currently just skips over all of the
active borrow bits.
But atop this commit, one *can* observe the analysis results by
looking at the graphviz output, e.g. via
```rust
#[rustc_mir(borrowck_graphviz_preflow="pre_two_phase.dot",
borrowck_graphviz_postflow="post_two_phase.dot")]
```
Includes doc for `FindPlaceUses`, as well as `Reservations` and
`ActiveBorrows` structs, which are wrappers are the `Borrows` struct
that dictate which flow analysis should be performed.
2017-12-01 12:32:51 +01:00
|
|
|
use rustc::mir::{self, Location, Place, Mir};
|
2018-04-07 08:01:21 -04:00
|
|
|
use rustc::ty::TyCtxt;
|
2018-11-10 23:52:20 +00:00
|
|
|
use rustc::ty::RegionVid;
|
2017-07-03 18:40:20 +02:00
|
|
|
|
2018-09-17 13:52:35 +10:00
|
|
|
use rustc_data_structures::bit_set::{BitSet, BitSetOperator};
|
2018-09-04 16:44:04 +10:00
|
|
|
use rustc_data_structures::fx::FxHashMap;
|
2018-09-17 13:52:35 +10:00
|
|
|
use rustc_data_structures::indexed_vec::{Idx, IndexVec};
|
2017-07-03 18:40:20 +02:00
|
|
|
|
2017-11-27 15:08:11 +01:00
|
|
|
use dataflow::{BitDenotation, BlockSets, InitialFlow};
|
2018-04-07 05:53:44 -04:00
|
|
|
pub use dataflow::indexes::BorrowIndex;
|
2017-11-17 04:34:02 -05:00
|
|
|
use borrow_check::nll::region_infer::RegionInferenceContext;
|
|
|
|
use borrow_check::nll::ToRegionVid;
|
2018-12-17 13:11:33 +01:00
|
|
|
use borrow_check::places_conflict;
|
2017-07-03 18:40:20 +02:00
|
|
|
|
2017-12-04 00:56:06 +02:00
|
|
|
use std::rc::Rc;
|
2017-07-03 18:40:20 +02:00
|
|
|
|
2017-12-13 01:06:39 -06:00
|
|
|
/// `Borrows` stores the data used in the analyses that track the flow
|
|
|
|
/// of borrows.
|
|
|
|
///
|
|
|
|
/// It uniquely identifies every borrow (`Rvalue::Ref`) by a
|
|
|
|
/// `BorrowIndex`, and maps each such index to a `BorrowData`
|
|
|
|
/// describing the borrow. These indexes are used for representing the
|
|
|
|
/// borrows in compact bitvectors.
|
2017-10-30 05:50:39 -04:00
|
|
|
pub struct Borrows<'a, 'gcx: 'tcx, 'tcx: 'a> {
|
|
|
|
tcx: TyCtxt<'a, 'gcx, 'tcx>,
|
2017-07-03 18:40:20 +02:00
|
|
|
mir: &'a Mir<'tcx>,
|
2017-12-13 01:06:39 -06:00
|
|
|
|
2018-04-07 07:11:01 -04:00
|
|
|
borrow_set: Rc<BorrowSet<'tcx>>,
|
2018-05-27 11:15:52 +01:00
|
|
|
borrows_out_of_scope_at_location: FxHashMap<Location, Vec<BorrowIndex>>,
|
New `ActiveBorrows` dataflow for two-phase `&mut`; not yet borrowed-checked.
High-level picture: The old `Borrows` analysis is now called
`Reservations` (implemented as a newtype wrapper around `Borrows`);
this continues to compute whether a `Rvalue::Ref` can reach a
statement without an intervening `EndRegion`. In addition, we also
track what `Place` each such `Rvalue::Ref` was immediately assigned
to in a given borrow (yay for MIR-structural properties!).
The new `ActiveBorrows` analysis then tracks the initial use of any of
those assigned `Places` for a given borrow. I.e. a borrow becomes
"active" immediately after it starts being "used" in some way. (This
is conservative in the sense that we will treat a copy `x = y;` as a
use of `y`; in principle one might further delay activation in such
cases.)
The new `ActiveBorrows` analysis needs to take the `Reservations`
results as an initial input, because the reservation state influences
the gen/kill sets for `ActiveBorrows`. In particular, a use of `a`
activates a borrow `a = &b` if and only if there exists a path (in the
control flow graph) from the borrow to that use. So we need to know if
the borrow reaches a given use to know if it really gets a gen-bit or
not.
* Incorporating the output from one dataflow analysis into the input
of another required more changes to the infrastructure than I had
expected, and even after those changes, the resulting code is still
a bit subtle.
* In particular, Since we need to know the intrablock reservation
state, we need to dynamically update a bitvector for the
reservations as we are also trying to compute the gen/kills
bitvector for the active borrows.
* The way I ended up deciding to do this (after also toying with at
least two other designs) is to put both the reservation state and
the active borrow state into a single bitvector. That is why we now
have separate (but related) `BorrowIndex` and
`ReserveOrActivateIndex`: each borrow index maps to a pair of
neighboring reservation and activation indexes.
As noted above, these changes are solely adding the active borrows
dataflow analysis (and updating the existing code to cope with the
switch from `Borrows` to `Reservations`). The code to process the
bitvector in the borrow checker currently just skips over all of the
active borrow bits.
But atop this commit, one *can* observe the analysis results by
looking at the graphviz output, e.g. via
```rust
#[rustc_mir(borrowck_graphviz_preflow="pre_two_phase.dot",
borrowck_graphviz_postflow="post_two_phase.dot")]
```
Includes doc for `FindPlaceUses`, as well as `Reservations` and
`ActiveBorrows` structs, which are wrappers are the `Borrows` struct
that dictate which flow analysis should be performed.
2017-12-01 12:32:51 +01:00
|
|
|
|
2018-03-05 02:44:10 -05:00
|
|
|
/// NLL region inference context with which NLL queries should be resolved
|
2018-05-27 11:15:52 +01:00
|
|
|
_nonlexical_regioncx: Rc<RegionInferenceContext<'tcx>>,
|
|
|
|
}
|
|
|
|
|
2018-09-04 16:44:04 +10:00
|
|
|
struct StackEntry {
|
|
|
|
bb: mir::BasicBlock,
|
|
|
|
lo: usize,
|
|
|
|
hi: usize,
|
|
|
|
first_part_only: bool
|
|
|
|
}
|
|
|
|
|
2018-07-18 16:33:30 -03:00
|
|
|
fn precompute_borrows_out_of_scope<'tcx>(
|
|
|
|
mir: &Mir<'tcx>,
|
2018-05-27 11:15:52 +01:00
|
|
|
regioncx: &Rc<RegionInferenceContext<'tcx>>,
|
|
|
|
borrows_out_of_scope_at_location: &mut FxHashMap<Location, Vec<BorrowIndex>>,
|
|
|
|
borrow_index: BorrowIndex,
|
|
|
|
borrow_region: RegionVid,
|
2018-05-27 22:26:10 +01:00
|
|
|
location: Location,
|
2018-05-27 11:15:52 +01:00
|
|
|
) {
|
2018-09-04 16:44:04 +10:00
|
|
|
// We visit one BB at a time. The complication is that we may start in the
|
|
|
|
// middle of the first BB visited (the one containing `location`), in which
|
|
|
|
// case we may have to later on process the first part of that BB if there
|
|
|
|
// is a path back to its start.
|
|
|
|
|
|
|
|
// For visited BBs, we record the index of the first statement processed.
|
|
|
|
// (In fully processed BBs this index is 0.) Note also that we add BBs to
|
|
|
|
// `visited` once they are added to `stack`, before they are actually
|
|
|
|
// processed, because this avoids the need to look them up again on
|
|
|
|
// completion.
|
2018-10-16 10:44:26 +02:00
|
|
|
let mut visited = FxHashMap::default();
|
2018-09-04 16:44:04 +10:00
|
|
|
visited.insert(location.block, location.statement_index);
|
2018-05-27 11:15:52 +01:00
|
|
|
|
2018-09-04 16:44:04 +10:00
|
|
|
let mut stack = vec![];
|
|
|
|
stack.push(StackEntry {
|
|
|
|
bb: location.block,
|
|
|
|
lo: location.statement_index,
|
|
|
|
hi: mir[location.block].statements.len(),
|
|
|
|
first_part_only: false,
|
|
|
|
});
|
|
|
|
|
|
|
|
while let Some(StackEntry { bb, lo, hi, first_part_only }) = stack.pop() {
|
|
|
|
let mut finished_early = first_part_only;
|
|
|
|
for i in lo ..= hi {
|
|
|
|
let location = Location { block: bb, statement_index: i };
|
|
|
|
// If region does not contain a point at the location, then add to list and skip
|
|
|
|
// successor locations.
|
|
|
|
if !regioncx.region_contains(borrow_region, location) {
|
|
|
|
debug!("borrow {:?} gets killed at {:?}", borrow_index, location);
|
|
|
|
borrows_out_of_scope_at_location
|
|
|
|
.entry(location)
|
|
|
|
.or_default()
|
|
|
|
.push(borrow_index);
|
|
|
|
finished_early = true;
|
|
|
|
break;
|
2018-05-29 18:22:01 +01:00
|
|
|
}
|
2018-09-04 16:44:04 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
if !finished_early {
|
|
|
|
// Add successor BBs to the work list, if necessary.
|
|
|
|
let bb_data = &mir[bb];
|
|
|
|
assert!(hi == bb_data.statements.len());
|
|
|
|
for &succ_bb in bb_data.terminator.as_ref().unwrap().successors() {
|
|
|
|
visited.entry(succ_bb)
|
|
|
|
.and_modify(|lo| {
|
|
|
|
// `succ_bb` has been seen before. If it wasn't
|
|
|
|
// fully processed, add its first part to `stack`
|
|
|
|
// for processing.
|
|
|
|
if *lo > 0 {
|
|
|
|
stack.push(StackEntry {
|
|
|
|
bb: succ_bb,
|
|
|
|
lo: 0,
|
|
|
|
hi: *lo - 1,
|
|
|
|
first_part_only: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
// And update this entry with 0, to represent the
|
|
|
|
// whole BB being processed.
|
|
|
|
*lo = 0;
|
|
|
|
})
|
|
|
|
.or_insert_with(|| {
|
|
|
|
// succ_bb hasn't been seen before. Add it to
|
|
|
|
// `stack` for processing.
|
|
|
|
stack.push(StackEntry {
|
|
|
|
bb: succ_bb,
|
|
|
|
lo: 0,
|
|
|
|
hi: mir[succ_bb].statements.len(),
|
|
|
|
first_part_only: false,
|
|
|
|
});
|
|
|
|
// Insert 0 for this BB, to represent the whole BB
|
|
|
|
// being processed.
|
|
|
|
0
|
|
|
|
});
|
2018-05-29 19:38:04 +01:00
|
|
|
}
|
2018-05-29 18:22:01 +01:00
|
|
|
}
|
2018-05-27 11:15:52 +01:00
|
|
|
}
|
New `ActiveBorrows` dataflow for two-phase `&mut`; not yet borrowed-checked.
High-level picture: The old `Borrows` analysis is now called
`Reservations` (implemented as a newtype wrapper around `Borrows`);
this continues to compute whether a `Rvalue::Ref` can reach a
statement without an intervening `EndRegion`. In addition, we also
track what `Place` each such `Rvalue::Ref` was immediately assigned
to in a given borrow (yay for MIR-structural properties!).
The new `ActiveBorrows` analysis then tracks the initial use of any of
those assigned `Places` for a given borrow. I.e. a borrow becomes
"active" immediately after it starts being "used" in some way. (This
is conservative in the sense that we will treat a copy `x = y;` as a
use of `y`; in principle one might further delay activation in such
cases.)
The new `ActiveBorrows` analysis needs to take the `Reservations`
results as an initial input, because the reservation state influences
the gen/kill sets for `ActiveBorrows`. In particular, a use of `a`
activates a borrow `a = &b` if and only if there exists a path (in the
control flow graph) from the borrow to that use. So we need to know if
the borrow reaches a given use to know if it really gets a gen-bit or
not.
* Incorporating the output from one dataflow analysis into the input
of another required more changes to the infrastructure than I had
expected, and even after those changes, the resulting code is still
a bit subtle.
* In particular, Since we need to know the intrablock reservation
state, we need to dynamically update a bitvector for the
reservations as we are also trying to compute the gen/kills
bitvector for the active borrows.
* The way I ended up deciding to do this (after also toying with at
least two other designs) is to put both the reservation state and
the active borrow state into a single bitvector. That is why we now
have separate (but related) `BorrowIndex` and
`ReserveOrActivateIndex`: each borrow index maps to a pair of
neighboring reservation and activation indexes.
As noted above, these changes are solely adding the active borrows
dataflow analysis (and updating the existing code to cope with the
switch from `Borrows` to `Reservations`). The code to process the
bitvector in the borrow checker currently just skips over all of the
active borrow bits.
But atop this commit, one *can* observe the analysis results by
looking at the graphviz output, e.g. via
```rust
#[rustc_mir(borrowck_graphviz_preflow="pre_two_phase.dot",
borrowck_graphviz_postflow="post_two_phase.dot")]
```
Includes doc for `FindPlaceUses`, as well as `Reservations` and
`ActiveBorrows` structs, which are wrappers are the `Borrows` struct
that dictate which flow analysis should be performed.
2017-12-01 12:32:51 +01:00
|
|
|
}
|
|
|
|
|
2017-10-30 05:50:39 -04:00
|
|
|
impl<'a, 'gcx, 'tcx> Borrows<'a, 'gcx, 'tcx> {
|
2018-04-06 15:57:21 -04:00
|
|
|
crate fn new(
|
|
|
|
tcx: TyCtxt<'a, 'gcx, 'tcx>,
|
|
|
|
mir: &'a Mir<'tcx>,
|
2018-04-09 05:28:00 -04:00
|
|
|
nonlexical_regioncx: Rc<RegionInferenceContext<'tcx>>,
|
2018-09-05 23:49:58 +01:00
|
|
|
borrow_set: &Rc<BorrowSet<'tcx>>,
|
2018-04-06 15:57:21 -04:00
|
|
|
) -> Self {
|
2018-10-16 10:44:26 +02:00
|
|
|
let mut borrows_out_of_scope_at_location = FxHashMap::default();
|
2018-05-27 11:15:52 +01:00
|
|
|
for (borrow_index, borrow_data) in borrow_set.borrows.iter_enumerated() {
|
|
|
|
let borrow_region = borrow_data.region.to_region_vid();
|
|
|
|
let location = borrow_set.borrows[borrow_index].reserve_location;
|
|
|
|
|
|
|
|
precompute_borrows_out_of_scope(mir, &nonlexical_regioncx,
|
|
|
|
&mut borrows_out_of_scope_at_location,
|
2018-05-29 18:22:01 +01:00
|
|
|
borrow_index, borrow_region, location);
|
2018-05-27 11:15:52 +01:00
|
|
|
}
|
|
|
|
|
2018-04-06 15:53:49 -04:00
|
|
|
Borrows {
|
|
|
|
tcx: tcx,
|
|
|
|
mir: mir,
|
2018-04-07 07:11:01 -04:00
|
|
|
borrow_set: borrow_set.clone(),
|
2018-05-27 11:15:52 +01:00
|
|
|
borrows_out_of_scope_at_location,
|
|
|
|
_nonlexical_regioncx: nonlexical_regioncx,
|
2018-03-05 02:44:10 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-06 15:18:01 -04:00
|
|
|
crate fn borrows(&self) -> &IndexVec<BorrowIndex, BorrowData<'tcx>> { &self.borrow_set.borrows }
|
2017-12-10 17:00:20 +00:00
|
|
|
|
2017-07-03 18:40:20 +02:00
|
|
|
pub fn location(&self, idx: BorrowIndex) -> &Location {
|
2018-04-06 15:18:01 -04:00
|
|
|
&self.borrow_set.borrows[idx].reserve_location
|
2017-07-03 18:40:20 +02:00
|
|
|
}
|
2017-09-27 10:01:42 +03:00
|
|
|
|
2017-10-30 08:28:07 -04:00
|
|
|
/// Add all borrows to the kill set, if those borrows are out of scope at `location`.
|
2018-11-10 23:02:13 +00:00
|
|
|
/// That means they went out of a nonlexical scope
|
2017-10-30 08:28:07 -04:00
|
|
|
fn kill_loans_out_of_scope_at_location(&self,
|
2018-04-07 05:53:44 -04:00
|
|
|
sets: &mut BlockSets<BorrowIndex>,
|
2018-03-05 02:44:10 -05:00
|
|
|
location: Location) {
|
2018-04-09 05:28:00 -04:00
|
|
|
// NOTE: The state associated with a given `location`
|
2018-05-27 11:15:52 +01:00
|
|
|
// reflects the dataflow on entry to the statement.
|
|
|
|
// Iterate over each of the borrows that we've precomputed
|
|
|
|
// to have went out of scope at this location and kill them.
|
2018-04-09 05:28:00 -04:00
|
|
|
//
|
|
|
|
// We are careful always to call this function *before* we
|
|
|
|
// set up the gen-bits for the statement or
|
|
|
|
// termanator. That way, if the effect of the statement or
|
|
|
|
// terminator *does* introduce a new loan of the same
|
|
|
|
// region, then setting that gen-bit will override any
|
|
|
|
// potential kill introduced here.
|
2018-05-27 11:15:52 +01:00
|
|
|
if let Some(indices) = self.borrows_out_of_scope_at_location.get(&location) {
|
2018-09-05 16:37:21 +02:00
|
|
|
sets.kill_all(indices);
|
2017-10-30 08:28:07 -04:00
|
|
|
}
|
|
|
|
}
|
2017-07-03 18:40:20 +02:00
|
|
|
|
2018-12-17 13:11:33 +01:00
|
|
|
/// Kill any borrows that conflict with `place`.
|
|
|
|
fn kill_borrows_on_place(
|
|
|
|
&self,
|
|
|
|
sets: &mut BlockSets<BorrowIndex>,
|
|
|
|
place: &Place<'tcx>
|
|
|
|
) {
|
2018-12-17 17:26:24 +01:00
|
|
|
debug!("kill_borrows_on_place: place={:?}", place);
|
2018-12-17 13:11:33 +01:00
|
|
|
// Handle the `Place::Local(..)` case first and exit early.
|
|
|
|
if let Place::Local(local) = place {
|
2018-12-17 17:26:24 +01:00
|
|
|
if let Some(borrow_indices) = self.borrow_set.local_map.get(&local) {
|
|
|
|
debug!("kill_borrows_on_place: borrow_indices={:?}", borrow_indices);
|
|
|
|
sets.kill_all(borrow_indices);
|
2018-12-17 13:11:33 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, look at all borrows that are live and if they conflict with the assignment
|
|
|
|
// into our place then we can kill them.
|
|
|
|
let mut borrows = sets.on_entry.clone();
|
|
|
|
let _ = borrows.union(sets.gen_set);
|
|
|
|
for borrow_index in borrows.iter() {
|
|
|
|
let borrow_data = &self.borrows()[borrow_index];
|
|
|
|
debug!(
|
|
|
|
"kill_borrows_on_place: borrow_index={:?} borrow_data={:?}",
|
|
|
|
borrow_index, borrow_data,
|
|
|
|
);
|
|
|
|
|
|
|
|
// By passing `PlaceConflictBias::NoOverlap`, we conservatively assume that any given
|
|
|
|
// pair of array indices are unequal, so that when `places_conflict` returns true, we
|
|
|
|
// will be assured that two places being compared definitely denotes the same sets of
|
|
|
|
// locations.
|
|
|
|
if places_conflict::places_conflict(
|
|
|
|
self.tcx,
|
|
|
|
self.mir,
|
|
|
|
&borrow_data.borrowed_place,
|
2019-01-30 19:49:31 +00:00
|
|
|
place,
|
2018-12-17 13:11:33 +01:00
|
|
|
places_conflict::PlaceConflictBias::NoOverlap,
|
|
|
|
) {
|
|
|
|
debug!(
|
2018-12-17 17:26:24 +01:00
|
|
|
"kill_borrows_on_place: (kill) borrow_index={:?} borrow_data={:?}",
|
|
|
|
borrow_index, borrow_data,
|
2018-12-17 13:11:33 +01:00
|
|
|
);
|
|
|
|
sets.kill(borrow_index);
|
|
|
|
}
|
2018-03-05 02:44:10 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-17 17:26:24 +01:00
|
|
|
impl<'a, 'gcx, 'tcx> BitDenotation<'tcx> for Borrows<'a, 'gcx, 'tcx> {
|
2018-04-07 05:53:44 -04:00
|
|
|
type Idx = BorrowIndex;
|
2018-03-05 02:44:10 -05:00
|
|
|
fn name() -> &'static str { "borrows" }
|
|
|
|
fn bits_per_block(&self) -> usize {
|
2018-04-06 15:18:01 -04:00
|
|
|
self.borrow_set.borrows.len() * 2
|
2018-03-05 02:44:10 -05:00
|
|
|
}
|
|
|
|
|
Merge indexed_set.rs into bitvec.rs, and rename it bit_set.rs.
Currently we have two files implementing bitsets (and 2D bit matrices).
This commit combines them into one, taking the best features from each.
This involves renaming a lot of things. The high level changes are as
follows.
- bitvec.rs --> bit_set.rs
- indexed_set.rs --> (removed)
- BitArray + IdxSet --> BitSet (merged, see below)
- BitVector --> GrowableBitSet
- {,Sparse,Hybrid}IdxSet --> {,Sparse,Hybrid}BitSet
- BitMatrix --> BitMatrix
- SparseBitMatrix --> SparseBitMatrix
The changes within the bitset types themselves are as follows.
```
OLD OLD NEW
BitArray<C> IdxSet<T> BitSet<T>
-------- ------ ------
grow - grow
new - (remove)
new_empty new_empty new_empty
new_filled new_filled new_filled
- to_hybrid to_hybrid
clear clear clear
set_up_to set_up_to set_up_to
clear_above - clear_above
count - count
contains(T) contains(&T) contains(T)
contains_all - superset
is_empty - is_empty
insert(T) add(&T) insert(T)
insert_all - insert_all()
remove(T) remove(&T) remove(T)
words words words
words_mut words_mut words_mut
- overwrite overwrite
merge union union
- subtract subtract
- intersect intersect
iter iter iter
```
In general, when choosing names I went with:
- names that are more obvious (e.g. `BitSet` over `IdxSet`).
- names that are more like the Rust libraries (e.g. `T` over `C`,
`insert` over `add`);
- names that are more set-like (e.g. `union` over `merge`, `superset`
over `contains_all`, `domain_size` over `num_bits`).
Also, using `T` for index arguments seems more sensible than `&T` --
even though the latter is standard in Rust collection types -- because
indices are always copyable. It also results in fewer `&` and `*`
sigils in practice.
2018-09-14 15:07:25 +10:00
|
|
|
fn start_block_effect(&self, _entry_set: &mut BitSet<BorrowIndex>) {
|
2018-03-05 02:44:10 -05:00
|
|
|
// no borrows of code region_scopes have been taken prior to
|
|
|
|
// function execution, so this method has no effect on
|
|
|
|
// `_sets`.
|
|
|
|
}
|
|
|
|
|
2018-03-06 03:37:21 -05:00
|
|
|
fn before_statement_effect(&self,
|
2018-04-07 05:53:44 -04:00
|
|
|
sets: &mut BlockSets<BorrowIndex>,
|
2018-03-06 03:37:21 -05:00
|
|
|
location: Location) {
|
2018-03-05 02:44:10 -05:00
|
|
|
debug!("Borrows::before_statement_effect sets: {:?} location: {:?}", sets, location);
|
|
|
|
self.kill_loans_out_of_scope_at_location(sets, location);
|
|
|
|
}
|
|
|
|
|
2018-04-07 05:53:44 -04:00
|
|
|
fn statement_effect(&self, sets: &mut BlockSets<BorrowIndex>, location: Location) {
|
2018-12-17 13:11:33 +01:00
|
|
|
debug!("Borrows::statement_effect: sets={:?} location={:?}", sets, location);
|
2018-03-05 02:44:10 -05:00
|
|
|
|
2017-07-03 18:40:20 +02:00
|
|
|
let block = &self.mir.basic_blocks().get(location.block).unwrap_or_else(|| {
|
|
|
|
panic!("could not find block at location {:?}", location);
|
|
|
|
});
|
|
|
|
let stmt = block.statements.get(location.statement_index).unwrap_or_else(|| {
|
|
|
|
panic!("could not find statement at location {:?}");
|
|
|
|
});
|
New `ActiveBorrows` dataflow for two-phase `&mut`; not yet borrowed-checked.
High-level picture: The old `Borrows` analysis is now called
`Reservations` (implemented as a newtype wrapper around `Borrows`);
this continues to compute whether a `Rvalue::Ref` can reach a
statement without an intervening `EndRegion`. In addition, we also
track what `Place` each such `Rvalue::Ref` was immediately assigned
to in a given borrow (yay for MIR-structural properties!).
The new `ActiveBorrows` analysis then tracks the initial use of any of
those assigned `Places` for a given borrow. I.e. a borrow becomes
"active" immediately after it starts being "used" in some way. (This
is conservative in the sense that we will treat a copy `x = y;` as a
use of `y`; in principle one might further delay activation in such
cases.)
The new `ActiveBorrows` analysis needs to take the `Reservations`
results as an initial input, because the reservation state influences
the gen/kill sets for `ActiveBorrows`. In particular, a use of `a`
activates a borrow `a = &b` if and only if there exists a path (in the
control flow graph) from the borrow to that use. So we need to know if
the borrow reaches a given use to know if it really gets a gen-bit or
not.
* Incorporating the output from one dataflow analysis into the input
of another required more changes to the infrastructure than I had
expected, and even after those changes, the resulting code is still
a bit subtle.
* In particular, Since we need to know the intrablock reservation
state, we need to dynamically update a bitvector for the
reservations as we are also trying to compute the gen/kills
bitvector for the active borrows.
* The way I ended up deciding to do this (after also toying with at
least two other designs) is to put both the reservation state and
the active borrow state into a single bitvector. That is why we now
have separate (but related) `BorrowIndex` and
`ReserveOrActivateIndex`: each borrow index maps to a pair of
neighboring reservation and activation indexes.
As noted above, these changes are solely adding the active borrows
dataflow analysis (and updating the existing code to cope with the
switch from `Borrows` to `Reservations`). The code to process the
bitvector in the borrow checker currently just skips over all of the
active borrow bits.
But atop this commit, one *can* observe the analysis results by
looking at the graphviz output, e.g. via
```rust
#[rustc_mir(borrowck_graphviz_preflow="pre_two_phase.dot",
borrowck_graphviz_postflow="post_two_phase.dot")]
```
Includes doc for `FindPlaceUses`, as well as `Reservations` and
`ActiveBorrows` structs, which are wrappers are the `Borrows` struct
that dictate which flow analysis should be performed.
2017-12-01 12:32:51 +01:00
|
|
|
|
2018-12-17 13:11:33 +01:00
|
|
|
debug!("Borrows::statement_effect: stmt={:?}", stmt);
|
2017-07-03 18:40:20 +02:00
|
|
|
match stmt.kind {
|
2017-12-20 18:15:33 +01:00
|
|
|
mir::StatementKind::Assign(ref lhs, ref rhs) => {
|
2017-12-15 14:27:06 -06:00
|
|
|
// Make sure there are no remaining borrows for variables
|
|
|
|
// that are assigned over.
|
2018-12-17 17:26:24 +01:00
|
|
|
self.kill_borrows_on_place(sets, lhs);
|
New `ActiveBorrows` dataflow for two-phase `&mut`; not yet borrowed-checked.
High-level picture: The old `Borrows` analysis is now called
`Reservations` (implemented as a newtype wrapper around `Borrows`);
this continues to compute whether a `Rvalue::Ref` can reach a
statement without an intervening `EndRegion`. In addition, we also
track what `Place` each such `Rvalue::Ref` was immediately assigned
to in a given borrow (yay for MIR-structural properties!).
The new `ActiveBorrows` analysis then tracks the initial use of any of
those assigned `Places` for a given borrow. I.e. a borrow becomes
"active" immediately after it starts being "used" in some way. (This
is conservative in the sense that we will treat a copy `x = y;` as a
use of `y`; in principle one might further delay activation in such
cases.)
The new `ActiveBorrows` analysis needs to take the `Reservations`
results as an initial input, because the reservation state influences
the gen/kill sets for `ActiveBorrows`. In particular, a use of `a`
activates a borrow `a = &b` if and only if there exists a path (in the
control flow graph) from the borrow to that use. So we need to know if
the borrow reaches a given use to know if it really gets a gen-bit or
not.
* Incorporating the output from one dataflow analysis into the input
of another required more changes to the infrastructure than I had
expected, and even after those changes, the resulting code is still
a bit subtle.
* In particular, Since we need to know the intrablock reservation
state, we need to dynamically update a bitvector for the
reservations as we are also trying to compute the gen/kills
bitvector for the active borrows.
* The way I ended up deciding to do this (after also toying with at
least two other designs) is to put both the reservation state and
the active borrow state into a single bitvector. That is why we now
have separate (but related) `BorrowIndex` and
`ReserveOrActivateIndex`: each borrow index maps to a pair of
neighboring reservation and activation indexes.
As noted above, these changes are solely adding the active borrows
dataflow analysis (and updating the existing code to cope with the
switch from `Borrows` to `Reservations`). The code to process the
bitvector in the borrow checker currently just skips over all of the
active borrow bits.
But atop this commit, one *can* observe the analysis results by
looking at the graphviz output, e.g. via
```rust
#[rustc_mir(borrowck_graphviz_preflow="pre_two_phase.dot",
borrowck_graphviz_postflow="post_two_phase.dot")]
```
Includes doc for `FindPlaceUses`, as well as `Reservations` and
`ActiveBorrows` structs, which are wrappers are the `Borrows` struct
that dictate which flow analysis should be performed.
2017-12-01 12:32:51 +01:00
|
|
|
|
2018-11-30 21:48:31 +00:00
|
|
|
if let mir::Rvalue::Ref(_, _, ref place) = **rhs {
|
2018-09-05 23:49:58 +01:00
|
|
|
if place.ignore_borrow(
|
|
|
|
self.tcx,
|
|
|
|
self.mir,
|
|
|
|
&self.borrow_set.locals_state_at_exit,
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
2018-04-06 15:18:01 -04:00
|
|
|
let index = self.borrow_set.location_map.get(&location).unwrap_or_else(|| {
|
2018-03-05 22:43:43 -05:00
|
|
|
panic!("could not find BorrowIndex for location {:?}", location);
|
|
|
|
});
|
|
|
|
|
Merge indexed_set.rs into bitvec.rs, and rename it bit_set.rs.
Currently we have two files implementing bitsets (and 2D bit matrices).
This commit combines them into one, taking the best features from each.
This involves renaming a lot of things. The high level changes are as
follows.
- bitvec.rs --> bit_set.rs
- indexed_set.rs --> (removed)
- BitArray + IdxSet --> BitSet (merged, see below)
- BitVector --> GrowableBitSet
- {,Sparse,Hybrid}IdxSet --> {,Sparse,Hybrid}BitSet
- BitMatrix --> BitMatrix
- SparseBitMatrix --> SparseBitMatrix
The changes within the bitset types themselves are as follows.
```
OLD OLD NEW
BitArray<C> IdxSet<T> BitSet<T>
-------- ------ ------
grow - grow
new - (remove)
new_empty new_empty new_empty
new_filled new_filled new_filled
- to_hybrid to_hybrid
clear clear clear
set_up_to set_up_to set_up_to
clear_above - clear_above
count - count
contains(T) contains(&T) contains(T)
contains_all - superset
is_empty - is_empty
insert(T) add(&T) insert(T)
insert_all - insert_all()
remove(T) remove(&T) remove(T)
words words words
words_mut words_mut words_mut
- overwrite overwrite
merge union union
- subtract subtract
- intersect intersect
iter iter iter
```
In general, when choosing names I went with:
- names that are more obvious (e.g. `BitSet` over `IdxSet`).
- names that are more like the Rust libraries (e.g. `T` over `C`,
`insert` over `add`);
- names that are more set-like (e.g. `union` over `merge`, `superset`
over `contains_all`, `domain_size` over `num_bits`).
Also, using `T` for index arguments seems more sensible than `&T` --
even though the latter is standard in Rust collection types -- because
indices are always copyable. It also results in fewer `&` and `*`
sigils in practice.
2018-09-14 15:07:25 +10:00
|
|
|
sets.gen(*index);
|
2017-07-03 18:40:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-04 13:21:28 +02:00
|
|
|
mir::StatementKind::StorageDead(local) => {
|
|
|
|
// Make sure there are no remaining borrows for locals that
|
|
|
|
// are gone out of scope.
|
2018-12-17 17:26:24 +01:00
|
|
|
self.kill_borrows_on_place(sets, &Place::Local(local));
|
2017-12-04 13:21:28 +02:00
|
|
|
}
|
|
|
|
|
2017-12-23 23:45:07 +00:00
|
|
|
mir::StatementKind::InlineAsm { ref outputs, ref asm, .. } => {
|
|
|
|
for (output, kind) in outputs.iter().zip(&asm.outputs) {
|
|
|
|
if !kind.is_indirect && !kind.is_rw {
|
2018-12-17 17:26:24 +01:00
|
|
|
self.kill_borrows_on_place(sets, output);
|
2017-12-23 23:45:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-14 21:05:31 +02:00
|
|
|
mir::StatementKind::FakeRead(..) |
|
2017-07-03 18:40:20 +02:00
|
|
|
mir::StatementKind::SetDiscriminant { .. } |
|
|
|
|
mir::StatementKind::StorageLive(..) |
|
2018-10-24 11:47:17 +02:00
|
|
|
mir::StatementKind::Retag { .. } |
|
2018-08-31 18:59:35 -04:00
|
|
|
mir::StatementKind::AscribeUserType(..) |
|
2017-07-03 18:40:20 +02:00
|
|
|
mir::StatementKind::Nop => {}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2017-10-30 08:28:07 -04:00
|
|
|
|
2018-03-06 03:37:21 -05:00
|
|
|
fn before_terminator_effect(&self,
|
2018-04-07 05:53:44 -04:00
|
|
|
sets: &mut BlockSets<BorrowIndex>,
|
2018-03-06 03:37:21 -05:00
|
|
|
location: Location) {
|
2018-03-05 02:44:10 -05:00
|
|
|
debug!("Borrows::before_terminator_effect sets: {:?} location: {:?}", sets, location);
|
|
|
|
self.kill_loans_out_of_scope_at_location(sets, location);
|
2017-12-15 14:27:06 -06:00
|
|
|
}
|
|
|
|
|
2018-11-10 23:52:20 +00:00
|
|
|
fn terminator_effect(&self, _: &mut BlockSets<BorrowIndex>, _: Location) {}
|
2017-07-03 18:40:20 +02:00
|
|
|
|
2018-12-17 17:26:24 +01:00
|
|
|
fn propagate_call_return(
|
|
|
|
&self,
|
|
|
|
_in_out: &mut BitSet<BorrowIndex>,
|
|
|
|
_call_bb: mir::BasicBlock,
|
|
|
|
_dest_bb: mir::BasicBlock,
|
|
|
|
_dest_place: &mir::Place<'tcx>,
|
|
|
|
) {
|
New `ActiveBorrows` dataflow for two-phase `&mut`; not yet borrowed-checked.
High-level picture: The old `Borrows` analysis is now called
`Reservations` (implemented as a newtype wrapper around `Borrows`);
this continues to compute whether a `Rvalue::Ref` can reach a
statement without an intervening `EndRegion`. In addition, we also
track what `Place` each such `Rvalue::Ref` was immediately assigned
to in a given borrow (yay for MIR-structural properties!).
The new `ActiveBorrows` analysis then tracks the initial use of any of
those assigned `Places` for a given borrow. I.e. a borrow becomes
"active" immediately after it starts being "used" in some way. (This
is conservative in the sense that we will treat a copy `x = y;` as a
use of `y`; in principle one might further delay activation in such
cases.)
The new `ActiveBorrows` analysis needs to take the `Reservations`
results as an initial input, because the reservation state influences
the gen/kill sets for `ActiveBorrows`. In particular, a use of `a`
activates a borrow `a = &b` if and only if there exists a path (in the
control flow graph) from the borrow to that use. So we need to know if
the borrow reaches a given use to know if it really gets a gen-bit or
not.
* Incorporating the output from one dataflow analysis into the input
of another required more changes to the infrastructure than I had
expected, and even after those changes, the resulting code is still
a bit subtle.
* In particular, Since we need to know the intrablock reservation
state, we need to dynamically update a bitvector for the
reservations as we are also trying to compute the gen/kills
bitvector for the active borrows.
* The way I ended up deciding to do this (after also toying with at
least two other designs) is to put both the reservation state and
the active borrow state into a single bitvector. That is why we now
have separate (but related) `BorrowIndex` and
`ReserveOrActivateIndex`: each borrow index maps to a pair of
neighboring reservation and activation indexes.
As noted above, these changes are solely adding the active borrows
dataflow analysis (and updating the existing code to cope with the
switch from `Borrows` to `Reservations`). The code to process the
bitvector in the borrow checker currently just skips over all of the
active borrow bits.
But atop this commit, one *can* observe the analysis results by
looking at the graphviz output, e.g. via
```rust
#[rustc_mir(borrowck_graphviz_preflow="pre_two_phase.dot",
borrowck_graphviz_postflow="post_two_phase.dot")]
```
Includes doc for `FindPlaceUses`, as well as `Reservations` and
`ActiveBorrows` structs, which are wrappers are the `Borrows` struct
that dictate which flow analysis should be performed.
2017-12-01 12:32:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-17 13:52:35 +10:00
|
|
|
impl<'a, 'gcx, 'tcx> BitSetOperator for Borrows<'a, 'gcx, 'tcx> {
|
New `ActiveBorrows` dataflow for two-phase `&mut`; not yet borrowed-checked.
High-level picture: The old `Borrows` analysis is now called
`Reservations` (implemented as a newtype wrapper around `Borrows`);
this continues to compute whether a `Rvalue::Ref` can reach a
statement without an intervening `EndRegion`. In addition, we also
track what `Place` each such `Rvalue::Ref` was immediately assigned
to in a given borrow (yay for MIR-structural properties!).
The new `ActiveBorrows` analysis then tracks the initial use of any of
those assigned `Places` for a given borrow. I.e. a borrow becomes
"active" immediately after it starts being "used" in some way. (This
is conservative in the sense that we will treat a copy `x = y;` as a
use of `y`; in principle one might further delay activation in such
cases.)
The new `ActiveBorrows` analysis needs to take the `Reservations`
results as an initial input, because the reservation state influences
the gen/kill sets for `ActiveBorrows`. In particular, a use of `a`
activates a borrow `a = &b` if and only if there exists a path (in the
control flow graph) from the borrow to that use. So we need to know if
the borrow reaches a given use to know if it really gets a gen-bit or
not.
* Incorporating the output from one dataflow analysis into the input
of another required more changes to the infrastructure than I had
expected, and even after those changes, the resulting code is still
a bit subtle.
* In particular, Since we need to know the intrablock reservation
state, we need to dynamically update a bitvector for the
reservations as we are also trying to compute the gen/kills
bitvector for the active borrows.
* The way I ended up deciding to do this (after also toying with at
least two other designs) is to put both the reservation state and
the active borrow state into a single bitvector. That is why we now
have separate (but related) `BorrowIndex` and
`ReserveOrActivateIndex`: each borrow index maps to a pair of
neighboring reservation and activation indexes.
As noted above, these changes are solely adding the active borrows
dataflow analysis (and updating the existing code to cope with the
switch from `Borrows` to `Reservations`). The code to process the
bitvector in the borrow checker currently just skips over all of the
active borrow bits.
But atop this commit, one *can* observe the analysis results by
looking at the graphviz output, e.g. via
```rust
#[rustc_mir(borrowck_graphviz_preflow="pre_two_phase.dot",
borrowck_graphviz_postflow="post_two_phase.dot")]
```
Includes doc for `FindPlaceUses`, as well as `Reservations` and
`ActiveBorrows` structs, which are wrappers are the `Borrows` struct
that dictate which flow analysis should be performed.
2017-12-01 12:32:51 +01:00
|
|
|
#[inline]
|
2018-09-17 13:52:35 +10:00
|
|
|
fn join<T: Idx>(&self, inout_set: &mut BitSet<T>, in_set: &BitSet<T>) -> bool {
|
|
|
|
inout_set.union(in_set) // "maybe" means we union effects of both preds
|
2017-07-03 18:40:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-05 02:44:10 -05:00
|
|
|
impl<'a, 'gcx, 'tcx> InitialFlow for Borrows<'a, 'gcx, 'tcx> {
|
2017-07-03 18:40:20 +02:00
|
|
|
#[inline]
|
|
|
|
fn bottom_value() -> bool {
|
2018-03-05 02:44:10 -05:00
|
|
|
false // bottom = nothing is reserved or activated yet
|
2017-07-03 18:40:20 +02:00
|
|
|
}
|
|
|
|
}
|