Move rustc_query_system::query::dep_graph to rustc_middle.

Most of the files within the `dep_graph` module can be moved wholesale
into `rustc_middle`. But two of them (`mod.rs` and `dep_node.rs`) have
the same name as existing files in `rustc_middle`, so for those I just
copied the contents into the existing files.

The commit also moves `QueryContext` and `incremental_verify_ich*`
because they are tightly intertwined with the dep graph code. And a
couple of error structs moved as well.
This commit is contained in:
Nicholas Nethercote 2026-02-11 17:02:27 +11:00
parent fa3b046aa5
commit ed091aaf5d
33 changed files with 580 additions and 610 deletions

View file

@ -4093,7 +4093,6 @@ dependencies = [
"rustc_passes",
"rustc_privacy",
"rustc_query_impl",
"rustc_query_system",
"rustc_resolve",
"rustc_session",
"rustc_span",
@ -4519,13 +4518,11 @@ dependencies = [
"rustc_errors",
"rustc_feature",
"rustc_hir",
"rustc_index",
"rustc_macros",
"rustc_serialize",
"rustc_session",
"rustc_span",
"smallvec",
"tracing",
]
[[package]]

View file

@ -35,7 +35,6 @@ rustc_parse = { path = "../rustc_parse" }
rustc_passes = { path = "../rustc_passes" }
rustc_privacy = { path = "../rustc_privacy" }
rustc_query_impl = { path = "../rustc_query_impl" }
rustc_query_system = { path = "../rustc_query_system" }
rustc_resolve = { path = "../rustc_resolve" }
rustc_session = { path = "../rustc_session" }
rustc_span = { path = "../rustc_span" }

View file

@ -12,11 +12,10 @@
use std::fmt;
use rustc_errors::{DiagInner, TRACK_DIAGNOSTIC};
use rustc_middle::dep_graph::{DepNodeExt, TaskDepsRef};
use rustc_middle::dep_graph::dep_node::default_dep_kind_debug;
use rustc_middle::dep_graph::{DepContext, DepKind, DepNode, DepNodeExt, TaskDepsRef};
use rustc_middle::ty::tls;
use rustc_query_impl::QueryCtxt;
use rustc_query_system::dep_graph::dep_node::default_dep_kind_debug;
use rustc_query_system::dep_graph::{DepContext, DepKind, DepNode};
fn track_span_parent(def_id: rustc_span::def_id::LocalDefId) {
tls::with_context_opt(|icx| {
@ -107,9 +106,9 @@ pub fn dep_node_debug(node: DepNode, f: &mut std::fmt::Formatter<'_>) -> std::fm
pub fn setup_callbacks() {
rustc_span::SPAN_TRACK.swap(&(track_span_parent as fn(_)));
rustc_hir::def_id::DEF_ID_DEBUG.swap(&(def_id_debug as fn(_, &mut fmt::Formatter<'_>) -> _));
rustc_query_system::dep_graph::dep_node::DEP_KIND_DEBUG
rustc_middle::dep_graph::dep_node::DEP_KIND_DEBUG
.swap(&(dep_kind_debug as fn(_, &mut fmt::Formatter<'_>) -> _));
rustc_query_system::dep_graph::dep_node::DEP_NODE_DEBUG
rustc_middle::dep_graph::dep_node::DEP_NODE_DEBUG
.swap(&(dep_node_debug as fn(_, &mut fmt::Formatter<'_>) -> _));
TRACK_DIAGNOSTIC.swap(&(track_diagnostic as _));
}

View file

@ -1,12 +1,333 @@
//! This module defines the [`DepNode`] type which the compiler uses to represent
//! nodes in the [dependency graph]. A `DepNode` consists of a [`DepKind`] (which
//! specifies the kind of thing it represents, like a piece of HIR, MIR, etc.)
//! and a [`Fingerprint`], a 128-bit hash value, the exact meaning of which
//! depends on the node's `DepKind`. Together, the kind and the fingerprint
//! fully identify a dependency node, even across multiple compilation sessions.
//! In other words, the value of the fingerprint does not depend on anything
//! that is specific to a given compilation session, like an unpredictable
//! interning key (e.g., `NodeId`, `DefId`, `Symbol`) or the numeric value of a
//! pointer. The concept behind this could be compared to how git commit hashes
//! uniquely identify a given commit. The fingerprinting approach has
//! a few advantages:
//!
//! * A `DepNode` can simply be serialized to disk and loaded in another session
//! without the need to do any "rebasing" (like we have to do for Spans and
//! NodeIds) or "retracing" (like we had to do for `DefId` in earlier
//! implementations of the dependency graph).
//! * A `Fingerprint` is just a bunch of bits, which allows `DepNode` to
//! implement `Copy`, `Sync`, `Send`, `Freeze`, etc.
//! * Since we just have a bit pattern, `DepNode` can be mapped from disk into
//! memory without any post-processing (e.g., "abomination-style" pointer
//! reconstruction).
//! * Because a `DepNode` is self-contained, we can instantiate `DepNodes` that
//! refer to things that do not exist anymore. In previous implementations
//! `DepNode` contained a `DefId`. A `DepNode` referring to something that
//! had been removed between the previous and the current compilation session
//! could not be instantiated because the current compilation session
//! contained no `DefId` for thing that had been removed.
//!
//! `DepNode` definition happens in `rustc_middle` with the
//! `define_dep_nodes!()` macro. This macro defines the `DepKind` enum. Each
//! `DepKind` has its own parameters that are needed at runtime in order to
//! construct a valid `DepNode` fingerprint. However, only `CompileCodegenUnit`
//! and `CompileMonoItem` are constructed explicitly (with
//! `make_compile_codegen_unit` and `make_compile_mono_item`).
//!
//! Because the macro sees what parameters a given `DepKind` requires, it can
//! "infer" some properties for each kind of `DepNode`:
//!
//! * Whether a `DepNode` of a given kind has any parameters at all. Some
//! `DepNode`s could represent global concepts with only one value.
//! * Whether it is possible, in principle, to reconstruct a query key from a
//! given `DepNode`. Many `DepKind`s only require a single `DefId` parameter,
//! in which case it is possible to map the node's fingerprint back to the
//! `DefId` it was computed from. In other cases, too much information gets
//! lost during fingerprint computation.
//!
//! `make_compile_codegen_unit` and `make_compile_mono_items`, together with
//! `DepNode::new()`, ensure that only valid `DepNode` instances can be
//! constructed. For example, the API does not allow for constructing
//! parameterless `DepNode`s with anything other than a zeroed out fingerprint.
//! More generally speaking, it relieves the user of the `DepNode` API of
//! having to know how to compute the expected fingerprint for a given set of
//! node parameters.
//!
//! [dependency graph]: https://rustc-dev-guide.rust-lang.org/query.html
use std::fmt;
use std::hash::Hash;
use rustc_data_structures::AtomicRef;
use rustc_data_structures::fingerprint::{Fingerprint, PackedFingerprint};
use rustc_data_structures::stable_hasher::{HashStable, StableHasher, StableOrd, ToStableHashKey};
use rustc_hir::def_id::DefId;
use rustc_hir::definitions::DefPathHash;
use rustc_query_system::dep_graph::dep_node::DepKind;
use rustc_query_system::dep_graph::{DepContext, DepNode, FingerprintStyle};
use rustc_macros::{Decodable, Encodable};
use rustc_query_system::ich::StableHashingContext;
use rustc_span::Symbol;
use super::{DepContext, FingerprintStyle, SerializedDepNodeIndex};
use crate::mir::mono::MonoItem;
use crate::ty::TyCtxt;
/// This serves as an index into arrays built by `make_dep_kind_array`.
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct DepKind {
variant: u16,
}
impl DepKind {
#[inline]
pub const fn new(variant: u16) -> Self {
Self { variant }
}
#[inline]
pub const fn as_inner(&self) -> u16 {
self.variant
}
#[inline]
pub const fn as_usize(&self) -> usize {
self.variant as usize
}
}
pub fn default_dep_kind_debug(kind: DepKind, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("DepKind").field("variant", &kind.variant).finish()
}
pub static DEP_KIND_DEBUG: AtomicRef<fn(DepKind, &mut fmt::Formatter<'_>) -> fmt::Result> =
AtomicRef::new(&(default_dep_kind_debug as fn(_, &mut fmt::Formatter<'_>) -> _));
impl fmt::Debug for DepKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
(*DEP_KIND_DEBUG)(*self, f)
}
}
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct DepNode {
pub kind: DepKind,
pub hash: PackedFingerprint,
}
impl DepNode {
/// Creates a new, parameterless DepNode. This method will assert
/// that the DepNode corresponding to the given DepKind actually
/// does not require any parameters.
pub fn new_no_params<Tcx>(tcx: Tcx, kind: DepKind) -> DepNode
where
Tcx: super::DepContext,
{
debug_assert_eq!(tcx.fingerprint_style(kind), FingerprintStyle::Unit);
DepNode { kind, hash: Fingerprint::ZERO.into() }
}
pub fn construct<Tcx, Key>(tcx: Tcx, kind: DepKind, arg: &Key) -> DepNode
where
Tcx: super::DepContext,
Key: DepNodeKey<Tcx>,
{
let hash = arg.to_fingerprint(tcx);
let dep_node = DepNode { kind, hash: hash.into() };
#[cfg(debug_assertions)]
{
if !tcx.fingerprint_style(kind).reconstructible()
&& (tcx.sess().opts.unstable_opts.incremental_info
|| tcx.sess().opts.unstable_opts.query_dep_graph)
{
tcx.dep_graph().register_dep_node_debug_str(dep_node, || arg.to_debug_str(tcx));
}
}
dep_node
}
/// Construct a DepNode from the given DepKind and DefPathHash. This
/// method will assert that the given DepKind actually requires a
/// single DefId/DefPathHash parameter.
pub fn from_def_path_hash<Tcx>(tcx: Tcx, def_path_hash: DefPathHash, kind: DepKind) -> Self
where
Tcx: super::DepContext,
{
debug_assert!(tcx.fingerprint_style(kind) == FingerprintStyle::DefPathHash);
DepNode { kind, hash: def_path_hash.0.into() }
}
}
pub fn default_dep_node_debug(node: DepNode, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("DepNode").field("kind", &node.kind).field("hash", &node.hash).finish()
}
pub static DEP_NODE_DEBUG: AtomicRef<fn(DepNode, &mut fmt::Formatter<'_>) -> fmt::Result> =
AtomicRef::new(&(default_dep_node_debug as fn(_, &mut fmt::Formatter<'_>) -> _));
impl fmt::Debug for DepNode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
(*DEP_NODE_DEBUG)(*self, f)
}
}
/// Trait for query keys as seen by dependency-node tracking.
pub trait DepNodeKey<Tcx: DepContext>: fmt::Debug + Sized {
fn fingerprint_style() -> FingerprintStyle;
/// This method turns a query key into an opaque `Fingerprint` to be used
/// in `DepNode`.
fn to_fingerprint(&self, _: Tcx) -> Fingerprint;
fn to_debug_str(&self, tcx: Tcx) -> String;
/// This method tries to recover the query key from the given `DepNode`,
/// something which is needed when forcing `DepNode`s during red-green
/// evaluation. The query system will only call this method if
/// `fingerprint_style()` is not `FingerprintStyle::Opaque`.
/// It is always valid to return `None` here, in which case incremental
/// compilation will treat the query as having changed instead of forcing it.
fn recover(tcx: Tcx, dep_node: &DepNode) -> Option<Self>;
}
// Blanket impl of `DepNodeKey`, which is specialized by other impls elsewhere.
impl<Tcx: DepContext, T> DepNodeKey<Tcx> for T
where
T: for<'a> HashStable<StableHashingContext<'a>> + fmt::Debug,
{
#[inline(always)]
default fn fingerprint_style() -> FingerprintStyle {
FingerprintStyle::Opaque
}
#[inline(always)]
default fn to_fingerprint(&self, tcx: Tcx) -> Fingerprint {
tcx.with_stable_hashing_context(|mut hcx| {
let mut hasher = StableHasher::new();
self.hash_stable(&mut hcx, &mut hasher);
hasher.finish()
})
}
#[inline(always)]
default fn to_debug_str(&self, tcx: Tcx) -> String {
// Make sure to print dep node params with reduced queries since printing
// may themselves call queries, which may lead to (possibly untracked!)
// query cycles.
tcx.with_reduced_queries(|| format!("{self:?}"))
}
#[inline(always)]
default fn recover(_: Tcx, _: &DepNode) -> Option<Self> {
None
}
}
/// 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 DepKindVTable<Tcx: DepContext> {
/// 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.
pub is_anon: bool,
/// Eval-always queries do not track their dependencies, and are always recomputed, even if
/// their inputs have not changed since the last compiler invocation. The result is still
/// cached within one compiler invocation.
pub is_eval_always: bool,
/// Indicates whether and how the query key can be recovered from its hashed fingerprint.
///
/// The [`DepNodeKey`] trait determines the fingerprint style for each key type.
pub fingerprint_style: FingerprintStyle,
/// The red/green evaluation system will try to mark a specific DepNode in the
/// dependency graph as green by recursively trying to mark the dependencies of
/// that `DepNode` as green. While doing so, it will sometimes encounter a `DepNode`
/// where we don't know if it is red or green and we therefore actually have
/// to recompute its value in order to find out. Since the only piece of
/// information that we have at that point is the `DepNode` we are trying to
/// re-evaluate, we need some way to re-run a query from just that. This is what
/// `force_from_dep_node()` implements.
///
/// In the general case, a `DepNode` consists of a `DepKind` and an opaque
/// GUID/fingerprint that will uniquely identify the node. This GUID/fingerprint
/// is usually constructed by computing a stable hash of the query-key that the
/// `DepNode` corresponds to. Consequently, it is not in general possible to go
/// back from hash to query-key (since hash functions are not reversible). For
/// this reason `force_from_dep_node()` is expected to fail from time to time
/// because we just cannot find out, from the `DepNode` alone, what the
/// corresponding query-key is and therefore cannot re-run the query.
///
/// The system deals with this case letting `try_mark_green` fail which forces
/// the root query to be re-evaluated.
///
/// Now, if `force_from_dep_node()` would always fail, it would be pretty useless.
/// Fortunately, we can use some contextual information that will allow us to
/// reconstruct query-keys for certain kinds of `DepNode`s. In particular, we
/// enforce by construction that the GUID/fingerprint of certain `DepNode`s is a
/// valid `DefPathHash`. Since we also always build a huge table that maps every
/// `DefPathHash` in the current codebase to the corresponding `DefId`, we have
/// everything we need to re-run the query.
///
/// Take the `mir_promoted` query as an example. Like many other queries, it
/// just has a single parameter: the `DefId` of the item it will compute the
/// validated MIR for. Now, when we call `force_from_dep_node()` on a `DepNode`
/// with kind `MirValidated`, we know that the GUID/fingerprint of the `DepNode`
/// is actually a `DefPathHash`, and can therefore just look up the corresponding
/// `DefId` in `tcx.def_path_hash_to_def_id`.
pub force_from_dep_node:
Option<fn(tcx: Tcx, dep_node: DepNode, prev_index: SerializedDepNodeIndex) -> bool>,
/// Invoke a query to put the on-disk cached value in memory.
pub try_load_from_on_disk_cache: Option<fn(Tcx, DepNode)>,
/// The name of this dep kind.
pub name: &'static &'static str,
}
/// A "work product" corresponds to a `.o` (or other) file that we
/// save in between runs. These IDs do not have a `DefId` but rather
/// some independent path or string that persists between runs without
/// the need to be mapped or unmapped. (This ensures we can serialize
/// them even in the absence of a tcx.)
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Encodable, Decodable)]
pub struct WorkProductId {
hash: Fingerprint,
}
impl WorkProductId {
pub fn from_cgu_name(cgu_name: &str) -> WorkProductId {
let mut hasher = StableHasher::new();
cgu_name.hash(&mut hasher);
WorkProductId { hash: hasher.finish() }
}
}
impl<HCX> HashStable<HCX> for WorkProductId {
#[inline]
fn hash_stable(&self, hcx: &mut HCX, hasher: &mut StableHasher) {
self.hash.hash_stable(hcx, hasher)
}
}
impl<HCX> ToStableHashKey<HCX> for WorkProductId {
type KeyType = Fingerprint;
#[inline]
fn to_stable_hash_key(&self, _: &HCX) -> Self::KeyType {
self.hash
}
}
impl StableOrd for WorkProductId {
// Fingerprint can use unstable (just a tuple of `u64`s), so WorkProductId can as well
const CAN_USE_UNSTABLE_SORT: bool = true;
// `WorkProductId` sort order is not affected by (de)serialization.
const THIS_IMPLEMENTATION_HAS_BEEN_TRIPLE_CHECKED: () = ();
}
macro_rules! define_dep_nodes {
(
$(
@ -172,3 +493,18 @@ pub fn dep_kind_from_label(label: &str) -> DepKind {
dep_kind_from_label_string(label)
.unwrap_or_else(|_| panic!("Query label {label} does not exist"))
}
// Some types are used a lot. Make sure they don't unintentionally get bigger.
#[cfg(target_pointer_width = "64")]
mod size_asserts {
use rustc_data_structures::static_assert_size;
use super::*;
// tidy-alphabetical-start
static_assert_size!(DepKind, 2);
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
static_assert_size!(DepNode, 18);
#[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
static_assert_size!(DepNode, 24);
// tidy-alphabetical-end
}

View file

@ -2,9 +2,8 @@ use rustc_data_structures::fingerprint::Fingerprint;
use rustc_hir::def_id::{CrateNum, DefId, LOCAL_CRATE, LocalDefId, LocalModDefId, ModDefId};
use rustc_hir::definitions::DefPathHash;
use rustc_hir::{HirId, ItemLocalId, OwnerId};
use rustc_query_system::dep_graph::{DepContext, DepNode, DepNodeKey, FingerprintStyle};
use crate::dep_graph::DepNodeExt;
use crate::dep_graph::{DepContext, DepNode, DepNodeExt, DepNodeKey, FingerprintStyle};
use crate::ty::TyCtxt;
impl<'tcx> DepNodeKey<TyCtxt<'tcx>> for () {

View file

@ -15,6 +15,8 @@ use rustc_data_structures::{assert_matches, outline};
use rustc_errors::DiagInner;
use rustc_index::IndexVec;
use rustc_macros::{Decodable, Encodable};
use rustc_query_system::ich::StableHashingContext;
use rustc_query_system::query::QuerySideEffect;
use rustc_serialize::opaque::{FileEncodeResult, FileEncoder};
use rustc_session::Session;
use tracing::{debug, instrument};
@ -25,8 +27,8 @@ use super::query::DepGraphQuery;
use super::serialized::{GraphEncoder, SerializedDepGraph, SerializedDepNodeIndex};
use super::{DepContext, DepKind, DepNode, Deps, HasDepContext, WorkProductId};
use crate::dep_graph::edges::EdgesVec;
use crate::ich::StableHashingContext;
use crate::query::{QueryContext, QuerySideEffect};
use crate::query::QueryContext;
use crate::verify_ich::incremental_verify_ich;
pub struct DepGraph<D: Deps> {
data: Option<Arc<DepGraphData<D>>>,
@ -583,7 +585,7 @@ impl<D: Deps> DepGraph<D> {
if let Some(prev_index) = data.previous.node_to_index_opt(&node) {
let dep_node_index = data.colors.current(prev_index);
if let Some(dep_node_index) = dep_node_index {
crate::query::incremental_verify_ich(
incremental_verify_ich(
cx,
data,
result,

View file

@ -1,26 +1,187 @@
use std::panic;
use rustc_data_structures::profiling::SelfProfilerRef;
use rustc_data_structures::sync::DynSync;
use rustc_query_system::ich::StableHashingContext;
use rustc_session::Session;
use tracing::instrument;
pub use self::dep_node::{
DepKind, DepNode, DepNodeExt, DepNodeKey, WorkProductId, dep_kind_from_label, dep_kinds,
label_strs,
};
pub use self::graph::{
DepGraphData, DepNodeIndex, TaskDepsRef, WorkProduct, WorkProductMap, hash_result,
};
use self::graph::{MarkFrame, print_markframe_trace};
pub use self::query::DepGraphQuery;
pub use self::serialized::{SerializedDepGraph, SerializedDepNodeIndex};
pub use crate::dep_graph::debug::{DepNodeFilter, EdgeFilter};
use crate::ty::print::with_reduced_queries;
use crate::ty::{self, TyCtxt};
#[macro_use]
mod dep_node;
mod debug;
pub mod dep_node;
mod dep_node_key;
mod edges;
mod graph;
mod query;
mod serialized;
pub use rustc_query_system::dep_graph::debug::{DepNodeFilter, EdgeFilter};
pub use rustc_query_system::dep_graph::{
DepContext, DepGraphQuery, DepKind, DepNode, DepNodeIndex, Deps, SerializedDepGraph,
SerializedDepNodeIndex, TaskDepsRef, WorkProduct, WorkProductId, WorkProductMap, hash_result,
};
pub trait DepContext: Copy {
type Deps: Deps;
pub use self::dep_node::{DepNodeExt, dep_kind_from_label, dep_kinds, label_strs};
pub(crate) use self::dep_node::{make_compile_codegen_unit, make_compile_mono_item, make_metadata};
/// Create a hashing context for hashing new results.
fn with_stable_hashing_context<R>(self, f: impl FnOnce(StableHashingContext<'_>) -> R) -> R;
pub type DepGraph = rustc_query_system::dep_graph::DepGraph<DepsType>;
/// Access the DepGraph.
fn dep_graph(&self) -> &graph::DepGraph<Self::Deps>;
pub type DepKindVTable<'tcx> = rustc_query_system::dep_graph::DepKindVTable<TyCtxt<'tcx>>;
/// Access the profiler.
fn profiler(&self) -> &SelfProfilerRef;
/// Access the compiler session.
fn sess(&self) -> &Session;
fn dep_kind_vtable(&self, dep_node: DepKind) -> &dep_node::DepKindVTable<Self>;
#[inline(always)]
fn fingerprint_style(self, kind: DepKind) -> FingerprintStyle {
self.dep_kind_vtable(kind).fingerprint_style
}
#[inline(always)]
/// Return whether this kind always require evaluation.
fn is_eval_always(self, kind: DepKind) -> bool {
self.dep_kind_vtable(kind).is_eval_always
}
/// Try to force a dep node to execute and see if it's green.
///
/// Returns true if the query has actually been forced. It is valid that a query
/// fails to be forced, e.g. when the query key cannot be reconstructed from the
/// dep-node or when the query kind outright does not support it.
#[inline]
#[instrument(skip(self, frame), level = "debug")]
fn try_force_from_dep_node(
self,
dep_node: DepNode,
prev_index: SerializedDepNodeIndex,
frame: &MarkFrame<'_>,
) -> bool {
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
}
}
/// Load data from the on-disk cache.
fn try_load_from_on_disk_cache(self, dep_node: &DepNode) {
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)
}
}
fn with_reduced_queries<T>(self, _: impl FnOnce() -> T) -> T;
}
pub trait Deps: DynSync {
/// Execute the operation with provided dependencies.
fn with_deps<OP, R>(deps: TaskDepsRef<'_>, op: OP) -> R
where
OP: FnOnce() -> R;
/// Access dependencies from current implicit context.
fn read_deps<OP>(op: OP)
where
OP: for<'a> FnOnce(TaskDepsRef<'a>);
fn name(dep_kind: DepKind) -> &'static str;
/// We use this for most things when incr. comp. is turned off.
const DEP_KIND_NULL: DepKind;
/// We use this to create a forever-red node.
const DEP_KIND_RED: DepKind;
/// We use this to create a side effect node.
const DEP_KIND_SIDE_EFFECT: DepKind;
/// We use this to create the anon node with zero dependencies.
const DEP_KIND_ANON_ZERO_DEPS: DepKind;
/// This is the highest value a `DepKind` can have. It's used during encoding to
/// pack information into the unused bits.
const DEP_KIND_MAX: u16;
}
pub trait HasDepContext: Copy {
type Deps: self::Deps;
type DepContext: self::DepContext<Deps = Self::Deps>;
fn dep_context(&self) -> &Self::DepContext;
}
impl<T: DepContext> HasDepContext for T {
type Deps = T::Deps;
type DepContext = Self;
fn dep_context(&self) -> &Self::DepContext {
self
}
}
impl<T: HasDepContext, Q: Copy> HasDepContext for (T, Q) {
type Deps = T::Deps;
type DepContext = T::DepContext;
fn dep_context(&self) -> &Self::DepContext {
self.0.dep_context()
}
}
/// Describes the contents of the fingerprint generated by a given query.
///
/// This is mainly for determining whether and how we can reconstruct a key
/// from the fingerprint.
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
pub enum FingerprintStyle {
/// The fingerprint is actually a DefPathHash.
DefPathHash,
/// The fingerprint is actually a HirId.
HirId,
/// Query key was `()` or equivalent, so fingerprint is just zero.
Unit,
/// The fingerprint is an opaque hash, and a key cannot be reconstructed from it.
Opaque,
}
impl FingerprintStyle {
#[inline]
pub const fn reconstructible(self) -> bool {
match self {
FingerprintStyle::DefPathHash | FingerprintStyle::Unit | FingerprintStyle::HirId => {
true
}
FingerprintStyle::Opaque => false,
}
}
}
pub type DepGraph = graph::DepGraph<DepsType>;
pub type DepKindVTable<'tcx> = dep_node::DepKindVTable<TyCtxt<'tcx>>;
pub struct DepsType;

View file

@ -197,3 +197,20 @@ pub(crate) struct InvalidConstInValtree {
pub span: Span,
pub global_const_id: String,
}
#[derive(Diagnostic)]
#[diag("internal compiler error: reentrant incremental verify failure, suppressing message")]
pub(crate) struct Reentrant;
#[derive(Diagnostic)]
#[diag("internal compiler error: encountered incremental compilation error with {$dep_node}")]
#[note("please follow the instructions below to create a bug report with the provided information")]
#[note("for incremental compilation bugs, having a reproduction is vital")]
#[note(
"an ideal reproduction consists of the code before and some patch that then triggers the bug when applied and compiled again"
)]
#[note("as a workaround, you can run {$run_cmd} to allow your project to compile")]
pub(crate) struct IncrementCompilation {
pub run_cmd: String,
pub dep_node: String,
}

View file

@ -81,6 +81,7 @@ pub mod thir;
pub mod traits;
pub mod ty;
pub mod util;
pub mod verify_ich;
#[macro_use]
pub mod query;

View file

@ -18,6 +18,7 @@ use rustc_span::{Span, Symbol};
use rustc_target::spec::SymbolVisibility;
use tracing::debug;
use crate::dep_graph::dep_node::{make_compile_codegen_unit, make_compile_mono_item};
use crate::dep_graph::{DepNode, WorkProduct, WorkProductId};
use crate::middle::codegen_fn_attrs::CodegenFnAttrFlags;
use crate::ty::{self, GenericArgs, Instance, InstanceKind, SymbolName, Ty, TyCtxt};
@ -290,7 +291,7 @@ impl<'tcx> MonoItem<'tcx> {
// Only used by rustc_codegen_cranelift
pub fn codegen_dep_node(&self, tcx: TyCtxt<'tcx>) -> DepNode {
crate::dep_graph::make_compile_mono_item(tcx, self)
make_compile_mono_item(tcx, self)
}
/// Returns the item's `CrateNum`
@ -576,7 +577,7 @@ impl<'tcx> CodegenUnit<'tcx> {
}
pub fn codegen_dep_node(&self, tcx: TyCtxt<'tcx>) -> DepNode {
crate::dep_graph::make_compile_codegen_unit(tcx, self.name())
make_compile_codegen_unit(tcx, self.name())
}
}

View file

@ -1,10 +1,10 @@
//! Helper functions that serve as the immediate implementation of
//! `tcx.$query(..)` and its variations.
use rustc_query_system::dep_graph::{DepKind, DepNodeKey};
use rustc_span::{DUMMY_SP, ErrorGuaranteed, Span};
use crate::dep_graph;
use crate::dep_graph::{DepKind, DepNodeKey};
use crate::query::erase::{self, Erasable, Erased};
use crate::query::plumbing::QueryVTable;
use crate::query::{QueryCache, QueryMode};

View file

@ -5,9 +5,9 @@ use std::ffi::OsStr;
use rustc_ast::tokenstream::TokenStream;
use rustc_hir::def_id::{CrateNum, DefId, LOCAL_CRATE, LocalDefId, LocalModDefId};
use rustc_hir::hir_id::OwnerId;
use rustc_query_system::dep_graph::DepNodeIndex;
use rustc_span::{DUMMY_SP, Ident, LocalExpnId, Span, Symbol};
use crate::dep_graph::DepNodeIndex;
use crate::infer::canonical::CanonicalQueryInput;
use crate::mir::mono::CollectionMode;
use crate::query::{DefIdCache, DefaultCache, SingleCache, VecCache};

View file

@ -1,4 +1,6 @@
use rustc_data_structures::jobserver::Proxy;
use rustc_hir::def_id::LocalDefId;
use rustc_query_system::query::QuerySideEffect;
pub use self::caches::{
DefIdCache, DefaultCache, QueryCache, QueryCacheKey, SingleCache, VecCache,
@ -10,6 +12,7 @@ pub use self::plumbing::{
TyCtxtAt, TyCtxtEnsureDone, TyCtxtEnsureOk,
};
pub use self::stack::{QueryStackDeferred, QueryStackFrame, QueryStackFrameExtra};
use crate::dep_graph::{DepNodeIndex, HasDepContext, SerializedDepNodeIndex};
pub use crate::queries::Providers;
use crate::ty::TyCtxt;
@ -33,3 +36,18 @@ pub fn describe_as_module(def_id: impl Into<LocalDefId>, tcx: TyCtxt<'_>) -> Str
format!("module `{}`", tcx.def_path_str(def_id))
}
}
pub trait QueryContext<'tcx>: HasDepContext {
/// Gets a jobserver reference which is used to release then acquire
/// a token while waiting on a query.
fn jobserver_proxy(&self) -> &Proxy;
/// Load a side effect associated to the node in the previous session.
fn load_side_effect(
self,
prev_dep_node_index: SerializedDepNodeIndex,
) -> Option<QuerySideEffect>;
/// Register a side effect for the given node, for use in next session.
fn store_side_effect(self, dep_node_index: DepNodeIndex, side_effect: QuerySideEffect);
}

View file

@ -8,13 +8,12 @@ use rustc_data_structures::sync::{AtomicU64, WorkerLocal};
use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_hir::hir_id::OwnerId;
use rustc_macros::HashStable;
use rustc_query_system::dep_graph::{DepNodeIndex, SerializedDepNodeIndex};
use rustc_query_system::ich::StableHashingContext;
use rustc_span::{ErrorGuaranteed, Span};
pub use sealed::IntoQueryParam;
use crate::dep_graph;
use crate::dep_graph::DepKind;
use crate::dep_graph::{DepKind, DepNodeIndex, SerializedDepNodeIndex};
use crate::queries::{
ExternProviders, PerQueryVTables, Providers, QueryArenas, QueryCaches, QueryEngine, QueryStates,
};

View file

@ -39,7 +39,6 @@ use rustc_hir::lang_items::LangItem;
use rustc_hir::limit::Limit;
use rustc_hir::{self as hir, HirId, Node, TraitCandidate, find_attr};
use rustc_index::IndexVec;
use rustc_query_system::dep_graph::DepNodeIndex;
use rustc_query_system::ich::StableHashingContext;
use rustc_serialize::opaque::{FileEncodeResult, FileEncoder};
use rustc_session::Session;
@ -54,7 +53,8 @@ use rustc_type_ir::{CollectAndApply, TypeFlags, WithCachedTypeInfo, elaborate, s
use tracing::{debug, instrument};
use crate::arena::Arena;
use crate::dep_graph::{DepGraph, DepKindVTable};
use crate::dep_graph::dep_node::make_metadata;
use crate::dep_graph::{DepGraph, DepKindVTable, DepNodeIndex};
use crate::infer::canonical::{CanonicalParamEnvCache, CanonicalVarKind};
use crate::lint::lint_level;
use crate::metadata::ModChild;
@ -2751,7 +2751,7 @@ impl<'tcx> TyCtxt<'tcx> {
}
pub fn metadata_dep_node(self) -> crate::dep_graph::DepNode {
crate::dep_graph::make_metadata(self)
make_metadata(self)
}
pub fn needs_coroutine_by_move_body_def_id(self, def_id: DefId) -> bool {

View file

@ -9,11 +9,11 @@ use rustc_hir::def::{CtorKind, CtorOf, DefKind};
use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_hir::lang_items::LangItem;
use rustc_hir::{self as hir};
use rustc_query_system::dep_graph::DepNodeIndex;
use rustc_span::{DUMMY_SP, Span, Symbol};
use rustc_type_ir::lang_items::{SolverAdtLangItem, SolverLangItem, SolverTraitLangItem};
use rustc_type_ir::{CollectAndApply, Interner, TypeFoldable, search_graph};
use crate::dep_graph::DepNodeIndex;
use crate::infer::canonical::CanonicalVarKinds;
use crate::query::IntoQueryParam;
use crate::traits::cache::WithDepNode;

View file

@ -1,11 +1,10 @@
use std::cell::Cell;
use rustc_data_structures::fingerprint::Fingerprint;
use rustc_query_system::ich::StableHashingContext;
use tracing::instrument;
use crate::dep_graph::{DepContext, DepGraphData};
use crate::ich::StableHashingContext;
use crate::query::SerializedDepNodeIndex;
use crate::dep_graph::{DepContext, DepGraphData, SerializedDepNodeIndex};
#[inline]
#[instrument(skip(tcx, dep_graph_data, result, hash_result, format_value), level = "debug")]

View file

@ -1,8 +1,7 @@
use rustc_middle::bug;
use rustc_middle::dep_graph::DepKindVTable;
use rustc_middle::dep_graph::{DepKindVTable, DepNodeKey, FingerprintStyle};
use rustc_middle::query::QueryCache;
use rustc_middle::ty::TyCtxt;
use rustc_query_system::dep_graph::{DepNodeKey, FingerprintStyle};
use crate::plumbing::{force_from_dep_node_inner, try_load_from_on_disk_cache_inner};
use crate::{QueryCtxt, QueryDispatcherUnerased, QueryFlags};

View file

@ -5,14 +5,13 @@ use rustc_data_structures::hash_table::{Entry, HashTable};
use rustc_data_structures::stack::ensure_sufficient_stack;
use rustc_data_structures::{outline, sharded, sync};
use rustc_errors::{Diag, FatalError, StashKey};
use rustc_middle::dep_graph::DepsType;
use rustc_middle::dep_graph::{DepGraphData, DepNodeKey, DepsType, HasDepContext};
use rustc_middle::query::{
ActiveKeyStatus, CycleError, CycleErrorHandling, QueryCache, QueryJob, QueryJobId, QueryLatch,
QueryMode, QueryStackDeferred, QueryStackFrame, QueryState,
};
use rustc_middle::ty::TyCtxt;
use rustc_query_system::dep_graph::{DepGraphData, DepNodeKey, HasDepContext};
use rustc_query_system::query::incremental_verify_ich;
use rustc_middle::verify_ich::incremental_verify_ich;
use rustc_span::{DUMMY_SP, Span};
use crate::dep_graph::{DepContext, DepNode, DepNodeIndex};

View file

@ -12,7 +12,7 @@
use std::marker::ConstParamTy;
use rustc_data_structures::sync::AtomicU64;
use rustc_middle::dep_graph::{self, DepKind, DepNode, DepNodeIndex};
use rustc_middle::dep_graph::{self, DepKind, DepNode, DepNodeIndex, SerializedDepNodeIndex};
use rustc_middle::queries::{
self, ExternProviders, Providers, QueryCaches, QueryEngine, QueryStates,
};
@ -22,7 +22,6 @@ use rustc_middle::query::plumbing::{
};
use rustc_middle::query::{AsLocalKey, CycleError, CycleErrorHandling, QueryCache, QueryMode};
use rustc_middle::ty::TyCtxt;
use rustc_query_system::dep_graph::SerializedDepNodeIndex;
use rustc_span::{ErrorGuaranteed, Span};
pub use crate::dep_kind_vtables::make_dep_kind_vtables;

View file

@ -16,21 +16,22 @@ use rustc_middle::bug;
#[expect(unused_imports, reason = "used by doc comments")]
use rustc_middle::dep_graph::DepKindVTable;
use rustc_middle::dep_graph::{
self, DepContext, DepNode, DepNodeIndex, DepsType, SerializedDepNodeIndex, dep_kinds,
self, DepContext, DepNode, DepNodeIndex, DepNodeKey, DepsType, HasDepContext,
SerializedDepNodeIndex, dep_kinds,
};
use rustc_middle::query::on_disk_cache::{
AbsoluteBytePos, CacheDecoder, CacheEncoder, EncodedDepNodeIndex,
};
use rustc_middle::query::plumbing::QueryVTable;
use rustc_middle::query::{
Key, QueryCache, QueryJobId, QueryStackDeferred, QueryStackFrame, QueryStackFrameExtra,
Key, QueryCache, QueryContext, QueryJobId, QueryStackDeferred, QueryStackFrame,
QueryStackFrameExtra,
};
use rustc_middle::ty::codec::TyEncoder;
use rustc_middle::ty::print::with_reduced_queries;
use rustc_middle::ty::tls::{self, ImplicitCtxt};
use rustc_middle::ty::{self, TyCtxt};
use rustc_query_system::dep_graph::{DepNodeKey, HasDepContext};
use rustc_query_system::query::{QueryContext, QuerySideEffect};
use rustc_query_system::query::QuerySideEffect;
use rustc_serialize::{Decodable, Encodable};
use rustc_span::def_id::LOCAL_CRATE;
@ -275,8 +276,8 @@ macro_rules! feedable {
macro_rules! hash_result {
([][$V:ty]) => {{
Some(|hcx, result| {
let result = ::rustc_middle::query::erase::restore_val::<$V>(*result);
::rustc_query_system::dep_graph::hash_result(hcx, &result)
let result = rustc_middle::query::erase::restore_val::<$V>(*result);
rustc_middle::dep_graph::hash_result(hcx, &result)
})
}};
([(no_hash) $($rest:tt)*][$V:ty]) => {{

View file

@ -11,11 +11,9 @@ rustc_data_structures = { path = "../rustc_data_structures" }
rustc_errors = { path = "../rustc_errors" }
rustc_feature = { path = "../rustc_feature" }
rustc_hir = { path = "../rustc_hir" }
rustc_index = { path = "../rustc_index" }
rustc_macros = { path = "../rustc_macros" }
rustc_serialize = { path = "../rustc_serialize" }
rustc_session = { path = "../rustc_session" }
rustc_span = { path = "../rustc_span" }
smallvec = { version = "1.8.1", features = ["union", "may_dangle"] }
tracing = "0.1"
# tidy-alphabetical-end

View file

@ -1,340 +0,0 @@
//! This module defines the [`DepNode`] type which the compiler uses to represent
//! nodes in the [dependency graph]. A `DepNode` consists of a [`DepKind`] (which
//! specifies the kind of thing it represents, like a piece of HIR, MIR, etc.)
//! and a [`Fingerprint`], a 128-bit hash value, the exact meaning of which
//! depends on the node's `DepKind`. Together, the kind and the fingerprint
//! fully identify a dependency node, even across multiple compilation sessions.
//! In other words, the value of the fingerprint does not depend on anything
//! that is specific to a given compilation session, like an unpredictable
//! interning key (e.g., `NodeId`, `DefId`, `Symbol`) or the numeric value of a
//! pointer. The concept behind this could be compared to how git commit hashes
//! uniquely identify a given commit. The fingerprinting approach has
//! a few advantages:
//!
//! * A `DepNode` can simply be serialized to disk and loaded in another session
//! without the need to do any "rebasing" (like we have to do for Spans and
//! NodeIds) or "retracing" (like we had to do for `DefId` in earlier
//! implementations of the dependency graph).
//! * A `Fingerprint` is just a bunch of bits, which allows `DepNode` to
//! implement `Copy`, `Sync`, `Send`, `Freeze`, etc.
//! * Since we just have a bit pattern, `DepNode` can be mapped from disk into
//! memory without any post-processing (e.g., "abomination-style" pointer
//! reconstruction).
//! * Because a `DepNode` is self-contained, we can instantiate `DepNodes` that
//! refer to things that do not exist anymore. In previous implementations
//! `DepNode` contained a `DefId`. A `DepNode` referring to something that
//! had been removed between the previous and the current compilation session
//! could not be instantiated because the current compilation session
//! contained no `DefId` for thing that had been removed.
//!
//! `DepNode` definition happens in `rustc_middle` with the
//! `define_dep_nodes!()` macro. This macro defines the `DepKind` enum. Each
//! `DepKind` has its own parameters that are needed at runtime in order to
//! construct a valid `DepNode` fingerprint. However, only `CompileCodegenUnit`
//! and `CompileMonoItem` are constructed explicitly (with
//! `make_compile_codegen_unit` and `make_compile_mono_item`).
//!
//! Because the macro sees what parameters a given `DepKind` requires, it can
//! "infer" some properties for each kind of `DepNode`:
//!
//! * Whether a `DepNode` of a given kind has any parameters at all. Some
//! `DepNode`s could represent global concepts with only one value.
//! * Whether it is possible, in principle, to reconstruct a query key from a
//! given `DepNode`. Many `DepKind`s only require a single `DefId` parameter,
//! in which case it is possible to map the node's fingerprint back to the
//! `DefId` it was computed from. In other cases, too much information gets
//! lost during fingerprint computation.
//!
//! `make_compile_codegen_unit` and `make_compile_mono_items`, together with
//! `DepNode::new()`, ensure that only valid `DepNode` instances can be
//! constructed. For example, the API does not allow for constructing
//! parameterless `DepNode`s with anything other than a zeroed out fingerprint.
//! More generally speaking, it relieves the user of the `DepNode` API of
//! having to know how to compute the expected fingerprint for a given set of
//! node parameters.
//!
//! [dependency graph]: https://rustc-dev-guide.rust-lang.org/query.html
use std::fmt;
use std::hash::Hash;
use rustc_data_structures::AtomicRef;
use rustc_data_structures::fingerprint::{Fingerprint, PackedFingerprint};
use rustc_data_structures::stable_hasher::{HashStable, StableHasher, StableOrd, ToStableHashKey};
use rustc_hir::definitions::DefPathHash;
use rustc_macros::{Decodable, Encodable};
use super::{DepContext, FingerprintStyle, SerializedDepNodeIndex};
use crate::ich::StableHashingContext;
/// This serves as an index into arrays built by `make_dep_kind_array`.
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct DepKind {
variant: u16,
}
impl DepKind {
#[inline]
pub const fn new(variant: u16) -> Self {
Self { variant }
}
#[inline]
pub const fn as_inner(&self) -> u16 {
self.variant
}
#[inline]
pub const fn as_usize(&self) -> usize {
self.variant as usize
}
}
pub fn default_dep_kind_debug(kind: DepKind, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("DepKind").field("variant", &kind.variant).finish()
}
pub static DEP_KIND_DEBUG: AtomicRef<fn(DepKind, &mut fmt::Formatter<'_>) -> fmt::Result> =
AtomicRef::new(&(default_dep_kind_debug as fn(_, &mut fmt::Formatter<'_>) -> _));
impl fmt::Debug for DepKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
(*DEP_KIND_DEBUG)(*self, f)
}
}
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct DepNode {
pub kind: DepKind,
pub hash: PackedFingerprint,
}
impl DepNode {
/// Creates a new, parameterless DepNode. This method will assert
/// that the DepNode corresponding to the given DepKind actually
/// does not require any parameters.
pub fn new_no_params<Tcx>(tcx: Tcx, kind: DepKind) -> DepNode
where
Tcx: super::DepContext,
{
debug_assert_eq!(tcx.fingerprint_style(kind), FingerprintStyle::Unit);
DepNode { kind, hash: Fingerprint::ZERO.into() }
}
pub fn construct<Tcx, Key>(tcx: Tcx, kind: DepKind, arg: &Key) -> DepNode
where
Tcx: super::DepContext,
Key: DepNodeKey<Tcx>,
{
let hash = arg.to_fingerprint(tcx);
let dep_node = DepNode { kind, hash: hash.into() };
#[cfg(debug_assertions)]
{
if !tcx.fingerprint_style(kind).reconstructible()
&& (tcx.sess().opts.unstable_opts.incremental_info
|| tcx.sess().opts.unstable_opts.query_dep_graph)
{
tcx.dep_graph().register_dep_node_debug_str(dep_node, || arg.to_debug_str(tcx));
}
}
dep_node
}
/// Construct a DepNode from the given DepKind and DefPathHash. This
/// method will assert that the given DepKind actually requires a
/// single DefId/DefPathHash parameter.
pub fn from_def_path_hash<Tcx>(tcx: Tcx, def_path_hash: DefPathHash, kind: DepKind) -> Self
where
Tcx: super::DepContext,
{
debug_assert!(tcx.fingerprint_style(kind) == FingerprintStyle::DefPathHash);
DepNode { kind, hash: def_path_hash.0.into() }
}
}
pub fn default_dep_node_debug(node: DepNode, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("DepNode").field("kind", &node.kind).field("hash", &node.hash).finish()
}
pub static DEP_NODE_DEBUG: AtomicRef<fn(DepNode, &mut fmt::Formatter<'_>) -> fmt::Result> =
AtomicRef::new(&(default_dep_node_debug as fn(_, &mut fmt::Formatter<'_>) -> _));
impl fmt::Debug for DepNode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
(*DEP_NODE_DEBUG)(*self, f)
}
}
/// Trait for query keys as seen by dependency-node tracking.
pub trait DepNodeKey<Tcx: DepContext>: fmt::Debug + Sized {
fn fingerprint_style() -> FingerprintStyle;
/// This method turns a query key into an opaque `Fingerprint` to be used
/// in `DepNode`.
fn to_fingerprint(&self, _: Tcx) -> Fingerprint;
fn to_debug_str(&self, tcx: Tcx) -> String;
/// This method tries to recover the query key from the given `DepNode`,
/// something which is needed when forcing `DepNode`s during red-green
/// evaluation. The query system will only call this method if
/// `fingerprint_style()` is not `FingerprintStyle::Opaque`.
/// It is always valid to return `None` here, in which case incremental
/// compilation will treat the query as having changed instead of forcing it.
fn recover(tcx: Tcx, dep_node: &DepNode) -> Option<Self>;
}
// Blanket impl of `DepNodeKey`, which is specialized by other impls elsewhere.
impl<Tcx: DepContext, T> DepNodeKey<Tcx> for T
where
T: for<'a> HashStable<StableHashingContext<'a>> + fmt::Debug,
{
#[inline(always)]
default fn fingerprint_style() -> FingerprintStyle {
FingerprintStyle::Opaque
}
#[inline(always)]
default fn to_fingerprint(&self, tcx: Tcx) -> Fingerprint {
tcx.with_stable_hashing_context(|mut hcx| {
let mut hasher = StableHasher::new();
self.hash_stable(&mut hcx, &mut hasher);
hasher.finish()
})
}
#[inline(always)]
default fn to_debug_str(&self, tcx: Tcx) -> String {
// Make sure to print dep node params with reduced queries since printing
// may themselves call queries, which may lead to (possibly untracked!)
// query cycles.
tcx.with_reduced_queries(|| format!("{self:?}"))
}
#[inline(always)]
default fn recover(_: Tcx, _: &DepNode) -> Option<Self> {
None
}
}
/// 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 DepKindVTable<Tcx: DepContext> {
/// 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.
pub is_anon: bool,
/// Eval-always queries do not track their dependencies, and are always recomputed, even if
/// their inputs have not changed since the last compiler invocation. The result is still
/// cached within one compiler invocation.
pub is_eval_always: bool,
/// Indicates whether and how the query key can be recovered from its hashed fingerprint.
///
/// The [`DepNodeKey`] trait determines the fingerprint style for each key type.
pub fingerprint_style: FingerprintStyle,
/// The red/green evaluation system will try to mark a specific DepNode in the
/// dependency graph as green by recursively trying to mark the dependencies of
/// that `DepNode` as green. While doing so, it will sometimes encounter a `DepNode`
/// where we don't know if it is red or green and we therefore actually have
/// to recompute its value in order to find out. Since the only piece of
/// information that we have at that point is the `DepNode` we are trying to
/// re-evaluate, we need some way to re-run a query from just that. This is what
/// `force_from_dep_node()` implements.
///
/// In the general case, a `DepNode` consists of a `DepKind` and an opaque
/// GUID/fingerprint that will uniquely identify the node. This GUID/fingerprint
/// is usually constructed by computing a stable hash of the query-key that the
/// `DepNode` corresponds to. Consequently, it is not in general possible to go
/// back from hash to query-key (since hash functions are not reversible). For
/// this reason `force_from_dep_node()` is expected to fail from time to time
/// because we just cannot find out, from the `DepNode` alone, what the
/// corresponding query-key is and therefore cannot re-run the query.
///
/// The system deals with this case letting `try_mark_green` fail which forces
/// the root query to be re-evaluated.
///
/// Now, if `force_from_dep_node()` would always fail, it would be pretty useless.
/// Fortunately, we can use some contextual information that will allow us to
/// reconstruct query-keys for certain kinds of `DepNode`s. In particular, we
/// enforce by construction that the GUID/fingerprint of certain `DepNode`s is a
/// valid `DefPathHash`. Since we also always build a huge table that maps every
/// `DefPathHash` in the current codebase to the corresponding `DefId`, we have
/// everything we need to re-run the query.
///
/// Take the `mir_promoted` query as an example. Like many other queries, it
/// just has a single parameter: the `DefId` of the item it will compute the
/// validated MIR for. Now, when we call `force_from_dep_node()` on a `DepNode`
/// with kind `MirValidated`, we know that the GUID/fingerprint of the `DepNode`
/// is actually a `DefPathHash`, and can therefore just look up the corresponding
/// `DefId` in `tcx.def_path_hash_to_def_id`.
pub force_from_dep_node:
Option<fn(tcx: Tcx, dep_node: DepNode, prev_index: SerializedDepNodeIndex) -> bool>,
/// Invoke a query to put the on-disk cached value in memory.
pub try_load_from_on_disk_cache: Option<fn(Tcx, DepNode)>,
/// The name of this dep kind.
pub name: &'static &'static str,
}
/// A "work product" corresponds to a `.o` (or other) file that we
/// save in between runs. These IDs do not have a `DefId` but rather
/// some independent path or string that persists between runs without
/// the need to be mapped or unmapped. (This ensures we can serialize
/// them even in the absence of a tcx.)
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Encodable, Decodable)]
pub struct WorkProductId {
hash: Fingerprint,
}
impl WorkProductId {
pub fn from_cgu_name(cgu_name: &str) -> WorkProductId {
let mut hasher = StableHasher::new();
cgu_name.hash(&mut hasher);
WorkProductId { hash: hasher.finish() }
}
}
impl<HCX> HashStable<HCX> for WorkProductId {
#[inline]
fn hash_stable(&self, hcx: &mut HCX, hasher: &mut StableHasher) {
self.hash.hash_stable(hcx, hasher)
}
}
impl<HCX> ToStableHashKey<HCX> for WorkProductId {
type KeyType = Fingerprint;
#[inline]
fn to_stable_hash_key(&self, _: &HCX) -> Self::KeyType {
self.hash
}
}
impl StableOrd for WorkProductId {
// Fingerprint can use unstable (just a tuple of `u64`s), so WorkProductId can as well
const CAN_USE_UNSTABLE_SORT: bool = true;
// `WorkProductId` sort order is not affected by (de)serialization.
const THIS_IMPLEMENTATION_HAS_BEEN_TRIPLE_CHECKED: () = ();
}
// Some types are used a lot. Make sure they don't unintentionally get bigger.
#[cfg(target_pointer_width = "64")]
mod size_asserts {
use rustc_data_structures::static_assert_size;
use super::*;
// tidy-alphabetical-start
static_assert_size!(DepKind, 2);
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
static_assert_size!(DepNode, 18);
#[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
static_assert_size!(DepNode, 24);
// tidy-alphabetical-end
}

View file

@ -1,173 +0,0 @@
pub mod debug;
pub mod dep_node;
mod edges;
mod graph;
mod query;
mod serialized;
use std::panic;
pub use dep_node::{DepKind, DepKindVTable, DepNode, DepNodeKey, WorkProductId};
pub use graph::{
DepGraph, DepGraphData, DepNodeIndex, TaskDepsRef, WorkProduct, WorkProductMap, hash_result,
};
pub use query::DepGraphQuery;
use rustc_data_structures::profiling::SelfProfilerRef;
use rustc_data_structures::sync::DynSync;
use rustc_session::Session;
pub use serialized::{SerializedDepGraph, SerializedDepNodeIndex};
use tracing::instrument;
use self::graph::{MarkFrame, print_markframe_trace};
use crate::ich::StableHashingContext;
pub trait DepContext: Copy {
type Deps: Deps;
/// Create a hashing context for hashing new results.
fn with_stable_hashing_context<R>(self, f: impl FnOnce(StableHashingContext<'_>) -> R) -> R;
/// Access the DepGraph.
fn dep_graph(&self) -> &DepGraph<Self::Deps>;
/// Access the profiler.
fn profiler(&self) -> &SelfProfilerRef;
/// Access the compiler session.
fn sess(&self) -> &Session;
fn dep_kind_vtable(&self, dep_node: DepKind) -> &DepKindVTable<Self>;
#[inline(always)]
fn fingerprint_style(self, kind: DepKind) -> FingerprintStyle {
self.dep_kind_vtable(kind).fingerprint_style
}
#[inline(always)]
/// Return whether this kind always require evaluation.
fn is_eval_always(self, kind: DepKind) -> bool {
self.dep_kind_vtable(kind).is_eval_always
}
/// Try to force a dep node to execute and see if it's green.
///
/// Returns true if the query has actually been forced. It is valid that a query
/// fails to be forced, e.g. when the query key cannot be reconstructed from the
/// dep-node or when the query kind outright does not support it.
#[inline]
#[instrument(skip(self, frame), level = "debug")]
fn try_force_from_dep_node(
self,
dep_node: DepNode,
prev_index: SerializedDepNodeIndex,
frame: &MarkFrame<'_>,
) -> bool {
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
}
}
/// Load data from the on-disk cache.
fn try_load_from_on_disk_cache(self, dep_node: &DepNode) {
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)
}
}
fn with_reduced_queries<T>(self, _: impl FnOnce() -> T) -> T;
}
pub trait Deps: DynSync {
/// Execute the operation with provided dependencies.
fn with_deps<OP, R>(deps: TaskDepsRef<'_>, op: OP) -> R
where
OP: FnOnce() -> R;
/// Access dependencies from current implicit context.
fn read_deps<OP>(op: OP)
where
OP: for<'a> FnOnce(TaskDepsRef<'a>);
fn name(dep_kind: DepKind) -> &'static str;
/// We use this for most things when incr. comp. is turned off.
const DEP_KIND_NULL: DepKind;
/// We use this to create a forever-red node.
const DEP_KIND_RED: DepKind;
/// We use this to create a side effect node.
const DEP_KIND_SIDE_EFFECT: DepKind;
/// We use this to create the anon node with zero dependencies.
const DEP_KIND_ANON_ZERO_DEPS: DepKind;
/// This is the highest value a `DepKind` can have. It's used during encoding to
/// pack information into the unused bits.
const DEP_KIND_MAX: u16;
}
pub trait HasDepContext: Copy {
type Deps: self::Deps;
type DepContext: self::DepContext<Deps = Self::Deps>;
fn dep_context(&self) -> &Self::DepContext;
}
impl<T: DepContext> HasDepContext for T {
type Deps = T::Deps;
type DepContext = Self;
fn dep_context(&self) -> &Self::DepContext {
self
}
}
impl<T: HasDepContext, Q: Copy> HasDepContext for (T, Q) {
type Deps = T::Deps;
type DepContext = T::DepContext;
fn dep_context(&self) -> &Self::DepContext {
self.0.dep_context()
}
}
/// Describes the contents of the fingerprint generated by a given query.
///
/// This is mainly for determining whether and how we can reconstruct a key
/// from the fingerprint.
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
pub enum FingerprintStyle {
/// The fingerprint is actually a DefPathHash.
DefPathHash,
/// The fingerprint is actually a HirId.
HirId,
/// Query key was `()` or equivalent, so fingerprint is just zero.
Unit,
/// The fingerprint is an opaque hash, and a key cannot be reconstructed from it.
Opaque,
}
impl FingerprintStyle {
#[inline]
pub const fn reconstructible(self) -> bool {
match self {
FingerprintStyle::DefPathHash | FingerprintStyle::Unit | FingerprintStyle::HirId => {
true
}
FingerprintStyle::Opaque => false,
}
}
}

View file

@ -1,18 +0,0 @@
use rustc_macros::Diagnostic;
#[derive(Diagnostic)]
#[diag("internal compiler error: reentrant incremental verify failure, suppressing message")]
pub(crate) struct Reentrant;
#[derive(Diagnostic)]
#[diag("internal compiler error: encountered incremental compilation error with {$dep_node}")]
#[note("please follow the instructions below to create a bug report with the provided information")]
#[note("for incremental compilation bugs, having a reproduction is vital")]
#[note(
"an ideal reproduction consists of the code before and some patch that then triggers the bug when applied and compiled again"
)]
#[note("as a workaround, you can run {$run_cmd} to allow your project to compile")]
pub(crate) struct IncrementCompilation {
pub run_cmd: String,
pub dep_node: String,
}

View file

@ -4,7 +4,5 @@
#![feature(min_specialization)]
// tidy-alphabetical-end
pub mod dep_graph;
mod error;
pub mod ich;
pub mod query;

View file

@ -1,14 +1,8 @@
use std::fmt::Debug;
use rustc_data_structures::jobserver::Proxy;
use rustc_errors::DiagInner;
use rustc_macros::{Decodable, Encodable};
pub use self::plumbing::*;
use crate::dep_graph::{DepNodeIndex, HasDepContext, SerializedDepNodeIndex};
mod plumbing;
/// Tracks 'side effects' for a particular query.
/// This struct is saved to disk along with the query result,
/// and loaded from disk if we mark the query as green.
@ -26,18 +20,3 @@ pub enum QuerySideEffect {
/// effect dep node as a dependency.
Diagnostic(DiagInner),
}
pub trait QueryContext<'tcx>: HasDepContext {
/// Gets a jobserver reference which is used to release then acquire
/// a token while waiting on a query.
fn jobserver_proxy(&self) -> &Proxy;
/// Load a side effect associated to the node in the previous session.
fn load_side_effect(
self,
prev_dep_node_index: SerializedDepNodeIndex,
) -> Option<QuerySideEffect>;
/// Register a side effect for the given node, for use in next session.
fn store_side_effect(self, dep_node_index: DepNodeIndex, side_effect: QuerySideEffect);
}

View file

@ -1556,6 +1556,7 @@ dep-bumps = [
"/compiler/rustc_llvm" = ["@cuviper"]
"/compiler/rustc_codegen_llvm/src/debuginfo" = ["compiler", "debuginfo"]
"/compiler/rustc_codegen_ssa" = ["compiler", "codegen"]
"/compiler/rustc_middle/src/dep_graph" = ["compiler", "incremental", "query-system"]
"/compiler/rustc_middle/src/mir" = ["compiler", "mir"]
"/compiler/rustc_middle/src/traits" = ["compiler", "types"]
"/compiler/rustc_middle/src/ty" = ["compiler", "types"]
@ -1567,7 +1568,6 @@ dep-bumps = [
"/compiler/rustc_parse/src/lexer" = ["compiler", "lexer"]
"/compiler/rustc_query_impl" = ["compiler", "query-system"]
"/compiler/rustc_query_system" = ["compiler", "query-system"]
"/compiler/rustc_query_system/src/dep_graph" = ["compiler", "incremental", "query-system"]
"/compiler/rustc_query_system/src/ich" = ["compiler", "incremental", "query-system"]
"/compiler/rustc_trait_selection" = ["compiler", "types"]
"/compiler/rustc_traits" = ["compiler", "types"]