1
Fork 0

Auto merge of #68414 - michaelwoerister:share-drop-glue, r=alexcrichton

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 https://github.com/rust-lang/rust/issues/64140. (cc @alexcrichton)
The changes here are related to @matthewjasper's https://github.com/rust-lang/rust/pull/67332 but should be mostly orthogonal.

r? @ghost
This commit is contained in:
bors 2020-01-24 14:00:56 +00:00
commit 73f76b74ef
14 changed files with 212 additions and 160 deletions

View file

@ -1,10 +1,7 @@
use crate::ich::StableHashingContext;
use crate::ty::subst::SubstsRef; use crate::ty::subst::SubstsRef;
use crate::ty::{self, TyCtxt}; use crate::ty::{self, Ty, TyCtxt};
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
use rustc_hir::def_id::{DefId, LOCAL_CRATE}; use rustc_hir::def_id::{DefId, LOCAL_CRATE};
use std::cmp; use rustc_macros::HashStable;
use std::mem;
/// The SymbolExportLevel of a symbols specifies from which kinds of crates /// The SymbolExportLevel of a symbols specifies from which kinds of crates
/// the symbol will be exported. `C` symbols will be exported from any /// the symbol will be exported. `C` symbols will be exported from any
@ -24,10 +21,11 @@ impl SymbolExportLevel {
} }
} }
#[derive(Eq, PartialEq, Debug, Copy, Clone, RustcEncodable, RustcDecodable)] #[derive(Eq, PartialEq, Debug, Copy, Clone, RustcEncodable, RustcDecodable, HashStable)]
pub enum ExportedSymbol<'tcx> { pub enum ExportedSymbol<'tcx> {
NonGeneric(DefId), NonGeneric(DefId),
Generic(DefId, SubstsRef<'tcx>), Generic(DefId, SubstsRef<'tcx>),
DropGlue(Ty<'tcx>),
NoDefId(ty::SymbolName), NoDefId(ty::SymbolName),
} }
@ -40,46 +38,12 @@ impl<'tcx> ExportedSymbol<'tcx> {
ExportedSymbol::Generic(def_id, substs) => { ExportedSymbol::Generic(def_id, substs) => {
tcx.symbol_name(ty::Instance::new(def_id, substs)) tcx.symbol_name(ty::Instance::new(def_id, substs))
} }
ExportedSymbol::DropGlue(ty) => {
tcx.symbol_name(ty::Instance::resolve_drop_in_place(tcx, ty))
}
ExportedSymbol::NoDefId(symbol_name) => symbol_name, ExportedSymbol::NoDefId(symbol_name) => symbol_name,
} }
} }
pub fn compare_stable(&self, tcx: TyCtxt<'tcx>, other: &ExportedSymbol<'tcx>) -> cmp::Ordering {
match *self {
ExportedSymbol::NonGeneric(self_def_id) => match *other {
ExportedSymbol::NonGeneric(other_def_id) => {
tcx.def_path_hash(self_def_id).cmp(&tcx.def_path_hash(other_def_id))
}
ExportedSymbol::Generic(..) | ExportedSymbol::NoDefId(_) => cmp::Ordering::Less,
},
ExportedSymbol::Generic(self_def_id, self_substs) => match *other {
ExportedSymbol::NonGeneric(_) => cmp::Ordering::Greater,
ExportedSymbol::Generic(other_def_id, other_substs) => {
// We compare the symbol names because they are cached as query
// results which makes them relatively cheap to access repeatedly.
//
// It might be even faster to build a local cache of stable IDs
// for sorting. Exported symbols are really only sorted once
// in order to make the `exported_symbols` query result stable.
let self_symbol_name =
tcx.symbol_name(ty::Instance::new(self_def_id, self_substs));
let other_symbol_name =
tcx.symbol_name(ty::Instance::new(other_def_id, other_substs));
self_symbol_name.cmp(&other_symbol_name)
}
ExportedSymbol::NoDefId(_) => cmp::Ordering::Less,
},
ExportedSymbol::NoDefId(self_symbol_name) => match *other {
ExportedSymbol::NonGeneric(_) | ExportedSymbol::Generic(..) => {
cmp::Ordering::Greater
}
ExportedSymbol::NoDefId(ref other_symbol_name) => {
self_symbol_name.cmp(other_symbol_name)
}
},
}
}
} }
pub fn metadata_symbol_name(tcx: TyCtxt<'_>) -> String { pub fn metadata_symbol_name(tcx: TyCtxt<'_>) -> String {
@ -89,21 +53,3 @@ pub fn metadata_symbol_name(tcx: TyCtxt<'_>) -> String {
tcx.crate_disambiguator(LOCAL_CRATE).to_fingerprint().to_hex() tcx.crate_disambiguator(LOCAL_CRATE).to_fingerprint().to_hex()
) )
} }
impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for ExportedSymbol<'tcx> {
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
mem::discriminant(self).hash_stable(hcx, hasher);
match *self {
ExportedSymbol::NonGeneric(def_id) => {
def_id.hash_stable(hcx, hasher);
}
ExportedSymbol::Generic(def_id, substs) => {
def_id.hash_stable(hcx, hasher);
substs.hash_stable(hcx, hasher);
}
ExportedSymbol::NoDefId(symbol_name) => {
symbol_name.hash_stable(hcx, hasher);
}
}
}
}

View file

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

View file

@ -776,13 +776,47 @@ rustc_queries! {
} }
Codegen { Codegen {
/// The entire set of monomorphizations the local crate can safely link
/// to because they are exported from upstream crates. Do not depend on
/// this directly, as its value changes anytime a monomorphization gets
/// added or removed in any upstream crate. Instead use the narrower
/// `upstream_monomorphizations_for`, `upstream_drop_glue_for`, or, even
/// better, `Instance::upstream_monomorphization()`.
query upstream_monomorphizations( query upstream_monomorphizations(
k: CrateNum k: CrateNum
) -> &'tcx DefIdMap<FxHashMap<SubstsRef<'tcx>, CrateNum>> { ) -> &'tcx DefIdMap<FxHashMap<SubstsRef<'tcx>, CrateNum>> {
desc { "collecting available upstream monomorphizations `{:?}`", k } desc { "collecting available upstream monomorphizations `{:?}`", k }
} }
/// Returns the set of upstream monomorphizations available for the
/// generic function identified by the given `def_id`. The query makes
/// sure to make a stable selection if the same monomorphization is
/// available in multiple upstream crates.
///
/// You likely want to call `Instance::upstream_monomorphization()`
/// instead of invoking this query directly.
query upstream_monomorphizations_for(_: DefId) query upstream_monomorphizations_for(_: DefId)
-> Option<&'tcx FxHashMap<SubstsRef<'tcx>, CrateNum>> {} -> Option<&'tcx FxHashMap<SubstsRef<'tcx>, CrateNum>> {}
/// Returns the upstream crate that exports drop-glue for the given
/// type (`substs` is expected to be a single-item list containing the
/// type one wants drop-glue for).
///
/// This is a subset of `upstream_monomorphizations_for` in order to
/// increase dep-tracking granularity. Otherwise adding or removing any
/// type with drop-glue in any upstream crate would invalidate all
/// functions calling drop-glue of an upstream type.
///
/// You likely want to call `Instance::upstream_monomorphization()`
/// instead of invoking this query directly.
///
/// NOTE: This query could easily be extended to also support other
/// common functions that have are large set of monomorphizations
/// (like `Clone::clone` for example).
query upstream_drop_glue_for(substs: SubstsRef<'tcx>) -> Option<CrateNum> {
desc { "available upstream drop-glue for `{:?}`", substs }
no_force
}
} }
Other { Other {

View file

@ -4,7 +4,7 @@ use crate::traits;
use crate::ty::print::{FmtPrinter, Printer}; use crate::ty::print::{FmtPrinter, Printer};
use crate::ty::{self, SubstsRef, Ty, TyCtxt, TypeFoldable}; use crate::ty::{self, SubstsRef, Ty, TyCtxt, TypeFoldable};
use rustc_hir::def::Namespace; use rustc_hir::def::Namespace;
use rustc_hir::def_id::DefId; use rustc_hir::def_id::{CrateNum, DefId};
use rustc_macros::HashStable; use rustc_macros::HashStable;
use rustc_target::spec::abi::Abi; use rustc_target::spec::abi::Abi;
@ -91,6 +91,40 @@ impl<'tcx> Instance<'tcx> {
let ty = tcx.type_of(self.def.def_id()); let ty = tcx.type_of(self.def.def_id());
tcx.subst_and_normalize_erasing_regions(self.substs, param_env, &ty) tcx.subst_and_normalize_erasing_regions(self.substs, param_env, &ty)
} }
/// Finds a crate that contains a monomorphization of this instance that
/// can be linked to from the local crate. A return value of `None` means
/// no upstream crate provides such an exported monomorphization.
///
/// This method already takes into account the global `-Zshare-generics`
/// setting, always returning `None` if `share-generics` is off.
pub fn upstream_monomorphization(&self, tcx: TyCtxt<'tcx>) -> Option<CrateNum> {
// If we are not in share generics mode, we don't link to upstream
// monomorphizations but always instantiate our own internal versions
// instead.
if !tcx.sess.opts.share_generics() {
return None;
}
// If this is an item that is defined in the local crate, no upstream
// crate can know about it/provide a monomorphization.
if self.def_id().is_local() {
return None;
}
// If this a non-generic instance, it cannot be a shared monomorphization.
if self.substs.non_erasable_generics().next().is_none() {
return None;
}
match self.def {
InstanceDef::Item(def_id) => tcx
.upstream_monomorphizations_for(def_id)
.and_then(|monos| monos.get(&self.substs).cloned()),
InstanceDef::DropGlue(_, Some(_)) => tcx.upstream_drop_glue_for(self.substs),
_ => None,
}
}
} }
impl<'tcx> InstanceDef<'tcx> { impl<'tcx> InstanceDef<'tcx> {
@ -114,7 +148,12 @@ impl<'tcx> InstanceDef<'tcx> {
tcx.get_attrs(self.def_id()) tcx.get_attrs(self.def_id())
} }
pub fn is_inline(&self, tcx: TyCtxt<'tcx>) -> bool { /// Returns `true` if the LLVM version of this instance is unconditionally
/// marked with `inline`. This implies that a copy of this instance is
/// generated in every codegen unit.
/// Note that this is only a hint. See the documentation for
/// `generates_cgu_internal_copy` for more information.
pub fn requires_inline(&self, tcx: TyCtxt<'tcx>) -> bool {
use crate::hir::map::DefPathData; use crate::hir::map::DefPathData;
let def_id = match *self { let def_id = match *self {
ty::InstanceDef::Item(def_id) => def_id, ty::InstanceDef::Item(def_id) => def_id,
@ -127,8 +166,15 @@ impl<'tcx> InstanceDef<'tcx> {
} }
} }
pub fn requires_local(&self, tcx: TyCtxt<'tcx>) -> bool { /// Returns `true` if the machine code for this instance is instantiated in
if self.is_inline(tcx) { /// each codegen unit that references it.
/// Note that this is only a hint! The compiler can globally decide to *not*
/// do this in order to speed up compilation. CGU-internal copies are
/// only exist to enable inlining. If inlining is not performed (e.g. at
/// `-Copt-level=0`) then the time for generating them is wasted and it's
/// better to create a single copy with external linkage.
pub fn generates_cgu_internal_copy(&self, tcx: TyCtxt<'tcx>) -> bool {
if self.requires_inline(tcx) {
return true; return true;
} }
if let ty::InstanceDef::DropGlue(..) = *self { if let ty::InstanceDef::DropGlue(..) = *self {

View file

@ -116,6 +116,15 @@ impl Key for (DefId, SimplifiedType) {
} }
} }
impl<'tcx> Key for SubstsRef<'tcx> {
fn query_crate(&self) -> CrateNum {
LOCAL_CRATE
}
fn default_span(&self, _: TyCtxt<'_>) -> Span {
DUMMY_SP
}
}
impl<'tcx> Key for (DefId, SubstsRef<'tcx>) { impl<'tcx> Key for (DefId, SubstsRef<'tcx>) {
fn query_crate(&self) -> CrateNum { fn query_crate(&self) -> CrateNum {
self.0.krate self.0.krate

View file

@ -246,7 +246,7 @@ pub fn from_fn_attrs(
} }
// FIXME(eddyb) consolidate these two `inline` calls (and avoid overwrites). // FIXME(eddyb) consolidate these two `inline` calls (and avoid overwrites).
if instance.def.is_inline(cx.tcx) { if instance.def.requires_inline(cx.tcx) {
inline(cx, llfn, attributes::InlineAttr::Hint); inline(cx, llfn, attributes::InlineAttr::Hint);
} }

View file

@ -130,12 +130,7 @@ pub fn get_fn(cx: &CodegenCx<'ll, 'tcx>, instance: Instance<'tcx>) -> &'ll Value
} else { } else {
// This is a monomorphization of a generic function // This is a monomorphization of a generic function
// defined in an upstream crate. // defined in an upstream crate.
if cx if instance.upstream_monomorphization(tcx).is_some() {
.tcx
.upstream_monomorphizations_for(instance_def_id)
.map(|set| set.contains_key(instance.substs))
.unwrap_or(false)
{
// This is instantiated in another crate. It cannot // This is instantiated in another crate. It cannot
// be `hidden`. // be `hidden`.
} else { } else {

View file

@ -5,7 +5,7 @@ use rustc::middle::codegen_fn_attrs::CodegenFnAttrFlags;
use rustc::middle::exported_symbols::{metadata_symbol_name, ExportedSymbol, SymbolExportLevel}; use rustc::middle::exported_symbols::{metadata_symbol_name, ExportedSymbol, SymbolExportLevel};
use rustc::session::config::{self, Sanitizer}; use rustc::session::config::{self, Sanitizer};
use rustc::ty::query::Providers; use rustc::ty::query::Providers;
use rustc::ty::subst::SubstsRef; use rustc::ty::subst::{GenericArgKind, SubstsRef};
use rustc::ty::Instance; use rustc::ty::Instance;
use rustc::ty::{SymbolName, TyCtxt}; use rustc::ty::{SymbolName, TyCtxt};
use rustc_codegen_utils::symbol_names; use rustc_codegen_utils::symbol_names;
@ -17,8 +17,6 @@ use rustc_hir::Node;
use rustc_index::vec::IndexVec; use rustc_index::vec::IndexVec;
use syntax::expand::allocator::ALLOCATOR_METHODS; use syntax::expand::allocator::ALLOCATOR_METHODS;
pub type ExportedSymbols = FxHashMap<CrateNum, Arc<Vec<(String, SymbolExportLevel)>>>;
pub fn threshold(tcx: TyCtxt<'_>) -> SymbolExportLevel { pub fn threshold(tcx: TyCtxt<'_>) -> SymbolExportLevel {
crates_export_threshold(&tcx.sess.crate_types.borrow()) crates_export_threshold(&tcx.sess.crate_types.borrow())
} }
@ -96,7 +94,7 @@ fn reachable_non_generics_provider(
if !generics.requires_monomorphization(tcx) && if !generics.requires_monomorphization(tcx) &&
// Functions marked with #[inline] are only ever codegened // Functions marked with #[inline] are only ever codegened
// with "internal" linkage and are never exported. // with "internal" linkage and are never exported.
!Instance::mono(tcx, def_id).def.requires_local(tcx) !Instance::mono(tcx, def_id).def.generates_cgu_internal_copy(tcx)
{ {
Some(def_id) Some(def_id)
} else { } else {
@ -250,19 +248,30 @@ fn exported_symbols_provider_local(
continue; continue;
} }
if let &MonoItem::Fn(Instance { def: InstanceDef::Item(def_id), substs }) = mono_item { match *mono_item {
if substs.non_erasable_generics().next().is_some() { MonoItem::Fn(Instance { def: InstanceDef::Item(def_id), substs }) => {
symbols if substs.non_erasable_generics().next().is_some() {
.push((ExportedSymbol::Generic(def_id, substs), SymbolExportLevel::Rust)); let symbol = ExportedSymbol::Generic(def_id, substs);
symbols.push((symbol, SymbolExportLevel::Rust));
}
}
MonoItem::Fn(Instance { def: InstanceDef::DropGlue(_, Some(ty)), substs }) => {
// A little sanity-check
debug_assert_eq!(
substs.non_erasable_generics().next(),
Some(GenericArgKind::Type(ty))
);
symbols.push((ExportedSymbol::DropGlue(ty), SymbolExportLevel::Rust));
}
_ => {
// Any other symbols don't qualify for sharing
} }
} }
} }
} }
// Sort so we get a stable incr. comp. hash. // Sort so we get a stable incr. comp. hash.
symbols.sort_unstable_by(|&(ref symbol1, ..), &(ref symbol2, ..)| { symbols.sort_by_cached_key(|s| s.0.symbol_name_for_local_instance(tcx));
symbol1.compare_stable(tcx, symbol2)
});
Arc::new(symbols) Arc::new(symbols)
} }
@ -288,24 +297,41 @@ fn upstream_monomorphizations_provider(
cnum_stable_ids cnum_stable_ids
}; };
let drop_in_place_fn_def_id = tcx.lang_items().drop_in_place_fn();
for &cnum in cnums.iter() { for &cnum in cnums.iter() {
for (exported_symbol, _) in tcx.exported_symbols(cnum).iter() { for (exported_symbol, _) in tcx.exported_symbols(cnum).iter() {
if let &ExportedSymbol::Generic(def_id, substs) = exported_symbol { let (def_id, substs) = match *exported_symbol {
let substs_map = instances.entry(def_id).or_default(); ExportedSymbol::Generic(def_id, substs) => (def_id, substs),
ExportedSymbol::DropGlue(ty) => {
match substs_map.entry(substs) { if let Some(drop_in_place_fn_def_id) = drop_in_place_fn_def_id {
Occupied(mut e) => { (drop_in_place_fn_def_id, tcx.intern_substs(&[ty.into()]))
// If there are multiple monomorphizations available, } else {
// we select one deterministically. // `drop_in_place` in place does not exist, don't try
let other_cnum = *e.get(); // to use it.
if cnum_stable_ids[other_cnum] > cnum_stable_ids[cnum] { continue;
e.insert(cnum);
}
} }
Vacant(e) => { }
ExportedSymbol::NonGeneric(..) | ExportedSymbol::NoDefId(..) => {
// These are no monomorphizations
continue;
}
};
let substs_map = instances.entry(def_id).or_default();
match substs_map.entry(substs) {
Occupied(mut e) => {
// If there are multiple monomorphizations available,
// we select one deterministically.
let other_cnum = *e.get();
if cnum_stable_ids[other_cnum] > cnum_stable_ids[cnum] {
e.insert(cnum); e.insert(cnum);
} }
} }
Vacant(e) => {
e.insert(cnum);
}
} }
} }
} }
@ -321,6 +347,17 @@ fn upstream_monomorphizations_for_provider(
tcx.upstream_monomorphizations(LOCAL_CRATE).get(&def_id) tcx.upstream_monomorphizations(LOCAL_CRATE).get(&def_id)
} }
fn upstream_drop_glue_for_provider<'tcx>(
tcx: TyCtxt<'tcx>,
substs: SubstsRef<'tcx>,
) -> Option<CrateNum> {
if let Some(def_id) = tcx.lang_items().drop_in_place_fn() {
tcx.upstream_monomorphizations_for(def_id).and_then(|monos| monos.get(&substs).cloned())
} else {
None
}
}
fn is_unreachable_local_definition_provider(tcx: TyCtxt<'_>, def_id: DefId) -> bool { fn is_unreachable_local_definition_provider(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
if let Some(hir_id) = tcx.hir().as_local_hir_id(def_id) { if let Some(hir_id) = tcx.hir().as_local_hir_id(def_id) {
!tcx.reachable_set(LOCAL_CRATE).contains(&hir_id) !tcx.reachable_set(LOCAL_CRATE).contains(&hir_id)
@ -335,6 +372,7 @@ pub fn provide(providers: &mut Providers<'_>) {
providers.exported_symbols = exported_symbols_provider_local; providers.exported_symbols = exported_symbols_provider_local;
providers.upstream_monomorphizations = upstream_monomorphizations_provider; providers.upstream_monomorphizations = upstream_monomorphizations_provider;
providers.is_unreachable_local_definition = is_unreachable_local_definition_provider; providers.is_unreachable_local_definition = is_unreachable_local_definition_provider;
providers.upstream_drop_glue_for = upstream_drop_glue_for_provider;
} }
pub fn provide_extern(providers: &mut Providers<'_>) { pub fn provide_extern(providers: &mut Providers<'_>) {
@ -395,6 +433,11 @@ pub fn symbol_name_for_instance_in_crate<'tcx>(
Instance::new(def_id, substs), Instance::new(def_id, substs),
instantiating_crate, instantiating_crate,
), ),
ExportedSymbol::DropGlue(ty) => symbol_names::symbol_name_for_instance_in_crate(
tcx,
Instance::resolve_drop_in_place(tcx, ty),
instantiating_crate,
),
ExportedSymbol::NoDefId(symbol_name) => symbol_name.to_string(), ExportedSymbol::NoDefId(symbol_name) => symbol_name.to_string(),
} }
} }

View file

@ -2,7 +2,8 @@ use super::command::Command;
use super::link::{self, get_linker, remove}; use super::link::{self, get_linker, remove};
use super::linker::LinkerInfo; use super::linker::LinkerInfo;
use super::lto::{self, SerializedModule}; use super::lto::{self, SerializedModule};
use super::symbol_export::{symbol_name_for_instance_in_crate, ExportedSymbols}; use super::symbol_export::symbol_name_for_instance_in_crate;
use crate::{ use crate::{
CachedModuleCodegen, CodegenResults, CompiledModule, CrateInfo, ModuleCodegen, ModuleKind, CachedModuleCodegen, CodegenResults, CompiledModule, CrateInfo, ModuleCodegen, ModuleKind,
RLIB_BYTECODE_EXTENSION, RLIB_BYTECODE_EXTENSION,
@ -12,6 +13,7 @@ use crate::traits::*;
use jobserver::{Acquired, Client}; use jobserver::{Acquired, Client};
use rustc::dep_graph::{WorkProduct, WorkProductFileKind, WorkProductId}; use rustc::dep_graph::{WorkProduct, WorkProductFileKind, WorkProductId};
use rustc::middle::cstore::EncodedMetadata; use rustc::middle::cstore::EncodedMetadata;
use rustc::middle::exported_symbols::SymbolExportLevel;
use rustc::session::config::{ use rustc::session::config::{
self, Lto, OutputFilenames, OutputType, Passes, Sanitizer, SwitchWithOptPath, self, Lto, OutputFilenames, OutputType, Passes, Sanitizer, SwitchWithOptPath,
}; };
@ -205,6 +207,8 @@ impl<B: WriteBackendMethods> Clone for TargetMachineFactory<B> {
} }
} }
pub type ExportedSymbols = FxHashMap<CrateNum, Arc<Vec<(String, SymbolExportLevel)>>>;
/// Additional resources used by optimize_and_codegen (not module specific) /// Additional resources used by optimize_and_codegen (not module specific)
#[derive(Clone)] #[derive(Clone)]
pub struct CodegenContext<B: WriteBackendMethods> { pub struct CodegenContext<B: WriteBackendMethods> {

View file

@ -126,29 +126,10 @@ fn symbol_name_provider(tcx: TyCtxt<'tcx>, instance: Instance<'tcx>) -> ty::Symb
// This closure determines the instantiating crate for instances that // This closure determines the instantiating crate for instances that
// need an instantiating-crate-suffix for their symbol name, in order // need an instantiating-crate-suffix for their symbol name, in order
// to differentiate between local copies. // to differentiate between local copies.
//
// For generics we might find re-usable upstream instances. For anything
// else we rely on their being a local copy available.
if is_generic(instance.substs) { if is_generic(instance.substs) {
let def_id = instance.def_id(); // For generics we might find re-usable upstream instances. If there
// is one, we rely on the symbol being instantiated locally.
if !def_id.is_local() && tcx.sess.opts.share_generics() { instance.upstream_monomorphization(tcx).unwrap_or(LOCAL_CRATE)
// If we are re-using a monomorphization from another crate,
// we have to compute the symbol hash accordingly.
let upstream_monomorphizations = tcx.upstream_monomorphizations_for(def_id);
upstream_monomorphizations
.and_then(|monos| monos.get(&instance.substs).cloned())
// If there is no instance available upstream, there'll be
// one in the current crate.
.unwrap_or(LOCAL_CRATE)
} else {
// For generic functions defined in the current crate, there
// can be no upstream instances. Also, if we don't share
// generics, we'll instantiate a local copy too.
LOCAL_CRATE
}
} else { } else {
// For non-generic things that need to avoid naming conflicts, we // For non-generic things that need to avoid naming conflicts, we
// always instantiate a copy in the local crate. // always instantiate a copy in the local crate.

View file

@ -713,7 +713,8 @@ fn visit_instance_use<'tcx>(
// need a mono item. // need a mono item.
fn should_monomorphize_locally<'tcx>(tcx: TyCtxt<'tcx>, instance: &Instance<'tcx>) -> bool { fn should_monomorphize_locally<'tcx>(tcx: TyCtxt<'tcx>, instance: &Instance<'tcx>) -> bool {
let def_id = match instance.def { let def_id = match instance.def {
ty::InstanceDef::Item(def_id) => def_id, ty::InstanceDef::Item(def_id) | ty::InstanceDef::DropGlue(def_id, Some(_)) => def_id,
ty::InstanceDef::VtableShim(..) ty::InstanceDef::VtableShim(..)
| ty::InstanceDef::ReifyShim(..) | ty::InstanceDef::ReifyShim(..)
| ty::InstanceDef::ClosureOnceShim { .. } | ty::InstanceDef::ClosureOnceShim { .. }
@ -725,18 +726,18 @@ fn should_monomorphize_locally<'tcx>(tcx: TyCtxt<'tcx>, instance: &Instance<'tcx
}; };
if tcx.is_foreign_item(def_id) { if tcx.is_foreign_item(def_id) {
// We can always link to foreign items. // Foreign items are always linked against, there's no way of
// instantiating them.
return false; return false;
} }
if def_id.is_local() { if def_id.is_local() {
// Local items cannot be referred to locally without monomorphizing them locally. // Local items cannot be referred to locally without
// monomorphizing them locally.
return true; return true;
} }
if tcx.is_reachable_non_generic(def_id) if tcx.is_reachable_non_generic(def_id) || instance.upstream_monomorphization(tcx).is_some() {
|| is_available_upstream_generic(tcx, def_id, instance.substs)
{
// We can link to the item in question, no instance needed // We can link to the item in question, no instance needed
// in this crate. // in this crate.
return false; return false;
@ -745,35 +746,8 @@ fn should_monomorphize_locally<'tcx>(tcx: TyCtxt<'tcx>, instance: &Instance<'tcx
if !tcx.is_mir_available(def_id) { if !tcx.is_mir_available(def_id) {
bug!("cannot create local mono-item for {:?}", def_id) bug!("cannot create local mono-item for {:?}", def_id)
} }
return true; return true;
fn is_available_upstream_generic<'tcx>(
tcx: TyCtxt<'tcx>,
def_id: DefId,
substs: SubstsRef<'tcx>,
) -> bool {
debug_assert!(!def_id.is_local());
// If we are not in share generics mode, we don't link to upstream
// monomorphizations but always instantiate our own internal versions
// instead.
if !tcx.sess.opts.share_generics() {
return false;
}
// If this instance has non-erasable parameters, it cannot be a shared
// monomorphization. Non-generic instances are already handled above
// by `is_reachable_non_generic()`.
if substs.non_erasable_generics().next().is_none() {
return false;
}
// Take a look at the available monomorphizations listed in the metadata
// of upstream crates.
tcx.upstream_monomorphizations_for(def_id)
.map(|set| set.contains_key(substs))
.unwrap_or(false)
}
} }
/// For a given pair of source and target type that occur in an unsizing coercion, /// For a given pair of source and target type that occur in an unsizing coercion,

View file

@ -324,7 +324,7 @@ fn mono_item_visibility(
}; };
let def_id = match instance.def { let def_id = match instance.def {
InstanceDef::Item(def_id) => def_id, InstanceDef::Item(def_id) | InstanceDef::DropGlue(def_id, Some(_)) => def_id,
// These are all compiler glue and such, never exported, always hidden. // These are all compiler glue and such, never exported, always hidden.
InstanceDef::VtableShim(..) InstanceDef::VtableShim(..)

View file

@ -1,4 +1,6 @@
// compile-flags:-Zshare-generics=yes // NOTE: We always compile this test with -Copt-level=0 because higher opt-levels
// prevent drop-glue from participating in share-generics.
// compile-flags:-Zshare-generics=yes -Copt-level=0
// no-prefer-dynamic // no-prefer-dynamic
#![crate_type="rlib"] #![crate_type="rlib"]
@ -8,5 +10,17 @@ pub fn generic_fn<T>(x: T, y: T) -> (T, T) {
} }
pub fn use_generic_fn_f32() -> (f32, f32) { pub fn use_generic_fn_f32() -> (f32, f32) {
// This line causes drop glue for Foo to be instantiated. We want to make
// sure that this crate exports an instance to be re-used by share-generics.
let _ = Foo(0);
generic_fn(0.0f32, 1.0f32) generic_fn(0.0f32, 1.0f32)
} }
pub struct Foo(pub u32);
impl Drop for Foo {
fn drop(&mut self) {
println!("foo");
}
}

View file

@ -1,6 +1,8 @@
// ignore-tidy-linelength // ignore-tidy-linelength
// no-prefer-dynamic // no-prefer-dynamic
// compile-flags:-Zprint-mono-items=eager -Zshare-generics=yes -Zincremental=tmp/partitioning-tests/shared-generics-exe // NOTE: We always compile this test with -Copt-level=0 because higher opt-levels
// prevent drop-glue from participating in share-generics.
// compile-flags:-Zprint-mono-items=eager -Zshare-generics=yes -Zincremental=tmp/partitioning-tests/shared-generics-exe -Copt-level=0
#![crate_type="rlib"] #![crate_type="rlib"]
@ -16,6 +18,10 @@ pub fn foo() {
// This should not generate a monomorphization because it's already // This should not generate a monomorphization because it's already
// available in `shared_generics_aux`. // available in `shared_generics_aux`.
let _ = shared_generics_aux::generic_fn(0.0f32, 3.0f32); let _ = shared_generics_aux::generic_fn(0.0f32, 3.0f32);
}
// MONO_ITEM drop-glue i8 // The following line will drop an instance of `Foo`, generating a call to
// Foo's drop-glue function. However, share-generics should take care of
// reusing the drop-glue from the upstream crate, so we do not expect a
// mono item for the drop-glue
let _ = shared_generics_aux::Foo(1);
}