Skip to content

Commit e9acaef

Browse files
committed
Auto merge of #68414 - michaelwoerister:share-drop-glue, r=<try>
Also share drop-glue when compiling with -Zshare-generics (i.e. at opt-level=0) This PR adds drop-glue to the set of monomorphizations that can be shared across crates via `-Zshare-generics`. This version of the PR might have detrimental effects on performance as it makes lots of stuff dependent on a single query results (`upstream_monomorphizations_for(def_id_of_drop_in_place)`). That should be fixable but let's do a perf run first. Potentially fixes issue #64140. (cc @alexcrichton) The changes here are related to @matthewjasper's #67332 but should be mostly orthogonal. r? @ghost
2 parents ce361fb + 98034e9 commit e9acaef

File tree

10 files changed

+77
-65
lines changed

10 files changed

+77
-65
lines changed

src/librustc/middle/exported_symbols.rs

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use crate::ty::subst::SubstsRef;
33
use crate::ty::{self, TyCtxt};
44
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
55
use rustc_hir::def_id::{DefId, LOCAL_CRATE};
6-
use std::cmp;
76
use std::mem;
87

98
/// The SymbolExportLevel of a symbols specifies from which kinds of crates
@@ -43,43 +42,6 @@ impl<'tcx> ExportedSymbol<'tcx> {
4342
ExportedSymbol::NoDefId(symbol_name) => symbol_name,
4443
}
4544
}
46-
47-
pub fn compare_stable(&self, tcx: TyCtxt<'tcx>, other: &ExportedSymbol<'tcx>) -> cmp::Ordering {
48-
match *self {
49-
ExportedSymbol::NonGeneric(self_def_id) => match *other {
50-
ExportedSymbol::NonGeneric(other_def_id) => {
51-
tcx.def_path_hash(self_def_id).cmp(&tcx.def_path_hash(other_def_id))
52-
}
53-
ExportedSymbol::Generic(..) | ExportedSymbol::NoDefId(_) => cmp::Ordering::Less,
54-
},
55-
ExportedSymbol::Generic(self_def_id, self_substs) => match *other {
56-
ExportedSymbol::NonGeneric(_) => cmp::Ordering::Greater,
57-
ExportedSymbol::Generic(other_def_id, other_substs) => {
58-
// We compare the symbol names because they are cached as query
59-
// results which makes them relatively cheap to access repeatedly.
60-
//
61-
// It might be even faster to build a local cache of stable IDs
62-
// for sorting. Exported symbols are really only sorted once
63-
// in order to make the `exported_symbols` query result stable.
64-
let self_symbol_name =
65-
tcx.symbol_name(ty::Instance::new(self_def_id, self_substs));
66-
let other_symbol_name =
67-
tcx.symbol_name(ty::Instance::new(other_def_id, other_substs));
68-
69-
self_symbol_name.cmp(&other_symbol_name)
70-
}
71-
ExportedSymbol::NoDefId(_) => cmp::Ordering::Less,
72-
},
73-
ExportedSymbol::NoDefId(self_symbol_name) => match *other {
74-
ExportedSymbol::NonGeneric(_) | ExportedSymbol::Generic(..) => {
75-
cmp::Ordering::Greater
76-
}
77-
ExportedSymbol::NoDefId(ref other_symbol_name) => {
78-
self_symbol_name.cmp(other_symbol_name)
79-
}
80-
},
81-
}
82-
}
8345
}
8446

8547
pub fn metadata_symbol_name(tcx: TyCtxt<'_>) -> String {

src/librustc/mir/mono.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ impl<'tcx> MonoItem<'tcx> {
7979
}
8080

8181
pub fn instantiation_mode(&self, tcx: TyCtxt<'tcx>) -> InstantiationMode {
82-
let inline_in_all_cgus = tcx
82+
let generate_cgu_internal_copies = tcx
8383
.sess
8484
.opts
8585
.debugging_opts
@@ -93,7 +93,7 @@ impl<'tcx> MonoItem<'tcx> {
9393
// If this function isn't inlined or otherwise has explicit
9494
// linkage, then we'll be creating a globally shared version.
9595
if self.explicit_linkage(tcx).is_some()
96-
|| !instance.def.requires_local(tcx)
96+
|| !instance.def.generates_cgu_internal_copy(tcx)
9797
|| Some(instance.def_id()) == entry_def_id
9898
{
9999
return InstantiationMode::GloballyShared { may_conflict: false };
@@ -102,7 +102,7 @@ impl<'tcx> MonoItem<'tcx> {
102102
// At this point we don't have explicit linkage and we're an
103103
// inlined function. If we're inlining into all CGUs then we'll
104104
// be creating a local copy per CGU
105-
if inline_in_all_cgus {
105+
if generate_cgu_internal_copies {
106106
return InstantiationMode::LocalCopy;
107107
}
108108

src/librustc/ty/instance.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,12 @@ impl<'tcx> InstanceDef<'tcx> {
114114
tcx.get_attrs(self.def_id())
115115
}
116116

117-
pub fn is_inline(&self, tcx: TyCtxt<'tcx>) -> bool {
117+
/// Returns `true` if the LLVM version of this instance is unconditionally
118+
/// marked with `inline`. This implies that a copy of this instance is
119+
/// generated in every codegen unit.
120+
/// Note that this is only a hint. See the documentation for
121+
/// `generates_cgu_internal_copy` for more information.
122+
pub fn requires_inline(&self, tcx: TyCtxt<'tcx>) -> bool {
118123
use crate::hir::map::DefPathData;
119124
let def_id = match *self {
120125
ty::InstanceDef::Item(def_id) => def_id,
@@ -127,8 +132,15 @@ impl<'tcx> InstanceDef<'tcx> {
127132
}
128133
}
129134

130-
pub fn requires_local(&self, tcx: TyCtxt<'tcx>) -> bool {
131-
if self.is_inline(tcx) {
135+
/// Returns `true` if the machine code for this instance is instantiated in
136+
/// each codegen unit that references it.
137+
/// Note that this is only a hint! The compiler can globally decide to *not*
138+
/// do this in order to speed up compilation. CGU-internal copies are
139+
/// only exist to enable inlining. If inlining is not performed (e.g. at
140+
/// `-Copt-level=0`) then the time for generating them is wasted and it's
141+
/// better to create a single copy with external linkage.
142+
pub fn generates_cgu_internal_copy(&self, tcx: TyCtxt<'tcx>) -> bool {
143+
if self.requires_inline(tcx) {
132144
return true;
133145
}
134146
if let ty::InstanceDef::DropGlue(..) = *self {

src/librustc_codegen_llvm/attributes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ pub fn from_fn_attrs(
246246
}
247247

248248
// FIXME(eddyb) consolidate these two `inline` calls (and avoid overwrites).
249-
if instance.def.is_inline(cx.tcx) {
249+
if instance.def.requires_inline(cx.tcx) {
250250
inline(cx, llfn, attributes::InlineAttr::Hint);
251251
}
252252

src/librustc_codegen_ssa/back/symbol_export.rs

Lines changed: 21 additions & 11 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;
@@ -17,8 +17,6 @@ use rustc_hir::Node;
1717
use rustc_index::vec::IndexVec;
1818
use syntax::expand::allocator::ALLOCATOR_METHODS;
1919

20-
pub type ExportedSymbols = FxHashMap<CrateNum, Arc<Vec<(String, SymbolExportLevel)>>>;
21-
2220
pub fn threshold(tcx: TyCtxt<'_>) -> SymbolExportLevel {
2321
crates_export_threshold(&tcx.sess.crate_types.borrow())
2422
}
@@ -96,7 +94,7 @@ fn reachable_non_generics_provider(
9694
if !generics.requires_monomorphization(tcx) &&
9795
// Functions marked with #[inline] are only ever codegened
9896
// with "internal" linkage and are never exported.
99-
!Instance::mono(tcx, def_id).def.requires_local(tcx)
97+
!Instance::mono(tcx, def_id).def.generates_cgu_internal_copy(tcx)
10098
{
10199
Some(def_id)
102100
} else {
@@ -240,19 +238,31 @@ fn exported_symbols_provider_local(
240238
continue;
241239
}
242240

243-
if let &MonoItem::Fn(Instance { def: InstanceDef::Item(def_id), substs }) = mono_item {
244-
if substs.non_erasable_generics().next().is_some() {
245-
symbols
246-
.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
247259
}
248260
}
249261
}
250262
}
251263

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

257267
Arc::new(symbols)
258268
}

src/librustc_codegen_ssa/back/write.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ use super::command::Command;
22
use super::link::{self, get_linker, remove};
33
use super::linker::LinkerInfo;
44
use super::lto::{self, SerializedModule};
5-
use super::symbol_export::{symbol_name_for_instance_in_crate, ExportedSymbols};
5+
use super::symbol_export::symbol_name_for_instance_in_crate;
6+
67
use crate::{
78
CachedModuleCodegen, CodegenResults, CompiledModule, CrateInfo, ModuleCodegen, ModuleKind,
89
RLIB_BYTECODE_EXTENSION,
@@ -12,6 +13,7 @@ use crate::traits::*;
1213
use jobserver::{Acquired, Client};
1314
use rustc::dep_graph::{WorkProduct, WorkProductFileKind, WorkProductId};
1415
use rustc::middle::cstore::EncodedMetadata;
16+
use rustc::middle::exported_symbols::SymbolExportLevel;
1517
use rustc::session::config::{
1618
self, Lto, OutputFilenames, OutputType, Passes, Sanitizer, SwitchWithOptPath,
1719
};
@@ -205,6 +207,8 @@ impl<B: WriteBackendMethods> Clone for TargetMachineFactory<B> {
205207
}
206208
}
207209

210+
pub type ExportedSymbols = FxHashMap<CrateNum, Arc<Vec<(String, SymbolExportLevel)>>>;
211+
208212
/// Additional resources used by optimize_and_codegen (not module specific)
209213
#[derive(Clone)]
210214
pub struct CodegenContext<B: WriteBackendMethods> {

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)