Skip to content

Run linking and incremental saving / finalizing in parallel #121880

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3545,6 +3545,7 @@ dependencies = [
"portable-atomic",
"rustc-hash 2.1.1",
"rustc-rayon",
"rustc-rayon-core",
"rustc-stable-hash",
"rustc_arena",
"rustc_graphviz",
Expand Down
10 changes: 7 additions & 3 deletions compiler/rustc_codegen_cranelift/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
// Note: please avoid adding other feature gates where possible
#![feature(rustc_private)]
// Note: please avoid adding other feature gates where possible
#![recursion_limit = "256"]
#![warn(rust_2018_idioms)]
#![warn(unreachable_pub)]
#![warn(unused_lifetimes)]
Expand Down Expand Up @@ -43,6 +44,7 @@ use cranelift_codegen::isa::TargetIsa;
use cranelift_codegen::settings::{self, Configurable};
use rustc_codegen_ssa::CodegenResults;
use rustc_codegen_ssa::traits::CodegenBackend;
use rustc_data_structures::sync::{DynSend, downcast_box_any_dyn_send};
use rustc_metadata::EncodedMetadata;
use rustc_middle::dep_graph::{WorkProduct, WorkProductId};
use rustc_session::Session;
Expand Down Expand Up @@ -207,7 +209,7 @@ impl CodegenBackend for CraneliftCodegenBackend {
tcx: TyCtxt<'_>,
metadata: EncodedMetadata,
need_metadata_module: bool,
) -> Box<dyn Any> {
) -> Box<dyn Any + DynSend> {
info!("codegen crate {}", tcx.crate_name(LOCAL_CRATE));
let config = self.config.clone().unwrap_or_else(|| {
BackendConfig::from_opts(&tcx.sess.opts.cg.llvm_args)
Expand All @@ -226,11 +228,13 @@ impl CodegenBackend for CraneliftCodegenBackend {

fn join_codegen(
&self,
ongoing_codegen: Box<dyn Any>,
ongoing_codegen: Box<dyn Any + DynSend>,
sess: &Session,
outputs: &OutputFilenames,
) -> (CodegenResults, FxIndexMap<WorkProductId, WorkProduct>) {
ongoing_codegen.downcast::<driver::aot::OngoingCodegen>().unwrap().join(sess, outputs)
downcast_box_any_dyn_send::<driver::aot::OngoingCodegen>(ongoing_codegen)
.unwrap()
.join(sess, outputs)
}
}

Expand Down
13 changes: 6 additions & 7 deletions compiler/rustc_codegen_gcc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ use rustc_codegen_ssa::base::codegen_crate;
use rustc_codegen_ssa::traits::{CodegenBackend, ExtraBackendMethods, WriteBackendMethods};
use rustc_codegen_ssa::{CodegenResults, CompiledModule, ModuleCodegen};
use rustc_data_structures::fx::FxIndexMap;
use rustc_data_structures::sync::IntoDynSyncSend;
use rustc_data_structures::sync::{DynSend, IntoDynSyncSend, downcast_box_any_dyn_send};
use rustc_errors::DiagCtxtHandle;
use rustc_metadata::EncodedMetadata;
use rustc_middle::dep_graph::{WorkProduct, WorkProductId};
Expand Down Expand Up @@ -230,12 +230,12 @@ impl CodegenBackend for GccCodegenBackend {
providers.global_backend_features = |tcx, ()| gcc_util::global_gcc_features(tcx.sess, true)
}

fn codegen_crate(
fn codegen_crate<'tcx>(
&self,
tcx: TyCtxt<'_>,
tcx: TyCtxt<'tcx>,
metadata: EncodedMetadata,
need_metadata_module: bool,
) -> Box<dyn Any> {
) -> Box<dyn Any + DynSend> {
let target_cpu = target_cpu(tcx.sess);
let res = codegen_crate(
self.clone(),
Expand All @@ -250,12 +250,11 @@ impl CodegenBackend for GccCodegenBackend {

fn join_codegen(
&self,
ongoing_codegen: Box<dyn Any>,
ongoing_codegen: Box<dyn Any + DynSend>,
sess: &Session,
_outputs: &OutputFilenames,
) -> (CodegenResults, FxIndexMap<WorkProductId, WorkProduct>) {
ongoing_codegen
.downcast::<rustc_codegen_ssa::back::write::OngoingCodegen<GccCodegenBackend>>()
downcast_box_any_dyn_send::<rustc_codegen_ssa::back::write::OngoingCodegen<GccCodegenBackend>>( ongoing_codegen)
.expect("Expected GccCodegenBackend's OngoingCodegen, found Box<Any>")
.join(sess)
}
Expand Down
15 changes: 9 additions & 6 deletions compiler/rustc_codegen_llvm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#![feature(rustdoc_internals)]
#![feature(slice_as_array)]
#![feature(try_blocks)]
#![recursion_limit = "256"]
// tidy-alphabetical-end

use std::any::Any;
Expand All @@ -39,6 +40,7 @@ use rustc_codegen_ssa::back::write::{
use rustc_codegen_ssa::traits::*;
use rustc_codegen_ssa::{CodegenResults, CompiledModule, ModuleCodegen};
use rustc_data_structures::fx::FxIndexMap;
use rustc_data_structures::sync::{DynSend, downcast_box_any_dyn_send};
use rustc_errors::{DiagCtxtHandle, FatalError};
use rustc_metadata::EncodedMetadata;
use rustc_middle::dep_graph::{WorkProduct, WorkProductId};
Expand Down Expand Up @@ -347,7 +349,7 @@ impl CodegenBackend for LlvmCodegenBackend {
tcx: TyCtxt<'tcx>,
metadata: EncodedMetadata,
need_metadata_module: bool,
) -> Box<dyn Any> {
) -> Box<dyn Any + DynSend> {
Box::new(rustc_codegen_ssa::base::codegen_crate(
LlvmCodegenBackend(()),
tcx,
Expand All @@ -359,14 +361,15 @@ impl CodegenBackend for LlvmCodegenBackend {

fn join_codegen(
&self,
ongoing_codegen: Box<dyn Any>,
ongoing_codegen: Box<dyn Any + DynSend>,
sess: &Session,
outputs: &OutputFilenames,
) -> (CodegenResults, FxIndexMap<WorkProductId, WorkProduct>) {
let (codegen_results, work_products) = ongoing_codegen
.downcast::<rustc_codegen_ssa::back::write::OngoingCodegen<LlvmCodegenBackend>>()
.expect("Expected LlvmCodegenBackend's OngoingCodegen, found Box<Any>")
.join(sess);
let (codegen_results, work_products) = downcast_box_any_dyn_send::<
rustc_codegen_ssa::back::write::OngoingCodegen<LlvmCodegenBackend>,
>(ongoing_codegen)
.expect("Expected LlvmCodegenBackend's OngoingCodegen, found Box<dyn Any + DynSend>")
.join(sess);

if sess.opts.unstable_opts.llvm_time_trace {
sess.time("llvm_dump_timing_file", || {
Expand Down
10 changes: 5 additions & 5 deletions compiler/rustc_codegen_ssa/src/traits/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub trait BackendTypes {
type DIVariable: Copy;
}

pub trait CodegenBackend {
pub trait CodegenBackend: DynSync + DynSend {
/// Locale resources for diagnostic messages - a string the content of the Fluent resource.
/// Called before `init` so that all other functions are able to emit translatable diagnostics.
fn locale_resource(&self) -> &'static str;
Expand Down Expand Up @@ -73,16 +73,16 @@ pub trait CodegenBackend {
tcx: TyCtxt<'tcx>,
metadata: EncodedMetadata,
need_metadata_module: bool,
) -> Box<dyn Any>;
) -> Box<dyn Any + DynSend>;

/// This is called on the returned `Box<dyn Any>` from [`codegen_crate`](Self::codegen_crate)
/// This is called on the returned `Box<dyn Any + DynSend>` from [`codegen_crate`](Self::codegen_crate)
///
/// # Panics
///
/// Panics when the passed `Box<dyn Any>` was not returned by [`codegen_crate`](Self::codegen_crate).
/// Panics when the passed `Box<dyn Any + DynSend>` was not returned by [`codegen_crate`](Self::codegen_crate).
fn join_codegen(
&self,
ongoing_codegen: Box<dyn Any>,
ongoing_codegen: Box<dyn Any + DynSend>,
sess: &Session,
outputs: &OutputFilenames,
) -> (CodegenResults, FxIndexMap<WorkProductId, WorkProduct>);
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_data_structures/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ jobserver_crate = { version = "0.1.28", package = "jobserver" }
measureme = "12.0.1"
rustc-hash = "2.0.0"
rustc-rayon = { version = "0.5.1", features = ["indexmap"] }
rustc-rayon-core = { version = "0.5.0" }
rustc-stable-hash = { version = "0.1.0", features = ["nightly"] }
rustc_arena = { path = "../rustc_arena" }
rustc_graphviz = { path = "../rustc_graphviz" }
Expand Down
44 changes: 37 additions & 7 deletions compiler/rustc_data_structures/src/marker.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::alloc::Allocator;
use std::any::Any;

#[rustc_on_unimplemented(message = "`{Self}` doesn't implement `DynSend`. \
Add it to `rustc_data_structures::marker` or use `IntoDynSyncSend` if it's already `Send`")]
Expand Down Expand Up @@ -51,9 +52,20 @@ macro_rules! already_send {

// These structures are already `Send`.
already_send!(
[std::backtrace::Backtrace][std::io::Stdout][std::io::Stderr][std::io::Error][std::fs::File]
[rustc_arena::DroplessArena][crate::memmap::Mmap][crate::profiling::SelfProfiler]
[crate::owned_slice::OwnedSlice]
[std::backtrace::Backtrace]
[std::io::Stdout]
[std::io::Stderr]
[std::io::Error]
[std::fs::File]
[std::sync::Condvar]
[jobserver_crate::Client]
[jobserver_crate::HelperThread]
[jobserver_crate::Acquired]
[Box<dyn Any + Send>]
[rustc_arena::DroplessArena]
[crate::memmap::Mmap]
[crate::profiling::SelfProfiler]
[crate::owned_slice::OwnedSlice]
);

macro_rules! impl_dyn_send {
Expand All @@ -64,10 +76,14 @@ macro_rules! impl_dyn_send {

impl_dyn_send!(
[std::sync::atomic::AtomicPtr<T> where T]
[std::sync::Mutex<T> where T: ?Sized+ DynSend]
[std::sync::Mutex<T> where T: ?Sized + DynSend]
[std::sync::RwLock<T> where T: ?Sized + DynSend]
[std::sync::mpsc::Sender<T> where T: DynSend]
[std::sync::mpsc::Receiver<T> where T: DynSend]
[std::sync::Arc<T> where T: ?Sized + DynSync + DynSend]
[std::sync::OnceLock<T> where T: DynSend]
[std::sync::LazyLock<T, F> where T: DynSend, F: DynSend]
[std::thread::JoinHandle<T> where T]
[std::collections::HashSet<K, S> where K: DynSend, S: DynSend]
[std::collections::HashMap<K, V, S> where K: DynSend, V: DynSend, S: DynSend]
[std::collections::BTreeMap<K, V, A> where K: DynSend, V: DynSend, A: std::alloc::Allocator + Clone + DynSend]
Expand Down Expand Up @@ -119,9 +135,9 @@ macro_rules! already_sync {
// These structures are already `Sync`.
already_sync!(
[std::sync::atomic::AtomicBool][std::sync::atomic::AtomicUsize][std::sync::atomic::AtomicU8]
[std::sync::atomic::AtomicU32][std::backtrace::Backtrace][std::io::Error][std::fs::File]
[jobserver_crate::Client][crate::memmap::Mmap][crate::profiling::SelfProfiler]
[crate::owned_slice::OwnedSlice]
[std::sync::atomic::AtomicU32][std::backtrace::Backtrace][std::sync::Condvar]
[std::io::Error][std::fs::File][jobserver_crate::Client][crate::memmap::Mmap]
[crate::profiling::SelfProfiler][crate::owned_slice::OwnedSlice]
);

// Use portable AtomicU64 for targets without native 64-bit atomics
Expand All @@ -142,7 +158,9 @@ impl_dyn_sync!(
[std::sync::OnceLock<T> where T: DynSend + DynSync]
[std::sync::Mutex<T> where T: ?Sized + DynSend]
[std::sync::Arc<T> where T: ?Sized + DynSync + DynSend]
[std::sync::RwLock<T> where T: ?Sized + DynSend + DynSync]
[std::sync::LazyLock<T, F> where T: DynSend + DynSync, F: DynSend]
[std::sync::mpsc::SyncSender<T> where T: DynSend]
[std::collections::HashSet<K, S> where K: DynSync, S: DynSync]
[std::collections::HashMap<K, V, S> where K: DynSync, V: DynSync, S: DynSync]
[std::collections::BTreeMap<K, V, A> where K: DynSync, V: DynSync, A: std::alloc::Allocator + Clone + DynSync]
Expand Down Expand Up @@ -224,3 +242,15 @@ impl<T> std::ops::DerefMut for IntoDynSyncSend<T> {
&mut self.0
}
}

#[inline]
pub fn downcast_box_any_dyn_send<T: Any>(this: Box<dyn Any + DynSend>) -> Result<Box<T>, ()> {
if <dyn Any>::is::<T>(&*this) {
unsafe {
let (raw, alloc): (*mut (dyn Any + DynSend), _) = Box::into_raw_with_allocator(this);
Ok(Box::from_raw_in(raw as *mut T, alloc))
}
} else {
Err(())
}
}
4 changes: 4 additions & 0 deletions compiler/rustc_data_structures/src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ pub use self::worker_local::{Registry, WorkerLocal};
pub use crate::marker::*;

mod freeze;

mod task;
pub use task::{Task, task};

mod lock;
mod parallel;
mod vec;
Expand Down
101 changes: 101 additions & 0 deletions compiler/rustc_data_structures/src/sync/task.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
use std::any::Any;
use std::mem;
use std::panic::{self, AssertUnwindSafe};
use std::sync::Arc;

use parking_lot::{Condvar, Mutex};

use crate::jobserver;
use crate::sync::{DynSend, FromDyn, IntoDynSyncSend, mode};

enum TaskState<T: DynSend + 'static> {
Unexecuted(Box<dyn FnOnce() -> T + DynSend>),
Running,
Joined,
Result(Result<T, IntoDynSyncSend<Box<dyn Any + Send + 'static>>>),
}

struct TaskData<T: DynSend + 'static> {
state: Mutex<TaskState<T>>,
waiter: Condvar,
}

#[must_use]
pub struct Task<T: DynSend + 'static> {
data: Arc<TaskData<T>>,
}

/// This attempts to run a closure in a background thread. It returns a `Task` type which
/// you must call `join` on to ensure that the closure runs.
pub fn task<T: DynSend>(f: impl FnOnce() -> T + DynSend + 'static) -> Task<T> {
let task = Task {
data: Arc::new(TaskData {
state: Mutex::new(TaskState::Unexecuted(Box::new(f))),
waiter: Condvar::new(),
}),
};

if mode::is_dyn_thread_safe() {
let data = FromDyn::from(Arc::clone(&task.data));

// Try to execute the task on a separate thread.
rayon::spawn(move || {
let data = data.into_inner();
let mut state = data.state.lock();
if matches!(*state, TaskState::Unexecuted(..)) {
if let TaskState::Unexecuted(f) = mem::replace(&mut *state, TaskState::Running) {
drop(state);
let result = panic::catch_unwind(AssertUnwindSafe(f));

let unblock = {
let mut state = data.state.lock();
let unblock = matches!(*state, TaskState::Joined);
*state = TaskState::Result(result.map_err(|e| IntoDynSyncSend(e)));
unblock
};

if unblock {
rayon_core::mark_unblocked(&rayon_core::Registry::current());
}

data.waiter.notify_one();
}
}
});
}

task
}

#[inline]
fn unwind<T>(result: Result<T, IntoDynSyncSend<Box<dyn Any + Send + 'static>>>) -> T {
match result {
Ok(r) => r,
Err(err) => panic::resume_unwind(err.0),
}
}

impl<T: DynSend> Task<T> {
pub fn join(self) -> T {
let mut state_guard = self.data.state.lock();

match mem::replace(&mut *state_guard, TaskState::Joined) {
TaskState::Unexecuted(f) => f(),
TaskState::Result(result) => unwind(result),
TaskState::Running => {
rayon_core::mark_blocked();
jobserver::release_thread();

self.data.waiter.wait(&mut state_guard);

jobserver::acquire_thread();

match mem::replace(&mut *state_guard, TaskState::Joined) {
TaskState::Result(result) => unwind(result),
_ => panic!(),
}
}
TaskState::Joined => panic!(),
}
}
}
5 changes: 3 additions & 2 deletions compiler/rustc_driver_impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,13 +380,14 @@ pub fn run_compiler(at_args: &[String], callbacks: &mut (dyn Callbacks + Send))
}
}

Some(Linker::codegen_and_build_linker(tcx, &*compiler.codegen_backend))
Some(Linker::codegen_and_build_linker(tcx, compiler))
});

// Linking is done outside the `compiler.enter()` so that the
// `GlobalCtxt` within `Queries` can be freed as early as possible.
if let Some(linker) = linker {
linker.link(sess, codegen_backend);
let _timer = sess.timer("waiting for linking");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we keep a timer for the entire time linking takes?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We still have link_crate which covers the codegen backend part of linking.

linker.join();
}
})
}
Expand Down
Loading
Loading