-
Notifications
You must be signed in to change notification settings - Fork 12.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #49045 - Zoxc:tls, r=michaelwoerister
Make queries thread safe This makes queries thread safe by removing the query stack and making queries point to their parents. Queries write to the query map when starting and cycles are detected by checking if there's already an entry in the query map. This makes cycle detection O(1) instead of O(n), where `n` is the size of the query stack. This is mostly corresponds to the method I described [here](https://internals.rust-lang.org/t/parallelizing-rustc-using-rayon/6606). cc @rust-lang/compiler r? @michaelwoerister
- Loading branch information
Showing
6 changed files
with
558 additions
and
183 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// 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. | ||
|
||
use rustc_data_structures::sync::{Lock, Lrc}; | ||
use syntax_pos::Span; | ||
use ty::tls; | ||
use ty::maps::Query; | ||
use ty::maps::plumbing::CycleError; | ||
use ty::context::TyCtxt; | ||
use errors::Diagnostic; | ||
|
||
/// Indicates the state of a query for a given key in a query map | ||
pub(super) enum QueryResult<'tcx, T> { | ||
/// An already executing query. The query job can be used to await for its completion | ||
Started(Lrc<QueryJob<'tcx>>), | ||
|
||
/// The query is complete and produced `T` | ||
Complete(T), | ||
|
||
/// The query panicked. Queries trying to wait on this will raise a fatal error / silently panic | ||
Poisoned, | ||
} | ||
|
||
/// A span and a query key | ||
#[derive(Clone, Debug)] | ||
pub struct QueryInfo<'tcx> { | ||
pub span: Span, | ||
pub query: Query<'tcx>, | ||
} | ||
|
||
/// A object representing an active query job. | ||
pub struct QueryJob<'tcx> { | ||
pub info: QueryInfo<'tcx>, | ||
|
||
/// The parent query job which created this job and is implicitly waiting on it. | ||
pub parent: Option<Lrc<QueryJob<'tcx>>>, | ||
|
||
/// Diagnostic messages which are emitted while the query executes | ||
pub diagnostics: Lock<Vec<Diagnostic>>, | ||
} | ||
|
||
impl<'tcx> QueryJob<'tcx> { | ||
/// Creates a new query job | ||
pub fn new(info: QueryInfo<'tcx>, parent: Option<Lrc<QueryJob<'tcx>>>) -> Self { | ||
QueryJob { | ||
diagnostics: Lock::new(Vec::new()), | ||
info, | ||
parent, | ||
} | ||
} | ||
|
||
/// 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. | ||
pub(super) fn await<'lcx>( | ||
&self, | ||
tcx: TyCtxt<'_, 'tcx, 'lcx>, | ||
span: Span, | ||
) -> Result<(), CycleError<'tcx>> { | ||
// Get the current executing query (waiter) and find the waitee amongst its parents | ||
let mut current_job = tls::with_related_context(tcx, |icx| icx.query.clone()); | ||
let mut cycle = Vec::new(); | ||
|
||
while let Some(job) = current_job { | ||
cycle.insert(0, job.info.clone()); | ||
|
||
if &*job as *const _ == self as *const _ { | ||
break; | ||
} | ||
|
||
current_job = job.parent.clone(); | ||
} | ||
|
||
Err(CycleError { span, cycle }) | ||
} | ||
|
||
/// 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 | ||
pub fn signal_complete(&self) {} | ||
} |
Oops, something went wrong.