Repository navigation
SysV x86_64 C ABI: classify packed structs at their packed offsets (#1195 follow-up) - #1369
Merged
Merged
Conversation
…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)
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
force-pushed
the
fix-sysv-packed-c-struct
branch
from
September 23, 2026 01:35
41184d6 to
676eb6a
Compare
ehartford
marked this pull request as ready for review
September 23, 2026 01:35
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Follow-up to #1195 / #1202. The linux-x86_64
release-raylib-spiral-uatstill 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.wc_abi_sysv_classify, lineif wl_is_packed_struct(ty): return -1. c_import emits@[packed]for every struct whose C alignment is 1 (src/CImport.wis_really_packed=clang_Type_getAlignOf == 1), so raylib'sColoris<{ 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-packedColor, 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:movq %r14,%rdi(sret) /movl %ebp,%esi(x) /movl %r15d,%edx(y) /callq get_colormovl %ebp,%edi(x) /movl %r14d,%esi(y) /callq get_colorFix: 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.wnow 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.