Skip to content

Commit fd51e35

Browse files
committed
rebase and set default threads to 8
1 parent 0796d1b commit fd51e35

File tree

19 files changed

+189
-259
lines changed

19 files changed

+189
-259
lines changed

compiler/rustc_codegen_ssa/src/base.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,7 @@ pub fn codegen_crate<B: ExtraBackendMethods>(
686686
// This likely is a temporary measure. Once we don't have to support the
687687
// non-parallel compiler anymore, we can compile CGUs end-to-end in
688688
// parallel and get rid of the complicated scheduling logic.
689-
let mut pre_compiled_cgus = if rustc_data_structures::sync::active() {
689+
let mut pre_compiled_cgus = if rustc_data_structures::sync::is_dyn_thread_safe() {
690690
tcx.sess.time("compile_first_CGU_batch", || {
691691
// Try to find one CGU to compile per thread.
692692
let cgus: Vec<_> = cgu_reuse

compiler/rustc_data_structures/src/sharded.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::fx::{FxHashMap, FxHasher};
2-
use crate::sync::LockLike;
2+
use crate::sync::{DynSync, LockLike};
33
use parking_lot::{Mutex, MutexGuard};
44
use std::borrow::Borrow;
55
use std::cell::{RefCell, RefMut};
@@ -131,13 +131,13 @@ pub struct DynSharded<T> {
131131
parallel_shard: ShardedImpl<T>,
132132
}
133133

134-
// just for speed test
135-
unsafe impl<T> Sync for DynSharded<T> {}
134+
#[cfg(parallel_compiler)]
135+
unsafe impl<T> DynSync for DynSharded<T> {}
136136

137137
impl<T: Default> Default for DynSharded<T> {
138138
#[inline]
139139
fn default() -> Self {
140-
let single_thread = !crate::sync::active();
140+
let single_thread = !crate::sync::is_dyn_thread_safe();
141141
DynSharded {
142142
single_thread,
143143
single_shard: RefCell::new(T::default()),
@@ -148,7 +148,7 @@ impl<T: Default> Default for DynSharded<T> {
148148

149149
impl<T: Default> DynSharded<T> {
150150
pub fn new(mut value: impl FnMut() -> T) -> Self {
151-
if !crate::sync::active() {
151+
if !crate::sync::is_dyn_thread_safe() {
152152
DynSharded {
153153
single_thread: true,
154154
single_shard: RefCell::new(value()),

compiler/rustc_data_structures/src/sync.rs

+22-91
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,10 @@ mod mode {
6565
use super::Ordering;
6666
use std::sync::atomic::AtomicU8;
6767

68-
const UNINITIALIZED: u8 = 0;
6968
const DYN_NOT_THREAD_SAFE: u8 = 1;
7069
const DYN_THREAD_SAFE: u8 = 2;
7170

72-
static DYN_THREAD_SAFE_MODE: AtomicU8 = AtomicU8::new(UNINITIALIZED);
71+
static DYN_THREAD_SAFE_MODE: AtomicU8 = AtomicU8::new(DYN_NOT_THREAD_SAFE);
7372

7473
// Whether thread safety is enabled (due to running under multiple threads).
7574
#[inline]
@@ -84,15 +83,9 @@ mod mode {
8483
// Only set by the `-Z threads` compile option
8584
pub fn set_dyn_thread_safe_mode(mode: bool) {
8685
let set: u8 = if mode { DYN_THREAD_SAFE } else { DYN_NOT_THREAD_SAFE };
87-
let previous = DYN_THREAD_SAFE_MODE.compare_exchange(
88-
UNINITIALIZED,
89-
set,
90-
Ordering::Relaxed,
91-
Ordering::Relaxed,
92-
);
9386

94-
// Check that the mode was either uninitialized or was already set to the requested mode.
95-
assert!(previous.is_ok() || previous == Err(set));
87+
// just for speed test
88+
DYN_THREAD_SAFE_MODE.store(set, Ordering::Relaxed);
9689
}
9790
}
9891

@@ -337,24 +330,6 @@ cfg_if! {
337330
}
338331
}
339332

340-
#[inline]
341-
pub fn join<A, B, RA: DynSend, RB: DynSend>(oper_a: A, oper_b: B) -> (RA, RB)
342-
where
343-
A: FnOnce() -> RA + DynSend,
344-
B: FnOnce() -> RB + DynSend,
345-
{
346-
if mode::active() {
347-
let oper_a = FromDyn::from(oper_a);
348-
let oper_b = FromDyn::from(oper_b);
349-
let (a, b) = rayon::join(move || FromDyn::from(oper_a.into_inner()()), move || FromDyn::from(oper_b.into_inner()()));
350-
(a.into_inner(), b.into_inner())
351-
} else {
352-
(oper_a(), oper_b())
353-
}
354-
}
355-
356-
use std::thread;
357-
358333
#[inline]
359334
pub fn join<A, B, RA: DynSend, RB: DynSend>(oper_a: A, oper_b: B) -> (RA, RB)
360335
where
@@ -385,19 +360,11 @@ cfg_if! {
385360
/// the current thread. Use that for the longest running block.
386361
#[macro_export]
387362
macro_rules! parallel {
388-
(impl $fblock:tt [$($c:tt,)*] [$block:tt $(, $rest:tt)*]) => {
389363
(impl $fblock:block [$($c:expr,)*] [$block:expr $(, $rest:expr)*]) => {
390364
parallel!(impl $fblock [$block, $($c,)*] [$($rest),*])
391365
};
392-
(impl $fblock:tt [$($blocks:tt,)*] []) => {
393366
(impl $fblock:block [$($blocks:expr,)*] []) => {
394367
::rustc_data_structures::sync::scope(|s| {
395-
396-
397-
398-
399-
400-
401368
$(let block = rustc_data_structures::sync::FromDyn::from(|| $blocks);
402369
s.spawn(move |_| block.into_inner()());)*
403370
(|| $fblock)();
@@ -421,7 +388,6 @@ cfg_if! {
421388
}
422389
}
423390
$(
424-
s.spawn(|_| $blocks);
425391
if let Err(p) = ::std::panic::catch_unwind(
426392
::std::panic::AssertUnwindSafe(|| $blocks)
427393
) {
@@ -430,19 +396,11 @@ cfg_if! {
430396
}
431397
}
432398
)*
433-
$fblock;
434-
})
435399
if let Some(panic) = panic {
436400
::std::panic::resume_unwind(panic);
437401
}
438402
}
439403
};
440-
($fblock:tt, $($blocks:tt),*) => {
441-
// Reverse the order of the later blocks since Rayon executes them in reverse order
442-
// when using a single thread. This ensures the execution order matches that
443-
// of a single threaded rustc
444-
parallel!(impl $fblock [] [$($blocks),*]);
445-
};
446404
}
447405

448406
use rayon::iter::{FromParallelIterator, IntoParallelIterator, ParallelIterator};
@@ -453,7 +411,7 @@ cfg_if! {
453411
) {
454412
if mode::is_dyn_thread_safe() {
455413
let for_each = FromDyn::from(for_each);
456-
let panic: Lock<Option<_>> = Lock::new(None);
414+
let panic: Mutex<Option<_>> = Mutex::new(None);
457415
t.into_par_iter().for_each(|i| if let Err(p) = catch_unwind(AssertUnwindSafe(|| for_each(i))) {
458416
let mut l = panic.lock();
459417
if l.is_none() {
@@ -491,7 +449,7 @@ cfg_if! {
491449
map: impl Fn(I) -> R + DynSync + DynSend
492450
) -> C {
493451
if mode::is_dyn_thread_safe() {
494-
let panic: Lock<Option<_>> = Lock::new(None);
452+
let panic: Mutex<Option<_>> = Mutex::new(None);
495453
let map = FromDyn::from(map);
496454
// We catch panics here ensuring that all the loop iterations execute.
497455
let r = t.into_par_iter().filter_map(|i| {
@@ -531,30 +489,6 @@ cfg_if! {
531489
r
532490
}
533491
}
534-
535-
pub unsafe trait DynSend {}
536-
pub unsafe trait DynSync {}
537-
538-
unsafe impl<T> DynSend for T where T: Send {}
539-
unsafe impl<T> DynSync for T where T: Sync {}
540-
541-
#[derive(Copy, Clone)]
542-
pub struct FromDyn<T>(T);
543-
544-
impl<T> FromDyn<T> {
545-
#[inline(always)]
546-
pub fn from(val: T) -> Self {
547-
// Check that `sync::active()` is true on creation so we can
548-
// implement `Send` and `Sync` for this structure when `T`
549-
// implements `DynSend` and `DynSync` respectively.
550-
#[cfg(parallel_compiler)]
551-
assert!(mode::active());
552-
FromDyn(val)
553-
}
554-
555-
#[inline(always)]
556-
pub fn into_inner(self) -> T {
557-
self.0
558492
}
559493
}
560494

@@ -607,7 +541,7 @@ impl<T> Lock<T> {
607541
#[inline]
608542
pub fn new(val: T) -> Self {
609543
Lock {
610-
single_thread: !active(),
544+
single_thread: !is_dyn_thread_safe(),
611545
data: UnsafeCell::new(val),
612546
borrow: Cell::new(false),
613547
mutex: RawMutex::INIT,
@@ -725,10 +659,6 @@ impl<T: Default> Default for Lock<T> {
725659
}
726660
}
727661

728-
// Just for speed test
729-
unsafe impl<T: Send> std::marker::Send for Lock<T> {}
730-
unsafe impl<T: Send> std::marker::Sync for Lock<T> {}
731-
732662
pub struct LockGuard<'a, T> {
733663
lock: &'a Lock<T>,
734664
marker: PhantomData<&'a mut T>,
@@ -1037,6 +967,10 @@ pub struct RwLock<T> {
1037967
data: UnsafeCell<T>,
1038968
}
1039969

970+
// just for speed test
971+
unsafe impl<T> std::marker::Send for RwLock<T> {}
972+
unsafe impl<T> std::marker::Sync for RwLock<T> {}
973+
1040974
impl<T: Debug> Debug for RwLock<T> {
1041975
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
1042976
f.debug_struct("Lock").field("data", self.read().deref()).finish()
@@ -1046,7 +980,11 @@ impl<T: Debug> Debug for RwLock<T> {
1046980
impl<T: Default> Default for RwLock<T> {
1047981
fn default() -> Self {
1048982
RwLock {
1049-
raw: RwLockRaw { single_thread: !active(), borrow: Cell::new(0), raw: RawRwLock::INIT },
983+
raw: RwLockRaw {
984+
single_thread: !is_dyn_thread_safe(),
985+
borrow: Cell::new(0),
986+
raw: RawRwLock::INIT,
987+
},
1050988

1051989
data: UnsafeCell::new(T::default()),
1052990
}
@@ -1057,7 +995,11 @@ impl<T> RwLock<T> {
1057995
#[inline(always)]
1058996
pub fn new(inner: T) -> Self {
1059997
RwLock {
1060-
raw: RwLockRaw { single_thread: !active(), borrow: Cell::new(0), raw: RawRwLock::INIT },
998+
raw: RwLockRaw {
999+
single_thread: !is_dyn_thread_safe(),
1000+
borrow: Cell::new(0),
1001+
raw: RawRwLock::INIT,
1002+
},
10611003

10621004
data: UnsafeCell::new(inner),
10631005
}
@@ -1197,10 +1139,6 @@ impl<T> RwLock<T> {
11971139
}
11981140
}
11991141

1200-
// just for speed test
1201-
unsafe impl<T: Send> std::marker::Send for RwLock<T> {}
1202-
unsafe impl<T: Send + Sync> std::marker::Sync for RwLock<T> {}
1203-
12041142
// FIXME: Probably a bad idea
12051143
impl<T: Clone> Clone for RwLock<T> {
12061144
#[inline]
@@ -1221,7 +1159,7 @@ impl<T> WorkerLocal<T> {
12211159
/// value this worker local should take for each thread in the thread pool.
12221160
#[inline]
12231161
pub fn new<F: FnMut(usize) -> T>(mut f: F) -> WorkerLocal<T> {
1224-
if !active() {
1162+
if !is_dyn_thread_safe() {
12251163
WorkerLocal { single_thread: true, inner: Some(f(0)), mt_inner: None }
12261164
} else {
12271165
WorkerLocal {
@@ -1246,9 +1184,6 @@ impl<T> Deref for WorkerLocal<T> {
12461184
}
12471185
}
12481186

1249-
// Just for speed test
1250-
unsafe impl<T: Send> std::marker::Sync for WorkerLocal<T> {}
1251-
12521187
use std::thread;
12531188
pub use worker_local::Registry;
12541189

@@ -1261,10 +1196,6 @@ pub struct OneThread<T> {
12611196
inner: T,
12621197
}
12631198

1264-
// just for speed test now
1265-
unsafe impl<T> std::marker::Sync for OneThread<T> {}
1266-
unsafe impl<T> std::marker::Send for OneThread<T> {}
1267-
12681199
impl<T> OneThread<T> {
12691200
#[inline(always)]
12701201
fn check(&self) {
@@ -1273,7 +1204,7 @@ impl<T> OneThread<T> {
12731204

12741205
#[inline(always)]
12751206
pub fn new(inner: T) -> Self {
1276-
OneThread { single_thread: !active(), thread: thread::current().id(), inner }
1207+
OneThread { single_thread: !is_dyn_thread_safe(), thread: thread::current().id(), inner }
12771208
}
12781209

12791210
#[inline(always)]

compiler/rustc_data_structures/src/sync/worker_local.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::sync::Lock;
1+
use parking_lot::Mutex;
22
use std::cell::Cell;
33
use std::cell::OnceCell;
44
use std::ops::Deref;
@@ -34,7 +34,7 @@ impl RegistryId {
3434
#[derive(Debug)]
3535
struct RegistryData {
3636
thread_limit: usize,
37-
threads: Lock<usize>,
37+
threads: Mutex<usize>,
3838
}
3939

4040
/// Represents a list of threads which can access worker locals.
@@ -64,7 +64,7 @@ thread_local! {
6464
impl Registry {
6565
/// Creates a registry which can hold up to `thread_limit` threads.
6666
pub fn new(thread_limit: usize) -> Self {
67-
Registry(Arc::new(RegistryData { thread_limit, threads: Lock::new(0) }))
67+
Registry(Arc::new(RegistryData { thread_limit, threads: Mutex::new(0) }))
6868
}
6969

7070
/// Gets the registry associated with the current thread. Panics if there's no such registry.

compiler/rustc_interface/src/interface.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ pub fn set_parallel_mode(sopts1: &config::UnstableOptions, sopts2: &config::Code
7272
}
7373
};
7474

75-
rustc_data_structures::sync::set(parallel);
75+
rustc_data_structures::sync::set_dyn_thread_safe_mode(parallel);
7676
}
7777

7878
/// Converts strings provided as `--cfg [cfgspec]` into a `crate_cfg`.

compiler/rustc_interface/src/util.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use rustc_codegen_ssa::traits::CodegenBackend;
66
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
77
#[cfg(parallel_compiler)]
88
use rustc_data_structures::sync;
9+
use rustc_data_structures::sync::FromDyn;
910
use rustc_errors::registry::Registry;
1011
use rustc_parse::validate_attr;
1112
use rustc_session as session;
@@ -134,7 +135,7 @@ pub(crate) fn run_in_thread_pool_with_globals<F: FnOnce() -> R + Send, R: Send>(
134135
f: F,
135136
) -> R {
136137
#[cfg(parallel_compiler)]
137-
if rustc_data_structures::sync::active() {
138+
if rustc_data_structures::sync::is_dyn_thread_safe() {
138139
return run_in_threads_pool_with_globals(edition, _threads, f);
139140
}
140141
// The "thread pool" is a single spawned thread in the non-parallel
@@ -203,14 +204,17 @@ pub(crate) fn run_in_threads_pool_with_globals<F: FnOnce() -> R + Send, R: Send>
203204
// `Send` in the parallel compiler.
204205
rustc_span::create_session_globals_then(edition, || {
205206
rustc_span::with_session_globals(|session_globals| {
207+
let session_globals = FromDyn::from(session_globals);
206208
builder
207209
.build_scoped(
208210
// Initialize each new worker thread when created.
209211
move |thread: rayon::ThreadBuilder| {
210212
// Register the thread for use with the `WorkerLocal` type.
211213
registry.register();
212214

213-
rustc_span::set_session_globals_then(session_globals, || thread.run())
215+
rustc_span::set_session_globals_then(session_globals.into_inner(), || {
216+
thread.run()
217+
})
214218
},
215219
// Run `f` on the first thread in the thread pool.
216220
move |pool: &rayon::ThreadPool| pool.install(f),

compiler/rustc_metadata/src/rmeta/encoder.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2233,7 +2233,7 @@ pub fn encode_metadata(tcx: TyCtxt<'_>, path: &Path) {
22332233
join(
22342234
|| encode_metadata_impl(tcx, path),
22352235
|| {
2236-
if !rustc_data_structures::sync::active() {
2236+
if !rustc_data_structures::sync::is_dyn_thread_safe() {
22372237
return;
22382238
}
22392239
// Prefetch some queries used by metadata encoding.

compiler/rustc_middle/src/query/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ use rustc_ast::expand::allocator::AllocatorKind;
5454
use rustc_attr as attr;
5555
use rustc_data_structures::fingerprint::Fingerprint;
5656
use rustc_data_structures::fx::{FxHashMap, FxIndexMap, FxIndexSet};
57+
use rustc_data_structures::sharded::{Shard, Sharded, SingleShard};
5758
use rustc_data_structures::steal::Steal;
5859
use rustc_data_structures::svh::Svh;
5960
use rustc_data_structures::sync::Lrc;
@@ -80,6 +81,7 @@ use rustc_span::symbol::Symbol;
8081
use rustc_span::{Span, DUMMY_SP};
8182
use rustc_target::abi;
8283
use rustc_target::spec::PanicStrategy;
84+
use std::intrinsics::likely;
8385
use std::mem;
8486
use std::ops::Deref;
8587
use std::path::PathBuf;

0 commit comments

Comments
 (0)