Skip to content

Commit

Permalink
Auto merge of rust-lang#119966 - cjgillot:tls, r=<try>
Browse files Browse the repository at this point in the history
Move TLS to rustc_query_system

Revival of rust-lang#86038

r? `@ghost`
  • Loading branch information
bors committed Jan 14, 2024
2 parents 2730354 + 68e9415 commit affa73e
Show file tree
Hide file tree
Showing 16 changed files with 390 additions and 390 deletions.
4 changes: 2 additions & 2 deletions compiler/rustc_incremental/src/persist/save.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::errors;
use rustc_data_structures::fx::FxIndexMap;
use rustc_data_structures::sync::join;
use rustc_middle::dep_graph::{
DepGraph, SerializedDepGraph, WorkProduct, WorkProductId, WorkProductMap,
DepGraph, DepsType, SerializedDepGraph, WorkProduct, WorkProductId, WorkProductMap,
};
use rustc_middle::ty::TyCtxt;
use rustc_serialize::opaque::{FileEncodeResult, FileEncoder};
Expand Down Expand Up @@ -171,7 +171,7 @@ pub(crate) fn build_dep_graph(
// First encode the commandline arguments hash
sess.opts.dep_tracking_hash(false).encode(&mut encoder);

Some(DepGraph::new(
Some(DepGraph::new::<DepsType>(
&sess.prof,
prev_graph,
prev_work_products,
Expand Down
25 changes: 2 additions & 23 deletions compiler/rustc_interface/src/callbacks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
//! The functions in this file should fall back to the default set in their
//! origin crate when the `TyCtxt` is not present in TLS.

use rustc_errors::{Diagnostic, TRACK_DIAGNOSTIC};
use rustc_middle::dep_graph::{DepNodeExt, TaskDepsRef};
use rustc_middle::dep_graph::DepNodeExt;
use rustc_middle::ty::tls;
use rustc_query_system::dep_graph::dep_node::default_dep_kind_debug;
use rustc_query_system::dep_graph::{DepContext, DepKind, DepNode};
Expand All @@ -26,26 +25,6 @@ fn track_span_parent(def_id: rustc_span::def_id::LocalDefId) {
})
}

/// This is a callback from `rustc_errors` as it cannot access the implicit state
/// in `rustc_middle` otherwise. It is used when diagnostic messages are
/// emitted and stores them in the current query, if there is one.
fn track_diagnostic(diagnostic: Diagnostic, f: &mut dyn FnMut(Diagnostic)) {
tls::with_context_opt(|icx| {
if let Some(icx) = icx {
if let Some(diagnostics) = icx.diagnostics {
diagnostics.lock().extend(Some(diagnostic.clone()));
}

// Diagnostics are tracked, we can ignore the dependency.
let icx = tls::ImplicitCtxt { task_deps: TaskDepsRef::Ignore, ..icx.clone() };
return tls::enter_context(&icx, move || (*f)(diagnostic));
}

// In any other case, invoke diagnostics anyway.
(*f)(diagnostic);
})
}

/// This is a callback from `rustc_hir` as it cannot access the implicit state
/// in `rustc_middle` otherwise.
fn def_id_debug(def_id: rustc_hir::def_id::DefId, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Expand Down Expand Up @@ -103,5 +82,5 @@ pub fn setup_callbacks() {
.swap(&(dep_kind_debug as fn(_, &mut fmt::Formatter<'_>) -> _));
rustc_query_system::dep_graph::dep_node::DEP_NODE_DEBUG
.swap(&(dep_node_debug as fn(_, &mut fmt::Formatter<'_>) -> _));
TRACK_DIAGNOSTIC.swap(&(track_diagnostic as _));
rustc_errors::TRACK_DIAGNOSTIC.swap(&(rustc_query_system::tls::track_diagnostic as _));
}
7 changes: 3 additions & 4 deletions compiler/rustc_interface/src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -450,11 +450,10 @@ pub fn try_print_query_stack(
// Be careful relying on global state here: this code is called from
// a panic hook, which means that the global `DiagCtxt` may be in a weird
// state if it was responsible for triggering the panic.
let i = ty::tls::with_context_opt(|icx| {
if let Some(icx) = icx {
let i = ty::tls::with_opt(|tcx| {
if let Some(tcx) = tcx {
ty::print::with_no_queries!(print_query_stack(
QueryCtxt::new(icx.tcx),
icx.query,
QueryCtxt::new(tcx),
dcx,
num_frames,
file,
Expand Down
27 changes: 2 additions & 25 deletions compiler/rustc_middle/src/dep_graph/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::ty::{self, TyCtxt};
use crate::ty::TyCtxt;
use rustc_data_structures::profiling::SelfProfilerRef;
use rustc_query_system::ich::StableHashingContext;
use rustc_session::Session;
Expand All @@ -16,43 +16,20 @@ pub use rustc_query_system::dep_graph::{
pub use dep_node::{dep_kinds, label_strs, DepKind, DepNode, DepNodeExt};
pub(crate) use dep_node::{make_compile_codegen_unit, make_compile_mono_item};

pub type DepGraph = rustc_query_system::dep_graph::DepGraph<DepsType>;
pub type DepGraph = rustc_query_system::dep_graph::DepGraph;

pub type DepKindStruct<'tcx> = rustc_query_system::dep_graph::DepKindStruct<TyCtxt<'tcx>>;

#[derive(Clone)]
pub struct DepsType;

impl Deps for DepsType {
fn with_deps<OP, R>(task_deps: TaskDepsRef<'_>, op: OP) -> R
where
OP: FnOnce() -> R,
{
ty::tls::with_context(|icx| {
let icx = ty::tls::ImplicitCtxt { task_deps, ..icx.clone() };

ty::tls::enter_context(&icx, op)
})
}

fn read_deps<OP>(op: OP)
where
OP: for<'a> FnOnce(TaskDepsRef<'a>),
{
ty::tls::with_context_opt(|icx| {
let Some(icx) = icx else { return };
op(icx.task_deps)
})
}

const DEP_KIND_NULL: DepKind = dep_kinds::Null;
const DEP_KIND_RED: DepKind = dep_kinds::Red;
const DEP_KIND_MAX: u16 = dep_node::DEP_KIND_VARIANTS - 1;
}

impl<'tcx> DepContext for TyCtxt<'tcx> {
type Deps = DepsType;

#[inline]
fn with_stable_hashing_context<R>(self, f: impl FnOnce(StableHashingContext<'_>) -> R) -> R {
TyCtxt::with_stable_hashing_context(self, f)
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_middle/src/query/plumbing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_hir::hir_id::OwnerId;
use rustc_query_system::dep_graph::DepNodeIndex;
use rustc_query_system::dep_graph::SerializedDepNodeIndex;
pub(crate) use rustc_query_system::query::QueryJobId;
use rustc_query_system::query::*;
use rustc_query_system::HandleCycleError;
use rustc_span::{ErrorGuaranteed, Span, DUMMY_SP};
Expand Down
31 changes: 27 additions & 4 deletions compiler/rustc_middle/src/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

#![allow(rustc::usage_of_ty_tykind)]

pub mod tls;

use crate::arena::Arena;
use crate::dep_graph::{DepGraph, DepKindStruct};
use crate::infer::canonical::{CanonicalParamEnvCache, CanonicalVarInfo, CanonicalVarInfos};
Expand Down Expand Up @@ -662,15 +660,40 @@ impl<'tcx> GlobalCtxt<'tcx> {
where
F: FnOnce(TyCtxt<'tcx>) -> R,
{
let icx = tls::ImplicitCtxt::new(self);
tls::enter_context(&icx, || f(icx.tcx))
rustc_query_system::tls::create_and_enter_context(self, || f(TyCtxt { gcx: self }))
}

pub fn finish(&self) -> FileEncodeResult {
self.dep_graph.finish_encoding(&self.sess.prof)
}
}

pub mod tls {
use super::TyCtxt;

/// Allows access to the `TyCtxt` in the current `ImplicitCtxt`.
/// Panics if there is no `ImplicitCtxt` available.
#[inline]
pub fn with<F, R>(f: F) -> R
where
F: for<'tcx> FnOnce(TyCtxt<'tcx>) -> R,
{
rustc_query_system::tls::with(|gcx| f(TyCtxt { gcx: unsafe { &*gcx.cast() } }))
}

/// Allows access to the `TyCtxt` in the current `ImplicitCtxt`.
/// The closure is passed None if there is no `ImplicitCtxt` available.
#[inline]
pub fn with_opt<F, R>(f: F) -> R
where
F: for<'tcx> FnOnce(Option<TyCtxt<'tcx>>) -> R,
{
rustc_query_system::tls::with_opt(|opt_context| {
f(opt_context.map(|gcx| TyCtxt { gcx: unsafe { &*gcx.cast() } }))
})
}
}

impl<'tcx> TyCtxt<'tcx> {
/// Expects a body and returns its codegen attributes.
///
Expand Down
155 changes: 0 additions & 155 deletions compiler/rustc_middle/src/ty/context/tls.rs

This file was deleted.

Loading

0 comments on commit affa73e

Please sign in to comment.