From 05886e28a4c3fbb7bc22d56bf5a52ba7cfa491d9 Mon Sep 17 00:00:00 2001 From: Joshua Nelson Date: Mon, 29 Aug 2022 17:45:18 -0500 Subject: [PATCH 1/4] Further simplify the macros generated by `rustc_queries` - Add a new `rustc_query_names` macro. This allows a much simpler syntax for the matchers in the macros passed to it as a callback. - Convert `define_dep_nodes` and `alloc_once` to use `rustc_query_names`. This is possible because they only use the names (despite the quite complicated matchers in `define_dep_nodes`, none of the other arguments are used). - Get rid of `rustc_dep_node_append`. --- compiler/rustc_macros/src/query.rs | 22 +++++++---------- .../rustc_middle/src/dep_graph/dep_node.rs | 24 ++++++------------- .../rustc_query_impl/src/profiling_support.rs | 4 ++-- 3 files changed, 18 insertions(+), 32 deletions(-) diff --git a/compiler/rustc_macros/src/query.rs b/compiler/rustc_macros/src/query.rs index f93fe2d519508..2a1bb10fdfc82 100644 --- a/compiler/rustc_macros/src/query.rs +++ b/compiler/rustc_macros/src/query.rs @@ -328,7 +328,7 @@ pub fn rustc_queries(input: TokenStream) -> TokenStream { let mut query_stream = quote! {}; let mut query_description_stream = quote! {}; - let mut dep_node_def_stream = quote! {}; + let mut all_names = quote! {}; let mut cached_queries = quote! {}; for query in queries.0 { @@ -344,6 +344,7 @@ pub fn rustc_queries(input: TokenStream) -> TokenStream { #name, }); } + all_names.extend(quote! { #name, }); let mut attributes = Vec::new(); @@ -400,35 +401,30 @@ pub fn rustc_queries(input: TokenStream) -> TokenStream { [#attribute_stream] fn #name(#arg) #result, }); - // Create a dep node for the query - dep_node_def_stream.extend(quote! { - [#attribute_stream] #name(#arg), - }); - add_query_description_impl(&query, &mut query_description_stream); } TokenStream::from(quote! { #[macro_export] macro_rules! rustc_query_append { - ($macro:ident !) => { + ($macro:ident!) => { $macro! { #query_stream } } } - macro_rules! rustc_dep_node_append { - ($macro:ident! [$($other:tt)*]) => { + #[macro_export] + macro_rules! rustc_query_names { + ($macro:ident! $( [$($other:tt)*] )?) => { $macro!( - $($other)* - - #dep_node_def_stream + $( $($other)* )? + #all_names ); } } #[macro_export] macro_rules! rustc_cached_queries { - ( $macro:ident! ) => { + ($macro:ident!) => { $macro!(#cached_queries); } } diff --git a/compiler/rustc_middle/src/dep_graph/dep_node.rs b/compiler/rustc_middle/src/dep_graph/dep_node.rs index 7718906ac4ee7..f54d75c24e209 100644 --- a/compiler/rustc_middle/src/dep_graph/dep_node.rs +++ b/compiler/rustc_middle/src/dep_graph/dep_node.rs @@ -144,10 +144,7 @@ impl DepKind { macro_rules! define_dep_nodes { ( - $( - [$($attrs:tt)*] - $variant:ident $(( $tuple_arg_ty:ty $(,)? ))* - ,)* + $( $variant:ident, )* ) => ( #[macro_export] macro_rules! make_dep_kind_array { @@ -179,21 +176,14 @@ macro_rules! define_dep_nodes { ); } -rustc_dep_node_append!(define_dep_nodes![ +rustc_query_names!(define_dep_nodes![ // We use this for most things when incr. comp. is turned off. - [] Null, - + Null, // We use this to create a forever-red node. - [] Red, - - [anon] TraitSelect, - - // WARNING: if `Symbol` is changed, make sure you update `make_compile_codegen_unit` below. - [] CompileCodegenUnit(Symbol), - - // WARNING: if `MonoItem` is changed, make sure you update `make_compile_mono_item` below. - // Only used by rustc_codegen_cranelift - [] CompileMonoItem(MonoItem), + Red, + TraitSelect, + CompileCodegenUnit, + CompileMonoItem, ]); // WARNING: `construct` is generic and does not know that `CompileCodegenUnit` takes `Symbol`s as keys. diff --git a/compiler/rustc_query_impl/src/profiling_support.rs b/compiler/rustc_query_impl/src/profiling_support.rs index 260af0d540815..a8e0210e8d664 100644 --- a/compiler/rustc_query_impl/src/profiling_support.rs +++ b/compiler/rustc_query_impl/src/profiling_support.rs @@ -307,7 +307,7 @@ pub fn alloc_self_profile_query_strings(tcx: TyCtxt<'_>) { macro_rules! alloc_once { ( - $($(#[$attr:meta])* [$($modifiers:tt)*] fn $name:ident($K:ty) -> $V:ty,)* + $($name:ident,)* ) => { $({ alloc_self_profile_query_strings_for_query_cache( @@ -320,5 +320,5 @@ pub fn alloc_self_profile_query_strings(tcx: TyCtxt<'_>) { } } - rustc_query_append! { alloc_once! } + rustc_query_names! { alloc_once! } } From c630c87ceb0c49c5dc2b1a6119c67e9033dce828 Mon Sep 17 00:00:00 2001 From: Joshua Nelson Date: Mon, 29 Aug 2022 18:46:56 -0500 Subject: [PATCH 2/4] Support doc-comments in `define_dep_nodes` --- compiler/rustc_macros/src/query.rs | 8 ++++++-- compiler/rustc_middle/src/dep_graph/dep_node.rs | 8 ++++---- compiler/rustc_query_impl/src/profiling_support.rs | 4 ++-- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/compiler/rustc_macros/src/query.rs b/compiler/rustc_macros/src/query.rs index 2a1bb10fdfc82..046a144913e00 100644 --- a/compiler/rustc_macros/src/query.rs +++ b/compiler/rustc_macros/src/query.rs @@ -344,7 +344,6 @@ pub fn rustc_queries(input: TokenStream) -> TokenStream { #name, }); } - all_names.extend(quote! { #name, }); let mut attributes = Vec::new(); @@ -394,13 +393,18 @@ pub fn rustc_queries(input: TokenStream) -> TokenStream { // be very useful. let span = name.span(); let attribute_stream = quote_spanned! {span=> #(#attributes),*}; - let doc_comments = query.doc_comments.iter(); + let doc_comments = &query.doc_comments; // Add the query to the group query_stream.extend(quote! { #(#doc_comments)* [#attribute_stream] fn #name(#arg) #result, }); + all_names.extend(quote! { + #(#doc_comments)* + #name, + }); + add_query_description_impl(&query, &mut query_description_stream); } diff --git a/compiler/rustc_middle/src/dep_graph/dep_node.rs b/compiler/rustc_middle/src/dep_graph/dep_node.rs index f54d75c24e209..21d174af4444e 100644 --- a/compiler/rustc_middle/src/dep_graph/dep_node.rs +++ b/compiler/rustc_middle/src/dep_graph/dep_node.rs @@ -144,7 +144,7 @@ impl DepKind { macro_rules! define_dep_nodes { ( - $( $variant:ident, )* + $( $( #[$attr:meta] )* $variant:ident, )+ ) => ( #[macro_export] macro_rules! make_dep_kind_array { @@ -155,7 +155,7 @@ macro_rules! define_dep_nodes { #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Encodable, Decodable)] #[allow(non_camel_case_types)] pub enum DepKind { - $($variant),* + $( $( #[$attr] )* $variant),* } fn dep_kind_from_label_string(label: &str) -> Result { @@ -177,9 +177,9 @@ macro_rules! define_dep_nodes { } rustc_query_names!(define_dep_nodes![ - // We use this for most things when incr. comp. is turned off. + /// We use this for most things when incr. comp. is turned off. Null, - // We use this to create a forever-red node. + /// We use this to create a forever-red node. Red, TraitSelect, CompileCodegenUnit, diff --git a/compiler/rustc_query_impl/src/profiling_support.rs b/compiler/rustc_query_impl/src/profiling_support.rs index a8e0210e8d664..a9bb4756dbccc 100644 --- a/compiler/rustc_query_impl/src/profiling_support.rs +++ b/compiler/rustc_query_impl/src/profiling_support.rs @@ -307,7 +307,7 @@ pub fn alloc_self_profile_query_strings(tcx: TyCtxt<'_>) { macro_rules! alloc_once { ( - $($name:ident,)* + $( $( #[$attr:meta] )* $name:ident, )+ ) => { $({ alloc_self_profile_query_strings_for_query_cache( @@ -316,7 +316,7 @@ pub fn alloc_self_profile_query_strings(tcx: TyCtxt<'_>) { &tcx.query_caches.$name, &mut string_cache, ); - })* + })+ } } From 3a4e3c778889fdf2c4ccb7af26f1e81fc324e98b Mon Sep 17 00:00:00 2001 From: Joshua Nelson Date: Tue, 6 Sep 2022 21:26:02 -0500 Subject: [PATCH 3/4] Get rid of the emitted `rustc_query_names` and `rustc_cached_queries` macro We can avoid these by adding slightly more information to `rustc_query_append` instead. --- compiler/rustc_macros/src/query.rs | 29 +++++-------------- .../rustc_middle/src/dep_graph/dep_node.rs | 19 ++++++------ compiler/rustc_query_impl/src/plumbing.rs | 20 ++++++++++--- .../rustc_query_impl/src/profiling_support.rs | 10 +++---- 4 files changed, 38 insertions(+), 40 deletions(-) diff --git a/compiler/rustc_macros/src/query.rs b/compiler/rustc_macros/src/query.rs index 046a144913e00..505b2d62a79d3 100644 --- a/compiler/rustc_macros/src/query.rs +++ b/compiler/rustc_macros/src/query.rs @@ -328,7 +328,6 @@ pub fn rustc_queries(input: TokenStream) -> TokenStream { let mut query_stream = quote! {}; let mut query_description_stream = quote! {}; - let mut all_names = quote! {}; let mut cached_queries = quote! {}; for query in queries.0 { @@ -384,6 +383,10 @@ pub fn rustc_queries(input: TokenStream) -> TokenStream { if let Some(remap_env_constness) = &modifiers.remap_env_constness { attributes.push(quote! { (#remap_env_constness) }); } + // Pass on the const modifier + if modifiers.cache.is_some() { + attributes.push(quote! { (cache) }); + } // This uses the span of the query definition for the commas, // which can be important if we later encounter any ambiguity @@ -400,38 +403,20 @@ pub fn rustc_queries(input: TokenStream) -> TokenStream { [#attribute_stream] fn #name(#arg) #result, }); - all_names.extend(quote! { - #(#doc_comments)* - #name, - }); - add_query_description_impl(&query, &mut query_description_stream); } TokenStream::from(quote! { #[macro_export] macro_rules! rustc_query_append { - ($macro:ident!) => { + ($macro:ident! $( [$($other:tt)*] )?) => { $macro! { + $( $($other)* )? #query_stream } } } - #[macro_export] - macro_rules! rustc_query_names { - ($macro:ident! $( [$($other:tt)*] )?) => { - $macro!( - $( $($other)* )? - #all_names - ); - } - } - #[macro_export] - macro_rules! rustc_cached_queries { - ($macro:ident!) => { - $macro!(#cached_queries); - } - } + #[macro_export] macro_rules! rustc_query_description { #query_description_stream diff --git a/compiler/rustc_middle/src/dep_graph/dep_node.rs b/compiler/rustc_middle/src/dep_graph/dep_node.rs index 21d174af4444e..1fa0c6bababe4 100644 --- a/compiler/rustc_middle/src/dep_graph/dep_node.rs +++ b/compiler/rustc_middle/src/dep_graph/dep_node.rs @@ -144,8 +144,9 @@ impl DepKind { macro_rules! define_dep_nodes { ( - $( $( #[$attr:meta] )* $variant:ident, )+ - ) => ( + $($(#[$attr:meta])* + [$($modifiers:tt)*] fn $variant:ident($($K:tt)*) -> $V:ty,)*) => { + #[macro_export] macro_rules! make_dep_kind_array { ($mod:ident) => {[ $($mod::$variant()),* ]}; @@ -173,17 +174,17 @@ macro_rules! define_dep_nodes { pub const $variant: &str = stringify!($variant); )* } - ); + }; } -rustc_query_names!(define_dep_nodes![ +rustc_query_append!(define_dep_nodes![ /// We use this for most things when incr. comp. is turned off. - Null, + [] fn Null() -> (), /// We use this to create a forever-red node. - Red, - TraitSelect, - CompileCodegenUnit, - CompileMonoItem, + [] fn Red() -> (), + [] fn TraitSelect() -> (), + [] fn CompileCodegenUnit() -> (), + [] fn CompileMonoItem() -> (), ]); // WARNING: `construct` is generic and does not know that `CompileCodegenUnit` takes `Symbol`s as keys. diff --git a/compiler/rustc_query_impl/src/plumbing.rs b/compiler/rustc_query_impl/src/plumbing.rs index 274df5b5e5e94..7e48125307a22 100644 --- a/compiler/rustc_query_impl/src/plumbing.rs +++ b/compiler/rustc_query_impl/src/plumbing.rs @@ -148,19 +148,31 @@ impl<'tcx> QueryCtxt<'tcx> { encoder: &mut on_disk_cache::CacheEncoder<'_, 'tcx>, query_result_index: &mut on_disk_cache::EncodedDepNodeIndex, ) { + macro_rules! expand_if_cached { + ([] $encode:expr) => {}; + ([(cache) $($rest:tt)*] $encode:expr) => { + $encode + }; + ([$other:tt $($modifiers:tt)*] $encode:expr) => { + expand_if_cached!([$($modifiers)*] $encode) + }; + } + macro_rules! encode_queries { - ($($query:ident,)*) => { + ( + $($(#[$attr:meta])* + [$($modifiers:tt)*] fn $query:ident($($K:tt)*) -> $V:ty,)*) => { $( - on_disk_cache::encode_query_results::<_, super::queries::$query<'_>>( + expand_if_cached!([$($modifiers)*] on_disk_cache::encode_query_results::<_, super::queries::$query<'_>>( self, encoder, query_result_index - ); + )); )* } } - rustc_cached_queries!(encode_queries!); + rustc_query_append!(encode_queries!); } pub fn try_print_query_stack( diff --git a/compiler/rustc_query_impl/src/profiling_support.rs b/compiler/rustc_query_impl/src/profiling_support.rs index a9bb4756dbccc..98ec3bc097732 100644 --- a/compiler/rustc_query_impl/src/profiling_support.rs +++ b/compiler/rustc_query_impl/src/profiling_support.rs @@ -307,18 +307,18 @@ pub fn alloc_self_profile_query_strings(tcx: TyCtxt<'_>) { macro_rules! alloc_once { ( - $( $( #[$attr:meta] )* $name:ident, )+ - ) => { - $({ + $($(#[$attr:meta])* + [$($modifiers:tt)*] fn $name:ident($($K:tt)*) -> $V:ty,)*) => { + $( alloc_self_profile_query_strings_for_query_cache( tcx, stringify!($name), &tcx.query_caches.$name, &mut string_cache, ); - })+ + )+ } } - rustc_query_names! { alloc_once! } + rustc_query_append! { alloc_once! } } From cb2949e6425688078b6b3eb38d91f607ab280a16 Mon Sep 17 00:00:00 2001 From: Camille Gillot Date: Wed, 14 Sep 2022 19:11:53 +0200 Subject: [PATCH 4/4] Update compiler/rustc_macros/src/query.rs --- compiler/rustc_macros/src/query.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_macros/src/query.rs b/compiler/rustc_macros/src/query.rs index 505b2d62a79d3..0536eed6bbf49 100644 --- a/compiler/rustc_macros/src/query.rs +++ b/compiler/rustc_macros/src/query.rs @@ -383,7 +383,7 @@ pub fn rustc_queries(input: TokenStream) -> TokenStream { if let Some(remap_env_constness) = &modifiers.remap_env_constness { attributes.push(quote! { (#remap_env_constness) }); } - // Pass on the const modifier + // Pass on the cache modifier if modifiers.cache.is_some() { attributes.push(quote! { (cache) }); }