Skip to content

Commit c61f433

Browse files
committed
Move query TLS to rustc_query_system.
1 parent 2f09207 commit c61f433

File tree

18 files changed

+228
-160
lines changed

18 files changed

+228
-160
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4019,6 +4019,7 @@ dependencies = [
40194019
"rustc_plugin_impl",
40204020
"rustc_privacy",
40214021
"rustc_query_impl",
4022+
"rustc_query_system",
40224023
"rustc_resolve",
40234024
"rustc_serialize",
40244025
"rustc_session",

compiler/rustc_interface/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ rustc_lint = { path = "../rustc_lint" }
4242
rustc_errors = { path = "../rustc_errors" }
4343
rustc_plugin_impl = { path = "../rustc_plugin_impl" }
4444
rustc_privacy = { path = "../rustc_privacy" }
45+
rustc_query_system = { path = "../rustc_query_system" }
4546
rustc_query_impl = { path = "../rustc_query_impl" }
4647
rustc_resolve = { path = "../rustc_resolve" }
4748
rustc_trait_selection = { path = "../rustc_trait_selection" }

compiler/rustc_interface/src/callbacks.rs

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
//! The functions in this file should fall back to the default set in their
1010
//! origin crate when the `TyCtxt` is not present in TLS.
1111
12-
use rustc_errors::{Diagnostic, TRACK_DIAGNOSTICS};
1312
use rustc_middle::ty::tls;
1413
use std::fmt;
1514

@@ -35,20 +34,6 @@ fn track_span_parent(def_id: rustc_span::def_id::LocalDefId) {
3534
})
3635
}
3736

38-
/// This is a callback from `rustc_ast` as it cannot access the implicit state
39-
/// in `rustc_middle` otherwise. It is used to when diagnostic messages are
40-
/// emitted and stores them in the current query, if there is one.
41-
fn track_diagnostic(diagnostic: &Diagnostic) {
42-
tls::with_context_opt(|icx| {
43-
if let Some(icx) = icx {
44-
if let Some(diagnostics) = icx.diagnostics {
45-
let mut diagnostics = diagnostics.lock();
46-
diagnostics.extend(Some(diagnostic.clone()));
47-
}
48-
}
49-
})
50-
}
51-
5237
/// This is a callback from `rustc_hir` as it cannot access the implicit state
5338
/// in `rustc_middle` otherwise.
5439
fn def_id_debug(def_id: rustc_hir::def_id::DefId, f: &mut fmt::Formatter<'_>) -> fmt::Result {
@@ -68,5 +53,5 @@ pub fn setup_callbacks() {
6853
rustc_span::SPAN_DEBUG.swap(&(span_debug as fn(_, &mut fmt::Formatter<'_>) -> _));
6954
rustc_span::SPAN_TRACK.swap(&(track_span_parent as fn(_)));
7055
rustc_hir::def_id::DEF_ID_DEBUG.swap(&(def_id_debug as fn(_, &mut fmt::Formatter<'_>) -> _));
71-
TRACK_DIAGNOSTICS.swap(&(track_diagnostic as fn(&_)));
56+
rustc_errors::TRACK_DIAGNOSTICS.swap(&(rustc_query_system::tls::track_diagnostic as fn(&_)));
7257
}

compiler/rustc_interface/src/interface.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ pub fn try_print_query_stack(handler: &Handler, num_frames: Option<usize>) {
231231
// state if it was responsible for triggering the panic.
232232
let i = ty::tls::with_context_opt(|icx| {
233233
if let Some(icx) = icx {
234-
QueryCtxt::from_tcx(icx.tcx).try_print_query_stack(icx.query, handler, num_frames)
234+
QueryCtxt::from_tcx(icx.tcx).try_print_query_stack(handler, num_frames)
235235
} else {
236236
0
237237
}

compiler/rustc_interface/src/passes.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -781,7 +781,10 @@ impl<'tcx> QueryContext<'tcx> {
781781
F: FnOnce(TyCtxt<'tcx>) -> R,
782782
{
783783
let icx = ty::tls::ImplicitCtxt::new(self.gcx);
784-
ty::tls::enter_context(&icx, |_| f(icx.tcx))
784+
let qcx = rustc_query_system::tls::ImplicitCtxt::<rustc_middle::dep_graph::DepKind>::new();
785+
ty::tls::enter_context(&icx, |_| {
786+
rustc_query_system::tls::enter_context(&qcx, |_| f(icx.tcx))
787+
})
785788
}
786789
}
787790

compiler/rustc_interface/src/util.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,19 +154,24 @@ pub fn setup_callbacks_and_run_in_thread_pool_with_globals<F: FnOnce() -> R + Se
154154
/// Must only be called when a deadlock is about to happen.
155155
#[cfg(parallel_compiler)]
156156
unsafe fn handle_deadlock() {
157+
use rustc_query_system::tls as qls;
157158
let registry = rustc_rayon_core::Registry::current();
158159

159160
let context = tls::get_tlv();
160161
assert!(context != 0);
161-
rustc_data_structures::sync::assert_sync::<tls::ImplicitCtxt<'_, '_>>();
162-
let icx: &tls::ImplicitCtxt<'_, '_> = &*(context as *const tls::ImplicitCtxt<'_, '_>);
162+
rustc_data_structures::sync::assert_sync::<tls::ImplicitCtxt<'_>>();
163+
let icx: &tls::ImplicitCtxt<'_> = &*(context as *const tls::ImplicitCtxt<'_>);
164+
rustc_data_structures::sync::assert_sync::<qls::ImplicitCtxt<'_>>();
165+
let qcx: &qls::ImplicitCtxt<'_> = &*(context as *const qls::ImplicitCtxt<'_>);
163166

164167
let session_globals = rustc_span::with_session_globals(|sg| sg as *const _);
165168
let session_globals = &*session_globals;
166169
thread::spawn(move || {
167170
tls::enter_context(icx, |_| {
168-
rustc_span::set_session_globals_then(session_globals, || {
169-
tls::with(|tcx| QueryCtxt::from_tcx(tcx).deadlock(&registry))
171+
qls::enter_context(icx, |_| {
172+
rustc_span::set_session_globals_then(session_globals, || {
173+
tls::with(|tcx| QueryCtxt::from_tcx(tcx).deadlock(&registry))
174+
})
170175
})
171176
});
172177
});

compiler/rustc_middle/src/dep_graph/dep_node.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,8 @@ static_assert_size!(DepNode, 18);
216216
#[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
217217
static_assert_size!(DepNode, 24);
218218

219+
static_assert_size!(DepKind, 2);
220+
219221
pub trait DepNodeExt: Sized {
220222
/// Construct a DepNode from the given DepKind and DefPathHash. This
221223
/// method will assert that the given DepKind actually requires a

compiler/rustc_middle/src/dep_graph/mod.rs

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use crate::ty::{self, TyCtxt};
22
use rustc_data_structures::profiling::SelfProfilerRef;
3-
use rustc_data_structures::sync::Lock;
43
use rustc_query_system::ich::StableHashingContext;
54
use rustc_session::Session;
65

@@ -44,27 +43,6 @@ impl rustc_query_system::dep_graph::DepKind for DepKind {
4443

4544
write!(f, ")")
4645
}
47-
48-
fn with_deps<OP, R>(task_deps: Option<&Lock<TaskDeps>>, op: OP) -> R
49-
where
50-
OP: FnOnce() -> R,
51-
{
52-
ty::tls::with_context(|icx| {
53-
let icx = ty::tls::ImplicitCtxt { task_deps, ..icx.clone() };
54-
55-
ty::tls::enter_context(&icx, |_| op())
56-
})
57-
}
58-
59-
fn read_deps<OP>(op: OP)
60-
where
61-
OP: for<'a> FnOnce(Option<&'a Lock<TaskDeps>>),
62-
{
63-
ty::tls::with_context_opt(|icx| {
64-
let icx = if let Some(icx) = icx { icx } else { return };
65-
op(icx.task_deps)
66-
})
67-
}
6846
}
6947

7048
impl<'tcx> DepContext for TyCtxt<'tcx> {

compiler/rustc_middle/src/ty/context.rs

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1685,12 +1685,7 @@ CloneLiftImpls! { for<'tcx> { Constness, traits::WellFormedLoc, } }
16851685

16861686
pub mod tls {
16871687
use super::{GlobalCtxt, TyCtxt};
1688-
1689-
use crate::dep_graph::{DepKind, TaskDeps};
1690-
use crate::ty::query;
1691-
use rustc_data_structures::sync::{self, Lock};
1692-
use rustc_data_structures::thin_vec::ThinVec;
1693-
use rustc_errors::Diagnostic;
1688+
use rustc_data_structures::sync;
16941689

16951690
#[cfg(not(parallel_compiler))]
16961691
use std::cell::Cell;
@@ -1704,30 +1699,18 @@ pub mod tls {
17041699
/// you should also have access to an `ImplicitCtxt` through the functions
17051700
/// in this module.
17061701
#[derive(Clone)]
1707-
pub struct ImplicitCtxt<'a, 'tcx> {
1702+
pub struct ImplicitCtxt<'tcx> {
17081703
/// The current `TyCtxt`.
17091704
pub tcx: TyCtxt<'tcx>,
17101705

1711-
/// The current query job, if any. This is updated by `JobOwner::start` in
1712-
/// `ty::query::plumbing` when executing a query.
1713-
pub query: Option<query::QueryJobId<DepKind>>,
1714-
1715-
/// Where to store diagnostics for the current query job, if any.
1716-
/// This is updated by `JobOwner::start` in `ty::query::plumbing` when executing a query.
1717-
pub diagnostics: Option<&'a Lock<ThinVec<Diagnostic>>>,
1718-
17191706
/// Used to prevent layout from recursing too deeply.
17201707
pub layout_depth: usize,
1721-
1722-
/// The current dep graph task. This is used to add dependencies to queries
1723-
/// when executing them.
1724-
pub task_deps: Option<&'a Lock<TaskDeps>>,
17251708
}
17261709

1727-
impl<'a, 'tcx> ImplicitCtxt<'a, 'tcx> {
1710+
impl<'tcx> ImplicitCtxt<'tcx> {
17281711
pub fn new(gcx: &'tcx GlobalCtxt<'tcx>) -> Self {
17291712
let tcx = TyCtxt { gcx };
1730-
ImplicitCtxt { tcx, query: None, diagnostics: None, layout_depth: 0, task_deps: None }
1713+
ImplicitCtxt { tcx, layout_depth: 0 }
17311714
}
17321715
}
17331716

@@ -1775,9 +1758,9 @@ pub mod tls {
17751758

17761759
/// Sets `context` as the new current `ImplicitCtxt` for the duration of the function `f`.
17771760
#[inline]
1778-
pub fn enter_context<'a, 'tcx, F, R>(context: &ImplicitCtxt<'a, 'tcx>, f: F) -> R
1761+
pub fn enter_context<'tcx, F, R>(context: &ImplicitCtxt<'tcx>, f: F) -> R
17791762
where
1780-
F: FnOnce(&ImplicitCtxt<'a, 'tcx>) -> R,
1763+
F: FnOnce(&ImplicitCtxt<'tcx>) -> R,
17811764
{
17821765
set_tlv(context as *const _ as usize, || f(&context))
17831766
}
@@ -1786,17 +1769,17 @@ pub mod tls {
17861769
#[inline]
17871770
pub fn with_context_opt<F, R>(f: F) -> R
17881771
where
1789-
F: for<'a, 'tcx> FnOnce(Option<&ImplicitCtxt<'a, 'tcx>>) -> R,
1772+
F: for<'tcx> FnOnce(Option<&ImplicitCtxt<'tcx>>) -> R,
17901773
{
17911774
let context = get_tlv();
17921775
if context == 0 {
17931776
f(None)
17941777
} else {
17951778
// We could get an `ImplicitCtxt` pointer from another thread.
17961779
// Ensure that `ImplicitCtxt` is `Sync`.
1797-
sync::assert_sync::<ImplicitCtxt<'_, '_>>();
1780+
sync::assert_sync::<ImplicitCtxt<'_>>();
17981781

1799-
unsafe { f(Some(&*(context as *const ImplicitCtxt<'_, '_>))) }
1782+
unsafe { f(Some(&*(context as *const ImplicitCtxt<'_>))) }
18001783
}
18011784
}
18021785

@@ -1805,7 +1788,7 @@ pub mod tls {
18051788
#[inline]
18061789
pub fn with_context<F, R>(f: F) -> R
18071790
where
1808-
F: for<'a, 'tcx> FnOnce(&ImplicitCtxt<'a, 'tcx>) -> R,
1791+
F: for<'tcx> FnOnce(&ImplicitCtxt<'tcx>) -> R,
18091792
{
18101793
with_context_opt(|opt_context| f(opt_context.expect("no ImplicitCtxt stored in tls")))
18111794
}

compiler/rustc_middle/src/ty/query.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ use std::ops::Deref;
6060
use std::path::PathBuf;
6161
use std::sync::Arc;
6262

63-
pub(crate) use rustc_query_system::query::QueryJobId;
6463
use rustc_query_system::query::*;
6564

6665
#[derive(Copy, Clone)]

compiler/rustc_query_impl/src/plumbing.rs

Lines changed: 5 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,11 @@
33
//! manage the caches, and so forth.
44
55
use crate::{on_disk_cache, queries, Queries};
6-
use rustc_middle::dep_graph::{DepKind, DepNodeIndex, SerializedDepNodeIndex};
7-
use rustc_middle::ty::tls::{self, ImplicitCtxt};
6+
use rustc_errors::Handler;
7+
use rustc_middle::dep_graph::{DepNodeIndex, SerializedDepNodeIndex};
88
use rustc_middle::ty::{self, TyCtxt};
99
use rustc_query_system::dep_graph::HasDepContext;
10-
use rustc_query_system::query::{
11-
QueryContext, QueryDescription, QueryJobId, QueryMap, QuerySideEffects,
12-
};
13-
14-
use rustc_data_structures::sync::Lock;
15-
use rustc_data_structures::thin_vec::ThinVec;
16-
use rustc_errors::{Diagnostic, Handler};
10+
use rustc_query_system::query::{QueryContext, QueryDescription, QueryMap, QuerySideEffects};
1711
use rustc_serialize::opaque;
1812
use rustc_span::def_id::LocalDefId;
1913

@@ -45,10 +39,6 @@ impl HasDepContext for QueryCtxt<'tcx> {
4539
}
4640

4741
impl QueryContext for QueryCtxt<'tcx> {
48-
fn current_query_job(&self) -> Option<QueryJobId<Self::DepKind>> {
49-
tls::with_context(|icx| icx.query)
50-
}
51-
5242
fn try_collect_active_jobs(&self) -> Option<QueryMap<Self::DepKind>> {
5343
self.queries.try_collect_active_jobs(**self)
5444
}
@@ -77,36 +67,6 @@ impl QueryContext for QueryCtxt<'tcx> {
7767
c.store_side_effects_for_anon_node(dep_node_index, side_effects)
7868
}
7969
}
80-
81-
/// Executes a job by changing the `ImplicitCtxt` to point to the
82-
/// new query job while it executes. It returns the diagnostics
83-
/// captured during execution and the actual result.
84-
#[inline(always)]
85-
fn start_query<R>(
86-
&self,
87-
token: QueryJobId<Self::DepKind>,
88-
diagnostics: Option<&Lock<ThinVec<Diagnostic>>>,
89-
compute: impl FnOnce() -> R,
90-
) -> R {
91-
// The `TyCtxt` stored in TLS has the same global interner lifetime
92-
// as `self`, so we use `with_context` to relate the 'tcx lifetimes
93-
// when accessing the `ImplicitCtxt`.
94-
tls::with_context(move |current_icx| {
95-
// Update the `ImplicitCtxt` to point to our new query job.
96-
let new_icx = ImplicitCtxt {
97-
tcx: **self,
98-
query: Some(token),
99-
diagnostics,
100-
layout_depth: current_icx.layout_depth,
101-
task_deps: current_icx.task_deps,
102-
};
103-
104-
// Use the `ImplicitCtxt` while we execute the query.
105-
tls::enter_context(&new_icx, |_| {
106-
rustc_data_structures::stack::ensure_sufficient_stack(compute)
107-
})
108-
})
109-
}
11070
}
11171

11272
impl<'tcx> QueryCtxt<'tcx> {
@@ -153,13 +113,8 @@ impl<'tcx> QueryCtxt<'tcx> {
153113
Ok(())
154114
}
155115

156-
pub fn try_print_query_stack(
157-
self,
158-
query: Option<QueryJobId<DepKind>>,
159-
handler: &Handler,
160-
num_frames: Option<usize>,
161-
) -> usize {
162-
rustc_query_system::query::print_query_stack(self, query, handler, num_frames)
116+
pub fn try_print_query_stack(self, handler: &Handler, num_frames: Option<usize>) -> usize {
117+
rustc_query_system::query::print_query_stack(self, handler, num_frames)
163118
}
164119
}
165120

compiler/rustc_query_system/src/dep_graph/graph.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ impl<K: DepKind> DepGraph<K> {
158158

159159
pub fn assert_ignored(&self) {
160160
if let Some(..) = self.data {
161-
K::read_deps(|task_deps| {
161+
crate::tls::read_deps::<K, _>(|task_deps| {
162162
assert!(task_deps.is_none(), "expected no task dependency tracking");
163163
})
164164
}
@@ -168,7 +168,7 @@ impl<K: DepKind> DepGraph<K> {
168168
where
169169
OP: FnOnce() -> R,
170170
{
171-
K::with_deps(None, op)
171+
crate::tls::with_deps::<K, _, _>(None, op)
172172
}
173173

174174
/// Starts a new dep-graph task. Dep-graph tasks are specified
@@ -253,7 +253,7 @@ impl<K: DepKind> DepGraph<K> {
253253
phantom_data: PhantomData,
254254
}))
255255
};
256-
let result = K::with_deps(task_deps.as_ref(), || task(cx, arg));
256+
let result = crate::tls::with_deps(task_deps.as_ref(), || task(cx, arg));
257257
let edges = task_deps.map_or_else(|| smallvec![], |lock| lock.into_inner().reads);
258258

259259
let dcx = cx.dep_context();
@@ -305,8 +305,8 @@ impl<K: DepKind> DepGraph<K> {
305305
debug_assert!(!cx.is_eval_always(dep_kind));
306306

307307
if let Some(ref data) = self.data {
308-
let task_deps = Lock::new(TaskDeps::default());
309-
let result = K::with_deps(Some(&task_deps), op);
308+
let task_deps = Lock::new(TaskDeps::<K>::default());
309+
let result = crate::tls::with_deps(Some(&task_deps), op);
310310
let task_deps = task_deps.into_inner();
311311
let task_deps = task_deps.reads;
312312

@@ -358,7 +358,7 @@ impl<K: DepKind> DepGraph<K> {
358358
#[inline]
359359
pub fn read_index(&self, dep_node_index: DepNodeIndex) {
360360
if let Some(ref data) = self.data {
361-
K::read_deps(|task_deps| {
361+
crate::tls::read_deps(|task_deps| {
362362
if let Some(task_deps) = task_deps {
363363
let mut task_deps = task_deps.lock();
364364
let task_deps = &mut *task_deps;

compiler/rustc_query_system/src/dep_graph/mod.rs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ pub use serialized::{SerializedDepGraph, SerializedDepNodeIndex};
1111

1212
use crate::ich::StableHashingContext;
1313
use rustc_data_structures::profiling::SelfProfilerRef;
14-
use rustc_data_structures::sync::Lock;
1514
use rustc_serialize::{opaque::FileEncoder, Encodable};
1615
use rustc_session::Session;
1716

@@ -88,14 +87,4 @@ pub trait DepKind: Copy + fmt::Debug + Eq + Hash + Send + Encodable<FileEncoder>
8887

8988
/// Implementation of `std::fmt::Debug` for `DepNode`.
9089
fn debug_node(node: &DepNode<Self>, f: &mut fmt::Formatter<'_>) -> fmt::Result;
91-
92-
/// Execute the operation with provided dependencies.
93-
fn with_deps<OP, R>(deps: Option<&Lock<TaskDeps<Self>>>, op: OP) -> R
94-
where
95-
OP: FnOnce() -> R;
96-
97-
/// Access dependencies from current implicit context.
98-
fn read_deps<OP>(op: OP)
99-
where
100-
OP: for<'a> FnOnce(Option<&'a Lock<TaskDeps<Self>>>);
10190
}

0 commit comments

Comments
 (0)