Skip to content

Commit f368922

Browse files
committed
Allow codegen backends to opt-out of parallel codegen
1 parent 9a77ec9 commit f368922

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

compiler/rustc_codegen_ssa/src/back/write.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,10 @@ pub struct CodegenContext<B: WriteBackendMethods> {
369369
pub incr_comp_session_dir: Option<PathBuf>,
370370
/// Channel back to the main control thread to send messages to
371371
pub coordinator_send: Sender<Box<dyn Any + Send>>,
372+
/// `true` if the codegen should be run in parallel.
373+
///
374+
/// Depends on [`CodegenBackend::supports_parallel()`] and `-Zno_parallel_backend`.
375+
pub parallel: bool,
372376
}
373377

374378
impl<B: WriteBackendMethods> CodegenContext<B> {
@@ -1129,6 +1133,7 @@ fn start_executing_work<B: ExtraBackendMethods>(
11291133
target_arch: tcx.sess.target.arch.to_string(),
11301134
split_debuginfo: tcx.sess.split_debuginfo(),
11311135
split_dwarf_kind: tcx.sess.opts.unstable_opts.split_dwarf_kind,
1136+
parallel: backend.supports_parallel() && !sess.opts.unstable_opts.no_parallel_backend,
11321137
};
11331138

11341139
// This is the "main loop" of parallel work happening for parallel codegen.
@@ -1399,7 +1404,7 @@ fn start_executing_work<B: ExtraBackendMethods>(
13991404
.binary_search_by_key(&cost, |&(_, cost)| cost)
14001405
.unwrap_or_else(|e| e);
14011406
work_items.insert(insertion_index, (work, cost));
1402-
if !cgcx.opts.unstable_opts.no_parallel_backend {
1407+
if cgcx.parallel {
14031408
helper.request_token();
14041409
}
14051410
}
@@ -1522,7 +1527,7 @@ fn start_executing_work<B: ExtraBackendMethods>(
15221527
};
15231528
work_items.insert(insertion_index, (llvm_work_item, cost));
15241529

1525-
if !cgcx.opts.unstable_opts.no_parallel_backend {
1530+
if cgcx.parallel {
15261531
helper.request_token();
15271532
}
15281533
assert_eq!(main_thread_state, MainThreadState::Codegenning);

compiler/rustc_codegen_ssa/src/traits/backend.rs

+7
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,13 @@ pub trait CodegenBackend {
111111
codegen_results: CodegenResults,
112112
outputs: &OutputFilenames,
113113
) -> Result<(), ErrorGuaranteed>;
114+
115+
/// Returns `true` if this backend can be safely called from multiple threads.
116+
///
117+
/// Defaults to `true`.
118+
fn supports_parallel(&self) -> bool {
119+
true
120+
}
114121
}
115122

116123
pub trait ExtraBackendMethods:

0 commit comments

Comments
 (0)