Skip to content

Commit 9f46259

Browse files
committed
Return a Result for query cache.
1 parent f8ab649 commit 9f46259

File tree

2 files changed

+68
-87
lines changed

2 files changed

+68
-87
lines changed

compiler/rustc_query_system/src/query/caches.rs

+26-30
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,15 @@ pub trait QueryCache: QueryStorage {
3131
/// It returns the shard index and a lock guard to the shard,
3232
/// which will be used if the query is not in the cache and we need
3333
/// to compute it.
34-
fn lookup<D, Q, R, OnHit, OnMiss>(
34+
fn lookup<'s, D, Q, R, OnHit>(
3535
&self,
36-
state: &QueryState<D, Q, Self>,
37-
key: Self::Key,
36+
state: &'s QueryState<D, Q, Self>,
37+
key: &Self::Key,
3838
// `on_hit` can be called while holding a lock to the query state shard.
3939
on_hit: OnHit,
40-
on_miss: OnMiss,
41-
) -> R
40+
) -> Result<R, QueryLookup<'s, D, Q, Self::Key, Self::Sharded>>
4241
where
43-
OnHit: FnOnce(&Self::Stored, DepNodeIndex) -> R,
44-
OnMiss: FnOnce(Self::Key, QueryLookup<'_, D, Q, Self::Key, Self::Sharded>) -> R;
42+
OnHit: FnOnce(&Self::Stored, DepNodeIndex) -> R;
4543

4644
fn complete(
4745
&self,
@@ -95,23 +93,24 @@ where
9593
type Sharded = FxHashMap<K, (V, DepNodeIndex)>;
9694

9795
#[inline(always)]
98-
fn lookup<D, Q, R, OnHit, OnMiss>(
96+
fn lookup<'s, D, Q, R, OnHit>(
9997
&self,
100-
state: &QueryState<D, Q, Self>,
101-
key: K,
98+
state: &'s QueryState<D, Q, Self>,
99+
key: &K,
102100
on_hit: OnHit,
103-
on_miss: OnMiss,
104-
) -> R
101+
) -> Result<R, QueryLookup<'s, D, Q, K, Self::Sharded>>
105102
where
106103
OnHit: FnOnce(&V, DepNodeIndex) -> R,
107-
OnMiss: FnOnce(K, QueryLookup<'_, D, Q, K, Self::Sharded>) -> R,
108104
{
109-
let mut lookup = state.get_lookup(&key);
110-
let lock = &mut *lookup.lock;
105+
let lookup = state.get_lookup(key);
106+
let result = lookup.lock.cache.raw_entry().from_key_hashed_nocheck(lookup.key_hash, key);
111107

112-
let result = lock.cache.raw_entry().from_key_hashed_nocheck(lookup.key_hash, &key);
113-
114-
if let Some((_, value)) = result { on_hit(&value.0, value.1) } else { on_miss(key, lookup) }
108+
if let Some((_, value)) = result {
109+
let hit_result = on_hit(&value.0, value.1);
110+
Ok(hit_result)
111+
} else {
112+
Err(lookup)
113+
}
115114
}
116115

117116
#[inline]
@@ -177,26 +176,23 @@ where
177176
type Sharded = FxHashMap<K, &'tcx (V, DepNodeIndex)>;
178177

179178
#[inline(always)]
180-
fn lookup<D, Q, R, OnHit, OnMiss>(
179+
fn lookup<'s, D, Q, R, OnHit>(
181180
&self,
182-
state: &QueryState<D, Q, Self>,
183-
key: K,
181+
state: &'s QueryState<D, Q, Self>,
182+
key: &K,
184183
on_hit: OnHit,
185-
on_miss: OnMiss,
186-
) -> R
184+
) -> Result<R, QueryLookup<'s, D, Q, K, Self::Sharded>>
187185
where
188186
OnHit: FnOnce(&&'tcx V, DepNodeIndex) -> R,
189-
OnMiss: FnOnce(K, QueryLookup<'_, D, Q, K, Self::Sharded>) -> R,
190187
{
191-
let mut lookup = state.get_lookup(&key);
192-
let lock = &mut *lookup.lock;
193-
194-
let result = lock.cache.raw_entry().from_key_hashed_nocheck(lookup.key_hash, &key);
188+
let lookup = state.get_lookup(key);
189+
let result = lookup.lock.cache.raw_entry().from_key_hashed_nocheck(lookup.key_hash, key);
195190

196191
if let Some((_, value)) = result {
197-
on_hit(&&value.0, value.1)
192+
let hit_result = on_hit(&&value.0, value.1);
193+
Ok(hit_result)
198194
} else {
199-
on_miss(key, lookup)
195+
Err(lookup)
200196
}
201197
}
202198

compiler/rustc_query_system/src/query/plumbing.rs

+42-57
Original file line numberDiff line numberDiff line change
@@ -248,13 +248,8 @@ where
248248
return TryGetJob::Cycle(value);
249249
}
250250

251-
let cached = try_get_cached(
252-
tcx,
253-
state,
254-
(*key).clone(),
255-
|value, index| (value.clone(), index),
256-
|_, _| panic!("value must be in cache after waiting"),
257-
);
251+
let cached = try_get_cached(tcx, state, key, |value, index| (value.clone(), index))
252+
.unwrap_or_else(|_| panic!("value must be in cache after waiting"));
258253

259254
if let Some(prof_timer) = _query_blocked_prof_timer.take() {
260255
prof_timer.finish_with_query_invocation_id(cached.1.into());
@@ -356,35 +351,28 @@ where
356351
/// It returns the shard index and a lock guard to the shard,
357352
/// which will be used if the query is not in the cache and we need
358353
/// to compute it.
359-
fn try_get_cached<CTX, C, R, OnHit, OnMiss>(
354+
fn try_get_cached<'a, CTX, C, R, OnHit>(
360355
tcx: CTX,
361-
state: &QueryState<CTX::DepKind, CTX::Query, C>,
362-
key: C::Key,
356+
state: &'a QueryState<CTX::DepKind, CTX::Query, C>,
357+
key: &C::Key,
363358
// `on_hit` can be called while holding a lock to the query cache
364359
on_hit: OnHit,
365-
on_miss: OnMiss,
366-
) -> R
360+
) -> Result<R, QueryLookup<'a, CTX::DepKind, CTX::Query, C::Key, C::Sharded>>
367361
where
368362
C: QueryCache,
369363
CTX: QueryContext,
370364
OnHit: FnOnce(&C::Stored, DepNodeIndex) -> R,
371-
OnMiss: FnOnce(C::Key, QueryLookup<'_, CTX::DepKind, CTX::Query, C::Key, C::Sharded>) -> R,
372365
{
373-
state.cache.lookup(
374-
state,
375-
key,
376-
|value, index| {
377-
if unlikely!(tcx.profiler().enabled()) {
378-
tcx.profiler().query_cache_hit(index.into());
379-
}
380-
#[cfg(debug_assertions)]
381-
{
382-
state.cache_hits.fetch_add(1, Ordering::Relaxed);
383-
}
384-
on_hit(value, index)
385-
},
386-
on_miss,
387-
)
366+
state.cache.lookup(state, &key, |value, index| {
367+
if unlikely!(tcx.profiler().enabled()) {
368+
tcx.profiler().query_cache_hit(index.into());
369+
}
370+
#[cfg(debug_assertions)]
371+
{
372+
state.cache_hits.fetch_add(1, Ordering::Relaxed);
373+
}
374+
on_hit(value, index)
375+
})
388376
}
389377

390378
fn try_execute_query<CTX, C>(
@@ -626,16 +614,14 @@ where
626614
C: QueryCache,
627615
C::Key: crate::dep_graph::DepNodeParams<CTX>,
628616
{
629-
try_get_cached(
630-
tcx,
631-
state,
632-
key,
633-
|value, index| {
634-
tcx.dep_graph().read_index(index);
635-
value.clone()
636-
},
637-
|key, lookup| try_execute_query(tcx, state, span, key, lookup, query),
638-
)
617+
let cached = try_get_cached(tcx, state, &key, |value, index| {
618+
tcx.dep_graph().read_index(index);
619+
value.clone()
620+
});
621+
match cached {
622+
Ok(value) => value,
623+
Err(lookup) => try_execute_query(tcx, state, span, key, lookup, query),
624+
}
639625
}
640626

641627
/// Ensure that either this query has all green inputs or been executed.
@@ -694,25 +680,24 @@ fn force_query_impl<CTX, C>(
694680
// We may be concurrently trying both execute and force a query.
695681
// Ensure that only one of them runs the query.
696682

697-
try_get_cached(
698-
tcx,
699-
state,
700-
key,
701-
|_, _| {
702-
// Cache hit, do nothing
703-
},
704-
|key, lookup| {
705-
let job = match JobOwner::<'_, CTX::DepKind, CTX::Query, C>::try_start(
706-
tcx, state, span, &key, lookup, query,
707-
) {
708-
TryGetJob::NotYetStarted(job) => job,
709-
TryGetJob::Cycle(_) => return,
710-
#[cfg(parallel_compiler)]
711-
TryGetJob::JobCompleted(_) => return,
712-
};
713-
force_query_with_job(tcx, key, job, dep_node, query);
714-
},
715-
);
683+
let cached = try_get_cached(tcx, state, &key, |_, _| {
684+
// Cache hit, do nothing
685+
});
686+
687+
let lookup = match cached {
688+
Ok(()) => return,
689+
Err(lookup) => lookup,
690+
};
691+
692+
let job = match JobOwner::<'_, CTX::DepKind, CTX::Query, C>::try_start(
693+
tcx, state, span, &key, lookup, query,
694+
) {
695+
TryGetJob::NotYetStarted(job) => job,
696+
TryGetJob::Cycle(_) => return,
697+
#[cfg(parallel_compiler)]
698+
TryGetJob::JobCompleted(_) => return,
699+
};
700+
force_query_with_job(tcx, key, job, dep_node, query);
716701
}
717702

718703
pub enum QueryMode {

0 commit comments

Comments
 (0)