Skip to content

Commit a1f4804

Browse files
committed
Use Rayon's TLV directly
1 parent 8be3c2b commit a1f4804

File tree

1 file changed

+14
-45
lines changed
  • compiler/rustc_middle/src/ty/context

1 file changed

+14
-45
lines changed

compiler/rustc_middle/src/ty/context/tls.rs

+14-45
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ use crate::dep_graph::TaskDepsRef;
44
use crate::ty::query;
55
use rustc_data_structures::sync::{self, Lock};
66
use rustc_errors::Diagnostic;
7+
#[cfg(not(parallel_compiler))]
8+
use std::cell::Cell;
79
use std::mem;
810
use std::ptr;
911
use thin_vec::ThinVec;
@@ -47,52 +49,15 @@ impl<'a, 'tcx> ImplicitCtxt<'a, 'tcx> {
4749
}
4850
}
4951

52+
// Import the thread-local variable from Rayon, which is preserved for Rayon jobs.
5053
#[cfg(parallel_compiler)]
51-
mod tlv {
52-
use rustc_rayon_core as rayon_core;
53-
use std::ptr;
54-
55-
/// Gets Rayon's thread-local variable, which is preserved for Rayon jobs.
56-
/// This is used to get the pointer to the current `ImplicitCtxt`.
57-
#[inline]
58-
pub(super) fn get_tlv() -> *const () {
59-
ptr::from_exposed_addr(rayon_core::tlv::get())
60-
}
61-
62-
/// Sets Rayon's thread-local variable, which is preserved for Rayon jobs
63-
/// to `value` during the call to `f`. It is restored to its previous value after.
64-
/// This is used to set the pointer to the new `ImplicitCtxt`.
65-
#[inline]
66-
pub(super) fn with_tlv<F: FnOnce() -> R, R>(value: *const (), f: F) -> R {
67-
rayon_core::tlv::with(value.expose_addr(), f)
68-
}
69-
}
54+
use rustc_rayon_core::tlv::TLV;
7055

56+
// Otherwise define our own
7157
#[cfg(not(parallel_compiler))]
72-
mod tlv {
73-
use std::cell::Cell;
74-
use std::ptr;
75-
76-
thread_local! {
77-
/// A thread local variable that stores a pointer to the current `ImplicitCtxt`.
78-
static TLV: Cell<*const ()> = const { Cell::new(ptr::null()) };
79-
}
80-
81-
/// Gets the pointer to the current `ImplicitCtxt`.
82-
#[inline]
83-
pub(super) fn get_tlv() -> *const () {
84-
TLV.with(|tlv| tlv.get())
85-
}
86-
87-
/// Sets TLV to `value` during the call to `f`.
88-
/// It is restored to its previous value after.
89-
/// This is used to set the pointer to the new `ImplicitCtxt`.
90-
#[inline]
91-
pub(super) fn with_tlv<F: FnOnce() -> R, R>(value: *const (), f: F) -> R {
92-
let old = TLV.replace(value);
93-
let _reset = rustc_data_structures::OnDrop(move || TLV.set(old));
94-
f()
95-
}
58+
thread_local! {
59+
/// A thread local variable that stores a pointer to the current `ImplicitCtxt`.
60+
static TLV: Cell<*const ()> = const { Cell::new(ptr::null()) };
9661
}
9762

9863
#[inline]
@@ -111,7 +76,11 @@ pub fn enter_context<'a, 'tcx, F, R>(context: &ImplicitCtxt<'a, 'tcx>, f: F) ->
11176
where
11277
F: FnOnce() -> R,
11378
{
114-
tlv::with_tlv(erase(context), f)
79+
TLV.with(|tlv| {
80+
let old = tlv.replace(erase(context));
81+
let _reset = rustc_data_structures::OnDrop(move || tlv.set(old));
82+
f()
83+
})
11584
}
11685

11786
/// Allows access to the current `ImplicitCtxt` in a closure if one is available.
@@ -120,7 +89,7 @@ pub fn with_context_opt<F, R>(f: F) -> R
12089
where
12190
F: for<'a, 'tcx> FnOnce(Option<&ImplicitCtxt<'a, 'tcx>>) -> R,
12291
{
123-
let context = tlv::get_tlv();
92+
let context = TLV.get();
12493
if context.is_null() {
12594
f(None)
12695
} else {

0 commit comments

Comments
 (0)