Repository navigation
Pass and return small C structs the way the target C ABI does (#1195) - #1202
Merged
Merged
Conversation
compute_fn_abi coerced a struct of at most 16 bytes to its register shape
only on darwin-arm64 and windows-x86_64. Everywhere else it stayed an LLVM
first-class aggregate, which is not the C ABI: on linux-x86_64 LLVM could
not return { i8, i8, i8, i8 } in registers and demoted it to a hidden
pointer, so `Color GetImageColor(Image, int, int)` received the return
slot's address as `x` and `x` as `y` (release UAT run 35388740596:
"Requested image pixel (1523262040, 304) out of bounds"; the cross-compiled
object shows `leaq 0x18(%rsp), %rdi; movl $7, %esi; movl $9, %edx`).
- System V x86_64 (Linux, macOS): classify each eightbyte INTEGER or SSE
(§3.2.3) and coerce to iN / float / double, or a pair of them. A struct
that does not fit in the registers still free goes to the stack whole.
- aarch64: the existing rule is AAPCS64's, not Darwin's; it now applies to
Linux and Windows too, and a non-HFA composite with float members goes in
general registers like any other.
- windows-x86_64: 1, 2, 4 and 8 byte structs go in one integer register
whatever their members are ({ float, float } was returned through a
hidden pointer).
Expected signatures in the test are clang's (our own build) for the same C
declarations on each target.
Fixes #1195
ehartford
pushed a commit
that referenced
this pull request
Sep 23, 2026
…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
added a commit
that referenced
this pull request
Sep 23, 2026
…1195 follow-up) (#1369) 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) Co-authored-by: Eric Hartford <[email protected]>
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.
Fixes #1195 — the linux-x86_64 raylib-spiral release UAT cell ("Requested image pixel (1523262040, 304) out of bounds").
Cause
compute_fn_abicoerced a C struct of at most 16 bytes to its register shape only on darwin-arm64 and windows-x86_64. Elsewhere it stayed an LLVM first-class aggregate, which is not the C ABI. On linux-x86_64 LLVM cannot return{ i8, i8, i8, i8 }in registers and demotes it to a hidden pointer, soColor GetImageColor(Image, int, int)got the return slot's address asxandxasy.Cross-compiled object, before:
leaq 0x18(%rsp), %rdi; movl $7, %esi; movl $9, %edx. After:movl $7, %edi; movl $9, %esi, result read fromeax.Change
iN/float/doubleor a pair. A struct that does not fit in the registers still free goes to the stack whole (byval).{ i32, f32 }) goes in a general register (wasw0+s0, wrong on darwin too).{ float, float }was returned through a hidden pointer).Ground truth is our own clang: 10 struct shapes x 5 targets. With's declarations match it, with two register-equivalent spellings (
doublefor clang's<2 x float>;{ double, i64 }for{ double, i32 }, the tail being padding).test/behavior/behav_c_abi_small_structs.wasserts the signatures for linux_x86_64, windows_x86_64, linux_aarch64, darwin_aarch64, windows_aarch64.Battery (seed-driven, ABI change alone in its batch)
build 284s, fixpoint ok, test-with-audits ok (977s), move-audit ok, test-green, last-green.
Workarounds noted in the diff for two bugs found on the way: #1196 (inferred return type invisible before the definition), #1197 (
for x in xsover&[T]).