Skip to content

Commit f0413f6

Browse files
committed
Represent diagnostic side effects as dep nodes
1 parent d9fe043 commit f0413f6

File tree

11 files changed

+135
-177
lines changed

11 files changed

+135
-177
lines changed

compiler/rustc_interface/src/callbacks.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use rustc_errors::{DiagInner, TRACK_DIAGNOSTIC};
1313
use rustc_middle::dep_graph::{DepNodeExt, TaskDepsRef};
1414
use rustc_middle::ty::tls;
15+
use rustc_query_impl::QueryCtxt;
1516
use rustc_query_system::dep_graph::dep_node::default_dep_kind_debug;
1617
use rustc_query_system::dep_graph::{DepContext, DepKind, DepNode};
1718
use std::fmt;
@@ -32,9 +33,7 @@ fn track_span_parent(def_id: rustc_span::def_id::LocalDefId) {
3233
fn track_diagnostic(diagnostic: DiagInner, f: &mut dyn FnMut(DiagInner)) {
3334
tls::with_context_opt(|icx| {
3435
if let Some(icx) = icx {
35-
if let Some(diagnostics) = icx.diagnostics {
36-
diagnostics.lock().extend(Some(diagnostic.clone()));
37-
}
36+
icx.tcx.dep_graph.record_diagnostic(QueryCtxt::new(icx.tcx), &diagnostic);
3837

3938
// Diagnostics are tracked, we can ignore the dependency.
4039
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
@@ -137,6 +137,7 @@ rustc_query_append!(define_dep_nodes![
137137
[] fn Null() -> (),
138138
/// We use this to create a forever-red node.
139139
[] fn Red() -> (),
140+
[] fn SideEffect() -> (),
140141
[] fn TraitSelect() -> (),
141142
[] fn CompileCodegenUnit() -> (),
142143
[] fn CompileMonoItem() -> (),

compiler/rustc_middle/src/dep_graph/mod.rs

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

4848
const DEP_KIND_NULL: DepKind = dep_kinds::Null;
4949
const DEP_KIND_RED: DepKind = dep_kinds::Red;
50+
const DEP_KIND_SIDE_EFFECT: DepKind = dep_kinds::SideEffect;
5051
const DEP_KIND_MAX: u16 = dep_node::DEP_KIND_VARIANTS - 1;
5152
}
5253

compiler/rustc_middle/src/query/on_disk_cache.rs

+2-18
Original file line numberDiff line numberDiff line change
@@ -357,11 +357,10 @@ impl<'sess> OnDiskCache<'sess> {
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<'sess> OnDiskCache<'sess> {
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
@@ -2,13 +2,11 @@ use super::{GlobalCtxt, TyCtxt};
22

33
use crate::dep_graph::TaskDepsRef;
44
use crate::query::plumbing::QueryJobId;
5-
use rustc_data_structures::sync::{self, Lock};
6-
use rustc_errors::DiagInner;
5+
use rustc_data_structures::sync;
76
#[cfg(not(parallel_compiler))]
87
use std::cell::Cell;
98
use std::mem;
109
use std::ptr;
11-
use thin_vec::ThinVec;
1210

1311
/// This is the implicit state of rustc. It contains the current
1412
/// `TyCtxt` and query. It is updated when creating a local interner or
@@ -24,10 +22,6 @@ pub struct ImplicitCtxt<'a, 'tcx> {
2422
/// `ty::query::plumbing` when executing a query.
2523
pub query: Option<QueryJobId>,
2624

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

@@ -39,13 +33,7 @@ pub struct ImplicitCtxt<'a, 'tcx> {
3933
impl<'a, 'tcx> ImplicitCtxt<'a, 'tcx> {
4034
pub fn new(gcx: &'tcx GlobalCtxt<'tcx>) -> Self {
4135
let tcx = TyCtxt { gcx };
42-
ImplicitCtxt {
43-
tcx,
44-
query: None,
45-
diagnostics: None,
46-
query_depth: 0,
47-
task_deps: TaskDepsRef::Ignore,
48-
}
36+
ImplicitCtxt { tcx, query: None, query_depth: 0, task_deps: TaskDepsRef::Ignore }
4937
}
5038
}
5139

compiler/rustc_query_impl/src/plumbing.rs

+23-25
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ use crate::rustc_middle::dep_graph::DepContext;
66
use crate::rustc_middle::ty::TyEncoder;
77
use crate::QueryConfigRestored;
88
use rustc_data_structures::stable_hasher::{Hash64, HashStable, StableHasher};
9-
use rustc_data_structures::sync::Lock;
10-
use rustc_errors::DiagInner;
119

1210
use rustc_index::Idx;
1311
use rustc_middle::dep_graph::dep_kinds;
@@ -32,7 +30,6 @@ use rustc_serialize::Encodable;
3230
use rustc_session::Limit;
3331
use rustc_span::def_id::LOCAL_CRATE;
3432
use std::num::NonZero;
35-
use thin_vec::ThinVec;
3633

3734
#[derive(Copy, Clone)]
3835
pub struct QueryCtxt<'tcx> {
@@ -90,12 +87,14 @@ impl QueryContext for QueryCtxt<'_> {
9087
}
9188

9289
// Interactions with on_disk_cache
93-
fn load_side_effects(self, prev_dep_node_index: SerializedDepNodeIndex) -> QuerySideEffects {
90+
fn load_side_effects(
91+
self,
92+
prev_dep_node_index: SerializedDepNodeIndex,
93+
) -> Option<QuerySideEffects> {
9494
self.query_system
9595
.on_disk_cache
9696
.as_ref()
97-
.map(|c| c.load_side_effects(self.tcx, prev_dep_node_index))
98-
.unwrap_or_default()
97+
.and_then(|c| c.load_side_effects(self.tcx, prev_dep_node_index))
9998
}
10099

101100
#[inline(never)]
@@ -106,27 +105,13 @@ impl QueryContext for QueryCtxt<'_> {
106105
}
107106
}
108107

109-
#[inline(never)]
110-
#[cold]
111-
fn store_side_effects_for_anon_node(
112-
self,
113-
dep_node_index: DepNodeIndex,
114-
side_effects: QuerySideEffects,
115-
) {
116-
if let Some(c) = self.query_system.on_disk_cache.as_ref() {
117-
c.store_side_effects_for_anon_node(dep_node_index, side_effects)
118-
}
119-
}
120-
121108
/// Executes a job by changing the `ImplicitCtxt` to point to the
122-
/// new query job while it executes. It returns the diagnostics
123-
/// captured during execution and the actual result.
109+
/// new query job while it executes.
124110
#[inline(always)]
125111
fn start_query<R>(
126112
self,
127113
token: QueryJobId,
128114
depth_limit: bool,
129-
diagnostics: Option<&Lock<ThinVec<DiagInner>>>,
130115
compute: impl FnOnce() -> R,
131116
) -> R {
132117
// The `TyCtxt` stored in TLS has the same global interner lifetime
@@ -141,7 +126,6 @@ impl QueryContext for QueryCtxt<'_> {
141126
let new_icx = ImplicitCtxt {
142127
tcx: self.tcx,
143128
query: Some(token),
144-
diagnostics,
145129
query_depth: current_icx.query_depth + depth_limit as usize,
146130
task_deps: current_icx.task_deps,
147131
};
@@ -467,7 +451,7 @@ where
467451
is_anon,
468452
is_eval_always,
469453
fingerprint_style,
470-
force_from_dep_node: Some(|tcx, dep_node| {
454+
force_from_dep_node: Some(|tcx, dep_node, _| {
471455
force_from_dep_node(Q::config(tcx), tcx, dep_node)
472456
}),
473457
try_load_from_on_disk_cache: Some(|tcx, dep_node| {
@@ -741,7 +725,7 @@ macro_rules! define_queries {
741725
is_anon: false,
742726
is_eval_always: false,
743727
fingerprint_style: FingerprintStyle::Unit,
744-
force_from_dep_node: Some(|_, dep_node| bug!("force_from_dep_node: encountered {:?}", dep_node)),
728+
force_from_dep_node: Some(|_, dep_node, _| bug!("force_from_dep_node: encountered {:?}", dep_node)),
745729
try_load_from_on_disk_cache: None,
746730
name: &"Null",
747731
}
@@ -753,12 +737,26 @@ macro_rules! define_queries {
753737
is_anon: false,
754738
is_eval_always: false,
755739
fingerprint_style: FingerprintStyle::Unit,
756-
force_from_dep_node: Some(|_, dep_node| bug!("force_from_dep_node: encountered {:?}", dep_node)),
740+
force_from_dep_node: Some(|_, dep_node, _| bug!("force_from_dep_node: encountered {:?}", dep_node)),
757741
try_load_from_on_disk_cache: None,
758742
name: &"Red",
759743
}
760744
}
761745

746+
pub fn SideEffect<'tcx>() -> DepKindStruct<'tcx> {
747+
DepKindStruct {
748+
is_anon: false,
749+
is_eval_always: false,
750+
fingerprint_style: FingerprintStyle::Unit,
751+
force_from_dep_node: Some(|tcx, _, prev_index| {
752+
tcx.dep_graph.force_diagnostic_node(QueryCtxt::new(tcx), prev_index);
753+
true
754+
}),
755+
try_load_from_on_disk_cache: None,
756+
name: &"SideEffect",
757+
}
758+
}
759+
762760
pub fn TraitSelect<'tcx>() -> DepKindStruct<'tcx> {
763761
DepKindStruct {
764762
is_anon: true,

compiler/rustc_query_system/src/dep_graph/dep_node.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
//! `DefId` it was computed from. In other cases, too much information gets
4343
//! lost during fingerprint computation.
4444
45-
use super::{DepContext, FingerprintStyle};
45+
use super::{DepContext, FingerprintStyle, SerializedDepNodeIndex};
4646
use crate::ich::StableHashingContext;
4747

4848
use rustc_data_structures::fingerprint::{Fingerprint, PackedFingerprint};
@@ -271,7 +271,8 @@ pub struct DepKindStruct<Tcx: DepContext> {
271271
/// with kind `MirValidated`, we know that the GUID/fingerprint of the `DepNode`
272272
/// is actually a `DefPathHash`, and can therefore just look up the corresponding
273273
/// `DefId` in `tcx.def_path_hash_to_def_id`.
274-
pub force_from_dep_node: Option<fn(tcx: Tcx, dep_node: DepNode) -> bool>,
274+
pub force_from_dep_node:
275+
Option<fn(tcx: Tcx, dep_node: DepNode, prev_index: SerializedDepNodeIndex) -> bool>,
275276

276277
/// Invoke a query to put the on-disk cached value in memory.
277278
pub try_load_from_on_disk_cache: Option<fn(Tcx, DepNode)>,

0 commit comments

Comments
 (0)