Skip to main content

rustc_middle/dep_graph/
mod.rs

1use std::panic;
2
3use rustc_data_structures::profiling::SelfProfilerRef;
4use rustc_data_structures::sync::DynSync;
5use rustc_query_system::ich::StableHashingContext;
6use rustc_session::Session;
7use tracing::instrument;
8
9pub use self::dep_node::{
10    DepKind, DepNode, DepNodeExt, DepNodeKey, WorkProductId, dep_kind_from_label, dep_kinds,
11    label_strs,
12};
13pub use self::graph::{
14    DepGraphData, DepNodeIndex, TaskDepsRef, WorkProduct, WorkProductMap, hash_result,
15};
16use self::graph::{MarkFrame, print_markframe_trace};
17pub use self::query::DepGraphQuery;
18pub use self::serialized::{SerializedDepGraph, SerializedDepNodeIndex};
19pub use crate::dep_graph::debug::{DepNodeFilter, EdgeFilter};
20use crate::ty::print::with_reduced_queries;
21use crate::ty::{self, TyCtxt};
22
23mod debug;
24pub mod dep_node;
25mod dep_node_key;
26mod edges;
27mod graph;
28mod query;
29mod serialized;
30
31pub trait DepContext: Copy {
32    type Deps: Deps;
33
34    /// Create a hashing context for hashing new results.
35    fn with_stable_hashing_context<R>(self, f: impl FnOnce(StableHashingContext<'_>) -> R) -> R;
36
37    /// Access the DepGraph.
38    fn dep_graph(&self) -> &graph::DepGraph<Self::Deps>;
39
40    /// Access the profiler.
41    fn profiler(&self) -> &SelfProfilerRef;
42
43    /// Access the compiler session.
44    fn sess(&self) -> &Session;
45
46    fn dep_kind_vtable(&self, dep_node: DepKind) -> &dep_node::DepKindVTable<Self>;
47
48    #[inline(always)]
49    fn fingerprint_style(self, kind: DepKind) -> FingerprintStyle {
50        self.dep_kind_vtable(kind).fingerprint_style
51    }
52
53    #[inline(always)]
54    /// Return whether this kind always require evaluation.
55    fn is_eval_always(self, kind: DepKind) -> bool {
56        self.dep_kind_vtable(kind).is_eval_always
57    }
58
59    /// Try to force a dep node to execute and see if it's green.
60    ///
61    /// Returns true if the query has actually been forced. It is valid that a query
62    /// fails to be forced, e.g. when the query key cannot be reconstructed from the
63    /// dep-node or when the query kind outright does not support it.
64    #[inline]
65    #[allow(clippy :: suspicious_else_formatting)]
{
    let __tracing_attr_span;
    let __tracing_attr_guard;
    if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() ||
            { false } {
        __tracing_attr_span =
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("try_force_from_dep_node",
                                    "rustc_middle::dep_graph", ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_middle/src/dep_graph/mod.rs"),
                                    ::tracing_core::__macro_support::Option::Some(65u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_middle::dep_graph"),
                                    ::tracing_core::field::FieldSet::new(&["dep_node",
                                                    "prev_index"],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::SPAN)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let mut interest = ::tracing::subscriber::Interest::never();
                if ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::LevelFilter::current() &&
                            { interest = __CALLSITE.interest(); !interest.is_never() }
                        &&
                        ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                            interest) {
                    let meta = __CALLSITE.metadata();
                    ::tracing::Span::new(meta,
                        &{
                                #[allow(unused_imports)]
                                use ::tracing::field::{debug, display, Value};
                                let mut iter = meta.fields().iter();
                                meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&dep_node)
                                                            as &dyn Value)),
                                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&prev_index)
                                                            as &dyn Value))])
                            })
                } else {
                    let span =
                        ::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
                    {};
                    span
                }
            };
        __tracing_attr_guard = __tracing_attr_span.enter();
    }

    #[warn(clippy :: suspicious_else_formatting)]
    {

        #[allow(unknown_lints, unreachable_code, clippy ::
        diverging_sub_expression, clippy :: empty_loop, clippy ::
        let_unit_value, clippy :: let_with_type_underscore, clippy ::
        needless_return, clippy :: unreachable)]
        if false {
            let __tracing_attr_fake_return: bool = loop {};
            return __tracing_attr_fake_return;
        }
        {
            if let Some(force_fn) =
                    self.dep_kind_vtable(dep_node.kind).force_from_dep_node {
                match panic::catch_unwind(panic::AssertUnwindSafe(||
                                { force_fn(self, dep_node, prev_index) })) {
                    Err(value) => {
                        if !value.is::<rustc_errors::FatalErrorMarker>() {
                            print_markframe_trace(self.dep_graph(), frame);
                        }
                        panic::resume_unwind(value)
                    }
                    Ok(query_has_been_forced) => query_has_been_forced,
                }
            } else { false }
        }
    }
}#[instrument(skip(self, frame), level = "debug")]
66    fn try_force_from_dep_node(
67        self,
68        dep_node: DepNode,
69        prev_index: SerializedDepNodeIndex,
70        frame: &MarkFrame<'_>,
71    ) -> bool {
72        if let Some(force_fn) = self.dep_kind_vtable(dep_node.kind).force_from_dep_node {
73            match panic::catch_unwind(panic::AssertUnwindSafe(|| {
74                force_fn(self, dep_node, prev_index)
75            })) {
76                Err(value) => {
77                    if !value.is::<rustc_errors::FatalErrorMarker>() {
78                        print_markframe_trace(self.dep_graph(), frame);
79                    }
80                    panic::resume_unwind(value)
81                }
82                Ok(query_has_been_forced) => query_has_been_forced,
83            }
84        } else {
85            false
86        }
87    }
88
89    /// Load data from the on-disk cache.
90    fn try_load_from_on_disk_cache(self, dep_node: &DepNode) {
91        if let Some(try_load_fn) = self.dep_kind_vtable(dep_node.kind).try_load_from_on_disk_cache {
92            try_load_fn(self, *dep_node)
93        }
94    }
95
96    fn with_reduced_queries<T>(self, _: impl FnOnce() -> T) -> T;
97}
98
99pub trait Deps: DynSync {
100    /// Execute the operation with provided dependencies.
101    fn with_deps<OP, R>(deps: TaskDepsRef<'_>, op: OP) -> R
102    where
103        OP: FnOnce() -> R;
104
105    /// Access dependencies from current implicit context.
106    fn read_deps<OP>(op: OP)
107    where
108        OP: for<'a> FnOnce(TaskDepsRef<'a>);
109
110    fn name(dep_kind: DepKind) -> &'static str;
111
112    /// We use this for most things when incr. comp. is turned off.
113    const DEP_KIND_NULL: DepKind;
114
115    /// We use this to create a forever-red node.
116    const DEP_KIND_RED: DepKind;
117
118    /// We use this to create a side effect node.
119    const DEP_KIND_SIDE_EFFECT: DepKind;
120
121    /// We use this to create the anon node with zero dependencies.
122    const DEP_KIND_ANON_ZERO_DEPS: DepKind;
123
124    /// This is the highest value a `DepKind` can have. It's used during encoding to
125    /// pack information into the unused bits.
126    const DEP_KIND_MAX: u16;
127}
128
129pub trait HasDepContext: Copy {
130    type Deps: self::Deps;
131    type DepContext: self::DepContext<Deps = Self::Deps>;
132
133    fn dep_context(&self) -> &Self::DepContext;
134}
135
136impl<T: DepContext> HasDepContext for T {
137    type Deps = T::Deps;
138    type DepContext = Self;
139
140    fn dep_context(&self) -> &Self::DepContext {
141        self
142    }
143}
144
145impl<T: HasDepContext, Q: Copy> HasDepContext for (T, Q) {
146    type Deps = T::Deps;
147    type DepContext = T::DepContext;
148
149    fn dep_context(&self) -> &Self::DepContext {
150        self.0.dep_context()
151    }
152}
153
154/// Describes the contents of the fingerprint generated by a given query.
155///
156/// This is mainly for determining whether and how we can reconstruct a key
157/// from the fingerprint.
158#[derive(#[automatically_derived]
impl ::core::fmt::Debug for FingerprintStyle {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::write_str(f,
            match self {
                FingerprintStyle::DefPathHash => "DefPathHash",
                FingerprintStyle::HirId => "HirId",
                FingerprintStyle::Unit => "Unit",
                FingerprintStyle::Opaque => "Opaque",
            })
    }
}Debug, #[automatically_derived]
impl ::core::cmp::PartialEq for FingerprintStyle {
    #[inline]
    fn eq(&self, other: &FingerprintStyle) -> bool {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        let __arg1_discr = ::core::intrinsics::discriminant_value(other);
        __self_discr == __arg1_discr
    }
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for FingerprintStyle {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_receiver_is_total_eq(&self) {}
}Eq, #[automatically_derived]
impl ::core::marker::Copy for FingerprintStyle { }Copy, #[automatically_derived]
impl ::core::clone::Clone for FingerprintStyle {
    #[inline]
    fn clone(&self) -> FingerprintStyle { *self }
}Clone)]
159pub enum FingerprintStyle {
160    /// The fingerprint is actually a DefPathHash.
161    DefPathHash,
162    /// The fingerprint is actually a HirId.
163    HirId,
164    /// Query key was `()` or equivalent, so fingerprint is just zero.
165    Unit,
166    /// The fingerprint is an opaque hash, and a key cannot be reconstructed from it.
167    Opaque,
168}
169
170impl FingerprintStyle {
171    #[inline]
172    pub const fn reconstructible(self) -> bool {
173        match self {
174            FingerprintStyle::DefPathHash | FingerprintStyle::Unit | FingerprintStyle::HirId => {
175                true
176            }
177            FingerprintStyle::Opaque => false,
178        }
179    }
180}
181
182pub type DepGraph = graph::DepGraph<DepsType>;
183
184pub type DepKindVTable<'tcx> = dep_node::DepKindVTable<TyCtxt<'tcx>>;
185
186pub struct DepsType;
187
188impl Deps for DepsType {
189    fn with_deps<OP, R>(task_deps: TaskDepsRef<'_>, op: OP) -> R
190    where
191        OP: FnOnce() -> R,
192    {
193        ty::tls::with_context(|icx| {
194            let icx = ty::tls::ImplicitCtxt { task_deps, ..icx.clone() };
195
196            ty::tls::enter_context(&icx, op)
197        })
198    }
199
200    fn read_deps<OP>(op: OP)
201    where
202        OP: for<'a> FnOnce(TaskDepsRef<'a>),
203    {
204        ty::tls::with_context_opt(|icx| {
205            let Some(icx) = icx else { return };
206            op(icx.task_deps)
207        })
208    }
209
210    fn name(dep_kind: DepKind) -> &'static str {
211        dep_node::DEP_KIND_NAMES[dep_kind.as_usize()]
212    }
213
214    const DEP_KIND_NULL: DepKind = dep_kinds::Null;
215    const DEP_KIND_RED: DepKind = dep_kinds::Red;
216    const DEP_KIND_SIDE_EFFECT: DepKind = dep_kinds::SideEffect;
217    const DEP_KIND_ANON_ZERO_DEPS: DepKind = dep_kinds::AnonZeroDeps;
218    const DEP_KIND_MAX: u16 = dep_node::DEP_KIND_VARIANTS - 1;
219}
220
221impl<'tcx> DepContext for TyCtxt<'tcx> {
222    type Deps = DepsType;
223
224    #[inline]
225    fn with_stable_hashing_context<R>(self, f: impl FnOnce(StableHashingContext<'_>) -> R) -> R {
226        TyCtxt::with_stable_hashing_context(self, f)
227    }
228
229    #[inline]
230    fn dep_graph(&self) -> &DepGraph {
231        &self.dep_graph
232    }
233
234    #[inline(always)]
235    fn profiler(&self) -> &SelfProfilerRef {
236        &self.prof
237    }
238
239    #[inline(always)]
240    fn sess(&self) -> &Session {
241        self.sess
242    }
243
244    #[inline]
245    fn dep_kind_vtable(&self, dk: DepKind) -> &DepKindVTable<'tcx> {
246        &self.dep_kind_vtables[dk.as_usize()]
247    }
248
249    fn with_reduced_queries<T>(self, f: impl FnOnce() -> T) -> T {
250        { let _guard = ReducedQueriesGuard::new(); f() }with_reduced_queries!(f())
251    }
252}