Skip to content

Commit f07a639

Browse files
authored
Unrolled build for #153112
Rollup merge of #153112 - nnethercote:query-key-stuff, r=nnethercote Query key cleanups The first three commits are simple. The last two are a bit more opinionated, see what you think. r? @Zalathar
2 parents 69b7853 + 9ba101f commit f07a639

File tree

10 files changed

+141
-138
lines changed

10 files changed

+141
-138
lines changed

compiler/rustc_middle/src/dep_graph/dep_node.rs

Lines changed: 2 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,14 @@ use std::fmt;
5151
use std::hash::Hash;
5252

5353
use rustc_data_structures::fingerprint::{Fingerprint, PackedFingerprint};
54-
use rustc_data_structures::stable_hasher::{HashStable, StableHasher, StableOrd, ToStableHashKey};
54+
use rustc_data_structures::stable_hasher::{StableHasher, StableOrd, ToStableHashKey};
5555
use rustc_hir::def_id::DefId;
5656
use rustc_hir::definitions::DefPathHash;
5757
use rustc_macros::{Decodable, Encodable, HashStable};
5858
use rustc_span::Symbol;
5959

6060
use super::{KeyFingerprintStyle, SerializedDepNodeIndex};
61-
use crate::ich::StableHashingContext;
61+
use crate::dep_graph::DepNodeKey;
6262
use crate::mir::mono::MonoItem;
6363
use crate::ty::{TyCtxt, tls};
6464

@@ -168,58 +168,6 @@ impl fmt::Debug for DepNode {
168168
}
169169
}
170170

171-
/// Trait for query keys as seen by dependency-node tracking.
172-
pub trait DepNodeKey<'tcx>: fmt::Debug + Sized {
173-
fn key_fingerprint_style() -> KeyFingerprintStyle;
174-
175-
/// This method turns a query key into an opaque `Fingerprint` to be used
176-
/// in `DepNode`.
177-
fn to_fingerprint(&self, tcx: TyCtxt<'tcx>) -> Fingerprint;
178-
179-
fn to_debug_str(&self, tcx: TyCtxt<'tcx>) -> String;
180-
181-
/// This method tries to recover the query key from the given `DepNode`,
182-
/// something which is needed when forcing `DepNode`s during red-green
183-
/// evaluation. The query system will only call this method if
184-
/// `fingerprint_style()` is not `FingerprintStyle::Opaque`.
185-
/// It is always valid to return `None` here, in which case incremental
186-
/// compilation will treat the query as having changed instead of forcing it.
187-
fn try_recover_key(tcx: TyCtxt<'tcx>, dep_node: &DepNode) -> Option<Self>;
188-
}
189-
190-
// Blanket impl of `DepNodeKey`, which is specialized by other impls elsewhere.
191-
impl<'tcx, T> DepNodeKey<'tcx> for T
192-
where
193-
T: for<'a> HashStable<StableHashingContext<'a>> + fmt::Debug,
194-
{
195-
#[inline(always)]
196-
default fn key_fingerprint_style() -> KeyFingerprintStyle {
197-
KeyFingerprintStyle::Opaque
198-
}
199-
200-
#[inline(always)]
201-
default fn to_fingerprint(&self, tcx: TyCtxt<'tcx>) -> Fingerprint {
202-
tcx.with_stable_hashing_context(|mut hcx| {
203-
let mut hasher = StableHasher::new();
204-
self.hash_stable(&mut hcx, &mut hasher);
205-
hasher.finish()
206-
})
207-
}
208-
209-
#[inline(always)]
210-
default fn to_debug_str(&self, tcx: TyCtxt<'tcx>) -> String {
211-
// Make sure to print dep node params with reduced queries since printing
212-
// may themselves call queries, which may lead to (possibly untracked!)
213-
// query cycles.
214-
tcx.with_reduced_queries(|| format!("{self:?}"))
215-
}
216-
217-
#[inline(always)]
218-
default fn try_recover_key(_: TyCtxt<'tcx>, _: &DepNode) -> Option<Self> {
219-
None
220-
}
221-
}
222-
223171
/// This struct stores function pointers and other metadata for a particular DepKind.
224172
///
225173
/// Information is retrieved by indexing the `DEP_KINDS` array using the integer value

compiler/rustc_middle/src/dep_graph/dep_node_key.rs

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,67 @@
1+
use std::fmt::Debug;
2+
13
use rustc_data_structures::fingerprint::Fingerprint;
4+
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
25
use rustc_hir::def_id::{CrateNum, DefId, LOCAL_CRATE, LocalDefId, LocalModDefId, ModDefId};
36
use rustc_hir::definitions::DefPathHash;
47
use rustc_hir::{HirId, ItemLocalId, OwnerId};
58

6-
use crate::dep_graph::{DepNode, DepNodeKey, KeyFingerprintStyle};
9+
use crate::dep_graph::{DepNode, KeyFingerprintStyle};
10+
use crate::ich::StableHashingContext;
711
use crate::ty::TyCtxt;
812

13+
/// Trait for query keys as seen by dependency-node tracking.
14+
pub trait DepNodeKey<'tcx>: Debug + Sized {
15+
fn key_fingerprint_style() -> KeyFingerprintStyle;
16+
17+
/// This method turns a query key into an opaque `Fingerprint` to be used
18+
/// in `DepNode`.
19+
fn to_fingerprint(&self, tcx: TyCtxt<'tcx>) -> Fingerprint;
20+
21+
fn to_debug_str(&self, tcx: TyCtxt<'tcx>) -> String;
22+
23+
/// This method tries to recover the query key from the given `DepNode`,
24+
/// something which is needed when forcing `DepNode`s during red-green
25+
/// evaluation. The query system will only call this method if
26+
/// `fingerprint_style()` is not `FingerprintStyle::Opaque`.
27+
/// It is always valid to return `None` here, in which case incremental
28+
/// compilation will treat the query as having changed instead of forcing it.
29+
fn try_recover_key(tcx: TyCtxt<'tcx>, dep_node: &DepNode) -> Option<Self>;
30+
}
31+
32+
// Blanket impl of `DepNodeKey`, which is specialized by other impls elsewhere.
33+
impl<'tcx, T> DepNodeKey<'tcx> for T
34+
where
35+
T: for<'a> HashStable<StableHashingContext<'a>> + Debug,
36+
{
37+
#[inline(always)]
38+
default fn key_fingerprint_style() -> KeyFingerprintStyle {
39+
KeyFingerprintStyle::Opaque
40+
}
41+
42+
#[inline(always)]
43+
default fn to_fingerprint(&self, tcx: TyCtxt<'tcx>) -> Fingerprint {
44+
tcx.with_stable_hashing_context(|mut hcx| {
45+
let mut hasher = StableHasher::new();
46+
self.hash_stable(&mut hcx, &mut hasher);
47+
hasher.finish()
48+
})
49+
}
50+
51+
#[inline(always)]
52+
default fn to_debug_str(&self, tcx: TyCtxt<'tcx>) -> String {
53+
// Make sure to print dep node params with reduced queries since printing
54+
// may themselves call queries, which may lead to (possibly untracked!)
55+
// query cycles.
56+
tcx.with_reduced_queries(|| format!("{self:?}"))
57+
}
58+
59+
#[inline(always)]
60+
default fn try_recover_key(_: TyCtxt<'tcx>, _: &DepNode) -> Option<Self> {
61+
None
62+
}
63+
}
64+
965
impl<'tcx> DepNodeKey<'tcx> for () {
1066
#[inline(always)]
1167
fn key_fingerprint_style() -> KeyFingerprintStyle {

compiler/rustc_middle/src/dep_graph/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ use std::panic;
33
use tracing::instrument;
44

55
pub use self::dep_node::{
6-
DepKind, DepKindVTable, DepNode, DepNodeKey, WorkProductId, dep_kind_from_label, label_strs,
6+
DepKind, DepKindVTable, DepNode, WorkProductId, dep_kind_from_label, label_strs,
77
};
8+
pub use self::dep_node_key::DepNodeKey;
89
pub use self::graph::{
910
DepGraph, DepGraphData, DepNodeIndex, QuerySideEffect, TaskDepsRef, WorkProduct,
1011
WorkProductMap, hash_result,

compiler/rustc_middle/src/query/caches.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,21 @@
1-
use std::fmt::Debug;
2-
use std::hash::Hash;
31
use std::sync::OnceLock;
42

53
use rustc_data_structures::sharded::ShardedHashMap;
6-
use rustc_data_structures::stable_hasher::HashStable;
74
pub use rustc_data_structures::vec_cache::VecCache;
85
use rustc_hir::def_id::LOCAL_CRATE;
96
use rustc_index::Idx;
107
use rustc_span::def_id::{DefId, DefIndex};
118

129
use crate::dep_graph::DepNodeIndex;
13-
use crate::ich::StableHashingContext;
14-
15-
/// Traits that all query keys must satisfy.
16-
pub trait QueryCacheKey = Hash + Eq + Copy + Debug + for<'a> HashStable<StableHashingContext<'a>>;
10+
use crate::query::keys::QueryKey;
1711

1812
/// Trait for types that serve as an in-memory cache for query results,
1913
/// for a given key (argument) type and value (return) type.
2014
///
2115
/// Types implementing this trait are associated with actual key/value types
2216
/// by the `Cache` associated type of the `rustc_middle::query::Key` trait.
2317
pub trait QueryCache: Sized {
24-
type Key: QueryCacheKey;
18+
type Key: QueryKey;
2519
type Value: Copy;
2620

2721
/// Returns the cached value (and other information) associated with the
@@ -53,7 +47,7 @@ impl<K, V> Default for DefaultCache<K, V> {
5347

5448
impl<K, V> QueryCache for DefaultCache<K, V>
5549
where
56-
K: QueryCacheKey,
50+
K: QueryKey,
5751
V: Copy,
5852
{
5953
type Key = K;
@@ -180,7 +174,7 @@ where
180174

181175
impl<K, V> QueryCache for VecCache<K, V, DepNodeIndex>
182176
where
183-
K: Idx + QueryCacheKey,
177+
K: Idx + QueryKey,
184178
V: Copy,
185179
{
186180
type Key = K;

0 commit comments

Comments
 (0)