From 29951edbdfcb3e65e689e43c997a98d8aad5e273 Mon Sep 17 00:00:00 2001 From: Michael Woerister Date: Mon, 20 Jan 2020 15:54:40 +0100 Subject: [PATCH] Clarify some methods around instance instantiation via comments and clearer names. --- src/librustc/mir/mono.rs | 6 +++--- src/librustc/ty/instance.rs | 18 +++++++++++++++--- src/librustc_codegen_llvm/attributes.rs | 2 +- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/librustc/mir/mono.rs b/src/librustc/mir/mono.rs index 51ce575e51f3..475c77adebd1 100644 --- a/src/librustc/mir/mono.rs +++ b/src/librustc/mir/mono.rs @@ -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; } diff --git a/src/librustc/ty/instance.rs b/src/librustc/ty/instance.rs index 1ea695e40b25..c4770184612c 100644 --- a/src/librustc/ty/instance.rs +++ b/src/librustc/ty/instance.rs @@ -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 { diff --git a/src/librustc_codegen_llvm/attributes.rs b/src/librustc_codegen_llvm/attributes.rs index 4ed4e8ac6efa..fc1b365cf90c 100644 --- a/src/librustc_codegen_llvm/attributes.rs +++ b/src/librustc_codegen_llvm/attributes.rs @@ -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); }