Skip to content
Merged
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
3 changes: 0 additions & 3 deletions compiler/rustc_borrowck/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -803,7 +803,6 @@ pub(crate) struct MirBorrowckCtxt<'a, 'diag, 'tcx> {
impl<'a, 'tcx> ResultsVisitor<'tcx, Borrowck<'a, 'tcx>> for MirBorrowckCtxt<'a, '_, 'tcx> {
fn visit_after_early_statement_effect(
&mut self,
_analysis: &Borrowck<'a, 'tcx>,
state: &BorrowckDomain,
stmt: &Statement<'tcx>,
location: Location,
Expand Down Expand Up @@ -878,7 +877,6 @@ impl<'a, 'tcx> ResultsVisitor<'tcx, Borrowck<'a, 'tcx>> for MirBorrowckCtxt<'a,

fn visit_after_early_terminator_effect(
&mut self,
_analysis: &Borrowck<'a, 'tcx>,
state: &BorrowckDomain,
term: &Terminator<'tcx>,
loc: Location,
Expand Down Expand Up @@ -991,7 +989,6 @@ impl<'a, 'tcx> ResultsVisitor<'tcx, Borrowck<'a, 'tcx>> for MirBorrowckCtxt<'a,

fn visit_after_primary_terminator_effect(
&mut self,
_analysis: &Borrowck<'a, 'tcx>,
state: &BorrowckDomain,
term: &Terminator<'tcx>,
loc: Location,
Expand Down
16 changes: 8 additions & 8 deletions compiler/rustc_mir_dataflow/src/framework/direction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,16 +144,16 @@ impl Direction for Backward {
let loc = Location { block, statement_index: block_data.statements.len() };
let term = block_data.terminator();
analysis.apply_early_terminator_effect(state, term, loc);
vis.visit_after_early_terminator_effect(analysis, state, term, loc);
vis.visit_after_early_terminator_effect(state, term, loc);
analysis.apply_primary_terminator_effect(state, term, loc);
vis.visit_after_primary_terminator_effect(analysis, state, term, loc);
vis.visit_after_primary_terminator_effect(state, term, loc);

for (statement_index, stmt) in block_data.statements.iter().enumerate().rev() {
let loc = Location { block, statement_index };
analysis.apply_early_statement_effect(state, stmt, loc);
vis.visit_after_early_statement_effect(analysis, state, stmt, loc);
vis.visit_after_early_statement_effect(state, stmt, loc);
analysis.apply_primary_statement_effect(state, stmt, loc);
vis.visit_after_primary_statement_effect(analysis, state, stmt, loc);
vis.visit_after_primary_statement_effect(state, stmt, loc);
}
}
}
Expand Down Expand Up @@ -259,16 +259,16 @@ impl Direction for Forward {
for (statement_index, stmt) in block_data.statements.iter().enumerate() {
let loc = Location { block, statement_index };
analysis.apply_early_statement_effect(state, stmt, loc);
vis.visit_after_early_statement_effect(analysis, state, stmt, loc);
vis.visit_after_early_statement_effect(state, stmt, loc);
analysis.apply_primary_statement_effect(state, stmt, loc);
vis.visit_after_primary_statement_effect(analysis, state, stmt, loc);
vis.visit_after_primary_statement_effect(state, stmt, loc);
}

let loc = Location { block, statement_index: block_data.statements.len() };
let term = block_data.terminator();
analysis.apply_early_terminator_effect(state, term, loc);
vis.visit_after_early_terminator_effect(analysis, state, term, loc);
vis.visit_after_early_terminator_effect(state, term, loc);
analysis.apply_primary_terminator_effect(state, term, loc);
vis.visit_after_primary_terminator_effect(analysis, state, term, loc);
vis.visit_after_primary_terminator_effect(state, term, loc);
}
}
33 changes: 15 additions & 18 deletions compiler/rustc_mir_dataflow/src/framework/graphviz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ where
fn write_node_label(
&mut self,
block: BasicBlock,
diffs: StateDiffCollector<A::Domain>,
diffs: StateDiffCollector<'_, 'tcx, A>,
) -> io::Result<Vec<u8>> {
use std::io::Write;

Expand Down Expand Up @@ -527,7 +527,7 @@ where
&mut self,
w: &mut impl io::Write,
block: BasicBlock,
diffs: StateDiffCollector<A::Domain>,
diffs: StateDiffCollector<'_, 'tcx, A>,
) -> io::Result<()> {
let mut diffs_before = diffs.before.map(|v| v.into_iter());
let mut diffs_after = diffs.after.into_iter();
Expand Down Expand Up @@ -627,24 +627,25 @@ where
}
}

struct StateDiffCollector<D> {
prev_state: D,
struct StateDiffCollector<'a, 'tcx, A: Analysis<'tcx>> {
analysis: &'a A,
prev_state: A::Domain,
before: Option<Vec<String>>,
after: Vec<String>,
}

impl<D: Clone> StateDiffCollector<D> {
fn run<'tcx, A>(
impl<'a, 'tcx, A: Analysis<'tcx>> StateDiffCollector<'a, 'tcx, A> {
fn run(
body: &Body<'tcx>,
block: BasicBlock,
results: &Results<'tcx, A>,
results: &'a Results<'tcx, A>,
style: OutputStyle,
) -> Self
where
A: Analysis<'tcx, Domain = D>,
D: DebugWithContext<A>,
A::Domain: DebugWithContext<A>,
{
let mut collector = StateDiffCollector {
analysis: &results.analysis,
prev_state: results.entry_states[block].clone(),
after: vec![],
before: (style == OutputStyle::BeforeAndAfter).then_some(vec![]),
Expand All @@ -655,56 +656,52 @@ impl<D: Clone> StateDiffCollector<D> {
}
}

impl<'tcx, A> ResultsVisitor<'tcx, A> for StateDiffCollector<A::Domain>
impl<'a, 'tcx, A: Analysis<'tcx>> ResultsVisitor<'tcx, A> for StateDiffCollector<'a, 'tcx, A>
where
A: Analysis<'tcx>,
A::Domain: DebugWithContext<A>,
{
fn visit_after_early_statement_effect(
&mut self,
analysis: &A,
state: &A::Domain,
_statement: &mir::Statement<'tcx>,
_location: Location,
) {
if let Some(before) = self.before.as_mut() {
before.push(diff_pretty(state, &self.prev_state, analysis));
before.push(diff_pretty(state, &self.prev_state, self.analysis));
self.prev_state.clone_from(state)
}
}

fn visit_after_primary_statement_effect(
&mut self,
analysis: &A,
state: &A::Domain,
_statement: &mir::Statement<'tcx>,
_location: Location,
) {
self.after.push(diff_pretty(state, &self.prev_state, analysis));
self.after.push(diff_pretty(state, &self.prev_state, self.analysis));
self.prev_state.clone_from(state)
}

fn visit_after_early_terminator_effect(
&mut self,
analysis: &A,
state: &A::Domain,
_terminator: &mir::Terminator<'tcx>,
_location: Location,
) {
if let Some(before) = self.before.as_mut() {
before.push(diff_pretty(state, &self.prev_state, analysis));
before.push(diff_pretty(state, &self.prev_state, self.analysis));
self.prev_state.clone_from(state)
}
}

fn visit_after_primary_terminator_effect(
&mut self,
analysis: &A,
state: &A::Domain,
_terminator: &mir::Terminator<'tcx>,
_location: Location,
) {
self.after.push(diff_pretty(state, &self.prev_state, analysis));
self.after.push(diff_pretty(state, &self.prev_state, self.analysis));
self.prev_state.clone_from(state)
}
}
Expand Down
4 changes: 0 additions & 4 deletions compiler/rustc_mir_dataflow/src/framework/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ where
/// Called after the "early" effect of the given statement is applied to `state`.
fn visit_after_early_statement_effect(
&mut self,
_analysis: &A,
_state: &A::Domain,
_statement: &mir::Statement<'tcx>,
_location: Location,
Expand All @@ -47,7 +46,6 @@ where
/// Called after the "primary" effect of the given statement is applied to `state`.
fn visit_after_primary_statement_effect(
&mut self,
_analysis: &A,
_state: &A::Domain,
_statement: &mir::Statement<'tcx>,
_location: Location,
Expand All @@ -57,7 +55,6 @@ where
/// Called after the "early" effect of the given terminator is applied to `state`.
fn visit_after_early_terminator_effect(
&mut self,
_analysis: &A,
_state: &A::Domain,
_terminator: &mir::Terminator<'tcx>,
_location: Location,
Expand All @@ -69,7 +66,6 @@ where
/// The `call_return_effect` (if one exists) will *not* be applied to `state`.
fn visit_after_primary_terminator_effect(
&mut self,
_analysis: &A,
_state: &A::Domain,
_terminator: &mir::Terminator<'tcx>,
_location: Location,
Expand Down
2 changes: 0 additions & 2 deletions compiler/rustc_mir_transform/src/coroutine/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,6 @@ struct StorageConflictVisitor<'a> {
impl<'a, 'tcx> ResultsVisitor<'tcx, MaybeRequiresStorage> for StorageConflictVisitor<'a> {
fn visit_after_early_statement_effect(
&mut self,
_analysis: &MaybeRequiresStorage,
state: &DenseBitSet<Local>,
_statement: &Statement<'tcx>,
_loc: Location,
Expand All @@ -311,7 +310,6 @@ impl<'a, 'tcx> ResultsVisitor<'tcx, MaybeRequiresStorage> for StorageConflictVis

fn visit_after_early_terminator_effect(
&mut self,
_analysis: &MaybeRequiresStorage,
state: &DenseBitSet<Local>,
_terminator: &Terminator<'tcx>,
_loc: Location,
Expand Down
35 changes: 15 additions & 20 deletions compiler/rustc_mir_transform/src/dataflow_const_prop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ impl<'tcx> crate::MirPass<'tcx> for DataflowConstProp {
.in_scope(|| ConstAnalysis::new(tcx, body, map).iterate_to_fixpoint(tcx, body, None));

// Collect results and patch the body afterwards.
let mut visitor = Collector::new(tcx, body);
let mut visitor = Collector::new(tcx, body, &const_.analysis.map);
debug_span!("collect").in_scope(|| {
visit_results(body, traversal::reachable(body).map(|(bb, _)| bb), &const_, &mut visitor)
});
Expand Down Expand Up @@ -767,23 +767,24 @@ struct Collector<'a, 'tcx> {
patch: Patch<'tcx>,
local_decls: &'a LocalDecls<'tcx>,
ecx: InterpCx<'tcx, DummyMachine>,
map: &'a Map<'tcx>,
}

impl<'a, 'tcx> Collector<'a, 'tcx> {
pub(crate) fn new(tcx: TyCtxt<'tcx>, body: &'a Body<'tcx>) -> Self {
pub(crate) fn new(tcx: TyCtxt<'tcx>, body: &'a Body<'tcx>, map: &'a Map<'tcx>) -> Self {
Self {
patch: Patch::new(tcx),
local_decls: &body.local_decls,
ecx: InterpCx::new(tcx, DUMMY_SP, body.typing_env(tcx), DummyMachine),
map,
}
}

#[instrument(level = "trace", skip(self, map), ret)]
#[instrument(level = "trace", skip(self), ret)]
fn try_make_constant(
&mut self,
place: Place<'tcx>,
state: &State<FlatSet<Scalar>>,
map: &Map<'tcx>,
) -> Option<Const<'tcx>> {
let ty = place.ty(self.local_decls, self.patch.tcx).ty;
let layout = self.ecx.layout_of(ty).ok()?;
Expand All @@ -796,9 +797,9 @@ impl<'a, 'tcx> Collector<'a, 'tcx> {
return None;
}

let place = map.find(place.as_ref())?;
let place = self.map.find(place.as_ref())?;
if layout.backend_repr.is_scalar()
&& let Some(value) = propagatable_scalar(place, state, map)
&& let Some(value) = propagatable_scalar(place, state, self.map)
{
return Some(Const::Val(ConstValue::Scalar(value), ty));
}
Expand All @@ -807,7 +808,7 @@ impl<'a, 'tcx> Collector<'a, 'tcx> {
let alloc_id = self
.ecx
.intern_with_temp_alloc(layout, |ecx, dest| {
try_write_constant(ecx, dest, place, ty, state, map)
try_write_constant(ecx, dest, place, ty, state, self.map)
})
.discard_err()?;
return Some(Const::Val(ConstValue::Indirect { alloc_id, offset: Size::ZERO }, ty));
Expand Down Expand Up @@ -940,27 +941,24 @@ fn try_write_constant<'tcx>(
}

impl<'tcx> ResultsVisitor<'tcx, ConstAnalysis<'_, 'tcx>> for Collector<'_, 'tcx> {
#[instrument(level = "trace", skip(self, analysis, statement))]
#[instrument(level = "trace", skip(self, statement))]
fn visit_after_early_statement_effect(
&mut self,
analysis: &ConstAnalysis<'_, 'tcx>,
state: &State<FlatSet<Scalar>>,
statement: &Statement<'tcx>,
location: Location,
) {
match &statement.kind {
StatementKind::Assign((_, rvalue)) => {
OperandCollector { state, visitor: self, map: &analysis.map }
.visit_rvalue(rvalue, location);
OperandCollector { state, visitor: self }.visit_rvalue(rvalue, location);
}
_ => (),
}
}

#[instrument(level = "trace", skip(self, analysis, statement))]
#[instrument(level = "trace", skip(self, statement))]
fn visit_after_primary_statement_effect(
&mut self,
analysis: &ConstAnalysis<'_, 'tcx>,
state: &State<FlatSet<Scalar>>,
statement: &Statement<'tcx>,
location: Location,
Expand All @@ -970,7 +968,7 @@ impl<'tcx> ResultsVisitor<'tcx, ConstAnalysis<'_, 'tcx>> for Collector<'_, 'tcx>
// Don't overwrite the assignment if it already uses a constant (to keep the span).
}
StatementKind::Assign((place, _)) => {
if let Some(value) = self.try_make_constant(place, state, &analysis.map) {
if let Some(value) = self.try_make_constant(place, state) {
self.patch.assignments.insert(location, value);
}
}
Expand All @@ -980,13 +978,11 @@ impl<'tcx> ResultsVisitor<'tcx, ConstAnalysis<'_, 'tcx>> for Collector<'_, 'tcx>

fn visit_after_early_terminator_effect(
&mut self,
analysis: &ConstAnalysis<'_, 'tcx>,
state: &State<FlatSet<Scalar>>,
terminator: &Terminator<'tcx>,
location: Location,
) {
OperandCollector { state, visitor: self, map: &analysis.map }
.visit_terminator(terminator, location);
OperandCollector { state, visitor: self }.visit_terminator(terminator, location);
}
}

Expand Down Expand Up @@ -1045,7 +1041,6 @@ impl<'tcx> MutVisitor<'tcx> for Patch<'tcx> {
struct OperandCollector<'a, 'b, 'tcx> {
state: &'a State<FlatSet<Scalar>>,
visitor: &'a mut Collector<'b, 'tcx>,
map: &'a Map<'tcx>,
}

impl<'tcx> Visitor<'tcx> for OperandCollector<'_, '_, 'tcx> {
Expand All @@ -1057,15 +1052,15 @@ impl<'tcx> Visitor<'tcx> for OperandCollector<'_, '_, 'tcx> {
location: Location,
) {
if let PlaceElem::Index(local) = elem
&& let Some(value) = self.visitor.try_make_constant(local.into(), self.state, self.map)
&& let Some(value) = self.visitor.try_make_constant(local.into(), self.state)
{
self.visitor.patch.before_effect.insert((location, local.into()), value);
}
}

fn visit_operand(&mut self, operand: &Operand<'tcx>, location: Location) {
if let Some(place) = operand.place() {
if let Some(value) = self.visitor.try_make_constant(place, self.state, self.map) {
if let Some(value) = self.visitor.try_make_constant(place, self.state) {
self.visitor.patch.before_effect.insert((location, place), value);
} else if !place.projection.is_empty() {
// Try to propagate into `Index` projections.
Expand Down
Loading