Skip to content

More improvents to dest prop #95700

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 3 commits into from
Closed
Changes from 1 commit
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
64 changes: 40 additions & 24 deletions compiler/rustc_mir_transform/src/dest_prop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,11 @@ use rustc_index::{
bit_set::{BitMatrix, BitSet},
vec::IndexVec,
};
use rustc_middle::mir::visit::{MutVisitor, PlaceContext, Visitor};
use rustc_middle::mir::visit::{MutVisitor, NonMutatingUseContext, PlaceContext, Visitor};
use rustc_middle::mir::{dump_mir, PassWhere};
use rustc_middle::mir::{
traversal, Body, InlineAsmOperand, Local, LocalKind, Location, Operand, Place, PlaceElem,
Rvalue, Statement, StatementKind, Terminator, TerminatorKind,
traversal, BasicBlock, Body, InlineAsmOperand, Local, LocalKind, Location, Operand, Place,
PlaceElem, Rvalue, Statement, StatementKind, Terminator, TerminatorKind,
};
use rustc_middle::ty::TyCtxt;
use rustc_mir_dataflow::impls::MaybeLiveLocals;
Expand Down Expand Up @@ -368,6 +368,12 @@ struct Conflicts<'a> {
}

impl<'a> Conflicts<'a> {
/// Build the conflicts table from the MIR body
///
/// This will mark two locals as conflicting, if they are ever live at the same time with a
/// single exception: Assign statements with `use` rvalues. If the optimization merges two
/// places, then it must promise to remove all assignments between those places in either
/// direction.
fn build<'tcx>(
tcx: TyCtxt<'tcx>,
body: &'_ Body<'tcx>,
Expand Down Expand Up @@ -488,10 +494,36 @@ impl<'a> Conflicts<'a> {
/// and must not be merged.
fn record_statement_conflicts(&mut self, stmt: &Statement<'_>) {
match &stmt.kind {
// While the left and right sides of an assignment must not overlap, we do not mark
// conflicts here as that would make this optimization useless. When we optimize, we
// eliminate the resulting self-assignments automatically.
StatementKind::Assign(_) => {}
StatementKind::Assign(v) => {
let p = v.0;
let rvalue = &v.1;
if p.is_indirect() {
return;
}
let lhs_local = p.local;

struct RvalueVisitor<'a, 'b>(&'a mut Conflicts<'b>, Local);

impl<'a, 'b, 'tcx> Visitor<'tcx> for RvalueVisitor<'a, 'b> {
fn visit_local(&mut self, local: &Local, _: PlaceContext, _: Location) {
self.0.record_local_conflict(self.1, *local, "intra-assignment");
}
}

let mut v = RvalueVisitor(self, lhs_local);
let location = Location { block: BasicBlock::MAX, statement_index: 0 };
if let Rvalue::Use(op) = rvalue {
if let Some(rhs_place) = op.place() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please leave a comment that this explicitly skips the place and only looks for index places in the projection

v.visit_projection(
rhs_place.as_ref(),
PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy),
location,
);
}
} else {
v.visit_rvalue(rvalue, location);
}
}

StatementKind::SetDiscriminant { .. }
| StatementKind::StorageLive(..)
Expand All @@ -507,23 +539,6 @@ impl<'a> Conflicts<'a> {

fn record_terminator_conflicts(&mut self, term: &Terminator<'_>) {
match &term.kind {
TerminatorKind::DropAndReplace {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why don't we need this anymore? Because we run so late this variant doesn't occur? Should probably group the ones that can't happen into a => bug!() arm

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DropAndReplace gets removed during drop elaboration.

place: dropped_place,
value,
target: _,
unwind: _,
} => {
if let Some(place) = value.place()
&& !place.is_indirect()
&& !dropped_place.is_indirect()
{
self.record_local_conflict(
place.local,
dropped_place.local,
"DropAndReplace operand overlap",
);
}
}
TerminatorKind::Yield { value, resume: _, resume_arg, drop: _ } => {
if let Some(place) = value.place() {
if !place.is_indirect() && !resume_arg.is_indirect() {
Expand Down Expand Up @@ -654,6 +669,7 @@ impl<'a> Conflicts<'a> {
}

TerminatorKind::Goto { .. }
| TerminatorKind::DropAndReplace { .. }
| TerminatorKind::Call { destination: None, .. }
| TerminatorKind::SwitchInt { .. }
| TerminatorKind::Resume
Expand Down