Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions compiler/rustc_abi/src/layout.rs
Comment thread
RalfJung marked this conversation as resolved.
Resolved
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ impl<Cx: HasDataLayout> LayoutCalculator<Cx> {
size,
max_repr_align: None,
unadjusted_abi_align: element.align,
repr_c: element.repr_c,
trivial_abi: element.trivial_abi && count_if_sized.is_some(),
randomization_seed: element.randomization_seed.wrapping_add(Hash64::new(count)),
})
}
Expand Down Expand Up @@ -511,8 +511,10 @@ impl<Cx: HasDataLayout> LayoutCalculator<Cx> {
};

// We also need to check for a `repr(transparent)` wrapper around `repr(C)`
let repr_c =
repr.c() || (repr.transparent() && only_variant.iter().any(|field| field.repr_c));
let trivial_abi = size == Size::ZERO
&& align == Align::ONE
&& !repr.c()
&& (!repr.transparent() || only_variant.iter().all(|field| field.trivial_abi));

let combined_seed = only_variant
.iter()
Expand All @@ -529,7 +531,7 @@ impl<Cx: HasDataLayout> LayoutCalculator<Cx> {
size: size.align_to(align),
max_repr_align,
unadjusted_abi_align,
repr_c,
trivial_abi,
randomization_seed: combined_seed,
})
}
Expand Down Expand Up @@ -916,7 +918,11 @@ impl<Cx: HasDataLayout> LayoutCalculator<Cx> {
};

// We also need to check for a `repr(transparent)` wrapper around `repr(C)`
let repr_c = repr.c() || (repr.transparent() && fields.iter().any(|field| field.repr_c));
let trivial_abi = sized
&& size == Size::ZERO
&& align == Align::ONE
&& !repr.c()
&& (!repr.transparent() || fields.iter().all(|field| field.trivial_abi));

let seed = field_seed.wrapping_add(repr.field_shuffle_seed);

Expand All @@ -930,7 +936,7 @@ impl<Cx: HasDataLayout> LayoutCalculator<Cx> {
size,
max_repr_align,
unadjusted_abi_align,
repr_c,
trivial_abi,
randomization_seed: seed,
})
}
Expand Down Expand Up @@ -1029,7 +1035,7 @@ where
align,
max_repr_align: None,
unadjusted_abi_align: elt.align,
repr_c: false,
trivial_abi: false,
randomization_seed: elt.randomization_seed.wrapping_add(Hash64::new(count.as_u64())),
})
}
6 changes: 3 additions & 3 deletions compiler/rustc_abi/src/layout/coroutine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ use rustc_index::{Idx, IndexSlice, IndexVec};
use tracing::{debug, trace};

use crate::{
BackendRepr, FieldsShape, HasDataLayout, Integer, LayoutData, Primitive, ReprOptions, Scalar,
StructKind, TagEncoding, VariantLayout, Variants, WrappingRange,
Align, BackendRepr, FieldsShape, HasDataLayout, Integer, LayoutData, Primitive, ReprOptions,
Scalar, Size, StructKind, TagEncoding, VariantLayout, Variants, WrappingRange,
};

/// This option controls how coroutine saved locals are packed
Expand Down Expand Up @@ -333,7 +333,7 @@ where
align,
max_repr_align: None,
unadjusted_abi_align: align,
repr_c: false,
trivial_abi: size == Size::ZERO && align == Align::ONE,
randomization_seed: Default::default(),
})
}
4 changes: 2 additions & 2 deletions compiler/rustc_abi/src/layout/enum_layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ where
size,
max_repr_align,
unadjusted_abi_align,
repr_c: repr.c(),
trivial_abi: size == Size::ZERO && align == Align::ONE && !repr.c(),
randomization_seed: combined_seed,
})
}
Expand Down Expand Up @@ -656,7 +656,7 @@ where
align,
max_repr_align,
unadjusted_abi_align,
repr_c: repr.c(),
trivial_abi: size == Size::ZERO && align == Align::ONE && !repr.c(),
randomization_seed: combined_seed,
};

Expand Down
12 changes: 6 additions & 6 deletions compiler/rustc_abi/src/layout/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl<FieldIdx: Idx, VariantIdx: Idx> LayoutData<FieldIdx, VariantIdx> {
size: Size::ZERO,
max_repr_align: None,
unadjusted_abi_align: Align::ONE,
repr_c: false,
trivial_abi: sized,
randomization_seed: Hash64::new(0),
}
}
Expand All @@ -41,7 +41,7 @@ impl<FieldIdx: Idx, VariantIdx: Idx> LayoutData<FieldIdx, VariantIdx> {
size: Size::ZERO,
max_repr_align: None,
unadjusted_abi_align: Align::ONE,
repr_c: false,
trivial_abi: true,
randomization_seed: Hash64::ZERO,
}
}
Expand Down Expand Up @@ -84,7 +84,7 @@ impl<FieldIdx: Idx, VariantIdx: Idx> LayoutData<FieldIdx, VariantIdx> {
align,
max_repr_align: None,
unadjusted_abi_align: align,
repr_c: false,
trivial_abi: false,
randomization_seed: Hash64::new(randomization_seed),
}
}
Expand Down Expand Up @@ -118,7 +118,7 @@ impl<FieldIdx: Idx, VariantIdx: Idx> LayoutData<FieldIdx, VariantIdx> {
size,
max_repr_align: None,
unadjusted_abi_align: align,
repr_c: false,
trivial_abi: false,
randomization_seed: Hash64::new(combined_seed),
}
}
Expand All @@ -144,7 +144,7 @@ impl<FieldIdx: Idx, VariantIdx: Idx> LayoutData<FieldIdx, VariantIdx> {
size: Size::ZERO,
max_repr_align: None,
unadjusted_abi_align: Align::ONE,
repr_c: false,
trivial_abi: true,
// Variant layouts never flow back into actual layout computations,
// so dummy values are fine here.
randomization_seed: Hash64::ZERO,
Expand All @@ -171,7 +171,7 @@ impl<FieldIdx: Idx, VariantIdx: Idx> LayoutData<FieldIdx, VariantIdx> {
align: parent.align,
max_repr_align: parent.max_repr_align,
unadjusted_abi_align: parent.unadjusted_abi_align,
repr_c: parent.repr_c,
trivial_abi: parent.trivial_abi,
// Variant layouts never flow back into actual layout computations,
// so dummy values are fine here.
randomization_seed: Hash64::ZERO,
Expand Down
33 changes: 19 additions & 14 deletions compiler/rustc_abi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2264,12 +2264,14 @@ pub struct LayoutData<FieldIdx: Idx, VariantIdx: Idx> {
/// alignment in some cases.
pub unadjusted_abi_align: Align,

/// Whether this type is `repr(C)`, or a `repr(transparent)` wrapper around such,
/// or an array of such.
/// This matters because we must follow the C ABI for these types.
/// Some C ABIs pass `repr(C)` ZSTs by pointer, but `repr(Rust)` ZSTs should always
/// be ignored.
pub repr_c: bool,
/// Whether this type is a 1-ZST that is not `repr(C)`, or a `repr(transparent)` wrapper around
/// such, or an array of such.
/// This matters because we must follow the C ABI for non-trivial ZSTs.
/// Some C ABIs pass `repr(C)` ZSTs by pointer, but `repr(Rust)` ZSTs should always be ignored.
/// This is `true` iff a type has trivial ABI as described in the [function ABI docs].
///
/// [function ABI docs]: https://doc.rust-lang.org/nightly/std/primitive.fn.html#abi-compatibility
pub trivial_abi: bool,
Comment thread
beetrees marked this conversation as resolved.
Resolved

/// The randomization seed based on this type's own repr and its fields.
///
Expand Down Expand Up @@ -2311,10 +2313,10 @@ impl<FieldIdx: Idx, VariantIdx: Idx> LayoutData<FieldIdx, VariantIdx> {
}
}

/// Returns `true` if this is a `repr(C)` type,
/// or an array of such, or a `repr(transparent)` wrapper around such
pub fn is_repr_c(&self) -> bool {
self.repr_c
/// Returns `true` if this is a 1-ZST that is not `repr(C)`,
/// or an array of such, or a `repr(transparent)` wrapper around such.
pub fn has_trivial_abi(&self) -> bool {
self.trivial_abi
}
}

Expand All @@ -2337,7 +2339,7 @@ where
variants,
max_repr_align,
unadjusted_abi_align,
repr_c,
trivial_abi,
randomization_seed,
} = self;
f.debug_struct("Layout")
Expand All @@ -2350,7 +2352,7 @@ where
.field("variants", variants)
.field("max_repr_align", max_repr_align)
.field("unadjusted_abi_align", unadjusted_abi_align)
.field("repr_c", repr_c)
.field("trivial_abi", trivial_abi)
.field("randomization_seed", randomization_seed)
.finish()
}
Expand Down Expand Up @@ -2463,19 +2465,21 @@ impl<FieldIdx: Idx, VariantIdx: Idx> LayoutData<FieldIdx, VariantIdx> {
size,
align,
backend_repr,
// `trivial_abi` is not directly checked by codegen, but something went very wrong if it
// does not match, so we check it anyway.
trivial_abi,
Comment thread
beetrees marked this conversation as resolved.
Resolved
// We assume aggregates are passed via `Cast` or `Indirect*` so their details either
// do not matter or they are checked by `PassMode::eq_abi`.
fields: _,
variants: _,
// Inhabitedness and niches are not ABI-relevant.
largest_niche: _,
uninhabited: _,
// max_repr_align, unadjusted_abi_align, repr_c are not read directly by codegen
// max_repr_align and unadjusted_abi_align are not read directly by codegen
// backends. They are only used to compute `PassMode`. So we can ignore them here, any
// differences they may cause are picked up by the `PassMode` check.
max_repr_align: _,
unadjusted_abi_align: _,
repr_c: _,
// The seed does not affect the ABI.
randomization_seed: _,
} = self;
Expand All @@ -2487,6 +2491,7 @@ impl<FieldIdx: Idx, VariantIdx: Idx> LayoutData<FieldIdx, VariantIdx> {
&& backend_repr.eq_up_to_validity(&other.backend_repr)
// `is_bool` checks validity and is ABI-relevant, so we need to consider it.
&& backend_repr.is_bool() == other.backend_repr.is_bool()
&& *trivial_abi == other.trivial_abi
}
}

Expand Down
49 changes: 2 additions & 47 deletions compiler/rustc_const_eval/src/interpret/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,51 +71,6 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
})
}

/// Returns whether the given type has trivial ABI.
fn has_trivial_abi(&self, layout: TyAndLayout<'tcx>) -> InterpResult<'tcx, bool> {
if !layout.is_1zst() {
return interp_ok(false);
}
match *layout.ty.kind() {
// Trivially trivial-ABI types (because Rust makes no promises about their ABI).
ty::Tuple(..)
| ty::Never
| ty::FnDef(..)
| ty::Closure(..)
| ty::Coroutine(..)
| ty::CoroutineClosure(..) => interp_ok(true),

ty::Array(elem, _len) => {
// 0-length arrays are in general *not* okay, but arrays of trivial-ABI types are.
self.has_trivial_abi(self.layout_of(elem)?)
}
ty::Adt(adt_def, _args) => {
if adt_def.repr().transparent() {
// All fields must have trivial ABI.
(0..layout.fields.count()).try_fold(true, |acc, idx| {
interp_ok(acc && self.has_trivial_abi(layout.field(self, idx))?)
})
} else if adt_def.repr().c() {
interp_ok(false)
} else {
// Can't be SIMD or Scalable (since this is a 1-ZST); only Rust is left.
assert!(adt_def.repr().rust());
interp_ok(true)
}
}
// Types that are considered transparent in `unfold_transparent` should also act
// like transparent types here.
ty::Pat(base, _) => self.has_trivial_abi(self.layout_of(base)?),
ty::UnsafeBinder(bound_ty) => {
let ty = self.tcx.instantiate_bound_regions_with_erased(bound_ty.into());
self.has_trivial_abi(self.layout_of(ty)?)
}

ty::Alias(..) => panic!("non-normalized type"),
_ => interp_ok(false),
}
}

/// Find the wrapped inner type of a transparent wrapper by going for the unique
/// non-trivial-ABI field.
///
Expand All @@ -132,7 +87,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
let mut found = None;
for idx in 0..layout.fields.count() {
let field = layout.field(self, idx);
if self.has_trivial_abi(field)? {
if field.has_trivial_abi() {
continue;
}
// Found a non-trivial ABI field!
Expand Down Expand Up @@ -302,7 +257,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
return interp_ok(true);
}
// Handle trivial-ABI types.
if self.has_trivial_abi(caller)? && self.has_trivial_abi(callee)? {
if caller.has_trivial_abi() && callee.has_trivial_abi() {
return interp_ok(true);
}
// Unfold newtypes and NPO optimizations.
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_target/src/callconv/powerpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ where
// powerpc-unknown-linux-{gnu,musl,uclibc} doesn't ignore ZSTs.
if cx.target_spec().os == Os::Linux
&& matches!(cx.target_spec().env, Env::Gnu | Env::Musl | Env::Uclibc)
&& arg.layout.is_repr_c()
&& !arg.layout.has_trivial_abi()
{
*gprs_in_use += 1;
arg.make_indirect_from_ignore();
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_target/src/callconv/s390x.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ where
// s390x-unknown-linux-{gnu,musl,uclibc} doesn't ignore ZSTs.
if cx.target_spec().os == Os::Linux
&& matches!(cx.target_spec().env, Env::Gnu | Env::Musl | Env::Uclibc)
&& arg.layout.is_repr_c()
&& !arg.layout.has_trivial_abi()
{
arg.make_indirect_from_ignore();
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_target/src/callconv/sparc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ where

for arg in fn_abi.args.iter_mut() {
if arg.is_ignore() {
if arg.layout.is_zst() && arg.layout.is_repr_c() {
if !arg.layout.has_trivial_abi() {
arg.make_indirect_from_ignore();
}
continue;
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_target/src/callconv/sparc64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ where
continue;
}
if arg.is_ignore() {
if passes_zsts && arg.layout.is_repr_c() {
if passes_zsts && !arg.layout.has_trivial_abi() {
arg.make_indirect_from_ignore();
double_word_count += 1;
}
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_target/src/callconv/x86_win64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ where
// but `repr(Rust)` ones are always ignored (ensuring that `()` matches C `void`).

if fn_abi.ret.is_ignore() {
if fn_abi.ret.layout.is_repr_c() {
if !fn_abi.ret.layout.has_trivial_abi() {
fn_abi.ret.make_indirect_from_ignore();
}
} else {
Expand All @@ -59,7 +59,7 @@ where

for arg in fn_abi.args.iter_mut() {
if arg.is_ignore() {
if arg.layout.is_repr_c() {
if !arg.layout.has_trivial_abi() {
arg.make_indirect_from_ignore();
}

Expand Down
5 changes: 5 additions & 0 deletions compiler/rustc_ty_utils/src/layout/invariant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ pub(super) fn layout_sanity_check<'tcx>(cx: &LayoutCx<'tcx>, layout: &TyAndLayou
if layout.size.bytes() >= tcx.data_layout.obj_size_bound() {
bug!("size is too large, in the following layout:\n{layout:#?}");
}

if layout.has_trivial_abi() && !layout.is_1zst() {
bug!("non-1-ZST with trivial ABI:\n{layout:#?}")
}

// FIXME(#124403): Once `repr_c_enums_larger_than_int` is a hard error, we could assert
// here that a repr(c) enum discriminant is never larger than a c_int.

Expand Down
Loading
Loading