Skip to content

Commit d45fd5e

Browse files
committed
Refactor CGU partitioning a little
1 parent 7bb9888 commit d45fd5e

File tree

1 file changed

+23
-21
lines changed

1 file changed

+23
-21
lines changed

compiler/rustc_monomorphize/src/partitioning.rs

Lines changed: 23 additions & 21 deletions
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.
@@ -421,6 +409,14 @@ fn merge_codegen_units<'tcx>(
421409
// Don't update `cgu_contents`, that's only for incremental builds.
422410
}
423411

412+
cgu_contents
413+
}
414+
415+
fn rename_codegen_units<'tcx>(
416+
cx: &PartitioningCx<'_, 'tcx>,
417+
codegen_units: &mut Vec<CodegenUnit<'tcx>>,
418+
cgu_contents: UnordMap<Symbol, Vec<Symbol>>,
419+
) {
424420
let cgu_name_builder = &mut CodegenUnitNameBuilder::new(cx.tcx);
425421

426422
// Rename the newly merged CGUs.
@@ -678,13 +674,16 @@ fn characteristic_def_id_of_mono_item<'tcx>(
678674
}
679675
}
680676

681-
fn compute_codegen_unit_name(
682-
tcx: TyCtxt<'_>,
677+
fn compute_codegen_unit_name<'tcx>(
678+
tcx: TyCtxt<'tcx>,
683679
name_builder: &mut CodegenUnitNameBuilder<'_>,
684-
def_id: DefId,
685-
volatile: bool,
680+
mono_item: MonoItem<'tcx>,
686681
cache: &mut CguNameCache,
687682
) -> Symbol {
683+
let Some(def_id) = characteristic_def_id_of_mono_item(tcx, mono_item) else {
684+
return fallback_cgu_name(name_builder);
685+
};
686+
688687
// Find the innermost module that is not nested within a function.
689688
let mut current_def_id = def_id;
690689
let mut cgu_def_id = None;
@@ -712,6 +711,9 @@ fn compute_codegen_unit_name(
712711

713712
let cgu_def_id = cgu_def_id.unwrap();
714713

714+
let is_incremental_build = tcx.sess.opts.incremental.is_some();
715+
let volatile = is_incremental_build && mono_item.is_generic_fn();
716+
715717
*cache.entry((cgu_def_id, volatile)).or_insert_with(|| {
716718
let def_path = tcx.def_path(cgu_def_id);
717719

0 commit comments

Comments
 (0)