From 9a77ec98b801e0fbd2b249d4f8edbb081c3d1e85 Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Thu, 15 Feb 2024 00:14:44 +0000 Subject: [PATCH 1/2] Rename `-Zno_parallel_llvm` -> `-Zno_parallel_backend` --- compiler/rustc_codegen_ssa/src/back/write.rs | 4 ++-- compiler/rustc_interface/src/tests.rs | 2 +- compiler/rustc_session/src/options.rs | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/compiler/rustc_codegen_ssa/src/back/write.rs b/compiler/rustc_codegen_ssa/src/back/write.rs index a63642d76b9..4a59e8faea7 100644 --- a/compiler/rustc_codegen_ssa/src/back/write.rs +++ b/compiler/rustc_codegen_ssa/src/back/write.rs @@ -1399,7 +1399,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.opts.unstable_opts.no_parallel_backend { helper.request_token(); } } @@ -1522,7 +1522,7 @@ fn start_executing_work( }; work_items.insert(insertion_index, (llvm_work_item, cost)); - if !cgcx.opts.unstable_opts.no_parallel_llvm { + if !cgcx.opts.unstable_opts.no_parallel_backend { helper.request_token(); } assert_eq!(main_thread_state, MainThreadState::Codegenning); diff --git a/compiler/rustc_interface/src/tests.rs b/compiler/rustc_interface/src/tests.rs index bfc4fc07d4c..d1fe2b3b5cd 100644 --- a/compiler/rustc_interface/src/tests.rs +++ b/compiler/rustc_interface/src/tests.rs @@ -685,7 +685,7 @@ fn test_unstable_options_tracking_hash() { untracked!(nll_facts, true); untracked!(no_analysis, true); untracked!(no_leak_check, true); - untracked!(no_parallel_llvm, true); + untracked!(no_parallel_backend, true); untracked!(parse_only, true); // `pre_link_arg` is omitted because it just forwards to `pre_link_args`. untracked!(pre_link_args, vec![String::from("abc"), String::from("def")]); diff --git a/compiler/rustc_session/src/options.rs b/compiler/rustc_session/src/options.rs index ea93ac5841f..923757eea82 100644 --- a/compiler/rustc_session/src/options.rs +++ b/compiler/rustc_session/src/options.rs @@ -1753,7 +1753,7 @@ options! { "disable the 'leak check' for subtyping; unsound, but useful for tests"), no_link: bool = (false, parse_no_flag, [TRACKED], "compile without linking"), - no_parallel_llvm: bool = (false, parse_no_flag, [UNTRACKED], + no_parallel_backend: bool = (false, parse_no_flag, [UNTRACKED], "run LLVM in non-parallel mode (while keeping codegen-units and ThinLTO)"), no_profiler_runtime: bool = (false, parse_no_flag, [TRACKED], "prevent automatic injection of the profiler_builtins crate"), From f368922cfa58d2ebc98b6153e62bcb65e27fcf67 Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Thu, 15 Feb 2024 00:23:56 +0000 Subject: [PATCH 2/2] Allow codegen backends to opt-out of parallel codegen --- compiler/rustc_codegen_ssa/src/back/write.rs | 9 +++++++-- compiler/rustc_codegen_ssa/src/traits/backend.rs | 7 +++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_codegen_ssa/src/back/write.rs b/compiler/rustc_codegen_ssa/src/back/write.rs index 4a59e8faea7..5f6d3c9aafc 100644 --- a/compiler/rustc_codegen_ssa/src/back/write.rs +++ b/compiler/rustc_codegen_ssa/src/back/write.rs @@ -369,6 +369,10 @@ pub struct CodegenContext { pub incr_comp_session_dir: Option, /// Channel back to the main control thread to send messages to pub coordinator_send: Sender>, + /// `true` if the codegen should be run in parallel. + /// + /// Depends on [`CodegenBackend::supports_parallel()`] and `-Zno_parallel_backend`. + pub parallel: bool, } impl CodegenContext { @@ -1129,6 +1133,7 @@ fn start_executing_work( target_arch: tcx.sess.target.arch.to_string(), split_debuginfo: tcx.sess.split_debuginfo(), split_dwarf_kind: tcx.sess.opts.unstable_opts.split_dwarf_kind, + parallel: backend.supports_parallel() && !sess.opts.unstable_opts.no_parallel_backend, }; // This is the "main loop" of parallel work happening for parallel codegen. @@ -1399,7 +1404,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_backend { + if cgcx.parallel { helper.request_token(); } } @@ -1522,7 +1527,7 @@ fn start_executing_work( }; work_items.insert(insertion_index, (llvm_work_item, cost)); - if !cgcx.opts.unstable_opts.no_parallel_backend { + 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 8e9907ed8bb..e8412a9b6eb 100644 --- a/compiler/rustc_codegen_ssa/src/traits/backend.rs +++ b/compiler/rustc_codegen_ssa/src/traits/backend.rs @@ -111,6 +111,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: