Skip to content

Commit

Permalink
Allow codegen backends to opt-out of parallel codegen
Browse files Browse the repository at this point in the history
  • Loading branch information
WaffleLapkin committed Oct 16, 2023
1 parent 9ace9da commit 23b0c96
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
10 changes: 8 additions & 2 deletions compiler/rustc_codegen_ssa/src/back/write.rs
Expand Up @@ -383,6 +383,12 @@ impl<B: WriteBackendMethods> CodegenContext<B> {
}
}

impl<B: CodegenBackend + WriteBackendMethods> CodegenContext<B> {
pub fn parallel(&self) -> bool {
self.backend.supports_parallel() && !self.opts.unstable_opts.no_parallel_llvm
}
}

fn generate_lto_work<B: ExtraBackendMethods>(
cgcx: &CodegenContext<B>,
needs_fat_lto: Vec<FatLtoInput<B>>,
Expand Down Expand Up @@ -1400,7 +1406,7 @@ fn start_executing_work<B: ExtraBackendMethods>(
.binary_search_by_key(&cost, |&(_, cost)| cost)
.unwrap_or_else(|e| e);
work_items.insert(insertion_index, (work, cost));
if !cgcx.opts.unstable_opts.no_parallel_llvm {
if cgcx.parallel() {
helper.request_token();
}
}
Expand Down Expand Up @@ -1523,7 +1529,7 @@ fn start_executing_work<B: ExtraBackendMethods>(
};
work_items.insert(insertion_index, (llvm_work_item, cost));

if !cgcx.opts.unstable_opts.no_parallel_llvm {
if cgcx.parallel() {
helper.request_token();
}
assert_eq!(main_thread_state, MainThreadState::Codegenning);
Expand Down
7 changes: 7 additions & 0 deletions compiler/rustc_codegen_ssa/src/traits/backend.rs
Expand Up @@ -115,6 +115,13 @@ pub trait CodegenBackend {
codegen_results: CodegenResults,
outputs: &OutputFilenames,
) -> Result<(), ErrorGuaranteed>;

/// Returns `true` if this backend can be safely called from multiple threads.
///
/// Defaults to `true`.
fn supports_parallel(&self) -> bool {
true
}
}

pub trait ExtraBackendMethods:
Expand Down

0 comments on commit 23b0c96

Please sign in to comment.