Skip to content

Implement ptr::{read,write}_unaligned via repr(packed)#158427

Merged
rust-bors[bot] merged 2 commits into
rust-lang:mainfrom
scottmcm:write-unaligned-via-packed
Jul 2, 2026
Merged

Implement ptr::{read,write}_unaligned via repr(packed)#158427
rust-bors[bot] merged 2 commits into
rust-lang:mainfrom
scottmcm:write-unaligned-via-packed

Conversation

@scottmcm

Copy link
Copy Markdown
Member

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 #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

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jun 26, 2026
Comment on lines +25 to +26
// CHECK-NOT: !noundef
// CHECK-NOT: !range

@scottmcm scottmcm Jun 26, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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.

View changes since the review

@scottmcm scottmcm Jun 28, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

...or could also be fixed by rust-lang/compiler-team#1007 and using repr(Rust), if that MCP lands.

@aapoalas aapoalas left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks good to me, seems like a small but nice reduction to MIR heft.

View changes since this review

@aapoalas

aapoalas commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

@bors r+

@rust-bors

rust-bors Bot commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 3a9c74d has been approved by aapoalas

It is now in the queue for this repository.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 1, 2026
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)
@rust-bors rust-bors Bot merged commit 02ee825 into rust-lang:main Jul 2, 2026
13 checks passed
@rustbot rustbot added this to the 1.98.0 milestone Jul 2, 2026
@scottmcm scottmcm deleted the write-unaligned-via-packed branch July 2, 2026 01:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants