Skip to content

Commit 8244b1b

Browse files
committed
Auto merge of rust-lang#74969 - nnethercote:rm-GCX_PTR, r=Mark-Simulacrum
Remove `GCX_PTR`. We store an `ImplicitCtxt` pointer in a thread-local value (TLV). This allows implicit access to a `GlobalCtxt` and some other things. We also store a `GlobalCtxt` pointer in `GCX_PTR`. This is always the same `GlobalCtxt` as the one within the `ImplicitCtxt` pointer in TLV. `GCX_PTR` is only used in the parallel compiler's `handle_deadlock()` function. This commit does the following. - It removes `GCX_PTR`. - It also adds `ImplicitCtxt::new()`, which constructs an `ImplicitCtxt` from a `GlobalCtxt`. `ImplicitCtxt::new()` + `tls::enter_context()` is now equivalent to the old `tls::enter_global()`. - Makes `tls::get_tlv()` public for the parallel compiler, because it's now used in `handle_deadlock()`. r? @petrochenkov
2 parents 19ecce3 + 8c78fd2 commit 8244b1b

File tree

9 files changed

+36
-79
lines changed

9 files changed

+36
-79
lines changed

Cargo.lock

-1
Original file line numberDiff line numberDiff line change
@@ -3677,7 +3677,6 @@ dependencies = [
36773677
"rustc_session",
36783678
"rustc_span",
36793679
"rustc_target",
3680-
"scoped-tls",
36813680
"smallvec 1.4.0",
36823681
"tracing",
36833682
]

src/librustc_infer/infer/canonical/canonicalizer.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,8 @@ impl CanonicalizeRegionMode for CanonicalizeQueryResponse {
199199
// rust-lang/rust#57464: `impl Trait` can leak local
200200
// scopes (in manner violating typeck). Therefore, use
201201
// `delay_span_bug` to allow type error over an ICE.
202-
ty::tls::with_context(|c| {
203-
c.tcx.sess.delay_span_bug(
202+
ty::tls::with(|tcx| {
203+
tcx.sess.delay_span_bug(
204204
rustc_span::DUMMY_SP,
205205
&format!("unexpected region in query response: `{:?}`", r),
206206
);

src/librustc_interface/passes.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -752,7 +752,8 @@ impl<'tcx> QueryContext<'tcx> {
752752
where
753753
F: FnOnce(TyCtxt<'tcx>) -> R,
754754
{
755-
ty::tls::enter_global(self.0, f)
755+
let icx = ty::tls::ImplicitCtxt::new(self.0);
756+
ty::tls::enter_context(&icx, |_| f(icx.tcx))
756757
}
757758

758759
pub fn print_stats(&mut self) {
@@ -811,8 +812,9 @@ pub fn create_global_ctxt<'tcx>(
811812
});
812813

813814
// Do some initialization of the DepGraph that can only be done with the tcx available.
814-
ty::tls::enter_global(&gcx, |tcx| {
815-
tcx.sess.time("dep_graph_tcx_init", || rustc_incremental::dep_graph_tcx_init(tcx));
815+
let icx = ty::tls::ImplicitCtxt::new(&gcx);
816+
ty::tls::enter_context(&icx, |_| {
817+
icx.tcx.sess.time("dep_graph_tcx_init", || rustc_incremental::dep_graph_tcx_init(icx.tcx));
816818
});
817819

818820
QueryContext(gcx)

src/librustc_interface/util.rs

+10-14
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,9 @@ use rustc_data_structures::fx::{FxHashMap, FxHashSet};
1010
#[cfg(parallel_compiler)]
1111
use rustc_data_structures::jobserver;
1212
use rustc_data_structures::stable_hasher::StableHasher;
13-
use rustc_data_structures::sync::{Lock, Lrc};
13+
use rustc_data_structures::sync::Lrc;
1414
use rustc_errors::registry::Registry;
1515
use rustc_metadata::dynamic_lib::DynamicLibrary;
16-
use rustc_middle::ty;
1716
use rustc_resolve::{self, Resolver};
1817
use rustc_session as session;
1918
use rustc_session::config::{self, CrateType};
@@ -144,12 +143,10 @@ pub fn setup_callbacks_and_run_in_thread_pool_with_globals<F: FnOnce() -> R + Se
144143

145144
let main_handler = move || {
146145
rustc_ast::with_session_globals(edition, || {
147-
ty::tls::GCX_PTR.set(&Lock::new(0), || {
148-
if let Some(stderr) = stderr {
149-
io::set_panic(Some(box Sink(stderr.clone())));
150-
}
151-
f()
152-
})
146+
if let Some(stderr) = stderr {
147+
io::set_panic(Some(box Sink(stderr.clone())));
148+
}
149+
f()
153150
})
154151
};
155152

@@ -163,6 +160,7 @@ pub fn setup_callbacks_and_run_in_thread_pool_with_globals<F: FnOnce() -> R + Se
163160
stderr: &Option<Arc<Mutex<Vec<u8>>>>,
164161
f: F,
165162
) -> R {
163+
use rustc_middle::ty;
166164
crate::callbacks::setup_callbacks();
167165

168166
let mut config = rayon::ThreadPoolBuilder::new()
@@ -189,12 +187,10 @@ pub fn setup_callbacks_and_run_in_thread_pool_with_globals<F: FnOnce() -> R + Se
189187
let main_handler = move |thread: rayon::ThreadBuilder| {
190188
rustc_ast::SESSION_GLOBALS.set(ast_session_globals, || {
191189
rustc_span::SESSION_GLOBALS.set(span_session_globals, || {
192-
ty::tls::GCX_PTR.set(&Lock::new(0), || {
193-
if let Some(stderr) = stderr {
194-
io::set_panic(Some(box Sink(stderr.clone())));
195-
}
196-
thread.run()
197-
})
190+
if let Some(stderr) = stderr {
191+
io::set_panic(Some(box Sink(stderr.clone())));
192+
}
193+
thread.run()
198194
})
199195
})
200196
};

src/librustc_middle/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ doctest = false
1212
[dependencies]
1313
rustc_arena = { path = "../librustc_arena" }
1414
bitflags = "1.2.1"
15-
scoped-tls = "1.0"
1615
log = { package = "tracing", version = "0.1" }
1716
rustc-rayon-core = "0.3.0"
1817
polonius-engine = "0.12.0"

src/librustc_middle/lib.rs

-2
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,6 @@
5555
#[macro_use]
5656
extern crate bitflags;
5757
#[macro_use]
58-
extern crate scoped_tls;
59-
#[macro_use]
6058
extern crate rustc_macros;
6159
#[macro_use]
6260
extern crate rustc_data_structures;

src/librustc_middle/mir/interpret/error.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -242,9 +242,9 @@ impl From<ErrorHandled> for InterpErrorInfo<'_> {
242242

243243
impl<'tcx> From<InterpError<'tcx>> for InterpErrorInfo<'tcx> {
244244
fn from(kind: InterpError<'tcx>) -> Self {
245-
let capture_backtrace = tls::with_context_opt(|ctxt| {
246-
if let Some(ctxt) = ctxt {
247-
*Lock::borrow(&ctxt.tcx.sess.ctfe_backtrace)
245+
let capture_backtrace = tls::with_opt(|tcx| {
246+
if let Some(tcx) = tcx {
247+
*Lock::borrow(&tcx.sess.ctfe_backtrace)
248248
} else {
249249
CtfeBacktrace::Disabled
250250
}

src/librustc_middle/ty/context.rs

+10-49
Original file line numberDiff line numberDiff line change
@@ -1632,7 +1632,6 @@ pub mod tls {
16321632
use crate::ty::query;
16331633
use rustc_data_structures::sync::{self, Lock};
16341634
use rustc_data_structures::thin_vec::ThinVec;
1635-
use rustc_data_structures::OnDrop;
16361635
use rustc_errors::Diagnostic;
16371636
use std::mem;
16381637

@@ -1649,8 +1648,7 @@ pub mod tls {
16491648
/// in this module.
16501649
#[derive(Clone)]
16511650
pub struct ImplicitCtxt<'a, 'tcx> {
1652-
/// The current `TyCtxt`. Initially created by `enter_global` and updated
1653-
/// by `enter_local` with a new local interner.
1651+
/// The current `TyCtxt`.
16541652
pub tcx: TyCtxt<'tcx>,
16551653

16561654
/// The current query job, if any. This is updated by `JobOwner::start` in
@@ -1669,6 +1667,13 @@ pub mod tls {
16691667
pub task_deps: Option<&'a Lock<TaskDeps>>,
16701668
}
16711669

1670+
impl<'a, 'tcx> ImplicitCtxt<'a, 'tcx> {
1671+
pub fn new(gcx: &'tcx GlobalCtxt<'tcx>) -> Self {
1672+
let tcx = TyCtxt { gcx };
1673+
ImplicitCtxt { tcx, query: None, diagnostics: None, layout_depth: 0, task_deps: None }
1674+
}
1675+
}
1676+
16721677
/// Sets Rayon's thread-local variable, which is preserved for Rayon jobs
16731678
/// to `value` during the call to `f`. It is restored to its previous value after.
16741679
/// This is used to set the pointer to the new `ImplicitCtxt`.
@@ -1682,7 +1687,7 @@ pub mod tls {
16821687
/// This is used to get the pointer to the current `ImplicitCtxt`.
16831688
#[cfg(parallel_compiler)]
16841689
#[inline]
1685-
fn get_tlv() -> usize {
1690+
pub fn get_tlv() -> usize {
16861691
rayon_core::tlv::get()
16871692
}
16881693

@@ -1699,7 +1704,7 @@ pub mod tls {
16991704
#[inline]
17001705
fn set_tlv<F: FnOnce() -> R, R>(value: usize, f: F) -> R {
17011706
let old = get_tlv();
1702-
let _reset = OnDrop(move || TLV.with(|tlv| tlv.set(old)));
1707+
let _reset = rustc_data_structures::OnDrop(move || TLV.with(|tlv| tlv.set(old)));
17031708
TLV.with(|tlv| tlv.set(value));
17041709
f()
17051710
}
@@ -1720,50 +1725,6 @@ pub mod tls {
17201725
set_tlv(context as *const _ as usize, || f(&context))
17211726
}
17221727

1723-
/// Enters `GlobalCtxt` by setting up librustc_ast callbacks and
1724-
/// creating a initial `TyCtxt` and `ImplicitCtxt`.
1725-
/// This happens once per rustc session and `TyCtxt`s only exists
1726-
/// inside the `f` function.
1727-
pub fn enter_global<'tcx, F, R>(gcx: &'tcx GlobalCtxt<'tcx>, f: F) -> R
1728-
where
1729-
F: FnOnce(TyCtxt<'tcx>) -> R,
1730-
{
1731-
// Update `GCX_PTR` to indicate there's a `GlobalCtxt` available.
1732-
GCX_PTR.with(|lock| {
1733-
*lock.lock() = gcx as *const _ as usize;
1734-
});
1735-
// Set `GCX_PTR` back to 0 when we exit.
1736-
let _on_drop = OnDrop(move || {
1737-
GCX_PTR.with(|lock| *lock.lock() = 0);
1738-
});
1739-
1740-
let tcx = TyCtxt { gcx };
1741-
let icx =
1742-
ImplicitCtxt { tcx, query: None, diagnostics: None, layout_depth: 0, task_deps: None };
1743-
enter_context(&icx, |_| f(tcx))
1744-
}
1745-
1746-
scoped_thread_local! {
1747-
/// Stores a pointer to the `GlobalCtxt` if one is available.
1748-
/// This is used to access the `GlobalCtxt` in the deadlock handler given to Rayon.
1749-
pub static GCX_PTR: Lock<usize>
1750-
}
1751-
1752-
/// Creates a `TyCtxt` and `ImplicitCtxt` based on the `GCX_PTR` thread local.
1753-
/// This is used in the deadlock handler.
1754-
pub unsafe fn with_global<F, R>(f: F) -> R
1755-
where
1756-
F: for<'tcx> FnOnce(TyCtxt<'tcx>) -> R,
1757-
{
1758-
let gcx = GCX_PTR.with(|lock| *lock.lock());
1759-
assert!(gcx != 0);
1760-
let gcx = &*(gcx as *const GlobalCtxt<'_>);
1761-
let tcx = TyCtxt { gcx };
1762-
let icx =
1763-
ImplicitCtxt { query: None, diagnostics: None, tcx, layout_depth: 0, task_deps: None };
1764-
enter_context(&icx, |_| f(tcx))
1765-
}
1766-
17671728
/// Allows access to the current `ImplicitCtxt` in a closure if one is available.
17681729
#[inline]
17691730
pub fn with_context_opt<F, R>(f: F) -> R

src/librustc_middle/ty/query/job.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,20 @@ use std::thread;
1010
pub unsafe fn handle_deadlock() {
1111
let registry = rayon_core::Registry::current();
1212

13-
let gcx_ptr = tls::GCX_PTR.with(|gcx_ptr| gcx_ptr as *const _);
14-
let gcx_ptr = &*gcx_ptr;
13+
let context = tls::get_tlv();
14+
assert!(context != 0);
15+
rustc_data_structures::sync::assert_sync::<tls::ImplicitCtxt<'_, '_>>();
16+
let icx: &tls::ImplicitCtxt<'_, '_> = &*(context as *const tls::ImplicitCtxt<'_, '_>);
1517

1618
let span_session_globals = rustc_span::SESSION_GLOBALS.with(|ssg| ssg as *const _);
1719
let span_session_globals = &*span_session_globals;
1820
let ast_session_globals = rustc_ast::attr::SESSION_GLOBALS.with(|asg| asg as *const _);
1921
let ast_session_globals = &*ast_session_globals;
2022
thread::spawn(move || {
21-
tls::GCX_PTR.set(gcx_ptr, || {
23+
tls::enter_context(icx, |_| {
2224
rustc_ast::attr::SESSION_GLOBALS.set(ast_session_globals, || {
2325
rustc_span::SESSION_GLOBALS
24-
.set(span_session_globals, || tls::with_global(|tcx| deadlock(tcx, &registry)))
26+
.set(span_session_globals, || tls::with(|tcx| deadlock(tcx, &registry)))
2527
});
2628
})
2729
});

0 commit comments

Comments
 (0)