Skip to content

Commit e2d062e

Browse files
authored
Rollup merge of rust-lang#142384 - celinval:chores-rayon-mv, r=oli-obk
Bringing `rustc_rayon_core` in tree as `rustc_thread_pool` This PR moves [`rustc_rayon_core`](https://github.com/rust-lang/rustc-rayon/tree/5fadf44/rayon-core) from commit `5fadf44` as suggested in [this zulip thread](https://rust-lang.zulipchat.com/#narrow/channel/187679-t-compiler.2Fparallel-rustc/topic/Bringing.20.60rustc_rayon_core.60.20in.20tree). I tried to split the work into separate commits so it is easy to review. The first commit is a simple copy and paste from the fork, and subsequent changes were made to use the new crate and to ensure the new crate complies with different format and lint expectations. **Call-out:** I was also wondering if I need to make any further changes to accommodate licensing requirements. r? oli-obk
2 parents a388270 + 6da3bf8 commit e2d062e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+7916
-38
lines changed

Cargo.lock

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2983,6 +2983,15 @@ dependencies = [
29832983
"getrandom 0.3.3",
29842984
]
29852985

2986+
[[package]]
2987+
name = "rand_xorshift"
2988+
version = "0.4.0"
2989+
source = "registry+https://github.com/rust-lang/crates.io-index"
2990+
checksum = "513962919efc330f829edb2535844d1b912b0fbe2ca165d613e4e8788bb05a5a"
2991+
dependencies = [
2992+
"rand_core 0.9.3",
2993+
]
2994+
29862995
[[package]]
29872996
name = "rand_xoshiro"
29882997
version = "0.7.0"
@@ -3182,16 +3191,6 @@ dependencies = [
31823191
"tikv-jemalloc-sys",
31833192
]
31843193

3185-
[[package]]
3186-
name = "rustc-rayon-core"
3187-
version = "0.5.1"
3188-
source = "registry+https://github.com/rust-lang/crates.io-index"
3189-
checksum = "2f42932dcd3bcbe484b38a3ccf79b7906fac41c02d408b5b1bac26da3416efdb"
3190-
dependencies = [
3191-
"crossbeam-deque",
3192-
"crossbeam-utils",
3193-
]
3194-
31953194
[[package]]
31963195
name = "rustc-semver"
31973196
version = "1.1.0"
@@ -3560,14 +3559,14 @@ dependencies = [
35603559
"parking_lot",
35613560
"portable-atomic",
35623561
"rustc-hash 2.1.1",
3563-
"rustc-rayon-core",
35643562
"rustc-stable-hash",
35653563
"rustc_arena",
35663564
"rustc_graphviz",
35673565
"rustc_hashes",
35683566
"rustc_index",
35693567
"rustc_macros",
35703568
"rustc_serialize",
3569+
"rustc_thread_pool",
35713570
"smallvec",
35723571
"stacker",
35733572
"tempfile",
@@ -3915,7 +3914,6 @@ dependencies = [
39153914
name = "rustc_interface"
39163915
version = "0.0.0"
39173916
dependencies = [
3918-
"rustc-rayon-core",
39193917
"rustc_abi",
39203918
"rustc_ast",
39213919
"rustc_ast_lowering",
@@ -3954,6 +3952,7 @@ dependencies = [
39543952
"rustc_span",
39553953
"rustc_symbol_mangling",
39563954
"rustc_target",
3955+
"rustc_thread_pool",
39573956
"rustc_trait_selection",
39583957
"rustc_traits",
39593958
"rustc_ty_utils",
@@ -4081,7 +4080,6 @@ dependencies = [
40814080
"either",
40824081
"gsgdt",
40834082
"polonius-engine",
4084-
"rustc-rayon-core",
40854083
"rustc_abi",
40864084
"rustc_apfloat",
40874085
"rustc_arena",
@@ -4105,6 +4103,7 @@ dependencies = [
41054103
"rustc_session",
41064104
"rustc_span",
41074105
"rustc_target",
4106+
"rustc_thread_pool",
41084107
"rustc_type_ir",
41094108
"smallvec",
41104109
"thin-vec",
@@ -4351,7 +4350,6 @@ version = "0.0.0"
43514350
dependencies = [
43524351
"hashbrown",
43534352
"parking_lot",
4354-
"rustc-rayon-core",
43554353
"rustc_abi",
43564354
"rustc_ast",
43574355
"rustc_attr_data_structures",
@@ -4366,6 +4364,7 @@ dependencies = [
43664364
"rustc_serialize",
43674365
"rustc_session",
43684366
"rustc_span",
4367+
"rustc_thread_pool",
43694368
"smallvec",
43704369
"tracing",
43714370
]
@@ -4527,6 +4526,18 @@ dependencies = [
45274526
"tracing",
45284527
]
45294528

4529+
[[package]]
4530+
name = "rustc_thread_pool"
4531+
version = "0.0.0"
4532+
dependencies = [
4533+
"crossbeam-deque",
4534+
"crossbeam-utils",
4535+
"libc",
4536+
"rand 0.9.1",
4537+
"rand_xorshift",
4538+
"scoped-tls",
4539+
]
4540+
45304541
[[package]]
45314542
name = "rustc_tools_util"
45324543
version = "0.4.2"

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ exclude = [
6060
"obj",
6161
]
6262

63-
[profile.release.package.rustc-rayon-core]
63+
[profile.release.package.rustc_thread_pool]
6464
# The rustc fork of Rayon has deadlock detection code which intermittently
6565
# causes overflows in the CI (see https://github.com/rust-lang/rust/issues/90227)
6666
# so we turn overflow checks off for now.

compiler/rustc_data_structures/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ indexmap = "2.4.0"
1414
jobserver_crate = { version = "0.1.28", package = "jobserver" }
1515
measureme = "12.0.1"
1616
rustc-hash = "2.0.0"
17-
rustc-rayon-core = { version = "0.5.0" }
1817
rustc-stable-hash = { version = "0.1.0", features = ["nightly"] }
1918
rustc_arena = { path = "../rustc_arena" }
2019
rustc_graphviz = { path = "../rustc_graphviz" }
2120
rustc_hashes = { path = "../rustc_hashes" }
2221
rustc_index = { path = "../rustc_index", package = "rustc_index" }
2322
rustc_macros = { path = "../rustc_macros" }
2423
rustc_serialize = { path = "../rustc_serialize" }
24+
rustc_thread_pool = { path = "../rustc_thread_pool" }
2525
smallvec = { version = "1.8.1", features = ["const_generics", "union", "may_dangle"] }
2626
stacker = "0.1.17"
2727
tempfile = "3.2"

compiler/rustc_data_structures/src/sync.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222
//! | | | `parking_lot::Mutex<T>` |
2323
//! | `RwLock<T>` | `RefCell<T>` | `parking_lot::RwLock<T>` |
2424
//! | `MTLock<T>` [^1] | `T` | `Lock<T>` |
25-
//! | | | |
26-
//! | `ParallelIterator` | `Iterator` | `rayon::iter::ParallelIterator` |
2725
//!
2826
//! [^1]: `MTLock` is similar to `Lock`, but the serial version avoids the cost
2927
//! of a `RefCell`. This is appropriate when interior mutability is not

compiler/rustc_data_structures/src/sync/parallel.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ macro_rules! parallel {
9696
pub fn spawn(func: impl FnOnce() + DynSend + 'static) {
9797
if mode::is_dyn_thread_safe() {
9898
let func = FromDyn::from(func);
99-
rayon_core::spawn(|| {
99+
rustc_thread_pool::spawn(|| {
100100
(func.into_inner())();
101101
});
102102
} else {
@@ -107,11 +107,11 @@ pub fn spawn(func: impl FnOnce() + DynSend + 'static) {
107107
// This function only works when `mode::is_dyn_thread_safe()`.
108108
pub fn scope<'scope, OP, R>(op: OP) -> R
109109
where
110-
OP: FnOnce(&rayon_core::Scope<'scope>) -> R + DynSend,
110+
OP: FnOnce(&rustc_thread_pool::Scope<'scope>) -> R + DynSend,
111111
R: DynSend,
112112
{
113113
let op = FromDyn::from(op);
114-
rayon_core::scope(|s| FromDyn::from(op.into_inner()(s))).into_inner()
114+
rustc_thread_pool::scope(|s| FromDyn::from(op.into_inner()(s))).into_inner()
115115
}
116116

117117
#[inline]
@@ -124,7 +124,7 @@ where
124124
let oper_a = FromDyn::from(oper_a);
125125
let oper_b = FromDyn::from(oper_b);
126126
let (a, b) = parallel_guard(|guard| {
127-
rayon_core::join(
127+
rustc_thread_pool::join(
128128
move || guard.run(move || FromDyn::from(oper_a.into_inner()())),
129129
move || guard.run(move || FromDyn::from(oper_b.into_inner()())),
130130
)
@@ -158,7 +158,7 @@ fn par_slice<I: DynSend>(
158158
let (left, right) = items.split_at_mut(items.len() / 2);
159159
let mut left = state.for_each.derive(left);
160160
let mut right = state.for_each.derive(right);
161-
rayon_core::join(move || par_rec(*left, state), move || par_rec(*right, state));
161+
rustc_thread_pool::join(move || par_rec(*left, state), move || par_rec(*right, state));
162162
}
163163
}
164164

@@ -241,7 +241,7 @@ pub fn par_map<I: DynSend, T: IntoIterator<Item = I>, R: DynSend, C: FromIterato
241241
pub fn broadcast<R: DynSend>(op: impl Fn(usize) -> R + DynSync) -> Vec<R> {
242242
if mode::is_dyn_thread_safe() {
243243
let op = FromDyn::from(op);
244-
let results = rayon_core::broadcast(|context| op.derive(op(context.index())));
244+
let results = rustc_thread_pool::broadcast(|context| op.derive(op(context.index())));
245245
results.into_iter().map(|r| r.into_inner()).collect()
246246
} else {
247247
vec![op(0)]

compiler/rustc_interface/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ edition = "2024"
55

66
[dependencies]
77
# tidy-alphabetical-start
8-
rustc-rayon-core = { version = "0.5.0" }
98
rustc_ast = { path = "../rustc_ast" }
109
rustc_ast_lowering = { path = "../rustc_ast_lowering" }
1110
rustc_ast_passes = { path = "../rustc_ast_passes" }
@@ -43,6 +42,7 @@ rustc_session = { path = "../rustc_session" }
4342
rustc_span = { path = "../rustc_span" }
4443
rustc_symbol_mangling = { path = "../rustc_symbol_mangling" }
4544
rustc_target = { path = "../rustc_target" }
45+
rustc_thread_pool = { path = "../rustc_thread_pool" }
4646
rustc_trait_selection = { path = "../rustc_trait_selection" }
4747
rustc_traits = { path = "../rustc_traits" }
4848
rustc_ty_utils = { path = "../rustc_ty_utils" }

compiler/rustc_interface/src/util.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ pub(crate) fn run_in_thread_pool_with_globals<
208208

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

220220
let current_gcx2 = current_gcx2.clone();
221-
let registry = rayon_core::Registry::current();
221+
let registry = rustc_thread_pool::Registry::current();
222222
let session_globals = rustc_span::with_session_globals(|session_globals| {
223223
session_globals as *const SessionGlobals as usize
224224
});
@@ -265,7 +265,7 @@ pub(crate) fn run_in_thread_pool_with_globals<
265265
builder
266266
.build_scoped(
267267
// Initialize each new worker thread when created.
268-
move |thread: rayon_core::ThreadBuilder| {
268+
move |thread: rustc_thread_pool::ThreadBuilder| {
269269
// Register the thread for use with the `WorkerLocal` type.
270270
registry.register();
271271

@@ -274,7 +274,7 @@ pub(crate) fn run_in_thread_pool_with_globals<
274274
})
275275
},
276276
// Run `f` on the first thread in the thread pool.
277-
move |pool: &rayon_core::ThreadPool| {
277+
move |pool: &rustc_thread_pool::ThreadPool| {
278278
pool.install(|| f(current_gcx.into_inner(), proxy))
279279
},
280280
)

compiler/rustc_middle/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ bitflags = "2.4.1"
99
either = "1.5.0"
1010
gsgdt = "0.1.2"
1111
polonius-engine = "0.13.0"
12-
rustc-rayon-core = { version = "0.5.0" }
1312
rustc_abi = { path = "../rustc_abi" }
1413
rustc_apfloat = "0.2.0"
1514
rustc_arena = { path = "../rustc_arena" }
@@ -33,6 +32,7 @@ rustc_serialize = { path = "../rustc_serialize" }
3332
rustc_session = { path = "../rustc_session" }
3433
rustc_span = { path = "../rustc_span" }
3534
rustc_target = { path = "../rustc_target" }
35+
rustc_thread_pool = { path = "../rustc_thread_pool" }
3636
rustc_type_ir = { path = "../rustc_type_ir" }
3737
smallvec = { version = "1.8.1", features = ["union", "may_dangle"] }
3838
thin-vec = "0.2.12"

compiler/rustc_middle/src/ty/context/tls.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ impl<'a, 'tcx> ImplicitCtxt<'a, 'tcx> {
3636
}
3737

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

4141
#[inline]
4242
fn erase(context: &ImplicitCtxt<'_, '_>) -> *const () {

compiler/rustc_query_system/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ edition = "2024"
66
[dependencies]
77
# tidy-alphabetical-start
88
parking_lot = "0.12"
9-
rustc-rayon-core = { version = "0.5.0" }
109
rustc_abi = { path = "../rustc_abi" }
1110
rustc_ast = { path = "../rustc_ast" }
1211
rustc_attr_data_structures = { path = "../rustc_attr_data_structures" }
@@ -21,6 +20,7 @@ rustc_macros = { path = "../rustc_macros" }
2120
rustc_serialize = { path = "../rustc_serialize" }
2221
rustc_session = { path = "../rustc_session" }
2322
rustc_span = { path = "../rustc_span" }
23+
rustc_thread_pool = { path = "../rustc_thread_pool" }
2424
smallvec = { version = "1.8.1", features = ["union", "may_dangle"] }
2525
tracing = "0.1"
2626
# tidy-alphabetical-end

compiler/rustc_query_system/src/query/job.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ impl<I> QueryLatch<I> {
236236
// If this detects a deadlock and the deadlock handler wants to resume this thread
237237
// we have to be in the `wait` call. This is ensured by the deadlock handler
238238
// getting the self.info lock.
239-
rayon_core::mark_blocked();
239+
rustc_thread_pool::mark_blocked();
240240
let proxy = qcx.jobserver_proxy();
241241
proxy.release_thread();
242242
waiter.condvar.wait(&mut info);
@@ -251,9 +251,9 @@ impl<I> QueryLatch<I> {
251251
let mut info = self.info.lock();
252252
debug_assert!(!info.complete);
253253
info.complete = true;
254-
let registry = rayon_core::Registry::current();
254+
let registry = rustc_thread_pool::Registry::current();
255255
for waiter in info.waiters.drain(..) {
256-
rayon_core::mark_unblocked(&registry);
256+
rustc_thread_pool::mark_unblocked(&registry);
257257
waiter.condvar.notify_one();
258258
}
259259
}
@@ -507,7 +507,7 @@ fn remove_cycle<I: Clone>(
507507
/// all active queries for cycles before finally resuming all the waiters at once.
508508
pub fn break_query_cycles<I: Clone + Debug>(
509509
query_map: QueryMap<I>,
510-
registry: &rayon_core::Registry,
510+
registry: &rustc_thread_pool::Registry,
511511
) {
512512
let mut wakelist = Vec::new();
513513
// It is OK per the comments:
@@ -543,7 +543,7 @@ pub fn break_query_cycles<I: Clone + Debug>(
543543
// we wake the threads up as otherwise Rayon could detect a deadlock if a thread we
544544
// resumed fell asleep and this thread had yet to mark the remaining threads as unblocked.
545545
for _ in 0..wakelist.len() {
546-
rayon_core::mark_unblocked(registry);
546+
rustc_thread_pool::mark_unblocked(registry);
547547
}
548548

549549
for waiter in wakelist.into_iter() {

compiler/rustc_thread_pool/Cargo.toml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
[package]
2+
name = "rustc_thread_pool"
3+
version = "0.0.0"
4+
authors = ["Niko Matsakis <[email protected]>",
5+
"Josh Stone <[email protected]>"]
6+
description = "Core APIs for Rayon - fork for rustc"
7+
license = "MIT OR Apache-2.0"
8+
rust-version = "1.63"
9+
edition = "2021"
10+
readme = "README.md"
11+
keywords = ["parallel", "thread", "concurrency", "join", "performance"]
12+
categories = ["concurrency"]
13+
14+
[dependencies]
15+
crossbeam-deque = "0.8"
16+
crossbeam-utils = "0.8"
17+
18+
[dev-dependencies]
19+
rand = "0.9"
20+
rand_xorshift = "0.4"
21+
scoped-tls = "1.0"
22+
23+
[target.'cfg(unix)'.dev-dependencies]
24+
libc = "0.2"
25+
26+
[[test]]
27+
name = "stack_overflow_crash"
28+
path = "tests/stack_overflow_crash.rs"
29+
30+
# NB: having one [[test]] manually defined means we need to declare them all
31+
32+
[[test]]
33+
name = "double_init_fail"
34+
path = "tests/double_init_fail.rs"
35+
36+
[[test]]
37+
name = "init_zero_threads"
38+
path = "tests/init_zero_threads.rs"
39+
40+
[[test]]
41+
name = "scope_join"
42+
path = "tests/scope_join.rs"
43+
44+
[[test]]
45+
name = "simple_panic"
46+
path = "tests/simple_panic.rs"
47+
48+
[[test]]
49+
name = "scoped_threadpool"
50+
path = "tests/scoped_threadpool.rs"

compiler/rustc_thread_pool/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Note: This is an unstable fork made for use in rustc
2+
3+
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.
4+
5+
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.
6+
7+
8+
Please see [Rayon Docs] for details about using Rayon.
9+
10+
[Rayon Docs]: https://docs.rs/rayon/

0 commit comments

Comments
 (0)