rustc_middle/mir/mono.rs
1use std::borrow::Cow;
2use std::fmt;
3use std::hash::Hash;
4
5use rustc_data_structures::base_n::{BaseNString, CASE_INSENSITIVE, ToBaseN};
6use rustc_data_structures::fingerprint::Fingerprint;
7use rustc_data_structures::fx::FxIndexMap;
8use rustc_data_structures::stable_hasher::{HashStable, StableHasher, ToStableHashKey};
9use rustc_data_structures::unord::UnordMap;
10use rustc_hashes::Hash128;
11use rustc_hir::ItemId;
12use rustc_hir::attrs::{InlineAttr, Linkage};
13use rustc_hir::def_id::{CrateNum, DefId, DefIdSet, LOCAL_CRATE};
14use rustc_macros::{HashStable, TyDecodable, TyEncodable};
15use rustc_query_system::ich::StableHashingContext;
16use rustc_session::config::OptLevel;
17use rustc_span::{Span, Symbol};
18use rustc_target::spec::SymbolVisibility;
19use tracing::debug;
20
21use crate::dep_graph::{DepNode, WorkProduct, WorkProductId};
22use crate::middle::codegen_fn_attrs::CodegenFnAttrFlags;
23use crate::ty::{self, GenericArgs, Instance, InstanceKind, SymbolName, Ty, TyCtxt};
24
25/// Describes how a monomorphization will be instantiated in object files.
26#[derive(PartialEq)]
27pub enum InstantiationMode {
28 /// There will be exactly one instance of the given MonoItem. It will have
29 /// external linkage so that it can be linked to from other codegen units.
30 GloballyShared {
31 /// In some compilation scenarios we may decide to take functions that
32 /// are typically `LocalCopy` and instead move them to `GloballyShared`
33 /// to avoid codegenning them a bunch of times. In this situation,
34 /// however, our local copy may conflict with other crates also
35 /// inlining the same function.
36 ///
37 /// This flag indicates that this situation is occurring, and informs
38 /// symbol name calculation that some extra mangling is needed to
39 /// avoid conflicts. Note that this may eventually go away entirely if
40 /// ThinLTO enables us to *always* have a globally shared instance of a
41 /// function within one crate's compilation.
42 may_conflict: bool,
43 },
44
45 /// Each codegen unit containing a reference to the given MonoItem will
46 /// have its own private copy of the function (with internal linkage).
47 LocalCopy,
48}
49
50#[derive(PartialEq, Eq, Clone, Copy, Debug, Hash, HashStable, TyEncodable, TyDecodable)]
51pub enum MonoItem<'tcx> {
52 Fn(Instance<'tcx>),
53 Static(DefId),
54 GlobalAsm(ItemId),
55}
56
57fn opt_incr_drop_glue_mode<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> InstantiationMode {
58 // Non-ADTs can't have a Drop impl. This case is mostly hit by closures whose captures require
59 // dropping.
60 let ty::Adt(adt_def, _) = ty.kind() else {
61 return InstantiationMode::LocalCopy;
62 };
63
64 // Types that don't have a direct Drop impl, but have fields that require dropping.
65 let Some(dtor) = adt_def.destructor(tcx) else {
66 // We use LocalCopy for drops of enums only; this code is inherited from
67 // https://github.com/rust-lang/rust/pull/67332 and the theory is that we get to optimize
68 // out code like drop_in_place(Option::None) before crate-local ThinLTO, which improves
69 // compile time. At the time of writing, simply removing this entire check does seem to
70 // regress incr-opt compile times. But it sure seems like a more sophisticated check could
71 // do better here.
72 if adt_def.is_enum() {
73 return InstantiationMode::LocalCopy;
74 } else {
75 return InstantiationMode::GloballyShared { may_conflict: true };
76 }
77 };
78
79 // We've gotten to a drop_in_place for a type that directly implements Drop.
80 // The drop glue is a wrapper for the Drop::drop impl, and we are an optimized build, so in an
81 // effort to coordinate with the mode that the actual impl will get, we make the glue also
82 // LocalCopy.
83 if tcx.cross_crate_inlinable(dtor.did) {
84 InstantiationMode::LocalCopy
85 } else {
86 InstantiationMode::GloballyShared { may_conflict: true }
87 }
88}
89
90impl<'tcx> MonoItem<'tcx> {
91 /// Returns `true` if the mono item is user-defined (i.e. not compiler-generated, like shims).
92 pub fn is_user_defined(&self) -> bool {
93 match *self {
94 MonoItem::Fn(instance) => matches!(instance.def, InstanceKind::Item(..)),
95 MonoItem::Static(..) | MonoItem::GlobalAsm(..) => true,
96 }
97 }
98
99 // Note: if you change how item size estimates work, you might need to
100 // change NON_INCR_MIN_CGU_SIZE as well.
101 pub fn size_estimate(&self, tcx: TyCtxt<'tcx>) -> usize {
102 match *self {
103 MonoItem::Fn(instance) => tcx.size_estimate(instance),
104 // Conservatively estimate the size of a static declaration or
105 // assembly item to be 1.
106 MonoItem::Static(_) | MonoItem::GlobalAsm(_) => 1,
107 }
108 }
109
110 pub fn is_generic_fn(&self) -> bool {
111 match self {
112 MonoItem::Fn(instance) => instance.args.non_erasable_generics().next().is_some(),
113 MonoItem::Static(..) | MonoItem::GlobalAsm(..) => false,
114 }
115 }
116
117 pub fn symbol_name(&self, tcx: TyCtxt<'tcx>) -> SymbolName<'tcx> {
118 match *self {
119 MonoItem::Fn(instance) => tcx.symbol_name(instance),
120 MonoItem::Static(def_id) => tcx.symbol_name(Instance::mono(tcx, def_id)),
121 MonoItem::GlobalAsm(item_id) => {
122 SymbolName::new(tcx, &format!("global_asm_{:?}", item_id.owner_id))
123 }
124 }
125 }
126
127 pub fn instantiation_mode(&self, tcx: TyCtxt<'tcx>) -> InstantiationMode {
128 // The case handling here is written in the same style as cross_crate_inlinable, we first
129 // handle the cases where we must use a particular instantiation mode, then cascade down
130 // through a sequence of heuristics.
131
132 // The first thing we do is detect MonoItems which we must instantiate exactly once in the
133 // whole program.
134
135 // Statics and global_asm! must be instantiated exactly once.
136 let instance = match *self {
137 MonoItem::Fn(instance) => instance,
138 MonoItem::Static(..) | MonoItem::GlobalAsm(..) => {
139 return InstantiationMode::GloballyShared { may_conflict: false };
140 }
141 };
142
143 // Similarly, the executable entrypoint must be instantiated exactly once.
144 if tcx.is_entrypoint(instance.def_id()) {
145 return InstantiationMode::GloballyShared { may_conflict: false };
146 }
147
148 // If the function is #[naked] or contains any other attribute that requires exactly-once
149 // instantiation:
150 // We emit an unused_attributes lint for this case, which should be kept in sync if possible.
151 let codegen_fn_attrs = tcx.codegen_instance_attrs(instance.def);
152 if codegen_fn_attrs.contains_extern_indicator(tcx, instance.def.def_id())
153 || codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::NAKED)
154 {
155 return InstantiationMode::GloballyShared { may_conflict: false };
156 }
157
158 // This is technically a heuristic even though it's in the "not a heuristic" part of
159 // instantiation mode selection.
160 // It is surely possible to untangle this; the root problem is that the way we instantiate
161 // InstanceKind other than Item is very complicated.
162 //
163 // The fallback case is to give everything else GloballyShared at OptLevel::No and
164 // LocalCopy at all other opt levels. This is a good default, except for one specific build
165 // configuration: Optimized incremental builds.
166 // In the current compiler architecture there is a fundamental tension between
167 // optimizations (which want big CGUs with as many things LocalCopy as possible) and
168 // incrementality (which wants small CGUs with as many things GloballyShared as possible).
169 // The heuristics implemented here do better than a completely naive approach in the
170 // compiler benchmark suite, but there is no reason to believe they are optimal.
171 if let InstanceKind::DropGlue(_, Some(ty)) = instance.def {
172 if tcx.sess.opts.optimize == OptLevel::No {
173 return InstantiationMode::GloballyShared { may_conflict: false };
174 }
175 if tcx.sess.opts.incremental.is_none() {
176 return InstantiationMode::LocalCopy;
177 }
178 return opt_incr_drop_glue_mode(tcx, ty);
179 }
180
181 // We need to ensure that we do not decide the InstantiationMode of an exported symbol is
182 // LocalCopy. Since exported symbols are computed based on the output of
183 // cross_crate_inlinable, we are beholden to our previous decisions.
184 //
185 // Note that just like above, this check for requires_inline is technically a heuristic
186 // even though it's in the "not a heuristic" part of instantiation mode selection.
187 if !tcx.cross_crate_inlinable(instance.def_id()) && !instance.def.requires_inline(tcx) {
188 return InstantiationMode::GloballyShared { may_conflict: false };
189 }
190
191 // Beginning of heuristics. The handling of link-dead-code and inline(always) are QoL only,
192 // the compiler should not crash and linkage should work, but codegen may be undesirable.
193
194 // -Clink-dead-code was given an unfortunate name; the point of the flag is to assist
195 // coverage tools which rely on having every function in the program appear in the
196 // generated code. If we select LocalCopy, functions which are not used because they are
197 // missing test coverage will disappear from such coverage reports, defeating the point.
198 // Note that -Cinstrument-coverage does not require such assistance from us, only coverage
199 // tools implemented without compiler support ironically require a special compiler flag.
200 if tcx.sess.link_dead_code() {
201 return InstantiationMode::GloballyShared { may_conflict: true };
202 }
203
204 // To ensure that #[inline(always)] can be inlined as much as possible, especially in unoptimized
205 // builds, we always select LocalCopy.
206 if codegen_fn_attrs.inline.always() {
207 return InstantiationMode::LocalCopy;
208 }
209
210 // #[inline(never)] functions in general are poor candidates for inlining and thus since
211 // LocalCopy generally increases code size for the benefit of optimizations from inlining,
212 // we want to give them GloballyShared codegen.
213 // The slight problem is that generic functions need to always support cross-crate
214 // compilation, so all previous stages of the compiler are obligated to treat generic
215 // functions the same as those that unconditionally get LocalCopy codegen. It's only when
216 // we get here that we can at least not codegen a #[inline(never)] generic function in all
217 // of our CGUs.
218 if let InlineAttr::Never = codegen_fn_attrs.inline
219 && self.is_generic_fn()
220 {
221 return InstantiationMode::GloballyShared { may_conflict: true };
222 }
223
224 // The fallthrough case is to generate LocalCopy for all optimized builds, and
225 // GloballyShared with conflict prevention when optimizations are disabled.
226 match tcx.sess.opts.optimize {
227 OptLevel::No => InstantiationMode::GloballyShared { may_conflict: true },
228 _ => InstantiationMode::LocalCopy,
229 }
230 }
231
232 pub fn explicit_linkage(&self, tcx: TyCtxt<'tcx>) -> Option<Linkage> {
233 let instance_kind = match *self {
234 MonoItem::Fn(ref instance) => instance.def,
235 MonoItem::Static(def_id) => InstanceKind::Item(def_id),
236 MonoItem::GlobalAsm(..) => return None,
237 };
238
239 tcx.codegen_instance_attrs(instance_kind).linkage
240 }
241
242 /// Returns `true` if this instance is instantiable - whether it has no unsatisfied
243 /// predicates.
244 ///
245 /// In order to codegen an item, all of its predicates must hold, because
246 /// otherwise the item does not make sense. Type-checking ensures that
247 /// the predicates of every item that is *used by* a valid item *do*
248 /// hold, so we can rely on that.
249 ///
250 /// However, we codegen collector roots (reachable items) and functions
251 /// in vtables when they are seen, even if they are not used, and so they
252 /// might not be instantiable. For example, a programmer can define this
253 /// public function:
254 ///
255 /// pub fn foo<'a>(s: &'a mut ()) where &'a mut (): Clone {
256 /// <&mut () as Clone>::clone(&s);
257 /// }
258 ///
259 /// That function can't be codegened, because the method `<&mut () as Clone>::clone`
260 /// does not exist. Luckily for us, that function can't ever be used,
261 /// because that would require for `&'a mut (): Clone` to hold, so we
262 /// can just not emit any code, or even a linker reference for it.
263 ///
264 /// Similarly, if a vtable method has such a signature, and therefore can't
265 /// be used, we can just not emit it and have a placeholder (a null pointer,
266 /// which will never be accessed) in its place.
267 pub fn is_instantiable(&self, tcx: TyCtxt<'tcx>) -> bool {
268 debug!("is_instantiable({:?})", self);
269 let (def_id, args) = match *self {
270 MonoItem::Fn(ref instance) => (instance.def_id(), instance.args),
271 MonoItem::Static(def_id) => (def_id, GenericArgs::empty()),
272 // global asm never has predicates
273 MonoItem::GlobalAsm(..) => return true,
274 };
275
276 !tcx.instantiate_and_check_impossible_predicates((def_id, &args))
277 }
278
279 pub fn local_span(&self, tcx: TyCtxt<'tcx>) -> Option<Span> {
280 match *self {
281 MonoItem::Fn(Instance { def, .. }) => def.def_id().as_local(),
282 MonoItem::Static(def_id) => def_id.as_local(),
283 MonoItem::GlobalAsm(item_id) => Some(item_id.owner_id.def_id),
284 }
285 .map(|def_id| tcx.def_span(def_id))
286 }
287
288 // Only used by rustc_codegen_cranelift
289 pub fn codegen_dep_node(&self, tcx: TyCtxt<'tcx>) -> DepNode {
290 crate::dep_graph::make_compile_mono_item(tcx, self)
291 }
292
293 /// Returns the item's `CrateNum`
294 pub fn krate(&self) -> CrateNum {
295 match self {
296 MonoItem::Fn(instance) => instance.def_id().krate,
297 MonoItem::Static(def_id) => def_id.krate,
298 MonoItem::GlobalAsm(..) => LOCAL_CRATE,
299 }
300 }
301
302 /// Returns the item's `DefId`
303 pub fn def_id(&self) -> DefId {
304 match *self {
305 MonoItem::Fn(Instance { def, .. }) => def.def_id(),
306 MonoItem::Static(def_id) => def_id,
307 MonoItem::GlobalAsm(item_id) => item_id.owner_id.to_def_id(),
308 }
309 }
310}
311
312impl<'tcx> fmt::Display for MonoItem<'tcx> {
313 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
314 match *self {
315 MonoItem::Fn(instance) => write!(f, "fn {instance}"),
316 MonoItem::Static(def_id) => {
317 write!(f, "static {}", Instance::new_raw(def_id, GenericArgs::empty()))
318 }
319 MonoItem::GlobalAsm(..) => write!(f, "global_asm"),
320 }
321 }
322}
323
324impl ToStableHashKey<StableHashingContext<'_>> for MonoItem<'_> {
325 type KeyType = Fingerprint;
326
327 fn to_stable_hash_key(&self, hcx: &StableHashingContext<'_>) -> Self::KeyType {
328 let mut hasher = StableHasher::new();
329 self.hash_stable(&mut hcx.clone(), &mut hasher);
330 hasher.finish()
331 }
332}
333
334#[derive(Debug, HashStable, Copy, Clone)]
335pub struct MonoItemPartitions<'tcx> {
336 pub codegen_units: &'tcx [CodegenUnit<'tcx>],
337 pub all_mono_items: &'tcx DefIdSet,
338}
339
340#[derive(Debug, HashStable)]
341pub struct CodegenUnit<'tcx> {
342 /// A name for this CGU. Incremental compilation requires that
343 /// name be unique amongst **all** crates. Therefore, it should
344 /// contain something unique to this crate (e.g., a module path)
345 /// as well as the crate name and disambiguator.
346 name: Symbol,
347 items: FxIndexMap<MonoItem<'tcx>, MonoItemData>,
348 size_estimate: usize,
349 primary: bool,
350 /// True if this is CGU is used to hold code coverage information for dead code,
351 /// false otherwise.
352 is_code_coverage_dead_code_cgu: bool,
353}
354
355/// Auxiliary info about a `MonoItem`.
356#[derive(Copy, Clone, PartialEq, Debug, HashStable)]
357pub struct MonoItemData {
358 /// A cached copy of the result of `MonoItem::instantiation_mode`, where
359 /// `GloballyShared` maps to `false` and `LocalCopy` maps to `true`.
360 pub inlined: bool,
361
362 pub linkage: Linkage,
363 pub visibility: Visibility,
364
365 /// A cached copy of the result of `MonoItem::size_estimate`.
366 pub size_estimate: usize,
367}
368
369/// Specifies the symbol visibility with regards to dynamic linking.
370///
371/// Visibility doesn't have any effect when linkage is internal.
372///
373/// DSO means dynamic shared object, that is a dynamically linked executable or dylib.
374#[derive(Copy, Clone, PartialEq, Debug, HashStable)]
375pub enum Visibility {
376 /// Export the symbol from the DSO and apply overrides of the symbol by outside DSOs to within
377 /// the DSO if the object file format supports this.
378 Default,
379 /// Hide the symbol outside of the defining DSO even when external linkage is used to export it
380 /// from the object file.
381 Hidden,
382 /// Export the symbol from the DSO, but don't apply overrides of the symbol by outside DSOs to
383 /// within the DSO. Equivalent to default visibility with object file formats that don't support
384 /// overriding exported symbols by another DSO.
385 Protected,
386}
387
388impl From<SymbolVisibility> for Visibility {
389 fn from(value: SymbolVisibility) -> Self {
390 match value {
391 SymbolVisibility::Hidden => Visibility::Hidden,
392 SymbolVisibility::Protected => Visibility::Protected,
393 SymbolVisibility::Interposable => Visibility::Default,
394 }
395 }
396}
397
398impl<'tcx> CodegenUnit<'tcx> {
399 #[inline]
400 pub fn new(name: Symbol) -> CodegenUnit<'tcx> {
401 CodegenUnit {
402 name,
403 items: Default::default(),
404 size_estimate: 0,
405 primary: false,
406 is_code_coverage_dead_code_cgu: false,
407 }
408 }
409
410 pub fn name(&self) -> Symbol {
411 self.name
412 }
413
414 pub fn set_name(&mut self, name: Symbol) {
415 self.name = name;
416 }
417
418 pub fn is_primary(&self) -> bool {
419 self.primary
420 }
421
422 pub fn make_primary(&mut self) {
423 self.primary = true;
424 }
425
426 pub fn items(&self) -> &FxIndexMap<MonoItem<'tcx>, MonoItemData> {
427 &self.items
428 }
429
430 pub fn items_mut(&mut self) -> &mut FxIndexMap<MonoItem<'tcx>, MonoItemData> {
431 &mut self.items
432 }
433
434 pub fn is_code_coverage_dead_code_cgu(&self) -> bool {
435 self.is_code_coverage_dead_code_cgu
436 }
437
438 /// Marks this CGU as the one used to contain code coverage information for dead code.
439 pub fn make_code_coverage_dead_code_cgu(&mut self) {
440 self.is_code_coverage_dead_code_cgu = true;
441 }
442
443 pub fn mangle_name(human_readable_name: &str) -> BaseNString {
444 let mut hasher = StableHasher::new();
445 human_readable_name.hash(&mut hasher);
446 let hash: Hash128 = hasher.finish();
447 hash.as_u128().to_base_fixed_len(CASE_INSENSITIVE)
448 }
449
450 pub fn shorten_name(human_readable_name: &str) -> Cow<'_, str> {
451 // Set a limit a somewhat below the common platform limits for file names.
452 const MAX_CGU_NAME_LENGTH: usize = 200;
453 const TRUNCATED_NAME_PREFIX: &str = "-trunc-";
454 if human_readable_name.len() > MAX_CGU_NAME_LENGTH {
455 let mangled_name = Self::mangle_name(human_readable_name);
456 // Determine a safe byte offset to truncate the name to
457 let truncate_to = human_readable_name.floor_char_boundary(
458 MAX_CGU_NAME_LENGTH - TRUNCATED_NAME_PREFIX.len() - mangled_name.len(),
459 );
460 format!(
461 "{}{}{}",
462 &human_readable_name[..truncate_to],
463 TRUNCATED_NAME_PREFIX,
464 mangled_name
465 )
466 .into()
467 } else {
468 // If the name is short enough, we can just return it as is.
469 human_readable_name.into()
470 }
471 }
472
473 pub fn compute_size_estimate(&mut self) {
474 // The size of a codegen unit as the sum of the sizes of the items
475 // within it.
476 self.size_estimate = self.items.values().map(|data| data.size_estimate).sum();
477 }
478
479 /// Should only be called if [`compute_size_estimate`] has previously been called.
480 ///
481 /// [`compute_size_estimate`]: Self::compute_size_estimate
482 #[inline]
483 pub fn size_estimate(&self) -> usize {
484 // Items are never zero-sized, so if we have items the estimate must be
485 // non-zero, unless we forgot to call `compute_size_estimate` first.
486 assert!(self.items.is_empty() || self.size_estimate != 0);
487 self.size_estimate
488 }
489
490 pub fn contains_item(&self, item: &MonoItem<'tcx>) -> bool {
491 self.items().contains_key(item)
492 }
493
494 pub fn work_product_id(&self) -> WorkProductId {
495 WorkProductId::from_cgu_name(self.name().as_str())
496 }
497
498 pub fn previous_work_product(&self, tcx: TyCtxt<'_>) -> WorkProduct {
499 let work_product_id = self.work_product_id();
500 tcx.dep_graph
501 .previous_work_product(&work_product_id)
502 .unwrap_or_else(|| panic!("Could not find work-product for CGU `{}`", self.name()))
503 }
504
505 pub fn items_in_deterministic_order(
506 &self,
507 tcx: TyCtxt<'tcx>,
508 ) -> Vec<(MonoItem<'tcx>, MonoItemData)> {
509 // The codegen tests rely on items being process in the same order as
510 // they appear in the file, so for local items, we sort by span first
511 #[derive(PartialEq, Eq, PartialOrd, Ord)]
512 struct ItemSortKey<'tcx>(Option<Span>, SymbolName<'tcx>);
513
514 // We only want to take HirIds of user-defines instances into account.
515 // The others don't matter for the codegen tests and can even make item
516 // order unstable.
517 fn local_item_id<'tcx>(item: MonoItem<'tcx>) -> Option<DefId> {
518 match item {
519 MonoItem::Fn(ref instance) => match instance.def {
520 InstanceKind::Item(def) => def.as_local().map(|_| def),
521 InstanceKind::VTableShim(..)
522 | InstanceKind::ReifyShim(..)
523 | InstanceKind::Intrinsic(..)
524 | InstanceKind::FnPtrShim(..)
525 | InstanceKind::Virtual(..)
526 | InstanceKind::ClosureOnceShim { .. }
527 | InstanceKind::ConstructCoroutineInClosureShim { .. }
528 | InstanceKind::DropGlue(..)
529 | InstanceKind::CloneShim(..)
530 | InstanceKind::ThreadLocalShim(..)
531 | InstanceKind::FnPtrAddrShim(..)
532 | InstanceKind::AsyncDropGlue(..)
533 | InstanceKind::FutureDropPollShim(..)
534 | InstanceKind::AsyncDropGlueCtorShim(..) => None,
535 },
536 MonoItem::Static(def_id) => def_id.as_local().map(|_| def_id),
537 MonoItem::GlobalAsm(item_id) => Some(item_id.owner_id.def_id.to_def_id()),
538 }
539 }
540 fn item_sort_key<'tcx>(tcx: TyCtxt<'tcx>, item: MonoItem<'tcx>) -> ItemSortKey<'tcx> {
541 ItemSortKey(
542 local_item_id(item)
543 .map(|def_id| tcx.def_span(def_id).find_ancestor_not_from_macro())
544 .flatten(),
545 item.symbol_name(tcx),
546 )
547 }
548
549 let mut items: Vec<_> = self.items().iter().map(|(&i, &data)| (i, data)).collect();
550 if !tcx.sess.opts.unstable_opts.codegen_source_order {
551 // In this case, we do not need to keep the items in any specific order, as the input
552 // is already deterministic.
553 //
554 // However, it seems that moving related things (such as different
555 // monomorphizations of the same function) close to one another is actually beneficial
556 // for LLVM performance.
557 // LLVM will codegen the items in the order we pass them to it, and when it handles
558 // similar things in succession, it seems that it leads to better cache utilization,
559 // less branch mispredictions and in general to better performance.
560 // For example, if we have functions `a`, `c::<u32>`, `b`, `c::<i16>`, `d` and
561 // `c::<bool>`, it seems that it helps LLVM's performance to codegen the three `c`
562 // instantiations right after one another, as they will likely reference similar types,
563 // call similar functions, etc.
564 //
565 // See https://github.com/rust-lang/rust/pull/145358 for more details.
566 //
567 // Sorting by symbol name should not incur any new non-determinism.
568 items.sort_by_cached_key(|&(i, _)| i.symbol_name(tcx));
569 } else {
570 items.sort_by_cached_key(|&(i, _)| item_sort_key(tcx, i));
571 }
572 items
573 }
574
575 pub fn codegen_dep_node(&self, tcx: TyCtxt<'tcx>) -> DepNode {
576 crate::dep_graph::make_compile_codegen_unit(tcx, self.name())
577 }
578}
579
580impl ToStableHashKey<StableHashingContext<'_>> for CodegenUnit<'_> {
581 type KeyType = String;
582
583 fn to_stable_hash_key(&self, _: &StableHashingContext<'_>) -> Self::KeyType {
584 // Codegen unit names are conceptually required to be stable across
585 // compilation session so that object file names match up.
586 self.name.to_string()
587 }
588}
589
590pub struct CodegenUnitNameBuilder<'tcx> {
591 tcx: TyCtxt<'tcx>,
592 cache: UnordMap<CrateNum, String>,
593}
594
595impl<'tcx> CodegenUnitNameBuilder<'tcx> {
596 pub fn new(tcx: TyCtxt<'tcx>) -> Self {
597 CodegenUnitNameBuilder { tcx, cache: Default::default() }
598 }
599
600 /// CGU names should fulfill the following requirements:
601 /// - They should be able to act as a file name on any kind of file system
602 /// - They should not collide with other CGU names, even for different versions
603 /// of the same crate.
604 ///
605 /// Consequently, we don't use special characters except for '.' and '-' and we
606 /// prefix each name with the crate-name and crate-disambiguator.
607 ///
608 /// This function will build CGU names of the form:
609 ///
610 /// ```text
611 /// <crate-name>.<crate-disambiguator>[-in-<local-crate-id>](-<component>)*[.<special-suffix>]
612 /// <local-crate-id> = <local-crate-name>.<local-crate-disambiguator>
613 /// ```
614 ///
615 /// The '.' before `<special-suffix>` makes sure that names with a special
616 /// suffix can never collide with a name built out of regular Rust
617 /// identifiers (e.g., module paths).
618 pub fn build_cgu_name<I, C, S>(
619 &mut self,
620 cnum: CrateNum,
621 components: I,
622 special_suffix: Option<S>,
623 ) -> Symbol
624 where
625 I: IntoIterator<Item = C>,
626 C: fmt::Display,
627 S: fmt::Display,
628 {
629 let cgu_name = self.build_cgu_name_no_mangle(cnum, components, special_suffix);
630
631 if self.tcx.sess.opts.unstable_opts.human_readable_cgu_names {
632 Symbol::intern(&CodegenUnit::shorten_name(cgu_name.as_str()))
633 } else {
634 Symbol::intern(&CodegenUnit::mangle_name(cgu_name.as_str()))
635 }
636 }
637
638 /// Same as `CodegenUnit::build_cgu_name()` but will never mangle the
639 /// resulting name.
640 pub fn build_cgu_name_no_mangle<I, C, S>(
641 &mut self,
642 cnum: CrateNum,
643 components: I,
644 special_suffix: Option<S>,
645 ) -> Symbol
646 where
647 I: IntoIterator<Item = C>,
648 C: fmt::Display,
649 S: fmt::Display,
650 {
651 use std::fmt::Write;
652
653 let mut cgu_name = String::with_capacity(64);
654
655 // Start out with the crate name and disambiguator
656 let tcx = self.tcx;
657 let crate_prefix = self.cache.entry(cnum).or_insert_with(|| {
658 // Whenever the cnum is not LOCAL_CRATE we also mix in the
659 // local crate's ID. Otherwise there can be collisions between CGUs
660 // instantiating stuff for upstream crates.
661 let local_crate_id = if cnum != LOCAL_CRATE {
662 let local_stable_crate_id = tcx.stable_crate_id(LOCAL_CRATE);
663 format!("-in-{}.{:08x}", tcx.crate_name(LOCAL_CRATE), local_stable_crate_id)
664 } else {
665 String::new()
666 };
667
668 let stable_crate_id = tcx.stable_crate_id(LOCAL_CRATE);
669 format!("{}.{:08x}{}", tcx.crate_name(cnum), stable_crate_id, local_crate_id)
670 });
671
672 write!(cgu_name, "{crate_prefix}").unwrap();
673
674 // Add the components
675 for component in components {
676 write!(cgu_name, "-{component}").unwrap();
677 }
678
679 if let Some(special_suffix) = special_suffix {
680 // We add a dot in here so it cannot clash with anything in a regular
681 // Rust identifier
682 write!(cgu_name, ".{special_suffix}").unwrap();
683 }
684
685 Symbol::intern(&cgu_name)
686 }
687}
688
689/// See module-level docs of `rustc_monomorphize::collector` on some context for "mentioned" items.
690#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, HashStable)]
691pub enum CollectionMode {
692 /// Collect items that are used, i.e., actually needed for codegen.
693 ///
694 /// Which items are used can depend on optimization levels, as MIR optimizations can remove
695 /// uses.
696 UsedItems,
697 /// Collect items that are mentioned. The goal of this mode is that it is independent of
698 /// optimizations: the set of "mentioned" items is computed before optimizations are run.
699 ///
700 /// The exact contents of this set are *not* a stable guarantee. (For instance, it is currently
701 /// computed after drop-elaboration. If we ever do some optimizations even in debug builds, we
702 /// might decide to run them before computing mentioned items.) The key property of this set is
703 /// that it is optimization-independent.
704 MentionedItems,
705}