Skip to content

Commit 3fc8ed6

Browse files
committed
Check query cache before calling into the query engine.
1 parent 280a286 commit 3fc8ed6

File tree

2 files changed

+56
-20
lines changed

2 files changed

+56
-20
lines changed

Diff for: compiler/rustc_middle/src/ty/query/plumbing.rs

+21-3
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,15 @@ macro_rules! define_queries {
422422
$($(#[$attr])*
423423
#[inline(always)]
424424
pub fn $name(self, key: query_helper_param_ty!($($K)*)) {
425-
get_query::<queries::$name<'_>, _>(self.tcx, DUMMY_SP, key.into_query_param(), QueryMode::Ensure);
425+
let key = key.into_query_param();
426+
let cached = try_get_cached(self.tcx, &self.tcx.query_caches.$name, &key, |_| {});
427+
428+
let lookup = match cached {
429+
Ok(()) => return,
430+
Err(lookup) => lookup,
431+
};
432+
433+
get_query::<queries::$name<'_>, _>(self.tcx, DUMMY_SP, key, lookup, QueryMode::Ensure);
426434
})*
427435
}
428436

@@ -465,7 +473,7 @@ macro_rules! define_queries {
465473
#[must_use]
466474
pub fn $name(self, key: query_helper_param_ty!($($K)*)) -> query_stored::$name<$tcx>
467475
{
468-
self.at(DUMMY_SP).$name(key.into_query_param())
476+
self.at(DUMMY_SP).$name(key)
469477
})*
470478

471479
/// All self-profiling events generated by the query engine use
@@ -503,7 +511,17 @@ macro_rules! define_queries {
503511
#[inline(always)]
504512
pub fn $name(self, key: query_helper_param_ty!($($K)*)) -> query_stored::$name<$tcx>
505513
{
506-
get_query::<queries::$name<'_>, _>(self.tcx, self.span, key.into_query_param(), QueryMode::Get).unwrap()
514+
let key = key.into_query_param();
515+
let cached = try_get_cached(self.tcx, &self.tcx.query_caches.$name, &key, |value| {
516+
value.clone()
517+
});
518+
519+
let lookup = match cached {
520+
Ok(value) => return value,
521+
Err(lookup) => lookup,
522+
};
523+
524+
get_query::<queries::$name<'_>, _>(self.tcx, self.span, key, lookup, QueryMode::Get).unwrap()
507525
})*
508526
}
509527

Diff for: compiler/rustc_query_system/src/query/plumbing.rs

+35-17
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,18 @@ where
263263
return TryGetJob::Cycle(value);
264264
}
265265

266-
let cached = try_get_cached(tcx, cache, key, |value, index| (value.clone(), index))
266+
let cached = cache
267+
.cache
268+
.lookup(cache, &key, |value, index| {
269+
if unlikely!(tcx.profiler().enabled()) {
270+
tcx.profiler().query_cache_hit(index.into());
271+
}
272+
#[cfg(debug_assertions)]
273+
{
274+
cache.cache_hits.fetch_add(1, Ordering::Relaxed);
275+
}
276+
(value.clone(), index)
277+
})
267278
.unwrap_or_else(|_| panic!("value must be in cache after waiting"));
268279

269280
if let Some(prof_timer) = _query_blocked_prof_timer.take() {
@@ -374,7 +385,7 @@ where
374385
/// It returns the shard index and a lock guard to the shard,
375386
/// which will be used if the query is not in the cache and we need
376387
/// to compute it.
377-
fn try_get_cached<'a, CTX, C, R, OnHit>(
388+
pub fn try_get_cached<'a, CTX, C, R, OnHit>(
378389
tcx: CTX,
379390
cache: &'a QueryCacheStore<C>,
380391
key: &C::Key,
@@ -384,7 +395,7 @@ fn try_get_cached<'a, CTX, C, R, OnHit>(
384395
where
385396
C: QueryCache,
386397
CTX: QueryContext,
387-
OnHit: FnOnce(&C::Stored, DepNodeIndex) -> R,
398+
OnHit: FnOnce(&C::Stored) -> R,
388399
{
389400
cache.cache.lookup(cache, &key, |value, index| {
390401
if unlikely!(tcx.profiler().enabled()) {
@@ -394,7 +405,8 @@ where
394405
{
395406
cache.cache_hits.fetch_add(1, Ordering::Relaxed);
396407
}
397-
on_hit(value, index)
408+
tcx.dep_graph().read_index(index);
409+
on_hit(value)
398410
})
399411
}
400412

@@ -632,21 +644,15 @@ fn get_query_impl<CTX, C>(
632644
cache: &QueryCacheStore<C>,
633645
span: Span,
634646
key: C::Key,
647+
lookup: QueryLookup,
635648
query: &QueryVtable<CTX, C::Key, C::Value>,
636649
) -> C::Stored
637650
where
638651
CTX: QueryContext,
639652
C: QueryCache,
640653
C::Key: crate::dep_graph::DepNodeParams<CTX>,
641654
{
642-
let cached = try_get_cached(tcx, cache, &key, |value, index| {
643-
tcx.dep_graph().read_index(index);
644-
value.clone()
645-
});
646-
match cached {
647-
Ok(value) => value,
648-
Err(lookup) => try_execute_query(tcx, state, cache, span, key, lookup, query),
649-
}
655+
try_execute_query(tcx, state, cache, span, key, lookup, query)
650656
}
651657

652658
/// Ensure that either this query has all green inputs or been executed.
@@ -705,9 +711,14 @@ fn force_query_impl<CTX, C>(
705711
{
706712
// We may be concurrently trying both execute and force a query.
707713
// Ensure that only one of them runs the query.
708-
709-
let cached = try_get_cached(tcx, cache, &key, |_, _| {
710-
// Cache hit, do nothing
714+
let cached = cache.cache.lookup(cache, &key, |_, index| {
715+
if unlikely!(tcx.profiler().enabled()) {
716+
tcx.profiler().query_cache_hit(index.into());
717+
}
718+
#[cfg(debug_assertions)]
719+
{
720+
cache.cache_hits.fetch_add(1, Ordering::Relaxed);
721+
}
711722
});
712723

713724
let lookup = match cached {
@@ -731,7 +742,13 @@ pub enum QueryMode {
731742
Ensure,
732743
}
733744

734-
pub fn get_query<Q, CTX>(tcx: CTX, span: Span, key: Q::Key, mode: QueryMode) -> Option<Q::Stored>
745+
pub fn get_query<Q, CTX>(
746+
tcx: CTX,
747+
span: Span,
748+
key: Q::Key,
749+
lookup: QueryLookup,
750+
mode: QueryMode,
751+
) -> Option<Q::Stored>
735752
where
736753
Q: QueryDescription<CTX>,
737754
Q::Key: crate::dep_graph::DepNodeParams<CTX>,
@@ -745,7 +762,8 @@ where
745762
}
746763

747764
debug!("ty::query::get_query<{}>(key={:?}, span={:?})", Q::NAME, key, span);
748-
let value = get_query_impl(tcx, Q::query_state(tcx), Q::query_cache(tcx), span, key, query);
765+
let value =
766+
get_query_impl(tcx, Q::query_state(tcx), Q::query_cache(tcx), span, key, lookup, query);
749767
Some(value)
750768
}
751769

0 commit comments

Comments
 (0)