Skip to content

Commit 64474a4

Browse files
committed
Move the WorkerLocal type from the rustc-rayon fork into rustc_data_structures
1 parent c6fb7b9 commit 64474a4

File tree

5 files changed

+213
-34
lines changed

5 files changed

+213
-34
lines changed

compiler/rustc_data_structures/src/sharded.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
use crate::fx::{FxHashMap, FxHasher};
2-
use crate::sync::{Lock, LockGuard};
2+
use crate::sync::{CacheAligned, Lock, LockGuard};
33
use std::borrow::Borrow;
44
use std::collections::hash_map::RawEntryMut;
55
use std::hash::{Hash, Hasher};
66
use std::mem;
77

8-
#[derive(Default)]
9-
#[cfg_attr(parallel_compiler, repr(align(64)))]
10-
struct CacheAligned<T>(T);
11-
128
#[cfg(parallel_compiler)]
139
// 32 shards is sufficient to reduce contention on an 8-core Ryzen 7 1700,
1410
// but this should be tested on higher core count CPUs. How the `Sharded` type gets used

compiler/rustc_data_structures/src/sync.rs

+7-29
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ use std::hash::{BuildHasher, Hash};
4545
use std::ops::{Deref, DerefMut};
4646
use std::panic::{catch_unwind, resume_unwind, AssertUnwindSafe};
4747

48+
mod worker_local;
49+
pub use worker_local::{Registry, WorkerLocal};
50+
4851
pub use std::sync::atomic::Ordering;
4952
pub use std::sync::atomic::Ordering::SeqCst;
5053

@@ -205,33 +208,6 @@ cfg_if! {
205208

206209
use std::cell::Cell;
207210

208-
#[derive(Debug)]
209-
pub struct WorkerLocal<T>(OneThread<T>);
210-
211-
impl<T> WorkerLocal<T> {
212-
/// Creates a new worker local where the `initial` closure computes the
213-
/// value this worker local should take for each thread in the thread pool.
214-
#[inline]
215-
pub fn new<F: FnMut(usize) -> T>(mut f: F) -> WorkerLocal<T> {
216-
WorkerLocal(OneThread::new(f(0)))
217-
}
218-
219-
/// Returns the worker-local value for each thread
220-
#[inline]
221-
pub fn into_inner(self) -> Vec<T> {
222-
vec![OneThread::into_inner(self.0)]
223-
}
224-
}
225-
226-
impl<T> Deref for WorkerLocal<T> {
227-
type Target = T;
228-
229-
#[inline(always)]
230-
fn deref(&self) -> &T {
231-
&self.0
232-
}
233-
}
234-
235211
pub type MTLockRef<'a, T> = &'a mut MTLock<T>;
236212

237213
#[derive(Debug, Default)]
@@ -351,8 +327,6 @@ cfg_if! {
351327
};
352328
}
353329

354-
pub use rayon_core::WorkerLocal;
355-
356330
pub use rayon::iter::ParallelIterator;
357331
use rayon::iter::IntoParallelIterator;
358332

@@ -383,6 +357,10 @@ pub fn assert_send<T: ?Sized + Send>() {}
383357
pub fn assert_send_val<T: ?Sized + Send>(_t: &T) {}
384358
pub fn assert_send_sync_val<T: ?Sized + Sync + Send>(_t: &T) {}
385359

360+
#[derive(Default)]
361+
#[cfg_attr(parallel_compiler, repr(align(64)))]
362+
pub struct CacheAligned<T>(pub T);
363+
386364
pub trait HashMapExt<K, V> {
387365
/// Same as HashMap::insert, but it may panic if there's already an
388366
/// entry for `key` with a value not equal to `value`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
use crate::sync::Lock;
2+
use std::cell::Cell;
3+
use std::cell::OnceCell;
4+
use std::ops::Deref;
5+
use std::ptr;
6+
use std::sync::Arc;
7+
8+
#[cfg(parallel_compiler)]
9+
use {crate::cold_path, crate::sync::CacheAligned};
10+
11+
/// A pointer to the `RegistryData` which uniquely identifies a registry.
12+
/// This identifier can be reused if the registry gets freed.
13+
#[derive(Clone, Copy, PartialEq)]
14+
struct RegistryId(*const RegistryData);
15+
16+
impl RegistryId {
17+
#[inline(always)]
18+
/// Verifies that the current thread is associated with the registry and returns its unique
19+
/// index within the registry. This panics if the current thread is not associated with this
20+
/// registry.
21+
///
22+
/// Note that there's a race possible where the identifer in `THREAD_DATA` could be reused
23+
/// so this can succeed from a different registry.
24+
#[cfg(parallel_compiler)]
25+
fn verify(self) -> usize {
26+
let (id, index) = THREAD_DATA.with(|data| (data.registry_id.get(), data.index.get()));
27+
28+
if id == self {
29+
index
30+
} else {
31+
cold_path(|| panic!("Unable to verify registry association"))
32+
}
33+
}
34+
}
35+
36+
struct RegistryData {
37+
thread_limit: usize,
38+
threads: Lock<usize>,
39+
}
40+
41+
/// Represents a list of threads which can access worker locals.
42+
#[derive(Clone)]
43+
pub struct Registry(Arc<RegistryData>);
44+
45+
thread_local! {
46+
/// The registry associated with the thread.
47+
/// This allows the `WorkerLocal` type to clone the registry in its constructor.
48+
static REGISTRY: OnceCell<Registry> = OnceCell::new();
49+
}
50+
51+
struct ThreadData {
52+
registry_id: Cell<RegistryId>,
53+
index: Cell<usize>,
54+
}
55+
56+
thread_local! {
57+
/// A thread local which contains the identifer of `REGISTRY` but allows for faster access.
58+
/// It also holds the index of the current thread.
59+
static THREAD_DATA: ThreadData = const { ThreadData {
60+
registry_id: Cell::new(RegistryId(ptr::null())),
61+
index: Cell::new(0),
62+
}};
63+
}
64+
65+
impl Registry {
66+
/// Creates a registry which can hold up to `thread_limit` threads.
67+
pub fn new(thread_limit: usize) -> Self {
68+
Registry(Arc::new(RegistryData { thread_limit, threads: Lock::new(0) }))
69+
}
70+
71+
/// Gets the registry associated with the current thread. Panics if there's no such registry.
72+
pub fn current() -> Self {
73+
REGISTRY.with(|registry| registry.get().cloned().expect("No assocated registry"))
74+
}
75+
76+
/// Registers the current thread with the registry so worker locals can be used on it.
77+
/// Panics if the thread limit is hit or if the thread already has an associated registry.
78+
pub fn register(&self) {
79+
let mut threads = self.0.threads.lock();
80+
if *threads < self.0.thread_limit {
81+
REGISTRY.with(|registry| {
82+
if registry.get().is_some() {
83+
drop(threads);
84+
panic!("Thread already has a registry");
85+
}
86+
registry.set(self.clone()).ok();
87+
THREAD_DATA.with(|data| {
88+
data.registry_id.set(self.id());
89+
data.index.set(*threads);
90+
});
91+
*threads += 1;
92+
});
93+
} else {
94+
drop(threads);
95+
panic!("Thread limit reached");
96+
}
97+
}
98+
99+
/// Gets the identifer of this registry.
100+
fn id(&self) -> RegistryId {
101+
RegistryId(&*self.0)
102+
}
103+
}
104+
105+
/// Holds worker local values for each possible thread in a registry. You can only access the
106+
/// worker local value through the `Deref` impl on the registry associated with the thread it was
107+
/// created on. It will panic otherwise.
108+
pub struct WorkerLocal<T> {
109+
#[cfg(not(parallel_compiler))]
110+
local: T,
111+
#[cfg(parallel_compiler)]
112+
locals: Box<[CacheAligned<T>]>,
113+
#[cfg(parallel_compiler)]
114+
registry: Registry,
115+
}
116+
117+
// This is safe because the `deref` call will return a reference to a `T` unique to each thread
118+
// or it will panic for threads without an associated local. So there isn't a need for `T` to do
119+
// it's own synchronization. The `verify` method on `RegistryId` has an issue where the the id
120+
// can be reused, but `WorkerLocal` has a reference to `Registry` which will prevent any reuse.
121+
#[cfg(parallel_compiler)]
122+
unsafe impl<T: Send> Sync for WorkerLocal<T> {}
123+
124+
impl<T> WorkerLocal<T> {
125+
/// Creates a new worker local where the `initial` closure computes the
126+
/// value this worker local should take for each thread in the registry.
127+
#[inline]
128+
pub fn new<F: FnMut(usize) -> T>(mut initial: F) -> WorkerLocal<T> {
129+
#[cfg(parallel_compiler)]
130+
{
131+
let registry = Registry::current();
132+
WorkerLocal {
133+
locals: (0..registry.0.thread_limit).map(|i| CacheAligned(initial(i))).collect(),
134+
registry,
135+
}
136+
}
137+
#[cfg(not(parallel_compiler))]
138+
{
139+
WorkerLocal { local: initial(0) }
140+
}
141+
}
142+
143+
/// Returns the worker-local values for each thread
144+
#[inline]
145+
pub fn into_inner(self) -> impl Iterator<Item = T> {
146+
#[cfg(parallel_compiler)]
147+
{
148+
self.locals.into_vec().into_iter().map(|local| local.0)
149+
}
150+
#[cfg(not(parallel_compiler))]
151+
{
152+
std::iter::once(self.local)
153+
}
154+
}
155+
}
156+
157+
impl<T> WorkerLocal<Vec<T>> {
158+
/// Joins the elements of all the worker locals into one Vec
159+
pub fn join(self) -> Vec<T> {
160+
self.into_inner().into_iter().flat_map(|v| v).collect()
161+
}
162+
}
163+
164+
impl<T> Deref for WorkerLocal<T> {
165+
type Target = T;
166+
167+
#[inline(always)]
168+
#[cfg(not(parallel_compiler))]
169+
fn deref(&self) -> &T {
170+
&self.local
171+
}
172+
173+
#[inline(always)]
174+
#[cfg(parallel_compiler)]
175+
fn deref(&self) -> &T {
176+
// This is safe because `verify` will only return values less than
177+
// `self.registry.thread_limit` which is the size of the `self.locals` array.
178+
unsafe { &self.locals.get_unchecked(self.registry.id().verify()).0 }
179+
}
180+
}

compiler/rustc_interface/src/interface.rs

+19
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use rustc_session::Session;
1919
use rustc_session::{early_error, CompilerIO};
2020
use rustc_span::source_map::{FileLoader, FileName};
2121
use rustc_span::symbol::sym;
22+
use std::cell::OnceCell;
2223
use std::path::PathBuf;
2324
use std::result;
2425

@@ -58,9 +59,25 @@ impl Compiler {
5859
}
5960
}
6061

62+
fn registry_setup() {
63+
thread_local! {
64+
static ONCE: OnceCell<()> = OnceCell::new();
65+
}
66+
67+
// Create a dummy registry to allow `WorkerLocal` construction.
68+
// We use `OnceCell` so we only register one dummy registry per thread.
69+
ONCE.with(|once| {
70+
once.get_or_init(|| {
71+
rustc_data_structures::sync::Registry::new(1).register();
72+
});
73+
});
74+
}
75+
6176
/// Converts strings provided as `--cfg [cfgspec]` into a `crate_cfg`.
6277
pub fn parse_cfgspecs(cfgspecs: Vec<String>) -> FxHashSet<(String, Option<String>)> {
6378
rustc_span::create_default_session_if_not_set_then(move |_| {
79+
registry_setup();
80+
6481
let cfg = cfgspecs
6582
.into_iter()
6683
.map(|s| {
@@ -120,6 +137,8 @@ pub fn parse_cfgspecs(cfgspecs: Vec<String>) -> FxHashSet<(String, Option<String
120137
/// Converts strings provided as `--check-cfg [specs]` into a `CheckCfg`.
121138
pub fn parse_check_cfg(specs: Vec<String>) -> CheckCfg {
122139
rustc_span::create_default_session_if_not_set_then(move |_| {
140+
registry_setup();
141+
123142
let mut cfg = CheckCfg::default();
124143

125144
'specs: for s in specs {

compiler/rustc_interface/src/util.rs

+6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ use libloading::Library;
44
use rustc_ast as ast;
55
use rustc_codegen_ssa::traits::CodegenBackend;
66
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
7+
#[cfg(parallel_compiler)]
8+
use rustc_data_structures::sync;
79
use rustc_errors::registry::Registry;
810
use rustc_parse::validate_attr;
911
use rustc_session as session;
@@ -170,6 +172,7 @@ pub(crate) fn run_in_thread_pool_with_globals<F: FnOnce() -> R + Send, R: Send>(
170172
use rustc_middle::ty::tls;
171173
use rustc_query_impl::{deadlock, QueryContext, QueryCtxt};
172174

175+
let registry = sync::Registry::new(threads);
173176
let mut builder = rayon::ThreadPoolBuilder::new()
174177
.thread_name(|_| "rustc".to_string())
175178
.acquire_thread_handler(jobserver::acquire_thread)
@@ -200,6 +203,9 @@ pub(crate) fn run_in_thread_pool_with_globals<F: FnOnce() -> R + Send, R: Send>(
200203
.build_scoped(
201204
// Initialize each new worker thread when created.
202205
move |thread: rayon::ThreadBuilder| {
206+
// Register the thread for use with the `WorkerLocal` type.
207+
registry.register();
208+
203209
rustc_span::set_session_globals_then(session_globals, || thread.run())
204210
},
205211
// Run `f` on the first thread in the thread pool.

0 commit comments

Comments
 (0)