2020-03-29 16:41:09 +02:00
|
|
|
use rustc_middle::mir::{self, Body, Location, Place};
|
|
|
|
use rustc_middle::ty::RegionVid;
|
|
|
|
use rustc_middle::ty::TyCtxt;
|
2017-07-03 18:40:20 +02:00
|
|
|
|
2018-09-04 16:44:04 +10:00
|
|
|
use rustc_data_structures::fx::FxHashMap;
|
2019-12-22 17:42:04 -05:00
|
|
|
use rustc_index::bit_set::BitSet;
|
2017-07-03 18:40:20 +02:00
|
|
|
|
2019-11-29 19:48:29 -06:00
|
|
|
use crate::borrow_check::{
|
2020-01-20 15:18:13 -08:00
|
|
|
places_conflict, BorrowSet, PlaceConflictBias, PlaceExt, RegionInferenceContext, ToRegionVid,
|
2019-11-29 19:48:29 -06:00
|
|
|
};
|
2020-01-20 15:14:24 -08:00
|
|
|
use crate::dataflow::BottomValue;
|
2020-02-28 22:02:20 -08:00
|
|
|
use crate::dataflow::{self, GenKill};
|
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
|
|
|
|
2019-09-26 05:38:33 +00:00
|
|
|
rustc_index::newtype_index! {
|
2019-04-01 19:38:00 +01:00
|
|
|
pub struct BorrowIndex {
|
|
|
|
DEBUG_FORMAT = "bw{}"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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.
|
2019-06-14 19:39:39 +03:00
|
|
|
pub struct Borrows<'a, 'tcx> {
|
2019-06-14 00:48:52 +03:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2019-06-03 18:26:48 -04:00
|
|
|
body: &'a Body<'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,
|
2019-12-22 17:42:04 -05:00
|
|
|
first_part_only: bool,
|
2018-09-04 16:44:04 +10:00
|
|
|
}
|
|
|
|
|
2018-07-18 16:33:30 -03:00
|
|
|
fn precompute_borrows_out_of_scope<'tcx>(
|
2019-06-03 18:26:48 -04:00
|
|
|
body: &Body<'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,
|
2019-06-03 18:26:48 -04:00
|
|
|
hi: body[location.block].statements.len(),
|
2018-09-04 16:44:04 +10:00
|
|
|
first_part_only: false,
|
|
|
|
});
|
|
|
|
|
|
|
|
while let Some(StackEntry { bb, lo, hi, first_part_only }) = stack.pop() {
|
|
|
|
let mut finished_early = first_part_only;
|
2019-12-22 17:42:04 -05:00
|
|
|
for i in lo..=hi {
|
2018-09-04 16:44:04 +10:00
|
|
|
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);
|
2019-12-22 17:42:04 -05:00
|
|
|
borrows_out_of_scope_at_location.entry(location).or_default().push(borrow_index);
|
2018-09-04 16:44:04 +10:00
|
|
|
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.
|
2019-06-03 18:26:48 -04:00
|
|
|
let bb_data = &body[bb];
|
2018-09-04 16:44:04 +10:00
|
|
|
assert!(hi == bb_data.statements.len());
|
2019-09-27 09:19:52 -04:00
|
|
|
for &succ_bb in bb_data.terminator().successors() {
|
2019-12-22 17:42:04 -05:00
|
|
|
visited
|
|
|
|
.entry(succ_bb)
|
2018-09-04 16:44:04 +10:00
|
|
|
.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,
|
2019-06-03 18:26:48 -04:00
|
|
|
hi: body[succ_bb].statements.len(),
|
2018-09-04 16:44:04 +10:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-06-14 00:48:52 +03:00
|
|
|
impl<'a, 'tcx> Borrows<'a, 'tcx> {
|
2018-04-06 15:57:21 -04:00
|
|
|
crate fn new(
|
2019-06-14 00:48:52 +03:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2019-06-03 18:26:48 -04:00
|
|
|
body: &'a Body<'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;
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
precompute_borrows_out_of_scope(
|
|
|
|
body,
|
|
|
|
&nonlexical_regioncx,
|
|
|
|
&mut borrows_out_of_scope_at_location,
|
|
|
|
borrow_index,
|
|
|
|
borrow_region,
|
|
|
|
location,
|
|
|
|
);
|
2018-05-27 11:15:52 +01:00
|
|
|
}
|
|
|
|
|
2018-04-06 15:53:49 -04:00
|
|
|
Borrows {
|
2019-11-06 12:00:46 -05:00
|
|
|
tcx,
|
|
|
|
body,
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
2019-12-22 17:42:04 -05:00
|
|
|
fn kill_loans_out_of_scope_at_location(
|
|
|
|
&self,
|
2020-01-20 15:14:24 -08:00
|
|
|
trans: &mut impl GenKill<BorrowIndex>,
|
2019-12-22 17:42:04 -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) {
|
2020-01-20 15:14:24 -08:00
|
|
|
trans.kill_all(indices.iter().copied());
|
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`.
|
2020-01-20 15:14:24 -08:00
|
|
|
fn kill_borrows_on_place(&self, trans: &mut impl GenKill<BorrowIndex>, place: &Place<'tcx>) {
|
2018-12-17 17:26:24 +01:00
|
|
|
debug!("kill_borrows_on_place: place={:?}", place);
|
2019-06-20 15:31:58 -07:00
|
|
|
|
2020-01-20 15:14:24 -08:00
|
|
|
let other_borrows_of_local = self
|
|
|
|
.borrow_set
|
|
|
|
.local_map
|
|
|
|
.get(&place.local)
|
|
|
|
.into_iter()
|
2020-02-29 03:05:14 +01:00
|
|
|
.flat_map(|bs| bs.iter())
|
2020-01-20 15:14:24 -08:00
|
|
|
.copied();
|
2019-12-11 16:50:03 -03:00
|
|
|
|
|
|
|
// If the borrowed place is a local with no projections, all other borrows of this
|
|
|
|
// local must conflict. This is purely an optimization so we don't have to call
|
|
|
|
// `places_conflict` for every borrow.
|
|
|
|
if place.projection.is_empty() {
|
|
|
|
if !self.body.local_decls[place.local].is_ref_to_static() {
|
|
|
|
trans.kill_all(other_borrows_of_local);
|
2019-12-11 10:39:24 -03:00
|
|
|
}
|
2019-12-11 16:50:03 -03:00
|
|
|
return;
|
2018-03-05 02:44:10 -05:00
|
|
|
}
|
2019-12-11 16:50:03 -03:00
|
|
|
|
|
|
|
// 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.
|
2020-01-20 15:14:24 -08:00
|
|
|
let definitely_conflicting_borrows = other_borrows_of_local.filter(|&i| {
|
2019-12-11 16:50:03 -03:00
|
|
|
places_conflict(
|
|
|
|
self.tcx,
|
|
|
|
self.body,
|
|
|
|
&self.borrow_set.borrows[i].borrowed_place,
|
|
|
|
place,
|
|
|
|
PlaceConflictBias::NoOverlap,
|
|
|
|
)
|
|
|
|
});
|
|
|
|
|
|
|
|
trans.kill_all(definitely_conflicting_borrows);
|
2018-03-05 02:44:10 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-28 22:02:20 -08:00
|
|
|
impl<'tcx> dataflow::AnalysisDomain<'tcx> for Borrows<'_, 'tcx> {
|
2018-04-07 05:53:44 -04:00
|
|
|
type Idx = BorrowIndex;
|
2020-01-20 15:14:24 -08:00
|
|
|
|
|
|
|
const NAME: &'static str = "borrows";
|
|
|
|
|
|
|
|
fn bits_per_block(&self, _: &mir::Body<'tcx>) -> usize {
|
2018-04-06 15:18:01 -04:00
|
|
|
self.borrow_set.borrows.len() * 2
|
2018-03-05 02:44:10 -05:00
|
|
|
}
|
|
|
|
|
2020-01-20 15:14:24 -08:00
|
|
|
fn initialize_start_block(&self, _: &mir::Body<'tcx>, _: &mut BitSet<Self::Idx>) {
|
2018-03-05 02:44:10 -05:00
|
|
|
// no borrows of code region_scopes have been taken prior to
|
rustc_mir: don't pass `on_entry` when building transfer functions.
This commit makes `sets.on_entry` inaccessible in
`{before_,}{statement,terminator}_effect`. This field was meant to allow
implementors of `BitDenotation` to access the initial state for each
block (optionally with the effect of all previous statements applied via
`accumulates_intrablock_state`) while defining transfer functions.
However, the ability to set the initial value for the entry set of each
basic block (except for START_BLOCK) no longer exists. As a result, this
functionality is mostly useless, and when it *was* used it was used
erroneously (see #62007).
Since `on_entry` is now useless, we can also remove `BlockSets`, which
held the `gen`, `kill`, and `on_entry` bitvectors and replace it with a
`GenKill` struct. Variables of this type are called `trans` since they
represent a transfer function. `GenKill`s are stored contiguously in
`AllSets`, which reduces the number of bounds checks and may improve
cache performance: one is almost never accessed without the other.
Replacing `BlockSets` with `GenKill` allows us to define some new helper
functions which streamline dataflow iteration and the
dataflow-at-location APIs. Notably, `state_for_location` used a subtle
side-effect of the `kill`/`kill_all` setters to apply the transfer
function, and could be incorrect if a transfer function depended on
effects of previous statements in the block on `gen_set`.
2019-06-12 15:00:43 -07:00
|
|
|
// function execution, so this method has no effect.
|
2018-03-05 02:44:10 -05:00
|
|
|
}
|
|
|
|
|
2020-01-20 15:14:24 -08:00
|
|
|
fn pretty_print_idx(&self, w: &mut impl std::io::Write, idx: Self::Idx) -> std::io::Result<()> {
|
|
|
|
write!(w, "{:?}", self.location(idx))
|
2018-03-05 02:44:10 -05:00
|
|
|
}
|
2020-01-20 15:14:24 -08:00
|
|
|
}
|
2018-03-05 02:44:10 -05:00
|
|
|
|
2020-02-28 22:02:20 -08:00
|
|
|
impl<'tcx> dataflow::GenKillAnalysis<'tcx> for Borrows<'_, 'tcx> {
|
2020-01-20 15:14:24 -08:00
|
|
|
fn before_statement_effect(
|
|
|
|
&self,
|
|
|
|
trans: &mut impl GenKill<Self::Idx>,
|
|
|
|
_statement: &mir::Statement<'tcx>,
|
|
|
|
location: Location,
|
|
|
|
) {
|
|
|
|
self.kill_loans_out_of_scope_at_location(trans, 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
|
|
|
|
2020-01-20 15:14:24 -08:00
|
|
|
fn statement_effect(
|
|
|
|
&self,
|
|
|
|
trans: &mut impl GenKill<Self::Idx>,
|
|
|
|
stmt: &mir::Statement<'tcx>,
|
|
|
|
location: Location,
|
|
|
|
) {
|
2017-07-03 18:40:20 +02:00
|
|
|
match stmt.kind {
|
2019-12-22 17:42:04 -05:00
|
|
|
mir::StatementKind::Assign(box (ref lhs, ref rhs)) => {
|
2019-09-11 16:05:45 -03:00
|
|
|
if let mir::Rvalue::Ref(_, _, ref place) = *rhs {
|
2018-09-05 23:49:58 +01:00
|
|
|
if place.ignore_borrow(
|
|
|
|
self.tcx,
|
2019-06-03 18:26:48 -04:00
|
|
|
self.body,
|
2018-09-05 23:49:58 +01:00
|
|
|
&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);
|
|
|
|
});
|
|
|
|
|
rustc_mir: don't pass `on_entry` when building transfer functions.
This commit makes `sets.on_entry` inaccessible in
`{before_,}{statement,terminator}_effect`. This field was meant to allow
implementors of `BitDenotation` to access the initial state for each
block (optionally with the effect of all previous statements applied via
`accumulates_intrablock_state`) while defining transfer functions.
However, the ability to set the initial value for the entry set of each
basic block (except for START_BLOCK) no longer exists. As a result, this
functionality is mostly useless, and when it *was* used it was used
erroneously (see #62007).
Since `on_entry` is now useless, we can also remove `BlockSets`, which
held the `gen`, `kill`, and `on_entry` bitvectors and replace it with a
`GenKill` struct. Variables of this type are called `trans` since they
represent a transfer function. `GenKill`s are stored contiguously in
`AllSets`, which reduces the number of bounds checks and may improve
cache performance: one is almost never accessed without the other.
Replacing `BlockSets` with `GenKill` allows us to define some new helper
functions which streamline dataflow iteration and the
dataflow-at-location APIs. Notably, `state_for_location` used a subtle
side-effect of the `kill`/`kill_all` setters to apply the transfer
function, and could be incorrect if a transfer function depended on
effects of previous statements in the block on `gen_set`.
2019-06-12 15:00:43 -07:00
|
|
|
trans.gen(*index);
|
2017-07-03 18:40:20 +02:00
|
|
|
}
|
2019-08-31 15:35:20 +01:00
|
|
|
|
|
|
|
// Make sure there are no remaining borrows for variables
|
|
|
|
// that are assigned over.
|
|
|
|
self.kill_borrows_on_place(trans, lhs);
|
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.
|
2019-06-24 17:46:09 +02:00
|
|
|
self.kill_borrows_on_place(trans, &Place::from(local));
|
2017-12-04 13:21:28 +02:00
|
|
|
}
|
|
|
|
|
2020-01-14 13:40:42 +00:00
|
|
|
mir::StatementKind::LlvmInlineAsm(ref asm) => {
|
2019-04-02 20:07:09 +11:00
|
|
|
for (output, kind) in asm.outputs.iter().zip(&asm.asm.outputs) {
|
2017-12-23 23:45:07 +00:00
|
|
|
if !kind.is_indirect && !kind.is_rw {
|
rustc_mir: don't pass `on_entry` when building transfer functions.
This commit makes `sets.on_entry` inaccessible in
`{before_,}{statement,terminator}_effect`. This field was meant to allow
implementors of `BitDenotation` to access the initial state for each
block (optionally with the effect of all previous statements applied via
`accumulates_intrablock_state`) while defining transfer functions.
However, the ability to set the initial value for the entry set of each
basic block (except for START_BLOCK) no longer exists. As a result, this
functionality is mostly useless, and when it *was* used it was used
erroneously (see #62007).
Since `on_entry` is now useless, we can also remove `BlockSets`, which
held the `gen`, `kill`, and `on_entry` bitvectors and replace it with a
`GenKill` struct. Variables of this type are called `trans` since they
represent a transfer function. `GenKill`s are stored contiguously in
`AllSets`, which reduces the number of bounds checks and may improve
cache performance: one is almost never accessed without the other.
Replacing `BlockSets` with `GenKill` allows us to define some new helper
functions which streamline dataflow iteration and the
dataflow-at-location APIs. Notably, `state_for_location` used a subtle
side-effect of the `kill`/`kill_all` setters to apply the transfer
function, and could be incorrect if a transfer function depended on
effects of previous statements in the block on `gen_set`.
2019-06-12 15:00:43 -07:00
|
|
|
self.kill_borrows_on_place(trans, output);
|
2017-12-23 23:45:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
mir::StatementKind::FakeRead(..)
|
|
|
|
| mir::StatementKind::SetDiscriminant { .. }
|
|
|
|
| mir::StatementKind::StorageLive(..)
|
|
|
|
| mir::StatementKind::Retag { .. }
|
|
|
|
| mir::StatementKind::AscribeUserType(..)
|
|
|
|
| mir::StatementKind::Nop => {}
|
2017-07-03 18:40:20 +02:00
|
|
|
}
|
|
|
|
}
|
2017-10-30 08:28:07 -04:00
|
|
|
|
2020-01-20 15:14:24 -08:00
|
|
|
fn before_terminator_effect(
|
|
|
|
&self,
|
|
|
|
trans: &mut impl GenKill<Self::Idx>,
|
|
|
|
_terminator: &mir::Terminator<'tcx>,
|
|
|
|
location: Location,
|
|
|
|
) {
|
rustc_mir: don't pass `on_entry` when building transfer functions.
This commit makes `sets.on_entry` inaccessible in
`{before_,}{statement,terminator}_effect`. This field was meant to allow
implementors of `BitDenotation` to access the initial state for each
block (optionally with the effect of all previous statements applied via
`accumulates_intrablock_state`) while defining transfer functions.
However, the ability to set the initial value for the entry set of each
basic block (except for START_BLOCK) no longer exists. As a result, this
functionality is mostly useless, and when it *was* used it was used
erroneously (see #62007).
Since `on_entry` is now useless, we can also remove `BlockSets`, which
held the `gen`, `kill`, and `on_entry` bitvectors and replace it with a
`GenKill` struct. Variables of this type are called `trans` since they
represent a transfer function. `GenKill`s are stored contiguously in
`AllSets`, which reduces the number of bounds checks and may improve
cache performance: one is almost never accessed without the other.
Replacing `BlockSets` with `GenKill` allows us to define some new helper
functions which streamline dataflow iteration and the
dataflow-at-location APIs. Notably, `state_for_location` used a subtle
side-effect of the `kill`/`kill_all` setters to apply the transfer
function, and could be incorrect if a transfer function depended on
effects of previous statements in the block on `gen_set`.
2019-06-12 15:00:43 -07:00
|
|
|
self.kill_loans_out_of_scope_at_location(trans, location);
|
2017-12-15 14:27:06 -06:00
|
|
|
}
|
|
|
|
|
2020-01-20 15:14:24 -08:00
|
|
|
fn terminator_effect(
|
|
|
|
&self,
|
|
|
|
_: &mut impl GenKill<Self::Idx>,
|
|
|
|
_: &mir::Terminator<'tcx>,
|
|
|
|
_: Location,
|
|
|
|
) {
|
|
|
|
}
|
2017-07-03 18:40:20 +02:00
|
|
|
|
2020-01-20 15:14:24 -08:00
|
|
|
fn call_return_effect(
|
2018-12-17 17:26:24 +01:00
|
|
|
&self,
|
2020-01-20 15:14:24 -08:00
|
|
|
_trans: &mut impl GenKill<Self::Idx>,
|
|
|
|
_block: mir::BasicBlock,
|
|
|
|
_func: &mir::Operand<'tcx>,
|
|
|
|
_args: &[mir::Operand<'tcx>],
|
2018-12-17 17:26:24 +01:00
|
|
|
_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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-21 12:39:01 -07:00
|
|
|
impl<'a, 'tcx> BottomValue for Borrows<'a, 'tcx> {
|
|
|
|
/// bottom = nothing is reserved or activated yet;
|
|
|
|
const BOTTOM_VALUE: bool = false;
|
2017-07-03 18:40:20 +02:00
|
|
|
}
|