Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor query system to maintain a global job id counter #93741

Merged
merged 1 commit into from
Feb 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions compiler/rustc_middle/src/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1668,7 +1668,7 @@ CloneLiftImpls! { for<'tcx> { Constness, traits::WellFormedLoc, } }
pub mod tls {
use super::{ptr_eq, GlobalCtxt, TyCtxt};

use crate::dep_graph::{DepKind, TaskDepsRef};
use crate::dep_graph::TaskDepsRef;
use crate::ty::query;
use rustc_data_structures::sync::{self, Lock};
use rustc_data_structures::thin_vec::ThinVec;
Expand All @@ -1693,7 +1693,7 @@ pub mod tls {

/// The current query job, if any. This is updated by `JobOwner::start` in
/// `ty::query::plumbing` when executing a query.
pub query: Option<query::QueryJobId<DepKind>>,
pub query: Option<query::QueryJobId>,

/// Where to store diagnostics for the current query job, if any.
/// This is updated by `JobOwner::start` in `ty::query::plumbing` when executing a query.
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_query_impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ extern crate rustc_macros;
extern crate rustc_middle;

use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
use rustc_data_structures::sync::AtomicU64;
use rustc_middle::arena::Arena;
use rustc_middle::dep_graph::{self, DepKindStruct, SerializedDepNodeIndex};
use rustc_middle::ty::query::{query_keys, query_storage, query_stored, query_values};
Expand Down
33 changes: 21 additions & 12 deletions compiler/rustc_query_impl/src/plumbing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//! manage the caches, and so forth.

use crate::{on_disk_cache, Queries};
use rustc_middle::dep_graph::{DepKind, DepNodeIndex, SerializedDepNodeIndex};
use rustc_middle::dep_graph::{DepNodeIndex, SerializedDepNodeIndex};
use rustc_middle::ty::tls::{self, ImplicitCtxt};
use rustc_middle::ty::TyCtxt;
use rustc_query_system::dep_graph::HasDepContext;
Expand All @@ -15,6 +15,7 @@ use rustc_errors::{Diagnostic, Handler};
use rustc_serialize::opaque;

use std::any::Any;
use std::num::NonZeroU64;

#[derive(Copy, Clone)]
pub struct QueryCtxt<'tcx> {
Expand Down Expand Up @@ -42,11 +43,20 @@ impl<'tcx> HasDepContext for QueryCtxt<'tcx> {
}

impl QueryContext for QueryCtxt<'_> {
fn current_query_job(&self) -> Option<QueryJobId<Self::DepKind>> {
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)
}

fn try_collect_active_jobs(&self) -> Option<QueryMap<Self::DepKind>> {
fn try_collect_active_jobs(&self) -> Option<QueryMap> {
self.queries.try_collect_active_jobs(**self)
}

Expand Down Expand Up @@ -81,7 +91,7 @@ impl QueryContext for QueryCtxt<'_> {
#[inline(always)]
fn start_query<R>(
&self,
token: QueryJobId<Self::DepKind>,
token: QueryJobId,
diagnostics: Option<&Lock<ThinVec<Diagnostic>>>,
compute: impl FnOnce() -> R,
) -> R {
Expand Down Expand Up @@ -152,7 +162,7 @@ impl<'tcx> QueryCtxt<'tcx> {

pub fn try_print_query_stack(
self,
query: Option<QueryJobId<DepKind>>,
query: Option<QueryJobId>,
handler: &Handler,
num_frames: Option<usize>,
) -> usize {
Expand Down Expand Up @@ -320,7 +330,7 @@ macro_rules! define_queries {
type Cache = query_storage::$name<$tcx>;

#[inline(always)]
fn query_state<'a>(tcx: QueryCtxt<$tcx>) -> &'a QueryState<crate::dep_graph::DepKind, Self::Key>
fn query_state<'a>(tcx: QueryCtxt<$tcx>) -> &'a QueryState<Self::Key>
where QueryCtxt<$tcx>: 'a
{
&tcx.queries.$name
Expand Down Expand Up @@ -471,10 +481,9 @@ macro_rules! define_queries_struct {

pub on_disk_cache: Option<OnDiskCache<$tcx>>,

$($(#[$attr])* $name: QueryState<
crate::dep_graph::DepKind,
query_keys::$name<$tcx>,
>,)*
jobs: AtomicU64,

$($(#[$attr])* $name: QueryState<query_keys::$name<$tcx>>,)*
}

impl<$tcx> Queries<$tcx> {
Expand All @@ -487,21 +496,21 @@ macro_rules! define_queries_struct {
local_providers: Box::new(local_providers),
extern_providers: Box::new(extern_providers),
on_disk_cache,
jobs: AtomicU64::new(1),
$($name: Default::default()),*
}
}

pub(crate) fn try_collect_active_jobs(
&$tcx self,
tcx: TyCtxt<$tcx>,
) -> Option<QueryMap<crate::dep_graph::DepKind>> {
) -> Option<QueryMap> {
let tcx = QueryCtxt { tcx, queries: self };
let mut jobs = QueryMap::default();

$(
self.$name.try_collect_active_jobs(
tcx,
dep_graph::DepKind::$name,
make_query::$name,
&mut jobs,
)?;
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_query_system/src/query/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ pub trait QueryDescription<CTX: QueryContext>: QueryConfig {
fn describe(tcx: CTX, key: Self::Key) -> String;

// 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, Self::Key>
fn query_state<'a>(tcx: CTX) -> &'a QueryState<Self::Key>
where
CTX: 'a;

Expand Down
Loading