From 9deed6f74ea2df0ba08fb72342bef4eb303d0777 Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Sat, 19 Feb 2022 22:44:19 -0500 Subject: Move Sharded maps into each QueryCache impl --- compiler/rustc_query_impl/src/on_disk_cache.rs | 4 ++-- compiler/rustc_query_impl/src/plumbing.rs | 2 +- compiler/rustc_query_impl/src/profiling_support.rs | 8 ++++---- 3 files changed, 7 insertions(+), 7 deletions(-) (limited to 'compiler/rustc_query_impl/src') diff --git a/compiler/rustc_query_impl/src/on_disk_cache.rs b/compiler/rustc_query_impl/src/on_disk_cache.rs index 06e276ab42b..f2f895367ff 100644 --- a/compiler/rustc_query_impl/src/on_disk_cache.rs +++ b/compiler/rustc_query_impl/src/on_disk_cache.rs @@ -13,7 +13,7 @@ use rustc_middle::thir; use rustc_middle::ty::codec::{RefDecodable, TyDecoder, TyEncoder}; use rustc_middle::ty::{self, Ty, TyCtxt}; use rustc_query_system::dep_graph::DepContext; -use rustc_query_system::query::{QueryContext, QuerySideEffects}; +use rustc_query_system::query::{QueryCache, QueryContext, QuerySideEffects}; use rustc_serialize::{ opaque::{self, FileEncodeResult, FileEncoder, IntEncodedWithFixedSize}, Decodable, Decoder, Encodable, Encoder, @@ -1034,7 +1034,7 @@ where assert!(Q::query_state(tcx).all_inactive()); let cache = Q::query_cache(tcx); let mut res = Ok(()); - cache.iter_results(&mut |key, value, dep_node| { + cache.iter(&mut |key, value, dep_node| { if res.is_err() { return; } diff --git a/compiler/rustc_query_impl/src/plumbing.rs b/compiler/rustc_query_impl/src/plumbing.rs index ff9d32a6776..1eaf5ee0c05 100644 --- a/compiler/rustc_query_impl/src/plumbing.rs +++ b/compiler/rustc_query_impl/src/plumbing.rs @@ -337,7 +337,7 @@ macro_rules! define_queries { } #[inline(always)] - fn query_cache<'a>(tcx: QueryCtxt<$tcx>) -> &'a QueryCacheStore + fn query_cache<'a>(tcx: QueryCtxt<$tcx>) -> &'a Self::Cache where 'tcx:'a { &tcx.query_caches.$name diff --git a/compiler/rustc_query_impl/src/profiling_support.rs b/compiler/rustc_query_impl/src/profiling_support.rs index da318fc7622..acccf43f062 100644 --- a/compiler/rustc_query_impl/src/profiling_support.rs +++ b/compiler/rustc_query_impl/src/profiling_support.rs @@ -4,7 +4,7 @@ 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 rustc_query_system::query::QueryCache; use std::fmt::Debug; use std::io::Write; @@ -229,7 +229,7 @@ where fn alloc_self_profile_query_strings_for_query_cache<'tcx, C>( tcx: TyCtxt<'tcx>, query_name: &'static str, - query_cache: &QueryCacheStore, + query_cache: &C, string_cache: &mut QueryKeyStringCache, ) where C: QueryCache, @@ -251,7 +251,7 @@ fn alloc_self_profile_query_strings_for_query_cache<'tcx, C>( // locked while doing so. Instead we copy out the // `(query_key, dep_node_index)` pairs and release the lock again. let mut query_keys_and_indices = Vec::new(); - query_cache.iter_results(&mut |k, _, i| query_keys_and_indices.push((k.clone(), i))); + query_cache.iter(&mut |k, _, i| query_keys_and_indices.push((k.clone(), i))); // Now actually allocate the strings. If allocating the strings // generates new entries in the query cache, we'll miss them but @@ -276,7 +276,7 @@ fn alloc_self_profile_query_strings_for_query_cache<'tcx, C>( let event_id = event_id_builder.from_label(query_name).to_string_id(); let mut query_invocation_ids = Vec::new(); - query_cache.iter_results(&mut |_, _, i| { + query_cache.iter(&mut |_, _, i| { query_invocation_ids.push(i.into()); }); -- cgit 1.4.1-3-g733a5 From 75ef06892089e0cf53b4a86419c7ff3b3c1f3c4c Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Sat, 19 Feb 2022 22:56:56 -0500 Subject: Delete QueryLookup This was largely just caching the shard value at this point, which is not particularly useful -- in the use sites the key was being hashed nearby anyway. --- compiler/rustc_middle/src/ty/query.rs | 17 ++++++------ compiler/rustc_query_impl/src/plumbing.rs | 3 +-- compiler/rustc_query_system/src/query/caches.rs | 17 +++++------- compiler/rustc_query_system/src/query/plumbing.rs | 32 ++++++----------------- 4 files changed, 24 insertions(+), 45 deletions(-) (limited to 'compiler/rustc_query_impl/src') diff --git a/compiler/rustc_middle/src/ty/query.rs b/compiler/rustc_middle/src/ty/query.rs index c72b823d849..f2e1d129e9b 100644 --- a/compiler/rustc_middle/src/ty/query.rs +++ b/compiler/rustc_middle/src/ty/query.rs @@ -222,12 +222,12 @@ macro_rules! define_callbacks { let cached = try_get_cached(self.tcx, &self.tcx.query_caches.$name, &key, noop); - let lookup = match cached { + match cached { Ok(()) => return, - Err(lookup) => lookup, - }; + Err(()) => (), + } - self.tcx.queries.$name(self.tcx, DUMMY_SP, key, lookup, QueryMode::Ensure); + self.tcx.queries.$name(self.tcx, DUMMY_SP, key, QueryMode::Ensure); })* } @@ -251,12 +251,12 @@ macro_rules! define_callbacks { let cached = try_get_cached(self.tcx, &self.tcx.query_caches.$name, &key, copy); - let lookup = match cached { + match cached { Ok(value) => return value, - Err(lookup) => lookup, - }; + Err(()) => (), + } - self.tcx.queries.$name(self.tcx, self.span, key, lookup, QueryMode::Get).unwrap() + self.tcx.queries.$name(self.tcx, self.span, key, QueryMode::Get).unwrap() })* } @@ -314,7 +314,6 @@ macro_rules! define_callbacks { tcx: TyCtxt<$tcx>, span: Span, key: query_keys::$name<$tcx>, - lookup: QueryLookup, mode: QueryMode, ) -> Option>;)* } diff --git a/compiler/rustc_query_impl/src/plumbing.rs b/compiler/rustc_query_impl/src/plumbing.rs index 1eaf5ee0c05..684b2e248c8 100644 --- a/compiler/rustc_query_impl/src/plumbing.rs +++ b/compiler/rustc_query_impl/src/plumbing.rs @@ -538,12 +538,11 @@ macro_rules! define_queries_struct { tcx: TyCtxt<$tcx>, span: Span, key: query_keys::$name<$tcx>, - lookup: QueryLookup, mode: QueryMode, ) -> Option> { opt_remap_env_constness!([$($modifiers)*][key]); let qcx = QueryCtxt { tcx, queries: self }; - get_query::, _>(qcx, span, key, lookup, mode) + get_query::, _>(qcx, span, key, mode) })* } }; diff --git a/compiler/rustc_query_system/src/query/caches.rs b/compiler/rustc_query_system/src/query/caches.rs index 05cc61c83aa..c5fa4c9ee6f 100644 --- a/compiler/rustc_query_system/src/query/caches.rs +++ b/compiler/rustc_query_system/src/query/caches.rs @@ -1,5 +1,4 @@ use crate::dep_graph::DepNodeIndex; -use crate::query::plumbing::QueryLookup; use rustc_arena::TypedArena; use rustc_data_structures::fx::FxHashMap; @@ -35,7 +34,7 @@ pub trait QueryCache: QueryStorage + Sized { key: &Self::Key, // `on_hit` can be called while holding a lock to the query state shard. on_hit: OnHit, - ) -> Result + ) -> Result where OnHit: FnOnce(&Self::Stored, DepNodeIndex) -> R; @@ -79,21 +78,20 @@ where type Key = K; #[inline(always)] - fn lookup(&self, key: &K, on_hit: OnHit) -> Result + fn lookup(&self, key: &K, on_hit: OnHit) -> Result where OnHit: FnOnce(&V, DepNodeIndex) -> R, { let key_hash = sharded::make_hash(key); let shard = sharded::get_shard_index_by_hash(key_hash); let lock = self.shards.get_shard_by_index(shard).lock(); - let lookup = QueryLookup { key_hash, shard }; - let result = lock.raw_entry().from_key_hashed_nocheck(lookup.key_hash, key); + let result = lock.raw_entry().from_key_hashed_nocheck(key_hash, key); if let Some((_, value)) = result { let hit_result = on_hit(&value.0, value.1); Ok(hit_result) } else { - Err(lookup) + Err(()) } } @@ -153,21 +151,20 @@ where type Key = K; #[inline(always)] - fn lookup(&self, key: &K, on_hit: OnHit) -> Result + fn lookup(&self, key: &K, on_hit: OnHit) -> Result where OnHit: FnOnce(&&'tcx V, DepNodeIndex) -> R, { let key_hash = sharded::make_hash(key); let shard = sharded::get_shard_index_by_hash(key_hash); let lock = self.shards.get_shard_by_index(shard).lock(); - let lookup = QueryLookup { key_hash, shard }; - let result = lock.raw_entry().from_key_hashed_nocheck(lookup.key_hash, key); + let result = lock.raw_entry().from_key_hashed_nocheck(key_hash, key); if let Some((_, value)) = result { let hit_result = on_hit(&&value.0, value.1); Ok(hit_result) } else { - Err(lookup) + Err(()) } } diff --git a/compiler/rustc_query_system/src/query/plumbing.rs b/compiler/rustc_query_system/src/query/plumbing.rs index 9278bb602e1..d55afaa0cb0 100644 --- a/compiler/rustc_query_system/src/query/plumbing.rs +++ b/compiler/rustc_query_system/src/query/plumbing.rs @@ -24,12 +24,6 @@ use std::hash::{Hash, Hasher}; use std::mem; use std::ptr; -/// Values used when checking a query cache which can be reused on a cache-miss to execute the query. -pub struct QueryLookup { - pub(super) key_hash: u64, - pub(super) shard: usize, -} - // We compute the key's hash once and then use it for both the // shard lookup and the hashmap lookup. This relies on the fact // that both of them use `FxHasher`. @@ -147,13 +141,11 @@ where state: &'b QueryState, span: Span, key: K, - lookup: QueryLookup, ) -> TryGetJob<'b, K> where CTX: QueryContext, { - let shard = lookup.shard; - let mut state_lock = state.shards.get_shard_by_index(shard).lock(); + let mut state_lock = state.shards.get_shard_by_value(&key).lock(); let lock = &mut *state_lock; match lock.active.entry(key) { @@ -303,7 +295,7 @@ pub fn try_get_cached<'a, CTX, C, R, OnHit>( key: &C::Key, // `on_hit` can be called while holding a lock to the query cache on_hit: OnHit, -) -> Result +) -> Result where C: QueryCache, CTX: DepContext, @@ -324,7 +316,6 @@ fn try_execute_query( cache: &C, span: Span, key: C::Key, - lookup: QueryLookup, dep_node: Option>, query: &QueryVtable, ) -> (C::Stored, Option) @@ -333,7 +324,7 @@ where C::Key: Clone + DepNodeParams, CTX: QueryContext, { - match JobOwner::<'_, C::Key>::try_start(&tcx, state, span, key.clone(), lookup) { + match JobOwner::<'_, C::Key>::try_start(&tcx, state, span, key.clone()) { TryGetJob::NotYetStarted(job) => { let (result, dep_node_index) = execute_job(tcx, key, dep_node, query, job.id); let result = job.complete(cache, result, dep_node_index); @@ -675,13 +666,7 @@ pub enum QueryMode { Ensure, } -pub fn get_query( - tcx: CTX, - span: Span, - key: Q::Key, - lookup: QueryLookup, - mode: QueryMode, -) -> Option +pub fn get_query(tcx: CTX, span: Span, key: Q::Key, mode: QueryMode) -> Option where Q: QueryDescription, Q::Key: DepNodeParams, @@ -705,7 +690,6 @@ where Q::query_cache(tcx), span, key, - lookup, dep_node, &query, ); @@ -730,14 +714,14 @@ where } }); - let lookup = match cached { + match cached { Ok(()) => return, - Err(lookup) => lookup, - }; + Err(()) => {} + } let query = Q::make_vtable(tcx, &key); let state = Q::query_state(tcx); debug_assert!(!query.anon); - try_execute_query(tcx, state, cache, DUMMY_SP, key, lookup, Some(dep_node), &query); + try_execute_query(tcx, state, cache, DUMMY_SP, key, Some(dep_node), &query); } -- cgit 1.4.1-3-g733a5