Skip to content

Safe transmute_copy()#232

Merged
Shnatsel merged 9 commits into
linebender:mainfrom
Shnatsel:bytemuck-at-home
May 25, 2026
Merged

Safe transmute_copy()#232
Shnatsel merged 9 commits into
linebender:mainfrom
Shnatsel:bytemuck-at-home

Conversation

@Shnatsel
Copy link
Copy Markdown
Contributor

"we have bytemuck at home"

These are the transmute_copy() safety improvements from #231 taken one step further: on top of checking the sizes match at compile time and that both types are Copy, we also add a marker trait to types that are safe to transmute and require it in checked_transmute_copy(), making that function fully safe to call.

The design follows the bytemuck crate closely. We could just use bytemuck, but it would pull in syn and other proc macro machinery, and we would still have to write the impl_aligned_simd_pod! macro because bytemuck rightfully refuses to derive Pod on generic wrappers like Aligned128<T> because they may introduce padding depending on the T. So the amount of code it saves us is minimal.

Rebasing #231 on top of this would be hellish, but a merge commit probably wouldn't be too bad.

@Shnatsel Shnatsel requested a review from LaurenzV May 25, 2026 13:27
@LaurenzV
Copy link
Copy Markdown
Collaborator

Thanks, not expecting you to rebase, merge commit is fine!

Copy link
Copy Markdown
Collaborator

@LaurenzV LaurenzV left a comment

Choose a reason for hiding this comment

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

LGTM, also nice so see the unsafe being more reduced to a single module.

We actually did use bytemuck initially, but got rid of it precisely to avoid pulling in bytemuck, with the argument that fearless_simd is low-level enough that we should just do our own thing here. It's a trade-off, but I think it's good to avoid extra dependencies here.

Comment thread fearless_simd/src/lib.rs Outdated
#![cfg_attr(not(test), warn(unused_crate_dependencies))]
// These lints shouldn't apply to examples.
#![warn(clippy::print_stdout, clippy::print_stderr)]
#![cfg_attr(not(test), deny(clippy::disallowed_methods))]
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Let's move this below // END LINEBENDER LINT SET since it's not part of the set to my understanding?

Comment thread fearless_simd/src/transmute.rs
Aligned512<[u32; 16]>,
);

#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Could you add a comment that the const is just to avoid repeating the cfg, that confused me a bit.

Comment on lines -380 to -383
// - A `#[repr(simd)]` type, which has the same layout as an array of scalars
// - An array of `#[repr(simd)]` types
// - For AArch64 specifically, a `#[repr(C)]` tuple of `#[repr(simd)]` types
//
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I think it would be valuable to retain those comments for the SimdPod implementation of the types.

@Shnatsel Shnatsel enabled auto-merge May 25, 2026 14:35
@Shnatsel Shnatsel added this pull request to the merge queue May 25, 2026
@github-merge-queue github-merge-queue Bot removed this pull request from the merge queue due to failed status checks May 25, 2026
@Shnatsel
Copy link
Copy Markdown
Contributor Author

transient DNS resolution error, retrying

@Shnatsel Shnatsel added this pull request to the merge queue May 25, 2026
Merged via the queue into linebender:main with commit 6d59cb3 May 25, 2026
22 checks passed
@Shnatsel Shnatsel deleted the bytemuck-at-home branch May 25, 2026 14:55
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.

2 participants