Skip to content

Commit 29951ed

Browse files
Clarify some methods around instance instantiation via comments and clearer names.
1 parent e0bbe79 commit 29951ed

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-7
lines changed

src/librustc/mir/mono.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ impl<'tcx> MonoItem<'tcx> {
7979
}
8080

8181
pub fn instantiation_mode(&self, tcx: TyCtxt<'tcx>) -> InstantiationMode {
82-
let inline_in_all_cgus = tcx
82+
let generate_cgu_internal_copies = tcx
8383
.sess
8484
.opts
8585
.debugging_opts
@@ -93,7 +93,7 @@ impl<'tcx> MonoItem<'tcx> {
9393
// If this function isn't inlined or otherwise has explicit
9494
// linkage, then we'll be creating a globally shared version.
9595
if self.explicit_linkage(tcx).is_some()
96-
|| !instance.def.requires_local(tcx)
96+
|| !instance.def.generates_cgu_internal_copy(tcx)
9797
|| Some(instance.def_id()) == entry_def_id
9898
{
9999
return InstantiationMode::GloballyShared { may_conflict: false };
@@ -102,7 +102,7 @@ impl<'tcx> MonoItem<'tcx> {
102102
// At this point we don't have explicit linkage and we're an
103103
// inlined function. If we're inlining into all CGUs then we'll
104104
// be creating a local copy per CGU
105-
if inline_in_all_cgus {
105+
if generate_cgu_internal_copies {
106106
return InstantiationMode::LocalCopy;
107107
}
108108

src/librustc/ty/instance.rs

+15-3
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,12 @@ impl<'tcx> InstanceDef<'tcx> {
114114
tcx.get_attrs(self.def_id())
115115
}
116116

117-
pub fn is_inline(&self, tcx: TyCtxt<'tcx>) -> bool {
117+
/// Returns `true` if the LLVM version of this instance is unconditionally
118+
/// marked with `inline`. This implies that a copy of this instance is
119+
/// generated in every codegen unit.
120+
/// Note that this is only a hint. See the documentation for
121+
/// `generates_cgu_internal_copy` for more information.
122+
pub fn requires_inline(&self, tcx: TyCtxt<'tcx>) -> bool {
118123
use crate::hir::map::DefPathData;
119124
let def_id = match *self {
120125
ty::InstanceDef::Item(def_id) => def_id,
@@ -127,8 +132,15 @@ impl<'tcx> InstanceDef<'tcx> {
127132
}
128133
}
129134

130-
pub fn requires_local(&self, tcx: TyCtxt<'tcx>) -> bool {
131-
if self.is_inline(tcx) {
135+
/// Returns `true` if the machine code for this instance is instantiated in
136+
/// each codegen unit that references it.
137+
/// Note that this is only a hint! The compiler can globally decide to *not*
138+
/// do this in order to speed up compilation. CGU-internal copies are
139+
/// only exist to enable inlining. If inlining is not performed (e.g. at
140+
/// `-Copt-level=0`) then the time for generating them is wasted and it's
141+
/// better to create a single copy with external linkage.
142+
pub fn generates_cgu_internal_copy(&self, tcx: TyCtxt<'tcx>) -> bool {
143+
if self.requires_inline(tcx) {
132144
return true;
133145
}
134146
if let ty::InstanceDef::DropGlue(..) = *self {

src/librustc_codegen_llvm/attributes.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ pub fn from_fn_attrs(
246246
}
247247

248248
// FIXME(eddyb) consolidate these two `inline` calls (and avoid overwrites).
249-
if instance.def.is_inline(cx.tcx) {
249+
if instance.def.requires_inline(cx.tcx) {
250250
inline(cx, llfn, attributes::InlineAttr::Hint);
251251
}
252252

0 commit comments

Comments
 (0)