Skip to content

Commit e3dfeea

Browse files
committed
Auto merge of #108167 - Zoxc:query-config-instance-slim, r=cjgillot
Make `rustc_query_system` take `QueryConfig` by instance. This allows for easy switching between virtual tables and specialized instances for queries. It also has the benefit of less turbofish. `QueryStorage` has also been merged with `QueryCache`. Split out from #107937. r? `@cjgillot`
2 parents 1a521db + d1c8430 commit e3dfeea

File tree

7 files changed

+186
-126
lines changed

7 files changed

+186
-126
lines changed

compiler/rustc_query_impl/src/on_disk_cache.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use rustc_middle::mir::{self, interpret};
1313
use rustc_middle::ty::codec::{RefDecodable, TyDecoder, TyEncoder};
1414
use rustc_middle::ty::{self, Ty, TyCtxt};
1515
use rustc_query_system::dep_graph::DepContext;
16-
use rustc_query_system::query::{QueryCache, QueryContext, QuerySideEffects};
16+
use rustc_query_system::query::{QueryCache, QuerySideEffects};
1717
use rustc_serialize::{
1818
opaque::{FileEncodeResult, FileEncoder, IntEncodedWithFixedSize, MemDecoder},
1919
Decodable, Decoder, Encodable, Encoder,
@@ -1056,24 +1056,24 @@ impl<'a, 'tcx> Encodable<CacheEncoder<'a, 'tcx>> for [u8] {
10561056
}
10571057
}
10581058

1059-
pub fn encode_query_results<'a, 'tcx, CTX, Q>(
1060-
tcx: CTX,
1059+
pub fn encode_query_results<'a, 'tcx, Q>(
1060+
query: Q,
1061+
qcx: QueryCtxt<'tcx>,
10611062
encoder: &mut CacheEncoder<'a, 'tcx>,
10621063
query_result_index: &mut EncodedDepNodeIndex,
10631064
) where
1064-
CTX: QueryContext + 'tcx,
1065-
Q: super::QueryConfig<CTX>,
1065+
Q: super::QueryConfig<QueryCtxt<'tcx>>,
10661066
Q::Value: Encodable<CacheEncoder<'a, 'tcx>>,
10671067
{
1068-
let _timer = tcx
1069-
.dep_context()
1068+
let _timer = qcx
1069+
.tcx
10701070
.profiler()
1071-
.verbose_generic_activity_with_arg("encode_query_results_for", std::any::type_name::<Q>());
1071+
.verbose_generic_activity_with_arg("encode_query_results_for", query.name());
10721072

1073-
assert!(Q::query_state(tcx).all_inactive());
1074-
let cache = Q::query_cache(tcx);
1073+
assert!(query.query_state(qcx).all_inactive());
1074+
let cache = query.query_cache(qcx);
10751075
cache.iter(&mut |key, value, dep_node| {
1076-
if Q::cache_on_disk(*tcx.dep_context(), &key) {
1076+
if query.cache_on_disk(qcx.tcx, &key) {
10771077
let dep_node = SerializedDepNodeIndex::new(dep_node.index());
10781078

10791079
// Record position of the cache entry.

compiler/rustc_query_impl/src/plumbing.rs

+79-33
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use rustc_query_system::ich::StableHashingContext;
1919
use rustc_query_system::query::{
2020
force_query, QueryConfig, QueryContext, QueryJobId, QueryMap, QuerySideEffects, QueryStackFrame,
2121
};
22-
use rustc_query_system::{LayoutOfDepth, QueryOverflow, Value};
22+
use rustc_query_system::{LayoutOfDepth, QueryOverflow};
2323
use rustc_serialize::Decodable;
2424
use rustc_session::Limit;
2525
use rustc_span::def_id::LOCAL_CRATE;
@@ -350,18 +350,17 @@ pub(crate) fn create_query_frame<
350350
QueryStackFrame::new(description, span, def_id, def_kind, kind, ty_adt_id, hash)
351351
}
352352

353-
fn try_load_from_on_disk_cache<'tcx, Q>(tcx: TyCtxt<'tcx>, dep_node: DepNode)
353+
fn try_load_from_on_disk_cache<'tcx, Q>(query: Q, tcx: TyCtxt<'tcx>, dep_node: DepNode)
354354
where
355355
Q: QueryConfig<QueryCtxt<'tcx>>,
356-
Q::Key: DepNodeParams<TyCtxt<'tcx>>,
357356
{
358357
debug_assert!(tcx.dep_graph.is_green(&dep_node));
359358

360359
let key = Q::Key::recover(tcx, &dep_node).unwrap_or_else(|| {
361360
panic!("Failed to recover key for {:?} with hash {}", dep_node, dep_node.hash)
362361
});
363-
if Q::cache_on_disk(tcx, &key) {
364-
let _ = Q::execute_query(tcx, key);
362+
if query.cache_on_disk(tcx, &key) {
363+
let _ = query.execute_query(tcx, key);
365364
}
366365
}
367366

@@ -375,11 +374,9 @@ where
375374
tcx.on_disk_cache().as_ref()?.try_load_query_result(*tcx, id)
376375
}
377376

378-
fn force_from_dep_node<'tcx, Q>(tcx: TyCtxt<'tcx>, dep_node: DepNode) -> bool
377+
fn force_from_dep_node<'tcx, Q>(query: Q, tcx: TyCtxt<'tcx>, dep_node: DepNode) -> bool
379378
where
380379
Q: QueryConfig<QueryCtxt<'tcx>>,
381-
Q::Key: DepNodeParams<TyCtxt<'tcx>>,
382-
Q::Value: Value<TyCtxt<'tcx>, DepKind>,
383380
{
384381
// We must avoid ever having to call `force_from_dep_node()` for a
385382
// `DepNode::codegen_unit`:
@@ -403,7 +400,7 @@ where
403400
#[cfg(debug_assertions)]
404401
let _guard = tracing::span!(tracing::Level::TRACE, stringify!($name), ?key).entered();
405402
let tcx = QueryCtxt::from_tcx(tcx);
406-
force_query::<Q, _, DepKind>(tcx, key, dep_node);
403+
force_query(query, tcx, key, dep_node);
407404
true
408405
} else {
409406
false
@@ -412,7 +409,7 @@ where
412409

413410
pub(crate) fn query_callback<'tcx, Q>(is_anon: bool, is_eval_always: bool) -> DepKindStruct<'tcx>
414411
where
415-
Q: QueryConfig<QueryCtxt<'tcx>>,
412+
Q: QueryConfig<QueryCtxt<'tcx>> + Default,
416413
Q::Key: DepNodeParams<TyCtxt<'tcx>>,
417414
{
418415
let fingerprint_style = Q::Key::fingerprint_style();
@@ -431,8 +428,10 @@ where
431428
is_anon,
432429
is_eval_always,
433430
fingerprint_style,
434-
force_from_dep_node: Some(force_from_dep_node::<Q>),
435-
try_load_from_on_disk_cache: Some(try_load_from_on_disk_cache::<Q>),
431+
force_from_dep_node: Some(|tcx, dep_node| force_from_dep_node(Q::default(), tcx, dep_node)),
432+
try_load_from_on_disk_cache: Some(|tcx, dep_node| {
433+
try_load_from_on_disk_cache(Q::default(), tcx, dep_node)
434+
}),
436435
}
437436
}
438437

@@ -462,54 +461,65 @@ macro_rules! define_queries {
462461
mod queries {
463462
use std::marker::PhantomData;
464463

465-
$(pub struct $name<'tcx> {
466-
data: PhantomData<&'tcx ()>
467-
})*
464+
$(
465+
#[derive(Copy, Clone, Default)]
466+
pub struct $name<'tcx> {
467+
data: PhantomData<&'tcx ()>
468+
}
469+
)*
468470
}
469471

470472
$(impl<'tcx> QueryConfig<QueryCtxt<'tcx>> for queries::$name<'tcx> {
471473
type Key = query_keys::$name<'tcx>;
472474
type Value = query_values::$name<'tcx>;
473-
const NAME: &'static str = stringify!($name);
475+
476+
#[inline(always)]
477+
fn name(self) -> &'static str {
478+
stringify!($name)
479+
}
474480

475481
#[inline]
476-
fn cache_on_disk(tcx: TyCtxt<'tcx>, key: &Self::Key) -> bool {
482+
fn cache_on_disk(self, tcx: TyCtxt<'tcx>, key: &Self::Key) -> bool {
477483
::rustc_middle::query::cached::$name(tcx, key)
478484
}
479485

480486
type Cache = query_storage::$name<'tcx>;
481487

482488
#[inline(always)]
483-
fn query_state<'a>(tcx: QueryCtxt<'tcx>) -> &'a QueryState<Self::Key, crate::dep_graph::DepKind>
489+
fn query_state<'a>(self, tcx: QueryCtxt<'tcx>) -> &'a QueryState<Self::Key, crate::dep_graph::DepKind>
484490
where QueryCtxt<'tcx>: 'a
485491
{
486492
&tcx.queries.$name
487493
}
488494

489495
#[inline(always)]
490-
fn query_cache<'a>(tcx: QueryCtxt<'tcx>) -> &'a Self::Cache
496+
fn query_cache<'a>(self, tcx: QueryCtxt<'tcx>) -> &'a Self::Cache
491497
where 'tcx:'a
492498
{
493499
&tcx.query_system.caches.$name
494500
}
495501

496-
fn execute_query(tcx: TyCtxt<'tcx>, key: Self::Key) -> Self::Value {
502+
fn execute_query(self, tcx: TyCtxt<'tcx>, key: Self::Key) -> Self::Value {
497503
tcx.$name(key)
498504
}
499505

500506
#[inline]
501507
#[allow(unused_variables)]
502-
fn compute(qcx: QueryCtxt<'tcx>, key: Self::Key) -> Self::Value {
508+
fn compute(self, qcx: QueryCtxt<'tcx>, key: Self::Key) -> Self::Value {
503509
query_provided_to_value::$name(
504510
qcx.tcx,
505511
get_provider!([$($modifiers)*][qcx, $name, key])(qcx.tcx, key)
506512
)
507513
}
508514

509515
#[inline]
510-
fn try_load_from_disk(_qcx: QueryCtxt<'tcx>, _key: &Self::Key) -> rustc_query_system::query::TryLoadFromDisk<QueryCtxt<'tcx>, Self> {
516+
fn try_load_from_disk(
517+
self,
518+
_qcx: QueryCtxt<'tcx>,
519+
_key: &Self::Key
520+
) -> rustc_query_system::query::TryLoadFromDisk<QueryCtxt<'tcx>, Self::Value> {
511521
should_ever_cache_on_disk!([$($modifiers)*] {
512-
if Self::cache_on_disk(_qcx.tcx, _key) {
522+
if ::rustc_middle::query::cached::$name(_qcx.tcx, _key) {
513523
Some(|qcx: QueryCtxt<'tcx>, dep_node| {
514524
let value = $crate::plumbing::try_load_from_disk::<query_provided::$name<'tcx>>(
515525
qcx,
@@ -525,15 +535,40 @@ macro_rules! define_queries {
525535
})
526536
}
527537

528-
const ANON: bool = is_anon!([$($modifiers)*]);
529-
const EVAL_ALWAYS: bool = is_eval_always!([$($modifiers)*]);
530-
const DEPTH_LIMIT: bool = depth_limit!([$($modifiers)*]);
531-
const FEEDABLE: bool = feedable!([$($modifiers)*]);
538+
#[inline(always)]
539+
fn anon(self) -> bool {
540+
is_anon!([$($modifiers)*])
541+
}
542+
543+
#[inline(always)]
544+
fn eval_always(self) -> bool {
545+
is_eval_always!([$($modifiers)*])
546+
}
547+
548+
#[inline(always)]
549+
fn depth_limit(self) -> bool {
550+
depth_limit!([$($modifiers)*])
551+
}
552+
553+
#[inline(always)]
554+
fn feedable(self) -> bool {
555+
feedable!([$($modifiers)*])
556+
}
532557

533-
const DEP_KIND: rustc_middle::dep_graph::DepKind = dep_graph::DepKind::$name;
534-
const HANDLE_CYCLE_ERROR: rustc_query_system::HandleCycleError = handle_cycle_error!([$($modifiers)*]);
558+
#[inline(always)]
559+
fn dep_kind(self) -> rustc_middle::dep_graph::DepKind {
560+
dep_graph::DepKind::$name
561+
}
535562

536-
const HASH_RESULT: rustc_query_system::query::HashResult<QueryCtxt<'tcx>, Self> = hash_result!([$($modifiers)*]);
563+
#[inline(always)]
564+
fn handle_cycle_error(self) -> rustc_query_system::HandleCycleError {
565+
handle_cycle_error!([$($modifiers)*])
566+
}
567+
568+
#[inline(always)]
569+
fn hash_result(self) -> rustc_query_system::query::HashResult<Self::Value> {
570+
hash_result!([$($modifiers)*])
571+
}
537572
})*
538573

539574
#[allow(nonstandard_style)]
@@ -649,8 +684,13 @@ macro_rules! define_queries {
649684
string_cache,
650685
)
651686
},
652-
encode_query_results: expand_if_cached!([$($modifiers)*], |tcx, encoder, query_result_index|
653-
$crate::on_disk_cache::encode_query_results::<_, super::queries::$name<'_>>(tcx, encoder, query_result_index)
687+
encode_query_results: expand_if_cached!([$($modifiers)*], |qcx, encoder, query_result_index|
688+
$crate::on_disk_cache::encode_query_results(
689+
super::queries::$name::default(),
690+
qcx,
691+
encoder,
692+
query_result_index,
693+
)
654694
),
655695
}})*
656696
}
@@ -739,7 +779,13 @@ macro_rules! define_queries_struct {
739779
mode: QueryMode,
740780
) -> Option<query_values::$name<'tcx>> {
741781
let qcx = QueryCtxt { tcx, queries: self };
742-
get_query::<queries::$name<'tcx>, _, rustc_middle::dep_graph::DepKind>(qcx, span, key, mode)
782+
get_query(
783+
queries::$name::default(),
784+
qcx,
785+
span,
786+
key,
787+
mode
788+
)
743789
})*
744790
}
745791
};

compiler/rustc_query_system/src/dep_graph/mod.rs

+9
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,15 @@ impl<T: DepContext> HasDepContext for T {
8888
}
8989
}
9090

91+
impl<T: HasDepContext, Q: Copy> HasDepContext for (T, Q) {
92+
type DepKind = T::DepKind;
93+
type DepContext = T::DepContext;
94+
95+
fn dep_context(&self) -> &Self::DepContext {
96+
self.0.dep_context()
97+
}
98+
}
99+
91100
/// Describes the contents of the fingerprint generated by a given query.
92101
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
93102
pub enum FingerprintStyle {

compiler/rustc_query_system/src/query/caches.rs

+5-17
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,9 @@ pub trait CacheSelector<'tcx, V> {
1616
V: Copy;
1717
}
1818

19-
pub trait QueryStorage {
20-
type Value: Copy;
21-
}
22-
23-
pub trait QueryCache: QueryStorage + Sized {
19+
pub trait QueryCache: Sized {
2420
type Key: Hash + Eq + Copy + Debug;
21+
type Value: Copy + Debug;
2522

2623
/// Checks if the query is already computed and in the cache.
2724
/// It returns the shard index and a lock guard to the shard,
@@ -55,16 +52,13 @@ impl<K, V> Default for DefaultCache<K, V> {
5552
}
5653
}
5754

58-
impl<K: Eq + Hash, V: Copy + Debug> QueryStorage for DefaultCache<K, V> {
59-
type Value = V;
60-
}
61-
6255
impl<K, V> QueryCache for DefaultCache<K, V>
6356
where
6457
K: Eq + Hash + Copy + Debug,
6558
V: Copy + Debug,
6659
{
6760
type Key = K;
61+
type Value = V;
6862

6963
#[inline(always)]
7064
fn lookup(&self, key: &K) -> Option<(V, DepNodeIndex)> {
@@ -127,15 +121,12 @@ impl<V> Default for SingleCache<V> {
127121
}
128122
}
129123

130-
impl<V: Copy + Debug> QueryStorage for SingleCache<V> {
131-
type Value = V;
132-
}
133-
134124
impl<V> QueryCache for SingleCache<V>
135125
where
136126
V: Copy + Debug,
137127
{
138128
type Key = ();
129+
type Value = V;
139130

140131
#[inline(always)]
141132
fn lookup(&self, _key: &()) -> Option<(V, DepNodeIndex)> {
@@ -173,16 +164,13 @@ impl<K: Idx, V> Default for VecCache<K, V> {
173164
}
174165
}
175166

176-
impl<K: Eq + Idx, V: Copy + Debug> QueryStorage for VecCache<K, V> {
177-
type Value = V;
178-
}
179-
180167
impl<K, V> QueryCache for VecCache<K, V>
181168
where
182169
K: Eq + Idx + Copy + Debug,
183170
V: Copy + Debug,
184171
{
185172
type Key = K;
173+
type Value = V;
186174

187175
#[inline(always)]
188176
fn lookup(&self, key: &K) -> Option<(V, DepNodeIndex)> {

0 commit comments

Comments
 (0)