Skip to content

Commit

Permalink
Add load_cached query modifier and keep dep node names consistent wit…
Browse files Browse the repository at this point in the history
…h query names
  • Loading branch information
Zoxc committed Mar 18, 2019
1 parent 7d90547 commit 9e9d03f
Show file tree
Hide file tree
Showing 17 changed files with 315 additions and 308 deletions.
2 changes: 0 additions & 2 deletions src/librustc/dep_graph/dep_node.rs
Expand Up @@ -492,8 +492,6 @@ rustc_dep_node_append!([define_dep_nodes!][ <'tcx>
// table in the tcx (or elsewhere) maps to one of these
// nodes.
[] AssociatedItems(DefId),
[] GenericsOfItem(DefId),
[] PredicatesOfItem(DefId),
[] ExplicitPredicatesOfItem(DefId),
[] PredicatesDefinedOnItem(DefId),
[] InferredOutlivesOf(DefId),
Expand Down
29 changes: 29 additions & 0 deletions src/librustc/query/mod.rs
@@ -1,6 +1,7 @@
use crate::ty::query::QueryDescription;
use crate::ty::query::queries;
use crate::ty::TyCtxt;
use crate::ty;
use crate::hir::def_id::CrateNum;
use crate::dep_graph::SerializedDepNodeIndex;
use std::borrow::Cow;
Expand All @@ -23,6 +24,34 @@ rustc_queries! {
cache { 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() }
load_cached(tcx, id) {
let generics: Option<ty::Generics> = tcx.queries.on_disk_cache
.try_load_query_result(tcx, id);
generics.map(|x| tcx.alloc_generics(x))
}
}

/// Maps from the `DefId` of an item (trait/struct/enum/fn) to the
/// predicates (where-clauses) that must be proven true in order
/// to reference it. This is almost always the "predicates query"
/// that you want.
///
/// `predicates_of` builds on `predicates_defined_on` -- in fact,
/// it is almost always the same as that query, except for the
/// case of traits. For traits, `predicates_of` contains
/// an additional `Self: Trait<...>` predicate that users don't
/// actually write. This reflects the fact that to invoke the
/// trait (e.g., via `Default::default`) you must supply types
/// that actually implement the trait. (However, this extra
/// 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) -> Lrc<ty::GenericPredicates<'tcx>> {}

query native_libraries(_: CrateNum) -> Lrc<Vec<NativeLibrary>> {
desc { "looking up the native libraries of a linked crate" }
}
Expand Down
15 changes: 0 additions & 15 deletions src/librustc/ty/query/config.rs
Expand Up @@ -937,21 +937,6 @@ impl<'tcx> QueryDescription<'tcx> for queries::instance_def_size_estimate<'tcx>
}
}

impl<'tcx> QueryDescription<'tcx> for queries::generics_of<'tcx> {
#[inline]
fn cache_on_disk(_: TyCtxt<'_, 'tcx, 'tcx>, def_id: Self::Key) -> bool {
def_id.is_local()
}

fn try_load_from_disk<'a>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
id: SerializedDepNodeIndex)
-> Option<Self::Value> {
let generics: Option<ty::Generics> = tcx.queries.on_disk_cache
.try_load_query_result(tcx, id);
generics.map(|x| tcx.alloc_generics(x))
}
}

impl<'tcx> QueryDescription<'tcx> for queries::program_clauses_for<'tcx> {
fn describe(_tcx: TyCtxt<'_, '_, '_>, _: DefId) -> Cow<'static, str> {
"generating chalk-style clauses".into()
Expand Down
21 changes: 0 additions & 21 deletions src/librustc/ty/query/mod.rs
Expand Up @@ -104,27 +104,6 @@ rustc_query_append! { [define_queries!][ <'tcx>
/// Run analysis passes on the crate
[] fn analysis: Analysis(CrateNum) -> Result<(), ErrorReported>,

/// Maps from the `DefId` of an item (trait/struct/enum/fn) to its
/// associated generics.
[] fn generics_of: GenericsOfItem(DefId) -> &'tcx ty::Generics,

/// Maps from the `DefId` of an item (trait/struct/enum/fn) to the
/// predicates (where-clauses) that must be proven true in order
/// to reference it. This is almost always the "predicates query"
/// that you want.
///
/// `predicates_of` builds on `predicates_defined_on` -- in fact,
/// it is almost always the same as that query, except for the
/// case of traits. For traits, `predicates_of` contains
/// an additional `Self: Trait<...>` predicate that users don't
/// actually write. This reflects the fact that to invoke the
/// trait (e.g., via `Default::default`) you must supply types
/// that actually implement the trait. (However, this extra
/// predicate gets in the way of some checks, which are intended
/// to operate over only the actual where-clauses written by the
/// user.)
[] fn predicates_of: PredicatesOfItem(DefId) -> Lrc<ty::GenericPredicates<'tcx>>,

/// Maps from the `DefId` of an item (trait/struct/enum/fn) to the
/// predicates (where-clauses) directly defined on it. This is
/// equal to the `explicit_predicates_of` predicates plus the
Expand Down
8 changes: 3 additions & 5 deletions src/librustc/ty/query/plumbing.rs
Expand Up @@ -1285,8 +1285,6 @@ pub fn force_from_dep_node<'tcx>(
DepKind::MirKeys => { force!(mir_keys, LOCAL_CRATE); }
DepKind::CrateVariances => { force!(crate_variances, LOCAL_CRATE); }
DepKind::AssociatedItems => { force!(associated_item, def_id!()); }
DepKind::GenericsOfItem => { force!(generics_of, def_id!()); }
DepKind::PredicatesOfItem => { force!(predicates_of, def_id!()); }
DepKind::PredicatesDefinedOnItem => { force!(predicates_defined_on, def_id!()); }
DepKind::ExplicitPredicatesOfItem => { force!(explicit_predicates_of, def_id!()); }
DepKind::InferredOutlivesOf => { force!(inferred_outlives_of, def_id!()); }
Expand Down Expand Up @@ -1501,9 +1499,9 @@ impl_load_from_cache!(
SymbolName => def_symbol_name,
ConstIsRvaluePromotableToStatic => const_is_rvalue_promotable_to_static,
CheckMatch => check_match,
TypeOf => type_of,
GenericsOfItem => generics_of,
PredicatesOfItem => predicates_of,
type_of => type_of,
generics_of => generics_of,
predicates_of => predicates_of,
UsedTraitImports => used_trait_imports,
CodegenFnAttrs => codegen_fn_attrs,
SpecializationGraph => specialization_graph_of,
Expand Down
22 changes: 11 additions & 11 deletions src/librustc_incremental/persist/dirty_clean.rs
Expand Up @@ -36,16 +36,16 @@ const CFG: &str = "cfg";

/// For typedef, constants, and statics
const BASE_CONST: &[&str] = &[
label_strs::TypeOf,
label_strs::type_of,
];

/// DepNodes for functions + methods
const BASE_FN: &[&str] = &[
// Callers will depend on the signature of these items, so we better test
label_strs::FnSignature,
label_strs::GenericsOfItem,
label_strs::PredicatesOfItem,
label_strs::TypeOf,
label_strs::generics_of,
label_strs::predicates_of,
label_strs::type_of,

// And a big part of compilation (that we eventually want to cache) is type inference
// information:
Expand All @@ -62,7 +62,7 @@ const BASE_HIR: &[&str] = &[
/// `impl` implementation of struct/trait
const BASE_IMPL: &[&str] = &[
label_strs::AssociatedItemDefIds,
label_strs::GenericsOfItem,
label_strs::generics_of,
label_strs::ImplTraitRef,
];

Expand All @@ -78,17 +78,17 @@ const BASE_MIR: &[&str] = &[
/// Note that changing the type of a field does not change the type of the struct or enum, but
/// adding/removing fields or changing a fields name or visibility does.
const BASE_STRUCT: &[&str] = &[
label_strs::GenericsOfItem,
label_strs::PredicatesOfItem,
label_strs::TypeOf,
label_strs::generics_of,
label_strs::predicates_of,
label_strs::type_of,
];

/// Trait definition `DepNode`s.
const BASE_TRAIT_DEF: &[&str] = &[
label_strs::AssociatedItemDefIds,
label_strs::GenericsOfItem,
label_strs::generics_of,
label_strs::ObjectSafety,
label_strs::PredicatesOfItem,
label_strs::predicates_of,
label_strs::SpecializationGraph,
label_strs::TraitDefOfItem,
label_strs::TraitImpls,
Expand Down Expand Up @@ -179,7 +179,7 @@ const LABELS_TRAIT: &[&[&str]] = &[
// Fields are kind of separate from their containers, as they can change independently from
// them. We should at least check
//
// TypeOf for these.
// type_of for these.

type Labels = FxHashSet<String>;

Expand Down
78 changes: 48 additions & 30 deletions src/librustc_macros/src/query.rs
@@ -1,7 +1,7 @@
use proc_macro::TokenStream;
use proc_macro2::Span;
use syn::{
Token, Ident, Type, Attribute, ReturnType, Expr,
Token, Ident, Type, Attribute, ReturnType, Expr, Block,
braced, parenthesized, parse_macro_input,
};
use syn::parse::{Result, Parse, ParseStream};
Expand All @@ -25,6 +25,7 @@ impl Parse for IdentOrWild {
enum QueryAttribute {
Desc(Option<Ident>, Punctuated<Expr, Token![,]>),
Cache(Option<Ident>, Expr),
LoadCached(Ident, Ident, Block),
FatalCycle,
}

Expand Down Expand Up @@ -63,6 +64,17 @@ impl Parse for QueryAttribute {
panic!("unexpected tokens in block");
};
Ok(QueryAttribute::Cache(tcx, expr))
} else if attr == "load_cached" {
let args;
parenthesized!(args in input);
let tcx = args.parse()?;
args.parse::<Token![,]>()?;
let id = args.parse()?;
if !args.is_empty() {
panic!("unexpected tokens in arguments");
};
let block = input.parse()?;
Ok(QueryAttribute::LoadCached(tcx, id, block))
} else if attr == "fatal_cycle" {
Ok(QueryAttribute::FatalCycle)
} else {
Expand Down Expand Up @@ -153,22 +165,6 @@ impl Parse for Group {
}
}

fn camel_case(string: &str) -> String {
let mut pos = vec![0];
for (i, c) in string.chars().enumerate() {
if c == '_' {
pos.push(i + 1);
}
}
string.chars().enumerate().filter(|c| c.1 != '_').flat_map(|(i, c)| {
if pos.contains(&i) {
c.to_uppercase().collect::<Vec<char>>()
} else {
vec![c]
}
}).collect()
}

pub fn rustc_queries(input: TokenStream) -> TokenStream {
let groups = parse_macro_input!(input as List<Group>);

Expand All @@ -181,9 +177,6 @@ pub fn rustc_queries(input: TokenStream) -> TokenStream {
let mut group_stream = quote! {};
for query in &group.queries.0 {
let name = &query.name;
let dep_node_name = Ident::new(
&camel_case(&name.to_string()),
name.span());
let arg = &query.arg;
let key = &query.key.0;
let result_full = &query.result;
Expand All @@ -192,35 +185,60 @@ pub fn rustc_queries(input: TokenStream) -> TokenStream {
_ => quote! { #result_full },
};

let load_cached = query.attrs.0.iter().find_map(|attr| match attr {
QueryAttribute::LoadCached(tcx, id, block) => Some((tcx, id, block)),
_ => None,
});

// Find out if we should cache the query on disk
let cache = query.attrs.0.iter().find_map(|attr| match attr {
QueryAttribute::Cache(tcx, expr) => Some((tcx, expr)),
_ => None,
}).map(|(tcx, expr)| {
let try_load_from_disk = if let Some((tcx, id, block)) = load_cached {
quote! {
#[inline]
fn try_load_from_disk(
#tcx: TyCtxt<'_, 'tcx, 'tcx>,
#id: SerializedDepNodeIndex
) -> Option<Self::Value> {
#block
}
}
} else {
quote! {
#[inline]
fn try_load_from_disk(
tcx: TyCtxt<'_, 'tcx, 'tcx>,
id: SerializedDepNodeIndex
) -> Option<Self::Value> {
tcx.queries.on_disk_cache.try_load_query_result(tcx, id)
}
}
};

let tcx = tcx.as_ref().map(|t| quote! { #t }).unwrap_or(quote! { _ });
quote! {
#[inline]
fn cache_on_disk(#tcx: TyCtxt<'_, 'tcx, 'tcx>, #key: Self::Key) -> bool {
#expr
}

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

if cache.is_none() && load_cached.is_some() {
panic!("load_cached modifier on query `{}` without a cache modifier", name);
}

let fatal_cycle = query.attrs.0.iter().find_map(|attr| match attr {
QueryAttribute::FatalCycle => Some(()),
_ => None,
}).map(|_| quote! { fatal_cycle }).unwrap_or(quote! {});

group_stream.extend(quote! {
[#fatal_cycle] fn #name: #dep_node_name(#arg) #result,
[#fatal_cycle] fn #name: #name(#arg) #result,
});

let desc = query.attrs.0.iter().find_map(|attr| match attr {
Expand Down Expand Up @@ -251,10 +269,10 @@ pub fn rustc_queries(input: TokenStream) -> TokenStream {
}

dep_node_def_stream.extend(quote! {
[] #dep_node_name(#arg),
[] #name(#arg),
});
dep_node_force_stream.extend(quote! {
DepKind::#dep_node_name => {
DepKind::#name => {
if let Some(key) = RecoverKey::recover($tcx, $dep_node) {
force_ex!($tcx, #name, key);
} else {
Expand Down
8 changes: 4 additions & 4 deletions src/test/incremental/hashes/consts.rs
Expand Up @@ -29,7 +29,7 @@ pub const CONST_VISIBILITY: u8 = 0;
const CONST_CHANGE_TYPE_1: i32 = 0;

#[cfg(not(cfail1))]
#[rustc_clean(cfg="cfail2", except="Hir,HirBody,TypeOf")]
#[rustc_clean(cfg="cfail2", except="Hir,HirBody,type_of")]
#[rustc_clean(cfg="cfail3")]
const CONST_CHANGE_TYPE_1: u32 = 0;

Expand All @@ -39,7 +39,7 @@ const CONST_CHANGE_TYPE_1: u32 = 0;
const CONST_CHANGE_TYPE_2: Option<u32> = None;

#[cfg(not(cfail1))]
#[rustc_clean(cfg="cfail2", except="Hir,HirBody,TypeOf")]
#[rustc_clean(cfg="cfail2", except="Hir,HirBody,type_of")]
#[rustc_clean(cfg="cfail3")]
const CONST_CHANGE_TYPE_2: Option<u64> = None;

Expand Down Expand Up @@ -99,11 +99,11 @@ mod const_change_type_indirectly {
#[cfg(not(cfail1))]
use super::ReferencedType2 as Type;

#[rustc_clean(cfg="cfail2", except="Hir,HirBody,TypeOf")]
#[rustc_clean(cfg="cfail2", except="Hir,HirBody,type_of")]
#[rustc_clean(cfg="cfail3")]
const CONST_CHANGE_TYPE_INDIRECTLY_1: Type = Type;

#[rustc_clean(cfg="cfail2", except="Hir,HirBody,TypeOf")]
#[rustc_clean(cfg="cfail2", except="Hir,HirBody,type_of")]
#[rustc_clean(cfg="cfail3")]
const CONST_CHANGE_TYPE_INDIRECTLY_2: Option<Type> = None;
}

0 comments on commit 9e9d03f

Please sign in to comment.