-
Notifications
You must be signed in to change notification settings - Fork 13.3k
[MIR] implement zero-on-move #31449
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
Closed
[MIR] implement zero-on-move #31449
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
// Copyright 2012-2014 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. | ||
|
||
use llvm::ValueRef; | ||
use rustc::middle::ty::Ty; | ||
use rustc::mir::repr as mir; | ||
use trans::adt; | ||
use trans::base; | ||
use trans::build; | ||
use trans::common::{self, Block}; | ||
use trans::debuginfo::DebugLoc; | ||
use trans::glue; | ||
use trans::machine; | ||
use trans::type_of; | ||
use trans::type_::Type; | ||
|
||
use super::{MirContext, TempRef}; | ||
|
||
impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> { | ||
pub fn trans_drop(&mut self, | ||
bcx: Block<'bcx, 'tcx>, | ||
value: &mir::Lvalue<'tcx>, | ||
target: mir::BasicBlock, | ||
unwind: Option<mir::BasicBlock>) { | ||
let lvalue = self.trans_lvalue(bcx, value); | ||
let ty = lvalue.ty.to_ty(bcx.tcx()); | ||
// Double check for necessity to drop | ||
if !glue::type_needs_drop(bcx.tcx(), ty) { | ||
build::Br(bcx, self.llblock(target), DebugLoc::None); | ||
return; | ||
} | ||
let drop_fn = glue::get_drop_glue(bcx.ccx(), ty); | ||
let drop_ty = glue::get_drop_glue_type(bcx.ccx(), ty); | ||
let llvalue = if drop_ty != ty { | ||
build::PointerCast(bcx, lvalue.llval, | ||
type_of::type_of(bcx.ccx(), drop_ty).ptr_to()) | ||
} else { | ||
lvalue.llval | ||
}; | ||
if let Some(unwind) = unwind { | ||
// block cannot be cleanup in this case, so a regular block is fine | ||
let intermediate_bcx = bcx.fcx.new_block("", None); | ||
let uwbcx = self.bcx(unwind); | ||
let unwind = self.make_landing_pad(uwbcx); | ||
// FIXME: it could be possible to do zeroing before invoking here if the drop glue | ||
// didn’t code in the checks inside itself. | ||
build::Invoke(bcx, | ||
drop_fn, | ||
&[llvalue], | ||
intermediate_bcx.llbb, | ||
unwind.llbb, | ||
None, | ||
DebugLoc::None); | ||
// FIXME: perhaps we also should fill inside failed branch? We do not want to re-drop a | ||
// failed drop again by mistake. (conflicts with MSVC SEH if we don’t want to introduce | ||
// a heap of hacks) | ||
self.drop_fill(intermediate_bcx, lvalue.llval, ty); | ||
build::Br(intermediate_bcx, self.llblock(target), DebugLoc::None); | ||
} else { | ||
build::Call(bcx, drop_fn, &[llvalue], None, DebugLoc::None); | ||
self.drop_fill(bcx, lvalue.llval, ty); | ||
build::Br(bcx, self.llblock(target), DebugLoc::None); | ||
} | ||
} | ||
|
||
pub fn drop_fill(&mut self, bcx: Block<'bcx, 'tcx>, value: ValueRef, ty: Ty<'tcx>) { | ||
let llty = type_of::type_of(bcx.ccx(), ty); | ||
let llptr = build::PointerCast(bcx, value, Type::i8(bcx.ccx()).ptr_to()); | ||
let filling = common::C_u8(bcx.ccx(), adt::DTOR_DONE); | ||
let size = machine::llsize_of(bcx.ccx(), llty); | ||
let align = common::C_u32(bcx.ccx(), machine::llalign_of_min(bcx.ccx(), llty)); | ||
base::call_memset(&build::B(bcx), llptr, filling, size, align, false); | ||
} | ||
|
||
pub fn set_operand_dropped(&mut self, | ||
bcx: Block<'bcx, 'tcx>, | ||
operand: &mir::Operand<'tcx>) { | ||
match *operand { | ||
mir::Operand::Constant(_) => return, | ||
mir::Operand::Consume(ref lvalue) => { | ||
if let mir::Lvalue::Temp(idx) = *lvalue { | ||
if let TempRef::Operand(..) = self.temps[idx as usize] { | ||
return // we do not handle these, should we? | ||
} | ||
} | ||
let lvalue = self.trans_lvalue(bcx, lvalue); | ||
let ty = lvalue.ty.to_ty(bcx.tcx()); | ||
if !glue::type_needs_drop(bcx.tcx(), ty) || | ||
common::type_is_fat_ptr(bcx.tcx(), ty) { | ||
return | ||
} else { | ||
self.drop_fill(bcx, lvalue.llval, ty); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -196,6 +196,7 @@ mod analyze; | |
mod block; | ||
mod constant; | ||
mod did; | ||
mod drop; | ||
mod lvalue; | ||
mod operand; | ||
mod rvalue; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Copyright 2016 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. | ||
#![feature(rustc_attrs)] | ||
// error-pattern:drop 1 | ||
// error-pattern:drop 2 | ||
use std::io::{self, Write}; | ||
|
||
|
||
/// Structure which will not allow to be dropped twice. | ||
struct Droppable<'a>(&'a mut bool, u32); | ||
impl<'a> Drop for Droppable<'a> { | ||
fn drop(&mut self) { | ||
if *self.0 { | ||
writeln!(io::stderr(), "{} dropped twice", self.1); | ||
::std::process::exit(1); | ||
} | ||
writeln!(io::stderr(), "drop {}", self.1); | ||
*self.0 = true; | ||
} | ||
} | ||
|
||
#[rustc_mir] | ||
fn mir(){ | ||
let mut b1 = false; | ||
let mut b2 = false; | ||
let x = Droppable(&mut b1, 1); | ||
let y = Droppable(&mut b2, 2); | ||
let mut z = x; | ||
let k = y; | ||
z = k; | ||
} | ||
|
||
fn main() { | ||
mir(); | ||
panic!(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Copyright 2016 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. | ||
#![feature(rustc_attrs)] | ||
// error-pattern:drop 1 | ||
use std::io::{self, Write}; | ||
|
||
|
||
/// Structure which will not allow to be dropped twice. | ||
struct Droppable<'a>(&'a mut bool, u32); | ||
impl<'a> Drop for Droppable<'a> { | ||
fn drop(&mut self) { | ||
if *self.0 { | ||
writeln!(io::stderr(), "{} dropped twice", self.1); | ||
::std::process::exit(1); | ||
} | ||
writeln!(io::stderr(), "drop {}", self.1); | ||
*self.0 = true; | ||
} | ||
} | ||
|
||
#[rustc_mir] | ||
fn mir(d: Droppable){ | ||
loop { | ||
let x = d; | ||
break; | ||
} | ||
} | ||
|
||
fn main() { | ||
let mut d1 = false; | ||
mir(Droppable(&mut d1, 1)); | ||
panic!(); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This also feels seriously hacky to me, but I don’t see an obvious way to dropflag-fill a
OperandValue::Immediate
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could use an out-of-line drop flag for these