From d6c63ec949c09e30f89f4bc63b42f7822c197b2b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20K=C3=A5re=20Alsaker?= Date: Fri, 20 Apr 2018 23:50:10 +0200 Subject: [PATCH 1/9] Fix OneThread --- src/librustc_data_structures/sync.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/librustc_data_structures/sync.rs b/src/librustc_data_structures/sync.rs index 3661763133014..9cceee65bf9a2 100644 --- a/src/librustc_data_structures/sync.rs +++ b/src/librustc_data_structures/sync.rs @@ -36,7 +36,6 @@ use std::marker::PhantomData; use std::fmt::Debug; use std::fmt::Formatter; use std::fmt; -use std; use std::ops::{Deref, DerefMut}; use owning_ref::{Erased, OwningRef}; @@ -200,6 +199,7 @@ cfg_if! { use parking_lot::Mutex as InnerLock; use parking_lot::RwLock as InnerRwLock; + use std; use std::thread; pub use rayon::{join, scope}; @@ -638,7 +638,9 @@ pub struct OneThread { inner: T, } +#[cfg(parallel_queries)] unsafe impl std::marker::Sync for OneThread {} +#[cfg(parallel_queries)] unsafe impl std::marker::Send for OneThread {} impl OneThread { From 9eb4f7329b130cca7a8850a93534197aa6af3657 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20K=C3=A5re=20Alsaker?= Date: Tue, 17 Apr 2018 20:09:40 +0200 Subject: [PATCH 2/9] Fix optimization_fuel --- src/librustc/session/mod.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/librustc/session/mod.rs b/src/librustc/session/mod.rs index 8df66d8d68855..076d56fb80842 100644 --- a/src/librustc/session/mod.rs +++ b/src/librustc/session/mod.rs @@ -845,10 +845,10 @@ impl Session { /// We want to know if we're allowed to do an optimization for crate foo from -z fuel=foo=n. /// This expends fuel if applicable, and records fuel if applicable. pub fn consider_optimizing String>(&self, crate_name: &str, msg: T) -> bool { - assert!(self.query_threads() == 1); let mut ret = true; match self.optimization_fuel_crate { Some(ref c) if c == crate_name => { + assert!(self.query_threads() == 1); let fuel = self.optimization_fuel_limit.get(); ret = fuel != 0; if fuel == 0 && !self.out_of_fuel.get() { @@ -862,6 +862,7 @@ impl Session { } match self.print_fuel_crate { Some(ref c) if c == crate_name => { + assert!(self.query_threads() == 1); self.print_fuel.set(self.print_fuel.get() + 1); } _ => {} From 85acb1d04610de6555ebabeaff1012d842d25d1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20K=C3=A5re=20Alsaker?= Date: Thu, 26 Apr 2018 01:03:54 +0200 Subject: [PATCH 3/9] Ensure ImplicitCtxt is Sync --- src/librustc/ty/context.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/librustc/ty/context.rs b/src/librustc/ty/context.rs index 68f55b4993301..0facbc6922aff 100644 --- a/src/librustc/ty/context.rs +++ b/src/librustc/ty/context.rs @@ -1704,7 +1704,7 @@ pub mod tls { use ty::maps; use errors::{Diagnostic, TRACK_DIAGNOSTICS}; use rustc_data_structures::OnDrop; - use rustc_data_structures::sync::Lrc; + use rustc_data_structures::sync::{self, Lrc}; use dep_graph::OpenTask; /// This is the implicit state of rustc. It contains the current @@ -1832,6 +1832,10 @@ pub mod tls { if context == 0 { f(None) } else { + // We could get a ImplicitCtxt pointer from another thread. + // Ensure that ImplicitCtxt is Sync + sync::assert_sync::(); + unsafe { f(Some(&*(context as *const ImplicitCtxt))) } } } From b7aabaa3fccc2b288841f29318253f2839da8a88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20K=C3=A5re=20Alsaker?= Date: Sat, 3 Mar 2018 06:22:19 +0100 Subject: [PATCH 4/9] Update recursion limits --- src/libproc_macro/lib.rs | 2 ++ src/librustc_codegen_utils/lib.rs | 2 ++ src/librustc_metadata/lib.rs | 2 ++ src/librustc_privacy/lib.rs | 2 ++ src/librustc_save_analysis/lib.rs | 2 ++ src/librustc_traits/lib.rs | 2 ++ 6 files changed, 12 insertions(+) diff --git a/src/libproc_macro/lib.rs b/src/libproc_macro/lib.rs index 9d037cefceed6..befc1ba064aa2 100644 --- a/src/libproc_macro/lib.rs +++ b/src/libproc_macro/lib.rs @@ -36,6 +36,8 @@ #![feature(lang_items)] #![feature(optin_builtin_traits)] +#![recursion_limit="256"] + extern crate syntax; extern crate syntax_pos; extern crate rustc_errors; diff --git a/src/librustc_codegen_utils/lib.rs b/src/librustc_codegen_utils/lib.rs index 0c18571f4ffe8..d09e8f4845e5e 100644 --- a/src/librustc_codegen_utils/lib.rs +++ b/src/librustc_codegen_utils/lib.rs @@ -23,6 +23,8 @@ #![feature(quote)] #![feature(rustc_diagnostic_macros)] +#![recursion_limit="256"] + extern crate ar; extern crate flate2; #[macro_use] diff --git a/src/librustc_metadata/lib.rs b/src/librustc_metadata/lib.rs index 7ecf2eba43ddf..d76ca5bdf2710 100644 --- a/src/librustc_metadata/lib.rs +++ b/src/librustc_metadata/lib.rs @@ -23,6 +23,8 @@ #![feature(specialization)] #![feature(rustc_private)] +#![recursion_limit="256"] + extern crate libc; #[macro_use] extern crate log; diff --git a/src/librustc_privacy/lib.rs b/src/librustc_privacy/lib.rs index bfb8c282d377b..f32f6eda8ff59 100644 --- a/src/librustc_privacy/lib.rs +++ b/src/librustc_privacy/lib.rs @@ -14,6 +14,8 @@ #![feature(rustc_diagnostic_macros)] +#![recursion_limit="256"] + #[macro_use] extern crate rustc; #[macro_use] extern crate syntax; extern crate rustc_typeck; diff --git a/src/librustc_save_analysis/lib.rs b/src/librustc_save_analysis/lib.rs index e57a793ff426f..64dcd3e51751c 100644 --- a/src/librustc_save_analysis/lib.rs +++ b/src/librustc_save_analysis/lib.rs @@ -15,6 +15,8 @@ #![cfg_attr(stage0, feature(macro_lifetime_matcher))] #![allow(unused_attributes)] +#![recursion_limit="256"] + #[macro_use] extern crate rustc; diff --git a/src/librustc_traits/lib.rs b/src/librustc_traits/lib.rs index 733d8e1708ec0..7fa69cb98338d 100644 --- a/src/librustc_traits/lib.rs +++ b/src/librustc_traits/lib.rs @@ -17,6 +17,8 @@ #![feature(iterator_find_map)] #![feature(in_band_lifetimes)] +#![recursion_limit="256"] + extern crate chalk_engine; #[macro_use] extern crate log; From 969296b79b22363c67b1e6786bd115ced881210f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20K=C3=A5re=20Alsaker?= Date: Fri, 11 May 2018 16:28:28 +0200 Subject: [PATCH 5/9] Add a WorkerLocal abstraction --- src/librustc_data_structures/Cargo.toml | 1 + src/librustc_data_structures/lib.rs | 1 + src/librustc_data_structures/sync.rs | 29 +++++++++++++++++++++++++ 3 files changed, 31 insertions(+) diff --git a/src/librustc_data_structures/Cargo.toml b/src/librustc_data_structures/Cargo.toml index 15956829ff9a5..17ee771e52940 100644 --- a/src/librustc_data_structures/Cargo.toml +++ b/src/librustc_data_structures/Cargo.toml @@ -17,6 +17,7 @@ cfg-if = "0.1.2" stable_deref_trait = "1.0.0" parking_lot_core = "0.2.8" rustc-rayon = "0.1.0" +rustc-rayon-core = "0.1.0" rustc-hash = "1.0.1" [dependencies.parking_lot] diff --git a/src/librustc_data_structures/lib.rs b/src/librustc_data_structures/lib.rs index 23a920739b964..7046a2a2a493d 100644 --- a/src/librustc_data_structures/lib.rs +++ b/src/librustc_data_structures/lib.rs @@ -44,6 +44,7 @@ extern crate parking_lot; extern crate cfg_if; extern crate stable_deref_trait; extern crate rustc_rayon as rayon; +extern crate rustc_rayon_core as rayon_core; extern crate rustc_hash; // See librustc_cratesio_shim/Cargo.toml for a comment explaining this. diff --git a/src/librustc_data_structures/sync.rs b/src/librustc_data_structures/sync.rs index 9cceee65bf9a2..6f7d9e1b54b1e 100644 --- a/src/librustc_data_structures/sync.rs +++ b/src/librustc_data_structures/sync.rs @@ -99,6 +99,33 @@ cfg_if! { use std::cell::Cell; + #[derive(Debug)] + pub struct WorkerLocal(OneThread); + + impl WorkerLocal { + /// Creates a new worker local where the `initial` closure computes the + /// value this worker local should take for each thread in the thread pool. + #[inline] + pub fn new T>(mut f: F) -> WorkerLocal { + WorkerLocal(OneThread::new(f(0))) + } + + /// Returns the worker-local value for each thread + #[inline] + pub fn into_inner(self) -> Vec { + vec![OneThread::into_inner(self.0)] + } + } + + impl Deref for WorkerLocal { + type Target = T; + + #[inline(always)] + fn deref(&self) -> &T { + &*self.0 + } + } + #[derive(Debug)] pub struct MTLock(T); @@ -203,6 +230,8 @@ cfg_if! { use std::thread; pub use rayon::{join, scope}; + pub use rayon_core::WorkerLocal; + pub use rayon::iter::ParallelIterator; use rayon::iter::IntoParallelIterator; From d402d2d0d4c0f8cbccf52c119666b143adcc35b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20K=C3=A5re=20Alsaker?= Date: Mon, 22 Jan 2018 14:31:23 +0100 Subject: [PATCH 6/9] Have worker-local GlobalArenas --- src/librustc/ty/context.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/librustc/ty/context.rs b/src/librustc/ty/context.rs index 0facbc6922aff..27cc9514e65bf 100644 --- a/src/librustc/ty/context.rs +++ b/src/librustc/ty/context.rs @@ -58,7 +58,7 @@ use rustc_data_structures::stable_hasher::{HashStable, hash_stable_hashmap, StableVec}; use arena::{TypedArena, SyncDroplessArena}; use rustc_data_structures::indexed_vec::IndexVec; -use rustc_data_structures::sync::{Lrc, Lock}; +use rustc_data_structures::sync::{Lrc, Lock, WorkerLocal}; use std::any::Any; use std::borrow::Borrow; use std::cmp::Ordering; @@ -80,14 +80,14 @@ use syntax_pos::Span; use hir; pub struct AllArenas<'tcx> { - pub global: GlobalArenas<'tcx>, + pub global: WorkerLocal>, pub interner: SyncDroplessArena, } impl<'tcx> AllArenas<'tcx> { pub fn new() -> Self { AllArenas { - global: GlobalArenas::new(), + global: WorkerLocal::new(|_| GlobalArenas::new()), interner: SyncDroplessArena::new(), } } @@ -854,7 +854,7 @@ impl<'a, 'gcx, 'tcx> Deref for TyCtxt<'a, 'gcx, 'tcx> { } pub struct GlobalCtxt<'tcx> { - global_arenas: &'tcx GlobalArenas<'tcx>, + global_arenas: &'tcx WorkerLocal>, global_interners: CtxtInterners<'tcx>, cstore: &'tcx CrateStoreDyn, From c6dbb14097c46d1438058ffd9d7838cb906f19fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20K=C3=A5re=20Alsaker?= Date: Fri, 20 Apr 2018 12:47:19 +0200 Subject: [PATCH 7/9] Assert that GlobalCtxt is Sync --- src/librustc/ty/context.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/librustc/ty/context.rs b/src/librustc/ty/context.rs index 27cc9514e65bf..35b2ce50da79d 100644 --- a/src/librustc/ty/context.rs +++ b/src/librustc/ty/context.rs @@ -58,7 +58,7 @@ use rustc_data_structures::stable_hasher::{HashStable, hash_stable_hashmap, StableVec}; use arena::{TypedArena, SyncDroplessArena}; use rustc_data_structures::indexed_vec::IndexVec; -use rustc_data_structures::sync::{Lrc, Lock, WorkerLocal}; +use rustc_data_structures::sync::{self, Lrc, Lock, WorkerLocal}; use std::any::Any; use std::borrow::Borrow; use std::cmp::Ordering; @@ -1179,6 +1179,8 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> { output_filenames: Arc::new(output_filenames.clone()), }; + sync::assert_send_val(&gcx); + tls::enter_global(gcx, f) } From 461fa8495ef0b5b5d9fdc9b84c0dd9b0fd6e25bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20K=C3=A5re=20Alsaker?= Date: Fri, 1 Jun 2018 15:00:17 +0200 Subject: [PATCH 8/9] Update Cargo.lock --- src/Cargo.lock | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Cargo.lock b/src/Cargo.lock index 3a27107f825d6..d5a1d18a67608 100644 --- a/src/Cargo.lock +++ b/src/Cargo.lock @@ -2044,6 +2044,7 @@ dependencies = [ "parking_lot_core 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)", "rustc-hash 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", "rustc-rayon 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc-rayon-core 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "rustc_cratesio_shim 0.0.0", "serialize 0.0.0", "stable_deref_trait 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", From 2119d04b2d4f5f6aaa922e529aff5378d2036420 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20K=C3=A5re=20Alsaker?= Date: Fri, 1 Jun 2018 15:22:37 +0200 Subject: [PATCH 9/9] Add Sync impl for Slice --- src/librustc/ty/mod.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/librustc/ty/mod.rs b/src/librustc/ty/mod.rs index 646c60c139c85..f0f4adde7ee91 100644 --- a/src/librustc/ty/mod.rs +++ b/src/librustc/ty/mod.rs @@ -617,6 +617,8 @@ pub struct Slice { opaque: OpaqueSliceContents, } +unsafe impl Sync for Slice {} + impl Slice { #[inline] fn from_arena<'tcx>(arena: &'tcx SyncDroplessArena, slice: &[T]) -> &'tcx Slice {