]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_middle/src/dep_graph/mod.rs
Rollup merge of #99011 - oli-obk:UnsoundCell, r=eddyb
[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     const RED: Self = DepKind::Red;
27
28     fn debug_node(node: &DepNode, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
29         write!(f, "{:?}(", node.kind)?;
30
31         ty::tls::with_opt(|opt_tcx| {
32             if let Some(tcx) = opt_tcx {
33                 if let Some(def_id) = node.extract_def_id(tcx) {
34                     write!(f, "{}", tcx.def_path_debug_str(def_id))?;
35                 } else if let Some(ref s) = tcx.dep_graph.dep_node_debug_str(*node) {
36                     write!(f, "{}", s)?;
37                 } else {
38                     write!(f, "{}", node.hash)?;
39                 }
40             } else {
41                 write!(f, "{}", node.hash)?;
42             }
43             Ok(())
44         })?;
45
46         write!(f, ")")
47     }
48
49     fn with_deps<OP, R>(task_deps: TaskDepsRef<'_>, op: OP) -> R
50     where
51         OP: FnOnce() -> R,
52     {
53         ty::tls::with_context(|icx| {
54             let icx = ty::tls::ImplicitCtxt { task_deps, ..icx.clone() };
55
56             ty::tls::enter_context(&icx, |_| op())
57         })
58     }
59
60     fn read_deps<OP>(op: OP)
61     where
62         OP: for<'a> FnOnce(TaskDepsRef<'a>),
63     {
64         ty::tls::with_context_opt(|icx| {
65             let Some(icx) = icx else { return };
66             op(icx.task_deps)
67         })
68     }
69 }
70
71 impl<'tcx> DepContext for TyCtxt<'tcx> {
72     type DepKind = DepKind;
73
74     #[inline]
75     fn with_stable_hashing_context<R>(&self, f: impl FnOnce(StableHashingContext<'_>) -> R) -> R {
76         TyCtxt::with_stable_hashing_context(*self, f)
77     }
78
79     #[inline]
80     fn dep_graph(&self) -> &DepGraph {
81         &self.dep_graph
82     }
83
84     #[inline(always)]
85     fn profiler(&self) -> &SelfProfilerRef {
86         &self.prof
87     }
88
89     #[inline(always)]
90     fn sess(&self) -> &Session {
91         self.sess
92     }
93
94     #[inline(always)]
95     fn fingerprint_style(&self, kind: DepKind) -> rustc_query_system::dep_graph::FingerprintStyle {
96         kind.fingerprint_style(*self)
97     }
98
99     #[inline(always)]
100     fn is_eval_always(&self, kind: DepKind) -> bool {
101         self.query_kind(kind).is_eval_always
102     }
103
104     fn try_force_from_dep_node(&self, dep_node: DepNode) -> bool {
105         debug!("try_force_from_dep_node({:?}) --- trying to force", dep_node);
106
107         // We must avoid ever having to call `force_from_dep_node()` for a
108         // `DepNode::codegen_unit`:
109         // Since we cannot reconstruct the query key of a `DepNode::codegen_unit`, we
110         // would always end up having to evaluate the first caller of the
111         // `codegen_unit` query that *is* reconstructible. This might very well be
112         // the `compile_codegen_unit` query, thus re-codegenning the whole CGU just
113         // to re-trigger calling the `codegen_unit` query with the right key. At
114         // that point we would already have re-done all the work we are trying to
115         // avoid doing in the first place.
116         // The solution is simple: Just explicitly call the `codegen_unit` query for
117         // each CGU, right after partitioning. This way `try_mark_green` will always
118         // hit the cache instead of having to go through `force_from_dep_node`.
119         // This assertion makes sure, we actually keep applying the solution above.
120         debug_assert!(
121             dep_node.kind != DepKind::codegen_unit,
122             "calling force_from_dep_node() on DepKind::codegen_unit"
123         );
124
125         let cb = self.query_kind(dep_node.kind);
126         if let Some(f) = cb.force_from_dep_node {
127             f(*self, dep_node);
128             true
129         } else {
130             false
131         }
132     }
133
134     fn try_load_from_on_disk_cache(&self, dep_node: DepNode) {
135         let cb = self.query_kind(dep_node.kind);
136         if let Some(f) = cb.try_load_from_on_disk_cache {
137             f(*self, dep_node)
138         }
139     }
140 }