Skip to content
Open
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
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_ssa/src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -911,7 +911,7 @@ impl CrateInfo {
.rev()
.copied()
.filter(|&cnum| {
let link = !tcx.dep_kind(cnum).macros_only();
let link = !tcx.crate_dep_kind(cnum).macros_only();
if link && tcx.is_compiler_builtins(cnum) {
compiler_builtins = Some(cnum);
return false;
Expand Down
6 changes: 2 additions & 4 deletions compiler/rustc_incremental/src/assert_dep_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@ use rustc_hir::attrs::AttributeKind;
use rustc_hir::def_id::{CRATE_DEF_ID, DefId, LocalDefId};
use rustc_hir::intravisit::{self, Visitor};
use rustc_middle::bug;
use rustc_middle::dep_graph::{
DepGraphQuery, DepKind, DepNode, DepNodeFilter, EdgeFilter, dep_kinds,
};
use rustc_middle::dep_graph::{DepGraphQuery, DepKind, DepNode, DepNodeFilter, EdgeFilter};
use rustc_middle::hir::nested_filter;
use rustc_middle::ty::TyCtxt;
use rustc_span::{Span, Symbol, sym};
Expand Down Expand Up @@ -117,7 +115,7 @@ impl<'tcx> IfThisChanged<'tcx> {
None => DepNode::from_def_path_hash(
self.tcx,
def_path_hash,
dep_kinds::opt_hir_owner_nodes,
DepKind::opt_hir_owner_nodes,
),
Some(n) => {
match DepNode::from_label_string(self.tcx, n.as_str(), def_path_hash) {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_incremental/src/persist/clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
//! - `#[rustc_clean(cfg="rev2")]` same as above, except that the
//! fingerprints must be the SAME (along with all other fingerprints).
//!
//! - `#[rustc_clean(cfg="rev2", loaded_from_disk='typeck")]` asserts that
//! - `#[rustc_clean(cfg="rev2", loaded_from_disk="typeck")]` asserts that
//! the query result for `DepNode::typeck(X)` was actually
//! loaded from disk (not just marked green). This can be useful
//! to ensure that a test is actually exercising the deserialization
Expand Down
10 changes: 5 additions & 5 deletions compiler/rustc_metadata/src/dependency_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ fn calculate_type(tcx: TyCtxt<'_>, ty: CrateType) -> DependencyList {
&& !sess.target.crt_static_allows_dylibs)
{
for &cnum in tcx.crates(()).iter() {
if tcx.dep_kind(cnum).macros_only() {
if tcx.crate_dep_kind(cnum).macros_only() {
continue;
}
let src = tcx.used_crate_source(cnum);
Expand All @@ -163,7 +163,7 @@ fn calculate_type(tcx: TyCtxt<'_>, ty: CrateType) -> DependencyList {

let all_dylibs = || {
tcx.crates(()).iter().filter(|&&cnum| {
!tcx.dep_kind(cnum).macros_only()
!tcx.crate_dep_kind(cnum).macros_only()
&& (tcx.used_crate_source(cnum).dylib.is_some()
|| tcx.used_crate_source(cnum).sdylib_interface.is_some())
})
Expand Down Expand Up @@ -241,7 +241,7 @@ fn calculate_type(tcx: TyCtxt<'_>, ty: CrateType) -> DependencyList {
let src = tcx.used_crate_source(cnum);
if src.dylib.is_none()
&& !formats.contains_key(&cnum)
&& tcx.dep_kind(cnum) == CrateDepKind::Unconditional
&& tcx.crate_dep_kind(cnum) == CrateDepKind::Unconditional
{
assert!(src.rlib.is_some() || src.rmeta.is_some());
info!("adding staticlib: {}", tcx.crate_name(cnum));
Expand Down Expand Up @@ -333,7 +333,7 @@ fn attempt_static(tcx: TyCtxt<'_>, unavailable: &mut Vec<CrateNum>) -> Option<De
.iter()
.copied()
.filter_map(|cnum| {
if tcx.dep_kind(cnum).macros_only() {
if tcx.crate_dep_kind(cnum).macros_only() {
return None;
}
let is_rlib = tcx.used_crate_source(cnum).rlib.is_some();
Expand All @@ -353,7 +353,7 @@ fn attempt_static(tcx: TyCtxt<'_>, unavailable: &mut Vec<CrateNum>) -> Option<De
assert_eq!(ret.push(Linkage::Static), LOCAL_CRATE);
for &cnum in tcx.crates(()) {
assert_eq!(
ret.push(match tcx.dep_kind(cnum) {
ret.push(match tcx.crate_dep_kind(cnum) {
CrateDepKind::Unconditional => Linkage::Static,
CrateDepKind::MacrosOnly | CrateDepKind::Conditional => Linkage::NotLinked,
}),
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ macro_rules! provide_one {
// External query providers call `crate_hash` in order to register a dependency
// on the crate metadata. The exception is `crate_hash` itself, which obviously
// doesn't need to do this (and can't, as it would cause a query cycle).
use rustc_middle::dep_graph::dep_kinds;
if dep_kinds::$name != dep_kinds::crate_hash && $tcx.dep_graph.is_fully_enabled() {
use rustc_middle::dep_graph::DepKind;
if DepKind::$name != DepKind::crate_hash && $tcx.dep_graph.is_fully_enabled() {
$tcx.ensure_ok().crate_hash($def_id.krate);
}

Expand Down Expand Up @@ -382,7 +382,7 @@ provide! { tcx, def_id, other, cdata,
implementations_of_trait => { cdata.get_implementations_of_trait(tcx, other) }
crate_incoherent_impls => { cdata.get_incoherent_impls(tcx, other) }

dep_kind => { cdata.dep_kind }
crate_dep_kind => { cdata.dep_kind }
module_children => {
tcx.arena.alloc_from_iter(cdata.get_module_children(tcx, def_id.index))
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_metadata/src/rmeta/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2072,7 +2072,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
name: self.tcx.crate_name(cnum),
hash: self.tcx.crate_hash(cnum),
host_hash: self.tcx.crate_host_hash(cnum),
kind: self.tcx.dep_kind(cnum),
kind: self.tcx.crate_dep_kind(cnum),
extra_filename: self.tcx.extra_filename(cnum).clone(),
is_private: self.tcx.is_private_dep(cnum),
};
Expand Down
95 changes: 26 additions & 69 deletions compiler/rustc_middle/src/dep_graph/dep_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,59 +62,30 @@ use crate::ich::StableHashingContext;
use crate::mir::mono::MonoItem;
use crate::ty::{TyCtxt, tls};

/// This serves as an index into arrays built by `make_dep_kind_array`.
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct DepKind {
variant: u16,
}

// `enum DepKind` is generated by `define_dep_nodes!` below.
impl DepKind {
#[inline]
pub const fn new(variant: u16) -> Self {
Self { variant }
pub(crate) fn from_u16(u: u16) -> Self {
if u > Self::MAX {
panic!("Invalid DepKind {u}");
}
// SAFETY: See comment on DEP_KIND_NUM_VARIANTS
unsafe { std::mem::transmute(u) }
}

#[inline]
pub const fn as_inner(&self) -> u16 {
self.variant
pub(crate) const fn as_u16(&self) -> u16 {
*self as u16
}

#[inline]
pub const fn as_usize(&self) -> usize {
self.variant as usize
*self as usize
}

pub(crate) fn name(self) -> &'static str {
DEP_KIND_NAMES[self.as_usize()]
}

/// We use this for most things when incr. comp. is turned off.
pub(crate) const NULL: DepKind = dep_kinds::Null;

/// We use this to create a forever-red node.
pub(crate) const RED: DepKind = dep_kinds::Red;

/// We use this to create a side effect node.
pub(crate) const SIDE_EFFECT: DepKind = dep_kinds::SideEffect;

/// We use this to create the anon node with zero dependencies.
pub(crate) const ANON_ZERO_DEPS: DepKind = dep_kinds::AnonZeroDeps;

/// This is the highest value a `DepKind` can have. It's used during encoding to
/// pack information into the unused bits.
pub(crate) const MAX: u16 = DEP_KIND_VARIANTS - 1;
}

impl fmt::Debug for DepKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
tls::with_opt(|opt_tcx| {
if let Some(tcx) = opt_tcx {
write!(f, "{}", tcx.dep_kind_vtable(*self).name)
} else {
f.debug_struct("DepKind").field("variant", &self.variant).finish()
}
})
}
pub(crate) const MAX: u16 = DEP_KIND_NUM_VARIANTS - 1;
}

/// Combination of a [`DepKind`] and a key fingerprint that uniquely identifies
Expand Down Expand Up @@ -312,9 +283,6 @@ pub struct DepKindVTable<'tcx> {

/// Invoke a query to put the on-disk cached value in memory.
pub try_load_from_on_disk_cache: Option<fn(TyCtxt<'tcx>, DepNode)>,

/// The name of this dep kind.
pub name: &'static &'static str,
}

/// A "work product" corresponds to a `.o` (or other) file that we
Expand Down Expand Up @@ -374,45 +342,32 @@ macro_rules! define_dep_nodes {
// encoding. The derived Encodable/Decodable uses leb128 encoding which is
// dense when only considering this enum. But DepKind is encoded in a larger
// struct, and there we can take advantage of the unused bits in the u16.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[allow(non_camel_case_types)]
#[repr(u16)] // Must be kept in sync with the inner type of `DepKind`.
enum DepKindDefs {
#[repr(u16)] // Must be kept in sync with the rest of `DepKind`.
pub enum DepKind {
$( $( #[$attr] )* $variant),*
}

#[allow(non_upper_case_globals)]
pub mod dep_kinds {
use super::*;

$(
// The `as u16` cast must be kept in sync with the inner type of `DepKind`.
pub const $variant: DepKind = DepKind::new(DepKindDefs::$variant as u16);
)*
}

// This checks that the discriminants of the variants have been assigned consecutively
// from 0 so that they can be used as a dense index.
pub(crate) const DEP_KIND_VARIANTS: u16 = {
let deps = &[$(dep_kinds::$variant,)*];
// This computes the number of dep kind variants. Along the way, it sanity-checks that the
// discriminants of the variants have been assigned consecutively from 0 so that they can
// be used as a dense index, and that all discriminants fit in a `u16`.
pub(crate) const DEP_KIND_NUM_VARIANTS: u16 = {
let deps = &[$(DepKind::$variant,)*];
let mut i = 0;
while i < deps.len() {
if i != deps[i].as_usize() {
panic!();
}
i += 1;
}
assert!(deps.len() <= u16::MAX as usize);
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 {
$( self::label_strs::$variant => Ok(self::dep_kinds::$variant), )*
$( stringify!($variant) => Ok(self::DepKind::$variant), )*
_ => Err(()),
}
}
Expand All @@ -433,7 +388,9 @@ rustc_with_all_queries!(define_dep_nodes![
[] fn Null() -> (),
/// We use this to create a forever-red node.
[] fn Red() -> (),
/// We use this to create a side effect node.
[] fn SideEffect() -> (),
/// We use this to create the anon node with zero dependencies.
[] fn AnonZeroDeps() -> (),
[] fn TraitSelect() -> (),
[] fn CompileCodegenUnit() -> (),
Expand All @@ -444,7 +401,7 @@ rustc_with_all_queries!(define_dep_nodes![
// WARNING: `construct` is generic and does not know that `CompileCodegenUnit` takes `Symbol`s as keys.
// Be very careful changing this type signature!
pub(crate) fn make_compile_codegen_unit(tcx: TyCtxt<'_>, name: Symbol) -> DepNode {
DepNode::construct(tcx, dep_kinds::CompileCodegenUnit, &name)
DepNode::construct(tcx, DepKind::CompileCodegenUnit, &name)
}

// WARNING: `construct` is generic and does not know that `CompileMonoItem` takes `MonoItem`s as keys.
Expand All @@ -453,13 +410,13 @@ pub(crate) fn make_compile_mono_item<'tcx>(
tcx: TyCtxt<'tcx>,
mono_item: &MonoItem<'tcx>,
) -> DepNode {
DepNode::construct(tcx, dep_kinds::CompileMonoItem, mono_item)
DepNode::construct(tcx, DepKind::CompileMonoItem, mono_item)
}

// WARNING: `construct` is generic and does not know that `Metadata` takes `()`s as keys.
// Be very careful changing this type signature!
pub(crate) fn make_metadata(tcx: TyCtxt<'_>) -> DepNode {
DepNode::construct(tcx, dep_kinds::Metadata, &())
DepNode::construct(tcx, DepKind::Metadata, &())
}

impl DepNode {
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_middle/src/dep_graph/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ impl DepGraph {

// Instantiate a node with zero dependencies only once for anonymous queries.
let _green_node_index = current.alloc_new_node(
DepNode { kind: DepKind::ANON_ZERO_DEPS, key_fingerprint: current.anon_id_seed.into() },
DepNode { kind: DepKind::AnonZeroDeps, key_fingerprint: current.anon_id_seed.into() },
EdgesVec::new(),
Fingerprint::ZERO,
);
Expand All @@ -152,7 +152,7 @@ impl DepGraph {
// Other nodes can use the always-red node as a fake dependency, to
// ensure that their dependency list will never be all-green.
let red_node_index = current.alloc_new_node(
DepNode { kind: DepKind::RED, key_fingerprint: Fingerprint::ZERO.into() },
DepNode { kind: DepKind::Red, key_fingerprint: Fingerprint::ZERO.into() },
EdgesVec::new(),
Fingerprint::ZERO,
);
Expand Down Expand Up @@ -680,7 +680,7 @@ impl DepGraphData {
// Use `send_new` so we get an unique index, even though the dep node is not.
let dep_node_index = self.current.encoder.send_new(
DepNode {
kind: DepKind::SIDE_EFFECT,
kind: DepKind::SideEffect,
key_fingerprint: PackedFingerprint::from(Fingerprint::ZERO),
},
Fingerprint::ZERO,
Expand Down Expand Up @@ -713,7 +713,7 @@ impl DepGraphData {
prev_index,
&self.colors,
DepNode {
kind: DepKind::SIDE_EFFECT,
kind: DepKind::SideEffect,
key_fingerprint: PackedFingerprint::from(Fingerprint::ZERO),
},
Fingerprint::ZERO,
Expand Down
5 changes: 2 additions & 3 deletions compiler/rustc_middle/src/dep_graph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ use std::panic;
use tracing::instrument;

pub use self::dep_node::{
DepKind, DepKindVTable, DepNode, DepNodeKey, WorkProductId, dep_kind_from_label, dep_kinds,
label_strs,
DepKind, DepKindVTable, DepNode, DepNodeKey, WorkProductId, dep_kind_from_label, label_strs,
};
pub use self::graph::{
DepGraph, DepGraphData, DepNodeIndex, QuerySideEffect, TaskDepsRef, WorkProduct,
Expand All @@ -18,7 +17,7 @@ use crate::ty::print::with_reduced_queries;
use crate::ty::{self, TyCtxt};

mod debug;
pub mod dep_node;
pub(crate) mod dep_node;
mod dep_node_key;
mod edges;
mod graph;
Expand Down
Loading
Loading