Skip to content

Commit 3607bf8

Browse files
committed
Move DepKind to rustc_query_system.
1 parent c61f433 commit 3607bf8

File tree

17 files changed

+366
-437
lines changed

17 files changed

+366
-437
lines changed

compiler/rustc_interface/src/callbacks.rs

Lines changed: 23 additions & 0 deletions
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::DepNode;
1315
use std::fmt;
1416

1517
/// This is a callback from `rustc_ast` as it cannot access the implicit state
@@ -47,11 +49,32 @@ fn def_id_debug(def_id: rustc_hir::def_id::DefId, f: &mut fmt::Formatter<'_>) ->
4749
write!(f, ")")
4850
}
4951

52+
fn dep_node_debug(node: &DepNode, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
53+
write!(f, "{:?}(", node.kind)?;
54+
55+
tls::with_opt(|opt_tcx| {
56+
if let Some(tcx) = opt_tcx {
57+
if let Some(def_id) = node.extract_def_id(tcx) {
58+
write!(f, "{}", tcx.def_path_debug_str(def_id))
59+
} else if let Some(ref s) = tcx.dep_graph.dep_node_debug_str(*node) {
60+
write!(f, "{}", s)
61+
} else {
62+
write!(f, "{}", node.hash)
63+
}
64+
} else {
65+
write!(f, "{}", node.hash)
66+
}
67+
})?;
68+
69+
write!(f, ")")
70+
}
71+
5072
/// Sets up the callbacks in prior crates which we want to refer to the
5173
/// TyCtxt in.
5274
pub fn setup_callbacks() {
5375
rustc_span::SPAN_DEBUG.swap(&(span_debug as fn(_, &mut fmt::Formatter<'_>) -> _));
5476
rustc_span::SPAN_TRACK.swap(&(track_span_parent as fn(_)));
5577
rustc_hir::def_id::DEF_ID_DEBUG.swap(&(def_id_debug as fn(_, &mut fmt::Formatter<'_>) -> _));
78+
rustc_query_system::dep_graph::NODE_DEBUG.swap(&(dep_node_debug as _));
5679
rustc_errors::TRACK_DIAGNOSTICS.swap(&(rustc_query_system::tls::track_diagnostic as fn(&_)));
5780
}

compiler/rustc_interface/src/passes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -781,7 +781,7 @@ impl<'tcx> QueryContext<'tcx> {
781781
F: FnOnce(TyCtxt<'tcx>) -> R,
782782
{
783783
let icx = ty::tls::ImplicitCtxt::new(self.gcx);
784-
let qcx = rustc_query_system::tls::ImplicitCtxt::<rustc_middle::dep_graph::DepKind>::new();
784+
let qcx = rustc_query_system::tls::ImplicitCtxt::new();
785785
ty::tls::enter_context(&icx, |_| {
786786
rustc_query_system::tls::enter_context(&qcx, |_| f(icx.tcx))
787787
})

compiler/rustc_middle/src/dep_graph/dep_node.rs

Lines changed: 18 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,9 @@ 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::dep_kind_from_label_string;
67+
use rustc_query_system::dep_graph::{DepKind, DepNode, DepNodeParams, FingerprintStyle};
6768
use rustc_span::symbol::Symbol;
68-
use std::hash::Hash;
69-
70-
pub use rustc_query_system::dep_graph::{DepContext, DepNodeParams};
7169

7270
/// This struct stores metadata about each DepKind.
7371
///
@@ -130,69 +128,6 @@ pub struct DepKindStruct {
130128
pub try_load_from_on_disk_cache: Option<fn(TyCtxt<'_>, DepNode)>,
131129
}
132130

133-
impl DepKind {
134-
#[inline(always)]
135-
pub fn fingerprint_style(self, tcx: TyCtxt<'_>) -> FingerprintStyle {
136-
// Only fetch the DepKindStruct once.
137-
let data = tcx.query_kind(self);
138-
if data.is_anon {
139-
return FingerprintStyle::Opaque;
140-
}
141-
data.fingerprint_style
142-
}
143-
}
144-
145-
macro_rules! define_dep_nodes {
146-
(<$tcx:tt>
147-
$(
148-
[$($attrs:tt)*]
149-
$variant:ident $(( $tuple_arg_ty:ty $(,)? ))*
150-
,)*
151-
) => (
152-
#[macro_export]
153-
macro_rules! make_dep_kind_array {
154-
($mod:ident) => {[ $($mod::$variant()),* ]};
155-
}
156-
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-
}
163-
164-
fn dep_kind_from_label_string(label: &str) -> Result<DepKind, ()> {
165-
match label {
166-
$(stringify!($variant) => Ok(DepKind::$variant),)*
167-
_ => Err(()),
168-
}
169-
}
170-
171-
/// Contains variant => str representations for constructing
172-
/// DepNode groups for tests.
173-
#[allow(dead_code, non_upper_case_globals)]
174-
pub mod label_strs {
175-
$(
176-
pub const $variant: &str = stringify!($variant);
177-
)*
178-
}
179-
);
180-
}
181-
182-
rustc_dep_node_append!([define_dep_nodes!][ <'tcx>
183-
// We use this for most things when incr. comp. is turned off.
184-
[] Null,
185-
186-
[anon] TraitSelect,
187-
188-
// WARNING: if `Symbol` is changed, make sure you update `make_compile_codegen_unit` below.
189-
[] CompileCodegenUnit(Symbol),
190-
191-
// WARNING: if `MonoItem` is changed, make sure you update `make_compile_mono_item` below.
192-
// Only used by rustc_codegen_cranelift
193-
[] CompileMonoItem(MonoItem),
194-
]);
195-
196131
// WARNING: `construct` is generic and does not know that `CompileCodegenUnit` takes `Symbol`s as keys.
197132
// Be very careful changing this type signature!
198133
crate fn make_compile_codegen_unit(tcx: TyCtxt<'_>, name: Symbol) -> DepNode {
@@ -205,18 +140,15 @@ crate fn make_compile_mono_item(tcx: TyCtxt<'tcx>, mono_item: &MonoItem<'tcx>) -
205140
DepNode::construct(tcx, DepKind::CompileMonoItem, mono_item)
206141
}
207142

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);
143+
#[inline(always)]
144+
pub fn fingerprint_style(tcx: TyCtxt<'_>, kind: DepKind) -> FingerprintStyle {
145+
// Only fetch the DepKindStruct once.
146+
let data = tcx.query_kind(kind);
147+
if data.is_anon {
148+
return FingerprintStyle::Opaque;
149+
}
150+
data.fingerprint_style
151+
}
220152

221153
pub trait DepNodeExt: Sized {
222154
/// Construct a DepNode from the given DepKind and DefPathHash. This
@@ -243,16 +175,15 @@ pub trait DepNodeExt: Sized {
243175
def_path_hash: DefPathHash,
244176
) -> Result<Self, ()>;
245177

246-
/// Used in testing
247-
fn has_label_string(label: &str) -> bool;
178+
fn fingerprint_style(self, tcx: TyCtxt<'_>) -> FingerprintStyle;
248179
}
249180

250181
impl DepNodeExt for DepNode {
251182
/// Construct a DepNode from the given DepKind and DefPathHash. This
252183
/// method will assert that the given DepKind actually requires a
253184
/// single DefId/DefPathHash parameter.
254185
fn from_def_path_hash(tcx: TyCtxt<'_>, def_path_hash: DefPathHash, kind: DepKind) -> DepNode {
255-
debug_assert!(kind.fingerprint_style(tcx) == FingerprintStyle::DefPathHash);
186+
debug_assert_eq!(fingerprint_style(tcx, kind), FingerprintStyle::DefPathHash);
256187
DepNode { kind, hash: def_path_hash.0.into() }
257188
}
258189

@@ -267,7 +198,7 @@ impl DepNodeExt for DepNode {
267198
/// refers to something from the previous compilation session that
268199
/// has been removed.
269200
fn extract_def_id(&self, tcx: TyCtxt<'tcx>) -> Option<DefId> {
270-
if self.kind.fingerprint_style(tcx) == FingerprintStyle::DefPathHash {
201+
if fingerprint_style(tcx, self.kind) == FingerprintStyle::DefPathHash {
271202
Some(tcx.def_path_hash_to_def_id(DefPathHash(self.hash.into())))
272203
} else {
273204
None
@@ -282,7 +213,7 @@ impl DepNodeExt for DepNode {
282213
) -> Result<DepNode, ()> {
283214
let kind = dep_kind_from_label_string(label)?;
284215

285-
match kind.fingerprint_style(tcx) {
216+
match fingerprint_style(tcx, kind) {
286217
FingerprintStyle::Opaque => Err(()),
287218
FingerprintStyle::Unit => Ok(DepNode::new_no_params(tcx, kind)),
288219
FingerprintStyle::DefPathHash => {
@@ -291,9 +222,9 @@ impl DepNodeExt for DepNode {
291222
}
292223
}
293224

294-
/// Used in testing
295-
fn has_label_string(label: &str) -> bool {
296-
dep_kind_from_label_string(label).is_ok()
225+
#[inline(always)]
226+
fn fingerprint_style(self, tcx: TyCtxt<'_>) -> FingerprintStyle {
227+
fingerprint_style(tcx, self.kind)
297228
}
298229
}
299230

compiler/rustc_middle/src/dep_graph/mod.rs

Lines changed: 7 additions & 39 deletions
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,47 +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, label_strs, DepContext, DepGraph,
11+
DepGraphQuery, DepKind, DepNode, DepNodeColor, DepNodeIndex, SerializedDepGraph,
12+
SerializedDepNodeIndex, TaskDeps, WorkProduct, WorkProductId,
1213
};
1314

14-
pub use dep_node::{label_strs, DepKind, DepKindStruct, DepNode, DepNodeExt};
15-
crate use dep_node::{make_compile_codegen_unit, make_compile_mono_item};
16-
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 DepGraphQuery = rustc_query_system::dep_graph::DepGraphQuery<DepKind>;
20-
pub type SerializedDepGraph = rustc_query_system::dep_graph::SerializedDepGraph<DepKind>;
21-
pub type EdgeFilter = rustc_query_system::dep_graph::debug::EdgeFilter<DepKind>;
22-
23-
impl rustc_query_system::dep_graph::DepKind for DepKind {
24-
const NULL: Self = DepKind::Null;
25-
26-
fn debug_node(node: &DepNode, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
27-
write!(f, "{:?}(", node.kind)?;
28-
29-
ty::tls::with_opt(|opt_tcx| {
30-
if let Some(tcx) = opt_tcx {
31-
if let Some(def_id) = node.extract_def_id(tcx) {
32-
write!(f, "{}", tcx.def_path_debug_str(def_id))?;
33-
} else if let Some(ref s) = tcx.dep_graph.dep_node_debug_str(*node) {
34-
write!(f, "{}", s)?;
35-
} else {
36-
write!(f, "{}", node.hash)?;
37-
}
38-
} else {
39-
write!(f, "{}", node.hash)?;
40-
}
41-
Ok(())
42-
})?;
43-
44-
write!(f, ")")
45-
}
46-
}
15+
crate use dep_node::{fingerprint_style, make_compile_codegen_unit, make_compile_mono_item};
16+
pub use dep_node::{DepKindStruct, DepNodeExt};
4717

4818
impl<'tcx> DepContext for TyCtxt<'tcx> {
49-
type DepKind = DepKind;
50-
5119
#[inline]
5220
fn create_stable_hashing_context(&self) -> StableHashingContext<'_> {
5321
TyCtxt::create_stable_hashing_context(*self)
@@ -70,7 +38,7 @@ impl<'tcx> DepContext for TyCtxt<'tcx> {
7038

7139
#[inline(always)]
7240
fn fingerprint_style(&self, kind: DepKind) -> rustc_query_system::dep_graph::FingerprintStyle {
73-
kind.fingerprint_style(*self)
41+
fingerprint_style(*self, kind)
7442
}
7543

7644
#[inline(always)]

compiler/rustc_query_impl/src/plumbing.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ impl<'tcx> std::ops::Deref for QueryCtxt<'tcx> {
2929
}
3030

3131
impl HasDepContext for QueryCtxt<'tcx> {
32-
type DepKind = rustc_middle::dep_graph::DepKind;
3332
type DepContext = TyCtxt<'tcx>;
3433

3534
#[inline]
@@ -39,7 +38,7 @@ impl HasDepContext for QueryCtxt<'tcx> {
3938
}
4039

4140
impl QueryContext for QueryCtxt<'tcx> {
42-
fn try_collect_active_jobs(&self) -> Option<QueryMap<Self::DepKind>> {
41+
fn try_collect_active_jobs(&self) -> Option<QueryMap> {
4342
self.queries.try_collect_active_jobs(**self)
4443
}
4544

@@ -254,7 +253,7 @@ macro_rules! define_queries {
254253
type Cache = query_storage::$name<$tcx>;
255254

256255
#[inline(always)]
257-
fn query_state<'a>(tcx: QueryCtxt<$tcx>) -> &'a QueryState<crate::dep_graph::DepKind, Self::Key>
256+
fn query_state<'a>(tcx: QueryCtxt<$tcx>) -> &'a QueryState<Self::Key>
258257
where QueryCtxt<$tcx>: 'a
259258
{
260259
&tcx.queries.$name
@@ -406,10 +405,7 @@ macro_rules! define_queries_struct {
406405

407406
pub on_disk_cache: Option<OnDiskCache<$tcx>>,
408407

409-
$($(#[$attr])* $name: QueryState<
410-
crate::dep_graph::DepKind,
411-
query_keys::$name<$tcx>,
412-
>,)*
408+
$($(#[$attr])* $name: QueryState<query_keys::$name<$tcx>>,)*
413409
}
414410

415411
impl<$tcx> Queries<$tcx> {
@@ -429,7 +425,7 @@ macro_rules! define_queries_struct {
429425
pub(crate) fn try_collect_active_jobs(
430426
&$tcx self,
431427
tcx: TyCtxt<$tcx>,
432-
) -> Option<QueryMap<crate::dep_graph::DepKind>> {
428+
) -> Option<QueryMap> {
433429
let tcx = QueryCtxt { tcx, queries: self };
434430
let mut jobs = QueryMap::default();
435431

compiler/rustc_query_system/src/dep_graph/debug.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Code for debugging the dep-graph.
22
3-
use super::{DepKind, DepNode, DepNodeIndex};
3+
use super::{DepNode, DepNodeIndex};
44
use rustc_data_structures::fx::FxHashMap;
55
use rustc_data_structures::sync::Lock;
66
use std::error::Error;
@@ -28,22 +28,22 @@ impl DepNodeFilter {
2828
}
2929

3030
/// Tests whether `node` meets the filter, returning true if so.
31-
pub fn test<K: DepKind>(&self, node: &DepNode<K>) -> bool {
31+
pub fn test(&self, node: &DepNode) -> bool {
3232
let debug_str = format!("{:?}", node);
3333
self.text.split('&').map(|s| s.trim()).all(|f| debug_str.contains(f))
3434
}
3535
}
3636

3737
/// A filter like `F -> G` where `F` and `G` are valid dep-node
3838
/// filters. This can be used to test the source/target independently.
39-
pub struct EdgeFilter<K: DepKind> {
39+
pub struct EdgeFilter {
4040
pub source: DepNodeFilter,
4141
pub target: DepNodeFilter,
42-
pub index_to_node: Lock<FxHashMap<DepNodeIndex, DepNode<K>>>,
42+
pub index_to_node: Lock<FxHashMap<DepNodeIndex, DepNode>>,
4343
}
4444

45-
impl<K: DepKind> EdgeFilter<K> {
46-
pub fn new(test: &str) -> Result<EdgeFilter<K>, Box<dyn Error>> {
45+
impl EdgeFilter {
46+
pub fn new(test: &str) -> Result<EdgeFilter, Box<dyn Error>> {
4747
let parts: Vec<_> = test.split("->").collect();
4848
if parts.len() != 2 {
4949
Err(format!("expected a filter like `a&b -> c&d`, not `{}`", test).into())
@@ -57,7 +57,7 @@ impl<K: DepKind> EdgeFilter<K> {
5757
}
5858

5959
#[cfg(debug_assertions)]
60-
pub fn test(&self, source: &DepNode<K>, target: &DepNode<K>) -> bool {
60+
pub fn test(&self, source: &DepNode, target: &DepNode) -> bool {
6161
self.source.test(source) && self.target.test(target)
6262
}
6363
}

0 commit comments

Comments
 (0)