Skip to content
Open
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
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_ssa/src/back/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1260,7 +1260,7 @@ fn start_executing_work<B: WriteBackendMethods>(
// 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(|_| {
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_codegen_ssa/src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_interface/src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,12 +379,12 @@ pub fn run_compiler<R: Send>(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()
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_interface/src/util.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -222,7 +222,7 @@ pub(crate) fn run_in_thread_pool_with_globals<F: FnOnce(CurrentGcx) -> 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 {
Expand Down
22 changes: 10 additions & 12 deletions compiler/rustc_metadata/src/rmeta/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_middle/src/dep_graph/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,7 @@ impl DepGraph {
impl DepGraphData {
fn assert_dep_node_not_yet_allocated_in_current_session<S: std::fmt::Display>(
&self,
sess: &Session,
_sess: &Session,
dep_node: &DepNode,
msg: impl FnOnce() -> S,
) {
Expand All @@ -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())
Expand Down
8 changes: 3 additions & 5 deletions compiler/rustc_query_impl/src/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
28 changes: 15 additions & 13 deletions compiler/rustc_session/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
}
}
}
Expand Down Expand Up @@ -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<NonZero<usize>>,
pub backend: Option<NonZero<usize>>,
pub frontend: NonZero<usize>,
pub backend: NonZero<usize>,
pub linker: LinkerJobs,
}

Expand All @@ -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`"));
}
Expand All @@ -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") {
Expand All @@ -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
},
};
Expand All @@ -1795,13 +1797,13 @@ fn parse_jobs_one(
s: &str,
unstable: bool,
available: &mut Option<u8>,
) -> Option<NonZero<usize>> {
) -> NonZero<usize> {
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.
Expand All @@ -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 {
Expand Down
Loading