Skip to content

Commit

Permalink
Make queries block and handle query cycles
Browse files Browse the repository at this point in the history
  • Loading branch information
Zoxc committed Jun 6, 2018
1 parent 685faa2 commit f9e6fbc
Show file tree
Hide file tree
Showing 7 changed files with 469 additions and 10 deletions.
4 changes: 4 additions & 0 deletions src/librustc/Cargo.toml
Expand Up @@ -15,9 +15,12 @@ fmt_macros = { path = "../libfmt_macros" }
graphviz = { path = "../libgraphviz" }
jobserver = "0.1"
lazy_static = "1.0.0"
scoped-tls = { version = "0.1.1", features = ["nightly"] }
log = { version = "0.4", features = ["release_max_level_info", "std"] }
polonius-engine = "0.5.0"
proc_macro = { path = "../libproc_macro" }
rustc-rayon = "0.1.0"
rustc-rayon-core = "0.1.0"
rustc_apfloat = { path = "../librustc_apfloat" }
rustc_target = { path = "../librustc_target" }
rustc_data_structures = { path = "../librustc_data_structures" }
Expand All @@ -26,6 +29,7 @@ serialize = { path = "../libserialize" }
syntax = { path = "../libsyntax" }
syntax_pos = { path = "../libsyntax_pos" }
backtrace = "0.3.3"
parking_lot = "0.5.5"
byteorder = { version = "1.1", features = ["i128"]}
chalk-engine = { version = "0.6.0", default-features=false }

Expand Down
5 changes: 5 additions & 0 deletions src/librustc/lib.rs
Expand Up @@ -67,6 +67,7 @@
#![feature(unboxed_closures)]
#![feature(trace_macros)]
#![feature(trusted_len)]
#![feature(vec_remove_item)]
#![feature(catch_expr)]
#![feature(integer_atomics)]
#![feature(test)]
Expand All @@ -83,13 +84,17 @@ extern crate fmt_macros;
extern crate getopts;
extern crate graphviz;
#[macro_use] extern crate lazy_static;
#[macro_use] extern crate scoped_tls;
#[cfg(windows)]
extern crate libc;
extern crate polonius_engine;
extern crate rustc_target;
#[macro_use] extern crate rustc_data_structures;
extern crate serialize;
extern crate parking_lot;
extern crate rustc_errors as errors;
extern crate rustc_rayon as rayon;
extern crate rustc_rayon_core as rayon_core;
#[macro_use] extern crate log;
#[macro_use] extern crate syntax;
extern crate syntax_pos;
Expand Down
50 changes: 48 additions & 2 deletions src/librustc/ty/context.rs
Expand Up @@ -1699,16 +1699,21 @@ impl<'a, 'tcx> Lift<'tcx> for &'a Slice<CanonicalVarInfo> {
pub mod tls {
use super::{GlobalCtxt, TyCtxt};

use std::cell::Cell;
use std::fmt;
use std::mem;
use syntax_pos;
use ty::maps;
use errors::{Diagnostic, TRACK_DIAGNOSTICS};
use rustc_data_structures::OnDrop;
use rustc_data_structures::sync::{self, Lrc};
use rustc_data_structures::sync::{self, Lrc, Lock};
use dep_graph::OpenTask;

#[cfg(not(parallel_queries))]
use std::cell::Cell;

#[cfg(parallel_queries)]
use rayon_core;

/// This is the implicit state of rustc. It contains the current
/// TyCtxt and query. It is updated when creating a local interner or
/// executing a new query. Whenever there's a TyCtxt value available
Expand All @@ -1732,16 +1737,29 @@ pub mod tls {
pub task: &'a OpenTask,
}

#[cfg(parallel_queries)]
fn set_tlv<F: FnOnce() -> R, R>(value: usize, f: F) -> R {
rayon_core::tlv::with(value, f)
}

#[cfg(parallel_queries)]
fn get_tlv() -> usize {
rayon_core::tlv::get()
}

// A thread local value which stores a pointer to the current ImplicitCtxt
#[cfg(not(parallel_queries))]
thread_local!(static TLV: Cell<usize> = Cell::new(0));

#[cfg(not(parallel_queries))]
fn set_tlv<F: FnOnce() -> R, R>(value: usize, f: F) -> R {
let old = get_tlv();
let _reset = OnDrop(move || TLV.with(|tlv| tlv.set(old)));
TLV.with(|tlv| tlv.set(value));
f()
}

#[cfg(not(parallel_queries))]
fn get_tlv() -> usize {
TLV.with(|tlv| tlv.get())
}
Expand Down Expand Up @@ -1810,6 +1828,13 @@ pub mod tls {
where F: for<'a> FnOnce(TyCtxt<'a, 'gcx, 'gcx>) -> R
{
with_thread_locals(|| {
GCX_PTR.with(|lock| {
*lock.lock() = gcx as *const _ as usize;
});
let _on_drop = OnDrop(move || {
GCX_PTR.with(|lock| *lock.lock() = 0);
});

let tcx = TyCtxt {
gcx,
interners: &gcx.global_interners,
Expand All @@ -1826,6 +1851,27 @@ pub mod tls {
})
}

scoped_thread_local!(pub static GCX_PTR: Lock<usize>);

pub unsafe fn with_global<F, R>(f: F) -> R
where F: for<'a, 'gcx, 'tcx> FnOnce(TyCtxt<'a, 'gcx, 'tcx>) -> R
{
let gcx = GCX_PTR.with(|lock| *lock.lock());
assert!(gcx != 0);
let gcx = &*(gcx as *const GlobalCtxt<'_>);
let tcx = TyCtxt {
gcx,
interners: &gcx.global_interners,
};
let icx = ImplicitCtxt {
query: None,
tcx,
layout_depth: 0,
task: &OpenTask::Ignore,
};
enter_context(&icx, |_| f(tcx))
}

/// Allows access to the current ImplicitCtxt in a closure if one is available
pub fn with_context_opt<F, R>(f: F) -> R
where F: for<'a, 'gcx, 'tcx> FnOnce(Option<&ImplicitCtxt<'a, 'gcx, 'tcx>>) -> R
Expand Down

0 comments on commit f9e6fbc

Please sign in to comment.