Skip to content
Closed
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
21 changes: 20 additions & 1 deletion compiler/rustc_middle/src/query/inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,33 @@
//! `tcx.$query(..)` and its variations.

use rustc_query_system::dep_graph::{DepKind, DepNodeKey};
use rustc_query_system::query::{QueryCache, QueryMode, try_get_cached};
use rustc_query_system::query::{QueryCache, QueryMode};
use rustc_span::{DUMMY_SP, ErrorGuaranteed, Span};

use crate::dep_graph;
use crate::query::erase::{self, Erasable, Erased};
use crate::query::plumbing::QueryVTable;
use crate::ty::TyCtxt;

/// Checks whether there is already a value for this key in the in-memory
/// query cache, returning that value if present.
///
/// (Also performs some associated bookkeeping, if a value was found.)
#[inline(always)]
fn try_get_cached<'tcx, C>(tcx: TyCtxt<'tcx>, cache: &C, key: &C::Key) -> Option<C::Value>
where
C: QueryCache,
{
match cache.lookup(key) {
Some((value, index)) => {
tcx.prof.query_cache_hit(index.into());
tcx.dep_graph.read_index(index);
Some(value)
}
None => None,
}
}

/// Shared implementation of `tcx.$query(..)` and `tcx.at(span).$query(..)`
/// for all queries.
#[inline(always)]
Expand Down
70 changes: 32 additions & 38 deletions compiler/rustc_query_impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
// tidy-alphabetical-start
#![allow(internal_features)]
#![feature(adt_const_params)]
#![feature(core_intrinsics)]
#![feature(min_specialization)]
#![feature(rustc_attrs)]
// tidy-alphabetical-end

use std::marker::ConstParamTy;

use rustc_data_structures::stable_hasher::HashStable;
use rustc_data_structures::sync::AtomicU64;
use rustc_middle::arena::Arena;
use rustc_middle::dep_graph::{self, DepKind, DepKindVTable, DepNodeIndex};
use rustc_middle::dep_graph::{self, DepKind, DepKindVTable, DepNode, DepNodeIndex};
use rustc_middle::queries::{
self, ExternProviders, Providers, QueryCaches, QueryEngine, QueryStates,
};
Expand All @@ -22,10 +22,8 @@ use rustc_middle::query::plumbing::{QuerySystem, QuerySystemFns, QueryVTable};
use rustc_middle::query::values::Value;
use rustc_middle::ty::TyCtxt;
use rustc_query_system::dep_graph::SerializedDepNodeIndex;
use rustc_query_system::ich::StableHashingContext;
use rustc_query_system::query::{
CycleError, CycleErrorHandling, HashResult, QueryCache, QueryDispatcher, QueryMap, QueryMode,
QueryState, get_query_incr, get_query_non_incr,
CycleError, CycleErrorHandling, HashResult, QueryCache, QueryMap, QueryMode, QueryState,
};
use rustc_span::{ErrorGuaranteed, Span};

Expand All @@ -52,8 +50,8 @@ struct QueryFlags {
is_feedable: bool,
}

/// Combines a [`QueryVTable`] with some additional compile-time booleans
/// to implement [`QueryDispatcher`], for use by code in [`rustc_query_system`].
/// Combines a [`QueryVTable`] with some additional compile-time booleans.
/// "Dispatcher" should be understood as a near-synonym of "vtable".
///
/// Baking these boolean flags into the type gives a modest but measurable
/// improvement to compiler perf and compiler code size; see
Expand All @@ -75,67 +73,60 @@ impl<'tcx, C: QueryCache, const FLAGS: QueryFlags> Clone
}
}

// This is `impl QueryDispatcher for SemiDynamicQueryDispatcher`.
impl<'tcx, C: QueryCache, const FLAGS: QueryFlags> QueryDispatcher<'tcx>
for SemiDynamicQueryDispatcher<'tcx, C, FLAGS>
where
for<'a> C::Key: HashStable<StableHashingContext<'a>>,
{
type Qcx = QueryCtxt<'tcx>;
type Key = C::Key;
type Value = C::Value;
type Cache = C;

impl<'tcx, C: QueryCache, const FLAGS: QueryFlags> SemiDynamicQueryDispatcher<'tcx, C, FLAGS> {
#[inline(always)]
fn name(self) -> &'static str {
self.vtable.name
}

#[inline(always)]
fn will_cache_on_disk_for_key(self, tcx: TyCtxt<'tcx>, key: &Self::Key) -> bool {
fn will_cache_on_disk_for_key(self, tcx: TyCtxt<'tcx>, key: &C::Key) -> bool {
self.vtable.will_cache_on_disk_for_key_fn.map_or(false, |f| f(tcx, key))
}

// Don't use this method to access query results, instead use the methods on TyCtxt.
#[inline(always)]
fn query_state(self, qcx: QueryCtxt<'tcx>) -> &'tcx QueryState<'tcx, Self::Key> {
fn query_state(self, qcx: QueryCtxt<'tcx>) -> &'tcx QueryState<'tcx, C::Key> {
// Safety:
// This is just manually doing the subfield referencing through pointer math.
unsafe {
&*(&qcx.tcx.query_system.states as *const QueryStates<'tcx>)
.byte_add(self.vtable.query_state)
.cast::<QueryState<'tcx, Self::Key>>()
.cast::<QueryState<'tcx, C::Key>>()
}
}

// Don't use this method to access query results, instead use the methods on TyCtxt.
#[inline(always)]
fn query_cache(self, qcx: QueryCtxt<'tcx>) -> &'tcx Self::Cache {
fn query_cache(self, qcx: QueryCtxt<'tcx>) -> &'tcx C {
// Safety:
// This is just manually doing the subfield referencing through pointer math.
unsafe {
&*(&qcx.tcx.query_system.caches as *const QueryCaches<'tcx>)
.byte_add(self.vtable.query_cache)
.cast::<Self::Cache>()
.cast::<C>()
}
}

// Don't use this method to compute query results, instead use the methods on TyCtxt.
#[inline(always)]
fn execute_query(self, tcx: TyCtxt<'tcx>, key: Self::Key) -> Self::Value {
fn execute_query(self, tcx: TyCtxt<'tcx>, key: C::Key) -> C::Value {
(self.vtable.execute_query)(tcx, key)
}

#[inline(always)]
fn compute(self, qcx: QueryCtxt<'tcx>, key: Self::Key) -> Self::Value {
fn compute(self, qcx: QueryCtxt<'tcx>, key: C::Key) -> C::Value {
(self.vtable.compute_fn)(qcx.tcx, key)
}

#[inline(always)]
fn try_load_from_disk(
self,
qcx: QueryCtxt<'tcx>,
key: &Self::Key,
key: &C::Key,
prev_index: SerializedDepNodeIndex,
index: DepNodeIndex,
) -> Option<Self::Value> {
) -> Option<C::Value> {
// `?` will return None immediately for queries that never cache to disk.
self.vtable.try_load_from_disk_fn?(qcx.tcx, key, prev_index, index)
}
Expand All @@ -144,23 +135,24 @@ where
fn is_loadable_from_disk(
self,
qcx: QueryCtxt<'tcx>,
key: &Self::Key,
key: &C::Key,
index: SerializedDepNodeIndex,
) -> bool {
self.vtable.is_loadable_from_disk_fn.map_or(false, |f| f(qcx.tcx, key, index))
}

/// Synthesize an error value to let compilation continue after a cycle.
fn value_from_cycle_error(
self,
tcx: TyCtxt<'tcx>,
cycle_error: &CycleError,
guar: ErrorGuaranteed,
) -> Self::Value {
) -> C::Value {
(self.vtable.value_from_cycle_error)(tcx, cycle_error, guar)
}

#[inline(always)]
fn format_value(self) -> fn(&Self::Value) -> String {
fn format_value(self) -> fn(&C::Value) -> String {
self.vtable.format_value
}

Expand Down Expand Up @@ -195,13 +187,17 @@ where
}

#[inline(always)]
fn hash_result(self) -> HashResult<Self::Value> {
fn hash_result(self) -> HashResult<C::Value> {
self.vtable.hash_result
}

fn construct_dep_node(self, tcx: TyCtxt<'tcx>, key: &C::Key) -> DepNode {
DepNode::construct(tcx, self.dep_kind(), key)
}
}

/// Provides access to vtable-like operations for a query
/// (by creating a [`QueryDispatcher`]),
/// (by creating a [`SemiDynamicQueryDispatcher`]),
/// but also keeps track of the "unerased" value type of the query
/// (i.e. the actual result type in the query declaration).
///
Expand All @@ -211,17 +207,15 @@ where
///
/// There is one macro-generated implementation of this trait for each query,
/// on the type `rustc_query_impl::query_impl::$name::QueryType`.
trait QueryDispatcherUnerased<'tcx> {
trait QueryDispatcherUnerased<'tcx, C: QueryCache, const FLAGS: QueryFlags> {
type UnerasedValue;
type Dispatcher: QueryDispatcher<'tcx, Qcx = QueryCtxt<'tcx>>;
//type Dispatcher: QueryDispatcher<'tcx, Qcx = QueryCtxt<'tcx>>; // njn: remove

const NAME: &'static &'static str;

fn query_dispatcher(tcx: TyCtxt<'tcx>) -> Self::Dispatcher;
fn query_dispatcher(tcx: TyCtxt<'tcx>) -> SemiDynamicQueryDispatcher<'tcx, C, FLAGS>;

fn restore_val(
value: <Self::Dispatcher as QueryDispatcher<'tcx>>::Value,
) -> Self::UnerasedValue;
fn restore_val(value: C::Value) -> Self::UnerasedValue;
}

pub fn query_system<'tcx>(
Expand Down
Loading
Loading