Skip to content

Commit 0bedd35

Browse files
committed
Move most of make_query into a generic function, away from the macro
This should both make the code easier to read and also greatly reduce the amount of codegen the compiler has to do, since it only needs to monomorphize `create_query_frame` for each new key and not for each query.
1 parent 1de08b1 commit 0bedd35

File tree

2 files changed

+56
-42
lines changed

2 files changed

+56
-42
lines changed

compiler/rustc_query_impl/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ extern crate rustc_macros;
1515
#[macro_use]
1616
extern crate rustc_middle;
1717

18-
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
1918
use rustc_data_structures::sync::AtomicU64;
2019
use rustc_middle::arena::Arena;
2120
use rustc_middle::dep_graph::{self, DepKindStruct, SerializedDepNodeIndex};

compiler/rustc_query_impl/src/plumbing.rs

+56-41
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,18 @@
22
//! generate the actual methods on tcx which find and execute the provider,
33
//! manage the caches, and so forth.
44
5+
use crate::keys::Key;
56
use crate::{on_disk_cache, Queries};
6-
use rustc_middle::dep_graph::{DepNodeIndex, SerializedDepNodeIndex};
7+
use rustc_middle::dep_graph::{self, DepKind, DepNodeIndex, SerializedDepNodeIndex};
78
use rustc_middle::ty::tls::{self, ImplicitCtxt};
8-
use rustc_middle::ty::TyCtxt;
9+
use rustc_middle::ty::{self, TyCtxt};
910
use rustc_query_system::dep_graph::HasDepContext;
10-
use rustc_query_system::query::{QueryContext, QueryJobId, QueryMap, QuerySideEffects};
11+
use rustc_query_system::ich::StableHashingContext;
12+
use rustc_query_system::query::{
13+
QueryContext, QueryJobId, QueryMap, QuerySideEffects, QueryStackFrame,
14+
};
1115

16+
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
1217
use rustc_data_structures::sync::Lock;
1318
use rustc_data_structures::thin_vec::ThinVec;
1419
use rustc_errors::{Diagnostic, Handler};
@@ -233,6 +238,53 @@ macro_rules! get_provider {
233238
};
234239
}
235240

241+
pub(crate) fn create_query_frame<
242+
'tcx,
243+
K: Copy + Key + for<'a> HashStable<StableHashingContext<'a>>,
244+
>(
245+
tcx: QueryCtxt<'tcx>,
246+
do_describe: fn(QueryCtxt<'tcx>, K) -> String,
247+
key: K,
248+
kind: DepKind,
249+
name: &'static str,
250+
) -> QueryStackFrame {
251+
// Disable visible paths printing for performance reasons.
252+
// Showing visible path instead of any path is not that important in production.
253+
let description = ty::print::with_no_visible_paths!(
254+
// Force filename-line mode to avoid invoking `type_of` query.
255+
ty::print::with_forced_impl_filename_line!(do_describe(tcx, key))
256+
);
257+
let description =
258+
if tcx.sess.verbose() { format!("{} [{}]", description, name) } else { description };
259+
let span = if kind == dep_graph::DepKind::def_span {
260+
// The `def_span` query is used to calculate `default_span`,
261+
// so exit to avoid infinite recursion.
262+
None
263+
} else {
264+
Some(key.default_span(*tcx))
265+
};
266+
let def_kind = if kind == dep_graph::DepKind::opt_def_kind {
267+
// Try to avoid infinite recursion.
268+
None
269+
} else {
270+
key.key_as_def_id()
271+
.and_then(|def_id| def_id.as_local())
272+
.and_then(|def_id| tcx.opt_def_kind(def_id))
273+
};
274+
let hash = || {
275+
tcx.with_stable_hashing_context(|mut hcx| {
276+
let mut hasher = StableHasher::new();
277+
std::mem::discriminant(&kind).hash_stable(&mut hcx, &mut hasher);
278+
key.hash_stable(&mut hcx, &mut hasher);
279+
hasher.finish::<u64>()
280+
})
281+
};
282+
283+
QueryStackFrame::new(name, description, span, def_kind, hash)
284+
}
285+
286+
// NOTE: `$V` isn't used here, but we still need to match on it so it can be passed to other macros
287+
// invoked by `rustc_query_append`.
236288
macro_rules! define_queries {
237289
(
238290
$($(#[$attr:meta])*
@@ -249,44 +301,7 @@ macro_rules! define_queries {
249301
pub fn $name<'tcx>(tcx: QueryCtxt<'tcx>, key: <queries::$name<'tcx> as QueryConfig>::Key) -> QueryStackFrame {
250302
let kind = dep_graph::DepKind::$name;
251303
let name = stringify!($name);
252-
// Disable visible paths printing for performance reasons.
253-
// Showing visible path instead of any path is not that important in production.
254-
let description = ty::print::with_no_visible_paths!(
255-
// Force filename-line mode to avoid invoking `type_of` query.
256-
ty::print::with_forced_impl_filename_line!(
257-
queries::$name::describe(tcx, key)
258-
)
259-
);
260-
let description = if tcx.sess.verbose() {
261-
format!("{} [{}]", description, name)
262-
} else {
263-
description
264-
};
265-
let span = if kind == dep_graph::DepKind::def_span {
266-
// The `def_span` query is used to calculate `default_span`,
267-
// so exit to avoid infinite recursion.
268-
None
269-
} else {
270-
Some(key.default_span(*tcx))
271-
};
272-
let def_kind = if kind == dep_graph::DepKind::opt_def_kind {
273-
// Try to avoid infinite recursion.
274-
None
275-
} else {
276-
key.key_as_def_id()
277-
.and_then(|def_id| def_id.as_local())
278-
.and_then(|def_id| tcx.opt_def_kind(def_id))
279-
};
280-
let hash = || {
281-
tcx.with_stable_hashing_context(|mut hcx|{
282-
let mut hasher = StableHasher::new();
283-
std::mem::discriminant(&kind).hash_stable(&mut hcx, &mut hasher);
284-
key.hash_stable(&mut hcx, &mut hasher);
285-
hasher.finish::<u64>()
286-
})
287-
};
288-
289-
QueryStackFrame::new(name, description, span, def_kind, hash)
304+
$crate::plumbing::create_query_frame(tcx, queries::$name::describe, key, kind, name)
290305
})*
291306
}
292307

0 commit comments

Comments
 (0)