diff options
| author | bors <bors@rust-lang.org> | 2023-04-29 21:58:13 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2023-04-29 21:58:13 +0000 |
| commit | f5adff6bd8b29ac7dd173b36f0c8c35bb1c593c5 (patch) | |
| tree | 0af42fdb67cd5ee8e9100915fda59e84efec4029 /compiler/rustc_middle/src/ty | |
| parent | 87b1f891ea76713462cfc5a15137a8fe2b24ecc2 (diff) | |
| parent | b6943736bd37e0e932089c27dd0638f0a7ddc3fe (diff) | |
| download | rust-f5adff6bd8b29ac7dd173b36f0c8c35bb1c593c5.tar.gz rust-f5adff6bd8b29ac7dd173b36f0c8c35bb1c593c5.zip | |
Auto merge of #109611 - Zoxc:query-engine-rem, r=cjgillot
Remove `QueryEngine` trait This removes the `QueryEngine` trait and `Queries` from `rustc_query_impl` and replaced them with function pointers and fields in `QuerySystem`. As a side effect `OnDiskCache` is moved back into `rustc_middle` and the `OnDiskCache` trait is also removed. This has a couple of benefits. - `TyCtxt` is used in the query system instead of the removed `QueryCtxt` which is larger. - Function pointers are more flexible to work with. A variant of https://github.com/rust-lang/rust/pull/107802 is included which avoids the double indirection. For https://github.com/rust-lang/rust/pull/108938 we can name entry point `__rust_end_short_backtrace` to avoid some overhead. For https://github.com/rust-lang/rust/pull/108062 it avoids the duplicate `QueryEngine` structs. - `QueryContext` now implements `DepContext` which avoids many `dep_context()` calls in `rustc_query_system`. - The `rustc_driver` size is reduced by 0.33%, hopefully that means some bootstrap improvements. - This avoids the unsafe code around the `QueryEngine` trait. r? `@cjgillot`
Diffstat (limited to 'compiler/rustc_middle/src/ty')
| -rw-r--r-- | compiler/rustc_middle/src/ty/codec.rs | 1 | ||||
| -rw-r--r-- | compiler/rustc_middle/src/ty/context.rs | 37 | ||||
| -rw-r--r-- | compiler/rustc_middle/src/ty/mod.rs | 3 | ||||
| -rw-r--r-- | compiler/rustc_middle/src/ty/query.rs | 180 |
4 files changed, 143 insertions, 78 deletions
diff --git a/compiler/rustc_middle/src/ty/codec.rs b/compiler/rustc_middle/src/ty/codec.rs index 3793d85c85a..7536903ef96 100644 --- a/compiler/rustc_middle/src/ty/codec.rs +++ b/compiler/rustc_middle/src/ty/codec.rs @@ -500,7 +500,6 @@ impl_arena_copy_decoder! {<'tcx> macro_rules! implement_ty_decoder { ($DecoderName:ident <$($typaram:tt),*>) => { mod __ty_decoder_impl { - use std::borrow::Cow; use rustc_serialize::Decoder; use super::$DecoderName; diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index 830231646c6..a309eaf048d 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -14,11 +14,14 @@ use crate::middle::resolve_bound_vars; use crate::middle::stability; use crate::mir::interpret::{self, Allocation, ConstAllocation}; use crate::mir::{Body, Local, Place, PlaceElem, ProjectionKind, Promoted}; +use crate::query::on_disk_cache::OnDiskCache; use crate::query::LocalCrate; use crate::thir::Thir; use crate::traits; use crate::traits::solve; use crate::traits::solve::{ExternalConstraints, ExternalConstraintsData}; +use crate::ty::query::QuerySystem; +use crate::ty::query::QuerySystemFns; use crate::ty::query::{self, TyCtxtAt}; use crate::ty::{ self, AdtDef, AdtDefData, AdtKind, Binder, Const, ConstData, FloatTy, FloatVar, FloatVid, @@ -31,7 +34,6 @@ use rustc_ast::{self as ast, attr}; use rustc_data_structures::fingerprint::Fingerprint; use rustc_data_structures::fx::{FxHashMap, FxHashSet}; use rustc_data_structures::intern::Interned; -use rustc_data_structures::memmap::Mmap; use rustc_data_structures::profiling::SelfProfilerRef; use rustc_data_structures::sharded::{IntoPointer, ShardedHashMap}; use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; @@ -61,7 +63,6 @@ use rustc_session::lint::Lint; use rustc_session::Limit; use rustc_session::Session; use rustc_span::def_id::{DefPathHash, StableCrateId}; -use rustc_span::source_map::SourceMap; use rustc_span::symbol::{kw, sym, Ident, Symbol}; use rustc_span::{Span, DUMMY_SP}; use rustc_target::abi::{FieldIdx, Layout, LayoutS, TargetDataLayout, VariantIdx}; @@ -84,21 +85,6 @@ use super::query::IntoQueryParam; const TINY_CONST_EVAL_LIMIT: Limit = Limit(20); -pub trait OnDiskCache<'tcx>: rustc_data_structures::sync::Sync { - /// Creates a new `OnDiskCache` instance from the serialized data in `data`. - fn new(sess: &'tcx Session, data: Mmap, start_pos: usize) -> Self - where - Self: Sized; - - fn new_empty(source_map: &'tcx SourceMap) -> Self - where - Self: Sized; - - fn drop_serialized_data(&self, tcx: TyCtxt<'tcx>); - - fn serialize(&self, tcx: TyCtxt<'tcx>, encoder: FileEncoder) -> FileEncodeResult; -} - #[allow(rustc::usage_of_ty_tykind)] impl<'tcx> Interner for TyCtxt<'tcx> { type AdtDef = ty::AdtDef<'tcx>; @@ -527,13 +513,6 @@ pub struct GlobalCtxt<'tcx> { untracked: Untracked, - /// This provides access to the incremental compilation on-disk cache for query results. - /// 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 on_disk_cache: Option<&'tcx dyn OnDiskCache<'tcx>>, - - pub queries: &'tcx dyn query::QueryEngine<'tcx>, pub query_system: query::QuerySystem<'tcx>, pub(crate) query_kinds: &'tcx [DepKindStruct<'tcx>], @@ -674,9 +653,9 @@ impl<'tcx> TyCtxt<'tcx> { hir_arena: &'tcx WorkerLocal<hir::Arena<'tcx>>, untracked: Untracked, dep_graph: DepGraph, - on_disk_cache: Option<&'tcx dyn OnDiskCache<'tcx>>, - queries: &'tcx dyn query::QueryEngine<'tcx>, + on_disk_cache: Option<OnDiskCache<'tcx>>, query_kinds: &'tcx [DepKindStruct<'tcx>], + query_system_fns: QuerySystemFns<'tcx>, ) -> GlobalCtxt<'tcx> { let data_layout = s.target.parse_data_layout().unwrap_or_else(|err| { s.emit_fatal(err); @@ -698,9 +677,7 @@ impl<'tcx> TyCtxt<'tcx> { lifetimes: common_lifetimes, consts: common_consts, untracked, - on_disk_cache, - queries, - query_system: Default::default(), + query_system: QuerySystem::new(query_system_fns, on_disk_cache), query_kinds, ty_rcache: Default::default(), pred_rcache: Default::default(), @@ -1039,7 +1016,7 @@ impl<'tcx> TyCtxt<'tcx> { } pub fn serialize_query_result_cache(self, encoder: FileEncoder) -> FileEncodeResult { - self.on_disk_cache.as_ref().map_or(Ok(0), |c| c.serialize(self, encoder)) + self.query_system.on_disk_cache.as_ref().map_or(Ok(0), |c| c.serialize(self, encoder)) } /// If `true`, we should use lazy normalization for constants, otherwise diff --git a/compiler/rustc_middle/src/ty/mod.rs b/compiler/rustc_middle/src/ty/mod.rs index 8e4e708b73c..db6b35026a8 100644 --- a/compiler/rustc_middle/src/ty/mod.rs +++ b/compiler/rustc_middle/src/ty/mod.rs @@ -84,8 +84,7 @@ pub use self::consts::{ Const, ConstData, ConstInt, ConstKind, Expr, InferConst, ScalarInt, UnevaluatedConst, ValTree, }; pub use self::context::{ - tls, CtxtInterners, DeducedParamAttrs, FreeRegionInfo, GlobalCtxt, Lift, OnDiskCache, TyCtxt, - TyCtxtFeed, + tls, CtxtInterners, DeducedParamAttrs, FreeRegionInfo, GlobalCtxt, Lift, TyCtxt, TyCtxtFeed, }; pub use self::instance::{Instance, InstanceDef, ShortInstance, UnusedGenericParams}; pub use self::list::List; diff --git a/compiler/rustc_middle/src/ty/query.rs b/compiler/rustc_middle/src/ty/query.rs index f4456628457..07d47cae5ee 100644 --- a/compiler/rustc_middle/src/ty/query.rs +++ b/compiler/rustc_middle/src/ty/query.rs @@ -1,6 +1,7 @@ #![allow(unused_parens)] use crate::dep_graph; +use crate::dep_graph::DepKind; use crate::infer::canonical::{self, Canonical}; use crate::lint::LintExpectation; use crate::metadata::ModChild; @@ -17,7 +18,11 @@ use crate::mir::interpret::{ }; use crate::mir::interpret::{LitToConstError, LitToConstInput}; use crate::mir::mono::CodegenUnit; + use crate::query::erase::{erase, restore, Erase}; +use crate::query::on_disk_cache::CacheEncoder; +use crate::query::on_disk_cache::EncodedDepNodeIndex; +use crate::query::on_disk_cache::OnDiskCache; use crate::query::{AsLocalKey, Key}; use crate::thir; use crate::traits::query::{ @@ -38,13 +43,16 @@ use crate::ty::subst::{GenericArg, SubstsRef}; use crate::ty::util::AlwaysRequiresDrop; use crate::ty::GeneratorDiagnosticData; use crate::ty::{self, CrateInherentImpls, ParamEnvAnd, Ty, TyCtxt, UnusedGenericParams}; +use measureme::StringId; use rustc_arena::TypedArena; use rustc_ast as ast; use rustc_ast::expand::allocator::AllocatorKind; use rustc_attr as attr; +use rustc_data_structures::fingerprint::Fingerprint; use rustc_data_structures::fx::{FxHashMap, FxIndexMap, FxIndexSet}; use rustc_data_structures::steal::Steal; use rustc_data_structures::svh::Svh; +use rustc_data_structures::sync::AtomicU64; use rustc_data_structures::sync::Lrc; use rustc_data_structures::sync::WorkerLocal; use rustc_data_structures::unord::UnordSet; @@ -58,6 +66,7 @@ use rustc_hir::hir_id::OwnerId; use rustc_hir::lang_items::{LangItem, LanguageItems}; use rustc_hir::{Crate, ItemLocalId, TraitCandidate}; use rustc_index::IndexVec; +use rustc_query_system::ich::StableHashingContext; pub(crate) use rustc_query_system::query::QueryJobId; use rustc_query_system::query::*; use rustc_session::config::{EntryFnType, OptLevel, OutputFilenames, SymbolManglingVersion}; @@ -76,17 +85,70 @@ use std::ops::Deref; use std::path::PathBuf; use std::sync::Arc; -use rustc_data_structures::fingerprint::Fingerprint; -use rustc_query_system::ich::StableHashingContext; +pub struct QueryKeyStringCache { + pub def_id_cache: FxHashMap<DefId, StringId>, +} + +impl QueryKeyStringCache { + pub fn new() -> QueryKeyStringCache { + QueryKeyStringCache { def_id_cache: Default::default() } + } +} + +#[derive(Clone, Copy)] +pub struct QueryStruct<'tcx> { + pub try_collect_active_jobs: fn(TyCtxt<'tcx>, &mut QueryMap<DepKind>) -> Option<()>, + pub alloc_self_profile_query_strings: fn(TyCtxt<'tcx>, &mut QueryKeyStringCache), + pub encode_query_results: + Option<fn(TyCtxt<'tcx>, &mut CacheEncoder<'_, 'tcx>, &mut EncodedDepNodeIndex)>, +} + +pub struct QuerySystemFns<'tcx> { + pub engine: QueryEngine, + pub local_providers: Providers, + pub extern_providers: ExternProviders, + pub query_structs: Vec<QueryStruct<'tcx>>, + pub encode_query_results: fn( + tcx: TyCtxt<'tcx>, + encoder: &mut CacheEncoder<'_, 'tcx>, + query_result_index: &mut EncodedDepNodeIndex, + ), + pub try_mark_green: fn(tcx: TyCtxt<'tcx>, dep_node: &dep_graph::DepNode) -> bool, +} -#[derive(Default)] pub struct QuerySystem<'tcx> { + pub states: QueryStates<'tcx>, pub arenas: QueryArenas<'tcx>, pub caches: QueryCaches<'tcx>, + + /// This provides access to the incremental compilation on-disk cache for query results. + /// 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 on_disk_cache: Option<OnDiskCache<'tcx>>, + + pub fns: QuerySystemFns<'tcx>, + + pub jobs: AtomicU64, + // Since we erase query value types we tell the typesystem about them with `PhantomData`. _phantom_values: QueryPhantomValues<'tcx>, } +impl<'tcx> QuerySystem<'tcx> { + pub fn new(fns: QuerySystemFns<'tcx>, on_disk_cache: Option<OnDiskCache<'tcx>>) -> Self { + QuerySystem { + states: Default::default(), + arenas: Default::default(), + caches: Default::default(), + on_disk_cache, + fns, + jobs: AtomicU64::new(1), + _phantom_values: Default::default(), + } + } +} + #[derive(Copy, Clone)] pub struct TyCtxtAt<'tcx> { pub tcx: TyCtxt<'tcx>, @@ -136,7 +198,41 @@ impl<'tcx> TyCtxt<'tcx> { } pub fn try_mark_green(self, dep_node: &dep_graph::DepNode) -> bool { - self.queries.try_mark_green(self, dep_node) + (self.query_system.fns.try_mark_green)(self, dep_node) + } +} + +#[inline] +fn query_get_at<'tcx, Cache>( + tcx: TyCtxt<'tcx>, + execute_query: fn(TyCtxt<'tcx>, Span, Cache::Key, QueryMode) -> Option<Cache::Value>, + query_cache: &Cache, + span: Span, + key: Cache::Key, +) -> Cache::Value +where + Cache: QueryCache, +{ + let key = key.into_query_param(); + match try_get_cached(tcx, query_cache, &key) { + Some(value) => value, + None => execute_query(tcx, span, key, QueryMode::Get).unwrap(), + } +} + +#[inline] +fn query_ensure<'tcx, Cache>( + tcx: TyCtxt<'tcx>, + execute_query: fn(TyCtxt<'tcx>, Span, Cache::Key, QueryMode) -> Option<Cache::Value>, + query_cache: &Cache, + key: Cache::Key, + check_cache: bool, +) where + Cache: QueryCache, +{ + let key = key.into_query_param(); + if try_get_cached(tcx, query_cache, &key).is_none() { + execute_query(tcx, DUMMY_SP, key, QueryMode::Ensure { check_cache }); } } @@ -345,17 +441,13 @@ macro_rules! define_callbacks { $($(#[$attr])* #[inline(always)] pub fn $name(self, key: query_helper_param_ty!($($K)*)) { - let key = key.into_query_param(); - - match try_get_cached(self.tcx, &self.tcx.query_system.caches.$name, &key) { - Some(_) => return, - None => self.tcx.queries.$name( - self.tcx, - DUMMY_SP, - key, - QueryMode::Ensure { check_cache: false }, - ), - }; + query_ensure( + self.tcx, + self.tcx.query_system.fns.engine.$name, + &self.tcx.query_system.caches.$name, + key.into_query_param(), + false, + ); })* } @@ -363,17 +455,13 @@ macro_rules! define_callbacks { $($(#[$attr])* #[inline(always)] pub fn $name(self, key: query_helper_param_ty!($($K)*)) { - let key = key.into_query_param(); - - match try_get_cached(self.tcx, &self.tcx.query_system.caches.$name, &key) { - Some(_) => return, - None => self.tcx.queries.$name( - self.tcx, - DUMMY_SP, - key, - QueryMode::Ensure { check_cache: true }, - ), - }; + query_ensure( + self.tcx, + self.tcx.query_system.fns.engine.$name, + &self.tcx.query_system.caches.$name, + key.into_query_param(), + true, + ); })* } @@ -392,15 +480,23 @@ macro_rules! define_callbacks { #[inline(always)] pub fn $name(self, key: query_helper_param_ty!($($K)*)) -> $V { - let key = key.into_query_param(); - - restore::<$V>(match try_get_cached(self.tcx, &self.tcx.query_system.caches.$name, &key) { - Some(value) => value, - None => self.tcx.queries.$name(self.tcx, self.span, key, QueryMode::Get).unwrap(), - }) + restore::<$V>(query_get_at( + self.tcx, + self.tcx.query_system.fns.engine.$name, + &self.tcx.query_system.caches.$name, + self.span, + key.into_query_param(), + )) })* } + #[derive(Default)] + pub struct QueryStates<'tcx> { + $( + pub $name: QueryState<$($K)*, DepKind>, + )* + } + pub struct Providers { $(pub $name: for<'tcx> fn( TyCtxt<'tcx>, @@ -446,19 +542,13 @@ macro_rules! define_callbacks { fn clone(&self) -> Self { *self } } - pub trait QueryEngine<'tcx>: rustc_data_structures::sync::Sync { - fn as_any(&'tcx self) -> &'tcx dyn std::any::Any; - - fn try_mark_green(&'tcx self, tcx: TyCtxt<'tcx>, dep_node: &dep_graph::DepNode) -> bool; - - $($(#[$attr])* - fn $name( - &'tcx self, - tcx: TyCtxt<'tcx>, - span: Span, - key: query_keys::$name<'tcx>, - mode: QueryMode, - ) -> Option<Erase<$V>>;)* + pub struct QueryEngine { + $(pub $name: for<'tcx> fn( + TyCtxt<'tcx>, + Span, + query_keys::$name<'tcx>, + QueryMode, + ) -> Option<Erase<$V>>,)* } }; } |
