Skip to content

Commit

Permalink
Auto merge of #107449 - saethlin:enable-copyprop, r=oli-obk
Browse files Browse the repository at this point in the history
Enable CopyProp

r? `@tmiasko`

`@rustbot` label +A-mir-opt
  • Loading branch information
bors committed Feb 16, 2023
2 parents dc7a676 + 37a875c commit 639377e
Show file tree
Hide file tree
Showing 18 changed files with 342 additions and 267 deletions.
1 change: 1 addition & 0 deletions compiler/rustc_middle/src/ty/adt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,7 @@ impl<'tcx> AdtDef<'tcx> {
}

/// Return the index of `VariantDef` given a variant id.
#[inline]
pub fn variant_index_with_id(self, vid: DefId) -> VariantIdx {
self.variants()
.iter_enumerated()
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_mir_transform/src/copy_prop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub struct CopyProp;

impl<'tcx> MirPass<'tcx> for CopyProp {
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
sess.mir_opt_level() >= 4
sess.mir_opt_level() >= 1
}

#[instrument(level = "trace", skip(self, tcx, body))]
Expand Down Expand Up @@ -96,7 +96,7 @@ fn fully_moved_locals(ssa: &SsaLocals, body: &Body<'_>) -> BitSet<Local> {
fully_moved
}

/// Utility to help performing subtitution of `*pattern` by `target`.
/// Utility to help performing substitution of `*pattern` by `target`.
struct Replacer<'a, 'tcx> {
tcx: TyCtxt<'tcx>,
fully_moved: BitSet<Local>,
Expand Down
43 changes: 39 additions & 4 deletions compiler/rustc_mir_transform/src/ssa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,33 @@ pub struct SsaLocals {
copy_classes: IndexVec<Local, Local>,
}

/// We often encounter MIR bodies with 1 or 2 basic blocks. In those cases, it's unnecessary to
/// actually compute dominators, we can just compare block indices because bb0 is always the first
/// block, and in any body all other blocks are always always dominated by bb0.
struct SmallDominators {
inner: Option<Dominators<BasicBlock>>,
}

trait DomExt {
fn dominates(self, _other: Self, dominators: &SmallDominators) -> bool;
}

impl DomExt for Location {
fn dominates(self, other: Location, dominators: &SmallDominators) -> bool {
if self.block == other.block {
self.statement_index <= other.statement_index
} else {
dominators.dominates(self.block, other.block)
}
}
}

impl SmallDominators {
fn dominates(&self, dom: BasicBlock, node: BasicBlock) -> bool {
if let Some(inner) = &self.inner { inner.dominates(dom, node) } else { dom < node }
}
}

impl SsaLocals {
pub fn new<'tcx>(
tcx: TyCtxt<'tcx>,
Expand All @@ -29,7 +56,9 @@ impl SsaLocals {
let assignment_order = Vec::new();

let assignments = IndexVec::from_elem(Set1::Empty, &body.local_decls);
let dominators = body.basic_blocks.dominators();
let dominators =
if body.basic_blocks.len() > 2 { Some(body.basic_blocks.dominators()) } else { None };
let dominators = SmallDominators { inner: dominators };
let mut visitor = SsaVisitor { assignments, assignment_order, dominators };

for (local, decl) in body.local_decls.iter_enumerated() {
Expand All @@ -41,8 +70,14 @@ impl SsaLocals {
}
}

for (bb, data) in traversal::reverse_postorder(body) {
visitor.visit_basic_block_data(bb, data);
if body.basic_blocks.len() > 2 {
for (bb, data) in traversal::reverse_postorder(body) {
visitor.visit_basic_block_data(bb, data);
}
} else {
for (bb, data) in body.basic_blocks.iter_enumerated() {
visitor.visit_basic_block_data(bb, data);
}
}

for var_debug_info in &body.var_debug_info {
Expand Down Expand Up @@ -139,7 +174,7 @@ enum LocationExtended {
}

struct SsaVisitor {
dominators: Dominators<BasicBlock>,
dominators: SmallDominators,
assignments: IndexVec<Local, Set1<LocationExtended>>,
assignment_order: Vec<Local>,
}
Expand Down
2 changes: 1 addition & 1 deletion tests/codegen/move-operands.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// compile-flags: -C no-prepopulate-passes -Zmir-enable-passes=+DestinationPropagation
// compile-flags: -C no-prepopulate-passes -Zmir-enable-passes=+DestinationPropagation,-CopyProp

#![crate_type = "lib"]

Expand Down
Loading

0 comments on commit 639377e

Please sign in to comment.