Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit c6011a7

Browse files
committedMar 11, 2025
Represent diagnostic side effects as dep nodes
1 parent f2d69d5 commit c6011a7

File tree

11 files changed

+138
-180
lines changed

11 files changed

+138
-180
lines changed
 

‎compiler/rustc_interface/src/callbacks.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use std::fmt;
1414
use rustc_errors::{DiagInner, TRACK_DIAGNOSTIC};
1515
use rustc_middle::dep_graph::{DepNodeExt, TaskDepsRef};
1616
use rustc_middle::ty::tls;
17+
use rustc_query_impl::QueryCtxt;
1718
use rustc_query_system::dep_graph::dep_node::default_dep_kind_debug;
1819
use rustc_query_system::dep_graph::{DepContext, DepKind, DepNode};
1920

@@ -41,9 +42,7 @@ fn track_span_parent(def_id: rustc_span::def_id::LocalDefId) {
4142
fn track_diagnostic<R>(diagnostic: DiagInner, f: &mut dyn FnMut(DiagInner) -> R) -> R {
4243
tls::with_context_opt(|icx| {
4344
if let Some(icx) = icx {
44-
if let Some(diagnostics) = icx.diagnostics {
45-
diagnostics.lock().extend(Some(diagnostic.clone()));
46-
}
45+
icx.tcx.dep_graph.record_diagnostic(QueryCtxt::new(icx.tcx), &diagnostic);
4746

4847
// Diagnostics are tracked, we can ignore the dependency.
4948
let icx = tls::ImplicitCtxt { task_deps: TaskDepsRef::Ignore, ..icx.clone() };

‎compiler/rustc_middle/src/dep_graph/dep_node.rs

+1
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ rustc_query_append!(define_dep_nodes![
7979
[] fn Null() -> (),
8080
/// We use this to create a forever-red node.
8181
[] fn Red() -> (),
82+
[] fn SideEffect() -> (),
8283
[] fn TraitSelect() -> (),
8384
[] fn CompileCodegenUnit() -> (),
8485
[] fn CompileMonoItem() -> (),

‎compiler/rustc_middle/src/dep_graph/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ impl Deps for DepsType {
4646

4747
const DEP_KIND_NULL: DepKind = dep_kinds::Null;
4848
const DEP_KIND_RED: DepKind = dep_kinds::Red;
49+
const DEP_KIND_SIDE_EFFECT: DepKind = dep_kinds::SideEffect;
4950
const DEP_KIND_MAX: u16 = dep_node::DEP_KIND_VARIANTS - 1;
5051
}
5152

‎compiler/rustc_middle/src/query/on_disk_cache.rs

+2-18
Original file line numberDiff line numberDiff line change
@@ -357,11 +357,10 @@ impl OnDiskCache {
357357
&self,
358358
tcx: TyCtxt<'_>,
359359
dep_node_index: SerializedDepNodeIndex,
360-
) -> QuerySideEffects {
360+
) -> Option<QuerySideEffects> {
361361
let side_effects: Option<QuerySideEffects> =
362362
self.load_indexed(tcx, dep_node_index, &self.prev_side_effects_index);
363-
364-
side_effects.unwrap_or_default()
363+
side_effects
365364
}
366365

367366
/// Stores a `QuerySideEffects` emitted during the current compilation session.
@@ -395,21 +394,6 @@ impl OnDiskCache {
395394
opt_value
396395
}
397396

398-
/// Stores side effect emitted during computation of an anonymous query.
399-
/// Since many anonymous queries can share the same `DepNode`, we aggregate
400-
/// them -- as opposed to regular queries where we assume that there is a
401-
/// 1:1 relationship between query-key and `DepNode`.
402-
pub fn store_side_effects_for_anon_node(
403-
&self,
404-
dep_node_index: DepNodeIndex,
405-
side_effects: QuerySideEffects,
406-
) {
407-
let mut current_side_effects = self.current_side_effects.borrow_mut();
408-
409-
let x = current_side_effects.entry(dep_node_index).or_default();
410-
x.append(side_effects);
411-
}
412-
413397
fn load_indexed<'tcx, T>(
414398
&self,
415399
tcx: TyCtxt<'tcx>,

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

+2-14
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
use std::{mem, ptr};
22

3-
use rustc_data_structures::sync::{self, Lock};
4-
use rustc_errors::DiagInner;
5-
use thin_vec::ThinVec;
3+
use rustc_data_structures::sync;
64

75
use super::{GlobalCtxt, TyCtxt};
86
use crate::dep_graph::TaskDepsRef;
@@ -22,10 +20,6 @@ pub struct ImplicitCtxt<'a, 'tcx> {
2220
/// `ty::query::plumbing` when executing a query.
2321
pub query: Option<QueryJobId>,
2422

25-
/// Where to store diagnostics for the current query job, if any.
26-
/// This is updated by `JobOwner::start` in `ty::query::plumbing` when executing a query.
27-
pub diagnostics: Option<&'a Lock<ThinVec<DiagInner>>>,
28-
2923
/// Used to prevent queries from calling too deeply.
3024
pub query_depth: usize,
3125

@@ -37,13 +31,7 @@ pub struct ImplicitCtxt<'a, 'tcx> {
3731
impl<'a, 'tcx> ImplicitCtxt<'a, 'tcx> {
3832
pub fn new(gcx: &'tcx GlobalCtxt<'tcx>) -> Self {
3933
let tcx = TyCtxt { gcx };
40-
ImplicitCtxt {
41-
tcx,
42-
query: None,
43-
diagnostics: None,
44-
query_depth: 0,
45-
task_deps: TaskDepsRef::Ignore,
46-
}
34+
ImplicitCtxt { tcx, query: None, query_depth: 0, task_deps: TaskDepsRef::Ignore }
4735
}
4836
}
4937

‎compiler/rustc_query_impl/src/plumbing.rs

+23-25
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
use std::num::NonZero;
66

77
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
8-
use rustc_data_structures::sync::Lock;
98
use rustc_data_structures::unord::UnordMap;
10-
use rustc_errors::DiagInner;
119
use rustc_hashes::Hash64;
1210
use rustc_index::Idx;
1311
use rustc_middle::bug;
@@ -32,7 +30,6 @@ use rustc_query_system::{QueryOverflow, QueryOverflowNote};
3230
use rustc_serialize::{Decodable, Encodable};
3331
use rustc_session::Limit;
3432
use rustc_span::def_id::LOCAL_CRATE;
35-
use thin_vec::ThinVec;
3633

3734
use crate::QueryConfigRestored;
3835

@@ -92,12 +89,14 @@ impl QueryContext for QueryCtxt<'_> {
9289
}
9390

9491
// Interactions with on_disk_cache
95-
fn load_side_effects(self, prev_dep_node_index: SerializedDepNodeIndex) -> QuerySideEffects {
92+
fn load_side_effects(
93+
self,
94+
prev_dep_node_index: SerializedDepNodeIndex,
95+
) -> Option<QuerySideEffects> {
9696
self.query_system
9797
.on_disk_cache
9898
.as_ref()
99-
.map(|c| c.load_side_effects(self.tcx, prev_dep_node_index))
100-
.unwrap_or_default()
99+
.and_then(|c| c.load_side_effects(self.tcx, prev_dep_node_index))
101100
}
102101

103102
#[inline(never)]
@@ -108,27 +107,13 @@ impl QueryContext for QueryCtxt<'_> {
108107
}
109108
}
110109

111-
#[inline(never)]
112-
#[cold]
113-
fn store_side_effects_for_anon_node(
114-
self,
115-
dep_node_index: DepNodeIndex,
116-
side_effects: QuerySideEffects,
117-
) {
118-
if let Some(c) = self.query_system.on_disk_cache.as_ref() {
119-
c.store_side_effects_for_anon_node(dep_node_index, side_effects)
120-
}
121-
}
122-
123110
/// Executes a job by changing the `ImplicitCtxt` to point to the
124-
/// new query job while it executes. It returns the diagnostics
125-
/// captured during execution and the actual result.
111+
/// new query job while it executes.
126112
#[inline(always)]
127113
fn start_query<R>(
128114
self,
129115
token: QueryJobId,
130116
depth_limit: bool,
131-
diagnostics: Option<&Lock<ThinVec<DiagInner>>>,
132117
compute: impl FnOnce() -> R,
133118
) -> R {
134119
// The `TyCtxt` stored in TLS has the same global interner lifetime
@@ -143,7 +128,6 @@ impl QueryContext for QueryCtxt<'_> {
143128
let new_icx = ImplicitCtxt {
144129
tcx: self.tcx,
145130
query: Some(token),
146-
diagnostics,
147131
query_depth: current_icx.query_depth + depth_limit as usize,
148132
task_deps: current_icx.task_deps,
149133
};
@@ -500,7 +484,7 @@ where
500484
is_anon,
501485
is_eval_always,
502486
fingerprint_style,
503-
force_from_dep_node: Some(|tcx, dep_node| {
487+
force_from_dep_node: Some(|tcx, dep_node, _| {
504488
force_from_dep_node(Q::config(tcx), tcx, dep_node)
505489
}),
506490
try_load_from_on_disk_cache: Some(|tcx, dep_node| {
@@ -802,7 +786,7 @@ macro_rules! define_queries {
802786
is_anon: false,
803787
is_eval_always: false,
804788
fingerprint_style: FingerprintStyle::Unit,
805-
force_from_dep_node: Some(|_, dep_node| bug!("force_from_dep_node: encountered {:?}", dep_node)),
789+
force_from_dep_node: Some(|_, dep_node, _| bug!("force_from_dep_node: encountered {:?}", dep_node)),
806790
try_load_from_on_disk_cache: None,
807791
name: &"Null",
808792
}
@@ -814,12 +798,26 @@ macro_rules! define_queries {
814798
is_anon: false,
815799
is_eval_always: false,
816800
fingerprint_style: FingerprintStyle::Unit,
817-
force_from_dep_node: Some(|_, dep_node| bug!("force_from_dep_node: encountered {:?}", dep_node)),
801+
force_from_dep_node: Some(|_, dep_node, _| bug!("force_from_dep_node: encountered {:?}", dep_node)),
818802
try_load_from_on_disk_cache: None,
819803
name: &"Red",
820804
}
821805
}
822806

807+
pub(crate) fn SideEffect<'tcx>() -> DepKindStruct<'tcx> {
808+
DepKindStruct {
809+
is_anon: false,
810+
is_eval_always: false,
811+
fingerprint_style: FingerprintStyle::Unit,
812+
force_from_dep_node: Some(|tcx, _, prev_index| {
813+
tcx.dep_graph.force_diagnostic_node(QueryCtxt::new(tcx), prev_index);
814+
true
815+
}),
816+
try_load_from_on_disk_cache: None,
817+
name: &"SideEffect",
818+
}
819+
}
820+
823821
pub(crate) fn TraitSelect<'tcx>() -> DepKindStruct<'tcx> {
824822
DepKindStruct {
825823
is_anon: true,

‎compiler/rustc_query_system/src/dep_graph/dep_node.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ use rustc_data_structures::stable_hasher::{HashStable, StableHasher, StableOrd,
6464
use rustc_hir::definitions::DefPathHash;
6565
use rustc_macros::{Decodable, Encodable};
6666

67-
use super::{DepContext, FingerprintStyle};
67+
use super::{DepContext, FingerprintStyle, SerializedDepNodeIndex};
6868
use crate::ich::StableHashingContext;
6969

7070
/// This serves as an index into arrays built by `make_dep_kind_array`.
@@ -275,7 +275,8 @@ pub struct DepKindStruct<Tcx: DepContext> {
275275
/// with kind `MirValidated`, we know that the GUID/fingerprint of the `DepNode`
276276
/// is actually a `DefPathHash`, and can therefore just look up the corresponding
277277
/// `DefId` in `tcx.def_path_hash_to_def_id`.
278-
pub force_from_dep_node: Option<fn(tcx: Tcx, dep_node: DepNode) -> bool>,
278+
pub force_from_dep_node:
279+
Option<fn(tcx: Tcx, dep_node: DepNode, prev_index: SerializedDepNodeIndex) -> bool>,
279280

280281
/// Invoke a query to put the on-disk cached value in memory.
281282
pub try_load_from_on_disk_cache: Option<fn(Tcx, DepNode)>,
There was a problem loading the remainder of the diff.

0 commit comments

Comments
 (0)
Failed to load comments.