Skip to content

Commit 67e6b4b

Browse files
committed
Fix compiler panic with a large number of threads
1 parent 16422db commit 67e6b4b

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

Diff for: compiler/rustc_session/src/config.rs

+7
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use std::collections::btree_map::{
99
use std::collections::{BTreeMap, BTreeSet};
1010
use std::ffi::OsStr;
1111
use std::hash::Hash;
12+
use std::num::NonZero;
1213
use std::path::{Path, PathBuf};
1314
use std::str::{self, FromStr};
1415
use std::sync::LazyLock;
@@ -2464,6 +2465,12 @@ pub fn build_session_options(early_dcx: &mut EarlyDiagCtxt, matches: &getopts::M
24642465
early_dcx.early_fatal("value for threads must be a positive non-zero integer");
24652466
}
24662467

2468+
let mtc =
2469+
std::thread::available_parallelism().map_or(parse::MAX_THREADS_CAP, NonZero::<usize>::get);
2470+
if unstable_opts.threads == mtc {
2471+
early_dcx.early_warn(format!("number of threads was capped at {}", mtc));
2472+
}
2473+
24672474
let fuel = unstable_opts.fuel.is_some() || unstable_opts.print_fuel.is_some();
24682475
if fuel && unstable_opts.threads > 1 {
24692476
early_dcx.early_fatal("optimization fuel is incompatible with multiple threads");

Diff for: compiler/rustc_session/src/options.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -457,10 +457,11 @@ mod desc {
457457
"either a boolean (`yes`, `no`, `on`, `off`, etc), or `nll` (default: `nll`)";
458458
}
459459

460-
mod parse {
460+
pub mod parse {
461461
use std::str::FromStr;
462462

463463
pub(crate) use super::*;
464+
pub(crate) const MAX_THREADS_CAP: usize = 256;
464465

465466
/// This is for boolean options that don't take a value and start with
466467
/// `no-`. This style of option is deprecated.
@@ -663,7 +664,11 @@ mod parse {
663664
true
664665
}
665666
Some(i) => {
666-
*slot = i;
667+
// We want to cap the number of threads here to avoid large numbers like 999999 and compiler panics.
668+
// This solution was suggested here https://github.com/rust-lang/rust/issues/117638#issuecomment-1800925067
669+
let mtc = std::thread::available_parallelism()
670+
.map_or(MAX_THREADS_CAP, NonZero::<usize>::get);
671+
*slot = i.min(mtc);
667672
true
668673
}
669674
None => false,

0 commit comments

Comments
 (0)