1 use crate::ty::{self, TyCtxt};
2 use rustc_data_structures::profiling::SelfProfilerRef;
3 use rustc_data_structures::sync::Lock;
4 use rustc_query_system::ich::StableHashingContext;
5 use rustc_session::Session;
6 
7 #[macro_use]
8 mod dep_node;
9 
10 pub use rustc_query_system::dep_graph::{
11     debug::DepNodeFilter, hash_result, DepContext, DepNodeColor, DepNodeIndex,
12     SerializedDepNodeIndex, WorkProduct, WorkProductId,
13 };
14 
15 pub use dep_node::{label_strs, DepKind, DepKindStruct, DepNode, DepNodeExt};
16 crate use dep_node::{make_compile_codegen_unit, make_compile_mono_item};
17 
18 pub type DepGraph = rustc_query_system::dep_graph::DepGraph<DepKind>;
19 pub type TaskDeps = rustc_query_system::dep_graph::TaskDeps<DepKind>;
20 pub type DepGraphQuery = rustc_query_system::dep_graph::DepGraphQuery<DepKind>;
21 pub type SerializedDepGraph = rustc_query_system::dep_graph::SerializedDepGraph<DepKind>;
22 pub type EdgeFilter = rustc_query_system::dep_graph::debug::EdgeFilter<DepKind>;
23 
24 impl rustc_query_system::dep_graph::DepKind for DepKind {
25     const NULL: Self = DepKind::Null;
26 
debug_node(node: &DepNode, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result27     fn debug_node(node: &DepNode, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
28         write!(f, "{:?}(", node.kind)?;
29 
30         ty::tls::with_opt(|opt_tcx| {
31             if let Some(tcx) = opt_tcx {
32                 if let Some(def_id) = node.extract_def_id(tcx) {
33                     write!(f, "{}", tcx.def_path_debug_str(def_id))?;
34                 } else if let Some(ref s) = tcx.dep_graph.dep_node_debug_str(*node) {
35                     write!(f, "{}", s)?;
36                 } else {
37                     write!(f, "{}", node.hash)?;
38                 }
39             } else {
40                 write!(f, "{}", node.hash)?;
41             }
42             Ok(())
43         })?;
44 
45         write!(f, ")")
46     }
47 
with_deps<OP, R>(task_deps: Option<&Lock<TaskDeps>>, op: OP) -> R where OP: FnOnce() -> R,48     fn with_deps<OP, R>(task_deps: Option<&Lock<TaskDeps>>, op: OP) -> R
49     where
50         OP: FnOnce() -> R,
51     {
52         ty::tls::with_context(|icx| {
53             let icx = ty::tls::ImplicitCtxt { task_deps, ..icx.clone() };
54 
55             ty::tls::enter_context(&icx, |_| op())
56         })
57     }
58 
read_deps<OP>(op: OP) where OP: for<'a> FnOnce(Option<&'a Lock<TaskDeps>>),59     fn read_deps<OP>(op: OP)
60     where
61         OP: for<'a> FnOnce(Option<&'a Lock<TaskDeps>>),
62     {
63         ty::tls::with_context_opt(|icx| {
64             let icx = if let Some(icx) = icx { icx } else { return };
65             op(icx.task_deps)
66         })
67     }
68 }
69 
70 impl<'tcx> DepContext for TyCtxt<'tcx> {
71     type DepKind = DepKind;
72 
73     #[inline]
create_stable_hashing_context(&self) -> StableHashingContext<'_>74     fn create_stable_hashing_context(&self) -> StableHashingContext<'_> {
75         TyCtxt::create_stable_hashing_context(*self)
76     }
77 
78     #[inline]
dep_graph(&self) -> &DepGraph79     fn dep_graph(&self) -> &DepGraph {
80         &self.dep_graph
81     }
82 
83     #[inline(always)]
profiler(&self) -> &SelfProfilerRef84     fn profiler(&self) -> &SelfProfilerRef {
85         &self.prof
86     }
87 
88     #[inline(always)]
sess(&self) -> &Session89     fn sess(&self) -> &Session {
90         self.sess
91     }
92 
93     #[inline(always)]
fingerprint_style(&self, kind: DepKind) -> rustc_query_system::dep_graph::FingerprintStyle94     fn fingerprint_style(&self, kind: DepKind) -> rustc_query_system::dep_graph::FingerprintStyle {
95         kind.fingerprint_style(*self)
96     }
97 
98     #[inline(always)]
is_eval_always(&self, kind: DepKind) -> bool99     fn is_eval_always(&self, kind: DepKind) -> bool {
100         self.query_kind(kind).is_eval_always
101     }
102 
try_force_from_dep_node(&self, dep_node: DepNode) -> bool103     fn try_force_from_dep_node(&self, dep_node: DepNode) -> bool {
104         debug!("try_force_from_dep_node({:?}) --- trying to force", dep_node);
105 
106         // We must avoid ever having to call `force_from_dep_node()` for a
107         // `DepNode::codegen_unit`:
108         // Since we cannot reconstruct the query key of a `DepNode::codegen_unit`, we
109         // would always end up having to evaluate the first caller of the
110         // `codegen_unit` query that *is* reconstructible. This might very well be
111         // the `compile_codegen_unit` query, thus re-codegenning the whole CGU just
112         // to re-trigger calling the `codegen_unit` query with the right key. At
113         // that point we would already have re-done all the work we are trying to
114         // avoid doing in the first place.
115         // The solution is simple: Just explicitly call the `codegen_unit` query for
116         // each CGU, right after partitioning. This way `try_mark_green` will always
117         // hit the cache instead of having to go through `force_from_dep_node`.
118         // This assertion makes sure, we actually keep applying the solution above.
119         debug_assert!(
120             dep_node.kind != DepKind::codegen_unit,
121             "calling force_from_dep_node() on DepKind::codegen_unit"
122         );
123 
124         let cb = self.query_kind(dep_node.kind);
125         if let Some(f) = cb.force_from_dep_node {
126             f(*self, dep_node);
127             true
128         } else {
129             false
130         }
131     }
132 
try_load_from_on_disk_cache(&self, dep_node: DepNode)133     fn try_load_from_on_disk_cache(&self, dep_node: DepNode) {
134         let cb = self.query_kind(dep_node.kind);
135         if let Some(f) = cb.try_load_from_on_disk_cache {
136             f(*self, dep_node)
137         }
138     }
139 }
140