From 3fd7c4a17d5c2b56dcd10adc8ee692abcc3be7f9 Mon Sep 17 00:00:00 2001 From: John Kåre Alsaker Date: Tue, 7 Feb 2023 08:32:30 +0100 Subject: Make `rustc_query_system` take `QueryConfig` by instance. --- compiler/rustc_query_impl/src/on_disk_cache.rs | 22 ++--- compiler/rustc_query_impl/src/plumbing.rs | 120 ++++++++++++++++++------- 2 files changed, 98 insertions(+), 44 deletions(-) (limited to 'compiler/rustc_query_impl') diff --git a/compiler/rustc_query_impl/src/on_disk_cache.rs b/compiler/rustc_query_impl/src/on_disk_cache.rs index 70c481fb0ee..0e7b268f25d 100644 --- a/compiler/rustc_query_impl/src/on_disk_cache.rs +++ b/compiler/rustc_query_impl/src/on_disk_cache.rs @@ -13,7 +13,7 @@ use rustc_middle::mir::{self, interpret}; use rustc_middle::ty::codec::{RefDecodable, TyDecoder, TyEncoder}; use rustc_middle::ty::{self, Ty, TyCtxt}; use rustc_query_system::dep_graph::DepContext; -use rustc_query_system::query::{QueryCache, QueryContext, QuerySideEffects}; +use rustc_query_system::query::{QueryCache, QuerySideEffects}; use rustc_serialize::{ opaque::{FileEncodeResult, FileEncoder, IntEncodedWithFixedSize, MemDecoder}, Decodable, Decoder, Encodable, Encoder, @@ -1056,24 +1056,24 @@ impl<'a, 'tcx> Encodable> for [u8] { } } -pub fn encode_query_results<'a, 'tcx, CTX, Q>( - tcx: CTX, +pub fn encode_query_results<'a, 'tcx, Q>( + query: Q, + qcx: QueryCtxt<'tcx>, encoder: &mut CacheEncoder<'a, 'tcx>, query_result_index: &mut EncodedDepNodeIndex, ) where - CTX: QueryContext + 'tcx, - Q: super::QueryConfig, + Q: super::QueryConfig>, Q::Value: Encodable>, { - let _timer = tcx - .dep_context() + let _timer = qcx + .tcx .profiler() - .verbose_generic_activity_with_arg("encode_query_results_for", std::any::type_name::()); + .verbose_generic_activity_with_arg("encode_query_results_for", query.name()); - assert!(Q::query_state(tcx).all_inactive()); - let cache = Q::query_cache(tcx); + assert!(query.query_state(qcx).all_inactive()); + let cache = query.query_cache(qcx); cache.iter(&mut |key, value, dep_node| { - if Q::cache_on_disk(*tcx.dep_context(), &key) { + if query.cache_on_disk(qcx.tcx, &key) { let dep_node = SerializedDepNodeIndex::new(dep_node.index()); // Record position of the cache entry. diff --git a/compiler/rustc_query_impl/src/plumbing.rs b/compiler/rustc_query_impl/src/plumbing.rs index a8592bd7086..c37dcc048d2 100644 --- a/compiler/rustc_query_impl/src/plumbing.rs +++ b/compiler/rustc_query_impl/src/plumbing.rs @@ -19,7 +19,7 @@ use rustc_query_system::ich::StableHashingContext; use rustc_query_system::query::{ force_query, QueryConfig, QueryContext, QueryJobId, QueryMap, QuerySideEffects, QueryStackFrame, }; -use rustc_query_system::{LayoutOfDepth, QueryOverflow, Value}; +use rustc_query_system::{LayoutOfDepth, QueryOverflow}; use rustc_serialize::Decodable; use rustc_session::Limit; use rustc_span::def_id::LOCAL_CRATE; @@ -350,18 +350,17 @@ pub(crate) fn create_query_frame< QueryStackFrame::new(description, span, def_id, def_kind, kind, ty_adt_id, hash) } -fn try_load_from_on_disk_cache<'tcx, Q>(tcx: TyCtxt<'tcx>, dep_node: DepNode) +fn try_load_from_on_disk_cache<'tcx, Q>(query: Q, tcx: TyCtxt<'tcx>, dep_node: DepNode) where Q: QueryConfig>, - Q::Key: DepNodeParams>, { debug_assert!(tcx.dep_graph.is_green(&dep_node)); let key = Q::Key::recover(tcx, &dep_node).unwrap_or_else(|| { panic!("Failed to recover key for {:?} with hash {}", dep_node, dep_node.hash) }); - if Q::cache_on_disk(tcx, &key) { - let _ = Q::execute_query(tcx, key); + if query.cache_on_disk(tcx, &key) { + let _ = query.execute_query(tcx, key); } } @@ -375,11 +374,9 @@ where tcx.on_disk_cache().as_ref()?.try_load_query_result(*tcx, id) } -fn force_from_dep_node<'tcx, Q>(tcx: TyCtxt<'tcx>, dep_node: DepNode) -> bool +fn force_from_dep_node<'tcx, Q>(query: Q, tcx: TyCtxt<'tcx>, dep_node: DepNode) -> bool where Q: QueryConfig>, - Q::Key: DepNodeParams>, - Q::Value: Value, DepKind>, { // We must avoid ever having to call `force_from_dep_node()` for a // `DepNode::codegen_unit`: @@ -403,7 +400,7 @@ where #[cfg(debug_assertions)] let _guard = tracing::span!(tracing::Level::TRACE, stringify!($name), ?key).entered(); let tcx = QueryCtxt::from_tcx(tcx); - force_query::(tcx, key, dep_node); + force_query(query, tcx, key, dep_node); true } else { false @@ -412,7 +409,7 @@ where pub(crate) fn query_callback<'tcx, Q>(is_anon: bool, is_eval_always: bool) -> DepKindStruct<'tcx> where - Q: QueryConfig>, + Q: QueryConfig> + Default, Q::Key: DepNodeParams>, { let fingerprint_style = Q::Key::fingerprint_style(); @@ -431,8 +428,10 @@ where is_anon, is_eval_always, fingerprint_style, - force_from_dep_node: Some(force_from_dep_node::), - try_load_from_on_disk_cache: Some(try_load_from_on_disk_cache::), + force_from_dep_node: Some(|tcx, dep_node| force_from_dep_node(Q::default(), tcx, dep_node)), + try_load_from_on_disk_cache: Some(|tcx, dep_node| { + try_load_from_on_disk_cache(Q::default(), tcx, dep_node) + }), } } @@ -462,44 +461,59 @@ macro_rules! define_queries { mod queries { use std::marker::PhantomData; - $(pub struct $name<'tcx> { - data: PhantomData<&'tcx ()> - })* + $( + #[derive(Copy, Clone, Debug)] + pub struct $name<'tcx> { + data: PhantomData<&'tcx ()> + } + + impl Default for $name<'_> { + fn default() -> Self { + Self { + data: PhantomData, + } + } + } + )* } $(impl<'tcx> QueryConfig> for queries::$name<'tcx> { type Key = query_keys::$name<'tcx>; type Value = query_values::$name<'tcx>; - const NAME: &'static str = stringify!($name); + + #[inline(always)] + fn name(self) -> &'static str { + stringify!($name) + } #[inline] - fn cache_on_disk(tcx: TyCtxt<'tcx>, key: &Self::Key) -> bool { + fn cache_on_disk(self, tcx: TyCtxt<'tcx>, key: &Self::Key) -> bool { ::rustc_middle::query::cached::$name(tcx, key) } type Cache = query_storage::$name<'tcx>; #[inline(always)] - fn query_state<'a>(tcx: QueryCtxt<'tcx>) -> &'a QueryState + fn query_state<'a>(self, tcx: QueryCtxt<'tcx>) -> &'a QueryState where QueryCtxt<'tcx>: 'a { &tcx.queries.$name } #[inline(always)] - fn query_cache<'a>(tcx: QueryCtxt<'tcx>) -> &'a Self::Cache + fn query_cache<'a>(self, tcx: QueryCtxt<'tcx>) -> &'a Self::Cache where 'tcx:'a { &tcx.query_system.caches.$name } - fn execute_query(tcx: TyCtxt<'tcx>, key: Self::Key) -> Self::Value { + fn execute_query(self, tcx: TyCtxt<'tcx>, key: Self::Key) -> Self::Value { tcx.$name(key) } #[inline] #[allow(unused_variables)] - fn compute(qcx: QueryCtxt<'tcx>, key: Self::Key) -> Self::Value { + fn compute(self, qcx: QueryCtxt<'tcx>, key: Self::Key) -> Self::Value { query_provided_to_value::$name( qcx.tcx, get_provider!([$($modifiers)*][qcx, $name, key])(qcx.tcx, key) @@ -507,9 +521,13 @@ macro_rules! define_queries { } #[inline] - fn try_load_from_disk(_qcx: QueryCtxt<'tcx>, _key: &Self::Key) -> rustc_query_system::query::TryLoadFromDisk, Self> { + fn try_load_from_disk( + self, + _qcx: QueryCtxt<'tcx>, + _key: &Self::Key + ) -> rustc_query_system::query::TryLoadFromDisk, Self::Value> { should_ever_cache_on_disk!([$($modifiers)*] { - if Self::cache_on_disk(_qcx.tcx, _key) { + if ::rustc_middle::query::cached::$name(_qcx.tcx, _key) { Some(|qcx: QueryCtxt<'tcx>, dep_node| { let value = $crate::plumbing::try_load_from_disk::>( qcx, @@ -525,15 +543,40 @@ macro_rules! define_queries { }) } - const ANON: bool = is_anon!([$($modifiers)*]); - const EVAL_ALWAYS: bool = is_eval_always!([$($modifiers)*]); - const DEPTH_LIMIT: bool = depth_limit!([$($modifiers)*]); - const FEEDABLE: bool = feedable!([$($modifiers)*]); + #[inline(always)] + fn anon(self) -> bool { + is_anon!([$($modifiers)*]) + } - const DEP_KIND: rustc_middle::dep_graph::DepKind = dep_graph::DepKind::$name; - const HANDLE_CYCLE_ERROR: rustc_query_system::HandleCycleError = handle_cycle_error!([$($modifiers)*]); + #[inline(always)] + fn eval_always(self) -> bool { + is_eval_always!([$($modifiers)*]) + } - const HASH_RESULT: rustc_query_system::query::HashResult, Self> = hash_result!([$($modifiers)*]); + #[inline(always)] + fn depth_limit(self) -> bool { + depth_limit!([$($modifiers)*]) + } + + #[inline(always)] + fn feedable(self) -> bool { + feedable!([$($modifiers)*]) + } + + #[inline(always)] + fn dep_kind(self) -> rustc_middle::dep_graph::DepKind { + dep_graph::DepKind::$name + } + + #[inline(always)] + fn handle_cycle_error(self) -> rustc_query_system::HandleCycleError { + handle_cycle_error!([$($modifiers)*]) + } + + #[inline(always)] + fn hash_result(self) -> rustc_query_system::query::HashResult { + hash_result!([$($modifiers)*]) + } })* #[allow(nonstandard_style)] @@ -649,8 +692,13 @@ macro_rules! define_queries { string_cache, ) }, - encode_query_results: expand_if_cached!([$($modifiers)*], |tcx, encoder, query_result_index| - $crate::on_disk_cache::encode_query_results::<_, super::queries::$name<'_>>(tcx, encoder, query_result_index) + encode_query_results: expand_if_cached!([$($modifiers)*], |qcx, encoder, query_result_index| + $crate::on_disk_cache::encode_query_results( + super::queries::$name::default(), + qcx, + encoder, + query_result_index, + ) ), }})* } @@ -739,7 +787,13 @@ macro_rules! define_queries_struct { mode: QueryMode, ) -> Option> { let qcx = QueryCtxt { tcx, queries: self }; - get_query::, _, rustc_middle::dep_graph::DepKind>(qcx, span, key, mode) + get_query( + queries::$name::default(), + qcx, + span, + key, + mode + ) })* } }; -- cgit 1.4.1-3-g733a5 From 3b26d71e04fd61deb42656b28371e097ab35c4aa Mon Sep 17 00:00:00 2001 From: John Kåre Alsaker Date: Sat, 25 Feb 2023 22:15:30 +0100 Subject: Avoid implementing Debug for QueryConfig --- compiler/rustc_query_impl/src/plumbing.rs | 2 +- compiler/rustc_query_system/src/dep_graph/mod.rs | 9 +++++++++ compiler/rustc_query_system/src/query/config.rs | 2 +- compiler/rustc_query_system/src/query/plumbing.rs | 6 +++--- 4 files changed, 14 insertions(+), 5 deletions(-) (limited to 'compiler/rustc_query_impl') diff --git a/compiler/rustc_query_impl/src/plumbing.rs b/compiler/rustc_query_impl/src/plumbing.rs index c37dcc048d2..dfe172fc3ff 100644 --- a/compiler/rustc_query_impl/src/plumbing.rs +++ b/compiler/rustc_query_impl/src/plumbing.rs @@ -462,7 +462,7 @@ macro_rules! define_queries { use std::marker::PhantomData; $( - #[derive(Copy, Clone, Debug)] + #[derive(Copy, Clone)] pub struct $name<'tcx> { data: PhantomData<&'tcx ()> } diff --git a/compiler/rustc_query_system/src/dep_graph/mod.rs b/compiler/rustc_query_system/src/dep_graph/mod.rs index 6969f2dbef8..ba83b775631 100644 --- a/compiler/rustc_query_system/src/dep_graph/mod.rs +++ b/compiler/rustc_query_system/src/dep_graph/mod.rs @@ -88,6 +88,15 @@ impl HasDepContext for T { } } +impl HasDepContext for (T, Q) { + type DepKind = T::DepKind; + type DepContext = T::DepContext; + + fn dep_context(&self) -> &Self::DepContext { + self.0.dep_context() + } +} + /// Describes the contents of the fingerprint generated by a given query. #[derive(Debug, PartialEq, Eq, Copy, Clone)] pub enum FingerprintStyle { diff --git a/compiler/rustc_query_system/src/query/config.rs b/compiler/rustc_query_system/src/query/config.rs index 939f509e994..e44a00ca6cb 100644 --- a/compiler/rustc_query_system/src/query/config.rs +++ b/compiler/rustc_query_system/src/query/config.rs @@ -14,7 +14,7 @@ pub type HashResult = Option, &V) -> Fingerp pub type TryLoadFromDisk = Option Option>; -pub trait QueryConfig: Copy + Debug { +pub trait QueryConfig: Copy { fn name(self) -> &'static str; // `Key` and `Value` are `Copy` instead of `Clone` to ensure copying them stays cheap, diff --git a/compiler/rustc_query_system/src/query/plumbing.rs b/compiler/rustc_query_system/src/query/plumbing.rs index b801951ae74..6eecda09a32 100644 --- a/compiler/rustc_query_system/src/query/plumbing.rs +++ b/compiler/rustc_query_system/src/query/plumbing.rs @@ -468,9 +468,9 @@ where dep_graph.with_task( dep_node, - qcx, - (key, query), - |qcx, (key, query)| query.compute(qcx, key), + (qcx, query), + key, + |(qcx, query), key| query.compute(qcx, key), query.hash_result(), ) }); -- cgit 1.4.1-3-g733a5 From d1c8430a34c01a5db8d5339b8a9cffa8a41002be Mon Sep 17 00:00:00 2001 From: John Kåre Alsaker Date: Sun, 26 Feb 2023 23:45:29 +0100 Subject: Derive Default for query structs --- compiler/rustc_query_impl/src/plumbing.rs | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) (limited to 'compiler/rustc_query_impl') diff --git a/compiler/rustc_query_impl/src/plumbing.rs b/compiler/rustc_query_impl/src/plumbing.rs index dfe172fc3ff..005ce16dbb9 100644 --- a/compiler/rustc_query_impl/src/plumbing.rs +++ b/compiler/rustc_query_impl/src/plumbing.rs @@ -462,18 +462,10 @@ macro_rules! define_queries { use std::marker::PhantomData; $( - #[derive(Copy, Clone)] + #[derive(Copy, Clone, Default)] pub struct $name<'tcx> { data: PhantomData<&'tcx ()> } - - impl Default for $name<'_> { - fn default() -> Self { - Self { - data: PhantomData, - } - } - } )* } -- cgit 1.4.1-3-g733a5