Skip to content

Commit a9ab809

Browse files
committed
Refactor storage of LandingPads
1 parent 06266eb commit a9ab809

File tree

7 files changed

+58
-34
lines changed

7 files changed

+58
-34
lines changed

src/librustc_trans/trans/base.rs

Lines changed: 2 additions & 1 deletion
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()),

src/librustc_trans/trans/build.rs

Lines changed: 3 additions & 6 deletions
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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -740,7 +740,7 @@ impl<'blk, 'tcx> CleanupHelperMethods<'blk, 'tcx> for FunctionContext<'blk, 'tcx
740740
UnwindExit(val) => {
741741
// Generate a block that will resume unwinding to the
742742
// calling function
743-
let bcx = self.new_block("resume", None);
743+
let bcx = self.new_block("resume", None, None);
744744
match val {
745745
UnwindKind::LandingPad => {
746746
let addr = self.landingpad_alloca.get()
@@ -830,7 +830,7 @@ impl<'blk, 'tcx> CleanupHelperMethods<'blk, 'tcx> for FunctionContext<'blk, 'tcx
830830
let name = scope.block_name("clean");
831831
debug!("generating cleanups for {}", name);
832832

833-
let bcx_in = self.new_block(&name[..], None);
833+
let bcx_in = self.new_block(&name[..], None, None);
834834
let exit_label = label.start(bcx_in);
835835
let mut bcx_out = bcx_in;
836836
let len = scope.cleanups.len();
@@ -873,7 +873,7 @@ impl<'blk, 'tcx> CleanupHelperMethods<'blk, 'tcx> for FunctionContext<'blk, 'tcx
873873
Some(llbb) => return llbb,
874874
None => {
875875
let name = last_scope.block_name("unwind");
876-
pad_bcx = self.new_block(&name[..], None);
876+
pad_bcx = self.new_block(&name[..], None, None);
877877
last_scope.cached_landing_pad = Some(pad_bcx.llbb);
878878
}
879879
}
@@ -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

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,9 @@ pub struct FunctionContext<'a, 'tcx: 'a> {
367367
// The arena that blocks are allocated from.
368368
pub block_arena: &'a TypedArena<BlockS<'a, 'tcx>>,
369369

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

@@ -431,28 +434,33 @@ impl<'a, 'tcx> FunctionContext<'a, 'tcx> {
431434

432435
pub fn new_block(&'a self,
433436
name: &str,
434-
opt_node_id: Option<ast::NodeId>)
437+
opt_node_id: Option<ast::NodeId>,
438+
landing_pad: Option<LandingPad>)
435439
-> Block<'a, 'tcx> {
436440
unsafe {
437441
let name = CString::new(name).unwrap();
438442
let llbb = llvm::LLVMAppendBasicBlockInContext(self.ccx.llcx(),
439443
self.llfn,
440444
name.as_ptr());
441-
BlockS::new(llbb, opt_node_id, self)
445+
let block = BlockS::new(llbb, opt_node_id, self);
446+
if let Some(landing_pad) = landing_pad {
447+
block.lpad.set(Some(self.lpad_arena.alloc(landing_pad)));
448+
}
449+
block
442450
}
443451
}
444452

445453
pub fn new_id_block(&'a self,
446454
name: &str,
447455
node_id: ast::NodeId)
448456
-> Block<'a, 'tcx> {
449-
self.new_block(name, Some(node_id))
457+
self.new_block(name, Some(node_id), None)
450458
}
451459

452460
pub fn new_temp_block(&'a self,
453461
name: &str)
454462
-> Block<'a, 'tcx> {
455-
self.new_block(name, None)
463+
self.new_block(name, None, None)
456464
}
457465

458466
pub fn join_blocks(&'a self,
@@ -584,7 +592,7 @@ pub struct BlockS<'blk, 'tcx: 'blk> {
584592

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

589597
// AST node-id associated with this block, if any. Used for
590598
// debugging purposes only.
@@ -606,7 +614,7 @@ impl<'blk, 'tcx> BlockS<'blk, 'tcx> {
606614
llbb: llbb,
607615
terminated: Cell::new(false),
608616
unreachable: Cell::new(false),
609-
lpad: RefCell::new(None),
617+
lpad: Cell::new(None),
610618
opt_node_id: opt_node_id,
611619
fcx: fcx
612620
})
@@ -623,6 +631,10 @@ impl<'blk, 'tcx> BlockS<'blk, 'tcx> {
623631
}
624632
pub fn sess(&self) -> &'blk Session { self.fcx.ccx.sess() }
625633

634+
pub fn lpad(&self) -> Option<&'blk LandingPad> {
635+
self.lpad.get()
636+
}
637+
626638
pub fn mir(&self) -> &'blk Mir<'tcx> {
627639
self.fcx.mir()
628640
}
@@ -747,6 +759,10 @@ impl<'blk, 'tcx> BlockAndBuilder<'blk, 'tcx> {
747759
self.bcx.llbb
748760
}
749761

762+
pub fn lpad(&self) -> Option<&'blk LandingPad> {
763+
self.bcx.lpad()
764+
}
765+
750766
pub fn mir(&self) -> &'blk Mir<'tcx> {
751767
self.bcx.mir()
752768
}

src/librustc_trans/trans/mir/block.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -119,13 +119,16 @@ impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> {
119119
if let Some(unwind) = unwind {
120120
let uwbcx = self.bcx(unwind);
121121
let unwind = self.make_landing_pad(uwbcx);
122+
let bundle = bcx.lpad().and_then(|b| b.bundle());
122123
bcx.invoke(drop_fn,
123124
&[llvalue],
124125
self.llblock(target),
125126
unwind.llbb(),
127+
bundle,
126128
None);
127129
} else {
128-
bcx.call(drop_fn, &[llvalue], None);
130+
let bundle = bcx.lpad().and_then(|b| b.bundle());
131+
bcx.call(drop_fn, &[llvalue], bundle, None);
129132
bcx.br(self.llblock(target));
130133
}
131134
}
@@ -187,24 +190,28 @@ impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> {
187190
let cleanup = self.bcx(cleanup);
188191
let landingpad = self.make_landing_pad(cleanup);
189192
let unreachable_blk = self.unreachable_block();
193+
let bundle = bcx.lpad().and_then(|b| b.bundle());
190194
bcx.invoke(callee.immediate(),
191195
&llargs[..],
192196
unreachable_blk.llbb,
193197
landingpad.llbb(),
198+
bundle,
194199
Some(attrs));
195200
},
196201
(false, false, &Some(cleanup), &Some((_, success))) => {
197202
let cleanup = self.bcx(cleanup);
198203
let landingpad = self.make_landing_pad(cleanup);
199204
let (target, postinvoke) = if must_copy_dest {
200-
(bcx.fcx().new_block("", None), Some(self.bcx(success)))
205+
(bcx.fcx().new_block("", None, None).build(), Some(self.bcx(success)))
201206
} else {
202207
(self.bcx(success), None)
203208
};
209+
let bundle = bcx.lpad().and_then(|b| b.bundle());
204210
let invokeret = bcx.invoke(callee.immediate(),
205211
&llargs[..],
206212
target.llbb(),
207213
landingpad.llbb(),
214+
bundle,
208215
Some(attrs));
209216
if let Some(postinvoketarget) = postinvoke {
210217
// We translate the copy into a temporary block. The temporary block is
@@ -240,7 +247,8 @@ impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> {
240247
}
241248
},
242249
(false, _, _, &None) => {
243-
bcx.call(callee.immediate(), &llargs[..], Some(attrs));
250+
let bundle = bcx.lpad().and_then(|b| b.bundle());
251+
bcx.call(callee.immediate(), &llargs[..], bundle, Some(attrs));
244252
bcx.unreachable();
245253
}
246254
(false, _, _, &Some((_, target))) => {
@@ -301,26 +309,25 @@ impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> {
301309
cleanup: BlockAndBuilder<'bcx, 'tcx>)
302310
-> BlockAndBuilder<'bcx, 'tcx>
303311
{
312+
let cleanup_llbb = cleanup.llbb();
304313
let bcx = cleanup.map_block(|cleanup| {
305-
cleanup.fcx.new_block("cleanup", None)
314+
// FIXME(#30941) this doesn't handle msvc-style exceptions
315+
cleanup.fcx.new_block("cleanup", None, Some(LandingPad::gnu()))
306316
});
307-
// FIXME(#30941) this doesn't handle msvc-style exceptions
308-
*bcx.lpad.borrow_mut() = Some(LandingPad::gnu());
309-
let bcx = bcx.build();
310317
let ccx = bcx.ccx();
311318
let llpersonality = bcx.fcx().eh_personality();
312319
let llretty = Type::struct_(ccx, &[Type::i8p(ccx), Type::i32(ccx)], false);
313320
let llretval = bcx.landing_pad(llretty, llpersonality, 1, bcx.fcx().llfn);
314321
bcx.set_cleanup(llretval);
315322
let slot = self.get_personality_slot(&bcx);
316323
bcx.store(llretval, slot);
317-
bcx.br(cleanup.llbb());
324+
bcx.br(cleanup_llbb);
318325
bcx
319326
}
320327

321328
fn unreachable_block(&mut self) -> Block<'bcx, 'tcx> {
322329
self.unreachable_block.unwrap_or_else(|| {
323-
let bl = self.fcx.new_block("unreachable", None);
330+
let bl = self.fcx.new_block("unreachable", None, None);
324331
bl.build().unreachable();
325332
self.unreachable_block = Some(bl);
326333
bl

src/librustc_trans/trans/mir/mod.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,13 @@ pub fn trans_mir<'bcx, 'tcx>(bcx: BlockAndBuilder<'bcx, 'tcx>) {
114114
let block_bcxs: Vec<Block<'bcx,'tcx>> =
115115
mir_blocks.iter()
116116
.map(|&bb|{
117-
let bcx = fcx.new_block(&format!("{:?}", bb), None);
118117
// FIXME(#30941) this doesn't handle msvc-style exceptions
119-
if mir.basic_block_data(bb).is_cleanup {
120-
*bcx.lpad.borrow_mut() = Some(LandingPad::gnu())
121-
}
122-
bcx
118+
let lpad = if mir.basic_block_data(bb).is_cleanup {
119+
Some(LandingPad::gnu())
120+
} else {
121+
None
122+
};
123+
fcx.new_block(&format!("{:?}", bb), None, lpad)
123124
})
124125
.collect();
125126

src/librustc_trans/trans/mir/rvalue.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -497,10 +497,12 @@ impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> {
497497
if input_ty == tcx.types.f32 {
498498
let lllhs = bcx.fpext(lhs, f64t);
499499
let llrhs = bcx.fpext(rhs, f64t);
500-
let llres = bcx.call(llfn, &[lllhs, llrhs], None);
500+
let bundle = bcx.lpad().and_then(|b| b.bundle());
501+
let llres = bcx.call(llfn, &[lllhs, llrhs], bundle, None);
501502
bcx.fptrunc(llres, Type::f32(bcx.ccx()))
502503
} else {
503-
bcx.call(llfn, &[lhs, rhs], None)
504+
let bundle = bcx.lpad().and_then(|b| b.bundle());
505+
bcx.call(llfn, &[lhs, rhs], bundle, None)
504506
}
505507
} else {
506508
bcx.frem(lhs, rhs)

0 commit comments

Comments
 (0)