Clarify some methods around instance instantiation via comments and clearer names.

This commit is contained in:
Michael Woerister 2020-01-20 15:54:40 +01:00
parent e0bbe7915e
commit 29951edbdf
3 changed files with 19 additions and 7 deletions

View File

@ -79,7 +79,7 @@ impl<'tcx> MonoItem<'tcx> {
}
pub fn instantiation_mode(&self, tcx: TyCtxt<'tcx>) -> InstantiationMode {
let inline_in_all_cgus = tcx
let generate_cgu_internal_copies = tcx
.sess
.opts
.debugging_opts
@ -93,7 +93,7 @@ impl<'tcx> MonoItem<'tcx> {
// 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)
|| !instance.def.generates_cgu_internal_copy(tcx)
|| Some(instance.def_id()) == entry_def_id
{
return InstantiationMode::GloballyShared { may_conflict: false };
@ -102,7 +102,7 @@ impl<'tcx> MonoItem<'tcx> {
// 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 {
if generate_cgu_internal_copies {
return InstantiationMode::LocalCopy;
}

View File

@ -114,7 +114,12 @@ impl<'tcx> InstanceDef<'tcx> {
tcx.get_attrs(self.def_id())
}
pub fn is_inline(&self, tcx: TyCtxt<'tcx>) -> bool {
/// Returns `true` if the LLVM version of this instance is unconditionally
/// marked with `inline`. This implies that a copy of this instance is
/// generated in every codegen unit.
/// Note that this is only a hint. See the documentation for
/// `generates_cgu_internal_copy` for more information.
pub fn requires_inline(&self, tcx: TyCtxt<'tcx>) -> bool {
use crate::hir::map::DefPathData;
let def_id = match *self {
ty::InstanceDef::Item(def_id) => def_id,
@ -127,8 +132,15 @@ impl<'tcx> InstanceDef<'tcx> {
}
}
pub fn requires_local(&self, tcx: TyCtxt<'tcx>) -> bool {
if self.is_inline(tcx) {
/// Returns `true` if the machine code for this instance is instantiated in
/// each codegen unit that references it.
/// Note that this is only a hint! The compiler can globally decide to *not*
/// do this in order to speed up compilation. CGU-internal copies are
/// only exist to enable inlining. If inlining is not performed (e.g. at
/// `-Copt-level=0`) then the time for generating them is wasted and it's
/// better to create a single copy with external linkage.
pub fn generates_cgu_internal_copy(&self, tcx: TyCtxt<'tcx>) -> bool {
if self.requires_inline(tcx) {
return true;
}
if let ty::InstanceDef::DropGlue(..) = *self {

View File

@ -246,7 +246,7 @@ pub fn from_fn_attrs(
}
// FIXME(eddyb) consolidate these two `inline` calls (and avoid overwrites).
if instance.def.is_inline(cx.tcx) {
if instance.def.requires_inline(cx.tcx) {
inline(cx, llfn, attributes::InlineAttr::Hint);
}