Skip to content

Abolish QueryVTable in favour of more assoc items on QueryConfig #106307

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion compiler/rustc_query_impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ use rustc_query_system::query::*;
pub use rustc_query_system::query::{deadlock, QueryContext};

pub use rustc_query_system::query::QueryConfig;
pub(crate) use rustc_query_system::query::QueryVTable;

mod on_disk_cache;
pub use on_disk_cache::OnDiskCache;
Expand Down
40 changes: 22 additions & 18 deletions compiler/rustc_query_impl/src/plumbing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -493,28 +493,32 @@ macro_rules! define_queries {
&tcx.query_caches.$name
}

fn execute_query(tcx: TyCtxt<'tcx>, key: Self::Key) -> Self::Stored {
tcx.$name(key)
}

#[inline]
fn make_vtable(tcx: QueryCtxt<'tcx>, key: &Self::Key) ->
QueryVTable<QueryCtxt<'tcx>, Self::Key, Self::Value>
{
let compute = get_provider!([$($modifiers)*][tcx, $name, key]);
let cache_on_disk = Self::cache_on_disk(tcx.tcx, key);
QueryVTable {
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,
try_load_from_disk: if cache_on_disk { should_ever_cache_on_disk!([$($modifiers)*]) } else { None },
}
// key is only sometimes used
#[allow(unused_variables)]
fn compute(qcx: QueryCtxt<'tcx>, key: &Self::Key) -> fn(TyCtxt<'tcx>, Self::Key) -> Self::Value {
get_provider!([$($modifiers)*][qcx, $name, key])
}

fn execute_query(tcx: TyCtxt<'tcx>, k: Self::Key) -> Self::Stored {
tcx.$name(k)
#[inline]
fn try_load_from_disk(qcx: QueryCtxt<'tcx>, key: &Self::Key) -> rustc_query_system::query::TryLoadFromDisk<QueryCtxt<'tcx>, Self> {
let cache_on_disk = Self::cache_on_disk(qcx.tcx, key);
if cache_on_disk { should_ever_cache_on_disk!([$($modifiers)*]) } else { None }
}

const ANON: bool = is_anon!([$($modifiers)*]);
const EVAL_ALWAYS: bool = is_eval_always!([$($modifiers)*]);
const DEPTH_LIMIT: bool = depth_limit!([$($modifiers)*]);
const FEEDABLE: bool = feedable!([$($modifiers)*]);

const DEP_KIND: rustc_middle::dep_graph::DepKind = dep_graph::DepKind::$name;
const HANDLE_CYCLE_ERROR: rustc_query_system::HandleCycleError = handle_cycle_error!([$($modifiers)*]);

const HASH_RESULT: rustc_query_system::query::HashResult<QueryCtxt<'tcx>, Self> = hash_result!([$($modifiers)*]);
})*

#[allow(nonstandard_style)]
Expand Down
53 changes: 23 additions & 30 deletions compiler/rustc_query_system/src/query/config.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//! Query configuration and description traits.

use crate::dep_graph::DepNode;
use crate::dep_graph::SerializedDepNodeIndex;
use crate::dep_graph::{DepNode, DepNodeParams, SerializedDepNodeIndex};
use crate::error::HandleCycleError;
use crate::ich::StableHashingContext;
use crate::query::caches::QueryCache;
Expand All @@ -11,10 +10,16 @@ use rustc_data_structures::fingerprint::Fingerprint;
use std::fmt::Debug;
use std::hash::Hash;

pub type HashResult<Qcx, Q> =
Option<fn(&mut StableHashingContext<'_>, &<Q as QueryConfig<Qcx>>::Value) -> Fingerprint>;

pub type TryLoadFromDisk<Qcx, Q> =
Option<fn(Qcx, SerializedDepNodeIndex) -> Option<<Q as QueryConfig<Qcx>>::Value>>;

pub trait QueryConfig<Qcx: QueryContext> {
const NAME: &'static str;

type Key: Eq + Hash + Clone + Debug;
type Key: DepNodeParams<Qcx::DepContext> + Eq + Hash + Clone + Debug;
type Value: Debug;
type Stored: Debug + Clone + std::borrow::Borrow<Self::Value>;

Expand All @@ -30,39 +35,27 @@ pub trait QueryConfig<Qcx: QueryContext> {
where
Qcx: 'a;

// Don't use this method to compute query results, instead use the methods on TyCtxt
fn make_vtable(tcx: Qcx, key: &Self::Key) -> QueryVTable<Qcx, Self::Key, Self::Value>;

fn cache_on_disk(tcx: Qcx::DepContext, key: &Self::Key) -> bool;

// Don't use this method to compute query results, instead use the methods on TyCtxt
fn execute_query(tcx: Qcx::DepContext, k: Self::Key) -> Self::Stored;
}

#[derive(Copy, Clone)]
pub struct QueryVTable<Qcx: QueryContext, K, V> {
pub anon: bool,
pub dep_kind: Qcx::DepKind,
pub eval_always: bool,
pub depth_limit: bool,
pub feedable: bool,

pub compute: fn(Qcx::DepContext, K) -> V,
pub hash_result: Option<fn(&mut StableHashingContext<'_>, &V) -> Fingerprint>,
pub handle_cycle_error: HandleCycleError,
// NOTE: this is also `None` if `cache_on_disk()` returns false, not just if it's unsupported by the query
pub try_load_from_disk: Option<fn(Qcx, SerializedDepNodeIndex) -> Option<V>>,
}
fn compute(tcx: Qcx, key: &Self::Key) -> fn(Qcx::DepContext, Self::Key) -> Self::Value;

impl<Qcx: QueryContext, K, V> QueryVTable<Qcx, K, V> {
pub(crate) fn to_dep_node(&self, tcx: Qcx::DepContext, key: &K) -> DepNode<Qcx::DepKind>
where
K: crate::dep_graph::DepNodeParams<Qcx::DepContext>,
{
DepNode::construct(tcx, self.dep_kind, key)
}
fn try_load_from_disk(qcx: Qcx, idx: &Self::Key) -> TryLoadFromDisk<Qcx, Self>;

const ANON: bool;
const EVAL_ALWAYS: bool;
const DEPTH_LIMIT: bool;
const FEEDABLE: bool;

const DEP_KIND: Qcx::DepKind;
const HANDLE_CYCLE_ERROR: HandleCycleError;

const HASH_RESULT: HashResult<Qcx, Self>;

pub(crate) fn compute(&self, tcx: Qcx::DepContext, key: K) -> V {
(self.compute)(tcx, key)
// Just here for convernience and checking that the key matches the kind, don't override this.
fn construct_dep_node(tcx: Qcx::DepContext, key: &Self::Key) -> DepNode<Qcx::DepKind> {
DepNode::construct(tcx, Self::DEP_KIND, key)
}
}
2 changes: 1 addition & 1 deletion compiler/rustc_query_system/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub use self::caches::{
};

mod config;
pub use self::config::{QueryConfig, QueryVTable};
pub use self::config::{HashResult, QueryConfig, TryLoadFromDisk};

use crate::dep_graph::DepKind;
use crate::dep_graph::{DepNodeIndex, HasDepContext, SerializedDepNodeIndex};
Expand Down
Loading