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