Skip to content

Commit aa6f5ab

Browse files
committed
Auto merge of rust-lang#133929 - saethlin:remove-inline-in-all-cgus, r=nnethercote
Remove -Zinline-in-all-cgus and clean up tests/codegen-units/ Implementation of rust-lang/compiler-team#814 I've taken some liberties with cleaning up the CGU partitioning tests, because that's the only place this flag was used and also mattered. I've often fought a lot with the contents of `tests/codegen-units` and it has never been clear to me when a test failure indicates a problem with my changes as opposed to a test just needing to be manually blessed. Hopefully the combination of the new README, new comments, and using `-Zprint-mono-items=lazy` in the partitioning tests improves that. I've also deleted some of the `tests/run-make/sepcomp` tests. I think all the "sepcomp" tests have been obviated for years by better-designed (less flaky, clearer failures) test suites, but here I'm just deleting the ones I'm confident in.
2 parents 66d6064 + bf9df97 commit aa6f5ab

34 files changed

+187
-367
lines changed

compiler/rustc_interface/src/tests.rs

-1
Original file line numberDiff line numberDiff line change
@@ -797,7 +797,6 @@ fn test_unstable_options_tracking_hash() {
797797
tracked!(function_sections, Some(false));
798798
tracked!(human_readable_cgu_names, true);
799799
tracked!(incremental_ignore_spans, true);
800-
tracked!(inline_in_all_cgus, Some(true));
801800
tracked!(inline_mir, Some(true));
802801
tracked!(inline_mir_hint_threshold, Some(123));
803802
tracked!(inline_mir_threshold, Some(123));

compiler/rustc_middle/src/mir/mono.rs

+4-9
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,8 @@ impl<'tcx> MonoItem<'tcx> {
9191
}
9292

9393
pub fn instantiation_mode(&self, tcx: TyCtxt<'tcx>) -> InstantiationMode {
94-
let generate_cgu_internal_copies = tcx
95-
.sess
96-
.opts
97-
.unstable_opts
98-
.inline_in_all_cgus
99-
.unwrap_or_else(|| tcx.sess.opts.optimize != OptLevel::No)
100-
&& !tcx.sess.link_dead_code();
94+
let generate_cgu_internal_copies =
95+
(tcx.sess.opts.optimize != OptLevel::No) && !tcx.sess.link_dead_code();
10196

10297
match *self {
10398
MonoItem::Fn(ref instance) => {
@@ -121,8 +116,8 @@ impl<'tcx> MonoItem<'tcx> {
121116
}
122117

123118
// At this point we don't have explicit linkage and we're an
124-
// inlined function. If we're inlining into all CGUs then we'll
125-
// be creating a local copy per CGU.
119+
// inlined function. If this crate's build settings permit,
120+
// we'll be creating a local copy per CGU.
126121
if generate_cgu_internal_copies {
127122
return InstantiationMode::LocalCopy;
128123
}

compiler/rustc_session/src/options.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1870,8 +1870,6 @@ options! {
18701870
"verify extended properties for incr. comp. (default: no):
18711871
- hashes of green query instances
18721872
- hash collisions of query keys"),
1873-
inline_in_all_cgus: Option<bool> = (None, parse_opt_bool, [TRACKED],
1874-
"control whether `#[inline]` functions are in all CGUs"),
18751873
inline_llvm: bool = (true, parse_bool, [TRACKED],
18761874
"enable LLVM inlining (default: yes)"),
18771875
inline_mir: Option<bool> = (None, parse_opt_bool, [TRACKED],

tests/codegen-units/item-collection/drop_in_place_intrinsic.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
//
21
//@ compile-flags:-Zprint-mono-items=eager
3-
//@ compile-flags:-Zinline-in-all-cgus
42
//@ compile-flags:-Zinline-mir=no
3+
//@ compile-flags: -O
54

65
#![crate_type = "lib"]
76

tests/codegen-units/item-collection/generic-drop-glue.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
//
21
//@ compile-flags:-Zprint-mono-items=eager
3-
//@ compile-flags:-Zinline-in-all-cgus
2+
//@ compile-flags: -O
43

54
#![deny(dead_code)]
65
#![crate_type = "lib"]

tests/codegen-units/item-collection/instantiation-through-vtable.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
//
2-
//@ compile-flags:-Zprint-mono-items=eager -Zinline-in-all-cgus -Zmir-opt-level=0
1+
//@ compile-flags:-Zprint-mono-items=eager -Zmir-opt-level=0
32

43
#![deny(dead_code)]
54
#![crate_type = "lib"]

tests/codegen-units/item-collection/non-generic-drop-glue.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
//
21
//@ compile-flags:-Zprint-mono-items=eager
3-
//@ compile-flags:-Zinline-in-all-cgus
2+
//@ compile-flags: -O
43

54
#![deny(dead_code)]
65
#![crate_type = "lib"]

tests/codegen-units/item-collection/transitive-drop-glue.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
//
21
//@ compile-flags:-Zprint-mono-items=eager
3-
//@ compile-flags:-Zinline-in-all-cgus
2+
//@ compile-flags: -O
43

54
#![deny(dead_code)]
65
#![crate_type = "lib"]

tests/codegen-units/item-collection/tuple-drop-glue.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
//
21
//@ compile-flags:-Zprint-mono-items=eager
3-
//@ compile-flags:-Zinline-in-all-cgus
2+
//@ compile-flags: -O
43

54
#![deny(dead_code)]
65
#![crate_type = "lib"]

tests/codegen-units/item-collection/unsizing.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
//@ compile-flags:-Zprint-mono-items=eager
2-
//@ compile-flags:-Zinline-in-all-cgus
32
//@ compile-flags:-Zmir-opt-level=0
43

54
#![deny(dead_code)]
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# codegen-units/partitioning tests
2+
3+
This test suite is designed to test that codegen unit partitioning works as intended.
4+
Note that it does not evaluate whether CGU partitioning is *good*. That is the job of the compiler benchmark suite.
5+
6+
All tests in this suite use the flag `-Zprint-mono-items=lazy`, which makes the compiler print a machine-readable summary of all MonoItems that were collected, which CGUs they were assigned to, and the linkage in each CGU. The output looks like:
7+
```
8+
MONO_ITEM <item> @@ <cgu name>[<linkage>] <other cgu name>[<linkage in other cgu>]
9+
```
10+
DO NOT add tests to this suite that use `-Zprint-mono-items=eager`. That flag changes the way that MonoItem collection works in rather fundamental ways that are otherwise only used by `-Clink-dead-code`, and thus the MonoItems collected and their linkage under `-Zprint-mono-items=eager` does not correlate very well with normal compilation behavior.
11+
12+
The current CGU partitioning algorithm essentially groups MonoItems by which module they are defined in, then merges small CGUs. There are a lot of inline modules in this test suite because that's the only way to observe the partitioning.
13+
14+
Currently, the test suite is very heavily biased towards incremental builds with -Copt-level=0. This is mostly an accident of history; the entire test suite was added as part of supporting incremental compilation in #32779. But also CGU partitioning is *mostly* valuable because the CGU is the unit of incrementality to the codegen backend (cached queries are the unit of incrementality for the rest of the compiler).

tests/codegen-units/partitioning/auxiliary/shared_generics_aux.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// NOTE: We always compile this test with -Copt-level=0 because higher opt-levels
22
// prevent drop-glue from participating in share-generics.
3-
//@ compile-flags:-Zshare-generics=yes -Copt-level=0
4-
//@ no-prefer-dynamic
3+
//@ compile-flags: -Zshare-generics=yes -Copt-level=0
54

65
#![crate_type = "rlib"]
76

tests/codegen-units/partitioning/extern-drop-glue.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
// We specify incremental here because we want to test the partitioning for incremental compilation
2-
// We specify opt-level=0 because `drop_in_place` is `Internal` when optimizing
31
//@ incremental
4-
//@ compile-flags:-Zprint-mono-items=lazy
5-
//@ compile-flags:-Zinline-in-all-cgus -Copt-level=0
2+
//@ compile-flags: -Zprint-mono-items=lazy -Copt-level=0
63

7-
#![allow(dead_code)]
84
#![crate_type = "rlib"]
95

106
//@ aux-build:cgu_extern_drop_glue.rs
117
extern crate cgu_extern_drop_glue;
128

9+
// This test checks that drop glue is generated, even for types not defined in this crate, and all
10+
// drop glue is put in the fallback CGU.
11+
1312
//~ MONO_ITEM fn std::ptr::drop_in_place::<cgu_extern_drop_glue::Struct> - shim(Some(cgu_extern_drop_glue::Struct)) @@ extern_drop_glue-fallback.cgu[External]
1413

1514
struct LocalStruct(cgu_extern_drop_glue::Struct);
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,36 @@
1-
// We specify incremental here because we want to test the partitioning for incremental compilation
21
//@ incremental
3-
//@ compile-flags:-Zprint-mono-items=eager -Zshare-generics=y
2+
//@ compile-flags: -Zprint-mono-items=lazy -Copt-level=0
43

5-
#![allow(dead_code)]
64
#![crate_type = "lib"]
75

86
//@ aux-build:cgu_generic_function.rs
97
extern crate cgu_generic_function;
108

11-
//~ MONO_ITEM fn user @@ extern_generic[Internal]
12-
fn user() {
9+
// This test checks that, in an unoptimized build, a generic function and its callees are only
10+
// instantiated once in this crate.
11+
12+
//~ MONO_ITEM fn user @@ extern_generic[External]
13+
pub fn user() {
1314
let _ = cgu_generic_function::foo("abc");
1415
}
1516

16-
mod mod1 {
17+
pub mod mod1 {
1718
use cgu_generic_function;
1819

19-
//~ MONO_ITEM fn mod1::user @@ extern_generic-mod1[Internal]
20-
fn user() {
20+
//~ MONO_ITEM fn mod1::user @@ extern_generic-mod1[External]
21+
pub fn user() {
2122
let _ = cgu_generic_function::foo("abc");
2223
}
2324

24-
mod mod1 {
25+
pub mod mod1 {
2526
use cgu_generic_function;
2627

27-
//~ MONO_ITEM fn mod1::mod1::user @@ extern_generic-mod1-mod1[Internal]
28-
fn user() {
28+
//~ MONO_ITEM fn mod1::mod1::user @@ extern_generic-mod1-mod1[External]
29+
pub fn user() {
2930
let _ = cgu_generic_function::foo("abc");
3031
}
3132
}
3233
}
3334

34-
mod mod2 {
35-
use cgu_generic_function;
36-
37-
//~ MONO_ITEM fn mod2::user @@ extern_generic-mod2[Internal]
38-
fn user() {
39-
let _ = cgu_generic_function::foo("abc");
40-
}
41-
}
42-
43-
mod mod3 {
44-
//~ MONO_ITEM fn mod3::non_user @@ extern_generic-mod3[Internal]
45-
fn non_user() {}
46-
}
47-
48-
// Make sure the two generic functions from the extern crate get instantiated
49-
// once for the current crate
5035
//~ MONO_ITEM fn cgu_generic_function::foo::<&str> @@ cgu_generic_function-in-extern_generic.volatile[External]
5136
//~ MONO_ITEM fn cgu_generic_function::bar::<&str> @@ cgu_generic_function-in-extern_generic.volatile[External]

tests/codegen-units/partitioning/incremental-merging.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1-
// We specify incremental here because we want to test the partitioning for incremental compilation
21
//@ incremental
3-
//@ compile-flags:-Zprint-mono-items=lazy
4-
//@ compile-flags:-Ccodegen-units=3
2+
//@ compile-flags: -Zprint-mono-items=lazy -Copt-level=0 -Ccodegen-units=3
53

64
#![crate_type = "rlib"]
75

86
// This test makes sure that merging of CGUs works together with incremental
97
// compilation but at the same time does not modify names of CGUs that were not
108
// affected by merging.
119
//
12-
// We expect CGUs `aaa` and `bbb` to be merged (because they are the smallest),
13-
// while `ccc` and `ddd` are supposed to stay untouched.
10+
// CGU partitioning creates one CGU per module, so with 4 modules and codegen-units=3,
11+
// two of the modules should be merged. We expect CGUs `aaa` and `bbb` to be merged
12+
// (because they are the smallest), while `ccc` and `ddd` should stay untouched.
1413

1514
pub mod aaa {
1615
//~ MONO_ITEM fn aaa::foo @@ incremental_merging-aaa--incremental_merging-bbb[External]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//@ incremental
2+
//@ compile-flags: -Zprint-mono-items=lazy -Copt-level=0
3+
4+
#![crate_type = "lib"]
5+
6+
// This test checks that a monomorphic inline(always) function is instantiated in every CGU that
7+
// references it, even though this is an unoptimized incremental build.
8+
// It also checks that an inline(always) function is only placed in CGUs that reference it.
9+
10+
mod inline {
11+
//~ MONO_ITEM fn inline::inlined_function @@ inline_always-user1[Internal] inline_always-user2[Internal]
12+
#[inline(always)]
13+
pub fn inlined_function() {}
14+
}
15+
16+
pub mod user1 {
17+
use super::inline;
18+
19+
//~ MONO_ITEM fn user1::foo @@ inline_always-user1[External]
20+
pub fn foo() {
21+
inline::inlined_function();
22+
}
23+
}
24+
25+
pub mod user2 {
26+
use super::inline;
27+
28+
//~ MONO_ITEM fn user2::bar @@ inline_always-user2[External]
29+
pub fn bar() {
30+
inline::inlined_function();
31+
}
32+
}
33+
34+
pub mod non_user {
35+
36+
//~ MONO_ITEM fn non_user::baz @@ inline_always-non_user[External]
37+
pub fn baz() {}
38+
}

tests/codegen-units/partitioning/inlining-from-extern-crate.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
// We specify incremental here because we want to test the partitioning for incremental compilation
21
//@ incremental
3-
//@ compile-flags:-Zprint-mono-items=lazy
4-
//@ compile-flags:-Zinline-in-all-cgus
2+
//@ compile-flags: -Zprint-mono-items=lazy -Copt-level=1
53

64
#![crate_type = "lib"]
75

tests/codegen-units/partitioning/local-drop-glue.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
// We specify incremental here because we want to test the partitioning for incremental compilation
2-
// We specify opt-level=0 because `drop_in_place` is `Internal` when optimizing
31
//@ incremental
4-
//@ compile-flags:-Zprint-mono-items=lazy
5-
//@ compile-flags:-Zinline-in-all-cgus -Copt-level=0
2+
//@ compile-flags: -Zprint-mono-items=lazy -Copt-level=0
63

7-
#![allow(dead_code)]
84
#![crate_type = "rlib"]
95

6+
// This test checks that drop glue is generated for types defined in this crate, and that all drop
7+
// glue is put in the fallback CGU.
8+
// This is rather similar to extern-drop-glue.rs.
9+
1010
//~ MONO_ITEM fn std::ptr::drop_in_place::<Struct> - shim(Some(Struct)) @@ local_drop_glue-fallback.cgu[External]
11-
struct Struct {
11+
pub struct Struct {
1212
_a: u32,
1313
}
1414

@@ -18,7 +18,7 @@ impl Drop for Struct {
1818
}
1919

2020
//~ MONO_ITEM fn std::ptr::drop_in_place::<Outer> - shim(Some(Outer)) @@ local_drop_glue-fallback.cgu[External]
21-
struct Outer {
21+
pub struct Outer {
2222
_a: Struct,
2323
}
2424

@@ -33,7 +33,7 @@ pub mod mod1 {
3333
//~ MONO_ITEM fn std::ptr::drop_in_place::<mod1::Struct2> - shim(Some(mod1::Struct2)) @@ local_drop_glue-fallback.cgu[External]
3434
struct Struct2 {
3535
_a: Struct,
36-
//~ MONO_ITEM fn std::ptr::drop_in_place::<(u32, Struct)> - shim(Some((u32, Struct))) @@ local_drop_glue-fallback.cgu[Internal]
36+
//~ MONO_ITEM fn std::ptr::drop_in_place::<(u32, Struct)> - shim(Some((u32, Struct))) @@ local_drop_glue-fallback.cgu[External]
3737
_b: (u32, Struct),
3838
}
3939

Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
// We specify incremental here because we want to test the partitioning for incremental compilation
21
//@ incremental
3-
//@ compile-flags:-Zprint-mono-items=eager
2+
//@ compile-flags: -Zprint-mono-items=lazy -Copt-level=0
43

5-
#![allow(dead_code)]
64
#![crate_type = "lib"]
75

6+
// This test checks that all the instantiations of a local generic fn are placed in the same CGU,
7+
// regardless of where it is called.
8+
89
//~ MONO_ITEM fn generic::<u32> @@ local_generic.volatile[External]
910
//~ MONO_ITEM fn generic::<u64> @@ local_generic.volatile[External]
1011
//~ MONO_ITEM fn generic::<char> @@ local_generic.volatile[External]
@@ -13,34 +14,34 @@ pub fn generic<T>(x: T) -> T {
1314
x
1415
}
1516

16-
//~ MONO_ITEM fn user @@ local_generic[Internal]
17-
fn user() {
17+
//~ MONO_ITEM fn user @@ local_generic[External]
18+
pub fn user() {
1819
let _ = generic(0u32);
1920
}
2021

21-
mod mod1 {
22+
pub mod mod1 {
2223
pub use super::generic;
2324

24-
//~ MONO_ITEM fn mod1::user @@ local_generic-mod1[Internal]
25-
fn user() {
25+
//~ MONO_ITEM fn mod1::user @@ local_generic-mod1[External]
26+
pub fn user() {
2627
let _ = generic(0u64);
2728
}
2829

29-
mod mod1 {
30+
pub mod mod1 {
3031
use super::generic;
3132

32-
//~ MONO_ITEM fn mod1::mod1::user @@ local_generic-mod1-mod1[Internal]
33-
fn user() {
33+
//~ MONO_ITEM fn mod1::mod1::user @@ local_generic-mod1-mod1[External]
34+
pub fn user() {
3435
let _ = generic('c');
3536
}
3637
}
3738
}
3839

39-
mod mod2 {
40+
pub mod mod2 {
4041
use super::generic;
4142

42-
//~ MONO_ITEM fn mod2::user @@ local_generic-mod2[Internal]
43-
fn user() {
43+
//~ MONO_ITEM fn mod2::user @@ local_generic-mod2[External]
44+
pub fn user() {
4445
let _ = generic("abc");
4546
}
4647
}

0 commit comments

Comments
 (0)