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;
9 pub use rustc_query_system::dep_graph::{
10 debug::DepNodeFilter, hash_result, DepContext, DepNodeColor, DepNodeIndex,
11 SerializedDepNodeIndex, WorkProduct, WorkProductId,
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};
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>;
24 impl rustc_query_system::dep_graph::DepKind for DepKind {
25 const NULL: Self = DepKind::Null;
26 const RED: Self = DepKind::Red;
28 fn debug_node(node: &DepNode, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
29 write!(f, "{:?}(", node.kind)?;
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) {
38 write!(f, "{}", node.hash)?;
41 write!(f, "{}", node.hash)?;
49 fn with_deps<OP, R>(task_deps: TaskDepsRef<'_>, op: OP) -> R
53 ty::tls::with_context(|icx| {
54 let icx = ty::tls::ImplicitCtxt { task_deps, ..icx.clone() };
56 ty::tls::enter_context(&icx, |_| op())
60 fn read_deps<OP>(op: OP)
62 OP: for<'a> FnOnce(TaskDepsRef<'a>),
64 ty::tls::with_context_opt(|icx| {
65 let Some(icx) = icx else { return };
71 impl<'tcx> DepContext for TyCtxt<'tcx> {
72 type DepKind = DepKind;
75 fn with_stable_hashing_context<R>(&self, f: impl FnOnce(StableHashingContext<'_>) -> R) -> R {
76 TyCtxt::with_stable_hashing_context(*self, f)
80 fn dep_graph(&self) -> &DepGraph {
85 fn profiler(&self) -> &SelfProfilerRef {
90 fn sess(&self) -> &Session {
95 fn fingerprint_style(&self, kind: DepKind) -> rustc_query_system::dep_graph::FingerprintStyle {
96 kind.fingerprint_style(*self)
100 fn is_eval_always(&self, kind: DepKind) -> bool {
101 self.query_kind(kind).is_eval_always
104 fn try_force_from_dep_node(&self, dep_node: DepNode) -> bool {
105 debug!("try_force_from_dep_node({:?}) --- trying to force", dep_node);
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.
121 dep_node.kind != DepKind::codegen_unit,
122 "calling force_from_dep_node() on DepKind::codegen_unit"
125 let cb = self.query_kind(dep_node.kind);
126 if let Some(f) = cb.force_from_dep_node {
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 {