Skip to content

Commit 23b0c96

Browse files
committed
Allow codegen backends to opt-out of parallel codegen
1 parent 9ace9da commit 23b0c96

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

compiler/rustc_codegen_ssa/src/back/write.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,12 @@ impl<B: WriteBackendMethods> CodegenContext<B> {
383383
}
384384
}
385385

386+
impl<B: CodegenBackend + WriteBackendMethods> CodegenContext<B> {
387+
pub fn parallel(&self) -> bool {
388+
self.backend.supports_parallel() && !self.opts.unstable_opts.no_parallel_llvm
389+
}
390+
}
391+
386392
fn generate_lto_work<B: ExtraBackendMethods>(
387393
cgcx: &CodegenContext<B>,
388394
needs_fat_lto: Vec<FatLtoInput<B>>,
@@ -1400,7 +1406,7 @@ fn start_executing_work<B: ExtraBackendMethods>(
14001406
.binary_search_by_key(&cost, |&(_, cost)| cost)
14011407
.unwrap_or_else(|e| e);
14021408
work_items.insert(insertion_index, (work, cost));
1403-
if !cgcx.opts.unstable_opts.no_parallel_llvm {
1409+
if cgcx.parallel() {
14041410
helper.request_token();
14051411
}
14061412
}
@@ -1523,7 +1529,7 @@ fn start_executing_work<B: ExtraBackendMethods>(
15231529
};
15241530
work_items.insert(insertion_index, (llvm_work_item, cost));
15251531

1526-
if !cgcx.opts.unstable_opts.no_parallel_llvm {
1532+
if cgcx.parallel() {
15271533
helper.request_token();
15281534
}
15291535
assert_eq!(main_thread_state, MainThreadState::Codegenning);

compiler/rustc_codegen_ssa/src/traits/backend.rs

+7
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,13 @@ pub trait CodegenBackend {
115115
codegen_results: CodegenResults,
116116
outputs: &OutputFilenames,
117117
) -> Result<(), ErrorGuaranteed>;
118+
119+
/// Returns `true` if this backend can be safely called from multiple threads.
120+
///
121+
/// Defaults to `true`.
122+
fn supports_parallel(&self) -> bool {
123+
true
124+
}
118125
}
119126

120127
pub trait ExtraBackendMethods:

0 commit comments

Comments
 (0)