1 //! Nodes in the dependency graph.
2 //!
3 //! A node in the [dependency graph] is represented by a [`DepNode`].
4 //! A `DepNode` consists of a [`DepKind`] (which
5 //! specifies the kind of thing it represents, like a piece of HIR, MIR, etc.)
6 //! and a [`Fingerprint`], a 128-bit hash value, the exact meaning of which
7 //! depends on the node's `DepKind`. Together, the kind and the fingerprint
8 //! fully identify a dependency node, even across multiple compilation sessions.
9 //! In other words, the value of the fingerprint does not depend on anything
10 //! that is specific to a given compilation session, like an unpredictable
11 //! interning key (e.g., `NodeId`, `DefId`, `Symbol`) or the numeric value of a
12 //! pointer. The concept behind this could be compared to how git commit hashes
13 //! uniquely identify a given commit. The fingerprinting approach has
14 //! a few advantages:
15 //!
16 //! * A `DepNode` can simply be serialized to disk and loaded in another session
17 //! without the need to do any "rebasing" (like we have to do for Spans and
18 //! NodeIds) or "retracing" (like we had to do for `DefId` in earlier
19 //! implementations of the dependency graph).
20 //! * A `Fingerprint` is just a bunch of bits, which allows `DepNode` to
21 //! implement `Copy`, `Sync`, `Send`, `Freeze`, etc.
22 //! * Since we just have a bit pattern, `DepNode` can be mapped from disk into
23 //! memory without any post-processing (e.g., "abomination-style" pointer
24 //! reconstruction).
25 //! * Because a `DepNode` is self-contained, we can instantiate `DepNodes` that
26 //! refer to things that do not exist anymore. In previous implementations
27 //! `DepNode` contained a `DefId`. A `DepNode` referring to something that
28 //! had been removed between the previous and the current compilation session
29 //! could not be instantiated because the current compilation session
30 //! contained no `DefId` for thing that had been removed.
31 //!
32 //! `DepNode` definition happens in the `define_dep_nodes!()` macro. This macro
33 //! defines the `DepKind` enum. Each `DepKind` has its own parameters that are
34 //! needed at runtime in order to construct a valid `DepNode` fingerprint.
35 //! However, only `CompileCodegenUnit` and `CompileMonoItem` are constructed
36 //! explicitly (with `make_compile_codegen_unit` cq `make_compile_mono_item`).
37 //!
38 //! Because the macro sees what parameters a given `DepKind` requires, it can
39 //! "infer" some properties for each kind of `DepNode`:
40 //!
41 //! * Whether a `DepNode` of a given kind has any parameters at all. Some
42 //! `DepNode`s could represent global concepts with only one value.
43 //! * Whether it is possible, in principle, to reconstruct a query key from a
44 //! given `DepNode`. Many `DepKind`s only require a single `DefId` parameter,
45 //! in which case it is possible to map the node's fingerprint back to the
46 //! `DefId` it was computed from. In other cases, too much information gets
47 //! lost during fingerprint computation.
48 //!
49 //! `make_compile_codegen_unit` and `make_compile_mono_items`, together with
50 //! `DepNode::new()`, ensures that only valid `DepNode` instances can be
51 //! constructed. For example, the API does not allow for constructing
52 //! parameterless `DepNode`s with anything other than a zeroed out fingerprint.
53 //! More generally speaking, it relieves the user of the `DepNode` API of
54 //! having to know how to compute the expected fingerprint for a given set of
55 //! node parameters.
56 //!
57 //! [dependency graph]: https://rustc-dev-guide.rust-lang.org/query.html
58
59 use crate::mir::mono::MonoItem;
60 use crate::ty::TyCtxt;
61
62 use rustc_data_structures::fingerprint::Fingerprint;
63 use rustc_hir::def_id::{CrateNum, DefId, LocalDefId};
64 use rustc_hir::definitions::DefPathHash;
65 use rustc_hir::{HirId, ItemLocalId, OwnerId};
66 use rustc_query_system::dep_graph::FingerprintStyle;
67 use rustc_span::symbol::Symbol;
68 use std::hash::Hash;
69
70 pub use rustc_query_system::dep_graph::{DepContext, DepNodeParams};
71
72 macro_rules! define_dep_nodes {
73 (
74 $($(#[$attr:meta])*
75 [$($modifiers:tt)*] fn $variant:ident($($K:tt)*) -> $V:ty,)*) => {
76
77 #[macro_export]
78 macro_rules! make_dep_kind_array {
79 ($mod:ident) => {[ $($mod::$variant()),* ]};
80 }
81
82 /// This enum serves as an index into arrays built by `make_dep_kind_array`.
83 #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Encodable, Decodable)]
84 #[allow(non_camel_case_types)]
85 pub enum DepKind {
86 $( $( #[$attr] )* $variant),*
87 }
88
89 pub(super) fn dep_kind_from_label_string(label: &str) -> Result<DepKind, ()> {
90 match label {
91 $(stringify!($variant) => Ok(DepKind::$variant),)*
92 _ => Err(()),
93 }
94 }
95
96 /// Contains variant => str representations for constructing
97 /// DepNode groups for tests.
98 #[allow(dead_code, non_upper_case_globals)]
99 pub mod label_strs {
100 $(
101 pub const $variant: &str = stringify!($variant);
102 )*
103 }
104 };
105 }
106
107 rustc_query_append!(define_dep_nodes![
108 /// We use this for most things when incr. comp. is turned off.
109 [] fn Null() -> (),
110 /// We use this to create a forever-red node.
111 [] fn Red() -> (),
112 [] fn TraitSelect() -> (),
113 [] fn CompileCodegenUnit() -> (),
114 [] fn CompileMonoItem() -> (),
115 ]);
116
117 // WARNING: `construct` is generic and does not know that `CompileCodegenUnit` takes `Symbol`s as keys.
118 // Be very careful changing this type signature!
make_compile_codegen_unit(tcx: TyCtxt<'_>, name: Symbol) -> DepNode119 pub(crate) fn make_compile_codegen_unit(tcx: TyCtxt<'_>, name: Symbol) -> DepNode {
120 DepNode::construct(tcx, DepKind::CompileCodegenUnit, &name)
121 }
122
123 // WARNING: `construct` is generic and does not know that `CompileMonoItem` takes `MonoItem`s as keys.
124 // Be very careful changing this type signature!
make_compile_mono_item<'tcx>( tcx: TyCtxt<'tcx>, mono_item: &MonoItem<'tcx>, ) -> DepNode125 pub(crate) fn make_compile_mono_item<'tcx>(
126 tcx: TyCtxt<'tcx>,
127 mono_item: &MonoItem<'tcx>,
128 ) -> DepNode {
129 DepNode::construct(tcx, DepKind::CompileMonoItem, mono_item)
130 }
131
132 pub type DepNode = rustc_query_system::dep_graph::DepNode<DepKind>;
133
134 // We keep a lot of `DepNode`s in memory during compilation. It's not
135 // required that their size stay the same, but we don't want to change
136 // it inadvertently. This assert just ensures we're aware of any change.
137 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
138 static_assert_size!(DepNode, 18);
139
140 #[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
141 static_assert_size!(DepNode, 24);
142
143 pub trait DepNodeExt: Sized {
144 /// Extracts the DefId corresponding to this DepNode. This will work
145 /// if two conditions are met:
146 ///
147 /// 1. The Fingerprint of the DepNode actually is a DefPathHash, and
148 /// 2. the item that the DefPath refers to exists in the current tcx.
149 ///
150 /// Condition (1) is determined by the DepKind variant of the
151 /// DepNode. Condition (2) might not be fulfilled if a DepNode
152 /// refers to something from the previous compilation session that
153 /// has been removed.
extract_def_id(&self, tcx: TyCtxt<'_>) -> Option<DefId>154 fn extract_def_id(&self, tcx: TyCtxt<'_>) -> Option<DefId>;
155
156 /// Used in testing
from_label_string( tcx: TyCtxt<'_>, label: &str, def_path_hash: DefPathHash, ) -> Result<Self, ()>157 fn from_label_string(
158 tcx: TyCtxt<'_>,
159 label: &str,
160 def_path_hash: DefPathHash,
161 ) -> Result<Self, ()>;
162
163 /// Used in testing
has_label_string(label: &str) -> bool164 fn has_label_string(label: &str) -> bool;
165 }
166
167 impl DepNodeExt for DepNode {
168 /// Extracts the DefId corresponding to this DepNode. This will work
169 /// if two conditions are met:
170 ///
171 /// 1. The Fingerprint of the DepNode actually is a DefPathHash, and
172 /// 2. the item that the DefPath refers to exists in the current tcx.
173 ///
174 /// Condition (1) is determined by the DepKind variant of the
175 /// DepNode. Condition (2) might not be fulfilled if a DepNode
176 /// refers to something from the previous compilation session that
177 /// has been removed.
extract_def_id(&self, tcx: TyCtxt<'_>) -> Option<DefId>178 fn extract_def_id(&self, tcx: TyCtxt<'_>) -> Option<DefId> {
179 if tcx.fingerprint_style(self.kind) == FingerprintStyle::DefPathHash {
180 Some(tcx.def_path_hash_to_def_id(DefPathHash(self.hash.into()), &mut || {
181 panic!("Failed to extract DefId: {:?} {}", self.kind, self.hash)
182 }))
183 } else {
184 None
185 }
186 }
187
188 /// Used in testing
from_label_string( tcx: TyCtxt<'_>, label: &str, def_path_hash: DefPathHash, ) -> Result<DepNode, ()>189 fn from_label_string(
190 tcx: TyCtxt<'_>,
191 label: &str,
192 def_path_hash: DefPathHash,
193 ) -> Result<DepNode, ()> {
194 let kind = dep_kind_from_label_string(label)?;
195
196 match tcx.fingerprint_style(kind) {
197 FingerprintStyle::Opaque | FingerprintStyle::HirId => Err(()),
198 FingerprintStyle::Unit => Ok(DepNode::new_no_params(tcx, kind)),
199 FingerprintStyle::DefPathHash => {
200 Ok(DepNode::from_def_path_hash(tcx, def_path_hash, kind))
201 }
202 }
203 }
204
205 /// Used in testing
has_label_string(label: &str) -> bool206 fn has_label_string(label: &str) -> bool {
207 dep_kind_from_label_string(label).is_ok()
208 }
209 }
210
211 impl<'tcx> DepNodeParams<TyCtxt<'tcx>> for () {
212 #[inline(always)]
fingerprint_style() -> FingerprintStyle213 fn fingerprint_style() -> FingerprintStyle {
214 FingerprintStyle::Unit
215 }
216
217 #[inline(always)]
to_fingerprint(&self, _: TyCtxt<'tcx>) -> Fingerprint218 fn to_fingerprint(&self, _: TyCtxt<'tcx>) -> Fingerprint {
219 Fingerprint::ZERO
220 }
221
222 #[inline(always)]
recover(_: TyCtxt<'tcx>, _: &DepNode) -> Option<Self>223 fn recover(_: TyCtxt<'tcx>, _: &DepNode) -> Option<Self> {
224 Some(())
225 }
226 }
227
228 impl<'tcx> DepNodeParams<TyCtxt<'tcx>> for DefId {
229 #[inline(always)]
fingerprint_style() -> FingerprintStyle230 fn fingerprint_style() -> FingerprintStyle {
231 FingerprintStyle::DefPathHash
232 }
233
234 #[inline(always)]
to_fingerprint(&self, tcx: TyCtxt<'tcx>) -> Fingerprint235 fn to_fingerprint(&self, tcx: TyCtxt<'tcx>) -> Fingerprint {
236 tcx.def_path_hash(*self).0
237 }
238
239 #[inline(always)]
to_debug_str(&self, tcx: TyCtxt<'tcx>) -> String240 fn to_debug_str(&self, tcx: TyCtxt<'tcx>) -> String {
241 tcx.def_path_str(*self)
242 }
243
244 #[inline(always)]
recover(tcx: TyCtxt<'tcx>, dep_node: &DepNode) -> Option<Self>245 fn recover(tcx: TyCtxt<'tcx>, dep_node: &DepNode) -> Option<Self> {
246 dep_node.extract_def_id(tcx)
247 }
248 }
249
250 impl<'tcx> DepNodeParams<TyCtxt<'tcx>> for LocalDefId {
251 #[inline(always)]
fingerprint_style() -> FingerprintStyle252 fn fingerprint_style() -> FingerprintStyle {
253 FingerprintStyle::DefPathHash
254 }
255
256 #[inline(always)]
to_fingerprint(&self, tcx: TyCtxt<'tcx>) -> Fingerprint257 fn to_fingerprint(&self, tcx: TyCtxt<'tcx>) -> Fingerprint {
258 self.to_def_id().to_fingerprint(tcx)
259 }
260
261 #[inline(always)]
to_debug_str(&self, tcx: TyCtxt<'tcx>) -> String262 fn to_debug_str(&self, tcx: TyCtxt<'tcx>) -> String {
263 self.to_def_id().to_debug_str(tcx)
264 }
265
266 #[inline(always)]
recover(tcx: TyCtxt<'tcx>, dep_node: &DepNode) -> Option<Self>267 fn recover(tcx: TyCtxt<'tcx>, dep_node: &DepNode) -> Option<Self> {
268 dep_node.extract_def_id(tcx).map(|id| id.expect_local())
269 }
270 }
271
272 impl<'tcx> DepNodeParams<TyCtxt<'tcx>> for OwnerId {
273 #[inline(always)]
fingerprint_style() -> FingerprintStyle274 fn fingerprint_style() -> FingerprintStyle {
275 FingerprintStyle::DefPathHash
276 }
277
278 #[inline(always)]
to_fingerprint(&self, tcx: TyCtxt<'tcx>) -> Fingerprint279 fn to_fingerprint(&self, tcx: TyCtxt<'tcx>) -> Fingerprint {
280 self.to_def_id().to_fingerprint(tcx)
281 }
282
283 #[inline(always)]
to_debug_str(&self, tcx: TyCtxt<'tcx>) -> String284 fn to_debug_str(&self, tcx: TyCtxt<'tcx>) -> String {
285 self.to_def_id().to_debug_str(tcx)
286 }
287
288 #[inline(always)]
recover(tcx: TyCtxt<'tcx>, dep_node: &DepNode) -> Option<Self>289 fn recover(tcx: TyCtxt<'tcx>, dep_node: &DepNode) -> Option<Self> {
290 dep_node.extract_def_id(tcx).map(|id| OwnerId { def_id: id.expect_local() })
291 }
292 }
293
294 impl<'tcx> DepNodeParams<TyCtxt<'tcx>> for CrateNum {
295 #[inline(always)]
fingerprint_style() -> FingerprintStyle296 fn fingerprint_style() -> FingerprintStyle {
297 FingerprintStyle::DefPathHash
298 }
299
300 #[inline(always)]
to_fingerprint(&self, tcx: TyCtxt<'tcx>) -> Fingerprint301 fn to_fingerprint(&self, tcx: TyCtxt<'tcx>) -> Fingerprint {
302 let def_id = self.as_def_id();
303 def_id.to_fingerprint(tcx)
304 }
305
306 #[inline(always)]
to_debug_str(&self, tcx: TyCtxt<'tcx>) -> String307 fn to_debug_str(&self, tcx: TyCtxt<'tcx>) -> String {
308 tcx.crate_name(*self).to_string()
309 }
310
311 #[inline(always)]
recover(tcx: TyCtxt<'tcx>, dep_node: &DepNode) -> Option<Self>312 fn recover(tcx: TyCtxt<'tcx>, dep_node: &DepNode) -> Option<Self> {
313 dep_node.extract_def_id(tcx).map(|id| id.krate)
314 }
315 }
316
317 impl<'tcx> DepNodeParams<TyCtxt<'tcx>> for (DefId, DefId) {
318 #[inline(always)]
fingerprint_style() -> FingerprintStyle319 fn fingerprint_style() -> FingerprintStyle {
320 FingerprintStyle::Opaque
321 }
322
323 // We actually would not need to specialize the implementation of this
324 // method but it's faster to combine the hashes than to instantiate a full
325 // hashing context and stable-hashing state.
326 #[inline(always)]
to_fingerprint(&self, tcx: TyCtxt<'tcx>) -> Fingerprint327 fn to_fingerprint(&self, tcx: TyCtxt<'tcx>) -> Fingerprint {
328 let (def_id_0, def_id_1) = *self;
329
330 let def_path_hash_0 = tcx.def_path_hash(def_id_0);
331 let def_path_hash_1 = tcx.def_path_hash(def_id_1);
332
333 def_path_hash_0.0.combine(def_path_hash_1.0)
334 }
335
336 #[inline(always)]
to_debug_str(&self, tcx: TyCtxt<'tcx>) -> String337 fn to_debug_str(&self, tcx: TyCtxt<'tcx>) -> String {
338 let (def_id_0, def_id_1) = *self;
339
340 format!("({}, {})", tcx.def_path_debug_str(def_id_0), tcx.def_path_debug_str(def_id_1))
341 }
342 }
343
344 impl<'tcx> DepNodeParams<TyCtxt<'tcx>> for HirId {
345 #[inline(always)]
fingerprint_style() -> FingerprintStyle346 fn fingerprint_style() -> FingerprintStyle {
347 FingerprintStyle::HirId
348 }
349
350 // We actually would not need to specialize the implementation of this
351 // method but it's faster to combine the hashes than to instantiate a full
352 // hashing context and stable-hashing state.
353 #[inline(always)]
to_fingerprint(&self, tcx: TyCtxt<'tcx>) -> Fingerprint354 fn to_fingerprint(&self, tcx: TyCtxt<'tcx>) -> Fingerprint {
355 let HirId { owner, local_id } = *self;
356 let def_path_hash = tcx.def_path_hash(owner.to_def_id());
357 Fingerprint::new(
358 // `owner` is local, so is completely defined by the local hash
359 def_path_hash.local_hash(),
360 local_id.as_u32() as u64,
361 )
362 }
363
364 #[inline(always)]
to_debug_str(&self, tcx: TyCtxt<'tcx>) -> String365 fn to_debug_str(&self, tcx: TyCtxt<'tcx>) -> String {
366 let HirId { owner, local_id } = *self;
367 format!("{}.{}", tcx.def_path_str(owner), local_id.as_u32())
368 }
369
370 #[inline(always)]
recover(tcx: TyCtxt<'tcx>, dep_node: &DepNode) -> Option<Self>371 fn recover(tcx: TyCtxt<'tcx>, dep_node: &DepNode) -> Option<Self> {
372 if tcx.fingerprint_style(dep_node.kind) == FingerprintStyle::HirId {
373 let (local_hash, local_id) = Fingerprint::from(dep_node.hash).split();
374 let def_path_hash = DefPathHash::new(tcx.sess.local_stable_crate_id(), local_hash);
375 let def_id = tcx
376 .def_path_hash_to_def_id(def_path_hash, &mut || {
377 panic!("Failed to extract HirId: {:?} {}", dep_node.kind, dep_node.hash)
378 })
379 .expect_local();
380 let local_id = local_id
381 .as_u64()
382 .try_into()
383 .unwrap_or_else(|_| panic!("local id should be u32, found {:?}", local_id));
384 Some(HirId { owner: OwnerId { def_id }, local_id: ItemLocalId::from_u32(local_id) })
385 } else {
386 None
387 }
388 }
389 }
390