Skip to content

Commit 25ac6de

Browse files
alexcrichtonMark-Simulacrum
authored andcommitted
Change how compiler-builtins gets many CGUs
This commit intends to fix an accidental regression from rust-lang#70846. The goal of rust-lang#70846 was to build compiler-builtins with a maximal number of CGUs to ensure that each module in the source corresponds to an object file. This high degree of control for compiler-builtins is desirable to ensure that there's at most one exported symbol per CGU, ideally enabling compiler-builtins to not conflict with the system libgcc as often. In rust-lang#70846, however, only part of the compiler understands that compiler-builtins is built with many CGUs. The rest of the compiler thinks it's building with `sess.codegen_units()`. Notably the calculation of `sess.lto()` consults `sess.codegen_units()`, which when there's only one CGU it disables ThinLTO. This means that compiler-builtins is built without ThinLTO, which is quite harmful to performance! This is the root of the cause from rust-lang#73135 where intrinsics were found to not be inlining trivial functions. The fix applied in this commit is to remove the special-casing of compiler-builtins in the compiler. Instead the build system is now responsible for special-casing compiler-builtins. It doesn't know exactly how many CGUs will be needed but it passes a large number that is assumed to be much greater than the number of source-level modules needed. After reading the various locations in the compiler source, this seemed like the best solution rather than adding more and more special casing in the compiler for compiler-builtins. Closes rust-lang#73135
1 parent ffebd8a commit 25ac6de

File tree

3 files changed

+15
-48
lines changed

3 files changed

+15
-48
lines changed

src/bootstrap/bin/rustc.rs

+14
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,20 @@ fn main() {
125125
} else {
126126
cmd.arg("-C").arg(format!("debug-assertions={}", debug_assertions));
127127
}
128+
129+
// For compiler-builtins we always use a high number of codegen units.
130+
// The goal here is to place every single intrinsic into its own object
131+
// file to avoid symbol clashes with the system libgcc if possible. Note
132+
// that this number doesn't actually produce this many object files, we
133+
// just don't create more than this number of object files.
134+
//
135+
// It's a bit of a bummer that we have to pass this here, unfortunately.
136+
// Ideally this would be specified through an env var to Cargo so Cargo
137+
// knows how many CGUs are for this specific crate, but for now
138+
// per-crate configuration isn't specifiable in the environment.
139+
if crate_name == Some("compiler_builtins") {
140+
cmd.arg("-Ccodegen-units=10000");
141+
}
128142
} else {
129143
// FIXME(rust-lang/cargo#5754) we shouldn't be using special env vars
130144
// here, but rather Cargo should know what flags to pass rustc itself.

src/librustc_mir/monomorphize/partitioning.rs

+1-8
Original file line numberDiff line numberDiff line change
@@ -454,18 +454,11 @@ fn default_visibility(tcx: TyCtxt<'_>, id: DefId, is_generic: bool) -> Visibilit
454454
fn merge_codegen_units<'tcx>(
455455
tcx: TyCtxt<'tcx>,
456456
initial_partitioning: &mut PreInliningPartitioning<'tcx>,
457-
mut target_cgu_count: usize,
457+
target_cgu_count: usize,
458458
) {
459459
assert!(target_cgu_count >= 1);
460460
let codegen_units = &mut initial_partitioning.codegen_units;
461461

462-
if tcx.is_compiler_builtins(LOCAL_CRATE) {
463-
// Compiler builtins require some degree of control over how mono items
464-
// are partitioned into compilation units. Provide it by keeping the
465-
// original partitioning when compiling the compiler builtins crate.
466-
target_cgu_count = codegen_units.len();
467-
}
468-
469462
// Note that at this point in time the `codegen_units` here may not be in a
470463
// deterministic order (but we know they're deterministically the same set).
471464
// We want this merging to produce a deterministic ordering of codegen units

src/test/codegen-units/partitioning/compiler-builtins.rs

-40
This file was deleted.

0 commit comments

Comments
 (0)