2019-02-08 13:53:55 +00:00
|
|
|
//! The implementation of the query system itself. This defines the macros that
|
|
|
|
//! generate the actual methods on tcx which find and execute the provider,
|
|
|
|
//! manage the caches, and so forth.
|
2017-09-18 09:40:13 +00:00
|
|
|
|
2022-12-30 22:25:19 +00:00
|
|
|
use crate::dep_graph::{DepContext, DepKind, DepNode, DepNodeIndex};
|
2022-09-04 20:42:05 +00:00
|
|
|
use crate::ich::StableHashingContext;
|
2020-03-19 13:13:31 +00:00
|
|
|
use crate::query::caches::QueryCache;
|
2022-02-07 16:03:51 +00:00
|
|
|
use crate::query::job::{report_cycle, QueryInfo, QueryJob, QueryJobId, QueryJobInfo};
|
2021-07-23 21:40:26 +00:00
|
|
|
use crate::query::{QueryContext, QueryMap, QuerySideEffects, QueryStackFrame};
|
2022-09-02 01:43:12 +00:00
|
|
|
use crate::values::Value;
|
|
|
|
use crate::HandleCycleError;
|
2020-03-19 13:13:31 +00:00
|
|
|
use rustc_data_structures::fingerprint::Fingerprint;
|
2022-02-20 20:06:47 +00:00
|
|
|
use rustc_data_structures::fx::FxHashMap;
|
2021-05-11 18:12:52 +00:00
|
|
|
#[cfg(parallel_compiler)]
|
|
|
|
use rustc_data_structures::profiling::TimingGuard;
|
2022-02-20 20:06:47 +00:00
|
|
|
#[cfg(parallel_compiler)]
|
|
|
|
use rustc_data_structures::sharded::Sharded;
|
2022-02-20 03:44:19 +00:00
|
|
|
use rustc_data_structures::sync::Lock;
|
2022-01-23 18:34:26 +00:00
|
|
|
use rustc_errors::{DiagnosticBuilder, ErrorGuaranteed, FatalError};
|
2021-10-12 02:33:16 +00:00
|
|
|
use rustc_session::Session;
|
2020-11-02 19:05:10 +00:00
|
|
|
use rustc_span::{Span, DUMMY_SP};
|
2022-10-29 13:04:38 +00:00
|
|
|
use std::borrow::Borrow;
|
2021-08-12 20:11:21 +00:00
|
|
|
use std::cell::Cell;
|
2018-04-18 02:35:40 +00:00
|
|
|
use std::collections::hash_map::Entry;
|
2021-01-03 14:19:16 +00:00
|
|
|
use std::fmt::Debug;
|
2022-02-20 20:06:47 +00:00
|
|
|
use std::hash::Hash;
|
2018-04-18 02:35:40 +00:00
|
|
|
use std::mem;
|
|
|
|
use std::ptr;
|
2022-08-17 04:22:30 +00:00
|
|
|
use thin_vec::ThinVec;
|
2017-09-18 09:40:13 +00:00
|
|
|
|
2022-11-05 15:04:43 +00:00
|
|
|
use super::QueryConfig;
|
|
|
|
|
2022-12-23 13:09:49 +00:00
|
|
|
pub struct QueryState<K, D: DepKind> {
|
2022-02-20 20:06:47 +00:00
|
|
|
#[cfg(parallel_compiler)]
|
2022-12-23 13:09:49 +00:00
|
|
|
active: Sharded<FxHashMap<K, QueryResult<D>>>,
|
2022-02-20 20:06:47 +00:00
|
|
|
#[cfg(not(parallel_compiler))]
|
2022-12-23 13:09:49 +00:00
|
|
|
active: Lock<FxHashMap<K, QueryResult<D>>>,
|
2017-09-25 11:51:49 +00:00
|
|
|
}
|
|
|
|
|
2019-11-01 04:16:52 +00:00
|
|
|
/// Indicates the state of a query for a given key in a query map.
|
2022-12-23 13:09:49 +00:00
|
|
|
enum QueryResult<D: DepKind> {
|
2019-11-01 04:16:52 +00:00
|
|
|
/// An already executing query. The query job can be used to await for its completion.
|
2022-12-23 13:09:49 +00:00
|
|
|
Started(QueryJob<D>),
|
2019-11-01 04:16:52 +00:00
|
|
|
|
2020-01-31 03:00:03 +00:00
|
|
|
/// The query panicked. Queries trying to wait on this will raise a fatal error which will
|
2019-11-01 04:16:52 +00:00
|
|
|
/// silently panic.
|
|
|
|
Poisoned,
|
|
|
|
}
|
|
|
|
|
2022-12-23 13:09:49 +00:00
|
|
|
impl<K, D> QueryState<K, D>
|
2020-10-12 14:29:41 +00:00
|
|
|
where
|
2021-02-06 12:49:08 +00:00
|
|
|
K: Eq + Hash + Clone + Debug,
|
2022-12-23 13:09:49 +00:00
|
|
|
D: DepKind,
|
2020-10-12 14:29:41 +00:00
|
|
|
{
|
2020-03-19 13:13:31 +00:00
|
|
|
pub fn all_inactive(&self) -> bool {
|
2022-02-20 20:06:47 +00:00
|
|
|
#[cfg(parallel_compiler)]
|
|
|
|
{
|
|
|
|
let shards = self.active.lock_shards();
|
|
|
|
shards.iter().all(|shard| shard.is_empty())
|
|
|
|
}
|
|
|
|
#[cfg(not(parallel_compiler))]
|
|
|
|
{
|
|
|
|
self.active.lock().is_empty()
|
|
|
|
}
|
2020-02-08 06:38:00 +00:00
|
|
|
}
|
2020-03-07 10:03:49 +00:00
|
|
|
|
2022-11-05 20:04:19 +00:00
|
|
|
pub fn try_collect_active_jobs<Qcx: Copy>(
|
2020-03-07 10:03:49 +00:00
|
|
|
&self,
|
2022-11-05 20:04:19 +00:00
|
|
|
qcx: Qcx,
|
2022-12-23 13:09:49 +00:00
|
|
|
make_query: fn(Qcx, K) -> QueryStackFrame<D>,
|
|
|
|
jobs: &mut QueryMap<D>,
|
2020-10-12 14:29:41 +00:00
|
|
|
) -> Option<()> {
|
2022-02-20 20:06:47 +00:00
|
|
|
#[cfg(parallel_compiler)]
|
|
|
|
{
|
|
|
|
// We use try_lock_shards here since we are called from the
|
|
|
|
// deadlock handler, and this shouldn't be locked.
|
|
|
|
let shards = self.active.try_lock_shards()?;
|
|
|
|
for shard in shards.iter() {
|
|
|
|
for (k, v) in shard.iter() {
|
|
|
|
if let QueryResult::Started(ref job) = *v {
|
2022-11-05 19:58:10 +00:00
|
|
|
let query = make_query(qcx, k.clone());
|
2022-02-20 20:06:47 +00:00
|
|
|
jobs.insert(job.id, QueryJobInfo { query, job: job.clone() });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#[cfg(not(parallel_compiler))]
|
|
|
|
{
|
|
|
|
// We use try_lock here since we are called from the
|
|
|
|
// deadlock handler, and this shouldn't be locked.
|
|
|
|
// (FIXME: Is this relevant for non-parallel compilers? It doesn't
|
|
|
|
// really hurt much.)
|
|
|
|
for (k, v) in self.active.try_lock()?.iter() {
|
2020-03-07 10:03:49 +00:00
|
|
|
if let QueryResult::Started(ref job) = *v {
|
2022-11-05 19:58:10 +00:00
|
|
|
let query = make_query(qcx, k.clone());
|
2022-02-07 16:03:51 +00:00
|
|
|
jobs.insert(job.id, QueryJobInfo { query, job: job.clone() });
|
2020-03-07 10:03:49 +00:00
|
|
|
}
|
2021-05-01 21:53:34 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-07 10:03:49 +00:00
|
|
|
|
|
|
|
Some(())
|
|
|
|
}
|
2020-02-08 06:38:00 +00:00
|
|
|
}
|
|
|
|
|
2022-12-23 13:09:49 +00:00
|
|
|
impl<K, D: DepKind> Default for QueryState<K, D> {
|
|
|
|
fn default() -> QueryState<K, D> {
|
2022-02-20 20:06:47 +00:00
|
|
|
QueryState { active: Default::default() }
|
2017-09-18 09:40:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-18 02:35:40 +00:00
|
|
|
/// A type representing the responsibility to execute the job in the `job` field.
|
|
|
|
/// This will poison the relevant query if dropped.
|
2022-12-23 13:09:49 +00:00
|
|
|
struct JobOwner<'tcx, K, D: DepKind>
|
2020-03-07 09:38:44 +00:00
|
|
|
where
|
2021-05-11 18:12:52 +00:00
|
|
|
K: Eq + Hash + Clone,
|
2020-03-07 09:38:44 +00:00
|
|
|
{
|
2022-12-23 13:09:49 +00:00
|
|
|
state: &'tcx QueryState<K, D>,
|
2021-05-11 18:12:52 +00:00
|
|
|
key: K,
|
2022-02-07 16:03:51 +00:00
|
|
|
id: QueryJobId,
|
2018-04-18 02:35:40 +00:00
|
|
|
}
|
|
|
|
|
2021-05-01 22:11:51 +00:00
|
|
|
#[cold]
|
|
|
|
#[inline(never)]
|
2023-02-03 18:13:45 +00:00
|
|
|
fn mk_cycle<Qcx, R, D: DepKind>(
|
2022-11-05 20:04:19 +00:00
|
|
|
qcx: Qcx,
|
2022-12-23 13:09:49 +00:00
|
|
|
cycle_error: CycleError<D>,
|
2022-09-02 01:43:12 +00:00
|
|
|
handler: HandleCycleError,
|
2021-05-01 22:11:51 +00:00
|
|
|
) -> R
|
|
|
|
where
|
2022-12-23 13:09:49 +00:00
|
|
|
Qcx: QueryContext + crate::query::HasDepContext<DepKind = D>,
|
2023-02-03 18:13:45 +00:00
|
|
|
R: std::fmt::Debug + Value<Qcx::DepContext, Qcx::DepKind>,
|
2021-05-01 22:11:51 +00:00
|
|
|
{
|
2022-11-05 19:58:10 +00:00
|
|
|
let error = report_cycle(qcx.dep_context().sess(), &cycle_error);
|
2023-02-03 18:13:45 +00:00
|
|
|
handle_cycle_error(*qcx.dep_context(), &cycle_error, error, handler)
|
2021-05-01 22:11:51 +00:00
|
|
|
}
|
|
|
|
|
2022-11-05 20:04:19 +00:00
|
|
|
fn handle_cycle_error<Tcx, V>(
|
|
|
|
tcx: Tcx,
|
2022-12-23 13:09:49 +00:00
|
|
|
cycle_error: &CycleError<Tcx::DepKind>,
|
2022-09-02 01:43:12 +00:00
|
|
|
mut error: DiagnosticBuilder<'_, ErrorGuaranteed>,
|
|
|
|
handler: HandleCycleError,
|
|
|
|
) -> V
|
|
|
|
where
|
2022-11-05 20:04:19 +00:00
|
|
|
Tcx: DepContext,
|
2022-12-23 13:09:49 +00:00
|
|
|
V: Value<Tcx, Tcx::DepKind>,
|
2022-09-02 01:43:12 +00:00
|
|
|
{
|
|
|
|
use HandleCycleError::*;
|
|
|
|
match handler {
|
|
|
|
Error => {
|
|
|
|
error.emit();
|
2022-08-15 19:11:11 +00:00
|
|
|
Value::from_cycle_error(tcx, &cycle_error.cycle)
|
2022-09-02 01:43:12 +00:00
|
|
|
}
|
|
|
|
Fatal => {
|
|
|
|
error.emit();
|
|
|
|
tcx.sess().abort_if_errors();
|
|
|
|
unreachable!()
|
|
|
|
}
|
|
|
|
DelayBug => {
|
|
|
|
error.delay_as_bug();
|
2022-08-15 19:11:11 +00:00
|
|
|
Value::from_cycle_error(tcx, &cycle_error.cycle)
|
2022-09-02 01:43:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-23 13:09:49 +00:00
|
|
|
impl<'tcx, K, D: DepKind> JobOwner<'tcx, K, D>
|
2020-03-07 09:38:44 +00:00
|
|
|
where
|
2021-05-11 18:12:52 +00:00
|
|
|
K: Eq + Hash + Clone,
|
2020-03-07 09:38:44 +00:00
|
|
|
{
|
2019-02-28 22:43:53 +00:00
|
|
|
/// Either gets a `JobOwner` corresponding the query, allowing us to
|
2019-09-06 02:57:44 +00:00
|
|
|
/// start executing the query, or returns with the result of the query.
|
2020-02-12 20:04:36 +00:00
|
|
|
/// This function assumes that `try_get_cached` is already called and returned `lookup`.
|
|
|
|
/// If the query is executing elsewhere, this will wait for it and return the result.
|
2018-04-18 02:35:40 +00:00
|
|
|
/// If the query panicked, this will silently panic.
|
2018-05-21 08:35:47 +00:00
|
|
|
///
|
2019-02-28 22:43:53 +00:00
|
|
|
/// This function is inlined because that results in a noticeable speed-up
|
2018-05-21 08:35:47 +00:00
|
|
|
/// for some compile-time benchmarks.
|
|
|
|
#[inline(always)]
|
2022-11-05 20:04:19 +00:00
|
|
|
fn try_start<'b, Qcx>(
|
|
|
|
qcx: &'b Qcx,
|
2022-12-23 13:09:49 +00:00
|
|
|
state: &'b QueryState<K, Qcx::DepKind>,
|
2020-02-12 20:04:36 +00:00
|
|
|
span: Span,
|
2021-05-11 18:12:52 +00:00
|
|
|
key: K,
|
2022-12-23 13:09:49 +00:00
|
|
|
) -> TryGetJob<'b, K, D>
|
2020-03-07 09:38:44 +00:00
|
|
|
where
|
2022-12-23 13:09:49 +00:00
|
|
|
Qcx: QueryContext + crate::query::HasDepContext<DepKind = D>,
|
2020-03-07 09:38:44 +00:00
|
|
|
{
|
2022-02-20 20:06:47 +00:00
|
|
|
#[cfg(parallel_compiler)]
|
|
|
|
let mut state_lock = state.active.get_shard_by_value(&key).lock();
|
|
|
|
#[cfg(not(parallel_compiler))]
|
|
|
|
let mut state_lock = state.active.lock();
|
2021-02-06 12:49:08 +00:00
|
|
|
let lock = &mut *state_lock;
|
2020-02-12 20:04:36 +00:00
|
|
|
|
2022-02-20 16:59:05 +00:00
|
|
|
match lock.entry(key) {
|
2020-02-12 20:04:36 +00:00
|
|
|
Entry::Vacant(entry) => {
|
2022-11-05 19:58:10 +00:00
|
|
|
let id = qcx.next_job_id();
|
|
|
|
let job = qcx.current_query_job();
|
2020-03-25 06:52:12 +00:00
|
|
|
let job = QueryJob::new(id, span, job);
|
2020-02-12 13:24:38 +00:00
|
|
|
|
2021-05-01 22:11:51 +00:00
|
|
|
let key = entry.key().clone();
|
2020-02-12 20:04:36 +00:00
|
|
|
entry.insert(QueryResult::Started(job));
|
2020-02-12 13:24:38 +00:00
|
|
|
|
2022-02-07 16:03:51 +00:00
|
|
|
let owner = JobOwner { state, id, key };
|
2020-02-12 20:04:36 +00:00
|
|
|
return TryGetJob::NotYetStarted(owner);
|
|
|
|
}
|
2021-05-01 22:11:51 +00:00
|
|
|
Entry::Occupied(mut entry) => {
|
|
|
|
match entry.get_mut() {
|
|
|
|
#[cfg(not(parallel_compiler))]
|
|
|
|
QueryResult::Started(job) => {
|
2022-02-07 16:03:51 +00:00
|
|
|
let id = job.id;
|
2021-05-01 22:11:51 +00:00
|
|
|
drop(state_lock);
|
|
|
|
|
|
|
|
// If we are single-threaded we know that we have cycle error,
|
|
|
|
// so we just return the error.
|
2021-05-11 18:12:52 +00:00
|
|
|
return TryGetJob::Cycle(id.find_cycle_in_stack(
|
2022-11-05 19:58:10 +00:00
|
|
|
qcx.try_collect_active_jobs().unwrap(),
|
|
|
|
&qcx.current_query_job(),
|
2021-05-01 22:11:51 +00:00
|
|
|
span,
|
|
|
|
));
|
2021-02-06 13:52:04 +00:00
|
|
|
}
|
2021-05-01 22:11:51 +00:00
|
|
|
#[cfg(parallel_compiler)]
|
|
|
|
QueryResult::Started(job) => {
|
|
|
|
// For parallel queries, we'll block and wait until the query running
|
|
|
|
// in another thread has completed. Record how long we wait in the
|
|
|
|
// self-profiler.
|
2022-11-05 19:58:10 +00:00
|
|
|
let query_blocked_prof_timer = qcx.dep_context().profiler().query_blocked();
|
2021-05-01 22:11:51 +00:00
|
|
|
|
|
|
|
// Get the latch out
|
|
|
|
let latch = job.latch();
|
|
|
|
|
|
|
|
drop(state_lock);
|
|
|
|
|
|
|
|
// With parallel queries we might just have to wait on some other
|
|
|
|
// thread.
|
2022-11-05 19:58:10 +00:00
|
|
|
let result = latch.wait_on(qcx.current_query_job(), span);
|
2021-05-01 22:11:51 +00:00
|
|
|
|
2021-05-11 18:12:52 +00:00
|
|
|
match result {
|
|
|
|
Ok(()) => TryGetJob::JobCompleted(query_blocked_prof_timer),
|
|
|
|
Err(cycle) => TryGetJob::Cycle(cycle),
|
2021-05-01 22:11:51 +00:00
|
|
|
}
|
2021-02-06 13:52:04 +00:00
|
|
|
}
|
2021-05-01 22:11:51 +00:00
|
|
|
QueryResult::Poisoned => FatalError.raise(),
|
|
|
|
}
|
2020-02-12 20:04:36 +00:00
|
|
|
}
|
2018-04-18 02:35:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-13 13:44:43 +00:00
|
|
|
/// Completes the query by updating the query cache with the `result`,
|
2018-04-18 02:35:40 +00:00
|
|
|
/// signals the waiter and forgets the JobOwner, so it won't poison the query
|
2023-02-08 18:53:48 +00:00
|
|
|
fn complete<C>(self, cache: &C, result: C::Value, dep_node_index: DepNodeIndex)
|
2021-05-11 18:12:52 +00:00
|
|
|
where
|
|
|
|
C: QueryCache<Key = K>,
|
|
|
|
{
|
2018-04-18 02:35:40 +00:00
|
|
|
// We can move out of `self` here because we `mem::forget` it below
|
|
|
|
let key = unsafe { ptr::read(&self.key) };
|
2020-03-07 09:38:44 +00:00
|
|
|
let state = self.state;
|
2018-04-18 02:35:40 +00:00
|
|
|
|
|
|
|
// Forget ourself so our destructor won't poison the query
|
|
|
|
mem::forget(self);
|
|
|
|
|
2023-02-08 18:53:48 +00:00
|
|
|
// Mark as complete before we remove the job from the active state
|
|
|
|
// so no other thread can re-execute this query.
|
|
|
|
cache.complete(key.clone(), result, dep_node_index);
|
|
|
|
|
|
|
|
let job = {
|
|
|
|
#[cfg(parallel_compiler)]
|
|
|
|
let mut lock = state.active.get_shard_by_value(&key).lock();
|
|
|
|
#[cfg(not(parallel_compiler))]
|
|
|
|
let mut lock = state.active.lock();
|
|
|
|
match lock.remove(&key).unwrap() {
|
|
|
|
QueryResult::Started(job) => job,
|
|
|
|
QueryResult::Poisoned => panic!(),
|
|
|
|
}
|
2020-01-31 03:00:03 +00:00
|
|
|
};
|
2018-04-18 02:35:40 +00:00
|
|
|
|
|
|
|
job.signal_complete();
|
|
|
|
}
|
2018-12-07 02:04:23 +00:00
|
|
|
}
|
2018-04-18 02:35:40 +00:00
|
|
|
|
2022-12-23 13:09:49 +00:00
|
|
|
impl<'tcx, K, D> Drop for JobOwner<'tcx, K, D>
|
2020-03-07 09:38:44 +00:00
|
|
|
where
|
2021-05-11 18:12:52 +00:00
|
|
|
K: Eq + Hash + Clone,
|
2022-12-23 13:09:49 +00:00
|
|
|
D: DepKind,
|
2020-03-07 09:38:44 +00:00
|
|
|
{
|
2018-12-05 17:59:48 +00:00
|
|
|
#[inline(never)]
|
|
|
|
#[cold]
|
2018-04-18 02:35:40 +00:00
|
|
|
fn drop(&mut self) {
|
2019-09-06 02:57:44 +00:00
|
|
|
// Poison the query so jobs waiting on it panic.
|
2020-03-07 09:38:44 +00:00
|
|
|
let state = self.state;
|
2020-01-31 03:00:03 +00:00
|
|
|
let job = {
|
2022-02-20 20:06:47 +00:00
|
|
|
#[cfg(parallel_compiler)]
|
|
|
|
let mut shard = state.active.get_shard_by_value(&self.key).lock();
|
|
|
|
#[cfg(not(parallel_compiler))]
|
|
|
|
let mut shard = state.active.lock();
|
2022-02-20 16:59:05 +00:00
|
|
|
let job = match shard.remove(&self.key).unwrap() {
|
2020-01-31 03:00:03 +00:00
|
|
|
QueryResult::Started(job) => job,
|
|
|
|
QueryResult::Poisoned => panic!(),
|
|
|
|
};
|
2022-02-20 16:59:05 +00:00
|
|
|
shard.insert(self.key.clone(), QueryResult::Poisoned);
|
2020-01-31 03:00:03 +00:00
|
|
|
job
|
|
|
|
};
|
2018-04-18 02:35:40 +00:00
|
|
|
// Also signal the completion of the job, so waiters
|
2019-09-06 02:57:44 +00:00
|
|
|
// will continue execution.
|
2020-01-31 03:00:03 +00:00
|
|
|
job.signal_complete();
|
2018-04-18 02:35:40 +00:00
|
|
|
}
|
2017-11-15 13:18:00 +00:00
|
|
|
}
|
|
|
|
|
2018-03-15 09:03:36 +00:00
|
|
|
#[derive(Clone)]
|
2022-12-23 13:09:49 +00:00
|
|
|
pub(crate) struct CycleError<D: DepKind> {
|
2019-09-06 02:57:44 +00:00
|
|
|
/// The query and related span that uses the cycle.
|
2022-12-23 13:09:49 +00:00
|
|
|
pub usage: Option<(Span, QueryStackFrame<D>)>,
|
|
|
|
pub cycle: Vec<QueryInfo<D>>,
|
2018-03-24 05:19:20 +00:00
|
|
|
}
|
|
|
|
|
2020-02-08 06:38:00 +00:00
|
|
|
/// The result of `try_start`.
|
2022-12-23 13:09:49 +00:00
|
|
|
enum TryGetJob<'tcx, K, D>
|
2020-03-07 17:36:24 +00:00
|
|
|
where
|
2021-05-11 18:12:52 +00:00
|
|
|
K: Eq + Hash + Clone,
|
2022-12-23 13:09:49 +00:00
|
|
|
D: DepKind,
|
2020-03-07 17:36:24 +00:00
|
|
|
{
|
2018-06-13 13:44:43 +00:00
|
|
|
/// The query is not yet started. Contains a guard to the cache eventually used to start it.
|
2022-12-23 13:09:49 +00:00
|
|
|
NotYetStarted(JobOwner<'tcx, K, D>),
|
2018-03-24 05:19:20 +00:00
|
|
|
|
|
|
|
/// The query was already completed.
|
2019-09-06 02:57:44 +00:00
|
|
|
/// Returns the result of the query and its dep-node index
|
|
|
|
/// if it succeeded or a cycle error if it failed.
|
2020-02-12 20:04:36 +00:00
|
|
|
#[cfg(parallel_compiler)]
|
2021-05-11 18:12:52 +00:00
|
|
|
JobCompleted(TimingGuard<'tcx>),
|
2019-01-24 19:05:19 +00:00
|
|
|
|
|
|
|
/// Trying to execute the query resulted in a cycle.
|
2022-12-23 13:09:49 +00:00
|
|
|
Cycle(CycleError<D>),
|
2017-09-18 09:40:13 +00:00
|
|
|
}
|
|
|
|
|
2020-03-26 08:40:50 +00:00
|
|
|
/// Checks if the query is already computed and in the cache.
|
|
|
|
/// It returns the shard index and a lock guard to the shard,
|
|
|
|
/// which will be used if the query is not in the cache and we need
|
|
|
|
/// to compute it.
|
2021-02-16 00:00:00 +00:00
|
|
|
#[inline]
|
2023-02-08 18:53:48 +00:00
|
|
|
pub fn try_get_cached<Tcx, C>(tcx: Tcx, cache: &C, key: &C::Key) -> Option<C::Value>
|
2020-03-26 08:40:50 +00:00
|
|
|
where
|
2020-03-24 22:46:47 +00:00
|
|
|
C: QueryCache,
|
2022-11-05 20:04:19 +00:00
|
|
|
Tcx: DepContext,
|
2020-03-26 08:40:50 +00:00
|
|
|
{
|
2023-02-04 15:16:59 +00:00
|
|
|
match cache.lookup(&key) {
|
|
|
|
Some((value, index)) => {
|
2023-02-01 03:49:09 +00:00
|
|
|
tcx.profiler().query_cache_hit(index.into());
|
2023-02-04 15:16:59 +00:00
|
|
|
tcx.dep_graph().read_index(index);
|
|
|
|
Some(value)
|
2020-10-23 20:34:32 +00:00
|
|
|
}
|
2023-02-04 15:16:59 +00:00
|
|
|
None => None,
|
|
|
|
}
|
2020-03-26 08:40:50 +00:00
|
|
|
}
|
2018-04-19 00:33:24 +00:00
|
|
|
|
2022-12-30 22:25:19 +00:00
|
|
|
fn try_execute_query<Q, Qcx>(
|
2022-11-05 20:04:19 +00:00
|
|
|
qcx: Qcx,
|
2022-12-30 22:25:19 +00:00
|
|
|
state: &QueryState<Q::Key, Qcx::DepKind>,
|
|
|
|
cache: &Q::Cache,
|
2020-03-26 08:40:50 +00:00
|
|
|
span: Span,
|
2022-12-30 22:25:19 +00:00
|
|
|
key: Q::Key,
|
2022-11-05 20:04:19 +00:00
|
|
|
dep_node: Option<DepNode<Qcx::DepKind>>,
|
2023-02-08 18:53:48 +00:00
|
|
|
) -> (Q::Value, Option<DepNodeIndex>)
|
2020-03-26 08:40:50 +00:00
|
|
|
where
|
2022-12-30 22:25:19 +00:00
|
|
|
Q: QueryConfig<Qcx>,
|
2022-11-05 20:04:19 +00:00
|
|
|
Qcx: QueryContext,
|
2020-03-26 08:40:50 +00:00
|
|
|
{
|
2022-12-30 22:25:19 +00:00
|
|
|
match JobOwner::<'_, Q::Key, Qcx::DepKind>::try_start(&qcx, state, span, key.clone()) {
|
2021-05-11 18:24:34 +00:00
|
|
|
TryGetJob::NotYetStarted(job) => {
|
2022-12-30 22:25:19 +00:00
|
|
|
let (result, dep_node_index) =
|
|
|
|
execute_job::<Q, Qcx>(qcx, key.clone(), dep_node, job.id);
|
|
|
|
if Q::FEEDABLE {
|
2022-10-29 13:04:38 +00:00
|
|
|
// We may have put a value inside the cache from inside the execution.
|
|
|
|
// Verify that it has the same hash as what we have now, to ensure consistency.
|
2023-02-04 15:16:59 +00:00
|
|
|
if let Some((cached_result, _)) = cache.lookup(&key) {
|
2022-12-30 22:25:19 +00:00
|
|
|
let hasher = Q::HASH_RESULT.expect("feedable forbids no_hash");
|
|
|
|
|
2023-02-04 15:16:59 +00:00
|
|
|
let old_hash = qcx.dep_context().with_stable_hashing_context(|mut hcx| {
|
|
|
|
hasher(&mut hcx, cached_result.borrow())
|
|
|
|
});
|
|
|
|
let new_hash = qcx
|
|
|
|
.dep_context()
|
|
|
|
.with_stable_hashing_context(|mut hcx| hasher(&mut hcx, &result));
|
2022-10-29 13:04:38 +00:00
|
|
|
debug_assert_eq!(
|
2023-02-04 15:16:59 +00:00
|
|
|
old_hash,
|
|
|
|
new_hash,
|
2022-10-29 13:04:38 +00:00
|
|
|
"Computed query value for {:?}({:?}) is inconsistent with fed value,\ncomputed={:#?}\nfed={:#?}",
|
2023-02-04 15:16:59 +00:00
|
|
|
Q::DEP_KIND,
|
|
|
|
key,
|
|
|
|
result,
|
|
|
|
cached_result,
|
2022-10-29 13:04:38 +00:00
|
|
|
);
|
2023-02-04 15:16:59 +00:00
|
|
|
}
|
2022-10-29 13:04:38 +00:00
|
|
|
}
|
2023-02-08 18:53:48 +00:00
|
|
|
job.complete(cache, result, dep_node_index);
|
2021-05-11 18:24:34 +00:00
|
|
|
(result, Some(dep_node_index))
|
|
|
|
}
|
2021-05-11 18:12:52 +00:00
|
|
|
TryGetJob::Cycle(error) => {
|
2023-02-03 18:13:45 +00:00
|
|
|
let result = mk_cycle(qcx, error, Q::HANDLE_CYCLE_ERROR);
|
2021-05-11 18:24:34 +00:00
|
|
|
(result, None)
|
2021-05-11 18:12:52 +00:00
|
|
|
}
|
2020-03-26 08:40:50 +00:00
|
|
|
#[cfg(parallel_compiler)]
|
2021-05-11 18:12:52 +00:00
|
|
|
TryGetJob::JobCompleted(query_blocked_prof_timer) => {
|
2023-02-04 15:56:21 +00:00
|
|
|
let Some((v, index)) = cache.lookup(&key) else {
|
|
|
|
panic!("value must be in cache after waiting")
|
|
|
|
};
|
2021-05-11 18:12:52 +00:00
|
|
|
|
2023-02-01 03:49:09 +00:00
|
|
|
qcx.dep_context().profiler().query_cache_hit(index.into());
|
2021-05-11 18:12:52 +00:00
|
|
|
query_blocked_prof_timer.finish_with_query_invocation_id(index.into());
|
|
|
|
|
2021-05-11 18:24:34 +00:00
|
|
|
(v, Some(index))
|
2018-04-19 00:33:24 +00:00
|
|
|
}
|
2021-05-11 18:24:34 +00:00
|
|
|
}
|
|
|
|
}
|
2018-04-19 00:33:24 +00:00
|
|
|
|
2022-12-30 22:25:19 +00:00
|
|
|
fn execute_job<Q, Qcx>(
|
2022-11-05 20:04:19 +00:00
|
|
|
qcx: Qcx,
|
2022-12-30 22:25:19 +00:00
|
|
|
key: Q::Key,
|
2022-11-05 20:04:19 +00:00
|
|
|
mut dep_node_opt: Option<DepNode<Qcx::DepKind>>,
|
2022-02-07 16:03:51 +00:00
|
|
|
job_id: QueryJobId,
|
2022-12-30 22:25:19 +00:00
|
|
|
) -> (Q::Value, DepNodeIndex)
|
2021-05-11 18:24:34 +00:00
|
|
|
where
|
2022-12-30 22:25:19 +00:00
|
|
|
Q: QueryConfig<Qcx>,
|
2022-11-05 20:04:19 +00:00
|
|
|
Qcx: QueryContext,
|
2021-05-11 18:24:34 +00:00
|
|
|
{
|
2022-11-05 19:58:10 +00:00
|
|
|
let dep_graph = qcx.dep_context().dep_graph();
|
2021-05-12 06:49:49 +00:00
|
|
|
|
|
|
|
// Fast path for when incr. comp. is off.
|
|
|
|
if !dep_graph.is_fully_enabled() {
|
2022-11-05 19:58:10 +00:00
|
|
|
let prof_timer = qcx.dep_context().profiler().query_provider();
|
2023-02-08 18:53:48 +00:00
|
|
|
let result =
|
|
|
|
qcx.start_query(job_id, Q::DEPTH_LIMIT, None, || Q::compute(*qcx.dep_context(), key));
|
2021-05-12 06:49:49 +00:00
|
|
|
let dep_node_index = dep_graph.next_virtual_depnode_index();
|
|
|
|
prof_timer.finish_with_query_invocation_id(dep_node_index.into());
|
2021-05-11 18:24:34 +00:00
|
|
|
return (result, dep_node_index);
|
2020-03-26 08:40:50 +00:00
|
|
|
}
|
2018-04-19 00:33:24 +00:00
|
|
|
|
2022-12-30 22:25:19 +00:00
|
|
|
if !Q::ANON && !Q::EVAL_ALWAYS {
|
2020-12-30 21:08:57 +00:00
|
|
|
// `to_dep_node` is expensive for some `DepKind`s.
|
|
|
|
let dep_node =
|
2022-12-30 22:25:19 +00:00
|
|
|
dep_node_opt.get_or_insert_with(|| Q::construct_dep_node(*qcx.dep_context(), &key));
|
2020-03-28 12:12:20 +00:00
|
|
|
|
2020-12-30 21:08:57 +00:00
|
|
|
// The diagnostics for this query will be promoted to the current session during
|
|
|
|
// `try_mark_green()`, so we can ignore them here.
|
2022-11-05 19:58:10 +00:00
|
|
|
if let Some(ret) = qcx.start_query(job_id, false, None, || {
|
2022-12-30 22:25:19 +00:00
|
|
|
try_load_from_disk_and_cache_in_memory::<Q, Qcx>(qcx, &key, &dep_node)
|
2020-12-30 21:08:57 +00:00
|
|
|
}) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
2020-03-28 12:12:20 +00:00
|
|
|
|
2022-11-05 19:58:10 +00:00
|
|
|
let prof_timer = qcx.dep_context().profiler().query_provider();
|
2020-12-30 21:08:57 +00:00
|
|
|
let diagnostics = Lock::new(ThinVec::new());
|
2020-03-28 12:12:20 +00:00
|
|
|
|
2022-08-24 01:42:12 +00:00
|
|
|
let (result, dep_node_index) =
|
2022-12-30 22:25:19 +00:00
|
|
|
qcx.start_query(job_id, Q::DEPTH_LIMIT, Some(&diagnostics), || {
|
|
|
|
if Q::ANON {
|
|
|
|
return dep_graph.with_anon_task(*qcx.dep_context(), Q::DEP_KIND, || {
|
2023-02-08 18:53:48 +00:00
|
|
|
Q::compute(*qcx.dep_context(), key)
|
2022-08-24 01:42:12 +00:00
|
|
|
});
|
|
|
|
}
|
2020-03-28 12:12:20 +00:00
|
|
|
|
2022-08-24 01:42:12 +00:00
|
|
|
// `to_dep_node` is expensive for some `DepKind`s.
|
|
|
|
let dep_node =
|
2022-12-30 22:25:19 +00:00
|
|
|
dep_node_opt.unwrap_or_else(|| Q::construct_dep_node(*qcx.dep_context(), &key));
|
2021-07-23 21:40:26 +00:00
|
|
|
|
2023-02-08 18:53:48 +00:00
|
|
|
dep_graph.with_task(dep_node, *qcx.dep_context(), key, Q::compute, Q::HASH_RESULT)
|
2022-08-24 01:42:12 +00:00
|
|
|
});
|
2018-04-19 00:33:24 +00:00
|
|
|
|
2020-12-30 21:08:57 +00:00
|
|
|
prof_timer.finish_with_query_invocation_id(dep_node_index.into());
|
2020-03-26 08:40:50 +00:00
|
|
|
|
2020-12-30 21:08:57 +00:00
|
|
|
let diagnostics = diagnostics.into_inner();
|
|
|
|
let side_effects = QuerySideEffects { diagnostics };
|
2020-03-26 08:40:50 +00:00
|
|
|
|
2022-05-31 21:42:42 +00:00
|
|
|
if std::intrinsics::unlikely(!side_effects.is_empty()) {
|
2022-12-30 22:25:19 +00:00
|
|
|
if Q::ANON {
|
2022-11-05 19:58:10 +00:00
|
|
|
qcx.store_side_effects_for_anon_node(dep_node_index, side_effects);
|
2020-11-02 20:27:36 +00:00
|
|
|
} else {
|
2022-11-05 19:58:10 +00:00
|
|
|
qcx.store_side_effects(dep_node_index, side_effects);
|
2018-04-19 00:33:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-30 21:08:57 +00:00
|
|
|
(result, dep_node_index)
|
2020-03-26 08:40:50 +00:00
|
|
|
}
|
2018-04-19 00:33:24 +00:00
|
|
|
|
2022-12-30 22:25:19 +00:00
|
|
|
fn try_load_from_disk_and_cache_in_memory<Q, Qcx>(
|
2022-11-05 20:04:19 +00:00
|
|
|
qcx: Qcx,
|
2022-12-30 22:25:19 +00:00
|
|
|
key: &Q::Key,
|
2022-11-05 20:04:19 +00:00
|
|
|
dep_node: &DepNode<Qcx::DepKind>,
|
2022-12-30 22:25:19 +00:00
|
|
|
) -> Option<(Q::Value, DepNodeIndex)>
|
2020-03-26 08:40:50 +00:00
|
|
|
where
|
2022-12-30 22:25:19 +00:00
|
|
|
Q: QueryConfig<Qcx>,
|
2022-11-05 20:04:19 +00:00
|
|
|
Qcx: QueryContext,
|
2020-03-26 08:40:50 +00:00
|
|
|
{
|
|
|
|
// Note this function can be called concurrently from the same query
|
|
|
|
// We must ensure that this is handled correctly.
|
2018-12-24 12:35:37 +00:00
|
|
|
|
2022-11-05 19:58:10 +00:00
|
|
|
let dep_graph = qcx.dep_context().dep_graph();
|
|
|
|
let (prev_dep_node_index, dep_node_index) = dep_graph.try_mark_green(qcx, &dep_node)?;
|
2020-11-02 21:17:29 +00:00
|
|
|
|
2021-05-12 09:21:12 +00:00
|
|
|
debug_assert!(dep_graph.is_green(dep_node));
|
2018-12-24 12:35:37 +00:00
|
|
|
|
2020-03-26 08:40:50 +00:00
|
|
|
// First we try to load the result from the on-disk cache.
|
2021-05-12 06:50:03 +00:00
|
|
|
// Some things are never cached on disk.
|
2022-12-30 22:25:19 +00:00
|
|
|
if let Some(try_load_from_disk) = Q::try_load_from_disk(qcx, &key) {
|
2022-11-05 19:58:10 +00:00
|
|
|
let prof_timer = qcx.dep_context().profiler().incr_cache_loading();
|
2021-12-14 15:59:29 +00:00
|
|
|
|
2021-12-21 02:46:55 +00:00
|
|
|
// The call to `with_query_deserialization` enforces that no new `DepNodes`
|
|
|
|
// are created during deserialization. See the docs of that method for more
|
|
|
|
// details.
|
2022-09-02 03:26:03 +00:00
|
|
|
let result =
|
2022-11-05 19:58:10 +00:00
|
|
|
dep_graph.with_query_deserialization(|| try_load_from_disk(qcx, prev_dep_node_index));
|
2021-12-21 02:46:55 +00:00
|
|
|
|
2020-03-26 08:40:50 +00:00
|
|
|
prof_timer.finish_with_query_invocation_id(dep_node_index.into());
|
2019-12-13 13:44:08 +00:00
|
|
|
|
2021-05-12 06:50:03 +00:00
|
|
|
if let Some(result) = result {
|
2022-05-31 21:42:42 +00:00
|
|
|
if std::intrinsics::unlikely(
|
2022-11-05 19:58:10 +00:00
|
|
|
qcx.dep_context().sess().opts.unstable_opts.query_dep_graph,
|
2022-05-31 21:42:42 +00:00
|
|
|
) {
|
2021-12-21 21:31:35 +00:00
|
|
|
dep_graph.mark_debug_loaded_from_disk(*dep_node)
|
|
|
|
}
|
|
|
|
|
2022-11-05 19:58:10 +00:00
|
|
|
let prev_fingerprint = qcx
|
2021-10-27 02:13:23 +00:00
|
|
|
.dep_context()
|
|
|
|
.dep_graph()
|
|
|
|
.prev_fingerprint_of(dep_node)
|
|
|
|
.unwrap_or(Fingerprint::ZERO);
|
2021-05-12 06:50:03 +00:00
|
|
|
// If `-Zincremental-verify-ich` is specified, re-hash results from
|
|
|
|
// the cache and make sure that they have the expected fingerprint.
|
2021-10-27 02:13:23 +00:00
|
|
|
//
|
|
|
|
// If not, we still seek to verify a subset of fingerprints loaded
|
|
|
|
// from disk. Re-hashing results is fairly expensive, so we can't
|
|
|
|
// currently afford to verify every hash. This subset should still
|
|
|
|
// give us some coverage of potential bugs though.
|
|
|
|
let try_verify = prev_fingerprint.as_value().1 % 32 == 0;
|
2022-05-31 21:42:42 +00:00
|
|
|
if std::intrinsics::unlikely(
|
2022-11-05 19:58:10 +00:00
|
|
|
try_verify || qcx.dep_context().sess().opts.unstable_opts.incremental_verify_ich,
|
2021-10-27 02:13:23 +00:00
|
|
|
) {
|
2022-12-30 22:25:19 +00:00
|
|
|
incremental_verify_ich(*qcx.dep_context(), &result, dep_node, Q::HASH_RESULT);
|
2021-05-12 06:50:03 +00:00
|
|
|
}
|
2018-12-05 22:28:38 +00:00
|
|
|
|
2021-05-12 06:50:03 +00:00
|
|
|
return Some((result, dep_node_index));
|
2021-03-11 06:12:07 +00:00
|
|
|
}
|
2021-10-16 18:10:23 +00:00
|
|
|
|
|
|
|
// We always expect to find a cached result for things that
|
|
|
|
// can be forced from `DepNode`.
|
|
|
|
debug_assert!(
|
2022-11-05 19:58:10 +00:00
|
|
|
!qcx.dep_context().fingerprint_style(dep_node.kind).reconstructible(),
|
2022-12-19 09:31:55 +00:00
|
|
|
"missing on-disk cache entry for {dep_node:?}"
|
2021-10-16 18:10:23 +00:00
|
|
|
);
|
2021-05-12 06:50:03 +00:00
|
|
|
}
|
2021-03-11 06:12:07 +00:00
|
|
|
|
2021-05-12 06:50:03 +00:00
|
|
|
// We could not load a result from the on-disk cache, so
|
|
|
|
// recompute.
|
2022-11-05 19:58:10 +00:00
|
|
|
let prof_timer = qcx.dep_context().profiler().query_provider();
|
2020-03-26 08:40:50 +00:00
|
|
|
|
2021-05-12 06:50:03 +00:00
|
|
|
// The dep-graph for this computation is already in-place.
|
2023-02-08 18:53:48 +00:00
|
|
|
let result = dep_graph.with_ignore(|| Q::compute(*qcx.dep_context(), key.clone()));
|
2020-03-26 08:40:50 +00:00
|
|
|
|
2021-05-12 06:50:03 +00:00
|
|
|
prof_timer.finish_with_query_invocation_id(dep_node_index.into());
|
2018-04-19 00:33:24 +00:00
|
|
|
|
2021-05-12 06:50:03 +00:00
|
|
|
// Verify that re-running the query produced a result with the expected hash
|
|
|
|
// This catches bugs in query implementations, turning them into ICEs.
|
|
|
|
// For example, a query might sort its result by `DefId` - since `DefId`s are
|
|
|
|
// not stable across compilation sessions, the result could get up getting sorted
|
|
|
|
// in a different order when the query is re-run, even though all of the inputs
|
|
|
|
// (e.g. `DefPathHash` values) were green.
|
|
|
|
//
|
|
|
|
// See issue #82920 for an example of a miscompilation that would get turned into
|
|
|
|
// an ICE by this check
|
2022-12-30 22:25:19 +00:00
|
|
|
incremental_verify_ich(*qcx.dep_context(), &result, dep_node, Q::HASH_RESULT);
|
2020-11-02 21:17:29 +00:00
|
|
|
|
|
|
|
Some((result, dep_node_index))
|
2020-03-26 08:40:50 +00:00
|
|
|
}
|
2018-04-19 00:33:24 +00:00
|
|
|
|
2022-09-04 20:42:05 +00:00
|
|
|
#[instrument(skip(tcx, result, hash_result), level = "debug")]
|
|
|
|
pub(crate) fn incremental_verify_ich<Tcx, V: Debug>(
|
|
|
|
tcx: Tcx,
|
2020-03-06 21:43:08 +00:00
|
|
|
result: &V,
|
2022-09-04 20:42:05 +00:00
|
|
|
dep_node: &DepNode<Tcx::DepKind>,
|
|
|
|
hash_result: Option<fn(&mut StableHashingContext<'_>, &V) -> Fingerprint>,
|
|
|
|
) -> Fingerprint
|
|
|
|
where
|
|
|
|
Tcx: DepContext,
|
2020-03-26 08:40:50 +00:00
|
|
|
{
|
|
|
|
assert!(
|
2022-09-04 20:42:05 +00:00
|
|
|
tcx.dep_graph().is_green(dep_node),
|
2022-12-19 09:31:55 +00:00
|
|
|
"fingerprint for green query instance not loaded from cache: {dep_node:?}",
|
2020-03-26 08:40:50 +00:00
|
|
|
);
|
2018-04-19 00:33:24 +00:00
|
|
|
|
2022-09-04 20:42:05 +00:00
|
|
|
let new_hash = hash_result.map_or(Fingerprint::ZERO, |f| {
|
|
|
|
tcx.with_stable_hashing_context(|mut hcx| f(&mut hcx, result))
|
2021-10-16 20:31:48 +00:00
|
|
|
});
|
2022-11-05 19:40:42 +00:00
|
|
|
|
2022-09-04 20:42:05 +00:00
|
|
|
let old_hash = tcx.dep_graph().prev_fingerprint_of(dep_node);
|
2018-04-19 00:33:24 +00:00
|
|
|
|
2021-05-06 17:44:17 +00:00
|
|
|
if Some(new_hash) != old_hash {
|
2022-11-05 19:26:41 +00:00
|
|
|
incremental_verify_ich_failed(
|
2022-09-04 20:42:05 +00:00
|
|
|
tcx.sess(),
|
2022-11-05 19:26:41 +00:00
|
|
|
DebugArg::from(&dep_node),
|
|
|
|
DebugArg::from(&result),
|
|
|
|
);
|
2021-10-12 02:33:16 +00:00
|
|
|
}
|
2022-09-04 20:42:05 +00:00
|
|
|
|
|
|
|
new_hash
|
2021-10-12 02:33:16 +00:00
|
|
|
}
|
2021-08-12 20:11:21 +00:00
|
|
|
|
2021-10-12 02:33:16 +00:00
|
|
|
// This DebugArg business is largely a mirror of std::fmt::ArgumentV1, which is
|
|
|
|
// currently not exposed publicly.
|
|
|
|
//
|
|
|
|
// The PR which added this attempted to use `&dyn Debug` instead, but that
|
|
|
|
// showed statistically significant worse compiler performance. It's not
|
|
|
|
// actually clear what the cause there was -- the code should be cold. If this
|
|
|
|
// can be replaced with `&dyn Debug` with on perf impact, then it probably
|
|
|
|
// should be.
|
|
|
|
extern "C" {
|
|
|
|
type Opaque;
|
|
|
|
}
|
2021-08-12 20:11:21 +00:00
|
|
|
|
2021-10-12 02:33:16 +00:00
|
|
|
struct DebugArg<'a> {
|
|
|
|
value: &'a Opaque,
|
|
|
|
fmt: fn(&Opaque, &mut std::fmt::Formatter<'_>) -> std::fmt::Result,
|
|
|
|
}
|
2021-08-12 20:11:21 +00:00
|
|
|
|
2021-10-12 02:33:16 +00:00
|
|
|
impl<'a, T> From<&'a T> for DebugArg<'a>
|
|
|
|
where
|
|
|
|
T: std::fmt::Debug,
|
|
|
|
{
|
|
|
|
fn from(value: &'a T) -> DebugArg<'a> {
|
|
|
|
DebugArg {
|
|
|
|
value: unsafe { std::mem::transmute(value) },
|
|
|
|
fmt: unsafe {
|
|
|
|
std::mem::transmute(<T as std::fmt::Debug>::fmt as fn(_, _) -> std::fmt::Result)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::fmt::Debug for DebugArg<'_> {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
(self.fmt)(self.value, f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note that this is marked #[cold] and intentionally takes the equivalent of
|
|
|
|
// `dyn Debug` for its arguments, as we want to avoid generating a bunch of
|
|
|
|
// different implementations for LLVM to chew on (and filling up the final
|
|
|
|
// binary, too).
|
|
|
|
#[cold]
|
2022-11-05 19:26:41 +00:00
|
|
|
fn incremental_verify_ich_failed(sess: &Session, dep_node: DebugArg<'_>, result: DebugArg<'_>) {
|
2021-10-12 02:33:16 +00:00
|
|
|
// When we emit an error message and panic, we try to debug-print the `DepNode`
|
|
|
|
// and query result. Unfortunately, this can cause us to run additional queries,
|
|
|
|
// which may result in another fingerprint mismatch while we're in the middle
|
|
|
|
// of processing this one. To avoid a double-panic (which kills the process
|
|
|
|
// before we can print out the query static), we print out a terse
|
|
|
|
// but 'safe' message if we detect a re-entrant call to this method.
|
|
|
|
thread_local! {
|
|
|
|
static INSIDE_VERIFY_PANIC: Cell<bool> = const { Cell::new(false) };
|
|
|
|
};
|
|
|
|
|
|
|
|
let old_in_panic = INSIDE_VERIFY_PANIC.with(|in_panic| in_panic.replace(true));
|
|
|
|
|
|
|
|
if old_in_panic {
|
2022-08-21 13:37:05 +00:00
|
|
|
sess.emit_err(crate::error::Reentrant);
|
2021-10-12 02:33:16 +00:00
|
|
|
} else {
|
2022-11-05 19:26:41 +00:00
|
|
|
let run_cmd = if let Some(crate_name) = &sess.opts.crate_name {
|
2022-12-19 09:31:55 +00:00
|
|
|
format!("`cargo clean -p {crate_name}` or `cargo clean`")
|
2022-11-05 19:26:41 +00:00
|
|
|
} else {
|
|
|
|
"`cargo clean`".to_string()
|
|
|
|
};
|
|
|
|
|
2022-08-21 13:37:05 +00:00
|
|
|
sess.emit_err(crate::error::IncrementCompilation {
|
|
|
|
run_cmd,
|
2022-12-19 09:31:55 +00:00
|
|
|
dep_node: format!("{dep_node:?}"),
|
2022-08-21 13:37:05 +00:00
|
|
|
});
|
2022-12-19 09:31:55 +00:00
|
|
|
panic!("Found unstable fingerprints for {dep_node:?}: {result:?}");
|
2021-05-06 17:44:17 +00:00
|
|
|
}
|
2021-10-12 02:33:16 +00:00
|
|
|
|
|
|
|
INSIDE_VERIFY_PANIC.with(|in_panic| in_panic.set(old_in_panic));
|
2020-03-26 08:40:50 +00:00
|
|
|
}
|
|
|
|
|
2020-03-27 06:35:32 +00:00
|
|
|
/// Ensure that either this query has all green inputs or been executed.
|
|
|
|
/// Executing `query::ensure(D)` is considered a read of the dep-node `D`.
|
2020-11-18 15:53:39 +00:00
|
|
|
/// Returns true if the query should still run.
|
2020-03-27 06:35:32 +00:00
|
|
|
///
|
|
|
|
/// This function is particularly useful when executing passes for their
|
|
|
|
/// side-effects -- e.g., in order to report errors for erroneous programs.
|
|
|
|
///
|
|
|
|
/// Note: The optimization is only available during incr. comp.
|
2020-03-29 09:44:40 +00:00
|
|
|
#[inline(never)]
|
2022-12-30 22:25:19 +00:00
|
|
|
fn ensure_must_run<Q, Qcx>(qcx: Qcx, key: &Q::Key) -> (bool, Option<DepNode<Qcx::DepKind>>)
|
2020-11-18 15:53:39 +00:00
|
|
|
where
|
2022-12-30 22:25:19 +00:00
|
|
|
Q: QueryConfig<Qcx>,
|
2022-11-05 20:04:19 +00:00
|
|
|
Qcx: QueryContext,
|
2020-03-19 17:43:01 +00:00
|
|
|
{
|
2022-12-30 22:25:19 +00:00
|
|
|
if Q::EVAL_ALWAYS {
|
2020-12-30 21:07:42 +00:00
|
|
|
return (true, None);
|
2020-03-19 17:43:01 +00:00
|
|
|
}
|
|
|
|
|
2020-03-27 06:35:32 +00:00
|
|
|
// Ensuring an anonymous query makes no sense
|
2022-12-30 22:25:19 +00:00
|
|
|
assert!(!Q::ANON);
|
2019-03-10 09:11:15 +00:00
|
|
|
|
2022-12-30 22:25:19 +00:00
|
|
|
let dep_node = Q::construct_dep_node(*qcx.dep_context(), key);
|
2019-06-24 23:41:16 +00:00
|
|
|
|
2022-11-05 19:58:10 +00:00
|
|
|
let dep_graph = qcx.dep_context().dep_graph();
|
|
|
|
match dep_graph.try_mark_green(qcx, &dep_node) {
|
2020-03-27 06:35:32 +00:00
|
|
|
None => {
|
2021-05-30 08:48:48 +00:00
|
|
|
// A None return from `try_mark_green` means that this is either
|
2020-03-27 06:35:32 +00:00
|
|
|
// a new dep node or that the dep node has already been marked red.
|
|
|
|
// Either way, we can't call `dep_graph.read()` as we don't have the
|
|
|
|
// DepNodeIndex. We must invoke the query itself. The performance cost
|
|
|
|
// this introduces should be negligible as we'll immediately hit the
|
|
|
|
// in-memory cache, or another query down the line will.
|
2020-12-30 21:07:42 +00:00
|
|
|
(true, Some(dep_node))
|
2020-03-27 06:35:32 +00:00
|
|
|
}
|
|
|
|
Some((_, dep_node_index)) => {
|
2021-05-30 08:48:48 +00:00
|
|
|
dep_graph.read_index(dep_node_index);
|
2023-02-01 03:49:09 +00:00
|
|
|
qcx.dep_context().profiler().query_cache_hit(dep_node_index.into());
|
2020-12-30 21:07:42 +00:00
|
|
|
(false, None)
|
2018-04-19 00:33:24 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-27 06:35:32 +00:00
|
|
|
}
|
2018-04-19 00:33:24 +00:00
|
|
|
|
2022-05-04 08:30:13 +00:00
|
|
|
#[derive(Debug)]
|
2020-11-18 15:53:39 +00:00
|
|
|
pub enum QueryMode {
|
|
|
|
Get,
|
|
|
|
Ensure,
|
2020-03-28 13:09:53 +00:00
|
|
|
}
|
|
|
|
|
2023-02-08 18:53:48 +00:00
|
|
|
pub fn get_query<Q, Qcx, D>(qcx: Qcx, span: Span, key: Q::Key, mode: QueryMode) -> Option<Q::Value>
|
2020-03-28 13:09:53 +00:00
|
|
|
where
|
2022-12-23 13:09:49 +00:00
|
|
|
D: DepKind,
|
2022-11-05 20:04:19 +00:00
|
|
|
Q: QueryConfig<Qcx>,
|
2022-12-23 13:09:49 +00:00
|
|
|
Q::Value: Value<Qcx::DepContext, D>,
|
2022-11-05 20:04:19 +00:00
|
|
|
Qcx: QueryContext,
|
2020-03-28 13:09:53 +00:00
|
|
|
{
|
2020-12-30 21:07:42 +00:00
|
|
|
let dep_node = if let QueryMode::Ensure = mode {
|
2022-12-30 22:25:19 +00:00
|
|
|
let (must_run, dep_node) = ensure_must_run::<Q, _>(qcx, &key);
|
2020-12-30 21:07:42 +00:00
|
|
|
if !must_run {
|
2020-11-18 15:53:39 +00:00
|
|
|
return None;
|
|
|
|
}
|
2020-12-30 21:07:42 +00:00
|
|
|
dep_node
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2020-11-18 15:53:39 +00:00
|
|
|
|
2022-12-30 22:25:19 +00:00
|
|
|
let (result, dep_node_index) = try_execute_query::<Q, Qcx>(
|
2022-11-05 19:58:10 +00:00
|
|
|
qcx,
|
|
|
|
Q::query_state(qcx),
|
|
|
|
Q::query_cache(qcx),
|
2021-05-10 17:09:30 +00:00
|
|
|
span,
|
|
|
|
key,
|
2020-12-30 21:07:42 +00:00
|
|
|
dep_node,
|
2021-05-10 17:09:30 +00:00
|
|
|
);
|
2020-12-30 21:07:42 +00:00
|
|
|
if let Some(dep_node_index) = dep_node_index {
|
2022-11-05 19:58:10 +00:00
|
|
|
qcx.dep_context().dep_graph().read_index(dep_node_index)
|
2020-12-30 21:07:42 +00:00
|
|
|
}
|
|
|
|
Some(result)
|
2020-03-28 13:09:53 +00:00
|
|
|
}
|
|
|
|
|
2022-12-23 13:09:49 +00:00
|
|
|
pub fn force_query<Q, Qcx, D>(qcx: Qcx, key: Q::Key, dep_node: DepNode<Qcx::DepKind>)
|
2020-03-28 13:09:53 +00:00
|
|
|
where
|
2022-12-23 13:09:49 +00:00
|
|
|
D: DepKind,
|
2022-11-05 20:04:19 +00:00
|
|
|
Q: QueryConfig<Qcx>,
|
2022-12-23 13:09:49 +00:00
|
|
|
Q::Value: Value<Qcx::DepContext, D>,
|
2022-11-05 20:04:19 +00:00
|
|
|
Qcx: QueryContext,
|
2020-03-28 13:09:53 +00:00
|
|
|
{
|
2021-10-16 19:12:34 +00:00
|
|
|
// We may be concurrently trying both execute and force a query.
|
|
|
|
// Ensure that only one of them runs the query.
|
2022-11-05 19:58:10 +00:00
|
|
|
let cache = Q::query_cache(qcx);
|
2023-02-04 15:16:59 +00:00
|
|
|
if let Some((_, index)) = cache.lookup(&key) {
|
2023-02-01 03:49:09 +00:00
|
|
|
qcx.dep_context().profiler().query_cache_hit(index.into());
|
2023-02-04 15:16:59 +00:00
|
|
|
return;
|
2022-02-20 03:56:56 +00:00
|
|
|
}
|
2021-05-10 17:09:30 +00:00
|
|
|
|
2022-11-05 19:58:10 +00:00
|
|
|
let state = Q::query_state(qcx);
|
2022-12-30 22:25:19 +00:00
|
|
|
debug_assert!(!Q::ANON);
|
2021-10-17 15:37:20 +00:00
|
|
|
|
2022-12-30 22:25:19 +00:00
|
|
|
try_execute_query::<Q, _>(qcx, state, cache, DUMMY_SP, key, Some(dep_node));
|
2020-03-28 13:09:53 +00:00
|
|
|
}
|