Skip to content

Bringing rustc_rayon_core in tree as rustc_thread_pool #142384

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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
39 changes: 25 additions & 14 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2977,6 +2977,15 @@ dependencies = [
"getrandom 0.3.3",
]

[[package]]
name = "rand_xorshift"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "513962919efc330f829edb2535844d1b912b0fbe2ca165d613e4e8788bb05a5a"
dependencies = [
"rand_core 0.9.3",
]

[[package]]
name = "rand_xoshiro"
version = "0.7.0"
Expand Down Expand Up @@ -3176,16 +3185,6 @@ dependencies = [
"tikv-jemalloc-sys",
]

[[package]]
name = "rustc-rayon-core"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2f42932dcd3bcbe484b38a3ccf79b7906fac41c02d408b5b1bac26da3416efdb"
dependencies = [
"crossbeam-deque",
"crossbeam-utils",
]

[[package]]
name = "rustc-semver"
version = "1.1.0"
Expand Down Expand Up @@ -3554,14 +3553,14 @@ dependencies = [
"parking_lot",
"portable-atomic",
"rustc-hash 2.1.1",
"rustc-rayon-core",
"rustc-stable-hash",
"rustc_arena",
"rustc_graphviz",
"rustc_hashes",
"rustc_index",
"rustc_macros",
"rustc_serialize",
"rustc_thread_pool",
"smallvec",
"stacker",
"tempfile",
Expand Down Expand Up @@ -3908,7 +3907,6 @@ dependencies = [
name = "rustc_interface"
version = "0.0.0"
dependencies = [
"rustc-rayon-core",
"rustc_abi",
"rustc_ast",
"rustc_ast_lowering",
Expand Down Expand Up @@ -3947,6 +3945,7 @@ dependencies = [
"rustc_span",
"rustc_symbol_mangling",
"rustc_target",
"rustc_thread_pool",
"rustc_trait_selection",
"rustc_traits",
"rustc_ty_utils",
Expand Down Expand Up @@ -4074,7 +4073,6 @@ dependencies = [
"either",
"gsgdt",
"polonius-engine",
"rustc-rayon-core",
"rustc_abi",
"rustc_apfloat",
"rustc_arena",
Expand All @@ -4098,6 +4096,7 @@ dependencies = [
"rustc_session",
"rustc_span",
"rustc_target",
"rustc_thread_pool",
"rustc_type_ir",
"smallvec",
"thin-vec",
Expand Down Expand Up @@ -4344,7 +4343,6 @@ version = "0.0.0"
dependencies = [
"hashbrown",
"parking_lot",
"rustc-rayon-core",
"rustc_abi",
"rustc_ast",
"rustc_attr_data_structures",
Expand All @@ -4359,6 +4357,7 @@ dependencies = [
"rustc_serialize",
"rustc_session",
"rustc_span",
"rustc_thread_pool",
"smallvec",
"tracing",
]
Expand Down Expand Up @@ -4520,6 +4519,18 @@ dependencies = [
"tracing",
]

[[package]]
name = "rustc_thread_pool"
version = "0.0.0"
dependencies = [
"crossbeam-deque",
"crossbeam-utils",
"libc",
"rand 0.9.1",
"rand_xorshift",
"scoped-tls",
]

[[package]]
name = "rustc_tools_util"
version = "0.4.2"
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ exclude = [
"obj",
]

[profile.release.package.rustc-rayon-core]
[profile.release.package.rustc_thread_pool]
# The rustc fork of Rayon has deadlock detection code which intermittently
# causes overflows in the CI (see https://github.com/rust-lang/rust/issues/90227)
# so we turn overflow checks off for now.
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_data_structures/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ indexmap = "2.4.0"
jobserver_crate = { version = "0.1.28", package = "jobserver" }
measureme = "12.0.1"
rustc-hash = "2.0.0"
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" }
rustc_hashes = { path = "../rustc_hashes" }
rustc_index = { path = "../rustc_index", package = "rustc_index" }
rustc_macros = { path = "../rustc_macros" }
rustc_serialize = { path = "../rustc_serialize" }
rustc_thread_pool = { path = "../rustc_thread_pool" }
smallvec = { version = "1.8.1", features = ["const_generics", "union", "may_dangle"] }
stacker = "0.1.17"
tempfile = "3.2"
Expand Down
2 changes: 0 additions & 2 deletions compiler/rustc_data_structures/src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
//! | | | `parking_lot::Mutex<T>` |
//! | `RwLock<T>` | `RefCell<T>` | `parking_lot::RwLock<T>` |
//! | `MTLock<T>` [^1] | `T` | `Lock<T>` |
//! | | | |
//! | `ParallelIterator` | `Iterator` | `rayon::iter::ParallelIterator` |
//!
//! [^1]: `MTLock` is similar to `Lock`, but the serial version avoids the cost
//! of a `RefCell`. This is appropriate when interior mutability is not
Expand Down
12 changes: 6 additions & 6 deletions compiler/rustc_data_structures/src/sync/parallel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ macro_rules! parallel {
pub fn spawn(func: impl FnOnce() + DynSend + 'static) {
if mode::is_dyn_thread_safe() {
let func = FromDyn::from(func);
rayon_core::spawn(|| {
rustc_thread_pool::spawn(|| {
(func.into_inner())();
});
} else {
Expand All @@ -107,11 +107,11 @@ pub fn spawn(func: impl FnOnce() + DynSend + 'static) {
// This function only works when `mode::is_dyn_thread_safe()`.
pub fn scope<'scope, OP, R>(op: OP) -> R
where
OP: FnOnce(&rayon_core::Scope<'scope>) -> R + DynSend,
OP: FnOnce(&rustc_thread_pool::Scope<'scope>) -> R + DynSend,
R: DynSend,
{
let op = FromDyn::from(op);
rayon_core::scope(|s| FromDyn::from(op.into_inner()(s))).into_inner()
rustc_thread_pool::scope(|s| FromDyn::from(op.into_inner()(s))).into_inner()
}

#[inline]
Expand All @@ -124,7 +124,7 @@ where
let oper_a = FromDyn::from(oper_a);
let oper_b = FromDyn::from(oper_b);
let (a, b) = parallel_guard(|guard| {
rayon_core::join(
rustc_thread_pool::join(
move || guard.run(move || FromDyn::from(oper_a.into_inner()())),
move || guard.run(move || FromDyn::from(oper_b.into_inner()())),
)
Expand Down Expand Up @@ -158,7 +158,7 @@ fn par_slice<I: DynSend>(
let (left, right) = items.split_at_mut(items.len() / 2);
let mut left = state.for_each.derive(left);
let mut right = state.for_each.derive(right);
rayon_core::join(move || par_rec(*left, state), move || par_rec(*right, state));
rustc_thread_pool::join(move || par_rec(*left, state), move || par_rec(*right, state));
}
}

Expand Down Expand Up @@ -241,7 +241,7 @@ pub fn par_map<I: DynSend, T: IntoIterator<Item = I>, R: DynSend, C: FromIterato
pub fn broadcast<R: DynSend>(op: impl Fn(usize) -> R + DynSync) -> Vec<R> {
if mode::is_dyn_thread_safe() {
let op = FromDyn::from(op);
let results = rayon_core::broadcast(|context| op.derive(op(context.index())));
let results = rustc_thread_pool::broadcast(|context| op.derive(op(context.index())));
results.into_iter().map(|r| r.into_inner()).collect()
} else {
vec![op(0)]
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_interface/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ edition = "2024"

[dependencies]
# tidy-alphabetical-start
rustc-rayon-core = { version = "0.5.0" }
rustc_ast = { path = "../rustc_ast" }
rustc_ast_lowering = { path = "../rustc_ast_lowering" }
rustc_ast_passes = { path = "../rustc_ast_passes" }
Expand Down Expand Up @@ -43,6 +42,7 @@ rustc_session = { path = "../rustc_session" }
rustc_span = { path = "../rustc_span" }
rustc_symbol_mangling = { path = "../rustc_symbol_mangling" }
rustc_target = { path = "../rustc_target" }
rustc_thread_pool = { path = "../rustc_thread_pool" }
rustc_trait_selection = { path = "../rustc_trait_selection" }
rustc_traits = { path = "../rustc_traits" }
rustc_ty_utils = { path = "../rustc_ty_utils" }
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_interface/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ pub(crate) fn run_in_thread_pool_with_globals<

let proxy_ = Arc::clone(&proxy);
let proxy__ = Arc::clone(&proxy);
let builder = rayon_core::ThreadPoolBuilder::new()
let builder = rustc_thread_pool::ThreadPoolBuilder::new()
.thread_name(|_| "rustc".to_string())
.acquire_thread_handler(move || proxy_.acquire_thread())
.release_thread_handler(move || proxy__.release_thread())
Expand All @@ -218,7 +218,7 @@ pub(crate) fn run_in_thread_pool_with_globals<
// locals to it. The new thread runs the deadlock handler.

let current_gcx2 = current_gcx2.clone();
let registry = rayon_core::Registry::current();
let registry = rustc_thread_pool::Registry::current();
let session_globals = rustc_span::with_session_globals(|session_globals| {
session_globals as *const SessionGlobals as usize
});
Expand Down Expand Up @@ -265,7 +265,7 @@ pub(crate) fn run_in_thread_pool_with_globals<
builder
.build_scoped(
// Initialize each new worker thread when created.
move |thread: rayon_core::ThreadBuilder| {
move |thread: rustc_thread_pool::ThreadBuilder| {
// Register the thread for use with the `WorkerLocal` type.
registry.register();

Expand All @@ -274,7 +274,7 @@ pub(crate) fn run_in_thread_pool_with_globals<
})
},
// Run `f` on the first thread in the thread pool.
move |pool: &rayon_core::ThreadPool| {
move |pool: &rustc_thread_pool::ThreadPool| {
pool.install(|| f(current_gcx.into_inner(), proxy))
},
)
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ bitflags = "2.4.1"
either = "1.5.0"
gsgdt = "0.1.2"
polonius-engine = "0.13.0"
rustc-rayon-core = { version = "0.5.0" }
rustc_abi = { path = "../rustc_abi" }
rustc_apfloat = "0.2.0"
rustc_arena = { path = "../rustc_arena" }
Expand All @@ -33,6 +32,7 @@ rustc_serialize = { path = "../rustc_serialize" }
rustc_session = { path = "../rustc_session" }
rustc_span = { path = "../rustc_span" }
rustc_target = { path = "../rustc_target" }
rustc_thread_pool = { path = "../rustc_thread_pool" }
rustc_type_ir = { path = "../rustc_type_ir" }
smallvec = { version = "1.8.1", features = ["union", "may_dangle"] }
thin-vec = "0.2.12"
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/ty/context/tls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl<'a, 'tcx> ImplicitCtxt<'a, 'tcx> {
}

// Import the thread-local variable from Rayon, which is preserved for Rayon jobs.
use rayon_core::tlv::TLV;
use rustc_thread_pool::tlv::TLV;

#[inline]
fn erase(context: &ImplicitCtxt<'_, '_>) -> *const () {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_query_system/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ edition = "2024"
[dependencies]
# tidy-alphabetical-start
parking_lot = "0.12"
rustc-rayon-core = { version = "0.5.0" }
rustc_abi = { path = "../rustc_abi" }
rustc_ast = { path = "../rustc_ast" }
rustc_attr_data_structures = { path = "../rustc_attr_data_structures" }
Expand All @@ -21,6 +20,7 @@ rustc_macros = { path = "../rustc_macros" }
rustc_serialize = { path = "../rustc_serialize" }
rustc_session = { path = "../rustc_session" }
rustc_span = { path = "../rustc_span" }
rustc_thread_pool = { path = "../rustc_thread_pool" }
smallvec = { version = "1.8.1", features = ["union", "may_dangle"] }
tracing = "0.1"
# tidy-alphabetical-end
Expand Down
10 changes: 5 additions & 5 deletions compiler/rustc_query_system/src/query/job.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ impl<I> QueryLatch<I> {
// If this detects a deadlock and the deadlock handler wants to resume this thread
// we have to be in the `wait` call. This is ensured by the deadlock handler
// getting the self.info lock.
rayon_core::mark_blocked();
rustc_thread_pool::mark_blocked();
let proxy = qcx.jobserver_proxy();
proxy.release_thread();
waiter.condvar.wait(&mut info);
Expand All @@ -251,9 +251,9 @@ impl<I> QueryLatch<I> {
let mut info = self.info.lock();
debug_assert!(!info.complete);
info.complete = true;
let registry = rayon_core::Registry::current();
let registry = rustc_thread_pool::Registry::current();
for waiter in info.waiters.drain(..) {
rayon_core::mark_unblocked(&registry);
rustc_thread_pool::mark_unblocked(&registry);
waiter.condvar.notify_one();
}
}
Expand Down Expand Up @@ -507,7 +507,7 @@ fn remove_cycle<I: Clone>(
/// all active queries for cycles before finally resuming all the waiters at once.
pub fn break_query_cycles<I: Clone + Debug>(
query_map: QueryMap<I>,
registry: &rayon_core::Registry,
registry: &rustc_thread_pool::Registry,
) {
let mut wakelist = Vec::new();
// It is OK per the comments:
Expand Down Expand Up @@ -543,7 +543,7 @@ pub fn break_query_cycles<I: Clone + Debug>(
// we wake the threads up as otherwise Rayon could detect a deadlock if a thread we
// resumed fell asleep and this thread had yet to mark the remaining threads as unblocked.
for _ in 0..wakelist.len() {
rayon_core::mark_unblocked(registry);
rustc_thread_pool::mark_unblocked(registry);
}

for waiter in wakelist.into_iter() {
Expand Down
50 changes: 50 additions & 0 deletions compiler/rustc_thread_pool/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
[package]
name = "rustc_thread_pool"
version = "0.0.0"
authors = ["Niko Matsakis <[email protected]>",
"Josh Stone <[email protected]>"]
description = "Core APIs for Rayon - fork for rustc"
license = "MIT OR Apache-2.0"
rust-version = "1.63"
edition = "2021"
readme = "README.md"
keywords = ["parallel", "thread", "concurrency", "join", "performance"]
categories = ["concurrency"]

[dependencies]
crossbeam-deque = "0.8"
crossbeam-utils = "0.8"

[dev-dependencies]
rand = "0.9"
rand_xorshift = "0.4"
scoped-tls = "1.0"

[target.'cfg(unix)'.dev-dependencies]
libc = "0.2"

[[test]]
name = "stack_overflow_crash"
path = "tests/stack_overflow_crash.rs"

# NB: having one [[test]] manually defined means we need to declare them all

[[test]]
name = "double_init_fail"
path = "tests/double_init_fail.rs"

[[test]]
name = "init_zero_threads"
path = "tests/init_zero_threads.rs"

[[test]]
name = "scope_join"
path = "tests/scope_join.rs"

[[test]]
name = "simple_panic"
path = "tests/simple_panic.rs"

[[test]]
name = "scoped_threadpool"
path = "tests/scoped_threadpool.rs"
10 changes: 10 additions & 0 deletions compiler/rustc_thread_pool/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Note: This is an unstable fork made for use in rustc

Rayon-core represents the "core, stable" APIs of Rayon: join, scope, and so forth, as well as the ability to create custom thread-pools with ThreadPool.

Maybe worth mentioning: users are not necessarily intended to directly access rustc_thread_pool; all its APIs are mirrored in the rayon crate. To that end, the examples in the docs use rayon::join and so forth rather than rayon_core::join.


Please see [Rayon Docs] for details about using Rayon.

[Rayon Docs]: https://docs.rs/rayon/
Loading
Loading