Rollup merge of #151402 - dep-kind-name, r=petrochenkov

Simplify lookup of `DepKind` names in duplicate dep node check

This PR simplifies parts of the query system, by removing the `make_dep_kind_name_array!` macro, and removing much of the associated plumbing added by 68fd771bc1.

Instead, we now create a `DEP_KIND_NAMES` constant in `define_dep_nodes!`, and look up names in that instead.
This commit is contained in:
Jacob Pratt 2026-01-21 02:04:02 -05:00 committed by GitHub
commit 980547529a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 31 additions and 39 deletions

View file

@ -91,10 +91,7 @@ fn delete_dirty_work_product(sess: &Session, swp: SerializedWorkProduct) {
work_product::delete_workproduct_files(sess, &swp.work_product);
}
fn load_dep_graph(
sess: &Session,
deps: &DepsType,
) -> LoadResult<(Arc<SerializedDepGraph>, WorkProductMap)> {
fn load_dep_graph(sess: &Session) -> LoadResult<(Arc<SerializedDepGraph>, WorkProductMap)> {
let prof = sess.prof.clone();
if sess.opts.incremental.is_none() {
@ -174,7 +171,7 @@ fn load_dep_graph(
return LoadResult::DataOutOfDate;
}
let dep_graph = SerializedDepGraph::decode::<DepsType>(&mut decoder, deps);
let dep_graph = SerializedDepGraph::decode::<DepsType>(&mut decoder);
LoadResult::Ok { data: (dep_graph, prev_work_products) }
}
@ -212,12 +209,11 @@ pub fn setup_dep_graph(
sess: &Session,
crate_name: Symbol,
stable_crate_id: StableCrateId,
deps: &DepsType,
) -> DepGraph {
// `load_dep_graph` can only be called after `prepare_session_directory`.
prepare_session_directory(sess, crate_name, stable_crate_id);
let res = sess.opts.build_dep_graph().then(|| load_dep_graph(sess, deps));
let res = sess.opts.build_dep_graph().then(|| load_dep_graph(sess));
if sess.opts.incremental.is_some() {
sess.time("incr_comp_garbage_collect_session_directories", || {

View file

@ -26,7 +26,6 @@ use rustc_lint::{BufferedEarlyLint, EarlyCheckNode, LintStore, unerased_lint_sto
use rustc_metadata::EncodedMetadata;
use rustc_metadata::creader::CStore;
use rustc_middle::arena::Arena;
use rustc_middle::dep_graph::DepsType;
use rustc_middle::ty::{self, CurrentGcx, GlobalCtxt, RegisteredTools, TyCtxt};
use rustc_middle::util::Providers;
use rustc_parse::lexer::StripTokens;
@ -940,8 +939,7 @@ pub fn create_and_enter_global_ctxt<T, F: for<'tcx> FnOnce(TyCtxt<'tcx>) -> T>(
let outputs = util::build_output_filenames(&pre_configured_attrs, sess);
let dep_type = DepsType { dep_names: rustc_query_impl::dep_kind_names() };
let dep_graph = setup_dep_graph(sess, crate_name, stable_crate_id, &dep_type);
let dep_graph = setup_dep_graph(sess, crate_name, stable_crate_id);
let cstore =
FreezeLock::new(Box::new(CStore::new(compiler.codegen_backend.metadata_loader())) as _);

View file

@ -24,15 +24,6 @@ macro_rules! define_dep_nodes {
($mod:ident) => {[ $($mod::$variant()),* ]};
}
#[macro_export]
macro_rules! make_dep_kind_name_array {
($mod:ident) => {
vec! {
$(*$mod::$variant().name),*
}
};
}
/// This enum serves as an index into arrays built by `make_dep_kind_array`.
// This enum has more than u8::MAX variants so we need some kind of multi-byte
// encoding. The derived Encodable/Decodable uses leb128 encoding which is
@ -68,20 +59,24 @@ macro_rules! define_dep_nodes {
deps.len() as u16
};
/// List containing the name of each dep kind as a static string,
/// indexable by `DepKind`.
pub(crate) const DEP_KIND_NAMES: &[&str] = &[
$( self::label_strs::$variant, )*
];
pub(super) fn dep_kind_from_label_string(label: &str) -> Result<DepKind, ()> {
match label {
$(stringify!($variant) => Ok(dep_kinds::$variant),)*
$( self::label_strs::$variant => Ok(self::dep_kinds::$variant), )*
_ => Err(()),
}
}
/// Contains variant => str representations for constructing
/// DepNode groups for tests.
#[allow(dead_code, non_upper_case_globals)]
#[expect(non_upper_case_globals)]
pub mod label_strs {
$(
pub const $variant: &str = stringify!($variant);
)*
$( pub const $variant: &str = stringify!($variant); )*
}
};
}

View file

@ -20,10 +20,7 @@ pub type DepGraph = rustc_query_system::dep_graph::DepGraph<DepsType>;
pub type DepKindStruct<'tcx> = rustc_query_system::dep_graph::DepKindStruct<TyCtxt<'tcx>>;
#[derive(Clone)]
pub struct DepsType {
pub dep_names: Vec<&'static str>,
}
pub struct DepsType;
impl Deps for DepsType {
fn with_deps<OP, R>(task_deps: TaskDepsRef<'_>, op: OP) -> R
@ -47,8 +44,8 @@ impl Deps for DepsType {
})
}
fn name(&self, dep_kind: DepKind) -> &'static str {
self.dep_names[dep_kind.as_usize()]
fn name(dep_kind: DepKind) -> &'static str {
dep_node::DEP_KIND_NAMES[dep_kind.as_usize()]
}
const DEP_KIND_NULL: DepKind = dep_kinds::Null;

View file

@ -921,9 +921,5 @@ macro_rules! define_queries {
pub fn query_callbacks<'tcx>(arena: &'tcx Arena<'tcx>) -> &'tcx [DepKindStruct<'tcx>] {
arena.alloc_from_iter(rustc_middle::make_dep_kind_array!(query_callbacks))
}
pub fn dep_kind_names() -> Vec<&'static str> {
rustc_middle::make_dep_kind_name_array!(query_callbacks)
}
}
}

View file

@ -28,7 +28,6 @@ use crate::dep_graph::edges::EdgesVec;
use crate::ich::StableHashingContext;
use crate::query::{QueryContext, QuerySideEffect};
#[derive(Clone)]
pub struct DepGraph<D: Deps> {
data: Option<Arc<DepGraphData<D>>>,
@ -39,6 +38,17 @@ pub struct DepGraph<D: Deps> {
virtual_dep_node_index: Arc<AtomicU32>,
}
/// Manual clone impl that does not require `D: Clone`.
impl<D: Deps> Clone for DepGraph<D> {
fn clone(&self) -> Self {
let Self { data, virtual_dep_node_index } = self;
Self {
data: Option::<Arc<_>>::clone(data),
virtual_dep_node_index: Arc::clone(virtual_dep_node_index),
}
}
}
rustc_index::newtype_index! {
pub struct DepNodeIndex {}
}

View file

@ -103,7 +103,7 @@ pub trait Deps: DynSync {
where
OP: for<'a> FnOnce(TaskDepsRef<'a>);
fn name(&self, dep_kind: DepKind) -> &'static str;
fn name(dep_kind: DepKind) -> &'static str;
/// We use this for most things when incr. comp. is turned off.
const DEP_KIND_NULL: DepKind;

View file

@ -191,8 +191,8 @@ fn mask(bits: usize) -> usize {
}
impl SerializedDepGraph {
#[instrument(level = "debug", skip(d, deps))]
pub fn decode<D: Deps>(d: &mut MemDecoder<'_>, deps: &D) -> Arc<SerializedDepGraph> {
#[instrument(level = "debug", skip(d))]
pub fn decode<D: Deps>(d: &mut MemDecoder<'_>) -> Arc<SerializedDepGraph> {
// The last 16 bytes are the node count and edge count.
debug!("position: {:?}", d.position());
@ -280,7 +280,7 @@ impl SerializedDepGraph {
if index[node.kind.as_usize()].insert(node.hash, idx).is_some() {
// Empty nodes and side effect nodes can have duplicates
if node.kind != D::DEP_KIND_NULL && node.kind != D::DEP_KIND_SIDE_EFFECT {
let name = deps.name(node.kind);
let name = D::name(node.kind);
panic!(
"Error: A dep graph node ({name}) does not have an unique index. \
Running a clean build on a nightly compiler with `-Z incremental-verify-ich` \