Skip to content

Commit

Permalink
Auto merge of rust-lang#59722 - Zoxc:query-cache, r=eddyb
Browse files Browse the repository at this point in the history
Clean up query cache code

There are a couple of queries for which we do not promote saved results, which have been removed from caching here. This was likely the cause of the regression in rust-lang#57293 (comment).

r? @michaelwoerister
  • Loading branch information
bors committed Jun 30, 2019
2 parents c06f80a + ede41ab commit 0af8e87
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 184 deletions.
37 changes: 13 additions & 24 deletions src/librustc/dep_graph/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -842,31 +842,20 @@ impl DepGraph {
// 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<'tcx>(&self, tcx: TyCtxt<'tcx>) {
let green_nodes: Vec<DepNode> = {
let data = self.data.as_ref().unwrap();
data.colors.values.indices().filter_map(|prev_index| {
match data.colors.get(prev_index) {
Some(DepNodeColor::Green(_)) => {
let dep_node = data.previous.index_to_node(prev_index);
if dep_node.cache_on_disk(tcx) {
Some(dep_node)
} else {
None
}
}
None |
Some(DepNodeColor::Red) => {
// We can skip red nodes because a node can only be marked
// as red if the query result was recomputed and thus is
// already in memory.
None
}
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);
dep_node.try_load_from_on_disk_cache(tcx);
}
}).collect()
};

for dep_node in green_nodes {
dep_node.load_from_on_disk_cache(tcx);
None |
Some(DepNodeColor::Red) => {
// We can skip red nodes because a node can only be marked
// as red if the query result was recomputed and thus is
// already in memory.
}
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/librustc/mir/interpret/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use errors::DiagnosticBuilder;
use syntax_pos::{Pos, Span};
use syntax::symbol::Symbol;

#[derive(Debug, Copy, Clone, PartialEq, Eq, HashStable)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, HashStable, RustcEncodable, RustcDecodable)]
pub enum ErrorHandled {
/// Already reported a lint or an error for this evaluation.
Reported,
Expand Down
63 changes: 40 additions & 23 deletions src/librustc/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::ty::query::QueryDescription;
use crate::ty::query::queries;
use crate::ty::{self, ParamEnvAnd, Ty, TyCtxt};
use crate::ty::subst::SubstsRef;
use crate::dep_graph::SerializedDepNodeIndex;
use crate::dep_graph::{RecoverKey,DepKind, DepNode, SerializedDepNodeIndex};
use crate::hir::def_id::{CrateNum, DefId, DefIndex};
use crate::mir;
use crate::mir::interpret::GlobalId;
Expand Down Expand Up @@ -33,13 +33,13 @@ rustc_queries! {
Other {
/// Records the type of every item.
query type_of(key: DefId) -> Ty<'tcx> {
cache { key.is_local() }
cache_on_disk_if { key.is_local() }
}

/// Maps from the `DefId` of an item (trait/struct/enum/fn) to its
/// associated generics.
query generics_of(key: DefId) -> &'tcx ty::Generics {
cache { key.is_local() }
cache_on_disk_if { key.is_local() }
load_cached(tcx, id) {
let generics: Option<ty::Generics> = tcx.queries.on_disk_cache
.try_load_query_result(tcx, id);
Expand All @@ -62,7 +62,9 @@ rustc_queries! {
/// predicate gets in the way of some checks, which are intended
/// to operate over only the actual where-clauses written by the
/// user.)
query predicates_of(_: DefId) -> &'tcx ty::GenericPredicates<'tcx> {}
query predicates_of(key: DefId) -> &'tcx ty::GenericPredicates<'tcx> {
cache_on_disk_if { key.is_local() }
}

query native_libraries(_: CrateNum) -> Lrc<Vec<NativeLibrary>> {
desc { "looking up the native libraries of a linked crate" }
Expand Down Expand Up @@ -93,7 +95,7 @@ rustc_queries! {
/// of the MIR qualify_consts pass. The actual meaning of
/// the value isn't known except to the pass itself.
query mir_const_qualif(key: DefId) -> (u8, &'tcx BitSet<mir::Local>) {
cache { key.is_local() }
cache_on_disk_if { key.is_local() }
}

/// Fetch the MIR for a given `DefId` right after it's built - this includes
Expand All @@ -115,7 +117,7 @@ rustc_queries! {
/// MIR after our optimization passes have run. This is MIR that is ready
/// for codegen. This is also the only query that can fetch non-local MIR, at present.
query optimized_mir(key: DefId) -> &'tcx mir::Body<'tcx> {
cache { key.is_local() }
cache_on_disk_if { key.is_local() }
load_cached(tcx, id) {
let mir: Option<crate::mir::Body<'tcx>> = tcx.queries.on_disk_cache
.try_load_query_result(tcx, id);
Expand Down Expand Up @@ -285,7 +287,9 @@ rustc_queries! {

TypeChecking {
/// The result of unsafety-checking this `DefId`.
query unsafety_check_result(_: DefId) -> mir::UnsafetyCheckResult {}
query unsafety_check_result(key: DefId) -> mir::UnsafetyCheckResult {
cache_on_disk_if { key.is_local() }
}

/// HACK: when evaluated, this reports a "unsafe derive on repr(packed)" error
query unsafe_derive_on_repr_packed(_: DefId) -> () {}
Expand Down Expand Up @@ -348,7 +352,7 @@ rustc_queries! {
}

query typeck_tables_of(key: DefId) -> &'tcx ty::TypeckTables<'tcx> {
cache { key.is_local() }
cache_on_disk_if { key.is_local() }
load_cached(tcx, id) {
let typeck_tables: Option<ty::TypeckTables<'tcx>> = tcx
.queries.on_disk_cache
Expand All @@ -360,7 +364,9 @@ rustc_queries! {
}

Other {
query used_trait_imports(_: DefId) -> &'tcx DefIdSet {}
query used_trait_imports(key: DefId) -> &'tcx DefIdSet {
cache_on_disk_if { key.is_local() }
}
}

TypeChecking {
Expand All @@ -372,11 +378,15 @@ rustc_queries! {
}

BorrowChecking {
query borrowck(_: DefId) -> &'tcx BorrowCheckResult {}
query borrowck(key: DefId) -> &'tcx BorrowCheckResult {
cache_on_disk_if { key.is_local() }
}

/// Borrow-checks the function body. If this is a closure, returns
/// additional requirements that the closure's creator must verify.
query mir_borrowck(_: DefId) -> mir::BorrowCheckResult<'tcx> {}
query mir_borrowck(key: DefId) -> mir::BorrowCheckResult<'tcx> {
cache_on_disk_if(tcx, _) { key.is_local() && tcx.is_closure(key) }
}
}

TypeChecking {
Expand Down Expand Up @@ -412,9 +422,10 @@ rustc_queries! {
"const-evaluating `{}`",
tcx.def_path_str(key.value.instance.def.def_id())
}
cache { true }
load_cached(tcx, id) {
tcx.queries.on_disk_cache.try_load_query_result(tcx, id).map(Ok)
cache_on_disk_if(_, opt_result) {
// Only store results without errors
// FIXME: We never store these
opt_result.map_or(true, |r| r.is_ok())
}
}

Expand All @@ -427,9 +438,9 @@ rustc_queries! {
"const-evaluating + checking `{}`",
tcx.def_path_str(key.value.instance.def.def_id())
}
cache { true }
load_cached(tcx, id) {
tcx.queries.on_disk_cache.try_load_query_result(tcx, id).map(Ok)
cache_on_disk_if(_, opt_result) {
// Only store results without errors
opt_result.map_or(true, |r| r.is_ok())
}
}

Expand All @@ -453,7 +464,9 @@ rustc_queries! {
}

TypeChecking {
query check_match(_: DefId) -> () {}
query check_match(key: DefId) -> () {
cache_on_disk_if { key.is_local() }
}

/// Performs part of the privacy check and computes "access levels".
query privacy_access_levels(_: CrateNum) -> &'tcx AccessLevels {
Expand Down Expand Up @@ -483,7 +496,7 @@ rustc_queries! {
query symbol_name(key: ty::Instance<'tcx>) -> ty::SymbolName {
no_force
desc { "computing the symbol for `{}`", key }
cache { true }
cache_on_disk_if { true }
}

query def_kind(_: DefId) -> Option<DefKind> {}
Expand All @@ -501,7 +514,9 @@ rustc_queries! {
}

Codegen {
query codegen_fn_attrs(_: DefId) -> CodegenFnAttrs {}
query codegen_fn_attrs(_: DefId) -> CodegenFnAttrs {
cache_on_disk_if { true }
}
}

Other {
Expand All @@ -519,7 +534,7 @@ rustc_queries! {
"const checking if rvalue is promotable to static `{}`",
tcx.def_path_str(key)
}
cache { true }
cache_on_disk_if { true }
}
query rvalue_promotable_map(key: DefId) -> &'tcx ItemLocalSet {
desc { |tcx|
Expand Down Expand Up @@ -548,7 +563,7 @@ rustc_queries! {
key: (ty::ParamEnv<'tcx>, ty::PolyTraitRef<'tcx>)
) -> Vtable<'tcx, ()> {
no_force
cache { true }
cache_on_disk_if { true }
desc { |tcx|
"checking if `{}` fulfills its obligations",
tcx.def_path_str(key.1.def_id())
Expand All @@ -560,7 +575,9 @@ rustc_queries! {
query trait_impls_of(key: DefId) -> &'tcx ty::trait_def::TraitImpls {
desc { |tcx| "trait impls of `{}`", tcx.def_path_str(key) }
}
query specialization_graph_of(_: DefId) -> &'tcx specialization_graph::Graph {}
query specialization_graph_of(_: DefId) -> &'tcx specialization_graph::Graph {
cache_on_disk_if { true }
}
query is_object_safe(key: DefId) -> bool {
desc { |tcx| "determine object safety of trait `{}`", tcx.def_path_str(key) }
}
Expand Down
32 changes: 1 addition & 31 deletions src/librustc/ty/query/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub(crate) trait QueryDescription<'tcx>: QueryAccessors<'tcx> {
fn describe(tcx: TyCtxt<'_>, key: Self::Key) -> Cow<'static, str>;

#[inline]
fn cache_on_disk(_: TyCtxt<'tcx>, _: Self::Key) -> bool {
fn cache_on_disk(_: TyCtxt<'tcx>, _: Self::Key, _: Option<&Self::Value>) -> bool {
false
}

Expand All @@ -80,33 +80,3 @@ impl<'tcx> QueryDescription<'tcx> for queries::analysis<'tcx> {
"running analysis passes on this crate".into()
}
}

macro_rules! impl_disk_cacheable_query(
($query_name:ident, |$tcx:tt, $key:tt| $cond:expr) => {
impl<'tcx> QueryDescription<'tcx> for queries::$query_name<'tcx> {
#[inline]
fn cache_on_disk($tcx: TyCtxt<'tcx>, $key: Self::Key) -> bool {
$cond
}

#[inline]
fn try_load_from_disk(tcx: TyCtxt<'tcx>,
id: SerializedDepNodeIndex)
-> Option<Self::Value> {
tcx.queries.on_disk_cache.try_load_query_result(tcx, id)
}
}
}
);

impl_disk_cacheable_query!(mir_borrowck, |tcx, def_id| {
def_id.is_local() && tcx.is_closure(def_id)
});

impl_disk_cacheable_query!(unsafety_check_result, |_, def_id| def_id.is_local());
impl_disk_cacheable_query!(borrowck, |_, def_id| def_id.is_local());
impl_disk_cacheable_query!(check_match, |_, def_id| def_id.is_local());
impl_disk_cacheable_query!(predicates_of, |_, def_id| def_id.is_local());
impl_disk_cacheable_query!(used_trait_imports, |_, def_id| def_id.is_local());
impl_disk_cacheable_query!(codegen_fn_attrs, |_, _| true);
impl_disk_cacheable_query!(specialization_graph_of, |_, _| true);
24 changes: 3 additions & 21 deletions src/librustc/ty/query/on_disk_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,26 +221,8 @@ impl<'sess> OnDiskCache<'sess> {
encode_query_results::<check_match<'_>, _>(tcx, enc, qri)?;
encode_query_results::<codegen_fn_attrs<'_>, _>(tcx, enc, qri)?;
encode_query_results::<specialization_graph_of<'_>, _>(tcx, enc, qri)?;

// const eval is special, it only encodes successfully evaluated constants
use crate::ty::query::QueryAccessors;
let cache = const_eval::query_cache(tcx).borrow();
assert!(cache.active.is_empty());
for (key, entry) in cache.results.iter() {
use crate::ty::query::config::QueryDescription;
if const_eval::cache_on_disk(tcx, key.clone()) {
if let Ok(ref value) = entry.value {
let dep_node = SerializedDepNodeIndex::new(entry.index.index());

// Record position of the cache entry
qri.push((dep_node, AbsoluteBytePos::new(enc.position())));

// Encode the type check tables with the SerializedDepNodeIndex
// as tag.
enc.encode_tagged(dep_node, value)?;
}
}
}
encode_query_results::<const_eval<'_>, _>(tcx, enc, qri)?;
// FIXME: Include const_eval_raw?

Ok(())
})?;
Expand Down Expand Up @@ -1090,7 +1072,7 @@ where
let map = Q::query_cache(tcx).borrow();
assert!(map.active.is_empty());
for (key, entry) in map.results.iter() {
if Q::cache_on_disk(tcx, key.clone()) {
if Q::cache_on_disk(tcx, key.clone(), Some(&entry.value)) {
let dep_node = SerializedDepNodeIndex::new(entry.index.index());

// Record position of the cache entry
Expand Down
Loading

0 comments on commit 0af8e87

Please sign in to comment.