]> git.proxmox.com Git - rustc.git/blob - compiler/rustc_query_system/src/dep_graph/mod.rs
New upstream version 1.68.2+dfsg1
[rustc.git] / compiler / rustc_query_system / src / dep_graph / mod.rs
1 pub mod debug;
2 mod dep_node;
3 mod graph;
4 mod query;
5 mod serialized;
6
7 pub use dep_node::{DepKindStruct, DepNode, DepNodeParams, WorkProductId};
8 pub use graph::{
9 hash_result, DepGraph, DepNodeColor, DepNodeIndex, TaskDeps, TaskDepsRef, WorkProduct,
10 };
11 pub use query::DepGraphQuery;
12 pub use serialized::{SerializedDepGraph, SerializedDepNodeIndex};
13
14 use crate::ich::StableHashingContext;
15 use rustc_data_structures::profiling::SelfProfilerRef;
16 use rustc_serialize::{opaque::FileEncoder, Encodable};
17 use rustc_session::Session;
18
19 use std::fmt;
20 use std::hash::Hash;
21
22 pub trait DepContext: Copy {
23 type DepKind: self::DepKind;
24
25 /// Create a hashing context for hashing new results.
26 fn with_stable_hashing_context<R>(&self, f: impl FnOnce(StableHashingContext<'_>) -> R) -> R;
27
28 /// Access the DepGraph.
29 fn dep_graph(&self) -> &DepGraph<Self::DepKind>;
30
31 /// Access the profiler.
32 fn profiler(&self) -> &SelfProfilerRef;
33
34 /// Access the compiler session.
35 fn sess(&self) -> &Session;
36
37 fn dep_kind_info(&self, dep_node: Self::DepKind) -> &DepKindStruct<Self>;
38
39 #[inline(always)]
40 fn fingerprint_style(&self, kind: Self::DepKind) -> FingerprintStyle {
41 let data = self.dep_kind_info(kind);
42 if data.is_anon {
43 return FingerprintStyle::Opaque;
44 }
45 data.fingerprint_style
46 }
47
48 #[inline(always)]
49 /// Return whether this kind always require evaluation.
50 fn is_eval_always(&self, kind: Self::DepKind) -> bool {
51 self.dep_kind_info(kind).is_eval_always
52 }
53
54 /// Try to force a dep node to execute and see if it's green.
55 #[instrument(skip(self), level = "debug")]
56 fn try_force_from_dep_node(self, dep_node: DepNode<Self::DepKind>) -> bool {
57 let cb = self.dep_kind_info(dep_node.kind);
58 if let Some(f) = cb.force_from_dep_node {
59 f(self, dep_node);
60 true
61 } else {
62 false
63 }
64 }
65
66 /// Load data from the on-disk cache.
67 fn try_load_from_on_disk_cache(self, dep_node: DepNode<Self::DepKind>) {
68 let cb = self.dep_kind_info(dep_node.kind);
69 if let Some(f) = cb.try_load_from_on_disk_cache {
70 f(self, dep_node)
71 }
72 }
73 }
74
75 pub trait HasDepContext: Copy {
76 type DepKind: self::DepKind;
77 type DepContext: self::DepContext<DepKind = Self::DepKind>;
78
79 fn dep_context(&self) -> &Self::DepContext;
80 }
81
82 impl<T: DepContext> HasDepContext for T {
83 type DepKind = T::DepKind;
84 type DepContext = Self;
85
86 fn dep_context(&self) -> &Self::DepContext {
87 self
88 }
89 }
90
91 /// Describes the contents of the fingerprint generated by a given query.
92 #[derive(Debug, PartialEq, Eq, Copy, Clone)]
93 pub enum FingerprintStyle {
94 /// The fingerprint is actually a DefPathHash.
95 DefPathHash,
96 /// The fingerprint is actually a HirId.
97 HirId,
98 /// Query key was `()` or equivalent, so fingerprint is just zero.
99 Unit,
100 /// Some opaque hash.
101 Opaque,
102 }
103
104 impl FingerprintStyle {
105 #[inline]
106 pub fn reconstructible(self) -> bool {
107 match self {
108 FingerprintStyle::DefPathHash | FingerprintStyle::Unit | FingerprintStyle::HirId => {
109 true
110 }
111 FingerprintStyle::Opaque => false,
112 }
113 }
114 }
115
116 /// Describe the different families of dependency nodes.
117 pub trait DepKind: Copy + fmt::Debug + Eq + Hash + Send + Encodable<FileEncoder> + 'static {
118 /// DepKind to use when incr. comp. is turned off.
119 const NULL: Self;
120
121 /// DepKind to use to create the initial forever-red node.
122 const RED: Self;
123
124 /// Implementation of `std::fmt::Debug` for `DepNode`.
125 fn debug_node(node: &DepNode<Self>, f: &mut fmt::Formatter<'_>) -> fmt::Result;
126
127 /// Execute the operation with provided dependencies.
128 fn with_deps<OP, R>(deps: TaskDepsRef<'_, Self>, op: OP) -> R
129 where
130 OP: FnOnce() -> R;
131
132 /// Access dependencies from current implicit context.
133 fn read_deps<OP>(op: OP)
134 where
135 OP: for<'a> FnOnce(TaskDepsRef<'a, Self>);
136 }