Auto merge of #80891 - cjgillot:noq, r=Mark-Simulacrum

Make the `Query` enum a simple struct.

A lot of code in `rustc_query_system` is generic over it, only to encode an exceptional error case: query cycles.
The delayed computations are now done at cycle detection.
This commit is contained in:
bors 2021-02-24 03:29:00 +00:00
commit 301ad8a4fa
12 changed files with 298 additions and 315 deletions

View File

@ -4202,6 +4202,7 @@ dependencies = [
"rustc_index", "rustc_index",
"rustc_macros", "rustc_macros",
"rustc_serialize", "rustc_serialize",
"rustc_session",
"rustc_span", "rustc_span",
"smallvec 1.6.1", "smallvec 1.6.1",
"tracing", "tracing",

View File

@ -2,6 +2,7 @@
use crate::ty::{self, TyCtxt}; use crate::ty::{self, TyCtxt};
use rustc_data_structures::profiling::SelfProfilerRef; use rustc_data_structures::profiling::SelfProfilerRef;
use rustc_data_structures::sync::Lock; use rustc_data_structures::sync::Lock;
use rustc_session::Session;
#[macro_use] #[macro_use]
mod dep_node; mod dep_node;
@ -101,20 +102,18 @@ fn create_stable_hashing_context(&self) -> Self::StableHashingContext {
TyCtxt::create_stable_hashing_context(*self) TyCtxt::create_stable_hashing_context(*self)
} }
fn debug_dep_tasks(&self) -> bool {
self.sess.opts.debugging_opts.dep_tasks
}
fn debug_dep_node(&self) -> bool {
self.sess.opts.debugging_opts.incremental_info
|| self.sess.opts.debugging_opts.query_dep_graph
}
#[inline] #[inline]
fn dep_graph(&self) -> &DepGraph { fn dep_graph(&self) -> &DepGraph {
&self.dep_graph &self.dep_graph
} }
#[inline(always)]
fn profiler(&self) -> &SelfProfilerRef { fn profiler(&self) -> &SelfProfilerRef {
&self.prof &self.prof
} }
#[inline(always)]
fn sess(&self) -> &Session {
self.sess
}
} }

View File

@ -17,19 +17,17 @@
extern crate tracing; extern crate tracing;
use rustc_data_structures::fingerprint::Fingerprint; use rustc_data_structures::fingerprint::Fingerprint;
use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
use rustc_errors::{Diagnostic, Handler, Level}; use rustc_errors::{DiagnosticBuilder, Handler};
use rustc_hir::def_id::CrateNum; use rustc_hir::def_id::CrateNum;
use rustc_index::vec::IndexVec; use rustc_index::vec::IndexVec;
use rustc_middle::dep_graph; use rustc_middle::dep_graph;
use rustc_middle::ich::StableHashingContext; use rustc_middle::ich::StableHashingContext;
use rustc_middle::ty::query::{query_keys, query_storage, query_stored, query_values}; use rustc_middle::ty::query::{query_keys, query_storage, query_stored, query_values};
use rustc_middle::ty::query::{Providers, QueryEngine}; use rustc_middle::ty::query::{Providers, QueryEngine};
use rustc_middle::ty::TyCtxt; use rustc_middle::ty::{self, TyCtxt};
use rustc_serialize::opaque; use rustc_serialize::opaque;
use rustc_span::{Span, DUMMY_SP}; use rustc_span::{Span, DUMMY_SP};
use std::mem;
#[macro_use] #[macro_use]
mod plumbing; mod plumbing;

View File

@ -2,22 +2,19 @@
//! 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 super::{queries, Query}; use super::queries;
use rustc_middle::dep_graph::{DepKind, DepNode, DepNodeExt, DepNodeIndex, SerializedDepNodeIndex}; use rustc_middle::dep_graph::{DepKind, DepNode, DepNodeExt, DepNodeIndex, SerializedDepNodeIndex};
use rustc_middle::ty::query::on_disk_cache; use rustc_middle::ty::query::on_disk_cache;
use rustc_middle::ty::tls::{self, ImplicitCtxt}; use rustc_middle::ty::tls::{self, ImplicitCtxt};
use rustc_middle::ty::{self, TyCtxt}; use rustc_middle::ty::{self, TyCtxt};
use rustc_query_system::dep_graph::HasDepContext; use rustc_query_system::dep_graph::HasDepContext;
use rustc_query_system::query::{CycleError, QueryJobId, QueryJobInfo}; use rustc_query_system::query::{QueryContext, QueryDescription, QueryJobId, QueryMap};
use rustc_query_system::query::{QueryContext, QueryDescription};
use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::sync::Lock; use rustc_data_structures::sync::Lock;
use rustc_data_structures::thin_vec::ThinVec; use rustc_data_structures::thin_vec::ThinVec;
use rustc_errors::{struct_span_err, Diagnostic, DiagnosticBuilder}; use rustc_errors::Diagnostic;
use rustc_serialize::opaque; use rustc_serialize::opaque;
use rustc_span::def_id::{DefId, LocalDefId}; use rustc_span::def_id::{DefId, LocalDefId};
use rustc_span::Span;
#[derive(Copy, Clone)] #[derive(Copy, Clone)]
pub struct QueryCtxt<'tcx> { pub struct QueryCtxt<'tcx> {
@ -45,15 +42,6 @@ fn dep_context(&self) -> &Self::DepContext {
} }
impl QueryContext for QueryCtxt<'tcx> { impl QueryContext for QueryCtxt<'tcx> {
type Query = Query<'tcx>;
fn incremental_verify_ich(&self) -> bool {
self.sess.opts.debugging_opts.incremental_verify_ich
}
fn verbose(&self) -> bool {
self.sess.verbose()
}
fn def_path_str(&self, def_id: DefId) -> String { fn def_path_str(&self, def_id: DefId) -> String {
self.tcx.def_path_str(def_id) self.tcx.def_path_str(def_id)
} }
@ -62,11 +50,8 @@ fn current_query_job(&self) -> Option<QueryJobId<Self::DepKind>> {
tls::with_related_context(**self, |icx| icx.query) tls::with_related_context(**self, |icx| icx.query)
} }
fn try_collect_active_jobs( fn try_collect_active_jobs(&self) -> Option<QueryMap<Self::DepKind>> {
&self, self.queries.try_collect_active_jobs(**self)
) -> Option<FxHashMap<QueryJobId<Self::DepKind>, QueryJobInfo<Self::DepKind, Self::Query>>>
{
self.queries.try_collect_active_jobs()
} }
fn try_load_from_on_disk_cache(&self, dep_node: &DepNode) { fn try_load_from_on_disk_cache(&self, dep_node: &DepNode) {
@ -132,14 +117,6 @@ fn try_force_from_dep_node(&self, dep_node: &DepNode) -> bool {
(cb.force_from_dep_node)(*self, dep_node) (cb.force_from_dep_node)(*self, dep_node)
} }
fn has_errors_or_delayed_span_bugs(&self) -> bool {
self.sess.has_errors_or_delayed_span_bugs()
}
fn diagnostic(&self) -> &rustc_errors::Handler {
self.sess.diagnostic()
}
// Interactions with on_disk_cache // Interactions with on_disk_cache
fn load_diagnostics(&self, prev_dep_node_index: SerializedDepNodeIndex) -> Vec<Diagnostic> { fn load_diagnostics(&self, prev_dep_node_index: SerializedDepNodeIndex) -> Vec<Diagnostic> {
self.on_disk_cache self.on_disk_cache
@ -196,54 +173,6 @@ fn start_query<R>(
} }
impl<'tcx> QueryCtxt<'tcx> { impl<'tcx> QueryCtxt<'tcx> {
#[inline(never)]
#[cold]
pub(super) fn report_cycle(
self,
CycleError { usage, cycle: stack }: CycleError<Query<'tcx>>,
) -> DiagnosticBuilder<'tcx> {
assert!(!stack.is_empty());
let fix_span = |span: Span, query: &Query<'tcx>| {
self.sess.source_map().guess_head_span(query.default_span(*self, span))
};
// Disable naming impls with types in this path, since that
// sometimes cycles itself, leading to extra cycle errors.
// (And cycle errors around impls tend to occur during the
// collect/coherence phases anyhow.)
ty::print::with_forced_impl_filename_line(|| {
let span = fix_span(stack[1 % stack.len()].span, &stack[0].query);
let mut err = struct_span_err!(
self.sess,
span,
E0391,
"cycle detected when {}",
stack[0].query.describe(self)
);
for i in 1..stack.len() {
let query = &stack[i].query;
let span = fix_span(stack[(i + 1) % stack.len()].span, query);
err.span_note(span, &format!("...which requires {}...", query.describe(self)));
}
err.note(&format!(
"...which again requires {}, completing the cycle",
stack[0].query.describe(self)
));
if let Some((span, query)) = usage {
err.span_note(
fix_span(span, &query),
&format!("cycle used when {}", query.describe(self)),
);
}
err
})
}
pub(super) fn encode_query_results( pub(super) fn encode_query_results(
self, self,
encoder: &mut on_disk_cache::CacheEncoder<'a, 'tcx, opaque::FileEncoder>, encoder: &mut on_disk_cache::CacheEncoder<'a, 'tcx, opaque::FileEncoder>,
@ -323,16 +252,16 @@ pub struct QueryStruct {
macro_rules! handle_cycle_error { macro_rules! handle_cycle_error {
([][$tcx: expr, $error:expr]) => {{ ([][$tcx: expr, $error:expr]) => {{
$tcx.report_cycle($error).emit(); $error.emit();
Value::from_cycle_error($tcx) Value::from_cycle_error($tcx)
}}; }};
([fatal_cycle $($rest:tt)*][$tcx:expr, $error:expr]) => {{ ([fatal_cycle $($rest:tt)*][$tcx:expr, $error:expr]) => {{
$tcx.report_cycle($error).emit(); $error.emit();
$tcx.sess.abort_if_errors(); $tcx.sess.abort_if_errors();
unreachable!() unreachable!()
}}; }};
([cycle_delay_bug $($rest:tt)*][$tcx:expr, $error:expr]) => {{ ([cycle_delay_bug $($rest:tt)*][$tcx:expr, $error:expr]) => {{
$tcx.report_cycle($error).delay_as_bug(); $error.delay_as_bug();
Value::from_cycle_error($tcx) Value::from_cycle_error($tcx)
}}; }};
([$other:ident $(($($other_args:tt)*))* $(, $($modifiers:tt)*)*][$($args:tt)*]) => { ([$other:ident $(($($other_args:tt)*))* $(, $($modifiers:tt)*)*][$($args:tt)*]) => {
@ -386,55 +315,40 @@ macro_rules! define_queries {
input: ($(([$($modifiers)*] [$($attr)*] [$name]))*) input: ($(([$($modifiers)*] [$($attr)*] [$name]))*)
} }
#[allow(nonstandard_style)] mod make_query {
#[derive(Clone, Debug)] use super::*;
pub enum Query<$tcx> {
$($(#[$attr])* $name(query_keys::$name<$tcx>)),*
}
impl<$tcx> Query<$tcx> { // Create an eponymous constructor for each query.
pub fn name(&self) -> &'static str { $(#[allow(nonstandard_style)] $(#[$attr])*
match *self { pub fn $name<$tcx>(tcx: QueryCtxt<$tcx>, key: query_keys::$name<$tcx>) -> QueryStackFrame {
$(Query::$name(_) => stringify!($name),)* let kind = dep_graph::DepKind::$name;
} let name = stringify!($name);
} let description = ty::print::with_forced_impl_filename_line(
// Force filename-line mode to avoid invoking `type_of` query.
pub(crate) fn describe(&self, tcx: QueryCtxt<$tcx>) -> String { || queries::$name::describe(tcx, key)
let (r, name) = match *self { );
$(Query::$name(key) => { let description = if tcx.sess.verbose() {
(queries::$name::describe(tcx, key), stringify!($name)) format!("{} [{}]", description, name)
})*
};
if tcx.sess.verbose() {
format!("{} [{}]", r, name)
} else { } else {
r description
} };
} let span = if kind == dep_graph::DepKind::def_span {
// The `def_span` query is used to calculate `default_span`,
// so exit to avoid infinite recursion.
None
} else {
Some(key.default_span(*tcx))
};
let hash = || {
let mut hcx = tcx.create_stable_hashing_context();
let mut hasher = StableHasher::new();
std::mem::discriminant(&kind).hash_stable(&mut hcx, &mut hasher);
key.hash_stable(&mut hcx, &mut hasher);
hasher.finish::<u64>()
};
// FIXME(eddyb) Get more valid `Span`s on queries. QueryStackFrame::new(name, description, span, hash)
pub fn default_span(&self, tcx: TyCtxt<$tcx>, span: Span) -> Span { })*
if !span.is_dummy() {
return span;
}
// The `def_span` query is used to calculate `default_span`,
// so exit to avoid infinite recursion.
if let Query::def_span(..) = *self {
return span
}
match *self {
$(Query::$name(key) => key.default_span(tcx),)*
}
}
}
impl<'a, $tcx> HashStable<StableHashingContext<'a>> for Query<$tcx> {
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
mem::discriminant(self).hash_stable(hcx, hasher);
match *self {
$(Query::$name(key) => key.hash_stable(hcx, hasher),)*
}
}
} }
#[allow(nonstandard_style)] #[allow(nonstandard_style)]
@ -461,7 +375,9 @@ impl<$tcx> QueryAccessors<QueryCtxt<$tcx>> for queries::$name<$tcx> {
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<crate::dep_graph::DepKind, Query<$tcx>, Self::Key> { fn query_state<'a>(tcx: QueryCtxt<$tcx>) -> &'a QueryState<crate::dep_graph::DepKind, Self::Key>
where QueryCtxt<$tcx>: 'a
{
&tcx.queries.$name &tcx.queries.$name
} }
@ -493,7 +409,7 @@ fn hash_result(
fn handle_cycle_error( fn handle_cycle_error(
tcx: QueryCtxt<'tcx>, tcx: QueryCtxt<'tcx>,
error: CycleError<Query<'tcx>> mut error: DiagnosticBuilder<'_>,
) -> Self::Value { ) -> Self::Value {
handle_cycle_error!([$($modifiers)*][tcx, error]) handle_cycle_error!([$($modifiers)*][tcx, error])
} }
@ -596,7 +512,6 @@ pub struct Queries<$tcx> {
$($(#[$attr])* $name: QueryState< $($(#[$attr])* $name: QueryState<
crate::dep_graph::DepKind, crate::dep_graph::DepKind,
Query<$tcx>,
query_keys::$name<$tcx>, query_keys::$name<$tcx>,
>,)* >,)*
} }
@ -614,14 +529,17 @@ pub fn new(
} }
pub(crate) fn try_collect_active_jobs( pub(crate) fn try_collect_active_jobs(
&self &$tcx self,
) -> Option<FxHashMap<QueryJobId<crate::dep_graph::DepKind>, QueryJobInfo<crate::dep_graph::DepKind, Query<$tcx>>>> { tcx: TyCtxt<$tcx>,
let mut jobs = FxHashMap::default(); ) -> Option<QueryMap<crate::dep_graph::DepKind>> {
let tcx = QueryCtxt { tcx, queries: self };
let mut jobs = QueryMap::default();
$( $(
self.$name.try_collect_active_jobs( self.$name.try_collect_active_jobs(
<queries::$name<'tcx> as QueryAccessors<QueryCtxt<'tcx>>>::DEP_KIND, tcx,
Query::$name, dep_graph::DepKind::$name,
make_query::$name,
&mut jobs, &mut jobs,
)?; )?;
)* )*
@ -666,38 +584,8 @@ fn try_print_query_stack(
handler: &Handler, handler: &Handler,
num_frames: Option<usize>, num_frames: Option<usize>,
) -> usize { ) -> usize {
let query_map = self.try_collect_active_jobs(); let qcx = QueryCtxt { tcx, queries: self };
rustc_query_system::query::print_query_stack(qcx, query, handler, num_frames)
let mut current_query = query;
let mut i = 0;
while let Some(query) = current_query {
if Some(i) == num_frames {
break;
}
let query_info = if let Some(info) = query_map.as_ref().and_then(|map| map.get(&query))
{
info
} else {
break;
};
let mut diag = Diagnostic::new(
Level::FailureNote,
&format!(
"#{} [{}] {}",
i,
query_info.info.query.name(),
query_info.info.query.describe(QueryCtxt { tcx, queries: self })
),
);
diag.span = tcx.sess.source_map().guess_head_span(query_info.info.span).into();
handler.force_print_diagnostic(diag);
current_query = query_info.job.parent;
i += 1;
}
i
} }
$($(#[$attr])* $($(#[$attr])*

View File

@ -16,6 +16,7 @@ rustc_errors = { path = "../rustc_errors" }
rustc_macros = { path = "../rustc_macros" } rustc_macros = { path = "../rustc_macros" }
rustc_index = { path = "../rustc_index" } rustc_index = { path = "../rustc_index" }
rustc_serialize = { path = "../rustc_serialize" } rustc_serialize = { path = "../rustc_serialize" }
rustc_session = { path = "../rustc_session" }
rustc_span = { path = "../rustc_span" } rustc_span = { path = "../rustc_span" }
parking_lot = "0.11" parking_lot = "0.11"
smallvec = { version = "1.6.1", features = ["union", "may_dangle"] } smallvec = { version = "1.6.1", features = ["union", "may_dangle"] }

View File

@ -87,7 +87,10 @@ pub fn construct<Ctxt, Key>(tcx: Ctxt, kind: K, arg: &Key) -> DepNode<K>
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
{ {
if !kind.can_reconstruct_query_key() && tcx.debug_dep_node() { if !kind.can_reconstruct_query_key()
&& (tcx.sess().opts.debugging_opts.incremental_info
|| tcx.sess().opts.debugging_opts.query_dep_graph)
{
tcx.dep_graph().register_dep_node_debug_str(dep_node, || arg.to_debug_str(tcx)); tcx.dep_graph().register_dep_node_debug_str(dep_node, || arg.to_debug_str(tcx));
} }
} }

View File

@ -280,7 +280,7 @@ fn with_task_impl<Ctxt: HasDepContext<DepKind = K>, A, R>(
let mut hcx = dcx.create_stable_hashing_context(); let mut hcx = dcx.create_stable_hashing_context();
let current_fingerprint = hash_result(&mut hcx, &result); let current_fingerprint = hash_result(&mut hcx, &result);
let print_status = cfg!(debug_assertions) && dcx.debug_dep_tasks(); let print_status = cfg!(debug_assertions) && dcx.sess().opts.debugging_opts.dep_tasks;
// Intern the new `DepNode`. // Intern the new `DepNode`.
let dep_node_index = if let Some(prev_index) = data.previous.node_to_index_opt(&key) { let dep_node_index = if let Some(prev_index) = data.previous.node_to_index_opt(&key) {
@ -731,7 +731,7 @@ fn try_mark_previous_green<Ctxt: QueryContext<DepKind = K>>(
return None; return None;
} }
None => { None => {
if !tcx.has_errors_or_delayed_span_bugs() { if !tcx.dep_context().sess().has_errors_or_delayed_span_bugs() {
panic!( panic!(
"try_mark_previous_green() - Forcing the DepNode \ "try_mark_previous_green() - Forcing the DepNode \
should have set its color" should have set its color"
@ -835,7 +835,7 @@ fn emit_diagnostics<Ctxt: QueryContext<DepKind = K>>(
// Promote the previous diagnostics to the current session. // Promote the previous diagnostics to the current session.
tcx.store_diagnostics(dep_node_index, diagnostics.clone().into()); tcx.store_diagnostics(dep_node_index, diagnostics.clone().into());
let handle = tcx.diagnostic(); let handle = tcx.dep_context().sess().diagnostic();
for diagnostic in diagnostics { for diagnostic in diagnostics {
handle.emit_diagnostic(&diagnostic); handle.emit_diagnostic(&diagnostic);

View File

@ -13,6 +13,7 @@
use rustc_data_structures::profiling::SelfProfilerRef; use rustc_data_structures::profiling::SelfProfilerRef;
use rustc_data_structures::sync::Lock; use rustc_data_structures::sync::Lock;
use rustc_session::Session;
use std::fmt; use std::fmt;
use std::hash::Hash; use std::hash::Hash;
@ -24,9 +25,6 @@ pub trait DepContext: Copy {
/// Create a hashing context for hashing new results. /// Create a hashing context for hashing new results.
fn create_stable_hashing_context(&self) -> Self::StableHashingContext; fn create_stable_hashing_context(&self) -> Self::StableHashingContext;
fn debug_dep_tasks(&self) -> bool;
fn debug_dep_node(&self) -> bool;
/// Access the DepGraph. /// Access the DepGraph.
fn dep_graph(&self) -> &DepGraph<Self::DepKind>; fn dep_graph(&self) -> &DepGraph<Self::DepKind>;
@ -34,6 +32,9 @@ pub trait DepContext: Copy {
/// Access the profiler. /// Access the profiler.
fn profiler(&self) -> &SelfProfilerRef; fn profiler(&self) -> &SelfProfilerRef;
/// Access the compiler session.
fn sess(&self) -> &Session;
} }
pub trait HasDepContext: Copy { pub trait HasDepContext: Copy {

View File

@ -3,10 +3,10 @@
use crate::dep_graph::DepNode; use crate::dep_graph::DepNode;
use crate::dep_graph::SerializedDepNodeIndex; use crate::dep_graph::SerializedDepNodeIndex;
use crate::query::caches::QueryCache; use crate::query::caches::QueryCache;
use crate::query::plumbing::CycleError;
use crate::query::{QueryCacheStore, QueryContext, QueryState}; use crate::query::{QueryCacheStore, QueryContext, QueryState};
use rustc_data_structures::fingerprint::Fingerprint; use rustc_data_structures::fingerprint::Fingerprint;
use rustc_errors::DiagnosticBuilder;
use std::fmt::Debug; use std::fmt::Debug;
use std::hash::Hash; use std::hash::Hash;
@ -27,7 +27,7 @@ pub(crate) struct QueryVtable<CTX: QueryContext, K, V> {
pub compute: fn(CTX, K) -> V, pub compute: fn(CTX, K) -> V,
pub hash_result: fn(&mut CTX::StableHashingContext, &V) -> Option<Fingerprint>, pub hash_result: fn(&mut CTX::StableHashingContext, &V) -> Option<Fingerprint>,
pub handle_cycle_error: fn(CTX, CycleError<CTX::Query>) -> V, pub handle_cycle_error: fn(CTX, DiagnosticBuilder<'_>) -> V,
pub cache_on_disk: fn(CTX, &K, Option<&V>) -> bool, pub cache_on_disk: fn(CTX, &K, Option<&V>) -> bool,
pub try_load_from_disk: fn(CTX, SerializedDepNodeIndex) -> Option<V>, pub try_load_from_disk: fn(CTX, SerializedDepNodeIndex) -> Option<V>,
} }
@ -52,8 +52,8 @@ pub(crate) fn hash_result(
(self.hash_result)(hcx, value) (self.hash_result)(hcx, value)
} }
pub(crate) fn handle_cycle_error(&self, tcx: CTX, error: CycleError<CTX::Query>) -> V { pub(crate) fn handle_cycle_error(&self, tcx: CTX, diag: DiagnosticBuilder<'_>) -> V {
(self.handle_cycle_error)(tcx, error) (self.handle_cycle_error)(tcx, diag)
} }
pub(crate) fn cache_on_disk(&self, tcx: CTX, key: &K, value: Option<&V>) -> bool { pub(crate) fn cache_on_disk(&self, tcx: CTX, key: &K, value: Option<&V>) -> bool {
@ -73,7 +73,9 @@ pub trait QueryAccessors<CTX: QueryContext>: QueryConfig {
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: CTX) -> &'a QueryState<CTX::DepKind, CTX::Query, Self::Key>; fn query_state<'a>(tcx: CTX) -> &'a QueryState<CTX::DepKind, Self::Key>
where
CTX: 'a;
// 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_cache<'a>(tcx: CTX) -> &'a QueryCacheStore<Self::Cache> fn query_cache<'a>(tcx: CTX) -> &'a QueryCacheStore<Self::Cache>
@ -88,7 +90,7 @@ fn hash_result(
result: &Self::Value, result: &Self::Value,
) -> Option<Fingerprint>; ) -> Option<Fingerprint>;
fn handle_cycle_error(tcx: CTX, error: CycleError<CTX::Query>) -> Self::Value; fn handle_cycle_error(tcx: CTX, diag: DiagnosticBuilder<'_>) -> Self::Value;
} }
pub trait QueryDescription<CTX: QueryContext>: QueryAccessors<CTX> { pub trait QueryDescription<CTX: QueryContext>: QueryAccessors<CTX> {

View File

@ -1,6 +1,10 @@
use crate::dep_graph::DepContext;
use crate::query::plumbing::CycleError; use crate::query::plumbing::CycleError;
use crate::query::{QueryContext, QueryStackFrame};
use rustc_data_structures::fx::FxHashMap; use rustc_data_structures::fx::FxHashMap;
use rustc_errors::{struct_span_err, Diagnostic, DiagnosticBuilder, Handler, Level};
use rustc_session::Session;
use rustc_span::Span; use rustc_span::Span;
use std::convert::TryFrom; use std::convert::TryFrom;
@ -10,11 +14,9 @@
#[cfg(parallel_compiler)] #[cfg(parallel_compiler)]
use { use {
crate::dep_graph::DepContext, crate::dep_graph::DepKind,
crate::query::QueryContext,
parking_lot::{Condvar, Mutex}, parking_lot::{Condvar, Mutex},
rustc_data_structures::fx::FxHashSet, rustc_data_structures::fx::FxHashSet,
rustc_data_structures::stable_hasher::{HashStable, StableHasher},
rustc_data_structures::sync::Lock, rustc_data_structures::sync::Lock,
rustc_data_structures::sync::Lrc, rustc_data_structures::sync::Lrc,
rustc_data_structures::{jobserver, OnDrop}, rustc_data_structures::{jobserver, OnDrop},
@ -26,13 +28,13 @@
/// Represents a span and a query key. /// Represents a span and a query key.
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct QueryInfo<Q> { pub struct QueryInfo {
/// 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: Q, pub query: QueryStackFrame,
} }
pub(crate) type QueryMap<D, Q> = FxHashMap<QueryJobId<D>, QueryJobInfo<D, Q>>; pub type QueryMap<D> = FxHashMap<QueryJobId<D>, QueryJobInfo<D>>;
/// A value uniquely identifying an active query job within a shard in the query cache. /// A value uniquely identifying an active query job within a shard in the query cache.
#[derive(Copy, Clone, Eq, PartialEq, Hash)] #[derive(Copy, Clone, Eq, PartialEq, Hash)]
@ -59,34 +61,34 @@ pub fn new(job: QueryShardJobId, shard: usize, kind: D) -> Self {
QueryJobId { job, shard: u16::try_from(shard).unwrap(), kind } QueryJobId { job, shard: u16::try_from(shard).unwrap(), kind }
} }
fn query<Q: Clone>(self, map: &QueryMap<D, Q>) -> Q { fn query(self, map: &QueryMap<D>) -> QueryStackFrame {
map.get(&self).unwrap().info.query.clone() map.get(&self).unwrap().info.query.clone()
} }
#[cfg(parallel_compiler)] #[cfg(parallel_compiler)]
fn span<Q: Clone>(self, map: &QueryMap<D, Q>) -> Span { fn span(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<Q: Clone>(self, map: &QueryMap<D, Q>) -> Option<QueryJobId<D>> { fn parent(self, map: &QueryMap<D>) -> Option<QueryJobId<D>> {
map.get(&self).unwrap().job.parent map.get(&self).unwrap().job.parent
} }
#[cfg(parallel_compiler)] #[cfg(parallel_compiler)]
fn latch<'a, Q: Clone>(self, map: &'a QueryMap<D, Q>) -> Option<&'a QueryLatch<D, Q>> { fn latch<'a>(self, map: &'a QueryMap<D>) -> Option<&'a QueryLatch<D>> {
map.get(&self).unwrap().job.latch.as_ref() map.get(&self).unwrap().job.latch.as_ref()
} }
} }
pub struct QueryJobInfo<D, Q> { pub struct QueryJobInfo<D> {
pub info: QueryInfo<Q>, pub info: QueryInfo,
pub job: QueryJob<D, Q>, pub job: QueryJob<D>,
} }
/// Represents an active query job. /// Represents an active query job.
#[derive(Clone)] #[derive(Clone)]
pub struct QueryJob<D, Q> { pub struct QueryJob<D> {
pub id: QueryShardJobId, pub id: QueryShardJobId,
/// The span corresponding to the reason for which this query was required. /// The span corresponding to the reason for which this query was required.
@ -97,15 +99,14 @@ pub struct QueryJob<D, Q> {
/// 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<D, Q>>, latch: Option<QueryLatch<D>>,
dummy: PhantomData<QueryLatch<D, Q>>, dummy: PhantomData<QueryLatch<D>>,
} }
impl<D, Q> QueryJob<D, Q> impl<D> QueryJob<D>
where where
D: Copy + Clone + Eq + Hash, D: Copy + Clone + Eq + Hash,
Q: Clone,
{ {
/// Creates a new query job. /// Creates a new query job.
pub fn new(id: QueryShardJobId, span: Span, parent: Option<QueryJobId<D>>) -> Self { pub fn new(id: QueryShardJobId, span: Span, parent: Option<QueryJobId<D>>) -> Self {
@ -120,7 +121,7 @@ pub fn new(id: QueryShardJobId, span: Span, parent: Option<QueryJobId<D>>) -> Se
} }
#[cfg(parallel_compiler)] #[cfg(parallel_compiler)]
pub(super) fn latch(&mut self, _id: QueryJobId<D>) -> QueryLatch<D, Q> { pub(super) fn latch(&mut self, _id: QueryJobId<D>) -> QueryLatch<D> {
if self.latch.is_none() { if self.latch.is_none() {
self.latch = Some(QueryLatch::new()); self.latch = Some(QueryLatch::new());
} }
@ -128,8 +129,8 @@ pub(super) fn latch(&mut self, _id: QueryJobId<D>) -> QueryLatch<D, Q> {
} }
#[cfg(not(parallel_compiler))] #[cfg(not(parallel_compiler))]
pub(super) fn latch(&mut self, id: QueryJobId<D>) -> QueryLatch<D, Q> { pub(super) fn latch(&mut self, id: QueryJobId<D>) -> QueryLatch<D> {
QueryLatch { id, dummy: PhantomData } QueryLatch { id }
} }
/// Signals to waiters that the query is complete. /// Signals to waiters that the query is complete.
@ -148,23 +149,21 @@ pub fn signal_complete(self) {
#[cfg(not(parallel_compiler))] #[cfg(not(parallel_compiler))]
#[derive(Clone)] #[derive(Clone)]
pub(super) struct QueryLatch<D, Q> { pub(super) struct QueryLatch<D> {
id: QueryJobId<D>, id: QueryJobId<D>,
dummy: PhantomData<Q>,
} }
#[cfg(not(parallel_compiler))] #[cfg(not(parallel_compiler))]
impl<D, Q> QueryLatch<D, Q> impl<D> QueryLatch<D>
where where
D: Copy + Clone + Eq + Hash, D: Copy + Clone + Eq + Hash,
Q: Clone,
{ {
pub(super) fn find_cycle_in_stack( pub(super) fn find_cycle_in_stack(
&self, &self,
query_map: QueryMap<D, Q>, query_map: QueryMap<D>,
current_job: &Option<QueryJobId<D>>, current_job: &Option<QueryJobId<D>>,
span: Span, span: Span,
) -> CycleError<Q> { ) -> CycleError {
// 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);
@ -198,15 +197,15 @@ pub(super) fn find_cycle_in_stack(
} }
#[cfg(parallel_compiler)] #[cfg(parallel_compiler)]
struct QueryWaiter<D, Q> { struct QueryWaiter<D> {
query: Option<QueryJobId<D>>, query: Option<QueryJobId<D>>,
condvar: Condvar, condvar: Condvar,
span: Span, span: Span,
cycle: Lock<Option<CycleError<Q>>>, cycle: Lock<Option<CycleError>>,
} }
#[cfg(parallel_compiler)] #[cfg(parallel_compiler)]
impl<D, Q> QueryWaiter<D, Q> { impl<D> 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();
@ -214,19 +213,19 @@ fn notify(&self, registry: &rayon_core::Registry) {
} }
#[cfg(parallel_compiler)] #[cfg(parallel_compiler)]
struct QueryLatchInfo<D, Q> { struct QueryLatchInfo<D> {
complete: bool, complete: bool,
waiters: Vec<Lrc<QueryWaiter<D, Q>>>, waiters: Vec<Lrc<QueryWaiter<D>>>,
} }
#[cfg(parallel_compiler)] #[cfg(parallel_compiler)]
#[derive(Clone)] #[derive(Clone)]
pub(super) struct QueryLatch<D, Q> { pub(super) struct QueryLatch<D> {
info: Lrc<Mutex<QueryLatchInfo<D, Q>>>, info: Lrc<Mutex<QueryLatchInfo<D>>>,
} }
#[cfg(parallel_compiler)] #[cfg(parallel_compiler)]
impl<D: Eq + Hash, Q: Clone> QueryLatch<D, Q> { impl<D: Eq + Hash> 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() })),
@ -235,13 +234,13 @@ fn new() -> Self {
} }
#[cfg(parallel_compiler)] #[cfg(parallel_compiler)]
impl<D, Q> QueryLatch<D, Q> { impl<D> QueryLatch<D> {
/// Awaits for the query job to complete. /// Awaits for the query job to complete.
pub(super) fn wait_on( pub(super) fn wait_on(
&self, &self,
query: Option<QueryJobId<D>>, query: Option<QueryJobId<D>>,
span: Span, span: Span,
) -> Result<(), CycleError<Q>> { ) -> Result<(), CycleError> {
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);
@ -256,7 +255,7 @@ pub(super) fn wait_on(
} }
/// 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<D, Q>>) { 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
@ -290,7 +289,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<D, Q>> { 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
@ -312,14 +311,13 @@ fn extract_waiter(&self, waiter: usize) -> Lrc<QueryWaiter<D, Q>> {
/// 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<D, Q, F>( fn visit_waiters<D, F>(
query_map: &QueryMap<D, Q>, query_map: &QueryMap<D>,
query: QueryJobId<D>, query: QueryJobId<D>,
mut visit: F, mut visit: F,
) -> Option<Option<Waiter<D>>> ) -> Option<Option<Waiter<D>>>
where where
D: Copy + Clone + Eq + Hash, D: Copy + Clone + Eq + Hash,
Q: Clone,
F: FnMut(Span, QueryJobId<D>) -> Option<Option<Waiter<D>>>, F: FnMut(Span, QueryJobId<D>) -> Option<Option<Waiter<D>>>,
{ {
// 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
@ -349,8 +347,8 @@ fn visit_waiters<D, Q, F>(
/// 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<D, Q>( fn cycle_check<D>(
query_map: &QueryMap<D, Q>, query_map: &QueryMap<D>,
query: QueryJobId<D>, query: QueryJobId<D>,
span: Span, span: Span,
stack: &mut Vec<(Span, QueryJobId<D>)>, stack: &mut Vec<(Span, QueryJobId<D>)>,
@ -358,7 +356,6 @@ fn cycle_check<D, Q>(
) -> Option<Option<Waiter<D>>> ) -> Option<Option<Waiter<D>>>
where where
D: Copy + Clone + Eq + Hash, D: Copy + Clone + Eq + Hash,
Q: Clone,
{ {
if !visited.insert(query) { if !visited.insert(query) {
return if let Some(p) = stack.iter().position(|q| q.1 == query) { return if let Some(p) = stack.iter().position(|q| q.1 == query) {
@ -394,14 +391,13 @@ fn cycle_check<D, Q>(
/// 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<D, Q>( fn connected_to_root<D>(
query_map: &QueryMap<D, Q>, query_map: &QueryMap<D>,
query: QueryJobId<D>, query: QueryJobId<D>,
visited: &mut FxHashSet<QueryJobId<D>>, visited: &mut FxHashSet<QueryJobId<D>>,
) -> bool ) -> bool
where where
D: Copy + Clone + Eq + Hash, D: Copy + Clone + Eq + Hash,
Q: Clone,
{ {
// We already visited this or we're deliberately ignoring it // We already visited this or we're deliberately ignoring it
if !visited.insert(query) { if !visited.insert(query) {
@ -421,30 +417,23 @@ fn connected_to_root<D, Q>(
// Deterministically pick an query from a list // Deterministically pick an query from a list
#[cfg(parallel_compiler)] #[cfg(parallel_compiler)]
fn pick_query<'a, CTX, T, F>( fn pick_query<'a, D, T, F>(query_map: &QueryMap<D>, queries: &'a [T], f: F) -> &'a T
query_map: &QueryMap<CTX::DepKind, CTX::Query>,
tcx: CTX,
queries: &'a [T],
f: F,
) -> &'a T
where where
CTX: QueryContext, D: Copy + Clone + Eq + Hash,
F: Fn(&T) -> (Span, QueryJobId<CTX::DepKind>), F: Fn(&T) -> (Span, QueryJobId<D>),
{ {
// Deterministically pick an entry point // Deterministically pick an entry point
// FIXME: Sort this instead // FIXME: Sort this instead
let mut hcx = tcx.dep_context().create_stable_hashing_context();
queries queries
.iter() .iter()
.min_by_key(|v| { .min_by_key(|v| {
let (span, query) = f(v); let (span, query) = f(v);
let mut stable_hasher = StableHasher::new(); let hash = query.query(query_map).hash;
query.query(query_map).hash_stable(&mut hcx, &mut stable_hasher);
// Prefer entry points which have valid spans for nicer error messages // Prefer entry points which have valid spans for nicer error messages
// We add an integer to the tuple ensuring that entry points // We add an integer to the tuple ensuring that entry points
// with valid spans are picked first // with valid spans are picked first
let span_cmp = if span == DUMMY_SP { 1 } else { 0 }; let span_cmp = if span == DUMMY_SP { 1 } else { 0 };
(span_cmp, stable_hasher.finish::<u64>()) (span_cmp, hash)
}) })
.unwrap() .unwrap()
} }
@ -455,11 +444,10 @@ fn pick_query<'a, CTX, T, F>(
/// 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<CTX: QueryContext>( fn remove_cycle<D: DepKind>(
query_map: &QueryMap<CTX::DepKind, CTX::Query>, query_map: &QueryMap<D>,
jobs: &mut Vec<QueryJobId<CTX::DepKind>>, jobs: &mut Vec<QueryJobId<D>>,
wakelist: &mut Vec<Lrc<QueryWaiter<CTX::DepKind, CTX::Query>>>, wakelist: &mut Vec<Lrc<QueryWaiter<D>>>,
tcx: CTX,
) -> bool { ) -> bool {
let mut visited = FxHashSet::default(); let mut visited = FxHashSet::default();
let mut stack = Vec::new(); let mut stack = Vec::new();
@ -509,15 +497,15 @@ fn remove_cycle<CTX: QueryContext>(
None None
} else { } else {
// Deterministically pick one of the waiters to show to the user // Deterministically pick one of the waiters to show to the user
let waiter = *pick_query(query_map, tcx, &waiters, |s| *s); let waiter = *pick_query(query_map, &waiters, |s| *s);
Some((span, query, Some(waiter))) Some((span, query, Some(waiter)))
} }
} }
}) })
.collect::<Vec<(Span, QueryJobId<CTX::DepKind>, Option<(Span, QueryJobId<CTX::DepKind>)>)>>(); .collect::<Vec<(Span, QueryJobId<D>, Option<(Span, QueryJobId<D>)>)>>();
// Deterministically pick an entry point // Deterministically pick an entry point
let (_, entry_point, usage) = pick_query(query_map, tcx, &entry_points, |e| (e.0, e.1)); let (_, entry_point, usage) = pick_query(query_map, &entry_points, |e| (e.0, e.1));
// Shift the stack so that our entry point is first // Shift the stack so that our entry point is first
let entry_point_pos = stack.iter().position(|(_, query)| query == entry_point); let entry_point_pos = stack.iter().position(|(_, query)| query == entry_point);
@ -574,7 +562,7 @@ pub fn deadlock<CTX: QueryContext>(tcx: CTX, registry: &rayon_core::Registry) {
let mut found_cycle = false; let mut found_cycle = false;
while jobs.len() > 0 { while jobs.len() > 0 {
if remove_cycle(&query_map, &mut jobs, &mut wakelist, tcx) { if remove_cycle(&query_map, &mut jobs, &mut wakelist) {
found_cycle = true; found_cycle = true;
} }
} }
@ -595,3 +583,76 @@ pub fn deadlock<CTX: QueryContext>(tcx: CTX, registry: &rayon_core::Registry) {
on_panic.disable(); on_panic.disable();
} }
#[inline(never)]
#[cold]
pub(crate) fn report_cycle<'a>(
sess: &'a Session,
CycleError { usage, cycle: stack }: CycleError,
) -> DiagnosticBuilder<'a> {
assert!(!stack.is_empty());
let fix_span = |span: Span, query: &QueryStackFrame| {
sess.source_map().guess_head_span(query.default_span(span))
};
let span = fix_span(stack[1 % stack.len()].span, &stack[0].query);
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;
let span = fix_span(stack[(i + 1) % stack.len()].span, query);
err.span_note(span, &format!("...which requires {}...", query.description));
}
err.note(&format!(
"...which again requires {}, completing the cycle",
stack[0].query.description
));
if let Some((span, query)) = usage {
err.span_note(fix_span(span, &query), &format!("cycle used when {}", query.description));
}
err
}
pub fn print_query_stack<CTX: QueryContext>(
tcx: CTX,
mut current_query: Option<QueryJobId<CTX::DepKind>>,
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;
}
let query_info = if let Some(info) = query_map.as_ref().and_then(|map| map.get(&query)) {
info
} else {
break;
};
let mut diag = Diagnostic::new(
Level::FailureNote,
&format!(
"#{} [{}] {}",
i, query_info.info.query.name, query_info.info.query.description
),
);
diag.span =
tcx.dep_context().sess().source_map().guess_head_span(query_info.info.span).into();
handler.force_print_diagnostic(diag);
current_query = query_info.job.parent;
i += 1;
}
i
}

View File

@ -4,7 +4,7 @@
mod job; mod job;
#[cfg(parallel_compiler)] #[cfg(parallel_compiler)]
pub use self::job::deadlock; pub use self::job::deadlock;
pub use self::job::{QueryInfo, QueryJob, QueryJobId, QueryJobInfo}; pub use self::job::{print_query_stack, QueryInfo, QueryJob, QueryJobId, QueryJobInfo, QueryMap};
mod caches; mod caches;
pub use self::caches::{ pub use self::caches::{
@ -15,27 +15,62 @@
pub use self::config::{QueryAccessors, QueryConfig, QueryDescription}; pub use self::config::{QueryAccessors, QueryConfig, QueryDescription};
use crate::dep_graph::{DepNode, DepNodeIndex, HasDepContext, SerializedDepNodeIndex}; use crate::dep_graph::{DepNode, DepNodeIndex, HasDepContext, SerializedDepNodeIndex};
use crate::query::job::QueryMap;
use rustc_data_structures::stable_hasher::HashStable;
use rustc_data_structures::sync::Lock; use rustc_data_structures::sync::Lock;
use rustc_data_structures::thin_vec::ThinVec; use rustc_data_structures::thin_vec::ThinVec;
use rustc_errors::Diagnostic; use rustc_errors::Diagnostic;
use rustc_span::def_id::DefId; use rustc_span::def_id::DefId;
use rustc_span::Span;
/// Description of a frame in the query stack.
///
/// This is mostly used in case of cycles for error reporting.
#[derive(Clone, Debug)]
pub struct QueryStackFrame {
pub name: &'static str,
pub description: String,
span: Option<Span>,
/// This hash is used to deterministically pick
/// a query to remove cycles in the parallel compiler.
#[cfg(parallel_compiler)]
hash: u64,
}
impl QueryStackFrame {
#[inline]
pub fn new(
name: &'static str,
description: String,
span: Option<Span>,
_hash: impl FnOnce() -> u64,
) -> Self {
Self {
name,
description,
span,
#[cfg(parallel_compiler)]
hash: _hash(),
}
}
// FIXME(eddyb) Get more valid `Span`s on queries.
#[inline]
pub fn default_span(&self, span: Span) -> Span {
if !span.is_dummy() {
return span;
}
self.span.unwrap_or(span)
}
}
pub trait QueryContext: HasDepContext { pub trait QueryContext: HasDepContext {
type Query: Clone + HashStable<Self::StableHashingContext>;
fn incremental_verify_ich(&self) -> bool;
fn verbose(&self) -> bool;
/// Get string representation from DefPath. /// Get string representation from DefPath.
fn def_path_str(&self, def_id: DefId) -> String; fn def_path_str(&self, def_id: DefId) -> String;
/// Get the query information from the TLS context. /// Get the query information from the TLS context.
fn current_query_job(&self) -> Option<QueryJobId<Self::DepKind>>; fn current_query_job(&self) -> Option<QueryJobId<Self::DepKind>>;
fn try_collect_active_jobs(&self) -> Option<QueryMap<Self::DepKind, Self::Query>>; fn try_collect_active_jobs(&self) -> Option<QueryMap<Self::DepKind>>;
/// Load data from the on-disk cache. /// Load data from the on-disk cache.
fn try_load_from_on_disk_cache(&self, dep_node: &DepNode<Self::DepKind>); fn try_load_from_on_disk_cache(&self, dep_node: &DepNode<Self::DepKind>);
@ -43,12 +78,6 @@ pub trait QueryContext: HasDepContext {
/// Try to force a dep node to execute and see if it's green. /// Try to force a dep node to execute and see if it's green.
fn try_force_from_dep_node(&self, dep_node: &DepNode<Self::DepKind>) -> bool; fn try_force_from_dep_node(&self, dep_node: &DepNode<Self::DepKind>) -> bool;
/// Return whether the current session is tainted by errors.
fn has_errors_or_delayed_span_bugs(&self) -> bool;
/// Return the diagnostic handler.
fn diagnostic(&self) -> &rustc_errors::Handler;
/// Load diagnostics associated to the node in the previous session. /// Load diagnostics associated to the node in the previous session.
fn load_diagnostics(&self, prev_dep_node_index: SerializedDepNodeIndex) -> Vec<Diagnostic>; fn load_diagnostics(&self, prev_dep_node_index: SerializedDepNodeIndex) -> Vec<Diagnostic>;

View File

@ -6,8 +6,10 @@
use crate::dep_graph::{DepNodeIndex, SerializedDepNodeIndex}; use crate::dep_graph::{DepNodeIndex, SerializedDepNodeIndex};
use crate::query::caches::QueryCache; use crate::query::caches::QueryCache;
use crate::query::config::{QueryDescription, QueryVtable, QueryVtableExt}; use crate::query::config::{QueryDescription, QueryVtable, QueryVtableExt};
use crate::query::job::{QueryInfo, QueryJob, QueryJobId, QueryJobInfo, QueryShardJobId}; use crate::query::job::{
use crate::query::{QueryContext, QueryMap}; report_cycle, QueryInfo, QueryJob, QueryJobId, QueryJobInfo, QueryShardJobId,
};
use crate::query::{QueryContext, QueryMap, QueryStackFrame};
#[cfg(not(parallel_compiler))] #[cfg(not(parallel_compiler))]
use rustc_data_structures::cold_path; use rustc_data_structures::cold_path;
@ -81,37 +83,36 @@ pub fn iter_results<R>(
} }
} }
struct QueryStateShard<D, Q, K> { struct QueryStateShard<D, K> {
active: FxHashMap<K, QueryResult<D, Q>>, active: FxHashMap<K, QueryResult<D>>,
/// Used to generate unique ids for active jobs. /// Used to generate unique ids for active jobs.
jobs: u32, jobs: u32,
} }
impl<D, Q, K> Default for QueryStateShard<D, Q, K> { impl<D, K> Default for QueryStateShard<D, K> {
fn default() -> QueryStateShard<D, Q, K> { fn default() -> QueryStateShard<D, K> {
QueryStateShard { active: Default::default(), jobs: 0 } QueryStateShard { active: Default::default(), jobs: 0 }
} }
} }
pub struct QueryState<D, Q, K> { pub struct QueryState<D, K> {
shards: Sharded<QueryStateShard<D, Q, K>>, shards: Sharded<QueryStateShard<D, K>>,
} }
/// 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<D, Q> { enum QueryResult<D> {
/// 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<D, Q>), 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<D, Q, K> QueryState<D, Q, K> impl<D, K> QueryState<D, K>
where where
D: Copy + Clone + Eq + Hash, D: Copy + Clone + Eq + Hash,
Q: Clone,
K: Eq + Hash + Clone + Debug, K: Eq + Hash + Clone + Debug,
{ {
pub fn all_inactive(&self) -> bool { pub fn all_inactive(&self) -> bool {
@ -119,11 +120,12 @@ pub fn all_inactive(&self) -> bool {
shards.iter().all(|shard| shard.active.is_empty()) shards.iter().all(|shard| shard.active.is_empty())
} }
pub fn try_collect_active_jobs( pub fn try_collect_active_jobs<CTX: Copy>(
&self, &self,
tcx: CTX,
kind: D, kind: D,
make_query: fn(K) -> Q, make_query: fn(CTX, K) -> QueryStackFrame,
jobs: &mut QueryMap<D, Q>, jobs: &mut QueryMap<D>,
) -> Option<()> { ) -> Option<()> {
// We use try_lock_shards here since we are called from the // We use try_lock_shards here since we are called from the
// deadlock handler, and this shouldn't be locked. // deadlock handler, and this shouldn't be locked.
@ -133,7 +135,7 @@ pub fn try_collect_active_jobs(
shard.active.iter().filter_map(move |(k, v)| { shard.active.iter().filter_map(move |(k, v)| {
if let QueryResult::Started(ref job) = *v { if let QueryResult::Started(ref job) = *v {
let id = QueryJobId::new(job.id, shard_id, kind); let id = QueryJobId::new(job.id, shard_id, kind);
let info = QueryInfo { span: job.span, query: make_query(k.clone()) }; let info = QueryInfo { span: job.span, query: make_query(tcx, k.clone()) };
Some((id, QueryJobInfo { info, job: job.clone() })) Some((id, QueryJobInfo { info, job: job.clone() }))
} else { } else {
None None
@ -145,30 +147,28 @@ pub fn try_collect_active_jobs(
} }
} }
impl<D, Q, K> Default for QueryState<D, Q, K> { impl<D, K> Default for QueryState<D, K> {
fn default() -> QueryState<D, Q, K> { fn default() -> QueryState<D, K> {
QueryState { shards: Default::default() } QueryState { shards: 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, D, Q, C> struct JobOwner<'tcx, D, C>
where where
D: Copy + Clone + Eq + Hash, D: Copy + Clone + Eq + Hash,
Q: Clone,
C: QueryCache, C: QueryCache,
{ {
state: &'tcx QueryState<D, Q, C::Key>, state: &'tcx QueryState<D, C::Key>,
cache: &'tcx QueryCacheStore<C>, cache: &'tcx QueryCacheStore<C>,
key: C::Key, key: C::Key,
id: QueryJobId<D>, id: QueryJobId<D>,
} }
impl<'tcx, D, Q, C> JobOwner<'tcx, D, Q, C> impl<'tcx, D, C> JobOwner<'tcx, D, C>
where where
D: Copy + Clone + Eq + Hash, D: Copy + Clone + Eq + Hash,
Q: Clone,
C: QueryCache, C: QueryCache,
{ {
/// Either gets a `JobOwner` corresponding the query, allowing us to /// Either gets a `JobOwner` corresponding the query, allowing us to
@ -182,13 +182,13 @@ impl<'tcx, D, Q, C> JobOwner<'tcx, D, Q, C>
#[inline(always)] #[inline(always)]
fn try_start<'b, CTX>( fn try_start<'b, CTX>(
tcx: CTX, tcx: CTX,
state: &'b QueryState<CTX::DepKind, CTX::Query, C::Key>, state: &'b QueryState<CTX::DepKind, C::Key>,
cache: &'b QueryCacheStore<C>, cache: &'b QueryCacheStore<C>,
span: Span, span: Span,
key: &C::Key, key: &C::Key,
lookup: QueryLookup, lookup: QueryLookup,
query: &QueryVtable<CTX, C::Key, C::Value>, query: &QueryVtable<CTX, C::Key, C::Value>,
) -> TryGetJob<'b, CTX::DepKind, CTX::Query, C> ) -> TryGetJob<'b, CTX::DepKind, C>
where where
CTX: QueryContext, CTX: QueryContext,
{ {
@ -242,11 +242,12 @@ fn try_start<'b, CTX>(
// so we just return the error. // so we just return the error.
#[cfg(not(parallel_compiler))] #[cfg(not(parallel_compiler))]
return TryGetJob::Cycle(cold_path(|| { return TryGetJob::Cycle(cold_path(|| {
let error: CycleError<CTX::Query> = latch.find_cycle_in_stack( let error: CycleError = latch.find_cycle_in_stack(
tcx.try_collect_active_jobs().unwrap(), tcx.try_collect_active_jobs().unwrap(),
&tcx.current_query_job(), &tcx.current_query_job(),
span, span,
); );
let error = report_cycle(tcx.dep_context().sess(), error);
let value = query.handle_cycle_error(tcx, error); let value = query.handle_cycle_error(tcx, error);
cache.cache.store_nocache(value) cache.cache.store_nocache(value)
})); }));
@ -258,6 +259,7 @@ fn try_start<'b, CTX>(
let result = latch.wait_on(tcx.current_query_job(), span); let result = latch.wait_on(tcx.current_query_job(), span);
if let Err(cycle) = result { if let Err(cycle) = result {
let cycle = report_cycle(tcx.dep_context().sess(), cycle);
let value = query.handle_cycle_error(tcx, cycle); let value = query.handle_cycle_error(tcx, cycle);
let value = cache.cache.store_nocache(value); let value = cache.cache.store_nocache(value);
return TryGetJob::Cycle(value); return TryGetJob::Cycle(value);
@ -327,10 +329,9 @@ fn with_diagnostics<F, R>(f: F) -> (R, ThinVec<Diagnostic>)
(result, diagnostics.into_inner()) (result, diagnostics.into_inner())
} }
impl<'tcx, D, Q, C> Drop for JobOwner<'tcx, D, Q, C> impl<'tcx, D, C> Drop for JobOwner<'tcx, D, C>
where where
D: Copy + Clone + Eq + Hash, D: Copy + Clone + Eq + Hash,
Q: Clone,
C: QueryCache, C: QueryCache,
{ {
#[inline(never)] #[inline(never)]
@ -355,21 +356,20 @@ fn drop(&mut self) {
} }
#[derive(Clone)] #[derive(Clone)]
pub struct CycleError<Q> { pub(crate) struct CycleError {
/// The query and related span that uses the cycle. /// The query and related span that uses the cycle.
pub usage: Option<(Span, Q)>, pub usage: Option<(Span, QueryStackFrame)>,
pub cycle: Vec<QueryInfo<Q>>, pub cycle: Vec<QueryInfo>,
} }
/// The result of `try_start`. /// The result of `try_start`.
enum TryGetJob<'tcx, D, Q, C> enum TryGetJob<'tcx, D, C>
where where
D: Copy + Clone + Eq + Hash, D: Copy + Clone + Eq + Hash,
Q: Clone,
C: QueryCache, C: QueryCache,
{ {
/// 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, D, Q, C>), NotYetStarted(JobOwner<'tcx, D, C>),
/// 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
@ -413,7 +413,7 @@ pub fn try_get_cached<'a, CTX, C, R, OnHit>(
fn try_execute_query<CTX, C>( fn try_execute_query<CTX, C>(
tcx: CTX, tcx: CTX,
state: &QueryState<CTX::DepKind, CTX::Query, C::Key>, state: &QueryState<CTX::DepKind, C::Key>,
cache: &QueryCacheStore<C>, cache: &QueryCacheStore<C>,
span: Span, span: Span,
key: C::Key, key: C::Key,
@ -425,7 +425,7 @@ fn try_execute_query<CTX, C>(
C::Key: crate::dep_graph::DepNodeParams<CTX::DepContext>, C::Key: crate::dep_graph::DepNodeParams<CTX::DepContext>,
CTX: QueryContext, CTX: QueryContext,
{ {
let job = match JobOwner::<'_, CTX::DepKind, CTX::Query, C>::try_start( let job = match JobOwner::<'_, CTX::DepKind, C>::try_start(
tcx, state, cache, span, &key, lookup, query, tcx, state, cache, span, &key, lookup, query,
) { ) {
TryGetJob::NotYetStarted(job) => job, TryGetJob::NotYetStarted(job) => job,
@ -550,7 +550,7 @@ fn load_from_disk_and_cache_in_memory<CTX, K, V: Debug>(
// If `-Zincremental-verify-ich` is specified, re-hash results from // If `-Zincremental-verify-ich` is specified, re-hash results from
// the cache and make sure that they have the expected fingerprint. // the cache and make sure that they have the expected fingerprint.
if unlikely!(tcx.incremental_verify_ich()) { if unlikely!(tcx.dep_context().sess().opts.debugging_opts.incremental_verify_ich) {
incremental_verify_ich(*tcx.dep_context(), &result, dep_node, dep_node_index, query); incremental_verify_ich(*tcx.dep_context(), &result, dep_node, dep_node_index, query);
} }
@ -589,7 +589,7 @@ fn incremental_verify_ich<CTX, K, V: Debug>(
fn force_query_with_job<C, CTX>( fn force_query_with_job<C, CTX>(
tcx: CTX, tcx: CTX,
key: C::Key, key: C::Key,
job: JobOwner<'_, CTX::DepKind, CTX::Query, C>, job: JobOwner<'_, CTX::DepKind, C>,
dep_node: DepNode<CTX::DepKind>, dep_node: DepNode<CTX::DepKind>,
query: &QueryVtable<CTX, C::Key, C::Value>, query: &QueryVtable<CTX, C::Key, C::Value>,
) -> (C::Stored, DepNodeIndex) ) -> (C::Stored, DepNodeIndex)
@ -649,7 +649,7 @@ fn force_query_with_job<C, CTX>(
#[inline(never)] #[inline(never)]
fn get_query_impl<CTX, C>( fn get_query_impl<CTX, C>(
tcx: CTX, tcx: CTX,
state: &QueryState<CTX::DepKind, CTX::Query, C::Key>, state: &QueryState<CTX::DepKind, C::Key>,
cache: &QueryCacheStore<C>, cache: &QueryCacheStore<C>,
span: Span, span: Span,
key: C::Key, key: C::Key,
@ -707,7 +707,7 @@ fn ensure_must_run<CTX, K, V>(tcx: CTX, key: &K, query: &QueryVtable<CTX, K, V>)
#[inline(never)] #[inline(never)]
fn force_query_impl<CTX, C>( fn force_query_impl<CTX, C>(
tcx: CTX, tcx: CTX,
state: &QueryState<CTX::DepKind, CTX::Query, C::Key>, state: &QueryState<CTX::DepKind, C::Key>,
cache: &QueryCacheStore<C>, cache: &QueryCacheStore<C>,
key: C::Key, key: C::Key,
span: Span, span: Span,
@ -735,7 +735,7 @@ fn force_query_impl<CTX, C>(
Err(lookup) => lookup, Err(lookup) => lookup,
}; };
let job = match JobOwner::<'_, CTX::DepKind, CTX::Query, C>::try_start( let job = match JobOwner::<'_, CTX::DepKind, C>::try_start(
tcx, state, cache, span, &key, lookup, query, tcx, state, cache, span, &key, lookup, query,
) { ) {
TryGetJob::NotYetStarted(job) => job, TryGetJob::NotYetStarted(job) => job,