Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Further simplify the macros generated by rustc_queries #101173

Merged
merged 4 commits into from
Sep 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 7 additions & 22 deletions compiler/rustc_macros/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,6 @@ 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 cached_queries = quote! {};

for query in queries.0 {
Expand Down Expand Up @@ -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 cache 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
Expand All @@ -393,45 +396,27 @@ 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,
});

// 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! $( [$($other:tt)*] )?) => {
$macro! {
$( $($other)* )?
#query_stream
}
}
}
macro_rules! rustc_dep_node_append {
($macro:ident! [$($other:tt)*]) => {
$macro!(
$($other)*

#dep_node_def_stream
);
}
}
#[macro_export]
macro_rules! rustc_cached_queries {
( $macro:ident! ) => {
$macro!(#cached_queries);
}
}
#[macro_export]
macro_rules! rustc_query_description {
#query_description_stream
Expand Down
35 changes: 13 additions & 22 deletions compiler/rustc_middle/src/dep_graph/dep_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,9 @@ impl DepKind {

macro_rules! define_dep_nodes {
(
$(
[$($attrs:tt)*]
$variant:ident $(( $tuple_arg_ty:ty $(,)? ))*
,)*
) => (
$($(#[$attr:meta])*
[$($modifiers:tt)*] fn $variant:ident($($K:tt)*) -> $V:ty,)*) => {

#[macro_export]
macro_rules! make_dep_kind_array {
($mod:ident) => {[ $($mod::$variant()),* ]};
Expand All @@ -158,7 +156,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<DepKind, ()> {
Expand All @@ -176,24 +174,17 @@ macro_rules! define_dep_nodes {
pub const $variant: &str = stringify!($variant);
)*
}
);
};
}

rustc_dep_node_append!(define_dep_nodes![
// We use this for most things when incr. comp. is turned off.
[] 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),
rustc_query_append!(define_dep_nodes![
/// We use this for most things when incr. comp. is turned off.
[] fn Null() -> (),
/// We use this to create a forever-red node.
[] fn Red() -> (),
[] fn TraitSelect() -> (),
[] fn CompileCodegenUnit() -> (),
[] fn CompileMonoItem() -> (),
]);

// WARNING: `construct` is generic and does not know that `CompileCodegenUnit` takes `Symbol`s as keys.
Expand Down
20 changes: 16 additions & 4 deletions compiler/rustc_query_impl/src/plumbing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_query_impl/src/profiling_support.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,16 +307,16 @@ pub fn alloc_self_profile_query_strings(tcx: TyCtxt<'_>) {

macro_rules! alloc_once {
(
$($(#[$attr:meta])* [$($modifiers:tt)*] fn $name:ident($K:ty) -> $V:ty,)*
) => {
$({
$($(#[$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,
);
})*
)+
}
}

Expand Down