Move the query system to rustc_query_impl.

This commit is contained in:
Camille GILLOT 2021-01-19 20:40:16 +01:00
parent 71f749a683
commit 4581d16bcb
17 changed files with 170 additions and 83 deletions

View File

@ -3901,6 +3901,7 @@ dependencies = [
"rustc_passes",
"rustc_plugin_impl",
"rustc_privacy",
"rustc_query_impl",
"rustc_resolve",
"rustc_serialize",
"rustc_session",
@ -4167,6 +4168,29 @@ dependencies = [
"tracing",
]
[[package]]
name = "rustc_query_impl"
version = "0.0.0"
dependencies = [
"measureme",
"rustc-rayon-core",
"rustc_ast",
"rustc_attr",
"rustc_data_structures",
"rustc_errors",
"rustc_feature",
"rustc_hir",
"rustc_index",
"rustc_macros",
"rustc_middle",
"rustc_query_system",
"rustc_serialize",
"rustc_session",
"rustc_span",
"rustc_target",
"tracing",
]
[[package]]
name = "rustc_query_system"
version = "0.0.0"

View File

@ -41,6 +41,7 @@ rustc_lint = { path = "../rustc_lint" }
rustc_errors = { path = "../rustc_errors" }
rustc_plugin_impl = { path = "../rustc_plugin_impl" }
rustc_privacy = { path = "../rustc_privacy" }
rustc_query_impl = { path = "../rustc_query_impl" }
rustc_resolve = { path = "../rustc_resolve" }
rustc_trait_selection = { path = "../rustc_trait_selection" }
rustc_ty_utils = { path = "../rustc_ty_utils" }

View File

@ -21,7 +21,6 @@ use rustc_middle::arena::Arena;
use rustc_middle::dep_graph::DepGraph;
use rustc_middle::middle;
use rustc_middle::middle::cstore::{CrateStore, MetadataLoader, MetadataLoaderDyn};
use rustc_middle::ty::query;
use rustc_middle::ty::query::Providers;
use rustc_middle::ty::{self, GlobalCtxt, ResolverOutputs, TyCtxt};
use rustc_mir as mir;
@ -29,6 +28,7 @@ use rustc_mir_build as mir_build;
use rustc_parse::{parse_crate_from_file, parse_crate_from_source_str};
use rustc_passes::{self, hir_stats, layout_test};
use rustc_plugin_impl as plugin;
use rustc_query_impl::Queries as TcxQueries;
use rustc_resolve::{Resolver, ResolverArenas};
use rustc_session::config::{CrateType, Input, OutputFilenames, OutputType, PpMode, PpSourceMode};
use rustc_session::lint;
@ -762,7 +762,7 @@ pub fn create_global_ctxt<'tcx>(
mut resolver_outputs: ResolverOutputs,
outputs: OutputFilenames,
crate_name: &str,
queries: &'tcx OnceCell<query::Queries<'tcx>>,
queries: &'tcx OnceCell<TcxQueries<'tcx>>,
global_ctxt: &'tcx OnceCell<GlobalCtxt<'tcx>>,
arena: &'tcx WorkerLocal<Arena<'tcx>>,
) -> QueryContext<'tcx> {
@ -791,7 +791,7 @@ pub fn create_global_ctxt<'tcx>(
let max_cnum = crates.iter().map(|c| c.as_usize()).max().unwrap_or(0);
let mut providers = IndexVec::from_elem_n(extern_providers, max_cnum + 1);
providers[LOCAL_CRATE] = local_providers;
queries.get_or_init(|| query::Queries::new(providers, extern_providers))
queries.get_or_init(|| TcxQueries::new(providers, extern_providers))
};
let gcx = sess.time("setup_global_ctxt", || {
@ -805,7 +805,7 @@ pub fn create_global_ctxt<'tcx>(
defs,
dep_graph,
query_result_on_disk_cache,
queries,
queries.as_dyn(),
&crate_name,
&outputs,
)

View File

@ -13,8 +13,8 @@ use rustc_incremental::DepGraphFuture;
use rustc_lint::LintStore;
use rustc_middle::arena::Arena;
use rustc_middle::dep_graph::DepGraph;
use rustc_middle::ty::query;
use rustc_middle::ty::{GlobalCtxt, ResolverOutputs, TyCtxt};
use rustc_query_impl::Queries as TcxQueries;
use rustc_serialize::json;
use rustc_session::config::{self, OutputFilenames, OutputType};
use rustc_session::{output::find_crate_name, Session};
@ -72,7 +72,7 @@ impl<T> Default for Query<T> {
pub struct Queries<'tcx> {
compiler: &'tcx Compiler,
gcx: OnceCell<GlobalCtxt<'tcx>>,
queries: OnceCell<query::Queries<'tcx>>,
queries: OnceCell<TcxQueries<'tcx>>,
arena: WorkerLocal<Arena<'tcx>>,
hir_arena: WorkerLocal<rustc_ast_lowering::Arena<'tcx>>,
@ -429,11 +429,11 @@ impl Compiler {
{
let _prof_timer =
queries.session().prof.generic_activity("self_profile_alloc_query_strings");
gcx.enter(query::alloc_self_profile_query_strings);
gcx.enter(rustc_query_impl::alloc_self_profile_query_strings);
}
if self.session().opts.debugging_opts.query_stats {
gcx.enter(query::print_stats);
gcx.enter(rustc_query_impl::print_stats);
}
}

View File

@ -495,6 +495,7 @@ pub fn rustc_queries(input: TokenStream) -> TokenStream {
}
TokenStream::from(quote! {
#[macro_export]
macro_rules! rustc_query_append {
([$($macro:tt)*][$($other:tt)*]) => {
$($macro)* {
@ -514,11 +515,13 @@ pub fn rustc_queries(input: TokenStream) -> TokenStream {
);
}
}
#[macro_export]
macro_rules! rustc_cached_queries {
($($macro:tt)*) => {
$($macro)*(#cached_queries);
}
}
#[macro_export]
macro_rules! rustc_query_description {
() => { #query_description_stream }
}

View File

@ -966,7 +966,7 @@ pub struct GlobalCtxt<'tcx> {
/// 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 on_disk_cache: Option<OnDiskCache<'tcx>>,
pub queries: &'tcx dyn query::QueryEngine<'tcx>,
pub query_caches: query::QueryCaches<'tcx>,

View File

@ -100,8 +100,6 @@ pub use self::list::List;
pub use self::trait_def::TraitDef;
pub use self::query::queries;
pub use self::consts::{Const, ConstInt, ConstKind, InferConst, ScalarInt};
pub mod _match;

View File

@ -31,13 +31,12 @@ use crate::traits::{self, ImplSource};
use crate::ty::subst::{GenericArg, SubstsRef};
use crate::ty::util::AlwaysRequiresDrop;
use crate::ty::{self, AdtSizedConstraint, CrateInherentImpls, ParamEnvAnd, Ty, TyCtxt};
use rustc_data_structures::fingerprint::Fingerprint;
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap};
use rustc_data_structures::stable_hasher::StableVec;
use rustc_data_structures::steal::Steal;
use rustc_data_structures::svh::Svh;
use rustc_data_structures::sync::Lrc;
use rustc_errors::{Diagnostic, ErrorReported, Handler, Level};
use rustc_errors::{ErrorReported, Handler};
use rustc_hir as hir;
use rustc_hir::def::DefKind;
use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, DefIdSet, LocalDefId};
@ -59,34 +58,12 @@ use std::ops::Deref;
use std::path::PathBuf;
use std::sync::Arc;
#[macro_use]
mod plumbing;
pub use plumbing::QueryCtxt;
use plumbing::QueryStruct;
pub(crate) use rustc_query_system::query::CycleError;
pub(crate) use rustc_query_system::query::QueryJobId;
use rustc_query_system::query::*;
mod stats;
pub use self::stats::print_stats;
pub use rustc_query_system::query::{QueryInfo, QueryJob, QueryJobId};
mod keys;
use self::keys::Key;
mod values;
use self::values::Value;
use rustc_query_system::query::QueryAccessors;
pub use rustc_query_system::query::QueryConfig;
pub(crate) use rustc_query_system::query::QueryDescription;
mod on_disk_cache;
pub mod on_disk_cache;
pub use self::on_disk_cache::OnDiskCache;
mod profiling_support;
pub use self::profiling_support::alloc_self_profile_query_strings;
#[derive(Copy, Clone)]
pub struct TyCtxtAt<'tcx> {
pub tcx: TyCtxt<'tcx>,
@ -131,6 +108,18 @@ macro_rules! query_helper_param_ty {
($K:ty) => { $K };
}
macro_rules! query_storage {
([][$K:ty, $V:ty]) => {
<DefaultCacheSelector as CacheSelector<$K, $V>>::Cache
};
([storage($ty:ty) $($rest:tt)*][$K:ty, $V:ty]) => {
<$ty as CacheSelector<$K, $V>>::Cache
};
([$other:ident $(($($other_args:tt)*))* $(, $($modifiers:tt)*)*][$($args:tt)*]) => {
query_storage!([$($($modifiers)*)*][$($args)*])
};
}
macro_rules! define_callbacks {
(<$tcx:tt>
$($(#[$attr:meta])*
@ -169,7 +158,7 @@ macro_rules! define_callbacks {
#[derive(Default)]
pub struct QueryCaches<$tcx> {
$($(#[$attr])* $name: QueryCacheStore<query_storage::$name<$tcx>>,)*
$($(#[$attr])* pub $name: QueryCacheStore<query_storage::$name<$tcx>>,)*
}
impl TyCtxtEnsure<$tcx> {
@ -288,7 +277,6 @@ macro_rules! define_callbacks {
// Queries marked with `fatal_cycle` do not need the latter implementation,
// as they will raise an fatal error on query cycles instead.
rustc_query_append! { [define_queries!][<'tcx>] }
rustc_query_append! { [define_callbacks!][<'tcx>] }
mod sealed {

View File

@ -502,7 +502,7 @@ impl<'sess> OnDiskCache<'sess> {
/// Returns the cached query result if there is something in the cache for
/// the given `SerializedDepNodeIndex`; otherwise returns `None`.
crate fn try_load_query_result<'tcx, T>(
pub fn try_load_query_result<'tcx, T>(
&self,
tcx: TyCtxt<'tcx>,
dep_node_index: SerializedDepNodeIndex,
@ -665,7 +665,7 @@ impl<'sess> OnDiskCache<'sess> {
/// A decoder that can read from the incremental compilation cache. It is similar to the one
/// we use for crate metadata decoding in that it can rebase spans and eventually
/// will also handle things that contain `Ty` instances.
crate struct CacheDecoder<'a, 'tcx> {
pub struct CacheDecoder<'a, 'tcx> {
tcx: TyCtxt<'tcx>,
opaque: opaque::Decoder<'a>,
source_map: &'a SourceMap,

View File

@ -0,0 +1,27 @@
[package]
authors = ["The Rust Project Developers"]
name = "rustc_query_impl"
version = "0.0.0"
edition = "2018"
[lib]
doctest = false
[dependencies]
measureme = "9.0.0"
rustc-rayon-core = "0.3.0"
tracing = "0.1"
rustc_ast = { path = "../rustc_ast" }
rustc_attr = { path = "../rustc_attr" }
rustc_data_structures = { path = "../rustc_data_structures" }
rustc_errors = { path = "../rustc_errors" }
rustc_feature = { path = "../rustc_feature" }
rustc_hir = { path = "../rustc_hir" }
rustc_index = { path = "../rustc_index" }
rustc_macros = { path = "../rustc_macros" }
rustc_middle = { path = "../rustc_middle" }
rustc_query_system = { path = "../rustc_query_system" }
rustc_span = { path = "../rustc_span" }
rustc_serialize = { path = "../rustc_serialize" }
rustc_session = { path = "../rustc_session" }
rustc_target = { path = "../rustc_target" }

View File

@ -1,11 +1,11 @@
//! Defines the set of legal keys that can be used in queries.
use crate::infer::canonical::Canonical;
use crate::mir;
use crate::ty::fast_reject::SimplifiedType;
use crate::ty::subst::{GenericArg, SubstsRef};
use crate::ty::{self, Ty, TyCtxt};
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, LOCAL_CRATE};
use rustc_middle::infer::canonical::Canonical;
use rustc_middle::mir;
use rustc_middle::ty::fast_reject::SimplifiedType;
use rustc_middle::ty::subst::{GenericArg, SubstsRef};
use rustc_middle::ty::{self, Ty, TyCtxt};
use rustc_span::symbol::{Ident, Symbol};
use rustc_span::{Span, DUMMY_SP};

View File

@ -0,0 +1,65 @@
//! Support for serializing the dep-graph and reloading it.
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
#![feature(in_band_lifetimes)]
#![feature(exhaustive_patterns)]
#![feature(nll)]
#![feature(min_specialization)]
#![feature(crate_visibility_modifier)]
#![feature(once_cell)]
#![feature(rustc_attrs)]
#![feature(never_type)]
#![recursion_limit = "256"]
#[macro_use]
extern crate rustc_middle;
#[macro_use]
extern crate tracing;
use rustc_data_structures::fingerprint::Fingerprint;
use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
use rustc_errors::{Diagnostic, Handler, Level};
use rustc_hir::def_id::CrateNum;
use rustc_index::vec::IndexVec;
use rustc_middle::dep_graph;
use rustc_middle::ich::StableHashingContext;
use rustc_middle::ty::query::{query_keys, query_storage, query_stored, query_values};
use rustc_middle::ty::query::{Providers, QueryEngine};
use rustc_middle::ty::TyCtxt;
use rustc_serialize::opaque;
use rustc_span::{Span, DUMMY_SP};
use std::mem;
#[macro_use]
mod plumbing;
pub use plumbing::QueryCtxt;
use plumbing::QueryStruct;
use rustc_query_system::query::*;
mod stats;
pub use self::stats::print_stats;
mod keys;
use keys::Key;
mod values;
use self::values::Value;
use rustc_query_system::query::QueryAccessors;
pub use rustc_query_system::query::QueryConfig;
pub(crate) use rustc_query_system::query::QueryDescription;
use rustc_middle::ty::query::on_disk_cache;
mod profiling_support;
pub use self::profiling_support::alloc_self_profile_query_strings;
rustc_query_append! { [define_queries!][<'tcx>] }
impl<'tcx> Queries<'tcx> {
// Force codegen in the dyn-trait transformation in this crate.
pub fn as_dyn(&'tcx self) -> &'tcx dyn QueryEngine<'tcx> {
self
}
}

View File

@ -2,10 +2,11 @@
//! generate the actual methods on tcx which find and execute the provider,
//! manage the caches, and so forth.
use crate::dep_graph::{DepKind, DepNode, DepNodeExt, DepNodeIndex, SerializedDepNodeIndex};
use crate::ty::query::{on_disk_cache, queries, Query};
use crate::ty::tls::{self, ImplicitCtxt};
use crate::ty::{self, TyCtxt};
use super::{queries, Query};
use rustc_middle::dep_graph::{DepKind, DepNode, DepNodeExt, DepNodeIndex, SerializedDepNodeIndex};
use rustc_middle::ty::query::on_disk_cache;
use rustc_middle::ty::tls::{self, ImplicitCtxt};
use rustc_middle::ty::{self, TyCtxt};
use rustc_query_system::dep_graph::HasDepContext;
use rustc_query_system::query::{CycleError, QueryJobId, QueryJobInfo};
use rustc_query_system::query::{QueryContext, QueryDescription};
@ -33,8 +34,8 @@ impl<'tcx> std::ops::Deref for QueryCtxt<'tcx> {
}
impl HasDepContext for QueryCtxt<'tcx> {
type DepKind = crate::dep_graph::DepKind;
type StableHashingContext = crate::ich::StableHashingContext<'tcx>;
type DepKind = rustc_middle::dep_graph::DepKind;
type StableHashingContext = rustc_middle::ich::StableHashingContext<'tcx>;
type DepContext = TyCtxt<'tcx>;
#[inline]
@ -251,7 +252,7 @@ impl<'tcx> QueryCtxt<'tcx> {
macro_rules! encode_queries {
($($query:ident,)*) => {
$(
on_disk_cache::encode_query_results::<_, ty::query::queries::$query<'_>>(
on_disk_cache::encode_query_results::<_, super::queries::$query<'_>>(
self,
encoder,
query_result_index
@ -363,18 +364,6 @@ macro_rules! is_eval_always {
};
}
macro_rules! query_storage {
([][$K:ty, $V:ty]) => {
<DefaultCacheSelector as CacheSelector<$K, $V>>::Cache
};
([storage($ty:ty) $($rest:tt)*][$K:ty, $V:ty]) => {
<$ty as CacheSelector<$K, $V>>::Cache
};
([$other:ident $(($($other_args:tt)*))* $(, $($modifiers:tt)*)*][$($args:tt)*]) => {
query_storage!([$($($modifiers)*)*][$($args)*])
};
}
macro_rules! hash_result {
([][$hcx:expr, $result:expr]) => {{
dep_graph::hash_result($hcx, &$result)
@ -392,13 +381,6 @@ macro_rules! define_queries {
$($(#[$attr:meta])*
[$($modifiers:tt)*] fn $name:ident($($K:tt)*) -> $V:ty,)*) => {
use std::mem;
use crate::{
rustc_data_structures::stable_hasher::HashStable,
rustc_data_structures::stable_hasher::StableHasher,
ich::StableHashingContext
};
define_queries_struct! {
tcx: $tcx,
input: ($(([$($modifiers)*] [$($attr)*] [$name]))*)
@ -407,7 +389,7 @@ macro_rules! define_queries {
#[allow(nonstandard_style)]
#[derive(Clone, Debug)]
pub enum Query<$tcx> {
$($(#[$attr])* $name($($K)*)),*
$($(#[$attr])* $name(query_keys::$name<$tcx>)),*
}
impl<$tcx> Query<$tcx> {
@ -465,8 +447,8 @@ macro_rules! define_queries {
}
$(impl<$tcx> QueryConfig for queries::$name<$tcx> {
type Key = $($K)*;
type Value = $V;
type Key = query_keys::$name<$tcx>;
type Value = query_values::$name<$tcx>;
type Stored = query_stored::$name<$tcx>;
const NAME: &'static str = stringify!($name);
}
@ -520,8 +502,8 @@ macro_rules! define_queries {
#[allow(non_upper_case_globals)]
pub mod query_callbacks {
use super::*;
use crate::dep_graph::DepNode;
use crate::ty::query::{queries, query_keys};
use rustc_middle::dep_graph::DepNode;
use rustc_middle::ty::query::query_keys;
use rustc_query_system::dep_graph::DepNodeParams;
use rustc_query_system::query::{force_query, QueryDescription};

View File

@ -1,10 +1,9 @@
use crate::ty::context::TyCtxt;
use crate::ty::WithOptConstParam;
use measureme::{StringComponent, StringId};
use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::profiling::SelfProfiler;
use rustc_hir::def_id::{CrateNum, DefId, DefIndex, LocalDefId, CRATE_DEF_INDEX, LOCAL_CRATE};
use rustc_hir::definitions::DefPathData;
use rustc_middle::ty::{TyCtxt, WithOptConstParam};
use rustc_query_system::query::{QueryCache, QueryCacheStore};
use std::fmt::Debug;
use std::io::Write;

View File

@ -1,6 +1,6 @@
use crate::ty::query::query_storage;
use crate::ty::TyCtxt;
use rustc_hir::def_id::{DefId, LOCAL_CRATE};
use rustc_middle::ty::query::query_storage;
use rustc_middle::ty::TyCtxt;
use rustc_query_system::query::{QueryCache, QueryCacheStore};
use std::any::type_name;

View File

@ -1,5 +1,5 @@
use crate::ty::query::QueryCtxt;
use crate::ty::{self, AdtSizedConstraint, Ty, TyS};
use super::QueryCtxt;
use rustc_middle::ty::{self, AdtSizedConstraint, Ty, TyS};
pub(super) trait Value<'tcx>: Sized {
fn from_cycle_error(tcx: QueryCtxt<'tcx>) -> Self;