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.
|
|
|
|
|
2022-09-13 18:32:22 -05:00
|
|
|
use crate::on_disk_cache::{CacheDecoder, CacheEncoder, EncodedDepNodeIndex};
|
|
|
|
use crate::profiling_support::QueryKeyStringCache;
|
2021-10-17 17:37:20 +02:00
|
|
|
use crate::{on_disk_cache, Queries};
|
2022-08-17 14:22:30 +10:00
|
|
|
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
|
2022-08-29 19:30:25 -05:00
|
|
|
use rustc_data_structures::sync::{AtomicU64, Lock};
|
2022-08-17 14:22:30 +10:00
|
|
|
use rustc_errors::{Diagnostic, Handler};
|
2022-08-23 23:31:12 -05:00
|
|
|
use rustc_middle::dep_graph::{
|
|
|
|
self, DepKind, DepKindStruct, DepNode, DepNodeIndex, SerializedDepNodeIndex,
|
|
|
|
};
|
2022-10-31 22:54:09 +00:00
|
|
|
use rustc_middle::query::Key;
|
2021-01-19 20:40:16 +01:00
|
|
|
use rustc_middle::ty::tls::{self, ImplicitCtxt};
|
2022-08-11 20:36:13 -05:00
|
|
|
use rustc_middle::ty::{self, TyCtxt};
|
2022-08-23 22:24:39 -05:00
|
|
|
use rustc_query_system::dep_graph::{DepNodeParams, HasDepContext};
|
2022-08-11 20:36:13 -05:00
|
|
|
use rustc_query_system::ich::StableHashingContext;
|
|
|
|
use rustc_query_system::query::{
|
2022-11-05 16:04:43 +01:00
|
|
|
force_query, QueryConfig, QueryContext, QueryJobId, QueryMap, QuerySideEffects, QueryStackFrame,
|
2022-08-11 20:36:13 -05:00
|
|
|
};
|
2023-02-07 08:32:30 +01:00
|
|
|
use rustc_query_system::{LayoutOfDepth, QueryOverflow};
|
2022-09-01 22:20:17 -05:00
|
|
|
use rustc_serialize::Decodable;
|
2022-09-15 15:45:17 +08:00
|
|
|
use rustc_session::Limit;
|
|
|
|
use rustc_span::def_id::LOCAL_CRATE;
|
2021-06-28 21:12:01 +02:00
|
|
|
use std::any::Any;
|
2022-02-07 11:03:51 -05:00
|
|
|
use std::num::NonZeroU64;
|
2022-08-17 14:22:30 +10:00
|
|
|
use thin_vec::ThinVec;
|
2021-06-28 21:12:01 +02:00
|
|
|
|
2020-04-08 17:03:34 +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
|
|
|
}
|
2020-04-08 17:03:34 +02:00
|
|
|
|
|
|
|
impl<'tcx> std::ops::Deref for QueryCtxt<'tcx> {
|
|
|
|
type Target = TyCtxt<'tcx>;
|
|
|
|
|
2021-05-12 13:19:57 +02:00
|
|
|
#[inline]
|
2020-04-08 17:03:34 +02:00
|
|
|
fn deref(&self) -> &Self::Target {
|
2020-10-11 10:34:13 +02:00
|
|
|
&self.tcx
|
2020-04-08 17:03:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-14 12:13:07 -05:00
|
|
|
impl<'tcx> HasDepContext for QueryCtxt<'tcx> {
|
2021-01-19 20:40:16 +01:00
|
|
|
type DepKind = rustc_middle::dep_graph::DepKind;
|
2020-04-08 17:03:34 +02:00
|
|
|
type DepContext = TyCtxt<'tcx>;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn dep_context(&self) -> &Self::DepContext {
|
2020-10-11 10:34:13 +02:00
|
|
|
&self.tcx
|
2020-04-08 17:03:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-14 12:13:07 -05:00
|
|
|
impl QueryContext for QueryCtxt<'_> {
|
2023-02-14 00:18:46 +01:00
|
|
|
fn next_job_id(self) -> QueryJobId {
|
2022-02-07 11:03:51 -05:00
|
|
|
QueryJobId(
|
|
|
|
NonZeroU64::new(
|
|
|
|
self.queries.jobs.fetch_add(1, rustc_data_structures::sync::Ordering::Relaxed),
|
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-02-14 00:18:46 +01:00
|
|
|
fn current_query_job(self) -> Option<QueryJobId> {
|
|
|
|
tls::with_related_context(*self, |icx| icx.query)
|
2020-03-19 14:24:21 +01:00
|
|
|
}
|
|
|
|
|
2023-02-14 00:18:46 +01:00
|
|
|
fn try_collect_active_jobs(self) -> Option<QueryMap<DepKind>> {
|
|
|
|
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
|
2023-02-14 00:18:46 +01:00
|
|
|
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()
|
2023-02-14 00:18:46 +01:00
|
|
|
.map(|c| c.load_side_effects(*self, prev_dep_node_index))
|
2021-01-18 23:53:42 +01:00
|
|
|
.unwrap_or_default()
|
|
|
|
}
|
|
|
|
|
2023-02-14 00:18:46 +01:00
|
|
|
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() {
|
2021-07-23 16:40:26 -05:00
|
|
|
c.store_side_effects(dep_node_index, side_effects)
|
2021-01-18 23:53:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-23 16:40:26 -05:00
|
|
|
fn store_side_effects_for_anon_node(
|
2023-02-14 00:18:46 +01:00
|
|
|
self,
|
2021-01-18 23:53:42 +01:00
|
|
|
dep_node_index: DepNodeIndex,
|
2021-07-23 16:40:26 -05:00
|
|
|
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() {
|
2021-07-23 16:40:26 -05:00
|
|
|
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>(
|
2023-02-14 00:18:46 +01:00
|
|
|
self,
|
2022-02-07 11:03:51 -05:00
|
|
|
token: QueryJobId,
|
2022-08-24 09:42:12 +08:00
|
|
|
depth_limit: bool,
|
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`.
|
2023-02-14 00:18:46 +01:00
|
|
|
tls::with_related_context(*self, move |current_icx| {
|
2022-08-24 09:42:12 +08:00
|
|
|
if depth_limit && !self.recursion_limit().value_within_limit(current_icx.query_depth) {
|
2022-09-14 21:00:00 +08:00
|
|
|
self.depth_limit_error(token);
|
2022-08-24 09:42:12 +08:00
|
|
|
}
|
|
|
|
|
2020-03-19 14:24:21 +01:00
|
|
|
// Update the `ImplicitCtxt` to point to our new query job.
|
|
|
|
let new_icx = ImplicitCtxt {
|
2023-02-14 00:18:46 +01:00
|
|
|
tcx: *self,
|
2020-03-19 14:24:21 +01:00
|
|
|
query: Some(token),
|
|
|
|
diagnostics,
|
2022-08-24 09:42:12 +08:00
|
|
|
query_depth: current_icx.query_depth + depth_limit as usize,
|
2020-03-19 14:24:21 +01:00
|
|
|
task_deps: current_icx.task_deps,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Use the `ImplicitCtxt` while we execute the query.
|
2023-02-16 19:34:31 +01:00
|
|
|
tls::enter_context(&new_icx, compute)
|
2020-03-19 14:24:21 +01:00
|
|
|
})
|
|
|
|
}
|
2022-09-15 15:45:17 +08:00
|
|
|
|
2023-02-14 00:18:46 +01:00
|
|
|
fn depth_limit_error(self, job: QueryJobId) {
|
2022-09-15 15:45:17 +08:00
|
|
|
let mut span = None;
|
|
|
|
let mut layout_of_depth = None;
|
|
|
|
if let Some(map) = self.try_collect_active_jobs() {
|
|
|
|
if let Some((info, depth)) = job.try_find_layout_root(map) {
|
|
|
|
span = Some(info.job.span);
|
|
|
|
layout_of_depth = Some(LayoutOfDepth { desc: info.query.description, depth });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let suggested_limit = match self.recursion_limit() {
|
|
|
|
Limit(0) => Limit(2),
|
|
|
|
limit => limit * 2,
|
|
|
|
};
|
|
|
|
|
|
|
|
self.sess.emit_fatal(QueryOverflow {
|
|
|
|
span,
|
|
|
|
layout_of_depth,
|
|
|
|
suggested_limit,
|
|
|
|
crate_name: self.crate_name(LOCAL_CRATE),
|
|
|
|
});
|
|
|
|
}
|
2020-03-19 14:24:21 +01:00
|
|
|
}
|
|
|
|
|
2020-04-08 17:03:34 +02: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 }
|
|
|
|
}
|
|
|
|
|
2022-05-20 19:51:09 -04:00
|
|
|
pub(crate) fn on_disk_cache(self) -> Option<&'tcx on_disk_cache::OnDiskCache<'tcx>> {
|
2021-06-28 21:12:01 +02:00
|
|
|
self.queries.on_disk_cache.as_ref()
|
|
|
|
}
|
|
|
|
|
2020-10-11 10:34:13 +02:00
|
|
|
pub(super) fn encode_query_results(
|
|
|
|
self,
|
2022-09-13 18:32:22 -05:00
|
|
|
encoder: &mut CacheEncoder<'_, 'tcx>,
|
|
|
|
query_result_index: &mut EncodedDepNodeIndex,
|
Use delayed error handling for `Encodable` and `Encoder` infallible.
There are two impls of the `Encoder` trait: `opaque::Encoder` and
`opaque::FileEncoder`. The former encodes into memory and is infallible, the
latter writes to file and is fallible.
Currently, standard `Result`/`?`/`unwrap` error handling is used, but this is a
bit verbose and has non-trivial cost, which is annoying given how rare failures
are (especially in the infallible `opaque::Encoder` case).
This commit changes how `Encoder` fallibility is handled. All the `emit_*`
methods are now infallible. `opaque::Encoder` requires no great changes for
this. `opaque::FileEncoder` now implements a delayed error handling strategy.
If a failure occurs, it records this via the `res` field, and all subsequent
encoding operations are skipped if `res` indicates an error has occurred. Once
encoding is complete, the new `finish` method is called, which returns a
`Result`. In other words, there is now a single `Result`-producing method
instead of many of them.
This has very little effect on how any file errors are reported if
`opaque::FileEncoder` has any failures.
Much of this commit is boring mechanical changes, removing `Result` return
values and `?` or `unwrap` from expressions. The more interesting parts are as
follows.
- serialize.rs: The `Encoder` trait gains an `Ok` associated type. The
`into_inner` method is changed into `finish`, which returns
`Result<Vec<u8>, !>`.
- opaque.rs: The `FileEncoder` adopts the delayed error handling
strategy. Its `Ok` type is a `usize`, returning the number of bytes
written, replacing previous uses of `FileEncoder::position`.
- Various methods that take an encoder now consume it, rather than being
passed a mutable reference, e.g. `serialize_query_result_cache`.
2022-06-07 13:30:45 +10:00
|
|
|
) {
|
2022-09-13 18:32:22 -05:00
|
|
|
for query in &self.queries.query_structs {
|
|
|
|
if let Some(encode) = query.encode_query_results {
|
|
|
|
encode(self, encoder, query_result_index);
|
2020-10-11 10:34:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-06-28 21:12:01 +02:00
|
|
|
|
|
|
|
pub fn try_print_query_stack(
|
|
|
|
self,
|
2022-02-07 11:03:51 -05:00
|
|
|
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-04-08 17:03:34 +02:00
|
|
|
}
|
2020-03-19 14:24:21 +01:00
|
|
|
|
2022-09-13 18:32:22 -05:00
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
pub(crate) struct QueryStruct<'tcx> {
|
2022-12-23 18:39:49 +05:30
|
|
|
pub try_collect_active_jobs: fn(QueryCtxt<'tcx>, &mut QueryMap<DepKind>) -> Option<()>,
|
2022-09-13 18:32:22 -05:00
|
|
|
pub alloc_self_profile_query_strings: fn(TyCtxt<'tcx>, &mut QueryKeyStringCache),
|
|
|
|
pub encode_query_results:
|
|
|
|
Option<fn(QueryCtxt<'tcx>, &mut CacheEncoder<'_, 'tcx>, &mut EncodedDepNodeIndex)>,
|
|
|
|
}
|
|
|
|
|
2020-03-19 14:24:21 +01:00
|
|
|
macro_rules! handle_cycle_error {
|
2022-09-01 20:43:12 -05:00
|
|
|
([]) => {{
|
|
|
|
rustc_query_system::HandleCycleError::Error
|
2020-03-19 14:24:21 +01:00
|
|
|
}};
|
2022-09-01 20:43:12 -05:00
|
|
|
([(fatal_cycle) $($rest:tt)*]) => {{
|
|
|
|
rustc_query_system::HandleCycleError::Fatal
|
2020-03-19 14:24:21 +01:00
|
|
|
}};
|
2022-09-01 20:43:12 -05:00
|
|
|
([(cycle_delay_bug) $($rest:tt)*]) => {{
|
|
|
|
rustc_query_system::HandleCycleError::DelayBug
|
2020-03-19 14:24:21 +01:00
|
|
|
}};
|
2022-09-01 20:43:12 -05:00
|
|
|
([$other:tt $($modifiers:tt)*]) => {
|
|
|
|
handle_cycle_error!([$($modifiers)*])
|
2020-03-19 14:24:21 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! is_anon {
|
|
|
|
([]) => {{
|
|
|
|
false
|
|
|
|
}};
|
2021-05-30 16:45:51 +02:00
|
|
|
([(anon) $($rest:tt)*]) => {{
|
2020-03-19 14:24:21 +01:00
|
|
|
true
|
|
|
|
}};
|
2021-05-30 16:45:51 +02:00
|
|
|
([$other:tt $($modifiers:tt)*]) => {
|
|
|
|
is_anon!([$($modifiers)*])
|
2020-03-19 14:24:21 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! is_eval_always {
|
|
|
|
([]) => {{
|
|
|
|
false
|
|
|
|
}};
|
2021-05-30 16:45:51 +02:00
|
|
|
([(eval_always) $($rest:tt)*]) => {{
|
2020-03-19 14:24:21 +01:00
|
|
|
true
|
|
|
|
}};
|
2021-05-30 16:45:51 +02:00
|
|
|
([$other:tt $($modifiers:tt)*]) => {
|
|
|
|
is_eval_always!([$($modifiers)*])
|
2020-03-19 14:24:21 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-08-24 09:42:12 +08:00
|
|
|
macro_rules! depth_limit {
|
|
|
|
([]) => {{
|
|
|
|
false
|
|
|
|
}};
|
|
|
|
([(depth_limit) $($rest:tt)*]) => {{
|
|
|
|
true
|
|
|
|
}};
|
|
|
|
([$other:tt $($modifiers:tt)*]) => {
|
|
|
|
depth_limit!([$($modifiers)*])
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-10-29 13:04:38 +00:00
|
|
|
macro_rules! feedable {
|
|
|
|
([]) => {{
|
|
|
|
false
|
|
|
|
}};
|
|
|
|
([(feedable) $($rest:tt)*]) => {{
|
|
|
|
true
|
|
|
|
}};
|
|
|
|
([$other:tt $($modifiers:tt)*]) => {
|
|
|
|
feedable!([$($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
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-05-30 17:24:54 +02:00
|
|
|
macro_rules! get_provider {
|
|
|
|
([][$tcx:expr, $name:ident, $key:expr]) => {{
|
2023-02-14 14:17:04 +01:00
|
|
|
$tcx.queries.local_providers.$name
|
2021-05-30 17:24:54 +02:00
|
|
|
}};
|
|
|
|
([(separate_provide_extern) $($rest:tt)*][$tcx:expr, $name:ident, $key:expr]) => {{
|
|
|
|
if $key.query_crate_is_local() {
|
2023-02-14 14:17:04 +01:00
|
|
|
$tcx.queries.local_providers.$name
|
2021-05-30 17:24:54 +02:00
|
|
|
} else {
|
2023-02-14 14:17:04 +01:00
|
|
|
$tcx.queries.extern_providers.$name
|
2021-05-30 17:24:54 +02:00
|
|
|
}
|
|
|
|
}};
|
|
|
|
([$other:tt $($modifiers:tt)*][$($args:tt)*]) => {
|
|
|
|
get_provider!([$($modifiers)*][$($args)*])
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-09-01 22:20:17 -05:00
|
|
|
macro_rules! should_ever_cache_on_disk {
|
2023-02-08 19:53:48 +01:00
|
|
|
([]$yes:tt $no:tt) => {{
|
|
|
|
$no
|
2022-09-01 22:20:17 -05:00
|
|
|
}};
|
2023-02-08 19:53:48 +01:00
|
|
|
([(cache) $($rest:tt)*]$yes:tt $no:tt) => {{
|
|
|
|
$yes
|
2022-09-01 22:20:17 -05:00
|
|
|
}};
|
2023-02-08 19:53:48 +01:00
|
|
|
([$other:tt $($modifiers:tt)*]$yes:tt $no:tt) => {
|
|
|
|
should_ever_cache_on_disk!([$($modifiers)*]$yes $no)
|
2022-09-01 22:20:17 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-08-11 20:36:13 -05:00
|
|
|
pub(crate) fn create_query_frame<
|
|
|
|
'tcx,
|
|
|
|
K: Copy + Key + for<'a> HashStable<StableHashingContext<'a>>,
|
|
|
|
>(
|
|
|
|
tcx: QueryCtxt<'tcx>,
|
2022-10-13 21:18:36 +02:00
|
|
|
do_describe: fn(TyCtxt<'tcx>, K) -> String,
|
2022-08-11 20:36:13 -05:00
|
|
|
key: K,
|
|
|
|
kind: DepKind,
|
|
|
|
name: &'static str,
|
2022-12-23 18:39:49 +05:30
|
|
|
) -> QueryStackFrame<DepKind> {
|
2023-02-02 05:49:07 +00:00
|
|
|
// Avoid calling queries while formatting the description
|
|
|
|
let description = ty::print::with_no_queries!(
|
|
|
|
// Disable visible paths printing for performance reasons.
|
|
|
|
// Showing visible path instead of any path is not that important in production.
|
|
|
|
ty::print::with_no_visible_paths!(
|
|
|
|
// Force filename-line mode to avoid invoking `type_of` query.
|
|
|
|
ty::print::with_forced_impl_filename_line!(do_describe(tcx.tcx, key))
|
|
|
|
)
|
2022-08-11 20:36:13 -05:00
|
|
|
);
|
|
|
|
let description =
|
2022-12-19 10:31:55 +01:00
|
|
|
if tcx.sess.verbose() { format!("{description} [{name:?}]") } else { description };
|
2022-08-11 20:36:13 -05: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
|
|
|
|
} else {
|
|
|
|
Some(key.default_span(*tcx))
|
|
|
|
};
|
2022-08-15 14:11:11 -05:00
|
|
|
let def_id = key.key_as_def_id();
|
2022-08-11 20:36:13 -05:00
|
|
|
let def_kind = if kind == dep_graph::DepKind::opt_def_kind {
|
|
|
|
// Try to avoid infinite recursion.
|
|
|
|
None
|
|
|
|
} else {
|
2022-08-15 14:11:11 -05:00
|
|
|
def_id.and_then(|def_id| def_id.as_local()).and_then(|def_id| tcx.opt_def_kind(def_id))
|
2022-08-11 20:36:13 -05:00
|
|
|
};
|
|
|
|
let hash = || {
|
|
|
|
tcx.with_stable_hashing_context(|mut hcx| {
|
|
|
|
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>()
|
|
|
|
})
|
|
|
|
};
|
2022-08-15 14:11:11 -05:00
|
|
|
let ty_adt_id = key.ty_adt_id();
|
2022-08-11 20:36:13 -05:00
|
|
|
|
2022-12-23 18:39:49 +05:30
|
|
|
QueryStackFrame::new(description, span, def_id, def_kind, kind, ty_adt_id, hash)
|
2022-08-11 20:36:13 -05:00
|
|
|
}
|
|
|
|
|
2023-02-07 08:32:30 +01:00
|
|
|
fn try_load_from_on_disk_cache<'tcx, Q>(query: Q, tcx: TyCtxt<'tcx>, dep_node: DepNode)
|
2022-08-29 10:20:14 -05:00
|
|
|
where
|
2022-11-05 16:04:43 +01:00
|
|
|
Q: QueryConfig<QueryCtxt<'tcx>>,
|
2022-08-23 23:34:23 -05:00
|
|
|
{
|
2022-08-23 22:10:47 -05:00
|
|
|
debug_assert!(tcx.dep_graph.is_green(&dep_node));
|
|
|
|
|
2022-08-23 23:34:23 -05:00
|
|
|
let key = Q::Key::recover(tcx, &dep_node).unwrap_or_else(|| {
|
2022-08-23 22:10:47 -05:00
|
|
|
panic!("Failed to recover key for {:?} with hash {}", dep_node, dep_node.hash)
|
|
|
|
});
|
2023-02-07 08:32:30 +01:00
|
|
|
if query.cache_on_disk(tcx, &key) {
|
|
|
|
let _ = query.execute_query(tcx, key);
|
2022-08-23 22:10:47 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-06 14:57:05 +00:00
|
|
|
pub(crate) fn loadable_from_disk<'tcx>(tcx: QueryCtxt<'tcx>, id: SerializedDepNodeIndex) -> bool {
|
|
|
|
if let Some(cache) = tcx.on_disk_cache().as_ref() {
|
|
|
|
cache.loadable_from_disk(id)
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-01 22:20:17 -05:00
|
|
|
pub(crate) fn try_load_from_disk<'tcx, V>(
|
|
|
|
tcx: QueryCtxt<'tcx>,
|
|
|
|
id: SerializedDepNodeIndex,
|
|
|
|
) -> Option<V>
|
|
|
|
where
|
|
|
|
V: for<'a> Decodable<CacheDecoder<'a, 'tcx>>,
|
|
|
|
{
|
|
|
|
tcx.on_disk_cache().as_ref()?.try_load_query_result(*tcx, id)
|
|
|
|
}
|
|
|
|
|
2023-02-07 08:32:30 +01:00
|
|
|
fn force_from_dep_node<'tcx, Q>(query: Q, tcx: TyCtxt<'tcx>, dep_node: DepNode) -> bool
|
2022-08-23 22:24:39 -05:00
|
|
|
where
|
2022-11-05 16:04:43 +01:00
|
|
|
Q: QueryConfig<QueryCtxt<'tcx>>,
|
2022-08-23 22:24:39 -05:00
|
|
|
{
|
2022-09-18 16:35:48 -05:00
|
|
|
// We must avoid ever having to call `force_from_dep_node()` for a
|
|
|
|
// `DepNode::codegen_unit`:
|
|
|
|
// Since we cannot reconstruct the query key of a `DepNode::codegen_unit`, we
|
|
|
|
// would always end up having to evaluate the first caller of the
|
|
|
|
// `codegen_unit` query that *is* reconstructible. This might very well be
|
|
|
|
// the `compile_codegen_unit` query, thus re-codegenning the whole CGU just
|
|
|
|
// to re-trigger calling the `codegen_unit` query with the right key. At
|
|
|
|
// that point we would already have re-done all the work we are trying to
|
|
|
|
// avoid doing in the first place.
|
|
|
|
// The solution is simple: Just explicitly call the `codegen_unit` query for
|
|
|
|
// each CGU, right after partitioning. This way `try_mark_green` will always
|
|
|
|
// hit the cache instead of having to go through `force_from_dep_node`.
|
|
|
|
// This assertion makes sure, we actually keep applying the solution above.
|
|
|
|
debug_assert!(
|
|
|
|
dep_node.kind != DepKind::codegen_unit,
|
|
|
|
"calling force_from_dep_node() on DepKind::codegen_unit"
|
|
|
|
);
|
|
|
|
|
2022-08-23 22:49:20 -05:00
|
|
|
if let Some(key) = Q::Key::recover(tcx, &dep_node) {
|
2022-08-23 22:24:39 -05:00
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
let _guard = tracing::span!(tracing::Level::TRACE, stringify!($name), ?key).entered();
|
|
|
|
let tcx = QueryCtxt::from_tcx(tcx);
|
2023-02-07 08:32:30 +01:00
|
|
|
force_query(query, tcx, key, dep_node);
|
2022-08-23 22:24:39 -05:00
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-05 16:04:43 +01:00
|
|
|
pub(crate) fn query_callback<'tcx, Q>(is_anon: bool, is_eval_always: bool) -> DepKindStruct<'tcx>
|
2022-08-23 23:31:12 -05:00
|
|
|
where
|
2023-02-07 08:32:30 +01:00
|
|
|
Q: QueryConfig<QueryCtxt<'tcx>> + Default,
|
2022-08-23 23:31:12 -05:00
|
|
|
Q::Key: DepNodeParams<TyCtxt<'tcx>>,
|
|
|
|
{
|
|
|
|
let fingerprint_style = Q::Key::fingerprint_style();
|
|
|
|
|
|
|
|
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,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
DepKindStruct {
|
|
|
|
is_anon,
|
|
|
|
is_eval_always,
|
|
|
|
fingerprint_style,
|
2023-02-07 08:32:30 +01:00
|
|
|
force_from_dep_node: Some(|tcx, dep_node| force_from_dep_node(Q::default(), tcx, dep_node)),
|
|
|
|
try_load_from_on_disk_cache: Some(|tcx, dep_node| {
|
|
|
|
try_load_from_on_disk_cache(Q::default(), tcx, dep_node)
|
|
|
|
}),
|
2022-08-23 23:31:12 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-13 18:32:22 -05:00
|
|
|
macro_rules! expand_if_cached {
|
|
|
|
([], $tokens:expr) => {{
|
|
|
|
None
|
|
|
|
}};
|
|
|
|
([(cache) $($rest:tt)*], $tokens:expr) => {{
|
|
|
|
Some($tokens)
|
|
|
|
}};
|
|
|
|
([$other:tt $($modifiers:tt)*], $tokens:expr) => {
|
|
|
|
expand_if_cached!([$($modifiers)*], $tokens)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-08-11 20:36:13 -05:00
|
|
|
// NOTE: `$V` isn't used here, but we still need to match on it so it can be passed to other macros
|
|
|
|
// invoked by `rustc_query_append`.
|
2020-10-11 20:44:24 +02:00
|
|
|
macro_rules! define_queries {
|
2022-08-11 19:54:42 -05:00
|
|
|
(
|
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! {
|
|
|
|
input: ($(([$($modifiers)*] [$($attr)*] [$name]))*)
|
|
|
|
}
|
|
|
|
|
2020-07-05 22:58:46 +03: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;
|
|
|
|
|
2023-02-07 08:32:30 +01:00
|
|
|
$(
|
2023-02-26 23:45:29 +01:00
|
|
|
#[derive(Copy, Clone, Default)]
|
2023-02-07 08:32:30 +01:00
|
|
|
pub struct $name<'tcx> {
|
|
|
|
data: PhantomData<&'tcx ()>
|
|
|
|
}
|
|
|
|
)*
|
2020-03-19 14:24:21 +01:00
|
|
|
}
|
|
|
|
|
2022-11-05 16:04:43 +01:00
|
|
|
$(impl<'tcx> QueryConfig<QueryCtxt<'tcx>> for queries::$name<'tcx> {
|
2022-08-11 19:54:42 -05:00
|
|
|
type Key = query_keys::$name<'tcx>;
|
|
|
|
type Value = query_values::$name<'tcx>;
|
2023-02-07 08:32:30 +01:00
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn name(self) -> &'static str {
|
|
|
|
stringify!($name)
|
|
|
|
}
|
2020-03-19 14:24:21 +01:00
|
|
|
|
2022-10-10 20:03:19 +02:00
|
|
|
#[inline]
|
2023-02-07 08:32:30 +01:00
|
|
|
fn cache_on_disk(self, tcx: TyCtxt<'tcx>, key: &Self::Key) -> bool {
|
2022-10-10 20:03:19 +02:00
|
|
|
::rustc_middle::query::cached::$name(tcx, key)
|
|
|
|
}
|
2020-03-19 14:24:21 +01:00
|
|
|
|
2022-08-11 19:54:42 -05:00
|
|
|
type Cache = query_storage::$name<'tcx>;
|
2020-03-19 14:24:21 +01:00
|
|
|
|
|
|
|
#[inline(always)]
|
2023-02-07 08:32:30 +01:00
|
|
|
fn query_state<'a>(self, tcx: QueryCtxt<'tcx>) -> &'a QueryState<Self::Key, crate::dep_graph::DepKind>
|
2022-08-11 19:54:42 -05:00
|
|
|
where QueryCtxt<'tcx>: 'a
|
2020-12-26 16:36:55 +01:00
|
|
|
{
|
2020-03-19 14:24:21 +01:00
|
|
|
&tcx.queries.$name
|
|
|
|
}
|
|
|
|
|
2021-02-06 13:49:08 +01:00
|
|
|
#[inline(always)]
|
2023-02-07 08:32:30 +01:00
|
|
|
fn query_cache<'a>(self, tcx: QueryCtxt<'tcx>) -> &'a Self::Cache
|
2021-02-06 13:49:08 +01:00
|
|
|
where 'tcx:'a
|
|
|
|
{
|
2023-02-08 19:53:48 +01:00
|
|
|
&tcx.query_system.caches.$name
|
2021-02-06 13:49:08 +01:00
|
|
|
}
|
|
|
|
|
2023-02-07 08:32:30 +01:00
|
|
|
fn execute_query(self, tcx: TyCtxt<'tcx>, key: Self::Key) -> Self::Value {
|
2022-12-30 23:25:19 +01:00
|
|
|
tcx.$name(key)
|
|
|
|
}
|
|
|
|
|
2020-03-19 14:24:21 +01:00
|
|
|
#[inline]
|
2022-12-30 23:25:19 +01:00
|
|
|
#[allow(unused_variables)]
|
2023-02-07 08:32:30 +01:00
|
|
|
fn compute(self, qcx: QueryCtxt<'tcx>, key: Self::Key) -> Self::Value {
|
2023-02-08 19:53:48 +01:00
|
|
|
query_provided_to_value::$name(
|
2023-02-14 14:17:04 +01:00
|
|
|
qcx.tcx,
|
|
|
|
get_provider!([$($modifiers)*][qcx, $name, key])(qcx.tcx, key)
|
2023-02-08 19:53:48 +01:00
|
|
|
)
|
2020-03-19 14:24:21 +01:00
|
|
|
}
|
2022-08-29 10:20:14 -05:00
|
|
|
|
2022-12-30 23:25:19 +01:00
|
|
|
#[inline]
|
2023-02-07 08:32:30 +01:00
|
|
|
fn try_load_from_disk(
|
|
|
|
self,
|
|
|
|
_qcx: QueryCtxt<'tcx>,
|
|
|
|
_key: &Self::Key
|
|
|
|
) -> rustc_query_system::query::TryLoadFromDisk<QueryCtxt<'tcx>, Self::Value> {
|
2023-02-08 19:53:48 +01:00
|
|
|
should_ever_cache_on_disk!([$($modifiers)*] {
|
2023-02-07 08:32:30 +01:00
|
|
|
if ::rustc_middle::query::cached::$name(_qcx.tcx, _key) {
|
2023-02-08 19:53:48 +01:00
|
|
|
Some(|qcx: QueryCtxt<'tcx>, dep_node| {
|
|
|
|
let value = $crate::plumbing::try_load_from_disk::<query_provided::$name<'tcx>>(
|
|
|
|
qcx,
|
|
|
|
dep_node
|
|
|
|
);
|
|
|
|
value.map(|value| query_provided_to_value::$name(qcx.tcx, value))
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
} {
|
|
|
|
None
|
|
|
|
})
|
2022-08-29 10:20:14 -05:00
|
|
|
}
|
2022-12-30 23:25:19 +01:00
|
|
|
|
2023-03-06 14:57:05 +00:00
|
|
|
#[inline]
|
|
|
|
fn loadable_from_disk(
|
|
|
|
self,
|
|
|
|
_qcx: QueryCtxt<'tcx>,
|
|
|
|
_key: &Self::Key,
|
|
|
|
_index: SerializedDepNodeIndex,
|
|
|
|
) -> bool {
|
|
|
|
should_ever_cache_on_disk!([$($modifiers)*] {
|
|
|
|
self.cache_on_disk(_qcx.tcx, _key) &&
|
|
|
|
$crate::plumbing::loadable_from_disk(_qcx, _index)
|
|
|
|
} {
|
|
|
|
false
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-02-07 08:32:30 +01:00
|
|
|
#[inline(always)]
|
|
|
|
fn anon(self) -> bool {
|
|
|
|
is_anon!([$($modifiers)*])
|
|
|
|
}
|
2022-12-30 23:25:19 +01:00
|
|
|
|
2023-02-07 08:32:30 +01:00
|
|
|
#[inline(always)]
|
|
|
|
fn eval_always(self) -> bool {
|
|
|
|
is_eval_always!([$($modifiers)*])
|
|
|
|
}
|
2022-12-30 23:25:19 +01:00
|
|
|
|
2023-02-07 08:32:30 +01:00
|
|
|
#[inline(always)]
|
|
|
|
fn depth_limit(self) -> bool {
|
|
|
|
depth_limit!([$($modifiers)*])
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn feedable(self) -> bool {
|
|
|
|
feedable!([$($modifiers)*])
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn dep_kind(self) -> rustc_middle::dep_graph::DepKind {
|
|
|
|
dep_graph::DepKind::$name
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn handle_cycle_error(self) -> rustc_query_system::HandleCycleError {
|
|
|
|
handle_cycle_error!([$($modifiers)*])
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn hash_result(self) -> rustc_query_system::query::HashResult<Self::Value> {
|
|
|
|
hash_result!([$($modifiers)*])
|
|
|
|
}
|
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::*;
|
2021-10-02 12:06:42 -04:00
|
|
|
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.
|
2022-08-29 10:20:14 -05:00
|
|
|
pub fn Null<'tcx>() -> DepKindStruct<'tcx> {
|
2021-10-16 21:12:34 +02:00
|
|
|
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
|
|
|
|
2022-05-03 22:04:49 +02:00
|
|
|
// We use this for the forever-red node.
|
2022-08-29 10:20:14 -05:00
|
|
|
pub fn Red<'tcx>() -> DepKindStruct<'tcx> {
|
2022-05-03 22:04:49 +02:00
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-29 10:20:14 -05:00
|
|
|
pub fn TraitSelect<'tcx>() -> DepKindStruct<'tcx> {
|
2021-10-16 21:12:34 +02:00
|
|
|
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
|
|
|
|
2022-08-29 10:20:14 -05:00
|
|
|
pub fn CompileCodegenUnit<'tcx>() -> DepKindStruct<'tcx> {
|
2021-10-16 21:12:34 +02:00
|
|
|
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
|
|
|
|
2022-08-29 10:20:14 -05:00
|
|
|
pub fn CompileMonoItem<'tcx>() -> DepKindStruct<'tcx> {
|
2021-10-16 21:12:34 +02:00
|
|
|
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-08-29 10:20:14 -05:00
|
|
|
$(pub(crate) fn $name<'tcx>()-> DepKindStruct<'tcx> {
|
|
|
|
$crate::plumbing::query_callback::<queries::$name<'tcx>>(
|
|
|
|
is_anon!([$($modifiers)*]),
|
|
|
|
is_eval_always!([$($modifiers)*]),
|
2022-08-23 23:31:12 -05:00
|
|
|
)
|
2021-10-16 21:12:34 +02:00
|
|
|
})*
|
2021-01-19 19:07:06 +01:00
|
|
|
}
|
|
|
|
|
2022-09-13 18:32:22 -05:00
|
|
|
mod query_structs {
|
|
|
|
use rustc_middle::ty::TyCtxt;
|
|
|
|
use $crate::plumbing::{QueryStruct, QueryCtxt};
|
|
|
|
use $crate::profiling_support::QueryKeyStringCache;
|
2022-10-13 21:18:36 +02:00
|
|
|
use rustc_query_system::query::QueryMap;
|
2022-12-23 18:39:49 +05:30
|
|
|
use rustc_middle::dep_graph::DepKind;
|
2022-09-13 18:32:22 -05:00
|
|
|
|
|
|
|
pub(super) const fn dummy_query_struct<'tcx>() -> QueryStruct<'tcx> {
|
2022-12-23 18:39:49 +05:30
|
|
|
fn noop_try_collect_active_jobs(_: QueryCtxt<'_>, _: &mut QueryMap<DepKind>) -> Option<()> {
|
2022-09-13 18:32:22 -05:00
|
|
|
None
|
|
|
|
}
|
|
|
|
fn noop_alloc_self_profile_query_strings(_: TyCtxt<'_>, _: &mut QueryKeyStringCache) {}
|
|
|
|
|
|
|
|
QueryStruct {
|
|
|
|
try_collect_active_jobs: noop_try_collect_active_jobs,
|
|
|
|
alloc_self_profile_query_strings: noop_alloc_self_profile_query_strings,
|
|
|
|
encode_query_results: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(super) use dummy_query_struct as Null;
|
|
|
|
pub(super) use dummy_query_struct as Red;
|
|
|
|
pub(super) use dummy_query_struct as TraitSelect;
|
|
|
|
pub(super) use dummy_query_struct as CompileCodegenUnit;
|
|
|
|
pub(super) use dummy_query_struct as CompileMonoItem;
|
|
|
|
|
|
|
|
$(
|
|
|
|
pub(super) const fn $name<'tcx>() -> QueryStruct<'tcx> { QueryStruct {
|
|
|
|
try_collect_active_jobs: |tcx, qmap| {
|
|
|
|
let make_query = |tcx, key| {
|
|
|
|
let kind = rustc_middle::dep_graph::DepKind::$name;
|
|
|
|
let name = stringify!($name);
|
2022-10-13 21:18:36 +02:00
|
|
|
$crate::plumbing::create_query_frame(tcx, rustc_middle::query::descs::$name, key, kind, name)
|
2022-09-13 18:32:22 -05:00
|
|
|
};
|
|
|
|
tcx.queries.$name.try_collect_active_jobs(
|
|
|
|
tcx,
|
|
|
|
make_query,
|
|
|
|
qmap,
|
|
|
|
)
|
|
|
|
},
|
|
|
|
alloc_self_profile_query_strings: |tcx, string_cache| {
|
|
|
|
$crate::profiling_support::alloc_self_profile_query_strings_for_query_cache(
|
|
|
|
tcx,
|
|
|
|
stringify!($name),
|
2023-02-08 19:53:48 +01:00
|
|
|
&tcx.query_system.caches.$name,
|
2022-09-13 18:32:22 -05:00
|
|
|
string_cache,
|
|
|
|
)
|
|
|
|
},
|
2023-02-07 08:32:30 +01:00
|
|
|
encode_query_results: expand_if_cached!([$($modifiers)*], |qcx, encoder, query_result_index|
|
|
|
|
$crate::on_disk_cache::encode_query_results(
|
|
|
|
super::queries::$name::default(),
|
|
|
|
qcx,
|
|
|
|
encoder,
|
|
|
|
query_result_index,
|
|
|
|
)
|
2022-09-13 18:32:22 -05:00
|
|
|
),
|
|
|
|
}})*
|
|
|
|
}
|
|
|
|
|
2022-08-29 10:20:14 -05:00
|
|
|
pub fn query_callbacks<'tcx>(arena: &'tcx Arena<'tcx>) -> &'tcx [DepKindStruct<'tcx>] {
|
2021-10-16 21:12:34 +02:00
|
|
|
arena.alloc_from_iter(make_dep_kind_array!(query_callbacks))
|
|
|
|
}
|
2020-03-19 14:24:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-14 14:17:04 +01:00
|
|
|
use crate::{ExternProviders, OnDiskCache, Providers};
|
2022-08-29 19:30:25 -05:00
|
|
|
|
|
|
|
impl<'tcx> Queries<'tcx> {
|
2023-02-14 14:17:04 +01:00
|
|
|
pub fn new(
|
|
|
|
local_providers: Providers,
|
|
|
|
extern_providers: ExternProviders,
|
|
|
|
on_disk_cache: Option<OnDiskCache<'tcx>>,
|
|
|
|
) -> Self {
|
2022-09-13 18:32:22 -05:00
|
|
|
use crate::query_structs;
|
2022-08-29 19:30:25 -05:00
|
|
|
Queries {
|
2023-02-14 14:17:04 +01:00
|
|
|
local_providers: Box::new(local_providers),
|
|
|
|
extern_providers: Box::new(extern_providers),
|
2022-09-13 18:32:22 -05:00
|
|
|
query_structs: make_dep_kind_array!(query_structs).to_vec(),
|
2022-08-29 19:30:25 -05:00
|
|
|
on_disk_cache,
|
|
|
|
jobs: AtomicU64::new(1),
|
|
|
|
..Queries::default()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-19 14:24:21 +01:00
|
|
|
macro_rules! define_queries_struct {
|
2022-08-11 19:54:42 -05:00
|
|
|
(
|
2020-03-19 14:24:21 +01:00
|
|
|
input: ($(([$($modifiers:tt)*] [$($attr:tt)*] [$name:ident]))*)) => {
|
2022-08-29 19:30:25 -05:00
|
|
|
#[derive(Default)]
|
2022-08-11 19:54:42 -05:00
|
|
|
pub struct Queries<'tcx> {
|
2023-02-14 14:17:04 +01:00
|
|
|
local_providers: Box<Providers>,
|
|
|
|
extern_providers: Box<ExternProviders>,
|
2022-09-13 18:32:22 -05:00
|
|
|
query_structs: Vec<$crate::plumbing::QueryStruct<'tcx>>,
|
2022-08-11 19:54:42 -05:00
|
|
|
pub on_disk_cache: Option<OnDiskCache<'tcx>>,
|
2022-02-07 11:03:51 -05:00
|
|
|
jobs: AtomicU64,
|
|
|
|
|
2022-11-05 16:04:43 +01:00
|
|
|
$(
|
|
|
|
$(#[$attr])*
|
|
|
|
$name: QueryState<
|
2022-12-23 18:39:49 +05:30
|
|
|
<queries::$name<'tcx> as QueryConfig<QueryCtxt<'tcx>>>::Key,
|
|
|
|
rustc_middle::dep_graph::DepKind,
|
2022-11-05 16:04:43 +01:00
|
|
|
>,
|
|
|
|
)*
|
2020-03-19 14:24:21 +01:00
|
|
|
}
|
|
|
|
|
2022-08-11 19:54:42 -05:00
|
|
|
impl<'tcx> Queries<'tcx> {
|
2020-03-19 14:24:21 +01:00
|
|
|
pub(crate) fn try_collect_active_jobs(
|
2022-08-11 19:54:42 -05:00
|
|
|
&'tcx self,
|
|
|
|
tcx: TyCtxt<'tcx>,
|
2022-12-23 18:39:49 +05:30
|
|
|
) -> Option<QueryMap<rustc_middle::dep_graph::DepKind>> {
|
2020-12-26 16:36:55 +01:00
|
|
|
let tcx = QueryCtxt { tcx, queries: self };
|
2020-11-28 22:48:05 +01:00
|
|
|
let mut jobs = QueryMap::default();
|
2020-03-19 14:24:21 +01:00
|
|
|
|
2022-09-13 18:32:22 -05:00
|
|
|
for query in &self.query_structs {
|
|
|
|
(query.try_collect_active_jobs)(tcx, &mut jobs);
|
|
|
|
}
|
2020-03-19 14:24:21 +01:00
|
|
|
|
|
|
|
Some(jobs)
|
|
|
|
}
|
2021-01-18 22:32:20 +01:00
|
|
|
}
|
2021-01-17 14:58:34 +01:00
|
|
|
|
2021-12-14 12:13:07 -05: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)]
|
2022-06-28 15:18:07 +00:00
|
|
|
#[tracing::instrument(level = "trace", skip(self, tcx), ret)]
|
2021-01-17 14:58:34 +01:00
|
|
|
fn $name(
|
2020-10-11 10:34:13 +02:00
|
|
|
&'tcx self,
|
2022-08-11 19:54:42 -05:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2021-01-17 14:58:34 +01:00
|
|
|
span: Span,
|
2022-11-05 16:04:43 +01:00
|
|
|
key: <queries::$name<'tcx> as QueryConfig<QueryCtxt<'tcx>>>::Key,
|
2021-01-17 14:58:34 +01:00
|
|
|
mode: QueryMode,
|
2023-02-08 19:53:48 +01:00
|
|
|
) -> Option<query_values::$name<'tcx>> {
|
2020-10-11 10:34:13 +02:00
|
|
|
let qcx = QueryCtxt { tcx, queries: self };
|
2023-02-07 08:32:30 +01:00
|
|
|
get_query(
|
|
|
|
queries::$name::default(),
|
|
|
|
qcx,
|
|
|
|
span,
|
|
|
|
key,
|
|
|
|
mode
|
|
|
|
)
|
2021-01-17 14:58:34 +01:00
|
|
|
})*
|
2020-03-19 14:24:21 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|