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

Encode DepKind as u16 #115391

Merged
merged 1 commit into from
Sep 4, 2023
Merged
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
48 changes: 47 additions & 1 deletion compiler/rustc_middle/src/dep_graph/dep_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,56 @@ macro_rules! define_dep_nodes {
}

/// This enum serves as an index into arrays built by `make_dep_kind_array`.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Encodable, Decodable)]
// 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
// 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)]
saethlin marked this conversation as resolved.
Show resolved Hide resolved
#[allow(non_camel_case_types)]
#[repr(u16)]
pub enum DepKind {
$( $( #[$attr] )* $variant),*
}

impl DepKind {
// This const implements two things: A bounds check so that we can decode
// a DepKind from a u16 with just one check, and a const check that the
// discriminants of the variants have been assigned consecutively from 0
// so that just the one comparison suffices to check that the u16 can be
// transmuted to a DepKind.
const VARIANTS: u16 = {
let deps: &[DepKind] = &[$(DepKind::$variant,)*];
let mut i = 0;
while i < deps.len() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for i in 0..deps.len() { will be simpler, no need for mut i, no i += 1.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since #110393 I'm pretty sure the only way to do this is with while or loop

if i as u16 != deps[i] as u16 {
panic!();
}
i += 1;
}
deps.len() as u16
};
}

impl<S: rustc_serialize::Encoder> rustc_serialize::Encodable<S> for DepKind {
#[inline]
fn encode(&self, s: &mut S) {
s.emit_u16(*self as u16);
}
}

impl<D: rustc_serialize::Decoder> rustc_serialize::Decodable<D> for DepKind {
#[inline]
fn decode(d: &mut D) -> DepKind {
let discrim = d.read_u16();
assert!(discrim < DepKind::VARIANTS);
// SAFETY: DepKind::VARIANTS checks that the discriminant values permit
// this one check to soundly guard the transmute.
unsafe {
std::mem::transmute::<u16, DepKind>(discrim)
}
}
}

pub(super) fn dep_kind_from_label_string(label: &str) -> Result<DepKind, ()> {
match label {
$(stringify!($variant) => Ok(DepKind::$variant),)*
Expand Down Expand Up @@ -114,6 +158,8 @@ rustc_query_append!(define_dep_nodes![
[] fn CompileMonoItem() -> (),
]);

static_assert_size!(DepKind, 2);

// 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 {
Expand Down
Loading