Replace some u64 hashes with Hash64

This commit is contained in:
Ben Kimock 2025-02-15 13:57:21 -05:00
parent fc147b4a81
commit 1d7cf0ff40
6 changed files with 25 additions and 15 deletions

View File

@ -2,6 +2,7 @@ use std::fmt::{self, Write};
use std::ops::{Bound, Deref}; use std::ops::{Bound, Deref};
use std::{cmp, iter}; use std::{cmp, iter};
use rustc_data_structures::stable_hasher::Hash64;
use rustc_index::Idx; use rustc_index::Idx;
use tracing::debug; use tracing::debug;
@ -133,7 +134,7 @@ impl<Cx: HasDataLayout> LayoutCalculator<Cx> {
size, size,
max_repr_align: None, max_repr_align: None,
unadjusted_abi_align: align.abi, unadjusted_abi_align: align.abi,
randomization_seed: combined_seed, randomization_seed: Hash64::new(combined_seed),
} }
} }
@ -226,7 +227,7 @@ impl<Cx: HasDataLayout> LayoutCalculator<Cx> {
size: Size::ZERO, size: Size::ZERO,
max_repr_align: None, max_repr_align: None,
unadjusted_abi_align: dl.i8_align.abi, unadjusted_abi_align: dl.i8_align.abi,
randomization_seed: 0, randomization_seed: Hash64::ZERO,
} }
} }
@ -1058,7 +1059,7 @@ impl<Cx: HasDataLayout> LayoutCalculator<Cx> {
// unsizable tail fields are excluded so that we use the same seed for the sized and unsized layouts. // unsizable tail fields are excluded so that we use the same seed for the sized and unsized layouts.
let field_seed = fields_excluding_tail let field_seed = fields_excluding_tail
.iter() .iter()
.fold(0u64, |acc, f| acc.wrapping_add(f.randomization_seed)); .fold(Hash64::ZERO, |acc, f| acc.wrapping_add(f.randomization_seed));
if optimize_field_order && fields.len() > 1 { if optimize_field_order && fields.len() > 1 {
// If `-Z randomize-layout` was enabled for the type definition we can shuffle // If `-Z randomize-layout` was enabled for the type definition we can shuffle
@ -1072,7 +1073,7 @@ impl<Cx: HasDataLayout> LayoutCalculator<Cx> {
// `ReprOptions.field_shuffle_seed` is a deterministic seed we can use to randomize field // `ReprOptions.field_shuffle_seed` is a deterministic seed we can use to randomize field
// ordering. // ordering.
let mut rng = rand_xoshiro::Xoshiro128StarStar::seed_from_u64( let mut rng = rand_xoshiro::Xoshiro128StarStar::seed_from_u64(
field_seed.wrapping_add(repr.field_shuffle_seed), field_seed.wrapping_add(repr.field_shuffle_seed).as_u64(),
); );
// Shuffle the ordering of the fields. // Shuffle the ordering of the fields.

View File

@ -48,6 +48,7 @@ use std::ops::{Add, AddAssign, Mul, RangeInclusive, Sub};
use std::str::FromStr; use std::str::FromStr;
use bitflags::bitflags; use bitflags::bitflags;
use rustc_data_structures::stable_hasher::Hash64;
#[cfg(feature = "nightly")] #[cfg(feature = "nightly")]
use rustc_data_structures::stable_hasher::StableOrd; use rustc_data_structures::stable_hasher::StableOrd;
use rustc_index::{Idx, IndexSlice, IndexVec}; use rustc_index::{Idx, IndexSlice, IndexVec};
@ -140,7 +141,7 @@ pub struct ReprOptions {
/// hash without loss, but it does pay the price of being larger. /// hash without loss, but it does pay the price of being larger.
/// Everything's a tradeoff, a 64-bit seed should be sufficient for our /// Everything's a tradeoff, a 64-bit seed should be sufficient for our
/// purposes (primarily `-Z randomize-layout`) /// purposes (primarily `-Z randomize-layout`)
pub field_shuffle_seed: u64, pub field_shuffle_seed: Hash64,
} }
impl ReprOptions { impl ReprOptions {
@ -1727,7 +1728,7 @@ pub struct LayoutData<FieldIdx: Idx, VariantIdx: Idx> {
/// transmuted to `Foo<U>` we aim to create probalistically distinct seeds so that Foo can choose /// transmuted to `Foo<U>` we aim to create probalistically distinct seeds so that Foo can choose
/// to reorder its fields based on that information. The current implementation is a conservative /// to reorder its fields based on that information. The current implementation is a conservative
/// approximation of this goal. /// approximation of this goal.
pub randomization_seed: u64, pub randomization_seed: Hash64,
} }
impl<FieldIdx: Idx, VariantIdx: Idx> LayoutData<FieldIdx, VariantIdx> { impl<FieldIdx: Idx, VariantIdx: Idx> LayoutData<FieldIdx, VariantIdx> {
@ -1781,7 +1782,7 @@ impl<FieldIdx: Idx, VariantIdx: Idx> LayoutData<FieldIdx, VariantIdx> {
align, align,
max_repr_align: None, max_repr_align: None,
unadjusted_abi_align: align.abi, unadjusted_abi_align: align.abi,
randomization_seed, randomization_seed: Hash64::new(randomization_seed),
} }
} }
} }

View File

@ -35,6 +35,11 @@ impl Hash64 {
pub fn as_u64(self) -> u64 { pub fn as_u64(self) -> u64 {
self.inner self.inner
} }
#[inline]
pub fn wrapping_add(self, other: Self) -> Self {
Self { inner: self.inner.wrapping_add(other.inner) }
}
} }
impl BitXorAssign<u64> for Hash64 { impl BitXorAssign<u64> for Hash64 {

View File

@ -7,6 +7,7 @@ use rustc_abi::{
PointeeInfo, PointerKind, Primitive, ReprOptions, Scalar, Size, TagEncoding, TargetDataLayout, PointeeInfo, PointerKind, Primitive, ReprOptions, Scalar, Size, TagEncoding, TargetDataLayout,
TyAbiInterface, VariantIdx, Variants, TyAbiInterface, VariantIdx, Variants,
}; };
use rustc_data_structures::stable_hasher::Hash64;
use rustc_error_messages::DiagMessage; use rustc_error_messages::DiagMessage;
use rustc_errors::{ use rustc_errors::{
Diag, DiagArgValue, DiagCtxtHandle, Diagnostic, EmissionGuarantee, IntoDiagArg, Level, Diag, DiagArgValue, DiagCtxtHandle, Diagnostic, EmissionGuarantee, IntoDiagArg, Level,
@ -778,7 +779,7 @@ where
size: Size::ZERO, size: Size::ZERO,
max_repr_align: None, max_repr_align: None,
unadjusted_abi_align: tcx.data_layout.i8_align.abi, unadjusted_abi_align: tcx.data_layout.i8_align.abi,
randomization_seed: 0, randomization_seed: Hash64::ZERO,
}) })
} }

View File

@ -1487,8 +1487,7 @@ impl<'tcx> TyCtxt<'tcx> {
// Generate a deterministically-derived seed from the item's path hash // Generate a deterministically-derived seed from the item's path hash
// to allow for cross-crate compilation to actually work // to allow for cross-crate compilation to actually work
let mut field_shuffle_seed = let mut field_shuffle_seed = self.def_path_hash(did.to_def_id()).0.to_smaller_hash();
self.def_path_hash(did.to_def_id()).0.to_smaller_hash().as_u64();
// If the user defined a custom seed for layout randomization, xor the item's // If the user defined a custom seed for layout randomization, xor the item's
// path hash with the user defined seed, this will allowing determinism while // path hash with the user defined seed, this will allowing determinism while

View File

@ -9,6 +9,7 @@ use rustc_abi::{
HasDataLayout, Layout, LayoutCalculatorError, LayoutData, Niche, ReprOptions, Scalar, Size, HasDataLayout, Layout, LayoutCalculatorError, LayoutData, Niche, ReprOptions, Scalar, Size,
StructKind, TagEncoding, VariantIdx, Variants, WrappingRange, StructKind, TagEncoding, VariantIdx, Variants, WrappingRange,
}; };
use rustc_data_structures::stable_hasher::Hash64;
use rustc_index::bit_set::DenseBitSet; use rustc_index::bit_set::DenseBitSet;
use rustc_index::{IndexSlice, IndexVec}; use rustc_index::{IndexSlice, IndexVec};
use rustc_middle::bug; use rustc_middle::bug;
@ -380,7 +381,7 @@ fn layout_of_uncached<'tcx>(
size, size,
max_repr_align: None, max_repr_align: None,
unadjusted_abi_align: element.align.abi, unadjusted_abi_align: element.align.abi,
randomization_seed: element.randomization_seed.wrapping_add(count), randomization_seed: element.randomization_seed.wrapping_add(Hash64::new(count)),
}) })
} }
ty::Slice(element) => { ty::Slice(element) => {
@ -395,7 +396,9 @@ fn layout_of_uncached<'tcx>(
max_repr_align: None, max_repr_align: None,
unadjusted_abi_align: element.align.abi, unadjusted_abi_align: element.align.abi,
// adding a randomly chosen value to distinguish slices // adding a randomly chosen value to distinguish slices
randomization_seed: element.randomization_seed.wrapping_add(0x2dcba99c39784102), randomization_seed: element
.randomization_seed
.wrapping_add(Hash64::new(0x2dcba99c39784102)),
}) })
} }
ty::Str => tcx.mk_layout(LayoutData { ty::Str => tcx.mk_layout(LayoutData {
@ -408,7 +411,7 @@ fn layout_of_uncached<'tcx>(
max_repr_align: None, max_repr_align: None,
unadjusted_abi_align: dl.i8_align.abi, unadjusted_abi_align: dl.i8_align.abi,
// another random value // another random value
randomization_seed: 0xc1325f37d127be22, randomization_seed: Hash64::new(0xc1325f37d127be22),
}), }),
// Odd unit types. // Odd unit types.
@ -585,7 +588,7 @@ fn layout_of_uncached<'tcx>(
align, align,
max_repr_align: None, max_repr_align: None,
unadjusted_abi_align: align.abi, unadjusted_abi_align: align.abi,
randomization_seed: e_ly.randomization_seed.wrapping_add(e_len), randomization_seed: e_ly.randomization_seed.wrapping_add(Hash64::new(e_len)),
}) })
} }
@ -1051,7 +1054,7 @@ fn coroutine_layout<'tcx>(
}; };
// this is similar to how ReprOptions populates its field_shuffle_seed // this is similar to how ReprOptions populates its field_shuffle_seed
let def_hash = tcx.def_path_hash(def_id).0.to_smaller_hash().as_u64(); let def_hash = tcx.def_path_hash(def_id).0.to_smaller_hash();
let layout = tcx.mk_layout(LayoutData { let layout = tcx.mk_layout(LayoutData {
variants: Variants::Multiple { variants: Variants::Multiple {