Skip to content

Commit b39b709

Browse files
committed
Remove the merging module.
Three of the four methods in `DefaultPartitioning` are defined in `default.rs`. But `merge_codegen_units` is defined in a separate module, `merging`, even though it's less than 100 lines of code and roughly the same size as the other three methods. (Also, the `merging` module currently sits alongside `default`, when it should be a submodule of `default`, adding to the confusion.) In rust-lang#74275 this explanation was given: > I pulled this out into a separate module since it seemed like we might > want a few different merge algorithms to choose from. But in the three years since there have been no additional merging algorithms, and there is no mechanism for choosing between different merging algorithms. (There is a mechanism, `-Zcgu-partitioning-strategy`, for choosing between different partitioning strategies, but the merging algorithm is just one piece of a partitioning strategy.) This commit merges `merging` into `default`, making the code easier to navigate and read.
1 parent e26c0c9 commit b39b709

File tree

3 files changed

+94
-109
lines changed

3 files changed

+94
-109
lines changed

compiler/rustc_monomorphize/src/partitioning/default.rs

Lines changed: 94 additions & 2 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,7 +15,6 @@ use rustc_span::symbol::Symbol;
1415

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

109201
fn place_inlined_mono_items(

compiler/rustc_monomorphize/src/partitioning/merging.rs

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

compiler/rustc_monomorphize/src/partitioning/mod.rs

Lines changed: 0 additions & 1 deletion
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};

0 commit comments

Comments
 (0)