Skip to content

Commit 730d6df

Browse files
committed
Auto merge of #81736 - tgnottingham:tune-cgu-scheduling-for-memory, r=nagisa
rustc_codegen_ssa: tune codegen scheduling to reduce memory usage For better throughput during parallel processing by LLVM, we used to sort CGUs largest to smallest. This would lead to better thread utilization by, for example, preventing a large CGU from being processed last and having only one LLVM thread working while the rest remained idle. However, this strategy would lead to high memory usage, as it meant the LLVM-IR for all of the largest CGUs would be resident in memory at once. Instead, we can compromise by ordering CGUs such that the largest and smallest are first, second largest and smallest are next, etc. If there are large size variations, this can reduce memory usage significantly.
2 parents 9e5d58f + 29711d8 commit 730d6df

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -3626,6 +3626,7 @@ version = "0.0.0"
36263626
dependencies = [
36273627
"bitflags",
36283628
"cc",
3629+
"itertools 0.9.0",
36293630
"jobserver",
36303631
"libc",
36313632
"memmap",

compiler/rustc_codegen_ssa/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ test = false
1010
[dependencies]
1111
bitflags = "1.2.1"
1212
cc = "1.0.1"
13+
itertools = "0.9"
1314
num_cpus = "1.0"
1415
memmap = "0.7"
1516
tracing = "0.1"

compiler/rustc_codegen_ssa/src/base.rs

+19-7
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,11 @@ use rustc_session::config::{self, EntryFnType};
3232
use rustc_session::Session;
3333
use rustc_target::abi::{Align, LayoutOf, VariantIdx};
3434

35-
use std::cmp;
3635
use std::ops::{Deref, DerefMut};
3736
use std::time::{Duration, Instant};
3837

38+
use itertools::Itertools;
39+
3940
pub fn bin_op_to_icmp_predicate(op: hir::BinOpKind, signed: bool) -> IntPredicate {
4041
match op {
4142
hir::BinOpKind::Eq => IntPredicate::IntEQ,
@@ -546,12 +547,23 @@ pub fn codegen_crate<B: ExtraBackendMethods>(
546547
ongoing_codegen.submit_pre_codegened_module_to_llvm(tcx, metadata_module);
547548
}
548549

549-
// We sort the codegen units by size. This way we can schedule work for LLVM
550-
// a bit more efficiently.
551-
let codegen_units = {
552-
let mut codegen_units = codegen_units.iter().collect::<Vec<_>>();
553-
codegen_units.sort_by_cached_key(|cgu| cmp::Reverse(cgu.size_estimate()));
554-
codegen_units
550+
// For better throughput during parallel processing by LLVM, we used to sort
551+
// CGUs largest to smallest. This would lead to better thread utilization
552+
// by, for example, preventing a large CGU from being processed last and
553+
// having only one LLVM thread working while the rest remained idle.
554+
//
555+
// However, this strategy would lead to high memory usage, as it meant the
556+
// LLVM-IR for all of the largest CGUs would be resident in memory at once.
557+
//
558+
// Instead, we can compromise by ordering CGUs such that the largest and
559+
// smallest are first, second largest and smallest are next, etc. If there
560+
// are large size variations, this can reduce memory usage significantly.
561+
let codegen_units: Vec<_> = {
562+
let mut sorted_cgus = codegen_units.iter().collect::<Vec<_>>();
563+
sorted_cgus.sort_by_cached_key(|cgu| cgu.size_estimate());
564+
565+
let (first_half, second_half) = sorted_cgus.split_at(sorted_cgus.len() / 2);
566+
second_half.iter().rev().interleave(first_half).copied().collect()
555567
};
556568

557569
// The non-parallel compiler can only translate codegen units to LLVM IR

0 commit comments

Comments
 (0)