Skip to content

Commit 37d7de3

Browse files
committed
Auto merge of #105252 - bjorn3:codegen_less_pair_values, r=nagisa
Use struct types during codegen in less places This makes it easier to use cg_ssa from a backend like Cranelift that doesn't have any struct types at all. After this PR struct types are still used for function arguments and return values. Removing those usages is harder but should still be doable.
2 parents 2176e3a + 262ace5 commit 37d7de3

File tree

7 files changed

+30
-35
lines changed

7 files changed

+30
-35
lines changed

compiler/rustc_codegen_gcc/src/builder.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -1119,18 +1119,18 @@ impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
11191119
// TODO(antoyo)
11201120
}
11211121

1122-
fn cleanup_landing_pad(&mut self, _ty: Type<'gcc>, _pers_fn: RValue<'gcc>) -> RValue<'gcc> {
1123-
let field1 = self.context.new_field(None, self.u8_type.make_pointer(), "landing_pad_field_1");
1124-
let field2 = self.context.new_field(None, self.i32_type, "landing_pad_field_1");
1125-
let struct_type = self.context.new_struct_type(None, "landing_pad", &[field1, field2]);
1126-
self.current_func().new_local(None, struct_type.as_type(), "landing_pad")
1127-
.to_rvalue()
1122+
fn cleanup_landing_pad(&mut self, _pers_fn: RValue<'gcc>) -> (RValue<'gcc>, RValue<'gcc>) {
1123+
(
1124+
self.current_func().new_local(None, self.u8_type.make_pointer(), "landing_pad0")
1125+
.to_rvalue(),
1126+
self.current_func().new_local(None, self.i32_type, "landing_pad1").to_rvalue(),
1127+
)
11281128
// TODO(antoyo): Properly implement unwinding.
11291129
// the above is just to make the compilation work as it seems
11301130
// rustc_codegen_ssa now calls the unwinding builder methods even on panic=abort.
11311131
}
11321132

1133-
fn resume(&mut self, _exn: RValue<'gcc>) {
1133+
fn resume(&mut self, _exn0: RValue<'gcc>, _exn1: RValue<'gcc>) {
11341134
// TODO(bjorn3): Properly implement unwinding.
11351135
self.unreachable();
11361136
}

compiler/rustc_codegen_llvm/src/builder.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -979,15 +979,20 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
979979
}
980980
}
981981

982-
fn cleanup_landing_pad(&mut self, ty: &'ll Type, pers_fn: &'ll Value) -> &'ll Value {
982+
fn cleanup_landing_pad(&mut self, pers_fn: &'ll Value) -> (&'ll Value, &'ll Value) {
983+
let ty = self.type_struct(&[self.type_i8p(), self.type_i32()], false);
983984
let landing_pad = self.landing_pad(ty, pers_fn, 1 /* FIXME should this be 0? */);
984985
unsafe {
985986
llvm::LLVMSetCleanup(landing_pad, llvm::True);
986987
}
987-
landing_pad
988+
(self.extract_value(landing_pad, 0), self.extract_value(landing_pad, 1))
988989
}
989990

990-
fn resume(&mut self, exn: &'ll Value) {
991+
fn resume(&mut self, exn0: &'ll Value, exn1: &'ll Value) {
992+
let ty = self.type_struct(&[self.type_i8p(), self.type_i32()], false);
993+
let mut exn = self.const_undef(ty);
994+
exn = self.insert_value(exn, exn0, 0);
995+
exn = self.insert_value(exn, exn1, 1);
991996
unsafe {
992997
llvm::LLVMBuildResume(self.llbuilder, exn);
993998
}

compiler/rustc_codegen_llvm/src/intrinsic.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,9 @@ impl<'ll, 'tcx> IntrinsicCallMethods<'tcx> for Builder<'_, 'll, 'tcx> {
424424
typeid: &'ll Value,
425425
) -> Self::Value {
426426
let vtable_byte_offset = self.const_i32(vtable_byte_offset as i32);
427-
self.call_intrinsic("llvm.type.checked.load", &[llvtable, vtable_byte_offset, typeid])
427+
let type_checked_load =
428+
self.call_intrinsic("llvm.type.checked.load", &[llvtable, vtable_byte_offset, typeid]);
429+
self.extract_value(type_checked_load, 0)
428430
}
429431

430432
fn va_start(&mut self, va_list: &'ll Value) -> &'ll Value {

compiler/rustc_codegen_ssa/src/meth.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ impl<'a, 'tcx> VirtualIndex {
3131
let typeid =
3232
bx.typeid_metadata(typeid_for_trait_ref(bx.tcx(), expect_dyn_trait_in_self(ty)));
3333
let vtable_byte_offset = self.0 * bx.data_layout().pointer_size.bytes();
34-
let type_checked_load = bx.type_checked_load(llvtable, vtable_byte_offset, typeid);
35-
let func = bx.extract_value(type_checked_load, 0);
34+
let func = bx.type_checked_load(llvtable, vtable_byte_offset, typeid);
3635
bx.pointercast(func, llty)
3736
} else {
3837
let ptr_align = bx.tcx().data_layout.pointer_align.abi;

compiler/rustc_codegen_ssa/src/mir/block.rs

+8-19
Original file line numberDiff line numberDiff line change
@@ -289,16 +289,13 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
289289
bx.cleanup_ret(funclet, None);
290290
} else {
291291
let slot = self.get_personality_slot(bx);
292-
let lp0 = slot.project_field(bx, 0);
293-
let lp0 = bx.load_operand(lp0).immediate();
294-
let lp1 = slot.project_field(bx, 1);
295-
let lp1 = bx.load_operand(lp1).immediate();
292+
let exn0 = slot.project_field(bx, 0);
293+
let exn0 = bx.load_operand(exn0).immediate();
294+
let exn1 = slot.project_field(bx, 1);
295+
let exn1 = bx.load_operand(exn1).immediate();
296296
slot.storage_dead(bx);
297297

298-
let mut lp = bx.const_undef(self.landing_pad_type());
299-
lp = bx.insert_value(lp, lp0, 0);
300-
lp = bx.insert_value(lp, lp1, 1);
301-
bx.resume(lp);
298+
bx.resume(exn0, exn1);
302299
}
303300
}
304301

@@ -1636,24 +1633,17 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
16361633
let mut cleanup_bx = Bx::build(self.cx, cleanup_llbb);
16371634

16381635
let llpersonality = self.cx.eh_personality();
1639-
let llretty = self.landing_pad_type();
1640-
let lp = cleanup_bx.cleanup_landing_pad(llretty, llpersonality);
1636+
let (exn0, exn1) = cleanup_bx.cleanup_landing_pad(llpersonality);
16411637

16421638
let slot = self.get_personality_slot(&mut cleanup_bx);
16431639
slot.storage_live(&mut cleanup_bx);
1644-
Pair(cleanup_bx.extract_value(lp, 0), cleanup_bx.extract_value(lp, 1))
1645-
.store(&mut cleanup_bx, slot);
1640+
Pair(exn0, exn1).store(&mut cleanup_bx, slot);
16461641

16471642
cleanup_bx.br(llbb);
16481643
cleanup_llbb
16491644
}
16501645
}
16511646

1652-
fn landing_pad_type(&self) -> Bx::Type {
1653-
let cx = self.cx;
1654-
cx.type_struct(&[cx.type_i8p(), cx.type_i32()], false)
1655-
}
1656-
16571647
fn unreachable_block(&mut self) -> Bx::BasicBlock {
16581648
self.unreachable_block.unwrap_or_else(|| {
16591649
let llbb = Bx::append_block(self.cx, self.llfn, "unreachable");
@@ -1673,8 +1663,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
16731663
self.set_debug_loc(&mut bx, mir::SourceInfo::outermost(self.mir.span));
16741664

16751665
let llpersonality = self.cx.eh_personality();
1676-
let llretty = self.landing_pad_type();
1677-
bx.cleanup_landing_pad(llretty, llpersonality);
1666+
bx.cleanup_landing_pad(llpersonality);
16781667

16791668
let (fn_abi, fn_ptr) = common::build_langcall(&bx, None, LangItem::PanicNoUnwind);
16801669
let fn_ty = bx.fn_decl_backend_type(&fn_abi);

compiler/rustc_codegen_ssa/src/mir/rvalue.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
462462
assert!(bx.cx().tcx().is_static(def_id));
463463
let static_ = bx.get_static(def_id);
464464
let layout = bx.layout_of(bx.cx().tcx().static_ptr_ty(def_id));
465-
OperandRef::from_immediate_or_packed_pair(bx, static_, layout)
465+
OperandRef { val: OperandValue::Immediate(static_), layout }
466466
}
467467
mir::Rvalue::Use(ref operand) => self.codegen_operand(bx, operand),
468468
mir::Rvalue::Repeat(..) | mir::Rvalue::Aggregate(..) => {

compiler/rustc_codegen_ssa/src/traits/builder.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -271,8 +271,8 @@ pub trait BuilderMethods<'a, 'tcx>:
271271
fn set_personality_fn(&mut self, personality: Self::Value);
272272

273273
// These are used by everyone except msvc
274-
fn cleanup_landing_pad(&mut self, ty: Self::Type, pers_fn: Self::Value) -> Self::Value;
275-
fn resume(&mut self, exn: Self::Value);
274+
fn cleanup_landing_pad(&mut self, pers_fn: Self::Value) -> (Self::Value, Self::Value);
275+
fn resume(&mut self, exn0: Self::Value, exn1: Self::Value);
276276

277277
// These are used only by msvc
278278
fn cleanup_pad(&mut self, parent: Option<Self::Value>, args: &[Self::Value]) -> Self::Funclet;

0 commit comments

Comments
 (0)