2019-07-16 21:06:17 +03:00
|
|
|
use std::collections::hash_map::Entry::*;
|
2017-09-13 13:22:20 -07:00
|
|
|
|
2020-02-29 20:37:32 +03:00
|
|
|
use rustc_ast::expand::allocator::ALLOCATOR_METHODS;
|
2019-12-22 17:42:04 -05:00
|
|
|
use rustc_data_structures::fingerprint::Fingerprint;
|
2019-12-24 05:02:53 +01:00
|
|
|
use rustc_data_structures::fx::FxHashMap;
|
2020-01-05 02:37:57 +01:00
|
|
|
use rustc_hir as hir;
|
2021-05-11 11:35:50 +02:00
|
|
|
use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, LocalDefId, CRATE_DEF_INDEX, LOCAL_CRATE};
|
2020-01-05 02:37:57 +01:00
|
|
|
use rustc_hir::Node;
|
2019-09-26 05:38:33 +00:00
|
|
|
use rustc_index::vec::IndexVec;
|
2020-03-29 17:19:48 +02:00
|
|
|
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
|
|
|
|
|
use rustc_middle::middle::exported_symbols::{
|
|
|
|
|
metadata_symbol_name, ExportedSymbol, SymbolExportLevel,
|
|
|
|
|
};
|
2021-05-30 17:24:54 +02:00
|
|
|
use rustc_middle::ty::query::{ExternProviders, Providers};
|
2020-03-29 17:19:48 +02:00
|
|
|
use rustc_middle::ty::subst::{GenericArgKind, SubstsRef};
|
|
|
|
|
use rustc_middle::ty::Instance;
|
|
|
|
|
use rustc_middle::ty::{SymbolName, TyCtxt};
|
2021-02-07 23:47:03 +02:00
|
|
|
use rustc_session::config::CrateType;
|
|
|
|
|
use rustc_target::spec::SanitizerSet;
|
2016-11-30 10:03:42 -05:00
|
|
|
|
2019-06-14 00:48:52 +03:00
|
|
|
pub fn threshold(tcx: TyCtxt<'_>) -> SymbolExportLevel {
|
2020-05-15 21:44:28 -07:00
|
|
|
crates_export_threshold(&tcx.sess.crate_types())
|
2017-09-13 13:22:20 -07:00
|
|
|
}
|
2017-09-12 09:32:37 -07:00
|
|
|
|
2020-05-02 01:30:23 +03:00
|
|
|
fn crate_export_threshold(crate_type: CrateType) -> SymbolExportLevel {
|
2017-09-13 13:22:20 -07:00
|
|
|
match crate_type {
|
2020-05-02 01:30:23 +03:00
|
|
|
CrateType::Executable | CrateType::Staticlib | CrateType::ProcMacro | CrateType::Cdylib => {
|
|
|
|
|
SymbolExportLevel::C
|
|
|
|
|
}
|
|
|
|
|
CrateType::Rlib | CrateType::Dylib => SymbolExportLevel::Rust,
|
2017-09-12 09:32:37 -07:00
|
|
|
}
|
2017-09-13 13:22:20 -07:00
|
|
|
}
|
2016-12-02 18:00:41 -05:00
|
|
|
|
2020-05-02 01:30:23 +03:00
|
|
|
pub fn crates_export_threshold(crate_types: &[CrateType]) -> SymbolExportLevel {
|
2019-12-22 17:42:04 -05:00
|
|
|
if crate_types
|
|
|
|
|
.iter()
|
|
|
|
|
.any(|&crate_type| crate_export_threshold(crate_type) == SymbolExportLevel::Rust)
|
2018-10-06 11:37:28 +02:00
|
|
|
{
|
2017-09-13 13:22:20 -07:00
|
|
|
SymbolExportLevel::Rust
|
|
|
|
|
} else {
|
|
|
|
|
SymbolExportLevel::C
|
2017-09-12 09:32:37 -07:00
|
|
|
}
|
2017-09-13 13:22:20 -07:00
|
|
|
}
|
2016-11-30 10:03:42 -05:00
|
|
|
|
2020-03-27 20:26:20 +01:00
|
|
|
fn reachable_non_generics_provider(tcx: TyCtxt<'_>, cnum: CrateNum) -> DefIdMap<SymbolExportLevel> {
|
2018-02-23 16:25:03 +01:00
|
|
|
assert_eq!(cnum, LOCAL_CRATE);
|
|
|
|
|
|
2018-05-08 16:10:16 +03:00
|
|
|
if !tcx.sess.opts.output_types.should_codegen() {
|
2020-03-27 20:26:20 +01:00
|
|
|
return Default::default();
|
2018-02-23 16:25:03 +01:00
|
|
|
}
|
|
|
|
|
|
2018-02-27 19:28:21 +01:00
|
|
|
// Check to see if this crate is a "special runtime crate". These
|
|
|
|
|
// crates, implementation details of the standard library, typically
|
|
|
|
|
// have a bunch of `pub extern` and `#[no_mangle]` functions as the
|
|
|
|
|
// ABI between them. We don't want their symbols to have a `C`
|
|
|
|
|
// export level, however, as they're just implementation details.
|
|
|
|
|
// Down below we'll hardwire all of the symbols to the `Rust` export
|
|
|
|
|
// level instead.
|
2019-12-22 17:42:04 -05:00
|
|
|
let special_runtime_crate =
|
|
|
|
|
tcx.is_panic_runtime(LOCAL_CRATE) || tcx.is_compiler_builtins(LOCAL_CRATE);
|
2018-02-27 19:28:21 +01:00
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
let mut reachable_non_generics: DefIdMap<_> = tcx
|
2021-05-11 11:35:50 +02:00
|
|
|
.reachable_set(())
|
2018-02-23 16:25:03 +01:00
|
|
|
.iter()
|
2020-07-15 02:51:46 +03:00
|
|
|
.filter_map(|&def_id| {
|
2018-02-23 16:25:03 +01:00
|
|
|
// We want to ignore some FFI functions that are not exposed from
|
|
|
|
|
// this crate. Reachable FFI functions can be lumped into two
|
|
|
|
|
// categories:
|
|
|
|
|
//
|
|
|
|
|
// 1. Those that are included statically via a static library
|
2018-11-27 02:59:49 +00:00
|
|
|
// 2. Those included otherwise (e.g., dynamically or via a framework)
|
2018-02-23 16:25:03 +01:00
|
|
|
//
|
|
|
|
|
// Although our LLVM module is not literally emitting code for the
|
|
|
|
|
// statically included symbols, it's an export of our library which
|
|
|
|
|
// needs to be passed on to the linker and encoded in the metadata.
|
|
|
|
|
//
|
|
|
|
|
// As a result, if this id is an FFI item (foreign item) then we only
|
|
|
|
|
// let it through if it's included statically.
|
2021-10-20 20:59:15 +02:00
|
|
|
match tcx.hir().get_by_def_id(def_id) {
|
2018-08-25 15:56:16 +01:00
|
|
|
Node::ForeignItem(..) => {
|
2019-12-06 12:18:32 +00:00
|
|
|
tcx.is_statically_included_foreign_item(def_id).then_some(def_id)
|
2018-02-23 16:25:03 +01:00
|
|
|
}
|
2016-11-30 10:03:42 -05:00
|
|
|
|
2018-02-23 16:25:03 +01:00
|
|
|
// Only consider nodes that actually have exported symbols.
|
2020-04-16 17:38:52 -07:00
|
|
|
Node::Item(&hir::Item {
|
|
|
|
|
kind: hir::ItemKind::Static(..) | hir::ItemKind::Fn(..),
|
|
|
|
|
..
|
|
|
|
|
})
|
2020-03-05 09:57:34 -06:00
|
|
|
| Node::ImplItem(&hir::ImplItem { kind: hir::ImplItemKind::Fn(..), .. }) => {
|
2018-02-23 16:25:03 +01:00
|
|
|
let generics = tcx.generics_of(def_id);
|
2020-06-05 16:47:37 +01:00
|
|
|
if !generics.requires_monomorphization(tcx)
|
2020-06-08 09:37:11 +01:00
|
|
|
// Functions marked with #[inline] are codegened with "internal"
|
|
|
|
|
// linkage and are not exported unless marked with an extern
|
|
|
|
|
// inidicator
|
|
|
|
|
&& (!Instance::mono(tcx, def_id.to_def_id()).def.generates_cgu_internal_copy(tcx)
|
|
|
|
|
|| tcx.codegen_fn_attrs(def_id.to_def_id()).contains_extern_indicator())
|
2019-12-22 17:42:04 -05:00
|
|
|
{
|
2018-02-23 16:25:03 +01:00
|
|
|
Some(def_id)
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
_ => None,
|
2017-09-13 13:22:20 -07:00
|
|
|
}
|
2018-02-23 16:25:03 +01:00
|
|
|
})
|
2018-03-06 10:18:48 +01:00
|
|
|
.map(|def_id| {
|
2018-02-27 19:28:21 +01:00
|
|
|
let export_level = if special_runtime_crate {
|
2020-07-08 11:04:10 +10:00
|
|
|
let name = tcx.symbol_name(Instance::mono(tcx, def_id.to_def_id())).name;
|
2018-02-27 19:28:21 +01:00
|
|
|
// We can probably do better here by just ensuring that
|
|
|
|
|
// it has hidden visibility rather than public
|
|
|
|
|
// visibility, as this is primarily here to ensure it's
|
|
|
|
|
// not stripped during LTO.
|
|
|
|
|
//
|
|
|
|
|
// In general though we won't link right if these
|
|
|
|
|
// symbols are stripped, and LTO currently strips them.
|
2020-07-08 11:04:10 +10:00
|
|
|
match name {
|
2020-07-10 15:45:05 +10:00
|
|
|
"rust_eh_personality"
|
|
|
|
|
| "rust_eh_register_frames"
|
|
|
|
|
| "rust_eh_unregister_frames" =>
|
2020-07-08 11:04:10 +10:00
|
|
|
SymbolExportLevel::C,
|
|
|
|
|
_ => SymbolExportLevel::Rust,
|
2018-02-27 19:28:21 +01:00
|
|
|
}
|
|
|
|
|
} else {
|
2020-04-09 09:43:00 +01:00
|
|
|
symbol_export_level(tcx, def_id.to_def_id())
|
2018-02-27 19:28:21 +01:00
|
|
|
};
|
2019-12-22 17:42:04 -05:00
|
|
|
debug!(
|
|
|
|
|
"EXPORTED SYMBOL (local): {} ({:?})",
|
2020-04-09 09:43:00 +01:00
|
|
|
tcx.symbol_name(Instance::mono(tcx, def_id.to_def_id())),
|
2019-12-22 17:42:04 -05:00
|
|
|
export_level
|
|
|
|
|
);
|
2020-04-09 09:43:00 +01:00
|
|
|
(def_id.to_def_id(), export_level)
|
2018-02-23 16:25:03 +01:00
|
|
|
})
|
|
|
|
|
.collect();
|
|
|
|
|
|
2021-05-11 12:12:52 +02:00
|
|
|
if let Some(id) = tcx.proc_macro_decls_static(()) {
|
|
|
|
|
reachable_non_generics.insert(id.to_def_id(), SymbolExportLevel::C);
|
2018-02-28 17:40:25 +01:00
|
|
|
}
|
|
|
|
|
|
2020-03-27 20:26:20 +01:00
|
|
|
reachable_non_generics
|
2018-03-06 10:18:48 +01:00
|
|
|
}
|
|
|
|
|
|
2019-06-21 18:51:27 +02:00
|
|
|
fn is_reachable_non_generic_provider_local(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
|
2018-03-06 10:18:48 +01:00
|
|
|
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)
|
|
|
|
|
} else {
|
|
|
|
|
false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-21 18:51:27 +02:00
|
|
|
fn is_reachable_non_generic_provider_extern(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
|
2018-03-06 10:18:48 +01:00
|
|
|
tcx.reachable_non_generics(def_id.krate).contains_key(&def_id)
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-13 21:52:35 -05:00
|
|
|
fn exported_symbols_provider_local<'tcx>(
|
2020-07-05 23:00:14 +03:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2019-06-12 00:11:55 +03:00
|
|
|
cnum: CrateNum,
|
2020-07-05 23:00:14 +03:00
|
|
|
) -> &'tcx [(ExportedSymbol<'tcx>, SymbolExportLevel)] {
|
2018-03-06 10:18:48 +01:00
|
|
|
assert_eq!(cnum, LOCAL_CRATE);
|
|
|
|
|
|
2018-05-08 16:10:16 +03:00
|
|
|
if !tcx.sess.opts.output_types.should_codegen() {
|
2020-03-14 12:41:32 +01:00
|
|
|
return &[];
|
2018-03-06 10:18:48 +01:00
|
|
|
}
|
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
let mut symbols: Vec<_> = tcx
|
|
|
|
|
.reachable_non_generics(LOCAL_CRATE)
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|(&def_id, &level)| (ExportedSymbol::NonGeneric(def_id), level))
|
|
|
|
|
.collect();
|
2018-03-06 10:18:48 +01:00
|
|
|
|
2021-05-11 12:00:59 +02:00
|
|
|
if tcx.entry_fn(()).is_some() {
|
2020-07-10 15:45:05 +10:00
|
|
|
let exported_symbol = ExportedSymbol::NoDefId(SymbolName::new(tcx, "main"));
|
2018-02-27 17:52:07 +01:00
|
|
|
|
|
|
|
|
symbols.push((exported_symbol, SymbolExportLevel::C));
|
2018-02-23 16:25:03 +01:00
|
|
|
}
|
2016-11-30 10:03:42 -05:00
|
|
|
|
2021-05-11 22:05:54 +02:00
|
|
|
if tcx.allocator_kind(()).is_some() {
|
2018-02-23 16:25:03 +01:00
|
|
|
for method in ALLOCATOR_METHODS {
|
2018-02-27 17:52:07 +01:00
|
|
|
let symbol_name = format!("__rust_{}", method.name);
|
2020-07-10 15:45:05 +10:00
|
|
|
let exported_symbol = ExportedSymbol::NoDefId(SymbolName::new(tcx, &symbol_name));
|
2018-02-27 17:52:07 +01:00
|
|
|
|
|
|
|
|
symbols.push((exported_symbol, SymbolExportLevel::Rust));
|
2017-09-13 13:22:20 -07:00
|
|
|
}
|
2018-02-23 16:25:03 +01:00
|
|
|
}
|
2017-11-07 15:15:45 +01:00
|
|
|
|
coverage bug fixes and optimization support
Adjusted LLVM codegen for code compiled with `-Zinstrument-coverage` to
address multiple, somewhat related issues.
Fixed a significant flaw in prior coverage solution: Every counter
generated a new counter variable, but there should have only been one
counter variable per function. This appears to have bloated .profraw
files significantly. (For a small program, it increased the size by
about 40%. I have not tested large programs, but there is anecdotal
evidence that profraw files were way too large. This is a good fix,
regardless, but hopefully it also addresses related issues.
Fixes: #82144
Invalid LLVM coverage data produced when compiled with -C opt-level=1
Existing tests now work up to at least `opt-level=3`. This required a
detailed analysis of the LLVM IR, comparisons with Clang C++ LLVM IR
when compiled with coverage, and a lot of trial and error with codegen
adjustments.
The biggest hurdle was figuring out how to continue to support coverage
results for unused functions and generics. Rust's coverage results have
three advantages over Clang's coverage results:
1. Rust's coverage map does not include any overlapping code regions,
making coverage counting unambiguous.
2. Rust generates coverage results (showing zero counts) for all unused
functions, including generics. (Clang does not generate coverage for
uninstantiated template functions.)
3. Rust's unused functions produce minimal stubbed functions in LLVM IR,
sufficient for including in the coverage results; while Clang must
generate the complete LLVM IR for each unused function, even though
it will never be called.
This PR removes the previous hack of attempting to inject coverage into
some other existing function instance, and generates dedicated instances
for each unused function. This change, and a few other adjustments
(similar to what is required for `-C link-dead-code`, but with lower
impact), makes it possible to support LLVM optimizations.
Fixes: #79651
Coverage report: "Unexecuted instantiation:..." for a generic function
from multiple crates
Fixed by removing the aforementioned hack. Some "Unexecuted
instantiation" notices are unavoidable, as explained in the
`used_crate.rs` test, but `-Zinstrument-coverage` has new options to
back off support for either unused generics, or all unused functions,
which avoids the notice, at the cost of less coverage of unused
functions.
Fixes: #82875
Invalid LLVM coverage data produced with crate brotli_decompressor
Fixed by disabling the LLVM function attribute that forces inlining, if
`-Z instrument-coverage` is enabled. This attribute is applied to
Rust functions with `#[inline(always)], and in some cases, the forced
inlining breaks coverage instrumentation and reports.
2021-03-15 16:32:45 -07:00
|
|
|
if tcx.sess.instrument_coverage() || tcx.sess.opts.cg.profile_generate.enabled() {
|
2018-03-13 12:40:57 +01:00
|
|
|
// These are weak symbols that point to the profile version and the
|
|
|
|
|
// profile name, which need to be treated as exported so LTO doesn't nix
|
|
|
|
|
// them.
|
2019-12-22 17:42:04 -05:00
|
|
|
const PROFILER_WEAK_SYMBOLS: [&str; 2] =
|
|
|
|
|
["__llvm_profile_raw_version", "__llvm_profile_filename"];
|
2018-10-06 11:45:11 +02:00
|
|
|
|
|
|
|
|
symbols.extend(PROFILER_WEAK_SYMBOLS.iter().map(|sym| {
|
2020-07-10 15:45:05 +10:00
|
|
|
let exported_symbol = ExportedSymbol::NoDefId(SymbolName::new(tcx, sym));
|
2018-10-06 11:45:11 +02:00
|
|
|
(exported_symbol, SymbolExportLevel::C)
|
|
|
|
|
}));
|
2018-03-13 12:40:57 +01:00
|
|
|
}
|
|
|
|
|
|
2020-06-14 00:00:00 +00:00
|
|
|
if tcx.sess.opts.debugging_opts.sanitizer.contains(SanitizerSet::MEMORY) {
|
2020-01-19 22:08:15 +01:00
|
|
|
// Similar to profiling, preserve weak msan symbol during LTO.
|
2020-01-21 00:00:00 +00:00
|
|
|
const MSAN_WEAK_SYMBOLS: [&str; 2] = ["__msan_track_origins", "__msan_keep_going"];
|
|
|
|
|
|
|
|
|
|
symbols.extend(MSAN_WEAK_SYMBOLS.iter().map(|sym| {
|
2020-07-10 15:45:05 +10:00
|
|
|
let exported_symbol = ExportedSymbol::NoDefId(SymbolName::new(tcx, sym));
|
2020-01-21 00:00:00 +00:00
|
|
|
(exported_symbol, SymbolExportLevel::C)
|
|
|
|
|
}));
|
2020-01-19 22:08:15 +01:00
|
|
|
}
|
|
|
|
|
|
2020-05-15 21:44:28 -07:00
|
|
|
if tcx.sess.crate_types().contains(&CrateType::Dylib) {
|
2018-02-27 17:52:07 +01:00
|
|
|
let symbol_name = metadata_symbol_name(tcx);
|
2020-07-10 15:45:05 +10:00
|
|
|
let exported_symbol = ExportedSymbol::NoDefId(SymbolName::new(tcx, &symbol_name));
|
2018-02-27 17:52:07 +01:00
|
|
|
|
|
|
|
|
symbols.push((exported_symbol, SymbolExportLevel::Rust));
|
2018-02-23 16:25:03 +01:00
|
|
|
}
|
2017-11-07 15:15:45 +01:00
|
|
|
|
2018-07-26 13:20:47 -06:00
|
|
|
if tcx.sess.opts.share_generics() && tcx.local_crate_exports_generics() {
|
2020-03-29 16:41:09 +02:00
|
|
|
use rustc_middle::mir::mono::{Linkage, MonoItem, Visibility};
|
|
|
|
|
use rustc_middle::ty::InstanceDef;
|
2018-03-06 10:33:42 +01:00
|
|
|
|
2018-03-21 12:23:57 +01:00
|
|
|
// Normally, we require that shared monomorphizations are not hidden,
|
|
|
|
|
// because if we want to re-use a monomorphization from a Rust dylib, it
|
|
|
|
|
// needs to be exported.
|
|
|
|
|
// However, on platforms that don't allow for Rust dylibs, having
|
|
|
|
|
// external linkage is enough for monomorphization to be linked to.
|
2020-11-08 14:27:51 +03:00
|
|
|
let need_visibility = tcx.sess.target.dynamic_linking && !tcx.sess.target.only_cdylib;
|
2018-03-21 12:23:57 +01:00
|
|
|
|
2021-05-11 14:39:04 +02:00
|
|
|
let (_, cgus) = tcx.collect_and_partition_mono_items(());
|
2018-03-06 10:33:42 +01:00
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
for (mono_item, &(linkage, visibility)) in cgus.iter().flat_map(|cgu| cgu.items().iter()) {
|
2018-03-21 12:23:57 +01:00
|
|
|
if linkage != Linkage::External {
|
|
|
|
|
// We can only re-use things with external linkage, otherwise
|
|
|
|
|
// we'll get a linker error
|
2019-12-22 17:42:04 -05:00
|
|
|
continue;
|
2018-03-21 12:23:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if need_visibility && visibility == Visibility::Hidden {
|
|
|
|
|
// If we potentially share things from Rust dylibs, they must
|
|
|
|
|
// not be hidden
|
2019-12-22 17:42:04 -05:00
|
|
|
continue;
|
2018-03-21 12:23:57 +01:00
|
|
|
}
|
|
|
|
|
|
2020-01-20 16:38:42 +01:00
|
|
|
match *mono_item {
|
2020-07-03 19:13:39 +02:00
|
|
|
MonoItem::Fn(Instance { def: InstanceDef::Item(def), substs }) => {
|
2020-01-20 16:38:42 +01:00
|
|
|
if substs.non_erasable_generics().next().is_some() {
|
2020-07-03 19:13:39 +02:00
|
|
|
let symbol = ExportedSymbol::Generic(def.did, substs);
|
2020-01-20 16:38:42 +01:00
|
|
|
symbols.push((symbol, SymbolExportLevel::Rust));
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-01-22 12:17:21 +01:00
|
|
|
MonoItem::Fn(Instance { def: InstanceDef::DropGlue(_, Some(ty)), substs }) => {
|
2020-01-20 16:38:42 +01:00
|
|
|
// A little sanity-check
|
|
|
|
|
debug_assert_eq!(
|
|
|
|
|
substs.non_erasable_generics().next(),
|
|
|
|
|
Some(GenericArgKind::Type(ty))
|
|
|
|
|
);
|
2020-01-22 12:17:21 +01:00
|
|
|
symbols.push((ExportedSymbol::DropGlue(ty), SymbolExportLevel::Rust));
|
2020-01-20 16:38:42 +01:00
|
|
|
}
|
|
|
|
|
_ => {
|
|
|
|
|
// Any other symbols don't qualify for sharing
|
2018-03-06 10:33:42 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-23 16:25:03 +01:00
|
|
|
// Sort so we get a stable incr. comp. hash.
|
2020-01-20 16:38:42 +01:00
|
|
|
symbols.sort_by_cached_key(|s| s.0.symbol_name_for_local_instance(tcx));
|
2018-01-30 11:54:07 -08:00
|
|
|
|
2020-03-14 12:41:32 +01:00
|
|
|
tcx.arena.alloc_from_iter(symbols)
|
2017-09-13 13:22:20 -07:00
|
|
|
}
|
|
|
|
|
|
2019-06-21 18:51:27 +02:00
|
|
|
fn upstream_monomorphizations_provider(
|
|
|
|
|
tcx: TyCtxt<'_>,
|
2021-05-11 14:39:04 +02:00
|
|
|
(): (),
|
2020-03-27 20:26:20 +01:00
|
|
|
) -> DefIdMap<FxHashMap<SubstsRef<'_>, CrateNum>> {
|
2021-06-07 11:03:17 +02:00
|
|
|
let cnums = tcx.crates(());
|
2018-03-06 10:33:42 +01:00
|
|
|
|
2018-07-21 22:15:11 +03:00
|
|
|
let mut instances: DefIdMap<FxHashMap<_, _>> = Default::default();
|
2018-03-06 10:33:42 +01:00
|
|
|
|
2018-03-12 18:28:53 +01:00
|
|
|
let cnum_stable_ids: IndexVec<CrateNum, Fingerprint> = {
|
2019-12-22 17:42:04 -05:00
|
|
|
let mut cnum_stable_ids = IndexVec::from_elem_n(Fingerprint::ZERO, cnums.len() + 1);
|
2018-03-12 18:28:53 +01:00
|
|
|
|
|
|
|
|
for &cnum in cnums.iter() {
|
2019-12-22 17:42:04 -05:00
|
|
|
cnum_stable_ids[cnum] =
|
|
|
|
|
tcx.def_path_hash(DefId { krate: cnum, index: CRATE_DEF_INDEX }).0;
|
2018-03-12 18:28:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cnum_stable_ids
|
|
|
|
|
};
|
|
|
|
|
|
2020-01-22 12:17:21 +01:00
|
|
|
let drop_in_place_fn_def_id = tcx.lang_items().drop_in_place_fn();
|
|
|
|
|
|
2018-03-06 10:33:42 +01:00
|
|
|
for &cnum in cnums.iter() {
|
rustc: Fix mixing crates with different `share_generics`
This commit addresses #64319 by removing the `dylib` crate type from the
list of crate type that exports generic symbols. The bug in #64319
arises because a `dylib` crate type was trying to export a symbol in an
uptream crate but it miscalculated the symbol name of the uptream
symbol. This isn't really necessary, though, since `dylib` crates aren't
that heavily used, so we can just conservatively say that the `dylib`
crate type never exports generic symbols, forcibly removing them from
the exported symbol lists if were to otherwise find them.
The fix here happens in two places:
* First is in the `local_crate_exports_generics` method, indicating that
it's now `false` for the `Dylib` crate type. Only rlibs actually
export generics at this point.
* Next is when we load exported symbols from upstream crate. If, for our
compilation session, the crate may be included from a dynamic library,
then its generic symbols are removed. When the crate was linked into a
dynamic library its symbols weren't exported, so we can't consider
them a candidate to link against.
Overally this should avoid situations where we incorrectly calculate the
upstream symbol names in the face of differnet `share_generics` options,
ultimately...
Closes #64319
2019-09-11 08:08:04 -07:00
|
|
|
for (exported_symbol, _) in tcx.exported_symbols(cnum).iter() {
|
2020-01-22 12:17:21 +01:00
|
|
|
let (def_id, substs) = match *exported_symbol {
|
|
|
|
|
ExportedSymbol::Generic(def_id, substs) => (def_id, substs),
|
|
|
|
|
ExportedSymbol::DropGlue(ty) => {
|
|
|
|
|
if let Some(drop_in_place_fn_def_id) = drop_in_place_fn_def_id {
|
|
|
|
|
(drop_in_place_fn_def_id, tcx.intern_substs(&[ty.into()]))
|
|
|
|
|
} else {
|
|
|
|
|
// `drop_in_place` in place does not exist, don't try
|
|
|
|
|
// to use it.
|
|
|
|
|
continue;
|
2018-03-12 18:28:53 +01:00
|
|
|
}
|
2020-01-22 12:17:21 +01:00
|
|
|
}
|
|
|
|
|
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] {
|
2018-03-12 18:28:53 +01:00
|
|
|
e.insert(cnum);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-01-22 12:17:21 +01:00
|
|
|
Vacant(e) => {
|
|
|
|
|
e.insert(cnum);
|
|
|
|
|
}
|
2018-03-06 10:33:42 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-27 20:26:20 +01:00
|
|
|
instances
|
2018-03-06 10:33:42 +01:00
|
|
|
}
|
|
|
|
|
|
2019-06-21 18:51:27 +02:00
|
|
|
fn upstream_monomorphizations_for_provider(
|
|
|
|
|
tcx: TyCtxt<'_>,
|
2019-06-12 00:11:55 +03:00
|
|
|
def_id: DefId,
|
2019-06-21 18:51:27 +02:00
|
|
|
) -> Option<&FxHashMap<SubstsRef<'_>, CrateNum>> {
|
2018-03-06 10:33:42 +01:00
|
|
|
debug_assert!(!def_id.is_local());
|
2021-05-11 14:39:04 +02:00
|
|
|
tcx.upstream_monomorphizations(()).get(&def_id)
|
2018-03-06 10:33:42 +01:00
|
|
|
}
|
|
|
|
|
|
2020-01-22 12:17:21 +01:00
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-11 11:35:50 +02:00
|
|
|
fn is_unreachable_local_definition_provider(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
|
|
|
|
|
!tcx.reachable_set(()).contains(&def_id)
|
2018-03-06 14:44:14 +01:00
|
|
|
}
|
|
|
|
|
|
2020-07-05 23:00:14 +03:00
|
|
|
pub fn provide(providers: &mut Providers) {
|
2018-02-23 16:25:03 +01:00
|
|
|
providers.reachable_non_generics = reachable_non_generics_provider;
|
2018-03-06 10:18:48 +01:00
|
|
|
providers.is_reachable_non_generic = is_reachable_non_generic_provider_local;
|
2018-02-23 16:25:03 +01:00
|
|
|
providers.exported_symbols = exported_symbols_provider_local;
|
2018-03-06 10:33:42 +01:00
|
|
|
providers.upstream_monomorphizations = upstream_monomorphizations_provider;
|
2018-03-06 14:44:14 +01:00
|
|
|
providers.is_unreachable_local_definition = is_unreachable_local_definition_provider;
|
2020-01-22 12:17:21 +01:00
|
|
|
providers.upstream_drop_glue_for = upstream_drop_glue_for_provider;
|
2021-03-29 10:13:50 +02:00
|
|
|
providers.wasm_import_module_map = wasm_import_module_map;
|
2018-02-23 16:25:03 +01:00
|
|
|
}
|
2016-11-30 10:03:42 -05:00
|
|
|
|
2021-05-30 17:24:54 +02:00
|
|
|
pub fn provide_extern(providers: &mut ExternProviders) {
|
2018-03-06 10:18:48 +01:00
|
|
|
providers.is_reachable_non_generic = is_reachable_non_generic_provider_extern;
|
2018-03-06 10:33:42 +01:00
|
|
|
providers.upstream_monomorphizations_for = upstream_monomorphizations_for_provider;
|
2016-11-30 10:03:42 -05:00
|
|
|
}
|
|
|
|
|
|
2019-06-14 00:48:52 +03:00
|
|
|
fn symbol_export_level(tcx: TyCtxt<'_>, sym_def_id: DefId) -> SymbolExportLevel {
|
2017-11-01 13:16:36 -07:00
|
|
|
// We export anything that's not mangled at the "C" layer as it probably has
|
|
|
|
|
// to do with ABI concerns. We do not, however, apply such treatment to
|
|
|
|
|
// special symbols in the standard library for various plumbing between
|
|
|
|
|
// core/std/allocators/etc. For example symbols used to hook up allocation
|
|
|
|
|
// are not considered for export
|
2018-05-08 16:10:16 +03:00
|
|
|
let codegen_fn_attrs = tcx.codegen_fn_attrs(sym_def_id);
|
|
|
|
|
let is_extern = codegen_fn_attrs.contains_extern_indicator();
|
|
|
|
|
let std_internal =
|
|
|
|
|
codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL);
|
2018-02-26 21:14:55 -05:00
|
|
|
|
2017-11-01 13:16:36 -07:00
|
|
|
if is_extern && !std_internal {
|
2020-10-15 11:44:00 +02:00
|
|
|
let target = &tcx.sess.target.llvm_target;
|
2019-10-18 14:47:54 -07:00
|
|
|
// WebAssembly cannot export data symbols, so reduce their export level
|
2020-01-07 16:20:58 +01:00
|
|
|
if target.contains("emscripten") {
|
2019-12-22 17:42:04 -05:00
|
|
|
if let Some(Node::Item(&hir::Item { kind: hir::ItemKind::Static(..), .. })) =
|
|
|
|
|
tcx.hir().get_if_local(sym_def_id)
|
|
|
|
|
{
|
2020-01-07 16:20:58 +01:00
|
|
|
return SymbolExportLevel::Rust;
|
2018-11-02 20:16:17 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-30 10:03:42 -05:00
|
|
|
SymbolExportLevel::C
|
2017-09-13 13:22:20 -07:00
|
|
|
} else {
|
|
|
|
|
SymbolExportLevel::Rust
|
2016-11-30 10:03:42 -05:00
|
|
|
}
|
|
|
|
|
}
|
2020-01-16 13:21:10 +01:00
|
|
|
|
|
|
|
|
/// This is the symbol name of the given instance instantiated in a specific crate.
|
|
|
|
|
pub fn symbol_name_for_instance_in_crate<'tcx>(
|
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
|
symbol: ExportedSymbol<'tcx>,
|
|
|
|
|
instantiating_crate: CrateNum,
|
|
|
|
|
) -> String {
|
|
|
|
|
// If this is something instantiated in the local crate then we might
|
|
|
|
|
// already have cached the name as a query result.
|
|
|
|
|
if instantiating_crate == LOCAL_CRATE {
|
|
|
|
|
return symbol.symbol_name_for_local_instance(tcx).to_string();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This is something instantiated in an upstream crate, so we have to use
|
|
|
|
|
// the slower (because uncached) version of computing the symbol name.
|
|
|
|
|
match symbol {
|
2020-03-12 18:07:58 -05:00
|
|
|
ExportedSymbol::NonGeneric(def_id) => {
|
|
|
|
|
rustc_symbol_mangling::symbol_name_for_instance_in_crate(
|
|
|
|
|
tcx,
|
|
|
|
|
Instance::mono(tcx, def_id),
|
|
|
|
|
instantiating_crate,
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
ExportedSymbol::Generic(def_id, substs) => {
|
|
|
|
|
rustc_symbol_mangling::symbol_name_for_instance_in_crate(
|
|
|
|
|
tcx,
|
|
|
|
|
Instance::new(def_id, substs),
|
|
|
|
|
instantiating_crate,
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
ExportedSymbol::DropGlue(ty) => rustc_symbol_mangling::symbol_name_for_instance_in_crate(
|
2020-01-22 12:17:21 +01:00
|
|
|
tcx,
|
|
|
|
|
Instance::resolve_drop_in_place(tcx, ty),
|
|
|
|
|
instantiating_crate,
|
|
|
|
|
),
|
2020-01-16 13:21:10 +01:00
|
|
|
ExportedSymbol::NoDefId(symbol_name) => symbol_name.to_string(),
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-29 10:13:50 +02:00
|
|
|
|
|
|
|
|
fn wasm_import_module_map(tcx: TyCtxt<'_>, cnum: CrateNum) -> FxHashMap<DefId, String> {
|
|
|
|
|
// Build up a map from DefId to a `NativeLib` structure, where
|
|
|
|
|
// `NativeLib` internally contains information about
|
|
|
|
|
// `#[link(wasm_import_module = "...")]` for example.
|
|
|
|
|
let native_libs = tcx.native_libraries(cnum);
|
|
|
|
|
|
|
|
|
|
let def_id_to_native_lib = native_libs
|
|
|
|
|
.iter()
|
|
|
|
|
.filter_map(|lib| lib.foreign_module.map(|id| (id, lib)))
|
|
|
|
|
.collect::<FxHashMap<_, _>>();
|
|
|
|
|
|
|
|
|
|
let mut ret = FxHashMap::default();
|
|
|
|
|
for (def_id, lib) in tcx.foreign_modules(cnum).iter() {
|
|
|
|
|
let module = def_id_to_native_lib.get(&def_id).and_then(|s| s.wasm_import_module);
|
|
|
|
|
let module = match module {
|
|
|
|
|
Some(s) => s,
|
|
|
|
|
None => continue,
|
|
|
|
|
};
|
|
|
|
|
ret.extend(lib.foreign_items.iter().map(|id| {
|
|
|
|
|
assert_eq!(id.krate, cnum);
|
|
|
|
|
(*id, module.to_string())
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ret
|
|
|
|
|
}
|