Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More thread-safety changes #49882

Merged
merged 6 commits into from
Apr 17, 2018
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/libarena/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ version = "0.0.0"
name = "arena"
path = "lib.rs"
crate-type = ["dylib"]

[dependencies]
rustc_data_structures = { path = "../librustc_data_structures" }
71 changes: 71 additions & 0 deletions src/libarena/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
#![allow(deprecated)]

extern crate alloc;
extern crate rustc_data_structures;

use rustc_data_structures::sync::MTLock;

use std::cell::{Cell, RefCell};
use std::cmp;
Expand Down Expand Up @@ -290,6 +293,8 @@ pub struct DroplessArena {
chunks: RefCell<Vec<TypedArenaChunk<u8>>>,
}

unsafe impl Send for DroplessArena {}

impl DroplessArena {
pub fn new() -> DroplessArena {
DroplessArena {
Expand Down Expand Up @@ -410,6 +415,72 @@ impl DroplessArena {
}
}

pub struct SyncTypedArena<T> {
lock: MTLock<TypedArena<T>>,
}

impl<T> SyncTypedArena<T> {
#[inline(always)]
pub fn new() -> SyncTypedArena<T> {
SyncTypedArena {
lock: MTLock::new(TypedArena::new())
}
}

#[inline(always)]
pub fn alloc(&self, object: T) -> &mut T {
// Extend the lifetime of the result since it's limited to the lock guard
unsafe { &mut *(self.lock.lock().alloc(object) as *mut T) }
}

#[inline(always)]
pub fn alloc_slice(&self, slice: &[T]) -> &mut [T]
where
T: Copy,
{
// Extend the lifetime of the result since it's limited to the lock guard
unsafe { &mut *(self.lock.lock().alloc_slice(slice) as *mut [T]) }
}

#[inline(always)]
pub fn clear(&mut self) {
self.lock.get_mut().clear();
}
}

pub struct SyncDroplessArena {
lock: MTLock<DroplessArena>,
}

impl SyncDroplessArena {
#[inline(always)]
pub fn new() -> SyncDroplessArena {
SyncDroplessArena {
lock: MTLock::new(DroplessArena::new())
}
}

#[inline(always)]
pub fn in_arena<T: ?Sized>(&self, ptr: *const T) -> bool {
self.lock.lock().in_arena(ptr)
}

#[inline(always)]
pub fn alloc<T>(&self, object: T) -> &mut T {
// Extend the lifetime of the result since it's limited to the lock guard
unsafe { &mut *(self.lock.lock().alloc(object) as *mut T) }
}

#[inline(always)]
pub fn alloc_slice<T>(&self, slice: &[T]) -> &mut [T]
where
T: Copy,
{
// Extend the lifetime of the result since it's limited to the lock guard
unsafe { &mut *(self.lock.lock().alloc_slice(slice) as *mut [T]) }
}
}

#[cfg(test)]
mod tests {
extern crate test;
Expand Down
6 changes: 3 additions & 3 deletions src/librustc/hir/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use hir::print::Nested;
use hir::svh::Svh;
use util::nodemap::{DefIdMap, FxHashMap};

use arena::TypedArena;
use arena::SyncTypedArena;
use std::io;
use ty::TyCtxt;

Expand Down Expand Up @@ -219,15 +219,15 @@ impl<'hir> MapEntry<'hir> {
pub struct Forest {
krate: Crate,
pub dep_graph: DepGraph,
inlined_bodies: TypedArena<Body>
inlined_bodies: SyncTypedArena<Body>
}

impl Forest {
pub fn new(krate: Crate, dep_graph: &DepGraph) -> Forest {
Forest {
krate,
dep_graph: dep_graph.clone(),
inlined_bodies: TypedArena::new()
inlined_bodies: SyncTypedArena::new()
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/librustc/infer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ use errors::DiagnosticBuilder;
use syntax_pos::{self, Span};
use syntax_pos::symbol::InternedString;
use util::nodemap::FxHashMap;
use arena::DroplessArena;
use arena::SyncDroplessArena;

use self::combine::CombineFields;
use self::higher_ranked::HrMatchResult;
Expand Down Expand Up @@ -407,15 +407,15 @@ impl fmt::Display for FixupError {
/// F: for<'b, 'tcx> where 'gcx: 'tcx FnOnce(InferCtxt<'b, 'gcx, 'tcx>).
pub struct InferCtxtBuilder<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
global_tcx: TyCtxt<'a, 'gcx, 'gcx>,
arena: DroplessArena,
arena: SyncDroplessArena,
fresh_tables: Option<RefCell<ty::TypeckTables<'tcx>>>,
}

impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'gcx> {
pub fn infer_ctxt(self) -> InferCtxtBuilder<'a, 'gcx, 'tcx> {
InferCtxtBuilder {
global_tcx: self,
arena: DroplessArena::new(),
arena: SyncDroplessArena::new(),
fresh_tables: None,

}
Expand Down
22 changes: 11 additions & 11 deletions src/librustc/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,11 @@ use rustc_data_structures::accumulate_vec::AccumulateVec;
use rustc_data_structures::stable_hasher::{HashStable, hash_stable_hashmap,
StableHasher, StableHasherResult,
StableVec};
use arena::{TypedArena, DroplessArena};
use arena::{TypedArena, SyncDroplessArena};
use rustc_data_structures::indexed_vec::IndexVec;
use rustc_data_structures::sync::{Lrc, Lock};
use std::any::Any;
use std::borrow::Borrow;
use std::cell::Cell;
use std::cmp::Ordering;
use std::collections::hash_map::{self, Entry};
use std::hash::{Hash, Hasher};
Expand All @@ -83,14 +82,14 @@ use hir;

pub struct AllArenas<'tcx> {
pub global: GlobalArenas<'tcx>,
pub interner: DroplessArena,
pub interner: SyncDroplessArena,
}

impl<'tcx> AllArenas<'tcx> {
pub fn new() -> Self {
AllArenas {
global: GlobalArenas::new(),
interner: DroplessArena::new(),
interner: SyncDroplessArena::new(),
}
}
}
Expand Down Expand Up @@ -130,7 +129,7 @@ type InternedSet<'tcx, T> = Lock<FxHashSet<Interned<'tcx, T>>>;

pub struct CtxtInterners<'tcx> {
/// The arena that types, regions, etc are allocated from
arena: &'tcx DroplessArena,
arena: &'tcx SyncDroplessArena,

/// Specifically use a speedy hash algorithm for these hash sets,
/// they're accessed quite often.
Expand All @@ -147,7 +146,7 @@ pub struct CtxtInterners<'tcx> {
}

impl<'gcx: 'tcx, 'tcx> CtxtInterners<'tcx> {
fn new(arena: &'tcx DroplessArena) -> CtxtInterners<'tcx> {
fn new(arena: &'tcx SyncDroplessArena) -> CtxtInterners<'tcx> {
CtxtInterners {
arena,
type_: Default::default(),
Expand Down Expand Up @@ -915,9 +914,6 @@ pub struct GlobalCtxt<'tcx> {
/// Data layout specification for the current target.
pub data_layout: TargetDataLayout,

/// Used to prevent layout from recursing too deeply.
pub layout_depth: Cell<usize>,

stability_interner: Lock<FxHashSet<&'tcx attr::Stability>>,

pub interpret_interner: InterpretInterner<'tcx>,
Expand Down Expand Up @@ -1292,7 +1288,6 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
crate_name: Symbol::intern(crate_name),
data_layout,
layout_interner: Lock::new(FxHashSet()),
layout_depth: Cell::new(0),
stability_interner: Lock::new(FxHashSet()),
interpret_interner: Default::default(),
tx_to_llvm_workers: Lock::new(tx),
Expand Down Expand Up @@ -1559,7 +1554,7 @@ impl<'gcx: 'tcx, 'tcx> GlobalCtxt<'gcx> {
/// Call the closure with a local `TyCtxt` using the given arena.
pub fn enter_local<F, R>(
&self,
arena: &'tcx DroplessArena,
arena: &'tcx SyncDroplessArena,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could the local arenas in theory be non-synchronized?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the arenas could be thread-local if we find a way to deal with lifting of types.

f: F
) -> R
where
Expand All @@ -1574,6 +1569,7 @@ impl<'gcx: 'tcx, 'tcx> GlobalCtxt<'gcx> {
let new_icx = ty::tls::ImplicitCtxt {
tcx,
query: icx.query.clone(),
layout_depth: icx.layout_depth,
};
ty::tls::enter_context(&new_icx, |new_icx| {
f(new_icx.tcx)
Expand Down Expand Up @@ -1768,6 +1764,9 @@ pub mod tls {
/// The current query job, if any. This is updated by start_job in
/// ty::maps::plumbing when executing a query
pub query: Option<Lrc<maps::QueryJob<'gcx>>>,

/// Used to prevent layout from recursing too deeply.
pub layout_depth: usize,
}

// A thread local value which stores a pointer to the current ImplicitCtxt
Expand Down Expand Up @@ -1853,6 +1852,7 @@ pub mod tls {
let icx = ImplicitCtxt {
tcx,
query: None,
layout_depth: 0,
};
enter_context(&icx, |_| {
f(tcx)
Expand Down
29 changes: 17 additions & 12 deletions src/librustc/ty/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -896,21 +896,26 @@ fn layout_raw<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
query: ty::ParamEnvAnd<'tcx, Ty<'tcx>>)
-> Result<&'tcx LayoutDetails, LayoutError<'tcx>>
{
let (param_env, ty) = query.into_parts();
ty::tls::with_related_context(tcx, move |icx| {
let rec_limit = *tcx.sess.recursion_limit.get();
let (param_env, ty) = query.into_parts();

let rec_limit = *tcx.sess.recursion_limit.get();
let depth = tcx.layout_depth.get();
if depth > rec_limit {
tcx.sess.fatal(
&format!("overflow representing the type `{}`", ty));
}
if icx.layout_depth > rec_limit {
tcx.sess.fatal(
&format!("overflow representing the type `{}`", ty));
}

tcx.layout_depth.set(depth+1);
let cx = LayoutCx { tcx, param_env };
let layout = cx.layout_raw_uncached(ty);
tcx.layout_depth.set(depth);
// Update the ImplicitCtxt to increase the layout_depth
let icx = ty::tls::ImplicitCtxt {
layout_depth: icx.layout_depth + 1,
..icx.clone()
};

layout
ty::tls::enter_context(&icx, |_| {
let cx = LayoutCx { tcx, param_env };
cx.layout_raw_uncached(ty)
})
})
}

pub fn provide(providers: &mut ty::maps::Providers) {
Expand Down
13 changes: 11 additions & 2 deletions src/librustc/ty/maps/on_disk_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ use ty;
use ty::maps::job::QueryResult;
use ty::codec::{self as ty_codec, TyDecoder, TyEncoder};
use ty::context::TyCtxt;
use util::common::time;

const TAG_FILE_FOOTER: u128 = 0xC0FFEE_C0FFEE_C0FFEE_C0FFEE_C0FFEE;

Expand Down Expand Up @@ -214,7 +215,7 @@ impl<'sess> OnDiskCache<'sess> {
// Encode query results
let mut query_result_index = EncodedQueryResultIndex::new();

{
time(tcx.sess, "encode query results", || {
use ty::maps::queries::*;
let enc = &mut encoder;
let qri = &mut query_result_index;
Expand Down Expand Up @@ -258,7 +259,9 @@ impl<'sess> OnDiskCache<'sess> {
}
}
}
}

Ok(())
})?;

// Encode diagnostics
let diagnostics_index = {
Expand Down Expand Up @@ -1125,6 +1128,11 @@ fn encode_query_results<'enc, 'a, 'tcx, Q, E>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
E: 'enc + TyEncoder,
Q::Value: Encodable,
{
let desc = &format!("encode_query_results for {}",
unsafe { ::std::intrinsics::type_name::<Q>() });

time(tcx.sess, desc, || {

for (key, entry) in Q::get_cache_internal(tcx).map.iter() {
if Q::cache_on_disk(key.clone()) {
let entry = match *entry {
Expand All @@ -1143,4 +1151,5 @@ fn encode_query_results<'enc, 'a, 'tcx, Q, E>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
}

Ok(())
})
}
1 change: 1 addition & 0 deletions src/librustc/ty/maps/plumbing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,7 @@ macro_rules! define_maps {
let icx = ty::tls::ImplicitCtxt {
tcx,
query: Some(job.clone()),
layout_depth: icx.layout_depth,
};

// Use the ImplicitCtxt while we execute the query
Expand Down
8 changes: 6 additions & 2 deletions src/librustc_driver/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -793,9 +793,13 @@ pub fn phase_2_configure_and_expand_inner<'a, F>(sess: &'a Session,
let mut ecx = ExtCtxt::new(&sess.parse_sess, cfg, &mut resolver);
let err_count = ecx.parse_sess.span_diagnostic.err_count();

let krate = ecx.monotonic_expander().expand_crate(krate);
let krate = time(sess, "expand crate", || {
ecx.monotonic_expander().expand_crate(krate)
});

ecx.check_unused_macros();
time(sess, "check unused macros", || {
ecx.check_unused_macros();
});

let mut missing_fragment_specifiers: Vec<_> =
ecx.parse_sess.missing_fragment_specifiers.borrow().iter().cloned().collect();
Expand Down
Loading