diff options
| author | Michael Woerister <michaelwoerister@posteo> | 2020-01-20 15:54:40 +0100 |
|---|---|---|
| committer | Michael Woerister <michaelwoerister@posteo> | 2020-01-23 13:15:14 +0100 |
| commit | 29951edbdfcb3e65e689e43c997a98d8aad5e273 (patch) | |
| tree | 4916fea8bd867e45c1881a38beae58295329acf1 | |
| parent | e0bbe7915e2b663ac84244918d6d06e0747ed33e (diff) | |
| download | rust-29951edbdfcb3e65e689e43c997a98d8aad5e273.tar.gz rust-29951edbdfcb3e65e689e43c997a98d8aad5e273.zip | |
Clarify some methods around instance instantiation via comments and clearer names.
| -rw-r--r-- | src/librustc/mir/mono.rs | 6 | ||||
| -rw-r--r-- | src/librustc/ty/instance.rs | 18 | ||||
| -rw-r--r-- | 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 51ce575e51f..475c77adebd 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 1ea695e40b2..c4770184612 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 4ed4e8ac6ef..fc1b365cf90 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); } |
