Skip to content

Commit f3f91bb

Browse files
committed
Move functions on DepKindStruct from rustc_middle to rustc_query_system
1 parent 93a0fb1 commit f3f91bb

File tree

4 files changed

+64
-71
lines changed

4 files changed

+64
-71
lines changed

compiler/rustc_middle/src/dep_graph/dep_node.rs

+3-15
Original file line numberDiff line numberDiff line change
@@ -69,18 +69,6 @@ use std::hash::Hash;
6969

7070
pub use rustc_query_system::dep_graph::{DepContext, DepNodeParams};
7171

72-
impl DepKind {
73-
#[inline(always)]
74-
pub fn fingerprint_style(self, tcx: TyCtxt<'_>) -> FingerprintStyle {
75-
// Only fetch the DepKindStruct once.
76-
let data = tcx.query_kind(self);
77-
if data.is_anon {
78-
return FingerprintStyle::Opaque;
79-
}
80-
data.fingerprint_style
81-
}
82-
}
83-
8472
macro_rules! define_dep_nodes {
8573
(
8674
$($(#[$attr:meta])*
@@ -186,7 +174,7 @@ impl DepNodeExt for DepNode {
186174
/// method will assert that the given DepKind actually requires a
187175
/// single DefId/DefPathHash parameter.
188176
fn from_def_path_hash(tcx: TyCtxt<'_>, def_path_hash: DefPathHash, kind: DepKind) -> DepNode {
189-
debug_assert!(kind.fingerprint_style(tcx) == FingerprintStyle::DefPathHash);
177+
debug_assert!(tcx.fingerprint_style(kind) == FingerprintStyle::DefPathHash);
190178
DepNode { kind, hash: def_path_hash.0.into() }
191179
}
192180

@@ -201,7 +189,7 @@ impl DepNodeExt for DepNode {
201189
/// refers to something from the previous compilation session that
202190
/// has been removed.
203191
fn extract_def_id<'tcx>(&self, tcx: TyCtxt<'tcx>) -> Option<DefId> {
204-
if self.kind.fingerprint_style(tcx) == FingerprintStyle::DefPathHash {
192+
if tcx.fingerprint_style(self.kind) == FingerprintStyle::DefPathHash {
205193
Some(tcx.def_path_hash_to_def_id(DefPathHash(self.hash.into()), &mut || {
206194
panic!("Failed to extract DefId: {:?} {}", self.kind, self.hash)
207195
}))
@@ -218,7 +206,7 @@ impl DepNodeExt for DepNode {
218206
) -> Result<DepNode, ()> {
219207
let kind = dep_kind_from_label_string(label)?;
220208

221-
match kind.fingerprint_style(tcx) {
209+
match tcx.fingerprint_style(kind) {
222210
FingerprintStyle::Opaque => Err(()),
223211
FingerprintStyle::Unit => Ok(DepNode::new_no_params(tcx, kind)),
224212
FingerprintStyle::DefPathHash => {

compiler/rustc_middle/src/dep_graph/mod.rs

+7-45
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ impl rustc_query_system::dep_graph::DepKind for DepKind {
2727
const NULL: Self = DepKind::Null;
2828
const RED: Self = DepKind::Red;
2929

30+
fn is_codegen_unit_query(self) -> bool {
31+
self == DepKind::codegen_unit
32+
}
33+
3034
fn debug_node(node: &DepNode, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
3135
write!(f, "{:?}(", node.kind)?;
3236

@@ -93,50 +97,8 @@ impl<'tcx> DepContext for TyCtxt<'tcx> {
9397
self.sess
9498
}
9599

96-
#[inline(always)]
97-
fn fingerprint_style(&self, kind: DepKind) -> rustc_query_system::dep_graph::FingerprintStyle {
98-
kind.fingerprint_style(*self)
99-
}
100-
101-
#[inline(always)]
102-
fn is_eval_always(&self, kind: DepKind) -> bool {
103-
self.query_kind(kind).is_eval_always
104-
}
105-
106-
fn try_force_from_dep_node(&self, dep_node: DepNode) -> bool {
107-
debug!("try_force_from_dep_node({:?}) --- trying to force", dep_node);
108-
109-
// We must avoid ever having to call `force_from_dep_node()` for a
110-
// `DepNode::codegen_unit`:
111-
// Since we cannot reconstruct the query key of a `DepNode::codegen_unit`, we
112-
// would always end up having to evaluate the first caller of the
113-
// `codegen_unit` query that *is* reconstructible. This might very well be
114-
// the `compile_codegen_unit` query, thus re-codegenning the whole CGU just
115-
// to re-trigger calling the `codegen_unit` query with the right key. At
116-
// that point we would already have re-done all the work we are trying to
117-
// avoid doing in the first place.
118-
// The solution is simple: Just explicitly call the `codegen_unit` query for
119-
// each CGU, right after partitioning. This way `try_mark_green` will always
120-
// hit the cache instead of having to go through `force_from_dep_node`.
121-
// This assertion makes sure, we actually keep applying the solution above.
122-
debug_assert!(
123-
dep_node.kind != DepKind::codegen_unit,
124-
"calling force_from_dep_node() on DepKind::codegen_unit"
125-
);
126-
127-
let cb = self.query_kind(dep_node.kind);
128-
if let Some(f) = cb.force_from_dep_node {
129-
f(*self, dep_node);
130-
true
131-
} else {
132-
false
133-
}
134-
}
135-
136-
fn try_load_from_on_disk_cache(&self, dep_node: DepNode) {
137-
let cb = self.query_kind(dep_node.kind);
138-
if let Some(f) = cb.try_load_from_on_disk_cache {
139-
f(*self, dep_node)
140-
}
100+
#[inline]
101+
fn dep_kind_info(&self, dep_kind: DepKind) -> &DepKindStruct<'tcx> {
102+
&self.query_kinds[dep_kind as usize]
141103
}
142104
}

compiler/rustc_middle/src/ty/context.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Type context book-keeping.
22
33
use crate::arena::Arena;
4-
use crate::dep_graph::{DepGraph, DepKind, DepKindStruct};
4+
use crate::dep_graph::{DepGraph, DepKindStruct};
55
use crate::hir::place::Place as HirPlace;
66
use crate::infer::canonical::{Canonical, CanonicalVarInfo, CanonicalVarInfos};
77
use crate::lint::{struct_lint_level, LintLevelSource};
@@ -1085,7 +1085,7 @@ pub struct GlobalCtxt<'tcx> {
10851085

10861086
pub queries: &'tcx dyn query::QueryEngine<'tcx>,
10871087
pub query_caches: query::QueryCaches<'tcx>,
1088-
query_kinds: &'tcx [DepKindStruct<'tcx>],
1088+
pub(crate) query_kinds: &'tcx [DepKindStruct<'tcx>],
10891089

10901090
// Internal caches for metadata decoding. No need to track deps on this.
10911091
pub ty_rcache: Lock<FxHashMap<ty::CReaderCacheKey, Ty<'tcx>>>,
@@ -1292,10 +1292,6 @@ impl<'tcx> TyCtxt<'tcx> {
12921292
}
12931293
}
12941294

1295-
pub(crate) fn query_kind(self, k: DepKind) -> &'tcx DepKindStruct<'tcx> {
1296-
&self.query_kinds[k as usize]
1297-
}
1298-
12991295
/// Constructs a `TyKind::Error` type and registers a `delay_span_bug` to ensure it gets used.
13001296
#[track_caller]
13011297
pub fn ty_error(self) -> Ty<'tcx> {

compiler/rustc_query_system/src/dep_graph/mod.rs

+52-5
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,61 @@ pub trait DepContext: Copy {
3434
/// Access the compiler session.
3535
fn sess(&self) -> &Session;
3636

37-
/// Return whether this kind always require evaluation.
38-
fn is_eval_always(&self, kind: Self::DepKind) -> bool;
37+
fn dep_kind_info(&self, dep_node: Self::DepKind) -> &DepKindStruct<Self>;
38+
39+
#[inline(always)]
40+
fn fingerprint_style(&self, kind: Self::DepKind) -> FingerprintStyle {
41+
let data = self.dep_kind_info(kind);
42+
if data.is_anon {
43+
return FingerprintStyle::Opaque;
44+
}
45+
data.fingerprint_style
46+
}
3947

40-
fn fingerprint_style(&self, kind: Self::DepKind) -> FingerprintStyle;
48+
#[inline(always)]
49+
/// Return whether this kind always require evaluation.
50+
fn is_eval_always(&self, kind: Self::DepKind) -> bool {
51+
self.dep_kind_info(kind).is_eval_always
52+
}
4153

4254
/// Try to force a dep node to execute and see if it's green.
43-
fn try_force_from_dep_node(&self, dep_node: DepNode<Self::DepKind>) -> bool;
55+
fn try_force_from_dep_node(self, dep_node: DepNode<Self::DepKind>) -> bool {
56+
debug!("try_force_from_dep_node({:?}) --- trying to force", dep_node);
57+
58+
// We must avoid ever having to call `force_from_dep_node()` for a
59+
// `DepNode::codegen_unit`:
60+
// Since we cannot reconstruct the query key of a `DepNode::codegen_unit`, we
61+
// would always end up having to evaluate the first caller of the
62+
// `codegen_unit` query that *is* reconstructible. This might very well be
63+
// the `compile_codegen_unit` query, thus re-codegenning the whole CGU just
64+
// to re-trigger calling the `codegen_unit` query with the right key. At
65+
// that point we would already have re-done all the work we are trying to
66+
// avoid doing in the first place.
67+
// The solution is simple: Just explicitly call the `codegen_unit` query for
68+
// each CGU, right after partitioning. This way `try_mark_green` will always
69+
// hit the cache instead of having to go through `force_from_dep_node`.
70+
// This assertion makes sure, we actually keep applying the solution above.
71+
debug_assert!(
72+
!dep_node.kind.is_codegen_unit_query(),
73+
"calling force_from_dep_node() on DepKind::codegen_unit"
74+
);
75+
76+
let cb = self.dep_kind_info(dep_node.kind);
77+
if let Some(f) = cb.force_from_dep_node {
78+
f(self, dep_node);
79+
true
80+
} else {
81+
false
82+
}
83+
}
4484

4585
/// Load data from the on-disk cache.
46-
fn try_load_from_on_disk_cache(&self, dep_node: DepNode<Self::DepKind>);
86+
fn try_load_from_on_disk_cache(self, dep_node: DepNode<Self::DepKind>) {
87+
let cb = self.dep_kind_info(dep_node.kind);
88+
if let Some(f) = cb.try_load_from_on_disk_cache {
89+
f(self, dep_node)
90+
}
91+
}
4792
}
4893

4994
pub trait HasDepContext: Copy {
@@ -91,6 +136,8 @@ pub trait DepKind: Copy + fmt::Debug + Eq + Hash + Send + Encodable<FileEncoder>
91136
/// DepKind to use to create the initial forever-red node.
92137
const RED: Self;
93138

139+
fn is_codegen_unit_query(self) -> bool;
140+
94141
/// Implementation of `std::fmt::Debug` for `DepNode`.
95142
fn debug_node(node: &DepNode<Self>, f: &mut fmt::Formatter<'_>) -> fmt::Result;
96143

0 commit comments

Comments
 (0)