Skip to content

Commit 1b9a035

Browse files
committed
Move rustc_query_system::query::job to rustc_middle.
This includes the types `QueryInfo`, `QueryJob`, `QueryJobId`, `QueryWaiter`, `QueryLatch`, and `QueryLatchInfo`. `CycleError` and `QueryStack*` had to come along too, due to type interdependencies. The `QueryStack*` types are put into a new submodule `rustc_middle::query::stack`.
1 parent e5b7f96 commit 1b9a035

15 files changed

Lines changed: 163 additions & 162 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4226,6 +4226,7 @@ dependencies = [
42264226
"bitflags",
42274227
"either",
42284228
"gsgdt",
4229+
"parking_lot",
42294230
"polonius-engine",
42304231
"rustc_abi",
42314232
"rustc_apfloat",
@@ -4512,20 +4513,17 @@ dependencies = [
45124513
name = "rustc_query_system"
45134514
version = "0.0.0"
45144515
dependencies = [
4515-
"parking_lot",
45164516
"rustc_abi",
45174517
"rustc_ast",
45184518
"rustc_data_structures",
45194519
"rustc_errors",
45204520
"rustc_feature",
4521-
"rustc_hashes",
45224521
"rustc_hir",
45234522
"rustc_index",
45244523
"rustc_macros",
45254524
"rustc_serialize",
45264525
"rustc_session",
45274526
"rustc_span",
4528-
"rustc_thread_pool",
45294527
"smallvec",
45304528
"tracing",
45314529
]

compiler/rustc_middle/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ edition = "2024"
88
bitflags = "2.4.1"
99
either = "1.5.0"
1010
gsgdt = "0.1.2"
11+
parking_lot = "0.12"
1112
polonius-engine = "0.13.0"
1213
rustc_abi = { path = "../rustc_abi" }
1314
rustc_apfloat = "0.2.0"

compiler/rustc_query_system/src/query/job.rs renamed to compiler/rustc_middle/src/query/job.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ use std::sync::Arc;
66
use parking_lot::{Condvar, Mutex};
77
use rustc_span::Span;
88

9-
use super::{QueryStackDeferred, QueryStackFrameExtra};
109
use crate::query::plumbing::CycleError;
11-
use crate::query::{QueryContext, QueryStackFrame};
10+
use crate::query::stack::{QueryStackDeferred, QueryStackFrame, QueryStackFrameExtra};
11+
use crate::ty::TyCtxt;
1212

1313
/// Represents a span and a query key.
1414
#[derive(Clone, Debug)]
@@ -98,13 +98,13 @@ impl<'tcx> QueryLatch<'tcx> {
9898
/// Awaits for the query job to complete.
9999
pub fn wait_on(
100100
&self,
101-
qcx: impl QueryContext<'tcx>,
101+
tcx: TyCtxt<'tcx>,
102102
query: Option<QueryJobId>,
103103
span: Span,
104104
) -> Result<(), CycleError<QueryStackDeferred<'tcx>>> {
105105
let waiter =
106106
Arc::new(QueryWaiter { query, span, cycle: Mutex::new(None), condvar: Condvar::new() });
107-
self.wait_on_inner(qcx, &waiter);
107+
self.wait_on_inner(tcx, &waiter);
108108
// FIXME: Get rid of this lock. We have ownership of the QueryWaiter
109109
// although another thread may still have a Arc reference so we cannot
110110
// use Arc::get_mut
@@ -116,7 +116,7 @@ impl<'tcx> QueryLatch<'tcx> {
116116
}
117117

118118
/// Awaits the caller on this latch by blocking the current thread.
119-
fn wait_on_inner(&self, qcx: impl QueryContext<'tcx>, waiter: &Arc<QueryWaiter<'tcx>>) {
119+
fn wait_on_inner(&self, tcx: TyCtxt<'tcx>, waiter: &Arc<QueryWaiter<'tcx>>) {
120120
let mut info = self.info.lock();
121121
if !info.complete {
122122
// We push the waiter on to the `waiters` list. It can be accessed inside
@@ -129,12 +129,11 @@ impl<'tcx> QueryLatch<'tcx> {
129129
// we have to be in the `wait` call. This is ensured by the deadlock handler
130130
// getting the self.info lock.
131131
rustc_thread_pool::mark_blocked();
132-
let proxy = qcx.jobserver_proxy();
133-
proxy.release_thread();
132+
tcx.jobserver_proxy.release_thread();
134133
waiter.condvar.wait(&mut info);
135134
// Release the lock before we potentially block in `acquire_thread`
136135
drop(info);
137-
proxy.acquire_thread();
136+
tcx.jobserver_proxy.acquire_thread();
138137
}
139138
}
140139

compiler/rustc_middle/src/query/mod.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,26 @@
11
use rustc_hir::def_id::LocalDefId;
22
pub use rustc_query_system::query::QueryMode;
33

4+
pub use self::job::{QueryInfo, QueryJob, QueryJobId, QueryLatch, QueryWaiter};
45
pub use self::keys::{AsLocalKey, Key, LocalCrate};
56
pub use self::plumbing::{
6-
ActiveKeyStatus, IntoQueryParam, QueryState, TyCtxtAt, TyCtxtEnsureDone, TyCtxtEnsureOk,
7+
ActiveKeyStatus, CycleError, IntoQueryParam, QueryState, TyCtxtAt, TyCtxtEnsureDone,
8+
TyCtxtEnsureOk,
79
};
10+
pub use self::stack::{QueryStackDeferred, QueryStackFrame, QueryStackFrameExtra};
811
pub use crate::queries::Providers;
912
use crate::ty::TyCtxt;
1013

1114
pub(crate) mod arena_cached;
1215
pub mod erase;
1316
pub(crate) mod inner;
17+
mod job;
1418
mod keys;
1519
pub mod on_disk_cache;
1620
#[macro_use]
1721
pub mod plumbing;
1822
pub(crate) mod modifiers;
23+
mod stack;
1924

2025
pub fn describe_as_module(def_id: impl Into<LocalDefId>, tcx: TyCtxt<'_>) -> String {
2126
let def_id = def_id.into();

compiler/rustc_middle/src/query/plumbing.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ use rustc_hir::hir_id::OwnerId;
99
use rustc_macros::HashStable;
1010
use rustc_query_system::dep_graph::{DepNodeIndex, SerializedDepNodeIndex};
1111
use rustc_query_system::ich::StableHashingContext;
12-
pub(crate) use rustc_query_system::query::QueryJobId;
13-
use rustc_query_system::query::{CycleError, CycleErrorHandling, QueryCache, QueryJob};
12+
use rustc_query_system::query::{CycleErrorHandling, QueryCache};
1413
use rustc_span::{ErrorGuaranteed, Span};
1514
pub use sealed::IntoQueryParam;
1615

@@ -20,6 +19,8 @@ use crate::queries::{
2019
ExternProviders, PerQueryVTables, Providers, QueryArenas, QueryCaches, QueryEngine, QueryStates,
2120
};
2221
use crate::query::on_disk_cache::{CacheEncoder, EncodedDepNodeIndex, OnDiskCache};
22+
use crate::query::stack::{QueryStackDeferred, QueryStackFrame, QueryStackFrameExtra};
23+
use crate::query::{QueryInfo, QueryJob};
2324
use crate::ty::TyCtxt;
2425

2526
/// For a particular query, keeps track of "active" keys, i.e. keys whose
@@ -67,6 +68,22 @@ pub type IsLoadableFromDiskFn<'tcx, Key> =
6768

6869
pub type HashResult<V> = Option<fn(&mut StableHashingContext<'_>, &V) -> Fingerprint>;
6970

71+
#[derive(Clone, Debug)]
72+
pub struct CycleError<I = QueryStackFrameExtra> {
73+
/// The query and related span that uses the cycle.
74+
pub usage: Option<(Span, QueryStackFrame<I>)>,
75+
pub cycle: Vec<QueryInfo<I>>,
76+
}
77+
78+
impl<'tcx> CycleError<QueryStackDeferred<'tcx>> {
79+
pub fn lift(&self) -> CycleError<QueryStackFrameExtra> {
80+
CycleError {
81+
usage: self.usage.as_ref().map(|(span, frame)| (*span, frame.lift())),
82+
cycle: self.cycle.iter().map(|info| info.lift()).collect(),
83+
}
84+
}
85+
}
86+
7087
/// Stores function pointers and other metadata for a particular query.
7188
///
7289
/// Used indirectly by query plumbing in `rustc_query_system` via a trait,
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
use std::fmt::Debug;
2+
use std::marker::PhantomData;
3+
use std::mem::transmute;
4+
use std::sync::Arc;
5+
6+
use rustc_data_structures::sync::{DynSend, DynSync};
7+
use rustc_hashes::Hash64;
8+
use rustc_hir::def::DefKind;
9+
use rustc_span::Span;
10+
use rustc_span::def_id::DefId;
11+
12+
use crate::dep_graph::DepKind;
13+
14+
/// Description of a frame in the query stack.
15+
///
16+
/// This is mostly used in case of cycles for error reporting.
17+
#[derive(Clone, Debug)]
18+
pub struct QueryStackFrame<I> {
19+
/// This field initially stores a `QueryStackDeferred` during collection,
20+
/// but can later be changed to `QueryStackFrameExtra` containing concrete information
21+
/// by calling `lift`. This is done so that collecting query does not need to invoke
22+
/// queries, instead `lift` will call queries in a more appropriate location.
23+
pub info: I,
24+
25+
pub dep_kind: DepKind,
26+
/// This hash is used to deterministically pick
27+
/// a query to remove cycles in the parallel compiler.
28+
pub hash: Hash64,
29+
pub def_id: Option<DefId>,
30+
/// A def-id that is extracted from a `Ty` in a query key
31+
pub def_id_for_ty_in_cycle: Option<DefId>,
32+
}
33+
34+
impl<'tcx> QueryStackFrame<QueryStackDeferred<'tcx>> {
35+
#[inline]
36+
pub fn new(
37+
info: QueryStackDeferred<'tcx>,
38+
dep_kind: DepKind,
39+
hash: Hash64,
40+
def_id: Option<DefId>,
41+
def_id_for_ty_in_cycle: Option<DefId>,
42+
) -> Self {
43+
Self { info, def_id, dep_kind, hash, def_id_for_ty_in_cycle }
44+
}
45+
46+
pub fn lift(&self) -> QueryStackFrame<QueryStackFrameExtra> {
47+
QueryStackFrame {
48+
info: self.info.extract(),
49+
dep_kind: self.dep_kind,
50+
hash: self.hash,
51+
def_id: self.def_id,
52+
def_id_for_ty_in_cycle: self.def_id_for_ty_in_cycle,
53+
}
54+
}
55+
}
56+
57+
#[derive(Clone, Debug)]
58+
pub struct QueryStackFrameExtra {
59+
pub description: String,
60+
pub span: Option<Span>,
61+
pub def_kind: Option<DefKind>,
62+
}
63+
64+
impl QueryStackFrameExtra {
65+
#[inline]
66+
pub fn new(description: String, span: Option<Span>, def_kind: Option<DefKind>) -> Self {
67+
Self { description, span, def_kind }
68+
}
69+
70+
// FIXME(eddyb) Get more valid `Span`s on queries.
71+
#[inline]
72+
pub fn default_span(&self, span: Span) -> Span {
73+
if !span.is_dummy() {
74+
return span;
75+
}
76+
self.span.unwrap_or(span)
77+
}
78+
}
79+
80+
/// Track a 'side effect' for a particular query.
81+
/// This is used to hold a closure which can create `QueryStackFrameExtra`.
82+
#[derive(Clone)]
83+
pub struct QueryStackDeferred<'tcx> {
84+
_dummy: PhantomData<&'tcx ()>,
85+
86+
// `extract` may contain references to 'tcx, but we can't tell drop checking that it won't
87+
// access it in the destructor.
88+
extract: Arc<dyn Fn() -> QueryStackFrameExtra + DynSync + DynSend>,
89+
}
90+
91+
impl<'tcx> QueryStackDeferred<'tcx> {
92+
pub fn new<C: Copy + DynSync + DynSend + 'tcx>(
93+
context: C,
94+
extract: fn(C) -> QueryStackFrameExtra,
95+
) -> Self {
96+
let extract: Arc<dyn Fn() -> QueryStackFrameExtra + DynSync + DynSend + 'tcx> =
97+
Arc::new(move || extract(context));
98+
// SAFETY: The `extract` closure does not access 'tcx in its destructor as the only
99+
// captured variable is `context` which is Copy and cannot have a destructor.
100+
Self { _dummy: PhantomData, extract: unsafe { transmute(extract) } }
101+
}
102+
103+
pub fn extract(&self) -> QueryStackFrameExtra {
104+
(self.extract)()
105+
}
106+
}
107+
108+
impl<'tcx> Debug for QueryStackDeferred<'tcx> {
109+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
110+
f.write_str("QueryStackDeferred")
111+
}
112+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use rustc_data_structures::sync;
44

55
use super::{GlobalCtxt, TyCtxt};
66
use crate::dep_graph::TaskDepsRef;
7-
use crate::query::plumbing::QueryJobId;
7+
use crate::query::QueryJobId;
88

99
/// This is the implicit state of rustc. It contains the current
1010
/// `TyCtxt` and query. It is updated when creating a local interner or

compiler/rustc_query_impl/src/execution.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ use rustc_data_structures::stack::ensure_sufficient_stack;
66
use rustc_data_structures::{outline, sharded, sync};
77
use rustc_errors::{Diag, FatalError, StashKey};
88
use rustc_middle::dep_graph::DepsType;
9-
use rustc_middle::query::{ActiveKeyStatus, QueryState};
9+
use rustc_middle::query::{
10+
ActiveKeyStatus, CycleError, QueryJob, QueryJobId, QueryLatch, QueryStackDeferred,
11+
QueryStackFrame, QueryState,
12+
};
1013
use rustc_middle::ty::TyCtxt;
1114
use rustc_query_system::dep_graph::{DepGraphData, DepNodeKey, HasDepContext};
1215
use rustc_query_system::query::{
13-
CycleError, CycleErrorHandling, QueryCache, QueryJob, QueryJobId, QueryLatch, QueryMode,
14-
QueryStackDeferred, QueryStackFrame, incremental_verify_ich,
16+
CycleErrorHandling, QueryCache, QueryMode, incremental_verify_ich,
1517
};
1618
use rustc_span::{DUMMY_SP, Span};
1719

@@ -239,7 +241,7 @@ fn wait_for_query<'tcx, C: QueryCache, const FLAGS: QueryFlags>(
239241

240242
// With parallel queries we might just have to wait on some other
241243
// thread.
242-
let result = latch.wait_on(qcx, current, span);
244+
let result = latch.wait_on(qcx.tcx, current, span);
243245

244246
match result {
245247
Ok(()) => {

compiler/rustc_query_impl/src/job.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::sync::Arc;
55
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
66
use rustc_errors::{Diag, DiagCtxtHandle};
77
use rustc_hir::def::DefKind;
8-
use rustc_query_system::query::{
8+
use rustc_middle::query::{
99
CycleError, QueryInfo, QueryJob, QueryJobId, QueryLatch, QueryStackDeferred, QueryStackFrame,
1010
QueryWaiter,
1111
};

compiler/rustc_query_impl/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ use rustc_middle::dep_graph::{self, DepKind, DepKindVTable, DepNode, DepNodeInde
1717
use rustc_middle::queries::{
1818
self, ExternProviders, Providers, QueryCaches, QueryEngine, QueryStates,
1919
};
20-
use rustc_middle::query::AsLocalKey;
2120
use rustc_middle::query::on_disk_cache::{CacheEncoder, EncodedDepNodeIndex, OnDiskCache};
2221
use rustc_middle::query::plumbing::{
2322
HashResult, QueryState, QuerySystem, QuerySystemFns, QueryVTable,
2423
};
24+
use rustc_middle::query::{AsLocalKey, CycleError};
2525
use rustc_middle::ty::TyCtxt;
2626
use rustc_query_system::dep_graph::SerializedDepNodeIndex;
27-
use rustc_query_system::query::{CycleError, CycleErrorHandling, QueryCache, QueryMode};
27+
use rustc_query_system::query::{CycleErrorHandling, QueryCache, QueryMode};
2828
use rustc_span::{ErrorGuaranteed, Span};
2929

3030
pub use crate::job::{QueryJobMap, break_query_cycles, print_query_stack};

0 commit comments

Comments
 (0)