Skip to content

Commit 4e09a13

Browse files
committed
Don't create two new closures for each query
- Parameterize DepKindStruct over `'tcx` This allows passing in an invariant function pointer in `query_callback`, rather than having to try and make it work for any lifetime. - Add a new `execute_query` function to `QueryDescription` so we can call `tcx.$name` without needing to be in a macro context
1 parent 4fcc745 commit 4e09a13

File tree

5 files changed

+31
-37
lines changed

5 files changed

+31
-37
lines changed

Diff for: compiler/rustc_middle/src/arena.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ macro_rules! arena_types {
100100
[decode] is_late_bound_map: rustc_data_structures::fx::FxIndexSet<rustc_hir::def_id::LocalDefId>,
101101
[decode] impl_source: rustc_middle::traits::ImplSource<'tcx, ()>,
102102

103-
[] dep_kind: rustc_middle::dep_graph::DepKindStruct,
103+
[] dep_kind: rustc_middle::dep_graph::DepKindStruct<'tcx>,
104104
]);
105105
)
106106
}

Diff for: compiler/rustc_middle/src/dep_graph/dep_node.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ pub use rustc_query_system::dep_graph::{DepContext, DepNodeParams};
7474
/// Information is retrieved by indexing the `DEP_KINDS` array using the integer value
7575
/// of the `DepKind`. Overall, this allows to implement `DepContext` using this manual
7676
/// jump table instead of large matches.
77-
pub struct DepKindStruct {
77+
pub struct DepKindStruct<'tcx> {
7878
/// Anonymous queries cannot be replayed from one compiler invocation to the next.
7979
/// When their result is needed, it is recomputed. They are useful for fine-grained
8080
/// dependency tracking, and caching within one compiler invocation.
@@ -124,10 +124,10 @@ pub struct DepKindStruct {
124124
/// with kind `MirValidated`, we know that the GUID/fingerprint of the `DepNode`
125125
/// is actually a `DefPathHash`, and can therefore just look up the corresponding
126126
/// `DefId` in `tcx.def_path_hash_to_def_id`.
127-
pub force_from_dep_node: Option<fn(tcx: TyCtxt<'_>, dep_node: DepNode) -> bool>,
127+
pub force_from_dep_node: Option<fn(tcx: TyCtxt<'tcx>, dep_node: DepNode) -> bool>,
128128

129129
/// Invoke a query to put the on-disk cached value in memory.
130-
pub try_load_from_on_disk_cache: Option<fn(TyCtxt<'_>, DepNode)>,
130+
pub try_load_from_on_disk_cache: Option<fn(TyCtxt<'tcx>, DepNode)>,
131131
}
132132

133133
impl DepKind {

Diff for: compiler/rustc_middle/src/ty/context.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1089,7 +1089,7 @@ pub struct GlobalCtxt<'tcx> {
10891089

10901090
pub queries: &'tcx dyn query::QueryEngine<'tcx>,
10911091
pub query_caches: query::QueryCaches<'tcx>,
1092-
query_kinds: &'tcx [DepKindStruct],
1092+
query_kinds: &'tcx [DepKindStruct<'tcx>],
10931093

10941094
// Internal caches for metadata decoding. No need to track deps on this.
10951095
pub ty_rcache: Lock<FxHashMap<ty::CReaderCacheKey, Ty<'tcx>>>,
@@ -1246,7 +1246,7 @@ impl<'tcx> TyCtxt<'tcx> {
12461246
dep_graph: DepGraph,
12471247
on_disk_cache: Option<&'tcx dyn OnDiskCache<'tcx>>,
12481248
queries: &'tcx dyn query::QueryEngine<'tcx>,
1249-
query_kinds: &'tcx [DepKindStruct],
1249+
query_kinds: &'tcx [DepKindStruct<'tcx>],
12501250
crate_name: &str,
12511251
output_filenames: OutputFilenames,
12521252
) -> GlobalCtxt<'tcx> {
@@ -1296,7 +1296,7 @@ impl<'tcx> TyCtxt<'tcx> {
12961296
}
12971297
}
12981298

1299-
pub(crate) fn query_kind(self, k: DepKind) -> &'tcx DepKindStruct {
1299+
pub(crate) fn query_kind(self, k: DepKind) -> &'tcx DepKindStruct<'tcx> {
13001300
&self.query_kinds[k as usize]
13011301
}
13021302

Diff for: compiler/rustc_query_impl/src/plumbing.rs

+21-30
Original file line numberDiff line numberDiff line change
@@ -301,11 +301,8 @@ pub(crate) fn create_query_frame<
301301
QueryStackFrame::new(name, description, span, def_kind, hash)
302302
}
303303

304-
pub(crate) fn try_load_from_on_disk_cache<'tcx, Q, V>(
305-
tcx: TyCtxt<'tcx>,
306-
dep_node: DepNode,
307-
cache_query_deps: fn(TyCtxt<'tcx>, Q::Key) -> V,
308-
) where
304+
fn try_load_from_on_disk_cache<'tcx, Q>(tcx: TyCtxt<'tcx>, dep_node: DepNode)
305+
where
309306
Q: QueryDescription<QueryCtxt<'tcx>>,
310307
Q::Key: DepNodeParams<TyCtxt<'tcx>>,
311308
{
@@ -315,11 +312,11 @@ pub(crate) fn try_load_from_on_disk_cache<'tcx, Q, V>(
315312
panic!("Failed to recover key for {:?} with hash {}", dep_node, dep_node.hash)
316313
});
317314
if Q::cache_on_disk(tcx, &key) {
318-
let _ = cache_query_deps(tcx, key);
315+
let _ = Q::execute_query(tcx, key);
319316
}
320317
}
321318

322-
pub(crate) fn force_from_dep_node<'tcx, Q>(tcx: TyCtxt<'tcx>, dep_node: DepNode) -> bool
319+
fn force_from_dep_node<'tcx, Q>(tcx: TyCtxt<'tcx>, dep_node: DepNode) -> bool
323320
where
324321
Q: QueryDescription<QueryCtxt<'tcx>>,
325322
Q::Key: DepNodeParams<TyCtxt<'tcx>>,
@@ -336,13 +333,9 @@ where
336333
}
337334

338335
pub(crate) fn query_callback<'tcx, Q: QueryConfig>(
339-
// NOTE: we can't remove these function pointers, because `recover` is invariant -> `try_load_from_on_disk_cache` takes a concrete lifetime, not a universal lifetime.
340-
// Instead, we infer the correct lifetime at the callsite, so we can pass in a HRTB function pointer to the DepKindStruct.
341-
try_load_from_on_disk_cache: fn(TyCtxt<'_>, DepNode),
342-
force_from_dep_node: fn(TyCtxt<'_>, DepNode) -> bool,
343336
is_anon: bool,
344337
is_eval_always: bool,
345-
) -> DepKindStruct
338+
) -> DepKindStruct<'tcx>
346339
where
347340
Q: QueryDescription<QueryCtxt<'tcx>>,
348341
Q::Key: DepNodeParams<TyCtxt<'tcx>>,
@@ -363,8 +356,8 @@ where
363356
is_anon,
364357
is_eval_always,
365358
fingerprint_style,
366-
force_from_dep_node: Some(force_from_dep_node),
367-
try_load_from_on_disk_cache: Some(try_load_from_on_disk_cache),
359+
force_from_dep_node: Some(force_from_dep_node::<Q>),
360+
try_load_from_on_disk_cache: Some(try_load_from_on_disk_cache::<Q>),
368361
}
369362
}
370363

@@ -431,6 +424,10 @@ macro_rules! define_queries {
431424
try_load_from_disk: Self::TRY_LOAD_FROM_DISK,
432425
}
433426
}
427+
428+
fn execute_query(tcx: TyCtxt<'tcx>, k: Self::Key) -> Self::Stored {
429+
tcx.$name(k)
430+
}
434431
})*
435432

436433
#[allow(nonstandard_style)]
@@ -439,7 +436,7 @@ macro_rules! define_queries {
439436
use rustc_query_system::dep_graph::FingerprintStyle;
440437

441438
// We use this for most things when incr. comp. is turned off.
442-
pub fn Null() -> DepKindStruct {
439+
pub fn Null<'tcx>() -> DepKindStruct<'tcx> {
443440
DepKindStruct {
444441
is_anon: false,
445442
is_eval_always: false,
@@ -450,7 +447,7 @@ macro_rules! define_queries {
450447
}
451448

452449
// We use this for the forever-red node.
453-
pub fn Red() -> DepKindStruct {
450+
pub fn Red<'tcx>() -> DepKindStruct<'tcx> {
454451
DepKindStruct {
455452
is_anon: false,
456453
is_eval_always: false,
@@ -460,7 +457,7 @@ macro_rules! define_queries {
460457
}
461458
}
462459

463-
pub fn TraitSelect() -> DepKindStruct {
460+
pub fn TraitSelect<'tcx>() -> DepKindStruct<'tcx> {
464461
DepKindStruct {
465462
is_anon: true,
466463
is_eval_always: false,
@@ -470,7 +467,7 @@ macro_rules! define_queries {
470467
}
471468
}
472469

473-
pub fn CompileCodegenUnit() -> DepKindStruct {
470+
pub fn CompileCodegenUnit<'tcx>() -> DepKindStruct<'tcx> {
474471
DepKindStruct {
475472
is_anon: false,
476473
is_eval_always: false,
@@ -480,7 +477,7 @@ macro_rules! define_queries {
480477
}
481478
}
482479

483-
pub fn CompileMonoItem() -> DepKindStruct {
480+
pub fn CompileMonoItem<'tcx>() -> DepKindStruct<'tcx> {
484481
DepKindStruct {
485482
is_anon: false,
486483
is_eval_always: false,
@@ -490,21 +487,15 @@ macro_rules! define_queries {
490487
}
491488
}
492489

493-
$(pub(crate) fn $name()-> DepKindStruct {
494-
let is_anon = is_anon!([$($modifiers)*]);
495-
let is_eval_always = is_eval_always!([$($modifiers)*]);
496-
type Q<'tcx> = queries::$name<'tcx>;
497-
498-
$crate::plumbing::query_callback::<Q<'_>>(
499-
|tcx, key| $crate::plumbing::try_load_from_on_disk_cache::<Q<'_>, _>(tcx, key, TyCtxt::$name),
500-
|tcx, key| $crate::plumbing::force_from_dep_node::<Q<'_>>(tcx, key),
501-
is_anon,
502-
is_eval_always
490+
$(pub(crate) fn $name<'tcx>()-> DepKindStruct<'tcx> {
491+
$crate::plumbing::query_callback::<queries::$name<'tcx>>(
492+
is_anon!([$($modifiers)*]),
493+
is_eval_always!([$($modifiers)*]),
503494
)
504495
})*
505496
}
506497

507-
pub fn query_callbacks<'tcx>(arena: &'tcx Arena<'tcx>) -> &'tcx [DepKindStruct] {
498+
pub fn query_callbacks<'tcx>(arena: &'tcx Arena<'tcx>) -> &'tcx [DepKindStruct<'tcx>] {
508499
arena.alloc_from_iter(make_dep_kind_array!(query_callbacks))
509500
}
510501
}

Diff for: compiler/rustc_query_system/src/query/config.rs

+3
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,7 @@ pub trait QueryDescription<CTX: QueryContext>: QueryConfig {
7373
fn make_vtable(tcx: CTX, key: &Self::Key) -> QueryVTable<CTX, Self::Key, Self::Value>;
7474

7575
fn cache_on_disk(tcx: CTX::DepContext, key: &Self::Key) -> bool;
76+
77+
// Don't use this method to compute query results, instead use the methods on TyCtxt
78+
fn execute_query(tcx: CTX::DepContext, k: Self::Key) -> Self::Stored;
7679
}

0 commit comments

Comments
 (0)