diff --git a/compiler/rustc_codegen_ssa/src/back/write.rs b/compiler/rustc_codegen_ssa/src/back/write.rs index 60ac8a2663b90..4304908b4b5ac 100644 --- a/compiler/rustc_codegen_ssa/src/back/write.rs +++ b/compiler/rustc_codegen_ssa/src/back/write.rs @@ -1260,7 +1260,7 @@ fn start_executing_work( // tokens before releasing them, so we can never accidentally release the last token // permanently held by rustc process. let parallel = match sess.opts.jobs.backend { - Some(n) if backend.supports_parallel() => Some(n), + n if backend.supports_parallel() => Some(n), _ => None, }; let jobserver_helper = parallel.map(|_| { diff --git a/compiler/rustc_codegen_ssa/src/base.rs b/compiler/rustc_codegen_ssa/src/base.rs index d909316194566..32843a3811d73 100644 --- a/compiler/rustc_codegen_ssa/src/base.rs +++ b/compiler/rustc_codegen_ssa/src/base.rs @@ -820,7 +820,8 @@ pub fn codegen_crate< // This likely is a temporary measure. Once we don't have to support the // non-parallel compiler anymore, we can compile CGUs end-to-end in // parallel and get rid of the complicated scheduling logic. - let mut pre_compiled_cgus = if let Some(threads) = tcx.sess.opts.jobs.frontend { + let mut pre_compiled_cgus = if true { + let threads = tcx.sess.opts.jobs.frontend; tcx.sess.time("compile_first_CGU_batch", || { // Try to find one CGU to compile per thread. let cgus: Vec<_> = cgu_reuse diff --git a/compiler/rustc_interface/src/interface.rs b/compiler/rustc_interface/src/interface.rs index da1f7d2a33967..897cb29e346a5 100644 --- a/compiler/rustc_interface/src/interface.rs +++ b/compiler/rustc_interface/src/interface.rs @@ -379,12 +379,12 @@ pub fn run_compiler(config: Config, f: impl FnOnce(&Compiler) -> R + Se trace!("run_compiler"); // Set parallel mode before thread pool creation, which will create `Lock`s. - rustc_data_structures::sync::set_dyn_thread_safe_mode(config.opts.jobs.frontend.is_some()); + rustc_data_structures::sync::set_dyn_thread_safe_mode(true); // Initialize jobserver as early as possible. let early_dcx = EarlyDiagCtxt::new(config.opts.error_format); let jobs = config.opts.jobs; - if let Some(limit) = jobs.frontend.max(jobs.backend).max(jobs.linker.limit()) { + if let Some(limit) = Some(jobs.frontend).max(Some(jobs.backend)).max(jobs.linker.limit()) { jobserver::initialize(limit.get(), |err| { let note = "the build environment is likely misconfigured"; early_dcx.early_struct_warn(err).with_note(note).emit() diff --git a/compiler/rustc_interface/src/util.rs b/compiler/rustc_interface/src/util.rs index 5173f12d6fb66..fa8b5546a65f9 100644 --- a/compiler/rustc_interface/src/util.rs +++ b/compiler/rustc_interface/src/util.rs @@ -1,6 +1,6 @@ use std::any::Any; use std::env::consts::{DLL_PREFIX, DLL_SUFFIX}; -use std::num::NonZero; +// use std::num::NonZero; use std::path::{Path, PathBuf}; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::{Arc, OnceLock}; @@ -222,7 +222,7 @@ pub(crate) fn run_in_thread_pool_with_globals R + Send, let thread_stack_size = init_stack_size(thread_builder_diag); - let jobs_frontend = jobs.frontend.or(NonZero::new(1)).unwrap(); + let jobs_frontend = jobs.frontend; let registry = sync::Registry::new(jobs_frontend); let Some(proof) = sync::check_dyn_thread_safe() else { diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index 7c2354ebc61a1..c04a765eb68ce 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -2517,18 +2517,16 @@ pub fn encode_metadata(tcx: TyCtxt<'_>, path: &Path, ref_path: Option<&Path>) { return; }; - if tcx.sess.opts.jobs.frontend.is_some() { - // Prefetch some queries used by metadata encoding. - // This is not necessary for correctness, but is only done for performance reasons. - // It can be removed if it turns out to cause trouble or be detrimental to performance. - par_join( - || prefetch_mir(tcx), - || { - let _ = tcx.exported_non_generic_symbols(LOCAL_CRATE); - let _ = tcx.exported_generic_symbols(LOCAL_CRATE); - }, - ); - } + // Prefetch some queries used by metadata encoding. + // This is not necessary for correctness, but is only done for performance reasons. + // It can be removed if it turns out to cause trouble or be detrimental to performance. + par_join( + || prefetch_mir(tcx), + || { + let _ = tcx.exported_non_generic_symbols(LOCAL_CRATE); + let _ = tcx.exported_generic_symbols(LOCAL_CRATE); + }, + ); // Perform metadata encoding inside a task, so the dep-graph can check if any encoded // information changes, and maybe reuse the work product. diff --git a/compiler/rustc_middle/src/dep_graph/graph.rs b/compiler/rustc_middle/src/dep_graph/graph.rs index a4165d793069d..7c3f11577f028 100644 --- a/compiler/rustc_middle/src/dep_graph/graph.rs +++ b/compiler/rustc_middle/src/dep_graph/graph.rs @@ -667,7 +667,7 @@ impl DepGraph { impl DepGraphData { fn assert_dep_node_not_yet_allocated_in_current_session( &self, - sess: &Session, + _sess: &Session, dep_node: &DepNode, msg: impl FnOnce() -> S, ) { @@ -676,7 +676,7 @@ impl DepGraphData { let ok = match color { DepNodeColor::Unknown => true, DepNodeColor::Red => false, - DepNodeColor::Green(..) => sess.opts.jobs.frontend.is_some(), // Other threads may mark this green + DepNodeColor::Green(..) => true, // Other threads may mark this green }; if !ok { panic!("{}", msg()) diff --git a/compiler/rustc_query_impl/src/execution.rs b/compiler/rustc_query_impl/src/execution.rs index d220826670729..905f6af7be597 100644 --- a/compiler/rustc_query_impl/src/execution.rs +++ b/compiler/rustc_query_impl/src/execution.rs @@ -231,11 +231,9 @@ fn try_execute_query<'tcx, C: QueryCache, const INCR: bool>( // re-executing the query since `try_start` only checks that the query is not currently // executing, but another thread may have already completed the query and stores it result // in the query cache. - if tcx.sess.opts.jobs.frontend.is_some() { - if let Some((value, index)) = query.cache.lookup(&key) { - tcx.prof.query_cache_hit(index.into()); - return (value, Some(index)); - } + if let Some((value, index)) = query.cache.lookup(&key) { + tcx.prof.query_cache_hit(index.into()); + return (value, Some(index)); } let current_job_id = current_query_job(); diff --git a/compiler/rustc_session/src/config.rs b/compiler/rustc_session/src/config.rs index d19566f8f632e..6fc1d4141267a 100644 --- a/compiler/rustc_session/src/config.rs +++ b/compiler/rustc_session/src/config.rs @@ -1515,7 +1515,11 @@ impl Default for Options { verbose: false, target_modifiers: BTreeMap::default(), mitigation_coverage_map: Default::default(), - jobs: Jobs { frontend: None, backend: None, linker: LinkerJobs::Default }, + jobs: Jobs { + frontend: NonZero::new(1).unwrap(), + backend: NonZero::new(1).unwrap(), + linker: LinkerJobs::Default, + }, } } } @@ -1705,12 +1709,10 @@ impl LinkerJobs { } } -/// `None` for frontend and backend means everything is single-threaded -/// and synchronization can be disabled. #[derive(Clone, Copy)] pub struct Jobs { - pub frontend: Option>, - pub backend: Option>, + pub frontend: NonZero, + pub backend: NonZero, pub linker: LinkerJobs, } @@ -1728,9 +1730,9 @@ fn parse_jobs_all( let jobs = matches .opt_str("jobs") .map(|s| parse_jobs_one(early_dcx, "--jobs", &s, unstable, &mut available)); - let check_upper_limit = |value: Option<_>, opt_name| { + let check_upper_limit = |value, opt_name| { if let Some(jobs) = jobs - && value.or(NonZero::new(1)) > jobs.or(NonZero::new(1)) + && value > jobs { early_dcx.early_fatal(format!("`{opt_name}` cannot be larger than `--jobs`")); } @@ -1754,7 +1756,7 @@ fn parse_jobs_all( check_upper_limit(frontend, opt_name); frontend } - None => jobs.flatten(), + None => jobs.or(NonZero::new(1)).unwrap(), }, }; let backend = match matches.opt_str("jobs-backend") { @@ -1777,10 +1779,10 @@ fn parse_jobs_all( let linker = parse_jobs_one(early_dcx, opt_name, &jobs_linker, unstable, &mut available); check_upper_limit(linker, opt_name); - LinkerJobs::Explicit(linker.or(NonZero::new(1)).unwrap()) + LinkerJobs::Explicit(linker) } None => match jobs { - Some(n) => LinkerJobs::Explicit(n.or(NonZero::new(1)).unwrap()), + Some(n) => LinkerJobs::Explicit(n), None => LinkerJobs::Default, // back compat with lld }, }; @@ -1795,13 +1797,13 @@ fn parse_jobs_one( s: &str, unstable: bool, available: &mut Option, -) -> Option> { +) -> NonZero { if s == "sync" { // Enable synchronization overhead for benchmarking despite only using one thread. if !unstable { early_dcx.early_fatal(format!("`{opt_name}=sync` requires `-Z unstable-options`")); } - return NonZero::new(1); + return NonZero::new(1).unwrap(); } // The number of jobs is capped by 255 (`u8::MAX`) to avoid arbitrary large numbers like 999999 // causing compiler panics (#117638). The limit can be potentially increased, because e.g. @@ -1816,7 +1818,7 @@ fn parse_jobs_one( .early_fatal(format!("`{opt_name}`: expected a number from 0 to 255 or `sync`")), }; // `Jobs` uses `usize` for more convenient use, even if the actual values are limited to `u8`. - (n > 1).then_some(NonZero::new(usize::from(n)).unwrap()) + NonZero::new(usize::from(n)).unwrap() } pub fn build_configuration(sess: &Session, mut user_cfg: Cfg) -> Cfg {