]> git.proxmox.com Git - rustc.git/blob - compiler/rustc_query_impl/src/plumbing.rs
New upstream version 1.55.0+dfsg1
[rustc.git] / compiler / rustc_query_impl / src / plumbing.rs
1 //! The implementation of the query system itself. This defines the macros that
2 //! generate the actual methods on tcx which find and execute the provider,
3 //! manage the caches, and so forth.
4
5 use crate::{on_disk_cache, queries, Queries};
6 use rustc_middle::dep_graph::{DepKind, DepNode, DepNodeIndex, SerializedDepNodeIndex};
7 use rustc_middle::ty::tls::{self, ImplicitCtxt};
8 use rustc_middle::ty::{self, TyCtxt};
9 use rustc_query_system::dep_graph::HasDepContext;
10 use rustc_query_system::query::{QueryContext, QueryDescription, QueryJobId, QueryMap};
11
12 use rustc_data_structures::sync::Lock;
13 use rustc_data_structures::thin_vec::ThinVec;
14 use rustc_errors::{Diagnostic, Handler};
15 use rustc_serialize::opaque;
16 use rustc_span::def_id::LocalDefId;
17
18 use std::any::Any;
19
20 #[derive(Copy, Clone)]
21 pub struct QueryCtxt<'tcx> {
22 pub tcx: TyCtxt<'tcx>,
23 pub queries: &'tcx Queries<'tcx>,
24 }
25
26 impl<'tcx> std::ops::Deref for QueryCtxt<'tcx> {
27 type Target = TyCtxt<'tcx>;
28
29 #[inline]
30 fn deref(&self) -> &Self::Target {
31 &self.tcx
32 }
33 }
34
35 impl HasDepContext for QueryCtxt<'tcx> {
36 type DepKind = rustc_middle::dep_graph::DepKind;
37 type StableHashingContext = rustc_middle::ich::StableHashingContext<'tcx>;
38 type DepContext = TyCtxt<'tcx>;
39
40 #[inline]
41 fn dep_context(&self) -> &Self::DepContext {
42 &self.tcx
43 }
44 }
45
46 impl QueryContext for QueryCtxt<'tcx> {
47 fn current_query_job(&self) -> Option<QueryJobId<Self::DepKind>> {
48 tls::with_related_context(**self, |icx| icx.query)
49 }
50
51 fn try_collect_active_jobs(&self) -> Option<QueryMap<Self::DepKind>> {
52 self.queries.try_collect_active_jobs(**self)
53 }
54
55 fn try_load_from_on_disk_cache(&self, dep_node: &DepNode) {
56 let cb = &super::QUERY_CALLBACKS[dep_node.kind as usize];
57 (cb.try_load_from_on_disk_cache)(*self, dep_node)
58 }
59
60 fn try_force_from_dep_node(&self, dep_node: &DepNode) -> bool {
61 debug!("try_force_from_dep_node({:?}) --- trying to force", dep_node);
62
63 // We must avoid ever having to call `force_from_dep_node()` for a
64 // `DepNode::codegen_unit`:
65 // Since we cannot reconstruct the query key of a `DepNode::codegen_unit`, we
66 // would always end up having to evaluate the first caller of the
67 // `codegen_unit` query that *is* reconstructible. This might very well be
68 // the `compile_codegen_unit` query, thus re-codegenning the whole CGU just
69 // to re-trigger calling the `codegen_unit` query with the right key. At
70 // that point we would already have re-done all the work we are trying to
71 // avoid doing in the first place.
72 // The solution is simple: Just explicitly call the `codegen_unit` query for
73 // each CGU, right after partitioning. This way `try_mark_green` will always
74 // hit the cache instead of having to go through `force_from_dep_node`.
75 // This assertion makes sure, we actually keep applying the solution above.
76 debug_assert!(
77 dep_node.kind != DepKind::codegen_unit,
78 "calling force_from_dep_node() on DepKind::codegen_unit"
79 );
80
81 let cb = &super::QUERY_CALLBACKS[dep_node.kind as usize];
82 (cb.force_from_dep_node)(*self, dep_node)
83 }
84
85 // Interactions with on_disk_cache
86 fn load_diagnostics(&self, prev_dep_node_index: SerializedDepNodeIndex) -> Vec<Diagnostic> {
87 self.queries
88 .on_disk_cache
89 .as_ref()
90 .map(|c| c.load_diagnostics(**self, prev_dep_node_index))
91 .unwrap_or_default()
92 }
93
94 fn store_diagnostics(&self, dep_node_index: DepNodeIndex, diagnostics: ThinVec<Diagnostic>) {
95 if let Some(c) = self.queries.on_disk_cache.as_ref() {
96 c.store_diagnostics(dep_node_index, diagnostics)
97 }
98 }
99
100 fn store_diagnostics_for_anon_node(
101 &self,
102 dep_node_index: DepNodeIndex,
103 diagnostics: ThinVec<Diagnostic>,
104 ) {
105 if let Some(c) = self.queries.on_disk_cache.as_ref() {
106 c.store_diagnostics_for_anon_node(dep_node_index, diagnostics)
107 }
108 }
109
110 /// Executes a job by changing the `ImplicitCtxt` to point to the
111 /// new query job while it executes. It returns the diagnostics
112 /// captured during execution and the actual result.
113 #[inline(always)]
114 fn start_query<R>(
115 &self,
116 token: QueryJobId<Self::DepKind>,
117 diagnostics: Option<&Lock<ThinVec<Diagnostic>>>,
118 compute: impl FnOnce() -> R,
119 ) -> R {
120 // The `TyCtxt` stored in TLS has the same global interner lifetime
121 // as `self`, so we use `with_related_context` to relate the 'tcx lifetimes
122 // when accessing the `ImplicitCtxt`.
123 tls::with_related_context(**self, move |current_icx| {
124 // Update the `ImplicitCtxt` to point to our new query job.
125 let new_icx = ImplicitCtxt {
126 tcx: **self,
127 query: Some(token),
128 diagnostics,
129 layout_depth: current_icx.layout_depth,
130 task_deps: current_icx.task_deps,
131 };
132
133 // Use the `ImplicitCtxt` while we execute the query.
134 tls::enter_context(&new_icx, |_| {
135 rustc_data_structures::stack::ensure_sufficient_stack(compute)
136 })
137 })
138 }
139 }
140
141 impl<'tcx> QueryCtxt<'tcx> {
142 #[inline]
143 pub fn from_tcx(tcx: TyCtxt<'tcx>) -> Self {
144 let queries = tcx.queries.as_any();
145 let queries = unsafe {
146 let queries = std::mem::transmute::<&dyn Any, &dyn Any>(queries);
147 let queries = queries.downcast_ref().unwrap();
148 let queries = std::mem::transmute::<&Queries<'_>, &Queries<'_>>(queries);
149 queries
150 };
151 QueryCtxt { tcx, queries }
152 }
153
154 crate fn on_disk_cache(self) -> Option<&'tcx on_disk_cache::OnDiskCache<'tcx>> {
155 self.queries.on_disk_cache.as_ref()
156 }
157
158 #[cfg(parallel_compiler)]
159 pub unsafe fn deadlock(self, registry: &rustc_rayon_core::Registry) {
160 rustc_query_system::query::deadlock(self, registry)
161 }
162
163 pub(super) fn encode_query_results(
164 self,
165 encoder: &mut on_disk_cache::CacheEncoder<'a, 'tcx, opaque::FileEncoder>,
166 query_result_index: &mut on_disk_cache::EncodedQueryResultIndex,
167 ) -> opaque::FileEncodeResult {
168 macro_rules! encode_queries {
169 ($($query:ident,)*) => {
170 $(
171 on_disk_cache::encode_query_results::<_, super::queries::$query<'_>>(
172 self,
173 encoder,
174 query_result_index
175 )?;
176 )*
177 }
178 }
179
180 rustc_cached_queries!(encode_queries!);
181
182 Ok(())
183 }
184
185 pub fn try_print_query_stack(
186 self,
187 query: Option<QueryJobId<DepKind>>,
188 handler: &Handler,
189 num_frames: Option<usize>,
190 ) -> usize {
191 rustc_query_system::query::print_query_stack(self, query, handler, num_frames)
192 }
193 }
194
195 /// This struct stores metadata about each Query.
196 ///
197 /// Information is retrieved by indexing the `QUERIES` array using the integer value
198 /// of the `DepKind`. Overall, this allows to implement `QueryContext` using this manual
199 /// jump table instead of large matches.
200 pub struct QueryStruct {
201 /// The red/green evaluation system will try to mark a specific DepNode in the
202 /// dependency graph as green by recursively trying to mark the dependencies of
203 /// that `DepNode` as green. While doing so, it will sometimes encounter a `DepNode`
204 /// where we don't know if it is red or green and we therefore actually have
205 /// to recompute its value in order to find out. Since the only piece of
206 /// information that we have at that point is the `DepNode` we are trying to
207 /// re-evaluate, we need some way to re-run a query from just that. This is what
208 /// `force_from_dep_node()` implements.
209 ///
210 /// In the general case, a `DepNode` consists of a `DepKind` and an opaque
211 /// GUID/fingerprint that will uniquely identify the node. This GUID/fingerprint
212 /// is usually constructed by computing a stable hash of the query-key that the
213 /// `DepNode` corresponds to. Consequently, it is not in general possible to go
214 /// back from hash to query-key (since hash functions are not reversible). For
215 /// this reason `force_from_dep_node()` is expected to fail from time to time
216 /// because we just cannot find out, from the `DepNode` alone, what the
217 /// corresponding query-key is and therefore cannot re-run the query.
218 ///
219 /// The system deals with this case letting `try_mark_green` fail which forces
220 /// the root query to be re-evaluated.
221 ///
222 /// Now, if `force_from_dep_node()` would always fail, it would be pretty useless.
223 /// Fortunately, we can use some contextual information that will allow us to
224 /// reconstruct query-keys for certain kinds of `DepNode`s. In particular, we
225 /// enforce by construction that the GUID/fingerprint of certain `DepNode`s is a
226 /// valid `DefPathHash`. Since we also always build a huge table that maps every
227 /// `DefPathHash` in the current codebase to the corresponding `DefId`, we have
228 /// everything we need to re-run the query.
229 ///
230 /// Take the `mir_promoted` query as an example. Like many other queries, it
231 /// just has a single parameter: the `DefId` of the item it will compute the
232 /// validated MIR for. Now, when we call `force_from_dep_node()` on a `DepNode`
233 /// with kind `MirValidated`, we know that the GUID/fingerprint of the `DepNode`
234 /// is actually a `DefPathHash`, and can therefore just look up the corresponding
235 /// `DefId` in `tcx.def_path_hash_to_def_id`.
236 ///
237 /// When you implement a new query, it will likely have a corresponding new
238 /// `DepKind`, and you'll have to support it here in `force_from_dep_node()`. As
239 /// a rule of thumb, if your query takes a `DefId` or `LocalDefId` as sole parameter,
240 /// then `force_from_dep_node()` should not fail for it. Otherwise, you can just
241 /// add it to the "We don't have enough information to reconstruct..." group in
242 /// the match below.
243 pub(crate) force_from_dep_node: fn(tcx: QueryCtxt<'_>, dep_node: &DepNode) -> bool,
244
245 /// Invoke a query to put the on-disk cached value in memory.
246 pub(crate) try_load_from_on_disk_cache: fn(QueryCtxt<'_>, &DepNode),
247 }
248
249 macro_rules! handle_cycle_error {
250 ([][$tcx: expr, $error:expr]) => {{
251 $error.emit();
252 Value::from_cycle_error($tcx)
253 }};
254 ([fatal_cycle $($rest:tt)*][$tcx:expr, $error:expr]) => {{
255 $error.emit();
256 $tcx.sess.abort_if_errors();
257 unreachable!()
258 }};
259 ([cycle_delay_bug $($rest:tt)*][$tcx:expr, $error:expr]) => {{
260 $error.delay_as_bug();
261 Value::from_cycle_error($tcx)
262 }};
263 ([$other:ident $(($($other_args:tt)*))* $(, $($modifiers:tt)*)*][$($args:tt)*]) => {
264 handle_cycle_error!([$($($modifiers)*)*][$($args)*])
265 };
266 }
267
268 macro_rules! is_anon {
269 ([]) => {{
270 false
271 }};
272 ([anon $($rest:tt)*]) => {{
273 true
274 }};
275 ([$other:ident $(($($other_args:tt)*))* $(, $($modifiers:tt)*)*]) => {
276 is_anon!([$($($modifiers)*)*])
277 };
278 }
279
280 macro_rules! is_eval_always {
281 ([]) => {{
282 false
283 }};
284 ([eval_always $($rest:tt)*]) => {{
285 true
286 }};
287 ([$other:ident $(($($other_args:tt)*))* $(, $($modifiers:tt)*)*]) => {
288 is_eval_always!([$($($modifiers)*)*])
289 };
290 }
291
292 macro_rules! hash_result {
293 ([][$hcx:expr, $result:expr]) => {{
294 dep_graph::hash_result($hcx, &$result)
295 }};
296 ([no_hash $($rest:tt)*][$hcx:expr, $result:expr]) => {{
297 None
298 }};
299 ([$other:ident $(($($other_args:tt)*))* $(, $($modifiers:tt)*)*][$($args:tt)*]) => {
300 hash_result!([$($($modifiers)*)*][$($args)*])
301 };
302 }
303
304 macro_rules! define_queries {
305 (<$tcx:tt>
306 $($(#[$attr:meta])*
307 [$($modifiers:tt)*] fn $name:ident($($K:tt)*) -> $V:ty,)*) => {
308
309 define_queries_struct! {
310 tcx: $tcx,
311 input: ($(([$($modifiers)*] [$($attr)*] [$name]))*)
312 }
313
314 mod make_query {
315 use super::*;
316
317 // Create an eponymous constructor for each query.
318 $(#[allow(nonstandard_style)] $(#[$attr])*
319 pub fn $name<$tcx>(tcx: QueryCtxt<$tcx>, key: query_keys::$name<$tcx>) -> QueryStackFrame {
320 let kind = dep_graph::DepKind::$name;
321 let name = stringify!($name);
322 let description = ty::print::with_forced_impl_filename_line(
323 // Force filename-line mode to avoid invoking `type_of` query.
324 || queries::$name::describe(tcx, key)
325 );
326 let description = if tcx.sess.verbose() {
327 format!("{} [{}]", description, name)
328 } else {
329 description
330 };
331 let span = if kind == dep_graph::DepKind::def_span {
332 // The `def_span` query is used to calculate `default_span`,
333 // so exit to avoid infinite recursion.
334 None
335 } else {
336 Some(key.default_span(*tcx))
337 };
338 let hash = || {
339 let mut hcx = tcx.create_stable_hashing_context();
340 let mut hasher = StableHasher::new();
341 std::mem::discriminant(&kind).hash_stable(&mut hcx, &mut hasher);
342 key.hash_stable(&mut hcx, &mut hasher);
343 hasher.finish::<u64>()
344 };
345
346 QueryStackFrame::new(name, description, span, hash)
347 })*
348 }
349
350 #[allow(nonstandard_style)]
351 pub mod queries {
352 use std::marker::PhantomData;
353
354 $(pub struct $name<$tcx> {
355 data: PhantomData<&$tcx ()>
356 })*
357 }
358
359 $(impl<$tcx> QueryConfig for queries::$name<$tcx> {
360 type Key = query_keys::$name<$tcx>;
361 type Value = query_values::$name<$tcx>;
362 type Stored = query_stored::$name<$tcx>;
363 const NAME: &'static str = stringify!($name);
364 }
365
366 impl<$tcx> QueryAccessors<QueryCtxt<$tcx>> for queries::$name<$tcx> {
367 const ANON: bool = is_anon!([$($modifiers)*]);
368 const EVAL_ALWAYS: bool = is_eval_always!([$($modifiers)*]);
369 const DEP_KIND: dep_graph::DepKind = dep_graph::DepKind::$name;
370
371 type Cache = query_storage::$name<$tcx>;
372
373 #[inline(always)]
374 fn query_state<'a>(tcx: QueryCtxt<$tcx>) -> &'a QueryState<crate::dep_graph::DepKind, Self::Key>
375 where QueryCtxt<$tcx>: 'a
376 {
377 &tcx.queries.$name
378 }
379
380 #[inline(always)]
381 fn query_cache<'a>(tcx: QueryCtxt<$tcx>) -> &'a QueryCacheStore<Self::Cache>
382 where 'tcx:'a
383 {
384 &tcx.query_caches.$name
385 }
386
387 #[inline]
388 fn compute_fn(tcx: QueryCtxt<'tcx>, key: &Self::Key) ->
389 fn(TyCtxt<'tcx>, Self::Key) -> Self::Value
390 {
391 if key.query_crate_is_local() {
392 tcx.queries.local_providers.$name
393 } else {
394 tcx.queries.extern_providers.$name
395 }
396 }
397
398 fn hash_result(
399 _hcx: &mut StableHashingContext<'_>,
400 _result: &Self::Value
401 ) -> Option<Fingerprint> {
402 hash_result!([$($modifiers)*][_hcx, _result])
403 }
404
405 fn handle_cycle_error(
406 tcx: QueryCtxt<'tcx>,
407 mut error: DiagnosticBuilder<'_>,
408 ) -> Self::Value {
409 handle_cycle_error!([$($modifiers)*][tcx, error])
410 }
411 })*
412
413 #[allow(non_upper_case_globals)]
414 pub mod query_callbacks {
415 use super::*;
416 use rustc_middle::dep_graph::DepNode;
417 use rustc_middle::ty::query::query_keys;
418 use rustc_query_system::dep_graph::DepNodeParams;
419 use rustc_query_system::query::{force_query, QueryDescription};
420
421 // We use this for most things when incr. comp. is turned off.
422 pub const Null: QueryStruct = QueryStruct {
423 force_from_dep_node: |_, dep_node| bug!("force_from_dep_node: encountered {:?}", dep_node),
424 try_load_from_on_disk_cache: |_, _| {},
425 };
426
427 pub const TraitSelect: QueryStruct = QueryStruct {
428 force_from_dep_node: |_, _| false,
429 try_load_from_on_disk_cache: |_, _| {},
430 };
431
432 pub const CompileCodegenUnit: QueryStruct = QueryStruct {
433 force_from_dep_node: |_, _| false,
434 try_load_from_on_disk_cache: |_, _| {},
435 };
436
437 pub const CompileMonoItem: QueryStruct = QueryStruct {
438 force_from_dep_node: |_, _| false,
439 try_load_from_on_disk_cache: |_, _| {},
440 };
441
442 $(pub const $name: QueryStruct = {
443 const is_anon: bool = is_anon!([$($modifiers)*]);
444
445 #[inline(always)]
446 fn can_reconstruct_query_key() -> bool {
447 <query_keys::$name<'_> as DepNodeParams<TyCtxt<'_>>>
448 ::can_reconstruct_query_key()
449 }
450
451 fn recover<'tcx>(tcx: TyCtxt<'tcx>, dep_node: &DepNode) -> Option<query_keys::$name<'tcx>> {
452 <query_keys::$name<'_> as DepNodeParams<TyCtxt<'_>>>::recover(tcx, dep_node)
453 }
454
455 fn force_from_dep_node(tcx: QueryCtxt<'_>, dep_node: &DepNode) -> bool {
456 force_query::<queries::$name<'_>, _>(tcx, dep_node)
457 }
458
459 fn try_load_from_on_disk_cache(tcx: QueryCtxt<'_>, dep_node: &DepNode) {
460 if is_anon {
461 return
462 }
463
464 if !can_reconstruct_query_key() {
465 return
466 }
467
468 debug_assert!(tcx.dep_graph.is_green(dep_node));
469
470 let key = recover(*tcx, dep_node).unwrap_or_else(|| panic!("Failed to recover key for {:?} with hash {}", dep_node, dep_node.hash));
471 if queries::$name::cache_on_disk(tcx, &key, None) {
472 let _ = tcx.$name(key);
473 }
474 }
475
476 QueryStruct {
477 force_from_dep_node,
478 try_load_from_on_disk_cache,
479 }
480 };)*
481 }
482
483 static QUERY_CALLBACKS: &[QueryStruct] = &make_dep_kind_array!(query_callbacks);
484 }
485 }
486
487 // FIXME(eddyb) this macro (and others?) use `$tcx` and `'tcx` interchangeably.
488 // We should either not take `$tcx` at all and use `'tcx` everywhere, or use
489 // `$tcx` everywhere (even if that isn't necessary due to lack of hygiene).
490 macro_rules! define_queries_struct {
491 (tcx: $tcx:tt,
492 input: ($(([$($modifiers:tt)*] [$($attr:tt)*] [$name:ident]))*)) => {
493 pub struct Queries<$tcx> {
494 local_providers: Box<Providers>,
495 extern_providers: Box<Providers>,
496
497 pub on_disk_cache: Option<OnDiskCache<$tcx>>,
498
499 $($(#[$attr])* $name: QueryState<
500 crate::dep_graph::DepKind,
501 query_keys::$name<$tcx>,
502 >,)*
503 }
504
505 impl<$tcx> Queries<$tcx> {
506 pub fn new(
507 local_providers: Providers,
508 extern_providers: Providers,
509 on_disk_cache: Option<OnDiskCache<$tcx>>,
510 ) -> Self {
511 Queries {
512 local_providers: Box::new(local_providers),
513 extern_providers: Box::new(extern_providers),
514 on_disk_cache,
515 $($name: Default::default()),*
516 }
517 }
518
519 pub(crate) fn try_collect_active_jobs(
520 &$tcx self,
521 tcx: TyCtxt<$tcx>,
522 ) -> Option<QueryMap<crate::dep_graph::DepKind>> {
523 let tcx = QueryCtxt { tcx, queries: self };
524 let mut jobs = QueryMap::default();
525
526 $(
527 self.$name.try_collect_active_jobs(
528 tcx,
529 dep_graph::DepKind::$name,
530 make_query::$name,
531 &mut jobs,
532 )?;
533 )*
534
535 Some(jobs)
536 }
537 }
538
539 impl QueryEngine<'tcx> for Queries<'tcx> {
540 fn as_any(&'tcx self) -> &'tcx dyn std::any::Any {
541 let this = unsafe { std::mem::transmute::<&Queries<'_>, &Queries<'_>>(self) };
542 this as _
543 }
544
545 fn try_mark_green(&'tcx self, tcx: TyCtxt<'tcx>, dep_node: &dep_graph::DepNode) -> bool {
546 let qcx = QueryCtxt { tcx, queries: self };
547 tcx.dep_graph.try_mark_green(qcx, dep_node).is_some()
548 }
549
550 $($(#[$attr])*
551 #[inline(always)]
552 fn $name(
553 &'tcx self,
554 tcx: TyCtxt<$tcx>,
555 span: Span,
556 key: query_keys::$name<$tcx>,
557 lookup: QueryLookup,
558 mode: QueryMode,
559 ) -> Option<query_stored::$name<$tcx>> {
560 let qcx = QueryCtxt { tcx, queries: self };
561 get_query::<queries::$name<$tcx>, _>(qcx, span, key, lookup, mode)
562 })*
563 }
564 };
565 }
566
567 fn describe_as_module(def_id: LocalDefId, tcx: TyCtxt<'_>) -> String {
568 if def_id.is_top_level_module() {
569 "top-level module".to_string()
570 } else {
571 format!("module `{}`", tcx.def_path_str(def_id.to_def_id()))
572 }
573 }
574
575 rustc_query_description! {}