]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_query_impl/src/plumbing.rs
Move `try_on_disk_cache` out of the giant macro
[rust.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::keys::Key;
6 use crate::{on_disk_cache, Queries};
7 use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
8 use rustc_data_structures::sync::Lock;
9 use rustc_errors::{Diagnostic, Handler};
10 use rustc_middle::dep_graph::{self, DepKind, DepNode, DepNodeIndex, SerializedDepNodeIndex};
11 use rustc_middle::ty::tls::{self, ImplicitCtxt};
12 use rustc_middle::ty::{self, TyCtxt};
13 use rustc_query_system::dep_graph::HasDepContext;
14 use rustc_query_system::ich::StableHashingContext;
15 use rustc_query_system::query::{
16     QueryContext, QueryJobId, QueryMap, QuerySideEffects, QueryStackFrame,
17 };
18 use std::any::Any;
19 use std::num::NonZeroU64;
20 use thin_vec::ThinVec;
21
22 #[derive(Copy, Clone)]
23 pub struct QueryCtxt<'tcx> {
24     pub tcx: TyCtxt<'tcx>,
25     pub queries: &'tcx Queries<'tcx>,
26 }
27
28 impl<'tcx> std::ops::Deref for QueryCtxt<'tcx> {
29     type Target = TyCtxt<'tcx>;
30
31     #[inline]
32     fn deref(&self) -> &Self::Target {
33         &self.tcx
34     }
35 }
36
37 impl<'tcx> HasDepContext for QueryCtxt<'tcx> {
38     type DepKind = rustc_middle::dep_graph::DepKind;
39     type DepContext = TyCtxt<'tcx>;
40
41     #[inline]
42     fn dep_context(&self) -> &Self::DepContext {
43         &self.tcx
44     }
45 }
46
47 impl QueryContext for QueryCtxt<'_> {
48     fn next_job_id(&self) -> QueryJobId {
49         QueryJobId(
50             NonZeroU64::new(
51                 self.queries.jobs.fetch_add(1, rustc_data_structures::sync::Ordering::Relaxed),
52             )
53             .unwrap(),
54         )
55     }
56
57     fn current_query_job(&self) -> Option<QueryJobId> {
58         tls::with_related_context(**self, |icx| icx.query)
59     }
60
61     fn try_collect_active_jobs(&self) -> Option<QueryMap> {
62         self.queries.try_collect_active_jobs(**self)
63     }
64
65     // Interactions with on_disk_cache
66     fn load_side_effects(&self, prev_dep_node_index: SerializedDepNodeIndex) -> QuerySideEffects {
67         self.queries
68             .on_disk_cache
69             .as_ref()
70             .map(|c| c.load_side_effects(**self, prev_dep_node_index))
71             .unwrap_or_default()
72     }
73
74     fn store_side_effects(&self, dep_node_index: DepNodeIndex, side_effects: QuerySideEffects) {
75         if let Some(c) = self.queries.on_disk_cache.as_ref() {
76             c.store_side_effects(dep_node_index, side_effects)
77         }
78     }
79
80     fn store_side_effects_for_anon_node(
81         &self,
82         dep_node_index: DepNodeIndex,
83         side_effects: QuerySideEffects,
84     ) {
85         if let Some(c) = self.queries.on_disk_cache.as_ref() {
86             c.store_side_effects_for_anon_node(dep_node_index, side_effects)
87         }
88     }
89
90     /// Executes a job by changing the `ImplicitCtxt` to point to the
91     /// new query job while it executes. It returns the diagnostics
92     /// captured during execution and the actual result.
93     #[inline(always)]
94     fn start_query<R>(
95         &self,
96         token: QueryJobId,
97         depth_limit: bool,
98         diagnostics: Option<&Lock<ThinVec<Diagnostic>>>,
99         compute: impl FnOnce() -> R,
100     ) -> R {
101         // The `TyCtxt` stored in TLS has the same global interner lifetime
102         // as `self`, so we use `with_related_context` to relate the 'tcx lifetimes
103         // when accessing the `ImplicitCtxt`.
104         tls::with_related_context(**self, move |current_icx| {
105             if depth_limit && !self.recursion_limit().value_within_limit(current_icx.query_depth) {
106                 self.depth_limit_error();
107             }
108
109             // Update the `ImplicitCtxt` to point to our new query job.
110             let new_icx = ImplicitCtxt {
111                 tcx: **self,
112                 query: Some(token),
113                 diagnostics,
114                 query_depth: current_icx.query_depth + depth_limit as usize,
115                 task_deps: current_icx.task_deps,
116             };
117
118             // Use the `ImplicitCtxt` while we execute the query.
119             tls::enter_context(&new_icx, |_| {
120                 rustc_data_structures::stack::ensure_sufficient_stack(compute)
121             })
122         })
123     }
124 }
125
126 impl<'tcx> QueryCtxt<'tcx> {
127     #[inline]
128     pub fn from_tcx(tcx: TyCtxt<'tcx>) -> Self {
129         let queries = tcx.queries.as_any();
130         let queries = unsafe {
131             let queries = std::mem::transmute::<&dyn Any, &dyn Any>(queries);
132             let queries = queries.downcast_ref().unwrap();
133             let queries = std::mem::transmute::<&Queries<'_>, &Queries<'_>>(queries);
134             queries
135         };
136         QueryCtxt { tcx, queries }
137     }
138
139     pub(crate) fn on_disk_cache(self) -> Option<&'tcx on_disk_cache::OnDiskCache<'tcx>> {
140         self.queries.on_disk_cache.as_ref()
141     }
142
143     pub(super) fn encode_query_results(
144         self,
145         encoder: &mut on_disk_cache::CacheEncoder<'_, 'tcx>,
146         query_result_index: &mut on_disk_cache::EncodedDepNodeIndex,
147     ) {
148         macro_rules! encode_queries {
149             ($($query:ident,)*) => {
150                 $(
151                     on_disk_cache::encode_query_results::<_, super::queries::$query<'_>>(
152                         self,
153                         encoder,
154                         query_result_index
155                     );
156                 )*
157             }
158         }
159
160         rustc_cached_queries!(encode_queries!);
161     }
162
163     pub fn try_print_query_stack(
164         self,
165         query: Option<QueryJobId>,
166         handler: &Handler,
167         num_frames: Option<usize>,
168     ) -> usize {
169         rustc_query_system::query::print_query_stack(self, query, handler, num_frames)
170     }
171 }
172
173 macro_rules! handle_cycle_error {
174     ([][$tcx: expr, $error:expr]) => {{
175         $error.emit();
176         Value::from_cycle_error($tcx)
177     }};
178     ([(fatal_cycle) $($rest:tt)*][$tcx:expr, $error:expr]) => {{
179         $error.emit();
180         $tcx.sess.abort_if_errors();
181         unreachable!()
182     }};
183     ([(cycle_delay_bug) $($rest:tt)*][$tcx:expr, $error:expr]) => {{
184         $error.delay_as_bug();
185         Value::from_cycle_error($tcx)
186     }};
187     ([$other:tt $($modifiers:tt)*][$($args:tt)*]) => {
188         handle_cycle_error!([$($modifiers)*][$($args)*])
189     };
190 }
191
192 macro_rules! is_anon {
193     ([]) => {{
194         false
195     }};
196     ([(anon) $($rest:tt)*]) => {{
197         true
198     }};
199     ([$other:tt $($modifiers:tt)*]) => {
200         is_anon!([$($modifiers)*])
201     };
202 }
203
204 macro_rules! is_eval_always {
205     ([]) => {{
206         false
207     }};
208     ([(eval_always) $($rest:tt)*]) => {{
209         true
210     }};
211     ([$other:tt $($modifiers:tt)*]) => {
212         is_eval_always!([$($modifiers)*])
213     };
214 }
215
216 macro_rules! depth_limit {
217     ([]) => {{
218         false
219     }};
220     ([(depth_limit) $($rest:tt)*]) => {{
221         true
222     }};
223     ([$other:tt $($modifiers:tt)*]) => {
224         depth_limit!([$($modifiers)*])
225     };
226 }
227
228 macro_rules! hash_result {
229     ([]) => {{
230         Some(dep_graph::hash_result)
231     }};
232     ([(no_hash) $($rest:tt)*]) => {{
233         None
234     }};
235     ([$other:tt $($modifiers:tt)*]) => {
236         hash_result!([$($modifiers)*])
237     };
238 }
239
240 macro_rules! get_provider {
241     ([][$tcx:expr, $name:ident, $key:expr]) => {{
242         $tcx.queries.local_providers.$name
243     }};
244     ([(separate_provide_extern) $($rest:tt)*][$tcx:expr, $name:ident, $key:expr]) => {{
245         if $key.query_crate_is_local() {
246             $tcx.queries.local_providers.$name
247         } else {
248             $tcx.queries.extern_providers.$name
249         }
250     }};
251     ([$other:tt $($modifiers:tt)*][$($args:tt)*]) => {
252         get_provider!([$($modifiers)*][$($args)*])
253     };
254 }
255
256 pub(crate) fn create_query_frame<
257     'tcx,
258     K: Copy + Key + for<'a> HashStable<StableHashingContext<'a>>,
259 >(
260     tcx: QueryCtxt<'tcx>,
261     do_describe: fn(QueryCtxt<'tcx>, K) -> String,
262     key: K,
263     kind: DepKind,
264     name: &'static str,
265 ) -> QueryStackFrame {
266     // Disable visible paths printing for performance reasons.
267     // Showing visible path instead of any path is not that important in production.
268     let description = ty::print::with_no_visible_paths!(
269         // Force filename-line mode to avoid invoking `type_of` query.
270         ty::print::with_forced_impl_filename_line!(do_describe(tcx, key))
271     );
272     let description =
273         if tcx.sess.verbose() { format!("{} [{}]", description, name) } else { description };
274     let span = if kind == dep_graph::DepKind::def_span {
275         // The `def_span` query is used to calculate `default_span`,
276         // so exit to avoid infinite recursion.
277         None
278     } else {
279         Some(key.default_span(*tcx))
280     };
281     let def_kind = if kind == dep_graph::DepKind::opt_def_kind {
282         // Try to avoid infinite recursion.
283         None
284     } else {
285         key.key_as_def_id()
286             .and_then(|def_id| def_id.as_local())
287             .and_then(|def_id| tcx.opt_def_kind(def_id))
288     };
289     let hash = || {
290         tcx.with_stable_hashing_context(|mut hcx| {
291             let mut hasher = StableHasher::new();
292             std::mem::discriminant(&kind).hash_stable(&mut hcx, &mut hasher);
293             key.hash_stable(&mut hcx, &mut hasher);
294             hasher.finish::<u64>()
295         })
296     };
297
298     QueryStackFrame::new(name, description, span, def_kind, hash)
299 }
300
301 pub(crate) fn try_load_from_on_disk_cache<'tcx, K, V>(
302     tcx: TyCtxt<'tcx>,
303     dep_node: DepNode,
304     recover: fn(TyCtxt<'tcx>, DepNode) -> Option<K>,
305     cache_on_disk: fn(TyCtxt<'tcx>, &K) -> bool,
306     do_query: fn(TyCtxt<'tcx>, K) -> V,
307 ) {
308     debug_assert!(tcx.dep_graph.is_green(&dep_node));
309
310     let key = recover(tcx, dep_node).unwrap_or_else(|| {
311         panic!("Failed to recover key for {:?} with hash {}", dep_node, dep_node.hash)
312     });
313     if cache_on_disk(tcx, &key) {
314         let _ = do_query(tcx, key);
315     }
316 }
317
318 // NOTE: `$V` isn't used here, but we still need to match on it so it can be passed to other macros
319 // invoked by `rustc_query_append`.
320 macro_rules! define_queries {
321     (
322      $($(#[$attr:meta])*
323         [$($modifiers:tt)*] fn $name:ident($($K:tt)*) -> $V:ty,)*) => {
324         define_queries_struct! {
325             input: ($(([$($modifiers)*] [$($attr)*] [$name]))*)
326         }
327
328         #[allow(nonstandard_style)]
329         mod queries {
330             use std::marker::PhantomData;
331
332             $(pub struct $name<'tcx> {
333                 data: PhantomData<&'tcx ()>
334             })*
335         }
336
337         $(impl<'tcx> QueryConfig for queries::$name<'tcx> {
338             type Key = query_keys::$name<'tcx>;
339             type Value = query_values::$name<'tcx>;
340             type Stored = query_stored::$name<'tcx>;
341             const NAME: &'static str = stringify!($name);
342         }
343
344         impl<'tcx> QueryDescription<QueryCtxt<'tcx>> for queries::$name<'tcx> {
345             rustc_query_description! { $name }
346
347             type Cache = query_storage::$name<'tcx>;
348
349             #[inline(always)]
350             fn query_state<'a>(tcx: QueryCtxt<'tcx>) -> &'a QueryState<Self::Key>
351                 where QueryCtxt<'tcx>: 'a
352             {
353                 &tcx.queries.$name
354             }
355
356             #[inline(always)]
357             fn query_cache<'a>(tcx: QueryCtxt<'tcx>) -> &'a Self::Cache
358                 where 'tcx:'a
359             {
360                 &tcx.query_caches.$name
361             }
362
363             #[inline]
364             fn make_vtable(tcx: QueryCtxt<'tcx>, key: &Self::Key) ->
365                 QueryVTable<QueryCtxt<'tcx>, Self::Key, Self::Value>
366             {
367                 let compute = get_provider!([$($modifiers)*][tcx, $name, key]);
368                 let cache_on_disk = Self::cache_on_disk(tcx.tcx, key);
369                 QueryVTable {
370                     anon: is_anon!([$($modifiers)*]),
371                     eval_always: is_eval_always!([$($modifiers)*]),
372                     depth_limit: depth_limit!([$($modifiers)*]),
373                     dep_kind: dep_graph::DepKind::$name,
374                     hash_result: hash_result!([$($modifiers)*]),
375                     handle_cycle_error: |tcx, mut error| handle_cycle_error!([$($modifiers)*][tcx, error]),
376                     compute,
377                     cache_on_disk,
378                     try_load_from_disk: Self::TRY_LOAD_FROM_DISK,
379                 }
380             }
381         })*
382
383         #[allow(nonstandard_style)]
384         mod query_callbacks {
385             use super::*;
386             use rustc_middle::dep_graph::DepNode;
387             use rustc_query_system::dep_graph::DepNodeParams;
388             use rustc_query_system::query::{force_query, QueryDescription};
389             use rustc_query_system::dep_graph::FingerprintStyle;
390
391             // We use this for most things when incr. comp. is turned off.
392             pub fn Null() -> DepKindStruct {
393                 DepKindStruct {
394                     is_anon: false,
395                     is_eval_always: false,
396                     fingerprint_style: FingerprintStyle::Unit,
397                     force_from_dep_node: Some(|_, dep_node| bug!("force_from_dep_node: encountered {:?}", dep_node)),
398                     try_load_from_on_disk_cache: None,
399                 }
400             }
401
402             // We use this for the forever-red node.
403             pub fn Red() -> DepKindStruct {
404                 DepKindStruct {
405                     is_anon: false,
406                     is_eval_always: false,
407                     fingerprint_style: FingerprintStyle::Unit,
408                     force_from_dep_node: Some(|_, dep_node| bug!("force_from_dep_node: encountered {:?}", dep_node)),
409                     try_load_from_on_disk_cache: None,
410                 }
411             }
412
413             pub fn TraitSelect() -> DepKindStruct {
414                 DepKindStruct {
415                     is_anon: true,
416                     is_eval_always: false,
417                     fingerprint_style: FingerprintStyle::Unit,
418                     force_from_dep_node: None,
419                     try_load_from_on_disk_cache: None,
420                 }
421             }
422
423             pub fn CompileCodegenUnit() -> DepKindStruct {
424                 DepKindStruct {
425                     is_anon: false,
426                     is_eval_always: false,
427                     fingerprint_style: FingerprintStyle::Opaque,
428                     force_from_dep_node: None,
429                     try_load_from_on_disk_cache: None,
430                 }
431             }
432
433             pub fn CompileMonoItem() -> DepKindStruct {
434                 DepKindStruct {
435                     is_anon: false,
436                     is_eval_always: false,
437                     fingerprint_style: FingerprintStyle::Opaque,
438                     force_from_dep_node: None,
439                     try_load_from_on_disk_cache: None,
440                 }
441             }
442
443             $(pub(crate) fn $name()-> DepKindStruct {
444                 let is_anon = is_anon!([$($modifiers)*]);
445                 let is_eval_always = is_eval_always!([$($modifiers)*]);
446
447                 let fingerprint_style =
448                     <<queries::$name<'_> as QueryConfig>::Key as DepNodeParams<TyCtxt<'_>>>::fingerprint_style();
449
450                 if is_anon || !fingerprint_style.reconstructible() {
451                     return DepKindStruct {
452                         is_anon,
453                         is_eval_always,
454                         fingerprint_style,
455                         force_from_dep_node: None,
456                         try_load_from_on_disk_cache: None,
457                     }
458                 }
459
460                 #[inline(always)]
461                 fn recover<'tcx>(tcx: TyCtxt<'tcx>, dep_node: DepNode) -> Option<<queries::$name<'tcx> as QueryConfig>::Key> {
462                     <<queries::$name<'_> as QueryConfig>::Key as DepNodeParams<TyCtxt<'_>>>::recover(tcx, &dep_node)
463                 }
464
465                 fn force_from_dep_node(tcx: TyCtxt<'_>, dep_node: DepNode) -> bool {
466                     if let Some(key) = recover(tcx, dep_node) {
467                         #[cfg(debug_assertions)]
468                         let _guard = tracing::span!(tracing::Level::TRACE, stringify!($name), ?key).entered();
469                         let tcx = QueryCtxt::from_tcx(tcx);
470                         force_query::<queries::$name<'_>, _>(tcx, key, dep_node);
471                         true
472                     } else {
473                         false
474                     }
475                 }
476
477                 DepKindStruct {
478                     is_anon,
479                     is_eval_always,
480                     fingerprint_style,
481                     force_from_dep_node: Some(force_from_dep_node),
482                     try_load_from_on_disk_cache: Some(|tcx, key| $crate::plumbing::try_load_from_on_disk_cache(tcx, key, recover, queries::$name::cache_on_disk, TyCtxt::$name)),
483                 }
484             })*
485         }
486
487         pub fn query_callbacks<'tcx>(arena: &'tcx Arena<'tcx>) -> &'tcx [DepKindStruct] {
488             arena.alloc_from_iter(make_dep_kind_array!(query_callbacks))
489         }
490     }
491 }
492
493 macro_rules! define_queries_struct {
494     (
495      input: ($(([$($modifiers:tt)*] [$($attr:tt)*] [$name:ident]))*)) => {
496         pub struct Queries<'tcx> {
497             local_providers: Box<Providers>,
498             extern_providers: Box<ExternProviders>,
499
500             pub on_disk_cache: Option<OnDiskCache<'tcx>>,
501
502             jobs: AtomicU64,
503
504             $($(#[$attr])*  $name: QueryState<<queries::$name<'tcx> as QueryConfig>::Key>,)*
505         }
506
507         impl<'tcx> Queries<'tcx> {
508             pub fn new(
509                 local_providers: Providers,
510                 extern_providers: ExternProviders,
511                 on_disk_cache: Option<OnDiskCache<'tcx>>,
512             ) -> Self {
513                 Queries {
514                     local_providers: Box::new(local_providers),
515                     extern_providers: Box::new(extern_providers),
516                     on_disk_cache,
517                     jobs: AtomicU64::new(1),
518                     $($name: Default::default()),*
519                 }
520             }
521
522             pub(crate) fn try_collect_active_jobs(
523                 &'tcx self,
524                 tcx: TyCtxt<'tcx>,
525             ) -> Option<QueryMap> {
526                 let tcx = QueryCtxt { tcx, queries: self };
527                 let mut jobs = QueryMap::default();
528
529                 $(
530                     let make_query = |tcx, key| {
531                         let kind = dep_graph::DepKind::$name;
532                         let name = stringify!($name);
533                         $crate::plumbing::create_query_frame(tcx, queries::$name::describe, key, kind, name)
534                     };
535                     self.$name.try_collect_active_jobs(
536                         tcx,
537                         make_query,
538                         &mut jobs,
539                     )?;
540                 )*
541
542                 Some(jobs)
543             }
544         }
545
546         impl<'tcx> QueryEngine<'tcx> for Queries<'tcx> {
547             fn as_any(&'tcx self) -> &'tcx dyn std::any::Any {
548                 let this = unsafe { std::mem::transmute::<&Queries<'_>, &Queries<'_>>(self) };
549                 this as _
550             }
551
552             fn try_mark_green(&'tcx self, tcx: TyCtxt<'tcx>, dep_node: &dep_graph::DepNode) -> bool {
553                 let qcx = QueryCtxt { tcx, queries: self };
554                 tcx.dep_graph.try_mark_green(qcx, dep_node).is_some()
555             }
556
557             $($(#[$attr])*
558             #[inline(always)]
559             #[tracing::instrument(level = "trace", skip(self, tcx), ret)]
560             fn $name(
561                 &'tcx self,
562                 tcx: TyCtxt<'tcx>,
563                 span: Span,
564                 key: <queries::$name<'tcx> as QueryConfig>::Key,
565                 mode: QueryMode,
566             ) -> Option<query_stored::$name<'tcx>> {
567                 let qcx = QueryCtxt { tcx, queries: self };
568                 get_query::<queries::$name<'tcx>, _>(qcx, span, key, mode)
569             })*
570         }
571     };
572 }