Skip to content

Commit cdd8e9d

Browse files
committed
Track the invoking DepNode in the implicit context.
1 parent 4feb1b7 commit cdd8e9d

File tree

8 files changed

+42
-12
lines changed

8 files changed

+42
-12
lines changed

compiler/rustc_data_structures/src/fingerprint.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,10 @@ impl<D: Decoder> Decodable<D> for Fingerprint {
196196
#[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Clone, Copy, Hash)]
197197
pub struct PackedFingerprint(Fingerprint);
198198

199+
impl PackedFingerprint {
200+
pub const ZERO: PackedFingerprint = PackedFingerprint(Fingerprint::ZERO);
201+
}
202+
199203
impl std::fmt::Display for PackedFingerprint {
200204
#[inline]
201205
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {

compiler/rustc_middle/src/dep_graph/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ pub(crate) use dep_node::{make_compile_codegen_unit, make_compile_mono_item};
1616

1717
pub type DepGraph = rustc_query_system::dep_graph::DepGraph<DepKind>;
1818

19+
pub type CurrentDepNode = rustc_query_system::dep_graph::CurrentDepNode<DepKind>;
1920
pub type TaskDeps = rustc_query_system::dep_graph::TaskDeps<DepKind>;
2021
pub type TaskDepsRef<'a> = rustc_query_system::dep_graph::TaskDepsRef<'a, DepKind>;
2122
pub type DepGraphQuery = rustc_query_system::dep_graph::DepGraphQuery<DepKind>;
@@ -48,12 +49,12 @@ impl rustc_query_system::dep_graph::DepKind for DepKind {
4849
write!(f, ")")
4950
}
5051

51-
fn with_deps<OP, R>(task_deps: TaskDepsRef<'_>, op: OP) -> R
52+
fn with_deps<OP, R>(current_node: CurrentDepNode, task_deps: TaskDepsRef<'_>, op: OP) -> R
5253
where
5354
OP: FnOnce() -> R,
5455
{
5556
ty::tls::with_context(|icx| {
56-
let icx = ty::tls::ImplicitCtxt { task_deps, ..icx.clone() };
57+
let icx = ty::tls::ImplicitCtxt { current_node, task_deps, ..icx.clone() };
5758

5859
ty::tls::enter_context(&icx, op)
5960
})

compiler/rustc_middle/src/ty/context/tls.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::{GlobalCtxt, TyCtxt};
22

3-
use crate::dep_graph::TaskDepsRef;
3+
use crate::dep_graph::{CurrentDepNode, DepNode, TaskDepsRef};
44
use crate::query::plumbing::QueryJobId;
55
use rustc_data_structures::sync::{self, Lock};
66
use rustc_errors::Diagnostic;
@@ -31,6 +31,10 @@ pub struct ImplicitCtxt<'a, 'tcx> {
3131
/// Used to prevent queries from calling too deeply.
3232
pub query_depth: usize,
3333

34+
/// The DepNode of the query being executed. This is updated by the dep-graph. Thisis used to
35+
/// know which query created an expansion.
36+
pub current_node: CurrentDepNode,
37+
3438
/// The current dep graph task. This is used to add dependencies to queries
3539
/// when executing them.
3640
pub task_deps: TaskDepsRef<'a>,
@@ -39,11 +43,17 @@ pub struct ImplicitCtxt<'a, 'tcx> {
3943
impl<'a, 'tcx> ImplicitCtxt<'a, 'tcx> {
4044
pub fn new(gcx: &'tcx GlobalCtxt<'tcx>) -> Self {
4145
let tcx = TyCtxt { gcx };
46+
let current_node = if tcx.dep_graph.is_fully_enabled() {
47+
CurrentDepNode::Untracked
48+
} else {
49+
CurrentDepNode::Regular(DepNode::NULL)
50+
};
4251
ImplicitCtxt {
4352
tcx,
4453
query: None,
4554
diagnostics: None,
4655
query_depth: 0,
56+
current_node,
4757
task_deps: TaskDepsRef::Ignore,
4858
}
4959
}

compiler/rustc_query_impl/src/plumbing.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ impl QueryContext for QueryCtxt<'_> {
142142
query: Some(token),
143143
diagnostics,
144144
query_depth: current_icx.query_depth + depth_limit as usize,
145+
current_node: current_icx.current_node,
145146
task_deps: current_icx.task_deps,
146147
};
147148

compiler/rustc_query_system/src/dep_graph/dep_node.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ pub struct DepNode<K> {
5858
}
5959

6060
impl<K: DepKind> DepNode<K> {
61+
pub const NULL: DepNode<K> = DepNode { kind: K::NULL, hash: PackedFingerprint::ZERO };
62+
6163
/// Creates a new, parameterless DepNode. This method will assert
6264
/// that the DepNode corresponding to the given DepKind actually
6365
/// does not require any parameters.

compiler/rustc_query_system/src/dep_graph/graph.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use std::sync::atomic::Ordering::Relaxed;
1818

1919
use super::query::DepGraphQuery;
2020
use super::serialized::{GraphEncoder, SerializedDepGraph, SerializedDepNodeIndex};
21-
use super::{DepContext, DepKind, DepNode, HasDepContext, WorkProductId};
21+
use super::{CurrentDepNode, DepContext, DepKind, DepNode, HasDepContext, WorkProductId};
2222
use crate::ich::StableHashingContext;
2323
use crate::query::{QueryContext, QuerySideEffects};
2424

@@ -218,7 +218,7 @@ impl<K: DepKind> DepGraph<K> {
218218
where
219219
OP: FnOnce() -> R,
220220
{
221-
K::with_deps(TaskDepsRef::Ignore, op)
221+
K::with_deps(CurrentDepNode::Untracked, TaskDepsRef::Ignore, op)
222222
}
223223

224224
/// Used to wrap the deserialization of a query result from disk,
@@ -271,7 +271,7 @@ impl<K: DepKind> DepGraph<K> {
271271
where
272272
OP: FnOnce() -> R,
273273
{
274-
K::with_deps(TaskDepsRef::Forbid, op)
274+
K::with_deps(CurrentDepNode::Untracked, TaskDepsRef::Forbid, op)
275275
}
276276

277277
#[inline(always)]
@@ -354,7 +354,8 @@ impl<K: DepKind> DepGraphData<K> {
354354
- dep-node: {key:?}"
355355
);
356356

357-
let with_deps = |task_deps| K::with_deps(task_deps, || task(cx, arg));
357+
let with_deps =
358+
|task_deps| K::with_deps(CurrentDepNode::Regular(key), task_deps, || task(cx, arg));
358359
let (result, edges) = if cx.dep_context().is_eval_always(key.kind) {
359360
(with_deps(TaskDepsRef::EvalAlways), smallvec![])
360361
} else {
@@ -414,7 +415,7 @@ impl<K: DepKind> DepGraphData<K> {
414415
debug_assert!(!cx.is_eval_always(dep_kind));
415416

416417
let task_deps = Lock::new(TaskDeps::default());
417-
let result = K::with_deps(TaskDepsRef::Allow(&task_deps), op);
418+
let result = K::with_deps(CurrentDepNode::Anonymous, TaskDepsRef::Allow(&task_deps), op);
418419
let task_deps = task_deps.into_inner();
419420
let task_deps = task_deps.reads;
420421

compiler/rustc_query_system/src/dep_graph/mod.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,13 @@ impl FingerprintStyle {
137137
}
138138
}
139139

140+
#[derive(Copy, Clone, Hash)]
141+
pub enum CurrentDepNode<K> {
142+
Regular(DepNode<K>),
143+
Anonymous,
144+
Untracked,
145+
}
146+
140147
/// Describe the different families of dependency nodes.
141148
pub trait DepKind: Copy + fmt::Debug + Eq + Hash + Send + Encodable<FileEncoder> + 'static {
142149
/// DepKind to use when incr. comp. is turned off.
@@ -149,7 +156,7 @@ pub trait DepKind: Copy + fmt::Debug + Eq + Hash + Send + Encodable<FileEncoder>
149156
fn debug_node(node: &DepNode<Self>, f: &mut fmt::Formatter<'_>) -> fmt::Result;
150157

151158
/// Execute the operation with provided dependencies.
152-
fn with_deps<OP, R>(deps: TaskDepsRef<'_, Self>, op: OP) -> R
159+
fn with_deps<OP, R>(node: CurrentDepNode<Self>, deps: TaskDepsRef<'_, Self>, op: OP) -> R
153160
where
154161
OP: FnOnce() -> R;
155162

compiler/rustc_query_system/src/query/plumbing.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
//! generate the actual methods on tcx which find and execute the provider,
33
//! manage the caches, and so forth.
44
5+
use crate::dep_graph::{CurrentDepNode, DepGraphData, HasDepContext, TaskDepsRef};
56
use crate::dep_graph::{DepContext, DepKind, DepNode, DepNodeIndex, DepNodeParams};
6-
use crate::dep_graph::{DepGraphData, HasDepContext};
77
use crate::ich::StableHashingContext;
88
use crate::query::caches::QueryCache;
99
#[cfg(parallel_compiler)]
@@ -629,8 +629,12 @@ where
629629
// recompute.
630630
let prof_timer = qcx.dep_context().profiler().query_provider();
631631

632-
// The dep-graph for this computation is already in-place.
633-
let result = qcx.dep_context().dep_graph().with_ignore(|| query.compute(qcx, *key));
632+
// The dep-graph for this computation is already in-place, but any side effect due to this
633+
// query needs to know our DepNode.
634+
let result =
635+
Qcx::DepKind::with_deps(CurrentDepNode::Regular(*dep_node), TaskDepsRef::Ignore, || {
636+
query.compute(qcx, *key)
637+
});
634638

635639
prof_timer.finish_with_query_invocation_id(dep_node_index.into());
636640

0 commit comments

Comments
 (0)