Skip to content

Commit 7cf1a94

Browse files
committed
Enforce the compiler-builtins partitioning scheme
1 parent ce55b20 commit 7cf1a94

File tree

3 files changed

+39
-35
lines changed

3 files changed

+39
-35
lines changed

compiler/rustc_mir_transform/src/cross_crate_inline.rs

+8
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ fn cross_crate_inlinable(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
3434
return true;
3535
}
3636

37+
// compiler-builtins only defines intrinsics (which are handled above by checking
38+
// contains_extern_indicator) and helper functions used by those intrinsics. The helper
39+
// functions should always be inlined
40+
// See https://github.com/rust-lang/rust/issues/73135
41+
if tcx.is_compiler_builtins(rustc_span::def_id::LOCAL_CRATE) {
42+
return true;
43+
}
44+
3745
if tcx.has_attr(def_id, sym::rustc_intrinsic) {
3846
// Intrinsic fallback bodies are always cross-crate inlineable.
3947
// To ensure that the MIR inliner doesn't cluelessly try to inline fallback

compiler/rustc_monomorphize/src/partitioning.rs

+31-22
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,8 @@ where
165165
// estimates.
166166
{
167167
let _prof_timer = tcx.prof.generic_activity("cgu_partitioning_merge_cgus");
168-
merge_codegen_units(cx, &mut codegen_units);
168+
let cgu_contents = merge_codegen_units(cx, &mut codegen_units);
169+
rename_codegen_units(cx, &mut codegen_units, cgu_contents);
169170
debug_dump(tcx, "MERGE", &codegen_units);
170171
}
171172

@@ -200,7 +201,6 @@ where
200201
I: Iterator<Item = MonoItem<'tcx>>,
201202
{
202203
let mut codegen_units = UnordMap::default();
203-
let is_incremental_build = cx.tcx.sess.opts.incremental.is_some();
204204
let mut internalization_candidates = UnordSet::default();
205205

206206
// Determine if monomorphizations instantiated in this crate will be made
@@ -227,20 +227,8 @@ where
227227
}
228228
}
229229

230-
let characteristic_def_id = characteristic_def_id_of_mono_item(cx.tcx, mono_item);
231-
let is_volatile = is_incremental_build && mono_item.is_generic_fn();
232-
233-
let cgu_name = match characteristic_def_id {
234-
Some(def_id) => compute_codegen_unit_name(
235-
cx.tcx,
236-
cgu_name_builder,
237-
def_id,
238-
is_volatile,
239-
cgu_name_cache,
240-
),
241-
None => fallback_cgu_name(cgu_name_builder),
242-
};
243-
230+
let cgu_name =
231+
compute_codegen_unit_name(cx.tcx, cgu_name_builder, mono_item, cgu_name_cache);
244232
let cgu = codegen_units.entry(cgu_name).or_insert_with(|| CodegenUnit::new(cgu_name));
245233

246234
let mut can_be_internalized = true;
@@ -321,7 +309,7 @@ where
321309
fn merge_codegen_units<'tcx>(
322310
cx: &PartitioningCx<'_, 'tcx>,
323311
codegen_units: &mut Vec<CodegenUnit<'tcx>>,
324-
) {
312+
) -> UnordMap<Symbol, Vec<Symbol>> {
325313
assert!(cx.tcx.sess.codegen_units().as_usize() >= 1);
326314

327315
// A sorted order here ensures merging is deterministic.
@@ -331,6 +319,10 @@ fn merge_codegen_units<'tcx>(
331319
let mut cgu_contents: UnordMap<Symbol, Vec<Symbol>> =
332320
codegen_units.iter().map(|cgu| (cgu.name(), vec![cgu.name()])).collect();
333321

322+
if cx.tcx.is_compiler_builtins(LOCAL_CRATE) {
323+
return cgu_contents;
324+
}
325+
334326
// If N is the maximum number of CGUs, and the CGUs are sorted from largest
335327
// to smallest, we repeatedly find which CGU in codegen_units[N..] has the
336328
// greatest overlap of inlined items with codegen_units[N-1], merge that
@@ -421,8 +413,15 @@ fn merge_codegen_units<'tcx>(
421413
// Don't update `cgu_contents`, that's only for incremental builds.
422414
}
423415

424-
let cgu_name_builder = &mut CodegenUnitNameBuilder::new(cx.tcx);
416+
cgu_contents
417+
}
425418

419+
fn rename_codegen_units<'tcx>(
420+
cx: &PartitioningCx<'_, 'tcx>,
421+
codegen_units: &mut Vec<CodegenUnit<'tcx>>,
422+
cgu_contents: UnordMap<Symbol, Vec<Symbol>>,
423+
) {
424+
let cgu_name_builder = &mut CodegenUnitNameBuilder::new(cx.tcx);
426425
// Rename the newly merged CGUs.
427426
if cx.tcx.sess.opts.incremental.is_some() {
428427
// If we are doing incremental compilation, we want CGU names to
@@ -678,13 +677,21 @@ fn characteristic_def_id_of_mono_item<'tcx>(
678677
}
679678
}
680679

681-
fn compute_codegen_unit_name(
682-
tcx: TyCtxt<'_>,
680+
fn compute_codegen_unit_name<'tcx>(
681+
tcx: TyCtxt<'tcx>,
683682
name_builder: &mut CodegenUnitNameBuilder<'_>,
684-
def_id: DefId,
685-
volatile: bool,
683+
mono_item: MonoItem<'tcx>,
686684
cache: &mut CguNameCache,
687685
) -> Symbol {
686+
if tcx.is_compiler_builtins(LOCAL_CRATE) {
687+
let name = mono_item.symbol_name(tcx);
688+
return Symbol::intern(name.name);
689+
}
690+
691+
let Some(def_id) = characteristic_def_id_of_mono_item(tcx, mono_item) else {
692+
return fallback_cgu_name(name_builder);
693+
};
694+
688695
// Find the innermost module that is not nested within a function.
689696
let mut current_def_id = def_id;
690697
let mut cgu_def_id = None;
@@ -712,6 +719,8 @@ fn compute_codegen_unit_name(
712719

713720
let cgu_def_id = cgu_def_id.unwrap();
714721

722+
let is_incremental_build = tcx.sess.opts.incremental.is_some();
723+
let volatile = is_incremental_build && mono_item.is_generic_fn();
715724
*cache.entry((cgu_def_id, volatile)).or_insert_with(|| {
716725
let def_path = tcx.def_path(cgu_def_id);
717726

library/Cargo.toml

-13
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,6 @@ exclude = [
1111
"windows_targets"
1212
]
1313

14-
[profile.release.package.compiler_builtins]
15-
# For compiler-builtins we always use a high number of codegen units.
16-
# The goal here is to place every single intrinsic into its own object
17-
# file to avoid symbol clashes with the system libgcc if possible. Note
18-
# that this number doesn't actually produce this many object files, we
19-
# just don't create more than this number of object files.
20-
#
21-
# It's a bit of a bummer that we have to pass this here, unfortunately.
22-
# Ideally this would be specified through an env var to Cargo so Cargo
23-
# knows how many CGUs are for this specific crate, but for now
24-
# per-crate configuration isn't specifiable in the environment.
25-
codegen-units = 10000
26-
2714
# These dependencies of the standard library implement symbolication for
2815
# backtraces on most platforms. Their debuginfo causes both linking to be slower
2916
# (more data to chew through) and binaries to be larger without really all that

0 commit comments

Comments
 (0)