Skip to content

Commit

Permalink
Auto merge of #66703 - matthewjasper:drop-trees, r=<try>
Browse files Browse the repository at this point in the history
[PERF] further drop changes

r? @ghost
  • Loading branch information
bors committed Nov 24, 2019
2 parents b56b239 + f09a177 commit a707b15
Show file tree
Hide file tree
Showing 48 changed files with 1,709 additions and 1,403 deletions.
32 changes: 26 additions & 6 deletions src/librustc/mir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1088,8 +1088,18 @@ pub enum TerminatorKind<'tcx> {
/// Indicates a terminator that can never be reached.
Unreachable,

/// Drop the `Place`.
Drop { location: Place<'tcx>, target: BasicBlock, unwind: Option<BasicBlock> },
/// Drop the `Place`, possibly conditioned on a flag being true.
Drop {
location: Place<'tcx>,
/// Whether to drop the value.
///
/// Before drop elaboration this is always `None. After drop elaboration
/// If this is `None` then the drop is unconditional, otherwise the drop
/// is only evaluated when the flag is true.
flag: Option<Place<'tcx>>,
target: BasicBlock,
unwind: Option<BasicBlock>,
},

/// Drop the `Place` and assign the new value over it. This ensures
/// that the assignment to `P` occurs *even if* the destructor for
Expand Down Expand Up @@ -1464,7 +1474,10 @@ impl<'tcx> TerminatorKind<'tcx> {
Abort => write!(fmt, "abort"),
Yield { ref value, .. } => write!(fmt, "_1 = suspend({:?})", value),
Unreachable => write!(fmt, "unreachable"),
Drop { ref location, .. } => write!(fmt, "drop({:?})", location),
Drop { ref location, flag: Some(ref flag), .. } => {
write!(fmt, "if {:?} drop({:?})", flag, location)
}
Drop { ref location, flag: None, .. } => write!(fmt, "drop({:?})", location),
DropAndReplace { ref location, ref value, .. } => {
write!(fmt, "replace({:?} <- {:?})", location, value)
}
Expand Down Expand Up @@ -2967,8 +2980,13 @@ impl<'tcx> TypeFoldable<'tcx> for Terminator<'tcx> {
values: values.clone(),
targets: targets.clone(),
},
Drop { ref location, target, unwind } => {
Drop { location: location.fold_with(folder), target, unwind }
Drop { ref location, ref flag, target, unwind } => {
Drop {
location: location.fold_with(folder),
flag: flag.fold_with(folder),
target,
unwind,
}
}
DropAndReplace { ref location, ref value, target, unwind } => DropAndReplace {
location: location.fold_with(folder),
Expand Down Expand Up @@ -3025,7 +3043,9 @@ impl<'tcx> TypeFoldable<'tcx> for Terminator<'tcx> {
SwitchInt { ref discr, switch_ty, .. } => {
discr.visit_with(visitor) || switch_ty.visit_with(visitor)
}
Drop { ref location, .. } => location.visit_with(visitor),
Drop { ref location, ref flag, .. } => {
location.visit_with(visitor) || flag.visit_with(visitor)
},
DropAndReplace { ref location, ref value, .. } => {
location.visit_with(visitor) || value.visit_with(visitor)
}
Expand Down
10 changes: 9 additions & 1 deletion src/librustc/mir/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -432,14 +432,22 @@ macro_rules! make_mir_visitor {

TerminatorKind::Drop {
location,
flag,
target: _,
unwind: _,
} => {
self.visit_place(
location,
PlaceContext::MutatingUse(MutatingUseContext::Drop),
source_location
source_location,
);
if let Some(flag) = flag {
self.visit_place(
flag,
PlaceContext::NonMutatingUse(NonMutatingUseContext::Inspect),
source_location,
);
}
}

TerminatorKind::DropAndReplace {
Expand Down
8 changes: 6 additions & 2 deletions src/librustc/ty/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,14 @@ impl<'tcx> InstanceDef<'tcx> {
return true
}
if let ty::InstanceDef::DropGlue(..) = *self {
// Drop glue wants to be instantiated at every codegen
// Drop glue generally wants to be instantiated at every codegen
// unit, but without an #[inline] hint. We should make this
// available to normal end-users.
return true
//
// When compiling with incremental, we can generate a lot of
// codegen units. Including drop glue into all of them has a
// considerable compile time cost.
return tcx.sess.opts.incremental.is_none();
}
tcx.codegen_fn_attrs(self.def_id()).requests_inline()
}
Expand Down
24 changes: 22 additions & 2 deletions src/librustc_codegen_ssa/mir/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,8 +321,10 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
helper: TerminatorCodegenHelper<'b, 'tcx>,
mut bx: Bx,
location: &mir::Place<'tcx>,
flag: &Option<mir::Place<'tcx>>,
target: mir::BasicBlock,
unwind: Option<mir::BasicBlock>,
source_info: mir::SourceInfo,
) {
let ty = location.ty(self.mir, bx.tcx()).ty;
let ty = self.monomorphize(&ty);
Expand All @@ -335,6 +337,16 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
return
}

if let Some(flag) = flag {
let flag = self.codegen_consume(&mut bx, &flag.as_ref()).immediate();
let lltarget = helper.llblock(self, target);
let drop_block = self.new_block("drop");
helper.maybe_sideeffect(self.mir, &mut bx, &[target]);
bx.cond_br(flag, drop_block.llbb(), lltarget);
bx = drop_block;
self.set_debug_loc(&mut bx, source_info);
}

let place = self.codegen_place(&mut bx, &location.as_ref());
let (args1, args2);
let mut args = if let Some(llextra) = place.llextra {
Expand Down Expand Up @@ -854,8 +866,16 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
bx.unreachable();
}

mir::TerminatorKind::Drop { ref location, target, unwind } => {
self.codegen_drop_terminator(helper, bx, location, target, unwind);
mir::TerminatorKind::Drop { ref location, ref flag, target, unwind } => {
self.codegen_drop_terminator(
helper,
bx,
location,
flag,
target,
unwind,
terminator.source_info,
);
}

mir::TerminatorKind::Assert { ref cond, expected, ref msg, target, cleanup } => {
Expand Down
1 change: 1 addition & 0 deletions src/librustc_mir/borrow_check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,7 @@ impl<'cx, 'tcx> DataflowResultsConsumer<'cx, 'tcx> for MirBorrowckCtxt<'cx, 'tcx
}
TerminatorKind::Drop {
location: ref drop_place,
flag: _,
target: _,
unwind: _,
} => {
Expand Down
1 change: 1 addition & 0 deletions src/librustc_mir/borrow_check/nll/invalidation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ impl<'cx, 'tcx> Visitor<'tcx> for InvalidationGenerator<'cx, 'tcx> {
}
TerminatorKind::Drop {
location: ref drop_place,
flag: _,
target: _,
unwind: _,
} => {
Expand Down
74 changes: 45 additions & 29 deletions src/librustc_mir/build/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@ use crate::build::{BlockAnd, BlockAndExtension, BlockFrame, Builder};
use crate::build::ForGuard::OutsideGuard;
use crate::build::matches::ArmHasGuard;
use crate::hair::*;
use rustc::middle::region;
use rustc::mir::*;
use rustc::hir;
use syntax_pos::Span;

impl<'a, 'tcx> Builder<'a, 'tcx> {
pub fn ast_block(&mut self,
destination: &Place<'tcx>,
block: BasicBlock,
ast_block: &'tcx hir::Block,
source_info: SourceInfo)
-> BlockAnd<()> {
pub fn ast_block(
&mut self,
destination: &Place<'tcx>,
scope: Option<region::Scope>,
block: BasicBlock,
ast_block: &'tcx hir::Block,
source_info: SourceInfo,
) -> BlockAnd<()> {
let Block {
region_scope,
opt_destruction_scope,
Expand All @@ -21,37 +24,50 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
expr,
targeted_by_break,
safety_mode
} =
self.hir.mirror(ast_block);
} = self.hir.mirror(ast_block);
self.in_opt_scope(opt_destruction_scope.map(|de|(de, source_info)), move |this| {
this.in_scope((region_scope, source_info), LintLevel::Inherited, move |this| {
if targeted_by_break {
// This is a `break`-able block
let exit_block = this.cfg.start_new_block();
let block_exit = this.in_breakable_scope(
None, exit_block, destination.clone(), |this| {
this.ast_block_stmts(destination, block, span, stmts, expr,
safety_mode)
});
this.cfg.terminate(unpack!(block_exit), source_info,
TerminatorKind::Goto { target: exit_block });
exit_block.unit()
this.in_breakable_scope(
None,
destination.clone(),
scope,
span,
|this| Some(this.ast_block_stmts(
destination,
scope,
block,
span,
stmts,
expr,
safety_mode,
)),
)
} else {
this.ast_block_stmts(destination, block, span, stmts, expr,
safety_mode)
this.ast_block_stmts(
destination,
scope,
block,
span,
stmts,
expr,
safety_mode,
)
}
})
})
}

fn ast_block_stmts(&mut self,
destination: &Place<'tcx>,
mut block: BasicBlock,
span: Span,
stmts: Vec<StmtRef<'tcx>>,
expr: Option<ExprRef<'tcx>>,
safety_mode: BlockSafety)
-> BlockAnd<()> {
fn ast_block_stmts(
&mut self,
destination: &Place<'tcx>,
scope: Option<region::Scope>,
mut block: BasicBlock,
span: Span,
stmts: Vec<StmtRef<'tcx>>,
expr: Option<ExprRef<'tcx>>,
safety_mode: BlockSafety,
) -> BlockAnd<()> {
let this = self;

// This convoluted structure is to avoid using recursion as we walk down a list
Expand Down Expand Up @@ -177,7 +193,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
this.block_context.currently_ignores_tail_results();
this.block_context.push(BlockFrame::TailExpr { tail_result_is_ignored });

unpack!(block = this.into(destination, block, expr));
unpack!(block = this.into(destination, scope, block, expr));
let popped = this.block_context.pop();

assert!(popped.map_or(false, |bf|bf.is_tail_expr()));
Expand Down
11 changes: 7 additions & 4 deletions src/librustc_mir/build/expr/as_rvalue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,14 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
this.cfg
.push_assign(block, source_info, &Place::from(result), box_);

// initialize the box contents:
// Initialize the box contents. No scope is needed since the
// `Box` is already scheduled to be dropped.
unpack!(
block = this.into(
&this.hir.tcx().mk_place_deref(Place::from(result)),
block, value
None,
block,
value,
)
);
block.and(Rvalue::Use(Operand::Move(Place::from(result))))
Expand Down Expand Up @@ -257,14 +260,14 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
ExprKind::Yield { value } => {
let value = unpack!(block = this.as_operand(block, scope, value));
let resume = this.cfg.start_new_block();
let cleanup = this.generator_drop_cleanup();
this.generator_drop_cleanup(block);
this.cfg.terminate(
block,
source_info,
TerminatorKind::Yield {
value: value,
resume: resume,
drop: cleanup,
drop: None,
},
);
resume.and(this.unit_rvalue())
Expand Down
11 changes: 1 addition & 10 deletions src/librustc_mir/build/expr/as_temp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,16 +114,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
}
}

unpack!(block = this.into(temp_place, block, expr));

if let Some(temp_lifetime) = temp_lifetime {
this.schedule_drop(
expr_span,
temp_lifetime,
temp,
DropKind::Value,
);
}
unpack!(block = this.into(temp_place, temp_lifetime, block, expr));

block.and(temp)
}
Expand Down
Loading

0 comments on commit a707b15

Please sign in to comment.