Tweak CGU size estimate code.

- Rename `create_size_estimate` as `compute_size_estimate`, because that
  makes more sense for the second and subsequent calls for each CGU.
- Change `CodegenUnit::size_estimate` from `Option<usize>` to `usize`.
  We can still assert that `compute_size_estimate` is called first.
- Move the size estimation for `place_mono_items` inside the function,
  for consistency with `merge_codegen_units`.
This commit is contained in:
Nicholas Nethercote 2023-06-21 16:38:47 +10:00
parent 105ac1c26d
commit abde9ba527
2 changed files with 17 additions and 14 deletions

View File

@ -231,7 +231,7 @@ pub struct CodegenUnit<'tcx> {
/// as well as the crate name and disambiguator. /// as well as the crate name and disambiguator.
name: Symbol, name: Symbol,
items: FxHashMap<MonoItem<'tcx>, (Linkage, Visibility)>, items: FxHashMap<MonoItem<'tcx>, (Linkage, Visibility)>,
size_estimate: Option<usize>, size_estimate: usize,
primary: bool, primary: bool,
/// True if this is CGU is used to hold code coverage information for dead code, /// True if this is CGU is used to hold code coverage information for dead code,
/// false otherwise. /// false otherwise.
@ -269,7 +269,7 @@ impl<'tcx> CodegenUnit<'tcx> {
CodegenUnit { CodegenUnit {
name, name,
items: Default::default(), items: Default::default(),
size_estimate: None, size_estimate: 0,
primary: false, primary: false,
is_code_coverage_dead_code_cgu: false, is_code_coverage_dead_code_cgu: false,
} }
@ -320,19 +320,21 @@ impl<'tcx> CodegenUnit<'tcx> {
base_n::encode(hash, base_n::CASE_INSENSITIVE) base_n::encode(hash, base_n::CASE_INSENSITIVE)
} }
pub fn create_size_estimate(&mut self, tcx: TyCtxt<'tcx>) { pub fn compute_size_estimate(&mut self, tcx: TyCtxt<'tcx>) {
// Estimate the size of a codegen unit as (approximately) the number of MIR // Estimate the size of a codegen unit as (approximately) the number of MIR
// statements it corresponds to. // statements it corresponds to.
self.size_estimate = Some(self.items.keys().map(|mi| mi.size_estimate(tcx)).sum()); self.size_estimate = self.items.keys().map(|mi| mi.size_estimate(tcx)).sum();
} }
#[inline] #[inline]
/// Should only be called if [`create_size_estimate`] has previously been called. /// Should only be called if [`compute_size_estimate`] has previously been called.
/// ///
/// [`create_size_estimate`]: Self::create_size_estimate /// [`compute_size_estimate`]: Self::compute_size_estimate
pub fn size_estimate(&self) -> usize { pub fn size_estimate(&self) -> usize {
// Items are never zero-sized, so if we have items the estimate must be
// non-zero, unless we forgot to call `compute_size_estimate` first.
assert!(self.items.is_empty() || self.size_estimate != 0);
self.size_estimate self.size_estimate
.expect("create_size_estimate must be called before getting a size_estimate")
} }
pub fn contains_item(&self, item: &MonoItem<'tcx>) -> bool { pub fn contains_item(&self, item: &MonoItem<'tcx>) -> bool {

View File

@ -150,14 +150,11 @@ where
let cx = &PartitioningCx { tcx, usage_map }; let cx = &PartitioningCx { tcx, usage_map };
// Place all mono items into a codegen unit. // Place all mono items into a codegen unit. `place_mono_items` is
// responsible for initializing the CGU size estimates.
let PlacedMonoItems { mut codegen_units, internalization_candidates, unique_inlined_stats } = { let PlacedMonoItems { mut codegen_units, internalization_candidates, unique_inlined_stats } = {
let _prof_timer = tcx.prof.generic_activity("cgu_partitioning_place_items"); let _prof_timer = tcx.prof.generic_activity("cgu_partitioning_place_items");
let mut placed = place_mono_items(cx, mono_items); let placed = place_mono_items(cx, mono_items);
for cgu in &mut placed.codegen_units {
cgu.create_size_estimate(tcx);
}
debug_dump(tcx, "PLACE", &placed.codegen_units, placed.unique_inlined_stats); debug_dump(tcx, "PLACE", &placed.codegen_units, placed.unique_inlined_stats);
@ -282,6 +279,10 @@ where
let mut codegen_units: Vec<_> = codegen_units.into_values().collect(); let mut codegen_units: Vec<_> = codegen_units.into_values().collect();
codegen_units.sort_by(|a, b| a.name().as_str().cmp(b.name().as_str())); codegen_units.sort_by(|a, b| a.name().as_str().cmp(b.name().as_str()));
for cgu in codegen_units.iter_mut() {
cgu.compute_size_estimate(cx.tcx);
}
return PlacedMonoItems { return PlacedMonoItems {
codegen_units, codegen_units,
internalization_candidates, internalization_candidates,
@ -351,7 +352,7 @@ fn merge_codegen_units<'tcx>(
// may be duplicate inlined items, in which case the destination CGU is // may be duplicate inlined items, in which case the destination CGU is
// unaffected. Recalculate size estimates afterwards. // unaffected. Recalculate size estimates afterwards.
second_smallest.items_mut().extend(smallest.items_mut().drain()); second_smallest.items_mut().extend(smallest.items_mut().drain());
second_smallest.create_size_estimate(cx.tcx); second_smallest.compute_size_estimate(cx.tcx);
// Record that `second_smallest` now contains all the stuff that was // Record that `second_smallest` now contains all the stuff that was
// in `smallest` before. // in `smallest` before.