1use 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
36enum QueryResult {
38 Started(QueryJob),
40
41 Poisoned,
44}
45
46impl QueryResult {
47 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 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 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
101struct 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 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 mem::forget(self);
175
176 cache.complete(key, result, dep_node_index);
179
180 let job = {
181 let val = {
182 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 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 job.signal_complete();
215 }
216}
217
218#[derive(Clone, Debug)]
219pub struct CycleError {
220 pub usage: Option<(Span, QueryStackFrame)>,
222 pub cycle: Vec<QueryInfo>,
223}
224
225#[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 let query_blocked_prof_timer = qcx.dep_context().profiler().query_blocked();
279
280 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 let lock = query.query_state(qcx).active.get_shard_by_value(&key).lock();
291
292 match lock.get(&key) {
293 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 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 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(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 let latch = job.latch();
361 drop(state_lock);
362
363 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 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 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 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 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#[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 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 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 let dep_node =
506 dep_node_opt.get_or_insert_with(|| query.construct_dep_node(*qcx.dep_context(), &key));
507
508 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 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 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 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 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 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 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 let prof_timer = qcx.dep_context().profiler().query_provider();
627
628 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 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#[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 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#[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 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 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 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 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}