Skip to content

Commit

Permalink
Make the randomize feature of rustc_abi additive
Browse files Browse the repository at this point in the history
  • Loading branch information
HKalbasi committed Nov 5, 2023
1 parent a42d94e commit 850d1a5
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
20 changes: 19 additions & 1 deletion compiler/rustc_abi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ pub struct ReprOptions {
pub int: Option<IntegerType>,
pub align: Option<Align>,
pub pack: Option<Align>,
pub flags: ReprFlags,
flags: ReprFlags,
#[cfg(feature = "randomize")]
/// The seed to be used for randomizing a type's layout
///
Expand All @@ -88,6 +88,24 @@ pub struct ReprOptions {
}

impl ReprOptions {
pub fn new(int: Option<IntegerType>, align: Option<Align>, pack: Option<Align>) -> Self {
Self { int, align, pack, ..ReprOptions::default() }
}

pub fn with_flags(mut self, flags: ReprFlags) -> Self {
self.flags = flags;
self
}

#[cfg(feature = "randomize")]
pub fn with_shuffle_seed(
mut self,
field_shuffle_seed: rustc_data_structures::stable_hasher::Hash64,
) -> Self {
self.field_shuffle_seed = field_shuffle_seed;
self
}

#[inline]
pub fn simd(&self) -> bool {
self.flags.contains(ReprFlags::IS_SIMD)
Expand Down
4 changes: 3 additions & 1 deletion compiler/rustc_middle/src/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2076,7 +2076,9 @@ impl<'tcx> TyCtxt<'tcx> {
flags.insert(ReprFlags::IS_LINEAR);
}

ReprOptions { int: size, align: max_align, pack: min_pack, flags, field_shuffle_seed }
ReprOptions::new(size, max_align, min_pack)
.with_flags(flags)
.with_shuffle_seed(field_shuffle_seed)
}

/// Look up the name of a definition across crates. This does not look at HIR.
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_mir_transform/src/sroa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use rustc_middle::mir::visit::*;
use rustc_middle::mir::*;
use rustc_middle::ty::{self, Ty, TyCtxt};
use rustc_mir_dataflow::value_analysis::{excluded_locals, iter_fields};
use rustc_target::abi::{FieldIdx, ReprFlags, FIRST_VARIANT};
use rustc_target::abi::{FieldIdx, FIRST_VARIANT};

pub struct ScalarReplacementOfAggregates;

Expand Down Expand Up @@ -66,7 +66,7 @@ fn escaping_locals<'tcx>(
return true;
}
if let ty::Adt(def, _args) = ty.kind() {
if def.repr().flags.contains(ReprFlags::IS_SIMD) {
if def.repr().simd() {
// Exclude #[repr(simd)] types so that they are not de-optimized into an array
return true;
}
Expand Down

0 comments on commit 850d1a5

Please sign in to comment.