1
Fork 0

Move functions on DepKindStruct from rustc_middle to rustc_query_system

This commit is contained in:
Joshua Nelson 2022-09-11 23:18:08 -05:00
parent 93a0fb190e
commit f3f91bb514
4 changed files with 64 additions and 71 deletions

View file

@ -69,18 +69,6 @@ use std::hash::Hash;
pub use rustc_query_system::dep_graph::{DepContext, DepNodeParams};
impl DepKind {
#[inline(always)]
pub fn fingerprint_style(self, tcx: TyCtxt<'_>) -> FingerprintStyle {
// Only fetch the DepKindStruct once.
let data = tcx.query_kind(self);
if data.is_anon {
return FingerprintStyle::Opaque;
}
data.fingerprint_style
}
}
macro_rules! define_dep_nodes {
(
$($(#[$attr:meta])*
@ -186,7 +174,7 @@ impl DepNodeExt for DepNode {
/// method will assert that the given DepKind actually requires a
/// single DefId/DefPathHash parameter.
fn from_def_path_hash(tcx: TyCtxt<'_>, def_path_hash: DefPathHash, kind: DepKind) -> DepNode {
debug_assert!(kind.fingerprint_style(tcx) == FingerprintStyle::DefPathHash);
debug_assert!(tcx.fingerprint_style(kind) == FingerprintStyle::DefPathHash);
DepNode { kind, hash: def_path_hash.0.into() }
}
@ -201,7 +189,7 @@ impl DepNodeExt for DepNode {
/// refers to something from the previous compilation session that
/// has been removed.
fn extract_def_id<'tcx>(&self, tcx: TyCtxt<'tcx>) -> Option<DefId> {
if self.kind.fingerprint_style(tcx) == FingerprintStyle::DefPathHash {
if tcx.fingerprint_style(self.kind) == FingerprintStyle::DefPathHash {
Some(tcx.def_path_hash_to_def_id(DefPathHash(self.hash.into()), &mut || {
panic!("Failed to extract DefId: {:?} {}", self.kind, self.hash)
}))
@ -218,7 +206,7 @@ impl DepNodeExt for DepNode {
) -> Result<DepNode, ()> {
let kind = dep_kind_from_label_string(label)?;
match kind.fingerprint_style(tcx) {
match tcx.fingerprint_style(kind) {
FingerprintStyle::Opaque => Err(()),
FingerprintStyle::Unit => Ok(DepNode::new_no_params(tcx, kind)),
FingerprintStyle::DefPathHash => {

View file

@ -27,6 +27,10 @@ impl rustc_query_system::dep_graph::DepKind for DepKind {
const NULL: Self = DepKind::Null;
const RED: Self = DepKind::Red;
fn is_codegen_unit_query(self) -> bool {
self == DepKind::codegen_unit
}
fn debug_node(node: &DepNode, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}(", node.kind)?;
@ -93,50 +97,8 @@ impl<'tcx> DepContext for TyCtxt<'tcx> {
self.sess
}
#[inline(always)]
fn fingerprint_style(&self, kind: DepKind) -> rustc_query_system::dep_graph::FingerprintStyle {
kind.fingerprint_style(*self)
}
#[inline(always)]
fn is_eval_always(&self, kind: DepKind) -> bool {
self.query_kind(kind).is_eval_always
}
fn try_force_from_dep_node(&self, dep_node: DepNode) -> bool {
debug!("try_force_from_dep_node({:?}) --- trying to force", dep_node);
// We must avoid ever having to call `force_from_dep_node()` for a
// `DepNode::codegen_unit`:
// Since we cannot reconstruct the query key of a `DepNode::codegen_unit`, we
// would always end up having to evaluate the first caller of the
// `codegen_unit` query that *is* reconstructible. This might very well be
// the `compile_codegen_unit` query, thus re-codegenning the whole CGU just
// to re-trigger calling the `codegen_unit` query with the right key. At
// that point we would already have re-done all the work we are trying to
// avoid doing in the first place.
// The solution is simple: Just explicitly call the `codegen_unit` query for
// each CGU, right after partitioning. This way `try_mark_green` will always
// hit the cache instead of having to go through `force_from_dep_node`.
// This assertion makes sure, we actually keep applying the solution above.
debug_assert!(
dep_node.kind != DepKind::codegen_unit,
"calling force_from_dep_node() on DepKind::codegen_unit"
);
let cb = self.query_kind(dep_node.kind);
if let Some(f) = cb.force_from_dep_node {
f(*self, dep_node);
true
} else {
false
}
}
fn try_load_from_on_disk_cache(&self, dep_node: DepNode) {
let cb = self.query_kind(dep_node.kind);
if let Some(f) = cb.try_load_from_on_disk_cache {
f(*self, dep_node)
}
#[inline]
fn dep_kind_info(&self, dep_kind: DepKind) -> &DepKindStruct<'tcx> {
&self.query_kinds[dep_kind as usize]
}
}

View file

@ -1,7 +1,7 @@
//! Type context book-keeping.
use crate::arena::Arena;
use crate::dep_graph::{DepGraph, DepKind, DepKindStruct};
use crate::dep_graph::{DepGraph, DepKindStruct};
use crate::hir::place::Place as HirPlace;
use crate::infer::canonical::{Canonical, CanonicalVarInfo, CanonicalVarInfos};
use crate::lint::{struct_lint_level, LintLevelSource};
@ -1085,7 +1085,7 @@ pub struct GlobalCtxt<'tcx> {
pub queries: &'tcx dyn query::QueryEngine<'tcx>,
pub query_caches: query::QueryCaches<'tcx>,
query_kinds: &'tcx [DepKindStruct<'tcx>],
pub(crate) query_kinds: &'tcx [DepKindStruct<'tcx>],
// Internal caches for metadata decoding. No need to track deps on this.
pub ty_rcache: Lock<FxHashMap<ty::CReaderCacheKey, Ty<'tcx>>>,
@ -1292,10 +1292,6 @@ impl<'tcx> TyCtxt<'tcx> {
}
}
pub(crate) fn query_kind(self, k: DepKind) -> &'tcx DepKindStruct<'tcx> {
&self.query_kinds[k as usize]
}
/// Constructs a `TyKind::Error` type and registers a `delay_span_bug` to ensure it gets used.
#[track_caller]
pub fn ty_error(self) -> Ty<'tcx> {

View file

@ -34,16 +34,61 @@ pub trait DepContext: Copy {
/// Access the compiler session.
fn sess(&self) -> &Session;
/// Return whether this kind always require evaluation.
fn is_eval_always(&self, kind: Self::DepKind) -> bool;
fn dep_kind_info(&self, dep_node: Self::DepKind) -> &DepKindStruct<Self>;
fn fingerprint_style(&self, kind: Self::DepKind) -> FingerprintStyle;
#[inline(always)]
fn fingerprint_style(&self, kind: Self::DepKind) -> FingerprintStyle {
let data = self.dep_kind_info(kind);
if data.is_anon {
return FingerprintStyle::Opaque;
}
data.fingerprint_style
}
#[inline(always)]
/// Return whether this kind always require evaluation.
fn is_eval_always(&self, kind: Self::DepKind) -> bool {
self.dep_kind_info(kind).is_eval_always
}
/// Try to force a dep node to execute and see if it's green.
fn try_force_from_dep_node(&self, dep_node: DepNode<Self::DepKind>) -> bool;
fn try_force_from_dep_node(self, dep_node: DepNode<Self::DepKind>) -> bool {
debug!("try_force_from_dep_node({:?}) --- trying to force", dep_node);
// We must avoid ever having to call `force_from_dep_node()` for a
// `DepNode::codegen_unit`:
// Since we cannot reconstruct the query key of a `DepNode::codegen_unit`, we
// would always end up having to evaluate the first caller of the
// `codegen_unit` query that *is* reconstructible. This might very well be
// the `compile_codegen_unit` query, thus re-codegenning the whole CGU just
// to re-trigger calling the `codegen_unit` query with the right key. At
// that point we would already have re-done all the work we are trying to
// avoid doing in the first place.
// The solution is simple: Just explicitly call the `codegen_unit` query for
// each CGU, right after partitioning. This way `try_mark_green` will always
// hit the cache instead of having to go through `force_from_dep_node`.
// This assertion makes sure, we actually keep applying the solution above.
debug_assert!(
!dep_node.kind.is_codegen_unit_query(),
"calling force_from_dep_node() on DepKind::codegen_unit"
);
let cb = self.dep_kind_info(dep_node.kind);
if let Some(f) = cb.force_from_dep_node {
f(self, dep_node);
true
} else {
false
}
}
/// Load data from the on-disk cache.
fn try_load_from_on_disk_cache(&self, dep_node: DepNode<Self::DepKind>);
fn try_load_from_on_disk_cache(self, dep_node: DepNode<Self::DepKind>) {
let cb = self.dep_kind_info(dep_node.kind);
if let Some(f) = cb.try_load_from_on_disk_cache {
f(self, dep_node)
}
}
}
pub trait HasDepContext: Copy {
@ -91,6 +136,8 @@ pub trait DepKind: Copy + fmt::Debug + Eq + Hash + Send + Encodable<FileEncoder>
/// DepKind to use to create the initial forever-red node.
const RED: Self;
fn is_codegen_unit_query(self) -> bool;
/// Implementation of `std::fmt::Debug` for `DepNode`.
fn debug_node(node: &DepNode<Self>, f: &mut fmt::Formatter<'_>) -> fmt::Result;