diff --git a/compiler/rustc_interface/src/callbacks.rs b/compiler/rustc_interface/src/callbacks.rs index 7c6b7157f71a..3d8d5d59b118 100644 --- a/compiler/rustc_interface/src/callbacks.rs +++ b/compiler/rustc_interface/src/callbacks.rs @@ -72,7 +72,7 @@ fn def_id_debug(def_id: rustc_hir::def_id::DefId, f: &mut fmt::Formatter<'_>) -> pub fn dep_kind_debug(kind: DepKind, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { tls::with_opt(|opt_tcx| { if let Some(tcx) = opt_tcx { - write!(f, "{}", tcx.dep_kind_info(kind).name) + write!(f, "{}", tcx.dep_kind_vtable(kind).name) } else { default_dep_kind_debug(kind, f) } diff --git a/compiler/rustc_interface/src/passes.rs b/compiler/rustc_interface/src/passes.rs index 3228a0499acc..60b45f7391b5 100644 --- a/compiler/rustc_interface/src/passes.rs +++ b/compiler/rustc_interface/src/passes.rs @@ -992,7 +992,7 @@ pub fn create_and_enter_global_ctxt FnOnce(TyCtxt<'tcx>) -> T>( hir_arena, untracked, dep_graph, - rustc_query_impl::query_callbacks(arena), + rustc_query_impl::make_dep_kind_vtables(arena), rustc_query_impl::query_system( providers.queries, providers.extern_queries, diff --git a/compiler/rustc_middle/src/arena.rs b/compiler/rustc_middle/src/arena.rs index 0bdc1bfd45ee..0f254aaa9fa0 100644 --- a/compiler/rustc_middle/src/arena.rs +++ b/compiler/rustc_middle/src/arena.rs @@ -104,7 +104,7 @@ macro_rules! arena_types { [decode] is_late_bound_map: rustc_data_structures::fx::FxIndexSet, [decode] impl_source: rustc_middle::traits::ImplSource<'tcx, ()>, - [] dep_kind: rustc_middle::dep_graph::DepKindStruct<'tcx>, + [] dep_kind_vtable: rustc_middle::dep_graph::DepKindVTable<'tcx>, [decode] trait_impl_trait_tys: rustc_data_structures::unord::UnordMap< diff --git a/compiler/rustc_middle/src/dep_graph/mod.rs b/compiler/rustc_middle/src/dep_graph/mod.rs index 049e868879e9..f28ba10d52e2 100644 --- a/compiler/rustc_middle/src/dep_graph/mod.rs +++ b/compiler/rustc_middle/src/dep_graph/mod.rs @@ -18,7 +18,7 @@ pub use rustc_query_system::dep_graph::{ pub type DepGraph = rustc_query_system::dep_graph::DepGraph; -pub type DepKindStruct<'tcx> = rustc_query_system::dep_graph::DepKindStruct>; +pub type DepKindVTable<'tcx> = rustc_query_system::dep_graph::DepKindVTable>; pub struct DepsType; @@ -79,8 +79,8 @@ impl<'tcx> DepContext for TyCtxt<'tcx> { } #[inline] - fn dep_kind_info(&self, dk: DepKind) -> &DepKindStruct<'tcx> { - &self.query_kinds[dk.as_usize()] + fn dep_kind_vtable(&self, dk: DepKind) -> &DepKindVTable<'tcx> { + &self.dep_kind_vtables[dk.as_usize()] } fn with_reduced_queries(self, f: impl FnOnce() -> T) -> T { diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index 5ba5a3c3d4dc..f015d0edc56c 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -59,7 +59,7 @@ use rustc_type_ir::{ use tracing::{debug, instrument}; use crate::arena::Arena; -use crate::dep_graph::{DepGraph, DepKindStruct}; +use crate::dep_graph::{DepGraph, DepKindVTable}; use crate::infer::canonical::{CanonicalParamEnvCache, CanonicalVarKind, CanonicalVarKinds}; use crate::lint::lint_level; use crate::metadata::ModChild; @@ -1580,7 +1580,7 @@ pub struct GlobalCtxt<'tcx> { untracked: Untracked, pub query_system: QuerySystem<'tcx>, - pub(crate) query_kinds: &'tcx [DepKindStruct<'tcx>], + pub(crate) dep_kind_vtables: &'tcx [DepKindVTable<'tcx>], // Internal caches for metadata decoding. No need to track deps on this. pub ty_rcache: Lock>>, @@ -1801,7 +1801,7 @@ impl<'tcx> TyCtxt<'tcx> { hir_arena: &'tcx WorkerLocal>, untracked: Untracked, dep_graph: DepGraph, - query_kinds: &'tcx [DepKindStruct<'tcx>], + dep_kind_vtables: &'tcx [DepKindVTable<'tcx>], query_system: QuerySystem<'tcx>, hooks: crate::hooks::Providers, current_gcx: CurrentGcx, @@ -1831,7 +1831,7 @@ impl<'tcx> TyCtxt<'tcx> { consts: common_consts, untracked, query_system, - query_kinds, + dep_kind_vtables, ty_rcache: Default::default(), selection_cache: Default::default(), evaluation_cache: Default::default(), diff --git a/compiler/rustc_query_impl/src/lib.rs b/compiler/rustc_query_impl/src/lib.rs index c9abc4bdcdfc..57027e937a4a 100644 --- a/compiler/rustc_query_impl/src/lib.rs +++ b/compiler/rustc_query_impl/src/lib.rs @@ -9,7 +9,7 @@ use rustc_data_structures::stable_hasher::HashStable; use rustc_data_structures::sync::AtomicU64; use rustc_middle::arena::Arena; -use rustc_middle::dep_graph::{self, DepKind, DepKindStruct, DepNodeIndex}; +use rustc_middle::dep_graph::{self, DepKind, DepKindVTable, DepNodeIndex}; use rustc_middle::query::erase::{Erase, erase, restore}; use rustc_middle::query::on_disk_cache::{CacheEncoder, EncodedDepNodeIndex, OnDiskCache}; use rustc_middle::query::plumbing::{DynamicQuery, QuerySystem, QuerySystemFns}; diff --git a/compiler/rustc_query_impl/src/plumbing.rs b/compiler/rustc_query_impl/src/plumbing.rs index 7479a992e297..246152f5390c 100644 --- a/compiler/rustc_query_impl/src/plumbing.rs +++ b/compiler/rustc_query_impl/src/plumbing.rs @@ -12,7 +12,7 @@ use rustc_hir::limit::Limit; use rustc_index::Idx; use rustc_middle::bug; use rustc_middle::dep_graph::{ - self, DepContext, DepKind, DepKindStruct, DepNode, DepNodeIndex, SerializedDepNodeIndex, + self, DepContext, DepKind, DepKindVTable, DepNode, DepNodeIndex, SerializedDepNodeIndex, dep_kinds, }; use rustc_middle::query::Key; @@ -489,14 +489,17 @@ where } } -pub(crate) fn query_callback<'tcx, Q>(is_anon: bool, is_eval_always: bool) -> DepKindStruct<'tcx> +pub(crate) fn make_dep_kind_vtable_for_query<'tcx, Q>( + is_anon: bool, + is_eval_always: bool, +) -> DepKindVTable<'tcx> where Q: QueryConfigRestored<'tcx>, { let fingerprint_style = >>::Key::fingerprint_style(); if is_anon || !fingerprint_style.reconstructible() { - return DepKindStruct { + return DepKindVTable { is_anon, is_eval_always, fingerprint_style, @@ -506,7 +509,7 @@ where }; } - DepKindStruct { + DepKindVTable { is_anon, is_eval_always, fingerprint_style, @@ -811,15 +814,19 @@ macro_rules! define_queries { for<'tcx> fn(TyCtxt<'tcx>) ] = &[$(query_impl::$name::query_key_hash_verify),*]; - #[allow(nonstandard_style)] - mod query_callbacks { + /// Module containing a named function for each dep kind (including queries) + /// that creates a `DepKindVTable`. + /// + /// Consumed via `make_dep_kind_array!` to create a list of vtables. + #[expect(non_snake_case)] + mod _dep_kind_vtable_ctors { use super::*; use rustc_middle::bug; use rustc_query_system::dep_graph::FingerprintStyle; // We use this for most things when incr. comp. is turned off. - pub(crate) fn Null<'tcx>() -> DepKindStruct<'tcx> { - DepKindStruct { + pub(crate) fn Null<'tcx>() -> DepKindVTable<'tcx> { + DepKindVTable { is_anon: false, is_eval_always: false, fingerprint_style: FingerprintStyle::Unit, @@ -830,8 +837,8 @@ macro_rules! define_queries { } // We use this for the forever-red node. - pub(crate) fn Red<'tcx>() -> DepKindStruct<'tcx> { - DepKindStruct { + pub(crate) fn Red<'tcx>() -> DepKindVTable<'tcx> { + DepKindVTable { is_anon: false, is_eval_always: false, fingerprint_style: FingerprintStyle::Unit, @@ -841,8 +848,8 @@ macro_rules! define_queries { } } - pub(crate) fn SideEffect<'tcx>() -> DepKindStruct<'tcx> { - DepKindStruct { + pub(crate) fn SideEffect<'tcx>() -> DepKindVTable<'tcx> { + DepKindVTable { is_anon: false, is_eval_always: false, fingerprint_style: FingerprintStyle::Unit, @@ -855,8 +862,8 @@ macro_rules! define_queries { } } - pub(crate) fn AnonZeroDeps<'tcx>() -> DepKindStruct<'tcx> { - DepKindStruct { + pub(crate) fn AnonZeroDeps<'tcx>() -> DepKindVTable<'tcx> { + DepKindVTable { is_anon: true, is_eval_always: false, fingerprint_style: FingerprintStyle::Opaque, @@ -866,8 +873,8 @@ macro_rules! define_queries { } } - pub(crate) fn TraitSelect<'tcx>() -> DepKindStruct<'tcx> { - DepKindStruct { + pub(crate) fn TraitSelect<'tcx>() -> DepKindVTable<'tcx> { + DepKindVTable { is_anon: true, is_eval_always: false, fingerprint_style: FingerprintStyle::Unit, @@ -877,8 +884,8 @@ macro_rules! define_queries { } } - pub(crate) fn CompileCodegenUnit<'tcx>() -> DepKindStruct<'tcx> { - DepKindStruct { + pub(crate) fn CompileCodegenUnit<'tcx>() -> DepKindVTable<'tcx> { + DepKindVTable { is_anon: false, is_eval_always: false, fingerprint_style: FingerprintStyle::Opaque, @@ -888,8 +895,8 @@ macro_rules! define_queries { } } - pub(crate) fn CompileMonoItem<'tcx>() -> DepKindStruct<'tcx> { - DepKindStruct { + pub(crate) fn CompileMonoItem<'tcx>() -> DepKindVTable<'tcx> { + DepKindVTable { is_anon: false, is_eval_always: false, fingerprint_style: FingerprintStyle::Opaque, @@ -899,8 +906,8 @@ macro_rules! define_queries { } } - pub(crate) fn Metadata<'tcx>() -> DepKindStruct<'tcx> { - DepKindStruct { + pub(crate) fn Metadata<'tcx>() -> DepKindVTable<'tcx> { + DepKindVTable { is_anon: false, is_eval_always: false, fingerprint_style: FingerprintStyle::Unit, @@ -910,16 +917,17 @@ macro_rules! define_queries { } } - $(pub(crate) fn $name<'tcx>()-> DepKindStruct<'tcx> { - $crate::plumbing::query_callback::>( + $(pub(crate) fn $name<'tcx>() -> DepKindVTable<'tcx> { + use $crate::query_impl::$name::QueryType; + $crate::plumbing::make_dep_kind_vtable_for_query::>( is_anon!([$($modifiers)*]), is_eval_always!([$($modifiers)*]), ) })* } - pub fn query_callbacks<'tcx>(arena: &'tcx Arena<'tcx>) -> &'tcx [DepKindStruct<'tcx>] { - arena.alloc_from_iter(rustc_middle::make_dep_kind_array!(query_callbacks)) + pub fn make_dep_kind_vtables<'tcx>(arena: &'tcx Arena<'tcx>) -> &'tcx [DepKindVTable<'tcx>] { + arena.alloc_from_iter(rustc_middle::make_dep_kind_array!(_dep_kind_vtable_ctors)) } } } diff --git a/compiler/rustc_query_system/src/dep_graph/dep_node.rs b/compiler/rustc_query_system/src/dep_graph/dep_node.rs index bdd1d5f3e88a..72bdcd2d534d 100644 --- a/compiler/rustc_query_system/src/dep_graph/dep_node.rs +++ b/compiler/rustc_query_system/src/dep_graph/dep_node.rs @@ -221,12 +221,12 @@ where } } -/// This struct stores metadata about each DepKind. +/// This struct stores function pointers and other metadata for a particular DepKind. /// /// Information is retrieved by indexing the `DEP_KINDS` array using the integer value /// of the `DepKind`. Overall, this allows to implement `DepContext` using this manual /// jump table instead of large matches. -pub struct DepKindStruct { +pub struct DepKindVTable { /// Anonymous queries cannot be replayed from one compiler invocation to the next. /// When their result is needed, it is recomputed. They are useful for fine-grained /// dependency tracking, and caching within one compiler invocation. diff --git a/compiler/rustc_query_system/src/dep_graph/mod.rs b/compiler/rustc_query_system/src/dep_graph/mod.rs index 8b9e4fe1bf29..874b41cbf3b1 100644 --- a/compiler/rustc_query_system/src/dep_graph/mod.rs +++ b/compiler/rustc_query_system/src/dep_graph/mod.rs @@ -7,7 +7,7 @@ mod serialized; use std::panic; -pub use dep_node::{DepKind, DepKindStruct, DepNode, DepNodeParams, WorkProductId}; +pub use dep_node::{DepKind, DepKindVTable, DepNode, DepNodeParams, WorkProductId}; pub(crate) use graph::DepGraphData; pub use graph::{DepGraph, DepNodeIndex, TaskDepsRef, WorkProduct, WorkProductMap, hash_result}; pub use query::DepGraphQuery; @@ -35,21 +35,21 @@ pub trait DepContext: Copy { /// Access the compiler session. fn sess(&self) -> &Session; - fn dep_kind_info(&self, dep_node: DepKind) -> &DepKindStruct; + fn dep_kind_vtable(&self, dep_node: DepKind) -> &DepKindVTable; #[inline(always)] fn fingerprint_style(self, kind: DepKind) -> FingerprintStyle { - let data = self.dep_kind_info(kind); - if data.is_anon { + let vtable = self.dep_kind_vtable(kind); + if vtable.is_anon { return FingerprintStyle::Opaque; } - data.fingerprint_style + vtable.fingerprint_style } #[inline(always)] /// Return whether this kind always require evaluation. fn is_eval_always(self, kind: DepKind) -> bool { - self.dep_kind_info(kind).is_eval_always + self.dep_kind_vtable(kind).is_eval_always } /// Try to force a dep node to execute and see if it's green. @@ -65,9 +65,10 @@ pub trait DepContext: Copy { prev_index: SerializedDepNodeIndex, frame: &MarkFrame<'_>, ) -> bool { - let cb = self.dep_kind_info(dep_node.kind); - if let Some(f) = cb.force_from_dep_node { - match panic::catch_unwind(panic::AssertUnwindSafe(|| f(self, dep_node, prev_index))) { + 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::() { print_markframe_trace(self.dep_graph(), frame); @@ -83,9 +84,8 @@ pub trait DepContext: Copy { /// Load data from the on-disk cache. fn try_load_from_on_disk_cache(self, dep_node: DepNode) { - let cb = self.dep_kind_info(dep_node.kind); - if let Some(f) = cb.try_load_from_on_disk_cache { - f(self, dep_node) + if let Some(try_load_fn) = self.dep_kind_vtable(dep_node.kind).try_load_from_on_disk_cache { + try_load_fn(self, dep_node) } } diff --git a/compiler/rustc_query_system/src/query/job.rs b/compiler/rustc_query_system/src/query/job.rs index 79d08d33c0b1..0431151c74c9 100644 --- a/compiler/rustc_query_system/src/query/job.rs +++ b/compiler/rustc_query_system/src/query/job.rs @@ -626,7 +626,7 @@ pub fn print_query_stack( file, "#{} [{}] {}", count_total, - qcx.dep_context().dep_kind_info(query_info.query.dep_kind).name, + qcx.dep_context().dep_kind_vtable(query_info.query.dep_kind).name, query_info.query.description ); }