2020-03-19 08:13:31 -05:00
|
|
|
use crate::query::plumbing::CycleError;
|
2022-02-16 17:06:50 -06:00
|
|
|
use crate::query::{QueryContext, QueryStackFrame};
|
|
|
|
use rustc_hir::def::DefKind;
|
2019-02-05 11:20:45 -06:00
|
|
|
|
2020-01-30 21:00:03 -06:00
|
|
|
use rustc_data_structures::fx::FxHashMap;
|
2022-01-23 12:34:26 -06:00
|
|
|
use rustc_errors::{
|
|
|
|
struct_span_err, Diagnostic, DiagnosticBuilder, ErrorGuaranteed, Handler, Level,
|
|
|
|
};
|
2020-11-28 17:21:56 -06:00
|
|
|
use rustc_session::Session;
|
2019-12-31 11:15:40 -06:00
|
|
|
use rustc_span::Span;
|
2019-02-05 11:20:45 -06:00
|
|
|
|
2020-10-12 09:29:41 -05:00
|
|
|
use std::hash::Hash;
|
2022-02-07 10:03:51 -06:00
|
|
|
use std::num::NonZeroU64;
|
2018-08-18 05:55:43 -05:00
|
|
|
|
2019-01-28 08:51:47 -06:00
|
|
|
#[cfg(parallel_compiler)]
|
2018-04-06 05:56:59 -05:00
|
|
|
use {
|
|
|
|
parking_lot::{Condvar, Mutex},
|
2019-07-24 03:44:14 -05:00
|
|
|
rustc_data_structures::fx::FxHashSet,
|
|
|
|
rustc_data_structures::sync::Lock,
|
2020-01-30 21:00:03 -06:00
|
|
|
rustc_data_structures::sync::Lrc,
|
2019-07-24 03:44:14 -05:00
|
|
|
rustc_data_structures::{jobserver, OnDrop},
|
|
|
|
rustc_rayon_core as rayon_core,
|
2019-12-31 11:15:40 -06:00
|
|
|
rustc_span::DUMMY_SP,
|
2021-03-08 17:32:41 -06:00
|
|
|
std::iter::{self, FromIterator},
|
2020-03-19 08:13:31 -05:00
|
|
|
std::{mem, process},
|
2018-04-06 05:56:59 -05:00
|
|
|
};
|
2018-03-15 04:03:36 -05:00
|
|
|
|
2019-02-08 07:53:55 -06:00
|
|
|
/// Represents a span and a query key.
|
2018-03-15 04:03:36 -05:00
|
|
|
#[derive(Clone, Debug)]
|
2020-11-28 15:48:05 -06:00
|
|
|
pub struct QueryInfo {
|
2019-02-08 07:53:55 -06:00
|
|
|
/// The span corresponding to the reason for which this query was required.
|
2018-03-15 04:03:36 -05:00
|
|
|
pub span: Span,
|
2020-11-28 15:48:05 -06:00
|
|
|
pub query: QueryStackFrame,
|
2018-03-15 04:03:36 -05:00
|
|
|
}
|
|
|
|
|
2022-02-07 10:03:51 -06:00
|
|
|
pub type QueryMap = FxHashMap<QueryJobId, QueryJobInfo>;
|
2020-01-30 21:00:03 -06:00
|
|
|
|
2020-11-21 02:06:45 -06:00
|
|
|
/// A value uniquely identifying an active query job.
|
2020-01-30 21:00:03 -06:00
|
|
|
#[derive(Copy, Clone, Eq, PartialEq, Hash)]
|
2022-02-07 10:03:51 -06:00
|
|
|
pub struct QueryJobId(pub NonZeroU64);
|
2020-02-12 04:50:00 -06:00
|
|
|
|
2022-02-07 10:03:51 -06:00
|
|
|
impl QueryJobId {
|
|
|
|
fn query(self, map: &QueryMap) -> QueryStackFrame {
|
2021-09-01 13:07:33 -05:00
|
|
|
map.get(&self).unwrap().query.clone()
|
2020-01-30 21:00:03 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(parallel_compiler)]
|
2022-02-07 10:03:51 -06:00
|
|
|
fn span(self, map: &QueryMap) -> Span {
|
2020-01-30 21:00:03 -06:00
|
|
|
map.get(&self).unwrap().job.span
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(parallel_compiler)]
|
2022-02-07 10:03:51 -06:00
|
|
|
fn parent(self, map: &QueryMap) -> Option<QueryJobId> {
|
2020-01-30 21:00:03 -06:00
|
|
|
map.get(&self).unwrap().job.parent
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(parallel_compiler)]
|
2022-02-07 10:03:51 -06:00
|
|
|
fn latch<'a>(self, map: &'a QueryMap) -> Option<&'a QueryLatch> {
|
2020-01-30 21:00:03 -06:00
|
|
|
map.get(&self).unwrap().job.latch.as_ref()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-07 10:03:51 -06:00
|
|
|
pub struct QueryJobInfo {
|
2021-09-01 13:07:33 -05:00
|
|
|
pub query: QueryStackFrame,
|
2022-02-07 10:03:51 -06:00
|
|
|
pub job: QueryJob,
|
2020-01-30 21:00:03 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Represents an active query job.
|
|
|
|
#[derive(Clone)]
|
2022-02-07 10:03:51 -06:00
|
|
|
pub struct QueryJob {
|
|
|
|
pub id: QueryJobId,
|
2020-01-30 21:00:03 -06:00
|
|
|
|
|
|
|
/// The span corresponding to the reason for which this query was required.
|
|
|
|
pub span: Span,
|
2018-03-24 00:19:20 -05:00
|
|
|
|
|
|
|
/// The parent query job which created this job and is implicitly waiting on it.
|
2022-02-07 10:03:51 -06:00
|
|
|
pub parent: Option<QueryJobId>,
|
2018-03-24 00:19:20 -05:00
|
|
|
|
2019-02-08 07:53:55 -06:00
|
|
|
/// The latch that is used to wait on this job.
|
2019-01-28 08:51:47 -06:00
|
|
|
#[cfg(parallel_compiler)]
|
2022-02-07 10:03:51 -06:00
|
|
|
latch: Option<QueryLatch>,
|
2018-03-15 04:03:36 -05:00
|
|
|
}
|
|
|
|
|
2022-02-07 10:03:51 -06:00
|
|
|
impl QueryJob {
|
2019-02-08 07:53:55 -06:00
|
|
|
/// Creates a new query job.
|
2022-07-06 19:00:00 -05:00
|
|
|
#[inline]
|
2022-02-07 10:03:51 -06:00
|
|
|
pub fn new(id: QueryJobId, span: Span, parent: Option<QueryJobId>) -> Self {
|
2018-03-15 04:03:36 -05:00
|
|
|
QueryJob {
|
2020-02-12 04:50:00 -06:00
|
|
|
id,
|
2020-01-30 21:00:03 -06:00
|
|
|
span,
|
2018-03-15 04:03:36 -05:00
|
|
|
parent,
|
2019-01-28 08:51:47 -06:00
|
|
|
#[cfg(parallel_compiler)]
|
2020-01-30 21:00:03 -06:00
|
|
|
latch: None,
|
2018-03-15 04:03:36 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-28 08:51:47 -06:00
|
|
|
#[cfg(parallel_compiler)]
|
2022-02-07 10:03:51 -06:00
|
|
|
pub(super) fn latch(&mut self) -> QueryLatch {
|
2020-01-30 21:00:03 -06:00
|
|
|
if self.latch.is_none() {
|
|
|
|
self.latch = Some(QueryLatch::new());
|
|
|
|
}
|
|
|
|
self.latch.as_ref().unwrap().clone()
|
2018-04-06 05:56:59 -05:00
|
|
|
}
|
|
|
|
|
2020-01-30 21:00:03 -06:00
|
|
|
/// Signals to waiters that the query is complete.
|
|
|
|
///
|
|
|
|
/// This does nothing for single threaded rustc,
|
|
|
|
/// as there are no concurrent jobs which could be waiting on us
|
2022-07-06 19:00:00 -05:00
|
|
|
#[inline]
|
2020-01-30 21:00:03 -06:00
|
|
|
pub fn signal_complete(self) {
|
|
|
|
#[cfg(parallel_compiler)]
|
2020-04-24 15:58:41 -05:00
|
|
|
{
|
|
|
|
if let Some(latch) = self.latch {
|
|
|
|
latch.set();
|
|
|
|
}
|
|
|
|
}
|
2020-01-30 21:00:03 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(parallel_compiler))]
|
2022-02-07 10:03:51 -06:00
|
|
|
impl QueryJobId {
|
2021-05-11 13:12:52 -05:00
|
|
|
#[cold]
|
|
|
|
#[inline(never)]
|
2020-10-12 09:29:41 -05:00
|
|
|
pub(super) fn find_cycle_in_stack(
|
|
|
|
&self,
|
2022-02-07 10:03:51 -06:00
|
|
|
query_map: QueryMap,
|
|
|
|
current_job: &Option<QueryJobId>,
|
2020-10-12 09:29:41 -05:00
|
|
|
span: Span,
|
2020-11-28 15:48:05 -06:00
|
|
|
) -> CycleError {
|
2020-10-12 09:29:41 -05:00
|
|
|
// Find the waitee amongst `current_job` parents
|
2018-03-15 04:03:36 -05:00
|
|
|
let mut cycle = Vec::new();
|
2020-10-12 09:29:41 -05:00
|
|
|
let mut current_job = Option::clone(current_job);
|
2018-03-15 04:03:36 -05:00
|
|
|
|
|
|
|
while let Some(job) = current_job {
|
2020-01-30 21:00:03 -06:00
|
|
|
let info = query_map.get(&job).unwrap();
|
2021-09-01 13:07:33 -05:00
|
|
|
cycle.push(QueryInfo { span: info.job.span, query: info.query.clone() });
|
2018-03-15 04:03:36 -05:00
|
|
|
|
2021-05-01 17:11:51 -05:00
|
|
|
if job == *self {
|
2018-10-01 08:47:47 -05:00
|
|
|
cycle.reverse();
|
|
|
|
|
2018-04-13 15:20:10 -05:00
|
|
|
// This is the end of the cycle
|
|
|
|
// The span entry we included was for the usage
|
|
|
|
// of the cycle itself, and not part of the cycle
|
|
|
|
// Replace it with the span which caused the cycle to form
|
|
|
|
cycle[0].span = span;
|
|
|
|
// Find out why the cycle itself was used
|
2020-01-30 21:00:03 -06:00
|
|
|
let usage = info
|
|
|
|
.job
|
|
|
|
.parent
|
|
|
|
.as_ref()
|
2021-09-01 13:07:33 -05:00
|
|
|
.map(|parent| (info.job.span, parent.query(&query_map)));
|
2018-12-04 09:26:34 -06:00
|
|
|
return CycleError { usage, cycle };
|
2018-03-15 04:03:36 -05:00
|
|
|
}
|
|
|
|
|
2020-02-24 07:48:40 -06:00
|
|
|
current_job = info.job.parent;
|
2018-03-15 04:03:36 -05:00
|
|
|
}
|
|
|
|
|
2018-04-13 15:20:10 -05:00
|
|
|
panic!("did not find a cycle")
|
2018-03-15 04:03:36 -05:00
|
|
|
}
|
2018-04-06 05:56:59 -05:00
|
|
|
}
|
|
|
|
|
2019-01-28 08:51:47 -06:00
|
|
|
#[cfg(parallel_compiler)]
|
2022-02-07 10:03:51 -06:00
|
|
|
struct QueryWaiter {
|
|
|
|
query: Option<QueryJobId>,
|
2018-04-06 05:56:59 -05:00
|
|
|
condvar: Condvar,
|
|
|
|
span: Span,
|
2020-11-28 15:48:05 -06:00
|
|
|
cycle: Lock<Option<CycleError>>,
|
2018-04-06 05:56:59 -05:00
|
|
|
}
|
|
|
|
|
2019-01-28 08:51:47 -06:00
|
|
|
#[cfg(parallel_compiler)]
|
2022-02-07 10:03:51 -06:00
|
|
|
impl QueryWaiter {
|
2018-05-27 02:01:57 -05:00
|
|
|
fn notify(&self, registry: &rayon_core::Registry) {
|
2018-04-06 05:56:59 -05:00
|
|
|
rayon_core::mark_unblocked(registry);
|
|
|
|
self.condvar.notify_one();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-28 08:51:47 -06:00
|
|
|
#[cfg(parallel_compiler)]
|
2022-02-07 10:03:51 -06:00
|
|
|
struct QueryLatchInfo {
|
2018-04-06 05:56:59 -05:00
|
|
|
complete: bool,
|
2022-02-07 10:03:51 -06:00
|
|
|
waiters: Vec<Lrc<QueryWaiter>>,
|
2018-04-06 05:56:59 -05:00
|
|
|
}
|
|
|
|
|
2019-01-28 08:51:47 -06:00
|
|
|
#[cfg(parallel_compiler)]
|
2020-01-30 21:00:03 -06:00
|
|
|
#[derive(Clone)]
|
2022-02-07 10:03:51 -06:00
|
|
|
pub(super) struct QueryLatch {
|
|
|
|
info: Lrc<Mutex<QueryLatchInfo>>,
|
2018-04-06 05:56:59 -05:00
|
|
|
}
|
|
|
|
|
2019-01-28 08:51:47 -06:00
|
|
|
#[cfg(parallel_compiler)]
|
2022-02-07 10:03:51 -06:00
|
|
|
impl QueryLatch {
|
2018-04-06 05:56:59 -05:00
|
|
|
fn new() -> Self {
|
2020-01-30 21:00:03 -06:00
|
|
|
QueryLatch {
|
|
|
|
info: Lrc::new(Mutex::new(QueryLatchInfo { complete: false, waiters: Vec::new() })),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Awaits for the query job to complete.
|
2022-02-07 10:03:51 -06:00
|
|
|
pub(super) fn wait_on(&self, query: Option<QueryJobId>, span: Span) -> Result<(), CycleError> {
|
2020-03-25 01:52:12 -05:00
|
|
|
let waiter =
|
|
|
|
Lrc::new(QueryWaiter { query, span, cycle: Lock::new(None), condvar: Condvar::new() });
|
|
|
|
self.wait_on_inner(&waiter);
|
|
|
|
// FIXME: Get rid of this lock. We have ownership of the QueryWaiter
|
|
|
|
// although another thread may still have a Lrc reference so we cannot
|
|
|
|
// use Lrc::get_mut
|
|
|
|
let mut cycle = waiter.cycle.lock();
|
|
|
|
match cycle.take() {
|
|
|
|
None => Ok(()),
|
|
|
|
Some(cycle) => Err(cycle),
|
|
|
|
}
|
2018-04-06 05:56:59 -05:00
|
|
|
}
|
|
|
|
|
2018-05-27 06:05:41 -05:00
|
|
|
/// Awaits the caller on this latch by blocking the current thread.
|
2022-02-07 10:03:51 -06:00
|
|
|
fn wait_on_inner(&self, waiter: &Lrc<QueryWaiter>) {
|
2018-04-06 05:56:59 -05:00
|
|
|
let mut info = self.info.lock();
|
|
|
|
if !info.complete {
|
2018-05-27 06:05:41 -05:00
|
|
|
// We push the waiter on to the `waiters` list. It can be accessed inside
|
|
|
|
// the `wait` call below, by 1) the `set` method or 2) by deadlock detection.
|
|
|
|
// Both of these will remove it from the `waiters` list before resuming
|
|
|
|
// this thread.
|
2018-05-31 13:24:56 -05:00
|
|
|
info.waiters.push(waiter.clone());
|
2018-05-27 06:05:41 -05:00
|
|
|
|
2018-05-31 16:04:21 -05:00
|
|
|
// If this detects a deadlock and the deadlock handler wants to resume this thread
|
2018-04-06 05:56:59 -05:00
|
|
|
// we have to be in the `wait` call. This is ensured by the deadlock handler
|
|
|
|
// getting the self.info lock.
|
|
|
|
rayon_core::mark_blocked();
|
2018-12-18 02:03:38 -06:00
|
|
|
jobserver::release_thread();
|
2018-05-27 06:05:41 -05:00
|
|
|
waiter.condvar.wait(&mut info);
|
2018-12-18 02:03:38 -06:00
|
|
|
// Release the lock before we potentially block in `acquire_thread`
|
|
|
|
mem::drop(info);
|
|
|
|
jobserver::acquire_thread();
|
2018-04-06 05:56:59 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-27 06:05:41 -05:00
|
|
|
/// Sets the latch and resumes all waiters on it
|
2018-05-27 02:01:57 -05:00
|
|
|
fn set(&self) {
|
2018-04-06 05:56:59 -05:00
|
|
|
let mut info = self.info.lock();
|
|
|
|
debug_assert!(!info.complete);
|
|
|
|
info.complete = true;
|
|
|
|
let registry = rayon_core::Registry::current();
|
|
|
|
for waiter in info.waiters.drain(..) {
|
2018-05-31 13:24:56 -05:00
|
|
|
waiter.notify(®istry);
|
2018-04-06 05:56:59 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-08 07:53:55 -06:00
|
|
|
/// Removes a single waiter from the list of waiters.
|
2018-05-27 06:05:41 -05:00
|
|
|
/// This is used to break query cycles.
|
2022-02-07 10:03:51 -06:00
|
|
|
fn extract_waiter(&self, waiter: usize) -> Lrc<QueryWaiter> {
|
2018-04-06 05:56:59 -05:00
|
|
|
let mut info = self.info.lock();
|
|
|
|
debug_assert!(!info.complete);
|
|
|
|
// Remove the waiter from the list of waiters
|
2018-05-27 06:05:41 -05:00
|
|
|
info.waiters.remove(waiter)
|
2018-04-06 05:56:59 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-27 06:05:41 -05:00
|
|
|
/// A resumable waiter of a query. The usize is the index into waiters in the query's latch
|
2019-01-28 08:51:47 -06:00
|
|
|
#[cfg(parallel_compiler)]
|
2022-02-07 10:03:51 -06:00
|
|
|
type Waiter = (QueryJobId, usize);
|
2018-04-06 05:56:59 -05:00
|
|
|
|
2018-05-27 06:05:41 -05:00
|
|
|
/// Visits all the non-resumable and resumable waiters of a query.
|
|
|
|
/// Only waiters in a query are visited.
|
|
|
|
/// `visit` is called for every waiter and is passed a query waiting on `query_ref`
|
|
|
|
/// and a span indicating the reason the query waited on `query_ref`.
|
|
|
|
/// If `visit` returns Some, this function returns.
|
|
|
|
/// For visits of non-resumable waiters it returns the return value of `visit`.
|
|
|
|
/// For visits of resumable waiters it returns Some(Some(Waiter)) which has the
|
|
|
|
/// required information to resume the waiter.
|
|
|
|
/// If all `visit` calls returns None, this function also returns None.
|
2019-01-28 08:51:47 -06:00
|
|
|
#[cfg(parallel_compiler)]
|
2022-02-07 10:03:51 -06:00
|
|
|
fn visit_waiters<F>(query_map: &QueryMap, query: QueryJobId, mut visit: F) -> Option<Option<Waiter>>
|
2018-04-06 05:56:59 -05:00
|
|
|
where
|
2022-02-07 10:03:51 -06:00
|
|
|
F: FnMut(Span, QueryJobId) -> Option<Option<Waiter>>,
|
2018-04-06 05:56:59 -05:00
|
|
|
{
|
2018-05-27 06:05:41 -05:00
|
|
|
// Visit the parent query which is a non-resumable waiter since it's on the same stack
|
2020-01-30 21:00:03 -06:00
|
|
|
if let Some(parent) = query.parent(query_map) {
|
|
|
|
if let Some(cycle) = visit(query.span(query_map), parent) {
|
2018-04-06 05:56:59 -05:00
|
|
|
return Some(cycle);
|
|
|
|
}
|
|
|
|
}
|
2018-05-27 06:05:41 -05:00
|
|
|
|
2018-08-19 08:30:23 -05:00
|
|
|
// Visit the explicit waiters which use condvars and are resumable
|
2020-01-30 21:00:03 -06:00
|
|
|
if let Some(latch) = query.latch(query_map) {
|
|
|
|
for (i, waiter) in latch.info.lock().waiters.iter().enumerate() {
|
|
|
|
if let Some(waiter_query) = waiter.query {
|
|
|
|
if visit(waiter.span, waiter_query).is_some() {
|
|
|
|
// Return a value which indicates that this waiter can be resumed
|
|
|
|
return Some(Some((query, i)));
|
|
|
|
}
|
2018-04-06 05:56:59 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-01-30 21:00:03 -06:00
|
|
|
|
2018-04-06 05:56:59 -05:00
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2018-05-27 06:05:41 -05:00
|
|
|
/// Look for query cycles by doing a depth first search starting at `query`.
|
|
|
|
/// `span` is the reason for the `query` to execute. This is initially DUMMY_SP.
|
|
|
|
/// If a cycle is detected, this initial value is replaced with the span causing
|
|
|
|
/// the cycle.
|
2019-01-28 08:51:47 -06:00
|
|
|
#[cfg(parallel_compiler)]
|
2022-02-07 10:03:51 -06:00
|
|
|
fn cycle_check(
|
|
|
|
query_map: &QueryMap,
|
|
|
|
query: QueryJobId,
|
2018-04-06 05:56:59 -05:00
|
|
|
span: Span,
|
2022-02-07 10:03:51 -06:00
|
|
|
stack: &mut Vec<(Span, QueryJobId)>,
|
|
|
|
visited: &mut FxHashSet<QueryJobId>,
|
|
|
|
) -> Option<Option<Waiter>> {
|
2020-01-30 21:00:03 -06:00
|
|
|
if !visited.insert(query) {
|
|
|
|
return if let Some(p) = stack.iter().position(|q| q.1 == query) {
|
2018-05-27 06:05:41 -05:00
|
|
|
// We detected a query cycle, fix up the initial span and return Some
|
|
|
|
|
2018-04-06 05:56:59 -05:00
|
|
|
// Remove previous stack entries
|
2018-12-19 10:36:39 -06:00
|
|
|
stack.drain(0..p);
|
2018-04-06 05:56:59 -05:00
|
|
|
// Replace the span for the first query with the cycle cause
|
|
|
|
stack[0].0 = span;
|
|
|
|
Some(None)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-12-19 06:14:03 -06:00
|
|
|
// Query marked as visited is added it to the stack
|
2020-01-30 21:00:03 -06:00
|
|
|
stack.push((span, query));
|
2018-04-06 05:56:59 -05:00
|
|
|
|
2018-05-27 06:05:41 -05:00
|
|
|
// Visit all the waiters
|
2020-01-30 21:00:03 -06:00
|
|
|
let r = visit_waiters(query_map, query, |span, successor| {
|
|
|
|
cycle_check(query_map, successor, span, stack, visited)
|
|
|
|
});
|
2018-04-06 05:56:59 -05:00
|
|
|
|
2018-05-27 06:05:41 -05:00
|
|
|
// Remove the entry in our stack if we didn't find a cycle
|
2018-04-06 05:56:59 -05:00
|
|
|
if r.is_none() {
|
|
|
|
stack.pop();
|
|
|
|
}
|
|
|
|
|
|
|
|
r
|
|
|
|
}
|
|
|
|
|
2018-05-27 06:05:41 -05:00
|
|
|
/// Finds out if there's a path to the compiler root (aka. code which isn't in a query)
|
|
|
|
/// from `query` without going through any of the queries in `visited`.
|
|
|
|
/// This is achieved with a depth first search.
|
2019-01-28 08:51:47 -06:00
|
|
|
#[cfg(parallel_compiler)]
|
2022-02-07 10:03:51 -06:00
|
|
|
fn connected_to_root(
|
|
|
|
query_map: &QueryMap,
|
|
|
|
query: QueryJobId,
|
|
|
|
visited: &mut FxHashSet<QueryJobId>,
|
|
|
|
) -> bool {
|
2018-10-01 08:44:23 -05:00
|
|
|
// We already visited this or we're deliberately ignoring it
|
2020-01-30 21:00:03 -06:00
|
|
|
if !visited.insert(query) {
|
2018-10-01 08:44:23 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-12-01 20:01:12 -06:00
|
|
|
// This query is connected to the root (it has no query parent), return true
|
2020-01-30 21:00:03 -06:00
|
|
|
if query.parent(query_map).is_none() {
|
2018-12-01 20:01:12 -06:00
|
|
|
return true;
|
|
|
|
}
|
2018-04-06 05:56:59 -05:00
|
|
|
|
2020-01-30 21:00:03 -06:00
|
|
|
visit_waiters(query_map, query, |_, successor| {
|
|
|
|
connected_to_root(query_map, successor, visited).then_some(None)
|
|
|
|
})
|
|
|
|
.is_some()
|
2018-04-06 05:56:59 -05:00
|
|
|
}
|
|
|
|
|
2018-12-01 20:01:12 -06:00
|
|
|
// Deterministically pick an query from a list
|
2019-01-28 08:51:47 -06:00
|
|
|
#[cfg(parallel_compiler)]
|
2022-02-07 10:03:51 -06:00
|
|
|
fn pick_query<'a, T, F>(query_map: &QueryMap, queries: &'a [T], f: F) -> &'a T
|
2020-03-19 02:28:24 -05:00
|
|
|
where
|
2022-02-07 10:03:51 -06:00
|
|
|
F: Fn(&T) -> (Span, QueryJobId),
|
2020-03-19 02:28:24 -05:00
|
|
|
{
|
2018-12-01 20:01:12 -06:00
|
|
|
// Deterministically pick an entry point
|
|
|
|
// FIXME: Sort this instead
|
|
|
|
queries
|
|
|
|
.iter()
|
|
|
|
.min_by_key(|v| {
|
|
|
|
let (span, query) = f(v);
|
2021-02-09 11:53:38 -06:00
|
|
|
let hash = query.query(query_map).hash;
|
2018-12-01 20:01:12 -06:00
|
|
|
// Prefer entry points which have valid spans for nicer error messages
|
|
|
|
// We add an integer to the tuple ensuring that entry points
|
|
|
|
// with valid spans are picked first
|
|
|
|
let span_cmp = if span == DUMMY_SP { 1 } else { 0 };
|
2021-02-09 11:53:38 -06:00
|
|
|
(span_cmp, hash)
|
2018-12-01 20:01:12 -06:00
|
|
|
})
|
|
|
|
.unwrap()
|
|
|
|
}
|
|
|
|
|
2018-05-27 06:05:41 -05:00
|
|
|
/// Looks for query cycles starting from the last query in `jobs`.
|
|
|
|
/// If a cycle is found, all queries in the cycle is removed from `jobs` and
|
|
|
|
/// the function return true.
|
|
|
|
/// If a cycle was not found, the starting query is removed from `jobs` and
|
|
|
|
/// the function returns false.
|
2019-01-28 08:51:47 -06:00
|
|
|
#[cfg(parallel_compiler)]
|
2022-02-07 10:03:51 -06:00
|
|
|
fn remove_cycle(
|
|
|
|
query_map: &QueryMap,
|
|
|
|
jobs: &mut Vec<QueryJobId>,
|
|
|
|
wakelist: &mut Vec<Lrc<QueryWaiter>>,
|
2020-03-19 07:15:34 -05:00
|
|
|
) -> bool {
|
2018-08-18 05:55:43 -05:00
|
|
|
let mut visited = FxHashSet::default();
|
2018-04-06 05:56:59 -05:00
|
|
|
let mut stack = Vec::new();
|
2018-05-27 06:05:41 -05:00
|
|
|
// Look for a cycle starting with the last query in `jobs`
|
2020-01-30 21:00:03 -06:00
|
|
|
if let Some(waiter) =
|
|
|
|
cycle_check(query_map, jobs.pop().unwrap(), DUMMY_SP, &mut stack, &mut visited)
|
|
|
|
{
|
2018-12-19 06:08:17 -06:00
|
|
|
// The stack is a vector of pairs of spans and queries; reverse it so that
|
|
|
|
// the earlier entries require later entries
|
|
|
|
let (mut spans, queries): (Vec<_>, Vec<_>) = stack.into_iter().rev().unzip();
|
2018-04-06 05:56:59 -05:00
|
|
|
|
2018-05-27 06:05:41 -05:00
|
|
|
// Shift the spans so that queries are matched with the span for their waitee
|
2018-10-01 08:47:47 -05:00
|
|
|
spans.rotate_right(1);
|
2018-04-06 05:56:59 -05:00
|
|
|
|
2018-05-27 06:05:41 -05:00
|
|
|
// Zip them back together
|
2021-03-08 17:32:41 -06:00
|
|
|
let mut stack: Vec<_> = iter::zip(spans, queries).collect();
|
2018-04-06 05:56:59 -05:00
|
|
|
|
|
|
|
// Remove the queries in our cycle from the list of jobs to look at
|
|
|
|
for r in &stack {
|
2020-06-20 04:38:15 -05:00
|
|
|
if let Some(pos) = jobs.iter().position(|j| j == &r.1) {
|
|
|
|
jobs.remove(pos);
|
|
|
|
}
|
2018-04-06 05:56:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Find the queries in the cycle which are
|
|
|
|
// connected to queries outside the cycle
|
2018-12-19 06:19:48 -06:00
|
|
|
let entry_points = stack
|
|
|
|
.iter()
|
2020-01-30 21:00:03 -06:00
|
|
|
.filter_map(|&(span, query)| {
|
|
|
|
if query.parent(query_map).is_none() {
|
2018-12-01 20:01:12 -06:00
|
|
|
// This query is connected to the root (it has no query parent)
|
2020-01-30 21:00:03 -06:00
|
|
|
Some((span, query, None))
|
2018-12-01 20:01:12 -06:00
|
|
|
} else {
|
|
|
|
let mut waiters = Vec::new();
|
|
|
|
// Find all the direct waiters who lead to the root
|
2020-01-30 21:00:03 -06:00
|
|
|
visit_waiters(query_map, query, |span, waiter| {
|
2018-05-27 06:05:41 -05:00
|
|
|
// Mark all the other queries in the cycle as already visited
|
2020-01-30 21:00:03 -06:00
|
|
|
let mut visited = FxHashSet::from_iter(stack.iter().map(|q| q.1));
|
2019-12-22 16:42:04 -06:00
|
|
|
|
2020-01-30 21:00:03 -06:00
|
|
|
if connected_to_root(query_map, waiter, &mut visited) {
|
2018-12-01 20:01:12 -06:00
|
|
|
waiters.push((span, waiter));
|
2019-12-22 16:42:04 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
});
|
2018-12-01 20:01:12 -06:00
|
|
|
if waiters.is_empty() {
|
2019-12-22 16:42:04 -06:00
|
|
|
None
|
|
|
|
} else {
|
2018-12-01 20:01:12 -06:00
|
|
|
// Deterministically pick one of the waiters to show to the user
|
2021-02-09 11:53:38 -06:00
|
|
|
let waiter = *pick_query(query_map, &waiters, |s| *s);
|
2020-01-30 21:00:03 -06:00
|
|
|
Some((span, query, Some(waiter)))
|
2019-12-22 16:42:04 -06:00
|
|
|
}
|
2018-04-06 05:56:59 -05:00
|
|
|
}
|
2018-12-19 06:19:48 -06:00
|
|
|
})
|
2022-02-07 10:03:51 -06:00
|
|
|
.collect::<Vec<(Span, QueryJobId, Option<(Span, QueryJobId)>)>>();
|
2018-04-06 05:56:59 -05:00
|
|
|
|
|
|
|
// Deterministically pick an entry point
|
2021-02-09 11:53:38 -06:00
|
|
|
let (_, entry_point, usage) = pick_query(query_map, &entry_points, |e| (e.0, e.1));
|
2018-04-06 05:56:59 -05:00
|
|
|
|
2018-10-01 08:47:47 -05:00
|
|
|
// Shift the stack so that our entry point is first
|
2020-01-30 21:00:03 -06:00
|
|
|
let entry_point_pos = stack.iter().position(|(_, query)| query == entry_point);
|
2018-10-01 08:47:47 -05:00
|
|
|
if let Some(pos) = entry_point_pos {
|
2018-12-01 20:01:12 -06:00
|
|
|
stack.rotate_left(pos);
|
2018-04-06 05:56:59 -05:00
|
|
|
}
|
|
|
|
|
2020-01-30 21:00:03 -06:00
|
|
|
let usage = usage.as_ref().map(|(span, query)| (*span, query.query(query_map)));
|
2018-12-01 20:01:12 -06:00
|
|
|
|
2018-05-27 06:05:41 -05:00
|
|
|
// Create the cycle error
|
2019-07-24 03:44:14 -05:00
|
|
|
let error = CycleError {
|
2018-12-01 20:01:12 -06:00
|
|
|
usage,
|
2018-06-05 16:12:19 -05:00
|
|
|
cycle: stack
|
|
|
|
.iter()
|
2020-01-30 21:00:03 -06:00
|
|
|
.map(|&(s, ref q)| QueryInfo { span: s, query: q.query(query_map) })
|
2018-04-06 05:56:59 -05:00
|
|
|
.collect(),
|
|
|
|
};
|
|
|
|
|
2018-05-27 06:05:41 -05:00
|
|
|
// We unwrap `waiter` here since there must always be one
|
2020-11-21 02:06:45 -06:00
|
|
|
// edge which is resumable / waited using a query latch
|
2018-05-27 06:05:41 -05:00
|
|
|
let (waitee_query, waiter_idx) = waiter.unwrap();
|
|
|
|
|
|
|
|
// Extract the waiter we want to resume
|
2020-01-30 21:00:03 -06:00
|
|
|
let waiter = waitee_query.latch(query_map).unwrap().extract_waiter(waiter_idx);
|
2018-05-27 06:05:41 -05:00
|
|
|
|
2018-05-31 13:24:56 -05:00
|
|
|
// Set the cycle error so it will be picked up when resumed
|
2018-06-05 16:12:19 -05:00
|
|
|
*waiter.cycle.lock() = Some(error);
|
2018-05-27 06:05:41 -05:00
|
|
|
|
|
|
|
// Put the waiter on the list of things to resume
|
|
|
|
wakelist.push(waiter);
|
|
|
|
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
2018-04-06 05:56:59 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-27 06:05:41 -05:00
|
|
|
/// Detects query cycles by using depth first search over all active query jobs.
|
|
|
|
/// If a query cycle is found it will break the cycle by finding an edge which
|
|
|
|
/// uses a query latch and then resuming that waiter.
|
|
|
|
/// There may be multiple cycles involved in a deadlock, so this searches
|
|
|
|
/// all active queries for cycles before finally resuming all the waiters at once.
|
2019-01-28 08:51:47 -06:00
|
|
|
#[cfg(parallel_compiler)]
|
2022-06-28 21:02:30 -05:00
|
|
|
pub fn deadlock(query_map: QueryMap, registry: &rayon_core::Registry) {
|
2018-04-06 05:56:59 -05:00
|
|
|
let on_panic = OnDrop(|| {
|
|
|
|
eprintln!("deadlock handler panicked, aborting process");
|
|
|
|
process::abort();
|
|
|
|
});
|
|
|
|
|
|
|
|
let mut wakelist = Vec::new();
|
2022-02-07 10:03:51 -06:00
|
|
|
let mut jobs: Vec<QueryJobId> = query_map.keys().cloned().collect();
|
2018-04-06 05:56:59 -05:00
|
|
|
|
2018-05-27 06:05:41 -05:00
|
|
|
let mut found_cycle = false;
|
|
|
|
|
2018-04-06 05:56:59 -05:00
|
|
|
while jobs.len() > 0 {
|
2021-02-09 11:53:38 -06:00
|
|
|
if remove_cycle(&query_map, &mut jobs, &mut wakelist) {
|
2018-05-27 06:05:41 -05:00
|
|
|
found_cycle = true;
|
|
|
|
}
|
2018-04-06 05:56:59 -05:00
|
|
|
}
|
|
|
|
|
2018-05-27 06:05:41 -05:00
|
|
|
// Check that a cycle was found. It is possible for a deadlock to occur without
|
|
|
|
// a query cycle if a query which can be waited on uses Rayon to do multithreading
|
|
|
|
// internally. Such a query (X) may be executing on 2 threads (A and B) and A may
|
|
|
|
// wait using Rayon on B. Rayon may then switch to executing another query (Y)
|
|
|
|
// which in turn will wait on X causing a deadlock. We have a false dependency from
|
|
|
|
// X to Y due to Rayon waiting and a true dependency from Y to X. The algorithm here
|
|
|
|
// only considers the true dependency and won't detect a cycle.
|
|
|
|
assert!(found_cycle);
|
2018-04-06 05:56:59 -05:00
|
|
|
|
|
|
|
// FIXME: Ensure this won't cause a deadlock before we return
|
|
|
|
for waiter in wakelist.into_iter() {
|
2018-05-31 13:24:56 -05:00
|
|
|
waiter.notify(registry);
|
2018-04-06 05:56:59 -05:00
|
|
|
}
|
|
|
|
|
2018-05-27 00:47:44 -05:00
|
|
|
on_panic.disable();
|
2018-03-15 04:03:36 -05:00
|
|
|
}
|
2020-11-28 17:21:56 -06:00
|
|
|
|
|
|
|
#[inline(never)]
|
|
|
|
#[cold]
|
|
|
|
pub(crate) fn report_cycle<'a>(
|
|
|
|
sess: &'a Session,
|
|
|
|
CycleError { usage, cycle: stack }: CycleError,
|
2022-01-23 12:34:26 -06:00
|
|
|
) -> DiagnosticBuilder<'a, ErrorGuaranteed> {
|
2020-11-28 17:21:56 -06:00
|
|
|
assert!(!stack.is_empty());
|
|
|
|
|
2022-07-29 15:11:23 -05:00
|
|
|
let span = stack[0].query.default_span(stack[1 % stack.len()].span);
|
2020-11-28 17:21:56 -06:00
|
|
|
let mut err =
|
|
|
|
struct_span_err!(sess, span, E0391, "cycle detected when {}", stack[0].query.description);
|
|
|
|
|
|
|
|
for i in 1..stack.len() {
|
|
|
|
let query = &stack[i].query;
|
2022-07-29 15:11:23 -05:00
|
|
|
let span = query.default_span(stack[(i + 1) % stack.len()].span);
|
2020-11-28 17:21:56 -06:00
|
|
|
err.span_note(span, &format!("...which requires {}...", query.description));
|
|
|
|
}
|
|
|
|
|
2021-07-20 20:06:39 -05:00
|
|
|
if stack.len() == 1 {
|
|
|
|
err.note(&format!("...which immediately requires {} again", stack[0].query.description));
|
|
|
|
} else {
|
|
|
|
err.note(&format!(
|
|
|
|
"...which again requires {}, completing the cycle",
|
|
|
|
stack[0].query.description
|
|
|
|
));
|
|
|
|
}
|
2020-11-28 17:21:56 -06:00
|
|
|
|
2021-08-21 16:19:59 -05:00
|
|
|
if stack.iter().all(|entry| {
|
2022-02-16 17:06:50 -06:00
|
|
|
entry
|
|
|
|
.query
|
|
|
|
.def_kind
|
|
|
|
.map_or(false, |def_kind| matches!(def_kind, DefKind::TyAlias | DefKind::TraitAlias))
|
2021-08-21 16:19:59 -05:00
|
|
|
}) {
|
|
|
|
if stack.iter().all(|entry| {
|
2022-02-16 17:06:50 -06:00
|
|
|
entry.query.def_kind.map_or(false, |def_kind| matches!(def_kind, DefKind::TyAlias))
|
2021-08-21 16:19:59 -05:00
|
|
|
}) {
|
|
|
|
err.note("type aliases cannot be recursive");
|
|
|
|
err.help("consider using a struct, enum, or union instead to break the cycle");
|
|
|
|
err.help("see <https://doc.rust-lang.org/reference/types.html#recursive-types> for more information");
|
|
|
|
} else {
|
|
|
|
err.note("trait aliases cannot be recursive");
|
|
|
|
}
|
2021-08-19 22:30:33 -05:00
|
|
|
}
|
|
|
|
|
2020-11-28 17:21:56 -06:00
|
|
|
if let Some((span, query)) = usage {
|
2022-07-29 15:11:23 -05:00
|
|
|
err.span_note(query.default_span(span), &format!("cycle used when {}", query.description));
|
2020-11-28 17:21:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
err
|
|
|
|
}
|
2020-11-28 17:39:34 -06:00
|
|
|
|
|
|
|
pub fn print_query_stack<CTX: QueryContext>(
|
|
|
|
tcx: CTX,
|
2022-02-07 10:03:51 -06:00
|
|
|
mut current_query: Option<QueryJobId>,
|
2020-11-28 17:39:34 -06:00
|
|
|
handler: &Handler,
|
|
|
|
num_frames: Option<usize>,
|
|
|
|
) -> usize {
|
|
|
|
// Be careful relying on global state here: this code is called from
|
|
|
|
// a panic hook, which means that the global `Handler` may be in a weird
|
|
|
|
// state if it was responsible for triggering the panic.
|
|
|
|
let mut i = 0;
|
|
|
|
let query_map = tcx.try_collect_active_jobs();
|
|
|
|
|
|
|
|
while let Some(query) = current_query {
|
|
|
|
if Some(i) == num_frames {
|
|
|
|
break;
|
|
|
|
}
|
2021-10-15 20:45:14 -05:00
|
|
|
let Some(query_info) = query_map.as_ref().and_then(|map| map.get(&query)) else {
|
2020-11-28 17:39:34 -06:00
|
|
|
break;
|
|
|
|
};
|
|
|
|
let mut diag = Diagnostic::new(
|
|
|
|
Level::FailureNote,
|
2021-09-01 13:07:33 -05:00
|
|
|
&format!("#{} [{}] {}", i, query_info.query.name, query_info.query.description),
|
2020-11-28 17:39:34 -06:00
|
|
|
);
|
2022-07-23 07:42:54 -05:00
|
|
|
diag.span = query_info.job.span.into();
|
2020-11-28 17:39:34 -06:00
|
|
|
handler.force_print_diagnostic(diag);
|
|
|
|
|
|
|
|
current_query = query_info.job.parent;
|
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
i
|
|
|
|
}
|