Skip to content

Commit 98034e9

Browse files
Make drop-glue take advantage of -Zshare-generics.
1 parent e65eb79 commit 98034e9

File tree

5 files changed

+52
-16
lines changed

5 files changed

+52
-16
lines changed

src/librustc_codegen_ssa/back/symbol_export.rs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use rustc::middle::codegen_fn_attrs::CodegenFnAttrFlags;
55
use rustc::middle::exported_symbols::{metadata_symbol_name, ExportedSymbol, SymbolExportLevel};
66
use rustc::session::config;
77
use rustc::ty::query::Providers;
8-
use rustc::ty::subst::SubstsRef;
8+
use rustc::ty::subst::{GenericArgKind, SubstsRef};
99
use rustc::ty::Instance;
1010
use rustc::ty::{SymbolName, TyCtxt};
1111
use rustc_codegen_utils::symbol_names;
@@ -238,19 +238,31 @@ fn exported_symbols_provider_local(
238238
continue;
239239
}
240240

241-
if let &MonoItem::Fn(Instance { def: InstanceDef::Item(def_id), substs }) = mono_item {
242-
if substs.non_erasable_generics().next().is_some() {
243-
symbols
244-
.push((ExportedSymbol::Generic(def_id, substs), SymbolExportLevel::Rust));
241+
match *mono_item {
242+
MonoItem::Fn(Instance { def: InstanceDef::Item(def_id), substs }) => {
243+
if substs.non_erasable_generics().next().is_some() {
244+
let symbol = ExportedSymbol::Generic(def_id, substs);
245+
symbols.push((symbol, SymbolExportLevel::Rust));
246+
}
247+
}
248+
MonoItem::Fn(Instance { def: InstanceDef::DropGlue(def_id, Some(ty)), substs }) => {
249+
// A little sanity-check
250+
debug_assert_eq!(
251+
substs.non_erasable_generics().next(),
252+
Some(GenericArgKind::Type(ty))
253+
);
254+
let symbol = ExportedSymbol::Generic(def_id, substs);
255+
symbols.push((symbol, SymbolExportLevel::Rust));
256+
}
257+
_ => {
258+
// Any other symbols don't qualify for sharing
245259
}
246260
}
247261
}
248262
}
249263

250264
// Sort so we get a stable incr. comp. hash.
251-
symbols.sort_unstable_by(|&(ref symbol1, ..), &(ref symbol2, ..)| {
252-
symbol1.compare_stable(tcx, symbol2)
253-
});
265+
symbols.sort_by_cached_key(|s| s.0.symbol_name_for_local_instance(tcx));
254266

255267
Arc::new(symbols)
256268
}

src/librustc_mir/monomorphize/collector.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -713,7 +713,8 @@ fn visit_instance_use<'tcx>(
713713
// need a mono item.
714714
fn should_monomorphize_locally<'tcx>(tcx: TyCtxt<'tcx>, instance: &Instance<'tcx>) -> bool {
715715
let def_id = match instance.def {
716-
ty::InstanceDef::Item(def_id) => def_id,
716+
ty::InstanceDef::Item(def_id) | ty::InstanceDef::DropGlue(def_id, Some(_)) => def_id,
717+
717718
ty::InstanceDef::VtableShim(..)
718719
| ty::InstanceDef::ReifyShim(..)
719720
| ty::InstanceDef::ClosureOnceShim { .. }
@@ -725,12 +726,14 @@ fn should_monomorphize_locally<'tcx>(tcx: TyCtxt<'tcx>, instance: &Instance<'tcx
725726
};
726727

727728
if tcx.is_foreign_item(def_id) {
728-
// We can always link to foreign items.
729+
// Foreign items are always linked against, there's no way of
730+
// instantiating them.
729731
return false;
730732
}
731733

732734
if def_id.is_local() {
733-
// Local items cannot be referred to locally without monomorphizing them locally.
735+
// Local items cannot be referred to locally without
736+
// monomorphizing them locally.
734737
return true;
735738
}
736739

@@ -745,6 +748,7 @@ fn should_monomorphize_locally<'tcx>(tcx: TyCtxt<'tcx>, instance: &Instance<'tcx
745748
if !tcx.is_mir_available(def_id) {
746749
bug!("cannot create local mono-item for {:?}", def_id)
747750
}
751+
748752
return true;
749753

750754
fn is_available_upstream_generic<'tcx>(

src/librustc_mir/monomorphize/partitioning.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ fn mono_item_visibility(
324324
};
325325

326326
let def_id = match instance.def {
327-
InstanceDef::Item(def_id) => def_id,
327+
InstanceDef::Item(def_id) | InstanceDef::DropGlue(def_id, Some(_)) => def_id,
328328

329329
// These are all compiler glue and such, never exported, always hidden.
330330
InstanceDef::VtableShim(..)

src/test/codegen-units/partitioning/auxiliary/shared_generics_aux.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
// compile-flags:-Zshare-generics=yes
1+
// NOTE: We always compile this test with -Copt-level=0 because higher opt-levels
2+
// prevent drop-glue from participating in share-generics.
3+
// compile-flags:-Zshare-generics=yes -Copt-level=0
24
// no-prefer-dynamic
35

46
#![crate_type="rlib"]
@@ -8,5 +10,17 @@ pub fn generic_fn<T>(x: T, y: T) -> (T, T) {
810
}
911

1012
pub fn use_generic_fn_f32() -> (f32, f32) {
13+
// This line causes drop glue for Foo to be instantiated. We want to make
14+
// sure that this crate exports an instance to be re-used by share-generics.
15+
let _ = Foo(0);
16+
1117
generic_fn(0.0f32, 1.0f32)
1218
}
19+
20+
pub struct Foo(pub u32);
21+
22+
impl Drop for Foo {
23+
fn drop(&mut self) {
24+
println!("foo");
25+
}
26+
}

src/test/codegen-units/partitioning/shared-generics.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// ignore-tidy-linelength
22
// no-prefer-dynamic
3-
// compile-flags:-Zprint-mono-items=eager -Zshare-generics=yes -Zincremental=tmp/partitioning-tests/shared-generics-exe
3+
// NOTE: We always compile this test with -Copt-level=0 because higher opt-levels
4+
// prevent drop-glue from participating in share-generics.
5+
// compile-flags:-Zprint-mono-items=eager -Zshare-generics=yes -Zincremental=tmp/partitioning-tests/shared-generics-exe -Copt-level=0
46

57
#![crate_type="rlib"]
68

@@ -16,6 +18,10 @@ pub fn foo() {
1618
// This should not generate a monomorphization because it's already
1719
// available in `shared_generics_aux`.
1820
let _ = shared_generics_aux::generic_fn(0.0f32, 3.0f32);
19-
}
2021

21-
// MONO_ITEM drop-glue i8
22+
// The following line will drop an instance of `Foo`, generating a call to
23+
// Foo's drop-glue function. However, share-generics should take care of
24+
// reusing the drop-glue from the upstream crate, so we do not expect a
25+
// mono item for the drop-glue
26+
let _ = shared_generics_aux::Foo(1);
27+
}

0 commit comments

Comments
 (0)