Skip to content
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

[MIR] Refactor the MIR translator to use LLVM Builder directly #31282

Merged
merged 4 commits into from
Feb 9, 2016
Merged
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
5 changes: 3 additions & 2 deletions src/librustc_trans/trans/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -988,7 +988,7 @@ pub fn wants_msvc_seh(sess: &Session) -> bool {
}

pub fn avoid_invoke(bcx: Block) -> bool {
bcx.sess().no_landing_pads() || bcx.lpad.borrow().is_some()
bcx.sess().no_landing_pads() || bcx.lpad().is_some()
}

pub fn need_invoke(bcx: Block) -> bool {
Expand Down Expand Up @@ -1616,6 +1616,7 @@ pub fn new_fn_ctxt<'a, 'tcx>(ccx: &'a CrateContext<'a, 'tcx>,
param_substs: param_substs,
span: sp,
block_arena: block_arena,
lpad_arena: TypedArena::new(),
Copy link
Member

Choose a reason for hiding this comment

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

You might want to leave off this change for a separate PR, honestly (or at least factor it out into a separate commit, so it easier to see related changes).

Code related to this field is very precondition-sensitive, I’m worried changing it like this might invalidate some of those preconditions. E.g. MSVC landing blocks, especially, are on per-block basis and it is likely to be wrong there.

Copy link
Member

Choose a reason for hiding this comment

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

Ah, I see it, you’re still storing a pointer inside a Block. A few thoughts:

  1. Size of BlockS does not really matter, because we only ever pass a Block around, which is a pub type Block<'blk, 'tcx> = &'blk BlockS<'blk, 'tcx>; – i.e. is a reference and there’s barely any performance overhead passing a pointer to larger or smaller struct;
  2. From my experimentation with implementing MSVC LPs in MIR, all that really needs to be stored around for MIR itself is a single ValueRef (a pointer, basically) inside MirContext. All the landing-pad related things in Block are for the old trans to use, and I wouldn’t bother much changing it (but since you’ve already changed it, its fine, I guess?).

I’d still like it split up into a separate commit so it is easier to review and these changes do not mix with the Builder refactor.

ccx: ccx,
debug_context: debug_context,
scopes: RefCell::new(Vec::new()),
Expand Down Expand Up @@ -2003,7 +2004,7 @@ pub fn trans_closure<'a, 'b, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
let mut bcx = init_function(&fcx, false, output_type);

if attributes.iter().any(|item| item.check_name("rustc_mir")) {
mir::trans_mir(bcx);
mir::trans_mir(bcx.build());
fcx.cleanup();
return;
}
Expand Down
9 changes: 3 additions & 6 deletions src/librustc_trans/trans/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,7 @@ pub fn Invoke(cx: Block,
cx.val_to_string(fn_),
args.iter().map(|a| cx.val_to_string(*a)).collect::<Vec<String>>().join(", "));
debug_loc.apply(cx.fcx);
let lpad = cx.lpad.borrow();
let bundle = lpad.as_ref().and_then(|b| b.bundle());
let bundle = cx.lpad().and_then(|b| b.bundle());
B(cx).invoke(fn_, args, then, catch, bundle, attributes)
}

Expand Down Expand Up @@ -916,8 +915,7 @@ pub fn Call(cx: Block,
return _UndefReturn(cx, fn_);
}
debug_loc.apply(cx.fcx);
let lpad = cx.lpad.borrow();
let bundle = lpad.as_ref().and_then(|b| b.bundle());
let bundle = cx.lpad.get().and_then(|b| b.bundle());
B(cx).call(fn_, args, bundle, attributes)
}

Expand All @@ -932,8 +930,7 @@ pub fn CallWithConv(cx: Block,
return _UndefReturn(cx, fn_);
}
debug_loc.apply(cx.fcx);
let lpad = cx.lpad.borrow();
let bundle = lpad.as_ref().and_then(|b| b.bundle());
let bundle = cx.lpad.get().and_then(|b| b.bundle());
B(cx).call_with_conv(fn_, args, conv, bundle, attributes)
}

Expand Down
4 changes: 2 additions & 2 deletions src/librustc_trans/trans/cleanup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1054,11 +1054,11 @@ impl EarlyExitLabel {
match *self {
UnwindExit(UnwindKind::CleanupPad(..)) => {
let pad = build::CleanupPad(bcx, None, &[]);
*bcx.lpad.borrow_mut() = Some(LandingPad::msvc(pad));
bcx.lpad.set(Some(bcx.fcx.lpad_arena.alloc(LandingPad::msvc(pad))));
UnwindExit(UnwindKind::CleanupPad(pad))
}
UnwindExit(UnwindKind::LandingPad) => {
*bcx.lpad.borrow_mut() = Some(LandingPad::gnu());
bcx.lpad.set(Some(bcx.fcx.lpad_arena.alloc(LandingPad::gnu())));
*self
}
label => label,
Expand Down
119 changes: 117 additions & 2 deletions src/librustc_trans/trans/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use middle::lang_items::LangItem;
use middle::subst::{self, Substs};
use trans::base;
use trans::build;
use trans::builder::Builder;
use trans::callee;
use trans::cleanup;
use trans::consts;
Expand All @@ -45,6 +46,7 @@ use util::nodemap::{FnvHashMap, NodeMap};

use arena::TypedArena;
use libc::{c_uint, c_char};
use std::ops::Deref;
use std::ffi::CString;
use std::cell::{Cell, RefCell};
use std::vec::Vec;
Expand Down Expand Up @@ -365,6 +367,9 @@ pub struct FunctionContext<'a, 'tcx: 'a> {
// The arena that blocks are allocated from.
pub block_arena: &'a TypedArena<BlockS<'a, 'tcx>>,

// The arena that landing pads are allocated from.
pub lpad_arena: TypedArena<LandingPad>,

// This function's enclosing crate context.
pub ccx: &'a CrateContext<'a, 'tcx>,

Expand Down Expand Up @@ -582,7 +587,7 @@ pub struct BlockS<'blk, 'tcx: 'blk> {

// If this block part of a landing pad, then this is `Some` indicating what
// kind of landing pad its in, otherwise this is none.
pub lpad: RefCell<Option<LandingPad>>,
pub lpad: Cell<Option<&'blk LandingPad>>,

// AST node-id associated with this block, if any. Used for
// debugging purposes only.
Expand All @@ -604,7 +609,7 @@ impl<'blk, 'tcx> BlockS<'blk, 'tcx> {
llbb: llbb,
terminated: Cell::new(false),
unreachable: Cell::new(false),
lpad: RefCell::new(None),
lpad: Cell::new(None),
opt_node_id: opt_node_id,
fcx: fcx
})
Expand All @@ -613,11 +618,18 @@ impl<'blk, 'tcx> BlockS<'blk, 'tcx> {
pub fn ccx(&self) -> &'blk CrateContext<'blk, 'tcx> {
self.fcx.ccx
}
pub fn fcx(&self) -> &'blk FunctionContext<'blk, 'tcx> {
self.fcx
}
pub fn tcx(&self) -> &'blk ty::ctxt<'tcx> {
self.fcx.ccx.tcx()
}
pub fn sess(&self) -> &'blk Session { self.fcx.ccx.sess() }

pub fn lpad(&self) -> Option<&'blk LandingPad> {
self.lpad.get()
}

pub fn mir(&self) -> &'blk Mir<'tcx> {
self.fcx.mir()
}
Expand Down Expand Up @@ -659,6 +671,109 @@ impl<'blk, 'tcx> BlockS<'blk, 'tcx> {
self.fcx.param_substs,
value)
}

pub fn build(&'blk self) -> BlockAndBuilder<'blk, 'tcx> {
BlockAndBuilder::new(self, OwnedBuilder::new_with_ccx(self.ccx()))
}
}

pub struct OwnedBuilder<'blk, 'tcx: 'blk> {
builder: Builder<'blk, 'tcx>
}

impl<'blk, 'tcx> OwnedBuilder<'blk, 'tcx> {
pub fn new_with_ccx(ccx: &'blk CrateContext<'blk, 'tcx>) -> Self {
// Create a fresh builder from the crate context.
let llbuilder = unsafe {
llvm::LLVMCreateBuilderInContext(ccx.llcx())
};
OwnedBuilder {
builder: Builder {
llbuilder: llbuilder,
ccx: ccx,
}
}
}
}

impl<'blk, 'tcx> Drop for OwnedBuilder<'blk, 'tcx> {
fn drop(&mut self) {
unsafe {
llvm::LLVMDisposeBuilder(self.builder.llbuilder);
}
}
}

pub struct BlockAndBuilder<'blk, 'tcx: 'blk> {
bcx: Block<'blk, 'tcx>,
owned_builder: OwnedBuilder<'blk, 'tcx>,
}

impl<'blk, 'tcx> BlockAndBuilder<'blk, 'tcx> {
pub fn new(bcx: Block<'blk, 'tcx>, owned_builder: OwnedBuilder<'blk, 'tcx>) -> Self {
// Set the builder's position to this block's end.
owned_builder.builder.position_at_end(bcx.llbb);
BlockAndBuilder {
bcx: bcx,
owned_builder: owned_builder,
}
}

pub fn with_block<F, R>(&self, f: F) -> R
where F: FnOnce(Block<'blk, 'tcx>) -> R
{
let result = f(self.bcx);
self.position_at_end(self.bcx.llbb);
result
}

pub fn map_block<F>(self, f: F) -> Self
where F: FnOnce(Block<'blk, 'tcx>) -> Block<'blk, 'tcx>
{
let BlockAndBuilder { bcx, owned_builder } = self;
let bcx = f(bcx);
BlockAndBuilder::new(bcx, owned_builder)
}

// Methods delegated to bcx

pub fn ccx(&self) -> &'blk CrateContext<'blk, 'tcx> {
self.bcx.ccx()
}
pub fn fcx(&self) -> &'blk FunctionContext<'blk, 'tcx> {
self.bcx.fcx()
}
pub fn tcx(&self) -> &'blk ty::ctxt<'tcx> {
self.bcx.tcx()
}
pub fn sess(&self) -> &'blk Session {
self.bcx.sess()
}

pub fn llbb(&self) -> BasicBlockRef {
self.bcx.llbb
}

pub fn mir(&self) -> &'blk Mir<'tcx> {
self.bcx.mir()
}

pub fn val_to_string(&self, val: ValueRef) -> String {
self.bcx.val_to_string(val)
}

pub fn monomorphize<T>(&self, value: &T) -> T
where T: TypeFoldable<'tcx>
{
self.bcx.monomorphize(value)
}
}

impl<'blk, 'tcx> Deref for BlockAndBuilder<'blk, 'tcx> {
Copy link
Member

Choose a reason for hiding this comment

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

Nice!

type Target = Builder<'blk, 'tcx>;
fn deref(&self) -> &Self::Target {
&self.owned_builder.builder
}
}

/// A structure representing an active landing pad for the duration of a basic
Expand Down
Loading