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;
27 fn debug_node(node: &DepNode, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
28 write!(f, "{:?}(", node.kind)?;
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) {
37 write!(f, "{}", node.hash)?;
40 write!(f, "{}", node.hash)?;
48 fn with_deps<OP, R>(task_deps: TaskDepsRef<'_>, op: OP) -> R
52 ty::tls::with_context(|icx| {
53 let icx = ty::tls::ImplicitCtxt { task_deps, ..icx.clone() };
55 ty::tls::enter_context(&icx, |_| op())
59 fn read_deps<OP>(op: OP)
61 OP: for<'a> FnOnce(TaskDepsRef<'a>),
63 ty::tls::with_context_opt(|icx| {
64 let Some(icx) = icx else { return };
70 impl<'tcx> DepContext for TyCtxt<'tcx> {
71 type DepKind = DepKind;
74 fn create_stable_hashing_context(&self) -> StableHashingContext<'_> {
75 TyCtxt::create_stable_hashing_context(*self)
79 fn dep_graph(&self) -> &DepGraph {
84 fn profiler(&self) -> &SelfProfilerRef {
89 fn sess(&self) -> &Session {
94 fn fingerprint_style(&self, kind: DepKind) -> rustc_query_system::dep_graph::FingerprintStyle {
95 kind.fingerprint_style(*self)
99 fn is_eval_always(&self, kind: DepKind) -> bool {
100 self.query_kind(kind).is_eval_always
103 fn try_force_from_dep_node(&self, dep_node: DepNode) -> bool {
104 debug!("try_force_from_dep_node({:?}) --- trying to force", dep_node);
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.
120 dep_node.kind != DepKind::codegen_unit,
121 "calling force_from_dep_node() on DepKind::codegen_unit"
124 let cb = self.query_kind(dep_node.kind);
125 if let Some(f) = cb.force_from_dep_node {
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 {