Skip to content

Commit

Permalink
Move the query system to rustc_query_impl.
Browse files Browse the repository at this point in the history
  • Loading branch information
cjgillot committed Feb 19, 2021
1 parent 71f749a commit 4581d16
Show file tree
Hide file tree
Showing 17 changed files with 170 additions and 83 deletions.
24 changes: 24 additions & 0 deletions Cargo.lock
Expand Up @@ -3901,6 +3901,7 @@ dependencies = [
"rustc_passes",
"rustc_plugin_impl",
"rustc_privacy",
"rustc_query_impl",
"rustc_resolve",
"rustc_serialize",
"rustc_session",
Expand Down Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_interface/Cargo.toml
Expand Up @@ -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" }
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_interface/src/passes.rs
Expand Up @@ -21,14 +21,14 @@ 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;
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;
Expand Down Expand Up @@ -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> {
Expand Down Expand Up @@ -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", || {
Expand All @@ -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,
)
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_interface/src/queries.rs
Expand Up @@ -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};
Expand Down Expand Up @@ -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>>,
Expand Down Expand Up @@ -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);
}
}

Expand Down
3 changes: 3 additions & 0 deletions compiler/rustc_macros/src/query.rs
Expand Up @@ -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)* {
Expand All @@ -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 }
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/ty/context.rs
Expand Up @@ -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>,
Expand Down
2 changes: 0 additions & 2 deletions compiler/rustc_middle/src/ty/mod.rs
Expand Up @@ -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;
Expand Down
44 changes: 16 additions & 28 deletions compiler/rustc_middle/src/ty/query/mod.rs
Expand Up @@ -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};
Expand All @@ -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>,
Expand Down Expand Up @@ -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])*
Expand Down Expand Up @@ -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> {
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_middle/src/ty/query/on_disk_cache.rs
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
27 changes: 27 additions & 0 deletions compiler/rustc_query_impl/Cargo.toml
@@ -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" }
File renamed without changes.
@@ -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};

Expand Down
65 changes: 65 additions & 0 deletions compiler/rustc_query_impl/src/lib.rs
@@ -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
}
}

0 comments on commit 4581d16

Please sign in to comment.