Skip to content

Commit

Permalink
Auto merge of rust-lang#132062 - nnethercote:still-more-rustc_mir_dat…
Browse files Browse the repository at this point in the history
…aflow-cleanups, r=<try>

Still more `rustc_mir_dataflow` cleanups

A sequel to rust-lang#118203, rust-lang#118230, rust-lang#118638, and rust-lang#131481.

I'm pleased with this PR. It cleans up a few things that have been bugging me for a while. It took quite some effort to find these improvements.

r? `@cjgillot`
  • Loading branch information
bors committed Oct 23, 2024
2 parents 4f2f477 + 9e735ca commit 1c188eb
Show file tree
Hide file tree
Showing 28 changed files with 407 additions and 445 deletions.
16 changes: 8 additions & 8 deletions compiler/rustc_borrowck/src/dataflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl<'a, 'tcx> ResultsVisitable<'tcx> for BorrowckResults<'a, 'tcx> {
}

fn reconstruct_before_statement_effect(
&mut self,
&self,
state: &mut Self::Domain,
stmt: &mir::Statement<'tcx>,
loc: Location,
Expand All @@ -55,7 +55,7 @@ impl<'a, 'tcx> ResultsVisitable<'tcx> for BorrowckResults<'a, 'tcx> {
}

fn reconstruct_statement_effect(
&mut self,
&self,
state: &mut Self::Domain,
stmt: &mir::Statement<'tcx>,
loc: Location,
Expand All @@ -66,7 +66,7 @@ impl<'a, 'tcx> ResultsVisitable<'tcx> for BorrowckResults<'a, 'tcx> {
}

fn reconstruct_before_terminator_effect(
&mut self,
&self,
state: &mut Self::Domain,
term: &mir::Terminator<'tcx>,
loc: Location,
Expand All @@ -77,7 +77,7 @@ impl<'a, 'tcx> ResultsVisitable<'tcx> for BorrowckResults<'a, 'tcx> {
}

fn reconstruct_terminator_effect(
&mut self,
&self,
state: &mut Self::Domain,
term: &mir::Terminator<'tcx>,
loc: Location,
Expand Down Expand Up @@ -511,7 +511,7 @@ impl<'tcx> rustc_mir_dataflow::Analysis<'tcx> for Borrows<'_, 'tcx> {
}

fn apply_before_statement_effect(
&mut self,
&self,
trans: &mut Self::Domain,
_statement: &mir::Statement<'tcx>,
location: Location,
Expand All @@ -520,7 +520,7 @@ impl<'tcx> rustc_mir_dataflow::Analysis<'tcx> for Borrows<'_, 'tcx> {
}

fn apply_statement_effect(
&mut self,
&self,
trans: &mut Self::Domain,
stmt: &mir::Statement<'tcx>,
location: Location,
Expand Down Expand Up @@ -568,7 +568,7 @@ impl<'tcx> rustc_mir_dataflow::Analysis<'tcx> for Borrows<'_, 'tcx> {
}

fn apply_before_terminator_effect(
&mut self,
&self,
trans: &mut Self::Domain,
_terminator: &mir::Terminator<'tcx>,
location: Location,
Expand All @@ -577,7 +577,7 @@ impl<'tcx> rustc_mir_dataflow::Analysis<'tcx> for Borrows<'_, 'tcx> {
}

fn apply_terminator_effect<'mir>(
&mut self,
&self,
trans: &mut Self::Domain,
terminator: &'mir mir::Terminator<'tcx>,
_location: Location,
Expand Down
37 changes: 19 additions & 18 deletions compiler/rustc_borrowck/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,7 @@ fn do_mir_borrowck<'tcx>(
.map(|(idx, body)| (idx, MoveData::gather_moves(body, tcx, |_| true)));

let mut flow_inits = MaybeInitializedPlaces::new(tcx, body, &move_data)
.into_engine(tcx, body)
.pass_name("borrowck")
.iterate_to_fixpoint()
.iterate_to_fixpoint(tcx, body, Some("borrowck"))
.into_results_cursor(body);

let locals_are_invalidated_at_exit = tcx.hir().body_owner_kind(def).is_fn_or_closure();
Expand Down Expand Up @@ -243,18 +241,21 @@ fn do_mir_borrowck<'tcx>(
// usage significantly on some benchmarks.
drop(flow_inits);

let flow_borrows = Borrows::new(tcx, body, &regioncx, &borrow_set)
.into_engine(tcx, body)
.pass_name("borrowck")
.iterate_to_fixpoint();
let flow_uninits = MaybeUninitializedPlaces::new(tcx, body, &move_data)
.into_engine(tcx, body)
.pass_name("borrowck")
.iterate_to_fixpoint();
let flow_ever_inits = EverInitializedPlaces::new(body, &move_data)
.into_engine(tcx, body)
.pass_name("borrowck")
.iterate_to_fixpoint();
let flow_borrows = Borrows::new(tcx, body, &regioncx, &borrow_set).iterate_to_fixpoint(
tcx,
body,
Some("borrowck"),
);
let flow_uninits = MaybeUninitializedPlaces::new(tcx, body, &move_data).iterate_to_fixpoint(
tcx,
body,
Some("borrowck"),
);
let flow_ever_inits = EverInitializedPlaces::new(body, &move_data).iterate_to_fixpoint(
tcx,
body,
Some("borrowck"),
);

let movable_coroutine =
// The first argument is the coroutine type passed by value
Expand Down Expand Up @@ -602,7 +603,7 @@ impl<'a, 'tcx, R> rustc_mir_dataflow::ResultsVisitor<'a, 'tcx, R>

fn visit_statement_before_primary_effect(
&mut self,
_results: &mut R,
_results: &R,
state: &BorrowckDomain<'a, 'tcx>,
stmt: &'a Statement<'tcx>,
location: Location,
Expand Down Expand Up @@ -672,7 +673,7 @@ impl<'a, 'tcx, R> rustc_mir_dataflow::ResultsVisitor<'a, 'tcx, R>

fn visit_terminator_before_primary_effect(
&mut self,
_results: &mut R,
_results: &R,
state: &BorrowckDomain<'a, 'tcx>,
term: &'a Terminator<'tcx>,
loc: Location,
Expand Down Expand Up @@ -785,7 +786,7 @@ impl<'a, 'tcx, R> rustc_mir_dataflow::ResultsVisitor<'a, 'tcx, R>

fn visit_terminator_after_primary_effect(
&mut self,
_results: &mut R,
_results: &R,
state: &BorrowckDomain<'a, 'tcx>,
term: &'a Terminator<'tcx>,
loc: Location,
Expand Down
12 changes: 4 additions & 8 deletions compiler/rustc_const_eval/src/check_consts/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ impl<'mir, 'tcx> Qualifs<'mir, 'tcx> {
let ConstCx { tcx, body, .. } = *ccx;

FlowSensitiveAnalysis::new(NeedsDrop, ccx)
.into_engine(tcx, body)
.iterate_to_fixpoint()
.iterate_to_fixpoint(tcx, body, None)
.into_results_cursor(body)
});

Expand Down Expand Up @@ -93,8 +92,7 @@ impl<'mir, 'tcx> Qualifs<'mir, 'tcx> {
let ConstCx { tcx, body, .. } = *ccx;

FlowSensitiveAnalysis::new(NeedsNonConstDrop, ccx)
.into_engine(tcx, body)
.iterate_to_fixpoint()
.iterate_to_fixpoint(tcx, body, None)
.into_results_cursor(body)
});

Expand Down Expand Up @@ -123,8 +121,7 @@ impl<'mir, 'tcx> Qualifs<'mir, 'tcx> {
let ConstCx { tcx, body, .. } = *ccx;

FlowSensitiveAnalysis::new(HasMutInterior, ccx)
.into_engine(tcx, body)
.iterate_to_fixpoint()
.iterate_to_fixpoint(tcx, body, None)
.into_results_cursor(body)
});

Expand Down Expand Up @@ -239,8 +236,7 @@ impl<'mir, 'tcx> Checker<'mir, 'tcx> {
let always_live_locals = &always_storage_live_locals(&ccx.body);
let mut maybe_storage_live =
MaybeStorageLive::new(Cow::Borrowed(always_live_locals))
.into_engine(ccx.tcx, &ccx.body)
.iterate_to_fixpoint()
.iterate_to_fixpoint(ccx.tcx, &ccx.body, None)
.into_results_cursor(&ccx.body);

// And then check all `Return` in the MIR, and if a local is "maybe live" at a
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_const_eval/src/check_consts/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ where
}

fn apply_statement_effect(
&mut self,
&self,
state: &mut Self::Domain,
statement: &mir::Statement<'tcx>,
location: Location,
Expand All @@ -339,7 +339,7 @@ where
}

fn apply_terminator_effect<'mir>(
&mut self,
&self,
state: &mut Self::Domain,
terminator: &'mir mir::Terminator<'tcx>,
location: Location,
Expand All @@ -349,7 +349,7 @@ where
}

fn apply_call_return_effect(
&mut self,
&self,
state: &mut Self::Domain,
block: BasicBlock,
return_places: CallReturnPlaces<'_, 'tcx>,
Expand Down
51 changes: 31 additions & 20 deletions compiler/rustc_mir_dataflow/src/framework/cursor.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,39 @@
//! Random access inspection of the results of a dataflow analysis.

use std::cmp::Ordering;
use std::ops::Deref;

#[cfg(debug_assertions)]
use rustc_index::bit_set::BitSet;
use rustc_middle::mir::{self, BasicBlock, Location};

use super::{Analysis, Direction, Effect, EffectIndex, Results};

/// Some `ResultsCursor`s want to own a `Results`, and some want to borrow a `Results`. This type
/// allows either. We could use `Cow` but that would require `Results` and `A` to impl `Clone`. So
/// this is a greatly cut-down alternative to `Cow`.
pub enum ResultsHandle<'a, 'tcx, A>
where
A: Analysis<'tcx>,
{
Borrowed(&'a Results<'tcx, A>),
Owned(Results<'tcx, A>),
}

impl<'tcx, A> Deref for ResultsHandle<'_, 'tcx, A>
where
A: Analysis<'tcx>,
{
type Target = Results<'tcx, A>;

fn deref(&self) -> &Results<'tcx, A> {
match self {
ResultsHandle::Borrowed(borrowed) => borrowed,
ResultsHandle::Owned(owned) => owned,
}
}
}

/// Allows random access inspection of the results of a dataflow analysis.
///
/// This cursor only has linear performance within a basic block when its statements are visited in
Expand All @@ -19,7 +45,7 @@ where
A: Analysis<'tcx>,
{
body: &'mir mir::Body<'tcx>,
results: Results<'tcx, A>,
results: ResultsHandle<'mir, 'tcx, A>,
state: A::Domain,

pos: CursorPosition,
Expand Down Expand Up @@ -47,13 +73,8 @@ where
self.body
}

/// Unwraps this cursor, returning the underlying `Results`.
pub fn into_results(self) -> Results<'tcx, A> {
self.results
}

/// Returns a new cursor that can inspect `results`.
pub fn new(body: &'mir mir::Body<'tcx>, results: Results<'tcx, A>) -> Self {
pub fn new(body: &'mir mir::Body<'tcx>, results: ResultsHandle<'mir, 'tcx, A>) -> Self {
let bottom_value = results.analysis.bottom_value(body);
ResultsCursor {
body,
Expand Down Expand Up @@ -83,21 +104,11 @@ where
&self.results
}

/// Returns the underlying `Results`.
pub fn mut_results(&mut self) -> &mut Results<'tcx, A> {
&mut self.results
}

/// Returns the `Analysis` used to generate the underlying `Results`.
pub fn analysis(&self) -> &A {
&self.results.analysis
}

/// Returns the `Analysis` used to generate the underlying `Results`.
pub fn mut_analysis(&mut self) -> &mut A {
&mut self.results.analysis
}

/// Resets the cursor to hold the entry set for the given basic block.
///
/// For forward dataflow analyses, this is the dataflow state prior to the first statement.
Expand Down Expand Up @@ -199,7 +210,7 @@ where
let target_effect_index = effect.at_index(target.statement_index);

A::Direction::apply_effects_in_range(
&mut self.results.analysis,
&self.results.analysis,
&mut self.state,
target.block,
block_data,
Expand All @@ -214,8 +225,8 @@ where
///
/// This can be used, e.g., to apply the call return effect directly to the cursor without
/// creating an extra copy of the dataflow state.
pub fn apply_custom_effect(&mut self, f: impl FnOnce(&mut A, &mut A::Domain)) {
f(&mut self.results.analysis, &mut self.state);
pub fn apply_custom_effect(&mut self, f: impl FnOnce(&A, &mut A::Domain)) {
f(&self.results.analysis, &mut self.state);
self.state_needs_reset = true;
}
}
Expand Down
Loading

0 comments on commit 1c188eb

Please sign in to comment.