Skip to content

Commit 052d0ed

Browse files
committed
rustc: compute FnAbi's for virtual calls through FnAbi::of_instance.
1 parent 39e50e2 commit 052d0ed

File tree

5 files changed

+36
-36
lines changed

5 files changed

+36
-36
lines changed

src/librustc/ty/layout.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2347,9 +2347,8 @@ where
23472347
+ HasTyCtxt<'tcx>
23482348
+ HasParamEnv<'tcx>,
23492349
{
2350-
fn of_instance(cx: &C, instance: ty::Instance<'tcx>) -> Self;
23512350
fn new(cx: &C, sig: ty::FnSig<'tcx>, extra_args: &[Ty<'tcx>]) -> Self;
2352-
fn new_vtable(cx: &C, sig: ty::FnSig<'tcx>, extra_args: &[Ty<'tcx>]) -> Self;
2351+
fn of_instance(cx: &C, instance: ty::Instance<'tcx>, extra_args: &[Ty<'tcx>]) -> Self;
23532352
fn new_internal(
23542353
cx: &C,
23552354
sig: ty::FnSig<'tcx>,
@@ -2367,25 +2366,22 @@ where
23672366
+ HasTyCtxt<'tcx>
23682367
+ HasParamEnv<'tcx>,
23692368
{
2370-
fn of_instance(cx: &C, instance: ty::Instance<'tcx>) -> Self {
2369+
fn new(cx: &C, sig: ty::FnSig<'tcx>, extra_args: &[Ty<'tcx>]) -> Self {
2370+
call::FnAbi::new_internal(cx, sig, extra_args, |ty, _| ArgAbi::new(cx.layout_of(ty)))
2371+
}
2372+
2373+
fn of_instance(cx: &C, instance: ty::Instance<'tcx>, extra_args: &[Ty<'tcx>]) -> Self {
23712374
let sig = instance.fn_sig(cx.tcx());
23722375
let sig = cx
23732376
.tcx()
23742377
.normalize_erasing_late_bound_regions(ty::ParamEnv::reveal_all(), &sig);
2375-
call::FnAbi::new(cx, sig, &[])
2376-
}
2377-
2378-
fn new(cx: &C, sig: ty::FnSig<'tcx>, extra_args: &[Ty<'tcx>]) -> Self {
2379-
call::FnAbi::new_internal(cx, sig, extra_args, |ty, _| ArgAbi::new(cx.layout_of(ty)))
2380-
}
23812378

2382-
fn new_vtable(cx: &C, sig: ty::FnSig<'tcx>, extra_args: &[Ty<'tcx>]) -> Self {
2383-
FnAbiExt::new_internal(cx, sig, extra_args, |ty, arg_idx| {
2379+
call::FnAbi::new_internal(cx, sig, extra_args, |ty, arg_idx| {
23842380
let mut layout = cx.layout_of(ty);
23852381
// Don't pass the vtable, it's not an argument of the virtual fn.
23862382
// Instead, pass just the data pointer, but give it the type `*const/mut dyn Trait`
23872383
// or `&/&mut dyn Trait` because this is special-cased elsewhere in codegen
2388-
if arg_idx == Some(0) {
2384+
if let (ty::InstanceDef::Virtual(..), Some(0)) = (&instance.def, arg_idx) {
23892385
let fat_pointer_ty = if layout.is_unsized() {
23902386
// unsized `self` is passed as a pointer to `self`
23912387
// FIXME (mikeyhew) change this to use &own if it is ever added to the language

src/librustc_codegen_llvm/callee.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ pub fn get_fn(
4040
let sym = tcx.symbol_name(instance).name.as_str();
4141
debug!("get_fn({:?}: {:?}) => {}", instance, instance.ty(cx.tcx()), sym);
4242

43-
let fn_abi = FnAbi::of_instance(cx, instance);
43+
let fn_abi = FnAbi::of_instance(cx, instance, &[]);
4444

4545
let llfn = if let Some(llfn) = cx.get_declared_value(&sym) {
4646
// Create a fn pointer with the new signature.

src/librustc_codegen_llvm/mono_item.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ impl PreDefineMethods<'tcx> for CodegenCx<'ll, 'tcx> {
4343
assert!(!instance.substs.needs_infer() &&
4444
!instance.substs.has_param_types());
4545

46-
let fn_abi = FnAbi::of_instance(self, instance);
46+
let fn_abi = FnAbi::of_instance(self, instance, &[]);
4747
let lldecl = self.declare_fn(symbol_name, &fn_abi);
4848
unsafe { llvm::LLVMRustSetLinkage(lldecl, base::linkage_to_llvm(linkage)) };
4949
let attrs = self.tcx.codegen_fn_attrs(instance.def_id());

src/librustc_codegen_ssa/mir/block.rs

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -345,20 +345,21 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
345345
&args1[..]
346346
};
347347
let (drop_fn, fn_abi) = match ty.kind {
348+
// FIXME(eddyb) perhaps move some of this logic into
349+
// `Instance::resolve_drop_in_place`?
348350
ty::Dynamic(..) => {
349-
let sig = drop_fn.fn_sig(self.cx.tcx());
350-
let sig = self.cx.tcx().normalize_erasing_late_bound_regions(
351-
ty::ParamEnv::reveal_all(),
352-
&sig,
353-
);
354-
let fn_abi = FnAbi::new_vtable(&bx, sig, &[]);
351+
let virtual_drop = Instance {
352+
def: ty::InstanceDef::Virtual(drop_fn.def_id(), 0),
353+
substs: drop_fn.substs,
354+
};
355+
let fn_abi = FnAbi::of_instance(&bx, virtual_drop, &[]);
355356
let vtable = args[1];
356357
args = &args[..1];
357358
(meth::DESTRUCTOR.get_fn(&mut bx, vtable, &fn_abi), fn_abi)
358359
}
359360
_ => {
360361
(bx.get_fn_addr(drop_fn),
361-
FnAbi::of_instance(&bx, drop_fn))
362+
FnAbi::of_instance(&bx, drop_fn, &[]))
362363
}
363364
};
364365
helper.maybe_sideeffect(self.mir, &mut bx, &[target]);
@@ -439,7 +440,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
439440
// Obtain the panic entry point.
440441
let def_id = common::langcall(bx.tcx(), Some(span), "", lang_item);
441442
let instance = ty::Instance::mono(bx.tcx(), def_id);
442-
let fn_abi = FnAbi::of_instance(&bx, instance);
443+
let fn_abi = FnAbi::of_instance(&bx, instance, &[]);
443444
let llfn = bx.get_fn_addr(instance);
444445

445446
// Codegen the actual panic invoke/call.
@@ -474,6 +475,18 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
474475
_ => bug!("{} is not callable", callee.layout.ty),
475476
};
476477
let def = instance.map(|i| i.def);
478+
479+
if let Some(ty::InstanceDef::DropGlue(_, None)) = def {
480+
// Empty drop glue; a no-op.
481+
let &(_, target) = destination.as_ref().unwrap();
482+
helper.maybe_sideeffect(self.mir, &mut bx, &[target]);
483+
helper.funclet_br(self, &mut bx, target);
484+
return;
485+
}
486+
487+
// FIXME(eddyb) avoid computing this if possible, when `instance` is
488+
// available - right now `sig` is only needed for getting the `abi`
489+
// and figuring out how many extra args were passed to a C-variadic `fn`.
477490
let sig = callee.layout.ty.fn_sig(bx.tcx());
478491
let sig = bx.tcx().normalize_erasing_late_bound_regions(
479492
ty::ParamEnv::reveal_all(),
@@ -514,18 +527,9 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
514527
self.monomorphize(&op_ty)
515528
}).collect::<Vec<_>>();
516529

517-
let fn_abi = match def {
518-
Some(ty::InstanceDef::Virtual(..)) => {
519-
FnAbi::new_vtable(&bx, sig, &extra_args)
520-
}
521-
Some(ty::InstanceDef::DropGlue(_, None)) => {
522-
// Empty drop glue; a no-op.
523-
let &(_, target) = destination.as_ref().unwrap();
524-
helper.maybe_sideeffect(self.mir, &mut bx, &[target]);
525-
helper.funclet_br(self, &mut bx, target);
526-
return;
527-
}
528-
_ => FnAbi::new(&bx, sig, &extra_args)
530+
let fn_abi = match instance {
531+
Some(instance) => FnAbi::of_instance(&bx, instance, &extra_args),
532+
None => FnAbi::new(&bx, sig, &extra_args)
529533
};
530534

531535
// For normal codegen, this Miri-specific intrinsic is just a NOP.
@@ -549,7 +553,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
549553
let def_id =
550554
common::langcall(bx.tcx(), Some(span), "", lang_items::PanicFnLangItem);
551555
let instance = ty::Instance::mono(bx.tcx(), def_id);
552-
let fn_abi = FnAbi::of_instance(&bx, instance);
556+
let fn_abi = FnAbi::of_instance(&bx, instance, &[]);
553557
let llfn = bx.get_fn_addr(instance);
554558

555559
if let Some((_, target)) = destination.as_ref() {

src/librustc_codegen_ssa/mir/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ pub fn codegen_mir<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
129129

130130
let mir = cx.tcx().instance_mir(instance.def);
131131

132-
let fn_abi = FnAbi::of_instance(cx, instance);
132+
let fn_abi = FnAbi::of_instance(cx, instance, &[]);
133133
debug!("fn_abi: {:?}", fn_abi);
134134

135135
let debug_context = cx.create_function_debug_context(instance, &fn_abi, llfn, &mir);

0 commit comments

Comments
 (0)