1 //! Nodes in the dependency graph.
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
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
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.
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`).
38 //! Because the macro sees what parameters a given `DepKind` requires, it can
39 //! "infer" some properties for each kind of `DepNode`:
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.
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
57 //! [dependency graph]: https://rustc-dev-guide.rust-lang.org/query.html
59 use crate::mir::mono::MonoItem;
60 use crate::ty::TyCtxt;
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;
70 pub use rustc_query_system::dep_graph::{DepContext, DepNodeParams};
72 macro_rules! define_dep_nodes {
75 [$($modifiers:tt)*] fn $variant:ident($($K:tt)*) -> $V:ty,)*) => {
78 macro_rules! make_dep_kind_array {
79 ($mod:ident) => {[ $($mod::$variant()),* ]};
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)]
86 $( $( #[$attr] )* $variant),*
89 pub(super) fn dep_kind_from_label_string(label: &str) -> Result<DepKind, ()> {
91 $(stringify!($variant) => Ok(DepKind::$variant),)*
96 /// Contains variant => str representations for constructing
97 /// DepNode groups for tests.
98 #[allow(dead_code, non_upper_case_globals)]
101 pub const $variant: &str = stringify!($variant);
107 rustc_query_append!(define_dep_nodes![
108 /// We use this for most things when incr. comp. is turned off.
110 /// We use this to create a forever-red node.
112 [] fn TraitSelect() -> (),
113 [] fn CompileCodegenUnit() -> (),
114 [] fn CompileMonoItem() -> (),
117 // WARNING: `construct` is generic and does not know that `CompileCodegenUnit` takes `Symbol`s as keys.
118 // Be very careful changing this type signature!
119 pub(crate) fn make_compile_codegen_unit(tcx: TyCtxt<'_>, name: Symbol) -> DepNode {
120 DepNode::construct(tcx, DepKind::CompileCodegenUnit, &name)
123 // WARNING: `construct` is generic and does not know that `CompileMonoItem` takes `MonoItem`s as keys.
124 // Be very careful changing this type signature!
125 pub(crate) fn make_compile_mono_item<'tcx>(
127 mono_item: &MonoItem<'tcx>,
129 DepNode::construct(tcx, DepKind::CompileMonoItem, mono_item)
132 pub type DepNode = rustc_query_system::dep_graph::DepNode<DepKind>;
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);
140 #[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
141 static_assert_size!(DepNode, 24);
143 pub trait DepNodeExt: Sized {
144 /// Extracts the DefId corresponding to this DepNode. This will work
145 /// if two conditions are met:
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.
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.
154 fn extract_def_id(&self, tcx: TyCtxt<'_>) -> Option<DefId>;
157 fn from_label_string(
160 def_path_hash: DefPathHash,
161 ) -> Result<Self, ()>;
164 fn has_label_string(label: &str) -> bool;
167 impl DepNodeExt for DepNode {
168 /// Extracts the DefId corresponding to this DepNode. This will work
169 /// if two conditions are met:
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.
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.
178 fn extract_def_id<'tcx>(&self, tcx: TyCtxt<'tcx>) -> 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)
189 fn from_label_string(
192 def_path_hash: DefPathHash,
193 ) -> Result<DepNode, ()> {
194 let kind = dep_kind_from_label_string(label)?;
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))
206 fn has_label_string(label: &str) -> bool {
207 dep_kind_from_label_string(label).is_ok()
211 impl<'tcx> DepNodeParams<TyCtxt<'tcx>> for () {
213 fn fingerprint_style() -> FingerprintStyle {
214 FingerprintStyle::Unit
218 fn to_fingerprint(&self, _: TyCtxt<'tcx>) -> Fingerprint {
223 fn recover(_: TyCtxt<'tcx>, _: &DepNode) -> Option<Self> {
228 impl<'tcx> DepNodeParams<TyCtxt<'tcx>> for DefId {
230 fn fingerprint_style() -> FingerprintStyle {
231 FingerprintStyle::DefPathHash
235 fn to_fingerprint(&self, tcx: TyCtxt<'tcx>) -> Fingerprint {
236 tcx.def_path_hash(*self).0
240 fn to_debug_str(&self, tcx: TyCtxt<'tcx>) -> String {
241 tcx.def_path_str(*self)
245 fn recover(tcx: TyCtxt<'tcx>, dep_node: &DepNode) -> Option<Self> {
246 dep_node.extract_def_id(tcx)
250 impl<'tcx> DepNodeParams<TyCtxt<'tcx>> for LocalDefId {
252 fn fingerprint_style() -> FingerprintStyle {
253 FingerprintStyle::DefPathHash
257 fn to_fingerprint(&self, tcx: TyCtxt<'tcx>) -> Fingerprint {
258 self.to_def_id().to_fingerprint(tcx)
262 fn to_debug_str(&self, tcx: TyCtxt<'tcx>) -> String {
263 self.to_def_id().to_debug_str(tcx)
267 fn recover(tcx: TyCtxt<'tcx>, dep_node: &DepNode) -> Option<Self> {
268 dep_node.extract_def_id(tcx).map(|id| id.expect_local())
272 impl<'tcx> DepNodeParams<TyCtxt<'tcx>> for OwnerId {
274 fn fingerprint_style() -> FingerprintStyle {
275 FingerprintStyle::DefPathHash
279 fn to_fingerprint(&self, tcx: TyCtxt<'tcx>) -> Fingerprint {
280 self.to_def_id().to_fingerprint(tcx)
284 fn to_debug_str(&self, tcx: TyCtxt<'tcx>) -> String {
285 self.to_def_id().to_debug_str(tcx)
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() })
294 impl<'tcx> DepNodeParams<TyCtxt<'tcx>> for CrateNum {
296 fn fingerprint_style() -> FingerprintStyle {
297 FingerprintStyle::DefPathHash
301 fn to_fingerprint(&self, tcx: TyCtxt<'tcx>) -> Fingerprint {
302 let def_id = self.as_def_id();
303 def_id.to_fingerprint(tcx)
307 fn to_debug_str(&self, tcx: TyCtxt<'tcx>) -> String {
308 tcx.crate_name(*self).to_string()
312 fn recover(tcx: TyCtxt<'tcx>, dep_node: &DepNode) -> Option<Self> {
313 dep_node.extract_def_id(tcx).map(|id| id.krate)
317 impl<'tcx> DepNodeParams<TyCtxt<'tcx>> for (DefId, DefId) {
319 fn fingerprint_style() -> FingerprintStyle {
320 FingerprintStyle::Opaque
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.
327 fn to_fingerprint(&self, tcx: TyCtxt<'tcx>) -> Fingerprint {
328 let (def_id_0, def_id_1) = *self;
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);
333 def_path_hash_0.0.combine(def_path_hash_1.0)
337 fn to_debug_str(&self, tcx: TyCtxt<'tcx>) -> String {
338 let (def_id_0, def_id_1) = *self;
340 format!("({}, {})", tcx.def_path_debug_str(def_id_0), tcx.def_path_debug_str(def_id_1))
344 impl<'tcx> DepNodeParams<TyCtxt<'tcx>> for HirId {
346 fn fingerprint_style() -> FingerprintStyle {
347 FingerprintStyle::HirId
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.
354 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());
358 // `owner` is local, so is completely defined by the local hash
359 def_path_hash.local_hash(),
360 local_id.as_u32().into(),
365 fn to_debug_str(&self, tcx: TyCtxt<'tcx>) -> String {
366 let HirId { owner, local_id } = *self;
367 format!("{}.{}", tcx.def_path_str(owner.to_def_id()), local_id.as_u32())
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).as_value();
374 let def_path_hash = DefPathHash::new(tcx.sess.local_stable_crate_id(), local_hash);
376 .def_path_hash_to_def_id(def_path_hash, &mut || {
377 panic!("Failed to extract HirId: {:?} {}", dep_node.kind, dep_node.hash)
380 let local_id = local_id
382 .unwrap_or_else(|_| panic!("local id should be u32, found {:?}", local_id));
383 Some(HirId { owner: OwnerId { def_id }, local_id: ItemLocalId::from_u32(local_id) })