Skip to content

Commit cdc0b19

Browse files
committed
Split DepKindStruct in two.
1 parent 1ac21e4 commit cdc0b19

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
@@ -55,15 +55,13 @@
5555
//!
5656
//! [dependency graph]: https://rustc-dev-guide.rust-lang.org/query.html
5757
58-
use crate::ty::query::QueryCtxt;
5958
use crate::ty::TyCtxt;
6059

6160
use rustc_data_structures::fingerprint::Fingerprint;
6261
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, CRATE_DEF_INDEX};
6362
use rustc_hir::definitions::DefPathHash;
6463
use rustc_hir::HirId;
6564
use rustc_span::symbol::Symbol;
66-
use rustc_span::DUMMY_SP;
6765
use std::hash::Hash;
6866

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

14495
impl std::ops::Deref for DepKind {
@@ -197,8 +148,7 @@ macro_rules! contains_eval_always_attr {
197148
#[allow(non_upper_case_globals)]
198149
pub mod dep_kind {
199150
use super::*;
200-
use crate::ty::query::{queries, query_keys};
201-
use rustc_query_system::query::{force_query, QueryDescription};
151+
use crate::ty::query::query_keys;
202152

203153
// We use this for most things when incr. comp. is turned off.
204154
pub const Null: DepKindStruct = DepKindStruct {
@@ -207,8 +157,6 @@ pub mod dep_kind {
207157
is_eval_always: false,
208158

209159
can_reconstruct_query_key: || true,
210-
force_from_dep_node: |_, dep_node| bug!("force_from_dep_node: encountered {:?}", dep_node),
211-
try_load_from_on_disk_cache: |_, _| {},
212160
};
213161

214162
pub const TraitSelect: DepKindStruct = DepKindStruct {
@@ -217,8 +165,6 @@ pub mod dep_kind {
217165
is_eval_always: false,
218166

219167
can_reconstruct_query_key: || true,
220-
force_from_dep_node: |_, _| false,
221-
try_load_from_on_disk_cache: |_, _| {},
222168
};
223169

224170
pub const CompileCodegenUnit: DepKindStruct = DepKindStruct {
@@ -227,8 +173,6 @@ pub mod dep_kind {
227173
is_eval_always: false,
228174

229175
can_reconstruct_query_key: || false,
230-
force_from_dep_node: |_, _| false,
231-
try_load_from_on_disk_cache: |_, _| {},
232176
};
233177

234178
macro_rules! define_query_dep_kinds {
@@ -247,54 +191,11 @@ pub mod dep_kind {
247191
::can_reconstruct_query_key()
248192
}
249193

250-
fn recover<'tcx>(tcx: TyCtxt<'tcx>, dep_node: &DepNode) -> Option<query_keys::$variant<'tcx>> {
251-
<query_keys::$variant<'_> as DepNodeParams<TyCtxt<'_>>>::recover(tcx, dep_node)
252-
}
253-
254-
fn force_from_dep_node(tcx: QueryCtxt<'_>, dep_node: &DepNode) -> bool {
255-
if is_anon {
256-
return false;
257-
}
258-
259-
if !can_reconstruct_query_key() {
260-
return false;
261-
}
262-
263-
if let Some(key) = recover(*tcx, dep_node) {
264-
force_query::<queries::$variant<'_>, _>(tcx, key, DUMMY_SP, *dep_node);
265-
return true;
266-
}
267-
268-
false
269-
}
270-
271-
fn try_load_from_on_disk_cache(tcx: QueryCtxt<'_>, dep_node: &DepNode) {
272-
if is_anon {
273-
return
274-
}
275-
276-
if !can_reconstruct_query_key() {
277-
return
278-
}
279-
280-
debug_assert!(tcx.dep_graph
281-
.node_color(dep_node)
282-
.map(|c| c.is_green())
283-
.unwrap_or(false));
284-
285-
let key = recover(*tcx, dep_node).unwrap_or_else(|| panic!("Failed to recover key for {:?} with hash {}", dep_node, dep_node.hash));
286-
if queries::$variant::cache_on_disk(tcx, &key, None) {
287-
let _ = tcx.$variant(key);
288-
}
289-
}
290-
291194
DepKindStruct {
292195
has_params,
293196
is_anon,
294197
is_eval_always,
295198
can_reconstruct_query_key,
296-
force_from_dep_node,
297-
try_load_from_on_disk_cache,
298199
}
299200
};)*
300201
);
@@ -310,7 +211,12 @@ macro_rules! define_dep_nodes {
310211
$variant:ident $(( $tuple_arg_ty:ty $(,)? ))*
311212
,)*
312213
) => (
313-
static DEP_KINDS: &[DepKindStruct] = &[ $(dep_kind::$variant),* ];
214+
#[macro_export]
215+
macro_rules! make_dep_kind_array {
216+
($mod:ident) => {[ $(($mod::$variant),)* ]};
217+
}
218+
219+
static DEP_KINDS: &[DepKindStruct] = &make_dep_kind_array!(dep_kind);
314220

315221
/// This enum serves as an index into the `DEP_KINDS` array.
316222
#[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
@@ -62,6 +62,7 @@ use std::sync::Arc;
6262
#[macro_use]
6363
mod plumbing;
6464
pub use plumbing::QueryCtxt;
65+
use plumbing::QueryStruct;
6566
pub(crate) use rustc_query_system::query::CycleError;
6667
use rustc_query_system::query::*;
6768

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();
@@ -545,6 +601,89 @@ macro_rules! define_queries {
545601
}
546602
})*
547603

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

0 commit comments

Comments
 (0)