Add SymbolExportInfo

This is currently a wrapper to `SymbolExportLevel` but it allows
later addition of extra information.
This commit is contained in:
Gary Guo 2022-04-02 22:27:33 +01:00
parent 419e3ba97b
commit 49cc6d1f84
12 changed files with 75 additions and 38 deletions

View File

@ -16,7 +16,7 @@ use rustc_errors::{FatalError, Handler};
use rustc_hir::def_id::LOCAL_CRATE;
use rustc_middle::bug;
use rustc_middle::dep_graph::WorkProduct;
use rustc_middle::middle::exported_symbols::SymbolExportLevel;
use rustc_middle::middle::exported_symbols::{SymbolExportInfo, SymbolExportLevel};
use rustc_session::cgu_reuse_tracker::CguReuse;
use rustc_session::config::{self, CrateType, Lto};
use tracing::{debug, info};
@ -55,8 +55,8 @@ fn prepare_lto(
Lto::No => panic!("didn't request LTO but we're doing LTO"),
};
let symbol_filter = &|&(ref name, level): &(String, SymbolExportLevel)| {
if level.is_below_threshold(export_threshold) {
let symbol_filter = &|&(ref name, info): &(String, SymbolExportInfo)| {
if info.level.is_below_threshold(export_threshold) {
Some(CString::new(name.as_str()).unwrap())
} else {
None

View File

@ -1526,8 +1526,8 @@ pub(crate) fn exported_symbols(tcx: TyCtxt<'_>, crate_type: CrateType) -> Vec<St
let mut symbols = Vec::new();
let export_threshold = symbol_export::crates_export_threshold(&[crate_type]);
for &(symbol, level) in tcx.exported_symbols(LOCAL_CRATE).iter() {
if level.is_below_threshold(export_threshold) {
for &(symbol, info) in tcx.exported_symbols(LOCAL_CRATE).iter() {
if info.level.is_below_threshold(export_threshold) {
symbols.push(symbol_export::symbol_name_for_instance_in_crate(
tcx,
symbol,
@ -1544,8 +1544,8 @@ pub(crate) fn exported_symbols(tcx: TyCtxt<'_>, crate_type: CrateType) -> Vec<St
// For each dependency that we are linking to statically ...
if *dep_format == Linkage::Static {
// ... we add its symbol list to our export list.
for &(symbol, level) in tcx.exported_symbols(cnum).iter() {
if !level.is_below_threshold(export_threshold) {
for &(symbol, info) in tcx.exported_symbols(cnum).iter() {
if !info.level.is_below_threshold(export_threshold) {
continue;
}

View File

@ -9,7 +9,7 @@ use rustc_hir::Node;
use rustc_index::vec::IndexVec;
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
use rustc_middle::middle::exported_symbols::{
metadata_symbol_name, ExportedSymbol, SymbolExportLevel,
metadata_symbol_name, ExportedSymbol, SymbolExportInfo, SymbolExportLevel,
};
use rustc_middle::ty::query::{ExternProviders, Providers};
use rustc_middle::ty::subst::{GenericArgKind, SubstsRef};
@ -42,7 +42,7 @@ pub fn crates_export_threshold(crate_types: &[CrateType]) -> SymbolExportLevel {
}
}
fn reachable_non_generics_provider(tcx: TyCtxt<'_>, cnum: CrateNum) -> DefIdMap<SymbolExportLevel> {
fn reachable_non_generics_provider(tcx: TyCtxt<'_>, cnum: CrateNum) -> DefIdMap<SymbolExportInfo> {
assert_eq!(cnum, LOCAL_CRATE);
if !tcx.sess.opts.output_types.should_codegen() {
@ -129,12 +129,17 @@ fn reachable_non_generics_provider(tcx: TyCtxt<'_>, cnum: CrateNum) -> DefIdMap<
tcx.symbol_name(Instance::mono(tcx, def_id.to_def_id())),
export_level
);
(def_id.to_def_id(), export_level)
(def_id.to_def_id(), SymbolExportInfo {
level: export_level,
})
})
.collect();
if let Some(id) = tcx.proc_macro_decls_static(()) {
reachable_non_generics.insert(id.to_def_id(), SymbolExportLevel::C);
reachable_non_generics.insert(
id.to_def_id(),
SymbolExportInfo { level: SymbolExportLevel::C },
);
}
reachable_non_generics
@ -143,8 +148,8 @@ fn reachable_non_generics_provider(tcx: TyCtxt<'_>, cnum: CrateNum) -> DefIdMap<
fn is_reachable_non_generic_provider_local(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
let export_threshold = threshold(tcx);
if let Some(&level) = tcx.reachable_non_generics(def_id.krate).get(&def_id) {
level.is_below_threshold(export_threshold)
if let Some(&info) = tcx.reachable_non_generics(def_id.krate).get(&def_id) {
info.level.is_below_threshold(export_threshold)
} else {
false
}
@ -157,7 +162,7 @@ fn is_reachable_non_generic_provider_extern(tcx: TyCtxt<'_>, def_id: DefId) -> b
fn exported_symbols_provider_local<'tcx>(
tcx: TyCtxt<'tcx>,
cnum: CrateNum,
) -> &'tcx [(ExportedSymbol<'tcx>, SymbolExportLevel)] {
) -> &'tcx [(ExportedSymbol<'tcx>, SymbolExportInfo)] {
assert_eq!(cnum, LOCAL_CRATE);
if !tcx.sess.opts.output_types.should_codegen() {
@ -167,13 +172,16 @@ fn exported_symbols_provider_local<'tcx>(
let mut symbols: Vec<_> = tcx
.reachable_non_generics(LOCAL_CRATE)
.iter()
.map(|(&def_id, &level)| (ExportedSymbol::NonGeneric(def_id), level))
.map(|(&def_id, &info)| (ExportedSymbol::NonGeneric(def_id), info))
.collect();
if tcx.entry_fn(()).is_some() {
let exported_symbol = ExportedSymbol::NoDefId(SymbolName::new(tcx, "main"));
symbols.push((exported_symbol, SymbolExportLevel::C));
symbols.push((
exported_symbol,
SymbolExportInfo { level: SymbolExportLevel::C },
));
}
if tcx.allocator_kind(()).is_some() {
@ -181,7 +189,10 @@ fn exported_symbols_provider_local<'tcx>(
let symbol_name = format!("__rust_{}", method.name);
let exported_symbol = ExportedSymbol::NoDefId(SymbolName::new(tcx, &symbol_name));
symbols.push((exported_symbol, SymbolExportLevel::Rust));
symbols.push((
exported_symbol,
SymbolExportInfo { level: SymbolExportLevel::Rust },
));
}
}
@ -194,7 +205,10 @@ fn exported_symbols_provider_local<'tcx>(
symbols.extend(PROFILER_WEAK_SYMBOLS.iter().map(|sym| {
let exported_symbol = ExportedSymbol::NoDefId(SymbolName::new(tcx, sym));
(exported_symbol, SymbolExportLevel::C)
(
exported_symbol,
SymbolExportInfo { level: SymbolExportLevel::C },
)
}));
}
@ -204,7 +218,10 @@ fn exported_symbols_provider_local<'tcx>(
symbols.extend(MSAN_WEAK_SYMBOLS.iter().map(|sym| {
let exported_symbol = ExportedSymbol::NoDefId(SymbolName::new(tcx, sym));
(exported_symbol, SymbolExportLevel::C)
(
exported_symbol,
SymbolExportInfo { level: SymbolExportLevel::C },
)
}));
}
@ -212,7 +229,10 @@ fn exported_symbols_provider_local<'tcx>(
let symbol_name = metadata_symbol_name(tcx);
let exported_symbol = ExportedSymbol::NoDefId(SymbolName::new(tcx, &symbol_name));
symbols.push((exported_symbol, SymbolExportLevel::Rust));
symbols.push((
exported_symbol,
SymbolExportInfo { level: SymbolExportLevel::Rust },
));
}
if tcx.sess.opts.share_generics() && tcx.local_crate_exports_generics() {
@ -245,7 +265,12 @@ fn exported_symbols_provider_local<'tcx>(
MonoItem::Fn(Instance { def: InstanceDef::Item(def), substs }) => {
if substs.non_erasable_generics().next().is_some() {
let symbol = ExportedSymbol::Generic(def.did, substs);
symbols.push((symbol, SymbolExportLevel::Rust));
symbols.push((
symbol,
SymbolExportInfo {
level: SymbolExportLevel::Rust,
},
));
}
}
MonoItem::Fn(Instance { def: InstanceDef::DropGlue(_, Some(ty)), substs }) => {
@ -254,7 +279,12 @@ fn exported_symbols_provider_local<'tcx>(
substs.non_erasable_generics().next(),
Some(GenericArgKind::Type(ty))
);
symbols.push((ExportedSymbol::DropGlue(ty), SymbolExportLevel::Rust));
symbols.push((
ExportedSymbol::DropGlue(ty),
SymbolExportInfo {
level: SymbolExportLevel::Rust,
},
));
}
_ => {
// Any other symbols don't qualify for sharing

View File

@ -23,7 +23,7 @@ use rustc_incremental::{
};
use rustc_metadata::EncodedMetadata;
use rustc_middle::dep_graph::{WorkProduct, WorkProductId};
use rustc_middle::middle::exported_symbols::SymbolExportLevel;
use rustc_middle::middle::exported_symbols::SymbolExportInfo;
use rustc_middle::ty::TyCtxt;
use rustc_session::cgu_reuse_tracker::CguReuseTracker;
use rustc_session::config::{self, CrateType, Lto, OutputFilenames, OutputType};
@ -304,7 +304,7 @@ pub type TargetMachineFactoryFn<B> = Arc<
+ Sync,
>;
pub type ExportedSymbols = FxHashMap<CrateNum, Arc<Vec<(String, SymbolExportLevel)>>>;
pub type ExportedSymbols = FxHashMap<CrateNum, Arc<Vec<(String, SymbolExportInfo)>>>;
/// Additional resources used by optimize_and_codegen (not module specific)
#[derive(Clone)]

View File

@ -22,7 +22,7 @@ use rustc_hir::lang_items;
use rustc_index::vec::{Idx, IndexVec};
use rustc_middle::arena::ArenaAllocatable;
use rustc_middle::metadata::ModChild;
use rustc_middle::middle::exported_symbols::{ExportedSymbol, SymbolExportLevel};
use rustc_middle::middle::exported_symbols::{ExportedSymbol, SymbolExportInfo};
use rustc_middle::middle::stability::DeprecationEntry;
use rustc_middle::mir::interpret::{AllocDecodingSession, AllocDecodingState};
use rustc_middle::thir;
@ -1405,7 +1405,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
fn exported_symbols(
self,
tcx: TyCtxt<'tcx>,
) -> &'tcx [(ExportedSymbol<'tcx>, SymbolExportLevel)] {
) -> &'tcx [(ExportedSymbol<'tcx>, SymbolExportInfo)] {
tcx.arena.alloc_from_iter(self.root.exported_symbols.decode((self, tcx)))
}

View File

@ -188,9 +188,9 @@ provide! { <'tcx> tcx, def_id, other, cdata,
let reachable_non_generics = tcx
.exported_symbols(cdata.cnum)
.iter()
.filter_map(|&(exported_symbol, export_level)| {
.filter_map(|&(exported_symbol, export_info)| {
if let ExportedSymbol::NonGeneric(def_id) = exported_symbol {
Some((def_id, export_level))
Some((def_id, export_info))
} else {
None
}

View File

@ -21,7 +21,7 @@ use rustc_index::vec::Idx;
use rustc_middle::hir::nested_filter;
use rustc_middle::middle::dependency_format::Linkage;
use rustc_middle::middle::exported_symbols::{
metadata_symbol_name, ExportedSymbol, SymbolExportLevel,
metadata_symbol_name, ExportedSymbol, SymbolExportInfo,
};
use rustc_middle::mir::interpret;
use rustc_middle::thir;
@ -1844,8 +1844,8 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
// definition (as that's not defined in this crate).
fn encode_exported_symbols(
&mut self,
exported_symbols: &[(ExportedSymbol<'tcx>, SymbolExportLevel)],
) -> Lazy<[(ExportedSymbol<'tcx>, SymbolExportLevel)]> {
exported_symbols: &[(ExportedSymbol<'tcx>, SymbolExportInfo)],
) -> Lazy<[(ExportedSymbol<'tcx>, SymbolExportInfo)]> {
empty_proc_macro!(self);
// The metadata symbol name is special. It should not show up in
// downstream crates.

View File

@ -13,7 +13,7 @@ use rustc_hir::definitions::DefKey;
use rustc_hir::lang_items;
use rustc_index::{bit_set::FiniteBitSet, vec::IndexVec};
use rustc_middle::metadata::ModChild;
use rustc_middle::middle::exported_symbols::{ExportedSymbol, SymbolExportLevel};
use rustc_middle::middle::exported_symbols::{ExportedSymbol, SymbolExportInfo};
use rustc_middle::mir;
use rustc_middle::thir;
use rustc_middle::ty::fast_reject::SimplifiedType;
@ -218,7 +218,7 @@ crate struct CrateRoot<'tcx> {
tables: LazyTables<'tcx>,
exported_symbols: Lazy!([(ExportedSymbol<'tcx>, SymbolExportLevel)]),
exported_symbols: Lazy!([(ExportedSymbol<'tcx>, SymbolExportInfo)]),
syntax_contexts: SyntaxContextTable,
expn_data: ExpnDataTable,

View File

@ -21,6 +21,13 @@ impl SymbolExportLevel {
}
}
/// The `SymbolExportInfo` of a symbols specifies symbol-related information
/// that is relevant to code generation and linking.
#[derive(Eq, PartialEq, Debug, Copy, Clone, TyEncodable, TyDecodable, HashStable)]
pub struct SymbolExportInfo {
pub level: SymbolExportLevel,
}
#[derive(Eq, PartialEq, Debug, Copy, Clone, TyEncodable, TyDecodable, HashStable)]
pub enum ExportedSymbol<'tcx> {
NonGeneric(DefId),

View File

@ -1355,7 +1355,7 @@ rustc_queries! {
// Does not include external symbols that don't have a corresponding DefId,
// like the compiler-generated `main` function and so on.
query reachable_non_generics(_: CrateNum)
-> DefIdMap<SymbolExportLevel> {
-> DefIdMap<SymbolExportInfo> {
storage(ArenaCacheSelector<'tcx>)
desc { "looking up the exported symbols of a crate" }
separate_provide_extern
@ -1672,7 +1672,7 @@ rustc_queries! {
/// correspond to a publicly visible symbol in `cnum` machine code.
/// - The `exported_symbols` sets of different crates do not intersect.
query exported_symbols(_: CrateNum)
-> &'tcx [(ExportedSymbol<'tcx>, SymbolExportLevel)] {
-> &'tcx [(ExportedSymbol<'tcx>, SymbolExportInfo)] {
desc { "exported_symbols" }
separate_provide_extern
}

View File

@ -3,7 +3,7 @@ use crate::infer::canonical::{self, Canonical};
use crate::lint::LintLevelMap;
use crate::metadata::ModChild;
use crate::middle::codegen_fn_attrs::CodegenFnAttrs;
use crate::middle::exported_symbols::{ExportedSymbol, SymbolExportLevel};
use crate::middle::exported_symbols::{ExportedSymbol, SymbolExportInfo};
use crate::middle::lib_features::LibFeatures;
use crate::middle::privacy::AccessLevels;
use crate::middle::region;

View File

@ -5,7 +5,7 @@ use rustc_hir::def::DefKind;
use rustc_hir::def_id::{DefId, CRATE_DEF_INDEX, LOCAL_CRATE};
use rustc_hir::definitions::DefPathDataName;
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
use rustc_middle::middle::exported_symbols::SymbolExportLevel;
use rustc_middle::middle::exported_symbols::{SymbolExportInfo, SymbolExportLevel};
use rustc_middle::mir::mono::{CodegenUnit, CodegenUnitNameBuilder, Linkage, Visibility};
use rustc_middle::mir::mono::{InstantiationMode, MonoItem};
use rustc_middle::ty::print::characteristic_def_id_of_type;
@ -554,7 +554,7 @@ fn default_visibility(tcx: TyCtxt<'_>, id: DefId, is_generic: bool) -> Visibilit
// C-export level items remain at `Default`, all other internal
// items become `Hidden`.
match tcx.reachable_non_generics(id.krate).get(&id) {
Some(SymbolExportLevel::C) => Visibility::Default,
Some(SymbolExportInfo { level: SymbolExportLevel::C, .. }) => Visibility::Default,
_ => Visibility::Hidden,
}
}