Skip to content

Commit

Permalink
Remove <CTX: QueryContext> in a bunch of places.
Browse files Browse the repository at this point in the history
It was only needed by `find_cycle_in_stack()` in job.rs, but needed to be forwarded through dozens of types.
  • Loading branch information
Julian Wollersberger committed Oct 19, 2020
1 parent 39b0e79 commit 52cedca
Show file tree
Hide file tree
Showing 8 changed files with 211 additions and 167 deletions.
10 changes: 6 additions & 4 deletions compiler/rustc_middle/src/ty/query/plumbing.rs
Expand Up @@ -40,7 +40,8 @@ impl QueryContext for TyCtxt<'tcx> {

fn try_collect_active_jobs(
&self,
) -> Option<FxHashMap<QueryJobId<Self::DepKind>, QueryJobInfo<Self>>> {
) -> Option<FxHashMap<QueryJobId<Self::DepKind>, QueryJobInfo<Self::DepKind, Self::Query>>>
{
self.queries.try_collect_active_jobs()
}

Expand Down Expand Up @@ -365,7 +366,7 @@ macro_rules! define_queries_inner {
type Cache = query_storage!([$($modifiers)*][$($K)*, $V]);

#[inline(always)]
fn query_state<'a>(tcx: TyCtxt<$tcx>) -> &'a QueryState<TyCtxt<$tcx>, Self::Cache> {
fn query_state<'a>(tcx: TyCtxt<$tcx>) -> &'a QueryState<crate::dep_graph::DepKind, <TyCtxt<$tcx> as QueryContext>::Query, Self::Cache> {
&tcx.queries.$name
}

Expand Down Expand Up @@ -520,7 +521,8 @@ macro_rules! define_queries_struct {
fallback_extern_providers: Box<Providers>,

$($(#[$attr])* $name: QueryState<
TyCtxt<$tcx>,
crate::dep_graph::DepKind,
<TyCtxt<$tcx> as QueryContext>::Query,
<queries::$name<$tcx> as QueryAccessors<TyCtxt<'tcx>>>::Cache,
>,)*
}
Expand All @@ -541,7 +543,7 @@ macro_rules! define_queries_struct {

pub(crate) fn try_collect_active_jobs(
&self
) -> Option<FxHashMap<QueryJobId<crate::dep_graph::DepKind>, QueryJobInfo<TyCtxt<'tcx>>>> {
) -> Option<FxHashMap<QueryJobId<crate::dep_graph::DepKind>, QueryJobInfo<crate::dep_graph::DepKind, <TyCtxt<$tcx> as QueryContext>::Query>>> {
let mut jobs = FxHashMap::default();

$(
Expand Down
5 changes: 2 additions & 3 deletions compiler/rustc_middle/src/ty/query/profiling_support.rs
Expand Up @@ -5,8 +5,7 @@ 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_query_system::query::QueryCache;
use rustc_query_system::query::QueryState;
use rustc_query_system::query::{QueryCache, QueryContext, QueryState};
use std::fmt::Debug;
use std::io::Write;

Expand Down Expand Up @@ -231,7 +230,7 @@ where
pub(super) fn alloc_self_profile_query_strings_for_query_cache<'tcx, C>(
tcx: TyCtxt<'tcx>,
query_name: &'static str,
query_state: &QueryState<TyCtxt<'tcx>, C>,
query_state: &QueryState<crate::dep_graph::DepKind, <TyCtxt<'tcx> as QueryContext>::Query, C>,
string_cache: &mut QueryKeyStringCache,
) where
C: QueryCache,
Expand Down
18 changes: 10 additions & 8 deletions compiler/rustc_middle/src/ty/query/stats.rs
@@ -1,11 +1,10 @@
use crate::ty::query::queries;
use crate::ty::TyCtxt;
use rustc_hir::def_id::{DefId, LOCAL_CRATE};
use rustc_query_system::query::QueryCache;
use rustc_query_system::query::QueryState;
use rustc_query_system::query::{QueryAccessors, QueryContext};
use rustc_query_system::query::{QueryAccessors, QueryCache, QueryContext, QueryState};

use std::any::type_name;
use std::hash::Hash;
use std::mem;
#[cfg(debug_assertions)]
use std::sync::atomic::Ordering;
Expand Down Expand Up @@ -38,10 +37,12 @@ struct QueryStats {
local_def_id_keys: Option<usize>,
}

fn stats<CTX: QueryContext, C: QueryCache>(
name: &'static str,
map: &QueryState<CTX, C>,
) -> QueryStats {
fn stats<D, Q, C>(name: &'static str, map: &QueryState<D, Q, C>) -> QueryStats
where
D: Copy + Clone + Eq + Hash,
Q: Clone,
C: QueryCache,
{
let mut stats = QueryStats {
name,
#[cfg(debug_assertions)]
Expand Down Expand Up @@ -127,7 +128,8 @@ macro_rules! print_stats {

$($(
queries.push(stats::<
TyCtxt<'_>,
crate::dep_graph::DepKind,
<TyCtxt<'_> as QueryContext>::Query,
<queries::$name<'_> as QueryAccessors<TyCtxt<'_>>>::Cache,
>(
stringify!($name),
Expand Down
33 changes: 20 additions & 13 deletions compiler/rustc_query_system/src/query/caches.rs
@@ -1,12 +1,12 @@
use crate::dep_graph::DepNodeIndex;
use crate::query::plumbing::{QueryLookup, QueryState};
use crate::query::QueryContext;

use rustc_arena::TypedArena;
use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::sharded::Sharded;
use rustc_data_structures::sync::WorkerLocal;
use std::default::Default;
use std::fmt::Debug;
use std::hash::Hash;
use std::marker::PhantomData;

Expand All @@ -24,24 +24,24 @@ pub trait QueryStorage: Default {
}

pub trait QueryCache: QueryStorage {
type Key: Hash;
type Key: Hash + Eq + Clone + Debug;
type Sharded: Default;

/// Checks if the query is already computed and in the cache.
/// It returns the shard index and a lock guard to the shard,
/// which will be used if the query is not in the cache and we need
/// to compute it.
fn lookup<CTX: QueryContext, R, OnHit, OnMiss>(
fn lookup<D, Q, R, OnHit, OnMiss>(
&self,
state: &QueryState<CTX, Self>,
state: &QueryState<D, Q, Self>,
key: Self::Key,
// `on_hit` can be called while holding a lock to the query state shard.
on_hit: OnHit,
on_miss: OnMiss,
) -> R
where
OnHit: FnOnce(&Self::Stored, DepNodeIndex) -> R,
OnMiss: FnOnce(Self::Key, QueryLookup<'_, CTX, Self::Key, Self::Sharded>) -> R;
OnMiss: FnOnce(Self::Key, QueryLookup<'_, D, Q, Self::Key, Self::Sharded>) -> R;

fn complete(
&self,
Expand Down Expand Up @@ -86,21 +86,25 @@ impl<K: Eq + Hash, V: Clone> QueryStorage for DefaultCache<K, V> {
}
}

impl<K: Eq + Hash, V: Clone> QueryCache for DefaultCache<K, V> {
impl<K, V> QueryCache for DefaultCache<K, V>
where
K: Eq + Hash + Clone + Debug,
V: Clone,
{
type Key = K;
type Sharded = FxHashMap<K, (V, DepNodeIndex)>;

#[inline(always)]
fn lookup<CTX: QueryContext, R, OnHit, OnMiss>(
fn lookup<D, Q, R, OnHit, OnMiss>(
&self,
state: &QueryState<CTX, Self>,
state: &QueryState<D, Q, Self>,
key: K,
on_hit: OnHit,
on_miss: OnMiss,
) -> R
where
OnHit: FnOnce(&V, DepNodeIndex) -> R,
OnMiss: FnOnce(K, QueryLookup<'_, CTX, K, Self::Sharded>) -> R,
OnMiss: FnOnce(K, QueryLookup<'_, D, Q, K, Self::Sharded>) -> R,
{
let mut lookup = state.get_lookup(&key);
let lock = &mut *lookup.lock;
Expand Down Expand Up @@ -164,21 +168,24 @@ impl<'tcx, K: Eq + Hash, V: 'tcx> QueryStorage for ArenaCache<'tcx, K, V> {
}
}

impl<'tcx, K: Eq + Hash, V: 'tcx> QueryCache for ArenaCache<'tcx, K, V> {
impl<'tcx, K, V: 'tcx> QueryCache for ArenaCache<'tcx, K, V>
where
K: Eq + Hash + Clone + Debug,
{
type Key = K;
type Sharded = FxHashMap<K, &'tcx (V, DepNodeIndex)>;

#[inline(always)]
fn lookup<CTX: QueryContext, R, OnHit, OnMiss>(
fn lookup<D, Q, R, OnHit, OnMiss>(
&self,
state: &QueryState<CTX, Self>,
state: &QueryState<D, Q, Self>,
key: K,
on_hit: OnHit,
on_miss: OnMiss,
) -> R
where
OnHit: FnOnce(&&'tcx V, DepNodeIndex) -> R,
OnMiss: FnOnce(K, QueryLookup<'_, CTX, K, Self::Sharded>) -> R,
OnMiss: FnOnce(K, QueryLookup<'_, D, Q, K, Self::Sharded>) -> R,
{
let mut lookup = state.get_lookup(&key);
let lock = &mut *lookup.lock;
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_query_system/src/query/config.rs
Expand Up @@ -76,7 +76,7 @@ pub trait QueryAccessors<CTX: QueryContext>: QueryConfig {
type Cache: QueryCache<Key = Self::Key, Stored = Self::Stored, Value = Self::Value>;

// Don't use this method to access query results, instead use the methods on TyCtxt
fn query_state<'a>(tcx: CTX) -> &'a QueryState<CTX, Self::Cache>;
fn query_state<'a>(tcx: CTX) -> &'a QueryState<CTX::DepKind, CTX::Query, Self::Cache>;

fn to_dep_node(tcx: CTX, key: &Self::Key) -> DepNode<CTX::DepKind>
where
Expand Down

0 comments on commit 52cedca

Please sign in to comment.