Skip to content

Commit 3e0cbd6

Browse files
committed
Auto merge of #152215 - nnethercote:start-cutting-down-rustc_query_system-AND-more, r=<try>
Remove `QueryDispatcher`
2 parents cf16cd9 + a8931b3 commit 3e0cbd6

11 files changed

Lines changed: 871 additions & 1005 deletions

File tree

compiler/rustc_middle/src/query/inner.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,33 @@
22
//! `tcx.$query(..)` and its variations.
33
44
use rustc_query_system::dep_graph::{DepKind, DepNodeKey};
5-
use rustc_query_system::query::{QueryCache, QueryMode, try_get_cached};
5+
use rustc_query_system::query::{QueryCache, QueryMode};
66
use rustc_span::{DUMMY_SP, ErrorGuaranteed, Span};
77

88
use crate::dep_graph;
99
use crate::query::erase::{self, Erasable, Erased};
1010
use crate::query::plumbing::QueryVTable;
1111
use crate::ty::TyCtxt;
1212

13+
/// Checks whether there is already a value for this key in the in-memory
14+
/// query cache, returning that value if present.
15+
///
16+
/// (Also performs some associated bookkeeping, if a value was found.)
17+
#[inline(always)]
18+
fn try_get_cached<'tcx, C>(tcx: TyCtxt<'tcx>, cache: &C, key: &C::Key) -> Option<C::Value>
19+
where
20+
C: QueryCache,
21+
{
22+
match cache.lookup(key) {
23+
Some((value, index)) => {
24+
tcx.prof.query_cache_hit(index.into());
25+
tcx.dep_graph.read_index(index);
26+
Some(value)
27+
}
28+
None => None,
29+
}
30+
}
31+
1332
/// Shared implementation of `tcx.$query(..)` and `tcx.at(span).$query(..)`
1433
/// for all queries.
1534
#[inline(always)]

compiler/rustc_query_impl/src/lib.rs

Lines changed: 32 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@
33
// tidy-alphabetical-start
44
#![allow(internal_features)]
55
#![feature(adt_const_params)]
6+
#![feature(core_intrinsics)]
67
#![feature(min_specialization)]
78
#![feature(rustc_attrs)]
89
// tidy-alphabetical-end
910

1011
use std::marker::ConstParamTy;
1112

12-
use rustc_data_structures::stable_hasher::HashStable;
1313
use rustc_data_structures::sync::AtomicU64;
1414
use rustc_middle::arena::Arena;
15-
use rustc_middle::dep_graph::{self, DepKind, DepKindVTable, DepNodeIndex};
15+
use rustc_middle::dep_graph::{self, DepKind, DepKindVTable, DepNode, DepNodeIndex};
1616
use rustc_middle::queries::{
1717
self, ExternProviders, Providers, QueryCaches, QueryEngine, QueryStates,
1818
};
@@ -22,10 +22,8 @@ use rustc_middle::query::plumbing::{QuerySystem, QuerySystemFns, QueryVTable};
2222
use rustc_middle::query::values::Value;
2323
use rustc_middle::ty::TyCtxt;
2424
use rustc_query_system::dep_graph::SerializedDepNodeIndex;
25-
use rustc_query_system::ich::StableHashingContext;
2625
use rustc_query_system::query::{
27-
CycleError, CycleErrorHandling, HashResult, QueryCache, QueryDispatcher, QueryMap, QueryMode,
28-
QueryState, get_query_incr, get_query_non_incr,
26+
CycleError, CycleErrorHandling, HashResult, QueryCache, QueryMap, QueryMode, QueryState,
2927
};
3028
use rustc_span::{ErrorGuaranteed, Span};
3129

@@ -52,8 +50,8 @@ struct QueryFlags {
5250
is_feedable: bool,
5351
}
5452

55-
/// Combines a [`QueryVTable`] with some additional compile-time booleans
56-
/// to implement [`QueryDispatcher`], for use by code in [`rustc_query_system`].
53+
/// Combines a [`QueryVTable`] with some additional compile-time booleans.
54+
/// "Dispatcher" should be understood as a near-synonym of "vtable".
5755
///
5856
/// Baking these boolean flags into the type gives a modest but measurable
5957
/// improvement to compiler perf and compiler code size; see
@@ -75,67 +73,60 @@ impl<'tcx, C: QueryCache, const FLAGS: QueryFlags> Clone
7573
}
7674
}
7775

78-
// This is `impl QueryDispatcher for SemiDynamicQueryDispatcher`.
79-
impl<'tcx, C: QueryCache, const FLAGS: QueryFlags> QueryDispatcher<'tcx>
80-
for SemiDynamicQueryDispatcher<'tcx, C, FLAGS>
81-
where
82-
for<'a> C::Key: HashStable<StableHashingContext<'a>>,
83-
{
84-
type Qcx = QueryCtxt<'tcx>;
85-
type Key = C::Key;
86-
type Value = C::Value;
87-
type Cache = C;
88-
76+
impl<'tcx, C: QueryCache, const FLAGS: QueryFlags> SemiDynamicQueryDispatcher<'tcx, C, FLAGS> {
8977
#[inline(always)]
9078
fn name(self) -> &'static str {
9179
self.vtable.name
9280
}
9381

9482
#[inline(always)]
95-
fn will_cache_on_disk_for_key(self, tcx: TyCtxt<'tcx>, key: &Self::Key) -> bool {
83+
fn will_cache_on_disk_for_key(self, tcx: TyCtxt<'tcx>, key: &C::Key) -> bool {
9684
self.vtable.will_cache_on_disk_for_key_fn.map_or(false, |f| f(tcx, key))
9785
}
9886

87+
// Don't use this method to access query results, instead use the methods on TyCtxt.
9988
#[inline(always)]
100-
fn query_state(self, qcx: QueryCtxt<'tcx>) -> &'tcx QueryState<'tcx, Self::Key> {
89+
fn query_state(self, qcx: QueryCtxt<'tcx>) -> &'tcx QueryState<'tcx, C::Key> {
10190
// Safety:
10291
// This is just manually doing the subfield referencing through pointer math.
10392
unsafe {
10493
&*(&qcx.tcx.query_system.states as *const QueryStates<'tcx>)
10594
.byte_add(self.vtable.query_state)
106-
.cast::<QueryState<'tcx, Self::Key>>()
95+
.cast::<QueryState<'tcx, C::Key>>()
10796
}
10897
}
10998

99+
// Don't use this method to access query results, instead use the methods on TyCtxt.
110100
#[inline(always)]
111-
fn query_cache(self, qcx: QueryCtxt<'tcx>) -> &'tcx Self::Cache {
101+
fn query_cache(self, qcx: QueryCtxt<'tcx>) -> &'tcx C {
112102
// Safety:
113103
// This is just manually doing the subfield referencing through pointer math.
114104
unsafe {
115105
&*(&qcx.tcx.query_system.caches as *const QueryCaches<'tcx>)
116106
.byte_add(self.vtable.query_cache)
117-
.cast::<Self::Cache>()
107+
.cast::<C>()
118108
}
119109
}
120110

111+
// Don't use this method to compute query results, instead use the methods on TyCtxt.
121112
#[inline(always)]
122-
fn execute_query(self, tcx: TyCtxt<'tcx>, key: Self::Key) -> Self::Value {
113+
fn execute_query(self, tcx: TyCtxt<'tcx>, key: C::Key) -> C::Value {
123114
(self.vtable.execute_query)(tcx, key)
124115
}
125116

126117
#[inline(always)]
127-
fn compute(self, qcx: QueryCtxt<'tcx>, key: Self::Key) -> Self::Value {
118+
fn compute(self, qcx: QueryCtxt<'tcx>, key: C::Key) -> C::Value {
128119
(self.vtable.compute_fn)(qcx.tcx, key)
129120
}
130121

131122
#[inline(always)]
132123
fn try_load_from_disk(
133124
self,
134125
qcx: QueryCtxt<'tcx>,
135-
key: &Self::Key,
126+
key: &C::Key,
136127
prev_index: SerializedDepNodeIndex,
137128
index: DepNodeIndex,
138-
) -> Option<Self::Value> {
129+
) -> Option<C::Value> {
139130
// `?` will return None immediately for queries that never cache to disk.
140131
self.vtable.try_load_from_disk_fn?(qcx.tcx, key, prev_index, index)
141132
}
@@ -144,23 +135,24 @@ where
144135
fn is_loadable_from_disk(
145136
self,
146137
qcx: QueryCtxt<'tcx>,
147-
key: &Self::Key,
138+
key: &C::Key,
148139
index: SerializedDepNodeIndex,
149140
) -> bool {
150141
self.vtable.is_loadable_from_disk_fn.map_or(false, |f| f(qcx.tcx, key, index))
151142
}
152143

144+
/// Synthesize an error value to let compilation continue after a cycle.
153145
fn value_from_cycle_error(
154146
self,
155147
tcx: TyCtxt<'tcx>,
156148
cycle_error: &CycleError,
157149
guar: ErrorGuaranteed,
158-
) -> Self::Value {
150+
) -> C::Value {
159151
(self.vtable.value_from_cycle_error)(tcx, cycle_error, guar)
160152
}
161153

162154
#[inline(always)]
163-
fn format_value(self) -> fn(&Self::Value) -> String {
155+
fn format_value(self) -> fn(&C::Value) -> String {
164156
self.vtable.format_value
165157
}
166158

@@ -195,13 +187,17 @@ where
195187
}
196188

197189
#[inline(always)]
198-
fn hash_result(self) -> HashResult<Self::Value> {
190+
fn hash_result(self) -> HashResult<C::Value> {
199191
self.vtable.hash_result
200192
}
193+
194+
fn construct_dep_node(self, tcx: TyCtxt<'tcx>, key: &C::Key) -> DepNode {
195+
DepNode::construct(tcx, self.dep_kind(), key)
196+
}
201197
}
202198

203199
/// Provides access to vtable-like operations for a query
204-
/// (by creating a [`QueryDispatcher`]),
200+
/// (by creating a [`SemiDynamicQueryDispatcher`]),
205201
/// but also keeps track of the "unerased" value type of the query
206202
/// (i.e. the actual result type in the query declaration).
207203
///
@@ -211,17 +207,15 @@ where
211207
///
212208
/// There is one macro-generated implementation of this trait for each query,
213209
/// on the type `rustc_query_impl::query_impl::$name::QueryType`.
214-
trait QueryDispatcherUnerased<'tcx> {
210+
trait QueryDispatcherUnerased<'tcx, C: QueryCache, const FLAGS: QueryFlags> {
215211
type UnerasedValue;
216-
type Dispatcher: QueryDispatcher<'tcx, Qcx = QueryCtxt<'tcx>>;
212+
//type Dispatcher: QueryDispatcher<'tcx, Qcx = QueryCtxt<'tcx>>; // njn: remove
217213

218214
const NAME: &'static &'static str;
219215

220-
fn query_dispatcher(tcx: TyCtxt<'tcx>) -> Self::Dispatcher;
216+
fn query_dispatcher(tcx: TyCtxt<'tcx>) -> SemiDynamicQueryDispatcher<'tcx, C, FLAGS>;
221217

222-
fn restore_val(
223-
value: <Self::Dispatcher as QueryDispatcher<'tcx>>::Value,
224-
) -> Self::UnerasedValue;
218+
fn restore_val(value: C::Value) -> Self::UnerasedValue;
225219
}
226220

227221
pub fn query_system<'tcx>(

0 commit comments

Comments
 (0)