Skip to content

Commit abde9ba

Browse files
committed
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`.
1 parent 105ac1c commit abde9ba

File tree

2 files changed

+17
-14
lines changed

2 files changed

+17
-14
lines changed

Diff for: compiler/rustc_middle/src/mir/mono.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ pub struct CodegenUnit<'tcx> {
231231
/// as well as the crate name and disambiguator.
232232
name: Symbol,
233233
items: FxHashMap<MonoItem<'tcx>, (Linkage, Visibility)>,
234-
size_estimate: Option<usize>,
234+
size_estimate: usize,
235235
primary: bool,
236236
/// True if this is CGU is used to hold code coverage information for dead code,
237237
/// false otherwise.
@@ -269,7 +269,7 @@ impl<'tcx> CodegenUnit<'tcx> {
269269
CodegenUnit {
270270
name,
271271
items: Default::default(),
272-
size_estimate: None,
272+
size_estimate: 0,
273273
primary: false,
274274
is_code_coverage_dead_code_cgu: false,
275275
}
@@ -320,19 +320,21 @@ impl<'tcx> CodegenUnit<'tcx> {
320320
base_n::encode(hash, base_n::CASE_INSENSITIVE)
321321
}
322322

323-
pub fn create_size_estimate(&mut self, tcx: TyCtxt<'tcx>) {
323+
pub fn compute_size_estimate(&mut self, tcx: TyCtxt<'tcx>) {
324324
// Estimate the size of a codegen unit as (approximately) the number of MIR
325325
// statements it corresponds to.
326-
self.size_estimate = Some(self.items.keys().map(|mi| mi.size_estimate(tcx)).sum());
326+
self.size_estimate = self.items.keys().map(|mi| mi.size_estimate(tcx)).sum();
327327
}
328328

329329
#[inline]
330-
/// Should only be called if [`create_size_estimate`] has previously been called.
330+
/// Should only be called if [`compute_size_estimate`] has previously been called.
331331
///
332-
/// [`create_size_estimate`]: Self::create_size_estimate
332+
/// [`compute_size_estimate`]: Self::compute_size_estimate
333333
pub fn size_estimate(&self) -> usize {
334+
// Items are never zero-sized, so if we have items the estimate must be
335+
// non-zero, unless we forgot to call `compute_size_estimate` first.
336+
assert!(self.items.is_empty() || self.size_estimate != 0);
334337
self.size_estimate
335-
.expect("create_size_estimate must be called before getting a size_estimate")
336338
}
337339

338340
pub fn contains_item(&self, item: &MonoItem<'tcx>) -> bool {

Diff for: compiler/rustc_monomorphize/src/partitioning.rs

+8-7
Original file line numberDiff line numberDiff line change
@@ -150,14 +150,11 @@ where
150150

151151
let cx = &PartitioningCx { tcx, usage_map };
152152

153-
// Place all mono items into a codegen unit.
153+
// Place all mono items into a codegen unit. `place_mono_items` is
154+
// responsible for initializing the CGU size estimates.
154155
let PlacedMonoItems { mut codegen_units, internalization_candidates, unique_inlined_stats } = {
155156
let _prof_timer = tcx.prof.generic_activity("cgu_partitioning_place_items");
156-
let mut placed = place_mono_items(cx, mono_items);
157-
158-
for cgu in &mut placed.codegen_units {
159-
cgu.create_size_estimate(tcx);
160-
}
157+
let placed = place_mono_items(cx, mono_items);
161158

162159
debug_dump(tcx, "PLACE", &placed.codegen_units, placed.unique_inlined_stats);
163160

@@ -282,6 +279,10 @@ where
282279
let mut codegen_units: Vec<_> = codegen_units.into_values().collect();
283280
codegen_units.sort_by(|a, b| a.name().as_str().cmp(b.name().as_str()));
284281

282+
for cgu in codegen_units.iter_mut() {
283+
cgu.compute_size_estimate(cx.tcx);
284+
}
285+
285286
return PlacedMonoItems {
286287
codegen_units,
287288
internalization_candidates,
@@ -351,7 +352,7 @@ fn merge_codegen_units<'tcx>(
351352
// may be duplicate inlined items, in which case the destination CGU is
352353
// unaffected. Recalculate size estimates afterwards.
353354
second_smallest.items_mut().extend(smallest.items_mut().drain());
354-
second_smallest.create_size_estimate(cx.tcx);
355+
second_smallest.compute_size_estimate(cx.tcx);
355356

356357
// Record that `second_smallest` now contains all the stuff that was
357358
// in `smallest` before.

0 commit comments

Comments
 (0)