Skip to content

Commit dc8ab7e

Browse files
committed
XXX: split big CGUs
1 parent 59c5259 commit dc8ab7e

File tree

4 files changed

+74
-3
lines changed

4 files changed

+74
-3
lines changed

compiler/rustc_middle/src/mir/mono.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,13 +333,22 @@ impl<'tcx> CodegenUnit<'tcx> {
333333
.expect("create_size_estimate must be called before getting a size_estimate")
334334
}
335335

336-
pub fn modify_size_estimate(&mut self, delta: usize) {
336+
pub fn increase_size_estimate(&mut self, delta: usize) {
337+
// njn: make this nicer, with as_mut().expect()
337338
assert!(self.size_estimate.is_some());
338339
if let Some(size_estimate) = self.size_estimate {
339340
self.size_estimate = Some(size_estimate + delta);
340341
}
341342
}
342343

344+
pub fn decrease_size_estimate(&mut self, delta: usize) {
345+
// njn: make this nicer, with as_mut().expect()
346+
assert!(self.size_estimate.is_some());
347+
if let Some(size_estimate) = self.size_estimate {
348+
self.size_estimate = Some(size_estimate - delta);
349+
}
350+
}
351+
343352
pub fn contains_item(&self, item: &MonoItem<'tcx>) -> bool {
344353
self.items().contains_key(item)
345354
}

compiler/rustc_monomorphize/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#![feature(array_windows)]
2+
#![feature(hash_drain_filter)]
23
#![recursion_limit = "256"]
34
#![allow(rustc::potential_query_instability)]
45
#![deny(rustc::untranslatable_diagnostic)]

compiler/rustc_monomorphize/src/partitioning/default.rs

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,67 @@ impl<'tcx> Partition<'tcx> for DefaultPartitioning {
111111
// and deterministic.
112112
codegen_units.sort_by(|a, b| a.name().as_str().cmp(b.name().as_str()));
113113

114+
//---------------------------------------------------------------------------
115+
// njn: split big CGUs if necessary
116+
if false && codegen_units.len() > cx.target_cgu_count {
117+
// njn: type ann?
118+
let total_size: usize = codegen_units.iter().map(|cgu| cgu.size_estimate()).sum();
119+
let target_size = total_size / cx.target_cgu_count;
120+
eprintln!("----");
121+
eprintln!("SPLIT0: total:{} target:{}", total_size, target_size);
122+
// njn: need a while loop because we're modifying codegen_units as we go
123+
// njn: make it a for loop?
124+
// njn: explain all this
125+
let mut i = 0;
126+
let n = codegen_units.len();
127+
while i < n {
128+
let old_cgu = &mut codegen_units[i];
129+
if old_cgu.size_estimate() > target_size && old_cgu.items().len() > 1 {
130+
eprintln!("SPLIT1: old:{} old:{}", old_cgu.size_estimate(), old_cgu.name());
131+
132+
// njn: too big; split
133+
// njn: explain how a very big CGU will be split multiple
134+
// times
135+
136+
let mut new_name = old_cgu.name().to_string();
137+
new_name += "-split";
138+
let mut new_cgu = CodegenUnit::new(Symbol::intern(&new_name));
139+
new_cgu.create_size_estimate(cx.tcx); // initially zero
140+
141+
// njn: size stuff is a bit clumsy
142+
let mut moved_size = 0;
143+
144+
// njn: what if this empties old_cgu?
145+
146+
// njn: nicer way to do this?
147+
// njn: don't move if it's the last item
148+
old_cgu.items_mut().drain_filter(|item, rest| {
149+
// njn: true->remove
150+
if moved_size < target_size {
151+
let item_size = item.size_estimate(cx.tcx);
152+
eprintln!("MOVE: {}", item_size);
153+
moved_size += item_size;
154+
new_cgu.items_mut().insert(*item, *rest);
155+
true
156+
} else {
157+
false
158+
}
159+
});
160+
new_cgu.increase_size_estimate(moved_size);
161+
old_cgu.decrease_size_estimate(moved_size);
162+
163+
eprintln!("SPLIT2: old:{} -> new:{} new:{}", old_cgu.size_estimate(), new_cgu.size_estimate(), new_cgu.name());
164+
165+
codegen_units.push(new_cgu);
166+
// njn: explain lack of `i += 1`;
167+
} else {
168+
// njn: explain this
169+
i += 1;
170+
}
171+
}
172+
}
173+
//---------------------------------------------------------------------------
174+
114175
// This map keeps track of what got merged into what.
115176
let mut cgu_contents: FxHashMap<Symbol, Vec<Symbol>> =
116177
codegen_units.iter().map(|cgu| (cgu.name(), vec![cgu.name()])).collect();
@@ -124,7 +185,7 @@ impl<'tcx> Partition<'tcx> for DefaultPartitioning {
124185
let second_smallest = codegen_units.last_mut().unwrap();
125186

126187
// Move the mono-items from `smallest` to `second_smallest`
127-
second_smallest.modify_size_estimate(smallest.size_estimate());
188+
second_smallest.increase_size_estimate(smallest.size_estimate());
128189
for (k, v) in smallest.items_mut().drain() {
129190
second_smallest.items_mut().insert(k, v);
130191
}

compiler/rustc_monomorphize/src/partitioning/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ fn debug_dump<'a, 'tcx: 'a>(tcx: TyCtxt<'tcx>, label: &str, cgus: &[CodegenUnit<
382382
std::mem::take(s)
383383
};
384384

385-
debug!("{}", dump());
385+
eprintln!("{}", dump());
386386
}
387387

388388
#[inline(never)] // give this a place in the profiler

0 commit comments

Comments
 (0)