rustc_query_system/query/
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
5use std::cell::Cell;
6use std::collections::hash_map::Entry;
7use std::fmt::Debug;
8use std::hash::Hash;
9use std::mem;
10
11use rustc_data_structures::fingerprint::Fingerprint;
12use rustc_data_structures::fx::FxHashMap;
13use rustc_data_structures::sharded::Sharded;
14use rustc_data_structures::stack::ensure_sufficient_stack;
15use rustc_data_structures::sync::Lock;
16use rustc_data_structures::{outline, sync};
17use rustc_errors::{Diag, FatalError, StashKey};
18use rustc_span::{DUMMY_SP, Span};
19use thin_vec::ThinVec;
20use tracing::instrument;
21
22use super::QueryConfig;
23use crate::HandleCycleError;
24use crate::dep_graph::{DepContext, DepGraphData, DepNode, DepNodeIndex, DepNodeParams};
25use crate::ich::StableHashingContext;
26use crate::query::caches::QueryCache;
27use crate::query::job::{QueryInfo, QueryJob, QueryJobId, QueryJobInfo, QueryLatch, report_cycle};
28use crate::query::{
29    QueryContext, QueryMap, QuerySideEffects, QueryStackFrame, SerializedDepNodeIndex,
30};
31
32pub struct QueryState<K> {
33    active: Sharded<FxHashMap<K, QueryResult>>,
34}
35
36/// Indicates the state of a query for a given key in a query map.
37enum QueryResult {
38    /// An already executing query. The query job can be used to await for its completion.
39    Started(QueryJob),
40
41    /// The query panicked. Queries trying to wait on this will raise a fatal error which will
42    /// silently panic.
43    Poisoned,
44}
45
46impl QueryResult {
47    /// Unwraps the query job expecting that it has started.
48    fn expect_job(self) -> QueryJob {
49        match self {
50            Self::Started(job) => job,
51            Self::Poisoned => {
52                panic!("job for query failed to start and was poisoned")
53            }
54        }
55    }
56}
57
58impl<K> QueryState<K>
59where
60    K: Eq + Hash + Copy + Debug,
61{
62    pub fn all_inactive(&self) -> bool {
63        self.active.lock_shards().all(|shard| shard.is_empty())
64    }
65
66    pub fn try_collect_active_jobs<Qcx: Copy>(
67        &self,
68        qcx: Qcx,
69        make_query: fn(Qcx, K) -> QueryStackFrame,
70        jobs: &mut QueryMap,
71    ) -> Option<()> {
72        let mut active = Vec::new();
73
74        // We use try_lock_shards here since we are called from the
75        // deadlock handler, and this shouldn't be locked.
76        for shard in self.active.try_lock_shards() {
77            for (k, v) in shard?.iter() {
78                if let QueryResult::Started(ref job) = *v {
79                    active.push((*k, job.clone()));
80                }
81            }
82        }
83
84        // Call `make_query` while we're not holding a `self.active` lock as `make_query` may call
85        // queries leading to a deadlock.
86        for (key, job) in active {
87            let query = make_query(qcx, key);
88            jobs.insert(job.id, QueryJobInfo { query, job });
89        }
90
91        Some(())
92    }
93}
94
95impl<K> Default for QueryState<K> {
96    fn default() -> QueryState<K> {
97        QueryState { active: Default::default() }
98    }
99}
100
101/// A type representing the responsibility to execute the job in the `job` field.
102/// This will poison the relevant query if dropped.
103struct JobOwner<'tcx, K>
104where
105    K: Eq + Hash + Copy,
106{
107    state: &'tcx QueryState<K>,
108    key: K,
109}
110
111#[cold]
112#[inline(never)]
113fn mk_cycle<Q, Qcx>(query: Q, qcx: Qcx, cycle_error: CycleError) -> Q::Value
114where
115    Q: QueryConfig<Qcx>,
116    Qcx: QueryContext,
117{
118    let error = report_cycle(qcx.dep_context().sess(), &cycle_error);
119    handle_cycle_error(query, qcx, &cycle_error, error)
120}
121
122fn handle_cycle_error<Q, Qcx>(
123    query: Q,
124    qcx: Qcx,
125    cycle_error: &CycleError,
126    error: Diag<'_>,
127) -> Q::Value
128where
129    Q: QueryConfig<Qcx>,
130    Qcx: QueryContext,
131{
132    use HandleCycleError::*;
133    match query.handle_cycle_error() {
134        Error => {
135            let guar = error.emit();
136            query.value_from_cycle_error(*qcx.dep_context(), cycle_error, guar)
137        }
138        Fatal => {
139            error.emit();
140            qcx.dep_context().sess().dcx().abort_if_errors();
141            unreachable!()
142        }
143        DelayBug => {
144            let guar = error.delay_as_bug();
145            query.value_from_cycle_error(*qcx.dep_context(), cycle_error, guar)
146        }
147        Stash => {
148            let guar = if let Some(root) = cycle_error.cycle.first()
149                && let Some(span) = root.query.span
150            {
151                error.stash(span, StashKey::Cycle).unwrap()
152            } else {
153                error.emit()
154            };
155            query.value_from_cycle_error(*qcx.dep_context(), cycle_error, guar)
156        }
157    }
158}
159
160impl<'tcx, K> JobOwner<'tcx, K>
161where
162    K: Eq + Hash + Copy,
163{
164    /// Completes the query by updating the query cache with the `result`,
165    /// signals the waiter and forgets the JobOwner, so it won't poison the query
166    fn complete<C>(self, cache: &C, result: C::Value, dep_node_index: DepNodeIndex)
167    where
168        C: QueryCache<Key = K>,
169    {
170        let key = self.key;
171        let state = self.state;
172
173        // Forget ourself so our destructor won't poison the query
174        mem::forget(self);
175
176        // Mark as complete before we remove the job from the active state
177        // so no other thread can re-execute this query.
178        cache.complete(key, result, dep_node_index);
179
180        let job = {
181            let val = {
182                // don't keep the lock during the `unwrap()` of the retrieved value, or we taint the
183                // underlying shard.
184                // since unwinding also wants to look at this map, this can also prevent a double
185                // panic.
186                let mut lock = state.active.lock_shard_by_value(&key);
187                lock.remove(&key)
188            };
189            val.unwrap().expect_job()
190        };
191
192        job.signal_complete();
193    }
194}
195
196impl<'tcx, K> Drop for JobOwner<'tcx, K>
197where
198    K: Eq + Hash + Copy,
199{
200    #[inline(never)]
201    #[cold]
202    fn drop(&mut self) {
203        // Poison the query so jobs waiting on it panic.
204        let state = self.state;
205        let job = {
206            let mut shard = state.active.lock_shard_by_value(&self.key);
207            let job = shard.remove(&self.key).unwrap().expect_job();
208
209            shard.insert(self.key, QueryResult::Poisoned);
210            job
211        };
212        // Also signal the completion of the job, so waiters
213        // will continue execution.
214        job.signal_complete();
215    }
216}
217
218#[derive(Clone, Debug)]
219pub struct CycleError {
220    /// The query and related span that uses the cycle.
221    pub usage: Option<(Span, QueryStackFrame)>,
222    pub cycle: Vec<QueryInfo>,
223}
224
225/// Checks whether there is already a value for this key in the in-memory
226/// query cache, returning that value if present.
227///
228/// (Also performs some associated bookkeeping, if a value was found.)
229#[inline(always)]
230pub fn try_get_cached<Tcx, C>(tcx: Tcx, cache: &C, key: &C::Key) -> Option<C::Value>
231where
232    C: QueryCache,
233    Tcx: DepContext,
234{
235    match cache.lookup(key) {
236        Some((value, index)) => {
237            tcx.profiler().query_cache_hit(index.into());
238            tcx.dep_graph().read_index(index);
239            Some(value)
240        }
241        None => None,
242    }
243}
244
245#[cold]
246#[inline(never)]
247fn cycle_error<Q, Qcx>(
248    query: Q,
249    qcx: Qcx,
250    try_execute: QueryJobId,
251    span: Span,
252) -> (Q::Value, Option<DepNodeIndex>)
253where
254    Q: QueryConfig<Qcx>,
255    Qcx: QueryContext,
256{
257    let error =
258        try_execute.find_cycle_in_stack(qcx.collect_active_jobs(), &qcx.current_query_job(), span);
259    (mk_cycle(query, qcx, error), None)
260}
261
262#[inline(always)]
263fn wait_for_query<Q, Qcx>(
264    query: Q,
265    qcx: Qcx,
266    span: Span,
267    key: Q::Key,
268    latch: QueryLatch,
269    current: Option<QueryJobId>,
270) -> (Q::Value, Option<DepNodeIndex>)
271where
272    Q: QueryConfig<Qcx>,
273    Qcx: QueryContext,
274{
275    // For parallel queries, we'll block and wait until the query running
276    // in another thread has completed. Record how long we wait in the
277    // self-profiler.
278    let query_blocked_prof_timer = qcx.dep_context().profiler().query_blocked();
279
280    // With parallel queries we might just have to wait on some other
281    // thread.
282    let result = latch.wait_on(current, span);
283
284    match result {
285        Ok(()) => {
286            let Some((v, index)) = query.query_cache(qcx).lookup(&key) else {
287                outline(|| {
288                    // We didn't find the query result in the query cache. Check if it was
289                    // poisoned due to a panic instead.
290                    let lock = query.query_state(qcx).active.get_shard_by_value(&key).lock();
291
292                    match lock.get(&key) {
293                        // The query we waited on panicked. Continue unwinding here.
294                        Some(QueryResult::Poisoned) => FatalError.raise(),
295                        _ => panic!(
296                            "query '{}' result must be in the cache or the query must be poisoned after a wait",
297                            query.name()
298                        ),
299                    }
300                })
301            };
302
303            qcx.dep_context().profiler().query_cache_hit(index.into());
304            query_blocked_prof_timer.finish_with_query_invocation_id(index.into());
305
306            (v, Some(index))
307        }
308        Err(cycle) => (mk_cycle(query, qcx, cycle), None),
309    }
310}
311
312#[inline(never)]
313fn try_execute_query<Q, Qcx, const INCR: bool>(
314    query: Q,
315    qcx: Qcx,
316    span: Span,
317    key: Q::Key,
318    dep_node: Option<DepNode>,
319) -> (Q::Value, Option<DepNodeIndex>)
320where
321    Q: QueryConfig<Qcx>,
322    Qcx: QueryContext,
323{
324    let state = query.query_state(qcx);
325    let mut state_lock = state.active.lock_shard_by_value(&key);
326
327    // For the parallel compiler we need to check both the query cache and query state structures
328    // while holding the state lock to ensure that 1) the query has not yet completed and 2) the
329    // query is not still executing. Without checking the query cache here, we can end up
330    // re-executing the query since `try_start` only checks that the query is not currently
331    // executing, but another thread may have already completed the query and stores it result
332    // in the query cache.
333    if qcx.dep_context().sess().threads() > 1 {
334        if let Some((value, index)) = query.query_cache(qcx).lookup(&key) {
335            qcx.dep_context().profiler().query_cache_hit(index.into());
336            return (value, Some(index));
337        }
338    }
339
340    let current_job_id = qcx.current_query_job();
341
342    match state_lock.entry(key) {
343        Entry::Vacant(entry) => {
344            // Nothing has computed or is computing the query, so we start a new job and insert it in the
345            // state map.
346            let id = qcx.next_job_id();
347            let job = QueryJob::new(id, span, current_job_id);
348            entry.insert(QueryResult::Started(job));
349
350            // Drop the lock before we start executing the query
351            drop(state_lock);
352
353            execute_job::<_, _, INCR>(query, qcx, state, key, id, dep_node)
354        }
355        Entry::Occupied(mut entry) => {
356            match entry.get_mut() {
357                QueryResult::Started(job) => {
358                    if sync::is_dyn_thread_safe() {
359                        // Get the latch out
360                        let latch = job.latch();
361                        drop(state_lock);
362
363                        // Only call `wait_for_query` if we're using a Rayon thread pool
364                        // as it will attempt to mark the worker thread as blocked.
365                        return wait_for_query(query, qcx, span, key, latch, current_job_id);
366                    }
367
368                    let id = job.id;
369                    drop(state_lock);
370
371                    // If we are single-threaded we know that we have cycle error,
372                    // so we just return the error.
373                    cycle_error(query, qcx, id, span)
374                }
375                QueryResult::Poisoned => FatalError.raise(),
376            }
377        }
378    }
379}
380
381#[inline(always)]
382fn execute_job<Q, Qcx, const INCR: bool>(
383    query: Q,
384    qcx: Qcx,
385    state: &QueryState<Q::Key>,
386    key: Q::Key,
387    id: QueryJobId,
388    dep_node: Option<DepNode>,
389) -> (Q::Value, Option<DepNodeIndex>)
390where
391    Q: QueryConfig<Qcx>,
392    Qcx: QueryContext,
393{
394    // Use `JobOwner` so the query will be poisoned if executing it panics.
395    let job_owner = JobOwner { state, key };
396
397    debug_assert_eq!(qcx.dep_context().dep_graph().is_fully_enabled(), INCR);
398
399    let (result, dep_node_index) = if INCR {
400        execute_job_incr(
401            query,
402            qcx,
403            qcx.dep_context().dep_graph().data().unwrap(),
404            key,
405            dep_node,
406            id,
407        )
408    } else {
409        execute_job_non_incr(query, qcx, key, id)
410    };
411
412    let cache = query.query_cache(qcx);
413    if query.feedable() {
414        // We should not compute queries that also got a value via feeding.
415        // This can't happen, as query feeding adds the very dependencies to the fed query
416        // as its feeding query had. So if the fed query is red, so is its feeder, which will
417        // get evaluated first, and re-feed the query.
418        if let Some((cached_result, _)) = cache.lookup(&key) {
419            let Some(hasher) = query.hash_result() else {
420                panic!(
421                    "no_hash fed query later has its value computed.\n\
422                    Remove `no_hash` modifier to allow recomputation.\n\
423                    The already cached value: {}",
424                    (query.format_value())(&cached_result)
425                );
426            };
427
428            let (old_hash, new_hash) = qcx.dep_context().with_stable_hashing_context(|mut hcx| {
429                (hasher(&mut hcx, &cached_result), hasher(&mut hcx, &result))
430            });
431            let formatter = query.format_value();
432            if old_hash != new_hash {
433                // We have an inconsistency. This can happen if one of the two
434                // results is tainted by errors.
435                assert!(
436                    qcx.dep_context().sess().dcx().has_errors().is_some(),
437                    "Computed query value for {:?}({:?}) is inconsistent with fed value,\n\
438                        computed={:#?}\nfed={:#?}",
439                    query.dep_kind(),
440                    key,
441                    formatter(&result),
442                    formatter(&cached_result),
443                );
444            }
445        }
446    }
447    job_owner.complete(cache, result, dep_node_index);
448
449    (result, Some(dep_node_index))
450}
451
452// Fast path for when incr. comp. is off.
453#[inline(always)]
454fn execute_job_non_incr<Q, Qcx>(
455    query: Q,
456    qcx: Qcx,
457    key: Q::Key,
458    job_id: QueryJobId,
459) -> (Q::Value, DepNodeIndex)
460where
461    Q: QueryConfig<Qcx>,
462    Qcx: QueryContext,
463{
464    debug_assert!(!qcx.dep_context().dep_graph().is_fully_enabled());
465
466    // Fingerprint the key, just to assert that it doesn't
467    // have anything we don't consider hashable
468    if cfg!(debug_assertions) {
469        let _ = key.to_fingerprint(*qcx.dep_context());
470    }
471
472    let prof_timer = qcx.dep_context().profiler().query_provider();
473    let result = qcx.start_query(job_id, query.depth_limit(), None, || query.compute(qcx, key));
474    let dep_node_index = qcx.dep_context().dep_graph().next_virtual_depnode_index();
475    prof_timer.finish_with_query_invocation_id(dep_node_index.into());
476
477    // Similarly, fingerprint the result to assert that
478    // it doesn't have anything not considered hashable.
479    if cfg!(debug_assertions)
480        && let Some(hash_result) = query.hash_result()
481    {
482        qcx.dep_context().with_stable_hashing_context(|mut hcx| {
483            hash_result(&mut hcx, &result);
484        });
485    }
486
487    (result, dep_node_index)
488}
489
490#[inline(always)]
491fn execute_job_incr<Q, Qcx>(
492    query: Q,
493    qcx: Qcx,
494    dep_graph_data: &DepGraphData<Qcx::Deps>,
495    key: Q::Key,
496    mut dep_node_opt: Option<DepNode>,
497    job_id: QueryJobId,
498) -> (Q::Value, DepNodeIndex)
499where
500    Q: QueryConfig<Qcx>,
501    Qcx: QueryContext,
502{
503    if !query.anon() && !query.eval_always() {
504        // `to_dep_node` is expensive for some `DepKind`s.
505        let dep_node =
506            dep_node_opt.get_or_insert_with(|| query.construct_dep_node(*qcx.dep_context(), &key));
507
508        // The diagnostics for this query will be promoted to the current session during
509        // `try_mark_green()`, so we can ignore them here.
510        if let Some(ret) = qcx.start_query(job_id, false, None, || {
511            try_load_from_disk_and_cache_in_memory(query, dep_graph_data, qcx, &key, dep_node)
512        }) {
513            return ret;
514        }
515    }
516
517    let prof_timer = qcx.dep_context().profiler().query_provider();
518    let diagnostics = Lock::new(ThinVec::new());
519
520    let (result, dep_node_index) =
521        qcx.start_query(job_id, query.depth_limit(), Some(&diagnostics), || {
522            if query.anon() {
523                return dep_graph_data.with_anon_task_inner(
524                    *qcx.dep_context(),
525                    query.dep_kind(),
526                    || query.compute(qcx, key),
527                );
528            }
529
530            // `to_dep_node` is expensive for some `DepKind`s.
531            let dep_node =
532                dep_node_opt.unwrap_or_else(|| query.construct_dep_node(*qcx.dep_context(), &key));
533
534            dep_graph_data.with_task(
535                dep_node,
536                (qcx, query),
537                key,
538                |(qcx, query), key| query.compute(qcx, key),
539                query.hash_result(),
540            )
541        });
542
543    prof_timer.finish_with_query_invocation_id(dep_node_index.into());
544
545    let side_effects = QuerySideEffects { diagnostics: diagnostics.into_inner() };
546
547    if std::intrinsics::unlikely(side_effects.maybe_any()) {
548        if query.anon() {
549            qcx.store_side_effects_for_anon_node(dep_node_index, side_effects);
550        } else {
551            qcx.store_side_effects(dep_node_index, side_effects);
552        }
553    }
554
555    (result, dep_node_index)
556}
557
558#[inline(always)]
559fn try_load_from_disk_and_cache_in_memory<Q, Qcx>(
560    query: Q,
561    dep_graph_data: &DepGraphData<Qcx::Deps>,
562    qcx: Qcx,
563    key: &Q::Key,
564    dep_node: &DepNode,
565) -> Option<(Q::Value, DepNodeIndex)>
566where
567    Q: QueryConfig<Qcx>,
568    Qcx: QueryContext,
569{
570    // Note this function can be called concurrently from the same query
571    // We must ensure that this is handled correctly.
572
573    let (prev_dep_node_index, dep_node_index) = dep_graph_data.try_mark_green(qcx, dep_node)?;
574
575    debug_assert!(dep_graph_data.is_index_green(prev_dep_node_index));
576
577    // First we try to load the result from the on-disk cache.
578    // Some things are never cached on disk.
579    if let Some(result) = query.try_load_from_disk(qcx, key, prev_dep_node_index, dep_node_index) {
580        if std::intrinsics::unlikely(qcx.dep_context().sess().opts.unstable_opts.query_dep_graph) {
581            dep_graph_data.mark_debug_loaded_from_disk(*dep_node)
582        }
583
584        let prev_fingerprint = dep_graph_data.prev_fingerprint_of(prev_dep_node_index);
585        // If `-Zincremental-verify-ich` is specified, re-hash results from
586        // the cache and make sure that they have the expected fingerprint.
587        //
588        // If not, we still seek to verify a subset of fingerprints loaded
589        // from disk. Re-hashing results is fairly expensive, so we can't
590        // currently afford to verify every hash. This subset should still
591        // give us some coverage of potential bugs though.
592        let try_verify = prev_fingerprint.split().1.as_u64() % 32 == 0;
593        if std::intrinsics::unlikely(
594            try_verify || qcx.dep_context().sess().opts.unstable_opts.incremental_verify_ich,
595        ) {
596            incremental_verify_ich(
597                *qcx.dep_context(),
598                dep_graph_data,
599                &result,
600                prev_dep_node_index,
601                query.hash_result(),
602                query.format_value(),
603            );
604        }
605
606        return Some((result, dep_node_index));
607    }
608
609    // We always expect to find a cached result for things that
610    // can be forced from `DepNode`.
611    debug_assert!(
612        !query.cache_on_disk(*qcx.dep_context(), key)
613            || !qcx.dep_context().fingerprint_style(dep_node.kind).reconstructible(),
614        "missing on-disk cache entry for {dep_node:?}"
615    );
616
617    // Sanity check for the logic in `ensure`: if the node is green and the result loadable,
618    // we should actually be able to load it.
619    debug_assert!(
620        !query.loadable_from_disk(qcx, key, prev_dep_node_index),
621        "missing on-disk cache entry for loadable {dep_node:?}"
622    );
623
624    // We could not load a result from the on-disk cache, so
625    // recompute.
626    let prof_timer = qcx.dep_context().profiler().query_provider();
627
628    // The dep-graph for this computation is already in-place.
629    let result = qcx.dep_context().dep_graph().with_ignore(|| query.compute(qcx, *key));
630
631    prof_timer.finish_with_query_invocation_id(dep_node_index.into());
632
633    // Verify that re-running the query produced a result with the expected hash
634    // This catches bugs in query implementations, turning them into ICEs.
635    // For example, a query might sort its result by `DefId` - since `DefId`s are
636    // not stable across compilation sessions, the result could get up getting sorted
637    // in a different order when the query is re-run, even though all of the inputs
638    // (e.g. `DefPathHash` values) were green.
639    //
640    // See issue #82920 for an example of a miscompilation that would get turned into
641    // an ICE by this check
642    incremental_verify_ich(
643        *qcx.dep_context(),
644        dep_graph_data,
645        &result,
646        prev_dep_node_index,
647        query.hash_result(),
648        query.format_value(),
649    );
650
651    Some((result, dep_node_index))
652}
653
654#[inline]
655#[instrument(skip(tcx, dep_graph_data, result, hash_result, format_value), level = "debug")]
656pub(crate) fn incremental_verify_ich<Tcx, V>(
657    tcx: Tcx,
658    dep_graph_data: &DepGraphData<Tcx::Deps>,
659    result: &V,
660    prev_index: SerializedDepNodeIndex,
661    hash_result: Option<fn(&mut StableHashingContext<'_>, &V) -> Fingerprint>,
662    format_value: fn(&V) -> String,
663) where
664    Tcx: DepContext,
665{
666    if !dep_graph_data.is_index_green(prev_index) {
667        incremental_verify_ich_not_green(tcx, prev_index)
668    }
669
670    let new_hash = hash_result.map_or(Fingerprint::ZERO, |f| {
671        tcx.with_stable_hashing_context(|mut hcx| f(&mut hcx, result))
672    });
673
674    let old_hash = dep_graph_data.prev_fingerprint_of(prev_index);
675
676    if new_hash != old_hash {
677        incremental_verify_ich_failed(tcx, prev_index, &|| format_value(result));
678    }
679}
680
681#[cold]
682#[inline(never)]
683fn incremental_verify_ich_not_green<Tcx>(tcx: Tcx, prev_index: SerializedDepNodeIndex)
684where
685    Tcx: DepContext,
686{
687    panic!(
688        "fingerprint for green query instance not loaded from cache: {:?}",
689        tcx.dep_graph().data().unwrap().prev_node_of(prev_index)
690    )
691}
692
693// Note that this is marked #[cold] and intentionally takes `dyn Debug` for `result`,
694// as we want to avoid generating a bunch of different implementations for LLVM to
695// chew on (and filling up the final binary, too).
696#[cold]
697#[inline(never)]
698fn incremental_verify_ich_failed<Tcx>(
699    tcx: Tcx,
700    prev_index: SerializedDepNodeIndex,
701    result: &dyn Fn() -> String,
702) where
703    Tcx: DepContext,
704{
705    // When we emit an error message and panic, we try to debug-print the `DepNode`
706    // and query result. Unfortunately, this can cause us to run additional queries,
707    // which may result in another fingerprint mismatch while we're in the middle
708    // of processing this one. To avoid a double-panic (which kills the process
709    // before we can print out the query static), we print out a terse
710    // but 'safe' message if we detect a reentrant call to this method.
711    thread_local! {
712        static INSIDE_VERIFY_PANIC: Cell<bool> = const { Cell::new(false) };
713    };
714
715    let old_in_panic = INSIDE_VERIFY_PANIC.with(|in_panic| in_panic.replace(true));
716
717    if old_in_panic {
718        tcx.sess().dcx().emit_err(crate::error::Reentrant);
719    } else {
720        let run_cmd = if let Some(crate_name) = &tcx.sess().opts.crate_name {
721            format!("`cargo clean -p {crate_name}` or `cargo clean`")
722        } else {
723            "`cargo clean`".to_string()
724        };
725
726        let dep_node = tcx.dep_graph().data().unwrap().prev_node_of(prev_index);
727        tcx.sess().dcx().emit_err(crate::error::IncrementCompilation {
728            run_cmd,
729            dep_node: format!("{dep_node:?}"),
730        });
731        panic!("Found unstable fingerprints for {dep_node:?}: {}", result());
732    }
733
734    INSIDE_VERIFY_PANIC.with(|in_panic| in_panic.set(old_in_panic));
735}
736
737/// Ensure that either this query has all green inputs or been executed.
738/// Executing `query::ensure(D)` is considered a read of the dep-node `D`.
739/// Returns true if the query should still run.
740///
741/// This function is particularly useful when executing passes for their
742/// side-effects -- e.g., in order to report errors for erroneous programs.
743///
744/// Note: The optimization is only available during incr. comp.
745#[inline(never)]
746fn ensure_must_run<Q, Qcx>(
747    query: Q,
748    qcx: Qcx,
749    key: &Q::Key,
750    check_cache: bool,
751) -> (bool, Option<DepNode>)
752where
753    Q: QueryConfig<Qcx>,
754    Qcx: QueryContext,
755{
756    if query.eval_always() {
757        return (true, None);
758    }
759
760    // Ensuring an anonymous query makes no sense
761    assert!(!query.anon());
762
763    let dep_node = query.construct_dep_node(*qcx.dep_context(), key);
764
765    let dep_graph = qcx.dep_context().dep_graph();
766    let serialized_dep_node_index = match dep_graph.try_mark_green(qcx, &dep_node) {
767        None => {
768            // A None return from `try_mark_green` means that this is either
769            // a new dep node or that the dep node has already been marked red.
770            // Either way, we can't call `dep_graph.read()` as we don't have the
771            // DepNodeIndex. We must invoke the query itself. The performance cost
772            // this introduces should be negligible as we'll immediately hit the
773            // in-memory cache, or another query down the line will.
774            return (true, Some(dep_node));
775        }
776        Some((serialized_dep_node_index, dep_node_index)) => {
777            dep_graph.read_index(dep_node_index);
778            qcx.dep_context().profiler().query_cache_hit(dep_node_index.into());
779            serialized_dep_node_index
780        }
781    };
782
783    // We do not need the value at all, so do not check the cache.
784    if !check_cache {
785        return (false, None);
786    }
787
788    let loadable = query.loadable_from_disk(qcx, key, serialized_dep_node_index);
789    (!loadable, Some(dep_node))
790}
791
792#[derive(Debug)]
793pub enum QueryMode {
794    Get,
795    Ensure { check_cache: bool },
796}
797
798#[inline(always)]
799pub fn get_query_non_incr<Q, Qcx>(query: Q, qcx: Qcx, span: Span, key: Q::Key) -> Q::Value
800where
801    Q: QueryConfig<Qcx>,
802    Qcx: QueryContext,
803{
804    debug_assert!(!qcx.dep_context().dep_graph().is_fully_enabled());
805
806    ensure_sufficient_stack(|| try_execute_query::<Q, Qcx, false>(query, qcx, span, key, None).0)
807}
808
809#[inline(always)]
810pub fn get_query_incr<Q, Qcx>(
811    query: Q,
812    qcx: Qcx,
813    span: Span,
814    key: Q::Key,
815    mode: QueryMode,
816) -> Option<Q::Value>
817where
818    Q: QueryConfig<Qcx>,
819    Qcx: QueryContext,
820{
821    debug_assert!(qcx.dep_context().dep_graph().is_fully_enabled());
822
823    let dep_node = if let QueryMode::Ensure { check_cache } = mode {
824        let (must_run, dep_node) = ensure_must_run(query, qcx, &key, check_cache);
825        if !must_run {
826            return None;
827        }
828        dep_node
829    } else {
830        None
831    };
832
833    let (result, dep_node_index) = ensure_sufficient_stack(|| {
834        try_execute_query::<_, _, true>(query, qcx, span, key, dep_node)
835    });
836    if let Some(dep_node_index) = dep_node_index {
837        qcx.dep_context().dep_graph().read_index(dep_node_index)
838    }
839    Some(result)
840}
841
842pub fn force_query<Q, Qcx>(query: Q, qcx: Qcx, key: Q::Key, dep_node: DepNode)
843where
844    Q: QueryConfig<Qcx>,
845    Qcx: QueryContext,
846{
847    // We may be concurrently trying both execute and force a query.
848    // Ensure that only one of them runs the query.
849    if let Some((_, index)) = query.query_cache(qcx).lookup(&key) {
850        qcx.dep_context().profiler().query_cache_hit(index.into());
851        return;
852    }
853
854    debug_assert!(!query.anon());
855
856    ensure_sufficient_stack(|| {
857        try_execute_query::<_, _, true>(query, qcx, DUMMY_SP, key, Some(dep_node))
858    });
859}