Skip to content

Commit c989f89

Browse files
committed
Define DepKind in rustc_query_system.
1 parent 353dbdf commit c989f89

File tree

19 files changed

+302
-311
lines changed

19 files changed

+302
-311
lines changed

compiler/rustc_incremental/src/assert_dep_graph.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ use rustc_hir as hir;
4141
use rustc_hir::def_id::DefId;
4242
use rustc_hir::intravisit::{self, Visitor};
4343
use rustc_middle::dep_graph::{
44-
DepGraphQuery, DepKind, DepNode, DepNodeExt, DepNodeFilter, EdgeFilter,
44+
dep_kind, DepGraphQuery, DepNode, DepNodeExt, DepNodeFilter, EdgeFilter,
4545
};
4646
use rustc_middle::hir::nested_filter;
4747
use rustc_middle::ty::TyCtxt;
@@ -128,7 +128,7 @@ impl<'tcx> IfThisChanged<'tcx> {
128128
let dep_node_interned = self.argument(attr);
129129
let dep_node = match dep_node_interned {
130130
None => {
131-
DepNode::from_def_path_hash(self.tcx, def_path_hash, DepKind::hir_owner)
131+
DepNode::from_def_path_hash(self.tcx, def_path_hash, dep_kind::hir_owner)
132132
}
133133
Some(n) => {
134134
match DepNode::from_label_string(self.tcx, n.as_str(), def_path_hash) {

compiler/rustc_interface/src/callbacks.rs

+34
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
//! The functions in this file should fall back to the default set in their
1010
//! origin crate when the `TyCtxt` is not present in TLS.
1111
12+
use rustc_middle::dep_graph::DepNodeExt;
1213
use rustc_middle::ty::tls;
14+
use rustc_query_system::dep_graph::{DepKind, DepNode};
1315
use std::fmt;
1416

1517
fn track_span_parent(def_id: rustc_span::def_id::LocalDefId) {
@@ -35,10 +37,42 @@ fn def_id_debug(def_id: rustc_hir::def_id::DefId, f: &mut fmt::Formatter<'_>) ->
3537
write!(f, ")")
3638
}
3739

40+
fn dep_kind_debug(kind: &DepKind, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
41+
tls::with_opt(|opt_tcx| {
42+
if let Some(tcx) = opt_tcx {
43+
write!(f, "{}", tcx.query_name(*kind))
44+
} else {
45+
write!(f, "DepKind({:?})", kind.index())
46+
}
47+
})
48+
}
49+
50+
fn dep_node_debug(node: &DepNode, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
51+
tls::with_opt(|opt_tcx| {
52+
if let Some(tcx) = opt_tcx {
53+
write!(f, "{}(", tcx.query_name(node.kind))?;
54+
55+
if let Some(def_id) = node.extract_def_id(tcx) {
56+
write!(f, "{}", tcx.def_path_debug_str(def_id))?;
57+
} else if let Some(ref s) = tcx.dep_graph.dep_node_debug_str(*node) {
58+
write!(f, "{}", s)?;
59+
} else {
60+
write!(f, "{}", node.hash)?;
61+
}
62+
63+
write!(f, ")")
64+
} else {
65+
write!(f, "DepKind({:?})({})", node.kind.index(), node.hash)
66+
}
67+
})
68+
}
69+
3870
/// Sets up the callbacks in prior crates which we want to refer to the
3971
/// TyCtxt in.
4072
pub fn setup_callbacks() {
4173
rustc_span::SPAN_TRACK.swap(&(track_span_parent as fn(_)));
4274
rustc_hir::def_id::DEF_ID_DEBUG.swap(&(def_id_debug as fn(_, &mut fmt::Formatter<'_>) -> _));
75+
rustc_query_system::dep_graph::KIND_DEBUG.swap(&(dep_kind_debug as _));
76+
rustc_query_system::dep_graph::NODE_DEBUG.swap(&(dep_node_debug as _));
4377
rustc_errors::TRACK_DIAGNOSTICS.swap(&(rustc_query_system::tls::track_diagnostic as fn(&_)));
4478
}

compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ macro_rules! provide_one {
5050
// External query providers call `crate_hash` in order to register a dependency
5151
// on the crate metadata. The exception is `crate_hash` itself, which obviously
5252
// doesn't need to do this (and can't, as it would cause a query cycle).
53-
use rustc_middle::dep_graph::DepKind;
54-
if DepKind::$name != DepKind::crate_hash && $tcx.dep_graph.is_fully_enabled() {
53+
use rustc_middle::dep_graph::dep_kind;
54+
if dep_kind::$name != dep_kind::crate_hash && $tcx.dep_graph.is_fully_enabled() {
5555
$tcx.ensure().crate_hash($def_id.krate);
5656
}
5757

compiler/rustc_middle/src/dep_graph/dep_node.rs

+33-39
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,8 @@ use rustc_data_structures::fingerprint::Fingerprint;
6363
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, CRATE_DEF_INDEX};
6464
use rustc_hir::definitions::DefPathHash;
6565
use rustc_hir::HirId;
66-
use rustc_query_system::dep_graph::FingerprintStyle;
66+
use rustc_query_system::dep_graph::{DepKind, DepNode, DepNodeParams, FingerprintStyle};
6767
use rustc_span::symbol::Symbol;
68-
use std::hash::Hash;
69-
70-
pub use rustc_query_system::dep_graph::{DepContext, DepNodeParams};
7168

7269
/// This struct stores metadata about each DepKind.
7370
///
@@ -89,6 +86,9 @@ pub struct DepKindStruct {
8986
/// See [DepNodeParams] trait for the behaviour of each key type.
9087
pub fingerprint_style: FingerprintStyle,
9188

89+
/// Name of the query for pretty-printing.
90+
pub label: &'static str,
91+
9292
/// The red/green evaluation system will try to mark a specific DepNode in the
9393
/// dependency graph as green by recursively trying to mark the dependencies of
9494
/// that `DepNode` as green. While doing so, it will sometimes encounter a `DepNode`
@@ -130,16 +130,21 @@ pub struct DepKindStruct {
130130
pub try_load_from_on_disk_cache: Option<fn(TyCtxt<'_>, DepNode)>,
131131
}
132132

133-
impl DepKind {
133+
impl TyCtxt<'_> {
134134
#[inline(always)]
135-
pub fn fingerprint_style(self, tcx: TyCtxt<'_>) -> FingerprintStyle {
135+
crate fn query_fingerprint_style(self, dep_kind: DepKind) -> FingerprintStyle {
136136
// Only fetch the DepKindStruct once.
137-
let data = tcx.query_kind(self);
137+
let data = self.query_kind(dep_kind);
138138
if data.is_anon {
139139
return FingerprintStyle::Opaque;
140140
}
141141
data.fingerprint_style
142142
}
143+
144+
#[inline(always)]
145+
pub fn query_name(self, dep_kind: DepKind) -> &'static str {
146+
self.query_kind(dep_kind).label
147+
}
143148
}
144149

145150
macro_rules! define_dep_nodes {
@@ -151,38 +156,40 @@ macro_rules! define_dep_nodes {
151156
) => (
152157
#[macro_export]
153158
macro_rules! make_dep_kind_array {
154-
($mod:ident) => {[ $($mod::$variant()),* ]};
159+
($mod:ident) => {[ $mod::NULL(), $($mod::$variant()),* ]};
155160
}
156161

157-
/// This enum serves as an index into arrays built by `make_dep_kind_array`.
158-
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Encodable, Decodable)]
159-
#[allow(non_camel_case_types)]
160-
pub enum DepKind {
161-
$($variant),*
162+
/// Definition of the `DepKind`s for the known queries.
163+
/// The constants are indices in the arrays produced by `make_dep_kind_array`,
164+
/// and should be kept in sync.
165+
#[allow(non_upper_case_globals)]
166+
pub mod dep_kind {
167+
use super::DepKind;
168+
169+
/// We use this for most things when incr. comp. is turned off.
170+
pub const NULL: DepKind = DepKind::NULL;
171+
$(pub const $variant: DepKind = DepKind::new(1 + ${index()});)*
162172
}
163173

164174
fn dep_kind_from_label_string(label: &str) -> Result<DepKind, ()> {
165175
match label {
166-
$(stringify!($variant) => Ok(DepKind::$variant),)*
176+
"NULL" => Ok(dep_kind::NULL),
177+
$(stringify!($variant) => Ok(dep_kind::$variant),)*
167178
_ => Err(()),
168179
}
169180
}
170181

171182
/// Contains variant => str representations for constructing
172183
/// DepNode groups for tests.
173-
#[allow(dead_code, non_upper_case_globals)]
184+
#[allow(non_upper_case_globals)]
174185
pub mod label_strs {
175-
$(
176-
pub const $variant: &str = stringify!($variant);
177-
)*
186+
pub const NULL: &str = "NULL";
187+
$(pub const $variant: &str = stringify!($variant);)*
178188
}
179189
);
180190
}
181191

182192
rustc_dep_node_append!([define_dep_nodes!][ <'tcx>
183-
// We use this for most things when incr. comp. is turned off.
184-
[] Null,
185-
186193
[anon] TraitSelect,
187194

188195
// WARNING: if `Symbol` is changed, make sure you update `make_compile_codegen_unit` below.
@@ -196,28 +203,15 @@ rustc_dep_node_append!([define_dep_nodes!][ <'tcx>
196203
// WARNING: `construct` is generic and does not know that `CompileCodegenUnit` takes `Symbol`s as keys.
197204
// Be very careful changing this type signature!
198205
crate fn make_compile_codegen_unit(tcx: TyCtxt<'_>, name: Symbol) -> DepNode {
199-
DepNode::construct(tcx, DepKind::CompileCodegenUnit, &name)
206+
DepNode::construct(tcx, dep_kind::CompileCodegenUnit, &name)
200207
}
201208

202209
// WARNING: `construct` is generic and does not know that `CompileMonoItem` takes `MonoItem`s as keys.
203210
// Be very careful changing this type signature!
204211
crate fn make_compile_mono_item<'tcx>(tcx: TyCtxt<'tcx>, mono_item: &MonoItem<'tcx>) -> DepNode {
205-
DepNode::construct(tcx, DepKind::CompileMonoItem, mono_item)
212+
DepNode::construct(tcx, dep_kind::CompileMonoItem, mono_item)
206213
}
207214

208-
pub type DepNode = rustc_query_system::dep_graph::DepNode<DepKind>;
209-
210-
// We keep a lot of `DepNode`s in memory during compilation. It's not
211-
// required that their size stay the same, but we don't want to change
212-
// it inadvertently. This assert just ensures we're aware of any change.
213-
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
214-
static_assert_size!(DepNode, 18);
215-
216-
#[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
217-
static_assert_size!(DepNode, 24);
218-
219-
static_assert_size!(DepKind, 2);
220-
221215
pub trait DepNodeExt: Sized {
222216
/// Construct a DepNode from the given DepKind and DefPathHash. This
223217
/// method will assert that the given DepKind actually requires a
@@ -252,7 +246,7 @@ impl DepNodeExt for DepNode {
252246
/// method will assert that the given DepKind actually requires a
253247
/// single DefId/DefPathHash parameter.
254248
fn from_def_path_hash(tcx: TyCtxt<'_>, def_path_hash: DefPathHash, kind: DepKind) -> DepNode {
255-
debug_assert!(kind.fingerprint_style(tcx) == FingerprintStyle::DefPathHash);
249+
debug_assert!(tcx.query_fingerprint_style(kind) == FingerprintStyle::DefPathHash);
256250
DepNode { kind, hash: def_path_hash.0.into() }
257251
}
258252

@@ -267,7 +261,7 @@ impl DepNodeExt for DepNode {
267261
/// refers to something from the previous compilation session that
268262
/// has been removed.
269263
fn extract_def_id<'tcx>(&self, tcx: TyCtxt<'tcx>) -> Option<DefId> {
270-
if self.kind.fingerprint_style(tcx) == FingerprintStyle::DefPathHash {
264+
if tcx.query_fingerprint_style(self.kind) == FingerprintStyle::DefPathHash {
271265
Some(tcx.def_path_hash_to_def_id(DefPathHash(self.hash.into()), &mut || {
272266
panic!("Failed to extract DefId: {:?} {}", self.kind, self.hash)
273267
}))
@@ -284,7 +278,7 @@ impl DepNodeExt for DepNode {
284278
) -> Result<DepNode, ()> {
285279
let kind = dep_kind_from_label_string(label)?;
286280

287-
match kind.fingerprint_style(tcx) {
281+
match tcx.query_fingerprint_style(kind) {
288282
FingerprintStyle::Opaque => Err(()),
289283
FingerprintStyle::Unit => Ok(DepNode::new_no_params(tcx, kind)),
290284
FingerprintStyle::DefPathHash => {

compiler/rustc_middle/src/dep_graph/mod.rs

+8-41
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::ty::{self, TyCtxt};
1+
use crate::ty::TyCtxt;
22
use rustc_data_structures::profiling::SelfProfilerRef;
33
use rustc_query_system::ich::StableHashingContext;
44
use rustc_session::Session;
@@ -7,48 +7,15 @@ use rustc_session::Session;
77
mod dep_node;
88

99
pub use rustc_query_system::dep_graph::{
10-
debug::DepNodeFilter, hash_result, DepContext, DepNodeColor, DepNodeIndex,
11-
SerializedDepNodeIndex, WorkProduct, WorkProductId,
10+
debug::DepNodeFilter, debug::EdgeFilter, hash_result, DepContext, DepGraph, DepGraphQuery,
11+
DepKind, DepNode, DepNodeColor, DepNodeIndex, SerializedDepGraph, SerializedDepNodeIndex,
12+
TaskDeps, WorkProduct, WorkProductId,
1213
};
1314

14-
pub use dep_node::{label_strs, DepKind, DepKindStruct, DepNode, DepNodeExt};
15+
pub use dep_node::{dep_kind, label_strs, DepKindStruct, DepNodeExt};
1516
crate use dep_node::{make_compile_codegen_unit, make_compile_mono_item};
1617

17-
pub type DepGraph = rustc_query_system::dep_graph::DepGraph<DepKind>;
18-
pub type TaskDeps = rustc_query_system::dep_graph::TaskDeps<DepKind>;
19-
pub type TaskDepsRef<'a> = rustc_query_system::dep_graph::TaskDepsRef<'a, DepKind>;
20-
pub type DepGraphQuery = rustc_query_system::dep_graph::DepGraphQuery<DepKind>;
21-
pub type SerializedDepGraph = rustc_query_system::dep_graph::SerializedDepGraph<DepKind>;
22-
pub type EdgeFilter = rustc_query_system::dep_graph::debug::EdgeFilter<DepKind>;
23-
24-
impl rustc_query_system::dep_graph::DepKind for DepKind {
25-
const NULL: Self = DepKind::Null;
26-
27-
fn debug_node(node: &DepNode, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
28-
write!(f, "{:?}(", node.kind)?;
29-
30-
ty::tls::with_opt(|opt_tcx| {
31-
if let Some(tcx) = opt_tcx {
32-
if let Some(def_id) = node.extract_def_id(tcx) {
33-
write!(f, "{}", tcx.def_path_debug_str(def_id))?;
34-
} else if let Some(ref s) = tcx.dep_graph.dep_node_debug_str(*node) {
35-
write!(f, "{}", s)?;
36-
} else {
37-
write!(f, "{}", node.hash)?;
38-
}
39-
} else {
40-
write!(f, "{}", node.hash)?;
41-
}
42-
Ok(())
43-
})?;
44-
45-
write!(f, ")")
46-
}
47-
}
48-
4918
impl<'tcx> DepContext for TyCtxt<'tcx> {
50-
type DepKind = DepKind;
51-
5219
#[inline]
5320
fn create_stable_hashing_context(&self) -> StableHashingContext<'_> {
5421
TyCtxt::create_stable_hashing_context(*self)
@@ -71,7 +38,7 @@ impl<'tcx> DepContext for TyCtxt<'tcx> {
7138

7239
#[inline(always)]
7340
fn fingerprint_style(&self, kind: DepKind) -> rustc_query_system::dep_graph::FingerprintStyle {
74-
kind.fingerprint_style(*self)
41+
self.query_fingerprint_style(kind)
7542
}
7643

7744
#[inline(always)]
@@ -96,8 +63,8 @@ impl<'tcx> DepContext for TyCtxt<'tcx> {
9663
// hit the cache instead of having to go through `force_from_dep_node`.
9764
// This assertion makes sure, we actually keep applying the solution above.
9865
debug_assert!(
99-
dep_node.kind != DepKind::codegen_unit,
100-
"calling force_from_dep_node() on DepKind::codegen_unit"
66+
dep_node.kind != dep_kind::codegen_unit,
67+
"calling force_from_dep_node() on dep_kind::codegen_unit"
10168
);
10269

10370
let cb = self.query_kind(dep_node.kind);

compiler/rustc_middle/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
#![feature(once_cell)]
4444
#![feature(let_chains)]
4545
#![feature(let_else)]
46+
#![feature(macro_metavar_expr)]
4647
#![feature(min_specialization)]
4748
#![feature(trusted_len)]
4849
#![feature(type_alias_impl_trait)]

compiler/rustc_middle/src/ty/context.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1176,7 +1176,7 @@ impl<'tcx> TyCtxt<'tcx> {
11761176
}
11771177

11781178
crate fn query_kind(self, k: DepKind) -> &'tcx DepKindStruct {
1179-
&self.query_kinds[k as usize]
1179+
&self.query_kinds[k.index() as usize]
11801180
}
11811181

11821182
/// Constructs a `TyKind::Error` type and registers a `delay_span_bug` to ensure it gets used.

compiler/rustc_query_impl/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ extern crate rustc_middle;
1717
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
1818
use rustc_data_structures::sync::AtomicU64;
1919
use rustc_middle::arena::Arena;
20-
use rustc_middle::dep_graph::{self, DepKindStruct, SerializedDepNodeIndex};
20+
use rustc_middle::dep_graph::{self, label_strs, DepKindStruct, SerializedDepNodeIndex};
2121
use rustc_middle::ty::query::{query_keys, query_storage, query_stored, query_values};
2222
use rustc_middle::ty::query::{ExternProviders, Providers, QueryEngine};
2323
use rustc_middle::ty::{self, TyCtxt};

0 commit comments

Comments
 (0)