From 23b0c9625d3a762e381e16a793cee9ae45099b00 Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Mon, 16 Oct 2023 11:51:04 +0000 Subject: [PATCH] Allow codegen backends to opt-out of parallel codegen --- compiler/rustc_codegen_ssa/src/back/write.rs | 10 ++++++++-- compiler/rustc_codegen_ssa/src/traits/backend.rs | 7 +++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_codegen_ssa/src/back/write.rs b/compiler/rustc_codegen_ssa/src/back/write.rs index 3d6a212433463..c5291c39b409e 100644 --- a/compiler/rustc_codegen_ssa/src/back/write.rs +++ b/compiler/rustc_codegen_ssa/src/back/write.rs @@ -383,6 +383,12 @@ impl CodegenContext { } } +impl CodegenContext { + pub fn parallel(&self) -> bool { + self.backend.supports_parallel() && !self.opts.unstable_opts.no_parallel_llvm + } +} + fn generate_lto_work( cgcx: &CodegenContext, needs_fat_lto: Vec>, @@ -1400,7 +1406,7 @@ fn start_executing_work( .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(); } } @@ -1523,7 +1529,7 @@ fn start_executing_work( }; 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); diff --git a/compiler/rustc_codegen_ssa/src/traits/backend.rs b/compiler/rustc_codegen_ssa/src/traits/backend.rs index ac8123bc1be91..6643b0f89df84 100644 --- a/compiler/rustc_codegen_ssa/src/traits/backend.rs +++ b/compiler/rustc_codegen_ssa/src/traits/backend.rs @@ -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: