Skip to content

Commit 730689e

Browse files
committed
Estimate CGU cost in place_root_mono_items
1 parent e478795 commit 730689e

File tree

2 files changed

+49
-51
lines changed

2 files changed

+49
-51
lines changed

src/librustc/mir/mono.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ pub struct CodegenUnit<'tcx> {
234234
/// as well as the crate name and disambiguator.
235235
name: Symbol,
236236
items: FxHashMap<MonoItem<'tcx>, (Linkage, Visibility)>,
237-
size_estimate: Option<usize>,
237+
size_estimate: usize,
238238
}
239239

240240
#[derive(Copy, Clone, PartialEq, Debug, RustcEncodable, RustcDecodable, HashStable)]
@@ -261,7 +261,7 @@ pub enum Visibility {
261261

262262
impl<'tcx> CodegenUnit<'tcx> {
263263
pub fn new(name: Symbol) -> CodegenUnit<'tcx> {
264-
CodegenUnit { name: name, items: Default::default(), size_estimate: None }
264+
CodegenUnit { name: name, items: Default::default(), size_estimate: 0 }
265265
}
266266

267267
pub fn name(&self) -> Symbol {
@@ -293,19 +293,15 @@ impl<'tcx> CodegenUnit<'tcx> {
293293
pub fn estimate_size(&mut self, tcx: TyCtxt<'tcx>) {
294294
// Estimate the size of a codegen unit as (approximately) the number of MIR
295295
// statements it corresponds to.
296-
self.size_estimate = Some(self.items.keys().map(|mi| mi.size_estimate(tcx)).sum());
296+
self.size_estimate = self.items.keys().map(|mi| mi.size_estimate(tcx)).sum();
297297
}
298298

299299
pub fn size_estimate(&self) -> usize {
300-
// Should only be called if `estimate_size` has previously been called.
301-
self.size_estimate.expect("estimate_size must be called before getting a size_estimate")
300+
self.size_estimate
302301
}
303302

304303
pub fn modify_size_estimate(&mut self, delta: usize) {
305-
assert!(self.size_estimate.is_some());
306-
if let Some(size_estimate) = self.size_estimate {
307-
self.size_estimate = Some(size_estimate + delta);
308-
}
304+
self.size_estimate += delta;
309305
}
310306

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

src/librustc_mir/monomorphize/partitioning.rs

Lines changed: 44 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,6 @@ where
147147
|| accessor_map(tcx, inlining_map),
148148
);
149149

150-
initial_partitioning.codegen_units.iter_mut().for_each(|cgu| cgu.estimate_size(tcx));
151-
152150
debug_dump(tcx, "INITIAL PARTITIONING:", initial_partitioning.codegen_units.iter());
153151

154152
// If the partitioning should produce a fixed count of codegen units, merge
@@ -226,54 +224,57 @@ where
226224

227225
let mono_items: Vec<_> = mono_items.collect();
228226

229-
let _prof_timer = tcx.prof.generic_activity("place_root_mono_items_par");
227+
let chunks = tcx.prof.generic_activity("place_root_mono_items_par").run(|| {
228+
sync::par_partition(&mono_items, 2, |chunk| {
229+
let mut roots = Vec::new();
230+
let mut codegen_units = FxHashMap::default();
231+
let mut internalization_candidates = Vec::new();
230232

231-
let chunks = sync::par_partition(&mono_items, 2, |chunk| {
232-
let mut roots = Vec::new();
233-
let mut codegen_units = FxHashMap::default();
234-
let mut internalization_candidates = Vec::new();
233+
let cgu_name_builder = &mut CodegenUnitNameBuilder::new(tcx);
234+
let cgu_name_cache = &mut FxHashMap::default();
235235

236-
let cgu_name_builder = &mut CodegenUnitNameBuilder::new(tcx);
237-
let cgu_name_cache = &mut FxHashMap::default();
236+
for &mono_item in chunk {
237+
match mono_item.instantiation_mode(tcx) {
238+
InstantiationMode::GloballyShared { .. } => {}
239+
InstantiationMode::LocalCopy => continue,
240+
}
238241

239-
for &mono_item in chunk {
240-
match mono_item.instantiation_mode(tcx) {
241-
InstantiationMode::GloballyShared { .. } => {}
242-
InstantiationMode::LocalCopy => continue,
243-
}
242+
let characteristic_def_id = characteristic_def_id_of_mono_item(tcx, mono_item);
243+
let is_volatile = is_incremental_build && mono_item.is_generic_fn();
244+
245+
let codegen_unit_name = match characteristic_def_id {
246+
Some(def_id) => compute_codegen_unit_name(
247+
tcx,
248+
cgu_name_builder,
249+
def_id,
250+
is_volatile,
251+
cgu_name_cache,
252+
),
253+
None => fallback_cgu_name(cgu_name_builder),
254+
};
244255

245-
let characteristic_def_id = characteristic_def_id_of_mono_item(tcx, mono_item);
246-
let is_volatile = is_incremental_build && mono_item.is_generic_fn();
256+
let codegen_unit =
257+
codegen_units.entry(codegen_unit_name).or_insert_with(|| (Vec::new(), 0));
247258

248-
let codegen_unit_name = match characteristic_def_id {
249-
Some(def_id) => compute_codegen_unit_name(
259+
let mut can_be_internalized = true;
260+
let (linkage, visibility) = mono_item_linkage_and_visibility(
250261
tcx,
251-
cgu_name_builder,
252-
def_id,
253-
is_volatile,
254-
cgu_name_cache,
255-
),
256-
None => fallback_cgu_name(cgu_name_builder),
257-
};
262+
&mono_item,
263+
&mut can_be_internalized,
264+
export_generics,
265+
);
266+
if visibility == Visibility::Hidden && can_be_internalized {
267+
internalization_candidates.push(mono_item);
268+
}
258269

259-
let codegen_unit = codegen_units.entry(codegen_unit_name).or_insert_with(|| Vec::new());
260-
261-
let mut can_be_internalized = true;
262-
let (linkage, visibility) = mono_item_linkage_and_visibility(
263-
tcx,
264-
&mono_item,
265-
&mut can_be_internalized,
266-
export_generics,
267-
);
268-
if visibility == Visibility::Hidden && can_be_internalized {
269-
internalization_candidates.push(mono_item);
270-
}
270+
codegen_unit.0.push((mono_item, (linkage, visibility)));
271+
codegen_unit.1 += mono_item.size_estimate(tcx);
271272

272-
codegen_unit.push((mono_item, (linkage, visibility)));
273-
roots.push(mono_item);
274-
}
273+
roots.push(mono_item);
274+
}
275275

276-
(roots, codegen_units, internalization_candidates)
276+
(roots, codegen_units, internalization_candidates)
277+
})
277278
});
278279

279280
let _prof_timer = tcx.prof.generic_activity("place_root_mono_items_merge");
@@ -285,8 +286,9 @@ where
285286
for (chunk_roots, chunk_codegen_units, chunk_internalization_candidates) in chunks {
286287
roots.extend(chunk_roots);
287288
internalization_candidates.extend(chunk_internalization_candidates);
288-
for (name, items) in chunk_codegen_units {
289+
for (name, (items, cost)) in chunk_codegen_units {
289290
let codegen_unit = codegen_units.entry(name).or_insert_with(|| CodegenUnit::new(name));
291+
codegen_unit.modify_size_estimate(cost);
290292
codegen_unit.items_mut().extend(items);
291293
}
292294
}

0 commit comments

Comments
 (0)