Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
36 changes: 20 additions & 16 deletions compiler/rustc_borrowck/src/type_check/liveness/trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use rustc_middle::ty::{GenericArg, Ty, TypeVisitableExt};
use rustc_mir_dataflow::impls::MaybeInitializedPlaces;
use rustc_mir_dataflow::move_paths::{HasMoveData, MoveData, MovePathIndex};
use rustc_mir_dataflow::points::{DenseLocationMap, PointIndex};
use rustc_mir_dataflow::{Analysis, MaybeReachable, ResultsCursor};
use rustc_mir_dataflow::{Analysis, MaybeReachable, ResultsCursor, on_all_children_bits};
use rustc_span::{DUMMY_SP, ErrorGuaranteed, Span};
use rustc_trait_selection::error_reporting::InferCtxtErrorExt;
use rustc_trait_selection::traits::ObligationCtxt;
Expand Down Expand Up @@ -715,20 +715,21 @@ impl<'a, 'tcx> LivenessComputation<'a, 'tcx> {
let flow_inits = self.flow_inits.get_or_insert_with(|| {
let tcx = self.infcx.tcx;
let body = self.body;
// FIXME: reduce the `MaybeInitializedPlaces` domain to the useful `MovePath`s.
//
// This dataflow analysis computes maybe-initializedness of all move paths, which
// explains why it can be expensive on big functions. But this data is only used in
// drop-liveness. Therefore, most of the move paths computed here are ultimately unused,
// even if the results are computed lazily and "no relevant live locals with drop
// points" is the common case.
//
// So we only need the ones for 1) relevant live locals 2) that have drop points. That's
// a much, much smaller domain: in our benchmarks, when it's not zero (the most likely
// case), there are a few dozens compared to e.g. thousands or tens of thousands of
// locals and move paths.
// Drop-liveness reads only the move paths of locals that have drop points. Track only
// these move paths, because a large function can have thousands of move paths.
let mut selected = DenseBitSet::new_empty(self.move_data.move_paths.len());
for local in body.local_decls.indices() {
if self.local_use_map.drops(local).next().is_some()
&& let Some(root) = self.move_data.rev_lookup.find_local(local)
{
on_all_children_bits(self.move_data, root, |path| {
selected.insert(path);
});
}
}
let _timer = tcx.prof.generic_activity("borrowck_dataflow_maybe_inits");
let flow_inits = MaybeInitializedPlaces::new(tcx, body, self.move_data)
.restrict_to_move_paths(&selected)
.iterate_to_fixpoint(tcx, body, Some("borrowck"))
.into_results_cursor(body);
flow_inits
Expand All @@ -747,12 +748,15 @@ impl<'a, 'tcx> LivenessComputation<'a, 'tcx> {
}
flow_inits.get().clone()
});
if state.contains(mpi) {
let analysis = flow_inits.analysis();
if analysis.contains_move_path(state, mpi) {
return true;
}

let move_paths = &flow_inits.analysis().move_data().move_paths;
move_paths[mpi].find_descendant(move_paths, |mpi| state.contains(mpi)).is_some()
let move_paths = &analysis.move_data().move_paths;
move_paths[mpi]
.find_descendant(move_paths, |mpi| analysis.contains_move_path(state, mpi))
.is_some()
}

/// Returns `true` if the local variable (or some part of it) is initialized in
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_mir_dataflow/src/framework/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,6 @@ where
C: crate::move_paths::HasMoveData<'tcx>,
{
fn fmt_with(&self, ctxt: &C, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", ctxt.move_data().move_paths[*self])
write!(f, "{}", ctxt.move_data().move_paths[ctxt.original_move_path(*self)])
}
}
79 changes: 67 additions & 12 deletions compiler/rustc_mir_dataflow/src/impls/initialized.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::assert_matches;

use rustc_abi::VariantIdx;
use rustc_data_structures::fx::FxIndexSet;
use rustc_index::IndexVec;
use rustc_index::bit_set::{DenseBitSet, MixedBitSet};
use rustc_middle::mir::{
self, BasicBlock, Body, CallReturnPlaces, Local, Location, StatementKind, TerminatorEdges,
Expand Down Expand Up @@ -140,20 +141,58 @@ pub struct MaybeInitializedPlaces<'a, 'tcx> {
tcx: TyCtxt<'tcx>,
body: &'a Body<'tcx>,
move_data: &'a MoveData<'tcx>,
tracked: Option<TrackedMovePaths>,
exclude_inactive_in_otherwise: bool,
skip_unreachable_unwind: bool,
}

/// A subset of the move paths. `to_tracked` maps a move path to its domain index, and
/// `to_original` maps a domain index back to its move path.
struct TrackedMovePaths {
to_tracked: IndexVec<MovePathIndex, Option<MovePathIndex>>,
to_original: IndexVec<MovePathIndex, MovePathIndex>,
}

impl<'a, 'tcx> MaybeInitializedPlaces<'a, 'tcx> {
pub fn new(tcx: TyCtxt<'tcx>, body: &'a Body<'tcx>, move_data: &'a MoveData<'tcx>) -> Self {
MaybeInitializedPlaces {
tcx,
body,
move_data,
tracked: None,
exclude_inactive_in_otherwise: false,
skip_unreachable_unwind: false,
}
}

/// Tracks only the move paths in `selected`. The domain then uses its own indices, so use
/// `contains_move_path` to read a state.
pub fn restrict_to_move_paths(mut self, selected: &DenseBitSet<MovePathIndex>) -> Self {
assert_eq!(selected.domain_size(), self.move_data.move_paths.len());
let mut to_tracked = IndexVec::from_elem(None, &self.move_data.move_paths);
let mut to_original = IndexVec::new();
for path in selected.iter() {
to_tracked[path] = Some(to_original.push(path));
}
self.tracked = Some(TrackedMovePaths { to_tracked, to_original });
self
}

fn domain_size(&self) -> usize {
self.tracked.as_ref().map_or(self.move_data.move_paths.len(), |t| t.to_original.len())
}

fn tracked_path(&self, path: MovePathIndex) -> Option<MovePathIndex> {
self.tracked.as_ref().map_or(Some(path), |t| t.to_tracked[path])
}

pub fn contains_move_path(
&self,
state: &<Self as Analysis<'tcx>>::Domain,
path: MovePathIndex,
) -> bool {
state.contains(self.tracked_path(path).expect("queried move path was not selected"))
}
}

impl<'tcx> MaybeInitializedPlaces<'_, 'tcx> {
Expand All @@ -177,7 +216,7 @@ impl<'tcx> MaybeInitializedPlaces<'_, 'tcx> {
if let LookupResult::Exact(path) = self.move_data().rev_lookup.find(place.as_ref()) {
let mut maybe_live = false;
on_all_children_bits(self.move_data(), path, |child| {
maybe_live |= state.contains(child);
maybe_live |= self.tracked_path(child).is_none_or(|child| state.contains(child));
});
!maybe_live
} else {
Expand All @@ -186,13 +225,16 @@ impl<'tcx> MaybeInitializedPlaces<'_, 'tcx> {
}

fn update_bits(
&self,
state: &mut <Self as Analysis<'tcx>>::Domain,
path: MovePathIndex,
dfstate: DropFlagState,
) {
match dfstate {
DropFlagState::Absent => state.kill(path),
DropFlagState::Present => state.gen_(path),
if let Some(path) = self.tracked_path(path) {
match dfstate {
DropFlagState::Absent => state.kill(path),
DropFlagState::Present => state.gen_(path),
}
}
}
}
Expand All @@ -201,6 +243,10 @@ impl<'a, 'tcx> HasMoveData<'tcx> for MaybeInitializedPlaces<'a, 'tcx> {
fn move_data(&self) -> &MoveData<'tcx> {
self.move_data
}

fn original_move_path(&self, path: MovePathIndex) -> MovePathIndex {
self.tracked.as_ref().map_or(path, |t| t.to_original[path])
}
}

impl<'tcx> Analysis<'tcx> for MaybeInitializedPlaces<'_, 'tcx> {
Expand All @@ -218,11 +264,12 @@ impl<'tcx> Analysis<'tcx> for MaybeInitializedPlaces<'_, 'tcx> {
}

fn initialize_start_block(&self, _: &mir::Body<'tcx>, state: &mut Self::Domain) {
*state =
MaybeReachable::Reachable(MixedBitSet::new_empty(self.move_data().move_paths.len()));
*state = MaybeReachable::Reachable(MixedBitSet::new_empty(self.domain_size()));
drop_flag_effects_for_function_entry(self.body, self.move_data, |path, s| {
debug_assert!(s == DropFlagState::Present);
state.gen_(path);
if let Some(path) = self.tracked_path(path) {
state.gen_(path);
}
});
}

Expand All @@ -233,7 +280,7 @@ impl<'tcx> Analysis<'tcx> for MaybeInitializedPlaces<'_, 'tcx> {
location: Location,
) {
drop_flag_effects_for_location(self.body, self.move_data, location, |path, s| {
Self::update_bits(state, path, s)
self.update_bits(state, path, s)
});

// Mark all places as "maybe init" if they are mutably borrowed. See #90752.
Expand All @@ -245,7 +292,9 @@ impl<'tcx> Analysis<'tcx> for MaybeInitializedPlaces<'_, 'tcx> {
&& let LookupResult::Exact(mpi) = self.move_data().rev_lookup.find(place.as_ref())
{
on_all_children_bits(self.move_data(), mpi, |child| {
state.gen_(child);
if let Some(child) = self.tracked_path(child) {
state.gen_(child);
}
})
}
}
Expand Down Expand Up @@ -278,7 +327,7 @@ impl<'tcx> Analysis<'tcx> for MaybeInitializedPlaces<'_, 'tcx> {
location: Location,
) {
drop_flag_effects_for_location(self.body, self.move_data, location, |path, s| {
Self::update_bits(state, path, s)
self.update_bits(state, path, s)
});
}

Expand All @@ -295,7 +344,9 @@ impl<'tcx> Analysis<'tcx> for MaybeInitializedPlaces<'_, 'tcx> {
self.move_data(),
self.move_data().rev_lookup.find(place.as_ref()),
|mpi| {
state.gen_(mpi);
if let Some(mpi) = self.tracked_path(mpi) {
state.gen_(mpi);
}
},
);
});
Expand Down Expand Up @@ -336,7 +387,11 @@ impl<'tcx> Analysis<'tcx> for MaybeInitializedPlaces<'_, 'tcx> {
self.move_data,
data.enum_place,
&inactive_variants,
|mpi| state.kill(mpi),
|mpi| {
if let Some(mpi) = self.tracked_path(mpi) {
state.kill(mpi);
}
},
);
}
}
Expand Down
5 changes: 5 additions & 0 deletions compiler/rustc_mir_dataflow/src/move_paths/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,11 @@ pub struct MoveData<'tcx> {

pub trait HasMoveData<'tcx> {
fn move_data(&self) -> &MoveData<'tcx>;

/// Maps a domain index to its move path. Override if the domain uses other indices.
fn original_move_path(&self, path: MovePathIndex) -> MovePathIndex {
path
}
}

#[derive(Debug)]
Expand Down
Loading