Skip to content

Commit ddf2cc7

Browse files
committed
Auto merge of rust-lang#76896 - spastorino:codegen-inline-fns2, r=davidtwco,wesleywiser
Do not make local copies of inline fns in debug mode r? `@wesleywiser` cc `@rust-lang/wg-incr-comp` If this is correct it supersedes rust-lang#76889 Related to rust-lang#54089
2 parents 937f629 + 07a5982 commit ddf2cc7

File tree

4 files changed

+39
-26
lines changed

4 files changed

+39
-26
lines changed

compiler/rustc_middle/src/mir/mono.rs

+18-22
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use crate::dep_graph::{DepConstructor, DepNode, WorkProduct, WorkProductId};
22
use crate::ich::{NodeIdHashingMode, StableHashingContext};
33
use crate::ty::{subst::InternalSubsts, Instance, InstanceDef, SymbolName, TyCtxt};
4-
use rustc_attr::InlineAttr;
54
use rustc_data_structures::base_n;
65
use rustc_data_structures::fingerprint::Fingerprint;
76
use rustc_data_structures::fx::FxHashMap;
@@ -79,14 +78,6 @@ impl<'tcx> MonoItem<'tcx> {
7978
}
8079

8180
pub fn instantiation_mode(&self, tcx: TyCtxt<'tcx>) -> InstantiationMode {
82-
let generate_cgu_internal_copies = tcx
83-
.sess
84-
.opts
85-
.debugging_opts
86-
.inline_in_all_cgus
87-
.unwrap_or_else(|| tcx.sess.opts.optimize != OptLevel::No)
88-
&& !tcx.sess.link_dead_code();
89-
9081
match *self {
9182
MonoItem::Fn(ref instance) => {
9283
let entry_def_id = tcx.entry_fn(LOCAL_CRATE).map(|(id, _)| id);
@@ -99,21 +90,26 @@ impl<'tcx> MonoItem<'tcx> {
9990
return InstantiationMode::GloballyShared { may_conflict: false };
10091
}
10192

93+
let generate_cgu_internal_copies = tcx
94+
.sess
95+
.opts
96+
.debugging_opts
97+
.inline_in_all_cgus
98+
.unwrap_or_else(|| tcx.sess.opts.optimize != OptLevel::No)
99+
&& !tcx.sess.link_dead_code();
100+
102101
// At this point we don't have explicit linkage and we're an
103-
// inlined function. If we're inlining into all CGUs then we'll
104-
// be creating a local copy per CGU.
102+
// inlined function. If we should generate local copies for each CGU,
103+
// then return `LocalCopy`, otherwise we'll just generate one copy
104+
// and share it with all CGUs in this crate.
105105
if generate_cgu_internal_copies {
106-
return InstantiationMode::LocalCopy;
107-
}
108-
109-
// Finally, if this is `#[inline(always)]` we're sure to respect
110-
// that with an inline copy per CGU, but otherwise we'll be
111-
// creating one copy of this `#[inline]` function which may
112-
// conflict with upstream crates as it could be an exported
113-
// symbol.
114-
match tcx.codegen_fn_attrs(instance.def_id()).inline {
115-
InlineAttr::Always => InstantiationMode::LocalCopy,
116-
_ => InstantiationMode::GloballyShared { may_conflict: true },
106+
InstantiationMode::LocalCopy
107+
} else {
108+
// Finally, if we've reached this point, then we should optimize for
109+
// compilation speed. In that regard, we will ignore any `#[inline]`
110+
// annotations on the function and simply codegen it as usual. This could
111+
// conflict with upstream crates as it could be an exported symbol.
112+
InstantiationMode::GloballyShared { may_conflict: true }
117113
}
118114
}
119115
MonoItem::Static(..) | MonoItem::GlobalAsm(..) => {

src/test/incremental/hygiene/load_cached_hygiene.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// revisions:rpass1 rpass2
2-
// compile-flags: -Z query-dep-graph
2+
// compile-flags: -Z query-dep-graph -O
33
// aux-build:cached_hygiene.rs
44

55
// This tests the folllowing scenario
@@ -19,7 +19,12 @@
1919
// the metadata. Specifically, we were not resetting `orig_id`
2020
// for an `EpxnData` generate in the current crate, which would cause
2121
// us to serialize the `ExpnId` pointing to a garbage location in
22-
// the metadata.
22+
// the metadata.o
23+
24+
// NOTE: We're explicitly passing the `-O` optimization flag because if optimizations are not
25+
// enabled, then rustc will ignore the `#[inline(always)]` attribute which means we do not load
26+
// the optimized mir for the unmodified function to be loaded and so the CGU containing that
27+
// function will be reused.
2328

2429
#![feature(rustc_attrs)]
2530

src/test/incremental/remapped_paths_cc/main.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
// revisions:rpass1 rpass2 rpass3
2-
// compile-flags: -Z query-dep-graph -g
2+
// compile-flags: -Z query-dep-graph -g -O
33
// aux-build:extern_crate.rs
44

55
// ignore-asmjs wasm2js does not support source maps yet
6+
67
// This test case makes sure that we detect if paths emitted into debuginfo
78
// are changed, even when the change happens in an external crate.
89

10+
// NOTE: We're explicitly passing the `-O` optimization flag because if no optimizations are
11+
// requested, rustc will ignore the `#[inline]` attribute. This is a performance optimization for
12+
// non-optimized builds which causes us to generate fewer copies of inlined functions when
13+
// runtime performance doesn't matter. Without this flag, the function will go into a different
14+
// CGU which can be reused by this crate.
15+
916
#![feature(rustc_attrs)]
1017

1118
#![rustc_partition_reused(module="main", cfg="rpass2")]

src/test/run-make-fulldeps/inline-always-many-cgu/Makefile

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
-include ../tools.mk
22

33
all:
4-
$(RUSTC) foo.rs --emit llvm-ir -C codegen-units=2
4+
$(RUSTC) foo.rs --emit llvm-ir -C codegen-units=2 -C opt-level=0
5+
if ![cat $(TMPDIR)/*.ll | $(CGREP) -e '\bcall\b']; then \
6+
echo "not found call instruction when one was expected"; \
7+
exit 1; \
8+
fi
9+
$(RUSTC) foo.rs --emit llvm-ir -C codegen-units=2 -C opt-level=1
510
if cat $(TMPDIR)/*.ll | $(CGREP) -e '\bcall\b'; then \
611
echo "found call instruction when one wasn't expected"; \
712
exit 1; \

0 commit comments

Comments
 (0)