Skip to content

[experiment] Make incremental compilation respect the -Ccodegen-units flag. #67834

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 5 additions & 20 deletions src/librustc_mir/monomorphize/partitioning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,6 @@ use rustc_span::symbol::Symbol;
use crate::monomorphize::collector::InliningMap;
use crate::monomorphize::collector::{self, MonoItemCollectionMode};

pub enum PartitioningStrategy {
/// Generates one codegen unit per source-level module.
PerModule,

/// Partition the whole crate into a fixed number of codegen units.
FixedUnitCount(usize),
}

// Anything we can't find a proper codegen unit for goes into this.
fn fallback_cgu_name(name_builder: &mut CodegenUnitNameBuilder<'_>) -> Symbol {
name_builder.build_cgu_name(LOCAL_CRATE, &["fallback"], Some("cgu"))
Expand All @@ -128,7 +120,7 @@ fn fallback_cgu_name(name_builder: &mut CodegenUnitNameBuilder<'_>) -> Symbol {
pub fn partition<'tcx, I>(
tcx: TyCtxt<'tcx>,
mono_items: I,
strategy: PartitioningStrategy,
max_cgu_count: usize,
inlining_map: &InliningMap<'tcx>,
) -> Vec<CodegenUnit<'tcx>>
where
Expand All @@ -148,11 +140,10 @@ where

debug_dump(tcx, "INITIAL PARTITIONING:", initial_partitioning.codegen_units.iter());

// If the partitioning should produce a fixed count of codegen units, merge
// until that count is reached.
if let PartitioningStrategy::FixedUnitCount(count) = strategy {
// Merge until we have at most `max_cgu_count` codegen units.
{
let _prof_timer = tcx.prof.generic_activity("cgu_partitioning_merge_cgus");
merge_codegen_units(tcx, &mut initial_partitioning, count);
merge_codegen_units(tcx, &mut initial_partitioning, max_cgu_count);
debug_dump(tcx, "POST MERGING:", initial_partitioning.codegen_units.iter());
}

Expand Down Expand Up @@ -875,13 +866,7 @@ fn collect_and_partition_mono_items(
let (codegen_units, _) = tcx.sess.time("partition_and_assert_distinct_symbols", || {
sync::join(
|| {
let strategy = if tcx.sess.opts.incremental.is_some() {
PartitioningStrategy::PerModule
} else {
PartitioningStrategy::FixedUnitCount(tcx.sess.codegen_units())
};

partition(tcx, items.iter().cloned(), strategy, &inlining_map)
partition(tcx, items.iter().cloned(), tcx.sess.codegen_units(), &inlining_map)
.into_iter()
.map(Arc::new)
.collect::<Vec<_>>()
Expand Down
7 changes: 7 additions & 0 deletions src/librustc_session/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,13 @@ impl Session {
return n as usize;
}

// If incremental compilation is turned on, we default to a high number
// codegen units in order to reduce the "collatoral damage" small
// changes cause.
if self.opts.incremental.is_some() {
return 256;
}

// Why is 16 codegen units the default all the time?
//
// The main reason for enabling multiple codegen units by default is to
Expand Down