Skip to content

Commit

Permalink
Auto merge of rust-lang#133929 - saethlin:remove-inline-in-all-cgus, …
Browse files Browse the repository at this point in the history
…r=<try>

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.

try-job: x86_64-gnu-nopt
  • Loading branch information
bors committed Jan 28, 2025
2 parents 2f348cb + bf9df97 commit 75620fd
Show file tree
Hide file tree
Showing 34 changed files with 187 additions and 367 deletions.
1 change: 0 additions & 1 deletion compiler/rustc_interface/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -797,7 +797,6 @@ fn test_unstable_options_tracking_hash() {
tracked!(function_sections, Some(false));
tracked!(human_readable_cgu_names, true);
tracked!(incremental_ignore_spans, true);
tracked!(inline_in_all_cgus, Some(true));
tracked!(inline_mir, Some(true));
tracked!(inline_mir_hint_threshold, Some(123));
tracked!(inline_mir_threshold, Some(123));
Expand Down
13 changes: 4 additions & 9 deletions compiler/rustc_middle/src/mir/mono.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,8 @@ impl<'tcx> MonoItem<'tcx> {
}

pub fn instantiation_mode(&self, tcx: TyCtxt<'tcx>) -> InstantiationMode {
let generate_cgu_internal_copies = tcx
.sess
.opts
.unstable_opts
.inline_in_all_cgus
.unwrap_or_else(|| tcx.sess.opts.optimize != OptLevel::No)
&& !tcx.sess.link_dead_code();
let generate_cgu_internal_copies =
(tcx.sess.opts.optimize != OptLevel::No) && !tcx.sess.link_dead_code();

match *self {
MonoItem::Fn(ref instance) => {
Expand All @@ -121,8 +116,8 @@ 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.
// inlined function. If this crate's build settings permit,
// we'll be creating a local copy per CGU.
if generate_cgu_internal_copies {
return InstantiationMode::LocalCopy;
}
Expand Down
2 changes: 0 additions & 2 deletions compiler/rustc_session/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1870,8 +1870,6 @@ options! {
"verify extended properties for incr. comp. (default: no):
- hashes of green query instances
- hash collisions of query keys"),
inline_in_all_cgus: Option<bool> = (None, parse_opt_bool, [TRACKED],
"control whether `#[inline]` functions are in all CGUs"),
inline_llvm: bool = (true, parse_bool, [TRACKED],
"enable LLVM inlining (default: yes)"),
inline_mir: Option<bool> = (None, parse_opt_bool, [TRACKED],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//
//@ compile-flags:-Zprint-mono-items=eager
//@ compile-flags:-Zinline-in-all-cgus
//@ compile-flags:-Zinline-mir=no
//@ compile-flags: -O

#![crate_type = "lib"]

Expand Down
3 changes: 1 addition & 2 deletions tests/codegen-units/item-collection/generic-drop-glue.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//
//@ compile-flags:-Zprint-mono-items=eager
//@ compile-flags:-Zinline-in-all-cgus
//@ compile-flags: -O

#![deny(dead_code)]
#![crate_type = "lib"]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//
//@ compile-flags:-Zprint-mono-items=eager -Zinline-in-all-cgus -Zmir-opt-level=0
//@ compile-flags:-Zprint-mono-items=eager -Zmir-opt-level=0

#![deny(dead_code)]
#![crate_type = "lib"]
Expand Down
3 changes: 1 addition & 2 deletions tests/codegen-units/item-collection/non-generic-drop-glue.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//
//@ compile-flags:-Zprint-mono-items=eager
//@ compile-flags:-Zinline-in-all-cgus
//@ compile-flags: -O

#![deny(dead_code)]
#![crate_type = "lib"]
Expand Down
3 changes: 1 addition & 2 deletions tests/codegen-units/item-collection/transitive-drop-glue.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//
//@ compile-flags:-Zprint-mono-items=eager
//@ compile-flags:-Zinline-in-all-cgus
//@ compile-flags: -O

#![deny(dead_code)]
#![crate_type = "lib"]
Expand Down
3 changes: 1 addition & 2 deletions tests/codegen-units/item-collection/tuple-drop-glue.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//
//@ compile-flags:-Zprint-mono-items=eager
//@ compile-flags:-Zinline-in-all-cgus
//@ compile-flags: -O

#![deny(dead_code)]
#![crate_type = "lib"]
Expand Down
1 change: 0 additions & 1 deletion tests/codegen-units/item-collection/unsizing.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//@ compile-flags:-Zprint-mono-items=eager
//@ compile-flags:-Zinline-in-all-cgus
//@ compile-flags:-Zmir-opt-level=0

#![deny(dead_code)]
Expand Down
14 changes: 14 additions & 0 deletions tests/codegen-units/partitioning/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# codegen-units/partitioning tests

This test suite is designed to test that codegen unit partitioning works as intended.
Note that it does not evaluate whether CGU partitioning is *good*. That is the job of the compiler benchmark suite.

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:
```
MONO_ITEM <item> @@ <cgu name>[<linkage>] <other cgu name>[<linkage in other cgu>]
```
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.

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.

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).
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// NOTE: We always compile this test with -Copt-level=0 because higher opt-levels
// prevent drop-glue from participating in share-generics.
//@ compile-flags:-Zshare-generics=yes -Copt-level=0
//@ no-prefer-dynamic
//@ compile-flags: -Zshare-generics=yes -Copt-level=0

#![crate_type = "rlib"]

Expand Down
9 changes: 4 additions & 5 deletions tests/codegen-units/partitioning/extern-drop-glue.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
// We specify incremental here because we want to test the partitioning for incremental compilation
// We specify opt-level=0 because `drop_in_place` is `Internal` when optimizing
//@ incremental
//@ compile-flags:-Zprint-mono-items=lazy
//@ compile-flags:-Zinline-in-all-cgus -Copt-level=0
//@ compile-flags: -Zprint-mono-items=lazy -Copt-level=0

#![allow(dead_code)]
#![crate_type = "rlib"]

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

// This test checks that drop glue is generated, even for types not defined in this crate, and all
// drop glue is put in the fallback CGU.

//~ 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]

struct LocalStruct(cgu_extern_drop_glue::Struct);
Expand Down
39 changes: 12 additions & 27 deletions tests/codegen-units/partitioning/extern-generic.rs
Original file line number Diff line number Diff line change
@@ -1,51 +1,36 @@
// We specify incremental here because we want to test the partitioning for incremental compilation
//@ incremental
//@ compile-flags:-Zprint-mono-items=eager -Zshare-generics=y
//@ compile-flags: -Zprint-mono-items=lazy -Copt-level=0

#![allow(dead_code)]
#![crate_type = "lib"]

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

//~ MONO_ITEM fn user @@ extern_generic[Internal]
fn user() {
// This test checks that, in an unoptimized build, a generic function and its callees are only
// instantiated once in this crate.

//~ MONO_ITEM fn user @@ extern_generic[External]
pub fn user() {
let _ = cgu_generic_function::foo("abc");
}

mod mod1 {
pub mod mod1 {
use cgu_generic_function;

//~ MONO_ITEM fn mod1::user @@ extern_generic-mod1[Internal]
fn user() {
//~ MONO_ITEM fn mod1::user @@ extern_generic-mod1[External]
pub fn user() {
let _ = cgu_generic_function::foo("abc");
}

mod mod1 {
pub mod mod1 {
use cgu_generic_function;

//~ MONO_ITEM fn mod1::mod1::user @@ extern_generic-mod1-mod1[Internal]
fn user() {
//~ MONO_ITEM fn mod1::mod1::user @@ extern_generic-mod1-mod1[External]
pub fn user() {
let _ = cgu_generic_function::foo("abc");
}
}
}

mod mod2 {
use cgu_generic_function;

//~ MONO_ITEM fn mod2::user @@ extern_generic-mod2[Internal]
fn user() {
let _ = cgu_generic_function::foo("abc");
}
}

mod mod3 {
//~ MONO_ITEM fn mod3::non_user @@ extern_generic-mod3[Internal]
fn non_user() {}
}

// Make sure the two generic functions from the extern crate get instantiated
// once for the current crate
//~ MONO_ITEM fn cgu_generic_function::foo::<&str> @@ cgu_generic_function-in-extern_generic.volatile[External]
//~ MONO_ITEM fn cgu_generic_function::bar::<&str> @@ cgu_generic_function-in-extern_generic.volatile[External]
9 changes: 4 additions & 5 deletions tests/codegen-units/partitioning/incremental-merging.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
// We specify incremental here because we want to test the partitioning for incremental compilation
//@ incremental
//@ compile-flags:-Zprint-mono-items=lazy
//@ compile-flags:-Ccodegen-units=3
//@ compile-flags: -Zprint-mono-items=lazy -Copt-level=0 -Ccodegen-units=3

#![crate_type = "rlib"]

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

pub mod aaa {
//~ MONO_ITEM fn aaa::foo @@ incremental_merging-aaa--incremental_merging-bbb[External]
Expand Down
38 changes: 38 additions & 0 deletions tests/codegen-units/partitioning/inline-always.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//@ incremental
//@ compile-flags: -Zprint-mono-items=lazy -Copt-level=0

#![crate_type = "lib"]

// This test checks that a monomorphic inline(always) function is instantiated in every CGU that
// references it, even though this is an unoptimized incremental build.
// It also checks that an inline(always) function is only placed in CGUs that reference it.

mod inline {
//~ MONO_ITEM fn inline::inlined_function @@ inline_always-user1[Internal] inline_always-user2[Internal]
#[inline(always)]
pub fn inlined_function() {}
}

pub mod user1 {
use super::inline;

//~ MONO_ITEM fn user1::foo @@ inline_always-user1[External]
pub fn foo() {
inline::inlined_function();
}
}

pub mod user2 {
use super::inline;

//~ MONO_ITEM fn user2::bar @@ inline_always-user2[External]
pub fn bar() {
inline::inlined_function();
}
}

pub mod non_user {

//~ MONO_ITEM fn non_user::baz @@ inline_always-non_user[External]
pub fn baz() {}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
// We specify incremental here because we want to test the partitioning for incremental compilation
//@ incremental
//@ compile-flags:-Zprint-mono-items=lazy
//@ compile-flags:-Zinline-in-all-cgus
//@ compile-flags: -Zprint-mono-items=lazy -Copt-level=1

#![crate_type = "lib"]

Expand Down
16 changes: 8 additions & 8 deletions tests/codegen-units/partitioning/local-drop-glue.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
// We specify incremental here because we want to test the partitioning for incremental compilation
// We specify opt-level=0 because `drop_in_place` is `Internal` when optimizing
//@ incremental
//@ compile-flags:-Zprint-mono-items=lazy
//@ compile-flags:-Zinline-in-all-cgus -Copt-level=0
//@ compile-flags: -Zprint-mono-items=lazy -Copt-level=0

#![allow(dead_code)]
#![crate_type = "rlib"]

// This test checks that drop glue is generated for types defined in this crate, and that all drop
// glue is put in the fallback CGU.
// This is rather similar to extern-drop-glue.rs.

//~ MONO_ITEM fn std::ptr::drop_in_place::<Struct> - shim(Some(Struct)) @@ local_drop_glue-fallback.cgu[External]
struct Struct {
pub struct Struct {
_a: u32,
}

Expand All @@ -18,7 +18,7 @@ impl Drop for Struct {
}

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

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

Expand Down
29 changes: 15 additions & 14 deletions tests/codegen-units/partitioning/local-generic.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// We specify incremental here because we want to test the partitioning for incremental compilation
//@ incremental
//@ compile-flags:-Zprint-mono-items=eager
//@ compile-flags: -Zprint-mono-items=lazy -Copt-level=0

#![allow(dead_code)]
#![crate_type = "lib"]

// This test checks that all the instantiations of a local generic fn are placed in the same CGU,
// regardless of where it is called.

//~ MONO_ITEM fn generic::<u32> @@ local_generic.volatile[External]
//~ MONO_ITEM fn generic::<u64> @@ local_generic.volatile[External]
//~ MONO_ITEM fn generic::<char> @@ local_generic.volatile[External]
Expand All @@ -13,34 +14,34 @@ pub fn generic<T>(x: T) -> T {
x
}

//~ MONO_ITEM fn user @@ local_generic[Internal]
fn user() {
//~ MONO_ITEM fn user @@ local_generic[External]
pub fn user() {
let _ = generic(0u32);
}

mod mod1 {
pub mod mod1 {
pub use super::generic;

//~ MONO_ITEM fn mod1::user @@ local_generic-mod1[Internal]
fn user() {
//~ MONO_ITEM fn mod1::user @@ local_generic-mod1[External]
pub fn user() {
let _ = generic(0u64);
}

mod mod1 {
pub mod mod1 {
use super::generic;

//~ MONO_ITEM fn mod1::mod1::user @@ local_generic-mod1-mod1[Internal]
fn user() {
//~ MONO_ITEM fn mod1::mod1::user @@ local_generic-mod1-mod1[External]
pub fn user() {
let _ = generic('c');
}
}
}

mod mod2 {
pub mod mod2 {
use super::generic;

//~ MONO_ITEM fn mod2::user @@ local_generic-mod2[Internal]
fn user() {
//~ MONO_ITEM fn mod2::user @@ local_generic-mod2[External]
pub fn user() {
let _ = generic("abc");
}
}
Loading

0 comments on commit 75620fd

Please sign in to comment.