Skip to content
/ rust Public
forked from rust-lang/rust

Commit c10150c

Browse files
authored
Rollup merge of rust-lang#136858 - safinaskar:parallel-cleanup-2025-02-11-07-54, r=SparrowLii
Parallel-compiler-related cleanup Parallel-compiler-related cleanup I carefully split changes into commits. Commit messages are self-explanatory. Squashing is not recommended. cc "Parallel Rustc Front-end" rust-lang#113349 r? SparrowLii `@rustbot` label: +WG-compiler-parallel
2 parents 13c1b80 + 8506dd2 commit c10150c

File tree

9 files changed

+17
-77
lines changed

9 files changed

+17
-77
lines changed

compiler/rustc_codegen_gcc/src/back/lto.rs

-4
Original file line numberDiff line numberDiff line change
@@ -710,10 +710,6 @@ pub struct ThinBuffer {
710710
context: Arc<SyncContext>,
711711
}
712712

713-
// TODO: check if this makes sense to make ThinBuffer Send and Sync.
714-
unsafe impl Send for ThinBuffer {}
715-
unsafe impl Sync for ThinBuffer {}
716-
717713
impl ThinBuffer {
718714
pub(crate) fn new(context: &Arc<SyncContext>) -> Self {
719715
Self { context: Arc::clone(context) }

compiler/rustc_codegen_llvm/src/lib.rs

-3
Original file line numberDiff line numberDiff line change
@@ -245,9 +245,6 @@ impl WriteBackendMethods for LlvmCodegenBackend {
245245
}
246246
}
247247

248-
unsafe impl Send for LlvmCodegenBackend {} // Llvm is on a per-thread basis
249-
unsafe impl Sync for LlvmCodegenBackend {}
250-
251248
impl LlvmCodegenBackend {
252249
pub fn new() -> Box<dyn CodegenBackend> {
253250
Box::new(LlvmCodegenBackend(()))

compiler/rustc_data_structures/src/owned_slice.rs

+5-10
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,6 @@ use std::borrow::Borrow;
22
use std::ops::Deref;
33
use std::sync::Arc;
44

5-
// Use our fake Send/Sync traits when on not parallel compiler,
6-
// so that `OwnedSlice` only implements/requires Send/Sync
7-
// for parallel compiler builds.
8-
use crate::sync;
9-
105
/// An owned slice.
116
///
127
/// This is similar to `Arc<[u8]>` but allows slicing and using anything as the
@@ -34,7 +29,7 @@ pub struct OwnedSlice {
3429
// \/
3530
// ⊂(´・◡・⊂ )∘˚˳° (I am the phantom remnant of #97770)
3631
#[expect(dead_code)]
37-
owner: Arc<dyn sync::Send + sync::Sync>,
32+
owner: Arc<dyn Send + Sync>,
3833
}
3934

4035
/// Makes an [`OwnedSlice`] out of an `owner` and a `slicer` function.
@@ -61,7 +56,7 @@ pub struct OwnedSlice {
6156
/// ```
6257
pub fn slice_owned<O, F>(owner: O, slicer: F) -> OwnedSlice
6358
where
64-
O: sync::Send + sync::Sync + 'static,
59+
O: Send + Sync + 'static,
6560
F: FnOnce(&O) -> &[u8],
6661
{
6762
try_slice_owned(owner, |x| Ok::<_, !>(slicer(x))).into_ok()
@@ -72,7 +67,7 @@ where
7267
/// See [`slice_owned`] for the infallible version.
7368
pub fn try_slice_owned<O, F, E>(owner: O, slicer: F) -> Result<OwnedSlice, E>
7469
where
75-
O: sync::Send + sync::Sync + 'static,
70+
O: Send + Sync + 'static,
7671
F: FnOnce(&O) -> Result<&[u8], E>,
7772
{
7873
// We wrap the owner of the bytes in, so it doesn't move.
@@ -139,10 +134,10 @@ impl Borrow<[u8]> for OwnedSlice {
139134
}
140135

141136
// Safety: `OwnedSlice` is conceptually `(&'self.1 [u8], Arc<dyn Send + Sync>)`, which is `Send`
142-
unsafe impl sync::Send for OwnedSlice {}
137+
unsafe impl Send for OwnedSlice {}
143138

144139
// Safety: `OwnedSlice` is conceptually `(&'self.1 [u8], Arc<dyn Send + Sync>)`, which is `Sync`
145-
unsafe impl sync::Sync for OwnedSlice {}
140+
unsafe impl Sync for OwnedSlice {}
146141

147142
#[cfg(test)]
148143
mod tests;

compiler/rustc_data_structures/src/sync.rs

+3-40
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,8 @@
1818
//!
1919
//! | Type | Serial version | Parallel version |
2020
//! | ----------------------- | ------------------- | ------------------------------- |
21-
//! |` Weak<T>` | `rc::Weak<T>` | `sync::Weak<T>` |
2221
//! | `LRef<'a, T>` [^2] | `&'a mut T` | `&'a T` |
2322
//! | | | |
24-
//! | `AtomicBool` | `Cell<bool>` | `atomic::AtomicBool` |
25-
//! | `AtomicU32` | `Cell<u32>` | `atomic::AtomicU32` |
26-
//! | `AtomicU64` | `Cell<u64>` | `atomic::AtomicU64` |
27-
//! | `AtomicUsize` | `Cell<usize>` | `atomic::AtomicUsize` |
28-
//! | | | |
2923
//! | `Lock<T>` | `RefCell<T>` | `RefCell<T>` or |
3024
//! | | | `parking_lot::Mutex<T>` |
3125
//! | `RwLock<T>` | `RefCell<T>` | `parking_lot::RwLock<T>` |
@@ -103,18 +97,15 @@ mod mode {
10397

10498
// FIXME(parallel_compiler): Get rid of these aliases across the compiler.
10599

106-
pub use std::marker::{Send, Sync};
100+
pub use std::sync::OnceLock;
107101
// Use portable AtomicU64 for targets without native 64-bit atomics
108102
#[cfg(target_has_atomic = "64")]
109103
pub use std::sync::atomic::AtomicU64;
110-
pub use std::sync::atomic::{AtomicBool, AtomicU32, AtomicUsize};
111-
pub use std::sync::{OnceLock, Weak};
112104

113105
pub use mode::{is_dyn_thread_safe, set_dyn_thread_safe_mode};
114106
pub use parking_lot::{
115-
MappedMutexGuard as MappedLockGuard, MappedRwLockReadGuard as MappedReadGuard,
116-
MappedRwLockWriteGuard as MappedWriteGuard, RwLockReadGuard as ReadGuard,
117-
RwLockWriteGuard as WriteGuard,
107+
MappedRwLockReadGuard as MappedReadGuard, MappedRwLockWriteGuard as MappedWriteGuard,
108+
RwLockReadGuard as ReadGuard, RwLockWriteGuard as WriteGuard,
118109
};
119110
#[cfg(not(target_has_atomic = "64"))]
120111
pub use portable_atomic::AtomicU64;
@@ -203,12 +194,6 @@ impl<T> RwLock<T> {
203194
}
204195
}
205196

206-
#[inline(always)]
207-
#[track_caller]
208-
pub fn with_read_lock<F: FnOnce(&T) -> R, R>(&self, f: F) -> R {
209-
f(&*self.read())
210-
}
211-
212197
#[inline(always)]
213198
pub fn try_write(&self) -> Result<WriteGuard<'_, T>, ()> {
214199
self.0.try_write().ok_or(())
@@ -223,12 +208,6 @@ impl<T> RwLock<T> {
223208
}
224209
}
225210

226-
#[inline(always)]
227-
#[track_caller]
228-
pub fn with_write_lock<F: FnOnce(&mut T) -> R, R>(&self, f: F) -> R {
229-
f(&mut *self.write())
230-
}
231-
232211
#[inline(always)]
233212
#[track_caller]
234213
pub fn borrow(&self) -> ReadGuard<'_, T> {
@@ -240,20 +219,4 @@ impl<T> RwLock<T> {
240219
pub fn borrow_mut(&self) -> WriteGuard<'_, T> {
241220
self.write()
242221
}
243-
244-
#[inline(always)]
245-
pub fn leak(&self) -> &T {
246-
let guard = self.read();
247-
let ret = unsafe { &*(&raw const *guard) };
248-
std::mem::forget(guard);
249-
ret
250-
}
251-
}
252-
253-
// FIXME: Probably a bad idea
254-
impl<T: Clone> Clone for RwLock<T> {
255-
#[inline]
256-
fn clone(&self) -> Self {
257-
RwLock::new(self.borrow().clone())
258-
}
259222
}

compiler/rustc_data_structures/src/sync/freeze.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ use std::intrinsics::likely;
33
use std::marker::PhantomData;
44
use std::ops::{Deref, DerefMut};
55
use std::ptr::NonNull;
6-
use std::sync::atomic::Ordering;
6+
use std::sync::atomic::{AtomicBool, Ordering};
77

8-
use crate::sync::{AtomicBool, DynSend, DynSync, ReadGuard, RwLock, WriteGuard};
8+
use crate::sync::{DynSend, DynSync, ReadGuard, RwLock, WriteGuard};
99

1010
/// A type which allows mutation using a lock until
1111
/// the value is frozen and can be accessed lock-free.

compiler/rustc_data_structures/src/sync/worker_local.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,6 @@ pub struct WorkerLocal<T> {
106106
registry: Registry,
107107
}
108108

109-
// This is safe because the `deref` call will return a reference to a `T` unique to each thread
110-
// or it will panic for threads without an associated local. So there isn't a need for `T` to do
111-
// it's own synchronization. The `verify` method on `RegistryId` has an issue where the id
112-
// can be reused, but `WorkerLocal` has a reference to `Registry` which will prevent any reuse.
113-
unsafe impl<T: Send> Sync for WorkerLocal<T> {}
114-
115109
impl<T> WorkerLocal<T> {
116110
/// Creates a new worker local where the `initial` closure computes the
117111
/// value this worker local should take for each thread in the registry.
@@ -138,6 +132,11 @@ impl<T> Deref for WorkerLocal<T> {
138132
fn deref(&self) -> &T {
139133
// This is safe because `verify` will only return values less than
140134
// `self.registry.thread_limit` which is the size of the `self.locals` array.
135+
136+
// The `deref` call will return a reference to a `T` unique to each thread
137+
// or it will panic for threads without an associated local. So there isn't a need for `T` to do
138+
// it's own synchronization. The `verify` method on `RegistryId` has an issue where the id
139+
// can be reused, but `WorkerLocal` has a reference to `Registry` which will prevent any reuse.
141140
unsafe { &self.locals.get_unchecked(self.registry.id().verify()).0 }
142141
}
143142
}

compiler/rustc_query_system/src/dep_graph/graph.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ use std::fmt::Debug;
44
use std::hash::Hash;
55
use std::marker::PhantomData;
66
use std::sync::Arc;
7-
use std::sync::atomic::Ordering;
7+
use std::sync::atomic::{AtomicU32, Ordering};
88

99
use rustc_data_structures::fingerprint::Fingerprint;
1010
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
1111
use rustc_data_structures::profiling::{QueryInvocationId, SelfProfilerRef};
1212
use rustc_data_structures::sharded::{self, Sharded};
1313
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
14-
use rustc_data_structures::sync::{AtomicU32, AtomicU64, Lock};
14+
use rustc_data_structures::sync::{AtomicU64, Lock};
1515
use rustc_data_structures::unord::UnordMap;
1616
use rustc_index::IndexVec;
1717
use rustc_macros::{Decodable, Encodable};

config.example.toml

-5
Original file line numberDiff line numberDiff line change
@@ -595,11 +595,6 @@
595595
# Whether to always use incremental compilation when building rustc
596596
#incremental = false
597597

598-
# Build a multi-threaded rustc. This allows users to use parallel rustc
599-
# via the unstable option `-Z threads=n`.
600-
# This option is deprecated and always true.
601-
#parallel-compiler = true
602-
603598
# The default linker that will be hard-coded into the generated
604599
# compiler for targets that don't specify a default linker explicitly
605600
# in their target specifications. Note that this is not the linker

src/doc/rustc-dev-guide/src/parallel-rustc.md

-5
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,6 @@ are implemented differently depending on whether `parallel-compiler` is true.
4646

4747
| data structure | parallel | non-parallel |
4848
| -------------------------------- | --------------------------------------------------- | ------------ |
49-
| Weak | std::sync::Weak | std::rc::Weak |
50-
| Atomic{Bool}/{Usize}/{U32}/{U64} | std::sync::atomic::Atomic{Bool}/{Usize}/{U32}/{U64} | (std::cell::Cell<bool/usize/u32/u64>) |
5149
| OnceCell | std::sync::OnceLock | std::cell::OnceCell |
5250
| Lock\<T> | (parking_lot::Mutex\<T>) | (std::cell::RefCell) |
5351
| RwLock\<T> | (parking_lot::RwLock\<T>) | (std::cell::RefCell) |
@@ -58,7 +56,6 @@ are implemented differently depending on whether `parallel-compiler` is true.
5856
| WriteGuard | parking_lot::RwLockWriteGuard | std::cell::RefMut |
5957
| MappedWriteGuard | parking_lot::MappedRwLockWriteGuard | std::cell::RefMut |
6058
| LockGuard | parking_lot::MutexGuard | std::cell::RefMut |
61-
| MappedLockGuard | parking_lot::MappedMutexGuard | std::cell::RefMut |
6259

6360
- These thread-safe data structures are interspersed during compilation which
6461
can cause lock contention resulting in degraded performance as the number of
@@ -173,12 +170,10 @@ Here are some resources that can be used to learn more:
173170
- [This list of interior mutability in the compiler by nikomatsakis][imlist]
174171

175172
[`rayon`]: https://crates.io/crates/rayon
176-
[Arc]: https://doc.rust-lang.org/std/sync/struct.Arc.html
177173
[imlist]: https://github.com/nikomatsakis/rustc-parallelization/blob/master/interior-mutability-list.md
178174
[irlo0]: https://internals.rust-lang.org/t/parallelizing-rustc-using-rayon/6606
179175
[irlo1]: https://internals.rust-lang.org/t/help-test-parallel-rustc/11503
180176
[monomorphization]: backend/monomorph.md
181177
[parallel-rustdoc]: https://github.com/rust-lang/rust/issues/82741
182-
[Rc]: https://doc.rust-lang.org/std/rc/struct.Rc.html
183178
[rustc-rayon]: https://github.com/rust-lang/rustc-rayon
184179
[tracking]: https://github.com/rust-lang/rust/issues/48685

0 commit comments

Comments
 (0)