Skip to content

Commit 1ff5c5a

Browse files
committed
Mostly working - lots of wins, some big regressions
1 parent be3d1d6 commit 1ff5c5a

File tree

2 files changed

+41
-10
lines changed

2 files changed

+41
-10
lines changed

src/librustc_middle/mir/mono.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::dep_graph::{DepConstructor, DepNode, WorkProduct, WorkProductId};
22
use crate::ich::{NodeIdHashingMode, StableHashingContext};
33
use crate::ty::print::obsolete::DefPathBasedNames;
4-
use crate::ty::{subst::InternalSubsts, Instance, InstanceDef, SymbolName, TyCtxt};
4+
use crate::ty::{subst::InternalSubsts, subst::GenericArgKind, Instance, InstanceDef, SymbolName, TyCtxt};
55
use rustc_attr::InlineAttr;
66
use rustc_data_structures::base_n;
77
use rustc_data_structures::fingerprint::Fingerprint;
@@ -68,6 +68,16 @@ impl<'tcx> MonoItem<'tcx> {
6868
}
6969
}
7070

71+
pub fn has_closure_generic_argument(&self) -> bool {
72+
match *self {
73+
MonoItem::Fn(instance) => instance.substs.non_erasable_generics().any(|arg| match arg {
74+
GenericArgKind::Type(ty) => ty.is_closure(),
75+
GenericArgKind::Lifetime(_) | GenericArgKind::Const(_) => false,
76+
}),
77+
MonoItem::Static(..) | MonoItem::GlobalAsm(..) => false,
78+
}
79+
}
80+
7181
pub fn symbol_name(&self, tcx: TyCtxt<'tcx>) -> SymbolName {
7282
match *self {
7383
MonoItem::Fn(instance) => tcx.symbol_name(instance),
@@ -207,6 +217,14 @@ impl<'tcx> MonoItem<'tcx> {
207217
}
208218
.map(|hir_id| tcx.hir().span(hir_id))
209219
}
220+
221+
pub fn is_local(&self) -> bool {
222+
match *self {
223+
MonoItem::Fn(Instance { def, .. }) => def.def_id().is_local(),
224+
MonoItem::Static(def_id) => def_id.is_local(),
225+
MonoItem::GlobalAsm(..) => true
226+
}
227+
}
210228
}
211229

212230
impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for MonoItem<'tcx> {

src/librustc_mir/monomorphize/partitioning.rs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ use rustc_middle::mir::mono::{InstantiationMode, MonoItem};
106106
use rustc_middle::ty::print::characteristic_def_id_of_type;
107107
use rustc_middle::ty::query::Providers;
108108
use rustc_middle::ty::{self, DefIdTree, InstanceDef, TyCtxt};
109+
use rustc_session::config::OptLevel;
109110
use rustc_span::symbol::{Symbol, SymbolStr};
110111

111112
use crate::monomorphize::collector::InliningMap;
@@ -137,6 +138,8 @@ where
137138

138139
initial_partitioning.codegen_units.iter_mut().for_each(|cgu| cgu.estimate_size(tcx));
139140

141+
initial_partitioning.codegen_units.sort_by_key(|cgu| cgu.name().as_str());
142+
140143
debug_dump(tcx, "INITIAL PARTITIONING:", initial_partitioning.codegen_units.iter());
141144

142145
// Merge until we have at most `max_cgu_count` codegen units.
@@ -152,7 +155,8 @@ where
152155
// local functions the definition of which is marked with `#[inline]`.
153156
let mut post_inlining = {
154157
let _prof_timer = tcx.prof.generic_activity("cgu_partitioning_place_inline_items");
155-
place_inlined_mono_items(initial_partitioning, inlining_map)
158+
let is_debug_incremental = tcx.sess.opts.optimize == OptLevel::No && tcx.sess.opts.incremental.is_some();
159+
place_inlined_mono_items(initial_partitioning, inlining_map, is_debug_incremental)
156160
};
157161

158162
post_inlining.codegen_units.iter_mut().for_each(|cgu| cgu.estimate_size(tcx));
@@ -226,15 +230,19 @@ where
226230
let characteristic_def_id = characteristic_def_id_of_mono_item(tcx, mono_item);
227231
let is_volatile = is_incremental_build && mono_item.is_generic_fn();
228232

229-
let codegen_unit_name = match characteristic_def_id {
230-
Some(def_id) => compute_codegen_unit_name(
233+
let codegen_unit_name = match (characteristic_def_id, mono_item.is_local()) {
234+
(Some(def_id), false) if is_incremental_build && tcx.sess.opts.optimize == OptLevel::No => {
235+
let crate_name = tcx.crate_name(def_id.krate);
236+
cgu_name_builder.build_cgu_name(LOCAL_CRATE, &[&*crate_name.as_str(), if mono_item.has_closure_generic_argument() { "has_closure" } else { "" }], Some("cgu"))
237+
},
238+
(Some(def_id), _) => compute_codegen_unit_name(
231239
tcx,
232240
cgu_name_builder,
233241
def_id,
234242
is_volatile,
235243
cgu_name_cache,
236244
),
237-
None => fallback_cgu_name(cgu_name_builder),
245+
(None, _) => fallback_cgu_name(cgu_name_builder),
238246
};
239247

240248
let codegen_unit = codegen_units
@@ -459,7 +467,7 @@ fn merge_codegen_units<'tcx>(
459467
assert!(target_cgu_count >= 1);
460468
let codegen_units = &mut initial_partitioning.codegen_units;
461469

462-
if tcx.is_compiler_builtins(LOCAL_CRATE) {
470+
if tcx.is_compiler_builtins(LOCAL_CRATE) || (tcx.dep_graph.is_fully_enabled() && tcx.sess.opts.optimize == OptLevel::No) {
463471
// Compiler builtins require some degree of control over how mono items
464472
// are partitioned into compilation units. Provide it by keeping the
465473
// original partitioning when compiling the compiler builtins crate.
@@ -555,6 +563,7 @@ fn merge_codegen_units<'tcx>(
555563
fn place_inlined_mono_items<'tcx>(
556564
initial_partitioning: PreInliningPartitioning<'tcx>,
557565
inlining_map: &InliningMap<'tcx>,
566+
is_debug_incremental: bool,
558567
) -> PostInliningPartitioning<'tcx> {
559568
let mut new_partitioning = Vec::new();
560569
let mut mono_item_placements = FxHashMap::default();
@@ -587,10 +596,14 @@ fn place_inlined_mono_items<'tcx>(
587596
);
588597
}
589598

590-
// This is a CGU-private copy.
591-
new_codegen_unit
592-
.items_mut()
593-
.insert(mono_item, (Linkage::Internal, Visibility::Default));
599+
// In debug-incremental, do not create CGU-private copies unless it's for an external symbol
600+
// FIXME: put external symbols in a separate codegen unit
601+
if !is_debug_incremental || !mono_item.is_local() {
602+
// This is a CGU-private copy.
603+
new_codegen_unit
604+
.items_mut()
605+
.insert(mono_item, (Linkage::Internal, Visibility::Default));
606+
}
594607
}
595608

596609
if !single_codegen_unit {

0 commit comments

Comments
 (0)