Skip to content

Commit

Permalink
Move try_load_from_on_disk_cache to the QueryContext.
Browse files Browse the repository at this point in the history
  • Loading branch information
cjgillot committed Feb 19, 2021
1 parent 4dbf83a commit ea3d465
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 16 deletions.
9 changes: 4 additions & 5 deletions compiler/rustc_middle/src/dep_graph/dep_node.rs
Expand Up @@ -138,7 +138,7 @@ pub struct DepKindStruct {
pub(super) force_from_dep_node: fn(tcx: TyCtxt<'_>, dep_node: &DepNode) -> bool,

/// Invoke a query to put the on-disk cached value in memory.
pub(super) try_load_from_on_disk_cache: fn(TyCtxt<'_>, &DepNode),
pub(crate) try_load_from_on_disk_cache: fn(QueryCtxt<'_>, &DepNode),
}

impl std::ops::Deref for DepKind {
Expand Down Expand Up @@ -273,7 +273,7 @@ pub mod dep_kind {
false
}

fn try_load_from_on_disk_cache(tcx: TyCtxt<'_>, dep_node: &DepNode) {
fn try_load_from_on_disk_cache(tcx: QueryCtxt<'_>, dep_node: &DepNode) {
if is_anon {
return
}
Expand All @@ -287,9 +287,8 @@ pub mod dep_kind {
.map(|c| c.is_green())
.unwrap_or(false));

let key = recover(tcx, dep_node).unwrap_or_else(|| panic!("Failed to recover key for {:?} with hash {}", dep_node, dep_node.hash));
let qcx = QueryCtxt { tcx, queries: tcx.queries };
if queries::$variant::cache_on_disk(qcx, &key, None) {
let key = recover(*tcx, dep_node).unwrap_or_else(|| panic!("Failed to recover key for {:?} with hash {}", dep_node, dep_node.hash));
if queries::$variant::cache_on_disk(tcx, &key, None) {
let _ = tcx.$variant(key);
}
}
Expand Down
4 changes: 0 additions & 4 deletions compiler/rustc_middle/src/dep_graph/mod.rs
Expand Up @@ -180,10 +180,6 @@ impl<'tcx> DepContext for TyCtxt<'tcx> {
}

// Interactions with on_disk_cache
fn try_load_from_on_disk_cache(&self, dep_node: &DepNode) {
(dep_node.kind.try_load_from_on_disk_cache)(*self, dep_node)
}

fn load_diagnostics(&self, prev_dep_node_index: SerializedDepNodeIndex) -> Vec<Diagnostic> {
self.on_disk_cache
.as_ref()
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/ty/query/on_disk_cache.rs
Expand Up @@ -285,7 +285,7 @@ impl<'sess> OnDiskCache<'sess> {
// Do this *before* we clone 'latest_foreign_def_path_hashes', since
// loading existing queries may cause us to create new DepNodes, which
// may in turn end up invoking `store_foreign_def_id_hash`
tcx.dep_graph.exec_cache_promotions(tcx);
tcx.queries.exec_cache_promotions(tcx);

let latest_foreign_def_path_hashes = self.latest_foreign_def_path_hashes.lock().clone();
let hygiene_encode_context = HygieneEncodeContext::default();
Expand Down
9 changes: 9 additions & 0 deletions compiler/rustc_middle/src/ty/query/plumbing.rs
Expand Up @@ -68,6 +68,10 @@ impl QueryContext for QueryCtxt<'tcx> {
self.queries.try_collect_active_jobs()
}

fn try_load_from_on_disk_cache(&self, dep_node: &dep_graph::DepNode) {
(dep_node.kind.try_load_from_on_disk_cache)(*self, dep_node)
}

/// Executes a job by changing the `ImplicitCtxt` to point to the
/// new query job while it executes. It returns the diagnostics
/// captured during execution and the actual result.
Expand Down Expand Up @@ -603,6 +607,11 @@ macro_rules! define_queries_struct {
tcx.encode_query_results(encoder, query_result_index)
}

fn exec_cache_promotions(&'tcx self, tcx: TyCtxt<'tcx>) {
let tcx = QueryCtxt { tcx, queries: self };
tcx.dep_graph.exec_cache_promotions(tcx)
}

$($(#[$attr])*
#[inline(always)]
fn $name(
Expand Down
6 changes: 4 additions & 2 deletions compiler/rustc_query_system/src/dep_graph/graph.rs
Expand Up @@ -24,6 +24,7 @@ use super::prev::PreviousDepGraph;
use super::query::DepGraphQuery;
use super::serialized::SerializedDepNodeIndex;
use super::{DepContext, DepKind, DepNode, HasDepContext, WorkProductId};
use crate::query::QueryContext;

#[derive(Clone)]
pub struct DepGraph<K: DepKind> {
Expand Down Expand Up @@ -875,15 +876,16 @@ impl<K: DepKind> DepGraph<K> {
//
// This method will only load queries that will end up in the disk cache.
// Other queries will not be executed.
pub fn exec_cache_promotions<Ctxt: DepContext<DepKind = K>>(&self, tcx: Ctxt) {
pub fn exec_cache_promotions<Ctxt: QueryContext<DepKind = K>>(&self, qcx: Ctxt) {
let tcx = qcx.dep_context();
let _prof_timer = tcx.profiler().generic_activity("incr_comp_query_cache_promotion");

let data = self.data.as_ref().unwrap();
for prev_index in data.colors.values.indices() {
match data.colors.get(prev_index) {
Some(DepNodeColor::Green(_)) => {
let dep_node = data.previous.index_to_node(prev_index);
tcx.try_load_from_on_disk_cache(&dep_node);
qcx.try_load_from_on_disk_cache(&dep_node);
}
None | Some(DepNodeColor::Red) => {
// We can skip red nodes because a node can only be marked
Expand Down
3 changes: 0 additions & 3 deletions compiler/rustc_query_system/src/dep_graph/mod.rs
Expand Up @@ -43,9 +43,6 @@ pub trait DepContext: Copy {
/// Return the diagnostic handler.
fn diagnostic(&self) -> &rustc_errors::Handler;

/// Load data from the on-disk cache.
fn try_load_from_on_disk_cache(&self, dep_node: &DepNode<Self::DepKind>);

/// Load diagnostics associated to the node in the previous session.
fn load_diagnostics(&self, prev_dep_node_index: SerializedDepNodeIndex) -> Vec<Diagnostic>;

Expand Down
5 changes: 4 additions & 1 deletion compiler/rustc_query_system/src/query/mod.rs
Expand Up @@ -14,7 +14,7 @@ pub use self::caches::{
mod config;
pub use self::config::{QueryAccessors, QueryConfig, QueryDescription};

use crate::dep_graph::HasDepContext;
use crate::dep_graph::{DepNode, HasDepContext};
use crate::query::job::QueryMap;

use rustc_data_structures::stable_hasher::HashStable;
Expand All @@ -37,6 +37,9 @@ pub trait QueryContext: HasDepContext {

fn try_collect_active_jobs(&self) -> Option<QueryMap<Self::DepKind, Self::Query>>;

/// Load data from the on-disk cache.
fn try_load_from_on_disk_cache(&self, dep_node: &DepNode<Self::DepKind>);

/// Executes a job by changing the `ImplicitCtxt` to point to the
/// new query job while it executes. It returns the diagnostics
/// captured during execution and the actual result.
Expand Down

0 comments on commit ea3d465

Please sign in to comment.