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(self, dep_node: DepNode, frame: Option<&MarkFrame<'_>>) -> bool {
62        let cb = self.dep_kind_info(dep_node.kind);
63        if let Some(f) = cb.force_from_dep_node {
64            match panic::catch_unwind(panic::AssertUnwindSafe(|| f(self, dep_node))) {
65                Err(value) => {
66                    if !value.is::<rustc_errors::FatalErrorMarker>() {
67                        print_markframe_trace(self.dep_graph(), frame);
68                    }
69                    panic::resume_unwind(value)
70                }
71                Ok(query_has_been_forced) => query_has_been_forced,
72            }
73        } else {
74            false
75        }
76    }
77
78    /// Load data from the on-disk cache.
79    fn try_load_from_on_disk_cache(self, dep_node: DepNode) {
80        let cb = self.dep_kind_info(dep_node.kind);
81        if let Some(f) = cb.try_load_from_on_disk_cache {
82            f(self, dep_node)
83        }
84    }
85}
86
87pub trait Deps {
88    /// Execute the operation with provided dependencies.
89    fn with_deps<OP, R>(deps: TaskDepsRef<'_>, op: OP) -> R
90    where
91        OP: FnOnce() -> R;
92
93    /// Access dependencies from current implicit context.
94    fn read_deps<OP>(op: OP)
95    where
96        OP: for<'a> FnOnce(TaskDepsRef<'a>);
97
98    /// We use this for most things when incr. comp. is turned off.
99    const DEP_KIND_NULL: DepKind;
100
101    /// We use this to create a forever-red node.
102    const DEP_KIND_RED: DepKind;
103
104    /// This is the highest value a `DepKind` can have. It's used during encoding to
105    /// pack information into the unused bits.
106    const DEP_KIND_MAX: u16;
107}
108
109pub trait HasDepContext: Copy {
110    type Deps: self::Deps;
111    type DepContext: self::DepContext<Deps = Self::Deps>;
112
113    fn dep_context(&self) -> &Self::DepContext;
114}
115
116impl<T: DepContext> HasDepContext for T {
117    type Deps = T::Deps;
118    type DepContext = Self;
119
120    fn dep_context(&self) -> &Self::DepContext {
121        self
122    }
123}
124
125impl<T: HasDepContext, Q: Copy> HasDepContext for (T, Q) {
126    type Deps = T::Deps;
127    type DepContext = T::DepContext;
128
129    fn dep_context(&self) -> &Self::DepContext {
130        self.0.dep_context()
131    }
132}
133
134/// Describes the contents of the fingerprint generated by a given query.
135#[derive(Debug, PartialEq, Eq, Copy, Clone)]
136pub enum FingerprintStyle {
137    /// The fingerprint is actually a DefPathHash.
138    DefPathHash,
139    /// The fingerprint is actually a HirId.
140    HirId,
141    /// Query key was `()` or equivalent, so fingerprint is just zero.
142    Unit,
143    /// Some opaque hash.
144    Opaque,
145}
146
147impl FingerprintStyle {
148    #[inline]
149    pub fn reconstructible(self) -> bool {
150        match self {
151            FingerprintStyle::DefPathHash | FingerprintStyle::Unit | FingerprintStyle::HirId => {
152                true
153            }
154            FingerprintStyle::Opaque => false,
155        }
156    }
157}