Skip to content

Commit 4d72939

Browse files
Rollup merge of #77830 - cjgillot:remacro, r=oli-obk
Simplify query proc-macros The query code generation is split between proc-macros and regular macros in `rustc_middle::ty::query`. This PR removes unused capabilities of the proc-macros, and tend to use regular macros for the logic.
2 parents e34263d + 57ba8ed commit 4d72939

File tree

6 files changed

+72
-113
lines changed

6 files changed

+72
-113
lines changed

compiler/rustc_data_structures/src/profiling.rs

-11
Original file line numberDiff line numberDiff line change
@@ -111,17 +111,6 @@ cfg_if! {
111111

112112
type Profiler = measureme::Profiler<SerializationSink>;
113113

114-
#[derive(Clone, Copy, Debug, PartialEq, Eq, Ord, PartialOrd)]
115-
pub enum ProfileCategory {
116-
Parsing,
117-
Expansion,
118-
TypeChecking,
119-
BorrowChecking,
120-
Codegen,
121-
Linking,
122-
Other,
123-
}
124-
125114
bitflags::bitflags! {
126115
struct EventFilter: u32 {
127116
const GENERIC_ACTIVITIES = 1 << 0;

compiler/rustc_macros/src/query.rs

+6-66
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,11 @@ impl<T: Parse> Parse for List<T> {
190190
}
191191

192192
/// A named group containing queries.
193+
///
194+
/// For now, the name is not used any more, but the capability remains interesting for future
195+
/// developments of the query system.
193196
struct Group {
197+
#[allow(unused)]
194198
name: Ident,
195199
queries: List<Query>,
196200
}
@@ -417,12 +421,9 @@ pub fn rustc_queries(input: TokenStream) -> TokenStream {
417421
let mut query_stream = quote! {};
418422
let mut query_description_stream = quote! {};
419423
let mut dep_node_def_stream = quote! {};
420-
let mut dep_node_force_stream = quote! {};
421-
let mut try_load_from_on_disk_cache_stream = quote! {};
422424
let mut cached_queries = quote! {};
423425

424426
for group in groups.0 {
425-
let mut group_stream = quote! {};
426427
for mut query in group.queries.0 {
427428
let modifiers = process_modifiers(&mut query);
428429
let name = &query.name;
@@ -437,22 +438,6 @@ pub fn rustc_queries(input: TokenStream) -> TokenStream {
437438
cached_queries.extend(quote! {
438439
#name,
439440
});
440-
441-
try_load_from_on_disk_cache_stream.extend(quote! {
442-
::rustc_middle::dep_graph::DepKind::#name => {
443-
if <#arg as DepNodeParams<TyCtxt<'_>>>::can_reconstruct_query_key() {
444-
debug_assert!($tcx.dep_graph
445-
.node_color($dep_node)
446-
.map(|c| c.is_green())
447-
.unwrap_or(false));
448-
449-
let key = <#arg as DepNodeParams<TyCtxt<'_>>>::recover($tcx, $dep_node).unwrap();
450-
if queries::#name::cache_on_disk($tcx, &key, None) {
451-
let _ = $tcx.#name(key);
452-
}
453-
}
454-
}
455-
});
456441
}
457442

458443
let mut attributes = Vec::new();
@@ -485,47 +470,20 @@ pub fn rustc_queries(input: TokenStream) -> TokenStream {
485470
let attribute_stream = quote! {#(#attributes),*};
486471
let doc_comments = query.doc_comments.iter();
487472
// Add the query to the group
488-
group_stream.extend(quote! {
473+
query_stream.extend(quote! {
489474
#(#doc_comments)*
490-
[#attribute_stream] fn #name: #name(#arg) #result,
475+
[#attribute_stream] fn #name(#arg) #result,
491476
});
492477

493478
// Create a dep node for the query
494479
dep_node_def_stream.extend(quote! {
495480
[#attribute_stream] #name(#arg),
496481
});
497482

498-
// Add a match arm to force the query given the dep node
499-
dep_node_force_stream.extend(quote! {
500-
::rustc_middle::dep_graph::DepKind::#name => {
501-
if <#arg as DepNodeParams<TyCtxt<'_>>>::can_reconstruct_query_key() {
502-
if let Some(key) = <#arg as DepNodeParams<TyCtxt<'_>>>::recover($tcx, $dep_node) {
503-
force_query::<crate::ty::query::queries::#name<'_>, _>(
504-
$tcx,
505-
key,
506-
DUMMY_SP,
507-
*$dep_node
508-
);
509-
return true;
510-
}
511-
}
512-
}
513-
});
514-
515483
add_query_description_impl(&query, modifiers, &mut query_description_stream);
516484
}
517-
let name = &group.name;
518-
query_stream.extend(quote! {
519-
#name { #group_stream },
520-
});
521485
}
522486

523-
dep_node_force_stream.extend(quote! {
524-
::rustc_middle::dep_graph::DepKind::Null => {
525-
bug!("Cannot force dep node: {:?}", $dep_node)
526-
}
527-
});
528-
529487
TokenStream::from(quote! {
530488
macro_rules! rustc_query_append {
531489
([$($macro:tt)*][$($other:tt)*]) => {
@@ -546,30 +504,12 @@ pub fn rustc_queries(input: TokenStream) -> TokenStream {
546504
);
547505
}
548506
}
549-
macro_rules! rustc_dep_node_force {
550-
([$dep_node:expr, $tcx:expr] $($other:tt)*) => {
551-
match $dep_node.kind {
552-
$($other)*
553-
554-
#dep_node_force_stream
555-
}
556-
}
557-
}
558507
macro_rules! rustc_cached_queries {
559508
($($macro:tt)*) => {
560509
$($macro)*(#cached_queries);
561510
}
562511
}
563512

564513
#query_description_stream
565-
566-
macro_rules! rustc_dep_node_try_load_from_on_disk_cache {
567-
($dep_node:expr, $tcx:expr) => {
568-
match $dep_node.kind {
569-
#try_load_from_on_disk_cache_stream
570-
_ => (),
571-
}
572-
}
573-
}
574514
})
575515
}

compiler/rustc_middle/src/ty/query/mod.rs

+57-13
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ use crate::ty::util::AlwaysRequiresDrop;
3434
use crate::ty::{self, AdtSizedConstraint, CrateInherentImpls, ParamEnvAnd, Ty, TyCtxt};
3535
use rustc_data_structures::fingerprint::Fingerprint;
3636
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap};
37-
use rustc_data_structures::profiling::ProfileCategory::*;
3837
use rustc_data_structures::stable_hasher::StableVec;
3938
use rustc_data_structures::svh::Svh;
4039
use rustc_data_structures::sync::Lrc;
@@ -169,26 +168,71 @@ pub fn force_from_dep_node<'tcx>(tcx: TyCtxt<'tcx>, dep_node: &DepNode) -> bool
169168
return false;
170169
}
171170

172-
rustc_dep_node_force!([dep_node, tcx]
173-
// These are inputs that are expected to be pre-allocated and that
174-
// should therefore always be red or green already.
175-
DepKind::CrateMetadata |
171+
macro_rules! force_from_dep_node {
172+
($($(#[$attr:meta])* [$($modifiers:tt)*] $name:ident($K:ty),)*) => {
173+
match dep_node.kind {
174+
// These are inputs that are expected to be pre-allocated and that
175+
// should therefore always be red or green already.
176+
DepKind::CrateMetadata |
176177

177-
// These are anonymous nodes.
178-
DepKind::TraitSelect |
178+
// These are anonymous nodes.
179+
DepKind::TraitSelect |
179180

180-
// We don't have enough information to reconstruct the query key of
181-
// these.
182-
DepKind::CompileCodegenUnit => {
183-
bug!("force_from_dep_node: encountered {:?}", dep_node)
181+
// We don't have enough information to reconstruct the query key of
182+
// these.
183+
DepKind::CompileCodegenUnit |
184+
185+
// Forcing this makes no sense.
186+
DepKind::Null => {
187+
bug!("force_from_dep_node: encountered {:?}", dep_node)
188+
}
189+
190+
$(DepKind::$name => {
191+
debug_assert!(<$K as DepNodeParams<TyCtxt<'_>>>::can_reconstruct_query_key());
192+
193+
if let Some(key) = <$K as DepNodeParams<TyCtxt<'_>>>::recover(tcx, dep_node) {
194+
force_query::<queries::$name<'_>, _>(
195+
tcx,
196+
key,
197+
DUMMY_SP,
198+
*dep_node
199+
);
200+
return true;
201+
}
202+
})*
203+
}
184204
}
185-
);
205+
}
206+
207+
rustc_dep_node_append! { [force_from_dep_node!][] }
186208

187209
false
188210
}
189211

190212
pub(crate) fn try_load_from_on_disk_cache<'tcx>(tcx: TyCtxt<'tcx>, dep_node: &DepNode) {
191-
rustc_dep_node_try_load_from_on_disk_cache!(dep_node, tcx)
213+
macro_rules! try_load_from_on_disk_cache {
214+
($($name:ident,)*) => {
215+
match dep_node.kind {
216+
$(DepKind::$name => {
217+
if <query_keys::$name<'tcx> as DepNodeParams<TyCtxt<'_>>>::can_reconstruct_query_key() {
218+
debug_assert!(tcx.dep_graph
219+
.node_color(dep_node)
220+
.map(|c| c.is_green())
221+
.unwrap_or(false));
222+
223+
let key = <query_keys::$name<'tcx> as DepNodeParams<TyCtxt<'_>>>::recover(tcx, dep_node).unwrap();
224+
if queries::$name::cache_on_disk(tcx, &key, None) {
225+
let _ = tcx.$name(key);
226+
}
227+
}
228+
})*
229+
230+
_ => (),
231+
}
232+
}
233+
}
234+
235+
rustc_cached_queries!(try_load_from_on_disk_cache!);
192236
}
193237

194238
mod sealed {

compiler/rustc_middle/src/ty/query/plumbing.rs

+4-16
Original file line numberDiff line numberDiff line change
@@ -242,33 +242,22 @@ macro_rules! hash_result {
242242
};
243243
}
244244

245-
macro_rules! define_queries {
246-
(<$tcx:tt> $($category:tt {
247-
$($(#[$attr:meta])* [$($modifiers:tt)*] fn $name:ident: $node:ident($($K:tt)*) -> $V:ty,)*
248-
},)*) => {
249-
define_queries_inner! { <$tcx>
250-
$($( $(#[$attr])* category<$category> [$($modifiers)*] fn $name: $node($($K)*) -> $V,)*)*
251-
}
252-
}
253-
}
254-
255245
macro_rules! query_helper_param_ty {
256246
(DefId) => { impl IntoQueryParam<DefId> };
257247
($K:ty) => { $K };
258248
}
259249

260-
macro_rules! define_queries_inner {
250+
macro_rules! define_queries {
261251
(<$tcx:tt>
262-
$($(#[$attr:meta])* category<$category:tt>
263-
[$($modifiers:tt)*] fn $name:ident: $node:ident($($K:tt)*) -> $V:ty,)*) => {
252+
$($(#[$attr:meta])*
253+
[$($modifiers:tt)*] fn $name:ident($($K:tt)*) -> $V:ty,)*) => {
264254

265255
use std::mem;
266256
use crate::{
267257
rustc_data_structures::stable_hasher::HashStable,
268258
rustc_data_structures::stable_hasher::StableHasher,
269259
ich::StableHashingContext
270260
};
271-
use rustc_data_structures::profiling::ProfileCategory;
272261

273262
define_queries_struct! {
274263
tcx: $tcx,
@@ -362,13 +351,12 @@ macro_rules! define_queries_inner {
362351
as QueryStorage
363352
>::Stored;
364353
const NAME: &'static str = stringify!($name);
365-
const CATEGORY: ProfileCategory = $category;
366354
}
367355

368356
impl<$tcx> QueryAccessors<TyCtxt<$tcx>> for queries::$name<$tcx> {
369357
const ANON: bool = is_anon!([$($modifiers)*]);
370358
const EVAL_ALWAYS: bool = is_eval_always!([$($modifiers)*]);
371-
const DEP_KIND: dep_graph::DepKind = dep_graph::DepKind::$node;
359+
const DEP_KIND: dep_graph::DepKind = dep_graph::DepKind::$name;
372360

373361
type Cache = query_storage!([$($modifiers)*][$($K)*, $V]);
374362

compiler/rustc_middle/src/ty/query/stats.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -120,13 +120,13 @@ pub fn print_stats(tcx: TyCtxt<'_>) {
120120
}
121121

122122
macro_rules! print_stats {
123-
(<$tcx:tt> $($category:tt {
124-
$($(#[$attr:meta])* [$($modifiers:tt)*] fn $name:ident: $node:ident($K:ty) -> $V:ty,)*
125-
},)*) => {
123+
(<$tcx:tt>
124+
$($(#[$attr:meta])* [$($modifiers:tt)*] fn $name:ident($K:ty) -> $V:ty,)*
125+
) => {
126126
fn query_stats(tcx: TyCtxt<'_>) -> Vec<QueryStats> {
127127
let mut queries = Vec::new();
128128

129-
$($(
129+
$(
130130
queries.push(stats::<
131131
crate::dep_graph::DepKind,
132132
<TyCtxt<'_> as QueryContext>::Query,
@@ -135,7 +135,7 @@ macro_rules! print_stats {
135135
stringify!($name),
136136
&tcx.queries.$name,
137137
));
138-
)*)*
138+
)*
139139

140140
queries
141141
}

compiler/rustc_query_system/src/query/config.rs

-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use crate::dep_graph::SerializedDepNodeIndex;
55
use crate::query::caches::QueryCache;
66
use crate::query::plumbing::CycleError;
77
use crate::query::{QueryContext, QueryState};
8-
use rustc_data_structures::profiling::ProfileCategory;
98

109
use rustc_data_structures::fingerprint::Fingerprint;
1110
use std::borrow::Cow;
@@ -14,7 +13,6 @@ use std::hash::Hash;
1413

1514
pub trait QueryConfig {
1615
const NAME: &'static str;
17-
const CATEGORY: ProfileCategory;
1816

1917
type Key: Eq + Hash + Clone + Debug;
2018
type Value;

0 commit comments

Comments
 (0)