Skip to content

CopyProp: Propagate moves of constants #120689

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 2 commits into from
Closed
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
46 changes: 33 additions & 13 deletions compiler/rustc_mir_transform/src/copy_prop.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use rustc_data_structures::fx::FxIndexMap;
use rustc_index::bit_set::BitSet;
use rustc_index::IndexSlice;
use rustc_middle::mir::visit::*;
Expand Down Expand Up @@ -37,6 +38,8 @@ fn propagate_ssa<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {

let fully_moved = fully_moved_locals(&ssa, body);
debug!(?fully_moved);
let const_locals = const_locals(&ssa, body);
debug!(?const_locals);

let mut storage_to_remove = BitSet::new_empty(fully_moved.domain_size());
for (local, &head) in ssa.copy_classes().iter_enumerated() {
Expand All @@ -45,12 +48,13 @@ fn propagate_ssa<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
}
}

let any_replacement = ssa.copy_classes().iter_enumerated().any(|(l, &h)| l != h);
let any_replacement = !storage_to_remove.is_empty() || !const_locals.is_empty();

Replacer {
tcx,
copy_classes: ssa.copy_classes(),
fully_moved,
const_locals,
borrowed_locals,
storage_to_remove,
}
Expand All @@ -75,31 +79,44 @@ fn fully_moved_locals(ssa: &SsaLocals, body: &Body<'_>) -> BitSet<Local> {
let mut fully_moved = BitSet::new_filled(body.local_decls.len());

for (_, rvalue, _) in ssa.assignments(body) {
let (Rvalue::Use(Operand::Copy(place) | Operand::Move(place))
| Rvalue::CopyForDeref(place)) = rvalue
else {
let (Rvalue::Use(Operand::Copy(place)) | Rvalue::CopyForDeref(place)) = rvalue else {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Given that the Operand::Move case does nothing (due to the check below), we can remove that here and remove the duplicated check below

continue;
};

let Some(rhs) = place.as_local() else { continue };
if !ssa.is_ssa(rhs) {
continue;
}

if let Rvalue::Use(Operand::Copy(_)) | Rvalue::CopyForDeref(_) = rvalue {
fully_moved.remove(rhs);
}
fully_moved.remove(rhs);
}

ssa.meet_copy_equivalence(&mut fully_moved);

fully_moved
}

/// Finds all locals that are only assigned to once with a deterministic constant
#[instrument(level = "trace", skip(ssa, body))]
fn const_locals<'body, 'tcx>(
ssa: &'body SsaLocals,
body: &'body Body<'tcx>,
) -> FxIndexMap<Local, ConstOperand<'tcx>> {
let mut const_locals = FxIndexMap::default();
for (local, rvalue, _) in ssa.assignments(body) {
let Rvalue::Use(Operand::Constant(val)) = rvalue else { continue };
if !val.const_.is_deterministic() {
continue;
}
const_locals.insert(local, **val);
}
const_locals
}

/// Utility to help performing substitution of `*pattern` by `target`.
struct Replacer<'a, 'tcx> {
tcx: TyCtxt<'tcx>,
fully_moved: BitSet<Local>,
const_locals: FxIndexMap<Local, ConstOperand<'tcx>>,
storage_to_remove: BitSet<Local>,
borrowed_locals: BitSet<Local>,
copy_classes: &'a IndexSlice<Local, Local>,
Expand Down Expand Up @@ -151,12 +168,15 @@ impl<'tcx> MutVisitor<'tcx> for Replacer<'_, 'tcx> {
}

fn visit_operand(&mut self, operand: &mut Operand<'tcx>, loc: Location) {
if let Operand::Move(place) = *operand
if let Operand::Move(place) = *operand {
// A move out of a projection of a copy is equivalent to a copy of the original projection.
&& !place.is_indirect_first_projection()
&& !self.fully_moved.contains(place.local)
{
*operand = Operand::Copy(place);
if !place.is_indirect_first_projection() && !self.fully_moved.contains(place.local) {
*operand = Operand::Copy(place);
} else if let Some(local) = place.as_local()
&& let Some(val) = self.const_locals.get(&local)
{
*operand = Operand::Constant(Box::new(*val))
}
}
self.super_operand(operand, loc);
}
Expand Down