Merge AnalysisDomain into Analysis.

With `GenKillAnalysis` gone, there is no need for them to be separate.
This commit is contained in:
Nicholas Nethercote 2024-10-10 11:46:29 +11:00
parent 4dc1b4d0b1
commit ba13775319
11 changed files with 62 additions and 105 deletions

View File

@ -7,7 +7,7 @@
use rustc_middle::ty::{RegionVid, TyCtxt};
use rustc_mir_dataflow::fmt::DebugWithContext;
use rustc_mir_dataflow::impls::{EverInitializedPlaces, MaybeUninitializedPlaces};
use rustc_mir_dataflow::{Analysis, AnalysisDomain, Forward, GenKill, Results, ResultsVisitable};
use rustc_mir_dataflow::{Analysis, Forward, GenKill, Results, ResultsVisitable};
use tracing::debug;
use crate::{BorrowSet, PlaceConflictBias, PlaceExt, RegionInferenceContext, places_conflict};
@ -22,9 +22,9 @@ pub(crate) struct BorrowckResults<'a, 'tcx> {
/// The transient state of the dataflow analyses used by the borrow checker.
#[derive(Debug)]
pub(crate) struct BorrowckDomain<'a, 'tcx> {
pub(crate) borrows: <Borrows<'a, 'tcx> as AnalysisDomain<'tcx>>::Domain,
pub(crate) uninits: <MaybeUninitializedPlaces<'a, 'tcx> as AnalysisDomain<'tcx>>::Domain,
pub(crate) ever_inits: <EverInitializedPlaces<'a, 'tcx> as AnalysisDomain<'tcx>>::Domain,
pub(crate) borrows: <Borrows<'a, 'tcx> as Analysis<'tcx>>::Domain,
pub(crate) uninits: <MaybeUninitializedPlaces<'a, 'tcx> as Analysis<'tcx>>::Domain,
pub(crate) ever_inits: <EverInitializedPlaces<'a, 'tcx> as Analysis<'tcx>>::Domain,
}
impl<'a, 'tcx> ResultsVisitable<'tcx> for BorrowckResults<'a, 'tcx> {
@ -427,7 +427,7 @@ pub fn new(
/// That means they went out of a nonlexical scope
fn kill_loans_out_of_scope_at_location(
&self,
trans: &mut <Self as AnalysisDomain<'tcx>>::Domain,
trans: &mut <Self as Analysis<'tcx>>::Domain,
location: Location,
) {
// NOTE: The state associated with a given `location`
@ -449,7 +449,7 @@ fn kill_loans_out_of_scope_at_location(
/// Kill any borrows that conflict with `place`.
fn kill_borrows_on_place(
&self,
trans: &mut <Self as AnalysisDomain<'tcx>>::Domain,
trans: &mut <Self as Analysis<'tcx>>::Domain,
place: Place<'tcx>,
) {
debug!("kill_borrows_on_place: place={:?}", place);
@ -490,7 +490,14 @@ fn kill_borrows_on_place(
}
}
impl<'tcx> rustc_mir_dataflow::AnalysisDomain<'tcx> for Borrows<'_, 'tcx> {
/// Forward dataflow computation of the set of borrows that are in scope at a particular location.
/// - we gen the introduced loans
/// - we kill loans on locals going out of (regular) scope
/// - we kill the loans going out of their region's NLL scope: in NLL terms, the frontier where a
/// region stops containing the CFG points reachable from the issuing location.
/// - we also kill loans of conflicting places when overwriting a shared path: e.g. borrows of
/// `a.b.c` when `a` is overwritten.
impl<'tcx> rustc_mir_dataflow::Analysis<'tcx> for Borrows<'_, 'tcx> {
type Domain = BitSet<BorrowIndex>;
const NAME: &'static str = "borrows";
@ -504,16 +511,7 @@ fn initialize_start_block(&self, _: &mir::Body<'tcx>, _: &mut Self::Domain) {
// no borrows of code region_scopes have been taken prior to
// function execution, so this method has no effect.
}
}
/// Forward dataflow computation of the set of borrows that are in scope at a particular location.
/// - we gen the introduced loans
/// - we kill loans on locals going out of (regular) scope
/// - we kill the loans going out of their region's NLL scope: in NLL terms, the frontier where a
/// region stops containing the CFG points reachable from the issuing location.
/// - we also kill loans of conflicting places when overwriting a shared path: e.g. borrows of
/// `a.b.c` when `a` is overwritten.
impl<'tcx> rustc_mir_dataflow::Analysis<'tcx> for Borrows<'_, 'tcx> {
fn apply_before_statement_effect(
&mut self,
trans: &mut Self::Domain,

View File

@ -11,7 +11,7 @@
self, BasicBlock, CallReturnPlaces, Local, Location, Statement, StatementKind, TerminatorEdges,
};
use rustc_mir_dataflow::fmt::DebugWithContext;
use rustc_mir_dataflow::{Analysis, AnalysisDomain, JoinSemiLattice};
use rustc_mir_dataflow::{Analysis, JoinSemiLattice};
use super::{ConstCx, Qualif, qualifs};
@ -310,7 +310,7 @@ fn join(&mut self, other: &Self) -> bool {
}
}
impl<'tcx, Q> AnalysisDomain<'tcx> for FlowSensitiveAnalysis<'_, '_, 'tcx, Q>
impl<'tcx, Q> Analysis<'tcx> for FlowSensitiveAnalysis<'_, '_, 'tcx, Q>
where
Q: Qualif,
{
@ -328,12 +328,7 @@ fn bottom_value(&self, body: &mir::Body<'tcx>) -> Self::Domain {
fn initialize_start_block(&self, _body: &mir::Body<'tcx>, state: &mut Self::Domain) {
self.transfer_function(state).initialize_state();
}
}
impl<'tcx, Q> Analysis<'tcx> for FlowSensitiveAnalysis<'_, '_, 'tcx, Q>
where
Q: Qualif,
{
fn apply_statement_effect(
&mut self,
state: &mut Self::Domain,

View File

@ -16,14 +16,13 @@
use super::fmt::DebugWithContext;
use super::{
Analysis, AnalysisDomain, Direction, JoinSemiLattice, ResultsCursor, ResultsVisitor, graphviz,
visit_results,
Analysis, Direction, JoinSemiLattice, ResultsCursor, ResultsVisitor, graphviz, visit_results,
};
use crate::errors::{
DuplicateValuesFor, PathMustEndInFilename, RequiresAnArgument, UnknownFormatter,
};
type EntrySets<'tcx, A> = IndexVec<BasicBlock, <A as AnalysisDomain<'tcx>>::Domain>;
type EntrySets<'tcx, A> = IndexVec<BasicBlock, <A as Analysis<'tcx>>::Domain>;
/// A dataflow analysis that has converged to fixpoint.
#[derive(Clone)]

View File

@ -89,11 +89,26 @@ fn subtract(&mut self, other: &HybridBitSet<T>) {
}
}
/// Defines the domain of a dataflow problem.
/// A dataflow problem with an arbitrarily complex transfer function.
///
/// This trait specifies the lattice on which this analysis operates (the domain) as well as its
/// initial value at the entry point of each basic block.
pub trait AnalysisDomain<'tcx> {
/// This trait specifies the lattice on which this analysis operates (the domain), its
/// initial value at the entry point of each basic block, and various operations.
///
/// # Convergence
///
/// When implementing this trait it's possible to choose a transfer function such that the analysis
/// does not reach fixpoint. To guarantee convergence, your transfer functions must maintain the
/// following invariant:
///
/// > If the dataflow state **before** some point in the program changes to be greater
/// than the prior state **before** that point, the dataflow state **after** that point must
/// also change to be greater than the prior state **after** that point.
///
/// This invariant guarantees that the dataflow state at a given point in the program increases
/// monotonically until fixpoint is reached. Note that this monotonicity requirement only applies
/// to the same point in the program at different points in time. The dataflow state at a given
/// point in the program may or may not be greater than the state at any preceding point.
pub trait Analysis<'tcx> {
/// The type that holds the dataflow state at any given point in the program.
type Domain: Clone + JoinSemiLattice;
@ -118,25 +133,7 @@ pub trait AnalysisDomain<'tcx> {
// block where control flow could exit the MIR body (e.g., those terminated with `return` or
// `resume`). It's not obvious how to handle `yield` points in coroutines, however.
fn initialize_start_block(&self, body: &mir::Body<'tcx>, state: &mut Self::Domain);
}
/// A dataflow problem with an arbitrarily complex transfer function.
///
/// # Convergence
///
/// When implementing this trait it's possible to choose a transfer function such that the analysis
/// does not reach fixpoint. To guarantee convergence, your transfer functions must maintain the
/// following invariant:
///
/// > If the dataflow state **before** some point in the program changes to be greater
/// than the prior state **before** that point, the dataflow state **after** that point must
/// also change to be greater than the prior state **after** that point.
///
/// This invariant guarantees that the dataflow state at a given point in the program increases
/// monotonically until fixpoint is reached. Note that this monotonicity requirement only applies
/// to the same point in the program at different points in time. The dataflow state at a given
/// point in the program may or may not be greater than the state at any preceding point.
pub trait Analysis<'tcx>: AnalysisDomain<'tcx> {
/// Updates the current dataflow state with the effect of evaluating a statement.
fn apply_statement_effect(
&mut self,

View File

@ -154,7 +154,7 @@ fn expected_state_at_target(&self, target: SeekTarget) -> BitSet<usize> {
}
}
impl<'tcx, D: Direction> AnalysisDomain<'tcx> for MockAnalysis<'tcx, D> {
impl<'tcx, D: Direction> Analysis<'tcx> for MockAnalysis<'tcx, D> {
type Domain = BitSet<usize>;
type Direction = D;
@ -167,9 +167,7 @@ fn bottom_value(&self, body: &mir::Body<'tcx>) -> Self::Domain {
fn initialize_start_block(&self, _: &mir::Body<'tcx>, _: &mut Self::Domain) {
unimplemented!("This is never called since `MockAnalysis` is never iterated to fixpoint");
}
}
impl<'tcx, D: Direction> Analysis<'tcx> for MockAnalysis<'tcx, D> {
fn apply_statement_effect(
&mut self,
state: &mut Self::Domain,

View File

@ -2,7 +2,7 @@
use rustc_middle::mir::visit::Visitor;
use rustc_middle::mir::*;
use crate::{Analysis, AnalysisDomain, GenKill};
use crate::{Analysis, GenKill};
/// A dataflow analysis that tracks whether a pointer or reference could possibly exist that points
/// to a given local. This analysis ignores fake borrows, so it should not be used by
@ -20,7 +20,7 @@ pub(super) fn transfer_function<'a, T>(&'a self, trans: &'a mut T) -> TransferFu
}
}
impl<'tcx> AnalysisDomain<'tcx> for MaybeBorrowedLocals {
impl<'tcx> Analysis<'tcx> for MaybeBorrowedLocals {
type Domain = BitSet<Local>;
const NAME: &'static str = "maybe_borrowed_locals";
@ -32,9 +32,7 @@ fn bottom_value(&self, body: &Body<'tcx>) -> Self::Domain {
fn initialize_start_block(&self, _: &Body<'tcx>, _: &mut Self::Domain) {
// No locals are aliased on function entry
}
}
impl<'tcx> Analysis<'tcx> for MaybeBorrowedLocals {
fn apply_statement_effect(
&mut self,
trans: &mut Self::Domain,

View File

@ -11,9 +11,8 @@
use crate::framework::SwitchIntEdgeEffects;
use crate::move_paths::{HasMoveData, InitIndex, InitKind, LookupResult, MoveData, MovePathIndex};
use crate::{
Analysis, AnalysisDomain, GenKill, MaybeReachable, drop_flag_effects,
drop_flag_effects_for_function_entry, drop_flag_effects_for_location, lattice,
on_all_children_bits, on_lookup_result_bits,
Analysis, GenKill, MaybeReachable, drop_flag_effects, drop_flag_effects_for_function_entry,
drop_flag_effects_for_location, lattice, on_all_children_bits, on_lookup_result_bits,
};
/// `MaybeInitializedPlaces` tracks all places that might be
@ -270,7 +269,7 @@ fn move_data(&self) -> &MoveData<'tcx> {
impl<'a, 'tcx> MaybeInitializedPlaces<'a, 'tcx> {
fn update_bits(
trans: &mut <Self as AnalysisDomain<'tcx>>::Domain,
trans: &mut <Self as Analysis<'tcx>>::Domain,
path: MovePathIndex,
state: DropFlagState,
) {
@ -283,7 +282,7 @@ fn update_bits(
impl<'tcx> MaybeUninitializedPlaces<'_, 'tcx> {
fn update_bits(
trans: &mut <Self as AnalysisDomain<'tcx>>::Domain,
trans: &mut <Self as Analysis<'tcx>>::Domain,
path: MovePathIndex,
state: DropFlagState,
) {
@ -296,7 +295,7 @@ fn update_bits(
impl<'a, 'tcx> DefinitelyInitializedPlaces<'a, 'tcx> {
fn update_bits(
trans: &mut <Self as AnalysisDomain<'tcx>>::Domain,
trans: &mut <Self as Analysis<'tcx>>::Domain,
path: MovePathIndex,
state: DropFlagState,
) {
@ -307,7 +306,7 @@ fn update_bits(
}
}
impl<'tcx> AnalysisDomain<'tcx> for MaybeInitializedPlaces<'_, 'tcx> {
impl<'tcx> Analysis<'tcx> for MaybeInitializedPlaces<'_, 'tcx> {
/// There can be many more `MovePathIndex` than there are locals in a MIR body.
/// We use a chunked bitset to avoid paying too high a memory footprint.
type Domain = MaybeReachable<ChunkedBitSet<MovePathIndex>>;
@ -327,9 +326,7 @@ fn initialize_start_block(&self, _: &mir::Body<'tcx>, state: &mut Self::Domain)
state.gen_(path);
});
}
}
impl<'tcx> Analysis<'tcx> for MaybeInitializedPlaces<'_, 'tcx> {
fn apply_statement_effect(
&mut self,
trans: &mut Self::Domain,
@ -436,7 +433,7 @@ fn apply_switch_int_edge_effects(
}
}
impl<'tcx> AnalysisDomain<'tcx> for MaybeUninitializedPlaces<'_, 'tcx> {
impl<'tcx> Analysis<'tcx> for MaybeUninitializedPlaces<'_, 'tcx> {
/// There can be many more `MovePathIndex` than there are locals in a MIR body.
/// We use a chunked bitset to avoid paying too high a memory footprint.
type Domain = ChunkedBitSet<MovePathIndex>;
@ -458,9 +455,7 @@ fn initialize_start_block(&self, _: &mir::Body<'tcx>, state: &mut Self::Domain)
state.remove(path);
});
}
}
impl<'tcx> Analysis<'tcx> for MaybeUninitializedPlaces<'_, 'tcx> {
fn apply_statement_effect(
&mut self,
trans: &mut Self::Domain,
@ -559,7 +554,7 @@ fn apply_switch_int_edge_effects(
}
}
impl<'a, 'tcx> AnalysisDomain<'tcx> for DefinitelyInitializedPlaces<'a, 'tcx> {
impl<'a, 'tcx> Analysis<'tcx> for DefinitelyInitializedPlaces<'a, 'tcx> {
/// Use set intersection as the join operator.
type Domain = lattice::Dual<BitSet<MovePathIndex>>;
@ -579,9 +574,7 @@ fn initialize_start_block(&self, _: &mir::Body<'tcx>, state: &mut Self::Domain)
state.0.insert(path);
});
}
}
impl<'tcx> Analysis<'tcx> for DefinitelyInitializedPlaces<'_, 'tcx> {
fn apply_statement_effect(
&mut self,
trans: &mut Self::Domain,
@ -625,7 +618,7 @@ fn apply_call_return_effect(
}
}
impl<'tcx> AnalysisDomain<'tcx> for EverInitializedPlaces<'_, 'tcx> {
impl<'tcx> Analysis<'tcx> for EverInitializedPlaces<'_, 'tcx> {
/// There can be many more `InitIndex` than there are locals in a MIR body.
/// We use a chunked bitset to avoid paying too high a memory footprint.
type Domain = ChunkedBitSet<InitIndex>;
@ -642,9 +635,7 @@ fn initialize_start_block(&self, body: &mir::Body<'tcx>, state: &mut Self::Domai
state.insert(InitIndex::new(arg_init));
}
}
}
impl<'tcx> Analysis<'tcx> for EverInitializedPlaces<'_, 'tcx> {
#[instrument(skip(self, trans), level = "debug")]
fn apply_statement_effect(
&mut self,

View File

@ -4,7 +4,7 @@
self, CallReturnPlaces, Local, Location, Place, StatementKind, TerminatorEdges,
};
use crate::{Analysis, AnalysisDomain, Backward, GenKill};
use crate::{Analysis, Backward, GenKill};
/// A [live-variable dataflow analysis][liveness].
///
@ -25,7 +25,7 @@
/// [liveness]: https://en.wikipedia.org/wiki/Live_variable_analysis
pub struct MaybeLiveLocals;
impl<'tcx> AnalysisDomain<'tcx> for MaybeLiveLocals {
impl<'tcx> Analysis<'tcx> for MaybeLiveLocals {
type Domain = BitSet<Local>;
type Direction = Backward;
@ -39,9 +39,7 @@ fn bottom_value(&self, body: &mir::Body<'tcx>) -> Self::Domain {
fn initialize_start_block(&self, _: &mir::Body<'tcx>, _: &mut Self::Domain) {
// No variables are live until we observe a use
}
}
impl<'tcx> Analysis<'tcx> for MaybeLiveLocals {
fn apply_statement_effect(
&mut self,
trans: &mut Self::Domain,
@ -219,7 +217,7 @@ pub fn new(always_live: &'a BitSet<Local>) -> Self {
}
}
impl<'a, 'tcx> AnalysisDomain<'tcx> for MaybeTransitiveLiveLocals<'a> {
impl<'a, 'tcx> Analysis<'tcx> for MaybeTransitiveLiveLocals<'a> {
type Domain = BitSet<Local>;
type Direction = Backward;
@ -233,9 +231,7 @@ fn bottom_value(&self, body: &mir::Body<'tcx>) -> Self::Domain {
fn initialize_start_block(&self, _: &mir::Body<'tcx>, _: &mut Self::Domain) {
// No variables are live until we observe a use
}
}
impl<'a, 'tcx> Analysis<'tcx> for MaybeTransitiveLiveLocals<'a> {
fn apply_statement_effect(
&mut self,
trans: &mut Self::Domain,

View File

@ -5,7 +5,7 @@
use rustc_middle::mir::*;
use super::MaybeBorrowedLocals;
use crate::{Analysis, AnalysisDomain, GenKill, ResultsCursor};
use crate::{Analysis, GenKill, ResultsCursor};
pub struct MaybeStorageLive<'a> {
always_live_locals: Cow<'a, BitSet<Local>>,
@ -17,7 +17,7 @@ pub fn new(always_live_locals: Cow<'a, BitSet<Local>>) -> Self {
}
}
impl<'a, 'tcx> AnalysisDomain<'tcx> for MaybeStorageLive<'a> {
impl<'a, 'tcx> Analysis<'tcx> for MaybeStorageLive<'a> {
type Domain = BitSet<Local>;
const NAME: &'static str = "maybe_storage_live";
@ -37,9 +37,7 @@ fn initialize_start_block(&self, body: &Body<'tcx>, on_entry: &mut Self::Domain)
on_entry.insert(arg);
}
}
}
impl<'a, 'tcx> Analysis<'tcx> for MaybeStorageLive<'a> {
fn apply_statement_effect(
&mut self,
trans: &mut Self::Domain,
@ -83,7 +81,7 @@ pub fn new(always_live_locals: Cow<'a, BitSet<Local>>) -> Self {
}
}
impl<'a, 'tcx> AnalysisDomain<'tcx> for MaybeStorageDead<'a> {
impl<'a, 'tcx> Analysis<'tcx> for MaybeStorageDead<'a> {
type Domain = BitSet<Local>;
const NAME: &'static str = "maybe_storage_dead";
@ -102,9 +100,7 @@ fn initialize_start_block(&self, body: &Body<'tcx>, on_entry: &mut Self::Domain)
}
}
}
}
impl<'a, 'tcx> Analysis<'tcx> for MaybeStorageDead<'a> {
fn apply_statement_effect(
&mut self,
trans: &mut Self::Domain,
@ -152,7 +148,7 @@ pub fn new(borrowed_locals: BorrowedLocalsResults<'mir, 'tcx>) -> Self {
}
}
impl<'tcx> AnalysisDomain<'tcx> for MaybeRequiresStorage<'_, 'tcx> {
impl<'tcx> Analysis<'tcx> for MaybeRequiresStorage<'_, 'tcx> {
type Domain = BitSet<Local>;
const NAME: &'static str = "requires_storage";
@ -169,9 +165,7 @@ fn initialize_start_block(&self, body: &Body<'tcx>, on_entry: &mut Self::Domain)
on_entry.insert(arg);
}
}
}
impl<'tcx> Analysis<'tcx> for MaybeRequiresStorage<'_, 'tcx> {
fn apply_before_statement_effect(
&mut self,
trans: &mut Self::Domain,
@ -327,11 +321,7 @@ fn apply_call_return_effect(
impl<'tcx> MaybeRequiresStorage<'_, 'tcx> {
/// Kill locals that are fully moved and have not been borrowed.
fn check_for_move(
&mut self,
trans: &mut <Self as AnalysisDomain<'tcx>>::Domain,
loc: Location,
) {
fn check_for_move(&mut self, trans: &mut <Self as Analysis<'tcx>>::Domain, loc: Location) {
let body = self.borrowed_locals.body();
let mut visitor = MoveVisitor { trans, borrowed_locals: &mut self.borrowed_locals };
visitor.visit_location(body, loc);

View File

@ -18,9 +18,9 @@
move_path_children_matching, on_all_children_bits, on_lookup_result_bits,
};
pub use self::framework::{
Analysis, AnalysisDomain, Backward, Direction, Engine, Forward, GenKill, JoinSemiLattice,
MaybeReachable, Results, ResultsCursor, ResultsVisitable, ResultsVisitor, SwitchIntEdgeEffects,
fmt, graphviz, lattice, visit_results,
Analysis, Backward, Direction, Engine, Forward, GenKill, JoinSemiLattice, MaybeReachable,
Results, ResultsCursor, ResultsVisitable, ResultsVisitor, SwitchIntEdgeEffects, fmt, graphviz,
lattice, visit_results,
};
use self::move_paths::MoveData;

View File

@ -51,7 +51,7 @@
use crate::fmt::DebugWithContext;
use crate::lattice::{HasBottom, HasTop};
use crate::{Analysis, AnalysisDomain, JoinSemiLattice, SwitchIntEdgeEffects};
use crate::{Analysis, JoinSemiLattice, SwitchIntEdgeEffects};
pub trait ValueAnalysis<'tcx> {
/// For each place of interest, the analysis tracks a value of the given type.
@ -334,7 +334,7 @@ fn wrap(self) -> ValueAnalysisWrapper<Self>
pub struct ValueAnalysisWrapper<T>(pub T);
impl<'tcx, T: ValueAnalysis<'tcx>> AnalysisDomain<'tcx> for ValueAnalysisWrapper<T> {
impl<'tcx, T: ValueAnalysis<'tcx>> Analysis<'tcx> for ValueAnalysisWrapper<T> {
type Domain = State<T::Value>;
const NAME: &'static str = T::NAME;
@ -351,12 +351,7 @@ fn initialize_start_block(&self, body: &Body<'tcx>, state: &mut Self::Domain) {
state.flood(PlaceRef { local: arg, projection: &[] }, self.0.map());
}
}
}
impl<'tcx, T> Analysis<'tcx> for ValueAnalysisWrapper<T>
where
T: ValueAnalysis<'tcx>,
{
fn apply_statement_effect(
&mut self,
state: &mut Self::Domain,