From 3806bad6cf4f21bf9cf2085407384728bb71d673 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Thu, 1 Jun 2023 15:44:19 +1000 Subject: [PATCH] Simplify `place_inlined_mono_items`. Currently it overwrites all the CGUs with new CGUs. But those new CGUs are just copies of the old CGUs, possibly with some things added. This commit changes things so that each CGU just gets added to in place, which makes things simpler and clearer. --- .../rustc_monomorphize/src/partitioning.rs | 31 +++++-------------- 1 file changed, 7 insertions(+), 24 deletions(-) diff --git a/compiler/rustc_monomorphize/src/partitioning.rs b/compiler/rustc_monomorphize/src/partitioning.rs index 8932288a1617..d22031e25d6f 100644 --- a/compiler/rustc_monomorphize/src/partitioning.rs +++ b/compiler/rustc_monomorphize/src/partitioning.rs @@ -422,33 +422,22 @@ fn place_inlined_mono_items<'tcx>( let single_codegen_unit = codegen_units.len() == 1; - for old_codegen_unit in codegen_units.iter_mut() { + for cgu in codegen_units.iter_mut() { // Collect all items that need to be available in this codegen unit. let mut reachable = FxHashSet::default(); - for root in old_codegen_unit.items().keys() { + for root in cgu.items().keys() { follow_inlining(cx.tcx, *root, cx.usage_map, &mut reachable); } - let mut new_codegen_unit = CodegenUnit::new(old_codegen_unit.name()); - // Add all monomorphizations that are not already there. for mono_item in reachable { - if let Some(linkage) = old_codegen_unit.items().get(&mono_item) { - // This is a root, just copy it over. - new_codegen_unit.items_mut().insert(mono_item, *linkage); - } else { + if !cgu.items().contains_key(&mono_item) { if roots.contains(&mono_item) { - bug!( - "GloballyShared mono-item inlined into other CGU: \ - {:?}", - mono_item - ); + bug!("GloballyShared mono-item inlined into other CGU: {:?}", mono_item); } // This is a CGU-private copy. - new_codegen_unit - .items_mut() - .insert(mono_item, (Linkage::Internal, Visibility::Default)); + cgu.items_mut().insert(mono_item, (Linkage::Internal, Visibility::Default)); } if !single_codegen_unit { @@ -458,23 +447,17 @@ fn place_inlined_mono_items<'tcx>( Entry::Occupied(e) => { let placement = e.into_mut(); debug_assert!(match *placement { - MonoItemPlacement::SingleCgu { cgu_name } => { - cgu_name != new_codegen_unit.name() - } + MonoItemPlacement::SingleCgu { cgu_name } => cgu_name != cgu.name(), MonoItemPlacement::MultipleCgus => true, }); *placement = MonoItemPlacement::MultipleCgus; } Entry::Vacant(e) => { - e.insert(MonoItemPlacement::SingleCgu { - cgu_name: new_codegen_unit.name(), - }); + e.insert(MonoItemPlacement::SingleCgu { cgu_name: cgu.name() }); } } } } - - *old_codegen_unit = new_codegen_unit; } return mono_item_placements;