Implement ptr::{read,write}_unaligned via repr(packed)#158427
Merged
rust-bors[bot] merged 2 commits intoJul 2, 2026
Conversation
scottmcm
commented
Jun 26, 2026
Comment on lines
+25
to
+26
| // CHECK-NOT: !noundef | ||
| // CHECK-NOT: !range |
Member
Author
There was a problem hiding this comment.
Obviously it would be better to have metadata on these loads, but they're not there in nightly today either (https://rust.godbolt.org/z/753TYoa1Y) so this isn't a regression.
Should be better thanks to mir opts in future, though, without needing additional library changes.
Member
Author
There was a problem hiding this comment.
...or could also be fixed by rust-lang/compiler-team#1007 and using repr(Rust), if that MCP lands.
aapoalas
approved these changes
Jul 1, 2026
Contributor
|
@bors r+ |
Contributor
JonathanBrouwer
added a commit
to JonathanBrouwer/rust
that referenced
this pull request
Jul 1, 2026
…d, r=aapoalas
Implement `ptr::{read,write}_unaligned` via `repr(packed)`
We already support doing unaligned reads & writes via `repr(packed)` fields, so this just uses that support from the backend rather than needing to thinks about `memcpy` and `intrinsics::forget` and such.
Turns out in codegen this ends up essentially identical because the packed type read gets `BackendRepr::Memory` and thus the read/write of the packed type *is* an `align 1` memcpy, just like the library code before this PR. But doing this gives much simpler MIR and rust-lang#158291 will make it better than the previous version by not needing to emit the `memcpy` at all for things like `read_unaligned::<u32>`.
First commit are some tests that pass on master; Second commit changes the implementation and shows the corresponding test changes.
r? libs
rust-bors Bot
pushed a commit
that referenced
this pull request
Jul 1, 2026
…uwer Rollup of 6 pull requests Successful merges: - #156737 (Implement `DoubleEndedIterator::next_chunk_back`) - #158147 (std: fix stack buffer overflow in Windows junction_point) - #158180 (std: use `OnceLock` for SGX environment variable storage) - #158427 (Implement `ptr::{read,write}_unaligned` via `repr(packed)`) - #158531 (Change `adjust_ident_and_get_scope` arg to `LocalDefId`) - #158364 (rustc_target/asm: add LoongArch LSX/LASX inline asm register support)
rust-bors Bot
pushed a commit
that referenced
this pull request
Jul 1, 2026
Rollup of 8 pull requests Successful merges: - #156737 (Implement `DoubleEndedIterator::next_chunk_back`) - #158180 (std: use `OnceLock` for SGX environment variable storage) - #158427 (Implement `ptr::{read,write}_unaligned` via `repr(packed)`) - #158531 (Change `adjust_ident_and_get_scope` arg to `LocalDefId`) - #158574 (Clarify ExitStatusExt documentation) - #158334 (rustdoc: Show use-site paths for unevaluated const array lengths) - #158364 (rustc_target/asm: add LoongArch LSX/LASX inline asm register support) - #158667 (rustc_sanitizers: use twox-hash without default features)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
We already support doing unaligned reads & writes via
repr(packed)fields, so this just uses that support from the backend rather than needing to thinks aboutmemcpyandintrinsics::forgetand such.Turns out in codegen this ends up essentially identical because the packed type read gets
BackendRepr::Memoryand thus the read/write of the packed type is analign 1memcpy, just like the library code before this PR. But doing this gives much simpler MIR and #158291 will make it better than the previous version by not needing to emit thememcpyat all for things likeread_unaligned::<u32>.First commit are some tests that pass on master; Second commit changes the implementation and shows the corresponding test changes.
r? libs