Skip to content

Commit bad0688

Browse files
authored
Rollup merge of #112946 - nnethercote:improve-cgu-naming-and-ordering, r=wesleywiser
Improve cgu naming and ordering Some quality of life improvements when debugging and profiling CGU formation. r? `@wesleywiser`
2 parents eb76764 + 666b1b6 commit bad0688

File tree

2 files changed

+72
-27
lines changed

2 files changed

+72
-27
lines changed

compiler/rustc_codegen_ssa/src/back/write.rs

+42-21
Original file line numberDiff line numberDiff line change
@@ -698,28 +698,49 @@ impl<B: WriteBackendMethods> WorkItem<B> {
698698

699699
/// Generate a short description of this work item suitable for use as a thread name.
700700
fn short_description(&self) -> String {
701-
// `pthread_setname()` on *nix is limited to 15 characters and longer names are ignored.
702-
// Use very short descriptions in this case to maximize the space available for the module name.
703-
// Windows does not have that limitation so use slightly more descriptive names there.
701+
// `pthread_setname()` on *nix ignores anything beyond the first 15
702+
// bytes. Use short descriptions to maximize the space available for
703+
// the module name.
704+
#[cfg(not(windows))]
705+
fn desc(short: &str, _long: &str, name: &str) -> String {
706+
// The short label is three bytes, and is followed by a space. That
707+
// leaves 11 bytes for the CGU name. How we obtain those 11 bytes
708+
// depends on the the CGU name form.
709+
//
710+
// - Non-incremental, e.g. `regex.f10ba03eb5ec7975-cgu.0`: the part
711+
// before the `-cgu.0` is the same for every CGU, so use the
712+
// `cgu.0` part. The number suffix will be different for each
713+
// CGU.
714+
//
715+
// - Incremental (normal), e.g. `2i52vvl2hco29us0`: use the whole
716+
// name because each CGU will have a unique ASCII hash, and the
717+
// first 11 bytes will be enough to identify it.
718+
//
719+
// - Incremental (with `-Zhuman-readable-cgu-names`), e.g.
720+
// `regex.f10ba03eb5ec7975-re_builder.volatile`: use the whole
721+
// name. The first 11 bytes won't be enough to uniquely identify
722+
// it, but no obvious substring will, and this is a rarely used
723+
// option so it doesn't matter much.
724+
//
725+
assert_eq!(short.len(), 3);
726+
let name = if let Some(index) = name.find("-cgu.") {
727+
&name[index + 1..] // +1 skips the leading '-'.
728+
} else {
729+
name
730+
};
731+
format!("{short} {name}")
732+
}
733+
734+
// Windows has no thread name length limit, so use more descriptive names.
735+
#[cfg(windows)]
736+
fn desc(_short: &str, long: &str, name: &str) -> String {
737+
format!("{long} {name}")
738+
}
739+
704740
match self {
705-
WorkItem::Optimize(m) => {
706-
#[cfg(windows)]
707-
return format!("optimize module {}", m.name);
708-
#[cfg(not(windows))]
709-
return format!("opt {}", m.name);
710-
}
711-
WorkItem::CopyPostLtoArtifacts(m) => {
712-
#[cfg(windows)]
713-
return format!("copy LTO artifacts for {}", m.name);
714-
#[cfg(not(windows))]
715-
return format!("copy {}", m.name);
716-
}
717-
WorkItem::LTO(m) => {
718-
#[cfg(windows)]
719-
return format!("LTO module {}", m.name());
720-
#[cfg(not(windows))]
721-
return format!("LTO {}", m.name());
722-
}
741+
WorkItem::Optimize(m) => desc("opt", "optimize module {}", &m.name),
742+
WorkItem::CopyPostLtoArtifacts(m) => desc("cpy", "copy LTO artifacts for {}", &m.name),
743+
WorkItem::LTO(m) => desc("lto", "LTO module {}", m.name()),
723744
}
724745
}
725746
}

compiler/rustc_monomorphize/src/partitioning.rs

+30-6
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,7 @@ fn merge_codegen_units<'tcx>(
368368

369369
let cgu_name_builder = &mut CodegenUnitNameBuilder::new(cx.tcx);
370370

371+
// Rename the newly merged CGUs.
371372
if cx.tcx.sess.opts.incremental.is_some() {
372373
// If we are doing incremental compilation, we want CGU names to
373374
// reflect the path of the source level module they correspond to.
@@ -404,18 +405,41 @@ fn merge_codegen_units<'tcx>(
404405
}
405406
}
406407
}
408+
409+
// A sorted order here ensures what follows can be deterministic.
410+
codegen_units.sort_by(|a, b| a.name().as_str().cmp(b.name().as_str()));
407411
} else {
408-
// If we are compiling non-incrementally we just generate simple CGU
409-
// names containing an index.
412+
// When compiling non-incrementally, we rename the CGUS so they have
413+
// identical names except for the numeric suffix, something like
414+
// `regex.f10ba03eb5ec7975-cgu.N`, where `N` varies.
415+
//
416+
// It is useful for debugging and profiling purposes if the resulting
417+
// CGUs are sorted by name *and* reverse sorted by size. (CGU 0 is the
418+
// biggest, CGU 1 is the second biggest, etc.)
419+
//
420+
// So first we reverse sort by size. Then we generate the names with
421+
// zero-padded suffixes, which means they are automatically sorted by
422+
// names. The numeric suffix width depends on the number of CGUs, which
423+
// is always greater than zero:
424+
// - [1,9] CGUS: `0`, `1`, `2`, ...
425+
// - [10,99] CGUS: `00`, `01`, `02`, ...
426+
// - [100,999] CGUS: `000`, `001`, `002`, ...
427+
// - etc.
428+
//
429+
// If we didn't zero-pad the sorted-by-name order would be `XYZ-cgu.0`,
430+
// `XYZ-cgu.1`, `XYZ-cgu.10`, `XYZ-cgu.11`, ..., `XYZ-cgu.2`, etc.
431+
codegen_units.sort_by_key(|cgu| cmp::Reverse(cgu.size_estimate()));
432+
let num_digits = codegen_units.len().ilog10() as usize + 1;
410433
for (index, cgu) in codegen_units.iter_mut().enumerate() {
434+
// Note: `WorkItem::short_description` depends on this name ending
435+
// with `-cgu.` followed by a numeric suffix. Please keep it in
436+
// sync with this code.
437+
let suffix = format!("{index:0num_digits$}");
411438
let numbered_codegen_unit_name =
412-
cgu_name_builder.build_cgu_name_no_mangle(LOCAL_CRATE, &["cgu"], Some(index));
439+
cgu_name_builder.build_cgu_name_no_mangle(LOCAL_CRATE, &["cgu"], Some(suffix));
413440
cgu.set_name(numbered_codegen_unit_name);
414441
}
415442
}
416-
417-
// A sorted order here ensures what follows can be deterministic.
418-
codegen_units.sort_by(|a, b| a.name().as_str().cmp(b.name().as_str()));
419443
}
420444

421445
fn internalize_symbols<'tcx>(

0 commit comments

Comments
 (0)