]> git.proxmox.com Git - rustc.git/blame - compiler/rustc_middle/src/mir/mono.rs
New upstream version 1.57.0+dfsg1
[rustc.git] / compiler / rustc_middle / src / mir / mono.rs
CommitLineData
5869c6ff 1use crate::dep_graph::{DepNode, WorkProduct, WorkProductId};
dfeec247 2use crate::ty::{subst::InternalSubsts, Instance, InstanceDef, SymbolName, TyCtxt};
74b04a01 3use rustc_attr::InlineAttr;
ff7c6d11 4use rustc_data_structures::base_n;
ba9703b0 5use rustc_data_structures::fingerprint::Fingerprint;
dfeec247 6use rustc_data_structures::fx::FxHashMap;
e74abb32 7use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
cdc7bbd5 8use rustc_hir::def_id::{CrateNum, DefId, LOCAL_CRATE};
6a06907d 9use rustc_hir::{HirId, ItemId};
c295e0f8 10use rustc_query_system::ich::{NodeIdHashingMode, StableHashingContext};
ba9703b0 11use rustc_session::config::OptLevel;
dfeec247
XL
12use rustc_span::source_map::Span;
13use rustc_span::symbol::Symbol;
b7449926 14use std::fmt;
ff7c6d11 15use std::hash::Hash;
ea8adc8c 16
dc9dc135 17/// Describes how a monomorphization will be instantiated in object files.
e74abb32 18#[derive(PartialEq)]
dc9dc135
XL
19pub enum InstantiationMode {
20 /// There will be exactly one instance of the given MonoItem. It will have
21 /// external linkage so that it can be linked to from other codegen units.
22 GloballyShared {
23 /// In some compilation scenarios we may decide to take functions that
24 /// are typically `LocalCopy` and instead move them to `GloballyShared`
25 /// to avoid codegenning them a bunch of times. In this situation,
26 /// however, our local copy may conflict with other crates also
27 /// inlining the same function.
28 ///
29 /// This flag indicates that this situation is occurring, and informs
30 /// symbol name calculation that some extra mangling is needed to
31 /// avoid conflicts. Note that this may eventually go away entirely if
32 /// ThinLTO enables us to *always* have a globally shared instance of a
33 /// function within one crate's compilation.
34 may_conflict: bool,
35 },
36
37 /// Each codegen unit containing a reference to the given MonoItem will
38 /// have its own private copy of the function (with internal linkage).
39 LocalCopy,
40}
41
ea8adc8c 42#[derive(PartialEq, Eq, Clone, Copy, Debug, Hash)]
ff7c6d11 43pub enum MonoItem<'tcx> {
ea8adc8c 44 Fn(Instance<'tcx>),
0531ce1d 45 Static(DefId),
6a06907d 46 GlobalAsm(ItemId),
ea8adc8c
XL
47}
48
2c00a5a8 49impl<'tcx> MonoItem<'tcx> {
c295e0f8
XL
50 /// Returns `true` if the mono item is user-defined (i.e. not compiler-generated, like shims).
51 pub fn is_user_defined(&self) -> bool {
52 match *self {
53 MonoItem::Fn(instance) => matches!(instance.def, InstanceDef::Item(..)),
54 MonoItem::Static(..) | MonoItem::GlobalAsm(..) => true,
55 }
56 }
57
dc9dc135 58 pub fn size_estimate(&self, tcx: TyCtxt<'tcx>) -> usize {
2c00a5a8
XL
59 match *self {
60 MonoItem::Fn(instance) => {
61 // Estimate the size of a function based on how many statements
62 // it contains.
63 tcx.instance_def_size_estimate(instance.def)
dfeec247 64 }
2c00a5a8
XL
65 // Conservatively estimate the size of a static declaration
66 // or assembly to be 1.
dfeec247 67 MonoItem::Static(_) | MonoItem::GlobalAsm(_) => 1,
2c00a5a8
XL
68 }
69 }
dc9dc135
XL
70
71 pub fn is_generic_fn(&self) -> bool {
72 match *self {
dfeec247
XL
73 MonoItem::Fn(ref instance) => instance.substs.non_erasable_generics().next().is_some(),
74 MonoItem::Static(..) | MonoItem::GlobalAsm(..) => false,
dc9dc135
XL
75 }
76 }
77
3dfed10e 78 pub fn symbol_name(&self, tcx: TyCtxt<'tcx>) -> SymbolName<'tcx> {
dc9dc135
XL
79 match *self {
80 MonoItem::Fn(instance) => tcx.symbol_name(instance),
dfeec247 81 MonoItem::Static(def_id) => tcx.symbol_name(Instance::mono(tcx, def_id)),
6a06907d
XL
82 MonoItem::GlobalAsm(item_id) => {
83 SymbolName::new(tcx, &format!("global_asm_{:?}", item_id.def_id))
dc9dc135
XL
84 }
85 }
86 }
87
88 pub fn instantiation_mode(&self, tcx: TyCtxt<'tcx>) -> InstantiationMode {
dfeec247
XL
89 let generate_cgu_internal_copies = tcx
90 .sess
91 .opts
92 .debugging_opts
93 .inline_in_all_cgus
94 .unwrap_or_else(|| tcx.sess.opts.optimize != OptLevel::No)
1b1a35ee 95 && !tcx.sess.link_dead_code();
dc9dc135
XL
96
97 match *self {
98 MonoItem::Fn(ref instance) => {
17df50a5 99 let entry_def_id = tcx.entry_fn(()).map(|(id, _)| id);
f035d41b
XL
100 // If this function isn't inlined or otherwise has an extern
101 // indicator, then we'll be creating a globally shared version.
102 if tcx.codegen_fn_attrs(instance.def_id()).contains_extern_indicator()
dfeec247 103 || !instance.def.generates_cgu_internal_copy(tcx)
cdc7bbd5 104 || Some(instance.def_id()) == entry_def_id
dc9dc135 105 {
dfeec247 106 return InstantiationMode::GloballyShared { may_conflict: false };
dc9dc135
XL
107 }
108
109 // At this point we don't have explicit linkage and we're an
110 // inlined function. If we're inlining into all CGUs then we'll
f035d41b 111 // be creating a local copy per CGU.
dfeec247
XL
112 if generate_cgu_internal_copies {
113 return InstantiationMode::LocalCopy;
dc9dc135
XL
114 }
115
116 // Finally, if this is `#[inline(always)]` we're sure to respect
117 // that with an inline copy per CGU, but otherwise we'll be
118 // creating one copy of this `#[inline]` function which may
119 // conflict with upstream crates as it could be an exported
120 // symbol.
121 match tcx.codegen_fn_attrs(instance.def_id()).inline {
122 InlineAttr::Always => InstantiationMode::LocalCopy,
dfeec247 123 _ => InstantiationMode::GloballyShared { may_conflict: true },
dc9dc135
XL
124 }
125 }
dfeec247 126 MonoItem::Static(..) | MonoItem::GlobalAsm(..) => {
dc9dc135
XL
127 InstantiationMode::GloballyShared { may_conflict: false }
128 }
129 }
130 }
131
132 pub fn explicit_linkage(&self, tcx: TyCtxt<'tcx>) -> Option<Linkage> {
133 let def_id = match *self {
134 MonoItem::Fn(ref instance) => instance.def_id(),
135 MonoItem::Static(def_id) => def_id,
136 MonoItem::GlobalAsm(..) => return None,
137 };
138
139 let codegen_fn_attrs = tcx.codegen_fn_attrs(def_id);
140 codegen_fn_attrs.linkage
141 }
142
143 /// Returns `true` if this instance is instantiable - whether it has no unsatisfied
144 /// predicates.
145 ///
146 /// In order to codegen an item, all of its predicates must hold, because
147 /// otherwise the item does not make sense. Type-checking ensures that
148 /// the predicates of every item that is *used by* a valid item *do*
149 /// hold, so we can rely on that.
150 ///
151 /// However, we codegen collector roots (reachable items) and functions
152 /// in vtables when they are seen, even if they are not used, and so they
153 /// might not be instantiable. For example, a programmer can define this
154 /// public function:
155 ///
156 /// pub fn foo<'a>(s: &'a mut ()) where &'a mut (): Clone {
157 /// <&mut () as Clone>::clone(&s);
158 /// }
159 ///
160 /// That function can't be codegened, because the method `<&mut () as Clone>::clone`
161 /// does not exist. Luckily for us, that function can't ever be used,
162 /// because that would require for `&'a mut (): Clone` to hold, so we
163 /// can just not emit any code, or even a linker reference for it.
164 ///
165 /// Similarly, if a vtable method has such a signature, and therefore can't
166 /// be used, we can just not emit it and have a placeholder (a null pointer,
167 /// which will never be accessed) in its place.
168 pub fn is_instantiable(&self, tcx: TyCtxt<'tcx>) -> bool {
169 debug!("is_instantiable({:?})", self);
170 let (def_id, substs) = match *self {
171 MonoItem::Fn(ref instance) => (instance.def_id(), instance.substs),
172 MonoItem::Static(def_id) => (def_id, InternalSubsts::empty()),
173 // global asm never has predicates
dfeec247 174 MonoItem::GlobalAsm(..) => return true,
dc9dc135
XL
175 };
176
3dfed10e 177 !tcx.subst_and_check_impossible_predicates((def_id, &substs))
dc9dc135
XL
178 }
179
dc9dc135
XL
180 pub fn local_span(&self, tcx: TyCtxt<'tcx>) -> Option<Span> {
181 match *self {
f9f354fc 182 MonoItem::Fn(Instance { def, .. }) => {
3dfed10e 183 def.def_id().as_local().map(|def_id| tcx.hir().local_def_id_to_hir_id(def_id))
f9f354fc
XL
184 }
185 MonoItem::Static(def_id) => {
3dfed10e 186 def_id.as_local().map(|def_id| tcx.hir().local_def_id_to_hir_id(def_id))
f9f354fc 187 }
6a06907d 188 MonoItem::GlobalAsm(item_id) => Some(item_id.hir_id()),
dfeec247
XL
189 }
190 .map(|hir_id| tcx.hir().span(hir_id))
dc9dc135 191 }
cdc7bbd5
XL
192
193 // Only used by rustc_codegen_cranelift
194 pub fn codegen_dep_node(&self, tcx: TyCtxt<'tcx>) -> DepNode {
195 crate::dep_graph::make_compile_mono_item(tcx, self)
196 }
17df50a5
XL
197
198 /// Returns the item's `CrateNum`
199 pub fn krate(&self) -> CrateNum {
200 match self {
201 MonoItem::Fn(ref instance) => instance.def_id().krate,
202 MonoItem::Static(def_id) => def_id.krate,
203 MonoItem::GlobalAsm(..) => LOCAL_CRATE,
204 }
205 }
2c00a5a8
XL
206}
207
0531ce1d 208impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for MonoItem<'tcx> {
e74abb32 209 fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
ea8adc8c
XL
210 ::std::mem::discriminant(self).hash_stable(hcx, hasher);
211
212 match *self {
ff7c6d11 213 MonoItem::Fn(ref instance) => {
ea8adc8c
XL
214 instance.hash_stable(hcx, hasher);
215 }
0531ce1d
XL
216 MonoItem::Static(def_id) => {
217 def_id.hash_stable(hcx, hasher);
218 }
6a06907d 219 MonoItem::GlobalAsm(item_id) => {
ea8adc8c 220 hcx.with_node_id_hashing_mode(NodeIdHashingMode::HashDefPath, |hcx| {
6a06907d 221 item_id.hash_stable(hcx, hasher);
ea8adc8c
XL
222 })
223 }
224 }
225 }
226}
227
1b1a35ee
XL
228impl<'tcx> fmt::Display for MonoItem<'tcx> {
229 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
230 match *self {
231 MonoItem::Fn(instance) => write!(f, "fn {}", instance),
232 MonoItem::Static(def_id) => {
233 write!(f, "static {}", Instance::new(def_id, InternalSubsts::empty()))
234 }
235 MonoItem::GlobalAsm(..) => write!(f, "global_asm"),
236 }
237 }
238}
239
5869c6ff 240#[derive(Debug)]
ea8adc8c
XL
241pub struct CodegenUnit<'tcx> {
242 /// A name for this CGU. Incremental compilation requires that
9fa01778 243 /// name be unique amongst **all** crates. Therefore, it should
ea8adc8c
XL
244 /// contain something unique to this crate (e.g., a module path)
245 /// as well as the crate name and disambiguator.
e74abb32 246 name: Symbol,
ff7c6d11 247 items: FxHashMap<MonoItem<'tcx>, (Linkage, Visibility)>,
2c00a5a8 248 size_estimate: Option<usize>,
17df50a5 249 primary: bool,
ea8adc8c
XL
250}
251
f9f354fc
XL
252/// Specifies the linkage type for a `MonoItem`.
253///
29967ef6 254/// See <https://llvm.org/docs/LangRef.html#linkage-types> for more details about these variants.
3dfed10e 255#[derive(Copy, Clone, PartialEq, Debug, TyEncodable, TyDecodable, HashStable)]
ea8adc8c
XL
256pub enum Linkage {
257 External,
258 AvailableExternally,
259 LinkOnceAny,
260 LinkOnceODR,
261 WeakAny,
262 WeakODR,
263 Appending,
264 Internal,
265 Private,
266 ExternalWeak,
267 Common,
268}
269
60c5eb7d 270#[derive(Copy, Clone, PartialEq, Debug, HashStable)]
ea8adc8c
XL
271pub enum Visibility {
272 Default,
273 Hidden,
274 Protected,
275}
276
ea8adc8c 277impl<'tcx> CodegenUnit<'tcx> {
17df50a5 278 #[inline]
e74abb32 279 pub fn new(name: Symbol) -> CodegenUnit<'tcx> {
17df50a5 280 CodegenUnit { name, items: Default::default(), size_estimate: None, primary: false }
ea8adc8c
XL
281 }
282
e74abb32
XL
283 pub fn name(&self) -> Symbol {
284 self.name
ea8adc8c
XL
285 }
286
e74abb32 287 pub fn set_name(&mut self, name: Symbol) {
ea8adc8c
XL
288 self.name = name;
289 }
290
17df50a5
XL
291 pub fn is_primary(&self) -> bool {
292 self.primary
293 }
294
295 pub fn make_primary(&mut self) {
296 self.primary = true;
297 }
298
ff7c6d11 299 pub fn items(&self) -> &FxHashMap<MonoItem<'tcx>, (Linkage, Visibility)> {
ea8adc8c
XL
300 &self.items
301 }
302
dfeec247 303 pub fn items_mut(&mut self) -> &mut FxHashMap<MonoItem<'tcx>, (Linkage, Visibility)> {
ea8adc8c
XL
304 &mut self.items
305 }
ff7c6d11
XL
306
307 pub fn mangle_name(human_readable_name: &str) -> String {
308 // We generate a 80 bit hash from the name. This should be enough to
309 // avoid collisions and is still reasonably short for filenames.
310 let mut hasher = StableHasher::new();
311 human_readable_name.hash(&mut hasher);
312 let hash: u128 = hasher.finish();
313 let hash = hash & ((1u128 << 80) - 1);
314 base_n::encode(hash, base_n::CASE_INSENSITIVE)
315 }
2c00a5a8 316
dc9dc135 317 pub fn estimate_size(&mut self, tcx: TyCtxt<'tcx>) {
2c00a5a8
XL
318 // Estimate the size of a codegen unit as (approximately) the number of MIR
319 // statements it corresponds to.
320 self.size_estimate = Some(self.items.keys().map(|mi| mi.size_estimate(tcx)).sum());
321 }
322
17df50a5 323 #[inline]
2c00a5a8
XL
324 pub fn size_estimate(&self) -> usize {
325 // Should only be called if `estimate_size` has previously been called.
326 self.size_estimate.expect("estimate_size must be called before getting a size_estimate")
327 }
328
329 pub fn modify_size_estimate(&mut self, delta: usize) {
330 assert!(self.size_estimate.is_some());
331 if let Some(size_estimate) = self.size_estimate {
332 self.size_estimate = Some(size_estimate + delta);
333 }
334 }
dc9dc135
XL
335
336 pub fn contains_item(&self, item: &MonoItem<'tcx>) -> bool {
337 self.items().contains_key(item)
338 }
339
340 pub fn work_product_id(&self) -> WorkProductId {
341 WorkProductId::from_cgu_name(&self.name().as_str())
342 }
343
344 pub fn work_product(&self, tcx: TyCtxt<'_>) -> WorkProduct {
345 let work_product_id = self.work_product_id();
346 tcx.dep_graph
dfeec247
XL
347 .previous_work_product(&work_product_id)
348 .unwrap_or_else(|| panic!("Could not find work-product for CGU `{}`", self.name()))
dc9dc135
XL
349 }
350
351 pub fn items_in_deterministic_order(
352 &self,
353 tcx: TyCtxt<'tcx>,
354 ) -> Vec<(MonoItem<'tcx>, (Linkage, Visibility))> {
355 // The codegen tests rely on items being process in the same order as
356 // they appear in the file, so for local items, we sort by node_id first
357 #[derive(PartialEq, Eq, PartialOrd, Ord)]
3dfed10e 358 pub struct ItemSortKey<'tcx>(Option<HirId>, SymbolName<'tcx>);
dc9dc135 359
3dfed10e 360 fn item_sort_key<'tcx>(tcx: TyCtxt<'tcx>, item: MonoItem<'tcx>) -> ItemSortKey<'tcx> {
dfeec247
XL
361 ItemSortKey(
362 match item {
363 MonoItem::Fn(ref instance) => {
364 match instance.def {
365 // We only want to take HirIds of user-defined
366 // instances into account. The others don't matter for
367 // the codegen tests and can even make item order
368 // unstable.
3dfed10e
XL
369 InstanceDef::Item(def) => def
370 .did
371 .as_local()
372 .map(|def_id| tcx.hir().local_def_id_to_hir_id(def_id)),
dfeec247
XL
373 InstanceDef::VtableShim(..)
374 | InstanceDef::ReifyShim(..)
375 | InstanceDef::Intrinsic(..)
376 | InstanceDef::FnPtrShim(..)
377 | InstanceDef::Virtual(..)
378 | InstanceDef::ClosureOnceShim { .. }
379 | InstanceDef::DropGlue(..)
380 | InstanceDef::CloneShim(..) => None,
dc9dc135
XL
381 }
382 }
f9f354fc 383 MonoItem::Static(def_id) => {
3dfed10e 384 def_id.as_local().map(|def_id| tcx.hir().local_def_id_to_hir_id(def_id))
f9f354fc 385 }
6a06907d 386 MonoItem::GlobalAsm(item_id) => Some(item_id.hir_id()),
dfeec247
XL
387 },
388 item.symbol_name(tcx),
389 )
dc9dc135
XL
390 }
391
392 let mut items: Vec<_> = self.items().iter().map(|(&i, &l)| (i, l)).collect();
393 items.sort_by_cached_key(|&(i, _)| item_sort_key(tcx, i));
394 items
395 }
396
397 pub fn codegen_dep_node(&self, tcx: TyCtxt<'tcx>) -> DepNode {
5869c6ff 398 crate::dep_graph::make_compile_codegen_unit(tcx, self.name())
dc9dc135 399 }
ea8adc8c
XL
400}
401
0531ce1d 402impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for CodegenUnit<'tcx> {
e74abb32 403 fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
ea8adc8c
XL
404 let CodegenUnit {
405 ref items,
406 name,
2c00a5a8
XL
407 // The size estimate is not relevant to the hash
408 size_estimate: _,
17df50a5 409 primary: _,
ea8adc8c
XL
410 } = *self;
411
412 name.hash_stable(hcx, hasher);
413
dfeec247
XL
414 let mut items: Vec<(Fingerprint, _)> = items
415 .iter()
416 .map(|(mono_item, &attrs)| {
417 let mut hasher = StableHasher::new();
418 mono_item.hash_stable(hcx, &mut hasher);
419 let mono_item_fingerprint = hasher.finish();
420 (mono_item_fingerprint, attrs)
421 })
422 .collect();
ea8adc8c
XL
423
424 items.sort_unstable_by_key(|i| i.0);
425 items.hash_stable(hcx, hasher);
426 }
427}
428
dc9dc135
XL
429pub struct CodegenUnitNameBuilder<'tcx> {
430 tcx: TyCtxt<'tcx>,
b7449926
XL
431 cache: FxHashMap<CrateNum, String>,
432}
433
dc9dc135
XL
434impl CodegenUnitNameBuilder<'tcx> {
435 pub fn new(tcx: TyCtxt<'tcx>) -> Self {
dfeec247 436 CodegenUnitNameBuilder { tcx, cache: Default::default() }
b7449926
XL
437 }
438
439 /// CGU names should fulfill the following requirements:
440 /// - They should be able to act as a file name on any kind of file system
441 /// - They should not collide with other CGU names, even for different versions
442 /// of the same crate.
443 ///
444 /// Consequently, we don't use special characters except for '.' and '-' and we
445 /// prefix each name with the crate-name and crate-disambiguator.
446 ///
447 /// This function will build CGU names of the form:
448 ///
449 /// ```
450 /// <crate-name>.<crate-disambiguator>[-in-<local-crate-id>](-<component>)*[.<special-suffix>]
451 /// <local-crate-id> = <local-crate-name>.<local-crate-disambiguator>
452 /// ```
453 ///
454 /// The '.' before `<special-suffix>` makes sure that names with a special
455 /// suffix can never collide with a name built out of regular Rust
0731742a 456 /// identifiers (e.g., module paths).
dfeec247
XL
457 pub fn build_cgu_name<I, C, S>(
458 &mut self,
459 cnum: CrateNum,
460 components: I,
461 special_suffix: Option<S>,
462 ) -> Symbol
463 where
464 I: IntoIterator<Item = C>,
465 C: fmt::Display,
466 S: fmt::Display,
b7449926 467 {
dfeec247 468 let cgu_name = self.build_cgu_name_no_mangle(cnum, components, special_suffix);
b7449926
XL
469
470 if self.tcx.sess.opts.debugging_opts.human_readable_cgu_names {
471 cgu_name
472 } else {
3dfed10e 473 Symbol::intern(&CodegenUnit::mangle_name(&cgu_name.as_str()))
b7449926
XL
474 }
475 }
476
477 /// Same as `CodegenUnit::build_cgu_name()` but will never mangle the
478 /// resulting name.
dfeec247
XL
479 pub fn build_cgu_name_no_mangle<I, C, S>(
480 &mut self,
481 cnum: CrateNum,
482 components: I,
483 special_suffix: Option<S>,
484 ) -> Symbol
485 where
486 I: IntoIterator<Item = C>,
487 C: fmt::Display,
488 S: fmt::Display,
b7449926
XL
489 {
490 use std::fmt::Write;
491
492 let mut cgu_name = String::with_capacity(64);
493
494 // Start out with the crate name and disambiguator
495 let tcx = self.tcx;
496 let crate_prefix = self.cache.entry(cnum).or_insert_with(|| {
497 // Whenever the cnum is not LOCAL_CRATE we also mix in the
498 // local crate's ID. Otherwise there can be collisions between CGUs
499 // instantiating stuff for upstream crates.
500 let local_crate_id = if cnum != LOCAL_CRATE {
136023e0
XL
501 let local_stable_crate_id = tcx.sess.local_stable_crate_id();
502 format!(
503 "-in-{}.{:08x}",
504 tcx.crate_name(LOCAL_CRATE),
505 local_stable_crate_id.to_u64() as u32,
506 )
b7449926
XL
507 } else {
508 String::new()
509 };
510
136023e0
XL
511 let stable_crate_id = tcx.sess.local_stable_crate_id();
512 format!(
513 "{}.{:08x}{}",
514 tcx.crate_name(cnum),
515 stable_crate_id.to_u64() as u32,
516 local_crate_id,
517 )
b7449926
XL
518 });
519
520 write!(cgu_name, "{}", crate_prefix).unwrap();
521
522 // Add the components
523 for component in components {
524 write!(cgu_name, "-{}", component).unwrap();
525 }
526
527 if let Some(special_suffix) = special_suffix {
528 // We add a dot in here so it cannot clash with anything in a regular
529 // Rust identifier
530 write!(cgu_name, ".{}", special_suffix).unwrap();
531 }
532
e74abb32 533 Symbol::intern(&cgu_name[..])
b7449926
XL
534 }
535}