Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 2 additions & 54 deletions compiler/rustc_middle/src/dep_graph/dep_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@ use std::fmt;
use std::hash::Hash;

use rustc_data_structures::fingerprint::{Fingerprint, PackedFingerprint};
use rustc_data_structures::stable_hasher::{HashStable, StableHasher, StableOrd, ToStableHashKey};
use rustc_data_structures::stable_hasher::{StableHasher, StableOrd, ToStableHashKey};
use rustc_hir::def_id::DefId;
use rustc_hir::definitions::DefPathHash;
use rustc_macros::{Decodable, Encodable, HashStable};
use rustc_span::Symbol;

use super::{KeyFingerprintStyle, SerializedDepNodeIndex};
use crate::ich::StableHashingContext;
use crate::dep_graph::DepNodeKey;
use crate::mir::mono::MonoItem;
use crate::ty::{TyCtxt, tls};

Expand Down Expand Up @@ -168,58 +168,6 @@ impl fmt::Debug for DepNode {
}
}

/// Trait for query keys as seen by dependency-node tracking.
pub trait DepNodeKey<'tcx>: fmt::Debug + Sized {
fn key_fingerprint_style() -> KeyFingerprintStyle;

/// This method turns a query key into an opaque `Fingerprint` to be used
/// in `DepNode`.
fn to_fingerprint(&self, tcx: TyCtxt<'tcx>) -> Fingerprint;

fn to_debug_str(&self, tcx: TyCtxt<'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 try_recover_key(tcx: TyCtxt<'tcx>, dep_node: &DepNode) -> Option<Self>;
}

// Blanket impl of `DepNodeKey`, which is specialized by other impls elsewhere.
impl<'tcx, T> DepNodeKey<'tcx> for T
where
T: for<'a> HashStable<StableHashingContext<'a>> + fmt::Debug,
{
#[inline(always)]
default fn key_fingerprint_style() -> KeyFingerprintStyle {
KeyFingerprintStyle::Opaque
}

#[inline(always)]
default fn to_fingerprint(&self, tcx: TyCtxt<'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: TyCtxt<'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 try_recover_key(_: TyCtxt<'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
Expand Down
58 changes: 57 additions & 1 deletion compiler/rustc_middle/src/dep_graph/dep_node_key.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,67 @@
use std::fmt::Debug;

use rustc_data_structures::fingerprint::Fingerprint;
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
use rustc_hir::def_id::{CrateNum, DefId, LOCAL_CRATE, LocalDefId, LocalModDefId, ModDefId};
use rustc_hir::definitions::DefPathHash;
use rustc_hir::{HirId, ItemLocalId, OwnerId};

use crate::dep_graph::{DepNode, DepNodeKey, KeyFingerprintStyle};
use crate::dep_graph::{DepNode, KeyFingerprintStyle};
use crate::ich::StableHashingContext;
use crate::ty::TyCtxt;

/// Trait for query keys as seen by dependency-node tracking.
pub trait DepNodeKey<'tcx>: Debug + Sized {
fn key_fingerprint_style() -> KeyFingerprintStyle;

/// This method turns a query key into an opaque `Fingerprint` to be used
/// in `DepNode`.
fn to_fingerprint(&self, tcx: TyCtxt<'tcx>) -> Fingerprint;

fn to_debug_str(&self, tcx: TyCtxt<'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 try_recover_key(tcx: TyCtxt<'tcx>, dep_node: &DepNode) -> Option<Self>;
}

// Blanket impl of `DepNodeKey`, which is specialized by other impls elsewhere.
impl<'tcx, T> DepNodeKey<'tcx> for T
where
T: for<'a> HashStable<StableHashingContext<'a>> + Debug,
{
#[inline(always)]
default fn key_fingerprint_style() -> KeyFingerprintStyle {
KeyFingerprintStyle::Opaque
}

#[inline(always)]
default fn to_fingerprint(&self, tcx: TyCtxt<'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: TyCtxt<'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 try_recover_key(_: TyCtxt<'tcx>, _: &DepNode) -> Option<Self> {
None
}
}

impl<'tcx> DepNodeKey<'tcx> for () {
#[inline(always)]
fn key_fingerprint_style() -> KeyFingerprintStyle {
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_middle/src/dep_graph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ use std::panic;
use tracing::instrument;

pub use self::dep_node::{
DepKind, DepKindVTable, DepNode, DepNodeKey, WorkProductId, dep_kind_from_label, label_strs,
DepKind, DepKindVTable, DepNode, WorkProductId, dep_kind_from_label, label_strs,
};
pub use self::dep_node_key::DepNodeKey;
pub use self::graph::{
DepGraph, DepGraphData, DepNodeIndex, QuerySideEffect, TaskDepsRef, WorkProduct,
WorkProductMap, hash_result,
Expand Down
14 changes: 4 additions & 10 deletions compiler/rustc_middle/src/query/caches.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,21 @@
use std::fmt::Debug;
use std::hash::Hash;
use std::sync::OnceLock;

use rustc_data_structures::sharded::ShardedHashMap;
use rustc_data_structures::stable_hasher::HashStable;
pub use rustc_data_structures::vec_cache::VecCache;
use rustc_hir::def_id::LOCAL_CRATE;
use rustc_index::Idx;
use rustc_span::def_id::{DefId, DefIndex};

use crate::dep_graph::DepNodeIndex;
use crate::ich::StableHashingContext;

/// Traits that all query keys must satisfy.
pub trait QueryCacheKey = Hash + Eq + Copy + Debug + for<'a> HashStable<StableHashingContext<'a>>;
use crate::query::keys::QueryKey;

/// Trait for types that serve as an in-memory cache for query results,
/// for a given key (argument) type and value (return) type.
///
/// Types implementing this trait are associated with actual key/value types
/// by the `Cache` associated type of the `rustc_middle::query::Key` trait.
pub trait QueryCache: Sized {
type Key: QueryCacheKey;
type Key: QueryKey;
type Value: Copy;

/// Returns the cached value (and other information) associated with the
Expand Down Expand Up @@ -53,7 +47,7 @@ impl<K, V> Default for DefaultCache<K, V> {

impl<K, V> QueryCache for DefaultCache<K, V>
where
K: QueryCacheKey,
K: QueryKey,
V: Copy,
{
type Key = K;
Expand Down Expand Up @@ -180,7 +174,7 @@ where

impl<K, V> QueryCache for VecCache<K, V, DepNodeIndex>
where
K: Idx + QueryCacheKey,
K: Idx + QueryKey,
V: Copy,
{
type Key = K;
Expand Down
Loading
Loading