about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMark Mansi <markm@cs.wisc.edu>2019-05-24 10:57:30 -0500
committerMark Mansi <markm@cs.wisc.edu>2019-06-02 22:55:29 -0500
commit621bf0da80a9f304906d74657a91ed927f4f9cad (patch)
treedf9208e83048b13ad9d1def574336463369415de
parentf2b9b2d13bb14dd80d5bacbbb63d9d5cc6003a35 (diff)
downloadrust-621bf0da80a9f304906d74657a91ed927f4f9cad.tar.gz
rust-621bf0da80a9f304906d74657a91ed927f4f9cad.zip
move monoitemext to inherent methods
-rw-r--r--src/librustc/mir/mono.rs191
-rw-r--r--src/librustc_codegen_ssa/base.rs4
-rw-r--r--src/librustc_codegen_ssa/mono_item.rs31
-rw-r--r--src/librustc_codegen_utils/symbol_names.rs3
-rw-r--r--src/librustc_mir/monomorphize/collector.rs4
-rw-r--r--src/librustc_mir/monomorphize/mod.rs3
-rw-r--r--src/librustc_mir/monomorphize/partitioning.rs3
7 files changed, 211 insertions, 28 deletions
diff --git a/src/librustc/mir/mono.rs b/src/librustc/mir/mono.rs
index ca79bc15358..9ba0bb3eb2d 100644
--- a/src/librustc/mir/mono.rs
+++ b/src/librustc/mir/mono.rs
@@ -1,15 +1,44 @@
 use crate::hir::def_id::{DefId, CrateNum, LOCAL_CRATE};
 use crate::hir::HirId;
 use syntax::symbol::InternedString;
-use crate::ty::{Instance, TyCtxt};
+use syntax::attr::InlineAttr;
+use syntax::source_map::Span;
+use crate::ty::{Instance, TyCtxt, SymbolName, subst::InternalSubsts};
 use crate::util::nodemap::FxHashMap;
+use crate::ty::print::obsolete::DefPathBasedNames;
 use rustc_data_structures::base_n;
 use rustc_data_structures::stable_hasher::{HashStable, StableHasherResult,
                                            StableHasher};
 use crate::ich::{Fingerprint, StableHashingContext, NodeIdHashingMode};
+use crate::session::config::OptLevel;
 use std::fmt;
 use std::hash::Hash;
 
+/// Describes how a monomorphization will be instantiated in object files.
+#[derive(PartialEq, Eq, Clone, Copy, Debug, Hash)]
+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,
+}
+
 #[derive(PartialEq, Eq, Clone, Copy, Debug, Hash)]
 pub enum MonoItem<'tcx> {
     Fn(Instance<'tcx>),
@@ -31,6 +60,166 @@ impl<'tcx> MonoItem<'tcx> {
             MonoItem::GlobalAsm(_) => 1,
         }
     }
+
+    pub fn is_generic_fn(&self) -> bool {
+        match *self {
+            MonoItem::Fn(ref instance) => {
+                instance.substs.non_erasable_generics().next().is_some()
+            }
+            MonoItem::Static(..) |
+            MonoItem::GlobalAsm(..) => false,
+        }
+    }
+
+    pub fn symbol_name(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>) -> SymbolName {
+        match *self {
+            MonoItem::Fn(instance) => tcx.symbol_name(instance),
+            MonoItem::Static(def_id) => {
+                tcx.symbol_name(Instance::mono(tcx, def_id))
+            }
+            MonoItem::GlobalAsm(hir_id) => {
+                let def_id = tcx.hir().local_def_id_from_hir_id(hir_id);
+                SymbolName {
+                    name: InternedString::intern(&format!("global_asm_{:?}", def_id))
+                }
+            }
+        }
+    }
+
+    pub fn instantiation_mode(&self,
+                          tcx: TyCtxt<'a, 'tcx, 'tcx>)
+                          -> InstantiationMode {
+        let inline_in_all_cgus =
+            tcx.sess.opts.debugging_opts.inline_in_all_cgus.unwrap_or_else(|| {
+                tcx.sess.opts.optimize != OptLevel::No
+            }) && !tcx.sess.opts.cg.link_dead_code;
+
+        match *self {
+            MonoItem::Fn(ref instance) => {
+                let entry_def_id = tcx.entry_fn(LOCAL_CRATE).map(|(id, _)| id);
+                // If this function isn't inlined or otherwise has explicit
+                // linkage, then we'll be creating a globally shared version.
+                if self.explicit_linkage(tcx).is_some() ||
+                    !instance.def.requires_local(tcx) ||
+                    Some(instance.def_id()) == entry_def_id
+                {
+                    return InstantiationMode::GloballyShared  { may_conflict: false }
+                }
+
+                // At this point we don't have explicit linkage and we're an
+                // inlined function. If we're inlining into all CGUs then we'll
+                // be creating a local copy per CGU
+                if inline_in_all_cgus {
+                    return InstantiationMode::LocalCopy
+                }
+
+                // Finally, if this is `#[inline(always)]` we're sure to respect
+                // that with an inline copy per CGU, but otherwise we'll be
+                // creating one copy of this `#[inline]` function which may
+                // conflict with upstream crates as it could be an exported
+                // symbol.
+                match tcx.codegen_fn_attrs(instance.def_id()).inline {
+                    InlineAttr::Always => InstantiationMode::LocalCopy,
+                    _ => {
+                        InstantiationMode::GloballyShared  { may_conflict: true }
+                    }
+                }
+            }
+            MonoItem::Static(..) |
+            MonoItem::GlobalAsm(..) => {
+                InstantiationMode::GloballyShared { may_conflict: false }
+            }
+        }
+    }
+
+    pub fn explicit_linkage(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>) -> Option<Linkage> {
+        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.
+    pub fn is_instantiable(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>) -> bool {
+        debug!("is_instantiable({:?})", self);
+        let (def_id, substs) = match *self {
+            MonoItem::Fn(ref instance) => (instance.def_id(), instance.substs),
+            MonoItem::Static(def_id) => (def_id, InternalSubsts::empty()),
+            // global asm never has predicates
+            MonoItem::GlobalAsm(..) => return true
+        };
+
+        tcx.substitute_normalize_and_test_predicates((def_id, &substs))
+    }
+
+    pub fn to_string(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, debug: bool) -> String {
+        return match *self {
+            MonoItem::Fn(instance) => {
+                to_string_internal(tcx, "fn ", instance, debug)
+            },
+            MonoItem::Static(def_id) => {
+                let instance = Instance::new(def_id, tcx.intern_substs(&[]));
+                to_string_internal(tcx, "static ", instance, debug)
+            },
+            MonoItem::GlobalAsm(..) => {
+                "global_asm".to_string()
+            }
+        };
+
+        fn to_string_internal<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
+                                        prefix: &str,
+                                        instance: Instance<'tcx>,
+                                        debug: bool)
+                                        -> String {
+            let mut result = String::with_capacity(32);
+            result.push_str(prefix);
+            let printer = DefPathBasedNames::new(tcx, false, false);
+            printer.push_instance_as_string(instance, &mut result, debug);
+            result
+        }
+    }
+
+    pub fn local_span(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>) -> Option<Span> {
+        match *self {
+            MonoItem::Fn(Instance { def, .. }) => {
+                tcx.hir().as_local_hir_id(def.def_id())
+            }
+            MonoItem::Static(def_id) => {
+                tcx.hir().as_local_hir_id(def_id)
+            }
+            MonoItem::GlobalAsm(hir_id) => {
+                Some(hir_id)
+            }
+        }.map(|hir_id| tcx.hir().span_by_hir_id(hir_id))
+    }
 }
 
 impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for MonoItem<'tcx> {
diff --git a/src/librustc_codegen_ssa/base.rs b/src/librustc_codegen_ssa/base.rs
index fc3d0712fca..34e5b0681de 100644
--- a/src/librustc_codegen_ssa/base.rs
+++ b/src/librustc_codegen_ssa/base.rs
@@ -20,11 +20,10 @@ use rustc::hir::def_id::{DefId, LOCAL_CRATE};
 use rustc::middle::cstore::EncodedMetadata;
 use rustc::middle::lang_items::StartFnLangItem;
 use rustc::middle::weak_lang_items;
-use rustc::mir::mono::{CodegenUnitNameBuilder, CodegenUnit};
+use rustc::mir::mono::{CodegenUnitNameBuilder, CodegenUnit, MonoItem};
 use rustc::ty::{self, Ty, TyCtxt, Instance};
 use rustc::ty::layout::{self, Align, TyLayout, LayoutOf, VariantIdx, HasTyCtxt};
 use rustc::ty::query::Providers;
-use rustc::ty::print::obsolete::DefPathBasedNames;
 use rustc::middle::cstore::{self, LinkagePreference};
 use rustc::util::common::{time, print_time_passes_entry};
 use rustc::session::config::{self, EntryFnType, Lto};
@@ -42,7 +41,6 @@ use crate::callee;
 use crate::common::{RealPredicate, TypeKind, IntPredicate};
 use crate::meth;
 use crate::mir;
-use crate::mono_item::MonoItem;
 
 use crate::traits::*;
 
diff --git a/src/librustc_codegen_ssa/mono_item.rs b/src/librustc_codegen_ssa/mono_item.rs
index 60d75480e61..1c9c86a5f5d 100644
--- a/src/librustc_codegen_ssa/mono_item.rs
+++ b/src/librustc_codegen_ssa/mono_item.rs
@@ -1,19 +1,18 @@
 use rustc::hir;
 use rustc::mir::mono::{Linkage, Visibility};
 use rustc::ty::layout::HasTyCtxt;
-use std::fmt;
 use crate::base;
 use crate::traits::*;
 
-pub use rustc::mir::mono::MonoItem;
+use rustc::mir::mono::MonoItem;
 
-pub use rustc_mir::monomorphize::item::MonoItemExt as BaseMonoItemExt;
+pub trait MonoItemExt<'a, 'tcx: 'a> {
+    fn as_mono_item(&self) -> &MonoItem<'tcx>;
 
-pub trait MonoItemExt<'a, 'tcx: 'a>: fmt::Debug + BaseMonoItemExt<'a, 'tcx> {
     fn define<Bx: BuilderMethods<'a, 'tcx>>(&self, cx: &'a Bx::CodegenCx) {
         debug!("BEGIN IMPLEMENTING '{} ({})' in cgu {}",
-               self.to_string(cx.tcx(), true),
-               self.to_raw_string(),
+               self.as_mono_item().to_string(cx.tcx(), true),
+               self.as_mono_item().to_raw_string(),
                cx.codegen_unit().name());
 
         match *self.as_mono_item() {
@@ -34,8 +33,8 @@ pub trait MonoItemExt<'a, 'tcx: 'a>: fmt::Debug + BaseMonoItemExt<'a, 'tcx> {
         }
 
         debug!("END IMPLEMENTING '{} ({})' in cgu {}",
-               self.to_string(cx.tcx(), true),
-               self.to_raw_string(),
+               self.as_mono_item().to_string(cx.tcx(), true),
+               self.as_mono_item().to_raw_string(),
                cx.codegen_unit().name());
     }
 
@@ -46,11 +45,11 @@ pub trait MonoItemExt<'a, 'tcx: 'a>: fmt::Debug + BaseMonoItemExt<'a, 'tcx> {
         visibility: Visibility
     ) {
         debug!("BEGIN PREDEFINING '{} ({})' in cgu {}",
-               self.to_string(cx.tcx(), true),
-               self.to_raw_string(),
+               self.as_mono_item().to_string(cx.tcx(), true),
+               self.as_mono_item().to_raw_string(),
                cx.codegen_unit().name());
 
-        let symbol_name = self.symbol_name(cx.tcx()).as_str();
+        let symbol_name = self.as_mono_item().symbol_name(cx.tcx()).as_str();
 
         debug!("symbol {}", &symbol_name);
 
@@ -65,8 +64,8 @@ pub trait MonoItemExt<'a, 'tcx: 'a>: fmt::Debug + BaseMonoItemExt<'a, 'tcx> {
         }
 
         debug!("END PREDEFINING '{} ({})' in cgu {}",
-               self.to_string(cx.tcx(), true),
-               self.to_raw_string(),
+               self.as_mono_item().to_string(cx.tcx(), true),
+               self.as_mono_item().to_raw_string(),
                cx.codegen_unit().name());
     }
 
@@ -87,4 +86,8 @@ pub trait MonoItemExt<'a, 'tcx: 'a>: fmt::Debug + BaseMonoItemExt<'a, 'tcx> {
     }
 }
 
-impl<'a, 'tcx: 'a> MonoItemExt<'a, 'tcx> for MonoItem<'tcx> {}
+impl<'a, 'tcx: 'a> MonoItemExt<'a, 'tcx> for MonoItem<'tcx> {
+    fn as_mono_item(&self) -> &MonoItem<'tcx> {
+        self
+    }
+}
diff --git a/src/librustc_codegen_utils/symbol_names.rs b/src/librustc_codegen_utils/symbol_names.rs
index 9f710d7932d..47dc4e5b2ca 100644
--- a/src/librustc_codegen_utils/symbol_names.rs
+++ b/src/librustc_codegen_utils/symbol_names.rs
@@ -93,8 +93,7 @@ use rustc::hir::CodegenFnAttrFlags;
 use rustc::session::config::SymbolManglingVersion;
 use rustc::ty::query::Providers;
 use rustc::ty::{self, TyCtxt, Instance};
-use rustc::mir::mono::MonoItem;
-use rustc_mir::monomorphize::item::{InstantiationMode, MonoItemExt};
+use rustc::mir::mono::{MonoItem, InstantiationMode};
 
 use syntax_pos::symbol::InternedString;
 
diff --git a/src/librustc_mir/monomorphize/collector.rs b/src/librustc_mir/monomorphize/collector.rs
index 2fc42098669..eed051449e1 100644
--- a/src/librustc_mir/monomorphize/collector.rs
+++ b/src/librustc_mir/monomorphize/collector.rs
@@ -187,15 +187,13 @@ use rustc::ty::adjustment::{CustomCoerceUnsized, PointerCast};
 use rustc::session::config::EntryFnType;
 use rustc::mir::{self, Location, Place, PlaceBase, Promoted, Static, StaticKind};
 use rustc::mir::visit::Visitor as MirVisitor;
-use rustc::mir::mono::MonoItem;
+use rustc::mir::mono::{MonoItem, InstantiationMode};
 use rustc::mir::interpret::{Scalar, GlobalId, GlobalAlloc, ErrorHandled};
 
 use crate::monomorphize;
 use rustc::util::nodemap::{FxHashSet, FxHashMap, DefIdMap};
 use rustc::util::common::time;
 
-use crate::monomorphize::item::{MonoItemExt, InstantiationMode};
-
 use rustc_data_structures::bit_set::GrowableBitSet;
 use rustc_data_structures::sync::{MTRef, MTLock, ParallelIterator, par_iter};
 
diff --git a/src/librustc_mir/monomorphize/mod.rs b/src/librustc_mir/monomorphize/mod.rs
index f7b32dc84bd..51bcbff7f32 100644
--- a/src/librustc_mir/monomorphize/mod.rs
+++ b/src/librustc_mir/monomorphize/mod.rs
@@ -2,10 +2,7 @@ use rustc::traits;
 use rustc::ty::adjustment::CustomCoerceUnsized;
 use rustc::ty::{self, Ty, TyCtxt};
 
-pub use self::item::MonoItemExt;
-
 pub mod collector;
-pub mod item;
 pub mod partitioning;
 
 pub fn custom_coerce_unsize_info<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
diff --git a/src/librustc_mir/monomorphize/partitioning.rs b/src/librustc_mir/monomorphize/partitioning.rs
index cc90d6c95c9..6a7f67da1bf 100644
--- a/src/librustc_mir/monomorphize/partitioning.rs
+++ b/src/librustc_mir/monomorphize/partitioning.rs
@@ -108,11 +108,10 @@ use rustc::ty::print::characteristic_def_id_of_type;
 use rustc::ty::query::Providers;
 use rustc::util::common::time;
 use rustc::util::nodemap::{DefIdSet, FxHashMap, FxHashSet};
-use rustc::mir::mono::MonoItem;
+use rustc::mir::mono::{MonoItem, InstantiationMode};
 
 use crate::monomorphize::collector::InliningMap;
 use crate::monomorphize::collector::{self, MonoItemCollectionMode};
-use crate::monomorphize::item::{MonoItemExt, InstantiationMode};
 
 pub enum PartitioningStrategy {
     /// Generates one codegen unit per source-level module.