552 lines
19 KiB
Rust
Raw Normal View History

2020-03-19 14:24:21 +01:00
//! The implementation of the query system itself. This defines the macros that
//! generate the actual methods on tcx which find and execute the provider,
//! manage the caches, and so forth.
2021-10-17 17:37:20 +02:00
use crate::{on_disk_cache, Queries};
use rustc_middle::dep_graph::{DepNodeIndex, SerializedDepNodeIndex};
use rustc_middle::ty::tls::{self, ImplicitCtxt};
2021-10-17 17:37:20 +02:00
use rustc_middle::ty::TyCtxt;
use rustc_query_system::dep_graph::HasDepContext;
2021-10-17 17:37:20 +02:00
use rustc_query_system::query::{QueryContext, QueryJobId, QueryMap, QuerySideEffects};
2020-03-19 14:24:21 +01:00
use rustc_data_structures::sync::Lock;
use rustc_data_structures::thin_vec::ThinVec;
2021-06-28 21:12:01 +02:00
use rustc_errors::{Diagnostic, Handler};
2020-10-11 10:34:13 +02:00
use rustc_serialize::opaque;
2020-03-19 14:24:21 +01:00
2021-06-28 21:12:01 +02:00
use std::any::Any;
use std::num::NonZeroU64;
2021-06-28 21:12:01 +02:00
#[derive(Copy, Clone)]
2020-10-11 10:34:13 +02:00
pub struct QueryCtxt<'tcx> {
pub tcx: TyCtxt<'tcx>,
2021-06-28 21:12:01 +02:00
pub queries: &'tcx Queries<'tcx>,
2020-10-11 10:34:13 +02:00
}
impl<'tcx> std::ops::Deref for QueryCtxt<'tcx> {
type Target = TyCtxt<'tcx>;
2021-05-12 13:19:57 +02:00
#[inline]
fn deref(&self) -> &Self::Target {
2020-10-11 10:34:13 +02:00
&self.tcx
}
}
impl<'tcx> HasDepContext for QueryCtxt<'tcx> {
type DepKind = rustc_middle::dep_graph::DepKind;
type DepContext = TyCtxt<'tcx>;
#[inline]
fn dep_context(&self) -> &Self::DepContext {
2020-10-11 10:34:13 +02:00
&self.tcx
}
}
impl QueryContext for QueryCtxt<'_> {
fn next_job_id(&self) -> QueryJobId {
QueryJobId(
NonZeroU64::new(
self.queries.jobs.fetch_add(1, rustc_data_structures::sync::Ordering::Relaxed),
)
.unwrap(),
)
}
fn current_query_job(&self) -> Option<QueryJobId> {
tls::with_related_context(**self, |icx| icx.query)
2020-03-19 14:24:21 +01:00
}
fn try_collect_active_jobs(&self) -> Option<QueryMap> {
2020-12-26 16:36:55 +01:00
self.queries.try_collect_active_jobs(**self)
2020-03-19 14:24:21 +01:00
}
2021-01-18 23:53:42 +01:00
// Interactions with on_disk_cache
fn load_side_effects(&self, prev_dep_node_index: SerializedDepNodeIndex) -> QuerySideEffects {
2021-06-28 21:12:01 +02:00
self.queries
.on_disk_cache
2021-01-18 23:53:42 +01:00
.as_ref()
.map(|c| c.load_side_effects(**self, prev_dep_node_index))
2021-01-18 23:53:42 +01:00
.unwrap_or_default()
}
fn store_side_effects(&self, dep_node_index: DepNodeIndex, side_effects: QuerySideEffects) {
2021-06-28 21:12:01 +02:00
if let Some(c) = self.queries.on_disk_cache.as_ref() {
c.store_side_effects(dep_node_index, side_effects)
2021-01-18 23:53:42 +01:00
}
}
fn store_side_effects_for_anon_node(
2021-01-18 23:53:42 +01:00
&self,
dep_node_index: DepNodeIndex,
side_effects: QuerySideEffects,
2021-01-18 23:53:42 +01:00
) {
2021-06-28 21:12:01 +02:00
if let Some(c) = self.queries.on_disk_cache.as_ref() {
c.store_side_effects_for_anon_node(dep_node_index, side_effects)
2021-01-18 23:53:42 +01:00
}
}
2020-03-19 14:24:21 +01:00
/// Executes a job by changing the `ImplicitCtxt` to point to the
/// new query job while it executes. It returns the diagnostics
/// captured during execution and the actual result.
#[inline(always)]
fn start_query<R>(
&self,
token: QueryJobId,
2020-03-19 14:24:21 +01:00
diagnostics: Option<&Lock<ThinVec<Diagnostic>>>,
2020-10-18 21:01:36 +02:00
compute: impl FnOnce() -> R,
2020-03-19 14:24:21 +01:00
) -> R {
// The `TyCtxt` stored in TLS has the same global interner lifetime
// as `self`, so we use `with_related_context` to relate the 'tcx lifetimes
// when accessing the `ImplicitCtxt`.
tls::with_related_context(**self, move |current_icx| {
2020-03-19 14:24:21 +01:00
// Update the `ImplicitCtxt` to point to our new query job.
let new_icx = ImplicitCtxt {
tcx: **self,
2020-03-19 14:24:21 +01:00
query: Some(token),
diagnostics,
layout_depth: current_icx.layout_depth,
task_deps: current_icx.task_deps,
};
// Use the `ImplicitCtxt` while we execute the query.
tls::enter_context(&new_icx, |_| {
2020-10-18 21:01:36 +02:00
rustc_data_structures::stack::ensure_sufficient_stack(compute)
})
2020-03-19 14:24:21 +01:00
})
}
}
impl<'tcx> QueryCtxt<'tcx> {
2021-06-28 21:12:01 +02:00
#[inline]
pub fn from_tcx(tcx: TyCtxt<'tcx>) -> Self {
let queries = tcx.queries.as_any();
let queries = unsafe {
let queries = std::mem::transmute::<&dyn Any, &dyn Any>(queries);
let queries = queries.downcast_ref().unwrap();
let queries = std::mem::transmute::<&Queries<'_>, &Queries<'_>>(queries);
queries
};
QueryCtxt { tcx, queries }
}
crate fn on_disk_cache(self) -> Option<&'tcx on_disk_cache::OnDiskCache<'tcx>> {
self.queries.on_disk_cache.as_ref()
}
2021-06-28 21:33:47 +02:00
#[cfg(parallel_compiler)]
pub unsafe fn deadlock(self, registry: &rustc_rayon_core::Registry) {
rustc_query_system::query::deadlock(self, registry)
}
2020-10-11 10:34:13 +02:00
pub(super) fn encode_query_results(
self,
encoder: &mut on_disk_cache::CacheEncoder<'_, 'tcx, opaque::FileEncoder>,
query_result_index: &mut on_disk_cache::EncodedDepNodeIndex,
2020-10-11 10:34:13 +02:00
) -> opaque::FileEncodeResult {
macro_rules! encode_queries {
($($query:ident,)*) => {
$(
on_disk_cache::encode_query_results::<_, super::queries::$query<'_>>(
2020-10-11 10:34:13 +02:00
self,
encoder,
query_result_index
)?;
)*
}
}
rustc_cached_queries!(encode_queries!);
Ok(())
}
2021-06-28 21:12:01 +02:00
pub fn try_print_query_stack(
self,
query: Option<QueryJobId>,
2021-06-28 21:12:01 +02:00
handler: &Handler,
num_frames: Option<usize>,
) -> usize {
rustc_query_system::query::print_query_stack(self, query, handler, num_frames)
}
}
2020-03-19 14:24:21 +01:00
macro_rules! handle_cycle_error {
([][$tcx: expr, $error:expr]) => {{
$error.emit();
2020-03-19 14:24:21 +01:00
Value::from_cycle_error($tcx)
}};
([(fatal_cycle) $($rest:tt)*][$tcx:expr, $error:expr]) => {{
$error.emit();
2020-03-19 14:24:21 +01:00
$tcx.sess.abort_if_errors();
unreachable!()
}};
([(cycle_delay_bug) $($rest:tt)*][$tcx:expr, $error:expr]) => {{
$error.delay_as_bug();
2020-03-19 14:24:21 +01:00
Value::from_cycle_error($tcx)
}};
([$other:tt $($modifiers:tt)*][$($args:tt)*]) => {
handle_cycle_error!([$($modifiers)*][$($args)*])
2020-03-19 14:24:21 +01:00
};
}
macro_rules! is_anon {
([]) => {{
false
}};
([(anon) $($rest:tt)*]) => {{
2020-03-19 14:24:21 +01:00
true
}};
([$other:tt $($modifiers:tt)*]) => {
is_anon!([$($modifiers)*])
2020-03-19 14:24:21 +01:00
};
}
macro_rules! is_eval_always {
([]) => {{
false
}};
([(eval_always) $($rest:tt)*]) => {{
2020-03-19 14:24:21 +01:00
true
}};
([$other:tt $($modifiers:tt)*]) => {
is_eval_always!([$($modifiers)*])
2020-03-19 14:24:21 +01:00
};
}
macro_rules! hash_result {
2021-10-16 22:31:48 +02:00
([]) => {{
Some(dep_graph::hash_result)
2020-03-19 14:24:21 +01:00
}};
2021-10-16 22:31:48 +02:00
([(no_hash) $($rest:tt)*]) => {{
2020-03-19 14:24:21 +01:00
None
}};
2021-10-16 22:31:48 +02:00
([$other:tt $($modifiers:tt)*]) => {
hash_result!([$($modifiers)*])
2020-03-19 14:24:21 +01:00
};
}
macro_rules! get_provider {
([][$tcx:expr, $name:ident, $key:expr]) => {{
$tcx.queries.local_providers.$name
}};
([(separate_provide_extern) $($rest:tt)*][$tcx:expr, $name:ident, $key:expr]) => {{
if $key.query_crate_is_local() {
$tcx.queries.local_providers.$name
} else {
$tcx.queries.extern_providers.$name
}
}};
([$other:tt $($modifiers:tt)*][$($args:tt)*]) => {
get_provider!([$($modifiers)*][$($args)*])
};
}
2021-12-06 19:47:54 +08:00
macro_rules! opt_remap_env_constness {
([][$name:ident]) => {};
([(remap_env_constness) $($rest:tt)*][$name:ident]) => {
let $name = $name.without_const();
};
([$other:tt $($modifiers:tt)*][$name:ident]) => {
opt_remap_env_constness!([$($modifiers)*][$name])
};
}
2020-10-11 20:44:24 +02:00
macro_rules! define_queries {
2020-03-19 14:24:21 +01:00
(<$tcx:tt>
2020-10-11 20:44:24 +02:00
$($(#[$attr:meta])*
2020-10-11 20:46:46 +02:00
[$($modifiers:tt)*] fn $name:ident($($K:tt)*) -> $V:ty,)*) => {
2020-03-19 14:24:21 +01:00
define_queries_struct! {
tcx: $tcx,
input: ($(([$($modifiers)*] [$($attr)*] [$name]))*)
}
mod make_query {
use super::*;
2020-03-19 14:24:21 +01:00
// Create an eponymous constructor for each query.
2020-12-26 16:36:55 +01:00
$(#[allow(nonstandard_style)] $(#[$attr])*
pub fn $name<$tcx>(tcx: QueryCtxt<$tcx>, key: query_keys::$name<$tcx>) -> QueryStackFrame {
2021-12-06 19:47:54 +08:00
opt_remap_env_constness!([$($modifiers)*][key]);
2020-12-26 16:36:55 +01:00
let kind = dep_graph::DepKind::$name;
let name = stringify!($name);
// Disable visible paths printing for performance reasons.
// Showing visible path instead of any path is not that important in production.
let description = ty::print::with_no_visible_paths!(
2020-12-26 16:36:55 +01:00
// Force filename-line mode to avoid invoking `type_of` query.
ty::print::with_forced_impl_filename_line!(
queries::$name::describe(tcx, key)
)
);
2020-12-26 16:36:55 +01:00
let description = if tcx.sess.verbose() {
format!("{} [{}]", description, name)
} else {
description
2020-03-19 14:24:21 +01:00
};
2020-12-26 16:36:55 +01:00
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
2020-03-19 14:24:21 +01:00
} else {
2020-12-26 16:36:55 +01:00
Some(key.default_span(*tcx))
};
let def_id = key.key_as_def_id();
let def_kind = def_id
.and_then(|def_id| def_id.as_local())
// Use `tcx.hir().opt_def_kind()` to reduce the chance of
// accidentally triggering an infinite query loop.
.and_then(|def_id| tcx.hir().opt_def_kind(def_id))
.map(|def_kind| $crate::util::def_kind_to_simple_def_kind(def_kind));
2021-02-09 18:53:38 +01:00
let hash = || {
2020-12-26 16:36:55 +01:00
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);
2021-02-09 18:53:38 +01:00
hasher.finish::<u64>()
2020-12-26 16:36:55 +01:00
};
QueryStackFrame::new(name, description, span, def_kind, hash)
2020-12-26 16:36:55 +01:00
})*
2020-03-19 14:24:21 +01:00
}
#[allow(nonstandard_style)]
2022-02-08 00:13:31 +03:00
mod queries {
2020-03-19 14:24:21 +01:00
use std::marker::PhantomData;
$(pub struct $name<$tcx> {
2020-03-19 14:24:21 +01:00
data: PhantomData<&$tcx ()>
})*
}
$(impl<$tcx> QueryConfig for queries::$name<$tcx> {
type Key = query_keys::$name<$tcx>;
type Value = query_values::$name<$tcx>;
2021-01-17 14:57:07 +01:00
type Stored = query_stored::$name<$tcx>;
2020-03-19 14:24:21 +01:00
const NAME: &'static str = stringify!($name);
}
2021-10-17 17:37:20 +02:00
impl<$tcx> QueryDescription<QueryCtxt<$tcx>> for queries::$name<$tcx> {
rustc_query_description! { $name<$tcx> }
2020-03-19 14:24:21 +01:00
2021-01-19 20:02:05 +01:00
type Cache = query_storage::$name<$tcx>;
2020-03-19 14:24:21 +01:00
#[inline(always)]
fn query_state<'a>(tcx: QueryCtxt<$tcx>) -> &'a QueryState<Self::Key>
2020-12-26 16:36:55 +01:00
where QueryCtxt<$tcx>: 'a
{
2020-03-19 14:24:21 +01:00
&tcx.queries.$name
}
#[inline(always)]
fn query_cache<'a>(tcx: QueryCtxt<$tcx>) -> &'a QueryCacheStore<Self::Cache>
where 'tcx:'a
{
&tcx.query_caches.$name
}
2020-03-19 14:24:21 +01:00
#[inline]
2021-10-17 17:37:20 +02:00
fn make_vtable(tcx: QueryCtxt<'tcx>, key: &Self::Key) ->
QueryVtable<QueryCtxt<$tcx>, Self::Key, Self::Value>
2021-05-10 19:09:30 +02:00
{
let compute = get_provider!([$($modifiers)*][tcx, $name, key]);
let cache_on_disk = Self::cache_on_disk(tcx.tcx, key);
2021-10-17 17:37:20 +02:00
QueryVtable {
anon: is_anon!([$($modifiers)*]),
eval_always: is_eval_always!([$($modifiers)*]),
dep_kind: dep_graph::DepKind::$name,
hash_result: hash_result!([$($modifiers)*]),
handle_cycle_error: |tcx, mut error| handle_cycle_error!([$($modifiers)*][tcx, error]),
compute,
cache_on_disk,
try_load_from_disk: Self::TRY_LOAD_FROM_DISK,
2021-05-10 19:09:30 +02:00
}
2020-03-19 14:24:21 +01:00
}
})*
2021-10-16 21:12:34 +02:00
#[allow(nonstandard_style)]
2022-02-08 00:13:31 +03:00
mod query_callbacks {
2021-01-19 19:07:06 +01:00
use super::*;
use rustc_middle::dep_graph::DepNode;
use rustc_middle::ty::query::query_keys;
2021-01-19 19:07:06 +01:00
use rustc_query_system::dep_graph::DepNodeParams;
use rustc_query_system::query::{force_query, QueryDescription};
use rustc_query_system::dep_graph::FingerprintStyle;
2021-01-19 19:07:06 +01:00
// We use this for most things when incr. comp. is turned off.
2021-10-16 21:12:34 +02:00
pub fn Null() -> DepKindStruct {
DepKindStruct {
is_anon: false,
is_eval_always: false,
fingerprint_style: FingerprintStyle::Unit,
force_from_dep_node: Some(|_, dep_node| bug!("force_from_dep_node: encountered {:?}", dep_node)),
try_load_from_on_disk_cache: None,
}
}
2021-10-16 20:10:23 +02:00
2021-10-16 21:12:34 +02:00
pub fn TraitSelect() -> DepKindStruct {
DepKindStruct {
is_anon: true,
is_eval_always: false,
fingerprint_style: FingerprintStyle::Unit,
force_from_dep_node: None,
try_load_from_on_disk_cache: None,
}
}
2021-01-19 19:07:06 +01:00
2021-10-16 21:12:34 +02:00
pub fn CompileCodegenUnit() -> DepKindStruct {
DepKindStruct {
is_anon: false,
is_eval_always: false,
fingerprint_style: FingerprintStyle::Opaque,
force_from_dep_node: None,
try_load_from_on_disk_cache: None,
}
}
2021-10-16 20:10:23 +02:00
2021-10-16 21:12:34 +02:00
pub fn CompileMonoItem() -> DepKindStruct {
DepKindStruct {
is_anon: false,
is_eval_always: false,
fingerprint_style: FingerprintStyle::Opaque,
force_from_dep_node: None,
try_load_from_on_disk_cache: None,
}
}
2021-01-19 19:07:06 +01:00
2022-02-07 22:45:29 +03:00
$(pub(crate) fn $name()-> DepKindStruct {
2021-10-16 21:12:34 +02:00
let is_anon = is_anon!([$($modifiers)*]);
let is_eval_always = is_eval_always!([$($modifiers)*]);
2021-10-16 20:10:23 +02:00
2021-10-16 21:12:34 +02:00
let fingerprint_style =
<query_keys::$name<'_> as DepNodeParams<TyCtxt<'_>>>::fingerprint_style();
2021-04-12 13:58:12 +02:00
2021-10-16 21:12:34 +02:00
if is_anon || !fingerprint_style.reconstructible() {
return DepKindStruct {
is_anon,
is_eval_always,
fingerprint_style,
force_from_dep_node: None,
try_load_from_on_disk_cache: None,
}
}
2021-01-19 19:07:06 +01:00
#[inline(always)]
2021-10-16 21:12:34 +02:00
fn recover<'tcx>(tcx: TyCtxt<'tcx>, dep_node: DepNode) -> Option<query_keys::$name<'tcx>> {
<query_keys::$name<'_> as DepNodeParams<TyCtxt<'_>>>::recover(tcx, &dep_node)
2021-01-19 19:07:06 +01:00
}
2021-10-16 21:12:34 +02:00
fn force_from_dep_node(tcx: TyCtxt<'_>, dep_node: DepNode) -> bool {
if let Some(key) = recover(tcx, dep_node) {
let tcx = QueryCtxt::from_tcx(tcx);
force_query::<queries::$name<'_>, _>(tcx, key, dep_node);
true
} else {
false
}
2021-01-19 19:07:06 +01:00
}
2021-10-16 21:12:34 +02:00
fn try_load_from_on_disk_cache(tcx: TyCtxt<'_>, dep_node: DepNode) {
debug_assert!(tcx.dep_graph.is_green(&dep_node));
2021-01-19 19:07:06 +01:00
2021-10-16 21:12:34 +02:00
let key = recover(tcx, dep_node).unwrap_or_else(|| panic!("Failed to recover key for {:?} with hash {}", dep_node, dep_node.hash));
if queries::$name::cache_on_disk(tcx, &key) {
2021-01-19 19:07:06 +01:00
let _ = tcx.$name(key);
}
}
2021-10-16 20:10:23 +02:00
DepKindStruct {
is_anon,
is_eval_always,
fingerprint_style,
2021-10-16 21:12:34 +02:00
force_from_dep_node: Some(force_from_dep_node),
try_load_from_on_disk_cache: Some(try_load_from_on_disk_cache),
2021-01-19 19:07:06 +01:00
}
2021-10-16 21:12:34 +02:00
})*
2021-01-19 19:07:06 +01:00
}
2021-10-16 21:12:34 +02:00
pub fn query_callbacks<'tcx>(arena: &'tcx Arena<'tcx>) -> &'tcx [DepKindStruct] {
arena.alloc_from_iter(make_dep_kind_array!(query_callbacks))
}
2020-03-19 14:24:21 +01:00
}
}
// FIXME(eddyb) this macro (and others?) use `$tcx` and `'tcx` interchangeably.
// We should either not take `$tcx` at all and use `'tcx` everywhere, or use
// `$tcx` everywhere (even if that isn't necessary due to lack of hygiene).
2020-03-19 14:24:21 +01:00
macro_rules! define_queries_struct {
(tcx: $tcx:tt,
input: ($(([$($modifiers:tt)*] [$($attr:tt)*] [$name:ident]))*)) => {
pub struct Queries<$tcx> {
local_providers: Box<Providers>,
extern_providers: Box<ExternProviders>,
2020-03-19 14:24:21 +01:00
2021-06-28 21:12:01 +02:00
pub on_disk_cache: Option<OnDiskCache<$tcx>>,
jobs: AtomicU64,
$($(#[$attr])* $name: QueryState<query_keys::$name<$tcx>>,)*
2020-03-19 14:24:21 +01:00
}
impl<$tcx> Queries<$tcx> {
2020-10-11 10:34:13 +02:00
pub fn new(
local_providers: Providers,
extern_providers: ExternProviders,
2021-06-28 21:12:01 +02:00
on_disk_cache: Option<OnDiskCache<$tcx>>,
2020-03-19 14:24:21 +01:00
) -> Self {
Queries {
local_providers: Box::new(local_providers),
extern_providers: Box::new(extern_providers),
2021-06-28 21:12:01 +02:00
on_disk_cache,
jobs: AtomicU64::new(1),
2020-03-19 14:24:21 +01:00
$($name: Default::default()),*
}
}
pub(crate) fn try_collect_active_jobs(
2020-12-26 16:36:55 +01:00
&$tcx self,
tcx: TyCtxt<$tcx>,
) -> Option<QueryMap> {
2020-12-26 16:36:55 +01:00
let tcx = QueryCtxt { tcx, queries: self };
let mut jobs = QueryMap::default();
2020-03-19 14:24:21 +01:00
$(
self.$name.try_collect_active_jobs(
2020-12-26 16:36:55 +01:00
tcx,
make_query::$name,
2020-03-19 14:24:21 +01:00
&mut jobs,
)?;
)*
Some(jobs)
}
2021-01-18 22:32:20 +01:00
}
2021-01-17 14:58:34 +01:00
impl<'tcx> QueryEngine<'tcx> for Queries<'tcx> {
2021-06-28 21:12:01 +02:00
fn as_any(&'tcx self) -> &'tcx dyn std::any::Any {
let this = unsafe { std::mem::transmute::<&Queries<'_>, &Queries<'_>>(self) };
this as _
}
2021-01-18 22:32:20 +01:00
fn try_mark_green(&'tcx self, tcx: TyCtxt<'tcx>, dep_node: &dep_graph::DepNode) -> bool {
let qcx = QueryCtxt { tcx, queries: self };
tcx.dep_graph.try_mark_green(qcx, dep_node).is_some()
}
2021-01-17 14:58:34 +01:00
$($(#[$attr])*
#[inline(always)]
fn $name(
2020-10-11 10:34:13 +02:00
&'tcx self,
2021-01-17 14:58:34 +01:00
tcx: TyCtxt<$tcx>,
span: Span,
key: query_keys::$name<$tcx>,
lookup: QueryLookup,
mode: QueryMode,
) -> Option<query_stored::$name<$tcx>> {
2021-12-06 19:47:54 +08:00
opt_remap_env_constness!([$($modifiers)*][key]);
2020-10-11 10:34:13 +02:00
let qcx = QueryCtxt { tcx, queries: self };
2021-01-17 14:58:34 +01:00
get_query::<queries::$name<$tcx>, _>(qcx, span, key, lookup, mode)
})*
2020-03-19 14:24:21 +01:00
}
};
}