Skip to content

Commit e7813fe

Browse files
committed
Auto merge of rust-lang#107667 - cjgillot:no-on-hit, r=lcnr,Zoxc
Remove `OnHit` callback from query caches. This is not useful now that query results are `Copy`.
2 parents 0c13c17 + 635ff8e commit e7813fe

File tree

4 files changed

+68
-127
lines changed

4 files changed

+68
-127
lines changed

compiler/rustc_middle/src/ty/query.rs

+20-40
Original file line numberDiff line numberDiff line change
@@ -106,16 +106,6 @@ impl<'tcx> TyCtxt<'tcx> {
106106
}
107107
}
108108

109-
/// Helper for `TyCtxtEnsure` to avoid a closure.
110-
#[inline(always)]
111-
fn noop<T>(_: &T) {}
112-
113-
/// Helper to ensure that queries only return `Copy` types.
114-
#[inline(always)]
115-
fn copy<T: Copy>(x: &T) -> T {
116-
*x
117-
}
118-
119109
macro_rules! query_helper_param_ty {
120110
(DefId) => { impl IntoQueryParam<DefId> };
121111
(LocalDefId) => { impl IntoQueryParam<LocalDefId> };
@@ -225,14 +215,10 @@ macro_rules! define_callbacks {
225215
let key = key.into_query_param();
226216
opt_remap_env_constness!([$($modifiers)*][key]);
227217

228-
let cached = try_get_cached(self.tcx, &self.tcx.query_caches.$name, &key, noop);
229-
230-
match cached {
231-
Ok(()) => return,
232-
Err(()) => (),
233-
}
234-
235-
self.tcx.queries.$name(self.tcx, DUMMY_SP, key, QueryMode::Ensure);
218+
match try_get_cached(self.tcx, &self.tcx.query_caches.$name, &key) {
219+
Some(_) => return,
220+
None => self.tcx.queries.$name(self.tcx, DUMMY_SP, key, QueryMode::Ensure),
221+
};
236222
})*
237223
}
238224

@@ -254,14 +240,10 @@ macro_rules! define_callbacks {
254240
let key = key.into_query_param();
255241
opt_remap_env_constness!([$($modifiers)*][key]);
256242

257-
let cached = try_get_cached(self.tcx, &self.tcx.query_caches.$name, &key, copy);
258-
259-
match cached {
260-
Ok(value) => return value,
261-
Err(()) => (),
243+
match try_get_cached(self.tcx, &self.tcx.query_caches.$name, &key) {
244+
Some(value) => value,
245+
None => self.tcx.queries.$name(self.tcx, self.span, key, QueryMode::Get).unwrap(),
262246
}
263-
264-
self.tcx.queries.$name(self.tcx, self.span, key, QueryMode::Get).unwrap()
265247
})*
266248
}
267249

@@ -353,27 +335,25 @@ macro_rules! define_feedable {
353335
let tcx = self.tcx;
354336
let cache = &tcx.query_caches.$name;
355337

356-
let cached = try_get_cached(tcx, cache, &key, copy);
357-
358-
match cached {
359-
Ok(old) => {
338+
match try_get_cached(tcx, cache, &key) {
339+
Some(old) => {
360340
bug!(
361341
"Trying to feed an already recorded value for query {} key={key:?}:\nold value: {old:?}\nnew value: {value:?}",
362342
stringify!($name),
343+
)
344+
}
345+
None => {
346+
let dep_node = dep_graph::DepNode::construct(tcx, dep_graph::DepKind::$name, &key);
347+
let dep_node_index = tcx.dep_graph.with_feed_task(
348+
dep_node,
349+
tcx,
350+
key,
351+
&value,
352+
hash_result!([$($modifiers)*]),
363353
);
354+
cache.complete(key, value, dep_node_index)
364355
}
365-
Err(()) => (),
366356
}
367-
368-
let dep_node = dep_graph::DepNode::construct(tcx, dep_graph::DepKind::$name, &key);
369-
let dep_node_index = tcx.dep_graph.with_feed_task(
370-
dep_node,
371-
tcx,
372-
key,
373-
&value,
374-
hash_result!([$($modifiers)*]),
375-
);
376-
cache.complete(key, value, dep_node_index)
377357
}
378358
})*
379359
}

compiler/rustc_query_system/src/query/caches.rs

+17-56
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ use std::marker::PhantomData;
1616
pub trait CacheSelector<'tcx, V> {
1717
type Cache
1818
where
19-
V: Clone;
19+
V: Copy;
2020
type ArenaCache;
2121
}
2222

2323
pub trait QueryStorage {
2424
type Value: Debug;
25-
type Stored: Clone;
25+
type Stored: Copy;
2626

2727
/// Store a value without putting it in the cache.
2828
/// This is meant to be used with cycle errors.
@@ -36,14 +36,7 @@ pub trait QueryCache: QueryStorage + Sized {
3636
/// It returns the shard index and a lock guard to the shard,
3737
/// which will be used if the query is not in the cache and we need
3838
/// to compute it.
39-
fn lookup<R, OnHit>(
40-
&self,
41-
key: &Self::Key,
42-
// `on_hit` can be called while holding a lock to the query state shard.
43-
on_hit: OnHit,
44-
) -> Result<R, ()>
45-
where
46-
OnHit: FnOnce(&Self::Stored, DepNodeIndex) -> R;
39+
fn lookup(&self, key: &Self::Key) -> Option<(Self::Stored, DepNodeIndex)>;
4740

4841
fn complete(&self, key: Self::Key, value: Self::Value, index: DepNodeIndex) -> Self::Stored;
4942

@@ -55,7 +48,7 @@ pub struct DefaultCacheSelector<K>(PhantomData<K>);
5548
impl<'tcx, K: Eq + Hash, V: 'tcx> CacheSelector<'tcx, V> for DefaultCacheSelector<K> {
5649
type Cache = DefaultCache<K, V>
5750
where
58-
V: Clone;
51+
V: Copy;
5952
type ArenaCache = ArenaCache<'tcx, K, V>;
6053
}
6154

@@ -72,7 +65,7 @@ impl<K, V> Default for DefaultCache<K, V> {
7265
}
7366
}
7467

75-
impl<K: Eq + Hash, V: Clone + Debug> QueryStorage for DefaultCache<K, V> {
68+
impl<K: Eq + Hash, V: Copy + Debug> QueryStorage for DefaultCache<K, V> {
7669
type Value = V;
7770
type Stored = V;
7871

@@ -86,28 +79,20 @@ impl<K: Eq + Hash, V: Clone + Debug> QueryStorage for DefaultCache<K, V> {
8679
impl<K, V> QueryCache for DefaultCache<K, V>
8780
where
8881
K: Eq + Hash + Clone + Debug,
89-
V: Clone + Debug,
82+
V: Copy + Debug,
9083
{
9184
type Key = K;
9285

9386
#[inline(always)]
94-
fn lookup<R, OnHit>(&self, key: &K, on_hit: OnHit) -> Result<R, ()>
95-
where
96-
OnHit: FnOnce(&V, DepNodeIndex) -> R,
97-
{
87+
fn lookup(&self, key: &K) -> Option<(V, DepNodeIndex)> {
9888
let key_hash = sharded::make_hash(key);
9989
#[cfg(parallel_compiler)]
10090
let lock = self.cache.get_shard_by_hash(key_hash).lock();
10191
#[cfg(not(parallel_compiler))]
10292
let lock = self.cache.lock();
10393
let result = lock.raw_entry().from_key_hashed_nocheck(key_hash, key);
10494

105-
if let Some((_, value)) = result {
106-
let hit_result = on_hit(&value.0, value.1);
107-
Ok(hit_result)
108-
} else {
109-
Err(())
110-
}
95+
if let Some((_, value)) = result { Some(*value) } else { None }
11196
}
11297

11398
#[inline]
@@ -176,23 +161,15 @@ where
176161
type Key = K;
177162

178163
#[inline(always)]
179-
fn lookup<R, OnHit>(&self, key: &K, on_hit: OnHit) -> Result<R, ()>
180-
where
181-
OnHit: FnOnce(&&'tcx V, DepNodeIndex) -> R,
182-
{
164+
fn lookup(&self, key: &K) -> Option<(&'tcx V, DepNodeIndex)> {
183165
let key_hash = sharded::make_hash(key);
184166
#[cfg(parallel_compiler)]
185167
let lock = self.cache.get_shard_by_hash(key_hash).lock();
186168
#[cfg(not(parallel_compiler))]
187169
let lock = self.cache.lock();
188170
let result = lock.raw_entry().from_key_hashed_nocheck(key_hash, key);
189171

190-
if let Some((_, value)) = result {
191-
let hit_result = on_hit(&&value.0, value.1);
192-
Ok(hit_result)
193-
} else {
194-
Err(())
195-
}
172+
if let Some((_, value)) = result { Some((&value.0, value.1)) } else { None }
196173
}
197174

198175
#[inline]
@@ -234,7 +211,7 @@ pub struct VecCacheSelector<K>(PhantomData<K>);
234211
impl<'tcx, K: Idx, V: 'tcx> CacheSelector<'tcx, V> for VecCacheSelector<K> {
235212
type Cache = VecCache<K, V>
236213
where
237-
V: Clone;
214+
V: Copy;
238215
type ArenaCache = VecArenaCache<'tcx, K, V>;
239216
}
240217

@@ -251,7 +228,7 @@ impl<K: Idx, V> Default for VecCache<K, V> {
251228
}
252229
}
253230

254-
impl<K: Eq + Idx, V: Clone + Debug> QueryStorage for VecCache<K, V> {
231+
impl<K: Eq + Idx, V: Copy + Debug> QueryStorage for VecCache<K, V> {
255232
type Value = V;
256233
type Stored = V;
257234

@@ -265,25 +242,17 @@ impl<K: Eq + Idx, V: Clone + Debug> QueryStorage for VecCache<K, V> {
265242
impl<K, V> QueryCache for VecCache<K, V>
266243
where
267244
K: Eq + Idx + Clone + Debug,
268-
V: Clone + Debug,
245+
V: Copy + Debug,
269246
{
270247
type Key = K;
271248

272249
#[inline(always)]
273-
fn lookup<R, OnHit>(&self, key: &K, on_hit: OnHit) -> Result<R, ()>
274-
where
275-
OnHit: FnOnce(&V, DepNodeIndex) -> R,
276-
{
250+
fn lookup(&self, key: &K) -> Option<(V, DepNodeIndex)> {
277251
#[cfg(parallel_compiler)]
278252
let lock = self.cache.get_shard_by_hash(key.index() as u64).lock();
279253
#[cfg(not(parallel_compiler))]
280254
let lock = self.cache.lock();
281-
if let Some(Some(value)) = lock.get(*key) {
282-
let hit_result = on_hit(&value.0, value.1);
283-
Ok(hit_result)
284-
} else {
285-
Err(())
286-
}
255+
if let Some(Some(value)) = lock.get(*key) { Some(*value) } else { None }
287256
}
288257

289258
#[inline]
@@ -357,20 +326,12 @@ where
357326
type Key = K;
358327

359328
#[inline(always)]
360-
fn lookup<R, OnHit>(&self, key: &K, on_hit: OnHit) -> Result<R, ()>
361-
where
362-
OnHit: FnOnce(&&'tcx V, DepNodeIndex) -> R,
363-
{
329+
fn lookup(&self, key: &K) -> Option<(&'tcx V, DepNodeIndex)> {
364330
#[cfg(parallel_compiler)]
365331
let lock = self.cache.get_shard_by_hash(key.index() as u64).lock();
366332
#[cfg(not(parallel_compiler))]
367333
let lock = self.cache.lock();
368-
if let Some(Some(value)) = lock.get(*key) {
369-
let hit_result = on_hit(&&value.0, value.1);
370-
Ok(hit_result)
371-
} else {
372-
Err(())
373-
}
334+
if let Some(Some(value)) = lock.get(*key) { Some((&value.0, value.1)) } else { None }
374335
}
375336

376337
#[inline]

compiler/rustc_query_system/src/query/config.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pub trait QueryConfig<Qcx: QueryContext> {
2121

2222
type Key: DepNodeParams<Qcx::DepContext> + Eq + Hash + Clone + Debug;
2323
type Value: Debug;
24-
type Stored: Debug + Clone + std::borrow::Borrow<Self::Value>;
24+
type Stored: Debug + Copy + std::borrow::Borrow<Self::Value>;
2525

2626
type Cache: QueryCache<Key = Self::Key, Stored = Self::Stored, Value = Self::Value>;
2727

compiler/rustc_query_system/src/query/plumbing.rs

+30-30
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ fn mk_cycle<Qcx, V, R, D: DepKind>(
130130
where
131131
Qcx: QueryContext + crate::query::HasDepContext<DepKind = D>,
132132
V: std::fmt::Debug + Value<Qcx::DepContext, Qcx::DepKind>,
133-
R: Clone,
133+
R: Copy,
134134
{
135135
let error = report_cycle(qcx.dep_context().sess(), &cycle_error);
136136
let value = handle_cycle_error(*qcx.dep_context(), &cycle_error, error, handler);
@@ -339,25 +339,21 @@ where
339339
/// which will be used if the query is not in the cache and we need
340340
/// to compute it.
341341
#[inline]
342-
pub fn try_get_cached<Tcx, C, R, OnHit>(
343-
tcx: Tcx,
344-
cache: &C,
345-
key: &C::Key,
346-
// `on_hit` can be called while holding a lock to the query cache
347-
on_hit: OnHit,
348-
) -> Result<R, ()>
342+
pub fn try_get_cached<Tcx, C>(tcx: Tcx, cache: &C, key: &C::Key) -> Option<C::Stored>
349343
where
350344
C: QueryCache,
351345
Tcx: DepContext,
352-
OnHit: FnOnce(&C::Stored) -> R,
353346
{
354-
cache.lookup(&key, |value, index| {
355-
if std::intrinsics::unlikely(tcx.profiler().enabled()) {
356-
tcx.profiler().query_cache_hit(index.into());
347+
match cache.lookup(&key) {
348+
Some((value, index)) => {
349+
if std::intrinsics::unlikely(tcx.profiler().enabled()) {
350+
tcx.profiler().query_cache_hit(index.into());
351+
}
352+
tcx.dep_graph().read_index(index);
353+
Some(value)
357354
}
358-
tcx.dep_graph().read_index(index);
359-
on_hit(value)
360-
})
355+
None => None,
356+
}
361357
}
362358

363359
fn try_execute_query<Q, Qcx>(
@@ -379,17 +375,25 @@ where
379375
if Q::FEEDABLE {
380376
// We may have put a value inside the cache from inside the execution.
381377
// Verify that it has the same hash as what we have now, to ensure consistency.
382-
let _ = cache.lookup(&key, |cached_result, _| {
378+
if let Some((cached_result, _)) = cache.lookup(&key) {
383379
let hasher = Q::HASH_RESULT.expect("feedable forbids no_hash");
384380

385-
let old_hash = qcx.dep_context().with_stable_hashing_context(|mut hcx| hasher(&mut hcx, cached_result.borrow()));
386-
let new_hash = qcx.dep_context().with_stable_hashing_context(|mut hcx| hasher(&mut hcx, &result));
381+
let old_hash = qcx.dep_context().with_stable_hashing_context(|mut hcx| {
382+
hasher(&mut hcx, cached_result.borrow())
383+
});
384+
let new_hash = qcx
385+
.dep_context()
386+
.with_stable_hashing_context(|mut hcx| hasher(&mut hcx, &result));
387387
debug_assert_eq!(
388-
old_hash, new_hash,
388+
old_hash,
389+
new_hash,
389390
"Computed query value for {:?}({:?}) is inconsistent with fed value,\ncomputed={:#?}\nfed={:#?}",
390-
Q::DEP_KIND, key, result, cached_result,
391+
Q::DEP_KIND,
392+
key,
393+
result,
394+
cached_result,
391395
);
392-
});
396+
}
393397
}
394398
let result = job.complete(cache, result, dep_node_index);
395399
(result, Some(dep_node_index))
@@ -400,9 +404,9 @@ where
400404
}
401405
#[cfg(parallel_compiler)]
402406
TryGetJob::JobCompleted(query_blocked_prof_timer) => {
403-
let (v, index) = cache
404-
.lookup(&key, |value, index| (value.clone(), index))
405-
.unwrap_or_else(|_| panic!("value must be in cache after waiting"));
407+
let Some((v, index)) = cache.lookup(&key) else {
408+
panic!("value must be in cache after waiting")
409+
};
406410

407411
if std::intrinsics::unlikely(qcx.dep_context().profiler().enabled()) {
408412
qcx.dep_context().profiler().query_cache_hit(index.into());
@@ -771,15 +775,11 @@ where
771775
// We may be concurrently trying both execute and force a query.
772776
// Ensure that only one of them runs the query.
773777
let cache = Q::query_cache(qcx);
774-
let cached = cache.lookup(&key, |_, index| {
778+
if let Some((_, index)) = cache.lookup(&key) {
775779
if std::intrinsics::unlikely(qcx.dep_context().profiler().enabled()) {
776780
qcx.dep_context().profiler().query_cache_hit(index.into());
777781
}
778-
});
779-
780-
match cached {
781-
Ok(()) => return,
782-
Err(()) => {}
782+
return;
783783
}
784784

785785
let state = Q::query_state(qcx);

0 commit comments

Comments
 (0)