Skip to content

Commit fcea886

Browse files
committed
Break up DepsType.
By moving most of it into `DepKind`, and changing two methods into free functions.
1 parent 7c877d9 commit fcea886

4 files changed

Lines changed: 61 additions & 76 deletions

File tree

compiler/rustc_middle/src/dep_graph/dep_node.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,26 @@ impl DepKind {
9292
pub const fn as_usize(&self) -> usize {
9393
self.variant as usize
9494
}
95+
96+
pub(crate) fn name(self) -> &'static str {
97+
DEP_KIND_NAMES[self.as_usize()]
98+
}
99+
100+
/// We use this for most things when incr. comp. is turned off.
101+
pub(crate) const NULL: DepKind = dep_kinds::Null;
102+
103+
/// We use this to create a forever-red node.
104+
pub(crate) const RED: DepKind = dep_kinds::Red;
105+
106+
/// We use this to create a side effect node.
107+
pub(crate) const SIDE_EFFECT: DepKind = dep_kinds::SideEffect;
108+
109+
/// We use this to create the anon node with zero dependencies.
110+
pub(crate) const ANON_ZERO_DEPS: DepKind = dep_kinds::AnonZeroDeps;
111+
112+
/// This is the highest value a `DepKind` can have. It's used during encoding to
113+
/// pack information into the unused bits.
114+
pub(crate) const MAX: u16 = DEP_KIND_VARIANTS - 1;
95115
}
96116

97117
pub fn default_dep_kind_debug(kind: DepKind, f: &mut fmt::Formatter<'_>) -> fmt::Result {

compiler/rustc_middle/src/dep_graph/graph.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use {super::debug::EdgeFilter, std::env};
2525

2626
use super::query::DepGraphQuery;
2727
use super::serialized::{GraphEncoder, SerializedDepGraph, SerializedDepNodeIndex};
28-
use super::{DepKind, DepNode, DepsType, HasDepContext, WorkProductId};
28+
use super::{DepKind, DepNode, HasDepContext, WorkProductId, read_deps, with_deps};
2929
use crate::dep_graph::edges::EdgesVec;
3030
use crate::ty::TyCtxt;
3131
use crate::verify_ich::incremental_verify_ich;
@@ -126,15 +126,15 @@ impl DepGraph {
126126

127127
// Instantiate a node with zero dependencies only once for anonymous queries.
128128
let _green_node_index = current.alloc_new_node(
129-
DepNode { kind: DepsType::DEP_KIND_ANON_ZERO_DEPS, hash: current.anon_id_seed.into() },
129+
DepNode { kind: DepKind::ANON_ZERO_DEPS, hash: current.anon_id_seed.into() },
130130
EdgesVec::new(),
131131
Fingerprint::ZERO,
132132
);
133133
assert_eq!(_green_node_index, DepNodeIndex::SINGLETON_ZERO_DEPS_ANON_NODE);
134134

135135
// Instantiate a dependy-less red node only once for anonymous queries.
136136
let red_node_index = current.alloc_new_node(
137-
DepNode { kind: DepsType::DEP_KIND_RED, hash: Fingerprint::ZERO.into() },
137+
DepNode { kind: DepKind::RED, hash: Fingerprint::ZERO.into() },
138138
EdgesVec::new(),
139139
Fingerprint::ZERO,
140140
);
@@ -181,7 +181,7 @@ impl DepGraph {
181181

182182
pub fn assert_ignored(&self) {
183183
if let Some(..) = self.data {
184-
DepsType::read_deps(|task_deps| {
184+
read_deps(|task_deps| {
185185
assert_matches!(
186186
task_deps,
187187
TaskDepsRef::Ignore,
@@ -195,7 +195,7 @@ impl DepGraph {
195195
where
196196
OP: FnOnce() -> R,
197197
{
198-
DepsType::with_deps(TaskDepsRef::Ignore, op)
198+
with_deps(TaskDepsRef::Ignore, op)
199199
}
200200

201201
/// Used to wrap the deserialization of a query result from disk,
@@ -248,7 +248,7 @@ impl DepGraph {
248248
where
249249
OP: FnOnce() -> R,
250250
{
251-
DepsType::with_deps(TaskDepsRef::Forbid, op)
251+
with_deps(TaskDepsRef::Forbid, op)
252252
}
253253

254254
#[inline(always)]
@@ -340,7 +340,7 @@ impl DepGraphData {
340340
},
341341
);
342342

343-
let with_deps = |task_deps| DepsType::with_deps(task_deps, || task(cx, arg));
343+
let with_deps = |task_deps| with_deps(task_deps, || task(cx, arg));
344344
let (result, edges) = if cx.dep_context().is_eval_always(key.kind) {
345345
(with_deps(TaskDepsRef::EvalAlways), EdgesVec::new())
346346
} else {
@@ -387,7 +387,7 @@ impl DepGraphData {
387387
None,
388388
128,
389389
));
390-
let result = DepsType::with_deps(TaskDepsRef::Allow(&task_deps), op);
390+
let result = with_deps(TaskDepsRef::Allow(&task_deps), op);
391391
let task_deps = task_deps.into_inner();
392392
let reads = task_deps.reads;
393393

@@ -460,7 +460,7 @@ impl DepGraph {
460460
#[inline]
461461
pub fn read_index(&self, dep_node_index: DepNodeIndex) {
462462
if let Some(ref data) = self.data {
463-
DepsType::read_deps(|task_deps| {
463+
read_deps(|task_deps| {
464464
let mut task_deps = match task_deps {
465465
TaskDepsRef::Allow(deps) => deps.lock(),
466466
TaskDepsRef::EvalAlways => {
@@ -517,7 +517,7 @@ impl DepGraph {
517517
#[inline]
518518
pub fn record_diagnostic<'tcx>(&self, tcx: TyCtxt<'tcx>, diagnostic: &DiagInner) {
519519
if let Some(ref data) = self.data {
520-
DepsType::read_deps(|task_deps| match task_deps {
520+
read_deps(|task_deps| match task_deps {
521521
TaskDepsRef::EvalAlways | TaskDepsRef::Ignore => return,
522522
TaskDepsRef::Forbid | TaskDepsRef::Allow(..) => {
523523
self.read_index(data.encode_diagnostic(tcx, diagnostic));
@@ -594,7 +594,7 @@ impl DepGraph {
594594
}
595595

596596
let mut edges = EdgesVec::new();
597-
DepsType::read_deps(|task_deps| match task_deps {
597+
read_deps(|task_deps| match task_deps {
598598
TaskDepsRef::Allow(deps) => edges.extend(deps.lock().reads.iter().copied()),
599599
TaskDepsRef::EvalAlways => {
600600
edges.push(DepNodeIndex::FOREVER_RED_NODE);
@@ -678,7 +678,7 @@ impl DepGraphData {
678678
// Use `send_new` so we get an unique index, even though the dep node is not.
679679
let dep_node_index = self.current.encoder.send_new(
680680
DepNode {
681-
kind: DepsType::DEP_KIND_SIDE_EFFECT,
681+
kind: DepKind::SIDE_EFFECT,
682682
hash: PackedFingerprint::from(Fingerprint::ZERO),
683683
},
684684
Fingerprint::ZERO,
@@ -695,7 +695,7 @@ impl DepGraphData {
695695
/// refer to a node created used `encode_diagnostic` in the previous session.
696696
#[inline]
697697
fn force_diagnostic_node<'tcx>(&self, tcx: TyCtxt<'tcx>, prev_index: SerializedDepNodeIndex) {
698-
DepsType::with_deps(TaskDepsRef::Ignore, || {
698+
with_deps(TaskDepsRef::Ignore, || {
699699
let side_effect = tcx.load_side_effect(prev_index).unwrap();
700700

701701
match &side_effect {
@@ -711,7 +711,7 @@ impl DepGraphData {
711711
prev_index,
712712
&self.colors,
713713
DepNode {
714-
kind: DepsType::DEP_KIND_SIDE_EFFECT,
714+
kind: DepKind::SIDE_EFFECT,
715715
hash: PackedFingerprint::from(Fingerprint::ZERO),
716716
},
717717
Fingerprint::ZERO,

compiler/rustc_middle/src/dep_graph/mod.rs

Lines changed: 19 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -68,51 +68,26 @@ impl FingerprintStyle {
6868
}
6969
}
7070

71-
pub struct DepsType;
72-
73-
impl DepsType {
74-
/// Execute the operation with provided dependencies.
75-
fn with_deps<OP, R>(task_deps: TaskDepsRef<'_>, op: OP) -> R
76-
where
77-
OP: FnOnce() -> R,
78-
{
79-
ty::tls::with_context(|icx| {
80-
let icx = ty::tls::ImplicitCtxt { task_deps, ..icx.clone() };
81-
82-
ty::tls::enter_context(&icx, op)
83-
})
84-
}
85-
86-
/// Access dependencies from current implicit context.
87-
fn read_deps<OP>(op: OP)
88-
where
89-
OP: for<'a> FnOnce(TaskDepsRef<'a>),
90-
{
91-
ty::tls::with_context_opt(|icx| {
92-
let Some(icx) = icx else { return };
93-
op(icx.task_deps)
94-
})
95-
}
96-
97-
fn name(dep_kind: DepKind) -> &'static str {
98-
dep_node::DEP_KIND_NAMES[dep_kind.as_usize()]
99-
}
100-
101-
/// We use this for most things when incr. comp. is turned off.
102-
const DEP_KIND_NULL: DepKind = dep_kinds::Null;
103-
104-
/// We use this to create a forever-red node.
105-
const DEP_KIND_RED: DepKind = dep_kinds::Red;
106-
107-
/// We use this to create a side effect node.
108-
const DEP_KIND_SIDE_EFFECT: DepKind = dep_kinds::SideEffect;
109-
110-
/// We use this to create the anon node with zero dependencies.
111-
const DEP_KIND_ANON_ZERO_DEPS: DepKind = dep_kinds::AnonZeroDeps;
71+
/// Execute the operation with provided dependencies.
72+
fn with_deps<OP, R>(task_deps: TaskDepsRef<'_>, op: OP) -> R
73+
where
74+
OP: FnOnce() -> R,
75+
{
76+
ty::tls::with_context(|icx| {
77+
let icx = ty::tls::ImplicitCtxt { task_deps, ..icx.clone() };
78+
ty::tls::enter_context(&icx, op)
79+
})
80+
}
11281

113-
/// This is the highest value a `DepKind` can have. It's used during encoding to
114-
/// pack information into the unused bits.
115-
const DEP_KIND_MAX: u16 = dep_node::DEP_KIND_VARIANTS - 1;
82+
/// Access dependencies from current implicit context.
83+
fn read_deps<OP>(op: OP)
84+
where
85+
OP: for<'a> FnOnce(TaskDepsRef<'a>),
86+
{
87+
ty::tls::with_context_opt(|icx| {
88+
let Some(icx) = icx else { return };
89+
op(icx.task_deps)
90+
})
11691
}
11792

11893
impl<'tcx> TyCtxt<'tcx> {

compiler/rustc_middle/src/dep_graph/serialized.rs

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ use tracing::{debug, instrument};
6161
use super::graph::{CurrentDepGraph, DepNodeColorMap};
6262
use super::query::DepGraphQuery;
6363
use super::{DepKind, DepNode, DepNodeIndex};
64-
use crate::dep_graph::DepsType;
6564
use crate::dep_graph::edges::EdgesVec;
6665

6766
// The maximum value of `SerializedDepNodeIndex` leaves the upper two bits
@@ -213,10 +212,7 @@ impl SerializedDepGraph {
213212
let graph_bytes = d.len() - (3 * IntEncodedWithFixedSize::ENCODED_SIZE) - d.position();
214213

215214
let mut nodes = IndexVec::from_elem_n(
216-
DepNode {
217-
kind: DepsType::DEP_KIND_NULL,
218-
hash: PackedFingerprint::from(Fingerprint::ZERO),
219-
},
215+
DepNode { kind: DepKind::NULL, hash: PackedFingerprint::from(Fingerprint::ZERO) },
220216
node_max,
221217
);
222218
let mut fingerprints = IndexVec::from_elem_n(Fingerprint::ZERO, node_max);
@@ -244,10 +240,7 @@ impl SerializedDepGraph {
244240

245241
let node = &mut nodes[index];
246242
// Make sure there's no duplicate indices in the dep graph.
247-
assert!(
248-
node_header.node().kind != DepsType::DEP_KIND_NULL
249-
&& node.kind == DepsType::DEP_KIND_NULL
250-
);
243+
assert!(node_header.node().kind != DepKind::NULL && node.kind == DepKind::NULL);
251244
*node = node_header.node();
252245

253246
fingerprints[index] = node_header.fingerprint();
@@ -275,7 +268,7 @@ impl SerializedDepGraph {
275268
edge_list_data.extend(&[0u8; DEP_NODE_PAD]);
276269

277270
// Read the number of each dep kind and use it to create an hash map with a suitable size.
278-
let mut index: Vec<_> = (0..(DepsType::DEP_KIND_MAX + 1))
271+
let mut index: Vec<_> = (0..(DepKind::MAX + 1))
279272
.map(|_| UnhashMap::with_capacity_and_hasher(d.read_u32() as usize, Default::default()))
280273
.collect();
281274

@@ -284,10 +277,8 @@ impl SerializedDepGraph {
284277
for (idx, node) in nodes.iter_enumerated() {
285278
if index[node.kind.as_usize()].insert(node.hash, idx).is_some() {
286279
// Empty nodes and side effect nodes can have duplicates
287-
if node.kind != DepsType::DEP_KIND_NULL
288-
&& node.kind != DepsType::DEP_KIND_SIDE_EFFECT
289-
{
290-
let name = DepsType::name(node.kind);
280+
if node.kind != DepKind::NULL && node.kind != DepKind::SIDE_EFFECT {
281+
let name = node.kind.name();
291282
panic!(
292283
"Error: A dep graph node ({name}) does not have an unique index. \
293284
Running a clean build on a nightly compiler with `-Z incremental-verify-ich` \
@@ -347,7 +338,7 @@ impl SerializedNodeHeader {
347338
const TOTAL_BITS: usize = size_of::<DepKind>() * 8;
348339
const LEN_BITS: usize = Self::TOTAL_BITS - Self::KIND_BITS - Self::WIDTH_BITS;
349340
const WIDTH_BITS: usize = DEP_NODE_WIDTH_BITS;
350-
const KIND_BITS: usize = Self::TOTAL_BITS - DepsType::DEP_KIND_MAX.leading_zeros() as usize;
341+
const KIND_BITS: usize = Self::TOTAL_BITS - DepKind::MAX.leading_zeros() as usize;
351342
const MAX_INLINE_LEN: usize = (u16::MAX as usize >> (Self::TOTAL_BITS - Self::LEN_BITS)) - 1;
352343

353344
#[inline]
@@ -566,7 +557,7 @@ impl EncoderState {
566557
edge_count: 0,
567558
node_count: 0,
568559
encoder: MemEncoder::new(),
569-
kind_stats: iter::repeat_n(0, DepsType::DEP_KIND_MAX as usize + 1).collect(),
560+
kind_stats: iter::repeat_n(0, DepKind::MAX as usize + 1).collect(),
570561
})
571562
}),
572563
}
@@ -734,8 +725,7 @@ impl EncoderState {
734725

735726
let mut encoder = self.file.lock().take().unwrap();
736727

737-
let mut kind_stats: Vec<u32> =
738-
iter::repeat_n(0, DepsType::DEP_KIND_MAX as usize + 1).collect();
728+
let mut kind_stats: Vec<u32> = iter::repeat_n(0, DepKind::MAX as usize + 1).collect();
739729

740730
let mut node_max = 0;
741731
let mut node_count = 0;

0 commit comments

Comments
 (0)