Auto merge of #105550 - gimbles:master, r=Nilstrieb

Use `DepKind` instead of `&'static str` in `QueryStackFrame`

`@rustbot` author

Fixes #105168
This commit is contained in:
bors 2022-12-23 16:57:21 +00:00
commit c2ff8ad035
7 changed files with 141 additions and 114 deletions

View file

@ -21,7 +21,7 @@ pub trait QueryConfig<Qcx: QueryContext> {
type Cache: QueryCache<Key = Self::Key, Stored = Self::Stored, Value = Self::Value>;
// Don't use this method to access query results, instead use the methods on TyCtxt
fn query_state<'a>(tcx: Qcx) -> &'a QueryState<Self::Key>
fn query_state<'a>(tcx: Qcx) -> &'a QueryState<Self::Key, Qcx::DepKind>
where
Qcx: 'a;

View file

@ -1,6 +1,8 @@
use crate::dep_graph::DepKind;
use crate::error::CycleStack;
use crate::query::plumbing::CycleError;
use crate::query::{QueryContext, QueryStackFrame};
use core::marker::PhantomData;
use rustc_data_structures::fx::FxHashMap;
use rustc_errors::{
@ -28,48 +30,48 @@ use {
/// Represents a span and a query key.
#[derive(Clone, Debug)]
pub struct QueryInfo {
pub struct QueryInfo<D: DepKind> {
/// The span corresponding to the reason for which this query was required.
pub span: Span,
pub query: QueryStackFrame,
pub query: QueryStackFrame<D>,
}
pub type QueryMap = FxHashMap<QueryJobId, QueryJobInfo>;
pub type QueryMap<D> = FxHashMap<QueryJobId, QueryJobInfo<D>>;
/// A value uniquely identifying an active query job.
#[derive(Copy, Clone, Eq, PartialEq, Hash)]
pub struct QueryJobId(pub NonZeroU64);
impl QueryJobId {
fn query(self, map: &QueryMap) -> QueryStackFrame {
fn query<D: DepKind>(self, map: &QueryMap<D>) -> QueryStackFrame<D> {
map.get(&self).unwrap().query.clone()
}
#[cfg(parallel_compiler)]
fn span(self, map: &QueryMap) -> Span {
fn span<D: DepKind>(self, map: &QueryMap<D>) -> Span {
map.get(&self).unwrap().job.span
}
#[cfg(parallel_compiler)]
fn parent(self, map: &QueryMap) -> Option<QueryJobId> {
fn parent<D: DepKind>(self, map: &QueryMap<D>) -> Option<QueryJobId> {
map.get(&self).unwrap().job.parent
}
#[cfg(parallel_compiler)]
fn latch<'a>(self, map: &'a QueryMap) -> Option<&'a QueryLatch> {
fn latch<D: DepKind>(self, map: &QueryMap<D>) -> Option<&QueryLatch<D>> {
map.get(&self).unwrap().job.latch.as_ref()
}
}
#[derive(Clone)]
pub struct QueryJobInfo {
pub query: QueryStackFrame,
pub job: QueryJob,
pub struct QueryJobInfo<D: DepKind> {
pub query: QueryStackFrame<D>,
pub job: QueryJob<D>,
}
/// Represents an active query job.
#[derive(Clone)]
pub struct QueryJob {
pub struct QueryJob<D: DepKind> {
pub id: QueryJobId,
/// The span corresponding to the reason for which this query was required.
@ -80,10 +82,11 @@ pub struct QueryJob {
/// The latch that is used to wait on this job.
#[cfg(parallel_compiler)]
latch: Option<QueryLatch>,
latch: Option<QueryLatch<D>>,
spooky: core::marker::PhantomData<D>,
}
impl QueryJob {
impl<D: DepKind> QueryJob<D> {
/// Creates a new query job.
#[inline]
pub fn new(id: QueryJobId, span: Span, parent: Option<QueryJobId>) -> Self {
@ -93,11 +96,12 @@ impl QueryJob {
parent,
#[cfg(parallel_compiler)]
latch: None,
spooky: PhantomData,
}
}
#[cfg(parallel_compiler)]
pub(super) fn latch(&mut self) -> QueryLatch {
pub(super) fn latch(&mut self) -> QueryLatch<D> {
if self.latch.is_none() {
self.latch = Some(QueryLatch::new());
}
@ -123,12 +127,12 @@ impl QueryJobId {
#[cold]
#[inline(never)]
#[cfg(not(parallel_compiler))]
pub(super) fn find_cycle_in_stack(
pub(super) fn find_cycle_in_stack<D: DepKind>(
&self,
query_map: QueryMap,
query_map: QueryMap<D>,
current_job: &Option<QueryJobId>,
span: Span,
) -> CycleError {
) -> CycleError<D> {
// Find the waitee amongst `current_job` parents
let mut cycle = Vec::new();
let mut current_job = Option::clone(current_job);
@ -162,14 +166,18 @@ impl QueryJobId {
#[cold]
#[inline(never)]
pub fn try_find_layout_root(&self, query_map: QueryMap) -> Option<(QueryJobInfo, usize)> {
pub fn try_find_layout_root<D: DepKind>(
&self,
query_map: QueryMap<D>,
) -> Option<(QueryJobInfo<D>, usize)> {
let mut last_layout = None;
let mut current_id = Some(*self);
let mut depth = 0;
while let Some(id) = current_id {
let info = query_map.get(&id).unwrap();
if info.query.name == "layout_of" {
// FIXME: This string comparison should probably not be done.
if format!("{:?}", info.query.dep_kind) == "layout_of" {
depth += 1;
last_layout = Some((info.clone(), depth));
}
@ -180,15 +188,15 @@ impl QueryJobId {
}
#[cfg(parallel_compiler)]
struct QueryWaiter {
struct QueryWaiter<D: DepKind> {
query: Option<QueryJobId>,
condvar: Condvar,
span: Span,
cycle: Lock<Option<CycleError>>,
cycle: Lock<Option<CycleError<D>>>,
}
#[cfg(parallel_compiler)]
impl QueryWaiter {
impl<D: DepKind> QueryWaiter<D> {
fn notify(&self, registry: &rayon_core::Registry) {
rayon_core::mark_unblocked(registry);
self.condvar.notify_one();
@ -196,19 +204,19 @@ impl QueryWaiter {
}
#[cfg(parallel_compiler)]
struct QueryLatchInfo {
struct QueryLatchInfo<D: DepKind> {
complete: bool,
waiters: Vec<Lrc<QueryWaiter>>,
waiters: Vec<Lrc<QueryWaiter<D>>>,
}
#[cfg(parallel_compiler)]
#[derive(Clone)]
pub(super) struct QueryLatch {
info: Lrc<Mutex<QueryLatchInfo>>,
pub(super) struct QueryLatch<D: DepKind> {
info: Lrc<Mutex<QueryLatchInfo<D>>>,
}
#[cfg(parallel_compiler)]
impl QueryLatch {
impl<D: DepKind> QueryLatch<D> {
fn new() -> Self {
QueryLatch {
info: Lrc::new(Mutex::new(QueryLatchInfo { complete: false, waiters: Vec::new() })),
@ -216,7 +224,11 @@ impl QueryLatch {
}
/// Awaits for the query job to complete.
pub(super) fn wait_on(&self, query: Option<QueryJobId>, span: Span) -> Result<(), CycleError> {
pub(super) fn wait_on(
&self,
query: Option<QueryJobId>,
span: Span,
) -> Result<(), CycleError<D>> {
let waiter =
Lrc::new(QueryWaiter { query, span, cycle: Lock::new(None), condvar: Condvar::new() });
self.wait_on_inner(&waiter);
@ -231,7 +243,7 @@ impl QueryLatch {
}
/// Awaits the caller on this latch by blocking the current thread.
fn wait_on_inner(&self, waiter: &Lrc<QueryWaiter>) {
fn wait_on_inner(&self, waiter: &Lrc<QueryWaiter<D>>) {
let mut info = self.info.lock();
if !info.complete {
// We push the waiter on to the `waiters` list. It can be accessed inside
@ -265,7 +277,7 @@ impl QueryLatch {
/// Removes a single waiter from the list of waiters.
/// This is used to break query cycles.
fn extract_waiter(&self, waiter: usize) -> Lrc<QueryWaiter> {
fn extract_waiter(&self, waiter: usize) -> Lrc<QueryWaiter<D>> {
let mut info = self.info.lock();
debug_assert!(!info.complete);
// Remove the waiter from the list of waiters
@ -287,9 +299,14 @@ type Waiter = (QueryJobId, usize);
/// required information to resume the waiter.
/// If all `visit` calls returns None, this function also returns None.
#[cfg(parallel_compiler)]
fn visit_waiters<F>(query_map: &QueryMap, query: QueryJobId, mut visit: F) -> Option<Option<Waiter>>
fn visit_waiters<F, D>(
query_map: &QueryMap<D>,
query: QueryJobId,
mut visit: F,
) -> Option<Option<Waiter>>
where
F: FnMut(Span, QueryJobId) -> Option<Option<Waiter>>,
D: DepKind,
{
// Visit the parent query which is a non-resumable waiter since it's on the same stack
if let Some(parent) = query.parent(query_map) {
@ -318,8 +335,8 @@ where
/// If a cycle is detected, this initial value is replaced with the span causing
/// the cycle.
#[cfg(parallel_compiler)]
fn cycle_check(
query_map: &QueryMap,
fn cycle_check<D: DepKind>(
query_map: &QueryMap<D>,
query: QueryJobId,
span: Span,
stack: &mut Vec<(Span, QueryJobId)>,
@ -359,8 +376,8 @@ fn cycle_check(
/// from `query` without going through any of the queries in `visited`.
/// This is achieved with a depth first search.
#[cfg(parallel_compiler)]
fn connected_to_root(
query_map: &QueryMap,
fn connected_to_root<D: DepKind>(
query_map: &QueryMap<D>,
query: QueryJobId,
visited: &mut FxHashSet<QueryJobId>,
) -> bool {
@ -382,9 +399,10 @@ fn connected_to_root(
// Deterministically pick an query from a list
#[cfg(parallel_compiler)]
fn pick_query<'a, T, F>(query_map: &QueryMap, queries: &'a [T], f: F) -> &'a T
fn pick_query<'a, T, F, D>(query_map: &QueryMap<D>, queries: &'a [T], f: F) -> &'a T
where
F: Fn(&T) -> (Span, QueryJobId),
D: DepKind,
{
// Deterministically pick an entry point
// FIXME: Sort this instead
@ -408,10 +426,10 @@ where
/// If a cycle was not found, the starting query is removed from `jobs` and
/// the function returns false.
#[cfg(parallel_compiler)]
fn remove_cycle(
query_map: &QueryMap,
fn remove_cycle<D: DepKind>(
query_map: &QueryMap<D>,
jobs: &mut Vec<QueryJobId>,
wakelist: &mut Vec<Lrc<QueryWaiter>>,
wakelist: &mut Vec<Lrc<QueryWaiter<D>>>,
) -> bool {
let mut visited = FxHashSet::default();
let mut stack = Vec::new();
@ -513,7 +531,7 @@ fn remove_cycle(
/// 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.
#[cfg(parallel_compiler)]
pub fn deadlock(query_map: QueryMap, registry: &rayon_core::Registry) {
pub fn deadlock<D: DepKind>(query_map: QueryMap<D>, registry: &rayon_core::Registry) {
let on_panic = OnDrop(|| {
eprintln!("deadlock handler panicked, aborting process");
process::abort();
@ -549,9 +567,9 @@ pub fn deadlock(query_map: QueryMap, registry: &rayon_core::Registry) {
#[inline(never)]
#[cold]
pub(crate) fn report_cycle<'a>(
pub(crate) fn report_cycle<'a, D: DepKind>(
sess: &'a Session,
CycleError { usage, cycle: stack }: &CycleError,
CycleError { usage, cycle: stack }: &CycleError<D>,
) -> DiagnosticBuilder<'a, ErrorGuaranteed> {
assert!(!stack.is_empty());
@ -617,7 +635,7 @@ pub fn print_query_stack<Qcx: QueryContext>(
};
let mut diag = Diagnostic::new(
Level::FailureNote,
&format!("#{} [{}] {}", i, query_info.query.name, query_info.query.description),
&format!("#{} [{:?}] {}", i, query_info.query.dep_kind, query_info.query.description),
);
diag.span = query_info.job.span.into();
handler.force_print_diagnostic(diag);

View file

@ -14,6 +14,7 @@ pub use self::caches::{
mod config;
pub use self::config::{QueryConfig, QueryVTable};
use crate::dep_graph::DepKind;
use crate::dep_graph::{DepNodeIndex, HasDepContext, SerializedDepNodeIndex};
use rustc_data_structures::sync::Lock;
use rustc_errors::Diagnostic;
@ -26,37 +27,37 @@ use thin_vec::ThinVec;
///
/// This is mostly used in case of cycles for error reporting.
#[derive(Clone, Debug)]
pub struct QueryStackFrame {
pub name: &'static str,
pub struct QueryStackFrame<D: DepKind> {
pub description: String,
span: Option<Span>,
pub def_id: Option<DefId>,
pub def_kind: Option<DefKind>,
pub ty_adt_id: Option<DefId>,
pub dep_kind: D,
/// This hash is used to deterministically pick
/// a query to remove cycles in the parallel compiler.
#[cfg(parallel_compiler)]
hash: u64,
}
impl QueryStackFrame {
impl<D: DepKind> QueryStackFrame<D> {
#[inline]
pub fn new(
name: &'static str,
description: String,
span: Option<Span>,
def_id: Option<DefId>,
def_kind: Option<DefKind>,
dep_kind: D,
ty_adt_id: Option<DefId>,
_hash: impl FnOnce() -> u64,
) -> Self {
Self {
name,
description,
span,
def_id,
def_kind,
ty_adt_id,
dep_kind,
#[cfg(parallel_compiler)]
hash: _hash(),
}
@ -104,7 +105,7 @@ pub trait QueryContext: HasDepContext {
/// Get the query information from the TLS context.
fn current_query_job(&self) -> Option<QueryJobId>;
fn try_collect_active_jobs(&self) -> Option<QueryMap>;
fn try_collect_active_jobs(&self) -> Option<QueryMap<Self::DepKind>>;
/// Load side effects associated to the node in the previous session.
fn load_side_effects(&self, prev_dep_node_index: SerializedDepNodeIndex) -> QuerySideEffects;

View file

@ -2,7 +2,7 @@
//! generate the actual methods on tcx which find and execute the provider,
//! manage the caches, and so forth.
use crate::dep_graph::{DepContext, DepNode, DepNodeIndex, DepNodeParams};
use crate::dep_graph::{DepContext, DepKind, DepNode, DepNodeIndex, DepNodeParams};
use crate::ich::StableHashingContext;
use crate::query::caches::QueryCache;
use crate::query::config::QueryVTable;
@ -31,26 +31,27 @@ use thin_vec::ThinVec;
use super::QueryConfig;
pub struct QueryState<K> {
pub struct QueryState<K, D: DepKind> {
#[cfg(parallel_compiler)]
active: Sharded<FxHashMap<K, QueryResult>>,
active: Sharded<FxHashMap<K, QueryResult<D>>>,
#[cfg(not(parallel_compiler))]
active: Lock<FxHashMap<K, QueryResult>>,
active: Lock<FxHashMap<K, QueryResult<D>>>,
}
/// Indicates the state of a query for a given key in a query map.
enum QueryResult {
enum QueryResult<D: DepKind> {
/// An already executing query. The query job can be used to await for its completion.
Started(QueryJob),
Started(QueryJob<D>),
/// The query panicked. Queries trying to wait on this will raise a fatal error which will
/// silently panic.
Poisoned,
}
impl<K> QueryState<K>
impl<K, D> QueryState<K, D>
where
K: Eq + Hash + Clone + Debug,
D: DepKind,
{
pub fn all_inactive(&self) -> bool {
#[cfg(parallel_compiler)]
@ -67,8 +68,8 @@ where
pub fn try_collect_active_jobs<Qcx: Copy>(
&self,
qcx: Qcx,
make_query: fn(Qcx, K) -> QueryStackFrame,
jobs: &mut QueryMap,
make_query: fn(Qcx, K) -> QueryStackFrame<D>,
jobs: &mut QueryMap<D>,
) -> Option<()> {
#[cfg(parallel_compiler)]
{
@ -102,34 +103,34 @@ where
}
}
impl<K> Default for QueryState<K> {
fn default() -> QueryState<K> {
impl<K, D: DepKind> Default for QueryState<K, D> {
fn default() -> QueryState<K, D> {
QueryState { active: Default::default() }
}
}
/// A type representing the responsibility to execute the job in the `job` field.
/// This will poison the relevant query if dropped.
struct JobOwner<'tcx, K>
struct JobOwner<'tcx, K, D: DepKind>
where
K: Eq + Hash + Clone,
{
state: &'tcx QueryState<K>,
state: &'tcx QueryState<K, D>,
key: K,
id: QueryJobId,
}
#[cold]
#[inline(never)]
fn mk_cycle<Qcx, V, R>(
fn mk_cycle<Qcx, V, R, D: DepKind>(
qcx: Qcx,
cycle_error: CycleError,
cycle_error: CycleError<D>,
handler: HandleCycleError,
cache: &dyn crate::query::QueryStorage<Value = V, Stored = R>,
) -> R
where
Qcx: QueryContext,
V: std::fmt::Debug + Value<Qcx::DepContext>,
Qcx: QueryContext + crate::query::HasDepContext<DepKind = D>,
V: std::fmt::Debug + Value<Qcx::DepContext, Qcx::DepKind>,
R: Clone,
{
let error = report_cycle(qcx.dep_context().sess(), &cycle_error);
@ -139,13 +140,13 @@ where
fn handle_cycle_error<Tcx, V>(
tcx: Tcx,
cycle_error: &CycleError,
cycle_error: &CycleError<Tcx::DepKind>,
mut error: DiagnosticBuilder<'_, ErrorGuaranteed>,
handler: HandleCycleError,
) -> V
where
Tcx: DepContext,
V: Value<Tcx>,
V: Value<Tcx, Tcx::DepKind>,
{
use HandleCycleError::*;
match handler {
@ -165,7 +166,7 @@ where
}
}
impl<'tcx, K> JobOwner<'tcx, K>
impl<'tcx, K, D: DepKind> JobOwner<'tcx, K, D>
where
K: Eq + Hash + Clone,
{
@ -180,12 +181,12 @@ where
#[inline(always)]
fn try_start<'b, Qcx>(
qcx: &'b Qcx,
state: &'b QueryState<K>,
state: &'b QueryState<K, Qcx::DepKind>,
span: Span,
key: K,
) -> TryGetJob<'b, K>
) -> TryGetJob<'b, K, D>
where
Qcx: QueryContext,
Qcx: QueryContext + crate::query::HasDepContext<DepKind = D>,
{
#[cfg(parallel_compiler)]
let mut state_lock = state.active.get_shard_by_value(&key).lock();
@ -280,9 +281,10 @@ where
}
}
impl<'tcx, K> Drop for JobOwner<'tcx, K>
impl<'tcx, K, D> Drop for JobOwner<'tcx, K, D>
where
K: Eq + Hash + Clone,
D: DepKind,
{
#[inline(never)]
#[cold]
@ -308,19 +310,20 @@ where
}
#[derive(Clone)]
pub(crate) struct CycleError {
pub(crate) struct CycleError<D: DepKind> {
/// The query and related span that uses the cycle.
pub usage: Option<(Span, QueryStackFrame)>,
pub cycle: Vec<QueryInfo>,
pub usage: Option<(Span, QueryStackFrame<D>)>,
pub cycle: Vec<QueryInfo<D>>,
}
/// The result of `try_start`.
enum TryGetJob<'tcx, K>
enum TryGetJob<'tcx, K, D>
where
K: Eq + Hash + Clone,
D: DepKind,
{
/// The query is not yet started. Contains a guard to the cache eventually used to start it.
NotYetStarted(JobOwner<'tcx, K>),
NotYetStarted(JobOwner<'tcx, K, D>),
/// The query was already completed.
/// Returns the result of the query and its dep-node index
@ -329,7 +332,7 @@ where
JobCompleted(TimingGuard<'tcx>),
/// Trying to execute the query resulted in a cycle.
Cycle(CycleError),
Cycle(CycleError<D>),
}
/// Checks if the query is already computed and in the cache.
@ -360,7 +363,7 @@ where
fn try_execute_query<Qcx, C>(
qcx: Qcx,
state: &QueryState<C::Key>,
state: &QueryState<C::Key, Qcx::DepKind>,
cache: &C,
span: Span,
key: C::Key,
@ -370,11 +373,11 @@ fn try_execute_query<Qcx, C>(
where
C: QueryCache,
C::Key: Clone + DepNodeParams<Qcx::DepContext>,
C::Value: Value<Qcx::DepContext>,
C::Value: Value<Qcx::DepContext, Qcx::DepKind>,
C::Stored: Debug + std::borrow::Borrow<C::Value>,
Qcx: QueryContext,
{
match JobOwner::<'_, C::Key>::try_start(&qcx, state, span, key.clone()) {
match JobOwner::<'_, C::Key, Qcx::DepKind>::try_start(&qcx, state, span, key.clone()) {
TryGetJob::NotYetStarted(job) => {
let (result, dep_node_index) = execute_job(qcx, key.clone(), dep_node, query, job.id);
if query.feedable {
@ -739,11 +742,12 @@ pub enum QueryMode {
Ensure,
}
pub fn get_query<Q, Qcx>(qcx: Qcx, span: Span, key: Q::Key, mode: QueryMode) -> Option<Q::Stored>
pub fn get_query<Q, Qcx, D>(qcx: Qcx, span: Span, key: Q::Key, mode: QueryMode) -> Option<Q::Stored>
where
D: DepKind,
Q: QueryConfig<Qcx>,
Q::Key: DepNodeParams<Qcx::DepContext>,
Q::Value: Value<Qcx::DepContext>,
Q::Value: Value<Qcx::DepContext, D>,
Qcx: QueryContext,
{
let query = Q::make_vtable(qcx, &key);
@ -772,11 +776,12 @@ where
Some(result)
}
pub fn force_query<Q, Qcx>(qcx: Qcx, key: Q::Key, dep_node: DepNode<Qcx::DepKind>)
pub fn force_query<Q, Qcx, D>(qcx: Qcx, key: Q::Key, dep_node: DepNode<Qcx::DepKind>)
where
D: DepKind,
Q: QueryConfig<Qcx>,
Q::Key: DepNodeParams<Qcx::DepContext>,
Q::Value: Value<Qcx::DepContext>,
Q::Value: Value<Qcx::DepContext, D>,
Qcx: QueryContext,
{
// We may be concurrently trying both execute and force a query.

View file

@ -1,12 +1,12 @@
use crate::dep_graph::DepContext;
use crate::dep_graph::{DepContext, DepKind};
use crate::query::QueryInfo;
pub trait Value<Tcx: DepContext>: Sized {
fn from_cycle_error(tcx: Tcx, cycle: &[QueryInfo]) -> Self;
pub trait Value<Tcx: DepContext, D: DepKind>: Sized {
fn from_cycle_error(tcx: Tcx, cycle: &[QueryInfo<D>]) -> Self;
}
impl<Tcx: DepContext, T> Value<Tcx> for T {
default fn from_cycle_error(tcx: Tcx, _: &[QueryInfo]) -> T {
impl<Tcx: DepContext, T, D: DepKind> Value<Tcx, D> for T {
default fn from_cycle_error(tcx: Tcx, _: &[QueryInfo<D>]) -> T {
tcx.sess().abort_if_errors();
// Ideally we would use `bug!` here. But bug! is only defined in rustc_middle, and it's
// non-trivial to define it earlier.