From 1f405735078ac34be300fd2bd7c532aaa0bb6fc5 Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Sat, 14 Jun 2025 10:22:49 -0400 Subject: [PATCH] Skip collecting no-op DropGlue in vtables Since 122662 this no longer gets used in vtables, so we're safe to fully drop generating these empty functions. Those are eventually cleaned up by LLVM, but it's wasteful to produce them in the first place. This also adds a missing test for fn-ptr casts, which do still need to generate no-op drop glue. It's possible a future optimization could point all of those at the same drop glue (e.g., for *mut ()) rather than for each separate type, but that would require extra work for CFI and isn't particularly easy to do anyway. --- compiler/rustc_monomorphize/src/collector.rs | 12 ++++++++-- .../item-collection/drop-glue-noop.rs | 23 +++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 tests/codegen-units/item-collection/drop-glue-noop.rs diff --git a/compiler/rustc_monomorphize/src/collector.rs b/compiler/rustc_monomorphize/src/collector.rs index 173030e0326e..e90e32ebebb9 100644 --- a/compiler/rustc_monomorphize/src/collector.rs +++ b/compiler/rustc_monomorphize/src/collector.rs @@ -949,6 +949,9 @@ fn visit_instance_use<'tcx>( } ty::InstanceKind::DropGlue(_, None) => { // Don't need to emit noop drop glue if we are calling directly. + // + // Note that we also optimize away the call to visit_instance_use in vtable construction + // (see create_mono_items_for_vtable_methods). if !is_direct_call { output.push(create_fn_mono_item(tcx, instance, source)); } @@ -1177,8 +1180,13 @@ fn create_mono_items_for_vtable_methods<'tcx>( output.extend(methods); } - // Also add the destructor. - visit_drop_use(tcx, impl_ty, false, source, output); + // Also add the destructor, if it's necessary. + // + // This matches the check in vtable_allocation_provider in middle/ty/vtable.rs, + // if we don't need drop we're not adding an actual pointer to the vtable. + if impl_ty.needs_drop(tcx, ty::TypingEnv::fully_monomorphized()) { + visit_drop_use(tcx, impl_ty, false, source, output); + } } /// Scans the CTFE alloc in order to find function pointers and statics that must be monomorphized. diff --git a/tests/codegen-units/item-collection/drop-glue-noop.rs b/tests/codegen-units/item-collection/drop-glue-noop.rs new file mode 100644 index 000000000000..604ba883bb28 --- /dev/null +++ b/tests/codegen-units/item-collection/drop-glue-noop.rs @@ -0,0 +1,23 @@ +//@ compile-flags:-Clink-dead-code -Zmir-opt-level=0 + +#![deny(dead_code)] +#![crate_type = "lib"] + +//~ MONO_ITEM fn start +#[no_mangle] +pub fn start(_: isize, _: *const *const u8) -> isize { + // No item produced for this, it's a no-op drop and so is removed. + unsafe { + std::ptr::drop_in_place::(&mut 0); + } + + // No choice but to codegen for indirect drop as a function pointer, since we have to produce a + // function with the right signature. In vtables we can avoid that (tested in + // instantiation-through-vtable.rs) because we special case null pointer for drop glue since + // #122662. + // + //~ MONO_ITEM fn std::ptr::drop_in_place:: - shim(None) @@ drop_glue_noop-cgu.0[External] + std::ptr::drop_in_place:: as unsafe fn(*mut u64); + + 0 +}