Skip to content

Rollup of 5 pull requests #134380

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 19 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
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
  • Loading branch information
lqd committed Dec 15, 2024
commit 1740a5f84a19da866915e53f1dc05a9f929b4b37
33 changes: 9 additions & 24 deletions compiler/rustc_borrowck/src/polonius/legacy/accesses.rs
Original file line number Diff line number Diff line change
@@ -1,36 +1,24 @@
use rustc_middle::mir::visit::{MutatingUseContext, PlaceContext, Visitor};
use rustc_middle::mir::{Body, Local, Location, Place};
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 crate::def_use::{self, DefUse};
use crate::facts::AllFacts;
use crate::location::{LocationIndex, LocationTable};
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.
pub(crate) fn emit_access_facts<'tcx>(
facts: &mut AllFacts,
tcx: TyCtxt<'tcx>,
body: &Body<'tcx>,
move_data: &MoveData<'tcx>,
universal_regions: &UniversalRegions<'tcx>,
location_table: &LocationTable,
all_facts: &mut Option<AllFacts>,
) {
let Some(facts) = all_facts.as_mut() else { return };
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,
};
let mut extractor = AccessFactsExtractor { facts, move_data, location_table };
extractor.visit_body(body);

for (local, local_decl) in body.local_decls.iter_enumerated() {
Expand All @@ -44,12 +32,9 @@ pub(crate) fn emit_access_facts<'tcx>(

/// MIR visitor extracting point-wise facts about accesses.
struct AccessFactsExtractor<'a, 'tcx> {
var_defined_at: &'a mut VarPointRelation,
var_used_at: &'a mut VarPointRelation,
location_table: &'a LocationTable,
var_dropped_at: &'a mut VarPointRelation,
facts: &'a mut AllFacts,
move_data: &'a MoveData<'tcx>,
path_accessed_at_base: &'a mut PathPointRelation,
location_table: &'a LocationTable,
}

impl<'tcx> AccessFactsExtractor<'_, 'tcx> {
Expand All @@ -63,15 +48,15 @@ impl<'a, 'tcx> Visitor<'tcx> for AccessFactsExtractor<'a, 'tcx> {
match def_use::categorize(context) {
Some(DefUse::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) => {
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) => {
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)));
}
_ => (),
}
Expand All @@ -91,7 +76,7 @@ impl<'a, 'tcx> Visitor<'tcx> for AccessFactsExtractor<'a, 'tcx> {
}
};
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)));
}

_ => {}
Expand Down
38 changes: 12 additions & 26 deletions compiler/rustc_borrowck/src/polonius/legacy/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ mod accesses;
mod loan_invalidations;
mod loan_kills;

pub(crate) use accesses::emit_access_facts;

/// When requested, emit most of the facts needed by polonius:
/// - moves and assignments
/// - universal regions and their relations
/// - CFG points and edges
/// - loan kills
/// - 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.
pub(crate) fn emit_facts<'tcx>(
Expand All @@ -49,8 +49,16 @@ pub(crate) fn emit_facts<'tcx>(
let _prof_timer = tcx.prof.generic_activity("polonius_fact_generation");
emit_move_facts(all_facts, move_data, location_table, body);
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);
emit_loan_invalidations_facts(all_facts, tcx, location_table, body, borrow_set);
loan_kills::emit_loan_kills(tcx, all_facts, 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.
Expand Down Expand Up @@ -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
/// (`kind`), emit a `drop_of_var_derefs_origin(local, origin)` fact.
pub(crate) fn emit_drop_facts<'tcx>(
Expand Down
8 changes: 0 additions & 8 deletions compiler/rustc_borrowck/src/type_check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,14 +181,6 @@ pub(crate) fn type_check<'a, 'tcx>(

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(
infcx.tcx,
checker.constraints,
Expand Down