Skip to content

ty: Use underlying layout of typedefs if available. - #3453

Merged
emilio merged 1 commit into
mainfrom
over-aligned-typedef
Sep 6, 2026
Merged

ty: Use underlying layout of typedefs if available.#3453
emilio merged 1 commit into
mainfrom
over-aligned-typedef

Conversation

@emilio

@emilio emilio commented Sep 6, 2026

Copy link
Copy Markdown
Contributor

This fixes struct layout issues with over-aligned typedefs, which rust can't represent, see #3449 for context and some other discussion.

This is kinda ugly tho, but as far as I can tell there's no good way of getting the right ABI and struct layout at the same time...

@emilio

emilio commented Sep 6, 2026

Copy link
Copy Markdown
Contributor Author

cc @ojeda

This fixes struct layout issues with over-aligned typedefs, which
rust can't represent, see #3449 for context and some other discussion.

This is kinda ugly tho, but as far as I can tell there's no good way of
getting the right ABI and struct layout at the same time...
@emilio
emilio force-pushed the over-aligned-typedef branch from 5e6308c to 447f5f4 Compare September 6, 2026 19:38
@emilio
emilio added this pull request to the merge queue Sep 6, 2026
Merged via the queue into main with commit efb6e0c Sep 6, 2026
51 checks passed
@emilio
emilio deleted the over-aligned-typedef branch September 6, 2026 20:59
@ojeda

ojeda commented Sep 6, 2026

Copy link
Copy Markdown
Contributor

A few more cases for your tests/future considerations:

struct Inner { long long a, b; };
typedef struct Inner AlignedInner __attribute__((aligned(16)));

struct Outer {
    long long before;
    AlignedInner inner[1];
};

(and the same with --opaque-type AlignedInner).

struct Inner { long long a, b; };
typedef struct Inner AlignedInner __attribute__((aligned(16)));
struct Outer {
    long long before;
    AlignedInner inner[0];
    char tail;
};
enum __attribute__((aligned(16))) AlignedEnum { Value = 1 };
struct Outer {
    long long before;
    enum AlignedEnum inner;
};
typedef unsigned int U __attribute__((aligned(2)));
struct Outer {
    char before;
    U bits : 31;
    char mid;
    U inner;
    char tail;
};

All these changed w.r.t. 0.72.1.

I hope that helps & thanks!

@emilio

emilio commented Sep 7, 2026

Copy link
Copy Markdown
Contributor Author

@ojeda just to check, is any of those actively blocking the kernel?

Those hit issues that are all pre-existing afaict: Support for aligned enums (which can't on the default enum representation, because we just typedef to the underlying type, ugh), and same for arrays.

These are all fixable, but rather hackily (this was already a bit of a hack). Not impossible tho.

@emilio

emilio commented Sep 7, 2026

Copy link
Copy Markdown
Contributor Author

It's a bit sad that even on the rustified enum style, #[repr(u32, align(16))] would generate different behavior from clang :(

emilio added a commit that referenced this pull request Sep 7, 2026
This fixes most of the test-cases discussed in #3453.
emilio added a commit that referenced this pull request Sep 7, 2026
This fixes most of the test-cases discussed in #3453.

The remaining one is related to bitfields and needs a bit more thought
(I think we currently don't pad bitfields based on the clang-reported
offset and maybe should).
emilio added a commit that referenced this pull request Sep 7, 2026
This fixes most of the test-cases discussed in #3453.

The remaining one is related to bitfields and needs a bit more thought
(I think we currently don't pad bitfields based on the clang-reported
offset and maybe should).
@ojeda

ojeda commented Sep 7, 2026

Copy link
Copy Markdown
Contributor

@ojeda just to check, is any of those actively blocking the kernel?

Not that I have seen reported yet, but it is hard to say -- that is why I was trying to look at it from the perspective of differences w.r.t. 0.72.1, since that is where we have confidence, especially for cases that were matching C in the past (the cases I pasted above, except the last one, matched Clang in 0.72.1 as far as I can see, so they could have broken someone).

emilio added a commit that referenced this pull request Sep 7, 2026
This fixes the bitfield cases discussed in #3453.
@emilio

emilio commented Sep 7, 2026

Copy link
Copy Markdown
Contributor Author

Ok, I came up with a fix for the bitfield stuff that isn't super complicated, so I'll land a fix for that too before doing a dot release.

PhilipTaronQ pushed a commit to Qumulo/rust-bindgen that referenced this pull request Sep 7, 2026
This fixes most of the test-cases discussed in rust-lang#3453.

The remaining one is related to bitfields and needs a bit more thought
(I think we currently don't pad bitfields based on the clang-reported
offset and maybe should).
PhilipTaronQ pushed a commit to Qumulo/rust-bindgen that referenced this pull request Sep 7, 2026
This fixes the bitfield cases discussed in rust-lang#3453.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants