Skip to content

Commit

Permalink
Simplify place_inlined_mono_items.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nnethercote committed Jun 2, 2023
1 parent f1fe797 commit 3806bad
Showing 1 changed file with 7 additions and 24 deletions.
31 changes: 7 additions & 24 deletions compiler/rustc_monomorphize/src/partitioning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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;
Expand Down

0 comments on commit 3806bad

Please sign in to comment.