Decouple the on-disk cache from the query engine.

This commit is contained in:
Camille GILLOT 2021-01-04 23:38:20 +01:00
parent 49c1b07a9e
commit dab9b89221
7 changed files with 20 additions and 31 deletions

View File

@ -97,7 +97,7 @@ impl Parse for QueryModifier {
Ok(QueryModifier::Cache(args, block))
} else if modifier == "load_cached" {
// Parse a load_cached modifier like:
// `load_cached(tcx, id) { tcx.queries.on_disk_cache.try_load_query_result(tcx, id) }`
// `load_cached(tcx, id) { tcx.on_disk_cache.try_load_query_result(tcx, id) }`
let args;
parenthesized!(args in input);
let tcx = args.parse()?;
@ -368,7 +368,7 @@ fn add_query_description_impl(
tcx: TyCtxt<'tcx>,
id: SerializedDepNodeIndex
) -> Option<Self::Value> {
tcx.queries.on_disk_cache.as_ref().and_then(|c| c.try_load_query_result(tcx, id))
tcx.on_disk_cache.as_ref()?.try_load_query_result(tcx, id)
}
}
};

View File

@ -414,10 +414,7 @@ impl DepNodeExt for DepNode {
/// has been removed.
fn extract_def_id(&self, tcx: TyCtxt<'tcx>) -> Option<DefId> {
if self.kind.can_reconstruct_query_key() {
tcx.queries
.on_disk_cache
.as_ref()?
.def_path_hash_to_def_id(tcx, DefPathHash(self.hash.into()))
tcx.on_disk_cache.as_ref()?.def_path_hash_to_def_id(tcx, DefPathHash(self.hash.into()))
} else {
None
}
@ -472,7 +469,7 @@ impl<'tcx> DepNodeParams<TyCtxt<'tcx>> for DefId {
// we will use the old DefIndex as an initial guess for
// a lookup into the crate metadata.
if !self.is_local() {
if let Some(cache) = &tcx.queries.on_disk_cache {
if let Some(cache) = &tcx.on_disk_cache {
cache.store_foreign_def_id_hash(*self, hash);
}
}

View File

@ -94,7 +94,7 @@ impl<'tcx> DepContext for TyCtxt<'tcx> {
type StableHashingContext = StableHashingContext<'tcx>;
fn register_reused_dep_node(&self, dep_node: &DepNode) {
if let Some(cache) = self.queries.on_disk_cache.as_ref() {
if let Some(cache) = self.on_disk_cache.as_ref() {
cache.register_reused_dep_node(*self, dep_node)
}
}
@ -185,15 +185,14 @@ impl<'tcx> DepContext for TyCtxt<'tcx> {
}
fn load_diagnostics(&self, prev_dep_node_index: SerializedDepNodeIndex) -> Vec<Diagnostic> {
self.queries
.on_disk_cache
self.on_disk_cache
.as_ref()
.map(|c| c.load_diagnostics(*self, prev_dep_node_index))
.unwrap_or_default()
}
fn store_diagnostics(&self, dep_node_index: DepNodeIndex, diagnostics: ThinVec<Diagnostic>) {
if let Some(c) = self.queries.on_disk_cache.as_ref() {
if let Some(c) = self.on_disk_cache.as_ref() {
c.store_diagnostics(dep_node_index, diagnostics)
}
}
@ -203,7 +202,7 @@ impl<'tcx> DepContext for TyCtxt<'tcx> {
dep_node_index: DepNodeIndex,
diagnostics: ThinVec<Diagnostic>,
) {
if let Some(c) = self.queries.on_disk_cache.as_ref() {
if let Some(c) = self.on_disk_cache.as_ref() {
c.store_diagnostics_for_anon_node(dep_node_index, diagnostics)
}
}

View File

@ -125,11 +125,6 @@ rustc_queries! {
desc { |tcx| "computing generics of `{}`", tcx.def_path_str(key) }
storage(ArenaCacheSelector<'tcx>)
cache_on_disk_if { key.is_local() }
load_cached(tcx, id) {
let generics: Option<ty::Generics> = tcx.queries.on_disk_cache.as_ref()
.and_then(|c| c.try_load_query_result(tcx, id));
generics
}
}
/// Maps from the `DefId` of an item (trait/struct/enum/fn) to the
@ -702,7 +697,7 @@ rustc_queries! {
cache_on_disk_if { true }
load_cached(tcx, id) {
let typeck_results: Option<ty::TypeckResults<'tcx>> = tcx
.queries.on_disk_cache.as_ref()
.on_disk_cache.as_ref()
.and_then(|c| c.try_load_query_result(tcx, id));
typeck_results.map(|x| &*tcx.arena.alloc(x))

View File

@ -14,7 +14,7 @@ use crate::middle::stability;
use crate::mir::interpret::{self, Allocation, ConstValue, Scalar};
use crate::mir::{Body, Field, Local, Place, PlaceElem, ProjectionKind, Promoted};
use crate::traits;
use crate::ty::query::{self, TyCtxtAt};
use crate::ty::query::{self, OnDiskCache, TyCtxtAt};
use crate::ty::subst::{GenericArg, GenericArgKind, InternalSubsts, Subst, SubstsRef, UserSubsts};
use crate::ty::TyKind::*;
use crate::ty::{
@ -962,6 +962,12 @@ pub struct GlobalCtxt<'tcx> {
pub(crate) untracked_crate: &'tcx hir::Crate<'tcx>,
pub(crate) definitions: &'tcx Definitions,
/// This provides access to the incremental compilation on-disk cache for query results.
/// Do not access this directly. It is only meant to be used by
/// `DepGraph::try_mark_green()` and the query infrastructure.
/// This is `None` if we are not incremental compilation mode
pub(crate) on_disk_cache: Option<OnDiskCache<'tcx>>,
pub queries: query::Queries<'tcx>,
pub query_caches: query::QueryCaches<'tcx>,
@ -1110,7 +1116,7 @@ impl<'tcx> TyCtxt<'tcx> {
krate: &'tcx hir::Crate<'tcx>,
definitions: &'tcx Definitions,
dep_graph: DepGraph,
on_disk_query_result_cache: Option<query::OnDiskCache<'tcx>>,
on_disk_cache: Option<query::OnDiskCache<'tcx>>,
crate_name: &str,
output_filenames: &OutputFilenames,
) -> GlobalCtxt<'tcx> {
@ -1154,7 +1160,8 @@ impl<'tcx> TyCtxt<'tcx> {
extern_prelude: resolutions.extern_prelude,
untracked_crate: krate,
definitions,
queries: query::Queries::new(providers, extern_providers, on_disk_query_result_cache),
on_disk_cache,
queries: query::Queries::new(providers, extern_providers),
query_caches: query::QueryCaches::default(),
ty_rcache: Default::default(),
pred_rcache: Default::default(),
@ -1320,7 +1327,7 @@ impl<'tcx> TyCtxt<'tcx> {
}
pub fn serialize_query_result_cache(self, encoder: &mut FileEncoder) -> FileEncodeResult {
self.queries.on_disk_cache.as_ref().map_or(Ok(()), |c| c.serialize(self, encoder))
self.on_disk_cache.as_ref().map_or(Ok(()), |c| c.serialize(self, encoder))
}
/// If `true`, we should use the MIR-based borrowck, but also

View File

@ -918,7 +918,6 @@ impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for DefId {
// which means that the definition with this hash is guaranteed to
// still exist in the current compilation session.
Ok(d.tcx()
.queries
.on_disk_cache
.as_ref()
.unwrap()

View File

@ -534,12 +534,6 @@ macro_rules! define_queries_struct {
(tcx: $tcx:tt,
input: ($(([$($modifiers:tt)*] [$($attr:tt)*] [$name:ident]))*)) => {
pub struct Queries<$tcx> {
/// This provides access to the incremental compilation on-disk cache for query results.
/// Do not access this directly. It is only meant to be used by
/// `DepGraph::try_mark_green()` and the query infrastructure.
/// This is `None` if we are not incremental compilation mode
pub(crate) on_disk_cache: Option<OnDiskCache<'tcx>>,
providers: IndexVec<CrateNum, Providers>,
fallback_extern_providers: Box<Providers>,
@ -554,12 +548,10 @@ macro_rules! define_queries_struct {
pub(crate) fn new(
providers: IndexVec<CrateNum, Providers>,
fallback_extern_providers: Providers,
on_disk_cache: Option<OnDiskCache<'tcx>>,
) -> Self {
Queries {
providers,
fallback_extern_providers: Box::new(fallback_extern_providers),
on_disk_cache,
$($name: Default::default()),*
}
}