2018-03-15 04:03:36 -05:00
|
|
|
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2018-04-06 05:56:59 -05:00
|
|
|
#![allow(warnings)]
|
|
|
|
|
|
|
|
use std::mem;
|
2018-08-18 05:55:43 -05:00
|
|
|
use rustc_data_structures::fx::FxHashSet;
|
2018-04-06 05:56:59 -05:00
|
|
|
use rustc_data_structures::sync::{Lock, LockGuard, Lrc, Weak};
|
|
|
|
use rustc_data_structures::OnDrop;
|
2018-03-15 04:03:36 -05:00
|
|
|
use syntax_pos::Span;
|
|
|
|
use ty::tls;
|
2018-06-13 08:44:43 -05:00
|
|
|
use ty::query::Query;
|
|
|
|
use ty::query::plumbing::CycleError;
|
2018-03-15 04:03:36 -05:00
|
|
|
use ty::context::TyCtxt;
|
|
|
|
use errors::Diagnostic;
|
2018-04-06 05:56:59 -05:00
|
|
|
use std::process;
|
2018-07-18 13:53:54 -05:00
|
|
|
use std::{fmt, ptr};
|
2018-08-18 05:55:43 -05:00
|
|
|
|
2018-04-06 05:56:59 -05:00
|
|
|
#[cfg(parallel_queries)]
|
|
|
|
use {
|
|
|
|
rayon_core,
|
|
|
|
parking_lot::{Mutex, Condvar},
|
|
|
|
std::sync::atomic::Ordering,
|
|
|
|
std::thread,
|
|
|
|
std::iter,
|
|
|
|
std::iter::FromIterator,
|
|
|
|
syntax_pos::DUMMY_SP,
|
|
|
|
rustc_data_structures::stable_hasher::{StableHasherResult, StableHasher, HashStable},
|
|
|
|
};
|
2018-03-15 04:03:36 -05:00
|
|
|
|
2018-03-24 00:19:20 -05:00
|
|
|
/// Indicates the state of a query for a given key in a query map
|
2018-04-20 02:47:26 -05:00
|
|
|
pub(super) enum QueryResult<'tcx> {
|
2018-03-24 00:19:20 -05:00
|
|
|
/// An already executing query. The query job can be used to await for its completion
|
|
|
|
Started(Lrc<QueryJob<'tcx>>),
|
|
|
|
|
|
|
|
/// The query panicked. Queries trying to wait on this will raise a fatal error / silently panic
|
|
|
|
Poisoned,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A span and a query key
|
2018-03-15 04:03:36 -05:00
|
|
|
#[derive(Clone, Debug)]
|
2018-03-24 00:19:20 -05:00
|
|
|
pub struct QueryInfo<'tcx> {
|
2018-04-13 15:20:10 -05:00
|
|
|
/// The span for a reason this query was required
|
2018-03-15 04:03:36 -05:00
|
|
|
pub span: Span,
|
|
|
|
pub query: Query<'tcx>,
|
|
|
|
}
|
|
|
|
|
2018-03-24 00:19:20 -05:00
|
|
|
/// A object representing an active query job.
|
2018-03-15 04:03:36 -05:00
|
|
|
pub struct QueryJob<'tcx> {
|
2018-03-24 00:19:20 -05:00
|
|
|
pub info: QueryInfo<'tcx>,
|
|
|
|
|
|
|
|
/// The parent query job which created this job and is implicitly waiting on it.
|
2018-03-15 04:03:36 -05:00
|
|
|
pub parent: Option<Lrc<QueryJob<'tcx>>>,
|
2018-03-24 00:19:20 -05:00
|
|
|
|
|
|
|
/// Diagnostic messages which are emitted while the query executes
|
2018-03-15 04:03:36 -05:00
|
|
|
pub diagnostics: Lock<Vec<Diagnostic>>,
|
2018-04-06 05:56:59 -05:00
|
|
|
|
2018-05-31 16:04:21 -05:00
|
|
|
/// The latch which is used to wait on this job
|
2018-04-06 05:56:59 -05:00
|
|
|
#[cfg(parallel_queries)]
|
2018-05-27 02:01:57 -05:00
|
|
|
latch: QueryLatch<'tcx>,
|
2018-03-15 04:03:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> QueryJob<'tcx> {
|
2018-03-24 00:19:20 -05:00
|
|
|
/// Creates a new query job
|
|
|
|
pub fn new(info: QueryInfo<'tcx>, parent: Option<Lrc<QueryJob<'tcx>>>) -> Self {
|
2018-03-15 04:03:36 -05:00
|
|
|
QueryJob {
|
|
|
|
diagnostics: Lock::new(Vec::new()),
|
2018-03-24 00:19:20 -05:00
|
|
|
info,
|
2018-03-15 04:03:36 -05:00
|
|
|
parent,
|
2018-04-06 05:56:59 -05:00
|
|
|
#[cfg(parallel_queries)]
|
|
|
|
latch: QueryLatch::new(),
|
2018-03-15 04:03:36 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-24 00:19:20 -05:00
|
|
|
/// Awaits for the query job to complete.
|
|
|
|
///
|
|
|
|
/// For single threaded rustc there's no concurrent jobs running, so if we are waiting for any
|
|
|
|
/// query that means that there is a query cycle, thus this always running a cycle error.
|
2018-03-15 04:03:36 -05:00
|
|
|
pub(super) fn await<'lcx>(
|
|
|
|
&self,
|
|
|
|
tcx: TyCtxt<'_, 'tcx, 'lcx>,
|
|
|
|
span: Span,
|
2018-04-06 05:56:59 -05:00
|
|
|
) -> Result<(), CycleError<'tcx>> {
|
|
|
|
#[cfg(not(parallel_queries))]
|
|
|
|
{
|
|
|
|
self.find_cycle_in_stack(tcx, span)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(parallel_queries)]
|
|
|
|
{
|
|
|
|
tls::with_related_context(tcx, move |icx| {
|
2018-05-31 13:24:56 -05:00
|
|
|
let mut waiter = Lrc::new(QueryWaiter {
|
|
|
|
query: icx.query.clone(),
|
2018-04-06 05:56:59 -05:00
|
|
|
span,
|
2018-05-31 13:24:56 -05:00
|
|
|
cycle: Lock::new(None),
|
2018-04-06 05:56:59 -05:00
|
|
|
condvar: Condvar::new(),
|
2018-05-31 13:24:56 -05:00
|
|
|
});
|
|
|
|
self.latch.await(&waiter);
|
2018-12-02 14:57:47 -06:00
|
|
|
// 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() {
|
2018-04-06 05:56:59 -05:00
|
|
|
None => Ok(()),
|
|
|
|
Some(cycle) => Err(cycle)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(parallel_queries))]
|
|
|
|
fn find_cycle_in_stack<'lcx>(
|
|
|
|
&self,
|
|
|
|
tcx: TyCtxt<'_, 'tcx, 'lcx>,
|
|
|
|
span: Span,
|
2018-03-15 04:03:36 -05:00
|
|
|
) -> Result<(), CycleError<'tcx>> {
|
2018-03-24 00:19:20 -05:00
|
|
|
// Get the current executing query (waiter) and find the waitee amongst its parents
|
2018-03-15 04:03:36 -05:00
|
|
|
let mut current_job = tls::with_related_context(tcx, |icx| icx.query.clone());
|
|
|
|
let mut cycle = Vec::new();
|
|
|
|
|
|
|
|
while let Some(job) = current_job {
|
2018-10-01 08:47:47 -05:00
|
|
|
cycle.push(job.info.clone());
|
2018-03-15 04:03:36 -05:00
|
|
|
|
2018-07-18 13:53:54 -05:00
|
|
|
if ptr::eq(&*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
|
|
|
|
let usage = job.parent.as_ref().map(|parent| {
|
|
|
|
(job.info.span, parent.info.query.clone())
|
|
|
|
});
|
|
|
|
return Err(CycleError { usage, cycle });
|
2018-03-15 04:03:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
current_job = job.parent.clone();
|
|
|
|
}
|
|
|
|
|
2018-04-13 15:20:10 -05:00
|
|
|
panic!("did not find a cycle")
|
2018-03-15 04:03:36 -05:00
|
|
|
}
|
|
|
|
|
2018-03-24 00:19:20 -05: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
|
2018-05-27 02:01:57 -05:00
|
|
|
pub fn signal_complete(&self) {
|
2018-04-06 05:56:59 -05:00
|
|
|
#[cfg(parallel_queries)]
|
2018-05-27 02:01:57 -05:00
|
|
|
self.latch.set();
|
2018-04-06 05:56:59 -05:00
|
|
|
}
|
2018-06-05 16:12:19 -05:00
|
|
|
|
|
|
|
fn as_ptr(&self) -> *const QueryJob<'tcx> {
|
|
|
|
self as *const _
|
|
|
|
}
|
2018-04-06 05:56:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(parallel_queries)]
|
2018-05-27 02:01:57 -05:00
|
|
|
struct QueryWaiter<'tcx> {
|
2018-05-31 13:24:56 -05:00
|
|
|
query: Option<Lrc<QueryJob<'tcx>>>,
|
2018-04-06 05:56:59 -05:00
|
|
|
condvar: Condvar,
|
|
|
|
span: Span,
|
2018-05-31 13:24:56 -05:00
|
|
|
cycle: Lock<Option<CycleError<'tcx>>>,
|
2018-04-06 05:56:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(parallel_queries)]
|
2018-05-27 02:01:57 -05:00
|
|
|
impl<'tcx> QueryWaiter<'tcx> {
|
|
|
|
fn notify(&self, registry: &rayon_core::Registry) {
|
2018-04-06 05:56:59 -05:00
|
|
|
rayon_core::mark_unblocked(registry);
|
|
|
|
self.condvar.notify_one();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(parallel_queries)]
|
2018-05-27 02:01:57 -05:00
|
|
|
struct QueryLatchInfo<'tcx> {
|
2018-04-06 05:56:59 -05:00
|
|
|
complete: bool,
|
2018-05-31 13:24:56 -05:00
|
|
|
waiters: Vec<Lrc<QueryWaiter<'tcx>>>,
|
2018-04-06 05:56:59 -05:00
|
|
|
}
|
|
|
|
|
2018-05-27 02:01:57 -05:00
|
|
|
#[cfg(parallel_queries)]
|
|
|
|
struct QueryLatch<'tcx> {
|
|
|
|
info: Mutex<QueryLatchInfo<'tcx>>,
|
2018-04-06 05:56:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(parallel_queries)]
|
2018-05-27 02:01:57 -05:00
|
|
|
impl<'tcx> QueryLatch<'tcx> {
|
2018-04-06 05:56:59 -05:00
|
|
|
fn new() -> Self {
|
|
|
|
QueryLatch {
|
|
|
|
info: Mutex::new(QueryLatchInfo {
|
|
|
|
complete: false,
|
|
|
|
waiters: Vec::new(),
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-27 06:05:41 -05:00
|
|
|
/// Awaits the caller on this latch by blocking the current thread.
|
2018-05-31 13:24:56 -05:00
|
|
|
fn await(&self, waiter: &Lrc<QueryWaiter<'tcx>>) {
|
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-05-27 06:05:41 -05:00
|
|
|
waiter.condvar.wait(&mut info);
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-27 06:05:41 -05:00
|
|
|
/// Remove a single waiter from the list of waiters.
|
|
|
|
/// This is used to break query cycles.
|
|
|
|
fn extract_waiter(
|
2018-04-06 05:56:59 -05:00
|
|
|
&self,
|
|
|
|
waiter: usize,
|
2018-05-31 13:24:56 -05:00
|
|
|
) -> Lrc<QueryWaiter<'tcx>> {
|
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
|
2018-04-06 05:56:59 -05:00
|
|
|
#[cfg(parallel_queries)]
|
2018-06-05 16:12:19 -05:00
|
|
|
type Waiter<'tcx> = (Lrc<QueryJob<'tcx>>, 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.
|
2018-04-06 05:56:59 -05:00
|
|
|
#[cfg(parallel_queries)]
|
2018-06-05 16:12:19 -05:00
|
|
|
fn visit_waiters<'tcx, F>(query: Lrc<QueryJob<'tcx>>, mut visit: F) -> Option<Option<Waiter<'tcx>>>
|
2018-04-06 05:56:59 -05:00
|
|
|
where
|
2018-06-05 16:12:19 -05:00
|
|
|
F: FnMut(Span, Lrc<QueryJob<'tcx>>) -> Option<Option<Waiter<'tcx>>>
|
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
|
2018-04-06 05:56:59 -05:00
|
|
|
if let Some(ref parent) = query.parent {
|
2018-06-05 16:12:19 -05:00
|
|
|
if let Some(cycle) = visit(query.info.span, parent.clone()) {
|
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
|
2018-05-31 13:24:56 -05:00
|
|
|
for (i, waiter) in query.latch.info.lock().waiters.iter().enumerate() {
|
|
|
|
if let Some(ref waiter_query) = waiter.query {
|
2018-06-05 16:12:19 -05:00
|
|
|
if visit(waiter.span, waiter_query.clone()).is_some() {
|
2018-05-31 13:24:56 -05:00
|
|
|
// Return a value which indicates that this waiter can be resumed
|
2018-06-05 16:12:19 -05:00
|
|
|
return Some(Some((query.clone(), i)));
|
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.
|
2018-04-06 05:56:59 -05:00
|
|
|
#[cfg(parallel_queries)]
|
2018-06-05 16:12:19 -05:00
|
|
|
fn cycle_check<'tcx>(query: Lrc<QueryJob<'tcx>>,
|
2018-04-06 05:56:59 -05:00
|
|
|
span: Span,
|
2018-06-05 16:12:19 -05:00
|
|
|
stack: &mut Vec<(Span, Lrc<QueryJob<'tcx>>)>,
|
2018-08-18 05:55:43 -05:00
|
|
|
visited: &mut FxHashSet<*const QueryJob<'tcx>>
|
2018-06-05 16:12:19 -05:00
|
|
|
) -> Option<Option<Waiter<'tcx>>> {
|
|
|
|
if visited.contains(&query.as_ptr()) {
|
|
|
|
return if let Some(p) = stack.iter().position(|q| q.1.as_ptr() == query.as_ptr()) {
|
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
|
|
|
|
stack.splice(0..p, iter::empty());
|
|
|
|
// Replace the span for the first query with the cycle cause
|
|
|
|
stack[0].0 = span;
|
|
|
|
Some(None)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-27 06:05:41 -05:00
|
|
|
// Mark this query is visited and add it to the stack
|
2018-06-05 16:12:19 -05:00
|
|
|
visited.insert(query.as_ptr());
|
|
|
|
stack.push((span, query.clone()));
|
2018-04-06 05:56:59 -05:00
|
|
|
|
2018-05-27 06:05:41 -05:00
|
|
|
// Visit all the waiters
|
2018-04-06 05:56:59 -05:00
|
|
|
let r = visit_waiters(query, |span, successor| {
|
|
|
|
cycle_check(successor, span, stack, visited)
|
|
|
|
});
|
|
|
|
|
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.
|
2018-04-06 05:56:59 -05:00
|
|
|
#[cfg(parallel_queries)]
|
2018-06-05 16:12:19 -05:00
|
|
|
fn connected_to_root<'tcx>(
|
|
|
|
query: Lrc<QueryJob<'tcx>>,
|
2018-08-18 05:55:43 -05:00
|
|
|
visited: &mut FxHashSet<*const QueryJob<'tcx>>
|
2018-06-05 16:12:19 -05:00
|
|
|
) -> bool {
|
2018-10-01 08:44:23 -05:00
|
|
|
// We already visited this or we're deliberately ignoring it
|
|
|
|
if visited.contains(&query.as_ptr()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-12-01 20:01:12 -06:00
|
|
|
// This query is connected to the root (it has no query parent), return true
|
|
|
|
if query.parent.is_none() {
|
|
|
|
return true;
|
|
|
|
}
|
2018-04-06 05:56:59 -05:00
|
|
|
|
2018-12-01 20:01:12 -06:00
|
|
|
visited.insert(query.as_ptr());
|
2018-04-06 05:56:59 -05:00
|
|
|
|
|
|
|
visit_waiters(query, |_, successor| {
|
|
|
|
if connected_to_root(successor, visited) {
|
|
|
|
Some(None)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}).is_some()
|
|
|
|
}
|
|
|
|
|
2018-12-01 20:01:12 -06:00
|
|
|
// Deterministically pick an query from a list
|
|
|
|
#[cfg(parallel_queries)]
|
|
|
|
fn pick_query<'a, 'tcx, T, F: Fn(&T) -> (Span, Lrc<QueryJob<'tcx>>)>(
|
|
|
|
tcx: TyCtxt<'_, 'tcx, '_>,
|
|
|
|
queries: &'a [T],
|
|
|
|
f: F
|
|
|
|
) -> &'a T {
|
|
|
|
// Deterministically pick an entry point
|
|
|
|
// FIXME: Sort this instead
|
|
|
|
let mut hcx = tcx.create_stable_hashing_context();
|
|
|
|
queries.iter().min_by_key(|v| {
|
|
|
|
let (span, query) = f(v);
|
|
|
|
let mut stable_hasher = StableHasher::<u64>::new();
|
|
|
|
query.info.query.hash_stable(&mut hcx, &mut stable_hasher);
|
|
|
|
// 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 };
|
|
|
|
(span_cmp, stable_hasher.finish())
|
|
|
|
}).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.
|
2018-04-06 05:56:59 -05:00
|
|
|
#[cfg(parallel_queries)]
|
|
|
|
fn remove_cycle<'tcx>(
|
2018-06-05 16:12:19 -05:00
|
|
|
jobs: &mut Vec<Lrc<QueryJob<'tcx>>>,
|
2018-05-31 13:24:56 -05:00
|
|
|
wakelist: &mut Vec<Lrc<QueryWaiter<'tcx>>>,
|
2018-04-06 05:56:59 -05:00
|
|
|
tcx: TyCtxt<'_, 'tcx, '_>
|
2018-05-27 06:05:41 -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`
|
2018-04-06 05:56:59 -05:00
|
|
|
if let Some(waiter) = cycle_check(jobs.pop().unwrap(),
|
|
|
|
DUMMY_SP,
|
|
|
|
&mut stack,
|
|
|
|
&mut visited) {
|
|
|
|
// Reverse the stack so earlier entries require later entries
|
|
|
|
stack.reverse();
|
|
|
|
|
2018-10-01 08:47:47 -05:00
|
|
|
// The stack is a vector of pairs of spans and queries
|
|
|
|
let (mut spans, queries): (Vec<_>, Vec<_>) = stack.into_iter().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
|
2018-04-06 05:56:59 -05:00
|
|
|
let mut stack: Vec<_> = spans.into_iter().zip(queries).collect();
|
|
|
|
|
|
|
|
// Remove the queries in our cycle from the list of jobs to look at
|
|
|
|
for r in &stack {
|
2018-06-05 16:12:19 -05:00
|
|
|
if let Some(pos) = jobs.iter().position(|j| j.as_ptr() == r.1.as_ptr()) {
|
|
|
|
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-01 20:01:12 -06:00
|
|
|
let entry_points: Vec<_> = stack.iter().filter_map(|(span, query)| {
|
|
|
|
if query.parent.is_none() {
|
|
|
|
// This query is connected to the root (it has no query parent)
|
|
|
|
Some((*span, query.clone(), None))
|
|
|
|
} else {
|
|
|
|
let mut waiters = Vec::new();
|
|
|
|
// Find all the direct waiters who lead to the root
|
|
|
|
visit_waiters(query.clone(), |span, waiter| {
|
|
|
|
// Mark all the other queries in the cycle as already visited
|
|
|
|
let mut visited = FxHashSet::from_iter(stack.iter().map(|q| q.1.as_ptr()));
|
|
|
|
|
|
|
|
if connected_to_root(waiter.clone(), &mut visited) {
|
|
|
|
waiters.push((span, waiter));
|
|
|
|
}
|
|
|
|
|
2018-04-06 05:56:59 -05:00
|
|
|
None
|
2018-12-01 20:01:12 -06:00
|
|
|
});
|
|
|
|
if waiters.is_empty() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
// Deterministically pick one of the waiters to show to the user
|
|
|
|
let waiter = pick_query(tcx, &waiters, |s| s.clone()).clone();
|
|
|
|
Some((*span, query.clone(), Some(waiter)))
|
2018-04-06 05:56:59 -05:00
|
|
|
}
|
|
|
|
}
|
2018-12-01 20:01:12 -06:00
|
|
|
}).collect();
|
|
|
|
|
|
|
|
let entry_points: Vec<(Span, Lrc<QueryJob<'tcx>>, Option<(Span, Lrc<QueryJob<'tcx>>)>)>
|
|
|
|
= entry_points;
|
2018-04-06 05:56:59 -05:00
|
|
|
|
|
|
|
// Deterministically pick an entry point
|
2018-12-01 20:01:12 -06:00
|
|
|
let (_, entry_point, usage) = pick_query(tcx, &entry_points, |e| (e.0, e.1.clone()));
|
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
|
2018-12-01 20:01:12 -06:00
|
|
|
let entry_point_pos = stack.iter().position(|(_, query)| {
|
|
|
|
query.as_ptr() == entry_point.as_ptr()
|
|
|
|
});
|
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
|
|
|
}
|
|
|
|
|
2018-12-01 20:01:12 -06:00
|
|
|
let usage = usage.as_ref().map(|(span, query)| (*span, query.info.query.clone()));
|
|
|
|
|
2018-05-27 06:05:41 -05:00
|
|
|
// Create the cycle error
|
2018-04-06 05:56:59 -05:00
|
|
|
let mut error = CycleError {
|
2018-12-01 20:01:12 -06:00
|
|
|
usage,
|
2018-06-05 16:12:19 -05:00
|
|
|
cycle: stack.iter().map(|&(s, ref q)| QueryInfo {
|
2018-04-06 05:56:59 -05:00
|
|
|
span: s,
|
2018-06-05 16:12:19 -05:00
|
|
|
query: q.info.query.clone(),
|
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
|
|
|
|
// edge which is resumeable / waited using a query latch
|
|
|
|
let (waitee_query, waiter_idx) = waiter.unwrap();
|
|
|
|
|
|
|
|
// Extract the waiter we want to resume
|
|
|
|
let waiter = waitee_query.latch.extract_waiter(waiter_idx);
|
|
|
|
|
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
|
|
|
/// Creates a new thread and forwards information in thread locals to it.
|
|
|
|
/// The new thread runs the deadlock handler.
|
2018-06-05 16:12:19 -05:00
|
|
|
/// Must only be called when a deadlock is about to happen.
|
2018-04-06 05:56:59 -05:00
|
|
|
#[cfg(parallel_queries)]
|
2018-06-05 16:12:19 -05:00
|
|
|
pub unsafe fn handle_deadlock() {
|
2018-04-06 05:56:59 -05:00
|
|
|
use syntax;
|
|
|
|
use syntax_pos;
|
|
|
|
|
|
|
|
let registry = rayon_core::Registry::current();
|
|
|
|
|
|
|
|
let gcx_ptr = tls::GCX_PTR.with(|gcx_ptr| {
|
|
|
|
gcx_ptr as *const _
|
|
|
|
});
|
2018-06-05 16:12:19 -05:00
|
|
|
let gcx_ptr = &*gcx_ptr;
|
2018-04-06 05:56:59 -05:00
|
|
|
|
|
|
|
let syntax_globals = syntax::GLOBALS.with(|syntax_globals| {
|
|
|
|
syntax_globals as *const _
|
|
|
|
});
|
2018-06-05 16:12:19 -05:00
|
|
|
let syntax_globals = &*syntax_globals;
|
2018-04-06 05:56:59 -05:00
|
|
|
|
|
|
|
let syntax_pos_globals = syntax_pos::GLOBALS.with(|syntax_pos_globals| {
|
|
|
|
syntax_pos_globals as *const _
|
|
|
|
});
|
2018-06-05 16:12:19 -05:00
|
|
|
let syntax_pos_globals = &*syntax_pos_globals;
|
2018-04-06 05:56:59 -05:00
|
|
|
thread::spawn(move || {
|
|
|
|
tls::GCX_PTR.set(gcx_ptr, || {
|
|
|
|
syntax_pos::GLOBALS.set(syntax_pos_globals, || {
|
|
|
|
syntax_pos::GLOBALS.set(syntax_pos_globals, || {
|
|
|
|
tls::with_thread_locals(|| {
|
2018-06-05 16:12:19 -05:00
|
|
|
tls::with_global(|tcx| deadlock(tcx, ®istry))
|
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.
|
2018-04-06 05:56:59 -05:00
|
|
|
#[cfg(parallel_queries)]
|
|
|
|
fn deadlock(tcx: TyCtxt<'_, '_, '_>, registry: &rayon_core::Registry) {
|
|
|
|
let on_panic = OnDrop(|| {
|
|
|
|
eprintln!("deadlock handler panicked, aborting process");
|
|
|
|
process::abort();
|
|
|
|
});
|
|
|
|
|
|
|
|
let mut wakelist = Vec::new();
|
2018-06-13 08:44:43 -05:00
|
|
|
let mut jobs: Vec<_> = tcx.queries.collect_active_jobs();
|
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 {
|
2018-05-27 06:05:41 -05:00
|
|
|
if remove_cycle(&mut jobs, &mut wakelist, tcx) {
|
|
|
|
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
|
|
|
}
|