Unglob rustc_abi imports

This commit is contained in:
Lukas Wirth 2023-09-29 16:19:43 +02:00
parent 5ead7452e5
commit 3b99d73f5a
2 changed files with 24 additions and 12 deletions

View File

@ -1,14 +1,13 @@
use super::*;
use std::fmt::Write; use std::fmt::Write;
use std::{borrow::Borrow, cmp, iter, ops::Bound}; use std::{borrow::Borrow, cmp, iter, ops::Bound};
#[cfg(feature = "randomize")]
use rand::{seq::SliceRandom, SeedableRng};
#[cfg(feature = "randomize")]
use rand_xoshiro::Xoshiro128StarStar;
use tracing::debug; use tracing::debug;
use crate::{
Abi, AbiAndPrefAlign, Align, FieldIdx, FieldsShape, IndexSlice, IndexVec, Integer, Layout,
LayoutS, Niche, NonZeroUsize, Primitive, ReprOptions, Scalar, Size, StructKind, TagEncoding,
TargetDataLayout, VariantIdx, Variants, WrappingRange, FIRST_VARIANT,
};
pub trait LayoutCalculator { pub trait LayoutCalculator {
type TargetDataLayoutRef: Borrow<TargetDataLayout>; type TargetDataLayoutRef: Borrow<TargetDataLayout>;
@ -587,7 +586,7 @@ struct TmpLayout {
let tag_mask = ity.size().unsigned_int_max(); let tag_mask = ity.size().unsigned_int_max();
let tag = Scalar::Initialized { let tag = Scalar::Initialized {
value: Int(ity, signed), value: Primitive::Int(ity, signed),
valid_range: WrappingRange { valid_range: WrappingRange {
start: (min as u128 & tag_mask), start: (min as u128 & tag_mask),
end: (max as u128 & tag_mask), end: (max as u128 & tag_mask),
@ -873,9 +872,12 @@ fn univariant(
if repr.can_randomize_type_layout() && cfg!(feature = "randomize") { if repr.can_randomize_type_layout() && cfg!(feature = "randomize") {
#[cfg(feature = "randomize")] #[cfg(feature = "randomize")]
{ {
use rand::{seq::SliceRandom, SeedableRng};
// `ReprOptions.layout_seed` is a deterministic seed we can use to randomize field // `ReprOptions.layout_seed` is a deterministic seed we can use to randomize field
// ordering. // ordering.
let mut rng = Xoshiro128StarStar::seed_from_u64(repr.field_shuffle_seed.as_u64()); let mut rng = rand_xoshiro::Xoshiro128StarStar::seed_from_u64(
repr.field_shuffle_seed.as_u64(),
);
// Shuffle the ordering of the fields. // Shuffle the ordering of the fields.
optimizing.shuffle(&mut rng); optimizing.shuffle(&mut rng);

View File

@ -1,3 +1,5 @@
// We want to be able to build this crate with a stable compiler, so no
// `#![feature]` attributes should be added.
#![cfg_attr(feature = "nightly", feature(step_trait, rustc_attrs, min_specialization))] #![cfg_attr(feature = "nightly", feature(step_trait, rustc_attrs, min_specialization))]
#![cfg_attr(feature = "nightly", allow(internal_features))] #![cfg_attr(feature = "nightly", allow(internal_features))]
@ -28,9 +30,6 @@
/// instead of implementing everything in `rustc_middle`. /// instead of implementing everything in `rustc_middle`.
pub trait HashStableContext {} pub trait HashStableContext {}
use Integer::*;
use Primitive::*;
bitflags! { bitflags! {
#[derive(Default)] #[derive(Default)]
#[cfg_attr(feature = "nightly", derive(Encodable, Decodable, HashStable_Generic))] #[cfg_attr(feature = "nightly", derive(Encodable, Decodable, HashStable_Generic))]
@ -342,6 +341,7 @@ pub fn obj_size_bound(&self) -> u64 {
#[inline] #[inline]
pub fn ptr_sized_integer(&self) -> Integer { pub fn ptr_sized_integer(&self) -> Integer {
use Integer::*;
match self.pointer_size.bits() { match self.pointer_size.bits() {
16 => I16, 16 => I16,
32 => I32, 32 => I32,
@ -786,6 +786,7 @@ pub enum Integer {
impl Integer { impl Integer {
#[inline] #[inline]
pub fn size(self) -> Size { pub fn size(self) -> Size {
use Integer::*;
match self { match self {
I8 => Size::from_bytes(1), I8 => Size::from_bytes(1),
I16 => Size::from_bytes(2), I16 => Size::from_bytes(2),
@ -806,6 +807,7 @@ pub fn from_attr<C: HasDataLayout>(cx: &C, ity: IntegerType) -> Integer {
} }
pub fn align<C: HasDataLayout>(self, cx: &C) -> AbiAndPrefAlign { pub fn align<C: HasDataLayout>(self, cx: &C) -> AbiAndPrefAlign {
use Integer::*;
let dl = cx.data_layout(); let dl = cx.data_layout();
match self { match self {
@ -820,6 +822,7 @@ pub fn align<C: HasDataLayout>(self, cx: &C) -> AbiAndPrefAlign {
/// Returns the largest signed value that can be represented by this Integer. /// Returns the largest signed value that can be represented by this Integer.
#[inline] #[inline]
pub fn signed_max(self) -> i128 { pub fn signed_max(self) -> i128 {
use Integer::*;
match self { match self {
I8 => i8::MAX as i128, I8 => i8::MAX as i128,
I16 => i16::MAX as i128, I16 => i16::MAX as i128,
@ -832,6 +835,7 @@ pub fn signed_max(self) -> i128 {
/// Finds the smallest Integer type which can represent the signed value. /// Finds the smallest Integer type which can represent the signed value.
#[inline] #[inline]
pub fn fit_signed(x: i128) -> Integer { pub fn fit_signed(x: i128) -> Integer {
use Integer::*;
match x { match x {
-0x0000_0000_0000_0080..=0x0000_0000_0000_007f => I8, -0x0000_0000_0000_0080..=0x0000_0000_0000_007f => I8,
-0x0000_0000_0000_8000..=0x0000_0000_0000_7fff => I16, -0x0000_0000_0000_8000..=0x0000_0000_0000_7fff => I16,
@ -844,6 +848,7 @@ pub fn fit_signed(x: i128) -> Integer {
/// Finds the smallest Integer type which can represent the unsigned value. /// Finds the smallest Integer type which can represent the unsigned value.
#[inline] #[inline]
pub fn fit_unsigned(x: u128) -> Integer { pub fn fit_unsigned(x: u128) -> Integer {
use Integer::*;
match x { match x {
0..=0x0000_0000_0000_00ff => I8, 0..=0x0000_0000_0000_00ff => I8,
0..=0x0000_0000_0000_ffff => I16, 0..=0x0000_0000_0000_ffff => I16,
@ -855,6 +860,7 @@ pub fn fit_unsigned(x: u128) -> Integer {
/// Finds the smallest integer with the given alignment. /// Finds the smallest integer with the given alignment.
pub fn for_align<C: HasDataLayout>(cx: &C, wanted: Align) -> Option<Integer> { pub fn for_align<C: HasDataLayout>(cx: &C, wanted: Align) -> Option<Integer> {
use Integer::*;
let dl = cx.data_layout(); let dl = cx.data_layout();
[I8, I16, I32, I64, I128].into_iter().find(|&candidate| { [I8, I16, I32, I64, I128].into_iter().find(|&candidate| {
@ -864,6 +870,7 @@ pub fn for_align<C: HasDataLayout>(cx: &C, wanted: Align) -> Option<Integer> {
/// Find the largest integer with the given alignment or less. /// Find the largest integer with the given alignment or less.
pub fn approximate_align<C: HasDataLayout>(cx: &C, wanted: Align) -> Integer { pub fn approximate_align<C: HasDataLayout>(cx: &C, wanted: Align) -> Integer {
use Integer::*;
let dl = cx.data_layout(); let dl = cx.data_layout();
// FIXME(eddyb) maybe include I128 in the future, when it works everywhere. // FIXME(eddyb) maybe include I128 in the future, when it works everywhere.
@ -909,6 +916,7 @@ pub enum Primitive {
impl Primitive { impl Primitive {
pub fn size<C: HasDataLayout>(self, cx: &C) -> Size { pub fn size<C: HasDataLayout>(self, cx: &C) -> Size {
use Primitive::*;
let dl = cx.data_layout(); let dl = cx.data_layout();
match self { match self {
@ -923,6 +931,7 @@ pub fn size<C: HasDataLayout>(self, cx: &C) -> Size {
} }
pub fn align<C: HasDataLayout>(self, cx: &C) -> AbiAndPrefAlign { pub fn align<C: HasDataLayout>(self, cx: &C) -> AbiAndPrefAlign {
use Primitive::*;
let dl = cx.data_layout(); let dl = cx.data_layout();
match self { match self {
@ -1027,10 +1036,11 @@ pub enum Scalar {
impl Scalar { impl Scalar {
#[inline] #[inline]
pub fn is_bool(&self) -> bool { pub fn is_bool(&self) -> bool {
use Integer::*;
matches!( matches!(
self, self,
Scalar::Initialized { Scalar::Initialized {
value: Int(I8, false), value: Primitive::Int(I8, false),
valid_range: WrappingRange { start: 0, end: 1 } valid_range: WrappingRange { start: 0, end: 1 }
} }
) )