Skip to content

Commit 9a4d339

Browse files
committed
Split DepKindStruct in two.
1 parent 5273492 commit 9a4d339

File tree

5 files changed

+151
-103
lines changed

5 files changed

+151
-103
lines changed

compiler/rustc_middle/src/dep_graph/dep_node.rs

Lines changed: 7 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,13 @@
5252
//! user of the `DepNode` API of having to know how to compute the expected
5353
//! fingerprint for a given set of node parameters.
5454
55-
use crate::ty::query::QueryCtxt;
5655
use crate::ty::TyCtxt;
5756

5857
use rustc_data_structures::fingerprint::Fingerprint;
5958
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, CRATE_DEF_INDEX};
6059
use rustc_hir::definitions::DefPathHash;
6160
use rustc_hir::HirId;
6261
use rustc_span::symbol::Symbol;
63-
use rustc_span::DUMMY_SP;
6462
use std::hash::Hash;
6563

6664
pub use rustc_query_system::dep_graph::{DepContext, DepNodeParams};
@@ -89,53 +87,6 @@ pub struct DepKindStruct {
8987
// FIXME: Make this a simple boolean once DepNodeParams::can_reconstruct_query_key
9088
// can be made a specialized associated const.
9189
can_reconstruct_query_key: fn() -> bool,
92-
93-
/// The red/green evaluation system will try to mark a specific DepNode in the
94-
/// dependency graph as green by recursively trying to mark the dependencies of
95-
/// that `DepNode` as green. While doing so, it will sometimes encounter a `DepNode`
96-
/// where we don't know if it is red or green and we therefore actually have
97-
/// to recompute its value in order to find out. Since the only piece of
98-
/// information that we have at that point is the `DepNode` we are trying to
99-
/// re-evaluate, we need some way to re-run a query from just that. This is what
100-
/// `force_from_dep_node()` implements.
101-
///
102-
/// In the general case, a `DepNode` consists of a `DepKind` and an opaque
103-
/// GUID/fingerprint that will uniquely identify the node. This GUID/fingerprint
104-
/// is usually constructed by computing a stable hash of the query-key that the
105-
/// `DepNode` corresponds to. Consequently, it is not in general possible to go
106-
/// back from hash to query-key (since hash functions are not reversible). For
107-
/// this reason `force_from_dep_node()` is expected to fail from time to time
108-
/// because we just cannot find out, from the `DepNode` alone, what the
109-
/// corresponding query-key is and therefore cannot re-run the query.
110-
///
111-
/// The system deals with this case letting `try_mark_green` fail which forces
112-
/// the root query to be re-evaluated.
113-
///
114-
/// Now, if `force_from_dep_node()` would always fail, it would be pretty useless.
115-
/// Fortunately, we can use some contextual information that will allow us to
116-
/// reconstruct query-keys for certain kinds of `DepNode`s. In particular, we
117-
/// enforce by construction that the GUID/fingerprint of certain `DepNode`s is a
118-
/// valid `DefPathHash`. Since we also always build a huge table that maps every
119-
/// `DefPathHash` in the current codebase to the corresponding `DefId`, we have
120-
/// everything we need to re-run the query.
121-
///
122-
/// Take the `mir_promoted` query as an example. Like many other queries, it
123-
/// just has a single parameter: the `DefId` of the item it will compute the
124-
/// validated MIR for. Now, when we call `force_from_dep_node()` on a `DepNode`
125-
/// with kind `MirValidated`, we know that the GUID/fingerprint of the `DepNode`
126-
/// is actually a `DefPathHash`, and can therefore just look up the corresponding
127-
/// `DefId` in `tcx.def_path_hash_to_def_id`.
128-
///
129-
/// When you implement a new query, it will likely have a corresponding new
130-
/// `DepKind`, and you'll have to support it here in `force_from_dep_node()`. As
131-
/// a rule of thumb, if your query takes a `DefId` or `LocalDefId` as sole parameter,
132-
/// then `force_from_dep_node()` should not fail for it. Otherwise, you can just
133-
/// add it to the "We don't have enough information to reconstruct..." group in
134-
/// the match below.
135-
pub(crate) force_from_dep_node: fn(tcx: QueryCtxt<'_>, dep_node: &DepNode) -> bool,
136-
137-
/// Invoke a query to put the on-disk cached value in memory.
138-
pub(crate) try_load_from_on_disk_cache: fn(QueryCtxt<'_>, &DepNode),
13990
}
14091

14192
impl std::ops::Deref for DepKind {
@@ -194,8 +145,7 @@ macro_rules! contains_eval_always_attr {
194145
#[allow(non_upper_case_globals)]
195146
pub mod dep_kind {
196147
use super::*;
197-
use crate::ty::query::{queries, query_keys};
198-
use rustc_query_system::query::{force_query, QueryDescription};
148+
use crate::ty::query::query_keys;
199149

200150
// We use this for most things when incr. comp. is turned off.
201151
pub const Null: DepKindStruct = DepKindStruct {
@@ -204,8 +154,6 @@ pub mod dep_kind {
204154
is_eval_always: false,
205155

206156
can_reconstruct_query_key: || true,
207-
force_from_dep_node: |_, dep_node| bug!("force_from_dep_node: encountered {:?}", dep_node),
208-
try_load_from_on_disk_cache: |_, _| {},
209157
};
210158

211159
pub const TraitSelect: DepKindStruct = DepKindStruct {
@@ -214,8 +162,6 @@ pub mod dep_kind {
214162
is_eval_always: false,
215163

216164
can_reconstruct_query_key: || true,
217-
force_from_dep_node: |_, _| false,
218-
try_load_from_on_disk_cache: |_, _| {},
219165
};
220166

221167
pub const CompileCodegenUnit: DepKindStruct = DepKindStruct {
@@ -224,8 +170,6 @@ pub mod dep_kind {
224170
is_eval_always: false,
225171

226172
can_reconstruct_query_key: || false,
227-
force_from_dep_node: |_, _| false,
228-
try_load_from_on_disk_cache: |_, _| {},
229173
};
230174

231175
macro_rules! define_query_dep_kinds {
@@ -244,54 +188,11 @@ pub mod dep_kind {
244188
::can_reconstruct_query_key()
245189
}
246190

247-
fn recover<'tcx>(tcx: TyCtxt<'tcx>, dep_node: &DepNode) -> Option<query_keys::$variant<'tcx>> {
248-
<query_keys::$variant<'_> as DepNodeParams<TyCtxt<'_>>>::recover(tcx, dep_node)
249-
}
250-
251-
fn force_from_dep_node(tcx: QueryCtxt<'_>, dep_node: &DepNode) -> bool {
252-
if is_anon {
253-
return false;
254-
}
255-
256-
if !can_reconstruct_query_key() {
257-
return false;
258-
}
259-
260-
if let Some(key) = recover(*tcx, dep_node) {
261-
force_query::<queries::$variant<'_>, _>(tcx, key, DUMMY_SP, *dep_node);
262-
return true;
263-
}
264-
265-
false
266-
}
267-
268-
fn try_load_from_on_disk_cache(tcx: QueryCtxt<'_>, dep_node: &DepNode) {
269-
if is_anon {
270-
return
271-
}
272-
273-
if !can_reconstruct_query_key() {
274-
return
275-
}
276-
277-
debug_assert!(tcx.dep_graph
278-
.node_color(dep_node)
279-
.map(|c| c.is_green())
280-
.unwrap_or(false));
281-
282-
let key = recover(*tcx, dep_node).unwrap_or_else(|| panic!("Failed to recover key for {:?} with hash {}", dep_node, dep_node.hash));
283-
if queries::$variant::cache_on_disk(tcx, &key, None) {
284-
let _ = tcx.$variant(key);
285-
}
286-
}
287-
288191
DepKindStruct {
289192
has_params,
290193
is_anon,
291194
is_eval_always,
292195
can_reconstruct_query_key,
293-
force_from_dep_node,
294-
try_load_from_on_disk_cache,
295196
}
296197
};)*
297198
);
@@ -307,7 +208,12 @@ macro_rules! define_dep_nodes {
307208
$variant:ident $(( $tuple_arg_ty:ty $(,)? ))*
308209
,)*
309210
) => (
310-
static DEP_KINDS: &[DepKindStruct] = &[ $(dep_kind::$variant),* ];
211+
#[macro_export]
212+
macro_rules! make_dep_kind_array {
213+
($mod:ident) => {[ $(($mod::$variant),)* ]};
214+
}
215+
216+
static DEP_KINDS: &[DepKindStruct] = &make_dep_kind_array!(dep_kind);
311217

312218
/// This enum serves as an index into the `DEP_KINDS` array.
313219
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Encodable, Decodable)]

compiler/rustc_middle/src/dep_graph/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use crate::ty::{self, TyCtxt};
33
use rustc_data_structures::profiling::SelfProfilerRef;
44
use rustc_data_structures::sync::Lock;
55

6+
#[macro_use]
67
mod dep_node;
78

89
pub use rustc_query_system::dep_graph::{

compiler/rustc_middle/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ pub mod query;
7676

7777
#[macro_use]
7878
pub mod arena;
79+
#[macro_use]
7980
pub mod dep_graph;
8081
pub mod hir;
8182
pub mod ich;

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ use std::sync::Arc;
6363
#[macro_use]
6464
mod plumbing;
6565
pub use plumbing::QueryCtxt;
66+
use plumbing::QueryStruct;
6667
pub(crate) use rustc_query_system::query::CycleError;
6768
use rustc_query_system::query::*;
6869

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

Lines changed: 141 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ impl QueryContext for QueryCtxt<'tcx> {
6969
}
7070

7171
fn try_load_from_on_disk_cache(&self, dep_node: &DepNode) {
72-
(dep_node.kind.try_load_from_on_disk_cache)(*self, dep_node)
72+
let cb = &super::QUERY_CALLBACKS[dep_node.kind as usize];
73+
(cb.try_load_from_on_disk_cache)(*self, dep_node)
7374
}
7475

7576
fn try_force_from_dep_node(&self, dep_node: &DepNode) -> bool {
@@ -126,7 +127,8 @@ impl QueryContext for QueryCtxt<'tcx> {
126127
"calling force_from_dep_node() on DepKind::codegen_unit"
127128
);
128129

129-
(dep_node.kind.force_from_dep_node)(*self, dep_node)
130+
let cb = &super::QUERY_CALLBACKS[dep_node.kind as usize];
131+
(cb.force_from_dep_node)(*self, dep_node)
130132
}
131133

132134
fn has_errors_or_delayed_span_bugs(&self) -> bool {
@@ -307,6 +309,60 @@ impl<'tcx> Queries<'tcx> {
307309
}
308310
}
309311

312+
/// This struct stores metadata about each Query.
313+
///
314+
/// Information is retrieved by indexing the `QUERIES` array using the integer value
315+
/// of the `DepKind`. Overall, this allows to implement `QueryContext` using this manual
316+
/// jump table instead of large matches.
317+
pub struct QueryStruct {
318+
/// The red/green evaluation system will try to mark a specific DepNode in the
319+
/// dependency graph as green by recursively trying to mark the dependencies of
320+
/// that `DepNode` as green. While doing so, it will sometimes encounter a `DepNode`
321+
/// where we don't know if it is red or green and we therefore actually have
322+
/// to recompute its value in order to find out. Since the only piece of
323+
/// information that we have at that point is the `DepNode` we are trying to
324+
/// re-evaluate, we need some way to re-run a query from just that. This is what
325+
/// `force_from_dep_node()` implements.
326+
///
327+
/// In the general case, a `DepNode` consists of a `DepKind` and an opaque
328+
/// GUID/fingerprint that will uniquely identify the node. This GUID/fingerprint
329+
/// is usually constructed by computing a stable hash of the query-key that the
330+
/// `DepNode` corresponds to. Consequently, it is not in general possible to go
331+
/// back from hash to query-key (since hash functions are not reversible). For
332+
/// this reason `force_from_dep_node()` is expected to fail from time to time
333+
/// because we just cannot find out, from the `DepNode` alone, what the
334+
/// corresponding query-key is and therefore cannot re-run the query.
335+
///
336+
/// The system deals with this case letting `try_mark_green` fail which forces
337+
/// the root query to be re-evaluated.
338+
///
339+
/// Now, if `force_from_dep_node()` would always fail, it would be pretty useless.
340+
/// Fortunately, we can use some contextual information that will allow us to
341+
/// reconstruct query-keys for certain kinds of `DepNode`s. In particular, we
342+
/// enforce by construction that the GUID/fingerprint of certain `DepNode`s is a
343+
/// valid `DefPathHash`. Since we also always build a huge table that maps every
344+
/// `DefPathHash` in the current codebase to the corresponding `DefId`, we have
345+
/// everything we need to re-run the query.
346+
///
347+
/// Take the `mir_promoted` query as an example. Like many other queries, it
348+
/// just has a single parameter: the `DefId` of the item it will compute the
349+
/// validated MIR for. Now, when we call `force_from_dep_node()` on a `DepNode`
350+
/// with kind `MirValidated`, we know that the GUID/fingerprint of the `DepNode`
351+
/// is actually a `DefPathHash`, and can therefore just look up the corresponding
352+
/// `DefId` in `tcx.def_path_hash_to_def_id`.
353+
///
354+
/// When you implement a new query, it will likely have a corresponding new
355+
/// `DepKind`, and you'll have to support it here in `force_from_dep_node()`. As
356+
/// a rule of thumb, if your query takes a `DefId` or `LocalDefId` as sole parameter,
357+
/// then `force_from_dep_node()` should not fail for it. Otherwise, you can just
358+
/// add it to the "We don't have enough information to reconstruct..." group in
359+
/// the match below.
360+
pub(crate) force_from_dep_node: fn(tcx: QueryCtxt<'_>, dep_node: &DepNode) -> bool,
361+
362+
/// Invoke a query to put the on-disk cached value in memory.
363+
pub(crate) try_load_from_on_disk_cache: fn(QueryCtxt<'_>, &DepNode),
364+
}
365+
310366
macro_rules! handle_cycle_error {
311367
([][$tcx: expr, $error:expr]) => {{
312368
$tcx.report_cycle($error).emit();
@@ -530,6 +586,89 @@ macro_rules! define_queries {
530586
}
531587
})*
532588

589+
#[allow(non_upper_case_globals)]
590+
pub mod query_callbacks {
591+
use super::*;
592+
use crate::dep_graph::DepNode;
593+
use crate::ty::query::{queries, query_keys};
594+
use rustc_query_system::dep_graph::DepNodeParams;
595+
use rustc_query_system::query::{force_query, QueryDescription};
596+
597+
// We use this for most things when incr. comp. is turned off.
598+
pub const Null: QueryStruct = QueryStruct {
599+
force_from_dep_node: |_, dep_node| bug!("force_from_dep_node: encountered {:?}", dep_node),
600+
try_load_from_on_disk_cache: |_, _| {},
601+
};
602+
603+
pub const TraitSelect: QueryStruct = QueryStruct {
604+
force_from_dep_node: |_, _| false,
605+
try_load_from_on_disk_cache: |_, _| {},
606+
};
607+
608+
pub const CompileCodegenUnit: QueryStruct = QueryStruct {
609+
force_from_dep_node: |_, _| false,
610+
try_load_from_on_disk_cache: |_, _| {},
611+
};
612+
613+
$(pub const $name: QueryStruct = {
614+
const is_anon: bool = is_anon!([$($modifiers)*]);
615+
616+
#[inline(always)]
617+
fn can_reconstruct_query_key() -> bool {
618+
<query_keys::$name<'_> as DepNodeParams<TyCtxt<'_>>>
619+
::can_reconstruct_query_key()
620+
}
621+
622+
fn recover<'tcx>(tcx: TyCtxt<'tcx>, dep_node: &DepNode) -> Option<query_keys::$name<'tcx>> {
623+
<query_keys::$name<'_> as DepNodeParams<TyCtxt<'_>>>::recover(tcx, dep_node)
624+
}
625+
626+
fn force_from_dep_node(tcx: QueryCtxt<'_>, dep_node: &DepNode) -> bool {
627+
if is_anon {
628+
return false;
629+
}
630+
631+
if !can_reconstruct_query_key() {
632+
return false;
633+
}
634+
635+
if let Some(key) = recover(*tcx, dep_node) {
636+
force_query::<queries::$name<'_>, _>(tcx, key, DUMMY_SP, *dep_node);
637+
return true;
638+
}
639+
640+
false
641+
}
642+
643+
fn try_load_from_on_disk_cache(tcx: QueryCtxt<'_>, dep_node: &DepNode) {
644+
if is_anon {
645+
return
646+
}
647+
648+
if !can_reconstruct_query_key() {
649+
return
650+
}
651+
652+
debug_assert!(tcx.dep_graph
653+
.node_color(dep_node)
654+
.map(|c| c.is_green())
655+
.unwrap_or(false));
656+
657+
let key = recover(*tcx, dep_node).unwrap_or_else(|| panic!("Failed to recover key for {:?} with hash {}", dep_node, dep_node.hash));
658+
if queries::$name::cache_on_disk(tcx, &key, None) {
659+
let _ = tcx.$name(key);
660+
}
661+
}
662+
663+
QueryStruct {
664+
force_from_dep_node,
665+
try_load_from_on_disk_cache,
666+
}
667+
};)*
668+
}
669+
670+
static QUERY_CALLBACKS: &[QueryStruct] = &make_dep_kind_array!(query_callbacks);
671+
533672
define_provider_struct! {
534673
tcx: $tcx,
535674
input: ($(([$($modifiers)*] [$name] [$($K)*] [$V]))*)

0 commit comments

Comments
 (0)