Skip to content

Commit 2ceb92b

Browse files
Make drop-glue take advantage of -Zshare-generics.
1 parent 190f0c0 commit 2ceb92b

File tree

5 files changed

+52
-16
lines changed

5 files changed

+52
-16
lines changed

src/librustc_codegen_ssa/back/symbol_export.rs

+20-8
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::{self, Sanitizer};
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;
@@ -248,19 +248,31 @@ fn exported_symbols_provider_local(
248248
continue;
249249
}
250250

251-
if let &MonoItem::Fn(Instance { def: InstanceDef::Item(def_id), substs }) = mono_item {
252-
if substs.non_erasable_generics().next().is_some() {
253-
symbols
254-
.push((ExportedSymbol::Generic(def_id, substs), SymbolExportLevel::Rust));
251+
match *mono_item {
252+
MonoItem::Fn(Instance { def: InstanceDef::Item(def_id), substs }) => {
253+
if substs.non_erasable_generics().next().is_some() {
254+
let symbol = ExportedSymbol::Generic(def_id, substs);
255+
symbols.push((symbol, SymbolExportLevel::Rust));
256+
}
257+
}
258+
MonoItem::Fn(Instance { def: InstanceDef::DropGlue(def_id, Some(ty)), substs }) => {
259+
// A little sanity-check
260+
debug_assert_eq!(
261+
substs.non_erasable_generics().next(),
262+
Some(GenericArgKind::Type(ty))
263+
);
264+
let symbol = ExportedSymbol::Generic(def_id, substs);
265+
symbols.push((symbol, SymbolExportLevel::Rust));
266+
}
267+
_ => {
268+
// Any other symbols don't qualify for sharing
255269
}
256270
}
257271
}
258272
}
259273

260274
// Sort so we get a stable incr. comp. hash.
261-
symbols.sort_unstable_by(|&(ref symbol1, ..), &(ref symbol2, ..)| {
262-
symbol1.compare_stable(tcx, symbol2)
263-
});
275+
symbols.sort_by_cached_key(|s| s.0.symbol_name_for_local_instance(tcx));
264276

265277
Arc::new(symbols)
266278
}

src/librustc_mir/monomorphize/collector.rs

+7-3
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

+1-1
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

+15-1
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

+9-3
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)