Skip to content

Commit 295fa92

Browse files
committed
Extract DepKindVTable constructors to their own module
1 parent a565ae8 commit 295fa92

File tree

3 files changed

+200
-163
lines changed

3 files changed

+200
-163
lines changed
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
use rustc_middle::bug;
2+
use rustc_middle::dep_graph::DepKindVTable;
3+
use rustc_middle::ty::TyCtxt;
4+
use rustc_query_system::dep_graph::{DepNodeKey, FingerprintStyle};
5+
use rustc_query_system::query::QueryCache;
6+
7+
use crate::plumbing::{force_from_dep_node_inner, try_load_from_on_disk_cache_inner};
8+
use crate::{QueryCtxt, QueryDispatcherUnerased, QueryFlags};
9+
10+
/// [`DepKindVTable`] constructors for special dep kinds that aren't queries.
11+
#[expect(non_snake_case, reason = "use non-snake case to avoid collision with query names")]
12+
mod non_query {
13+
use super::*;
14+
15+
// We use this for most things when incr. comp. is turned off.
16+
pub(crate) fn Null<'tcx>() -> DepKindVTable<'tcx> {
17+
DepKindVTable {
18+
is_anon: false,
19+
is_eval_always: false,
20+
fingerprint_style: FingerprintStyle::Unit,
21+
force_from_dep_node: Some(|_, dep_node, _| {
22+
bug!("force_from_dep_node: encountered {dep_node:?}")
23+
}),
24+
try_load_from_on_disk_cache: None,
25+
name: &"Null",
26+
}
27+
}
28+
29+
// We use this for the forever-red node.
30+
pub(crate) fn Red<'tcx>() -> DepKindVTable<'tcx> {
31+
DepKindVTable {
32+
is_anon: false,
33+
is_eval_always: false,
34+
fingerprint_style: FingerprintStyle::Unit,
35+
force_from_dep_node: Some(|_, dep_node, _| {
36+
bug!("force_from_dep_node: encountered {dep_node:?}")
37+
}),
38+
try_load_from_on_disk_cache: None,
39+
name: &"Red",
40+
}
41+
}
42+
43+
pub(crate) fn SideEffect<'tcx>() -> DepKindVTable<'tcx> {
44+
DepKindVTable {
45+
is_anon: false,
46+
is_eval_always: false,
47+
fingerprint_style: FingerprintStyle::Unit,
48+
force_from_dep_node: Some(|tcx, _, prev_index| {
49+
tcx.dep_graph.force_diagnostic_node(QueryCtxt::new(tcx), prev_index);
50+
true
51+
}),
52+
try_load_from_on_disk_cache: None,
53+
name: &"SideEffect",
54+
}
55+
}
56+
57+
pub(crate) fn AnonZeroDeps<'tcx>() -> DepKindVTable<'tcx> {
58+
DepKindVTable {
59+
is_anon: true,
60+
is_eval_always: false,
61+
fingerprint_style: FingerprintStyle::Opaque,
62+
force_from_dep_node: Some(|_, _, _| bug!("cannot force an anon node")),
63+
try_load_from_on_disk_cache: None,
64+
name: &"AnonZeroDeps",
65+
}
66+
}
67+
68+
pub(crate) fn TraitSelect<'tcx>() -> DepKindVTable<'tcx> {
69+
DepKindVTable {
70+
is_anon: true,
71+
is_eval_always: false,
72+
fingerprint_style: FingerprintStyle::Unit,
73+
force_from_dep_node: None,
74+
try_load_from_on_disk_cache: None,
75+
name: &"TraitSelect",
76+
}
77+
}
78+
79+
pub(crate) fn CompileCodegenUnit<'tcx>() -> DepKindVTable<'tcx> {
80+
DepKindVTable {
81+
is_anon: false,
82+
is_eval_always: false,
83+
fingerprint_style: FingerprintStyle::Opaque,
84+
force_from_dep_node: None,
85+
try_load_from_on_disk_cache: None,
86+
name: &"CompileCodegenUnit",
87+
}
88+
}
89+
90+
pub(crate) fn CompileMonoItem<'tcx>() -> DepKindVTable<'tcx> {
91+
DepKindVTable {
92+
is_anon: false,
93+
is_eval_always: false,
94+
fingerprint_style: FingerprintStyle::Opaque,
95+
force_from_dep_node: None,
96+
try_load_from_on_disk_cache: None,
97+
name: &"CompileMonoItem",
98+
}
99+
}
100+
101+
pub(crate) fn Metadata<'tcx>() -> DepKindVTable<'tcx> {
102+
DepKindVTable {
103+
is_anon: false,
104+
is_eval_always: false,
105+
fingerprint_style: FingerprintStyle::Unit,
106+
force_from_dep_node: None,
107+
try_load_from_on_disk_cache: None,
108+
name: &"Metadata",
109+
}
110+
}
111+
}
112+
113+
/// Shared implementation of the [`DepKindVTable`] constructor for queries.
114+
/// Called from macro-generated code for each query.
115+
pub(crate) fn make_dep_kind_vtable_for_query<'tcx, Q, Cache, const FLAGS: QueryFlags>(
116+
is_eval_always: bool,
117+
) -> DepKindVTable<'tcx>
118+
where
119+
Q: QueryDispatcherUnerased<'tcx, Cache, FLAGS>,
120+
Cache: QueryCache + 'tcx,
121+
{
122+
let is_anon = FLAGS.is_anon;
123+
let fingerprint_style = if is_anon {
124+
FingerprintStyle::Opaque
125+
} else {
126+
<Cache::Key as DepNodeKey<TyCtxt<'tcx>>>::fingerprint_style()
127+
};
128+
129+
if is_anon || !fingerprint_style.reconstructible() {
130+
return DepKindVTable {
131+
is_anon,
132+
is_eval_always,
133+
fingerprint_style,
134+
force_from_dep_node: None,
135+
try_load_from_on_disk_cache: None,
136+
name: Q::NAME,
137+
};
138+
}
139+
140+
DepKindVTable {
141+
is_anon,
142+
is_eval_always,
143+
fingerprint_style,
144+
force_from_dep_node: Some(|tcx, dep_node, _| {
145+
force_from_dep_node_inner(Q::query_dispatcher(tcx), tcx, dep_node)
146+
}),
147+
try_load_from_on_disk_cache: Some(|tcx, dep_node| {
148+
try_load_from_on_disk_cache_inner(Q::query_dispatcher(tcx), tcx, dep_node)
149+
}),
150+
name: Q::NAME,
151+
}
152+
}
153+
154+
/// Helper module containing a [`DepKindVTable`] constructor for each dep kind,
155+
/// for use with [`rustc_middle::make_dep_kind_array`].
156+
///
157+
/// That macro will check that we gave it a constructor for every known dep kind.
158+
mod _dep_kind_vtable_ctors {
159+
// Re-export all of the vtable constructors for non-query and query dep kinds.
160+
161+
// Non-query vtable constructors are defined in normal code.
162+
pub(crate) use super::non_query::*;
163+
// Query vtable constructors are defined via a macro.
164+
pub(crate) use crate::_dep_kind_vtable_ctors_for_queries::*;
165+
}
166+
167+
pub fn make_dep_kind_vtables<'tcx>(
168+
arena: &'tcx rustc_middle::arena::Arena<'tcx>,
169+
) -> &'tcx [DepKindVTable<'tcx>] {
170+
// Create an array of vtables, one for each dep kind (non-query and query).
171+
let dep_kind_vtables: [DepKindVTable<'tcx>; _] =
172+
rustc_middle::make_dep_kind_array!(_dep_kind_vtable_ctors);
173+
arena.alloc_from_iter(dep_kind_vtables)
174+
}

compiler/rustc_query_impl/src/lib.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111
use std::marker::ConstParamTy;
1212

1313
use rustc_data_structures::sync::AtomicU64;
14-
use rustc_middle::arena::Arena;
15-
use rustc_middle::dep_graph::{self, DepKind, DepKindVTable, DepNode, DepNodeIndex};
14+
use rustc_middle::dep_graph::{self, DepKind, DepNode, DepNodeIndex};
1615
use rustc_middle::queries::{
1716
self, ExternProviders, Providers, QueryCaches, QueryEngine, QueryStates,
1817
};
@@ -26,18 +25,21 @@ use rustc_query_system::query::{
2625
};
2726
use rustc_span::{ErrorGuaranteed, Span};
2827

28+
pub use crate::dep_kind_vtables::make_dep_kind_vtables;
2929
pub use crate::job::{QueryMap, break_query_cycles, print_query_stack};
3030
pub use crate::plumbing::{QueryCtxt, query_key_hash_verify_all};
3131
use crate::plumbing::{encode_all_query_results, try_mark_green};
3232
use crate::profiling_support::QueryKeyStringCache;
3333
pub use crate::profiling_support::alloc_self_profile_query_strings;
3434
use crate::values::Value;
3535

36+
#[macro_use]
37+
mod plumbing;
38+
39+
mod dep_kind_vtables;
3640
mod error;
3741
mod execution;
3842
mod job;
39-
#[macro_use]
40-
mod plumbing;
4143
mod profiling_support;
4244
mod values;
4345

0 commit comments

Comments
 (0)