Skip to main content

rustc_query_impl/
execution.rs

1use std::hash::Hash;
2use std::mem;
3
4use rustc_data_structures::hash_table::{Entry, HashTable};
5use rustc_data_structures::stack::ensure_sufficient_stack;
6use rustc_data_structures::{outline, sharded, sync};
7use rustc_errors::{Diag, FatalError, StashKey};
8use rustc_middle::dep_graph::{DepGraphData, DepNodeKey};
9use rustc_middle::query::{
10    ActiveKeyStatus, CycleError, CycleErrorHandling, QueryCache, QueryJob, QueryJobId, QueryLatch,
11    QueryMode, QueryStackDeferred, QueryStackFrame, QueryState,
12};
13use rustc_middle::ty::TyCtxt;
14use rustc_middle::verify_ich::incremental_verify_ich;
15use rustc_span::{DUMMY_SP, Span};
16
17use crate::dep_graph::{DepNode, DepNodeIndex};
18use crate::job::{QueryJobInfo, QueryJobMap, find_cycle_in_stack, report_cycle};
19use crate::plumbing::{
20    collect_active_jobs_from_all_queries, current_query_job, next_job_id, start_query,
21};
22use crate::{QueryFlags, SemiDynamicQueryDispatcher};
23
24#[inline]
25fn equivalent_key<K: Eq, V>(k: &K) -> impl Fn(&(K, V)) -> bool + '_ {
26    move |x| x.0 == *k
27}
28
29/// Obtains the enclosed [`QueryJob`], or panics if this query evaluation
30/// was poisoned by a panic.
31fn expect_job<'tcx>(status: ActiveKeyStatus<'tcx>) -> QueryJob<'tcx> {
32    match status {
33        ActiveKeyStatus::Started(job) => job,
34        ActiveKeyStatus::Poisoned => {
35            {
    ::core::panicking::panic_fmt(format_args!("job for query failed to start and was poisoned"));
}panic!("job for query failed to start and was poisoned")
36        }
37    }
38}
39
40pub(crate) fn all_inactive<'tcx, K>(state: &QueryState<'tcx, K>) -> bool {
41    state.active.lock_shards().all(|shard| shard.is_empty())
42}
43
44/// Internal plumbing for collecting the set of active jobs for this query.
45///
46/// Should only be called from `gather_active_jobs`.
47pub(crate) fn gather_active_jobs_inner<'tcx, K: Copy>(
48    state: &QueryState<'tcx, K>,
49    tcx: TyCtxt<'tcx>,
50    make_frame: fn(TyCtxt<'tcx>, K) -> QueryStackFrame<QueryStackDeferred<'tcx>>,
51    require_complete: bool,
52    job_map_out: &mut QueryJobMap<'tcx>, // Out-param; job info is gathered into this map
53) -> Option<()> {
54    let mut active = Vec::new();
55
56    // Helper to gather active jobs from a single shard.
57    let mut gather_shard_jobs = |shard: &HashTable<(K, ActiveKeyStatus<'tcx>)>| {
58        for (k, v) in shard.iter() {
59            if let ActiveKeyStatus::Started(ref job) = *v {
60                active.push((*k, job.clone()));
61            }
62        }
63    };
64
65    // Lock shards and gather jobs from each shard.
66    if require_complete {
67        for shard in state.active.lock_shards() {
68            gather_shard_jobs(&shard);
69        }
70    } else {
71        // We use try_lock_shards here since we are called from the
72        // deadlock handler, and this shouldn't be locked.
73        for shard in state.active.try_lock_shards() {
74            let shard = shard?;
75            gather_shard_jobs(&shard);
76        }
77    }
78
79    // Call `make_frame` while we're not holding a `state.active` lock as `make_frame` may call
80    // queries leading to a deadlock.
81    for (key, job) in active {
82        let frame = make_frame(tcx, key);
83        job_map_out.insert(job.id, QueryJobInfo { frame, job });
84    }
85
86    Some(())
87}
88
89/// Guard object representing the responsibility to execute a query job and
90/// mark it as completed.
91///
92/// This will poison the relevant query key if it is dropped without calling
93/// [`Self::complete`].
94struct ActiveJobGuard<'tcx, K>
95where
96    K: Eq + Hash + Copy,
97{
98    state: &'tcx QueryState<'tcx, K>,
99    key: K,
100    key_hash: u64,
101}
102
103#[cold]
104#[inline(never)]
105fn mk_cycle<'tcx, C: QueryCache, const FLAGS: QueryFlags>(
106    query: SemiDynamicQueryDispatcher<'tcx, C, FLAGS>,
107    tcx: TyCtxt<'tcx>,
108    cycle_error: CycleError,
109) -> C::Value {
110    let error = report_cycle(tcx.sess, &cycle_error);
111    handle_cycle_error(query, tcx, &cycle_error, error)
112}
113
114fn handle_cycle_error<'tcx, C: QueryCache, const FLAGS: QueryFlags>(
115    query: SemiDynamicQueryDispatcher<'tcx, C, FLAGS>,
116    tcx: TyCtxt<'tcx>,
117    cycle_error: &CycleError,
118    error: Diag<'_>,
119) -> C::Value {
120    match query.cycle_error_handling() {
121        CycleErrorHandling::Error => {
122            let guar = error.emit();
123            query.value_from_cycle_error(tcx, cycle_error, guar)
124        }
125        CycleErrorHandling::Fatal => {
126            error.emit();
127            tcx.dcx().abort_if_errors();
128            ::core::panicking::panic("internal error: entered unreachable code")unreachable!()
129        }
130        CycleErrorHandling::DelayBug => {
131            let guar = error.delay_as_bug();
132            query.value_from_cycle_error(tcx, cycle_error, guar)
133        }
134        CycleErrorHandling::Stash => {
135            let guar = if let Some(root) = cycle_error.cycle.first()
136                && let Some(span) = root.frame.info.span
137            {
138                error.stash(span, StashKey::Cycle).unwrap()
139            } else {
140                error.emit()
141            };
142            query.value_from_cycle_error(tcx, cycle_error, guar)
143        }
144    }
145}
146
147impl<'tcx, K> ActiveJobGuard<'tcx, K>
148where
149    K: Eq + Hash + Copy,
150{
151    /// Completes the query by updating the query cache with the `result`,
152    /// signals the waiter, and forgets the guard so it won't poison the query.
153    fn complete<C>(self, cache: &C, result: C::Value, dep_node_index: DepNodeIndex)
154    where
155        C: QueryCache<Key = K>,
156    {
157        // Forget ourself so our destructor won't poison the query.
158        // (Extract fields by value first to make sure we don't leak anything.)
159        let Self { state, key, key_hash }: Self = self;
160        mem::forget(self);
161
162        // Mark as complete before we remove the job from the active state
163        // so no other thread can re-execute this query.
164        cache.complete(key, result, dep_node_index);
165
166        let job = {
167            // don't keep the lock during the `unwrap()` of the retrieved value, or we taint the
168            // underlying shard.
169            // since unwinding also wants to look at this map, this can also prevent a double
170            // panic.
171            let mut shard = state.active.lock_shard_by_hash(key_hash);
172            match shard.find_entry(key_hash, equivalent_key(&key)) {
173                Err(_) => None,
174                Ok(occupied) => Some(occupied.remove().0.1),
175            }
176        };
177        let job = expect_job(job.expect("active query job entry"));
178
179        job.signal_complete();
180    }
181}
182
183impl<'tcx, K> Drop for ActiveJobGuard<'tcx, K>
184where
185    K: Eq + Hash + Copy,
186{
187    #[inline(never)]
188    #[cold]
189    fn drop(&mut self) {
190        // Poison the query so jobs waiting on it panic.
191        let Self { state, key, key_hash } = *self;
192        let job = {
193            let mut shard = state.active.lock_shard_by_hash(key_hash);
194            match shard.find_entry(key_hash, equivalent_key(&key)) {
195                Err(_) => ::core::panicking::panic("explicit panic")panic!(),
196                Ok(occupied) => {
197                    let ((key, value), vacant) = occupied.remove();
198                    vacant.insert((key, ActiveKeyStatus::Poisoned));
199                    expect_job(value)
200                }
201            }
202        };
203        // Also signal the completion of the job, so waiters
204        // will continue execution.
205        job.signal_complete();
206    }
207}
208
209#[cold]
210#[inline(never)]
211fn cycle_error<'tcx, C: QueryCache, const FLAGS: QueryFlags>(
212    query: SemiDynamicQueryDispatcher<'tcx, C, FLAGS>,
213    tcx: TyCtxt<'tcx>,
214    try_execute: QueryJobId,
215    span: Span,
216) -> (C::Value, Option<DepNodeIndex>) {
217    // Ensure there was no errors collecting all active jobs.
218    // We need the complete map to ensure we find a cycle to break.
219    let job_map = collect_active_jobs_from_all_queries(tcx, false)
220        .ok()
221        .expect("failed to collect active queries");
222
223    let error = find_cycle_in_stack(try_execute, job_map, &current_query_job(tcx), span);
224    (mk_cycle(query, tcx, error.lift()), None)
225}
226
227#[inline(always)]
228fn wait_for_query<'tcx, C: QueryCache, const FLAGS: QueryFlags>(
229    query: SemiDynamicQueryDispatcher<'tcx, C, FLAGS>,
230    tcx: TyCtxt<'tcx>,
231    span: Span,
232    key: C::Key,
233    latch: QueryLatch<'tcx>,
234    current: Option<QueryJobId>,
235) -> (C::Value, Option<DepNodeIndex>) {
236    // For parallel queries, we'll block and wait until the query running
237    // in another thread has completed. Record how long we wait in the
238    // self-profiler.
239    let query_blocked_prof_timer = tcx.prof.query_blocked();
240
241    // With parallel queries we might just have to wait on some other
242    // thread.
243    let result = latch.wait_on(tcx, current, span);
244
245    match result {
246        Ok(()) => {
247            let Some((v, index)) = query.query_cache(tcx).lookup(&key) else {
248                outline(|| {
249                    // We didn't find the query result in the query cache. Check if it was
250                    // poisoned due to a panic instead.
251                    let key_hash = sharded::make_hash(&key);
252                    let shard = query.query_state(tcx).active.lock_shard_by_hash(key_hash);
253                    match shard.find(key_hash, equivalent_key(&key)) {
254                        // The query we waited on panicked. Continue unwinding here.
255                        Some((_, ActiveKeyStatus::Poisoned)) => FatalError.raise(),
256                        _ => {
    ::core::panicking::panic_fmt(format_args!("query \'{0}\' result must be in the cache or the query must be poisoned after a wait",
            query.name()));
}panic!(
257                            "query '{}' result must be in the cache or the query must be poisoned after a wait",
258                            query.name()
259                        ),
260                    }
261                })
262            };
263
264            tcx.prof.query_cache_hit(index.into());
265            query_blocked_prof_timer.finish_with_query_invocation_id(index.into());
266
267            (v, Some(index))
268        }
269        Err(cycle) => (mk_cycle(query, tcx, cycle.lift()), None),
270    }
271}
272
273#[inline(never)]
274fn try_execute_query<'tcx, C: QueryCache, const FLAGS: QueryFlags, const INCR: bool>(
275    query: SemiDynamicQueryDispatcher<'tcx, C, FLAGS>,
276    tcx: TyCtxt<'tcx>,
277    span: Span,
278    key: C::Key,
279    dep_node: Option<DepNode>,
280) -> (C::Value, Option<DepNodeIndex>) {
281    let state = query.query_state(tcx);
282    let key_hash = sharded::make_hash(&key);
283    let mut state_lock = state.active.lock_shard_by_hash(key_hash);
284
285    // For the parallel compiler we need to check both the query cache and query state structures
286    // while holding the state lock to ensure that 1) the query has not yet completed and 2) the
287    // query is not still executing. Without checking the query cache here, we can end up
288    // re-executing the query since `try_start` only checks that the query is not currently
289    // executing, but another thread may have already completed the query and stores it result
290    // in the query cache.
291    if tcx.sess.threads() > 1 {
292        if let Some((value, index)) = query.query_cache(tcx).lookup(&key) {
293            tcx.prof.query_cache_hit(index.into());
294            return (value, Some(index));
295        }
296    }
297
298    let current_job_id = current_query_job(tcx);
299
300    match state_lock.entry(key_hash, equivalent_key(&key), |(k, _)| sharded::make_hash(k)) {
301        Entry::Vacant(entry) => {
302            // Nothing has computed or is computing the query, so we start a new job and insert it in the
303            // state map.
304            let id = next_job_id(tcx);
305            let job = QueryJob::new(id, span, current_job_id);
306            entry.insert((key, ActiveKeyStatus::Started(job)));
307
308            // Drop the lock before we start executing the query
309            drop(state_lock);
310
311            execute_job::<C, FLAGS, INCR>(query, tcx, state, key, key_hash, id, dep_node)
312        }
313        Entry::Occupied(mut entry) => {
314            match &mut entry.get_mut().1 {
315                ActiveKeyStatus::Started(job) => {
316                    if sync::is_dyn_thread_safe() {
317                        // Get the latch out
318                        let latch = job.latch();
319                        drop(state_lock);
320
321                        // Only call `wait_for_query` if we're using a Rayon thread pool
322                        // as it will attempt to mark the worker thread as blocked.
323                        return wait_for_query(query, tcx, span, key, latch, current_job_id);
324                    }
325
326                    let id = job.id;
327                    drop(state_lock);
328
329                    // If we are single-threaded we know that we have cycle error,
330                    // so we just return the error.
331                    cycle_error(query, tcx, id, span)
332                }
333                ActiveKeyStatus::Poisoned => FatalError.raise(),
334            }
335        }
336    }
337}
338
339#[inline(always)]
340fn execute_job<'tcx, C: QueryCache, const FLAGS: QueryFlags, const INCR: bool>(
341    query: SemiDynamicQueryDispatcher<'tcx, C, FLAGS>,
342    tcx: TyCtxt<'tcx>,
343    state: &'tcx QueryState<'tcx, C::Key>,
344    key: C::Key,
345    key_hash: u64,
346    id: QueryJobId,
347    dep_node: Option<DepNode>,
348) -> (C::Value, Option<DepNodeIndex>) {
349    // Set up a guard object that will automatically poison the query if a
350    // panic occurs while executing the query (or any intermediate plumbing).
351    let job_guard = ActiveJobGuard { state, key, key_hash };
352
353    if true {
    match (&tcx.dep_graph.is_fully_enabled(), &INCR) {
        (left_val, right_val) => {
            if !(*left_val == *right_val) {
                let kind = ::core::panicking::AssertKind::Eq;
                ::core::panicking::assert_failed(kind, &*left_val,
                    &*right_val, ::core::option::Option::None);
            }
        }
    };
};debug_assert_eq!(tcx.dep_graph.is_fully_enabled(), INCR);
354
355    // Delegate to another function to actually execute the query job.
356    let (result, dep_node_index) = if INCR {
357        execute_job_incr(query, tcx, key, dep_node, id)
358    } else {
359        execute_job_non_incr(query, tcx, key, id)
360    };
361
362    let cache = query.query_cache(tcx);
363    if query.feedable() {
364        // We should not compute queries that also got a value via feeding.
365        // This can't happen, as query feeding adds the very dependencies to the fed query
366        // as its feeding query had. So if the fed query is red, so is its feeder, which will
367        // get evaluated first, and re-feed the query.
368        if let Some((cached_result, _)) = cache.lookup(&key) {
369            let Some(hasher) = query.hash_result() else {
370                {
    ::core::panicking::panic_fmt(format_args!("no_hash fed query later has its value computed.\nRemove `no_hash` modifier to allow recomputation.\nThe already cached value: {0}",
            (query.format_value())(&cached_result)));
};panic!(
371                    "no_hash fed query later has its value computed.\n\
372                    Remove `no_hash` modifier to allow recomputation.\n\
373                    The already cached value: {}",
374                    (query.format_value())(&cached_result)
375                );
376            };
377
378            let (old_hash, new_hash) = tcx.with_stable_hashing_context(|mut hcx| {
379                (hasher(&mut hcx, &cached_result), hasher(&mut hcx, &result))
380            });
381            let formatter = query.format_value();
382            if old_hash != new_hash {
383                // We have an inconsistency. This can happen if one of the two
384                // results is tainted by errors.
385                if !tcx.dcx().has_errors().is_some() {
    {
        ::core::panicking::panic_fmt(format_args!("Computed query value for {0:?}({1:?}) is inconsistent with fed value,\ncomputed={2:#?}\nfed={3:#?}",
                query.dep_kind(), key, formatter(&result),
                formatter(&cached_result)));
    }
};assert!(
386                    tcx.dcx().has_errors().is_some(),
387                    "Computed query value for {:?}({:?}) is inconsistent with fed value,\n\
388                        computed={:#?}\nfed={:#?}",
389                    query.dep_kind(),
390                    key,
391                    formatter(&result),
392                    formatter(&cached_result),
393                );
394            }
395        }
396    }
397
398    // Tell the guard to perform completion bookkeeping, and also to not poison the query.
399    job_guard.complete(cache, result, dep_node_index);
400
401    (result, Some(dep_node_index))
402}
403
404// Fast path for when incr. comp. is off.
405#[inline(always)]
406fn execute_job_non_incr<'tcx, C: QueryCache, const FLAGS: QueryFlags>(
407    query: SemiDynamicQueryDispatcher<'tcx, C, FLAGS>,
408    tcx: TyCtxt<'tcx>,
409    key: C::Key,
410    job_id: QueryJobId,
411) -> (C::Value, DepNodeIndex) {
412    if true {
    if !!tcx.dep_graph.is_fully_enabled() {
        ::core::panicking::panic("assertion failed: !tcx.dep_graph.is_fully_enabled()")
    };
};debug_assert!(!tcx.dep_graph.is_fully_enabled());
413
414    // Fingerprint the key, just to assert that it doesn't
415    // have anything we don't consider hashable
416    if truecfg!(debug_assertions) {
417        let _ = key.to_fingerprint(tcx);
418    }
419
420    let prof_timer = tcx.prof.query_provider();
421    // Call the query provider.
422    let result = start_query(tcx, job_id, query.depth_limit(), || query.invoke_provider(tcx, key));
423    let dep_node_index = tcx.dep_graph.next_virtual_depnode_index();
424    prof_timer.finish_with_query_invocation_id(dep_node_index.into());
425
426    // Similarly, fingerprint the result to assert that
427    // it doesn't have anything not considered hashable.
428    if truecfg!(debug_assertions)
429        && let Some(hash_result) = query.hash_result()
430    {
431        tcx.with_stable_hashing_context(|mut hcx| {
432            hash_result(&mut hcx, &result);
433        });
434    }
435
436    (result, dep_node_index)
437}
438
439#[inline(always)]
440fn execute_job_incr<'tcx, C: QueryCache, const FLAGS: QueryFlags>(
441    query: SemiDynamicQueryDispatcher<'tcx, C, FLAGS>,
442    tcx: TyCtxt<'tcx>,
443    key: C::Key,
444    mut dep_node_opt: Option<DepNode>,
445    job_id: QueryJobId,
446) -> (C::Value, DepNodeIndex) {
447    let dep_graph_data =
448        tcx.dep_graph.data().expect("should always be present in incremental mode");
449
450    if !query.anon() && !query.eval_always() {
451        // `to_dep_node` is expensive for some `DepKind`s.
452        let dep_node = dep_node_opt.get_or_insert_with(|| query.construct_dep_node(tcx, &key));
453
454        // The diagnostics for this query will be promoted to the current session during
455        // `try_mark_green()`, so we can ignore them here.
456        if let Some(ret) = start_query(tcx, job_id, false, || {
457            try_load_from_disk_and_cache_in_memory(query, dep_graph_data, tcx, &key, dep_node)
458        }) {
459            return ret;
460        }
461    }
462
463    let prof_timer = tcx.prof.query_provider();
464
465    let (result, dep_node_index) = start_query(tcx, job_id, query.depth_limit(), || {
466        if query.anon() {
467            // Call the query provider inside an anon task.
468            return dep_graph_data
469                .with_anon_task_inner(tcx, query.dep_kind(), || query.invoke_provider(tcx, key));
470        }
471
472        // `to_dep_node` is expensive for some `DepKind`s.
473        let dep_node = dep_node_opt.unwrap_or_else(|| query.construct_dep_node(tcx, &key));
474
475        // Call the query provider.
476        dep_graph_data.with_task(
477            dep_node,
478            tcx,
479            (query, key),
480            |tcx, (query, key)| query.invoke_provider(tcx, key),
481            query.hash_result(),
482        )
483    });
484
485    prof_timer.finish_with_query_invocation_id(dep_node_index.into());
486
487    (result, dep_node_index)
488}
489
490#[inline(always)]
491fn try_load_from_disk_and_cache_in_memory<'tcx, C: QueryCache, const FLAGS: QueryFlags>(
492    query: SemiDynamicQueryDispatcher<'tcx, C, FLAGS>,
493    dep_graph_data: &DepGraphData,
494    tcx: TyCtxt<'tcx>,
495    key: &C::Key,
496    dep_node: &DepNode,
497) -> Option<(C::Value, DepNodeIndex)> {
498    // Note this function can be called concurrently from the same query
499    // We must ensure that this is handled correctly.
500
501    let (prev_dep_node_index, dep_node_index) = dep_graph_data.try_mark_green(tcx, dep_node)?;
502
503    if true {
    if !dep_graph_data.is_index_green(prev_dep_node_index) {
        ::core::panicking::panic("assertion failed: dep_graph_data.is_index_green(prev_dep_node_index)")
    };
};debug_assert!(dep_graph_data.is_index_green(prev_dep_node_index));
504
505    // First we try to load the result from the on-disk cache.
506    // Some things are never cached on disk.
507    if let Some(result) = query.try_load_from_disk(tcx, key, prev_dep_node_index, dep_node_index) {
508        if std::intrinsics::unlikely(tcx.sess.opts.unstable_opts.query_dep_graph) {
509            dep_graph_data.mark_debug_loaded_from_disk(*dep_node)
510        }
511
512        let prev_fingerprint = dep_graph_data.prev_value_fingerprint_of(prev_dep_node_index);
513        // If `-Zincremental-verify-ich` is specified, re-hash results from
514        // the cache and make sure that they have the expected fingerprint.
515        //
516        // If not, we still seek to verify a subset of fingerprints loaded
517        // from disk. Re-hashing results is fairly expensive, so we can't
518        // currently afford to verify every hash. This subset should still
519        // give us some coverage of potential bugs though.
520        let try_verify = prev_fingerprint.split().1.as_u64().is_multiple_of(32);
521        if std::intrinsics::unlikely(
522            try_verify || tcx.sess.opts.unstable_opts.incremental_verify_ich,
523        ) {
524            incremental_verify_ich(
525                tcx,
526                dep_graph_data,
527                &result,
528                prev_dep_node_index,
529                query.hash_result(),
530                query.format_value(),
531            );
532        }
533
534        return Some((result, dep_node_index));
535    }
536
537    // We always expect to find a cached result for things that
538    // can be forced from `DepNode`.
539    if true {
    if !(!query.will_cache_on_disk_for_key(tcx, key) ||
                !tcx.key_fingerprint_style(dep_node.kind).reconstructible()) {
        {
            ::core::panicking::panic_fmt(format_args!("missing on-disk cache entry for {0:?}",
                    dep_node));
        }
    };
};debug_assert!(
540        !query.will_cache_on_disk_for_key(tcx, key)
541            || !tcx.key_fingerprint_style(dep_node.kind).reconstructible(),
542        "missing on-disk cache entry for {dep_node:?}"
543    );
544
545    // Sanity check for the logic in `ensure`: if the node is green and the result loadable,
546    // we should actually be able to load it.
547    if true {
    if !!query.is_loadable_from_disk(tcx, key, prev_dep_node_index) {
        {
            ::core::panicking::panic_fmt(format_args!("missing on-disk cache entry for loadable {0:?}",
                    dep_node));
        }
    };
};debug_assert!(
548        !query.is_loadable_from_disk(tcx, key, prev_dep_node_index),
549        "missing on-disk cache entry for loadable {dep_node:?}"
550    );
551
552    // We could not load a result from the on-disk cache, so
553    // recompute.
554    let prof_timer = tcx.prof.query_provider();
555
556    // The dep-graph for this computation is already in-place.
557    // Call the query provider.
558    let result = tcx.dep_graph.with_ignore(|| query.invoke_provider(tcx, *key));
559
560    prof_timer.finish_with_query_invocation_id(dep_node_index.into());
561
562    // Verify that re-running the query produced a result with the expected hash
563    // This catches bugs in query implementations, turning them into ICEs.
564    // For example, a query might sort its result by `DefId` - since `DefId`s are
565    // not stable across compilation sessions, the result could get up getting sorted
566    // in a different order when the query is re-run, even though all of the inputs
567    // (e.g. `DefPathHash` values) were green.
568    //
569    // See issue #82920 for an example of a miscompilation that would get turned into
570    // an ICE by this check
571    incremental_verify_ich(
572        tcx,
573        dep_graph_data,
574        &result,
575        prev_dep_node_index,
576        query.hash_result(),
577        query.format_value(),
578    );
579
580    Some((result, dep_node_index))
581}
582
583/// Ensure that either this query has all green inputs or been executed.
584/// Executing `query::ensure(D)` is considered a read of the dep-node `D`.
585/// Returns true if the query should still run.
586///
587/// This function is particularly useful when executing passes for their
588/// side-effects -- e.g., in order to report errors for erroneous programs.
589///
590/// Note: The optimization is only available during incr. comp.
591#[inline(never)]
592fn ensure_must_run<'tcx, C: QueryCache, const FLAGS: QueryFlags>(
593    query: SemiDynamicQueryDispatcher<'tcx, C, FLAGS>,
594    tcx: TyCtxt<'tcx>,
595    key: &C::Key,
596    check_cache: bool,
597) -> (bool, Option<DepNode>) {
598    if query.eval_always() {
599        return (true, None);
600    }
601
602    // Ensuring an anonymous query makes no sense
603    if !!query.anon() {
    ::core::panicking::panic("assertion failed: !query.anon()")
};assert!(!query.anon());
604
605    let dep_node = query.construct_dep_node(tcx, key);
606
607    let dep_graph = &tcx.dep_graph;
608    let serialized_dep_node_index = match dep_graph.try_mark_green(tcx, &dep_node) {
609        None => {
610            // A None return from `try_mark_green` means that this is either
611            // a new dep node or that the dep node has already been marked red.
612            // Either way, we can't call `dep_graph.read()` as we don't have the
613            // DepNodeIndex. We must invoke the query itself. The performance cost
614            // this introduces should be negligible as we'll immediately hit the
615            // in-memory cache, or another query down the line will.
616            return (true, Some(dep_node));
617        }
618        Some((serialized_dep_node_index, dep_node_index)) => {
619            dep_graph.read_index(dep_node_index);
620            tcx.prof.query_cache_hit(dep_node_index.into());
621            serialized_dep_node_index
622        }
623    };
624
625    // We do not need the value at all, so do not check the cache.
626    if !check_cache {
627        return (false, None);
628    }
629
630    let loadable = query.is_loadable_from_disk(tcx, key, serialized_dep_node_index);
631    (!loadable, Some(dep_node))
632}
633
634#[inline(always)]
635pub(super) fn get_query_non_incr<'tcx, C: QueryCache, const FLAGS: QueryFlags>(
636    query: SemiDynamicQueryDispatcher<'tcx, C, FLAGS>,
637    tcx: TyCtxt<'tcx>,
638    span: Span,
639    key: C::Key,
640) -> C::Value {
641    if true {
    if !!tcx.dep_graph.is_fully_enabled() {
        ::core::panicking::panic("assertion failed: !tcx.dep_graph.is_fully_enabled()")
    };
};debug_assert!(!tcx.dep_graph.is_fully_enabled());
642
643    ensure_sufficient_stack(|| try_execute_query::<C, FLAGS, false>(query, tcx, span, key, None).0)
644}
645
646#[inline(always)]
647pub(super) fn get_query_incr<'tcx, C: QueryCache, const FLAGS: QueryFlags>(
648    query: SemiDynamicQueryDispatcher<'tcx, C, FLAGS>,
649    tcx: TyCtxt<'tcx>,
650    span: Span,
651    key: C::Key,
652    mode: QueryMode,
653) -> Option<C::Value> {
654    if true {
    if !tcx.dep_graph.is_fully_enabled() {
        ::core::panicking::panic("assertion failed: tcx.dep_graph.is_fully_enabled()")
    };
};debug_assert!(tcx.dep_graph.is_fully_enabled());
655
656    let dep_node = if let QueryMode::Ensure { check_cache } = mode {
657        let (must_run, dep_node) = ensure_must_run(query, tcx, &key, check_cache);
658        if !must_run {
659            return None;
660        }
661        dep_node
662    } else {
663        None
664    };
665
666    let (result, dep_node_index) = ensure_sufficient_stack(|| {
667        try_execute_query::<C, FLAGS, true>(query, tcx, span, key, dep_node)
668    });
669    if let Some(dep_node_index) = dep_node_index {
670        tcx.dep_graph.read_index(dep_node_index)
671    }
672    Some(result)
673}
674
675pub(crate) fn force_query<'tcx, C: QueryCache, const FLAGS: QueryFlags>(
676    query: SemiDynamicQueryDispatcher<'tcx, C, FLAGS>,
677    tcx: TyCtxt<'tcx>,
678    key: C::Key,
679    dep_node: DepNode,
680) {
681    // We may be concurrently trying both execute and force a query.
682    // Ensure that only one of them runs the query.
683    if let Some((_, index)) = query.query_cache(tcx).lookup(&key) {
684        tcx.prof.query_cache_hit(index.into());
685        return;
686    }
687
688    if true {
    if !!query.anon() {
        ::core::panicking::panic("assertion failed: !query.anon()")
    };
};debug_assert!(!query.anon());
689
690    ensure_sufficient_stack(|| {
691        try_execute_query::<C, FLAGS, true>(query, tcx, DUMMY_SP, key, Some(dep_node))
692    });
693}