|
| 1 | +use rustc_middle::bug; |
| 2 | +use rustc_middle::dep_graph::DepKindVTable; |
| 3 | +use rustc_middle::ty::TyCtxt; |
| 4 | +use rustc_query_system::dep_graph::{DepNodeKey, FingerprintStyle}; |
| 5 | +use rustc_query_system::query::QueryCache; |
| 6 | + |
| 7 | +use crate::plumbing::{force_from_dep_node_inner, try_load_from_on_disk_cache_inner}; |
| 8 | +use crate::{QueryCtxt, QueryDispatcherUnerased, QueryFlags}; |
| 9 | + |
| 10 | +/// [`DepKindVTable`] constructors for special dep kinds that aren't queries. |
| 11 | +#[expect(non_snake_case, reason = "use non-snake case to avoid collision with query names")] |
| 12 | +mod non_query { |
| 13 | + use super::*; |
| 14 | + |
| 15 | + // We use this for most things when incr. comp. is turned off. |
| 16 | + pub(crate) fn Null<'tcx>() -> DepKindVTable<'tcx> { |
| 17 | + DepKindVTable { |
| 18 | + is_anon: false, |
| 19 | + is_eval_always: false, |
| 20 | + fingerprint_style: FingerprintStyle::Unit, |
| 21 | + force_from_dep_node: Some(|_, dep_node, _| { |
| 22 | + bug!("force_from_dep_node: encountered {dep_node:?}") |
| 23 | + }), |
| 24 | + try_load_from_on_disk_cache: None, |
| 25 | + name: &"Null", |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + // We use this for the forever-red node. |
| 30 | + pub(crate) fn Red<'tcx>() -> DepKindVTable<'tcx> { |
| 31 | + DepKindVTable { |
| 32 | + is_anon: false, |
| 33 | + is_eval_always: false, |
| 34 | + fingerprint_style: FingerprintStyle::Unit, |
| 35 | + force_from_dep_node: Some(|_, dep_node, _| { |
| 36 | + bug!("force_from_dep_node: encountered {dep_node:?}") |
| 37 | + }), |
| 38 | + try_load_from_on_disk_cache: None, |
| 39 | + name: &"Red", |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + pub(crate) fn SideEffect<'tcx>() -> DepKindVTable<'tcx> { |
| 44 | + DepKindVTable { |
| 45 | + is_anon: false, |
| 46 | + is_eval_always: false, |
| 47 | + fingerprint_style: FingerprintStyle::Unit, |
| 48 | + force_from_dep_node: Some(|tcx, _, prev_index| { |
| 49 | + tcx.dep_graph.force_diagnostic_node(QueryCtxt::new(tcx), prev_index); |
| 50 | + true |
| 51 | + }), |
| 52 | + try_load_from_on_disk_cache: None, |
| 53 | + name: &"SideEffect", |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + pub(crate) fn AnonZeroDeps<'tcx>() -> DepKindVTable<'tcx> { |
| 58 | + DepKindVTable { |
| 59 | + is_anon: true, |
| 60 | + is_eval_always: false, |
| 61 | + fingerprint_style: FingerprintStyle::Opaque, |
| 62 | + force_from_dep_node: Some(|_, _, _| bug!("cannot force an anon node")), |
| 63 | + try_load_from_on_disk_cache: None, |
| 64 | + name: &"AnonZeroDeps", |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + pub(crate) fn TraitSelect<'tcx>() -> DepKindVTable<'tcx> { |
| 69 | + DepKindVTable { |
| 70 | + is_anon: true, |
| 71 | + is_eval_always: false, |
| 72 | + fingerprint_style: FingerprintStyle::Unit, |
| 73 | + force_from_dep_node: None, |
| 74 | + try_load_from_on_disk_cache: None, |
| 75 | + name: &"TraitSelect", |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + pub(crate) fn CompileCodegenUnit<'tcx>() -> DepKindVTable<'tcx> { |
| 80 | + DepKindVTable { |
| 81 | + is_anon: false, |
| 82 | + is_eval_always: false, |
| 83 | + fingerprint_style: FingerprintStyle::Opaque, |
| 84 | + force_from_dep_node: None, |
| 85 | + try_load_from_on_disk_cache: None, |
| 86 | + name: &"CompileCodegenUnit", |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + pub(crate) fn CompileMonoItem<'tcx>() -> DepKindVTable<'tcx> { |
| 91 | + DepKindVTable { |
| 92 | + is_anon: false, |
| 93 | + is_eval_always: false, |
| 94 | + fingerprint_style: FingerprintStyle::Opaque, |
| 95 | + force_from_dep_node: None, |
| 96 | + try_load_from_on_disk_cache: None, |
| 97 | + name: &"CompileMonoItem", |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + pub(crate) fn Metadata<'tcx>() -> DepKindVTable<'tcx> { |
| 102 | + DepKindVTable { |
| 103 | + is_anon: false, |
| 104 | + is_eval_always: false, |
| 105 | + fingerprint_style: FingerprintStyle::Unit, |
| 106 | + force_from_dep_node: None, |
| 107 | + try_load_from_on_disk_cache: None, |
| 108 | + name: &"Metadata", |
| 109 | + } |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +fn make_dep_kind_vtable_for_query<'tcx, Q, Cache, const FLAGS: QueryFlags>( |
| 114 | + is_eval_always: bool, |
| 115 | +) -> DepKindVTable<'tcx> |
| 116 | +where |
| 117 | + Q: QueryDispatcherUnerased<'tcx, Cache, FLAGS>, |
| 118 | + Cache: QueryCache + 'tcx, |
| 119 | +{ |
| 120 | + let is_anon = FLAGS.is_anon; |
| 121 | + let fingerprint_style = if is_anon { |
| 122 | + FingerprintStyle::Opaque |
| 123 | + } else { |
| 124 | + <Cache::Key as DepNodeKey<TyCtxt<'tcx>>>::fingerprint_style() |
| 125 | + }; |
| 126 | + |
| 127 | + if is_anon || !fingerprint_style.reconstructible() { |
| 128 | + return DepKindVTable { |
| 129 | + is_anon, |
| 130 | + is_eval_always, |
| 131 | + fingerprint_style, |
| 132 | + force_from_dep_node: None, |
| 133 | + try_load_from_on_disk_cache: None, |
| 134 | + name: Q::NAME, |
| 135 | + }; |
| 136 | + } |
| 137 | + |
| 138 | + DepKindVTable { |
| 139 | + is_anon, |
| 140 | + is_eval_always, |
| 141 | + fingerprint_style, |
| 142 | + force_from_dep_node: Some(|tcx, dep_node, _| { |
| 143 | + force_from_dep_node_inner(Q::query_dispatcher(tcx), tcx, dep_node) |
| 144 | + }), |
| 145 | + try_load_from_on_disk_cache: Some(|tcx, dep_node| { |
| 146 | + try_load_from_on_disk_cache_inner(Q::query_dispatcher(tcx), tcx, dep_node) |
| 147 | + }), |
| 148 | + name: Q::NAME, |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +macro_rules! declare_query_dep_kind_vtable_ctors { |
| 153 | + ( |
| 154 | + $( |
| 155 | + $(#[$attr:meta])* |
| 156 | + [$($modifiers:tt)*] |
| 157 | + fn $name:ident($($K:tt)*) -> $V:ty, |
| 158 | + )* |
| 159 | + ) => { |
| 160 | + $( |
| 161 | + /// `DepKindVTable` constructor for this query. |
| 162 | + pub(crate) fn $name<'tcx>() -> ::rustc_middle::dep_graph::DepKindVTable<'tcx> { |
| 163 | + use $crate::query_impl::$name::QueryType; |
| 164 | + $crate::dep_kind_vtables::make_dep_kind_vtable_for_query::<QueryType<'tcx>, _, _>( |
| 165 | + $crate::query_impl::$name::IS_EVAL_ALWAYS, |
| 166 | + ) |
| 167 | + } |
| 168 | + )* |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | +/// Helper module containing a [`DepKindVTable`] constructor for each dep kind, |
| 173 | +/// for use with [`rustc_middle::make_dep_kind_array`]. |
| 174 | +mod _dep_kind_vtable_ctors { |
| 175 | + // Re-export all of the vtable constructors for non-query dep kinds. |
| 176 | + pub(crate) use super::non_query::{ |
| 177 | + AnonZeroDeps, CompileCodegenUnit, CompileMonoItem, Metadata, Null, Red, SideEffect, |
| 178 | + TraitSelect, |
| 179 | + }; |
| 180 | + // Declare a vtable constructor for each query. |
| 181 | + rustc_middle::rustc_with_all_queries! { declare_query_dep_kind_vtable_ctors! } |
| 182 | +} |
| 183 | + |
| 184 | +pub fn make_dep_kind_vtables<'tcx>( |
| 185 | + arena: &'tcx rustc_middle::arena::Arena<'tcx>, |
| 186 | +) -> &'tcx [DepKindVTable<'tcx>] { |
| 187 | + // Create an array of vtables, one for each dep kind (non-query and query). |
| 188 | + let dep_kind_vtables: [DepKindVTable<'tcx>; _] = |
| 189 | + rustc_middle::make_dep_kind_array!(_dep_kind_vtable_ctors); |
| 190 | + arena.alloc_from_iter(dep_kind_vtables) |
| 191 | +} |
0 commit comments