]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_middle/src/dep_graph/mod.rs
Rollup merge of #89685 - DeveloperC286:iter_fields_to_private, r=oli-obk
[rust.git] / compiler / rustc_middle / src / dep_graph / mod.rs
1 use crate::ty::{self, TyCtxt};
2 use rustc_data_structures::profiling::SelfProfilerRef;
3 use rustc_query_system::ich::StableHashingContext;
4 use rustc_session::Session;
5
6 #[macro_use]
7 mod dep_node;
8
9 pub use rustc_query_system::dep_graph::{
10     debug::DepNodeFilter, hash_result, DepContext, DepNodeColor, DepNodeIndex,
11     SerializedDepNodeIndex, WorkProduct, WorkProductId,
12 };
13
14 pub use dep_node::{label_strs, DepKind, DepKindStruct, DepNode, DepNodeExt};
15 pub(crate) use dep_node::{make_compile_codegen_unit, make_compile_mono_item};
16
17 pub type DepGraph = rustc_query_system::dep_graph::DepGraph<DepKind>;
18 pub type TaskDeps = rustc_query_system::dep_graph::TaskDeps<DepKind>;
19 pub type TaskDepsRef<'a> = rustc_query_system::dep_graph::TaskDepsRef<'a, 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
27     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
48     fn with_deps<OP, R>(task_deps: TaskDepsRef<'_>, 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
59     fn read_deps<OP>(op: OP)
60     where
61         OP: for<'a> FnOnce(TaskDepsRef<'a>),
62     {
63         ty::tls::with_context_opt(|icx| {
64             let Some(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]
74     fn create_stable_hashing_context(&self) -> StableHashingContext<'_> {
75         TyCtxt::create_stable_hashing_context(*self)
76     }
77
78     #[inline]
79     fn dep_graph(&self) -> &DepGraph {
80         &self.dep_graph
81     }
82
83     #[inline(always)]
84     fn profiler(&self) -> &SelfProfilerRef {
85         &self.prof
86     }
87
88     #[inline(always)]
89     fn sess(&self) -> &Session {
90         self.sess
91     }
92
93     #[inline(always)]
94     fn fingerprint_style(&self, kind: DepKind) -> rustc_query_system::dep_graph::FingerprintStyle {
95         kind.fingerprint_style(*self)
96     }
97
98     #[inline(always)]
99     fn is_eval_always(&self, kind: DepKind) -> bool {
100         self.query_kind(kind).is_eval_always
101     }
102
103     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
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 }