Skip to content

Commit 080d085

Browse files
Omit non-needs_drop drop_in_place in vtables
This replaces the drop_in_place reference with null in vtables. On librustc_driver.so, this drops about ~17k dynamic relocations from the output, since many vtables can now be placed in read-only memory, rather than having a relocated pointer included. This makes a tradeoff by adding a null check at vtable call sites. I'm not sure that's readily avoidable without changing the vtable format (e.g., so that we can use a pc-relative relocation instead of an absolute address, and avoid the dynamic relocation that way). But it seems likely that the check is cheap at runtime.
1 parent 61a1dbd commit 080d085

File tree

4 files changed

+70
-11
lines changed

4 files changed

+70
-11
lines changed

compiler/rustc_codegen_ssa/src/meth.rs

+13-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ impl<'a, 'tcx> VirtualIndex {
1313
VirtualIndex(index as u64)
1414
}
1515

16-
pub fn get_fn<Bx: BuilderMethods<'a, 'tcx>>(
16+
pub fn get_optional_fn<Bx: BuilderMethods<'a, 'tcx>>(
1717
self,
1818
bx: &mut Bx,
1919
llvtable: Bx::Value,
@@ -39,13 +39,24 @@ impl<'a, 'tcx> VirtualIndex {
3939
} else {
4040
let gep = bx.inbounds_ptradd(llvtable, bx.const_usize(vtable_byte_offset));
4141
let ptr = bx.load(llty, gep, ptr_align);
42-
bx.nonnull_metadata(ptr);
4342
// VTable loads are invariant.
4443
bx.set_invariant_load(ptr);
4544
ptr
4645
}
4746
}
4847

48+
pub fn get_fn<Bx: BuilderMethods<'a, 'tcx>>(
49+
self,
50+
bx: &mut Bx,
51+
llvtable: Bx::Value,
52+
ty: Ty<'tcx>,
53+
fn_abi: &FnAbi<'tcx, Ty<'tcx>>,
54+
) -> Bx::Value {
55+
let ptr = self.get_optional_fn(bx, llvtable, ty, fn_abi);
56+
bx.nonnull_metadata(ptr);
57+
ptr
58+
}
59+
4960
pub fn get_usize<Bx: BuilderMethods<'a, 'tcx>>(
5061
self,
5162
bx: &mut Bx,

compiler/rustc_codegen_ssa/src/mir/block.rs

+33-5
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
498498
&mut self,
499499
helper: TerminatorCodegenHelper<'tcx>,
500500
bx: &mut Bx,
501+
source_info: &mir::SourceInfo,
501502
location: mir::Place<'tcx>,
502503
target: mir::BasicBlock,
503504
unwind: mir::UnwindAction,
@@ -521,7 +522,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
521522
args1 = [place.val.llval];
522523
&args1[..]
523524
};
524-
let (drop_fn, fn_abi, drop_instance) =
525+
let (maybe_null, drop_fn, fn_abi, drop_instance) =
525526
match ty.kind() {
526527
// FIXME(eddyb) perhaps move some of this logic into
527528
// `Instance::resolve_drop_in_place`?
@@ -550,6 +551,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
550551
// Truncate vtable off of args list
551552
args = &args[..1];
552553
(
554+
true,
553555
meth::VirtualIndex::from_index(ty::COMMON_VTABLE_ENTRIES_DROPINPLACE)
554556
.get_fn(bx, vtable, ty, fn_abi),
555557
fn_abi,
@@ -593,18 +595,37 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
593595
args = &args[..1];
594596
debug!("args' = {:?}", args);
595597
(
598+
true,
596599
meth::VirtualIndex::from_index(ty::COMMON_VTABLE_ENTRIES_DROPINPLACE)
597600
.get_fn(bx, meta.immediate(), ty, fn_abi),
598601
fn_abi,
599602
virtual_drop,
600603
)
601604
}
602605
_ => (
606+
false,
603607
bx.get_fn_addr(drop_fn),
604608
bx.fn_abi_of_instance(drop_fn, ty::List::empty()),
605609
drop_fn,
606610
),
607611
};
612+
613+
// We generate a null check for the drop_fn. This saves a bunch of relocations being
614+
// generated for no-op drops.
615+
if maybe_null {
616+
let is_not_null = bx.append_sibling_block("is_not_null");
617+
let llty = bx.fn_ptr_backend_type(fn_abi);
618+
let null = bx.const_null(llty);
619+
let non_null = bx.icmp(
620+
base::bin_op_to_icmp_predicate(mir::BinOp::Ne.to_hir_binop(), false),
621+
drop_fn,
622+
null,
623+
);
624+
bx.cond_br(non_null, is_not_null, self.llbb(target));
625+
bx.switch_to_block(is_not_null);
626+
self.set_debug_loc(bx, *source_info);
627+
}
628+
608629
helper.do_call(
609630
self,
610631
bx,
@@ -615,7 +636,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
615636
unwind,
616637
&[],
617638
Some(drop_instance),
618-
mergeable_succ,
639+
!maybe_null && mergeable_succ,
619640
)
620641
}
621642

@@ -1344,9 +1365,16 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
13441365
MergingSucc::False
13451366
}
13461367

1347-
mir::TerminatorKind::Drop { place, target, unwind, replace: _ } => {
1348-
self.codegen_drop_terminator(helper, bx, place, target, unwind, mergeable_succ())
1349-
}
1368+
mir::TerminatorKind::Drop { place, target, unwind, replace: _ } => self
1369+
.codegen_drop_terminator(
1370+
helper,
1371+
bx,
1372+
&terminator.source_info,
1373+
place,
1374+
target,
1375+
unwind,
1376+
mergeable_succ(),
1377+
),
13501378

13511379
mir::TerminatorKind::Assert { ref cond, expected, ref msg, target, unwind } => self
13521380
.codegen_assert_terminator(

compiler/rustc_middle/src/ty/vtable.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,14 @@ pub(super) fn vtable_allocation_provider<'tcx>(
8383
let idx: u64 = u64::try_from(idx).unwrap();
8484
let scalar = match entry {
8585
VtblEntry::MetadataDropInPlace => {
86-
let instance = ty::Instance::resolve_drop_in_place(tcx, ty);
87-
let fn_alloc_id = tcx.reserve_and_set_fn_alloc(instance);
88-
let fn_ptr = Pointer::from(fn_alloc_id);
89-
Scalar::from_pointer(fn_ptr, &tcx)
86+
if ty.needs_drop(tcx, ty::ParamEnv::reveal_all()) {
87+
let instance = ty::Instance::resolve_drop_in_place(tcx, ty);
88+
let fn_alloc_id = tcx.reserve_and_set_fn_alloc(instance);
89+
let fn_ptr = Pointer::from(fn_alloc_id);
90+
Scalar::from_pointer(fn_ptr, &tcx)
91+
} else {
92+
Scalar::from_maybe_pointer(Pointer::null(), &tcx)
93+
}
9094
}
9195
VtblEntry::MetadataSize => Scalar::from_uint(size, ptr_size),
9296
VtblEntry::MetadataAlign => Scalar::from_uint(align, ptr_size),

tests/codegen/vtable-loads.rs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//@ compile-flags: -O
2+
3+
#![crate_type = "lib"]
4+
5+
// CHECK-LABEL: @loop_skips_vtable_load
6+
#[no_mangle]
7+
pub fn loop_skips_vtable_load(x: &dyn Fn()) {
8+
// CHECK: load ptr, ptr %0{{.*}}, !invariant.load
9+
// CHECK-NEXT: tail call void %1
10+
// CHECK-NOT: load ptr
11+
x();
12+
for _ in 0..100 {
13+
// CHECK: tail call void %1
14+
x();
15+
}
16+
}

0 commit comments

Comments
 (0)