Skip to content

Document memory layout of vector types - #330

Merged
Shnatsel merged 1 commit into
linebender:mainfrom
danderson:push-sqlkyxpmmnuy
Aug 23, 2026
Merged

Document memory layout of vector types#330
Shnatsel merged 1 commit into
linebender:mainfrom
danderson:push-sqlkyxpmmnuy

Conversation

@danderson

@danderson danderson commented Aug 15, 2026

Copy link
Copy Markdown
Contributor

Fixes #327

@Shnatsel

Copy link
Copy Markdown
Contributor

Thanks! I like the tests. We might even enforce stronger layout guarantees later, since we do align 512-bit vectors to 512 bits on all platforms AFAIK. Doesn't have to happen as part of this PR though.

It would also be nice to document that the layout of masks varies between platforms and shouldn't be relied upon.

I don't recall if our vectors are repr(C) right now, we might want to make them if they aren't (and see if that alters the layout in any way).

Comment thread fearless_simd/src/lib.rs Outdated
//! It benefited from conversations with Luca Versari, though he is not responsible for any of the mistakes or bad decisions.
//!
//! [`pulp`]: https://crates.io/crates/pulp
//! [libm]: https://crates.io/crates/libm

@DJMcNab DJMcNab Aug 15, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Welcome!

For awareness, our docs are intended to be built with all features enabled. If you look at https://docs.rs/fearless_simd/latest/fearless_simd/#feature-flags for example, you will see that this link is resolved.

In the past, we've fixed this with a conditional compilation approach:

https://docs.rs/color/latest/src/color/lib.rs.html#75

I'm not going to die on this hill though.
If you are going to apply this fix as-is, you need to remove the duplicate link in README.md (once you've regenned it)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aah, I see. In that case, since it was a random unrelated change to this PR anyway, I'll just drop out that doc change. I'll send a followup that fixes this with a conditional, and does it properly with cargo-rdme. Thanks for the pointers!

@danderson
danderson force-pushed the push-sqlkyxpmmnuy branch 3 times, most recently from a14bea4 to 337aa26 Compare August 16, 2026 17:47
@danderson

Copy link
Copy Markdown
Contributor Author

I had a brain fart when I first wrote the PR, and got my bits and bytes confused. After looking again: yes, the vector types are all repr(C) already, and their alignment is forced up to their size (so, 512-bit alignment for 512-bit types, even on simd levels that implement them with 128-bit native types).

I've corrected the doc description, and changed the alignment tests to check that align_of >= size_of. If we're comfortable saying that no future supported SIMD ISA will have native types with higher-than-natural alignment (e.g. a 512-bit register type that requires 1024-bit alignment), we could make the alignment check == instead and remove "at least" from the documented promise.

I was going to change the docs of the mask types as well, but it turns out they already document that the layout is opaque and not guaranteed:

The storage representation of this type is intentionally opaque and may vary depending on the SIMD level.

@Shnatsel

Copy link
Copy Markdown
Contributor

If we're comfortable saying that no future supported SIMD ISA will have native types with higher-than-natural alignment (e.g. a 512-bit register type that requires 1024-bit alignment), we could make the alignment check == instead and remove "at least" from the documented promise.

Alignment larger than the size would be wasteful: if the vectors laid out sequentially in memory, in an array, half the memory would just be wasted on padding. So I'm comfortable making a call that this is never going to happen. I've checked and no SIMD architecture or ABI does this today.

@danderson

Copy link
Copy Markdown
Contributor Author

Sounds good. Updated the docs to say that the alignment is exactly size_of bytes, and updated the tests to reflect that.

Fixes linebender#327

Signed-off-by: David Anderson <dave@natulte.net>
danderson added a commit to danderson/fearless_simd that referenced this pull request Aug 17, 2026
…inebender#333)

Checked that `cargo doc` with and without the libm feature builds
without warnings.

The README.md didn't change when regenerated by `cargo rdme`, which
surprised me slightly. But looking at the readme, it has a block of
explicit links outside of the generated section that fixes up libm
references already, so no change needed there.

(extracted from linebender#330 after feedback from @DJMcNab, thank you!)

Signed-off-by: David Anderson <dave@natulte.net>
@Shnatsel
Shnatsel added this pull request to the merge queue Aug 23, 2026
Merged via the queue into linebender:main with commit 8937416 Aug 23, 2026
22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Make explicit (documented) promises about layout of vector types?

3 participants