Skip to content

Commit 24a7c7f

Browse files
committed
Shrink TLS interface.
Consider that an empty registered TLS is equivalent to an empty TLS.
1 parent 3607bf8 commit 24a7c7f

File tree

3 files changed

+24
-49
lines changed

3 files changed

+24
-49
lines changed

compiler/rustc_interface/src/passes.rs

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

compiler/rustc_interface/src/util.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -154,24 +154,22 @@ 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;
158157
let registry = rustc_rayon_core::Registry::current();
159158

160159
let context = tls::get_tlv();
161160
assert!(context != 0);
162161
rustc_data_structures::sync::assert_sync::<tls::ImplicitCtxt<'_>>();
163162
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<'_>);
163+
164+
// We do not need to copy the query ImplicitCtxt since this deadlock handler is not part of a
165+
// normal query invocation.
166166

167167
let session_globals = rustc_span::with_session_globals(|sg| sg as *const _);
168168
let session_globals = &*session_globals;
169169
thread::spawn(move || {
170170
tls::enter_context(icx, |_| {
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-
})
171+
rustc_span::set_session_globals_then(session_globals, || {
172+
tls::with(|tcx| QueryCtxt::from_tcx(tcx).deadlock(&registry))
175173
})
176174
});
177175
});

compiler/rustc_query_system/src/tls.rs

Lines changed: 18 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ use rustc_rayon_core as rayon_core;
1313
/// This is the implicit state of rustc. It contains the current
1414
/// query. It is updated when
1515
/// executing a new query.
16-
#[derive(Clone)]
17-
pub struct ImplicitCtxt<'a> {
16+
#[derive(Clone, Default)]
17+
struct ImplicitCtxt<'a> {
1818
/// The current query job, if any. This is updated by `JobOwner::start` in
1919
/// `ty::query::plumbing` when executing a query.
2020
query: Option<QueryJobId>,
@@ -28,12 +28,6 @@ pub struct ImplicitCtxt<'a> {
2828
task_deps: Option<&'a Lock<TaskDeps>>,
2929
}
3030

31-
impl<'a> ImplicitCtxt<'a> {
32-
pub fn new() -> Self {
33-
ImplicitCtxt { query: None, diagnostics: None, task_deps: None }
34-
}
35-
}
36-
3731
/// Sets Rayon's thread-local variable, which is preserved for Rayon jobs
3832
/// to `value` during the call to `f`. It is restored to its previous value after.
3933
/// This is used to set the pointer to the new `ImplicitCtxt`.
@@ -78,7 +72,7 @@ fn get_tlv() -> usize {
7872

7973
/// Sets `context` as the new current `ImplicitCtxt` for the duration of the function `f`.
8074
#[inline]
81-
pub fn enter_context<'a, F, R>(context: &ImplicitCtxt<'a>, f: F) -> R
75+
fn enter_context<'a, F, R>(context: &ImplicitCtxt<'a>, f: F) -> R
8276
where
8377
F: FnOnce(&ImplicitCtxt<'a>) -> R,
8478
{
@@ -91,26 +85,14 @@ fn with_context_opt<F, R>(f: F) -> R
9185
where
9286
F: for<'a> FnOnce(Option<&ImplicitCtxt<'a>>) -> R,
9387
{
94-
let context = get_tlv();
95-
if context == 0 {
96-
f(None)
97-
} else {
98-
// We could get a `ImplicitCtxt` pointer from another thread.
99-
// Ensure that `ImplicitCtxt` is `Sync`.
100-
sync::assert_sync::<ImplicitCtxt<'_>>();
101-
102-
unsafe { f(Some(&*(context as *const ImplicitCtxt<'_>))) }
103-
}
104-
}
88+
// We could get a `ImplicitCtxt` pointer from another thread.
89+
// Ensure that `ImplicitCtxt` is `Sync`.
90+
sync::assert_sync::<ImplicitCtxt<'_>>();
10591

106-
/// Allows access to the current `ImplicitCtxt`.
107-
/// Panics if there is no `ImplicitCtxt` available.
108-
#[inline]
109-
fn with_context<F, R>(f: F) -> R
110-
where
111-
F: for<'a> FnOnce(&ImplicitCtxt<'a>) -> R,
112-
{
113-
with_context_opt(|opt_context| f(opt_context.expect("no ImplicitCtxt stored in tls")))
92+
let context = get_tlv();
93+
let context =
94+
if context == 0 { None } else { unsafe { Some(&*(context as *const ImplicitCtxt<'_>)) } };
95+
f(context)
11496
}
11597

11698
/// This is a callback from `rustc_ast` as it cannot access the implicit state
@@ -131,8 +113,8 @@ pub fn with_deps<OP, R>(task_deps: Option<&Lock<TaskDeps>>, op: OP) -> R
131113
where
132114
OP: FnOnce() -> R,
133115
{
134-
crate::tls::with_context(|icx| {
135-
let icx = crate::tls::ImplicitCtxt { task_deps, ..icx.clone() };
116+
crate::tls::with_context_opt(|icx| {
117+
let icx = crate::tls::ImplicitCtxt { task_deps, ..icx.cloned().unwrap_or_default() };
136118

137119
crate::tls::enter_context(&icx, |_| op())
138120
})
@@ -142,16 +124,13 @@ pub fn read_deps<OP>(op: OP)
142124
where
143125
OP: for<'a> FnOnce(Option<&'a Lock<TaskDeps>>),
144126
{
145-
crate::tls::with_context_opt(|icx| {
146-
let icx = if let Some(icx) = icx { icx } else { return };
147-
op(icx.task_deps)
148-
})
127+
crate::tls::with_context_opt(|icx| op(icx.and_then(|icx| icx.task_deps)))
149128
}
150129

151130
/// Get the query information from the TLS context.
152131
#[inline(always)]
153132
pub fn current_query_job() -> Option<QueryJobId> {
154-
with_context(|icx| icx.query)
133+
with_context_opt(|icx| icx?.query)
155134
}
156135

157136
/// Executes a job by changing the `ImplicitCtxt` to point to the
@@ -163,10 +142,11 @@ pub fn start_query<R>(
163142
diagnostics: Option<&Lock<ThinVec<Diagnostic>>>,
164143
compute: impl FnOnce() -> R,
165144
) -> R {
166-
with_context(move |current_icx| {
145+
with_context_opt(move |current_icx| {
146+
let task_deps = current_icx.and_then(|icx| icx.task_deps);
147+
167148
// Update the `ImplicitCtxt` to point to our new query job.
168-
let new_icx =
169-
ImplicitCtxt { query: Some(token), diagnostics, task_deps: current_icx.task_deps };
149+
let new_icx = ImplicitCtxt { query: Some(token), diagnostics, task_deps };
170150

171151
// Use the `ImplicitCtxt` while we execute the query.
172152
enter_context(&new_icx, |_| rustc_data_structures::stack::ensure_sufficient_stack(compute))

0 commit comments

Comments
 (0)