Skip to content

Commit 8b95b0a

Browse files
committed
Auto merge of #31282 - pczarn:mir-trans-builder, r=nagisa
Closes #31003
2 parents 0d410b8 + 38fa06b commit 8b95b0a

File tree

12 files changed

+515
-335
lines changed

12 files changed

+515
-335
lines changed

src/librustc_trans/trans/base.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -988,7 +988,7 @@ pub fn wants_msvc_seh(sess: &Session) -> bool {
988988
}
989989

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

994994
pub fn need_invoke(bcx: Block) -> bool {
@@ -1616,6 +1616,7 @@ pub fn new_fn_ctxt<'a, 'tcx>(ccx: &'a CrateContext<'a, 'tcx>,
16161616
param_substs: param_substs,
16171617
span: sp,
16181618
block_arena: block_arena,
1619+
lpad_arena: TypedArena::new(),
16191620
ccx: ccx,
16201621
debug_context: debug_context,
16211622
scopes: RefCell::new(Vec::new()),
@@ -2003,7 +2004,7 @@ pub fn trans_closure<'a, 'b, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
20032004
let mut bcx = init_function(&fcx, false, output_type);
20042005

20052006
if attributes.iter().any(|item| item.check_name("rustc_mir")) {
2006-
mir::trans_mir(bcx);
2007+
mir::trans_mir(bcx.build());
20072008
fcx.cleanup();
20082009
return;
20092010
}

src/librustc_trans/trans/build.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,7 @@ pub fn Invoke(cx: Block,
150150
cx.val_to_string(fn_),
151151
args.iter().map(|a| cx.val_to_string(*a)).collect::<Vec<String>>().join(", "));
152152
debug_loc.apply(cx.fcx);
153-
let lpad = cx.lpad.borrow();
154-
let bundle = lpad.as_ref().and_then(|b| b.bundle());
153+
let bundle = cx.lpad().and_then(|b| b.bundle());
155154
B(cx).invoke(fn_, args, then, catch, bundle, attributes)
156155
}
157156

@@ -916,8 +915,7 @@ pub fn Call(cx: Block,
916915
return _UndefReturn(cx, fn_);
917916
}
918917
debug_loc.apply(cx.fcx);
919-
let lpad = cx.lpad.borrow();
920-
let bundle = lpad.as_ref().and_then(|b| b.bundle());
918+
let bundle = cx.lpad.get().and_then(|b| b.bundle());
921919
B(cx).call(fn_, args, bundle, attributes)
922920
}
923921

@@ -932,8 +930,7 @@ pub fn CallWithConv(cx: Block,
932930
return _UndefReturn(cx, fn_);
933931
}
934932
debug_loc.apply(cx.fcx);
935-
let lpad = cx.lpad.borrow();
936-
let bundle = lpad.as_ref().and_then(|b| b.bundle());
933+
let bundle = cx.lpad.get().and_then(|b| b.bundle());
937934
B(cx).call_with_conv(fn_, args, conv, bundle, attributes)
938935
}
939936

src/librustc_trans/trans/cleanup.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1054,11 +1054,11 @@ impl EarlyExitLabel {
10541054
match *self {
10551055
UnwindExit(UnwindKind::CleanupPad(..)) => {
10561056
let pad = build::CleanupPad(bcx, None, &[]);
1057-
*bcx.lpad.borrow_mut() = Some(LandingPad::msvc(pad));
1057+
bcx.lpad.set(Some(bcx.fcx.lpad_arena.alloc(LandingPad::msvc(pad))));
10581058
UnwindExit(UnwindKind::CleanupPad(pad))
10591059
}
10601060
UnwindExit(UnwindKind::LandingPad) => {
1061-
*bcx.lpad.borrow_mut() = Some(LandingPad::gnu());
1061+
bcx.lpad.set(Some(bcx.fcx.lpad_arena.alloc(LandingPad::gnu())));
10621062
*self
10631063
}
10641064
label => label,

src/librustc_trans/trans/common.rs

+117-2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use middle::lang_items::LangItem;
2626
use middle::subst::{self, Substs};
2727
use trans::base;
2828
use trans::build;
29+
use trans::builder::Builder;
2930
use trans::callee;
3031
use trans::cleanup;
3132
use trans::consts;
@@ -45,6 +46,7 @@ use util::nodemap::{FnvHashMap, NodeMap};
4546

4647
use arena::TypedArena;
4748
use libc::{c_uint, c_char};
49+
use std::ops::Deref;
4850
use std::ffi::CString;
4951
use std::cell::{Cell, RefCell};
5052
use std::vec::Vec;
@@ -365,6 +367,9 @@ pub struct FunctionContext<'a, 'tcx: 'a> {
365367
// The arena that blocks are allocated from.
366368
pub block_arena: &'a TypedArena<BlockS<'a, 'tcx>>,
367369

370+
// The arena that landing pads are allocated from.
371+
pub lpad_arena: TypedArena<LandingPad>,
372+
368373
// This function's enclosing crate context.
369374
pub ccx: &'a CrateContext<'a, 'tcx>,
370375

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

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

587592
// AST node-id associated with this block, if any. Used for
588593
// debugging purposes only.
@@ -604,7 +609,7 @@ impl<'blk, 'tcx> BlockS<'blk, 'tcx> {
604609
llbb: llbb,
605610
terminated: Cell::new(false),
606611
unreachable: Cell::new(false),
607-
lpad: RefCell::new(None),
612+
lpad: Cell::new(None),
608613
opt_node_id: opt_node_id,
609614
fcx: fcx
610615
})
@@ -613,11 +618,18 @@ impl<'blk, 'tcx> BlockS<'blk, 'tcx> {
613618
pub fn ccx(&self) -> &'blk CrateContext<'blk, 'tcx> {
614619
self.fcx.ccx
615620
}
621+
pub fn fcx(&self) -> &'blk FunctionContext<'blk, 'tcx> {
622+
self.fcx
623+
}
616624
pub fn tcx(&self) -> &'blk ty::ctxt<'tcx> {
617625
self.fcx.ccx.tcx()
618626
}
619627
pub fn sess(&self) -> &'blk Session { self.fcx.ccx.sess() }
620628

629+
pub fn lpad(&self) -> Option<&'blk LandingPad> {
630+
self.lpad.get()
631+
}
632+
621633
pub fn mir(&self) -> &'blk Mir<'tcx> {
622634
self.fcx.mir()
623635
}
@@ -659,6 +671,109 @@ impl<'blk, 'tcx> BlockS<'blk, 'tcx> {
659671
self.fcx.param_substs,
660672
value)
661673
}
674+
675+
pub fn build(&'blk self) -> BlockAndBuilder<'blk, 'tcx> {
676+
BlockAndBuilder::new(self, OwnedBuilder::new_with_ccx(self.ccx()))
677+
}
678+
}
679+
680+
pub struct OwnedBuilder<'blk, 'tcx: 'blk> {
681+
builder: Builder<'blk, 'tcx>
682+
}
683+
684+
impl<'blk, 'tcx> OwnedBuilder<'blk, 'tcx> {
685+
pub fn new_with_ccx(ccx: &'blk CrateContext<'blk, 'tcx>) -> Self {
686+
// Create a fresh builder from the crate context.
687+
let llbuilder = unsafe {
688+
llvm::LLVMCreateBuilderInContext(ccx.llcx())
689+
};
690+
OwnedBuilder {
691+
builder: Builder {
692+
llbuilder: llbuilder,
693+
ccx: ccx,
694+
}
695+
}
696+
}
697+
}
698+
699+
impl<'blk, 'tcx> Drop for OwnedBuilder<'blk, 'tcx> {
700+
fn drop(&mut self) {
701+
unsafe {
702+
llvm::LLVMDisposeBuilder(self.builder.llbuilder);
703+
}
704+
}
705+
}
706+
707+
pub struct BlockAndBuilder<'blk, 'tcx: 'blk> {
708+
bcx: Block<'blk, 'tcx>,
709+
owned_builder: OwnedBuilder<'blk, 'tcx>,
710+
}
711+
712+
impl<'blk, 'tcx> BlockAndBuilder<'blk, 'tcx> {
713+
pub fn new(bcx: Block<'blk, 'tcx>, owned_builder: OwnedBuilder<'blk, 'tcx>) -> Self {
714+
// Set the builder's position to this block's end.
715+
owned_builder.builder.position_at_end(bcx.llbb);
716+
BlockAndBuilder {
717+
bcx: bcx,
718+
owned_builder: owned_builder,
719+
}
720+
}
721+
722+
pub fn with_block<F, R>(&self, f: F) -> R
723+
where F: FnOnce(Block<'blk, 'tcx>) -> R
724+
{
725+
let result = f(self.bcx);
726+
self.position_at_end(self.bcx.llbb);
727+
result
728+
}
729+
730+
pub fn map_block<F>(self, f: F) -> Self
731+
where F: FnOnce(Block<'blk, 'tcx>) -> Block<'blk, 'tcx>
732+
{
733+
let BlockAndBuilder { bcx, owned_builder } = self;
734+
let bcx = f(bcx);
735+
BlockAndBuilder::new(bcx, owned_builder)
736+
}
737+
738+
// Methods delegated to bcx
739+
740+
pub fn ccx(&self) -> &'blk CrateContext<'blk, 'tcx> {
741+
self.bcx.ccx()
742+
}
743+
pub fn fcx(&self) -> &'blk FunctionContext<'blk, 'tcx> {
744+
self.bcx.fcx()
745+
}
746+
pub fn tcx(&self) -> &'blk ty::ctxt<'tcx> {
747+
self.bcx.tcx()
748+
}
749+
pub fn sess(&self) -> &'blk Session {
750+
self.bcx.sess()
751+
}
752+
753+
pub fn llbb(&self) -> BasicBlockRef {
754+
self.bcx.llbb
755+
}
756+
757+
pub fn mir(&self) -> &'blk Mir<'tcx> {
758+
self.bcx.mir()
759+
}
760+
761+
pub fn val_to_string(&self, val: ValueRef) -> String {
762+
self.bcx.val_to_string(val)
763+
}
764+
765+
pub fn monomorphize<T>(&self, value: &T) -> T
766+
where T: TypeFoldable<'tcx>
767+
{
768+
self.bcx.monomorphize(value)
769+
}
770+
}
771+
772+
impl<'blk, 'tcx> Deref for BlockAndBuilder<'blk, 'tcx> {
773+
type Target = Builder<'blk, 'tcx>;
774+
fn deref(&self) -> &Self::Target {
775+
&self.owned_builder.builder
776+
}
662777
}
663778

664779
/// A structure representing an active landing pad for the duration of a basic

0 commit comments

Comments
 (0)