]> git.proxmox.com Git - rustc.git/blob - compiler/rustc_middle/src/dep_graph/dep_node.rs
New upstream version 1.50.0+dfsg1
[rustc.git] / compiler / rustc_middle / src / dep_graph / dep_node.rs
1 //! This module defines the `DepNode` type which the compiler uses to represent
2 //! nodes in the dependency graph.
3 //!
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 and has a few advantages:
14 //!
15 //! * A `DepNode` can simply be serialized to disk and loaded in another session
16 //! without the need to do any "rebasing" (like we have to do for Spans and
17 //! NodeIds) or "retracing" (like we had to do for `DefId` in earlier
18 //! implementations of the dependency graph).
19 //! * A `Fingerprint` is just a bunch of bits, which allows `DepNode` to
20 //! implement `Copy`, `Sync`, `Send`, `Freeze`, etc.
21 //! * Since we just have a bit pattern, `DepNode` can be mapped from disk into
22 //! memory without any post-processing (e.g., "abomination-style" pointer
23 //! reconstruction).
24 //! * Because a `DepNode` is self-contained, we can instantiate `DepNodes` that
25 //! refer to things that do not exist anymore. In previous implementations
26 //! `DepNode` contained a `DefId`. A `DepNode` referring to something that
27 //! had been removed between the previous and the current compilation session
28 //! could not be instantiated because the current compilation session
29 //! contained no `DefId` for thing that had been removed.
30 //!
31 //! `DepNode` definition happens in the `define_dep_nodes!()` macro. This macro
32 //! defines the `DepKind` enum and a corresponding `DepConstructor` enum. The
33 //! `DepConstructor` enum links a `DepKind` to the parameters that are needed at
34 //! runtime in order to construct a valid `DepNode` fingerprint.
35 //!
36 //! Because the macro sees what parameters a given `DepKind` requires, it can
37 //! "infer" some properties for each kind of `DepNode`:
38 //!
39 //! * Whether a `DepNode` of a given kind has any parameters at all. Some
40 //! `DepNode`s could represent global concepts with only one value.
41 //! * Whether it is possible, in principle, to reconstruct a query key from a
42 //! given `DepNode`. Many `DepKind`s only require a single `DefId` parameter,
43 //! in which case it is possible to map the node's fingerprint back to the
44 //! `DefId` it was computed from. In other cases, too much information gets
45 //! lost during fingerprint computation.
46 //!
47 //! The `DepConstructor` enum, together with `DepNode::new()`, ensures that only
48 //! valid `DepNode` instances can be constructed. For example, the API does not
49 //! allow for constructing parameterless `DepNode`s with anything other
50 //! than a zeroed out fingerprint. More generally speaking, it relieves the
51 //! user of the `DepNode` API of having to know how to compute the expected
52 //! fingerprint for a given set of node parameters.
53
54 use crate::mir::interpret::{GlobalId, LitToConstInput};
55 use crate::traits;
56 use crate::traits::query::{
57 CanonicalPredicateGoal, CanonicalProjectionGoal, CanonicalTyGoal,
58 CanonicalTypeOpAscribeUserTypeGoal, CanonicalTypeOpEqGoal, CanonicalTypeOpNormalizeGoal,
59 CanonicalTypeOpProvePredicateGoal, CanonicalTypeOpSubtypeGoal,
60 };
61 use crate::ty::subst::{GenericArg, SubstsRef};
62 use crate::ty::{self, ParamEnvAnd, Ty, TyCtxt};
63
64 use rustc_data_structures::fingerprint::Fingerprint;
65 use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, CRATE_DEF_INDEX};
66 use rustc_hir::definitions::DefPathHash;
67 use rustc_hir::HirId;
68 use rustc_span::symbol::Symbol;
69 use std::hash::Hash;
70
71 pub use rustc_query_system::dep_graph::{DepContext, DepNodeParams};
72
73 // erase!() just makes tokens go away. It's used to specify which macro argument
74 // is repeated (i.e., which sub-expression of the macro we are in) but don't need
75 // to actually use any of the arguments.
76 macro_rules! erase {
77 ($x:tt) => {{}};
78 }
79
80 macro_rules! is_anon_attr {
81 (anon) => {
82 true
83 };
84 ($attr:ident) => {
85 false
86 };
87 }
88
89 macro_rules! is_eval_always_attr {
90 (eval_always) => {
91 true
92 };
93 ($attr:ident) => {
94 false
95 };
96 }
97
98 macro_rules! contains_anon_attr {
99 ($($attr:ident $(($($attr_args:tt)*))* ),*) => ({$(is_anon_attr!($attr) | )* false});
100 }
101
102 macro_rules! contains_eval_always_attr {
103 ($($attr:ident $(($($attr_args:tt)*))* ),*) => ({$(is_eval_always_attr!($attr) | )* false});
104 }
105
106 macro_rules! define_dep_nodes {
107 (<$tcx:tt>
108 $(
109 [$($attrs:tt)*]
110 $variant:ident $(( $tuple_arg_ty:ty $(,)? ))*
111 ,)*
112 ) => (
113 #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Encodable, Decodable)]
114 #[allow(non_camel_case_types)]
115 pub enum DepKind {
116 $($variant),*
117 }
118
119 impl DepKind {
120 #[allow(unreachable_code)]
121 pub fn can_reconstruct_query_key<$tcx>(&self) -> bool {
122 match *self {
123 $(
124 DepKind :: $variant => {
125 if contains_anon_attr!($($attrs)*) {
126 return false;
127 }
128
129 // tuple args
130 $({
131 return <$tuple_arg_ty as DepNodeParams<TyCtxt<'_>>>
132 ::can_reconstruct_query_key();
133 })*
134
135 true
136 }
137 )*
138 }
139 }
140
141 pub fn is_anon(&self) -> bool {
142 match *self {
143 $(
144 DepKind :: $variant => { contains_anon_attr!($($attrs)*) }
145 )*
146 }
147 }
148
149 pub fn is_eval_always(&self) -> bool {
150 match *self {
151 $(
152 DepKind :: $variant => { contains_eval_always_attr!($($attrs)*) }
153 )*
154 }
155 }
156
157 #[allow(unreachable_code)]
158 pub fn has_params(&self) -> bool {
159 match *self {
160 $(
161 DepKind :: $variant => {
162 // tuple args
163 $({
164 erase!($tuple_arg_ty);
165 return true;
166 })*
167
168 false
169 }
170 )*
171 }
172 }
173 }
174
175 pub struct DepConstructor;
176
177 #[allow(non_camel_case_types)]
178 impl DepConstructor {
179 $(
180 #[inline(always)]
181 #[allow(unreachable_code, non_snake_case)]
182 pub fn $variant(_tcx: TyCtxt<'_>, $(arg: $tuple_arg_ty)*) -> DepNode {
183 // tuple args
184 $({
185 erase!($tuple_arg_ty);
186 return DepNode::construct(_tcx, DepKind::$variant, &arg)
187 })*
188
189 return DepNode::construct(_tcx, DepKind::$variant, &())
190 }
191 )*
192 }
193
194 pub type DepNode = rustc_query_system::dep_graph::DepNode<DepKind>;
195
196 // We keep a lot of `DepNode`s in memory during compilation. It's not
197 // required that their size stay the same, but we don't want to change
198 // it inadvertently. This assert just ensures we're aware of any change.
199 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
200 static_assert_size!(DepNode, 17);
201
202 #[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
203 static_assert_size!(DepNode, 24);
204
205 pub trait DepNodeExt: Sized {
206 /// Construct a DepNode from the given DepKind and DefPathHash. This
207 /// method will assert that the given DepKind actually requires a
208 /// single DefId/DefPathHash parameter.
209 fn from_def_path_hash(def_path_hash: DefPathHash, kind: DepKind) -> Self;
210
211 /// Extracts the DefId corresponding to this DepNode. This will work
212 /// if two conditions are met:
213 ///
214 /// 1. The Fingerprint of the DepNode actually is a DefPathHash, and
215 /// 2. the item that the DefPath refers to exists in the current tcx.
216 ///
217 /// Condition (1) is determined by the DepKind variant of the
218 /// DepNode. Condition (2) might not be fulfilled if a DepNode
219 /// refers to something from the previous compilation session that
220 /// has been removed.
221 fn extract_def_id(&self, tcx: TyCtxt<'_>) -> Option<DefId>;
222
223 /// Used in testing
224 fn from_label_string(label: &str, def_path_hash: DefPathHash)
225 -> Result<Self, ()>;
226
227 /// Used in testing
228 fn has_label_string(label: &str) -> bool;
229 }
230
231 impl DepNodeExt for DepNode {
232 /// Construct a DepNode from the given DepKind and DefPathHash. This
233 /// method will assert that the given DepKind actually requires a
234 /// single DefId/DefPathHash parameter.
235 fn from_def_path_hash(def_path_hash: DefPathHash, kind: DepKind) -> DepNode {
236 debug_assert!(kind.can_reconstruct_query_key() && kind.has_params());
237 DepNode {
238 kind,
239 hash: def_path_hash.0.into(),
240 }
241 }
242
243 /// Extracts the DefId corresponding to this DepNode. This will work
244 /// if two conditions are met:
245 ///
246 /// 1. The Fingerprint of the DepNode actually is a DefPathHash, and
247 /// 2. the item that the DefPath refers to exists in the current tcx.
248 ///
249 /// Condition (1) is determined by the DepKind variant of the
250 /// DepNode. Condition (2) might not be fulfilled if a DepNode
251 /// refers to something from the previous compilation session that
252 /// has been removed.
253 fn extract_def_id(&self, tcx: TyCtxt<'tcx>) -> Option<DefId> {
254 if self.kind.can_reconstruct_query_key() {
255 tcx.queries.on_disk_cache.as_ref()?.def_path_hash_to_def_id(tcx, DefPathHash(self.hash.into()))
256 } else {
257 None
258 }
259 }
260
261 /// Used in testing
262 fn from_label_string(label: &str, def_path_hash: DefPathHash) -> Result<DepNode, ()> {
263 let kind = match label {
264 $(
265 stringify!($variant) => DepKind::$variant,
266 )*
267 _ => return Err(()),
268 };
269
270 if !kind.can_reconstruct_query_key() {
271 return Err(());
272 }
273
274 if kind.has_params() {
275 Ok(DepNode::from_def_path_hash(def_path_hash, kind))
276 } else {
277 Ok(DepNode::new_no_params(kind))
278 }
279 }
280
281 /// Used in testing
282 fn has_label_string(label: &str) -> bool {
283 match label {
284 $(
285 stringify!($variant) => true,
286 )*
287 _ => false,
288 }
289 }
290 }
291
292 /// Contains variant => str representations for constructing
293 /// DepNode groups for tests.
294 #[allow(dead_code, non_upper_case_globals)]
295 pub mod label_strs {
296 $(
297 pub const $variant: &str = stringify!($variant);
298 )*
299 }
300 );
301 }
302
303 rustc_dep_node_append!([define_dep_nodes!][ <'tcx>
304 // We use this for most things when incr. comp. is turned off.
305 [] Null,
306
307 // Represents metadata from an extern crate.
308 [eval_always] CrateMetadata(CrateNum),
309
310 [anon] TraitSelect,
311
312 [] CompileCodegenUnit(Symbol),
313 ]);
314
315 impl<'tcx> DepNodeParams<TyCtxt<'tcx>> for DefId {
316 #[inline]
317 fn can_reconstruct_query_key() -> bool {
318 true
319 }
320
321 fn to_fingerprint(&self, tcx: TyCtxt<'tcx>) -> Fingerprint {
322 let hash = tcx.def_path_hash(*self);
323 // If this is a foreign `DefId`, store its current value
324 // in the incremental cache. When we decode the cache,
325 // we will use the old DefIndex as an initial guess for
326 // a lookup into the crate metadata.
327 if !self.is_local() {
328 if let Some(cache) = &tcx.queries.on_disk_cache {
329 cache.store_foreign_def_id_hash(*self, hash);
330 }
331 }
332 hash.0
333 }
334
335 fn to_debug_str(&self, tcx: TyCtxt<'tcx>) -> String {
336 tcx.def_path_str(*self)
337 }
338
339 fn recover(tcx: TyCtxt<'tcx>, dep_node: &DepNode) -> Option<Self> {
340 dep_node.extract_def_id(tcx)
341 }
342 }
343
344 impl<'tcx> DepNodeParams<TyCtxt<'tcx>> for LocalDefId {
345 #[inline]
346 fn can_reconstruct_query_key() -> bool {
347 true
348 }
349
350 fn to_fingerprint(&self, tcx: TyCtxt<'tcx>) -> Fingerprint {
351 self.to_def_id().to_fingerprint(tcx)
352 }
353
354 fn to_debug_str(&self, tcx: TyCtxt<'tcx>) -> String {
355 self.to_def_id().to_debug_str(tcx)
356 }
357
358 fn recover(tcx: TyCtxt<'tcx>, dep_node: &DepNode) -> Option<Self> {
359 dep_node.extract_def_id(tcx).map(|id| id.expect_local())
360 }
361 }
362
363 impl<'tcx> DepNodeParams<TyCtxt<'tcx>> for CrateNum {
364 #[inline]
365 fn can_reconstruct_query_key() -> bool {
366 true
367 }
368
369 fn to_fingerprint(&self, tcx: TyCtxt<'tcx>) -> Fingerprint {
370 let def_id = DefId { krate: *self, index: CRATE_DEF_INDEX };
371 def_id.to_fingerprint(tcx)
372 }
373
374 fn to_debug_str(&self, tcx: TyCtxt<'tcx>) -> String {
375 tcx.crate_name(*self).to_string()
376 }
377
378 fn recover(tcx: TyCtxt<'tcx>, dep_node: &DepNode) -> Option<Self> {
379 dep_node.extract_def_id(tcx).map(|id| id.krate)
380 }
381 }
382
383 impl<'tcx> DepNodeParams<TyCtxt<'tcx>> for (DefId, DefId) {
384 #[inline]
385 fn can_reconstruct_query_key() -> bool {
386 false
387 }
388
389 // We actually would not need to specialize the implementation of this
390 // method but it's faster to combine the hashes than to instantiate a full
391 // hashing context and stable-hashing state.
392 fn to_fingerprint(&self, tcx: TyCtxt<'tcx>) -> Fingerprint {
393 let (def_id_0, def_id_1) = *self;
394
395 let def_path_hash_0 = tcx.def_path_hash(def_id_0);
396 let def_path_hash_1 = tcx.def_path_hash(def_id_1);
397
398 def_path_hash_0.0.combine(def_path_hash_1.0)
399 }
400
401 fn to_debug_str(&self, tcx: TyCtxt<'tcx>) -> String {
402 let (def_id_0, def_id_1) = *self;
403
404 format!("({}, {})", tcx.def_path_debug_str(def_id_0), tcx.def_path_debug_str(def_id_1))
405 }
406 }
407
408 impl<'tcx> DepNodeParams<TyCtxt<'tcx>> for HirId {
409 #[inline]
410 fn can_reconstruct_query_key() -> bool {
411 false
412 }
413
414 // We actually would not need to specialize the implementation of this
415 // method but it's faster to combine the hashes than to instantiate a full
416 // hashing context and stable-hashing state.
417 fn to_fingerprint(&self, tcx: TyCtxt<'tcx>) -> Fingerprint {
418 let HirId { owner, local_id } = *self;
419
420 let def_path_hash = tcx.def_path_hash(owner.to_def_id());
421 let local_id = Fingerprint::from_smaller_hash(local_id.as_u32().into());
422
423 def_path_hash.0.combine(local_id)
424 }
425 }