2021-01-08 18:00:25 -05:00
|
|
|
use std::fmt;
|
2024-06-16 21:35:16 -04:00
|
|
|
use std::hash::Hash;
|
2024-07-29 08:13:50 +10:00
|
|
|
|
2025-01-29 21:31:13 -05:00
|
|
|
use rustc_ast::expand::autodiff_attrs::AutoDiffItem;
|
2024-12-13 14:47:11 +01:00
|
|
|
use rustc_attr_parsing::InlineAttr;
|
2024-04-03 14:09:21 -04:00
|
|
|
use rustc_data_structures::base_n::{BaseNString, CASE_INSENSITIVE, ToBaseN};
|
2020-03-24 09:09:42 +01:00
|
|
|
use rustc_data_structures::fingerprint::Fingerprint;
|
2024-02-11 19:50:50 +08:00
|
|
|
use rustc_data_structures::fx::FxIndexMap;
|
2024-06-03 14:36:55 +02:00
|
|
|
use rustc_data_structures::stable_hasher::{Hash128, HashStable, StableHasher, ToStableHashKey};
|
|
|
|
use rustc_data_structures::unord::UnordMap;
|
2021-10-30 10:11:50 -05:00
|
|
|
use rustc_hir::ItemId;
|
2025-01-27 08:11:02 +00:00
|
|
|
use rustc_hir::def_id::{CrateNum, DefId, DefIdSet, LOCAL_CRATE};
|
2023-04-19 10:57:17 +00:00
|
|
|
use rustc_index::Idx;
|
2024-04-29 11:14:55 +10:00
|
|
|
use rustc_macros::{HashStable, TyDecodable, TyEncodable};
|
2022-04-04 22:19:25 +02:00
|
|
|
use rustc_query_system::ich::StableHashingContext;
|
2020-03-11 12:49:08 +01:00
|
|
|
use rustc_session::config::OptLevel;
|
2024-12-13 10:29:23 +11:00
|
|
|
use rustc_span::{Span, Symbol};
|
2024-10-01 08:55:10 +10:00
|
|
|
use rustc_target::spec::SymbolVisibility;
|
2024-05-22 15:20:21 +10:00
|
|
|
use tracing::debug;
|
2017-09-12 11:04:46 -07:00
|
|
|
|
2021-01-08 18:00:25 -05:00
|
|
|
use crate::dep_graph::{DepNode, WorkProduct, WorkProductId};
|
2024-08-08 10:20:40 +02:00
|
|
|
use crate::middle::codegen_fn_attrs::CodegenFnAttrFlags;
|
2024-06-16 21:35:16 -04:00
|
|
|
use crate::ty::{GenericArgs, Instance, InstanceKind, SymbolName, TyCtxt};
|
2024-07-29 08:13:50 +10:00
|
|
|
|
2019-05-24 10:57:30 -05:00
|
|
|
/// Describes how a monomorphization will be instantiated in object files.
|
2019-10-20 15:54:53 +11:00
|
|
|
#[derive(PartialEq)]
|
2019-05-24 10:57:30 -05:00
|
|
|
pub enum InstantiationMode {
|
|
|
|
/// There will be exactly one instance of the given MonoItem. It will have
|
|
|
|
/// external linkage so that it can be linked to from other codegen units.
|
|
|
|
GloballyShared {
|
|
|
|
/// In some compilation scenarios we may decide to take functions that
|
|
|
|
/// are typically `LocalCopy` and instead move them to `GloballyShared`
|
|
|
|
/// to avoid codegenning them a bunch of times. In this situation,
|
|
|
|
/// however, our local copy may conflict with other crates also
|
|
|
|
/// inlining the same function.
|
|
|
|
///
|
|
|
|
/// This flag indicates that this situation is occurring, and informs
|
|
|
|
/// symbol name calculation that some extra mangling is needed to
|
|
|
|
/// avoid conflicts. Note that this may eventually go away entirely if
|
|
|
|
/// ThinLTO enables us to *always* have a globally shared instance of a
|
|
|
|
/// function within one crate's compilation.
|
|
|
|
may_conflict: bool,
|
|
|
|
},
|
|
|
|
|
|
|
|
/// Each codegen unit containing a reference to the given MonoItem will
|
|
|
|
/// have its own private copy of the function (with internal linkage).
|
|
|
|
LocalCopy,
|
|
|
|
}
|
|
|
|
|
2024-10-30 23:10:37 -04:00
|
|
|
#[derive(PartialEq, Eq, Clone, Copy, Debug, Hash, HashStable, TyEncodable, TyDecodable)]
|
2017-10-25 17:04:24 +02:00
|
|
|
pub enum MonoItem<'tcx> {
|
2017-09-12 11:04:46 -07:00
|
|
|
Fn(Instance<'tcx>),
|
2018-02-19 13:29:22 +01:00
|
|
|
Static(DefId),
|
2021-01-30 19:18:48 +01:00
|
|
|
GlobalAsm(ItemId),
|
2017-09-12 11:04:46 -07:00
|
|
|
}
|
|
|
|
|
2018-01-15 18:28:34 +00:00
|
|
|
impl<'tcx> MonoItem<'tcx> {
|
2021-10-01 16:23:07 +00:00
|
|
|
/// Returns `true` if the mono item is user-defined (i.e. not compiler-generated, like shims).
|
|
|
|
pub fn is_user_defined(&self) -> bool {
|
|
|
|
match *self {
|
2024-06-16 21:35:16 -04:00
|
|
|
MonoItem::Fn(instance) => matches!(instance.def, InstanceKind::Item(..)),
|
2021-10-01 16:23:07 +00:00
|
|
|
MonoItem::Static(..) | MonoItem::GlobalAsm(..) => true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-18 09:45:21 +10:00
|
|
|
// Note: if you change how item size estimates work, you might need to
|
|
|
|
// change NON_INCR_MIN_CGU_SIZE as well.
|
2019-06-14 00:48:52 +03:00
|
|
|
pub fn size_estimate(&self, tcx: TyCtxt<'tcx>) -> usize {
|
2018-01-15 18:28:34 +00:00
|
|
|
match *self {
|
2024-10-30 23:10:37 -04:00
|
|
|
MonoItem::Fn(instance) => tcx.size_estimate(instance),
|
2023-07-14 17:38:11 +10:00
|
|
|
// Conservatively estimate the size of a static declaration or
|
|
|
|
// assembly item to be 1.
|
2018-07-13 11:30:47 -07:00
|
|
|
MonoItem::Static(_) | MonoItem::GlobalAsm(_) => 1,
|
2018-01-15 18:28:34 +00:00
|
|
|
}
|
|
|
|
}
|
2019-05-24 10:57:30 -05:00
|
|
|
|
2024-10-26 10:18:42 +08:00
|
|
|
pub fn is_generic_fn(&self) -> bool {
|
2023-09-13 13:55:23 +00:00
|
|
|
match self {
|
2024-10-26 10:18:42 +08:00
|
|
|
MonoItem::Fn(instance) => instance.args.non_erasable_generics().next().is_some(),
|
2019-05-24 10:57:30 -05:00
|
|
|
MonoItem::Static(..) | MonoItem::GlobalAsm(..) => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-10 15:45:05 +10:00
|
|
|
pub fn symbol_name(&self, tcx: TyCtxt<'tcx>) -> SymbolName<'tcx> {
|
2019-05-24 10:57:30 -05:00
|
|
|
match *self {
|
|
|
|
MonoItem::Fn(instance) => tcx.symbol_name(instance),
|
|
|
|
MonoItem::Static(def_id) => tcx.symbol_name(Instance::mono(tcx, def_id)),
|
2021-01-30 19:18:48 +01:00
|
|
|
MonoItem::GlobalAsm(item_id) => {
|
2022-10-27 14:02:18 +11:00
|
|
|
SymbolName::new(tcx, &format!("global_asm_{:?}", item_id.owner_id))
|
2019-05-24 10:57:30 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-14 00:48:52 +03:00
|
|
|
pub fn instantiation_mode(&self, tcx: TyCtxt<'tcx>) -> InstantiationMode {
|
2025-02-01 12:43:39 -05:00
|
|
|
// The case handling here is written in the same style as cross_crate_inlinable, we first
|
|
|
|
// handle the cases where we must use a particular instantiation mode, then cascade down
|
|
|
|
// through a sequence of heuristics.
|
2021-01-11 16:27:59 -03:00
|
|
|
|
2025-02-01 12:43:39 -05:00
|
|
|
// The first thing we do is detect MonoItems which we must instantiate exactly once in the
|
|
|
|
// whole program.
|
|
|
|
|
|
|
|
// Statics and global_asm! must be instantiated exactly once.
|
|
|
|
let instance = match *self {
|
|
|
|
MonoItem::Fn(instance) => instance,
|
2019-05-24 10:57:30 -05:00
|
|
|
MonoItem::Static(..) | MonoItem::GlobalAsm(..) => {
|
2025-02-01 12:43:39 -05:00
|
|
|
return InstantiationMode::GloballyShared { may_conflict: false };
|
2019-05-24 10:57:30 -05:00
|
|
|
}
|
2025-02-01 12:43:39 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
// Similarly, the executable entrypoint must be instantiated exactly once.
|
|
|
|
if let Some((entry_def_id, _)) = tcx.entry_fn(()) {
|
|
|
|
if instance.def_id() == entry_def_id {
|
|
|
|
return InstantiationMode::GloballyShared { may_conflict: false };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the function is #[naked] or contains any other attribute that requires exactly-once
|
|
|
|
// instantiation:
|
|
|
|
let codegen_fn_attrs = tcx.codegen_fn_attrs(instance.def_id());
|
|
|
|
if codegen_fn_attrs.contains_extern_indicator()
|
|
|
|
|| codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::NAKED)
|
|
|
|
{
|
|
|
|
return InstantiationMode::GloballyShared { may_conflict: false };
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: The logic for which functions are permitted to get LocalCopy is actually spread
|
|
|
|
// across 4 functions:
|
|
|
|
// * cross_crate_inlinable(def_id)
|
|
|
|
// * InstanceKind::requires_inline
|
|
|
|
// * InstanceKind::generate_cgu_internal_copy
|
|
|
|
// * MonoItem::instantiation_mode
|
|
|
|
// Since reachable_non_generics calls InstanceKind::generates_cgu_internal_copy to decide
|
|
|
|
// which symbols this crate exports, we are obligated to only generate LocalCopy when
|
|
|
|
// generates_cgu_internal_copy returns true.
|
|
|
|
if !instance.def.generates_cgu_internal_copy(tcx) {
|
|
|
|
return InstantiationMode::GloballyShared { may_conflict: false };
|
|
|
|
}
|
|
|
|
|
|
|
|
// Beginning of heuristics. The handling of link-dead-code and inline(always) are QoL only,
|
|
|
|
// the compiler should not crash and linkage should work, but codegen may be undesirable.
|
|
|
|
|
|
|
|
// -Clink-dead-code was given an unfortunate name; the point of the flag is to assist
|
|
|
|
// coverage tools which rely on having every function in the program appear in the
|
|
|
|
// generated code. If we select LocalCopy, functions which are not used because they are
|
|
|
|
// missing test coverage will disappear from such coverage reports, defeating the point.
|
|
|
|
// Note that -Cinstrument-coverage does not require such assistance from us, only coverage
|
|
|
|
// tools implemented without compiler support ironically require a special compiler flag.
|
|
|
|
if tcx.sess.link_dead_code() {
|
|
|
|
return InstantiationMode::GloballyShared { may_conflict: true };
|
|
|
|
}
|
|
|
|
|
|
|
|
// To ensure that #[inline(always)] can be inlined as much as possible, especially in unoptimized
|
|
|
|
// builds, we always select LocalCopy.
|
|
|
|
if codegen_fn_attrs.inline.always() {
|
|
|
|
return InstantiationMode::LocalCopy;
|
|
|
|
}
|
|
|
|
|
|
|
|
// #[inline(never)] functions in general are poor candidates for inlining and thus since
|
|
|
|
// LocalCopy generally increases code size for the benefit of optimizations from inlining,
|
|
|
|
// we want to give them GloballyShared codegen.
|
|
|
|
// The slight problem is that generic functions need to always support cross-crate
|
|
|
|
// compilation, so all previous stages of the compiler are obligated to treat generic
|
|
|
|
// functions the same as those that unconditionally get LocalCopy codegen. It's only when
|
|
|
|
// we get here that we can at least not codegen a #[inline(never)] generic function in all
|
|
|
|
// of our CGUs.
|
|
|
|
if let InlineAttr::Never = tcx.codegen_fn_attrs(instance.def_id()).inline
|
|
|
|
&& self.is_generic_fn()
|
|
|
|
{
|
|
|
|
return InstantiationMode::GloballyShared { may_conflict: true };
|
|
|
|
}
|
|
|
|
|
|
|
|
// The fallthrough case is to generate LocalCopy for all optimized builds, and
|
|
|
|
// GloballyShared with conflict prevention when optimizations are disabled.
|
|
|
|
match tcx.sess.opts.optimize {
|
|
|
|
OptLevel::No => InstantiationMode::GloballyShared { may_conflict: true },
|
|
|
|
_ => InstantiationMode::LocalCopy,
|
2019-05-24 10:57:30 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-14 00:48:52 +03:00
|
|
|
pub fn explicit_linkage(&self, tcx: TyCtxt<'tcx>) -> Option<Linkage> {
|
2019-05-24 10:57:30 -05:00
|
|
|
let def_id = match *self {
|
|
|
|
MonoItem::Fn(ref instance) => instance.def_id(),
|
|
|
|
MonoItem::Static(def_id) => def_id,
|
|
|
|
MonoItem::GlobalAsm(..) => return None,
|
|
|
|
};
|
|
|
|
|
|
|
|
let codegen_fn_attrs = tcx.codegen_fn_attrs(def_id);
|
|
|
|
codegen_fn_attrs.linkage
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns `true` if this instance is instantiable - whether it has no unsatisfied
|
|
|
|
/// predicates.
|
|
|
|
///
|
|
|
|
/// In order to codegen an item, all of its predicates must hold, because
|
|
|
|
/// otherwise the item does not make sense. Type-checking ensures that
|
|
|
|
/// the predicates of every item that is *used by* a valid item *do*
|
|
|
|
/// hold, so we can rely on that.
|
|
|
|
///
|
|
|
|
/// However, we codegen collector roots (reachable items) and functions
|
|
|
|
/// in vtables when they are seen, even if they are not used, and so they
|
|
|
|
/// might not be instantiable. For example, a programmer can define this
|
|
|
|
/// public function:
|
|
|
|
///
|
|
|
|
/// pub fn foo<'a>(s: &'a mut ()) where &'a mut (): Clone {
|
|
|
|
/// <&mut () as Clone>::clone(&s);
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// That function can't be codegened, because the method `<&mut () as Clone>::clone`
|
|
|
|
/// does not exist. Luckily for us, that function can't ever be used,
|
|
|
|
/// because that would require for `&'a mut (): Clone` to hold, so we
|
|
|
|
/// can just not emit any code, or even a linker reference for it.
|
|
|
|
///
|
|
|
|
/// Similarly, if a vtable method has such a signature, and therefore can't
|
|
|
|
/// be used, we can just not emit it and have a placeholder (a null pointer,
|
|
|
|
/// which will never be accessed) in its place.
|
2019-06-14 00:48:52 +03:00
|
|
|
pub fn is_instantiable(&self, tcx: TyCtxt<'tcx>) -> bool {
|
2019-05-24 10:57:30 -05:00
|
|
|
debug!("is_instantiable({:?})", self);
|
2023-07-11 22:35:29 +01:00
|
|
|
let (def_id, args) = match *self {
|
|
|
|
MonoItem::Fn(ref instance) => (instance.def_id(), instance.args),
|
|
|
|
MonoItem::Static(def_id) => (def_id, GenericArgs::empty()),
|
2019-05-24 10:57:30 -05:00
|
|
|
// global asm never has predicates
|
|
|
|
MonoItem::GlobalAsm(..) => return true,
|
|
|
|
};
|
|
|
|
|
2024-02-12 15:39:32 +09:00
|
|
|
!tcx.instantiate_and_check_impossible_predicates((def_id, &args))
|
2019-05-24 10:57:30 -05:00
|
|
|
}
|
|
|
|
|
2019-06-14 00:48:52 +03:00
|
|
|
pub fn local_span(&self, tcx: TyCtxt<'tcx>) -> Option<Span> {
|
2019-05-24 10:57:30 -05:00
|
|
|
match *self {
|
2021-10-20 20:59:15 +02:00
|
|
|
MonoItem::Fn(Instance { def, .. }) => def.def_id().as_local(),
|
|
|
|
MonoItem::Static(def_id) => def_id.as_local(),
|
2022-10-27 14:02:18 +11:00
|
|
|
MonoItem::GlobalAsm(item_id) => Some(item_id.owner_id.def_id),
|
2019-05-24 10:57:30 -05:00
|
|
|
}
|
2021-10-20 20:59:15 +02:00
|
|
|
.map(|def_id| tcx.def_span(def_id))
|
2019-05-24 10:57:30 -05:00
|
|
|
}
|
2021-04-12 13:58:12 +02:00
|
|
|
|
2021-04-16 20:55:51 +02:00
|
|
|
// Only used by rustc_codegen_cranelift
|
2021-04-12 13:58:12 +02:00
|
|
|
pub fn codegen_dep_node(&self, tcx: TyCtxt<'tcx>) -> DepNode {
|
|
|
|
crate::dep_graph::make_compile_mono_item(tcx, self)
|
|
|
|
}
|
2021-05-16 12:34:42 +02:00
|
|
|
|
|
|
|
/// Returns the item's `CrateNum`
|
|
|
|
pub fn krate(&self) -> CrateNum {
|
|
|
|
match self {
|
|
|
|
MonoItem::Fn(ref instance) => instance.def_id().krate,
|
|
|
|
MonoItem::Static(def_id) => def_id.krate,
|
|
|
|
MonoItem::GlobalAsm(..) => LOCAL_CRATE,
|
|
|
|
}
|
|
|
|
}
|
2022-12-08 19:20:16 +00:00
|
|
|
|
|
|
|
/// Returns the item's `DefId`
|
|
|
|
pub fn def_id(&self) -> DefId {
|
|
|
|
match *self {
|
|
|
|
MonoItem::Fn(Instance { def, .. }) => def.def_id(),
|
|
|
|
MonoItem::Static(def_id) => def_id,
|
|
|
|
MonoItem::GlobalAsm(item_id) => item_id.owner_id.to_def_id(),
|
|
|
|
}
|
|
|
|
}
|
2018-01-15 18:28:34 +00:00
|
|
|
}
|
|
|
|
|
2020-08-28 14:31:03 +01:00
|
|
|
impl<'tcx> fmt::Display for MonoItem<'tcx> {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
match *self {
|
2023-07-25 22:00:13 +02:00
|
|
|
MonoItem::Fn(instance) => write!(f, "fn {instance}"),
|
2020-08-28 14:31:03 +01:00
|
|
|
MonoItem::Static(def_id) => {
|
2023-07-11 22:35:29 +01:00
|
|
|
write!(f, "static {}", Instance::new(def_id, GenericArgs::empty()))
|
2020-08-28 14:31:03 +01:00
|
|
|
}
|
|
|
|
MonoItem::GlobalAsm(..) => write!(f, "global_asm"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-03 14:36:55 +02:00
|
|
|
impl ToStableHashKey<StableHashingContext<'_>> for MonoItem<'_> {
|
|
|
|
type KeyType = Fingerprint;
|
|
|
|
|
|
|
|
fn to_stable_hash_key(&self, hcx: &StableHashingContext<'_>) -> Self::KeyType {
|
|
|
|
let mut hasher = StableHasher::new();
|
|
|
|
self.hash_stable(&mut hcx.clone(), &mut hasher);
|
|
|
|
hasher.finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-01-27 08:11:02 +00:00
|
|
|
#[derive(Debug, HashStable, Copy, Clone)]
|
|
|
|
pub struct MonoItemPartitions<'tcx> {
|
|
|
|
pub codegen_units: &'tcx [CodegenUnit<'tcx>],
|
|
|
|
pub all_mono_items: &'tcx DefIdSet,
|
2025-01-29 21:31:13 -05:00
|
|
|
pub autodiff_items: &'tcx [AutoDiffItem],
|
2025-01-27 08:11:02 +00:00
|
|
|
}
|
|
|
|
|
2024-06-03 14:36:55 +02:00
|
|
|
#[derive(Debug, HashStable)]
|
2017-09-12 11:04:46 -07:00
|
|
|
pub struct CodegenUnit<'tcx> {
|
|
|
|
/// A name for this CGU. Incremental compilation requires that
|
2019-02-08 14:53:55 +01:00
|
|
|
/// name be unique amongst **all** crates. Therefore, it should
|
2017-09-12 11:04:46 -07:00
|
|
|
/// contain something unique to this crate (e.g., a module path)
|
|
|
|
/// as well as the crate name and disambiguator.
|
2019-10-21 17:14:03 +11:00
|
|
|
name: Symbol,
|
2024-02-11 19:50:50 +08:00
|
|
|
items: FxIndexMap<MonoItem<'tcx>, MonoItemData>,
|
2023-06-21 16:38:47 +10:00
|
|
|
size_estimate: usize,
|
2021-04-24 13:16:34 +08:00
|
|
|
primary: bool,
|
2021-12-20 16:36:56 -05:00
|
|
|
/// True if this is CGU is used to hold code coverage information for dead code,
|
|
|
|
/// false otherwise.
|
|
|
|
is_code_coverage_dead_code_cgu: bool,
|
2017-09-12 11:04:46 -07:00
|
|
|
}
|
|
|
|
|
2023-07-14 16:32:10 +10:00
|
|
|
/// Auxiliary info about a `MonoItem`.
|
|
|
|
#[derive(Copy, Clone, PartialEq, Debug, HashStable)]
|
|
|
|
pub struct MonoItemData {
|
2023-07-17 13:04:07 +10:00
|
|
|
/// A cached copy of the result of `MonoItem::instantiation_mode`, where
|
|
|
|
/// `GloballyShared` maps to `false` and `LocalCopy` maps to `true`.
|
|
|
|
pub inlined: bool,
|
|
|
|
|
2023-07-14 16:32:10 +10:00
|
|
|
pub linkage: Linkage,
|
|
|
|
pub visibility: Visibility,
|
2023-07-17 13:04:07 +10:00
|
|
|
|
|
|
|
/// A cached copy of the result of `MonoItem::size_estimate`.
|
2023-07-14 16:02:32 +10:00
|
|
|
pub size_estimate: usize,
|
2023-07-14 16:32:10 +10:00
|
|
|
}
|
|
|
|
|
2020-05-12 08:11:58 -04:00
|
|
|
/// Specifies the linkage type for a `MonoItem`.
|
|
|
|
///
|
2020-11-05 14:33:23 +01:00
|
|
|
/// See <https://llvm.org/docs/LangRef.html#linkage-types> for more details about these variants.
|
2020-06-29 21:09:54 +01:00
|
|
|
#[derive(Copy, Clone, PartialEq, Debug, TyEncodable, TyDecodable, HashStable)]
|
2017-09-12 11:04:46 -07:00
|
|
|
pub enum Linkage {
|
|
|
|
External,
|
|
|
|
AvailableExternally,
|
|
|
|
LinkOnceAny,
|
|
|
|
LinkOnceODR,
|
|
|
|
WeakAny,
|
|
|
|
WeakODR,
|
|
|
|
Appending,
|
|
|
|
Internal,
|
|
|
|
Private,
|
|
|
|
ExternalWeak,
|
|
|
|
Common,
|
|
|
|
}
|
|
|
|
|
2024-12-13 16:39:11 +01:00
|
|
|
/// Specifies the symbol visibility with regards to dynamic linking.
|
|
|
|
///
|
|
|
|
/// Visibility doesn't have any effect when linkage is internal.
|
|
|
|
///
|
|
|
|
/// DSO means dynamic shared object, that is a dynamically linked executable or dylib.
|
2019-11-09 23:28:07 +01:00
|
|
|
#[derive(Copy, Clone, PartialEq, Debug, HashStable)]
|
2017-09-12 11:04:46 -07:00
|
|
|
pub enum Visibility {
|
2024-12-13 16:39:11 +01:00
|
|
|
/// Export the symbol from the DSO and apply overrides of the symbol by outside DSOs to within
|
|
|
|
/// the DSO if the object file format supports this.
|
2017-09-12 11:04:46 -07:00
|
|
|
Default,
|
2024-12-13 16:39:11 +01:00
|
|
|
/// Hide the symbol outside of the defining DSO even when external linkage is used to export it
|
|
|
|
/// from the object file.
|
2017-09-12 11:04:46 -07:00
|
|
|
Hidden,
|
2024-12-13 16:39:11 +01:00
|
|
|
/// Export the symbol from the DSO, but don't apply overrides of the symbol by outside DSOs to
|
|
|
|
/// within the DSO. Equivalent to default visibility with object file formats that don't support
|
|
|
|
/// overriding exported symbols by another DSO.
|
2017-09-12 11:04:46 -07:00
|
|
|
Protected,
|
|
|
|
}
|
|
|
|
|
2024-10-01 08:55:10 +10:00
|
|
|
impl From<SymbolVisibility> for Visibility {
|
|
|
|
fn from(value: SymbolVisibility) -> Self {
|
|
|
|
match value {
|
|
|
|
SymbolVisibility::Hidden => Visibility::Hidden,
|
|
|
|
SymbolVisibility::Protected => Visibility::Protected,
|
|
|
|
SymbolVisibility::Interposable => Visibility::Default,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-12 11:04:46 -07:00
|
|
|
impl<'tcx> CodegenUnit<'tcx> {
|
2021-06-01 00:00:00 +00:00
|
|
|
#[inline]
|
2019-10-21 17:14:03 +11:00
|
|
|
pub fn new(name: Symbol) -> CodegenUnit<'tcx> {
|
2021-12-20 16:36:56 -05:00
|
|
|
CodegenUnit {
|
|
|
|
name,
|
|
|
|
items: Default::default(),
|
2023-06-21 16:38:47 +10:00
|
|
|
size_estimate: 0,
|
2021-12-20 16:36:56 -05:00
|
|
|
primary: false,
|
|
|
|
is_code_coverage_dead_code_cgu: false,
|
|
|
|
}
|
2017-09-12 11:04:46 -07:00
|
|
|
}
|
|
|
|
|
2019-10-21 17:14:03 +11:00
|
|
|
pub fn name(&self) -> Symbol {
|
|
|
|
self.name
|
2017-09-12 11:04:46 -07:00
|
|
|
}
|
|
|
|
|
2019-10-21 17:14:03 +11:00
|
|
|
pub fn set_name(&mut self, name: Symbol) {
|
2017-09-12 11:04:46 -07:00
|
|
|
self.name = name;
|
|
|
|
}
|
|
|
|
|
2021-04-24 13:16:34 +08:00
|
|
|
pub fn is_primary(&self) -> bool {
|
|
|
|
self.primary
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn make_primary(&mut self) {
|
|
|
|
self.primary = true;
|
|
|
|
}
|
|
|
|
|
2024-02-11 19:50:50 +08:00
|
|
|
pub fn items(&self) -> &FxIndexMap<MonoItem<'tcx>, MonoItemData> {
|
2017-09-12 11:04:46 -07:00
|
|
|
&self.items
|
|
|
|
}
|
|
|
|
|
2024-02-11 19:50:50 +08:00
|
|
|
pub fn items_mut(&mut self) -> &mut FxIndexMap<MonoItem<'tcx>, MonoItemData> {
|
2017-09-12 11:04:46 -07:00
|
|
|
&mut self.items
|
|
|
|
}
|
2018-01-08 12:30:52 +01:00
|
|
|
|
2021-12-20 16:36:56 -05:00
|
|
|
pub fn is_code_coverage_dead_code_cgu(&self) -> bool {
|
|
|
|
self.is_code_coverage_dead_code_cgu
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Marks this CGU as the one used to contain code coverage information for dead code.
|
|
|
|
pub fn make_code_coverage_dead_code_cgu(&mut self) {
|
|
|
|
self.is_code_coverage_dead_code_cgu = true;
|
|
|
|
}
|
|
|
|
|
2024-04-03 14:09:21 -04:00
|
|
|
pub fn mangle_name(human_readable_name: &str) -> BaseNString {
|
2018-01-08 12:30:52 +01:00
|
|
|
let mut hasher = StableHasher::new();
|
|
|
|
human_readable_name.hash(&mut hasher);
|
2023-04-07 23:11:20 -04:00
|
|
|
let hash: Hash128 = hasher.finish();
|
2024-04-03 14:09:21 -04:00
|
|
|
hash.as_u128().to_base_fixed_len(CASE_INSENSITIVE)
|
2018-01-08 12:30:52 +01:00
|
|
|
}
|
2018-01-15 18:28:34 +00:00
|
|
|
|
2023-07-14 16:02:32 +10:00
|
|
|
pub fn compute_size_estimate(&mut self) {
|
|
|
|
// The size of a codegen unit as the sum of the sizes of the items
|
|
|
|
// within it.
|
|
|
|
self.size_estimate = self.items.values().map(|data| data.size_estimate).sum();
|
2018-01-15 18:28:34 +00:00
|
|
|
}
|
|
|
|
|
2023-06-21 16:38:47 +10:00
|
|
|
/// Should only be called if [`compute_size_estimate`] has previously been called.
|
2023-01-30 07:20:38 +02:00
|
|
|
///
|
2023-06-21 16:38:47 +10:00
|
|
|
/// [`compute_size_estimate`]: Self::compute_size_estimate
|
2023-07-14 16:02:32 +10:00
|
|
|
#[inline]
|
2018-01-15 18:28:34 +00:00
|
|
|
pub fn size_estimate(&self) -> usize {
|
2023-06-21 16:38:47 +10:00
|
|
|
// Items are never zero-sized, so if we have items the estimate must be
|
|
|
|
// non-zero, unless we forgot to call `compute_size_estimate` first.
|
|
|
|
assert!(self.items.is_empty() || self.size_estimate != 0);
|
2023-01-30 07:20:38 +02:00
|
|
|
self.size_estimate
|
2018-01-15 18:28:34 +00:00
|
|
|
}
|
|
|
|
|
2019-05-24 11:19:53 -05:00
|
|
|
pub fn contains_item(&self, item: &MonoItem<'tcx>) -> bool {
|
|
|
|
self.items().contains_key(item)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn work_product_id(&self) -> WorkProductId {
|
2021-12-15 14:39:23 +11:00
|
|
|
WorkProductId::from_cgu_name(self.name().as_str())
|
2019-05-24 11:19:53 -05:00
|
|
|
}
|
|
|
|
|
2022-05-13 10:32:03 +00:00
|
|
|
pub fn previous_work_product(&self, tcx: TyCtxt<'_>) -> WorkProduct {
|
2019-05-24 11:19:53 -05:00
|
|
|
let work_product_id = self.work_product_id();
|
|
|
|
tcx.dep_graph
|
|
|
|
.previous_work_product(&work_product_id)
|
|
|
|
.unwrap_or_else(|| panic!("Could not find work-product for CGU `{}`", self.name()))
|
|
|
|
}
|
|
|
|
|
2019-06-12 00:11:55 +03:00
|
|
|
pub fn items_in_deterministic_order(
|
|
|
|
&self,
|
2019-06-14 00:48:52 +03:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2023-07-14 16:32:10 +10:00
|
|
|
) -> Vec<(MonoItem<'tcx>, MonoItemData)> {
|
2019-05-24 11:19:53 -05:00
|
|
|
// The codegen tests rely on items being process in the same order as
|
|
|
|
// they appear in the file, so for local items, we sort by node_id first
|
|
|
|
#[derive(PartialEq, Eq, PartialOrd, Ord)]
|
2024-08-29 09:59:05 +10:00
|
|
|
struct ItemSortKey<'tcx>(Option<usize>, SymbolName<'tcx>);
|
2019-05-24 11:19:53 -05:00
|
|
|
|
2020-07-10 15:45:05 +10:00
|
|
|
fn item_sort_key<'tcx>(tcx: TyCtxt<'tcx>, item: MonoItem<'tcx>) -> ItemSortKey<'tcx> {
|
2019-05-24 11:19:53 -05:00
|
|
|
ItemSortKey(
|
|
|
|
match item {
|
|
|
|
MonoItem::Fn(ref instance) => {
|
|
|
|
match instance.def {
|
|
|
|
// We only want to take HirIds of user-defined
|
|
|
|
// instances into account. The others don't matter for
|
|
|
|
// the codegen tests and can even make item order
|
|
|
|
// unstable.
|
2024-06-16 21:35:16 -04:00
|
|
|
InstanceKind::Item(def) => def.as_local().map(Idx::index),
|
|
|
|
InstanceKind::VTableShim(..)
|
|
|
|
| InstanceKind::ReifyShim(..)
|
|
|
|
| InstanceKind::Intrinsic(..)
|
|
|
|
| InstanceKind::FnPtrShim(..)
|
|
|
|
| InstanceKind::Virtual(..)
|
|
|
|
| InstanceKind::ClosureOnceShim { .. }
|
|
|
|
| InstanceKind::ConstructCoroutineInClosureShim { .. }
|
|
|
|
| InstanceKind::DropGlue(..)
|
|
|
|
| InstanceKind::CloneShim(..)
|
|
|
|
| InstanceKind::ThreadLocalShim(..)
|
|
|
|
| InstanceKind::FnPtrAddrShim(..)
|
|
|
|
| InstanceKind::AsyncDropGlueCtorShim(..) => None,
|
2019-05-24 11:19:53 -05:00
|
|
|
}
|
|
|
|
}
|
2022-03-01 11:42:10 -08:00
|
|
|
MonoItem::Static(def_id) => def_id.as_local().map(Idx::index),
|
2022-10-27 14:02:18 +11:00
|
|
|
MonoItem::GlobalAsm(item_id) => Some(item_id.owner_id.def_id.index()),
|
2019-05-24 11:19:53 -05:00
|
|
|
},
|
|
|
|
item.symbol_name(tcx),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-07-14 16:32:10 +10:00
|
|
|
let mut items: Vec<_> = self.items().iter().map(|(&i, &data)| (i, data)).collect();
|
2019-05-24 11:19:53 -05:00
|
|
|
items.sort_by_cached_key(|&(i, _)| item_sort_key(tcx, i));
|
|
|
|
items
|
|
|
|
}
|
|
|
|
|
2019-06-14 00:48:52 +03:00
|
|
|
pub fn codegen_dep_node(&self, tcx: TyCtxt<'tcx>) -> DepNode {
|
2021-01-08 18:00:25 -05:00
|
|
|
crate::dep_graph::make_compile_codegen_unit(tcx, self.name())
|
2019-05-24 11:19:53 -05:00
|
|
|
}
|
2017-09-12 11:04:46 -07:00
|
|
|
}
|
2017-09-13 20:26:39 -07:00
|
|
|
|
2024-06-03 14:36:55 +02:00
|
|
|
impl ToStableHashKey<StableHashingContext<'_>> for CodegenUnit<'_> {
|
|
|
|
type KeyType = String;
|
|
|
|
|
|
|
|
fn to_stable_hash_key(&self, _: &StableHashingContext<'_>) -> Self::KeyType {
|
|
|
|
// Codegen unit names are conceptually required to be stable across
|
|
|
|
// compilation session so that object file names match up.
|
|
|
|
self.name.to_string()
|
2017-09-18 12:14:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-14 00:48:52 +03:00
|
|
|
pub struct CodegenUnitNameBuilder<'tcx> {
|
|
|
|
tcx: TyCtxt<'tcx>,
|
2024-06-03 14:36:55 +02:00
|
|
|
cache: UnordMap<CrateNum, String>,
|
2018-08-14 17:55:22 +02:00
|
|
|
}
|
|
|
|
|
2021-12-15 19:32:30 -05:00
|
|
|
impl<'tcx> CodegenUnitNameBuilder<'tcx> {
|
2019-06-14 00:48:52 +03:00
|
|
|
pub fn new(tcx: TyCtxt<'tcx>) -> Self {
|
2018-10-16 16:57:53 +02:00
|
|
|
CodegenUnitNameBuilder { tcx, cache: Default::default() }
|
2018-08-14 17:55:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// CGU names should fulfill the following requirements:
|
|
|
|
/// - They should be able to act as a file name on any kind of file system
|
|
|
|
/// - They should not collide with other CGU names, even for different versions
|
|
|
|
/// of the same crate.
|
|
|
|
///
|
|
|
|
/// Consequently, we don't use special characters except for '.' and '-' and we
|
|
|
|
/// prefix each name with the crate-name and crate-disambiguator.
|
|
|
|
///
|
|
|
|
/// This function will build CGU names of the form:
|
|
|
|
///
|
2022-04-15 15:04:34 -07:00
|
|
|
/// ```text
|
2018-09-12 12:46:48 +02:00
|
|
|
/// <crate-name>.<crate-disambiguator>[-in-<local-crate-id>](-<component>)*[.<special-suffix>]
|
|
|
|
/// <local-crate-id> = <local-crate-name>.<local-crate-disambiguator>
|
2018-08-14 17:55:22 +02:00
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// The '.' before `<special-suffix>` makes sure that names with a special
|
|
|
|
/// suffix can never collide with a name built out of regular Rust
|
2018-11-27 02:59:49 +00:00
|
|
|
/// identifiers (e.g., module paths).
|
2018-08-14 17:55:22 +02:00
|
|
|
pub fn build_cgu_name<I, C, S>(
|
|
|
|
&mut self,
|
|
|
|
cnum: CrateNum,
|
|
|
|
components: I,
|
|
|
|
special_suffix: Option<S>,
|
2019-10-21 17:14:03 +11:00
|
|
|
) -> Symbol
|
2018-08-14 17:55:22 +02:00
|
|
|
where
|
|
|
|
I: IntoIterator<Item = C>,
|
|
|
|
C: fmt::Display,
|
|
|
|
S: fmt::Display,
|
|
|
|
{
|
|
|
|
let cgu_name = self.build_cgu_name_no_mangle(cnum, components, special_suffix);
|
|
|
|
|
2022-07-06 07:44:47 -05:00
|
|
|
if self.tcx.sess.opts.unstable_opts.human_readable_cgu_names {
|
2018-08-14 17:55:22 +02:00
|
|
|
cgu_name
|
|
|
|
} else {
|
2021-12-15 14:39:23 +11:00
|
|
|
Symbol::intern(&CodegenUnit::mangle_name(cgu_name.as_str()))
|
2018-08-14 17:55:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Same as `CodegenUnit::build_cgu_name()` but will never mangle the
|
|
|
|
/// resulting name.
|
|
|
|
pub fn build_cgu_name_no_mangle<I, C, S>(
|
|
|
|
&mut self,
|
|
|
|
cnum: CrateNum,
|
|
|
|
components: I,
|
|
|
|
special_suffix: Option<S>,
|
2019-10-21 17:14:03 +11:00
|
|
|
) -> Symbol
|
2018-08-14 17:55:22 +02:00
|
|
|
where
|
|
|
|
I: IntoIterator<Item = C>,
|
|
|
|
C: fmt::Display,
|
|
|
|
S: fmt::Display,
|
|
|
|
{
|
|
|
|
use std::fmt::Write;
|
|
|
|
|
|
|
|
let mut cgu_name = String::with_capacity(64);
|
|
|
|
|
|
|
|
// Start out with the crate name and disambiguator
|
|
|
|
let tcx = self.tcx;
|
|
|
|
let crate_prefix = self.cache.entry(cnum).or_insert_with(|| {
|
2018-09-12 12:46:48 +02:00
|
|
|
// Whenever the cnum is not LOCAL_CRATE we also mix in the
|
|
|
|
// local crate's ID. Otherwise there can be collisions between CGUs
|
|
|
|
// instantiating stuff for upstream crates.
|
|
|
|
let local_crate_id = if cnum != LOCAL_CRATE {
|
2023-08-08 20:08:24 +08:00
|
|
|
let local_stable_crate_id = tcx.stable_crate_id(LOCAL_CRATE);
|
2023-04-07 23:11:20 -04:00
|
|
|
format!("-in-{}.{:08x}", tcx.crate_name(LOCAL_CRATE), local_stable_crate_id)
|
2018-09-12 12:46:48 +02:00
|
|
|
} else {
|
|
|
|
String::new()
|
|
|
|
};
|
|
|
|
|
2023-08-08 20:08:24 +08:00
|
|
|
let stable_crate_id = tcx.stable_crate_id(LOCAL_CRATE);
|
2023-04-07 23:11:20 -04:00
|
|
|
format!("{}.{:08x}{}", tcx.crate_name(cnum), stable_crate_id, local_crate_id)
|
2018-08-14 17:55:22 +02:00
|
|
|
});
|
|
|
|
|
2023-07-25 22:00:13 +02:00
|
|
|
write!(cgu_name, "{crate_prefix}").unwrap();
|
2018-08-14 17:55:22 +02:00
|
|
|
|
|
|
|
// Add the components
|
|
|
|
for component in components {
|
2023-07-25 22:00:13 +02:00
|
|
|
write!(cgu_name, "-{component}").unwrap();
|
2018-08-14 17:55:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(special_suffix) = special_suffix {
|
|
|
|
// We add a dot in here so it cannot clash with anything in a regular
|
|
|
|
// Rust identifier
|
2023-07-25 22:00:13 +02:00
|
|
|
write!(cgu_name, ".{special_suffix}").unwrap();
|
2018-08-14 17:55:22 +02:00
|
|
|
}
|
|
|
|
|
2021-12-03 03:06:36 +01:00
|
|
|
Symbol::intern(&cgu_name)
|
2018-08-14 17:55:22 +02:00
|
|
|
}
|
|
|
|
}
|
2024-10-30 23:10:37 -04:00
|
|
|
|
|
|
|
/// See module-level docs of `rustc_monomorphize::collector` on some context for "mentioned" items.
|
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, HashStable)]
|
|
|
|
pub enum CollectionMode {
|
|
|
|
/// Collect items that are used, i.e., actually needed for codegen.
|
|
|
|
///
|
|
|
|
/// Which items are used can depend on optimization levels, as MIR optimizations can remove
|
|
|
|
/// uses.
|
|
|
|
UsedItems,
|
|
|
|
/// Collect items that are mentioned. The goal of this mode is that it is independent of
|
|
|
|
/// optimizations: the set of "mentioned" items is computed before optimizations are run.
|
|
|
|
///
|
|
|
|
/// The exact contents of this set are *not* a stable guarantee. (For instance, it is currently
|
|
|
|
/// computed after drop-elaboration. If we ever do some optimizations even in debug builds, we
|
|
|
|
/// might decide to run them before computing mentioned items.) The key property of this set is
|
|
|
|
/// that it is optimization-independent.
|
|
|
|
MentionedItems,
|
|
|
|
}
|