mirror of
https://github.com/allankoechke/rust.git
synced 2026-11-30 11:05:36 +00:00
Rollup merge of #163384 - ada4a:push-xlkootrrlxkt, r=nnethercote
Various clean-ups around `LayoutCalculator` Details in individual commits. The commits are roughly in the order of increasing controversiality. The changes are small overall imo, and I don't see a clear splitting point, but if you do want me to split up this PR, let me know. cc @RalfJung (sorry I keep pinging you despite you not being on a review rotation.. others seem not too keen to look at this code)
This commit is contained in:
@@ -11,8 +11,9 @@ use tracing::{debug, trace};
|
||||
|
||||
use crate::{
|
||||
AbiAlign, Align, BackendLaneCount, BackendRepr, FieldsShape, HasDataLayout, IndexSlice,
|
||||
IndexVec, Integer, LayoutData, Niche, NumScalableVectors, Primitive, ReprOptions, Scalar, Size,
|
||||
StructKind, TagEncoding, TargetDataLayout, VariantLayout, Variants, WrappingRange,
|
||||
IndexVec, Integer, LayoutData, Niche, NicheOptimizations, NumScalableVectors, Primitive,
|
||||
ReprOptions, Scalar, Size, StructKind, TagEncoding, TargetDataLayout, VariantLayout, Variants,
|
||||
WrappingRange,
|
||||
};
|
||||
|
||||
mod coroutine;
|
||||
@@ -178,7 +179,7 @@ impl<Cx: HasDataLayout> LayoutCalculator<Cx> {
|
||||
Self { cx }
|
||||
}
|
||||
|
||||
pub fn array_like<FieldIdx: Idx, VariantIdx: Idx, F>(
|
||||
pub fn layout_of_array_like<FieldIdx: Idx, VariantIdx: Idx, F>(
|
||||
&self,
|
||||
element: &LayoutData<FieldIdx, VariantIdx>,
|
||||
count_if_sized: Option<u64>, // None for slices
|
||||
@@ -201,7 +202,7 @@ impl<Cx: HasDataLayout> LayoutCalculator<Cx> {
|
||||
})
|
||||
}
|
||||
|
||||
pub fn scalable_vector_type<FieldIdx, VariantIdx, F>(
|
||||
pub fn layout_of_scalable_vector_type<FieldIdx, VariantIdx, F>(
|
||||
&self,
|
||||
element: F,
|
||||
count: u64,
|
||||
@@ -220,7 +221,7 @@ impl<Cx: HasDataLayout> LayoutCalculator<Cx> {
|
||||
)
|
||||
}
|
||||
|
||||
pub fn simd_type<FieldIdx, VariantIdx, F>(
|
||||
pub fn layout_of_simd_type<FieldIdx, VariantIdx, F>(
|
||||
&self,
|
||||
element: F,
|
||||
count: u64,
|
||||
@@ -239,20 +240,20 @@ impl<Cx: HasDataLayout> LayoutCalculator<Cx> {
|
||||
///
|
||||
/// This uses dedicated code instead of [`Self::layout_of_struct_or_enum`], as coroutine
|
||||
/// fields may be shared between multiple variants (see the [`coroutine`] module for details).
|
||||
pub fn coroutine<
|
||||
'a,
|
||||
F: Deref<Target = &'a LayoutData<FieldIdx, VariantIdx>> + fmt::Debug + Copy,
|
||||
VariantIdx: Idx,
|
||||
FieldIdx: Idx,
|
||||
LocalIdx: Idx,
|
||||
>(
|
||||
pub fn layout_of_coroutine<'a, F, VariantIdx, FieldIdx, LocalIdx>(
|
||||
&self,
|
||||
local_layouts: &IndexSlice<LocalIdx, F>,
|
||||
prefix_layouts: IndexVec<FieldIdx, F>,
|
||||
variant_fields: &IndexSlice<VariantIdx, IndexVec<FieldIdx, LocalIdx>>,
|
||||
storage_conflicts: &BitMatrix<LocalIdx, LocalIdx>,
|
||||
tag_to_layout: impl Fn(Scalar) -> F,
|
||||
) -> LayoutCalculatorResult<FieldIdx, VariantIdx, F> {
|
||||
) -> LayoutCalculatorResult<FieldIdx, VariantIdx, F>
|
||||
where
|
||||
F: Deref<Target = &'a LayoutData<FieldIdx, VariantIdx>> + fmt::Debug + Copy,
|
||||
VariantIdx: Idx,
|
||||
FieldIdx: Idx,
|
||||
LocalIdx: Idx,
|
||||
{
|
||||
coroutine::layout(
|
||||
self,
|
||||
local_layouts,
|
||||
@@ -263,96 +264,96 @@ impl<Cx: HasDataLayout> LayoutCalculator<Cx> {
|
||||
)
|
||||
}
|
||||
|
||||
pub fn univariant<
|
||||
'a,
|
||||
FieldIdx: Idx,
|
||||
VariantIdx: Idx,
|
||||
F: Deref<Target = &'a LayoutData<FieldIdx, VariantIdx>> + fmt::Debug + Copy,
|
||||
>(
|
||||
/// Compute the layout for a univariant (see [`Variants::Single`]).
|
||||
///
|
||||
/// As a consumer of `rustc_abi`, you should only use this method for non-ADTs.
|
||||
/// For structs and univariant enums, use [`Self::layout_of_struct`] instead
|
||||
/// (it uses this function internally).
|
||||
pub fn layout_of_univariant<'a, FieldIdx, VariantIdx, F>(
|
||||
&self,
|
||||
fields: &IndexSlice<FieldIdx, F>,
|
||||
repr: &ReprOptions,
|
||||
kind: StructKind,
|
||||
) -> LayoutCalculatorResult<FieldIdx, VariantIdx, F> {
|
||||
) -> LayoutCalculatorResult<FieldIdx, VariantIdx, F>
|
||||
where
|
||||
FieldIdx: Idx,
|
||||
VariantIdx: Idx,
|
||||
F: Deref<Target = &'a LayoutData<FieldIdx, VariantIdx>> + fmt::Debug + Copy,
|
||||
{
|
||||
let dl = self.cx.data_layout();
|
||||
let layout = self.univariant_biased(fields, repr, kind, NicheBias::Start);
|
||||
let layout = self.layout_of_univariant_biased(fields, repr, kind, NicheBias::Start);
|
||||
// Enums prefer niches close to the beginning or the end of the variants so that other
|
||||
// (smaller) data-carrying variants can be packed into the space after/before the niche.
|
||||
// If the default field ordering does not give us a niche at the front then we do a second
|
||||
// run and bias niches to the right and then check which one is closer to one of the
|
||||
// struct's edges.
|
||||
if let Ok(layout) = &layout {
|
||||
if let Ok(layout) = &layout
|
||||
// Don't try to calculate an end-biased layout for unsizable structs,
|
||||
// otherwise we could end up with different layouts for
|
||||
// Foo<Type> and Foo<dyn Trait> which would break unsizing.
|
||||
if !matches!(kind, StructKind::MaybeUnsized) {
|
||||
if let Some(niche) = layout.largest_niche {
|
||||
let head_space = niche.offset.bytes();
|
||||
let niche_len = niche.value.size(dl).bytes();
|
||||
let tail_space = layout.size.bytes() - head_space - niche_len;
|
||||
&& !matches!(kind, StructKind::MaybeUnsized)
|
||||
&& let Some(niche) = layout.largest_niche
|
||||
&& let head_space = niche.offset.bytes()
|
||||
&& let niche_len = niche.value.size(dl).bytes()
|
||||
&& let tail_space = layout.size.bytes() - head_space - niche_len
|
||||
// This may end up doing redundant work if the niche is already in the last
|
||||
// field (e.g. a trailing bool) and there is tail padding. But it's non-trivial
|
||||
// to get the unpadded size so we try anyway.
|
||||
&& (fields.len() > 1 && head_space != 0 && tail_space > 0)
|
||||
{
|
||||
let alt_layout = self
|
||||
.layout_of_univariant_biased(fields, repr, kind, NicheBias::End)
|
||||
.expect("alt layout should always work");
|
||||
let alt_niche = alt_layout
|
||||
.largest_niche
|
||||
.expect("alt layout should have a niche like the regular one");
|
||||
let alt_head_space = alt_niche.offset.bytes();
|
||||
let alt_niche_len = alt_niche.value.size(dl).bytes();
|
||||
let alt_tail_space = alt_layout.size.bytes() - alt_head_space - alt_niche_len;
|
||||
|
||||
// This may end up doing redundant work if the niche is already in the last
|
||||
// field (e.g. a trailing bool) and there is tail padding. But it's non-trivial
|
||||
// to get the unpadded size so we try anyway.
|
||||
if fields.len() > 1 && head_space != 0 && tail_space > 0 {
|
||||
let alt_layout = self
|
||||
.univariant_biased(fields, repr, kind, NicheBias::End)
|
||||
.expect("alt layout should always work");
|
||||
let alt_niche = alt_layout
|
||||
.largest_niche
|
||||
.expect("alt layout should have a niche like the regular one");
|
||||
let alt_head_space = alt_niche.offset.bytes();
|
||||
let alt_niche_len = alt_niche.value.size(dl).bytes();
|
||||
let alt_tail_space =
|
||||
alt_layout.size.bytes() - alt_head_space - alt_niche_len;
|
||||
debug_assert_eq!(layout.size.bytes(), alt_layout.size.bytes());
|
||||
|
||||
debug_assert_eq!(layout.size.bytes(), alt_layout.size.bytes());
|
||||
let prefer_alt_layout = alt_head_space > head_space && alt_head_space > tail_space;
|
||||
|
||||
let prefer_alt_layout =
|
||||
alt_head_space > head_space && alt_head_space > tail_space;
|
||||
debug!(
|
||||
"sz: {}, default_niche_at: {}+{}, default_tail_space: {}, alt_niche_at/head_space: {}+{}, alt_tail: {}, num_fields: {}, better: {}\n\
|
||||
layout: {}\n\
|
||||
alt_layout: {}\n",
|
||||
layout.size.bytes(),
|
||||
head_space,
|
||||
niche_len,
|
||||
tail_space,
|
||||
alt_head_space,
|
||||
alt_niche_len,
|
||||
alt_tail_space,
|
||||
layout.fields.count(),
|
||||
prefer_alt_layout,
|
||||
self.format_field_niches(layout, fields),
|
||||
self.format_field_niches(&alt_layout, fields),
|
||||
);
|
||||
|
||||
debug!(
|
||||
"sz: {}, default_niche_at: {}+{}, default_tail_space: {}, alt_niche_at/head_space: {}+{}, alt_tail: {}, num_fields: {}, better: {}\n\
|
||||
layout: {}\n\
|
||||
alt_layout: {}\n",
|
||||
layout.size.bytes(),
|
||||
head_space,
|
||||
niche_len,
|
||||
tail_space,
|
||||
alt_head_space,
|
||||
alt_niche_len,
|
||||
alt_tail_space,
|
||||
layout.fields.count(),
|
||||
prefer_alt_layout,
|
||||
self.format_field_niches(layout, fields),
|
||||
self.format_field_niches(&alt_layout, fields),
|
||||
);
|
||||
|
||||
if prefer_alt_layout {
|
||||
return Ok(alt_layout);
|
||||
}
|
||||
}
|
||||
}
|
||||
if prefer_alt_layout {
|
||||
return Ok(alt_layout);
|
||||
}
|
||||
}
|
||||
layout
|
||||
}
|
||||
|
||||
pub fn layout_of_struct_or_enum<
|
||||
'a,
|
||||
FieldIdx: Idx,
|
||||
VariantIdx: Idx,
|
||||
F: Deref<Target = &'a LayoutData<FieldIdx, VariantIdx>> + fmt::Debug + Copy,
|
||||
>(
|
||||
pub fn layout_of_struct_or_enum<'a, FieldIdx, VariantIdx, F>(
|
||||
&self,
|
||||
repr: &ReprOptions,
|
||||
variants: &IndexSlice<VariantIdx, IndexVec<FieldIdx, F>>,
|
||||
is_enum: bool,
|
||||
is_special_no_niche: bool,
|
||||
niche_optimizations: NicheOptimizations,
|
||||
discr_range_of_repr: impl Fn(RangeFrom<i128>, RangeToInclusive<u128>) -> (Integer, bool),
|
||||
discriminants: impl Iterator<Item = (VariantIdx, u128)>,
|
||||
always_sized: bool,
|
||||
) -> LayoutCalculatorResult<FieldIdx, VariantIdx, F> {
|
||||
) -> LayoutCalculatorResult<FieldIdx, VariantIdx, F>
|
||||
where
|
||||
FieldIdx: Idx,
|
||||
VariantIdx: Idx,
|
||||
F: Deref<Target = &'a LayoutData<FieldIdx, VariantIdx>> + fmt::Debug + Copy,
|
||||
{
|
||||
let (present_first, present_second) = {
|
||||
let mut present_variants = variants.iter_enumerated().filter_map(|(i, v)| {
|
||||
if !repr.inhibit_enum_layout_opt() && absent(v) { None } else { Some(i) }
|
||||
@@ -378,10 +379,10 @@ impl<Cx: HasDataLayout> LayoutCalculator<Cx> {
|
||||
self.layout_of_struct(
|
||||
repr,
|
||||
variants,
|
||||
is_enum,
|
||||
is_special_no_niche,
|
||||
always_sized,
|
||||
present_first,
|
||||
is_enum,
|
||||
niche_optimizations,
|
||||
always_sized,
|
||||
)
|
||||
} else {
|
||||
// At this point, we have handled all unions and
|
||||
@@ -392,16 +393,16 @@ impl<Cx: HasDataLayout> LayoutCalculator<Cx> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn layout_of_union<
|
||||
'a,
|
||||
FieldIdx: Idx,
|
||||
VariantIdx: Idx,
|
||||
F: Deref<Target = &'a LayoutData<FieldIdx, VariantIdx>> + fmt::Debug + Copy,
|
||||
>(
|
||||
pub fn layout_of_union<'a, FieldIdx, VariantIdx, F>(
|
||||
&self,
|
||||
repr: &ReprOptions,
|
||||
variants: &IndexSlice<VariantIdx, IndexVec<FieldIdx, F>>,
|
||||
) -> LayoutCalculatorResult<FieldIdx, VariantIdx, F> {
|
||||
) -> LayoutCalculatorResult<FieldIdx, VariantIdx, F>
|
||||
where
|
||||
FieldIdx: Idx,
|
||||
VariantIdx: Idx,
|
||||
F: Deref<Target = &'a LayoutData<FieldIdx, VariantIdx>> + fmt::Debug + Copy,
|
||||
{
|
||||
let dl = self.cx.data_layout();
|
||||
let mut align = if repr.pack.is_some() { dl.i8_align } else { dl.aggregate_align };
|
||||
let mut max_repr_align = repr.align;
|
||||
@@ -519,36 +520,36 @@ impl<Cx: HasDataLayout> LayoutCalculator<Cx> {
|
||||
})
|
||||
}
|
||||
|
||||
/// single-variant enums are just structs, if you think about it
|
||||
fn layout_of_struct<
|
||||
'a,
|
||||
FieldIdx: Idx,
|
||||
VariantIdx: Idx,
|
||||
F: Deref<Target = &'a LayoutData<FieldIdx, VariantIdx>> + fmt::Debug + Copy,
|
||||
>(
|
||||
/// Calculate the layout for a struct, or a single-variant enum.
|
||||
///
|
||||
/// They are the same thing, if you think about it
|
||||
/// (Typechecking will reject discriminant-sizing attrs.)
|
||||
fn layout_of_struct<'a, FieldIdx, VariantIdx, F>(
|
||||
&self,
|
||||
repr: &ReprOptions,
|
||||
variants: &IndexSlice<VariantIdx, IndexVec<FieldIdx, F>>,
|
||||
variant_idx: VariantIdx,
|
||||
is_enum: bool,
|
||||
is_special_no_niche: bool,
|
||||
niche_optimizations: NicheOptimizations,
|
||||
always_sized: bool,
|
||||
present_first: VariantIdx,
|
||||
) -> LayoutCalculatorResult<FieldIdx, VariantIdx, F> {
|
||||
// Struct, or univariant enum equivalent to a struct.
|
||||
// (Typechecking will reject discriminant-sizing attrs.)
|
||||
|
||||
) -> LayoutCalculatorResult<FieldIdx, VariantIdx, F>
|
||||
where
|
||||
FieldIdx: Idx,
|
||||
VariantIdx: Idx,
|
||||
F: Deref<Target = &'a LayoutData<FieldIdx, VariantIdx>> + fmt::Debug + Copy,
|
||||
{
|
||||
let dl = self.cx.data_layout();
|
||||
let v = present_first;
|
||||
let v = variant_idx;
|
||||
let kind = if is_enum || variants[v].is_empty() || always_sized {
|
||||
StructKind::AlwaysSized
|
||||
} else {
|
||||
StructKind::MaybeUnsized
|
||||
};
|
||||
|
||||
let mut st = self.univariant(&variants[v], repr, kind)?;
|
||||
let mut st = self.layout_of_univariant(&variants[v], repr, kind)?;
|
||||
st.variants = Variants::Single { index: v };
|
||||
|
||||
if is_special_no_niche {
|
||||
if niche_optimizations == NicheOptimizations::Disabled {
|
||||
let hide_niches = |scalar: &mut _| match scalar {
|
||||
Scalar::Initialized { value, valid_range } => {
|
||||
*valid_range = WrappingRange::full(value.size(dl))
|
||||
@@ -573,18 +574,18 @@ impl<Cx: HasDataLayout> LayoutCalculator<Cx> {
|
||||
Ok(st)
|
||||
}
|
||||
|
||||
fn layout_of_enum<
|
||||
'a,
|
||||
FieldIdx: Idx,
|
||||
VariantIdx: Idx,
|
||||
F: Deref<Target = &'a LayoutData<FieldIdx, VariantIdx>> + fmt::Debug + Copy,
|
||||
>(
|
||||
fn layout_of_enum<'a, FieldIdx, VariantIdx, F>(
|
||||
&self,
|
||||
repr: &ReprOptions,
|
||||
variants: &IndexSlice<VariantIdx, IndexVec<FieldIdx, F>>,
|
||||
discr_range_of_repr: impl Fn(RangeFrom<i128>, RangeToInclusive<u128>) -> (Integer, bool),
|
||||
discriminants: impl Iterator<Item = (VariantIdx, u128)>,
|
||||
) -> LayoutCalculatorResult<FieldIdx, VariantIdx, F> {
|
||||
) -> LayoutCalculatorResult<FieldIdx, VariantIdx, F>
|
||||
where
|
||||
FieldIdx: Idx,
|
||||
VariantIdx: Idx,
|
||||
F: Deref<Target = &'a LayoutData<FieldIdx, VariantIdx>> + fmt::Debug + Copy,
|
||||
{
|
||||
let dl = self.cx.data_layout();
|
||||
// bail if the enum has an incoherent repr that cannot be computed
|
||||
if repr.packed() {
|
||||
@@ -613,7 +614,7 @@ impl<Cx: HasDataLayout> LayoutCalculator<Cx> {
|
||||
let mut variant_layouts = variants
|
||||
.iter()
|
||||
.map(|v| {
|
||||
let st = self.univariant(v, repr, StructKind::AlwaysSized).ok()?;
|
||||
let st = self.layout_of_univariant(v, repr, StructKind::AlwaysSized).ok()?;
|
||||
|
||||
variants_info.push(VariantLayoutInfo { align_abi: st.align.abi });
|
||||
|
||||
@@ -815,7 +816,7 @@ impl<Cx: HasDataLayout> LayoutCalculator<Cx> {
|
||||
let mut layout_variants = variants
|
||||
.iter()
|
||||
.map(|field_layouts| {
|
||||
let st = self.univariant(
|
||||
let st = self.layout_of_univariant(
|
||||
field_layouts,
|
||||
repr,
|
||||
StructKind::Prefixed(min_ity.size(), prefix_align),
|
||||
@@ -1078,18 +1079,18 @@ impl<Cx: HasDataLayout> LayoutCalculator<Cx> {
|
||||
Ok(best_layout)
|
||||
}
|
||||
|
||||
fn univariant_biased<
|
||||
'a,
|
||||
FieldIdx: Idx,
|
||||
VariantIdx: Idx,
|
||||
F: Deref<Target = &'a LayoutData<FieldIdx, VariantIdx>> + fmt::Debug + Copy,
|
||||
>(
|
||||
fn layout_of_univariant_biased<'a, FieldIdx, VariantIdx, F>(
|
||||
&self,
|
||||
fields: &IndexSlice<FieldIdx, F>,
|
||||
repr: &ReprOptions,
|
||||
kind: StructKind,
|
||||
niche_bias: NicheBias,
|
||||
) -> LayoutCalculatorResult<FieldIdx, VariantIdx, F> {
|
||||
) -> LayoutCalculatorResult<FieldIdx, VariantIdx, F>
|
||||
where
|
||||
FieldIdx: Idx,
|
||||
VariantIdx: Idx,
|
||||
F: Deref<Target = &'a LayoutData<FieldIdx, VariantIdx>> + fmt::Debug + Copy,
|
||||
{
|
||||
let dl = self.cx.data_layout();
|
||||
let pack = repr.pack;
|
||||
let mut align = if pack.is_some() { dl.i8_align } else { dl.aggregate_align };
|
||||
@@ -1424,16 +1425,16 @@ impl<Cx: HasDataLayout> LayoutCalculator<Cx> {
|
||||
})
|
||||
}
|
||||
|
||||
fn format_field_niches<
|
||||
'a,
|
||||
FieldIdx: Idx,
|
||||
VariantIdx: Idx,
|
||||
F: Deref<Target = &'a LayoutData<FieldIdx, VariantIdx>> + fmt::Debug,
|
||||
>(
|
||||
fn format_field_niches<'a, FieldIdx, VariantIdx, F>(
|
||||
&self,
|
||||
layout: &LayoutData<FieldIdx, VariantIdx>,
|
||||
fields: &IndexSlice<FieldIdx, F>,
|
||||
) -> String {
|
||||
) -> String
|
||||
where
|
||||
FieldIdx: Idx,
|
||||
VariantIdx: Idx,
|
||||
F: Deref<Target = &'a LayoutData<FieldIdx, VariantIdx>> + fmt::Debug,
|
||||
{
|
||||
let dl = self.cx.data_layout();
|
||||
let mut s = String::new();
|
||||
for i in layout.fields.index_by_increasing_offset() {
|
||||
@@ -1484,15 +1485,13 @@ where
|
||||
};
|
||||
|
||||
// Compute the size and alignment of the vector
|
||||
let size = elt
|
||||
.size
|
||||
.checked_mul(count.as_u64(), dl)
|
||||
.ok_or_else(|| LayoutCalculatorError::SizeOverflow)?;
|
||||
let size =
|
||||
elt.size.checked_mul(count.as_u64(), dl).ok_or(LayoutCalculatorError::SizeOverflow)?;
|
||||
let (repr, size, align) = match kind {
|
||||
SimdVectorKind::Scalable(number_of_vectors) => (
|
||||
BackendRepr::SimdScalableVector { element, count, number_of_vectors },
|
||||
size.checked_mul(number_of_vectors.0 as u64, dl)
|
||||
.ok_or_else(|| LayoutCalculatorError::SizeOverflow)?,
|
||||
.ok_or(LayoutCalculatorError::SizeOverflow)?,
|
||||
dl.rust_vector_align(size),
|
||||
),
|
||||
// Non-power-of-two vectors have padding up to the next power-of-two.
|
||||
|
||||
@@ -136,20 +136,20 @@ fn coroutine_saved_local_eligibility<VariantIdx: Idx, FieldIdx: Idx, LocalIdx: I
|
||||
}
|
||||
|
||||
/// Compute the full coroutine layout.
|
||||
pub(super) fn layout<
|
||||
'a,
|
||||
F: core::ops::Deref<Target = &'a LayoutData<FieldIdx, VariantIdx>> + core::fmt::Debug + Copy,
|
||||
VariantIdx: Idx,
|
||||
FieldIdx: Idx,
|
||||
LocalIdx: Idx,
|
||||
>(
|
||||
pub(super) fn layout<'a, F, VariantIdx, FieldIdx, LocalIdx>(
|
||||
calc: &super::LayoutCalculator<impl HasDataLayout>,
|
||||
local_layouts: &IndexSlice<LocalIdx, F>,
|
||||
mut prefix_layouts: IndexVec<FieldIdx, F>,
|
||||
variant_fields: &IndexSlice<VariantIdx, IndexVec<FieldIdx, LocalIdx>>,
|
||||
storage_conflicts: &BitMatrix<LocalIdx, LocalIdx>,
|
||||
tag_to_layout: impl Fn(Scalar) -> F,
|
||||
) -> super::LayoutCalculatorResult<FieldIdx, VariantIdx, F> {
|
||||
) -> super::LayoutCalculatorResult<FieldIdx, VariantIdx, F>
|
||||
where
|
||||
F: core::ops::Deref<Target = &'a LayoutData<FieldIdx, VariantIdx>> + core::fmt::Debug + Copy,
|
||||
VariantIdx: Idx,
|
||||
FieldIdx: Idx,
|
||||
LocalIdx: Idx,
|
||||
{
|
||||
use SavedLocalEligibility::*;
|
||||
|
||||
let (ineligible_locals, assignments) =
|
||||
@@ -171,8 +171,11 @@ pub(super) fn layout<
|
||||
let promoted_layouts = ineligible_locals.iter().map(|local| local_layouts[local]);
|
||||
prefix_layouts.push(tag_to_layout(tag));
|
||||
prefix_layouts.extend(promoted_layouts);
|
||||
let prefix =
|
||||
calc.univariant(&prefix_layouts, &ReprOptions::default(), StructKind::AlwaysSized)?;
|
||||
let prefix = calc.layout_of_univariant(
|
||||
&prefix_layouts,
|
||||
&ReprOptions::default(),
|
||||
StructKind::AlwaysSized,
|
||||
)?;
|
||||
|
||||
let (prefix_size, prefix_align) = (prefix.size, prefix.align);
|
||||
|
||||
@@ -225,7 +228,7 @@ pub(super) fn layout<
|
||||
})
|
||||
.map(|local| local_layouts[*local]);
|
||||
|
||||
let mut variant = calc.univariant(
|
||||
let mut variant = calc.layout_of_univariant(
|
||||
&variant_only_tys.collect::<IndexVec<_, _>>(),
|
||||
&ReprOptions::default(),
|
||||
StructKind::Prefixed(prefix_size, prefix_align.abi),
|
||||
|
||||
@@ -2027,10 +2027,10 @@ pub enum Variants<FieldIdx: Idx, VariantIdx: Idx> {
|
||||
/// 2. the never type
|
||||
Empty,
|
||||
|
||||
/// The type has a single valid variant.
|
||||
/// The type has a single valid variant. Such types are called "univariant".
|
||||
///
|
||||
/// This is the case for:
|
||||
/// 1. enums with a single inhabited variant
|
||||
/// 1. enums with a single inhabited variant, aka. "univariant enums"
|
||||
/// 2. structs, unions, and non-ADTs (except coroutines; see below),
|
||||
/// as those can't have multiple variants
|
||||
Single {
|
||||
@@ -2195,6 +2195,18 @@ impl Niche {
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether niche optimizations should be performed during layout calculation.
|
||||
///
|
||||
/// [`UnsafeCell`] and [`UnsafePinned`] both disable niche optimizations.
|
||||
///
|
||||
/// [`UnsafeCell`]: std::cell::UnsafeCell
|
||||
/// [`UnsafePinned`]: std::pin::UnsafePinned
|
||||
#[derive(PartialEq, Eq, Clone, Copy)]
|
||||
pub enum NicheOptimizations {
|
||||
Enabled,
|
||||
Disabled,
|
||||
}
|
||||
|
||||
// NOTE: This struct is generic over the FieldIdx and VariantIdx for rust-analyzer usage.
|
||||
#[derive(PartialEq, Eq, Hash, Clone)]
|
||||
#[cfg_attr(feature = "nightly", derive(StableHash))]
|
||||
|
||||
@@ -5,8 +5,8 @@ use rustc_abi::Integer::{I8, I32};
|
||||
use rustc_abi::Primitive::{self, Float, Int, Pointer};
|
||||
use rustc_abi::{
|
||||
AddressSpace, BackendRepr, FIRST_VARIANT, FieldIdx, FieldsShape, HasDataLayout, Layout,
|
||||
LayoutCalculatorError, LayoutData, Niche, ReprOptions, Scalar, Size, StructKind, TagEncoding,
|
||||
VariantIdx, Variants, WrappingRange,
|
||||
LayoutCalculatorError, LayoutData, Niche, NicheOptimizations, ReprOptions, Scalar, Size,
|
||||
StructKind, TagEncoding, VariantIdx, Variants, WrappingRange,
|
||||
};
|
||||
use rustc_attr_ir::find_attr;
|
||||
use rustc_attr_ir::lang_items::LangItem;
|
||||
@@ -235,7 +235,7 @@ fn layout_of_uncached<'tcx>(
|
||||
let univariant = |tys: &[Ty<'tcx>], kind| {
|
||||
let fields = tys.iter().map(|ty| cx.layout_of(*ty)).try_collect::<IndexVec<_, _>>()?;
|
||||
let repr = ReprOptions::default();
|
||||
map_layout(cx.calc.univariant(&fields, &repr, kind))
|
||||
map_layout(cx.calc.layout_of_univariant(&fields, &repr, kind))
|
||||
};
|
||||
debug_assert!(!ty.has_non_region_infer());
|
||||
|
||||
@@ -523,11 +523,11 @@ fn layout_of_uncached<'tcx>(
|
||||
.ok_or_else(|| error(cx, LayoutError::Unknown(ty)))?;
|
||||
|
||||
let element = cx.layout_of(element)?;
|
||||
map_layout(cx.calc.array_like(&element, Some(count)))?
|
||||
map_layout(cx.calc.layout_of_array_like(&element, Some(count)))?
|
||||
}
|
||||
ty::Slice(element) => {
|
||||
let element = cx.layout_of(element)?;
|
||||
map_layout(cx.calc.array_like(&element, None).map(|mut layout| {
|
||||
map_layout(cx.calc.layout_of_array_like(&element, None).map(|mut layout| {
|
||||
// a randomly chosen value to distinguish slices
|
||||
layout.randomization_seed = Hash64::new(0x2dcba99c39784102);
|
||||
layout
|
||||
@@ -535,7 +535,7 @@ fn layout_of_uncached<'tcx>(
|
||||
}
|
||||
ty::Str => {
|
||||
let element = scalar(Int(I8, false));
|
||||
map_layout(cx.calc.array_like(&element, None).map(|mut layout| {
|
||||
map_layout(cx.calc.layout_of_array_like(&element, None).map(|mut layout| {
|
||||
// another random value
|
||||
layout.randomization_seed = Hash64::new(0xc1325f37d127be22);
|
||||
layout
|
||||
@@ -586,7 +586,7 @@ fn layout_of_uncached<'tcx>(
|
||||
|
||||
let layout = cx
|
||||
.calc
|
||||
.coroutine(
|
||||
.layout_of_coroutine(
|
||||
&local_layouts,
|
||||
prefix_layouts,
|
||||
&info.variant_fields,
|
||||
@@ -638,7 +638,7 @@ fn layout_of_uncached<'tcx>(
|
||||
};
|
||||
|
||||
let element_layout = cx.layout_of(element_ty)?;
|
||||
map_layout(cx.calc.scalable_vector_type(
|
||||
map_layout(cx.calc.layout_of_scalable_vector_type(
|
||||
element_layout,
|
||||
element_count as u64,
|
||||
number_of_vectors,
|
||||
@@ -683,7 +683,7 @@ fn layout_of_uncached<'tcx>(
|
||||
}
|
||||
}
|
||||
|
||||
map_layout(cx.calc.simd_type(e_ly, e_len, def.repr().packed()))?
|
||||
map_layout(cx.calc.layout_of_simd_type(e_ly, e_len, def.repr().packed()))?
|
||||
}
|
||||
|
||||
// ADTs.
|
||||
@@ -713,7 +713,10 @@ fn layout_of_uncached<'tcx>(
|
||||
}
|
||||
|
||||
// UnsafeCell and UnsafePinned both disable niche optimizations
|
||||
let is_special_no_niche = def.is_unsafe_cell() || def.is_unsafe_pinned();
|
||||
let niche_optimizations = match def.is_unsafe_cell() || def.is_unsafe_pinned() {
|
||||
true => NicheOptimizations::Disabled,
|
||||
false => NicheOptimizations::Enabled,
|
||||
};
|
||||
|
||||
let discr_range_of_repr = |min: RangeFrom<i128>, max: RangeToInclusive<u128>| {
|
||||
abi::Integer::discr_range_of_repr(tcx, ty, &def.repr(), min.start, max.last)
|
||||
@@ -755,7 +758,7 @@ fn layout_of_uncached<'tcx>(
|
||||
&def.repr(),
|
||||
&variants,
|
||||
def.is_enum(),
|
||||
is_special_no_niche,
|
||||
niche_optimizations,
|
||||
discr_range_of_repr,
|
||||
discriminants_iter(),
|
||||
!maybe_unsized,
|
||||
@@ -784,7 +787,7 @@ fn layout_of_uncached<'tcx>(
|
||||
&def.repr(),
|
||||
&variants,
|
||||
def.is_enum(),
|
||||
is_special_no_niche,
|
||||
niche_optimizations,
|
||||
discr_range_of_repr,
|
||||
discriminants_iter(),
|
||||
!maybe_unsized,
|
||||
|
||||
Reference in New Issue
Block a user