@@ -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
421445fn internalize_symbols < ' tcx > (
0 commit comments