Skip to content

Commit d28feb0

Browse files
Auto merge of #142508 - Mark-Simulacrum:skip-noop-drop-glue, r=<try>
Skip noop drop glue 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. Opening this for a perf run before asking for review.
2 parents 9822e3d + c839507 commit d28feb0

File tree

5 files changed

+20
-19
lines changed

5 files changed

+20
-19
lines changed

compiler/rustc_monomorphize/src/collector.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -948,10 +948,14 @@ fn visit_instance_use<'tcx>(
948948
bug!("{:?} being reified", instance);
949949
}
950950
ty::InstanceKind::DropGlue(_, None) => {
951-
// Don't need to emit noop drop glue if we are calling directly.
952-
if !is_direct_call {
953-
output.push(create_fn_mono_item(tcx, instance, source));
954-
}
951+
// No-op drop glue never needs to be emitted.
952+
// In direct calls, we skip it in codegen. In indirect calls (vtables) we place a null
953+
// pointer rather than codegen'ing a pointer to the empty drop.
954+
//
955+
// Note that if user code casts drop_in_place to a fn(...) that's not a DropGlue, so
956+
// it won't hit this branch.
957+
//
958+
// If we get this wrong we'll see linker errors.
955959
}
956960
ty::InstanceKind::DropGlue(_, Some(_))
957961
| ty::InstanceKind::FutureDropPollShim(..)

tests/codegen-units/item-collection/instantiation-through-vtable.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,13 @@ impl<T> Trait for Struct<T> {
2424
pub fn start(_: isize, _: *const *const u8) -> isize {
2525
let s1 = Struct { _a: 0u32 };
2626

27-
//~ MONO_ITEM fn std::ptr::drop_in_place::<Struct<u32>> - shim(None) @@ instantiation_through_vtable-cgu.0[External]
2827
//~ MONO_ITEM fn <Struct<u32> as Trait>::foo
2928
//~ MONO_ITEM fn <Struct<u32> as Trait>::bar
3029
let r1 = &s1 as &Trait;
3130
r1.foo();
3231
r1.bar();
3332

3433
let s1 = Struct { _a: 0u64 };
35-
//~ MONO_ITEM fn std::ptr::drop_in_place::<Struct<u64>> - shim(None) @@ instantiation_through_vtable-cgu.0[External]
3634
//~ MONO_ITEM fn <Struct<u64> as Trait>::foo
3735
//~ MONO_ITEM fn <Struct<u64> as Trait>::bar
3836
let _ = &s1 as &Trait;

tests/codegen-units/item-collection/non-generic-closures.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//@ compile-flags:-Clink-dead-code -Zinline-mir=no
1+
//@ compile-flags:-Clink-dead-code -Zinline-mir=no -Zhuman-readable-cgu-names
22

33
#![deny(dead_code)]
44
#![crate_type = "lib"]
@@ -22,9 +22,8 @@ fn assigned_to_variable_but_not_executed() {
2222
//~ MONO_ITEM fn assigned_to_variable_executed_indirectly @@ non_generic_closures-cgu.0[External]
2323
fn assigned_to_variable_executed_indirectly() {
2424
//~ MONO_ITEM fn assigned_to_variable_executed_indirectly::{closure#0} @@ non_generic_closures-cgu.0[External]
25-
//~ MONO_ITEM fn <{closure@TEST_PATH:28:13: 28:21} as std::ops::FnOnce<(i32,)>>::call_once - shim @@ non_generic_closures-cgu.0[External]
26-
//~ MONO_ITEM fn <{closure@TEST_PATH:28:13: 28:21} as std::ops::FnOnce<(i32,)>>::call_once - shim(vtable) @@ non_generic_closures-cgu.0[External]
27-
//~ MONO_ITEM fn std::ptr::drop_in_place::<{closure@TEST_PATH:28:13: 28:21}> - shim(None) @@ non_generic_closures-cgu.0[External]
25+
//~ MONO_ITEM fn <{closure@TEST_PATH:27:13: 27:21} as std::ops::FnOnce<(i32,)>>::call_once - shim @@ non_generic_closures-cgu.0[External]
26+
//~ MONO_ITEM fn <{closure@TEST_PATH:27:13: 27:21} as std::ops::FnOnce<(i32,)>>::call_once - shim(vtable) @@ non_generic_closures-cgu.0[External]
2827
let f = |a: i32| {
2928
let _ = a + 2;
3029
};

tests/codegen-units/item-collection/unsizing.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,25 +47,21 @@ impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<Wrapper<U>> for Wrapper<T>
4747
pub fn start(_: isize, _: *const *const u8) -> isize {
4848
// simple case
4949
let bool_sized = &true;
50-
//~ MONO_ITEM fn std::ptr::drop_in_place::<bool> - shim(None) @@ unsizing-cgu.0[Internal]
5150
//~ MONO_ITEM fn <bool as Trait>::foo
5251
let _bool_unsized = bool_sized as &Trait;
5352

5453
let char_sized = &'a';
5554

56-
//~ MONO_ITEM fn std::ptr::drop_in_place::<char> - shim(None) @@ unsizing-cgu.0[Internal]
5755
//~ MONO_ITEM fn <char as Trait>::foo
5856
let _char_unsized = char_sized as &Trait;
5957

6058
// struct field
6159
let struct_sized = &Struct { _a: 1, _b: 2, _c: 3.0f64 };
62-
//~ MONO_ITEM fn std::ptr::drop_in_place::<f64> - shim(None) @@ unsizing-cgu.0[Internal]
6360
//~ MONO_ITEM fn <f64 as Trait>::foo
6461
let _struct_unsized = struct_sized as &Struct<Trait>;
6562

6663
// custom coercion
6764
let wrapper_sized = Wrapper(&0u32);
68-
//~ MONO_ITEM fn std::ptr::drop_in_place::<u32> - shim(None) @@ unsizing-cgu.0[Internal]
6965
//~ MONO_ITEM fn <u32 as Trait>::foo
7066
let _wrapper_sized = wrapper_sized as Wrapper<Trait>;
7167

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
// Verifies that type metadata identifiers for drop functions are emitted correctly.
22
//
3+
// Non needs_drop drop glue isn't codegen'd at all, so we don't try to check the IDs there. But we
4+
// do check it's not emitted which should help catch bugs if we do start generating it again in the
5+
// future.
6+
//
37
//@ needs-sanitizer-cfi
48
//@ compile-flags: -Clto -Cno-prepopulate-passes -Copt-level=0 -Zsanitizer=cfi -Ctarget-feature=-crt-static
59

@@ -10,18 +14,18 @@
1014
// CHECK: call i1 @llvm.type.test(ptr {{%.+}}, metadata !"_ZTSFvPu3dynIu{{[0-9]+}}NtNtNtC{{[[:print:]]+}}_4core3ops4drop4Dropu6regionEE")
1115

1216
struct EmptyDrop;
13-
// CHECK: define{{.*}}4core3ptr{{[0-9]+}}drop_in_place$LT${{.*}}EmptyDrop$GT${{.*}}!type ![[TYPE1]] !type !{{[0-9]+}} !type !{{[0-9]+}} !type !{{[0-9]+}}
17+
// CHECK-NOT: define{{.*}}4core3ptr{{[0-9]+}}drop_in_place$LT${{.*}}EmptyDrop$GT${{.*}}!type ![[TYPE1]] !type !{{[0-9]+}} !type !{{[0-9]+}} !type !{{[0-9]+}}
1418

15-
struct NonEmptyDrop;
19+
struct PresentDrop;
1620

17-
impl Drop for NonEmptyDrop {
21+
impl Drop for PresentDrop {
1822
fn drop(&mut self) {}
19-
// CHECK: define{{.*}}4core3ptr{{[0-9]+}}drop_in_place$LT${{.*}}NonEmptyDrop$GT${{.*}}!type ![[TYPE1]] !type !{{[0-9]+}} !type !{{[0-9]+}} !type !{{[0-9]+}}
23+
// CHECK: define{{.*}}4core3ptr{{[0-9]+}}drop_in_place$LT${{.*}}PresentDrop$GT${{.*}}!type ![[TYPE1]] !type !{{[0-9]+}} !type !{{[0-9]+}} !type !{{[0-9]+}}
2024
}
2125

2226
pub fn foo() {
2327
let _ = Box::new(EmptyDrop) as Box<dyn Send>;
24-
let _ = Box::new(NonEmptyDrop) as Box<dyn Send>;
28+
let _ = Box::new(PresentDrop) as Box<dyn Send>;
2529
}
2630

2731
// CHECK: ![[TYPE1]] = !{i64 0, !"_ZTSFvPu3dynIu{{[0-9]+}}NtNtNtC{{[[:print:]]+}}_4core3ops4drop4Dropu6regionEE"}

0 commit comments

Comments
 (0)