Skip to content

Commit 1a1067d

Browse files
Make the default parallelism 1
This changes the default parallelism for parallel compilers to one, instead of the previous default, which was "num cpus". This is likely not an optimal default long-term, but it is a good default for testing whether parallel compilers are not a significant regression over a sequential compiler. Notably, this in theory makes a parallel-enabled compiler behave exactly like a sequential compiler with respect to the jobserver.
1 parent a3639c6 commit 1a1067d

File tree

4 files changed

+24
-14
lines changed

4 files changed

+24
-14
lines changed

src/librustc/session/config.rs

+16-3
Original file line numberDiff line numberDiff line change
@@ -813,6 +813,7 @@ macro_rules! options {
813813
pub const parse_list: Option<&str> = Some("a space-separated list of strings");
814814
pub const parse_opt_list: Option<&str> = Some("a space-separated list of strings");
815815
pub const parse_opt_comma_list: Option<&str> = Some("a comma-separated list of strings");
816+
pub const parse_threads: Option<&str> = Some("a number");
816817
pub const parse_uint: Option<&str> = Some("a number");
817818
pub const parse_passes: Option<&str> =
818819
Some("a space-separated list of passes, or `all`");
@@ -956,6 +957,14 @@ macro_rules! options {
956957
}
957958
}
958959

960+
fn parse_threads(slot: &mut usize, v: Option<&str>) -> bool {
961+
match v.and_then(|s| s.parse().ok()) {
962+
Some(0) => { *slot = ::num_cpus::get(); true },
963+
Some(i) => { *slot = i; true },
964+
None => false
965+
}
966+
}
967+
959968
fn parse_uint(slot: &mut usize, v: Option<&str>) -> bool {
960969
match v.and_then(|s| s.parse().ok()) {
961970
Some(i) => { *slot = i; true },
@@ -1259,7 +1268,11 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
12591268
"prints the LLVM optimization passes being run"),
12601269
ast_json: bool = (false, parse_bool, [UNTRACKED],
12611270
"print the AST as JSON and halt"),
1262-
threads: Option<usize> = (None, parse_opt_uint, [UNTRACKED],
1271+
// We default to 1 here since we want to behave like
1272+
// a sequential compiler for now. This'll likely be adjusted
1273+
// in the future. Note that -Zthreads=0 is the way to get
1274+
// the num_cpus behavior.
1275+
threads: usize = (1, parse_threads, [UNTRACKED],
12631276
"use a thread pool with N threads"),
12641277
ast_json_noexpand: bool = (false, parse_bool, [UNTRACKED],
12651278
"print the pre-expansion AST as JSON and halt"),
@@ -2160,14 +2173,14 @@ pub fn build_session_options_and_crate_config(
21602173
}
21612174
}
21622175

2163-
if debugging_opts.threads == Some(0) {
2176+
if debugging_opts.threads == 0 {
21642177
early_error(
21652178
error_format,
21662179
"value for threads must be a positive non-zero integer",
21672180
);
21682181
}
21692182

2170-
if debugging_opts.threads.unwrap_or(1) > 1 && debugging_opts.fuel.is_some() {
2183+
if debugging_opts.threads > 1 && debugging_opts.fuel.is_some() {
21712184
early_error(
21722185
error_format,
21732186
"optimization fuel is incompatible with multiple threads",

src/librustc/session/mod.rs

+1-7
Original file line numberDiff line numberDiff line change
@@ -897,16 +897,10 @@ impl Session {
897897
ret
898898
}
899899

900-
/// Returns the number of query threads that should be used for this
901-
/// compilation
902-
pub fn threads_from_count(query_threads: Option<usize>) -> usize {
903-
query_threads.unwrap_or(::num_cpus::get())
904-
}
905-
906900
/// Returns the number of query threads that should be used for this
907901
/// compilation
908902
pub fn threads(&self) -> usize {
909-
Self::threads_from_count(self.opts.debugging_opts.threads)
903+
self.opts.debugging_opts.threads
910904
}
911905

912906
/// Returns the number of codegen units that should be used for this

src/librustc_interface/interface.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -147,5 +147,8 @@ where
147147
F: FnOnce() -> R + Send,
148148
R: Send,
149149
{
150-
util::spawn_thread_pool(edition, None, &None, f)
150+
// the 1 here is duplicating code in config.opts.debugging_opts.threads
151+
// which also defaults to 1; it ultimately doesn't matter as the default
152+
// isn't threaded, and just ignores this parameter
153+
util::spawn_thread_pool(edition, 1, &None, f)
151154
}

src/librustc_interface/util.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ pub fn scoped_thread<F: FnOnce() -> R + Send, R: Send>(cfg: thread::Builder, f:
173173
#[cfg(not(parallel_compiler))]
174174
pub fn spawn_thread_pool<F: FnOnce() -> R + Send, R: Send>(
175175
edition: Edition,
176-
_threads: Option<usize>,
176+
_threads: usize,
177177
stderr: &Option<Arc<Mutex<Vec<u8>>>>,
178178
f: F,
179179
) -> R {
@@ -198,7 +198,7 @@ pub fn spawn_thread_pool<F: FnOnce() -> R + Send, R: Send>(
198198
#[cfg(parallel_compiler)]
199199
pub fn spawn_thread_pool<F: FnOnce() -> R + Send, R: Send>(
200200
edition: Edition,
201-
threads: Option<usize>,
201+
threads: usize,
202202
stderr: &Option<Arc<Mutex<Vec<u8>>>>,
203203
f: F,
204204
) -> R {
@@ -209,7 +209,7 @@ pub fn spawn_thread_pool<F: FnOnce() -> R + Send, R: Send>(
209209
let mut config = ThreadPoolBuilder::new()
210210
.acquire_thread_handler(jobserver::acquire_thread)
211211
.release_thread_handler(jobserver::release_thread)
212-
.num_threads(Session::threads_from_count(threads))
212+
.num_threads(threads)
213213
.deadlock_handler(|| unsafe { ty::query::handle_deadlock() });
214214

215215
if let Some(size) = get_stack_size() {

0 commit comments

Comments
 (0)