Skip to content

Commit 4440e81

Browse files
committed
Add query accessor functions
1 parent 66d8543 commit 4440e81

File tree

2 files changed

+56
-33
lines changed

2 files changed

+56
-33
lines changed

compiler/rustc_middle/src/ty/query.rs

+55-32
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,40 @@ impl<'tcx> TyCtxt<'tcx> {
202202
}
203203
}
204204

205+
#[inline]
206+
fn query_get_at<'tcx, Cache>(
207+
tcx: TyCtxt<'tcx>,
208+
execute_query: fn(TyCtxt<'tcx>, Span, Cache::Key, QueryMode) -> Option<Cache::Value>,
209+
query_cache: &Cache,
210+
span: Span,
211+
key: Cache::Key,
212+
) -> Cache::Value
213+
where
214+
Cache: QueryCache,
215+
{
216+
let key = key.into_query_param();
217+
match try_get_cached(tcx, query_cache, &key) {
218+
Some(value) => value,
219+
None => execute_query(tcx, span, key, QueryMode::Get).unwrap(),
220+
}
221+
}
222+
223+
#[inline]
224+
fn query_ensure<'tcx, Cache>(
225+
tcx: TyCtxt<'tcx>,
226+
execute_query: fn(TyCtxt<'tcx>, Span, Cache::Key, QueryMode) -> Option<Cache::Value>,
227+
query_cache: &Cache,
228+
key: Cache::Key,
229+
check_cache: bool,
230+
) where
231+
Cache: QueryCache,
232+
{
233+
let key = key.into_query_param();
234+
if try_get_cached(tcx, query_cache, &key).is_none() {
235+
execute_query(tcx, DUMMY_SP, key, QueryMode::Ensure { check_cache });
236+
}
237+
}
238+
205239
macro_rules! query_helper_param_ty {
206240
(DefId) => { impl IntoQueryParam<DefId> };
207241
(LocalDefId) => { impl IntoQueryParam<LocalDefId> };
@@ -407,35 +441,27 @@ macro_rules! define_callbacks {
407441
$($(#[$attr])*
408442
#[inline(always)]
409443
pub fn $name(self, key: query_helper_param_ty!($($K)*)) {
410-
let key = key.into_query_param();
411-
412-
match try_get_cached(self.tcx, &self.tcx.query_system.caches.$name, &key) {
413-
Some(_) => return,
414-
None => (self.tcx.query_system.fns.engine.$name)(
415-
self.tcx,
416-
DUMMY_SP,
417-
key,
418-
QueryMode::Ensure { check_cache: false },
419-
),
420-
};
444+
query_ensure(
445+
self.tcx,
446+
self.tcx.query_system.fns.engine.$name,
447+
&self.tcx.query_system.caches.$name,
448+
key.into_query_param(),
449+
false,
450+
);
421451
})*
422452
}
423453

424454
impl<'tcx> TyCtxtEnsureWithValue<'tcx> {
425455
$($(#[$attr])*
426456
#[inline(always)]
427457
pub fn $name(self, key: query_helper_param_ty!($($K)*)) {
428-
let key = key.into_query_param();
429-
430-
match try_get_cached(self.tcx, &self.tcx.query_system.caches.$name, &key) {
431-
Some(_) => return,
432-
None => (self.tcx.query_system.fns.engine.$name)(
433-
self.tcx,
434-
DUMMY_SP,
435-
key,
436-
QueryMode::Ensure { check_cache: true },
437-
),
438-
};
458+
query_ensure(
459+
self.tcx,
460+
self.tcx.query_system.fns.engine.$name,
461+
&self.tcx.query_system.caches.$name,
462+
key.into_query_param(),
463+
true,
464+
);
439465
})*
440466
}
441467

@@ -454,16 +480,13 @@ macro_rules! define_callbacks {
454480
#[inline(always)]
455481
pub fn $name(self, key: query_helper_param_ty!($($K)*)) -> $V
456482
{
457-
let key = key.into_query_param();
458-
459-
restore::<$V>(match try_get_cached(self.tcx, &self.tcx.query_system.caches.$name, &key) {
460-
Some(value) => value,
461-
None => (self.tcx.query_system.fns.engine.$name)(
462-
self.tcx,
463-
self.span,
464-
key, QueryMode::Get
465-
).unwrap(),
466-
})
483+
restore::<$V>(query_get_at(
484+
self.tcx,
485+
self.tcx.query_system.fns.engine.$name,
486+
&self.tcx.query_system.caches.$name,
487+
self.span,
488+
key.into_query_param(),
489+
))
467490
})*
468491
}
469492

compiler/rustc_query_system/src/query/plumbing.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ pub(crate) struct CycleError<D: DepKind> {
236236
/// It returns the shard index and a lock guard to the shard,
237237
/// which will be used if the query is not in the cache and we need
238238
/// to compute it.
239-
#[inline]
239+
#[inline(always)]
240240
pub fn try_get_cached<Tcx, C>(tcx: Tcx, cache: &C, key: &C::Key) -> Option<C::Value>
241241
where
242242
C: QueryCache,

0 commit comments

Comments
 (0)