Use DepKind instead of &str

This commit is contained in:
gimbles 2022-12-23 18:39:49 +05:30
parent fd02567705
commit f8b30084ac
7 changed files with 141 additions and 114 deletions

View File

@ -1,3 +1,4 @@
use crate::dep_graph::DepKind;
use rustc_data_structures::fx::FxHashSet; use rustc_data_structures::fx::FxHashSet;
use rustc_errors::{pluralize, struct_span_err, Applicability, MultiSpan}; use rustc_errors::{pluralize, struct_span_err, Applicability, MultiSpan};
use rustc_hir as hir; use rustc_hir as hir;
@ -11,16 +12,16 @@
use std::fmt::Write; use std::fmt::Write;
impl<'tcx> Value<TyCtxt<'tcx>> for Ty<'_> { impl<'tcx> Value<TyCtxt<'tcx>, DepKind> for Ty<'_> {
fn from_cycle_error(tcx: TyCtxt<'tcx>, _: &[QueryInfo]) -> Self { fn from_cycle_error(tcx: TyCtxt<'tcx>, _: &[QueryInfo<DepKind>]) -> Self {
// SAFETY: This is never called when `Self` is not `Ty<'tcx>`. // SAFETY: This is never called when `Self` is not `Ty<'tcx>`.
// FIXME: Represent the above fact in the trait system somehow. // FIXME: Represent the above fact in the trait system somehow.
unsafe { std::mem::transmute::<Ty<'tcx>, Ty<'_>>(tcx.ty_error()) } unsafe { std::mem::transmute::<Ty<'tcx>, Ty<'_>>(tcx.ty_error()) }
} }
} }
impl<'tcx> Value<TyCtxt<'tcx>> for ty::SymbolName<'_> { impl<'tcx> Value<TyCtxt<'tcx>, DepKind> for ty::SymbolName<'_> {
fn from_cycle_error(tcx: TyCtxt<'tcx>, _: &[QueryInfo]) -> Self { fn from_cycle_error(tcx: TyCtxt<'tcx>, _: &[QueryInfo<DepKind>]) -> Self {
// SAFETY: This is never called when `Self` is not `SymbolName<'tcx>`. // SAFETY: This is never called when `Self` is not `SymbolName<'tcx>`.
// FIXME: Represent the above fact in the trait system somehow. // FIXME: Represent the above fact in the trait system somehow.
unsafe { unsafe {
@ -31,12 +32,12 @@ fn from_cycle_error(tcx: TyCtxt<'tcx>, _: &[QueryInfo]) -> Self {
} }
} }
impl<'tcx> Value<TyCtxt<'tcx>> for ty::Binder<'_, ty::FnSig<'_>> { impl<'tcx> Value<TyCtxt<'tcx>, DepKind> for ty::Binder<'_, ty::FnSig<'_>> {
fn from_cycle_error(tcx: TyCtxt<'tcx>, stack: &[QueryInfo]) -> Self { fn from_cycle_error(tcx: TyCtxt<'tcx>, stack: &[QueryInfo<DepKind>]) -> Self {
let err = tcx.ty_error(); let err = tcx.ty_error();
let arity = if let Some(frame) = stack.get(0) let arity = if let Some(frame) = stack.get(0)
&& frame.query.name == "fn_sig" && frame.query.dep_kind == DepKind::fn_sig
&& let Some(def_id) = frame.query.def_id && let Some(def_id) = frame.query.def_id
&& let Some(node) = tcx.hir().get_if_local(def_id) && let Some(node) = tcx.hir().get_if_local(def_id)
&& let Some(sig) = node.fn_sig() && let Some(sig) = node.fn_sig()
@ -61,12 +62,12 @@ fn from_cycle_error(tcx: TyCtxt<'tcx>, stack: &[QueryInfo]) -> Self {
} }
} }
impl<'tcx> Value<TyCtxt<'tcx>> for Representability { impl<'tcx> Value<TyCtxt<'tcx>, DepKind> for Representability {
fn from_cycle_error(tcx: TyCtxt<'tcx>, cycle: &[QueryInfo]) -> Self { fn from_cycle_error(tcx: TyCtxt<'tcx>, cycle: &[QueryInfo<DepKind>]) -> Self {
let mut item_and_field_ids = Vec::new(); let mut item_and_field_ids = Vec::new();
let mut representable_ids = FxHashSet::default(); let mut representable_ids = FxHashSet::default();
for info in cycle { for info in cycle {
if info.query.name == "representability" if info.query.dep_kind == DepKind::representability
&& let Some(field_id) = info.query.def_id && let Some(field_id) = info.query.def_id
&& let Some(field_id) = field_id.as_local() && let Some(field_id) = field_id.as_local()
&& let Some(DefKind::Field) = info.query.def_kind && let Some(DefKind::Field) = info.query.def_kind
@ -80,7 +81,7 @@ fn from_cycle_error(tcx: TyCtxt<'tcx>, cycle: &[QueryInfo]) -> Self {
} }
} }
for info in cycle { for info in cycle {
if info.query.name == "representability_adt_ty" if info.query.dep_kind == DepKind::representability_adt_ty
&& let Some(def_id) = info.query.ty_adt_id && let Some(def_id) = info.query.ty_adt_id
&& let Some(def_id) = def_id.as_local() && let Some(def_id) = def_id.as_local()
&& !item_and_field_ids.iter().any(|&(id, _)| id == def_id) && !item_and_field_ids.iter().any(|&(id, _)| id == def_id)

View File

@ -66,7 +66,7 @@ fn current_query_job(&self) -> Option<QueryJobId> {
tls::with_related_context(**self, |icx| icx.query) tls::with_related_context(**self, |icx| icx.query)
} }
fn try_collect_active_jobs(&self) -> Option<QueryMap> { fn try_collect_active_jobs(&self) -> Option<QueryMap<DepKind>> {
self.queries.try_collect_active_jobs(**self) self.queries.try_collect_active_jobs(**self)
} }
@ -195,7 +195,7 @@ pub fn try_print_query_stack(
#[derive(Clone, Copy)] #[derive(Clone, Copy)]
pub(crate) struct QueryStruct<'tcx> { pub(crate) struct QueryStruct<'tcx> {
pub try_collect_active_jobs: fn(QueryCtxt<'tcx>, &mut QueryMap) -> Option<()>, pub try_collect_active_jobs: fn(QueryCtxt<'tcx>, &mut QueryMap<DepKind>) -> Option<()>,
pub alloc_self_profile_query_strings: fn(TyCtxt<'tcx>, &mut QueryKeyStringCache), pub alloc_self_profile_query_strings: fn(TyCtxt<'tcx>, &mut QueryKeyStringCache),
pub encode_query_results: pub encode_query_results:
Option<fn(QueryCtxt<'tcx>, &mut CacheEncoder<'_, 'tcx>, &mut EncodedDepNodeIndex)>, Option<fn(QueryCtxt<'tcx>, &mut CacheEncoder<'_, 'tcx>, &mut EncodedDepNodeIndex)>,
@ -313,7 +313,7 @@ pub(crate) fn create_query_frame<
key: K, key: K,
kind: DepKind, kind: DepKind,
name: &'static str, name: &'static str,
) -> QueryStackFrame { ) -> QueryStackFrame<DepKind> {
// Disable visible paths printing for performance reasons. // Disable visible paths printing for performance reasons.
// Showing visible path instead of any path is not that important in production. // Showing visible path instead of any path is not that important in production.
let description = ty::print::with_no_visible_paths!( let description = ty::print::with_no_visible_paths!(
@ -346,7 +346,7 @@ pub(crate) fn create_query_frame<
}; };
let ty_adt_id = key.ty_adt_id(); let ty_adt_id = key.ty_adt_id();
QueryStackFrame::new(name, description, span, def_id, def_kind, ty_adt_id, hash) QueryStackFrame::new(description, span, def_id, def_kind, kind, ty_adt_id, hash)
} }
fn try_load_from_on_disk_cache<'tcx, Q>(tcx: TyCtxt<'tcx>, dep_node: DepNode) fn try_load_from_on_disk_cache<'tcx, Q>(tcx: TyCtxt<'tcx>, dep_node: DepNode)
@ -378,7 +378,7 @@ fn force_from_dep_node<'tcx, Q>(tcx: TyCtxt<'tcx>, dep_node: DepNode) -> bool
where where
Q: QueryConfig<QueryCtxt<'tcx>>, Q: QueryConfig<QueryCtxt<'tcx>>,
Q::Key: DepNodeParams<TyCtxt<'tcx>>, Q::Key: DepNodeParams<TyCtxt<'tcx>>,
Q::Value: Value<TyCtxt<'tcx>>, Q::Value: Value<TyCtxt<'tcx>, DepKind>,
{ {
// We must avoid ever having to call `force_from_dep_node()` for a // We must avoid ever having to call `force_from_dep_node()` for a
// `DepNode::codegen_unit`: // `DepNode::codegen_unit`:
@ -402,7 +402,7 @@ fn force_from_dep_node<'tcx, Q>(tcx: TyCtxt<'tcx>, dep_node: DepNode) -> bool
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
let _guard = tracing::span!(tracing::Level::TRACE, stringify!($name), ?key).entered(); let _guard = tracing::span!(tracing::Level::TRACE, stringify!($name), ?key).entered();
let tcx = QueryCtxt::from_tcx(tcx); let tcx = QueryCtxt::from_tcx(tcx);
force_query::<Q, _>(tcx, key, dep_node); force_query::<Q, _, DepKind>(tcx, key, dep_node);
true true
} else { } else {
false false
@ -480,7 +480,7 @@ fn cache_on_disk(tcx: TyCtxt<'tcx>, key: &Self::Key) -> bool {
type Cache = query_storage::$name<'tcx>; type Cache = query_storage::$name<'tcx>;
#[inline(always)] #[inline(always)]
fn query_state<'a>(tcx: QueryCtxt<'tcx>) -> &'a QueryState<Self::Key> fn query_state<'a>(tcx: QueryCtxt<'tcx>) -> &'a QueryState<Self::Key, crate::dep_graph::DepKind>
where QueryCtxt<'tcx>: 'a where QueryCtxt<'tcx>: 'a
{ {
&tcx.queries.$name &tcx.queries.$name
@ -587,9 +587,10 @@ mod query_structs {
use $crate::plumbing::{QueryStruct, QueryCtxt}; use $crate::plumbing::{QueryStruct, QueryCtxt};
use $crate::profiling_support::QueryKeyStringCache; use $crate::profiling_support::QueryKeyStringCache;
use rustc_query_system::query::QueryMap; use rustc_query_system::query::QueryMap;
use rustc_middle::dep_graph::DepKind;
pub(super) const fn dummy_query_struct<'tcx>() -> QueryStruct<'tcx> { pub(super) const fn dummy_query_struct<'tcx>() -> QueryStruct<'tcx> {
fn noop_try_collect_active_jobs(_: QueryCtxt<'_>, _: &mut QueryMap) -> Option<()> { fn noop_try_collect_active_jobs(_: QueryCtxt<'_>, _: &mut QueryMap<DepKind>) -> Option<()> {
None None
} }
fn noop_alloc_self_profile_query_strings(_: TyCtxt<'_>, _: &mut QueryKeyStringCache) {} fn noop_alloc_self_profile_query_strings(_: TyCtxt<'_>, _: &mut QueryKeyStringCache) {}
@ -675,7 +676,8 @@ pub struct Queries<'tcx> {
$( $(
$(#[$attr])* $(#[$attr])*
$name: QueryState< $name: QueryState<
<queries::$name<'tcx> as QueryConfig<QueryCtxt<'tcx>>>::Key <queries::$name<'tcx> as QueryConfig<QueryCtxt<'tcx>>>::Key,
rustc_middle::dep_graph::DepKind,
>, >,
)* )*
} }
@ -684,7 +686,7 @@ impl<'tcx> Queries<'tcx> {
pub(crate) fn try_collect_active_jobs( pub(crate) fn try_collect_active_jobs(
&'tcx self, &'tcx self,
tcx: TyCtxt<'tcx>, tcx: TyCtxt<'tcx>,
) -> Option<QueryMap> { ) -> Option<QueryMap<rustc_middle::dep_graph::DepKind>> {
let tcx = QueryCtxt { tcx, queries: self }; let tcx = QueryCtxt { tcx, queries: self };
let mut jobs = QueryMap::default(); let mut jobs = QueryMap::default();
@ -718,7 +720,7 @@ fn $name(
mode: QueryMode, mode: QueryMode,
) -> Option<query_stored::$name<'tcx>> { ) -> Option<query_stored::$name<'tcx>> {
let qcx = QueryCtxt { tcx, queries: self }; let qcx = QueryCtxt { tcx, queries: self };
get_query::<queries::$name<'tcx>, _>(qcx, span, key, mode) get_query::<queries::$name<'tcx>, _, rustc_middle::dep_graph::DepKind>(qcx, span, key, mode)
})* })*
} }
}; };

View File

@ -21,7 +21,7 @@ pub trait QueryConfig<Qcx: QueryContext> {
type Cache: QueryCache<Key = Self::Key, Stored = Self::Stored, Value = Self::Value>; 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 // 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 where
Qcx: 'a; Qcx: 'a;

View File

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

View File

@ -14,6 +14,7 @@
mod config; mod config;
pub use self::config::{QueryConfig, QueryVTable}; pub use self::config::{QueryConfig, QueryVTable};
use crate::dep_graph::DepKind;
use crate::dep_graph::{DepNodeIndex, HasDepContext, SerializedDepNodeIndex}; use crate::dep_graph::{DepNodeIndex, HasDepContext, SerializedDepNodeIndex};
use rustc_data_structures::sync::Lock; use rustc_data_structures::sync::Lock;
use rustc_errors::Diagnostic; use rustc_errors::Diagnostic;
@ -26,37 +27,37 @@
/// ///
/// This is mostly used in case of cycles for error reporting. /// This is mostly used in case of cycles for error reporting.
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct QueryStackFrame { pub struct QueryStackFrame<D: DepKind> {
pub name: &'static str,
pub description: String, pub description: String,
span: Option<Span>, span: Option<Span>,
pub def_id: Option<DefId>, pub def_id: Option<DefId>,
pub def_kind: Option<DefKind>, pub def_kind: Option<DefKind>,
pub ty_adt_id: Option<DefId>, pub ty_adt_id: Option<DefId>,
pub dep_kind: D,
/// This hash is used to deterministically pick /// This hash is used to deterministically pick
/// a query to remove cycles in the parallel compiler. /// a query to remove cycles in the parallel compiler.
#[cfg(parallel_compiler)] #[cfg(parallel_compiler)]
hash: u64, hash: u64,
} }
impl QueryStackFrame { impl<D: DepKind> QueryStackFrame<D> {
#[inline] #[inline]
pub fn new( pub fn new(
name: &'static str,
description: String, description: String,
span: Option<Span>, span: Option<Span>,
def_id: Option<DefId>, def_id: Option<DefId>,
def_kind: Option<DefKind>, def_kind: Option<DefKind>,
dep_kind: D,
ty_adt_id: Option<DefId>, ty_adt_id: Option<DefId>,
_hash: impl FnOnce() -> u64, _hash: impl FnOnce() -> u64,
) -> Self { ) -> Self {
Self { Self {
name,
description, description,
span, span,
def_id, def_id,
def_kind, def_kind,
ty_adt_id, ty_adt_id,
dep_kind,
#[cfg(parallel_compiler)] #[cfg(parallel_compiler)]
hash: _hash(), hash: _hash(),
} }
@ -104,7 +105,7 @@ pub trait QueryContext: HasDepContext {
/// Get the query information from the TLS context. /// Get the query information from the TLS context.
fn current_query_job(&self) -> Option<QueryJobId>; 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. /// Load side effects associated to the node in the previous session.
fn load_side_effects(&self, prev_dep_node_index: SerializedDepNodeIndex) -> QuerySideEffects; 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, //! generate the actual methods on tcx which find and execute the provider,
//! manage the caches, and so forth. //! 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::ich::StableHashingContext;
use crate::query::caches::QueryCache; use crate::query::caches::QueryCache;
use crate::query::config::QueryVTable; use crate::query::config::QueryVTable;
@ -31,26 +31,27 @@
use super::QueryConfig; use super::QueryConfig;
pub struct QueryState<K> { pub struct QueryState<K, D: DepKind> {
#[cfg(parallel_compiler)] #[cfg(parallel_compiler)]
active: Sharded<FxHashMap<K, QueryResult>>, active: Sharded<FxHashMap<K, QueryResult<D>>>,
#[cfg(not(parallel_compiler))] #[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. /// 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. /// 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 /// The query panicked. Queries trying to wait on this will raise a fatal error which will
/// silently panic. /// silently panic.
Poisoned, Poisoned,
} }
impl<K> QueryState<K> impl<K, D> QueryState<K, D>
where where
K: Eq + Hash + Clone + Debug, K: Eq + Hash + Clone + Debug,
D: DepKind,
{ {
pub fn all_inactive(&self) -> bool { pub fn all_inactive(&self) -> bool {
#[cfg(parallel_compiler)] #[cfg(parallel_compiler)]
@ -67,8 +68,8 @@ pub fn all_inactive(&self) -> bool {
pub fn try_collect_active_jobs<Qcx: Copy>( pub fn try_collect_active_jobs<Qcx: Copy>(
&self, &self,
qcx: Qcx, qcx: Qcx,
make_query: fn(Qcx, K) -> QueryStackFrame, make_query: fn(Qcx, K) -> QueryStackFrame<D>,
jobs: &mut QueryMap, jobs: &mut QueryMap<D>,
) -> Option<()> { ) -> Option<()> {
#[cfg(parallel_compiler)] #[cfg(parallel_compiler)]
{ {
@ -102,34 +103,34 @@ pub fn try_collect_active_jobs<Qcx: Copy>(
} }
} }
impl<K> Default for QueryState<K> { impl<K, D: DepKind> Default for QueryState<K, D> {
fn default() -> QueryState<K> { fn default() -> QueryState<K, D> {
QueryState { active: Default::default() } QueryState { active: Default::default() }
} }
} }
/// A type representing the responsibility to execute the job in the `job` field. /// A type representing the responsibility to execute the job in the `job` field.
/// This will poison the relevant query if dropped. /// This will poison the relevant query if dropped.
struct JobOwner<'tcx, K> struct JobOwner<'tcx, K, D: DepKind>
where where
K: Eq + Hash + Clone, K: Eq + Hash + Clone,
{ {
state: &'tcx QueryState<K>, state: &'tcx QueryState<K, D>,
key: K, key: K,
id: QueryJobId, id: QueryJobId,
} }
#[cold] #[cold]
#[inline(never)] #[inline(never)]
fn mk_cycle<Qcx, V, R>( fn mk_cycle<Qcx, V, R, D: DepKind>(
qcx: Qcx, qcx: Qcx,
cycle_error: CycleError, cycle_error: CycleError<D>,
handler: HandleCycleError, handler: HandleCycleError,
cache: &dyn crate::query::QueryStorage<Value = V, Stored = R>, cache: &dyn crate::query::QueryStorage<Value = V, Stored = R>,
) -> R ) -> R
where where
Qcx: QueryContext, Qcx: QueryContext + crate::query::HasDepContext<DepKind = D>,
V: std::fmt::Debug + Value<Qcx::DepContext>, V: std::fmt::Debug + Value<Qcx::DepContext, Qcx::DepKind>,
R: Clone, R: Clone,
{ {
let error = report_cycle(qcx.dep_context().sess(), &cycle_error); let error = report_cycle(qcx.dep_context().sess(), &cycle_error);
@ -139,13 +140,13 @@ fn mk_cycle<Qcx, V, R>(
fn handle_cycle_error<Tcx, V>( fn handle_cycle_error<Tcx, V>(
tcx: Tcx, tcx: Tcx,
cycle_error: &CycleError, cycle_error: &CycleError<Tcx::DepKind>,
mut error: DiagnosticBuilder<'_, ErrorGuaranteed>, mut error: DiagnosticBuilder<'_, ErrorGuaranteed>,
handler: HandleCycleError, handler: HandleCycleError,
) -> V ) -> V
where where
Tcx: DepContext, Tcx: DepContext,
V: Value<Tcx>, V: Value<Tcx, Tcx::DepKind>,
{ {
use HandleCycleError::*; use HandleCycleError::*;
match handler { match handler {
@ -165,7 +166,7 @@ fn handle_cycle_error<Tcx, V>(
} }
} }
impl<'tcx, K> JobOwner<'tcx, K> impl<'tcx, K, D: DepKind> JobOwner<'tcx, K, D>
where where
K: Eq + Hash + Clone, K: Eq + Hash + Clone,
{ {
@ -180,12 +181,12 @@ impl<'tcx, K> JobOwner<'tcx, K>
#[inline(always)] #[inline(always)]
fn try_start<'b, Qcx>( fn try_start<'b, Qcx>(
qcx: &'b Qcx, qcx: &'b Qcx,
state: &'b QueryState<K>, state: &'b QueryState<K, Qcx::DepKind>,
span: Span, span: Span,
key: K, key: K,
) -> TryGetJob<'b, K> ) -> TryGetJob<'b, K, D>
where where
Qcx: QueryContext, Qcx: QueryContext + crate::query::HasDepContext<DepKind = D>,
{ {
#[cfg(parallel_compiler)] #[cfg(parallel_compiler)]
let mut state_lock = state.active.get_shard_by_value(&key).lock(); let mut state_lock = state.active.get_shard_by_value(&key).lock();
@ -280,9 +281,10 @@ fn complete<C>(self, cache: &C, result: C::Value, dep_node_index: DepNodeIndex)
} }
} }
impl<'tcx, K> Drop for JobOwner<'tcx, K> impl<'tcx, K, D> Drop for JobOwner<'tcx, K, D>
where where
K: Eq + Hash + Clone, K: Eq + Hash + Clone,
D: DepKind,
{ {
#[inline(never)] #[inline(never)]
#[cold] #[cold]
@ -308,19 +310,20 @@ fn drop(&mut self) {
} }
#[derive(Clone)] #[derive(Clone)]
pub(crate) struct CycleError { pub(crate) struct CycleError<D: DepKind> {
/// The query and related span that uses the cycle. /// The query and related span that uses the cycle.
pub usage: Option<(Span, QueryStackFrame)>, pub usage: Option<(Span, QueryStackFrame<D>)>,
pub cycle: Vec<QueryInfo>, pub cycle: Vec<QueryInfo<D>>,
} }
/// The result of `try_start`. /// The result of `try_start`.
enum TryGetJob<'tcx, K> enum TryGetJob<'tcx, K, D>
where where
K: Eq + Hash + Clone, K: Eq + Hash + Clone,
D: DepKind,
{ {
/// The query is not yet started. Contains a guard to the cache eventually used to start it. /// 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. /// The query was already completed.
/// Returns the result of the query and its dep-node index /// Returns the result of the query and its dep-node index
@ -329,7 +332,7 @@ enum TryGetJob<'tcx, K>
JobCompleted(TimingGuard<'tcx>), JobCompleted(TimingGuard<'tcx>),
/// Trying to execute the query resulted in a cycle. /// 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. /// Checks if the query is already computed and in the cache.
@ -360,7 +363,7 @@ pub fn try_get_cached<'a, Tcx, C, R, OnHit>(
fn try_execute_query<Qcx, C>( fn try_execute_query<Qcx, C>(
qcx: Qcx, qcx: Qcx,
state: &QueryState<C::Key>, state: &QueryState<C::Key, Qcx::DepKind>,
cache: &C, cache: &C,
span: Span, span: Span,
key: C::Key, key: C::Key,
@ -370,11 +373,11 @@ fn try_execute_query<Qcx, C>(
where where
C: QueryCache, C: QueryCache,
C::Key: Clone + DepNodeParams<Qcx::DepContext>, 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>, C::Stored: Debug + std::borrow::Borrow<C::Value>,
Qcx: QueryContext, 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) => { TryGetJob::NotYetStarted(job) => {
let (result, dep_node_index) = execute_job(qcx, key.clone(), dep_node, query, job.id); let (result, dep_node_index) = execute_job(qcx, key.clone(), dep_node, query, job.id);
if query.feedable { if query.feedable {
@ -739,11 +742,12 @@ pub enum QueryMode {
Ensure, 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 where
D: DepKind,
Q: QueryConfig<Qcx>, Q: QueryConfig<Qcx>,
Q::Key: DepNodeParams<Qcx::DepContext>, Q::Key: DepNodeParams<Qcx::DepContext>,
Q::Value: Value<Qcx::DepContext>, Q::Value: Value<Qcx::DepContext, D>,
Qcx: QueryContext, Qcx: QueryContext,
{ {
let query = Q::make_vtable(qcx, &key); let query = Q::make_vtable(qcx, &key);
@ -772,11 +776,12 @@ pub fn get_query<Q, Qcx>(qcx: Qcx, span: Span, key: Q::Key, mode: QueryMode) ->
Some(result) 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 where
D: DepKind,
Q: QueryConfig<Qcx>, Q: QueryConfig<Qcx>,
Q::Key: DepNodeParams<Qcx::DepContext>, Q::Key: DepNodeParams<Qcx::DepContext>,
Q::Value: Value<Qcx::DepContext>, Q::Value: Value<Qcx::DepContext, D>,
Qcx: QueryContext, Qcx: QueryContext,
{ {
// We may be concurrently trying both execute and force a query. // 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; use crate::query::QueryInfo;
pub trait Value<Tcx: DepContext>: Sized { pub trait Value<Tcx: DepContext, D: DepKind>: Sized {
fn from_cycle_error(tcx: Tcx, cycle: &[QueryInfo]) -> Self; fn from_cycle_error(tcx: Tcx, cycle: &[QueryInfo<D>]) -> Self;
} }
impl<Tcx: DepContext, T> Value<Tcx> for T { impl<Tcx: DepContext, T, D: DepKind> Value<Tcx, D> for T {
default fn from_cycle_error(tcx: Tcx, _: &[QueryInfo]) -> T { default fn from_cycle_error(tcx: Tcx, _: &[QueryInfo<D>]) -> T {
tcx.sess().abort_if_errors(); tcx.sess().abort_if_errors();
// Ideally we would use `bug!` here. But bug! is only defined in rustc_middle, and it's // Ideally we would use `bug!` here. But bug! is only defined in rustc_middle, and it's
// non-trivial to define it earlier. // non-trivial to define it earlier.