Skip to content

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

Open
scottmcm wants to merge 1 commit into
rust-lang:mainfrom
scottmcm:scalar-but-packed
Open

Allow BackendRepr::Scalar for repr(Rust, packed) structs [MCP1007]#159033
scottmcm wants to merge 1 commit 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 on lines -2823 to 2824
#[repr(C, packed)]
#[repr(Rust, packed)]
struct Packed<T>(T);

@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.

I'd used C here out of paranoia -- probably from remembering how repr rust unions are useless -- but Rust is fine. The code using this double-checks the properties it cares about with const-asserts too:

const {
assert!(mem::offset_of!(Packed::<T>, 0) == 0);
assert!(size_of::<T>() == size_of::<Packed<T>>());
}

View changes since the review

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What even if this type for? A doc comment would be good.

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.

Removing this from this PR to do it in #159157 first instead.

fn check_layout_abi<'tcx>(cx: &LayoutCx<'tcx>, layout: &TyAndLayout<'tcx>) {
// Verify the ABI-mandated alignment and size for scalars.
let align = layout.backend_repr.scalar_platform_align(cx);
// Verify the size expectation for scalars.

@RalfJung RalfJung Jul 10, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
// Verify the size expectation for scalars.
// Verify the size expectation for scalars.
// Note that there is no alignment requirement, except for the
// implicit requirement that `align <= scalar_size` because otherwise
// the size check would fail due to the padding.

View changes since the review

Comment on lines -2823 to 2824
#[repr(C, packed)]
#[repr(Rust, packed)]
struct Packed<T>(T);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What even if this type for? A doc comment would be good.

@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

rust-bors Bot commented Jul 12, 2026

Copy link
Copy Markdown
Contributor

☔ The latest upstream changes (presumably #159166) made this pull request unmergeable. Please resolve the merge conflicts by rebasing.

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. 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