rustc_query_system/dep_graph/
mod.rs

1pub mod debug;
2pub mod dep_node;
3mod edges;
4mod graph;
5mod query;
6mod serialized;
7
8use std::panic;
9
10pub use dep_node::{DepKind, DepKindStruct, DepNode, DepNodeParams, WorkProductId};
11pub(crate) use graph::DepGraphData;
12pub use graph::{DepGraph, DepNodeIndex, TaskDepsRef, WorkProduct, WorkProductMap, hash_result};
13pub use query::DepGraphQuery;
14use rustc_data_structures::profiling::SelfProfilerRef;
15use rustc_data_structures::sync::DynSync;
16use rustc_session::Session;
17pub use serialized::{SerializedDepGraph, SerializedDepNodeIndex};
18use tracing::instrument;
19
20use self::graph::{MarkFrame, print_markframe_trace};
21use crate::ich::StableHashingContext;
22
23pub trait DepContext: Copy {
24    type Deps: Deps;
25
26    /// Create a hashing context for hashing new results.
27    fn with_stable_hashing_context<R>(self, f: impl FnOnce(StableHashingContext<'_>) -> R) -> R;
28
29    /// Access the DepGraph.
30    fn dep_graph(&self) -> &DepGraph<Self::Deps>;
31
32    /// Access the profiler.
33    fn profiler(&self) -> &SelfProfilerRef;
34
35    /// Access the compiler session.
36    fn sess(&self) -> &Session;
37
38    fn dep_kind_info(&self, dep_node: DepKind) -> &DepKindStruct<Self>;
39
40    #[inline(always)]
41    fn fingerprint_style(self, kind: DepKind) -> FingerprintStyle {
42        let data = self.dep_kind_info(kind);
43        if data.is_anon {
44            return FingerprintStyle::Opaque;
45        }
46        data.fingerprint_style
47    }
48
49    #[inline(always)]
50    /// Return whether this kind always require evaluation.
51    fn is_eval_always(self, kind: DepKind) -> bool {
52        self.dep_kind_info(kind).is_eval_always
53    }
54
55    /// Try to force a dep node to execute and see if it's green.
56    ///
57    /// Returns true if the query has actually been forced. It is valid that a query
58    /// fails to be forced, e.g. when the query key cannot be reconstructed from the
59    /// dep-node or when the query kind outright does not support it.
60    #[inline]
61    #[instrument(skip(self, frame), level = "debug")]
62    fn try_force_from_dep_node(
63        self,
64        dep_node: DepNode,
65        prev_index: SerializedDepNodeIndex,
66        frame: Option<&MarkFrame<'_>>,
67    ) -> bool {
68        let cb = self.dep_kind_info(dep_node.kind);
69        if let Some(f) = cb.force_from_dep_node {
70            match panic::catch_unwind(panic::AssertUnwindSafe(|| f(self, dep_node, prev_index))) {
71                Err(value) => {
72                    if !value.is::<rustc_errors::FatalErrorMarker>() {
73                        print_markframe_trace(self.dep_graph(), frame);
74                    }
75                    panic::resume_unwind(value)
76                }
77                Ok(query_has_been_forced) => query_has_been_forced,
78            }
79        } else {
80            false
81        }
82    }
83
84    /// Load data from the on-disk cache.
85    fn try_load_from_on_disk_cache(self, dep_node: DepNode) {
86        let cb = self.dep_kind_info(dep_node.kind);
87        if let Some(f) = cb.try_load_from_on_disk_cache {
88            f(self, dep_node)
89        }
90    }
91}
92
93pub trait Deps: DynSync {
94    /// Execute the operation with provided dependencies.
95    fn with_deps<OP, R>(deps: TaskDepsRef<'_>, op: OP) -> R
96    where
97        OP: FnOnce() -> R;
98
99    /// Access dependencies from current implicit context.
100    fn read_deps<OP>(op: OP)
101    where
102        OP: for<'a> FnOnce(TaskDepsRef<'a>);
103
104    fn name(&self, dep_kind: DepKind) -> &'static str;
105
106    /// We use this for most things when incr. comp. is turned off.
107    const DEP_KIND_NULL: DepKind;
108
109    /// We use this to create a forever-red node.
110    const DEP_KIND_RED: DepKind;
111
112    /// We use this to create a side effect node.
113    const DEP_KIND_SIDE_EFFECT: DepKind;
114
115    /// We use this to create the anon node with zero dependencies.
116    const DEP_KIND_ANON_ZERO_DEPS: DepKind;
117
118    /// This is the highest value a `DepKind` can have. It's used during encoding to
119    /// pack information into the unused bits.
120    const DEP_KIND_MAX: u16;
121}
122
123pub trait HasDepContext: Copy {
124    type Deps: self::Deps;
125    type DepContext: self::DepContext<Deps = Self::Deps>;
126
127    fn dep_context(&self) -> &Self::DepContext;
128}
129
130impl<T: DepContext> HasDepContext for T {
131    type Deps = T::Deps;
132    type DepContext = Self;
133
134    fn dep_context(&self) -> &Self::DepContext {
135        self
136    }
137}
138
139impl<T: HasDepContext, Q: Copy> HasDepContext for (T, Q) {
140    type Deps = T::Deps;
141    type DepContext = T::DepContext;
142
143    fn dep_context(&self) -> &Self::DepContext {
144        self.0.dep_context()
145    }
146}
147
148/// Describes the contents of the fingerprint generated by a given query.
149#[derive(Debug, PartialEq, Eq, Copy, Clone)]
150pub enum FingerprintStyle {
151    /// The fingerprint is actually a DefPathHash.
152    DefPathHash,
153    /// The fingerprint is actually a HirId.
154    HirId,
155    /// Query key was `()` or equivalent, so fingerprint is just zero.
156    Unit,
157    /// Some opaque hash.
158    Opaque,
159}
160
161impl FingerprintStyle {
162    #[inline]
163    pub const fn reconstructible(self) -> bool {
164        match self {
165            FingerprintStyle::DefPathHash | FingerprintStyle::Unit | FingerprintStyle::HirId => {
166                true
167            }
168            FingerprintStyle::Opaque => false,
169        }
170    }
171}