Skip to content

Allow BackendRepr::Scalar for repr(Rust, packed) structs [MCP1007]#159033

Open
scottmcm wants to merge 2 commits into
rust-lang:mainfrom
scottmcm:scalar-but-packed
Open

Allow BackendRepr::Scalar for repr(Rust, packed) structs [MCP1007]#159033
scottmcm wants to merge 2 commits into
rust-lang:mainfrom
scottmcm:scalar-but-packed

Conversation

@scottmcm

@scottmcm scottmcm commented Jul 9, 2026

Copy link
Copy Markdown
Member

Tracking issue: #158985

This only changes Scalar (not ScalarPair nor SimdVector) because I wanted to do this in small bites. It also preserves the requirement that there can't be padding, so won't apply for align(MORE_THAN_SIZE) either. I also didn't touch unions.

Note that after #158666 there are zero mentions of default_align in codegen, so this "just working" in codegen without any changes is IMHO expected. Codegen has long been expected to always use PlaceValue::align instead of anything from the layout when dealing with alignment.

r? workingjubilee

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

rustbot commented Jul 9, 2026

Copy link
Copy Markdown
Collaborator

workingjubilee is currently at their maximum review capacity.
They may take a while to respond.

Comment on lines +24 to +29
pub unsafe fn write_packed_scalar(ptr: *mut PackedScalar, val: PackedScalar) {
// CHECK: start
// CHECK-NEXT: store i64 %val, ptr %ptr, align 2
// CHECK-NEXT: ret void
*ptr = val;
}

@scottmcm scottmcm Jul 9, 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.

Compare what we emit today: https://rust.godbolt.org/z/8Pv61EhbK

This one, for example, emits 2 allocas and 2 memcpys without this PR:

define void @write_packed_scalar(ptr noundef %ptr, i64 noundef %0) unnamed_addr {
start:
  %1 = alloca [8 x i8], align 8
  %val = alloca [8 x i8], align 2
  call void @llvm.lifetime.start.p0(ptr %1)
  store i64 %0, ptr %1, align 8
  call void @llvm.memcpy.p0.p0.i64(ptr align 2 %val, ptr align 8 %1, i64 8, i1 false)
  call void @llvm.lifetime.end.p0(ptr %1)
  call void @llvm.memcpy.p0.p0.i64(ptr align 2 %ptr, ptr align 2 %val, i64 8, i1 false)
  ret void
}

(which LLVM of course cleans up even in -O1, but still nice to just not emit that in the first place.)

View changes since the review

Comment thread library/core/src/ptr/mod.rs Outdated
Comment thread compiler/rustc_ty_utils/src/layout/invariant.rs
Comment thread library/core/src/ptr/mod.rs Outdated
@scottmcm scottmcm 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 Jul 11, 2026
jhpratt added a commit to jhpratt/rust that referenced this pull request Jul 12, 2026
…uct, r=joshtriplett

Better comment the struct used by `{read,write}_unaligned`

To address rust-lang#159033 (comment)

r? @RalfJung
@rust-bors

This comment has been minimized.

rust-timer added a commit that referenced this pull request Jul 12, 2026
Rollup merge of #159157 - scottmcm:clearer-ptr-unaligned-struct, r=joshtriplett

Better comment the struct used by `{read,write}_unaligned`

To address #159033 (comment)

r? @RalfJung
@scottmcm
scottmcm force-pushed the scalar-but-packed branch from 44a579c to 2c6804e Compare July 13, 2026 04:14
@rustbot

This comment has been minimized.

@scottmcm scottmcm removed the T-libs Relevant to the library team, which will review and decide on the PR/issue. label Jul 13, 2026
@scottmcm
scottmcm force-pushed the scalar-but-packed branch from 2c6804e to de892dd Compare July 13, 2026 04:36
@scottmcm scottmcm added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jul 13, 2026
Comment thread compiler/rustc_ty_utils/src/layout/invariant.rs Outdated
@scottmcm
scottmcm force-pushed the scalar-but-packed branch from de892dd to 2c6804e Compare July 13, 2026 14:21
@scottmcm
scottmcm force-pushed the scalar-but-packed branch from 59fd279 to a1f6260 Compare July 19, 2026 19:43
@rustbot

rustbot commented Jul 19, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants