Add query accessor functions

This commit is contained in:
John Kåre Alsaker 2023-03-25 11:47:23 +01:00
parent 66d85438ca
commit 4440e8196a
2 changed files with 56 additions and 33 deletions

View File

@ -202,6 +202,40 @@ impl<'tcx> TyCtxt<'tcx> {
} }
} }
#[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 });
}
}
macro_rules! query_helper_param_ty { macro_rules! query_helper_param_ty {
(DefId) => { impl IntoQueryParam<DefId> }; (DefId) => { impl IntoQueryParam<DefId> };
(LocalDefId) => { impl IntoQueryParam<LocalDefId> }; (LocalDefId) => { impl IntoQueryParam<LocalDefId> };
@ -407,17 +441,13 @@ macro_rules! define_callbacks {
$($(#[$attr])* $($(#[$attr])*
#[inline(always)] #[inline(always)]
pub fn $name(self, key: query_helper_param_ty!($($K)*)) { pub fn $name(self, key: query_helper_param_ty!($($K)*)) {
let key = key.into_query_param(); query_ensure(
self.tcx,
match try_get_cached(self.tcx, &self.tcx.query_system.caches.$name, &key) { self.tcx.query_system.fns.engine.$name,
Some(_) => return, &self.tcx.query_system.caches.$name,
None => (self.tcx.query_system.fns.engine.$name)( key.into_query_param(),
self.tcx, false,
DUMMY_SP, );
key,
QueryMode::Ensure { check_cache: false },
),
};
})* })*
} }
@ -425,17 +455,13 @@ macro_rules! define_callbacks {
$($(#[$attr])* $($(#[$attr])*
#[inline(always)] #[inline(always)]
pub fn $name(self, key: query_helper_param_ty!($($K)*)) { pub fn $name(self, key: query_helper_param_ty!($($K)*)) {
let key = key.into_query_param(); query_ensure(
self.tcx,
match try_get_cached(self.tcx, &self.tcx.query_system.caches.$name, &key) { self.tcx.query_system.fns.engine.$name,
Some(_) => return, &self.tcx.query_system.caches.$name,
None => (self.tcx.query_system.fns.engine.$name)( key.into_query_param(),
self.tcx, true,
DUMMY_SP, );
key,
QueryMode::Ensure { check_cache: true },
),
};
})* })*
} }
@ -454,16 +480,13 @@ macro_rules! define_callbacks {
#[inline(always)] #[inline(always)]
pub fn $name(self, key: query_helper_param_ty!($($K)*)) -> $V pub fn $name(self, key: query_helper_param_ty!($($K)*)) -> $V
{ {
let key = key.into_query_param(); restore::<$V>(query_get_at(
self.tcx,
restore::<$V>(match try_get_cached(self.tcx, &self.tcx.query_system.caches.$name, &key) { self.tcx.query_system.fns.engine.$name,
Some(value) => value, &self.tcx.query_system.caches.$name,
None => (self.tcx.query_system.fns.engine.$name)( self.span,
self.tcx, key.into_query_param(),
self.span, ))
key, QueryMode::Get
).unwrap(),
})
})* })*
} }

View File

@ -236,7 +236,7 @@ pub(crate) struct CycleError<D: DepKind> {
/// It returns the shard index and a lock guard to the shard, /// 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 /// which will be used if the query is not in the cache and we need
/// to compute it. /// to compute it.
#[inline] #[inline(always)]
pub fn try_get_cached<Tcx, C>(tcx: Tcx, cache: &C, key: &C::Key) -> Option<C::Value> pub fn try_get_cached<Tcx, C>(tcx: Tcx, cache: &C, key: &C::Key) -> Option<C::Value>
where where
C: QueryCache, C: QueryCache,