Skip to content

Guarantee layout of homogeneous tuples #220

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
30 changes: 2 additions & 28 deletions reference/src/layout/packed-simd-vectors.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ That is, two distinct `repr(simd)` vector types that have the same `T` and the
same `N` have the same size and alignment.

Vector elements are laid out in source field order, enabling random access to
vector elements by reinterpreting the vector as an array:
vector elements by reinterpreting the vector as an array or as an homogeneous tuple:

```rust,ignore
union U {
vec: Vector<T, N>,
arr: [T; N]
arr: [T; N],
}

assert_eq!(size_of::<Vector<T, N>>(), size_of::<[T; N]>());
Expand All @@ -66,32 +66,6 @@ unsafe {
```

### Unresolved questions

* **Blocked**: Should the layout of packed SIMD vectors be the same as that of
homogeneous tuples ? Such that:

```rust,ignore
union U {
vec: Vector<T, N>,
tup: (T_0, ..., T_(N-1)),
}

assert_eq!(size_of::<Vector<T, N>>(), size_of::<(T_0, ..., T_(N-1))>());
assert!(align_of::<Vector<T, N>>() >= align_of::<(T_0, ..., T_(N-1))>());

unsafe {
let u = U { vec: Vector(t_0, ..., t_(N - 1)) };

assert_eq!(u.vec.0, u.tup.0);
// ...
assert_eq!(u.vec.(N - 1), u.tup.(N - 1));
}
```

This is blocked on the resolution of issue [#36] about the layout of
homogeneous structs and tuples.

[#36]: https://github.com/rust-rfcs/unsafe-code-guidelines/issues/36

* `MaybeUninit<T>` does not have the same `repr` as `T`, so
`MaybeUninit<Vector<T, N>>` are not `repr(simd)`, which has performance
Expand Down
11 changes: 4 additions & 7 deletions reference/src/layout/structs-and-tuples.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ guaranteed layout from a tuple, you are generally advised to create a
named struct with a `#[repr(C)]` annotation (see [the section on
structs for more details](#structs)).

Homogeneous tuples have the same layout as an array `[T; N]` where `N` is the
number of fields in the tuple and the tuple field `i` is located at the same
offset as the `i`-th array element.

Note that the final element of a tuple (`Pn`) is marked as `?Sized` to
permit unsized tuple coercion -- this is implemented on nightly but is
currently unstable ([tracking issue][#42877]). In the future, we may
Expand Down Expand Up @@ -207,13 +211,6 @@ issue has been opened for further discussion on the repository. This
section documents the questions and gives a few light details, but the
reader is referred to the issues for further discussion.

**Homogeneous structs ([#36]).** If you have homogeneous structs, where all
the `N` fields are of a single type `T`, can we guarantee a mapping to
the memory layout of `[T; N]`? How do we map between the field names
and the indices? What about zero-sized types?

[#36]: https://github.com/rust-rfcs/unsafe-code-guidelines/issues/36

**Deterministic layout ([#35]).** Can we say that layout is some deterministic
function of a certain, fixed set of inputs? This would allow you to be
sure that if you do not alter those inputs, your struct layout would
Expand Down