Skip to content

SysV x86_64 C ABI: classify packed structs at their packed offsets (#1195 follow-up) - #1369

Merged
ehartford merged 1 commit into
mainfrom
fix-sysv-packed-c-struct
Sep 23, 2026
Merged

ehartford merged 1 commit into
mainfrom
fix-sysv-packed-c-struct

Conversation

@ehartford

Copy link
Copy Markdown
Contributor

Follow-up to #1195 / #1202. The linux-x86_64 release-raylib-spiral-uat still failed on the nightly after #1202 merged (run 35616828206, head 395f302): GetImageColor(image, x, y) read x as a constant stack address (-342673224), and y was the caller's x.

Root cause: src/Codegen.w c_abi_sysv_classify, line if wl_is_packed_struct(ty): return -1. c_import emits @[packed] for every struct whose C alignment is 1 (src/CImport.w is_really_packed = clang_Type_getAlignOf == 1), so raylib's Color is <{ i8, i8, i8, i8 }>. The classifier refused every packed struct, so Color stayed a first-class aggregate, and LLVM demoted the return to a hidden sret pointer in rdi. #1195's repro used a hand-written, non-packed Color, so it could not catch this.

Evidence: the #1195 repro with @[packed] on Color, cross-emitted with --target x86_64-unknown-linux-gnu --emit-obj:

  • before: movq %r14,%rdi (sret) / movl %ebp,%esi (x) / movl %r15d,%edx (y) / callq get_color
  • after: movl %ebp,%edi (x) / movl %r14d,%esi (y) / callq get_color

Fix: classify a packed struct at its packed offsets. A field whose offset its alignment does not divide makes the object MEMORY (SysV §3.2.3), so it is passed byval and returned through sret. behav_c_abi_small_structs.w now pins clang's lowering for three packed shapes on linux_x86_64: <{i8 x4}> → i32, packed {i32,u8} → i40, packed {u8,i32} → sret + byval. The test fails on the installed compiler and passes on stage1 from this branch. Only SysV x86_64 changes. AAPCS64 and Microsoft x64 classify by size and were already right, which is why the spiral passes on linux-aarch64.

Verified so far (iterate tier): with check src/main.w, build :dev, the targeted test. The battery has not run yet; this PR stays a draft until it does.

…1195 follow-up)

c_import emits `@[packed]` for every struct whose C alignment is 1, so
raylib's `Color { unsigned char r, g, b, a }` lowers to `<{ i8, i8, i8, i8 }>`.
`c_abi_sysv_classify` returned -1 for any packed struct, so Color stayed a
first-class LLVM aggregate. LLVM demoted the return to a hidden sret pointer
in rdi and shifted every argument. The linux-x86_64 release-raylib-spiral-uat
still failed after #1202 (nightly 35616828206: `GetImageColor` read
x = -342673224, and y = the caller's x). The hand-written extern repro in
#1195 used a non-packed Color, so it did not catch this.

Proof: the #1195 repro with `@[packed]` on Color, cross-emitted with
`--target x86_64-unknown-linux-gnu --emit-obj`, before the fix:
  movq %r14, %rdi ; movl %ebp, %esi ; movl %r15d, %edx ; callq get_color
and after the fix: movl %ebp, %edi ; movl %r14d, %esi ; callq get_color.

A packed struct is now classified at its packed offsets. A field whose offset
its alignment does not divide makes the object MEMORY (§3.2.3), which means
sret/byval. Clang agrees on all three shapes the test now pins:
<{i8 x4}> -> i32, packed {i32, u8} -> i40, packed {u8, i32} -> sret + byval.
Only SysV x86_64 changes. AAPCS64 and Microsoft x64 classify by size and
were already right, which is why the spiral passes on linux-aarch64.

(cherry picked from commit 41184d6)
@ehartford

Copy link
Copy Markdown
Contributor Author

Battery (seed-driven, alone as an ABI change, on main dd786a4 → 676eb6a, force-pushed so the PR is the tested tree): build ok, :fixpoint ok, :test ok (no reds), :test-green ok, :last-green ok. The x86_64 behavior itself is exercised only on the linux-x86_64 lane / host; this battery proves no regression on darwin-aarch64.

@ehartford
ehartford force-pushed the fix-sysv-packed-c-struct branch from 41184d6 to 676eb6a Compare September 23, 2026 01:35
@ehartford
ehartford marked this pull request as ready for review September 23, 2026 01:35
@ehartford
ehartford merged commit 1adb672 into main Sep 23, 2026
@ehartford
ehartford deleted the fix-sysv-packed-c-struct branch September 24, 2026 16:56
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.

1 participant