Skip to content

Commit 6d818bd

Browse files
committed
Type-erase query caching.
1 parent 253da5d commit 6d818bd

File tree

4 files changed

+80
-22
lines changed

4 files changed

+80
-22
lines changed

compiler/rustc_macros/src/query.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,10 @@ pub fn rustc_queries(input: TokenStream) -> TokenStream {
466466
if modifiers.eval_always {
467467
attributes.push(quote! { eval_always });
468468
};
469+
// Pass on the cache modifier
470+
if modifiers.cache.is_some() {
471+
attributes.push(quote! { cached });
472+
};
469473

470474
let attribute_stream = quote! {#(#attributes),*};
471475
let doc_comments = query.doc_comments.iter();

compiler/rustc_middle/src/dep_graph/dep_node.rs

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, CRATE_DEF_INDEX};
6767
use rustc_hir::definitions::DefPathHash;
6868
use rustc_hir::HirId;
6969
use rustc_query_system::query::QueryAccessors;
70-
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
70+
use rustc_serialize::{opaque, Decodable, Decoder, Encodable, Encoder};
7171
use rustc_span::symbol::Symbol;
7272
use std::hash::Hash;
7373

@@ -84,6 +84,13 @@ pub trait DepKindTrait: std::fmt::Debug + Sync {
8484

8585
fn has_params(&self) -> bool;
8686

87+
fn encode_query_results<'a, 'tcx>(
88+
&self,
89+
tcx: TyCtxt<'tcx>,
90+
encoder: &mut query::on_disk_cache::CacheEncoder<'a, 'tcx, opaque::Encoder>,
91+
query_result_index: &mut query::on_disk_cache::EncodedQueryResultIndex,
92+
);
93+
8794
fn force_from_dep_node(&self, tcx: TyCtxt<'_>, dep_node: &DepNode) -> bool;
8895

8996
fn query_stats(&self, tcx: TyCtxt<'_>) -> Option<query::stats::QueryStats>;
@@ -124,6 +131,19 @@ macro_rules! contains_eval_always_attr {
124131
($($attr:ident $(($($attr_args:tt)*))* ),*) => ({$(is_eval_always_attr!($attr) | )* false});
125132
}
126133

134+
macro_rules! encode_query_results {
135+
([][$variant:ident][$($args:expr),*]) => {{}};
136+
([cached $($rest:tt)*][$variant:ident][$($args:expr),*]) => {{
137+
let ret = query::on_disk_cache::encode_query_results::<
138+
query::queries::$variant<'_>
139+
>($($args),*);
140+
match ret { Ok(()) => (), Err(_) => () }
141+
}};
142+
([$other:ident $(($($other_args:tt)*))* $(, $($modifiers:tt)*)*][$variant:ident][$($args:expr),*]) => {
143+
encode_query_results!([$($($modifiers)*)*][$variant][$($args),*])
144+
};
145+
}
146+
127147
macro_rules! define_dep_kinds {
128148
(<$tcx:tt>
129149
$(
@@ -176,6 +196,16 @@ macro_rules! define_dep_kinds {
176196
false
177197
}
178198

199+
#[inline]
200+
fn encode_query_results<'a, 'tcx>(
201+
&self,
202+
_tcx: TyCtxt<'tcx>,
203+
_encoder: &mut query::on_disk_cache::CacheEncoder<'a, 'tcx, opaque::Encoder>,
204+
_query_result_index: &mut query::on_disk_cache::EncodedQueryResultIndex,
205+
) {
206+
encode_query_results!([$($attrs)*][$variant][_tcx, _encoder, _query_result_index]);
207+
}
208+
179209
#[inline]
180210
fn force_from_dep_node(&self, tcx: TyCtxt<'tcx>, dep_node: &DepNode) -> bool {
181211
use rustc_query_system::query::force_query;
@@ -422,6 +452,15 @@ impl DepKindTrait for dep_kind::Null {
422452
false
423453
}
424454

455+
#[inline]
456+
fn encode_query_results<'a, 'tcx>(
457+
&self,
458+
_tcx: TyCtxt<'tcx>,
459+
_encoder: &mut query::on_disk_cache::CacheEncoder<'a, 'tcx, opaque::Encoder>,
460+
_query_result_index: &mut query::on_disk_cache::EncodedQueryResultIndex,
461+
) {
462+
}
463+
425464
#[inline]
426465
fn force_from_dep_node(&self, _tcx: TyCtxt<'tcx>, _dep_node: &DepNode) -> bool {
427466
// Forcing this makes no sense.
@@ -463,6 +502,15 @@ impl DepKindTrait for dep_kind::CrateMetadata {
463502
true
464503
}
465504

505+
#[inline]
506+
fn encode_query_results<'a, 'tcx>(
507+
&self,
508+
_tcx: TyCtxt<'tcx>,
509+
_encoder: &mut query::on_disk_cache::CacheEncoder<'a, 'tcx, opaque::Encoder>,
510+
_query_result_index: &mut query::on_disk_cache::EncodedQueryResultIndex,
511+
) {
512+
}
513+
466514
#[inline]
467515
fn force_from_dep_node(&self, _tcx: TyCtxt<'tcx>, _dep_node: &DepNode) -> bool {
468516
// These are inputs that are expected to be pre-allocated and that
@@ -509,6 +557,15 @@ impl DepKindTrait for dep_kind::TraitSelect {
509557
false
510558
}
511559

560+
#[inline]
561+
fn encode_query_results<'a, 'tcx>(
562+
&self,
563+
_tcx: TyCtxt<'tcx>,
564+
_encoder: &mut query::on_disk_cache::CacheEncoder<'a, 'tcx, opaque::Encoder>,
565+
_query_result_index: &mut query::on_disk_cache::EncodedQueryResultIndex,
566+
) {
567+
}
568+
512569
#[inline]
513570
fn force_from_dep_node(&self, _tcx: TyCtxt<'tcx>, _dep_node: &DepNode) -> bool {
514571
// These are anonymous nodes.
@@ -555,6 +612,15 @@ impl DepKindTrait for dep_kind::CompileCodegenUnit {
555612
true
556613
}
557614

615+
#[inline]
616+
fn encode_query_results<'a, 'tcx>(
617+
&self,
618+
_tcx: TyCtxt<'tcx>,
619+
_encoder: &mut query::on_disk_cache::CacheEncoder<'a, 'tcx, opaque::Encoder>,
620+
_query_result_index: &mut query::on_disk_cache::EncodedQueryResultIndex,
621+
) {
622+
}
623+
558624
#[inline]
559625
fn force_from_dep_node(&self, _tcx: TyCtxt<'tcx>, _dep_node: &DepNode) -> bool {
560626
// We don't have enough information to reconstruct the query key of these.

compiler/rustc_middle/src/ty/query/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ use rustc_query_system::query::QueryAccessors;
8383
pub use rustc_query_system::query::QueryConfig;
8484
pub(crate) use rustc_query_system::query::QueryDescription;
8585

86-
mod on_disk_cache;
86+
pub mod on_disk_cache;
8787
pub use self::on_disk_cache::OnDiskCache;
8888

8989
mod profiling_support;

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

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -97,15 +97,15 @@ struct Footer {
9797
expn_data: FxHashMap<u32, AbsoluteBytePos>,
9898
}
9999

100-
type EncodedQueryResultIndex = Vec<(SerializedDepNodeIndex, AbsoluteBytePos)>;
100+
pub type EncodedQueryResultIndex = Vec<(SerializedDepNodeIndex, AbsoluteBytePos)>;
101101
type EncodedDiagnosticsIndex = Vec<(SerializedDepNodeIndex, AbsoluteBytePos)>;
102102
type EncodedDiagnostics = Vec<Diagnostic>;
103103

104104
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, Encodable, Decodable)]
105105
struct SourceFileIndex(u32);
106106

107107
#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq, Encodable, Decodable)]
108-
struct AbsoluteBytePos(u32);
108+
pub struct AbsoluteBytePos(u32);
109109

110110
impl AbsoluteBytePos {
111111
fn new(pos: usize) -> AbsoluteBytePos {
@@ -226,22 +226,10 @@ impl<'sess> OnDiskCache<'sess> {
226226
let enc = &mut encoder;
227227
let qri = &mut query_result_index;
228228

229-
macro_rules! encode_queries {
230-
($($query:ident,)*) => {
231-
$(
232-
encode_query_results::<ty::query::queries::$query<'_>>(
233-
tcx,
234-
enc,
235-
qri
236-
)?;
237-
)*
238-
}
229+
for dk in rustc_middle::dep_graph::DEP_KINDS {
230+
dk.encode_query_results(tcx, enc, qri)
239231
}
240-
241-
rustc_cached_queries!(encode_queries!);
242-
243-
Ok(())
244-
})?;
232+
});
245233

246234
// Encode diagnostics.
247235
let diagnostics_index: EncodedDiagnosticsIndex = self
@@ -765,7 +753,7 @@ impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for &'tcx [Span] {
765753
//- ENCODING -------------------------------------------------------------------
766754

767755
/// This trait is a hack to work around specialization bug #55243.
768-
trait OpaqueEncoder: Encoder {
756+
pub trait OpaqueEncoder: Encoder {
769757
fn opaque(&mut self) -> &mut opaque::Encoder;
770758
fn encoder_position(&self) -> usize;
771759
}
@@ -782,7 +770,7 @@ impl OpaqueEncoder for opaque::Encoder {
782770
}
783771

784772
/// An encoder that can write the incr. comp. cache.
785-
struct CacheEncoder<'a, 'tcx, E: OpaqueEncoder> {
773+
pub struct CacheEncoder<'a, 'tcx, E: OpaqueEncoder> {
786774
tcx: TyCtxt<'tcx>,
787775
encoder: &'a mut E,
788776
type_shorthands: FxHashMap<Ty<'tcx>, usize>,
@@ -1005,7 +993,7 @@ impl<'a> Decodable<opaque::Decoder<'a>> for IntEncodedWithFixedSize {
1005993
}
1006994
}
1007995

1008-
fn encode_query_results<'a, 'tcx, Q>(
996+
pub fn encode_query_results<'a, 'tcx, Q>(
1009997
tcx: TyCtxt<'tcx>,
1010998
encoder: &mut CacheEncoder<'a, 'tcx, opaque::Encoder>,
1011999
query_result_index: &mut EncodedQueryResultIndex,

0 commit comments

Comments
 (0)