Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 78cc117

Browse files
authoredMay 26, 2023
Rollup merge of #111899 - nnethercote:cgu-cleanups, r=wesleywiser
CGU cleanups Some code clarity improvements I found when reading this code closely. r? ``@wesleywiser``
2 parents 42c7b8a + e6b99a6 commit 78cc117

File tree

4 files changed

+169
-206
lines changed

4 files changed

+169
-206
lines changed
 

‎compiler/rustc_middle/src/mir/mono.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -334,10 +334,7 @@ impl<'tcx> CodegenUnit<'tcx> {
334334
}
335335

336336
pub fn modify_size_estimate(&mut self, delta: usize) {
337-
assert!(self.size_estimate.is_some());
338-
if let Some(size_estimate) = self.size_estimate {
339-
self.size_estimate = Some(size_estimate + delta);
340-
}
337+
*self.size_estimate.as_mut().unwrap() += delta;
341338
}
342339

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

‎compiler/rustc_monomorphize/src/partitioning/default.rs

Lines changed: 113 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::cmp;
12
use std::collections::hash_map::Entry;
23

34
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
@@ -14,10 +15,7 @@ use rustc_span::symbol::Symbol;
1415

1516
use super::PartitioningCx;
1617
use crate::collector::InliningMap;
17-
use crate::partitioning::merging;
18-
use crate::partitioning::{
19-
MonoItemPlacement, Partition, PostInliningPartitioning, PreInliningPartitioning,
20-
};
18+
use crate::partitioning::{MonoItemPlacement, Partition, PlacedRootMonoItems};
2119

2220
pub struct DefaultPartitioning;
2321

@@ -26,7 +24,7 @@ impl<'tcx> Partition<'tcx> for DefaultPartitioning {
2624
&mut self,
2725
cx: &PartitioningCx<'_, 'tcx>,
2826
mono_items: &mut I,
29-
) -> PreInliningPartitioning<'tcx>
27+
) -> PlacedRootMonoItems<'tcx>
3028
where
3129
I: Iterator<Item = MonoItem<'tcx>>,
3230
{
@@ -91,38 +89,120 @@ impl<'tcx> Partition<'tcx> for DefaultPartitioning {
9189
codegen_units.insert(codegen_unit_name, CodegenUnit::new(codegen_unit_name));
9290
}
9391

94-
PreInliningPartitioning {
95-
codegen_units: codegen_units.into_values().collect(),
96-
roots,
97-
internalization_candidates,
98-
}
92+
let codegen_units = codegen_units.into_values().collect();
93+
PlacedRootMonoItems { codegen_units, roots, internalization_candidates }
9994
}
10095

10196
fn merge_codegen_units(
10297
&mut self,
10398
cx: &PartitioningCx<'_, 'tcx>,
104-
initial_partitioning: &mut PreInliningPartitioning<'tcx>,
99+
codegen_units: &mut Vec<CodegenUnit<'tcx>>,
105100
) {
106-
merging::merge_codegen_units(cx, initial_partitioning);
101+
assert!(cx.target_cgu_count >= 1);
102+
103+
// Note that at this point in time the `codegen_units` here may not be
104+
// in a deterministic order (but we know they're deterministically the
105+
// same set). We want this merging to produce a deterministic ordering
106+
// of codegen units from the input.
107+
//
108+
// Due to basically how we've implemented the merging below (merge the
109+
// two smallest into each other) we're sure to start off with a
110+
// deterministic order (sorted by name). This'll mean that if two cgus
111+
// have the same size the stable sort below will keep everything nice
112+
// and deterministic.
113+
codegen_units.sort_by(|a, b| a.name().as_str().cmp(b.name().as_str()));
114+
115+
// This map keeps track of what got merged into what.
116+
let mut cgu_contents: FxHashMap<Symbol, Vec<Symbol>> =
117+
codegen_units.iter().map(|cgu| (cgu.name(), vec![cgu.name()])).collect();
118+
119+
// Merge the two smallest codegen units until the target size is
120+
// reached.
121+
while codegen_units.len() > cx.target_cgu_count {
122+
// Sort small cgus to the back
123+
codegen_units.sort_by_cached_key(|cgu| cmp::Reverse(cgu.size_estimate()));
124+
let mut smallest = codegen_units.pop().unwrap();
125+
let second_smallest = codegen_units.last_mut().unwrap();
126+
127+
// Move the mono-items from `smallest` to `second_smallest`
128+
second_smallest.modify_size_estimate(smallest.size_estimate());
129+
for (k, v) in smallest.items_mut().drain() {
130+
second_smallest.items_mut().insert(k, v);
131+
}
132+
133+
// Record that `second_smallest` now contains all the stuff that was
134+
// in `smallest` before.
135+
let mut consumed_cgu_names = cgu_contents.remove(&smallest.name()).unwrap();
136+
cgu_contents.get_mut(&second_smallest.name()).unwrap().append(&mut consumed_cgu_names);
137+
138+
debug!(
139+
"CodegenUnit {} merged into CodegenUnit {}",
140+
smallest.name(),
141+
second_smallest.name()
142+
);
143+
}
144+
145+
let cgu_name_builder = &mut CodegenUnitNameBuilder::new(cx.tcx);
146+
147+
if cx.tcx.sess.opts.incremental.is_some() {
148+
// If we are doing incremental compilation, we want CGU names to
149+
// reflect the path of the source level module they correspond to.
150+
// For CGUs that contain the code of multiple modules because of the
151+
// merging done above, we use a concatenation of the names of all
152+
// contained CGUs.
153+
let new_cgu_names: FxHashMap<Symbol, String> = cgu_contents
154+
.into_iter()
155+
// This `filter` makes sure we only update the name of CGUs that
156+
// were actually modified by merging.
157+
.filter(|(_, cgu_contents)| cgu_contents.len() > 1)
158+
.map(|(current_cgu_name, cgu_contents)| {
159+
let mut cgu_contents: Vec<&str> =
160+
cgu_contents.iter().map(|s| s.as_str()).collect();
161+
162+
// Sort the names, so things are deterministic and easy to
163+
// predict. We are sorting primitive `&str`s here so we can
164+
// use unstable sort.
165+
cgu_contents.sort_unstable();
166+
167+
(current_cgu_name, cgu_contents.join("--"))
168+
})
169+
.collect();
170+
171+
for cgu in codegen_units.iter_mut() {
172+
if let Some(new_cgu_name) = new_cgu_names.get(&cgu.name()) {
173+
if cx.tcx.sess.opts.unstable_opts.human_readable_cgu_names {
174+
cgu.set_name(Symbol::intern(&new_cgu_name));
175+
} else {
176+
// If we don't require CGU names to be human-readable,
177+
// we use a fixed length hash of the composite CGU name
178+
// instead.
179+
let new_cgu_name = CodegenUnit::mangle_name(&new_cgu_name);
180+
cgu.set_name(Symbol::intern(&new_cgu_name));
181+
}
182+
}
183+
}
184+
} else {
185+
// If we are compiling non-incrementally we just generate simple CGU
186+
// names containing an index.
187+
for (index, cgu) in codegen_units.iter_mut().enumerate() {
188+
let numbered_codegen_unit_name =
189+
cgu_name_builder.build_cgu_name_no_mangle(LOCAL_CRATE, &["cgu"], Some(index));
190+
cgu.set_name(numbered_codegen_unit_name);
191+
}
192+
}
107193
}
108194

109195
fn place_inlined_mono_items(
110196
&mut self,
111197
cx: &PartitioningCx<'_, 'tcx>,
112-
initial_partitioning: PreInliningPartitioning<'tcx>,
113-
) -> PostInliningPartitioning<'tcx> {
114-
let mut new_partitioning = Vec::new();
198+
codegen_units: &mut [CodegenUnit<'tcx>],
199+
roots: FxHashSet<MonoItem<'tcx>>,
200+
) -> FxHashMap<MonoItem<'tcx>, MonoItemPlacement> {
115201
let mut mono_item_placements = FxHashMap::default();
116202

117-
let PreInliningPartitioning {
118-
codegen_units: initial_cgus,
119-
roots,
120-
internalization_candidates,
121-
} = initial_partitioning;
122-
123-
let single_codegen_unit = initial_cgus.len() == 1;
203+
let single_codegen_unit = codegen_units.len() == 1;
124204

125-
for old_codegen_unit in initial_cgus {
205+
for old_codegen_unit in codegen_units.iter_mut() {
126206
// Collect all items that need to be available in this codegen unit.
127207
let mut reachable = FxHashSet::default();
128208
for root in old_codegen_unit.items().keys() {
@@ -174,14 +254,10 @@ impl<'tcx> Partition<'tcx> for DefaultPartitioning {
174254
}
175255
}
176256

177-
new_partitioning.push(new_codegen_unit);
257+
*old_codegen_unit = new_codegen_unit;
178258
}
179259

180-
return PostInliningPartitioning {
181-
codegen_units: new_partitioning,
182-
mono_item_placements,
183-
internalization_candidates,
184-
};
260+
return mono_item_placements;
185261

186262
fn follow_inlining<'tcx>(
187263
mono_item: MonoItem<'tcx>,
@@ -201,14 +277,16 @@ impl<'tcx> Partition<'tcx> for DefaultPartitioning {
201277
fn internalize_symbols(
202278
&mut self,
203279
cx: &PartitioningCx<'_, 'tcx>,
204-
partitioning: &mut PostInliningPartitioning<'tcx>,
280+
codegen_units: &mut [CodegenUnit<'tcx>],
281+
mono_item_placements: FxHashMap<MonoItem<'tcx>, MonoItemPlacement>,
282+
internalization_candidates: FxHashSet<MonoItem<'tcx>>,
205283
) {
206-
if partitioning.codegen_units.len() == 1 {
284+
if codegen_units.len() == 1 {
207285
// Fast path for when there is only one codegen unit. In this case we
208286
// can internalize all candidates, since there is nowhere else they
209287
// could be accessed from.
210-
for cgu in &mut partitioning.codegen_units {
211-
for candidate in &partitioning.internalization_candidates {
288+
for cgu in codegen_units {
289+
for candidate in &internalization_candidates {
212290
cgu.items_mut().insert(*candidate, (Linkage::Internal, Visibility::Default));
213291
}
214292
}
@@ -225,15 +303,13 @@ impl<'tcx> Partition<'tcx> for DefaultPartitioning {
225303
}
226304
});
227305

228-
let mono_item_placements = &partitioning.mono_item_placements;
229-
230306
// For each internalization candidates in each codegen unit, check if it is
231307
// accessed from outside its defining codegen unit.
232-
for cgu in &mut partitioning.codegen_units {
308+
for cgu in codegen_units {
233309
let home_cgu = MonoItemPlacement::SingleCgu { cgu_name: cgu.name() };
234310

235311
for (accessee, linkage_and_visibility) in cgu.items_mut() {
236-
if !partitioning.internalization_candidates.contains(accessee) {
312+
if !internalization_candidates.contains(accessee) {
237313
// This item is no candidate for internalizing, so skip it.
238314
continue;
239315
}

‎compiler/rustc_monomorphize/src/partitioning/merging.rs

Lines changed: 0 additions & 111 deletions
This file was deleted.

‎compiler/rustc_monomorphize/src/partitioning/mod.rs

Lines changed: 55 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@
9393
//! inlining, even when they are not marked `#[inline]`.
9494
9595
mod default;
96-
mod merging;
9796

9897
use std::cmp;
9998
use std::fs::{self, File};
@@ -129,7 +128,7 @@ impl<'tcx> Partition<'tcx> for Partitioner {
129128
&mut self,
130129
cx: &PartitioningCx<'_, 'tcx>,
131130
mono_items: &mut I,
132-
) -> PreInliningPartitioning<'tcx>
131+
) -> PlacedRootMonoItems<'tcx>
133132
where
134133
I: Iterator<Item = MonoItem<'tcx>>,
135134
{
@@ -142,24 +141,23 @@ impl<'tcx> Partition<'tcx> for Partitioner {
142141
fn merge_codegen_units(
143142
&mut self,
144143
cx: &PartitioningCx<'_, 'tcx>,
145-
initial_partitioning: &mut PreInliningPartitioning<'tcx>,
144+
codegen_units: &mut Vec<CodegenUnit<'tcx>>,
146145
) {
147146
match self {
148-
Partitioner::Default(partitioner) => {
149-
partitioner.merge_codegen_units(cx, initial_partitioning)
150-
}
147+
Partitioner::Default(partitioner) => partitioner.merge_codegen_units(cx, codegen_units),
151148
Partitioner::Unknown => cx.tcx.sess.emit_fatal(UnknownPartitionStrategy),
152149
}
153150
}
154151

155152
fn place_inlined_mono_items(
156153
&mut self,
157154
cx: &PartitioningCx<'_, 'tcx>,
158-
initial_partitioning: PreInliningPartitioning<'tcx>,
159-
) -> PostInliningPartitioning<'tcx> {
155+
codegen_units: &mut [CodegenUnit<'tcx>],
156+
roots: FxHashSet<MonoItem<'tcx>>,
157+
) -> FxHashMap<MonoItem<'tcx>, MonoItemPlacement> {
160158
match self {
161159
Partitioner::Default(partitioner) => {
162-
partitioner.place_inlined_mono_items(cx, initial_partitioning)
160+
partitioner.place_inlined_mono_items(cx, codegen_units, roots)
163161
}
164162
Partitioner::Unknown => cx.tcx.sess.emit_fatal(UnknownPartitionStrategy),
165163
}
@@ -168,48 +166,62 @@ impl<'tcx> Partition<'tcx> for Partitioner {
168166
fn internalize_symbols(
169167
&mut self,
170168
cx: &PartitioningCx<'_, 'tcx>,
171-
post_inlining_partitioning: &mut PostInliningPartitioning<'tcx>,
169+
codegen_units: &mut [CodegenUnit<'tcx>],
170+
mono_item_placements: FxHashMap<MonoItem<'tcx>, MonoItemPlacement>,
171+
internalization_candidates: FxHashSet<MonoItem<'tcx>>,
172172
) {
173173
match self {
174-
Partitioner::Default(partitioner) => {
175-
partitioner.internalize_symbols(cx, post_inlining_partitioning)
176-
}
174+
Partitioner::Default(partitioner) => partitioner.internalize_symbols(
175+
cx,
176+
codegen_units,
177+
mono_item_placements,
178+
internalization_candidates,
179+
),
177180
Partitioner::Unknown => cx.tcx.sess.emit_fatal(UnknownPartitionStrategy),
178181
}
179182
}
180183
}
181184

182-
pub struct PartitioningCx<'a, 'tcx> {
185+
struct PartitioningCx<'a, 'tcx> {
183186
tcx: TyCtxt<'tcx>,
184187
target_cgu_count: usize,
185188
inlining_map: &'a InliningMap<'tcx>,
186189
}
187190

191+
pub struct PlacedRootMonoItems<'tcx> {
192+
codegen_units: Vec<CodegenUnit<'tcx>>,
193+
roots: FxHashSet<MonoItem<'tcx>>,
194+
internalization_candidates: FxHashSet<MonoItem<'tcx>>,
195+
}
196+
188197
trait Partition<'tcx> {
189198
fn place_root_mono_items<I>(
190199
&mut self,
191200
cx: &PartitioningCx<'_, 'tcx>,
192201
mono_items: &mut I,
193-
) -> PreInliningPartitioning<'tcx>
202+
) -> PlacedRootMonoItems<'tcx>
194203
where
195204
I: Iterator<Item = MonoItem<'tcx>>;
196205

197206
fn merge_codegen_units(
198207
&mut self,
199208
cx: &PartitioningCx<'_, 'tcx>,
200-
initial_partitioning: &mut PreInliningPartitioning<'tcx>,
209+
codegen_units: &mut Vec<CodegenUnit<'tcx>>,
201210
);
202211

203212
fn place_inlined_mono_items(
204213
&mut self,
205214
cx: &PartitioningCx<'_, 'tcx>,
206-
initial_partitioning: PreInliningPartitioning<'tcx>,
207-
) -> PostInliningPartitioning<'tcx>;
215+
codegen_units: &mut [CodegenUnit<'tcx>],
216+
roots: FxHashSet<MonoItem<'tcx>>,
217+
) -> FxHashMap<MonoItem<'tcx>, MonoItemPlacement>;
208218

209219
fn internalize_symbols(
210220
&mut self,
211221
cx: &PartitioningCx<'_, 'tcx>,
212-
partitioning: &mut PostInliningPartitioning<'tcx>,
222+
codegen_units: &mut [CodegenUnit<'tcx>],
223+
mono_item_placements: FxHashMap<MonoItem<'tcx>, MonoItemPlacement>,
224+
internalization_candidates: FxHashSet<MonoItem<'tcx>>,
213225
);
214226
}
215227

@@ -225,7 +237,7 @@ fn get_partitioner(tcx: TyCtxt<'_>) -> Partitioner {
225237
}
226238
}
227239

228-
pub fn partition<'tcx, I>(
240+
fn partition<'tcx, I>(
229241
tcx: TyCtxt<'tcx>,
230242
mono_items: &mut I,
231243
max_cgu_count: usize,
@@ -241,52 +253,59 @@ where
241253
// In the first step, we place all regular monomorphizations into their
242254
// respective 'home' codegen unit. Regular monomorphizations are all
243255
// functions and statics defined in the local crate.
244-
let mut initial_partitioning = {
256+
let PlacedRootMonoItems { mut codegen_units, roots, internalization_candidates } = {
245257
let _prof_timer = tcx.prof.generic_activity("cgu_partitioning_place_roots");
246258
partitioner.place_root_mono_items(cx, mono_items)
247259
};
248260

249-
for cgu in &mut initial_partitioning.codegen_units {
261+
for cgu in &mut codegen_units {
250262
cgu.create_size_estimate(tcx);
251263
}
252264

253-
debug_dump(tcx, "INITIAL PARTITIONING", &initial_partitioning.codegen_units);
265+
debug_dump(tcx, "INITIAL PARTITIONING", &codegen_units);
254266

255267
// Merge until we have at most `max_cgu_count` codegen units.
268+
// `merge_codegen_units` is responsible for updating the CGU size
269+
// estimates.
256270
{
257271
let _prof_timer = tcx.prof.generic_activity("cgu_partitioning_merge_cgus");
258-
partitioner.merge_codegen_units(cx, &mut initial_partitioning);
259-
debug_dump(tcx, "POST MERGING", &initial_partitioning.codegen_units);
272+
partitioner.merge_codegen_units(cx, &mut codegen_units);
273+
debug_dump(tcx, "POST MERGING", &codegen_units);
260274
}
261275

262276
// In the next step, we use the inlining map to determine which additional
263277
// monomorphizations have to go into each codegen unit. These additional
264278
// monomorphizations can be drop-glue, functions from external crates, and
265279
// local functions the definition of which is marked with `#[inline]`.
266-
let mut post_inlining = {
280+
let mono_item_placements = {
267281
let _prof_timer = tcx.prof.generic_activity("cgu_partitioning_place_inline_items");
268-
partitioner.place_inlined_mono_items(cx, initial_partitioning)
282+
partitioner.place_inlined_mono_items(cx, &mut codegen_units, roots)
269283
};
270284

271-
for cgu in &mut post_inlining.codegen_units {
285+
for cgu in &mut codegen_units {
272286
cgu.create_size_estimate(tcx);
273287
}
274288

275-
debug_dump(tcx, "POST INLINING", &post_inlining.codegen_units);
289+
debug_dump(tcx, "POST INLINING", &codegen_units);
276290

277291
// Next we try to make as many symbols "internal" as possible, so LLVM has
278292
// more freedom to optimize.
279293
if !tcx.sess.link_dead_code() {
280294
let _prof_timer = tcx.prof.generic_activity("cgu_partitioning_internalize_symbols");
281-
partitioner.internalize_symbols(cx, &mut post_inlining);
295+
partitioner.internalize_symbols(
296+
cx,
297+
&mut codegen_units,
298+
mono_item_placements,
299+
internalization_candidates,
300+
);
282301
}
283302

284303
let instrument_dead_code =
285304
tcx.sess.instrument_coverage() && !tcx.sess.instrument_coverage_except_unused_functions();
286305

287306
if instrument_dead_code {
288307
assert!(
289-
post_inlining.codegen_units.len() > 0,
308+
codegen_units.len() > 0,
290309
"There must be at least one CGU that code coverage data can be generated in."
291310
);
292311

@@ -297,7 +316,7 @@ where
297316
// the object file (CGU) containing the dead function stubs is included
298317
// in the final binary. This will probably require forcing these
299318
// function symbols to be included via `-u` or `/include` linker args.
300-
let mut cgus: Vec<_> = post_inlining.codegen_units.iter_mut().collect();
319+
let mut cgus: Vec<_> = codegen_units.iter_mut().collect();
301320
cgus.sort_by_key(|cgu| cgu.size_estimate());
302321

303322
let dead_code_cgu =
@@ -308,29 +327,17 @@ where
308327
} else {
309328
// If there are no CGUs that have externally linked items,
310329
// then we just pick the first CGU as a fallback.
311-
&mut post_inlining.codegen_units[0]
330+
&mut codegen_units[0]
312331
};
313332
dead_code_cgu.make_code_coverage_dead_code_cgu();
314333
}
315334

316335
// Finally, sort by codegen unit name, so that we get deterministic results.
317-
let PostInliningPartitioning {
318-
codegen_units: mut result,
319-
mono_item_placements: _,
320-
internalization_candidates: _,
321-
} = post_inlining;
336+
codegen_units.sort_by(|a, b| a.name().as_str().cmp(b.name().as_str()));
322337

323-
result.sort_by(|a, b| a.name().as_str().cmp(b.name().as_str()));
338+
debug_dump(tcx, "FINAL", &codegen_units);
324339

325-
debug_dump(tcx, "FINAL", &result);
326-
327-
result
328-
}
329-
330-
pub struct PreInliningPartitioning<'tcx> {
331-
codegen_units: Vec<CodegenUnit<'tcx>>,
332-
roots: FxHashSet<MonoItem<'tcx>>,
333-
internalization_candidates: FxHashSet<MonoItem<'tcx>>,
340+
codegen_units
334341
}
335342

336343
/// For symbol internalization, we need to know whether a symbol/mono-item is
@@ -342,12 +349,6 @@ enum MonoItemPlacement {
342349
MultipleCgus,
343350
}
344351

345-
struct PostInliningPartitioning<'tcx> {
346-
codegen_units: Vec<CodegenUnit<'tcx>>,
347-
mono_item_placements: FxHashMap<MonoItem<'tcx>, MonoItemPlacement>,
348-
internalization_candidates: FxHashSet<MonoItem<'tcx>>,
349-
}
350-
351352
fn debug_dump<'a, 'tcx: 'a>(tcx: TyCtxt<'tcx>, label: &str, cgus: &[CodegenUnit<'tcx>]) {
352353
let dump = move || {
353354
use std::fmt::Write;

0 commit comments

Comments
 (0)
Please sign in to comment.