Skip to content

Commit 1d7cf0f

Browse files
committed
Replace some u64 hashes with Hash64
1 parent fc147b4 commit 1d7cf0f

File tree

6 files changed

+25
-15
lines changed

6 files changed

+25
-15
lines changed

Diff for: compiler/rustc_abi/src/layout.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::fmt::{self, Write};
22
use std::ops::{Bound, Deref};
33
use std::{cmp, iter};
44

5+
use rustc_data_structures::stable_hasher::Hash64;
56
use rustc_index::Idx;
67
use tracing::debug;
78

@@ -133,7 +134,7 @@ impl<Cx: HasDataLayout> LayoutCalculator<Cx> {
133134
size,
134135
max_repr_align: None,
135136
unadjusted_abi_align: align.abi,
136-
randomization_seed: combined_seed,
137+
randomization_seed: Hash64::new(combined_seed),
137138
}
138139
}
139140

@@ -226,7 +227,7 @@ impl<Cx: HasDataLayout> LayoutCalculator<Cx> {
226227
size: Size::ZERO,
227228
max_repr_align: None,
228229
unadjusted_abi_align: dl.i8_align.abi,
229-
randomization_seed: 0,
230+
randomization_seed: Hash64::ZERO,
230231
}
231232
}
232233

@@ -1058,7 +1059,7 @@ impl<Cx: HasDataLayout> LayoutCalculator<Cx> {
10581059
// unsizable tail fields are excluded so that we use the same seed for the sized and unsized layouts.
10591060
let field_seed = fields_excluding_tail
10601061
.iter()
1061-
.fold(0u64, |acc, f| acc.wrapping_add(f.randomization_seed));
1062+
.fold(Hash64::ZERO, |acc, f| acc.wrapping_add(f.randomization_seed));
10621063

10631064
if optimize_field_order && fields.len() > 1 {
10641065
// If `-Z randomize-layout` was enabled for the type definition we can shuffle
@@ -1072,7 +1073,7 @@ impl<Cx: HasDataLayout> LayoutCalculator<Cx> {
10721073
// `ReprOptions.field_shuffle_seed` is a deterministic seed we can use to randomize field
10731074
// ordering.
10741075
let mut rng = rand_xoshiro::Xoshiro128StarStar::seed_from_u64(
1075-
field_seed.wrapping_add(repr.field_shuffle_seed),
1076+
field_seed.wrapping_add(repr.field_shuffle_seed).as_u64(),
10761077
);
10771078

10781079
// Shuffle the ordering of the fields.

Diff for: compiler/rustc_abi/src/lib.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ use std::ops::{Add, AddAssign, Mul, RangeInclusive, Sub};
4848
use std::str::FromStr;
4949

5050
use bitflags::bitflags;
51+
use rustc_data_structures::stable_hasher::Hash64;
5152
#[cfg(feature = "nightly")]
5253
use rustc_data_structures::stable_hasher::StableOrd;
5354
use rustc_index::{Idx, IndexSlice, IndexVec};
@@ -140,7 +141,7 @@ pub struct ReprOptions {
140141
/// hash without loss, but it does pay the price of being larger.
141142
/// Everything's a tradeoff, a 64-bit seed should be sufficient for our
142143
/// purposes (primarily `-Z randomize-layout`)
143-
pub field_shuffle_seed: u64,
144+
pub field_shuffle_seed: Hash64,
144145
}
145146

146147
impl ReprOptions {
@@ -1727,7 +1728,7 @@ pub struct LayoutData<FieldIdx: Idx, VariantIdx: Idx> {
17271728
/// transmuted to `Foo<U>` we aim to create probalistically distinct seeds so that Foo can choose
17281729
/// to reorder its fields based on that information. The current implementation is a conservative
17291730
/// approximation of this goal.
1730-
pub randomization_seed: u64,
1731+
pub randomization_seed: Hash64,
17311732
}
17321733

17331734
impl<FieldIdx: Idx, VariantIdx: Idx> LayoutData<FieldIdx, VariantIdx> {
@@ -1781,7 +1782,7 @@ impl<FieldIdx: Idx, VariantIdx: Idx> LayoutData<FieldIdx, VariantIdx> {
17811782
align,
17821783
max_repr_align: None,
17831784
unadjusted_abi_align: align.abi,
1784-
randomization_seed,
1785+
randomization_seed: Hash64::new(randomization_seed),
17851786
}
17861787
}
17871788
}

Diff for: compiler/rustc_data_structures/src/hashes.rs

+5
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ impl Hash64 {
3535
pub fn as_u64(self) -> u64 {
3636
self.inner
3737
}
38+
39+
#[inline]
40+
pub fn wrapping_add(self, other: Self) -> Self {
41+
Self { inner: self.inner.wrapping_add(other.inner) }
42+
}
3843
}
3944

4045
impl BitXorAssign<u64> for Hash64 {

Diff for: compiler/rustc_middle/src/ty/layout.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use rustc_abi::{
77
PointeeInfo, PointerKind, Primitive, ReprOptions, Scalar, Size, TagEncoding, TargetDataLayout,
88
TyAbiInterface, VariantIdx, Variants,
99
};
10+
use rustc_data_structures::stable_hasher::Hash64;
1011
use rustc_error_messages::DiagMessage;
1112
use rustc_errors::{
1213
Diag, DiagArgValue, DiagCtxtHandle, Diagnostic, EmissionGuarantee, IntoDiagArg, Level,
@@ -778,7 +779,7 @@ where
778779
size: Size::ZERO,
779780
max_repr_align: None,
780781
unadjusted_abi_align: tcx.data_layout.i8_align.abi,
781-
randomization_seed: 0,
782+
randomization_seed: Hash64::ZERO,
782783
})
783784
}
784785

Diff for: compiler/rustc_middle/src/ty/mod.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1487,8 +1487,7 @@ impl<'tcx> TyCtxt<'tcx> {
14871487

14881488
// Generate a deterministically-derived seed from the item's path hash
14891489
// to allow for cross-crate compilation to actually work
1490-
let mut field_shuffle_seed =
1491-
self.def_path_hash(did.to_def_id()).0.to_smaller_hash().as_u64();
1490+
let mut field_shuffle_seed = self.def_path_hash(did.to_def_id()).0.to_smaller_hash();
14921491

14931492
// If the user defined a custom seed for layout randomization, xor the item's
14941493
// path hash with the user defined seed, this will allowing determinism while

Diff for: compiler/rustc_ty_utils/src/layout.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use rustc_abi::{
99
HasDataLayout, Layout, LayoutCalculatorError, LayoutData, Niche, ReprOptions, Scalar, Size,
1010
StructKind, TagEncoding, VariantIdx, Variants, WrappingRange,
1111
};
12+
use rustc_data_structures::stable_hasher::Hash64;
1213
use rustc_index::bit_set::DenseBitSet;
1314
use rustc_index::{IndexSlice, IndexVec};
1415
use rustc_middle::bug;
@@ -380,7 +381,7 @@ fn layout_of_uncached<'tcx>(
380381
size,
381382
max_repr_align: None,
382383
unadjusted_abi_align: element.align.abi,
383-
randomization_seed: element.randomization_seed.wrapping_add(count),
384+
randomization_seed: element.randomization_seed.wrapping_add(Hash64::new(count)),
384385
})
385386
}
386387
ty::Slice(element) => {
@@ -395,7 +396,9 @@ fn layout_of_uncached<'tcx>(
395396
max_repr_align: None,
396397
unadjusted_abi_align: element.align.abi,
397398
// adding a randomly chosen value to distinguish slices
398-
randomization_seed: element.randomization_seed.wrapping_add(0x2dcba99c39784102),
399+
randomization_seed: element
400+
.randomization_seed
401+
.wrapping_add(Hash64::new(0x2dcba99c39784102)),
399402
})
400403
}
401404
ty::Str => tcx.mk_layout(LayoutData {
@@ -408,7 +411,7 @@ fn layout_of_uncached<'tcx>(
408411
max_repr_align: None,
409412
unadjusted_abi_align: dl.i8_align.abi,
410413
// another random value
411-
randomization_seed: 0xc1325f37d127be22,
414+
randomization_seed: Hash64::new(0xc1325f37d127be22),
412415
}),
413416

414417
// Odd unit types.
@@ -585,7 +588,7 @@ fn layout_of_uncached<'tcx>(
585588
align,
586589
max_repr_align: None,
587590
unadjusted_abi_align: align.abi,
588-
randomization_seed: e_ly.randomization_seed.wrapping_add(e_len),
591+
randomization_seed: e_ly.randomization_seed.wrapping_add(Hash64::new(e_len)),
589592
})
590593
}
591594

@@ -1051,7 +1054,7 @@ fn coroutine_layout<'tcx>(
10511054
};
10521055

10531056
// this is similar to how ReprOptions populates its field_shuffle_seed
1054-
let def_hash = tcx.def_path_hash(def_id).0.to_smaller_hash().as_u64();
1057+
let def_hash = tcx.def_path_hash(def_id).0.to_smaller_hash();
10551058

10561059
let layout = tcx.mk_layout(LayoutData {
10571060
variants: Variants::Multiple {

0 commit comments

Comments
 (0)