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