Skip to content

[WIP] Turn MIR copies into moves #46869

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 4 commits into from
Closed
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
10 changes: 8 additions & 2 deletions src/librustc/mir/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ macro_rules! make_mir_visitor {
ref $($mutability)* inputs,
asm: _ } => {
for output in & $($mutability)* outputs[..] {
self.visit_place(output, PlaceContext::Store, location);
self.visit_place(output, PlaceContext::AsmOutput, location);
}
for input in & $($mutability)* inputs[..] {
self.visit_operand(input, location);
Expand Down Expand Up @@ -835,6 +835,11 @@ pub enum PlaceContext<'tcx> {
// Appears as LHS of an assignment
Store,

// Can often be treated as a Store, but needs to be separate because
// ASM is allowed to read outputs as well, so a Store-AsmOutput sequence
// cannot be simplified the way a Store-Store can be.
AsmOutput,

// Dest of a call
Call,

Expand Down Expand Up @@ -910,7 +915,7 @@ impl<'tcx> PlaceContext<'tcx> {
/// Returns true if this place context represents a use that potentially changes the value.
pub fn is_mutating_use(&self) -> bool {
match *self {
PlaceContext::Store | PlaceContext::Call |
PlaceContext::Store | PlaceContext::AsmOutput | PlaceContext::Call |
PlaceContext::Borrow { kind: BorrowKind::Mut, .. } |
PlaceContext::Projection(Mutability::Mut) |
PlaceContext::Drop => true,
Expand All @@ -932,6 +937,7 @@ impl<'tcx> PlaceContext<'tcx> {
PlaceContext::Projection(Mutability::Not) |
PlaceContext::Copy | PlaceContext::Move => true,
PlaceContext::Borrow { kind: BorrowKind::Mut, .. } | PlaceContext::Store |
PlaceContext::AsmOutput |
PlaceContext::Call | PlaceContext::Projection(Mutability::Mut) |
PlaceContext::Drop | PlaceContext::StorageLive | PlaceContext::StorageDead |
PlaceContext::Validate => false,
Expand Down
4 changes: 4 additions & 0 deletions src/librustc_mir/dataflow/impls/borrows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,10 @@ impl<'a, 'b, 'tcx> FindPlaceUses<'a, 'b, 'tcx> {
// "deep" does validation go?
PlaceContext::Validate => false,

// FIXME: This is here to not change behaviour from before
// AsmOutput existed, but it's not necessarily a pure overwrite.
// so it's possible this should activate the place.
PlaceContext::AsmOutput |
// pure overwrites of an place do not activate it. (note
// PlaceContext::Call is solely about dest place)
PlaceContext::Store | PlaceContext::Call => false,
Expand Down
1 change: 1 addition & 0 deletions src/librustc_mir/transform/check_unsafety.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ impl<'a, 'tcx> Visitor<'tcx> for UnsafetyChecker<'a, 'tcx> {
ty::TyAdt(adt, _) => {
if adt.is_union() {
if context == PlaceContext::Store ||
context == PlaceContext::AsmOutput ||
context == PlaceContext::Drop
{
let elem_ty = match elem {
Expand Down
197 changes: 197 additions & 0 deletions src/librustc_mir/transform/last_use.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![warn(warnings)]

use rustc::mir::*;
use rustc::mir::visit::{MutVisitor, PlaceContext};
use rustc::ty::TyCtxt;
use rustc_data_structures::control_flow_graph::iterate::post_order_from;
use rustc_data_structures::fx::{FxHashMap};
use rustc_data_structures::indexed_set::{IdxSetBuf};
use rustc_data_structures::indexed_vec::Idx;
use transform::{MirPass, MirSource};

pub struct WeakenLastUse;

impl MirPass for WeakenLastUse {
fn run_pass<'a, 'tcx>(&self,
tcx: TyCtxt<'a, 'tcx, 'tcx>,
_src: MirSource,
mir: &mut Mir<'tcx>) {
// We only run when optimizing MIR (at any level).
if tcx.sess.opts.debugging_opts.mir_opt_level == 0 {
return
}

let post_order = post_order_from(mir, START_BLOCK);
let (basic_blocks, local_decls) = mir.basic_blocks_and_local_decls_mut();
let locals_len = local_decls.len();
let mut simplifier = CopySimplifier {
locals_len,
block_needs: FxHashMap::default(),
needs: None,
dups_check: IdxSetBuf::new_empty(locals_len),
bug: None,
};
for bb in post_order {
simplifier.simplify(bb, &mut basic_blocks[bb]);
if let Some((local, location)) = simplifier.bug.take() {
bug!("Local {:?} copied twice in {:?}; last-use logic is wrong. Blocks: {:#?}",
local, location, basic_blocks);
}
}
}
}

struct CopySimplifier {
locals_len: usize,
block_needs: FxHashMap<BasicBlock, IdxSetBuf<Local>>,
needs: Option<IdxSetBuf<Local>>,
dups_check: IdxSetBuf<Local>,
bug: Option<(Local, Location)>,
}

impl CopySimplifier {
fn simplify<'tcx>(&mut self, bb: BasicBlock, data: &mut BasicBlockData<'tcx>) {
self.needs = Some(self.succ_needs(data.terminator()));
debug!("Starting {:?} needing {:?}", bb, self.needs);

let mut location = Location {
block: bb,
statement_index: data.statements.len(),
};
self.dups_check.reset_to_empty();
self.visit_terminator(bb, data.terminator.as_mut().unwrap(), location);
for statement in data.statements.iter_mut().rev() {
location.statement_index -= 1;
self.dups_check.reset_to_empty();
self.visit_statement(bb, statement, location);
}
debug_assert_eq!(location.statement_index, 0);

self.block_needs.insert(bb, self.needs.take().unwrap());
}

fn succ_needs<'tcx>(&mut self, terminator: &Terminator<'tcx>) -> IdxSetBuf<Local> {
let mut needs: Option<IdxSetBuf<Local>> = None;
for bb in terminator.successors().iter() {
if let Some(succ_needs) = self.block_needs.get(bb) {
if let Some(ref mut needs) = needs {
needs.union(succ_needs);
} else {
needs = Some(succ_needs.clone());
}
} else {
// Back edge, so assume it needs everything
return IdxSetBuf::new_filled(self.locals_len);
}
}

needs.unwrap_or_else(|| IdxSetBuf::new_filled(self.locals_len))
}

fn needs(&mut self) -> &mut IdxSetBuf<Local> {
self.needs.as_mut().unwrap()
}
}

impl<'tcx> MutVisitor<'tcx> for CopySimplifier {
fn visit_local(
&mut self,
local: &mut Local,
context: PlaceContext<'tcx>,
_location: Location,
) {
match context {
PlaceContext::Store |
PlaceContext::StorageLive |
PlaceContext::StorageDead => {
self.needs().remove(local);
}

// A call doesn't need its output populated, but also might not
// store a value if the callee panics, so just do nothing here.
// FIXME: Smarter handling of successors in call terminators
// would let this be more precise, but this is sound.
PlaceContext::Call => {}

// While an InlineAsm output is expected to write to the output,
// they can be read-write, so assume we need the preceeding value.
PlaceContext::AsmOutput |
PlaceContext::Projection(..) |
PlaceContext::Borrow { .. } |
PlaceContext::Inspect |
PlaceContext::Copy |
PlaceContext::Move |
PlaceContext::Validate |
PlaceContext::Drop => {
self.needs().add(local);
}
}
}

fn visit_statement(
&mut self,
block: BasicBlock,
statement: &mut Statement<'tcx>,
location: Location,
) {
if let StatementKind::Assign(Place::Local(local), _) = statement.kind {
if !self.needs().contains(&local) {
// All rvalues are side-effect-free, so if nothing needs this
// local, we can just skip this. The local is being referenced
// directly, so must not be borrowed either.
statement.make_nop();
return;
}
}

self.super_statement(block, statement, location);
}

fn visit_operand(
&mut self,
operand: &mut Operand<'tcx>,
location: Location,
) {
if let Operand::Copy(Place::Local(local)) = *operand {
if self.dups_check.contains(&local) && self.bug.is_none() {
self.bug = Some((local, location));
}
if !self.needs().contains(&local) {
*operand = Operand::Move(Place::Local(local));
}
self.dups_check.add(&local);
}

self.super_operand(operand, location)
}

fn visit_terminator_kind(
&mut self,
block: BasicBlock,
kind: &mut TerminatorKind<'tcx>,
location: Location,
) {
match *kind {
TerminatorKind::Unreachable => {
self.needs().clear();
}
TerminatorKind::Return => {
self.needs().clear();
self.needs().add(&Local::new(0));
}
_ => {
self.super_terminator_kind(block, kind, location)
}
}
}
}
2 changes: 2 additions & 0 deletions src/librustc_mir/transform/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ pub mod instcombine;
pub mod copy_prop;
pub mod generator;
pub mod inline;
pub mod last_use;
pub mod lower_128bit;

pub(crate) fn provide(providers: &mut Providers) {
Expand Down Expand Up @@ -256,6 +257,7 @@ fn optimized_mir<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> &'tcx
// Optimizations begin.
inline::Inline,
instcombine::InstCombine,
last_use::WeakenLastUse,
deaggregator::Deaggregator,
copy_prop::CopyPropagation,
remove_noop_landing_pads::RemoveNoopLandingPads,
Expand Down
1 change: 1 addition & 0 deletions src/librustc_mir/transform/promote_consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ impl<'tcx> Visitor<'tcx> for TempCollector<'tcx> {
if *temp == TempState::Undefined {
match context {
PlaceContext::Store |
PlaceContext::AsmOutput |
PlaceContext::Call => {
*temp = TempState::Defined {
location,
Expand Down
3 changes: 3 additions & 0 deletions src/librustc_mir/util/liveness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,9 @@ impl<'tcx> Visitor<'tcx> for DefsUsesVisitor {

PlaceContext::Store |

// This is potentially both a def and a use...
PlaceContext::AsmOutput |

// We let Call define the result in both the success and
// unwind cases. This is not really correct, however it
// does not seem to be observable due to the way that we
Expand Down
1 change: 1 addition & 0 deletions src/librustc_trans/mir/analyze.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ impl<'mir, 'a, 'tcx> Visitor<'tcx> for LocalAnalyzer<'mir, 'a, 'tcx> {

PlaceContext::Inspect |
PlaceContext::Store |
PlaceContext::AsmOutput |
PlaceContext::Borrow { .. } |
PlaceContext::Projection(..) => {
self.mark_as_memory(index);
Expand Down
4 changes: 3 additions & 1 deletion src/test/compile-fail/array_const_index-0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,7 @@ const B: i32 = (&A)[1];
//~| index out of bounds: the len is 0 but the index is 1

fn main() {
let _ = B;
let b = B;

std::mem::drop(b); // force use
}
4 changes: 3 additions & 1 deletion src/test/compile-fail/array_const_index-1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,7 @@ const B: i32 = A[1];
//~| index out of bounds: the len is 0 but the index is 1

fn main() {
let _ = B;
let b = B;

std::mem::drop(b); // force use
}
4 changes: 3 additions & 1 deletion src/test/compile-fail/const-slice-oob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,7 @@ const BAR: u32 = FOO[5];
//~| index out of bounds: the len is 3 but the index is 5

fn main() {
let _ = BAR;
let b = BAR;

std::mem::drop(b); // force use
}
1 change: 1 addition & 0 deletions src/test/compile-fail/huge-array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

fn generic<T: Copy>(t: T) {
let s: [T; 1518600000] = [t; 1518600000];
std::mem::drop(s[0]); // force use
}

fn main() {
Expand Down
2 changes: 2 additions & 0 deletions src/test/compile-fail/huge-enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
#[cfg(target_pointer_width = "32")]
fn main() {
let big: Option<[u32; (1<<29)-1]> = None;
std::mem::drop(big); // force use
}

#[cfg(target_pointer_width = "64")]
fn main() {
let big: Option<[u32; (1<<45)-1]> = None;
std::mem::drop(big); // force use
}
1 change: 1 addition & 0 deletions src/test/compile-fail/huge-struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,5 @@ struct S1M<T> { val: S1k<S1k<T>> }

fn main() {
let fat: Option<S1M<S1M<S1M<u32>>>> = None;
std::mem::drop(fat); // force use
}
2 changes: 2 additions & 0 deletions src/test/compile-fail/issue-15919.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
#[cfg(target_pointer_width = "32")]
fn main() {
let x = [0usize; 0xffff_ffff];
std::mem::drop(x[0]); // force use
}

#[cfg(target_pointer_width = "64")]
fn main() {
let x = [0usize; 0xffff_ffff_ffff_ffff];
std::mem::drop(x[0]); // force use
}
4 changes: 2 additions & 2 deletions src/test/mir-opt/copy_propagation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ fn main() {
// START rustc.test.CopyPropagation.before.mir
// bb0: {
// ...
// _3 = _1;
// _3 = move _1;
// ...
// _2 = move _3;
// ...
// _4 = _2;
// _4 = move _2;
// _0 = move _4;
// ...
// return;
Expand Down
Loading