Skip to content

Preserve metadata like !range and !nonnull in unaligned operations#158202

Closed
scottmcm wants to merge 2 commits into
rust-lang:mainfrom
scottmcm:better-read-unaligned
Closed

Preserve metadata like !range and !nonnull in unaligned operations#158202
scottmcm wants to merge 2 commits into
rust-lang:mainfrom
scottmcm:better-read-unaligned

Conversation

@scottmcm

Copy link
Copy Markdown
Member

Today, unaligned loads lose their !range metadata in codegen: https://rust.godbolt.org/z/rjdq51o7E

That's because it goes through copy_nonoverlapping::<u8>, it seems, which is an untyped copy so that's what the load ends up being.

I started fixing that in codegen, adding an intrinsic with fallback mir since it'd be easy to emit in cg_ssa by setting the https://doc.rust-lang.org/nightly/nightly-rustc/rustc_codegen_ssa/mir/place/struct.PlaceValue.html#structfield.align to Align::ONE.

But then I realized that we have a way to do that in native Rust: repr(packed).

Unfortunately just doing it in pure rust in the library isn't enough because reading a repr(packed) type again emits a llvm.memcpy, leading to the same problem. (This is because all packed types end up with BackendRepr::Memory https://rust.godbolt.org/z/37dee8dqr it looks like.)

But that's ok, since it's still possible to do this in MIR that all the backends already need to handle 🎉

cc @RalfJung who I'm confident will have opinions about these new intrinsics

@rustbot

rustbot commented Jun 21, 2026

Copy link
Copy Markdown
Collaborator

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

Some changes occurred to the intrinsics. Make sure the CTFE / Miri interpreter
gets adapted for the changes, if necessary.

cc @rust-lang/miri, @RalfJung, @oli-obk, @lcnr

⚠️ #[rustc_intrinsic_const_stable_indirect] controls whether intrinsics can be exposed to stable const
code; adding it needs t-lang approval.

cc @rust-lang/wg-const-eval

@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 21, 2026
@rustbot

rustbot commented Jun 21, 2026

Copy link
Copy Markdown
Collaborator

r? @dingxiangfei2009

rustbot has assigned @dingxiangfei2009.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 73 candidates
  • Random selection from 19 candidates

@rust-log-analyzer

This comment has been minimized.

@scottmcm scottmcm force-pushed the better-read-unaligned branch from 0201cff to 2bb0b89 Compare June 21, 2026 07:26
@rust-log-analyzer

This comment has been minimized.

@scottmcm scottmcm force-pushed the better-read-unaligned branch from 2bb0b89 to e1341ac Compare June 21, 2026 17:24
@scottmcm

scottmcm commented Jun 21, 2026

Copy link
Copy Markdown
Member Author

You know what, thinking about this today, I might be able to just do this with a MIR-opt instead.

The problem I hit was basically

_3 = Packed(_2);
*_1 = move _3;

and I can probably GVN that to

(*_1).0 = _2;

instead of needing the intrinsic to do it...

@rustbot author


EDIT: well, probably not GVN actually, but maybe SRoA?

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 21, 2026
@scottmcm

Copy link
Copy Markdown
Member Author

First step of the new approach is in #158291

Pulled the test out into #158341

@scottmcm scottmcm closed this Jun 24, 2026
@rustbot rustbot removed the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Jun 24, 2026
@scottmcm scottmcm deleted the better-read-unaligned branch June 24, 2026 04:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

4 participants