Skip to content

Commit e7aca89

Browse files
committed
Auto merge of #93741 - Mark-Simulacrum:global-job-id, r=cjgillot
Refactor query system to maintain a global job id counter This replaces the per-shard counters with a single global counter, simplifying the JobId struct down to just a u64 and removing the need to pipe a DepKind generic through a bunch of code. The performance implications on non-parallel compilers are likely minimal (this switches to `Cell<u64>` as the backing storage over a `u64`, but the latter was already inside a `RefCell` so it's not really a significance divergence). On parallel compilers, the cost of a single global u64 counter may be more significant: it adds a serialization point in theory. On the other hand, we can imagine changing the counter to have a thread-local component if it becomes worrisome or some similar structure. The new design is sufficiently simpler that it warrants the potential for slight changes down the line if/when we get parallel compilation to be more of a default. A u64 counter, instead of u32 (the old per-shard width), is chosen to avoid possibly overflowing it and causing problems; it is effectively impossible that we would overflow a u64 counter in this context.
2 parents 9747ee4 + e240783 commit e7aca89

File tree

7 files changed

+115
-177
lines changed

7 files changed

+115
-177
lines changed

compiler/rustc_middle/src/ty/context.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1673,7 +1673,7 @@ CloneLiftImpls! { for<'tcx> { Constness, traits::WellFormedLoc, } }
16731673
pub mod tls {
16741674
use super::{ptr_eq, GlobalCtxt, TyCtxt};
16751675

1676-
use crate::dep_graph::{DepKind, TaskDepsRef};
1676+
use crate::dep_graph::TaskDepsRef;
16771677
use crate::ty::query;
16781678
use rustc_data_structures::sync::{self, Lock};
16791679
use rustc_data_structures::thin_vec::ThinVec;
@@ -1698,7 +1698,7 @@ pub mod tls {
16981698

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

17031703
/// Where to store diagnostics for the current query job, if any.
17041704
/// This is updated by `JobOwner::start` in `ty::query::plumbing` when executing a query.

compiler/rustc_query_impl/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ extern crate rustc_macros;
1515
extern crate rustc_middle;
1616

1717
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
18+
use rustc_data_structures::sync::AtomicU64;
1819
use rustc_middle::arena::Arena;
1920
use rustc_middle::dep_graph::{self, DepKindStruct, SerializedDepNodeIndex};
2021
use rustc_middle::ty::query::{query_keys, query_storage, query_stored, query_values};

compiler/rustc_query_impl/src/plumbing.rs

+21-12
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//! manage the caches, and so forth.
44
55
use crate::{on_disk_cache, Queries};
6-
use rustc_middle::dep_graph::{DepKind, DepNodeIndex, SerializedDepNodeIndex};
6+
use rustc_middle::dep_graph::{DepNodeIndex, SerializedDepNodeIndex};
77
use rustc_middle::ty::tls::{self, ImplicitCtxt};
88
use rustc_middle::ty::TyCtxt;
99
use rustc_query_system::dep_graph::HasDepContext;
@@ -15,6 +15,7 @@ use rustc_errors::{Diagnostic, Handler};
1515
use rustc_serialize::opaque;
1616

1717
use std::any::Any;
18+
use std::num::NonZeroU64;
1819

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

4445
impl QueryContext for QueryCtxt<'_> {
45-
fn current_query_job(&self) -> Option<QueryJobId<Self::DepKind>> {
46+
fn next_job_id(&self) -> QueryJobId {
47+
QueryJobId(
48+
NonZeroU64::new(
49+
self.queries.jobs.fetch_add(1, rustc_data_structures::sync::Ordering::Relaxed),
50+
)
51+
.unwrap(),
52+
)
53+
}
54+
55+
fn current_query_job(&self) -> Option<QueryJobId> {
4656
tls::with_related_context(**self, |icx| icx.query)
4757
}
4858

49-
fn try_collect_active_jobs(&self) -> Option<QueryMap<Self::DepKind>> {
59+
fn try_collect_active_jobs(&self) -> Option<QueryMap> {
5060
self.queries.try_collect_active_jobs(**self)
5161
}
5262

@@ -81,7 +91,7 @@ impl QueryContext for QueryCtxt<'_> {
8191
#[inline(always)]
8292
fn start_query<R>(
8393
&self,
84-
token: QueryJobId<Self::DepKind>,
94+
token: QueryJobId,
8595
diagnostics: Option<&Lock<ThinVec<Diagnostic>>>,
8696
compute: impl FnOnce() -> R,
8797
) -> R {
@@ -152,7 +162,7 @@ impl<'tcx> QueryCtxt<'tcx> {
152162

153163
pub fn try_print_query_stack(
154164
self,
155-
query: Option<QueryJobId<DepKind>>,
165+
query: Option<QueryJobId>,
156166
handler: &Handler,
157167
num_frames: Option<usize>,
158168
) -> usize {
@@ -320,7 +330,7 @@ macro_rules! define_queries {
320330
type Cache = query_storage::$name<$tcx>;
321331

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

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

474-
$($(#[$attr])* $name: QueryState<
475-
crate::dep_graph::DepKind,
476-
query_keys::$name<$tcx>,
477-
>,)*
484+
jobs: AtomicU64,
485+
486+
$($(#[$attr])* $name: QueryState<query_keys::$name<$tcx>>,)*
478487
}
479488

480489
impl<$tcx> Queries<$tcx> {
@@ -487,21 +496,21 @@ macro_rules! define_queries_struct {
487496
local_providers: Box::new(local_providers),
488497
extern_providers: Box::new(extern_providers),
489498
on_disk_cache,
499+
jobs: AtomicU64::new(1),
490500
$($name: Default::default()),*
491501
}
492502
}
493503

494504
pub(crate) fn try_collect_active_jobs(
495505
&$tcx self,
496506
tcx: TyCtxt<$tcx>,
497-
) -> Option<QueryMap<crate::dep_graph::DepKind>> {
507+
) -> Option<QueryMap> {
498508
let tcx = QueryCtxt { tcx, queries: self };
499509
let mut jobs = QueryMap::default();
500510

501511
$(
502512
self.$name.try_collect_active_jobs(
503513
tcx,
504-
dep_graph::DepKind::$name,
505514
make_query::$name,
506515
&mut jobs,
507516
)?;

compiler/rustc_query_system/src/query/config.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ pub trait QueryDescription<CTX: QueryContext>: QueryConfig {
5959
fn describe(tcx: CTX, key: Self::Key) -> String;
6060

6161
// Don't use this method to access query results, instead use the methods on TyCtxt
62-
fn query_state<'a>(tcx: CTX) -> &'a QueryState<CTX::DepKind, Self::Key>
62+
fn query_state<'a>(tcx: CTX) -> &'a QueryState<Self::Key>
6363
where
6464
CTX: 'a;
6565

0 commit comments

Comments
 (0)