1
Fork 0

simplify emit_access_facts and fact generation

- integrate it within existing fact generation instead of being called
  in typeck
- simplify access fact extraction
- also remove single use fact emit functions in root fact generation
This commit is contained in:
Rémy Rakic 2024-12-11 14:09:08 +00:00
parent 5486857448
commit 1740a5f84a
3 changed files with 21 additions and 58 deletions

View file

@ -1,7 +1,7 @@
use rustc_middle::mir::visit::{MutatingUseContext, PlaceContext, Visitor}; use rustc_middle::mir::visit::{MutatingUseContext, PlaceContext, Visitor};
use rustc_middle::mir::{Body, Local, Location, Place}; use rustc_middle::mir::{Body, Local, Location, Place};
use rustc_middle::ty::TyCtxt; use rustc_middle::ty::TyCtxt;
use rustc_mir_dataflow::move_paths::{LookupResult, MoveData, MovePathIndex}; use rustc_mir_dataflow::move_paths::{LookupResult, MoveData};
use tracing::debug; use tracing::debug;
use crate::def_use::{self, DefUse}; use crate::def_use::{self, DefUse};
@ -9,28 +9,16 @@ use crate::facts::AllFacts;
use crate::location::{LocationIndex, LocationTable}; use crate::location::{LocationIndex, LocationTable};
use crate::universal_regions::UniversalRegions; use crate::universal_regions::UniversalRegions;
type VarPointRelation = Vec<(Local, LocationIndex)>;
type PathPointRelation = Vec<(MovePathIndex, LocationIndex)>;
/// Emit polonius facts for variable defs, uses, drops, and path accesses. /// Emit polonius facts for variable defs, uses, drops, and path accesses.
pub(crate) fn emit_access_facts<'tcx>( pub(crate) fn emit_access_facts<'tcx>(
facts: &mut AllFacts,
tcx: TyCtxt<'tcx>, tcx: TyCtxt<'tcx>,
body: &Body<'tcx>, body: &Body<'tcx>,
move_data: &MoveData<'tcx>, move_data: &MoveData<'tcx>,
universal_regions: &UniversalRegions<'tcx>, universal_regions: &UniversalRegions<'tcx>,
location_table: &LocationTable, location_table: &LocationTable,
all_facts: &mut Option<AllFacts>,
) { ) {
let Some(facts) = all_facts.as_mut() else { return }; let mut extractor = AccessFactsExtractor { facts, move_data, location_table };
let _prof_timer = tcx.prof.generic_activity("polonius_fact_generation");
let mut extractor = AccessFactsExtractor {
var_defined_at: &mut facts.var_defined_at,
var_used_at: &mut facts.var_used_at,
var_dropped_at: &mut facts.var_dropped_at,
path_accessed_at_base: &mut facts.path_accessed_at_base,
location_table,
move_data,
};
extractor.visit_body(body); extractor.visit_body(body);
for (local, local_decl) in body.local_decls.iter_enumerated() { for (local, local_decl) in body.local_decls.iter_enumerated() {
@ -44,12 +32,9 @@ pub(crate) fn emit_access_facts<'tcx>(
/// MIR visitor extracting point-wise facts about accesses. /// MIR visitor extracting point-wise facts about accesses.
struct AccessFactsExtractor<'a, 'tcx> { struct AccessFactsExtractor<'a, 'tcx> {
var_defined_at: &'a mut VarPointRelation, facts: &'a mut AllFacts,
var_used_at: &'a mut VarPointRelation,
location_table: &'a LocationTable,
var_dropped_at: &'a mut VarPointRelation,
move_data: &'a MoveData<'tcx>, move_data: &'a MoveData<'tcx>,
path_accessed_at_base: &'a mut PathPointRelation, location_table: &'a LocationTable,
} }
impl<'tcx> AccessFactsExtractor<'_, 'tcx> { impl<'tcx> AccessFactsExtractor<'_, 'tcx> {
@ -63,15 +48,15 @@ impl<'a, 'tcx> Visitor<'tcx> for AccessFactsExtractor<'a, 'tcx> {
match def_use::categorize(context) { match def_use::categorize(context) {
Some(DefUse::Def) => { Some(DefUse::Def) => {
debug!("AccessFactsExtractor - emit def"); debug!("AccessFactsExtractor - emit def");
self.var_defined_at.push((local, self.location_to_index(location))); self.facts.var_defined_at.push((local, self.location_to_index(location)));
} }
Some(DefUse::Use) => { Some(DefUse::Use) => {
debug!("AccessFactsExtractor - emit use"); debug!("AccessFactsExtractor - emit use");
self.var_used_at.push((local, self.location_to_index(location))); self.facts.var_used_at.push((local, self.location_to_index(location)));
} }
Some(DefUse::Drop) => { Some(DefUse::Drop) => {
debug!("AccessFactsExtractor - emit drop"); debug!("AccessFactsExtractor - emit drop");
self.var_dropped_at.push((local, self.location_to_index(location))); self.facts.var_dropped_at.push((local, self.location_to_index(location)));
} }
_ => (), _ => (),
} }
@ -91,7 +76,7 @@ impl<'a, 'tcx> Visitor<'tcx> for AccessFactsExtractor<'a, 'tcx> {
} }
}; };
debug!("AccessFactsExtractor - emit path access ({path:?}, {location:?})"); debug!("AccessFactsExtractor - emit path access ({path:?}, {location:?})");
self.path_accessed_at_base.push((path, self.location_to_index(location))); self.facts.path_accessed_at_base.push((path, self.location_to_index(location)));
} }
_ => {} _ => {}

View file

@ -23,14 +23,14 @@ mod accesses;
mod loan_invalidations; mod loan_invalidations;
mod loan_kills; mod loan_kills;
pub(crate) use accesses::emit_access_facts;
/// When requested, emit most of the facts needed by polonius: /// When requested, emit most of the facts needed by polonius:
/// - moves and assignments /// - moves and assignments
/// - universal regions and their relations /// - universal regions and their relations
/// - CFG points and edges /// - CFG points and edges
/// - loan kills /// - loan kills
/// - loan invalidations /// - loan invalidations
/// - access facts such as variable definitions, uses, drops, and path accesses
/// - outlives constraints
/// ///
/// The rest of the facts are emitted during typeck and liveness. /// The rest of the facts are emitted during typeck and liveness.
pub(crate) fn emit_facts<'tcx>( pub(crate) fn emit_facts<'tcx>(
@ -49,8 +49,16 @@ pub(crate) fn emit_facts<'tcx>(
let _prof_timer = tcx.prof.generic_activity("polonius_fact_generation"); let _prof_timer = tcx.prof.generic_activity("polonius_fact_generation");
emit_move_facts(all_facts, move_data, location_table, body); emit_move_facts(all_facts, move_data, location_table, body);
emit_universal_region_facts(all_facts, borrow_set, universal_region_relations); emit_universal_region_facts(all_facts, borrow_set, universal_region_relations);
emit_cfg_and_loan_kills_facts(all_facts, tcx, location_table, body, borrow_set); loan_kills::emit_loan_kills(tcx, all_facts, location_table, body, borrow_set);
emit_loan_invalidations_facts(all_facts, tcx, location_table, body, borrow_set); loan_invalidations::emit_loan_invalidations(tcx, all_facts, location_table, body, borrow_set);
accesses::emit_access_facts(
all_facts,
tcx,
body,
move_data,
&universal_region_relations.universal_regions,
location_table,
);
} }
/// Emit facts needed for move/init analysis: moves and assignments. /// Emit facts needed for move/init analysis: moves and assignments.
@ -170,28 +178,6 @@ fn emit_universal_region_facts(
} }
} }
/// Emit facts about loan invalidations.
fn emit_loan_invalidations_facts<'tcx>(
all_facts: &mut AllFacts,
tcx: TyCtxt<'tcx>,
location_table: &LocationTable,
body: &Body<'tcx>,
borrow_set: &BorrowSet<'tcx>,
) {
loan_invalidations::emit_loan_invalidations(tcx, all_facts, location_table, body, borrow_set);
}
/// Emit facts about CFG points and edges, as well as locations where loans are killed.
fn emit_cfg_and_loan_kills_facts<'tcx>(
all_facts: &mut AllFacts,
tcx: TyCtxt<'tcx>,
location_table: &LocationTable,
body: &Body<'tcx>,
borrow_set: &BorrowSet<'tcx>,
) {
loan_kills::emit_loan_kills(tcx, all_facts, location_table, body, borrow_set);
}
/// For every potentially drop()-touched region `region` in `local`'s type /// For every potentially drop()-touched region `region` in `local`'s type
/// (`kind`), emit a `drop_of_var_derefs_origin(local, origin)` fact. /// (`kind`), emit a `drop_of_var_derefs_origin(local, origin)` fact.
pub(crate) fn emit_drop_facts<'tcx>( pub(crate) fn emit_drop_facts<'tcx>(

View file

@ -181,14 +181,6 @@ pub(crate) fn type_check<'a, 'tcx>(
liveness::generate(&mut checker, body, &elements, flow_inits, move_data); liveness::generate(&mut checker, body, &elements, flow_inits, move_data);
polonius::legacy::emit_access_facts(
infcx.tcx,
body,
move_data,
&universal_region_relations.universal_regions,
location_table,
checker.all_facts,
);
polonius::legacy::emit_outlives_facts( polonius::legacy::emit_outlives_facts(
infcx.tcx, infcx.tcx,
checker.constraints, checker.constraints,