Skip to content

Commit ada77e9

Browse files
authored
Rollup merge of #93261 - bjorn3:cg_ssa_refactor6, r=cjgillot
Some unwinding related cg_ssa cleanups These should make it a bit easier for alternative codegen backends to implement unwinding.
2 parents 18c8d0d + f6ce771 commit ada77e9

File tree

5 files changed

+63
-75
lines changed

5 files changed

+63
-75
lines changed

compiler/rustc_codegen_gcc/src/builder.rs

+13-16
Original file line numberDiff line numberDiff line change
@@ -1256,7 +1256,11 @@ impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
12561256
aggregate_value
12571257
}
12581258

1259-
fn landing_pad(&mut self, _ty: Type<'gcc>, _pers_fn: RValue<'gcc>, _num_clauses: usize) -> RValue<'gcc> {
1259+
fn set_personality_fn(&mut self, _personality: RValue<'gcc>) {
1260+
// TODO(antoyo)
1261+
}
1262+
1263+
fn cleanup_landing_pad(&mut self, _ty: Type<'gcc>, _pers_fn: RValue<'gcc>) -> RValue<'gcc> {
12601264
let field1 = self.context.new_field(None, self.u8_type, "landing_pad_field_1");
12611265
let field2 = self.context.new_field(None, self.i32_type, "landing_pad_field_1");
12621266
let struct_type = self.context.new_struct_type(None, "landing_pad", &[field1, field2]);
@@ -1267,38 +1271,31 @@ impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
12671271
// rustc_codegen_ssa now calls the unwinding builder methods even on panic=abort.
12681272
}
12691273

1270-
fn set_cleanup(&mut self, _landing_pad: RValue<'gcc>) {
1271-
// TODO(antoyo)
1272-
}
1273-
1274-
fn resume(&mut self, _exn: RValue<'gcc>) -> RValue<'gcc> {
1274+
fn resume(&mut self, _exn: RValue<'gcc>) {
12751275
unimplemented!();
12761276
}
12771277

12781278
fn cleanup_pad(&mut self, _parent: Option<RValue<'gcc>>, _args: &[RValue<'gcc>]) -> Funclet {
12791279
unimplemented!();
12801280
}
12811281

1282-
fn cleanup_ret(&mut self, _funclet: &Funclet, _unwind: Option<Block<'gcc>>) -> RValue<'gcc> {
1282+
fn cleanup_ret(&mut self, _funclet: &Funclet, _unwind: Option<Block<'gcc>>) {
12831283
unimplemented!();
12841284
}
12851285

12861286
fn catch_pad(&mut self, _parent: RValue<'gcc>, _args: &[RValue<'gcc>]) -> Funclet {
12871287
unimplemented!();
12881288
}
12891289

1290-
fn catch_switch(&mut self, _parent: Option<RValue<'gcc>>, _unwind: Option<Block<'gcc>>, _num_handlers: usize) -> RValue<'gcc> {
1290+
fn catch_switch(
1291+
&mut self,
1292+
_parent: Option<RValue<'gcc>>,
1293+
_unwind: Option<Block<'gcc>>,
1294+
_handlers: &[Block<'gcc>],
1295+
) -> RValue<'gcc> {
12911296
unimplemented!();
12921297
}
12931298

1294-
fn add_handler(&mut self, _catch_switch: RValue<'gcc>, _handler: Block<'gcc>) {
1295-
unimplemented!();
1296-
}
1297-
1298-
fn set_personality_fn(&mut self, _personality: RValue<'gcc>) {
1299-
// TODO(antoyo)
1300-
}
1301-
13021299
// Atomic Operations
13031300
fn atomic_cmpxchg(&mut self, dst: RValue<'gcc>, cmp: RValue<'gcc>, src: RValue<'gcc>, order: AtomicOrdering, failure_order: AtomicOrdering, weak: bool) -> RValue<'gcc> {
13041301
let expected = self.current_func().new_local(None, cmp.get_type(), "expected");

compiler/rustc_codegen_llvm/src/builder.rs

+37-36
Original file line numberDiff line numberDiff line change
@@ -956,29 +956,24 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
956956
unsafe { llvm::LLVMBuildInsertValue(self.llbuilder, agg_val, elt, idx as c_uint, UNNAMED) }
957957
}
958958

959-
fn landing_pad(
960-
&mut self,
961-
ty: &'ll Type,
962-
pers_fn: &'ll Value,
963-
num_clauses: usize,
964-
) -> &'ll Value {
965-
// Use LLVMSetPersonalityFn to set the personality. It supports arbitrary Consts while,
966-
// LLVMBuildLandingPad requires the argument to be a Function (as of LLVM 12). The
967-
// personality lives on the parent function anyway.
968-
self.set_personality_fn(pers_fn);
959+
fn set_personality_fn(&mut self, personality: &'ll Value) {
969960
unsafe {
970-
llvm::LLVMBuildLandingPad(self.llbuilder, ty, None, num_clauses as c_uint, UNNAMED)
961+
llvm::LLVMSetPersonalityFn(self.llfn(), personality);
971962
}
972963
}
973964

974-
fn set_cleanup(&mut self, landing_pad: &'ll Value) {
965+
fn cleanup_landing_pad(&mut self, ty: &'ll Type, pers_fn: &'ll Value) -> &'ll Value {
966+
let landing_pad = self.landing_pad(ty, pers_fn, 1 /* FIXME should this be 0? */);
975967
unsafe {
976968
llvm::LLVMSetCleanup(landing_pad, llvm::True);
977969
}
970+
landing_pad
978971
}
979972

980-
fn resume(&mut self, exn: &'ll Value) -> &'ll Value {
981-
unsafe { llvm::LLVMBuildResume(self.llbuilder, exn) }
973+
fn resume(&mut self, exn: &'ll Value) {
974+
unsafe {
975+
llvm::LLVMBuildResume(self.llbuilder, exn);
976+
}
982977
}
983978

984979
fn cleanup_pad(&mut self, parent: Option<&'ll Value>, args: &[&'ll Value]) -> Funclet<'ll> {
@@ -995,14 +990,11 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
995990
Funclet::new(ret.expect("LLVM does not have support for cleanuppad"))
996991
}
997992

998-
fn cleanup_ret(
999-
&mut self,
1000-
funclet: &Funclet<'ll>,
1001-
unwind: Option<&'ll BasicBlock>,
1002-
) -> &'ll Value {
1003-
let ret =
1004-
unsafe { llvm::LLVMRustBuildCleanupRet(self.llbuilder, funclet.cleanuppad(), unwind) };
1005-
ret.expect("LLVM does not have support for cleanupret")
993+
fn cleanup_ret(&mut self, funclet: &Funclet<'ll>, unwind: Option<&'ll BasicBlock>) {
994+
unsafe {
995+
llvm::LLVMRustBuildCleanupRet(self.llbuilder, funclet.cleanuppad(), unwind)
996+
.expect("LLVM does not have support for cleanupret");
997+
}
1006998
}
1007999

10081000
fn catch_pad(&mut self, parent: &'ll Value, args: &[&'ll Value]) -> Funclet<'ll> {
@@ -1023,31 +1015,25 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
10231015
&mut self,
10241016
parent: Option<&'ll Value>,
10251017
unwind: Option<&'ll BasicBlock>,
1026-
num_handlers: usize,
1018+
handlers: &[&'ll BasicBlock],
10271019
) -> &'ll Value {
10281020
let name = cstr!("catchswitch");
10291021
let ret = unsafe {
10301022
llvm::LLVMRustBuildCatchSwitch(
10311023
self.llbuilder,
10321024
parent,
10331025
unwind,
1034-
num_handlers as c_uint,
1026+
handlers.len() as c_uint,
10351027
name.as_ptr(),
10361028
)
10371029
};
1038-
ret.expect("LLVM does not have support for catchswitch")
1039-
}
1040-
1041-
fn add_handler(&mut self, catch_switch: &'ll Value, handler: &'ll BasicBlock) {
1042-
unsafe {
1043-
llvm::LLVMRustAddHandler(catch_switch, handler);
1044-
}
1045-
}
1046-
1047-
fn set_personality_fn(&mut self, personality: &'ll Value) {
1048-
unsafe {
1049-
llvm::LLVMSetPersonalityFn(self.llfn(), personality);
1030+
let ret = ret.expect("LLVM does not have support for catchswitch");
1031+
for handler in handlers {
1032+
unsafe {
1033+
llvm::LLVMRustAddHandler(ret, handler);
1034+
}
10501035
}
1036+
ret
10511037
}
10521038

10531039
// Atomic Operations
@@ -1478,4 +1464,19 @@ impl<'a, 'll, 'tcx> Builder<'a, 'll, 'tcx> {
14781464
None
14791465
}
14801466
}
1467+
1468+
pub(crate) fn landing_pad(
1469+
&mut self,
1470+
ty: &'ll Type,
1471+
pers_fn: &'ll Value,
1472+
num_clauses: usize,
1473+
) -> &'ll Value {
1474+
// Use LLVMSetPersonalityFn to set the personality. It supports arbitrary Consts while,
1475+
// LLVMBuildLandingPad requires the argument to be a Function (as of LLVM 12). The
1476+
// personality lives on the parent function anyway.
1477+
self.set_personality_fn(pers_fn);
1478+
unsafe {
1479+
llvm::LLVMBuildLandingPad(self.llbuilder, ty, None, num_clauses as c_uint, UNNAMED)
1480+
}
1481+
}
14811482
}

compiler/rustc_codegen_llvm/src/intrinsic.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -525,9 +525,8 @@ fn codegen_msvc_try<'ll>(
525525

526526
normal.ret(bx.const_i32(0));
527527

528-
let cs = catchswitch.catch_switch(None, None, 2);
529-
catchswitch.add_handler(cs, catchpad_rust.llbb());
530-
catchswitch.add_handler(cs, catchpad_foreign.llbb());
528+
let cs =
529+
catchswitch.catch_switch(None, None, &[catchpad_rust.llbb(), catchpad_foreign.llbb()]);
531530

532531
// We can't use the TypeDescriptor defined in libpanic_unwind because it
533532
// might be in another DLL and the SEH encoding only supports specifying

compiler/rustc_codegen_ssa/src/mir/block.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -1346,8 +1346,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
13461346
let mut cp_bx = self.new_block(&format!("cp_funclet{:?}", bb));
13471347
ret_llbb = cs_bx.llbb();
13481348

1349-
let cs = cs_bx.catch_switch(None, None, 1);
1350-
cs_bx.add_handler(cs, cp_bx.llbb());
1349+
let cs = cs_bx.catch_switch(None, None, &[cp_bx.llbb()]);
13511350

13521351
// The "null" here is actually a RTTI type descriptor for the
13531352
// C++ personality function, but `catch (...)` has no type so
@@ -1374,8 +1373,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
13741373

13751374
let llpersonality = self.cx.eh_personality();
13761375
let llretty = self.landing_pad_type();
1377-
let lp = bx.landing_pad(llretty, llpersonality, 1);
1378-
bx.set_cleanup(lp);
1376+
let lp = bx.cleanup_landing_pad(llretty, llpersonality);
13791377

13801378
let slot = self.get_personality_slot(&mut bx);
13811379
slot.storage_live(&mut bx);

compiler/rustc_codegen_ssa/src/traits/builder.rs

+9-16
Original file line numberDiff line numberDiff line change
@@ -421,29 +421,22 @@ pub trait BuilderMethods<'a, 'tcx>:
421421
fn extract_value(&mut self, agg_val: Self::Value, idx: u64) -> Self::Value;
422422
fn insert_value(&mut self, agg_val: Self::Value, elt: Self::Value, idx: u64) -> Self::Value;
423423

424-
fn landing_pad(
425-
&mut self,
426-
ty: Self::Type,
427-
pers_fn: Self::Value,
428-
num_clauses: usize,
429-
) -> Self::Value;
430-
fn set_cleanup(&mut self, landing_pad: Self::Value);
431-
fn resume(&mut self, exn: Self::Value) -> Self::Value;
424+
fn set_personality_fn(&mut self, personality: Self::Value);
425+
426+
// These are used by everyone except msvc
427+
fn cleanup_landing_pad(&mut self, ty: Self::Type, pers_fn: Self::Value) -> Self::Value;
428+
fn resume(&mut self, exn: Self::Value);
429+
430+
// These are used only by msvc
432431
fn cleanup_pad(&mut self, parent: Option<Self::Value>, args: &[Self::Value]) -> Self::Funclet;
433-
fn cleanup_ret(
434-
&mut self,
435-
funclet: &Self::Funclet,
436-
unwind: Option<Self::BasicBlock>,
437-
) -> Self::Value;
432+
fn cleanup_ret(&mut self, funclet: &Self::Funclet, unwind: Option<Self::BasicBlock>);
438433
fn catch_pad(&mut self, parent: Self::Value, args: &[Self::Value]) -> Self::Funclet;
439434
fn catch_switch(
440435
&mut self,
441436
parent: Option<Self::Value>,
442437
unwind: Option<Self::BasicBlock>,
443-
num_handlers: usize,
438+
handlers: &[Self::BasicBlock],
444439
) -> Self::Value;
445-
fn add_handler(&mut self, catch_switch: Self::Value, handler: Self::BasicBlock);
446-
fn set_personality_fn(&mut self, personality: Self::Value);
447440

448441
fn atomic_cmpxchg(
449442
&mut self,

0 commit comments

Comments
 (0)