From 1566273f482612fa5812ecb41d15a9c87a571465 Mon Sep 17 00:00:00 2001 From: Nilstrieb <48135649+Nilstrieb@users.noreply.github.com> Date: Mon, 10 Oct 2022 18:11:53 +0200 Subject: [PATCH 1/3] Remove unsued variable in query macro --- compiler/rustc_macros/src/query.rs | 7 ------- 1 file changed, 7 deletions(-) diff --git a/compiler/rustc_macros/src/query.rs b/compiler/rustc_macros/src/query.rs index d49926c90ca9f..8fd6830cbdae8 100644 --- a/compiler/rustc_macros/src/query.rs +++ b/compiler/rustc_macros/src/query.rs @@ -289,7 +289,6 @@ pub fn rustc_queries(input: TokenStream) -> TokenStream { let mut query_stream = quote! {}; let mut query_description_stream = quote! {}; - let mut cached_queries = quote! {}; for query in queries.0 { let Query { name, arg, modifiers, .. } = &query; @@ -299,12 +298,6 @@ pub fn rustc_queries(input: TokenStream) -> TokenStream { _ => quote! { #result_full }, }; - if modifiers.cache.is_some() { - cached_queries.extend(quote! { - #name, - }); - } - let mut attributes = Vec::new(); macro_rules! passthrough { From 167b3bd3b23bad1a8436f7a7a8637ec64d41acd6 Mon Sep 17 00:00:00 2001 From: Nilstrieb <48135649+Nilstrieb@users.noreply.github.com> Date: Mon, 10 Oct 2022 20:03:19 +0200 Subject: [PATCH 2/3] Get rid of `rustc_query_description!` Queries can provide an arbitrary expression for their description and their caching behavior. Before, these expressions where stored in a `rustc_query_description` macro emitted by the `rustc_queries` macro, and then used in `rustc_query_impl` to fill out the methods for the `QueryDescription` trait. Instead, we now emit two new modules from `rustc_queries` containing the functions with the expressions. `rustc_query_impl` calls these functions now instead of invoking the macro. Since we are now defining some of the functions in `rustc_middle::query`, we now need all the imports for the key types there as well. --- compiler/rustc_macros/src/query.rs | 45 ++++++++++++++--------- compiler/rustc_middle/src/query/mod.rs | 5 ++- compiler/rustc_middle/src/ty/print/mod.rs | 11 +++++- compiler/rustc_query_impl/src/lib.rs | 11 +----- compiler/rustc_query_impl/src/plumbing.rs | 9 ++++- 5 files changed, 51 insertions(+), 30 deletions(-) diff --git a/compiler/rustc_macros/src/query.rs b/compiler/rustc_macros/src/query.rs index 8fd6830cbdae8..7cefafef9d978 100644 --- a/compiler/rustc_macros/src/query.rs +++ b/compiler/rustc_macros/src/query.rs @@ -237,27 +237,32 @@ fn doc_comment_from_desc(list: &Punctuated) -> Result, #key: &Self::Key) -> bool { + pub fn #name<'tcx>(#tcx: TyCtxt<'tcx>, #key: &crate::ty::query::query_keys::#name<'tcx>) -> bool { #expr } } } else { quote! { + // we're taking `key` by reference, but some rustc types usually prefer being passed by value + #[allow(rustc::pass_by_value)] #[inline] - fn cache_on_disk(_: TyCtxt<'tcx>, _: &Self::Key) -> bool { + pub fn #name<'tcx>(_: TyCtxt<'tcx>, _: &crate::ty::query::query_keys::#name<'tcx>) -> bool { false } } @@ -268,19 +273,20 @@ fn add_query_description_impl(query: &Query, impls: &mut proc_macro2::TokenStrea let desc = quote! { #[allow(unused_variables)] - fn describe(tcx: QueryCtxt<'tcx>, key: Self::Key) -> String { - let (#tcx, #key) = (*tcx, key); + pub fn #name<'tcx>(tcx: TyCtxt<'tcx>, key: crate::ty::query::query_keys::#name<'tcx>) -> String { + let (#tcx, #key) = (tcx, key); ::rustc_middle::ty::print::with_no_trimmed_paths!( format!(#desc) ) } }; - impls.extend(quote! { - (#name) => { - #desc - #cache - }; + descs.extend(quote! { + #desc + }); + + cached.extend(quote! { + #cache }); } @@ -289,6 +295,7 @@ pub fn rustc_queries(input: TokenStream) -> TokenStream { let mut query_stream = quote! {}; let mut query_description_stream = quote! {}; + let mut query_cached_stream = quote! {}; for query in queries.0 { let Query { name, arg, modifiers, .. } = &query; @@ -343,7 +350,7 @@ pub fn rustc_queries(input: TokenStream) -> TokenStream { [#attribute_stream] fn #name(#arg) #result, }); - add_query_description_impl(&query, &mut query_description_stream); + add_query_desc_cached_impl(&query, &mut query_description_stream, &mut query_cached_stream); } TokenStream::from(quote! { @@ -357,9 +364,13 @@ pub fn rustc_queries(input: TokenStream) -> TokenStream { } } - #[macro_export] - macro_rules! rustc_query_description { + pub mod descs { + use super::*; #query_description_stream } + pub mod cached { + use super::*; + #query_cached_stream + } }) } diff --git a/compiler/rustc_middle/src/query/mod.rs b/compiler/rustc_middle/src/query/mod.rs index 06eb10c9137a1..cfdc21ac23430 100644 --- a/compiler/rustc_middle/src/query/mod.rs +++ b/compiler/rustc_middle/src/query/mod.rs @@ -4,6 +4,9 @@ //! ["Queries: demand-driven compilation"](https://rustc-dev-guide.rust-lang.org/query.html). //! This chapter includes instructions for adding new queries. +use crate::ty::{self, print::describe_as_module, TyCtxt}; +use rustc_span::def_id::LOCAL_CRATE; + // Each of these queries corresponds to a function pointer field in the // `Providers` struct for requesting a value of that type, and a method // on `tcx: TyCtxt` (and `tcx.at(span)`) for doing that request in a way @@ -1214,7 +1217,7 @@ rustc_queries! { desc { |tcx| "finding all vtable entries for trait {}", tcx.def_path_str(key.def_id()) } } - query vtable_trait_upcasting_coercion_new_vptr_slot(key: (ty::Ty<'tcx>, ty::Ty<'tcx>)) -> Option { + query vtable_trait_upcasting_coercion_new_vptr_slot(key: (Ty<'tcx>, Ty<'tcx>)) -> Option { desc { |tcx| "finding the slot within vtable for trait object {} vtable ptr during trait upcasting coercion from {} vtable", key.1, key.0 } } diff --git a/compiler/rustc_middle/src/ty/print/mod.rs b/compiler/rustc_middle/src/ty/print/mod.rs index e0a8d58f8a7f6..44b9548db89c8 100644 --- a/compiler/rustc_middle/src/ty/print/mod.rs +++ b/compiler/rustc_middle/src/ty/print/mod.rs @@ -3,7 +3,7 @@ use crate::ty::{self, DefIdTree, Ty, TyCtxt}; use rustc_data_structures::fx::FxHashSet; use rustc_data_structures::sso::SsoHashSet; -use rustc_hir::def_id::{CrateNum, DefId}; +use rustc_hir::def_id::{CrateNum, DefId, LocalDefId}; use rustc_hir::definitions::{DefPathData, DisambiguatedDefPathData}; // `pretty` is a separate module only for organization. @@ -325,3 +325,12 @@ impl<'tcx, P: Printer<'tcx>> Print<'tcx, P> for ty::Const<'tcx> { cx.print_const(*self) } } + +// This is only used by query descriptions +pub fn describe_as_module(def_id: LocalDefId, tcx: TyCtxt<'_>) -> String { + if def_id.is_top_level_module() { + "top-level module".to_string() + } else { + format!("module `{}`", tcx.def_path_str(def_id.to_def_id())) + } +} diff --git a/compiler/rustc_query_impl/src/lib.rs b/compiler/rustc_query_impl/src/lib.rs index 8e018d3e4a4bc..11d4c97e71ca0 100644 --- a/compiler/rustc_query_impl/src/lib.rs +++ b/compiler/rustc_query_impl/src/lib.rs @@ -22,8 +22,7 @@ use rustc_middle::arena::Arena; use rustc_middle::dep_graph::{self, DepKindStruct}; use rustc_middle::ty::query::{query_keys, query_storage, query_stored, query_values}; use rustc_middle::ty::query::{ExternProviders, Providers, QueryEngine}; -use rustc_middle::ty::{self, TyCtxt}; -use rustc_span::def_id::{LocalDefId, LOCAL_CRATE}; +use rustc_middle::ty::TyCtxt; use rustc_span::Span; #[macro_use] @@ -45,14 +44,6 @@ pub use on_disk_cache::OnDiskCache; mod profiling_support; pub use self::profiling_support::alloc_self_profile_query_strings; -fn describe_as_module(def_id: LocalDefId, tcx: TyCtxt<'_>) -> String { - if def_id.is_top_level_module() { - "top-level module".to_string() - } else { - format!("module `{}`", tcx.def_path_str(def_id.to_def_id())) - } -} - rustc_query_append! { define_queries! } impl<'tcx> Queries<'tcx> { diff --git a/compiler/rustc_query_impl/src/plumbing.rs b/compiler/rustc_query_impl/src/plumbing.rs index aaeaa3bd51d63..8f8a566d533ee 100644 --- a/compiler/rustc_query_impl/src/plumbing.rs +++ b/compiler/rustc_query_impl/src/plumbing.rs @@ -466,7 +466,14 @@ macro_rules! define_queries { } impl<'tcx> QueryDescription> for queries::$name<'tcx> { - rustc_query_description! { $name } + fn describe(tcx: QueryCtxt<'tcx>, key: Self::Key) -> String { + ::rustc_middle::query::descs::$name(tcx.tcx, key) + } + + #[inline] + fn cache_on_disk(tcx: TyCtxt<'tcx>, key: &Self::Key) -> bool { + ::rustc_middle::query::cached::$name(tcx, key) + } type Cache = query_storage::$name<'tcx>; From 24ce4cfa205da0afc8500684465bf8158bd4adae Mon Sep 17 00:00:00 2001 From: nils <48135649+Nilstrieb@users.noreply.github.com> Date: Thu, 13 Oct 2022 21:18:36 +0200 Subject: [PATCH 3/3] Remove the `describe` method from the `QueryDescription` trait It was called directly already, but now it's even more useless since it just forwards to the free function. Call it directly. --- compiler/rustc_query_impl/src/plumbing.rs | 12 ++++-------- compiler/rustc_query_system/src/query/config.rs | 2 -- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/compiler/rustc_query_impl/src/plumbing.rs b/compiler/rustc_query_impl/src/plumbing.rs index 8f8a566d533ee..1d17f4221969d 100644 --- a/compiler/rustc_query_impl/src/plumbing.rs +++ b/compiler/rustc_query_impl/src/plumbing.rs @@ -298,7 +298,7 @@ pub(crate) fn create_query_frame< K: Copy + Key + for<'a> HashStable>, >( tcx: QueryCtxt<'tcx>, - do_describe: fn(QueryCtxt<'tcx>, K) -> String, + do_describe: fn(TyCtxt<'tcx>, K) -> String, key: K, kind: DepKind, name: &'static str, @@ -307,7 +307,7 @@ pub(crate) fn create_query_frame< // Showing visible path instead of any path is not that important in production. let description = ty::print::with_no_visible_paths!( // Force filename-line mode to avoid invoking `type_of` query. - ty::print::with_forced_impl_filename_line!(do_describe(tcx, key)) + ty::print::with_forced_impl_filename_line!(do_describe(tcx.tcx, key)) ); let description = if tcx.sess.verbose() { format!("{} [{}]", description, name) } else { description }; @@ -466,10 +466,6 @@ macro_rules! define_queries { } impl<'tcx> QueryDescription> for queries::$name<'tcx> { - fn describe(tcx: QueryCtxt<'tcx>, key: Self::Key) -> String { - ::rustc_middle::query::descs::$name(tcx.tcx, key) - } - #[inline] fn cache_on_disk(tcx: TyCtxt<'tcx>, key: &Self::Key) -> bool { ::rustc_middle::query::cached::$name(tcx, key) @@ -583,7 +579,7 @@ macro_rules! define_queries { use rustc_middle::ty::TyCtxt; use $crate::plumbing::{QueryStruct, QueryCtxt}; use $crate::profiling_support::QueryKeyStringCache; - use rustc_query_system::query::{QueryDescription, QueryMap}; + use rustc_query_system::query::QueryMap; pub(super) const fn dummy_query_struct<'tcx>() -> QueryStruct<'tcx> { fn noop_try_collect_active_jobs(_: QueryCtxt<'_>, _: &mut QueryMap) -> Option<()> { @@ -610,7 +606,7 @@ macro_rules! define_queries { let make_query = |tcx, key| { let kind = rustc_middle::dep_graph::DepKind::$name; let name = stringify!($name); - $crate::plumbing::create_query_frame(tcx, super::queries::$name::describe, key, kind, name) + $crate::plumbing::create_query_frame(tcx, rustc_middle::query::descs::$name, key, kind, name) }; tcx.queries.$name.try_collect_active_jobs( tcx, diff --git a/compiler/rustc_query_system/src/query/config.rs b/compiler/rustc_query_system/src/query/config.rs index c4549cc9eb411..0a1cffa3b3331 100644 --- a/compiler/rustc_query_system/src/query/config.rs +++ b/compiler/rustc_query_system/src/query/config.rs @@ -49,8 +49,6 @@ impl QueryVTable { pub trait QueryDescription: QueryConfig { type Cache: QueryCache; - fn describe(tcx: CTX, key: Self::Key) -> String; - // Don't use this method to access query results, instead use the methods on TyCtxt fn query_state<'a>(tcx: CTX) -> &'a QueryState where