-
-
Notifications
You must be signed in to change notification settings - Fork 14.9k
Expand file tree
/
Copy pathplumbing.rs
More file actions
197 lines (173 loc) · 6.4 KB
/
plumbing.rs
File metadata and controls
197 lines (173 loc) · 6.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
use std::num::NonZero;
use rustc_data_structures::unord::UnordMap;
use rustc_hir::limit::Limit;
use rustc_middle::bug;
#[expect(unused_imports, reason = "used by doc comments")]
use rustc_middle::dep_graph::DepKindVTable;
use rustc_middle::dep_graph::{DepNode, DepNodeKey, SerializedDepNodeIndex};
use rustc_middle::query::erase::{Erasable, Erased};
use rustc_middle::query::on_disk_cache::{CacheDecoder, CacheEncoder};
use rustc_middle::query::{QueryCache, QueryJobId, QueryMode, QueryVTable, erase};
use rustc_middle::ty::TyCtxt;
use rustc_middle::ty::tls::{self, ImplicitCtxt};
use rustc_serialize::{Decodable, Encodable};
use rustc_span::DUMMY_SP;
use rustc_span::def_id::LOCAL_CRATE;
use crate::error::{QueryOverflow, QueryOverflowNote};
use crate::execution::all_inactive;
use crate::job::find_dep_kind_root;
use crate::query_impl::for_each_query_vtable;
use crate::{CollectActiveJobsKind, collect_active_query_jobs};
fn depth_limit_error<'tcx>(tcx: TyCtxt<'tcx>, job: QueryJobId) {
let job_map = collect_active_query_jobs(tcx, CollectActiveJobsKind::Full);
let (span, desc, depth) = find_dep_kind_root(tcx, job, job_map);
let suggested_limit = match tcx.recursion_limit() {
Limit(0) => Limit(2),
limit => limit * 2,
};
tcx.dcx().emit_fatal(QueryOverflow {
span,
note: QueryOverflowNote { desc, depth },
suggested_limit,
crate_name: tcx.crate_name(LOCAL_CRATE),
});
}
#[inline]
pub(crate) fn next_job_id<'tcx>(tcx: TyCtxt<'tcx>) -> QueryJobId {
QueryJobId(
NonZero::new(tcx.query_system.jobs.fetch_add(1, std::sync::atomic::Ordering::Relaxed))
.unwrap(),
)
}
#[inline]
pub(crate) fn current_query_job() -> Option<QueryJobId> {
tls::with_context(|icx| icx.query)
}
/// Executes a job by changing the `ImplicitCtxt` to point to the new query job while it executes.
#[inline(always)]
pub(crate) fn start_query<R>(
job_id: QueryJobId,
depth_limit: bool,
compute: impl FnOnce() -> R,
) -> R {
tls::with_context(move |icx| {
if depth_limit && !icx.tcx.recursion_limit().value_within_limit(icx.query_depth) {
depth_limit_error(icx.tcx, job_id);
}
// Update the `ImplicitCtxt` to point to our new query job.
let icx = ImplicitCtxt {
query: Some(job_id),
query_depth: icx.query_depth + if depth_limit { 1 } else { 0 },
..*icx
};
// Use the `ImplicitCtxt` while we execute the query.
tls::enter_context(&icx, compute)
})
}
pub(crate) fn encode_query_values<'tcx>(tcx: TyCtxt<'tcx>, encoder: &mut CacheEncoder<'_, 'tcx>) {
for_each_query_vtable!(CACHE_ON_DISK, tcx, |query| {
encode_query_values_inner(tcx, query, encoder)
});
}
fn encode_query_values_inner<'a, 'tcx, C, V>(
tcx: TyCtxt<'tcx>,
query: &'tcx QueryVTable<'tcx, C>,
encoder: &mut CacheEncoder<'a, 'tcx>,
) where
C: QueryCache<Value = Erased<V>>,
V: Erasable + Encodable<CacheEncoder<'a, 'tcx>>,
{
let _timer = tcx.prof.generic_activity_with_arg("encode_query_results_for", query.name);
assert!(all_inactive(&query.state));
query.cache.for_each(&mut |key, value, dep_node| {
if (query.will_cache_on_disk_for_key_fn)(*key) {
encoder.encode_query_value::<V>(dep_node, &erase::restore_val::<V>(*value));
}
});
}
pub(crate) fn verify_query_key_hashes<'tcx>(tcx: TyCtxt<'tcx>) {
if tcx.sess.opts.unstable_opts.incremental_verify_ich || cfg!(debug_assertions) {
tcx.sess.time("verify_query_key_hashes", || {
for_each_query_vtable!(ALL, tcx, |query| {
verify_query_key_hashes_inner(query, tcx);
});
});
}
}
fn verify_query_key_hashes_inner<'tcx, C: QueryCache>(
query: &'tcx QueryVTable<'tcx, C>,
tcx: TyCtxt<'tcx>,
) {
let _timer = tcx.prof.generic_activity_with_arg("query_key_hash_verify_for", query.name);
let cache = &query.cache;
let mut map = UnordMap::with_capacity(cache.len());
cache.for_each(&mut |key, _, _| {
let node = DepNode::construct(tcx, query.dep_kind, key);
if let Some(other_key) = map.insert(node, *key) {
bug!(
"query key:\n\
`{:?}`\n\
and key:\n\
`{:?}`\n\
mapped to the same dep node:\n\
{:?}",
key,
other_key,
node
);
}
});
}
/// Inner implementation of [`DepKindVTable::promote_from_disk_fn`] for queries.
pub(crate) fn promote_from_disk_inner<'tcx, C: QueryCache>(
tcx: TyCtxt<'tcx>,
query: &'tcx QueryVTable<'tcx, C>,
dep_node: DepNode,
) {
debug_assert!(tcx.dep_graph.is_green(&dep_node));
let key = C::Key::try_recover_key(tcx, &dep_node).unwrap_or_else(|| {
panic!(
"Failed to recover key for {dep_node:?} with key fingerprint {}",
dep_node.key_fingerprint
)
});
// If the recovered key isn't eligible for cache-on-disk, then there's no
// value on disk to promote.
if !(query.will_cache_on_disk_for_key_fn)(key) {
return;
}
match query.cache.lookup(&key) {
// If the value is already in memory, then promotion isn't needed.
Some(_) => {}
// "Execute" the query to load its disk-cached value into memory.
//
// We know that the key is cache-on-disk and its node is green,
// so there _must_ be a value on disk to load.
//
// FIXME(Zalathar): Is there a reasonable way to skip more of the
// query bookkeeping when doing this?
None => {
(query.execute_query_fn)(tcx, DUMMY_SP, key, QueryMode::Get);
}
}
}
pub(crate) fn loadable_from_disk<'tcx>(tcx: TyCtxt<'tcx>, id: SerializedDepNodeIndex) -> bool {
if let Some(cache) = tcx.query_system.on_disk_cache.as_ref() {
cache.loadable_from_disk(id)
} else {
false
}
}
pub(crate) fn try_load_from_disk<'tcx, V>(
tcx: TyCtxt<'tcx>,
prev_index: SerializedDepNodeIndex,
) -> Option<V>
where
V: for<'a> Decodable<CacheDecoder<'a, 'tcx>>,
{
let on_disk_cache = tcx.query_system.on_disk_cache.as_ref()?;
// The call to `with_query_deserialization` enforces that no new `DepNodes`
// are created during deserialization. See the docs of that method for more
// details.
tcx.dep_graph.with_query_deserialization(|| on_disk_cache.try_load_query_value(tcx, prev_index))
}