Skip to content

Commit 581b166

Browse files
committed
Auto merge of rust-lang#87416 - Aaron1011:query-side-effect, r=cjgillot
Create `QuerySideEffects` and use it for diagnostics The code for saving and loading diagnostics during execution is generalized to handle a new `QuerySideEffects` struct. Currently, this struct just holds diagnostics - in a follow-up PR, I plan to add support for storing attriutes marked as used during query execution. This is a pure refactor, with no intended behavior changes.
2 parents 8523788 + 87740ba commit 581b166

File tree

5 files changed

+102
-117
lines changed

5 files changed

+102
-117
lines changed

compiler/rustc_query_impl/src/on_disk_cache.rs

+38-49
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
use crate::QueryCtxt;
22
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexSet};
33
use rustc_data_structures::sync::{HashMapExt, Lock, Lrc, OnceCell};
4-
use rustc_data_structures::thin_vec::ThinVec;
54
use rustc_data_structures::unhash::UnhashMap;
6-
use rustc_errors::Diagnostic;
75
use rustc_hir::def_id::{CrateNum, DefId, DefIndex, LocalDefId, StableCrateId, LOCAL_CRATE};
86
use rustc_hir::definitions::DefPathHash;
97
use rustc_index::vec::{Idx, IndexVec};
@@ -13,7 +11,7 @@ use rustc_middle::mir::{self, interpret};
1311
use rustc_middle::ty::codec::{RefDecodable, TyDecoder, TyEncoder};
1412
use rustc_middle::ty::{self, Ty, TyCtxt};
1513
use rustc_query_system::dep_graph::DepContext;
16-
use rustc_query_system::query::QueryContext;
14+
use rustc_query_system::query::{QueryContext, QuerySideEffects};
1715
use rustc_serialize::{
1816
opaque::{self, FileEncodeResult, FileEncoder, IntEncodedWithFixedSize},
1917
Decodable, Decoder, Encodable, Encoder,
@@ -41,14 +39,14 @@ const TAG_EXPN_DATA: u8 = 1;
4139
/// Provides an interface to incremental compilation data cached from the
4240
/// previous compilation session. This data will eventually include the results
4341
/// of a few selected queries (like `typeck` and `mir_optimized`) and
44-
/// any diagnostics that have been emitted during a query.
42+
/// any side effects that have been emitted during a query.
4543
pub struct OnDiskCache<'sess> {
4644
// The complete cache data in serialized form.
4745
serialized_data: Vec<u8>,
4846

49-
// Collects all `Diagnostic`s emitted during the current compilation
47+
// Collects all `QuerySideEffects` created during the current compilation
5048
// session.
51-
current_diagnostics: Lock<FxHashMap<DepNodeIndex, Vec<Diagnostic>>>,
49+
current_side_effects: Lock<FxHashMap<DepNodeIndex, QuerySideEffects>>,
5250

5351
cnum_map: OnceCell<UnhashMap<StableCrateId, CrateNum>>,
5452

@@ -62,9 +60,9 @@ pub struct OnDiskCache<'sess> {
6260
// `serialized_data`.
6361
query_result_index: FxHashMap<SerializedDepNodeIndex, AbsoluteBytePos>,
6462

65-
// A map from dep-node to the position of any associated diagnostics in
63+
// A map from dep-node to the position of any associated `QuerySideEffects` in
6664
// `serialized_data`.
67-
prev_diagnostics_index: FxHashMap<SerializedDepNodeIndex, AbsoluteBytePos>,
65+
prev_side_effects_index: FxHashMap<SerializedDepNodeIndex, AbsoluteBytePos>,
6866

6967
alloc_decoding_state: AllocDecodingState,
7068

@@ -113,8 +111,8 @@ pub struct OnDiskCache<'sess> {
113111
#[derive(Encodable, Decodable)]
114112
struct Footer {
115113
file_index_to_stable_id: FxHashMap<SourceFileIndex, EncodedSourceFileId>,
116-
query_result_index: EncodedQueryResultIndex,
117-
diagnostics_index: EncodedQueryResultIndex,
114+
query_result_index: EncodedDepNodeIndex,
115+
side_effects_index: EncodedDepNodeIndex,
118116
// The location of all allocations.
119117
interpret_alloc_index: Vec<u32>,
120118
// See `OnDiskCache.syntax_contexts`
@@ -125,9 +123,7 @@ struct Footer {
125123
foreign_expn_data: UnhashMap<ExpnHash, u32>,
126124
}
127125

128-
pub type EncodedQueryResultIndex = Vec<(SerializedDepNodeIndex, AbsoluteBytePos)>;
129-
type EncodedDiagnosticsIndex = Vec<(SerializedDepNodeIndex, AbsoluteBytePos)>;
130-
type EncodedDiagnostics = Vec<Diagnostic>;
126+
pub type EncodedDepNodeIndex = Vec<(SerializedDepNodeIndex, AbsoluteBytePos)>;
131127

132128
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, Encodable, Decodable)]
133129
struct SourceFileIndex(u32);
@@ -213,9 +209,9 @@ impl<'sess> rustc_middle::ty::OnDiskCache<'sess> for OnDiskCache<'sess> {
213209
file_index_to_file: Default::default(),
214210
cnum_map: OnceCell::new(),
215211
source_map: sess.source_map(),
216-
current_diagnostics: Default::default(),
212+
current_side_effects: Default::default(),
217213
query_result_index: footer.query_result_index.into_iter().collect(),
218-
prev_diagnostics_index: footer.diagnostics_index.into_iter().collect(),
214+
prev_side_effects_index: footer.side_effects_index.into_iter().collect(),
219215
alloc_decoding_state: AllocDecodingState::new(footer.interpret_alloc_index),
220216
syntax_contexts: footer.syntax_contexts,
221217
expn_data: footer.expn_data,
@@ -234,9 +230,9 @@ impl<'sess> rustc_middle::ty::OnDiskCache<'sess> for OnDiskCache<'sess> {
234230
file_index_to_file: Default::default(),
235231
cnum_map: OnceCell::new(),
236232
source_map,
237-
current_diagnostics: Default::default(),
233+
current_side_effects: Default::default(),
238234
query_result_index: Default::default(),
239-
prev_diagnostics_index: Default::default(),
235+
prev_side_effects_index: Default::default(),
240236
alloc_decoding_state: AllocDecodingState::new(Vec::new()),
241237
syntax_contexts: FxHashMap::default(),
242238
expn_data: UnhashMap::default(),
@@ -301,26 +297,24 @@ impl<'sess> rustc_middle::ty::OnDiskCache<'sess> for OnDiskCache<'sess> {
301297
};
302298

303299
// Encode query results.
304-
let mut query_result_index = EncodedQueryResultIndex::new();
300+
let mut query_result_index = EncodedDepNodeIndex::new();
305301

306302
tcx.sess.time("encode_query_results", || -> FileEncodeResult {
307303
let enc = &mut encoder;
308304
let qri = &mut query_result_index;
309305
QueryCtxt::from_tcx(tcx).encode_query_results(enc, qri)
310306
})?;
311307

312-
// Encode diagnostics.
313-
let diagnostics_index: EncodedDiagnosticsIndex = self
314-
.current_diagnostics
308+
// Encode side effects.
309+
let side_effects_index: EncodedDepNodeIndex = self
310+
.current_side_effects
315311
.borrow()
316312
.iter()
317313
.map(
318-
|(dep_node_index, diagnostics)| -> Result<_, <FileEncoder as Encoder>::Error> {
314+
|(dep_node_index, side_effects)| -> Result<_, <FileEncoder as Encoder>::Error> {
319315
let pos = AbsoluteBytePos::new(encoder.position());
320-
// Let's make sure we get the expected type here.
321-
let diagnostics: &EncodedDiagnostics = diagnostics;
322316
let dep_node_index = SerializedDepNodeIndex::new(dep_node_index.index());
323-
encoder.encode_tagged(dep_node_index, diagnostics)?;
317+
encoder.encode_tagged(dep_node_index, side_effects)?;
324318

325319
Ok((dep_node_index, pos))
326320
},
@@ -386,7 +380,7 @@ impl<'sess> rustc_middle::ty::OnDiskCache<'sess> for OnDiskCache<'sess> {
386380
&Footer {
387381
file_index_to_stable_id,
388382
query_result_index,
389-
diagnostics_index,
383+
side_effects_index,
390384
interpret_alloc_index,
391385
syntax_contexts,
392386
expn_data,
@@ -488,30 +482,26 @@ impl<'sess> OnDiskCache<'sess> {
488482
self as _
489483
}
490484

491-
/// Loads a diagnostic emitted during the previous compilation session.
492-
pub fn load_diagnostics(
485+
/// Loads a `QuerySideEffects` created during the previous compilation session.
486+
pub fn load_side_effects(
493487
&self,
494488
tcx: TyCtxt<'_>,
495489
dep_node_index: SerializedDepNodeIndex,
496-
) -> Vec<Diagnostic> {
497-
let diagnostics: Option<EncodedDiagnostics> =
498-
self.load_indexed(tcx, dep_node_index, &self.prev_diagnostics_index, "diagnostics");
490+
) -> QuerySideEffects {
491+
let side_effects: Option<QuerySideEffects> =
492+
self.load_indexed(tcx, dep_node_index, &self.prev_side_effects_index, "side_effects");
499493

500-
diagnostics.unwrap_or_default()
494+
side_effects.unwrap_or_default()
501495
}
502496

503-
/// Stores a diagnostic emitted during the current compilation session.
504-
/// Anything stored like this will be available via `load_diagnostics` in
497+
/// Stores a `QuerySideEffects` emitted during the current compilation session.
498+
/// Anything stored like this will be available via `load_side_effects` in
505499
/// the next compilation session.
506500
#[inline(never)]
507501
#[cold]
508-
pub fn store_diagnostics(
509-
&self,
510-
dep_node_index: DepNodeIndex,
511-
diagnostics: ThinVec<Diagnostic>,
512-
) {
513-
let mut current_diagnostics = self.current_diagnostics.borrow_mut();
514-
let prev = current_diagnostics.insert(dep_node_index, diagnostics.into());
502+
pub fn store_side_effects(&self, dep_node_index: DepNodeIndex, side_effects: QuerySideEffects) {
503+
let mut current_side_effects = self.current_side_effects.borrow_mut();
504+
let prev = current_side_effects.insert(dep_node_index, side_effects);
515505
debug_assert!(prev.is_none());
516506
}
517507

@@ -539,22 +529,21 @@ impl<'sess> OnDiskCache<'sess> {
539529
self.load_indexed(tcx, dep_node_index, &self.query_result_index, "query result")
540530
}
541531

542-
/// Stores a diagnostic emitted during computation of an anonymous query.
532+
/// Stores side effect emitted during computation of an anonymous query.
543533
/// Since many anonymous queries can share the same `DepNode`, we aggregate
544534
/// them -- as opposed to regular queries where we assume that there is a
545535
/// 1:1 relationship between query-key and `DepNode`.
546536
#[inline(never)]
547537
#[cold]
548-
pub fn store_diagnostics_for_anon_node(
538+
pub fn store_side_effects_for_anon_node(
549539
&self,
550540
dep_node_index: DepNodeIndex,
551-
diagnostics: ThinVec<Diagnostic>,
541+
side_effects: QuerySideEffects,
552542
) {
553-
let mut current_diagnostics = self.current_diagnostics.borrow_mut();
554-
555-
let x = current_diagnostics.entry(dep_node_index).or_default();
543+
let mut current_side_effects = self.current_side_effects.borrow_mut();
556544

557-
x.extend(Into::<Vec<_>>::into(diagnostics));
545+
let x = current_side_effects.entry(dep_node_index).or_default();
546+
x.append(side_effects);
558547
}
559548

560549
fn load_indexed<'tcx, T>(
@@ -1155,7 +1144,7 @@ impl<'a, 'tcx> Encodable<CacheEncoder<'a, 'tcx, FileEncoder>> for [u8] {
11551144
pub fn encode_query_results<'a, 'tcx, CTX, Q>(
11561145
tcx: CTX,
11571146
encoder: &mut CacheEncoder<'a, 'tcx, FileEncoder>,
1158-
query_result_index: &mut EncodedQueryResultIndex,
1147+
query_result_index: &mut EncodedDepNodeIndex,
11591148
) -> FileEncodeResult
11601149
where
11611150
CTX: QueryContext + 'tcx,

compiler/rustc_query_impl/src/plumbing.rs

+11-9
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ use rustc_middle::dep_graph::{DepKind, DepNode, DepNodeIndex, SerializedDepNodeI
77
use rustc_middle::ty::tls::{self, ImplicitCtxt};
88
use rustc_middle::ty::{self, TyCtxt};
99
use rustc_query_system::dep_graph::HasDepContext;
10-
use rustc_query_system::query::{QueryContext, QueryDescription, QueryJobId, QueryMap};
10+
use rustc_query_system::query::{
11+
QueryContext, QueryDescription, QueryJobId, QueryMap, QuerySideEffects,
12+
};
1113

1214
use rustc_data_structures::sync::Lock;
1315
use rustc_data_structures::thin_vec::ThinVec;
@@ -83,27 +85,27 @@ impl QueryContext for QueryCtxt<'tcx> {
8385
}
8486

8587
// Interactions with on_disk_cache
86-
fn load_diagnostics(&self, prev_dep_node_index: SerializedDepNodeIndex) -> Vec<Diagnostic> {
88+
fn load_side_effects(&self, prev_dep_node_index: SerializedDepNodeIndex) -> QuerySideEffects {
8789
self.queries
8890
.on_disk_cache
8991
.as_ref()
90-
.map(|c| c.load_diagnostics(**self, prev_dep_node_index))
92+
.map(|c| c.load_side_effects(**self, prev_dep_node_index))
9193
.unwrap_or_default()
9294
}
9395

94-
fn store_diagnostics(&self, dep_node_index: DepNodeIndex, diagnostics: ThinVec<Diagnostic>) {
96+
fn store_side_effects(&self, dep_node_index: DepNodeIndex, side_effects: QuerySideEffects) {
9597
if let Some(c) = self.queries.on_disk_cache.as_ref() {
96-
c.store_diagnostics(dep_node_index, diagnostics)
98+
c.store_side_effects(dep_node_index, side_effects)
9799
}
98100
}
99101

100-
fn store_diagnostics_for_anon_node(
102+
fn store_side_effects_for_anon_node(
101103
&self,
102104
dep_node_index: DepNodeIndex,
103-
diagnostics: ThinVec<Diagnostic>,
105+
side_effects: QuerySideEffects,
104106
) {
105107
if let Some(c) = self.queries.on_disk_cache.as_ref() {
106-
c.store_diagnostics_for_anon_node(dep_node_index, diagnostics)
108+
c.store_side_effects_for_anon_node(dep_node_index, side_effects)
107109
}
108110
}
109111

@@ -163,7 +165,7 @@ impl<'tcx> QueryCtxt<'tcx> {
163165
pub(super) fn encode_query_results(
164166
self,
165167
encoder: &mut on_disk_cache::CacheEncoder<'a, 'tcx, opaque::FileEncoder>,
166-
query_result_index: &mut on_disk_cache::EncodedQueryResultIndex,
168+
query_result_index: &mut on_disk_cache::EncodedDepNodeIndex,
167169
) -> opaque::FileEncodeResult {
168170
macro_rules! encode_queries {
169171
($($query:ident,)*) => {

0 commit comments

Comments
 (0)