diff options
Diffstat (limited to 'compiler/rustc_query_impl/src')
| -rw-r--r-- | compiler/rustc_query_impl/src/keys.rs | 545 | ||||
| -rw-r--r-- | compiler/rustc_query_impl/src/lib.rs | 21 | ||||
| -rw-r--r-- | compiler/rustc_query_impl/src/on_disk_cache.rs | 29 | ||||
| -rw-r--r-- | compiler/rustc_query_impl/src/plumbing.rs | 263 | ||||
| -rw-r--r-- | compiler/rustc_query_impl/src/profiling_support.rs | 74 |
5 files changed, 247 insertions, 685 deletions
diff --git a/compiler/rustc_query_impl/src/keys.rs b/compiler/rustc_query_impl/src/keys.rs deleted file mode 100644 index 49175e97f41..00000000000 --- a/compiler/rustc_query_impl/src/keys.rs +++ /dev/null @@ -1,545 +0,0 @@ -//! Defines the set of legal keys that can be used in queries. - -use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, LOCAL_CRATE}; -use rustc_middle::infer::canonical::Canonical; -use rustc_middle::mir; -use rustc_middle::traits; -use rustc_middle::ty::fast_reject::SimplifiedType; -use rustc_middle::ty::subst::{GenericArg, SubstsRef}; -use rustc_middle::ty::{self, layout::TyAndLayout, Ty, TyCtxt}; -use rustc_span::symbol::{Ident, Symbol}; -use rustc_span::{Span, DUMMY_SP}; - -/// The `Key` trait controls what types can legally be used as the key -/// for a query. -pub trait Key { - /// Given an instance of this key, what crate is it referring to? - /// This is used to find the provider. - fn query_crate_is_local(&self) -> bool; - - /// In the event that a cycle occurs, if no explicit span has been - /// given for a query with key `self`, what span should we use? - fn default_span(&self, tcx: TyCtxt<'_>) -> Span; - - /// If the key is a [`DefId`] or `DefId`--equivalent, return that `DefId`. - /// Otherwise, return `None`. - fn key_as_def_id(&self) -> Option<DefId> { - None - } -} - -impl Key for () { - #[inline(always)] - fn query_crate_is_local(&self) -> bool { - true - } - - fn default_span(&self, _: TyCtxt<'_>) -> Span { - DUMMY_SP - } -} - -impl<'tcx> Key for ty::InstanceDef<'tcx> { - #[inline(always)] - fn query_crate_is_local(&self) -> bool { - true - } - - fn default_span(&self, tcx: TyCtxt<'_>) -> Span { - tcx.def_span(self.def_id()) - } -} - -impl<'tcx> Key for ty::Instance<'tcx> { - #[inline(always)] - fn query_crate_is_local(&self) -> bool { - true - } - - fn default_span(&self, tcx: TyCtxt<'_>) -> Span { - tcx.def_span(self.def_id()) - } -} - -impl<'tcx> Key for mir::interpret::GlobalId<'tcx> { - #[inline(always)] - fn query_crate_is_local(&self) -> bool { - true - } - - fn default_span(&self, tcx: TyCtxt<'_>) -> Span { - self.instance.default_span(tcx) - } -} - -impl<'tcx> Key for (Ty<'tcx>, Option<ty::PolyExistentialTraitRef<'tcx>>) { - #[inline(always)] - fn query_crate_is_local(&self) -> bool { - true - } - - fn default_span(&self, _: TyCtxt<'_>) -> Span { - DUMMY_SP - } -} - -impl<'tcx> Key for mir::interpret::LitToConstInput<'tcx> { - #[inline(always)] - fn query_crate_is_local(&self) -> bool { - true - } - - fn default_span(&self, _tcx: TyCtxt<'_>) -> Span { - DUMMY_SP - } -} - -impl Key for CrateNum { - #[inline(always)] - fn query_crate_is_local(&self) -> bool { - *self == LOCAL_CRATE - } - fn default_span(&self, _: TyCtxt<'_>) -> Span { - DUMMY_SP - } -} - -impl Key for LocalDefId { - #[inline(always)] - fn query_crate_is_local(&self) -> bool { - true - } - fn default_span(&self, tcx: TyCtxt<'_>) -> Span { - self.to_def_id().default_span(tcx) - } - fn key_as_def_id(&self) -> Option<DefId> { - Some(self.to_def_id()) - } -} - -impl Key for DefId { - #[inline(always)] - fn query_crate_is_local(&self) -> bool { - self.krate == LOCAL_CRATE - } - fn default_span(&self, tcx: TyCtxt<'_>) -> Span { - tcx.def_span(*self) - } - #[inline(always)] - fn key_as_def_id(&self) -> Option<DefId> { - Some(*self) - } -} - -impl Key for ty::WithOptConstParam<LocalDefId> { - #[inline(always)] - fn query_crate_is_local(&self) -> bool { - true - } - fn default_span(&self, tcx: TyCtxt<'_>) -> Span { - self.did.default_span(tcx) - } -} - -impl Key for SimplifiedType { - #[inline(always)] - fn query_crate_is_local(&self) -> bool { - true - } - fn default_span(&self, _: TyCtxt<'_>) -> Span { - DUMMY_SP - } -} - -impl Key for (DefId, DefId) { - #[inline(always)] - fn query_crate_is_local(&self) -> bool { - self.0.krate == LOCAL_CRATE - } - fn default_span(&self, tcx: TyCtxt<'_>) -> Span { - self.1.default_span(tcx) - } -} - -impl<'tcx> Key for (ty::Instance<'tcx>, LocalDefId) { - #[inline(always)] - fn query_crate_is_local(&self) -> bool { - true - } - fn default_span(&self, tcx: TyCtxt<'_>) -> Span { - self.0.default_span(tcx) - } -} - -impl Key for (DefId, LocalDefId) { - #[inline(always)] - fn query_crate_is_local(&self) -> bool { - self.0.krate == LOCAL_CRATE - } - fn default_span(&self, tcx: TyCtxt<'_>) -> Span { - self.1.default_span(tcx) - } -} - -impl Key for (LocalDefId, DefId) { - #[inline(always)] - fn query_crate_is_local(&self) -> bool { - true - } - fn default_span(&self, tcx: TyCtxt<'_>) -> Span { - self.0.default_span(tcx) - } -} - -impl Key for (LocalDefId, LocalDefId) { - #[inline(always)] - fn query_crate_is_local(&self) -> bool { - true - } - fn default_span(&self, tcx: TyCtxt<'_>) -> Span { - self.0.default_span(tcx) - } -} - -impl Key for (DefId, Option<Ident>) { - #[inline(always)] - fn query_crate_is_local(&self) -> bool { - self.0.krate == LOCAL_CRATE - } - fn default_span(&self, tcx: TyCtxt<'_>) -> Span { - tcx.def_span(self.0) - } - #[inline(always)] - fn key_as_def_id(&self) -> Option<DefId> { - Some(self.0) - } -} - -impl Key for (DefId, LocalDefId, Ident) { - #[inline(always)] - fn query_crate_is_local(&self) -> bool { - self.0.krate == LOCAL_CRATE - } - fn default_span(&self, tcx: TyCtxt<'_>) -> Span { - self.1.default_span(tcx) - } -} - -impl Key for (CrateNum, DefId) { - #[inline(always)] - fn query_crate_is_local(&self) -> bool { - self.0 == LOCAL_CRATE - } - fn default_span(&self, tcx: TyCtxt<'_>) -> Span { - self.1.default_span(tcx) - } -} - -impl Key for (CrateNum, SimplifiedType) { - #[inline(always)] - fn query_crate_is_local(&self) -> bool { - self.0 == LOCAL_CRATE - } - fn default_span(&self, _: TyCtxt<'_>) -> Span { - DUMMY_SP - } -} - -impl Key for (DefId, SimplifiedType) { - #[inline(always)] - fn query_crate_is_local(&self) -> bool { - self.0.krate == LOCAL_CRATE - } - fn default_span(&self, tcx: TyCtxt<'_>) -> Span { - self.0.default_span(tcx) - } -} - -impl<'tcx> Key for SubstsRef<'tcx> { - #[inline(always)] - fn query_crate_is_local(&self) -> bool { - true - } - fn default_span(&self, _: TyCtxt<'_>) -> Span { - DUMMY_SP - } -} - -impl<'tcx> Key for (DefId, SubstsRef<'tcx>) { - #[inline(always)] - fn query_crate_is_local(&self) -> bool { - self.0.krate == LOCAL_CRATE - } - fn default_span(&self, tcx: TyCtxt<'_>) -> Span { - self.0.default_span(tcx) - } -} - -impl<'tcx> Key for (ty::Unevaluated<'tcx, ()>, ty::Unevaluated<'tcx, ()>) { - #[inline(always)] - fn query_crate_is_local(&self) -> bool { - (self.0).def.did.krate == LOCAL_CRATE - } - fn default_span(&self, tcx: TyCtxt<'_>) -> Span { - (self.0).def.did.default_span(tcx) - } -} - -impl<'tcx> Key for (LocalDefId, DefId, SubstsRef<'tcx>) { - #[inline(always)] - fn query_crate_is_local(&self) -> bool { - true - } - fn default_span(&self, tcx: TyCtxt<'_>) -> Span { - self.0.default_span(tcx) - } -} - -impl<'tcx> Key for (ty::ParamEnv<'tcx>, ty::PolyTraitRef<'tcx>) { - #[inline(always)] - fn query_crate_is_local(&self) -> bool { - self.1.def_id().krate == LOCAL_CRATE - } - fn default_span(&self, tcx: TyCtxt<'_>) -> Span { - tcx.def_span(self.1.def_id()) - } -} - -impl<'tcx> Key for (ty::Const<'tcx>, mir::Field) { - #[inline(always)] - fn query_crate_is_local(&self) -> bool { - true - } - fn default_span(&self, _: TyCtxt<'_>) -> Span { - DUMMY_SP - } -} - -impl<'tcx> Key for mir::interpret::ConstAlloc<'tcx> { - #[inline(always)] - fn query_crate_is_local(&self) -> bool { - true - } - fn default_span(&self, _: TyCtxt<'_>) -> Span { - DUMMY_SP - } -} - -impl<'tcx> Key for ty::PolyTraitRef<'tcx> { - #[inline(always)] - fn query_crate_is_local(&self) -> bool { - self.def_id().krate == LOCAL_CRATE - } - fn default_span(&self, tcx: TyCtxt<'_>) -> Span { - tcx.def_span(self.def_id()) - } -} - -impl<'tcx> Key for ty::PolyExistentialTraitRef<'tcx> { - #[inline(always)] - fn query_crate_is_local(&self) -> bool { - self.def_id().krate == LOCAL_CRATE - } - fn default_span(&self, tcx: TyCtxt<'_>) -> Span { - tcx.def_span(self.def_id()) - } -} - -impl<'tcx> Key for (ty::PolyTraitRef<'tcx>, ty::PolyTraitRef<'tcx>) { - #[inline(always)] - fn query_crate_is_local(&self) -> bool { - self.0.def_id().krate == LOCAL_CRATE - } - fn default_span(&self, tcx: TyCtxt<'_>) -> Span { - tcx.def_span(self.0.def_id()) - } -} - -impl<'tcx> Key for GenericArg<'tcx> { - #[inline(always)] - fn query_crate_is_local(&self) -> bool { - true - } - fn default_span(&self, _: TyCtxt<'_>) -> Span { - DUMMY_SP - } -} - -impl<'tcx> Key for mir::ConstantKind<'tcx> { - #[inline(always)] - fn query_crate_is_local(&self) -> bool { - true - } - fn default_span(&self, _: TyCtxt<'_>) -> Span { - DUMMY_SP - } -} - -impl<'tcx> Key for ty::Const<'tcx> { - #[inline(always)] - fn query_crate_is_local(&self) -> bool { - true - } - fn default_span(&self, _: TyCtxt<'_>) -> Span { - DUMMY_SP - } -} - -impl<'tcx> Key for Ty<'tcx> { - #[inline(always)] - fn query_crate_is_local(&self) -> bool { - true - } - fn default_span(&self, _: TyCtxt<'_>) -> Span { - DUMMY_SP - } -} - -impl<'tcx> Key for TyAndLayout<'tcx> { - #[inline(always)] - fn query_crate_is_local(&self) -> bool { - true - } - fn default_span(&self, _: TyCtxt<'_>) -> Span { - DUMMY_SP - } -} - -impl<'tcx> Key for (Ty<'tcx>, Ty<'tcx>) { - #[inline(always)] - fn query_crate_is_local(&self) -> bool { - true - } - fn default_span(&self, _: TyCtxt<'_>) -> Span { - DUMMY_SP - } -} - -impl<'tcx> Key for &'tcx ty::List<ty::Predicate<'tcx>> { - #[inline(always)] - fn query_crate_is_local(&self) -> bool { - true - } - fn default_span(&self, _: TyCtxt<'_>) -> Span { - DUMMY_SP - } -} - -impl<'tcx> Key for ty::ParamEnv<'tcx> { - #[inline(always)] - fn query_crate_is_local(&self) -> bool { - true - } - fn default_span(&self, _: TyCtxt<'_>) -> Span { - DUMMY_SP - } -} - -impl<'tcx, T: Key> Key for ty::ParamEnvAnd<'tcx, T> { - #[inline(always)] - fn query_crate_is_local(&self) -> bool { - self.value.query_crate_is_local() - } - fn default_span(&self, tcx: TyCtxt<'_>) -> Span { - self.value.default_span(tcx) - } -} - -impl Key for Symbol { - #[inline(always)] - fn query_crate_is_local(&self) -> bool { - true - } - fn default_span(&self, _tcx: TyCtxt<'_>) -> Span { - DUMMY_SP - } -} - -impl Key for Option<Symbol> { - #[inline(always)] - fn query_crate_is_local(&self) -> bool { - true - } - fn default_span(&self, _tcx: TyCtxt<'_>) -> Span { - DUMMY_SP - } -} - -/// Canonical query goals correspond to abstract trait operations that -/// are not tied to any crate in particular. -impl<'tcx, T> Key for Canonical<'tcx, T> { - #[inline(always)] - fn query_crate_is_local(&self) -> bool { - true - } - - fn default_span(&self, _tcx: TyCtxt<'_>) -> Span { - DUMMY_SP - } -} - -impl Key for (Symbol, u32, u32) { - #[inline(always)] - fn query_crate_is_local(&self) -> bool { - true - } - - fn default_span(&self, _tcx: TyCtxt<'_>) -> Span { - DUMMY_SP - } -} - -impl<'tcx> Key for (DefId, Ty<'tcx>, SubstsRef<'tcx>, ty::ParamEnv<'tcx>) { - #[inline(always)] - fn query_crate_is_local(&self) -> bool { - true - } - - fn default_span(&self, _tcx: TyCtxt<'_>) -> Span { - DUMMY_SP - } -} - -impl<'tcx> Key for (ty::Predicate<'tcx>, traits::WellFormedLoc) { - #[inline(always)] - fn query_crate_is_local(&self) -> bool { - true - } - - fn default_span(&self, _tcx: TyCtxt<'_>) -> Span { - DUMMY_SP - } -} - -impl<'tcx> Key for (ty::PolyFnSig<'tcx>, &'tcx ty::List<Ty<'tcx>>) { - #[inline(always)] - fn query_crate_is_local(&self) -> bool { - true - } - - fn default_span(&self, _: TyCtxt<'_>) -> Span { - DUMMY_SP - } -} - -impl<'tcx> Key for (ty::Instance<'tcx>, &'tcx ty::List<Ty<'tcx>>) { - #[inline(always)] - fn query_crate_is_local(&self) -> bool { - true - } - - fn default_span(&self, tcx: TyCtxt<'_>) -> Span { - self.0.default_span(tcx) - } -} - -impl<'tcx> Key for (Ty<'tcx>, ty::ValTree<'tcx>) { - #[inline(always)] - fn query_crate_is_local(&self) -> bool { - true - } - - fn default_span(&self, _: TyCtxt<'_>) -> Span { - DUMMY_SP - } -} diff --git a/compiler/rustc_query_impl/src/lib.rs b/compiler/rustc_query_impl/src/lib.rs index 26d397f70e0..d426a2b6b78 100644 --- a/compiler/rustc_query_impl/src/lib.rs +++ b/compiler/rustc_query_impl/src/lib.rs @@ -1,6 +1,8 @@ //! Support for serializing the dep-graph and reloading it. #![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")] +// this shouldn't be necessary, but the check for `&mut _` is too naive and denies returning a function pointer that takes a mut ref +#![feature(const_mut_refs)] #![feature(min_specialization)] #![feature(never_type)] #![feature(once_cell)] @@ -17,11 +19,11 @@ extern crate rustc_middle; use rustc_data_structures::sync::AtomicU64; use rustc_middle::arena::Arena; -use rustc_middle::dep_graph::{self, DepKindStruct, SerializedDepNodeIndex}; +use rustc_middle::dep_graph::{self, DepKindStruct}; +use rustc_middle::query::Key; use rustc_middle::ty::query::{query_keys, query_storage, query_stored, query_values}; use rustc_middle::ty::query::{ExternProviders, Providers, QueryEngine}; -use rustc_middle::ty::{self, TyCtxt}; -use rustc_span::def_id::{LocalDefId, LOCAL_CRATE}; +use rustc_middle::ty::TyCtxt; use rustc_span::Span; #[macro_use] @@ -31,11 +33,8 @@ use rustc_query_system::query::*; #[cfg(parallel_compiler)] pub use rustc_query_system::query::{deadlock, QueryContext}; -mod keys; -use keys::Key; - pub use rustc_query_system::query::QueryConfig; -pub(crate) use rustc_query_system::query::{QueryDescription, QueryVTable}; +pub(crate) use rustc_query_system::query::QueryVTable; mod on_disk_cache; pub use on_disk_cache::OnDiskCache; @@ -43,14 +42,6 @@ pub use on_disk_cache::OnDiskCache; mod profiling_support; pub use self::profiling_support::alloc_self_profile_query_strings; -fn describe_as_module(def_id: LocalDefId, tcx: TyCtxt<'_>) -> String { - if def_id.is_top_level_module() { - "top-level module".to_string() - } else { - format!("module `{}`", tcx.def_path_str(def_id.to_def_id())) - } -} - rustc_query_append! { define_queries! } impl<'tcx> Queries<'tcx> { diff --git a/compiler/rustc_query_impl/src/on_disk_cache.rs b/compiler/rustc_query_impl/src/on_disk_cache.rs index 5ef95911f56..2bcfdab03c8 100644 --- a/compiler/rustc_query_impl/src/on_disk_cache.rs +++ b/compiler/rustc_query_impl/src/on_disk_cache.rs @@ -1,8 +1,9 @@ use crate::QueryCtxt; -use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexSet}; +use rustc_data_structures::fx::{FxHashMap, FxIndexSet}; use rustc_data_structures::memmap::Mmap; use rustc_data_structures::sync::{HashMapExt, Lock, Lrc, RwLock}; use rustc_data_structures::unhash::UnhashMap; +use rustc_data_structures::unord::UnordSet; use rustc_hir::def_id::{CrateNum, DefId, DefIndex, LocalDefId, StableCrateId, LOCAL_CRATE}; use rustc_hir::definitions::DefPathHash; use rustc_index::vec::{Idx, IndexVec}; @@ -118,12 +119,11 @@ pub type EncodedDepNodeIndex = Vec<(SerializedDepNodeIndex, AbsoluteBytePos)>; struct SourceFileIndex(u32); #[derive(Copy, Clone, Debug, Hash, Eq, PartialEq, Encodable, Decodable)] -pub struct AbsoluteBytePos(u32); +pub struct AbsoluteBytePos(u64); impl AbsoluteBytePos { fn new(pos: usize) -> AbsoluteBytePos { - debug_assert!(pos <= u32::MAX as usize); - AbsoluteBytePos(pos as u32) + AbsoluteBytePos(pos.try_into().expect("Incremental cache file size overflowed u64.")) } fn to_usize(self) -> usize { @@ -227,7 +227,7 @@ impl<'sess> rustc_middle::ty::OnDiskCache<'sess> for OnDiskCache<'sess> { *self.serialized_data.write() = None; } - fn serialize<'tcx>(&self, tcx: TyCtxt<'tcx>, encoder: FileEncoder) -> FileEncodeResult { + fn serialize(&self, tcx: TyCtxt<'_>, encoder: FileEncoder) -> FileEncodeResult { // Serializing the `DepGraph` should not modify it. tcx.dep_graph.with_ignore(|| { // Allocate `SourceFileIndex`es. @@ -792,7 +792,13 @@ impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for DefId { } } -impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for &'tcx FxHashSet<LocalDefId> { +impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for &'tcx UnordSet<LocalDefId> { + fn decode(d: &mut CacheDecoder<'a, 'tcx>) -> Self { + RefDecodable::decode(d) + } +} + +impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for &'tcx FxHashMap<DefId, Ty<'tcx>> { fn decode(d: &mut CacheDecoder<'a, 'tcx>) -> Self { RefDecodable::decode(d) } @@ -806,13 +812,13 @@ impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> } } -impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for &'tcx [ty::abstract_const::Node<'tcx>] { +impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for &'tcx [(ty::Predicate<'tcx>, Span)] { fn decode(d: &mut CacheDecoder<'a, 'tcx>) -> Self { RefDecodable::decode(d) } } -impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for &'tcx [(ty::Predicate<'tcx>, Span)] { +impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for &'tcx [(ty::Clause<'tcx>, Span)] { fn decode(d: &mut CacheDecoder<'a, 'tcx>) -> Self { RefDecodable::decode(d) } @@ -842,6 +848,7 @@ impl_ref_decoder! {<'tcx> rustc_span::def_id::DefId, rustc_span::def_id::LocalDefId, (rustc_middle::middle::exported_symbols::ExportedSymbol<'tcx>, rustc_middle::middle::exported_symbols::SymbolExportInfo), + ty::DeducedParamAttrs, } //- ENCODING ------------------------------------------------------------------- @@ -958,7 +965,7 @@ impl<'a, 'tcx> Encodable<CacheEncoder<'a, 'tcx>> for Symbol { s.emit_str(self.as_str()); } Entry::Occupied(o) => { - let x = o.get().clone(); + let x = *o.get(); s.emit_u8(SYMBOL_OFFSET); s.emit_usize(x); } @@ -1055,13 +1062,13 @@ pub fn encode_query_results<'a, 'tcx, CTX, Q>( query_result_index: &mut EncodedDepNodeIndex, ) where CTX: QueryContext + 'tcx, - Q: super::QueryDescription<CTX>, + Q: super::QueryConfig<CTX>, Q::Value: Encodable<CacheEncoder<'a, 'tcx>>, { let _timer = tcx .dep_context() .profiler() - .extra_verbose_generic_activity("encode_query_results_for", std::any::type_name::<Q>()); + .verbose_generic_activity_with_arg("encode_query_results_for", std::any::type_name::<Q>()); assert!(Q::query_state(tcx).all_inactive()); let cache = Q::query_cache(tcx); diff --git a/compiler/rustc_query_impl/src/plumbing.rs b/compiler/rustc_query_impl/src/plumbing.rs index 96679281f07..9ffcc5672cc 100644 --- a/compiler/rustc_query_impl/src/plumbing.rs +++ b/compiler/rustc_query_impl/src/plumbing.rs @@ -2,7 +2,8 @@ //! generate the actual methods on tcx which find and execute the provider, //! manage the caches, and so forth. -use crate::keys::Key; +use crate::on_disk_cache::{CacheDecoder, CacheEncoder, EncodedDepNodeIndex}; +use crate::profiling_support::QueryKeyStringCache; use crate::{on_disk_cache, Queries}; use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; use rustc_data_structures::sync::{AtomicU64, Lock}; @@ -10,15 +11,18 @@ use rustc_errors::{Diagnostic, Handler}; use rustc_middle::dep_graph::{ self, DepKind, DepKindStruct, DepNode, DepNodeIndex, SerializedDepNodeIndex, }; +use rustc_middle::query::Key; use rustc_middle::ty::tls::{self, ImplicitCtxt}; use rustc_middle::ty::{self, TyCtxt}; use rustc_query_system::dep_graph::{DepNodeParams, HasDepContext}; use rustc_query_system::ich::StableHashingContext; use rustc_query_system::query::{ - force_query, QueryConfig, QueryContext, QueryDescription, QueryJobId, QueryMap, - QuerySideEffects, QueryStackFrame, + force_query, QueryConfig, QueryContext, QueryJobId, QueryMap, QuerySideEffects, QueryStackFrame, }; -use rustc_query_system::Value; +use rustc_query_system::{LayoutOfDepth, QueryOverflow, Value}; +use rustc_serialize::Decodable; +use rustc_session::Limit; +use rustc_span::def_id::LOCAL_CRATE; use std::any::Any; use std::num::NonZeroU64; use thin_vec::ThinVec; @@ -62,7 +66,7 @@ impl QueryContext for QueryCtxt<'_> { tls::with_related_context(**self, |icx| icx.query) } - fn try_collect_active_jobs(&self) -> Option<QueryMap> { + fn try_collect_active_jobs(&self) -> Option<QueryMap<DepKind>> { self.queries.try_collect_active_jobs(**self) } @@ -107,7 +111,7 @@ impl QueryContext for QueryCtxt<'_> { // when accessing the `ImplicitCtxt`. tls::with_related_context(**self, move |current_icx| { if depth_limit && !self.recursion_limit().value_within_limit(current_icx.query_depth) { - self.depth_limit_error(); + self.depth_limit_error(token); } // Update the `ImplicitCtxt` to point to our new query job. @@ -125,6 +129,29 @@ impl QueryContext for QueryCtxt<'_> { }) }) } + + fn depth_limit_error(&self, job: QueryJobId) { + let mut span = None; + let mut layout_of_depth = None; + if let Some(map) = self.try_collect_active_jobs() { + if let Some((info, depth)) = job.try_find_layout_root(map) { + span = Some(info.job.span); + layout_of_depth = Some(LayoutOfDepth { desc: info.query.description, depth }); + } + } + + let suggested_limit = match self.recursion_limit() { + Limit(0) => Limit(2), + limit => limit * 2, + }; + + self.sess.emit_fatal(QueryOverflow { + span, + layout_of_depth, + suggested_limit, + crate_name: self.crate_name(LOCAL_CRATE), + }); + } } impl<'tcx> QueryCtxt<'tcx> { @@ -146,22 +173,14 @@ impl<'tcx> QueryCtxt<'tcx> { pub(super) fn encode_query_results( self, - encoder: &mut on_disk_cache::CacheEncoder<'_, 'tcx>, - query_result_index: &mut on_disk_cache::EncodedDepNodeIndex, + encoder: &mut CacheEncoder<'_, 'tcx>, + query_result_index: &mut EncodedDepNodeIndex, ) { - macro_rules! encode_queries { - ($($query:ident,)*) => { - $( - on_disk_cache::encode_query_results::<_, super::queries::$query<'_>>( - self, - encoder, - query_result_index - ); - )* + for query in &self.queries.query_structs { + if let Some(encode) = query.encode_query_results { + encode(self, encoder, query_result_index); } } - - rustc_cached_queries!(encode_queries!); } pub fn try_print_query_stack( @@ -174,6 +193,14 @@ impl<'tcx> QueryCtxt<'tcx> { } } +#[derive(Clone, Copy)] +pub(crate) struct QueryStruct<'tcx> { + pub try_collect_active_jobs: fn(QueryCtxt<'tcx>, &mut QueryMap<DepKind>) -> Option<()>, + pub alloc_self_profile_query_strings: fn(TyCtxt<'tcx>, &mut QueryKeyStringCache), + pub encode_query_results: + Option<fn(QueryCtxt<'tcx>, &mut CacheEncoder<'_, 'tcx>, &mut EncodedDepNodeIndex)>, +} + macro_rules! handle_cycle_error { ([]) => {{ rustc_query_system::HandleCycleError::Error @@ -225,6 +252,18 @@ macro_rules! depth_limit { }; } +macro_rules! feedable { + ([]) => {{ + false + }}; + ([(feedable) $($rest:tt)*]) => {{ + true + }}; + ([$other:tt $($modifiers:tt)*]) => { + feedable!([$($modifiers)*]) + }; +} + macro_rules! hash_result { ([]) => {{ Some(dep_graph::hash_result) @@ -253,24 +292,36 @@ macro_rules! get_provider { }; } +macro_rules! should_ever_cache_on_disk { + ([]) => {{ + None + }}; + ([(cache) $($rest:tt)*]) => {{ + Some($crate::plumbing::try_load_from_disk::<Self::Value>) + }}; + ([$other:tt $($modifiers:tt)*]) => { + should_ever_cache_on_disk!([$($modifiers)*]) + }; +} + pub(crate) fn create_query_frame< 'tcx, K: Copy + Key + for<'a> HashStable<StableHashingContext<'a>>, >( tcx: QueryCtxt<'tcx>, - do_describe: fn(QueryCtxt<'tcx>, K) -> String, + do_describe: fn(TyCtxt<'tcx>, K) -> String, key: K, kind: DepKind, name: &'static str, -) -> QueryStackFrame { +) -> QueryStackFrame<DepKind> { // Disable visible paths printing for performance reasons. // Showing visible path instead of any path is not that important in production. let description = ty::print::with_no_visible_paths!( // Force filename-line mode to avoid invoking `type_of` query. - ty::print::with_forced_impl_filename_line!(do_describe(tcx, key)) + ty::print::with_forced_impl_filename_line!(do_describe(tcx.tcx, key)) ); let description = - if tcx.sess.verbose() { format!("{} [{}]", description, name) } else { description }; + if tcx.sess.verbose() { format!("{} [{:?}]", description, name) } else { description }; let span = if kind == dep_graph::DepKind::def_span { // The `def_span` query is used to calculate `default_span`, // so exit to avoid infinite recursion. @@ -278,13 +329,12 @@ pub(crate) fn create_query_frame< } else { Some(key.default_span(*tcx)) }; + let def_id = key.key_as_def_id(); let def_kind = if kind == dep_graph::DepKind::opt_def_kind { // Try to avoid infinite recursion. None } else { - key.key_as_def_id() - .and_then(|def_id| def_id.as_local()) - .and_then(|def_id| tcx.opt_def_kind(def_id)) + def_id.and_then(|def_id| def_id.as_local()).and_then(|def_id| tcx.opt_def_kind(def_id)) }; let hash = || { tcx.with_stable_hashing_context(|mut hcx| { @@ -294,13 +344,14 @@ pub(crate) fn create_query_frame< hasher.finish::<u64>() }) }; + let ty_adt_id = key.ty_adt_id(); - QueryStackFrame::new(name, description, span, def_kind, hash) + QueryStackFrame::new(description, span, def_id, def_kind, kind, ty_adt_id, hash) } fn try_load_from_on_disk_cache<'tcx, Q>(tcx: TyCtxt<'tcx>, dep_node: DepNode) where - Q: QueryDescription<QueryCtxt<'tcx>>, + Q: QueryConfig<QueryCtxt<'tcx>>, Q::Key: DepNodeParams<TyCtxt<'tcx>>, { debug_assert!(tcx.dep_graph.is_green(&dep_node)); @@ -313,29 +364,54 @@ where } } +pub(crate) fn try_load_from_disk<'tcx, V>( + tcx: QueryCtxt<'tcx>, + id: SerializedDepNodeIndex, +) -> Option<V> +where + V: for<'a> Decodable<CacheDecoder<'a, 'tcx>>, +{ + tcx.on_disk_cache().as_ref()?.try_load_query_result(*tcx, id) +} + fn force_from_dep_node<'tcx, Q>(tcx: TyCtxt<'tcx>, dep_node: DepNode) -> bool where - Q: QueryDescription<QueryCtxt<'tcx>>, + Q: QueryConfig<QueryCtxt<'tcx>>, Q::Key: DepNodeParams<TyCtxt<'tcx>>, - Q::Value: Value<TyCtxt<'tcx>>, + Q::Value: Value<TyCtxt<'tcx>, DepKind>, { + // We must avoid ever having to call `force_from_dep_node()` for a + // `DepNode::codegen_unit`: + // Since we cannot reconstruct the query key of a `DepNode::codegen_unit`, we + // would always end up having to evaluate the first caller of the + // `codegen_unit` query that *is* reconstructible. This might very well be + // the `compile_codegen_unit` query, thus re-codegenning the whole CGU just + // to re-trigger calling the `codegen_unit` query with the right key. At + // that point we would already have re-done all the work we are trying to + // avoid doing in the first place. + // The solution is simple: Just explicitly call the `codegen_unit` query for + // each CGU, right after partitioning. This way `try_mark_green` will always + // hit the cache instead of having to go through `force_from_dep_node`. + // This assertion makes sure, we actually keep applying the solution above. + debug_assert!( + dep_node.kind != DepKind::codegen_unit, + "calling force_from_dep_node() on DepKind::codegen_unit" + ); + if let Some(key) = Q::Key::recover(tcx, &dep_node) { #[cfg(debug_assertions)] let _guard = tracing::span!(tracing::Level::TRACE, stringify!($name), ?key).entered(); let tcx = QueryCtxt::from_tcx(tcx); - force_query::<Q, _>(tcx, key, dep_node); + force_query::<Q, _, DepKind>(tcx, key, dep_node); true } else { false } } -pub(crate) fn query_callback<'tcx, Q: QueryConfig>( - is_anon: bool, - is_eval_always: bool, -) -> DepKindStruct<'tcx> +pub(crate) fn query_callback<'tcx, Q>(is_anon: bool, is_eval_always: bool) -> DepKindStruct<'tcx> where - Q: QueryDescription<QueryCtxt<'tcx>>, + Q: QueryConfig<QueryCtxt<'tcx>>, Q::Key: DepNodeParams<TyCtxt<'tcx>>, { let fingerprint_style = Q::Key::fingerprint_style(); @@ -359,6 +435,18 @@ where } } +macro_rules! expand_if_cached { + ([], $tokens:expr) => {{ + None + }}; + ([(cache) $($rest:tt)*], $tokens:expr) => {{ + Some($tokens) + }}; + ([$other:tt $($modifiers:tt)*], $tokens:expr) => { + expand_if_cached!([$($modifiers)*], $tokens) + }; +} + // NOTE: `$V` isn't used here, but we still need to match on it so it can be passed to other macros // invoked by `rustc_query_append`. macro_rules! define_queries { @@ -378,20 +466,21 @@ macro_rules! define_queries { })* } - $(impl<'tcx> QueryConfig for queries::$name<'tcx> { + $(impl<'tcx> QueryConfig<QueryCtxt<'tcx>> for queries::$name<'tcx> { type Key = query_keys::$name<'tcx>; type Value = query_values::$name<'tcx>; type Stored = query_stored::$name<'tcx>; const NAME: &'static str = stringify!($name); - } - impl<'tcx> QueryDescription<QueryCtxt<'tcx>> for queries::$name<'tcx> { - rustc_query_description! { $name } + #[inline] + fn cache_on_disk(tcx: TyCtxt<'tcx>, key: &Self::Key) -> bool { + ::rustc_middle::query::cached::$name(tcx, key) + } type Cache = query_storage::$name<'tcx>; #[inline(always)] - fn query_state<'a>(tcx: QueryCtxt<'tcx>) -> &'a QueryState<Self::Key> + fn query_state<'a>(tcx: QueryCtxt<'tcx>) -> &'a QueryState<Self::Key, crate::dep_graph::DepKind> where QueryCtxt<'tcx>: 'a { &tcx.queries.$name @@ -414,12 +503,12 @@ macro_rules! define_queries { anon: is_anon!([$($modifiers)*]), eval_always: is_eval_always!([$($modifiers)*]), depth_limit: depth_limit!([$($modifiers)*]), + feedable: feedable!([$($modifiers)*]), dep_kind: dep_graph::DepKind::$name, hash_result: hash_result!([$($modifiers)*]), handle_cycle_error: handle_cycle_error!([$($modifiers)*]), compute, - cache_on_disk, - try_load_from_disk: Self::TRY_LOAD_FROM_DISK, + try_load_from_disk: if cache_on_disk { should_ever_cache_on_disk!([$($modifiers)*]) } else { None }, } } @@ -493,6 +582,60 @@ macro_rules! define_queries { })* } + mod query_structs { + use rustc_middle::ty::TyCtxt; + use $crate::plumbing::{QueryStruct, QueryCtxt}; + use $crate::profiling_support::QueryKeyStringCache; + use rustc_query_system::query::QueryMap; + use rustc_middle::dep_graph::DepKind; + + pub(super) const fn dummy_query_struct<'tcx>() -> QueryStruct<'tcx> { + fn noop_try_collect_active_jobs(_: QueryCtxt<'_>, _: &mut QueryMap<DepKind>) -> Option<()> { + None + } + fn noop_alloc_self_profile_query_strings(_: TyCtxt<'_>, _: &mut QueryKeyStringCache) {} + + QueryStruct { + try_collect_active_jobs: noop_try_collect_active_jobs, + alloc_self_profile_query_strings: noop_alloc_self_profile_query_strings, + encode_query_results: None, + } + } + + pub(super) use dummy_query_struct as Null; + pub(super) use dummy_query_struct as Red; + pub(super) use dummy_query_struct as TraitSelect; + pub(super) use dummy_query_struct as CompileCodegenUnit; + pub(super) use dummy_query_struct as CompileMonoItem; + + $( + pub(super) const fn $name<'tcx>() -> QueryStruct<'tcx> { QueryStruct { + try_collect_active_jobs: |tcx, qmap| { + let make_query = |tcx, key| { + let kind = rustc_middle::dep_graph::DepKind::$name; + let name = stringify!($name); + $crate::plumbing::create_query_frame(tcx, rustc_middle::query::descs::$name, key, kind, name) + }; + tcx.queries.$name.try_collect_active_jobs( + tcx, + make_query, + qmap, + ) + }, + alloc_self_profile_query_strings: |tcx, string_cache| { + $crate::profiling_support::alloc_self_profile_query_strings_for_query_cache( + tcx, + stringify!($name), + &tcx.query_caches.$name, + string_cache, + ) + }, + encode_query_results: expand_if_cached!([$($modifiers)*], |tcx, encoder, query_result_index| + $crate::on_disk_cache::encode_query_results::<_, super::queries::$name<'_>>(tcx, encoder, query_result_index) + ), + }})* + } + pub fn query_callbacks<'tcx>(arena: &'tcx Arena<'tcx>) -> &'tcx [DepKindStruct<'tcx>] { arena.alloc_from_iter(make_dep_kind_array!(query_callbacks)) } @@ -507,9 +650,11 @@ impl<'tcx> Queries<'tcx> { extern_providers: ExternProviders, on_disk_cache: Option<OnDiskCache<'tcx>>, ) -> Self { + use crate::query_structs; Queries { local_providers: Box::new(local_providers), extern_providers: Box::new(extern_providers), + query_structs: make_dep_kind_array!(query_structs).to_vec(), on_disk_cache, jobs: AtomicU64::new(1), ..Queries::default() @@ -524,34 +669,30 @@ macro_rules! define_queries_struct { pub struct Queries<'tcx> { local_providers: Box<Providers>, extern_providers: Box<ExternProviders>, - + query_structs: Vec<$crate::plumbing::QueryStruct<'tcx>>, pub on_disk_cache: Option<OnDiskCache<'tcx>>, - jobs: AtomicU64, - $($(#[$attr])* $name: QueryState<<queries::$name<'tcx> as QueryConfig>::Key>,)* + $( + $(#[$attr])* + $name: QueryState< + <queries::$name<'tcx> as QueryConfig<QueryCtxt<'tcx>>>::Key, + rustc_middle::dep_graph::DepKind, + >, + )* } impl<'tcx> Queries<'tcx> { pub(crate) fn try_collect_active_jobs( &'tcx self, tcx: TyCtxt<'tcx>, - ) -> Option<QueryMap> { + ) -> Option<QueryMap<rustc_middle::dep_graph::DepKind>> { let tcx = QueryCtxt { tcx, queries: self }; let mut jobs = QueryMap::default(); - $( - let make_query = |tcx, key| { - let kind = dep_graph::DepKind::$name; - let name = stringify!($name); - $crate::plumbing::create_query_frame(tcx, queries::$name::describe, key, kind, name) - }; - self.$name.try_collect_active_jobs( - tcx, - make_query, - &mut jobs, - )?; - )* + for query in &self.query_structs { + (query.try_collect_active_jobs)(tcx, &mut jobs); + } Some(jobs) } @@ -575,11 +716,11 @@ macro_rules! define_queries_struct { &'tcx self, tcx: TyCtxt<'tcx>, span: Span, - key: <queries::$name<'tcx> as QueryConfig>::Key, + key: <queries::$name<'tcx> as QueryConfig<QueryCtxt<'tcx>>>::Key, mode: QueryMode, ) -> Option<query_stored::$name<'tcx>> { let qcx = QueryCtxt { tcx, queries: self }; - get_query::<queries::$name<'tcx>, _>(qcx, span, key, mode) + get_query::<queries::$name<'tcx>, _, rustc_middle::dep_graph::DepKind>(qcx, span, key, mode) })* } }; diff --git a/compiler/rustc_query_impl/src/profiling_support.rs b/compiler/rustc_query_impl/src/profiling_support.rs index 260af0d5408..5f54bab9c31 100644 --- a/compiler/rustc_query_impl/src/profiling_support.rs +++ b/compiler/rustc_query_impl/src/profiling_support.rs @@ -1,3 +1,4 @@ +use crate::QueryCtxt; use measureme::{StringComponent, StringId}; use rustc_data_structures::fx::FxHashMap; use rustc_data_structures::profiling::SelfProfiler; @@ -8,7 +9,7 @@ use rustc_query_system::query::QueryCache; use std::fmt::Debug; use std::io::Write; -struct QueryKeyStringCache { +pub(crate) struct QueryKeyStringCache { def_id_cache: FxHashMap<DefId, StringId>, } @@ -18,18 +19,18 @@ impl QueryKeyStringCache { } } -struct QueryKeyStringBuilder<'p, 'c, 'tcx> { +struct QueryKeyStringBuilder<'p, 'tcx> { profiler: &'p SelfProfiler, tcx: TyCtxt<'tcx>, - string_cache: &'c mut QueryKeyStringCache, + string_cache: &'p mut QueryKeyStringCache, } -impl<'p, 'c, 'tcx> QueryKeyStringBuilder<'p, 'c, 'tcx> { +impl<'p, 'tcx> QueryKeyStringBuilder<'p, 'tcx> { fn new( profiler: &'p SelfProfiler, tcx: TyCtxt<'tcx>, - string_cache: &'c mut QueryKeyStringCache, - ) -> QueryKeyStringBuilder<'p, 'c, 'tcx> { + string_cache: &'p mut QueryKeyStringCache, + ) -> QueryKeyStringBuilder<'p, 'tcx> { QueryKeyStringBuilder { profiler, tcx, string_cache } } @@ -98,7 +99,7 @@ impl<'p, 'c, 'tcx> QueryKeyStringBuilder<'p, 'c, 'tcx> { } trait IntoSelfProfilingString { - fn to_self_profile_string(&self, builder: &mut QueryKeyStringBuilder<'_, '_, '_>) -> StringId; + fn to_self_profile_string(&self, builder: &mut QueryKeyStringBuilder<'_, '_>) -> StringId; } // The default implementation of `IntoSelfProfilingString` just uses `Debug` @@ -108,7 +109,7 @@ trait IntoSelfProfilingString { impl<T: Debug> IntoSelfProfilingString for T { default fn to_self_profile_string( &self, - builder: &mut QueryKeyStringBuilder<'_, '_, '_>, + builder: &mut QueryKeyStringBuilder<'_, '_>, ) -> StringId { let s = format!("{:?}", self); builder.profiler.alloc_string(&s[..]) @@ -116,60 +117,42 @@ impl<T: Debug> IntoSelfProfilingString for T { } impl<T: SpecIntoSelfProfilingString> IntoSelfProfilingString for T { - fn to_self_profile_string(&self, builder: &mut QueryKeyStringBuilder<'_, '_, '_>) -> StringId { + fn to_self_profile_string(&self, builder: &mut QueryKeyStringBuilder<'_, '_>) -> StringId { self.spec_to_self_profile_string(builder) } } #[rustc_specialization_trait] trait SpecIntoSelfProfilingString: Debug { - fn spec_to_self_profile_string( - &self, - builder: &mut QueryKeyStringBuilder<'_, '_, '_>, - ) -> StringId; + fn spec_to_self_profile_string(&self, builder: &mut QueryKeyStringBuilder<'_, '_>) -> StringId; } impl SpecIntoSelfProfilingString for DefId { - fn spec_to_self_profile_string( - &self, - builder: &mut QueryKeyStringBuilder<'_, '_, '_>, - ) -> StringId { + fn spec_to_self_profile_string(&self, builder: &mut QueryKeyStringBuilder<'_, '_>) -> StringId { builder.def_id_to_string_id(*self) } } impl SpecIntoSelfProfilingString for CrateNum { - fn spec_to_self_profile_string( - &self, - builder: &mut QueryKeyStringBuilder<'_, '_, '_>, - ) -> StringId { + fn spec_to_self_profile_string(&self, builder: &mut QueryKeyStringBuilder<'_, '_>) -> StringId { builder.def_id_to_string_id(self.as_def_id()) } } impl SpecIntoSelfProfilingString for DefIndex { - fn spec_to_self_profile_string( - &self, - builder: &mut QueryKeyStringBuilder<'_, '_, '_>, - ) -> StringId { + fn spec_to_self_profile_string(&self, builder: &mut QueryKeyStringBuilder<'_, '_>) -> StringId { builder.def_id_to_string_id(DefId { krate: LOCAL_CRATE, index: *self }) } } impl SpecIntoSelfProfilingString for LocalDefId { - fn spec_to_self_profile_string( - &self, - builder: &mut QueryKeyStringBuilder<'_, '_, '_>, - ) -> StringId { + fn spec_to_self_profile_string(&self, builder: &mut QueryKeyStringBuilder<'_, '_>) -> StringId { builder.def_id_to_string_id(DefId { krate: LOCAL_CRATE, index: self.local_def_index }) } } impl<T: SpecIntoSelfProfilingString> SpecIntoSelfProfilingString for WithOptConstParam<T> { - fn spec_to_self_profile_string( - &self, - builder: &mut QueryKeyStringBuilder<'_, '_, '_>, - ) -> StringId { + fn spec_to_self_profile_string(&self, builder: &mut QueryKeyStringBuilder<'_, '_>) -> StringId { // We print `WithOptConstParam` values as tuples to make them shorter // and more readable, without losing information: // @@ -204,10 +187,7 @@ where T0: SpecIntoSelfProfilingString, T1: SpecIntoSelfProfilingString, { - fn spec_to_self_profile_string( - &self, - builder: &mut QueryKeyStringBuilder<'_, '_, '_>, - ) -> StringId { + fn spec_to_self_profile_string(&self, builder: &mut QueryKeyStringBuilder<'_, '_>) -> StringId { let val0 = self.0.to_self_profile_string(builder); let val1 = self.1.to_self_profile_string(builder); @@ -226,7 +206,7 @@ where /// Allocate the self-profiling query strings for a single query cache. This /// method is called from `alloc_self_profile_query_strings` which knows all /// the queries via macro magic. -fn alloc_self_profile_query_strings_for_query_cache<'tcx, C>( +pub(crate) fn alloc_self_profile_query_strings_for_query_cache<'tcx, C>( tcx: TyCtxt<'tcx>, query_name: &'static str, query_cache: &C, @@ -304,21 +284,9 @@ pub fn alloc_self_profile_query_strings(tcx: TyCtxt<'_>) { } let mut string_cache = QueryKeyStringCache::new(); + let queries = QueryCtxt::from_tcx(tcx); - macro_rules! alloc_once { - ( - $($(#[$attr:meta])* [$($modifiers:tt)*] fn $name:ident($K:ty) -> $V:ty,)* - ) => { - $({ - alloc_self_profile_query_strings_for_query_cache( - tcx, - stringify!($name), - &tcx.query_caches.$name, - &mut string_cache, - ); - })* - } + for query in &queries.queries.query_structs { + (query.alloc_self_profile_query_strings)(tcx, &mut string_cache); } - - rustc_query_append! { alloc_once! } } |
