Skip to content
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

Move most of TyCtxtAt::$name into a generic evaluate_query function #101178

Closed
wants to merge 2 commits into from
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
48 changes: 24 additions & 24 deletions compiler/rustc_middle/src/ty/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,28 @@ impl<'tcx> TyCtxt<'tcx> {
}
}

/// Helper for `TyCtxtEnsure` to avoid a closure.
#[inline(always)]
fn noop<T>(_: &T) {}

/// Helper to ensure that queries only return `Copy` types.
#[inline(always)]
fn copy<T: Copy>(x: &T) -> T {
*x
fn evaluate_query<'tcx, Cache>(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you try this with #[inline(always)]? The callers all have that so this should regress without it.

tcx: TyCtxt<'tcx>,
execute_query: fn(
&'tcx dyn QueryEngine<'tcx>,
TyCtxt<'tcx>,
Span,
Cache::Key,
QueryMode,
) -> Option<Cache::Stored>,
query_cache: &Cache,
span: Span,
key: Cache::Key,
mode: QueryMode,
) -> Option<Cache::Stored>
where
Cache::Stored: Copy,
Cache: QueryCache,
{
match try_get_cached(tcx, query_cache, &key, mode) {
Ok(value) => value,
Err(()) => execute_query(tcx.queries, tcx, span, key, mode),
}
}

macro_rules! query_helper_param_ty {
Expand Down Expand Up @@ -220,14 +234,7 @@ macro_rules! define_callbacks {
let key = key.into_query_param();
opt_remap_env_constness!([$($modifiers)*][key]);

let cached = try_get_cached(self.tcx, &self.tcx.query_caches.$name, &key, noop);

match cached {
Ok(()) => return,
Err(()) => (),
}

self.tcx.queries.$name(self.tcx, DUMMY_SP, key, QueryMode::Ensure);
let _ = evaluate_query(self.tcx, QueryEngine::$name, &self.tcx.query_caches.$name, DUMMY_SP, key, QueryMode::Ensure);
})*
}

Expand All @@ -249,14 +256,7 @@ macro_rules! define_callbacks {
let key = key.into_query_param();
opt_remap_env_constness!([$($modifiers)*][key]);

let cached = try_get_cached(self.tcx, &self.tcx.query_caches.$name, &key, copy);

match cached {
Ok(value) => return value,
Err(()) => (),
}

self.tcx.queries.$name(self.tcx, self.span, key, QueryMode::Get).unwrap()
evaluate_query(self.tcx, QueryEngine::$name, &self.tcx.query_caches.$name, self.span, key, QueryMode::Get).unwrap()
})*
}

Expand Down
13 changes: 6 additions & 7 deletions compiler/rustc_query_system/src/query/plumbing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,24 +303,23 @@ where
/// which will be used if the query is not in the cache and we need
/// to compute it.
#[inline]
pub fn try_get_cached<'a, CTX, C, R, OnHit>(
pub fn try_get_cached<'a, CTX, C>(
tcx: CTX,
cache: &'a C,
key: &C::Key,
// `on_hit` can be called while holding a lock to the query cache
on_hit: OnHit,
) -> Result<R, ()>
mode: QueryMode,
) -> Result<Option<C::Stored>, ()>
where
C: QueryCache,
C::Stored: Copy,
CTX: DepContext,
OnHit: FnOnce(&C::Stored) -> R,
{
cache.lookup(&key, |value, index| {
if std::intrinsics::unlikely(tcx.profiler().enabled()) {
tcx.profiler().query_cache_hit(index.into());
}
tcx.dep_graph().read_index(index);
on_hit(value)
if matches!(mode, QueryMode::Ensure) { None } else { Some(*value) }
})
}

Expand Down Expand Up @@ -676,7 +675,7 @@ where
}
}

#[derive(Debug)]
#[derive(Copy, Clone, Debug)]
pub enum QueryMode {
Get,
Ensure,
Expand Down