Skip to content

Commit e73bdda

Browse files
committed
Auto merge of #153950 - Zalathar:dep-kind-vtables, r=<try>
Store `dep_kind_vtables` in an array inside `QuerySystem`
2 parents fe7294f + 14b3d0a commit e73bdda

File tree

8 files changed

+33
-46
lines changed

8 files changed

+33
-46
lines changed

compiler/rustc_interface/src/passes.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -988,7 +988,6 @@ pub fn create_and_enter_global_ctxt<T, F: for<'tcx> FnOnce(TyCtxt<'tcx>) -> T>(
988988
&hir_arena,
989989
untracked,
990990
dep_graph,
991-
rustc_query_impl::make_dep_kind_vtables(&arena),
992991
rustc_query_impl::query_system(
993992
providers.queries,
994993
providers.extern_queries,

compiler/rustc_middle/src/dep_graph/dep_node.rs

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@
4747
//!
4848
//! [dependency graph]: https://rustc-dev-guide.rust-lang.org/query.html
4949
50-
use std::fmt;
5150
use std::hash::Hash;
51+
use std::{fmt, mem};
5252

5353
use rustc_data_structures::fingerprint::{Fingerprint, PackedFingerprint};
5454
use rustc_data_structures::stable_hasher::{StableHasher, StableOrd, ToStableHashKey};
@@ -66,11 +66,23 @@ use crate::ty::{TyCtxt, tls};
6666
impl DepKind {
6767
#[inline]
6868
pub(crate) fn from_u16(u: u16) -> Self {
69+
// Statically assert that every u16 up to `DepKind::MAX` can be transmuted
70+
// into a `DepKind` that round-trips back to that number.
71+
const _: () = {
72+
assert!(DepKind::MAX as usize + 1 == DepKind::NUM_VARIANTS);
73+
let mut i = 0;
74+
while i <= DepKind::MAX {
75+
let dep_kind = unsafe { mem::transmute::<u16, DepKind>(i) };
76+
assert!(dep_kind as u16 == i);
77+
i += 1;
78+
}
79+
};
80+
6981
if u > Self::MAX {
7082
panic!("Invalid DepKind {u}");
7183
}
72-
// SAFETY: See comment on DEP_KIND_NUM_VARIANTS
73-
unsafe { std::mem::transmute(u) }
84+
// SAFETY: See the static assertion above.
85+
unsafe { mem::transmute(u) }
7486
}
7587

7688
#[inline]
@@ -85,7 +97,7 @@ impl DepKind {
8597

8698
/// This is the highest value a `DepKind` can have. It's used during encoding to
8799
/// pack information into the unused bits.
88-
pub(crate) const MAX: u16 = DEP_KIND_NUM_VARIANTS - 1;
100+
pub(crate) const MAX: u16 = (DepKind::NUM_VARIANTS - 1) as u16;
89101
}
90102

91103
/// Combination of a [`DepKind`] and a key fingerprint that uniquely identifies
@@ -293,24 +305,10 @@ macro_rules! define_dep_nodes {
293305
$( $(#[$q_attr])* $q_name, )*
294306
}
295307

296-
// This computes the number of dep kind variants. Along the way, it sanity-checks that the
297-
// discriminants of the variants have been assigned consecutively from 0 so that they can
298-
// be used as a dense index, and that all discriminants fit in a `u16`.
299-
pub(crate) const DEP_KIND_NUM_VARIANTS: u16 = {
300-
let deps = &[
301-
$(DepKind::$nq_name,)*
302-
$(DepKind::$q_name,)*
303-
];
304-
let mut i = 0;
305-
while i < deps.len() {
306-
if i != deps[i].as_usize() {
307-
panic!();
308-
}
309-
i += 1;
310-
}
311-
assert!(deps.len() <= u16::MAX as usize);
312-
deps.len() as u16
313-
};
308+
impl DepKind {
309+
/// The total number of variants in [`DepKind`].
310+
pub const NUM_VARIANTS: usize = ${count($nq_name)} + ${count($q_name)};
311+
}
314312

315313
pub(super) fn dep_kind_from_label_string(label: &str) -> Result<DepKind, ()> {
316314
match label {

compiler/rustc_middle/src/dep_graph/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ where
8282
impl<'tcx> TyCtxt<'tcx> {
8383
#[inline]
8484
pub fn dep_kind_vtable(self, dk: DepKind) -> &'tcx DepKindVTable<'tcx> {
85-
&self.dep_kind_vtables[dk.as_usize()]
85+
&self.query_system.dep_kind_vtables[dk.as_usize()]
8686
}
8787

8888
fn with_reduced_queries<T>(self, f: impl FnOnce() -> T) -> T {

compiler/rustc_middle/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
#![feature(extern_types)]
4343
#![feature(file_buffered)]
4444
#![feature(gen_blocks)]
45+
#![feature(macro_metavar_expr)]
4546
#![feature(min_specialization)]
4647
#![feature(negative_impls)]
4748
#![feature(never_type)]

compiler/rustc_middle/src/query/plumbing.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rustc_hir::hir_id::OwnerId;
1111
use rustc_span::{Span, Spanned};
1212
pub use sealed::IntoQueryParam;
1313

14-
use crate::dep_graph::{DepKind, DepNodeIndex, SerializedDepNodeIndex};
14+
use crate::dep_graph::{DepKind, DepKindVTable, DepNodeIndex, SerializedDepNodeIndex};
1515
use crate::ich::StableHashingContext;
1616
use crate::queries::{ExternProviders, Providers, QueryArenas, QueryVTables, TaggedQueryKey};
1717
use crate::query::on_disk_cache::OnDiskCache;
@@ -159,6 +159,7 @@ impl<'tcx, C: QueryCache> fmt::Debug for QueryVTable<'tcx, C> {
159159
pub struct QuerySystem<'tcx> {
160160
pub arenas: WorkerLocal<QueryArenas<'tcx>>,
161161
pub query_vtables: QueryVTables<'tcx>,
162+
pub dep_kind_vtables: [DepKindVTable<'tcx>; DepKind::NUM_VARIANTS],
162163

163164
/// This provides access to the incremental compilation on-disk cache for query results.
164165
/// Do not access this directly. It is only meant to be used by

compiler/rustc_middle/src/ty/context.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ use tracing::{debug, instrument};
5252

5353
use crate::arena::Arena;
5454
use crate::dep_graph::dep_node::make_metadata;
55-
use crate::dep_graph::{DepGraph, DepKindVTable, DepNodeIndex};
55+
use crate::dep_graph::{DepGraph, DepNodeIndex};
5656
use crate::ich::StableHashingContext;
5757
use crate::infer::canonical::{CanonicalParamEnvCache, CanonicalVarKind};
5858
use crate::lint::emit_lint_base;
@@ -795,7 +795,6 @@ pub struct GlobalCtxt<'tcx> {
795795
untracked: Untracked,
796796

797797
pub query_system: QuerySystem<'tcx>,
798-
pub(crate) dep_kind_vtables: &'tcx [DepKindVTable<'tcx>],
799798

800799
// Internal caches for metadata decoding. No need to track deps on this.
801800
pub ty_rcache: Lock<FxHashMap<ty::CReaderCacheKey, Ty<'tcx>>>,
@@ -1018,7 +1017,6 @@ impl<'tcx> TyCtxt<'tcx> {
10181017
hir_arena: &'tcx WorkerLocal<hir::Arena<'tcx>>,
10191018
untracked: Untracked,
10201019
dep_graph: DepGraph,
1021-
dep_kind_vtables: &'tcx [DepKindVTable<'tcx>],
10221020
query_system: QuerySystem<'tcx>,
10231021
hooks: crate::hooks::Providers,
10241022
current_gcx: CurrentGcx,
@@ -1048,7 +1046,6 @@ impl<'tcx> TyCtxt<'tcx> {
10481046
consts: common_consts,
10491047
untracked,
10501048
query_system,
1051-
dep_kind_vtables,
10521049
ty_rcache: Default::default(),
10531050
selection_cache: Default::default(),
10541051
evaluation_cache: Default::default(),

compiler/rustc_query_impl/src/dep_kind_vtables.rs

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
use rustc_middle::arena::Arena;
21
use rustc_middle::bug;
3-
use rustc_middle::dep_graph::{DepKindVTable, DepNodeKey, KeyFingerprintStyle};
2+
use rustc_middle::dep_graph::{DepKind, DepKindVTable, DepNodeKey, KeyFingerprintStyle};
43
use rustc_middle::query::QueryCache;
54

65
use crate::GetQueryVTable;
@@ -152,15 +151,13 @@ macro_rules! define_dep_kind_vtables {
152151
)*
153152
}
154153
) => {{
155-
// The small number of non-query vtables: `Null`, `Red`, etc.
156-
let nq_vtables = [
154+
[
155+
// The small number of non-query vtables: `Null`, `Red`, etc.
157156
$(
158157
non_query::$nq_name(),
159158
)*
160-
];
161159

162-
// The large number of query vtables.
163-
let q_vtables: [DepKindVTable<'tcx>; _] = [
160+
// The large number of query vtables.
164161
$(
165162
$crate::dep_kind_vtables::make_dep_kind_vtable_for_query::<
166163
$crate::query_impl::$name::VTableGetter,
@@ -170,17 +167,11 @@ macro_rules! define_dep_kind_vtables {
170167
$eval_always,
171168
)
172169
),*
173-
];
174-
175-
(nq_vtables, q_vtables)
170+
]
176171
}}
177172
}
178173

179174
// Create an array of vtables, one for each dep kind (non-query and query).
180-
pub fn make_dep_kind_vtables<'tcx>(arena: &'tcx Arena<'tcx>) -> &'tcx [DepKindVTable<'tcx>] {
181-
let (nq_vtables, q_vtables) =
182-
rustc_middle::queries::rustc_with_all_queries! { define_dep_kind_vtables! };
183-
184-
// Non-query vtables must come before query vtables, to match the order of `DepKind`.
185-
arena.alloc_from_iter(nq_vtables.into_iter().chain(q_vtables.into_iter()))
175+
pub(crate) fn make_dep_kind_vtables<'tcx>() -> [DepKindVTable<'tcx>; DepKind::NUM_VARIANTS] {
176+
rustc_middle::queries::rustc_with_all_queries! { define_dep_kind_vtables! }
186177
}

compiler/rustc_query_impl/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ use rustc_middle::query::on_disk_cache::OnDiskCache;
1515
use rustc_middle::query::{QueryCache, QuerySystem, QueryVTable};
1616
use rustc_middle::ty::TyCtxt;
1717

18-
pub use crate::dep_kind_vtables::make_dep_kind_vtables;
1918
pub use crate::execution::{CollectActiveJobsKind, collect_active_query_jobs};
2019
pub use crate::job::{QueryJobMap, break_query_cycles, print_query_stack};
2120

@@ -53,6 +52,7 @@ pub fn query_system<'tcx>(
5352
QuerySystem {
5453
arenas: Default::default(),
5554
query_vtables,
55+
dep_kind_vtables: dep_kind_vtables::make_dep_kind_vtables(),
5656
on_disk_cache,
5757
local_providers,
5858
extern_providers,

0 commit comments

Comments
 (0)