← Back to index
|
Original Bugzilla link
Bug 21476 – [codegen] 64 bit C ABI not followed when returning struct with 3 floats
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2020-12-12T14:32:13Z
Last change time
2020-12-13T22:11:00Z
Keywords
backend, wrong-code
Assigned to
No Owner
Creator
MrSmith33
Comments
Comment #0
by mrsmith33 — 2020-12-12T14:32:13Z
struct vec3 { float x; float y; float z; } extern(C) vec3 pass_func(vec3 v) { // dmd returns `v` on stack, while C ABI requires returning in xmm0 and xmm1 return v; } void caller() { // same here. Value is loaded from stack instead of from xmm0 + xmm1 vec3 r = pass_func(vec3(1, 2, 3)); } Code for pass_func dmd ``` push rbp mov rbp,rsp sub rsp,0x18 mov QWORD PTR [rbp-0x18],rdi movsd QWORD PTR [rbp-0x10],xmm0 movss DWORD PTR [rbp-0x8],xmm1 lea rsi,[rbp-0x10] mov rdi,QWORD PTR [rbp-0x18] movs QWORD PTR es:[rdi],QWORD PTR ds:[rsi] movs BYTE PTR es:[rdi],BYTE PTR ds:[rsi] movs BYTE PTR es:[rdi],BYTE PTR ds:[rsi] movs BYTE PTR es:[rdi],BYTE PTR ds:[rsi] movs BYTE PTR es:[rdi],BYTE PTR ds:[rsi] mov rax,QWORD PTR [rbp-0x18] leave ret ``` ldc ``` push rbp mov rbp, rsp movsd qword ptr [rbp - 16], xmm0 movss dword ptr [rbp - 8], xmm1 mov eax, dword ptr [rbp - 8] mov dword ptr [rbp - 24], eax mov rcx, qword ptr [rbp - 16] mov qword ptr [rbp - 32], rcx movsd xmm0, qword ptr [rbp - 32] movss xmm1, dword ptr [rbp - 24] pop rbp ret ``` Code for caller: dmd ``` movsd xmm1,QWORD PTR [rbp-0x70] fstp QWORD PTR [rbp-0x70] movsd xmm0,QWORD PTR [rbp-0x70] lea rdi,[rbp-0x48] call rax lea rdx,[rip+0x9558c] mov edi,0x2 mov rsi,rdx fld QWORD PTR [rbp-0x48] fld QWORD PTR [rbp-0x40] ``` ldc ``` mov rax, qword ptr [rbp - 8] mov dword ptr [rbp - 40], 1065353216 mov dword ptr [rbp - 36], 1073741824 mov dword ptr [rbp - 32], 1077936128 mov ecx, dword ptr [rbp - 32] mov dword ptr [rbp - 48], ecx mov rdx, qword ptr [rbp - 40] mov qword ptr [rbp - 56], rdx movss xmm1, dword ptr [rbp - 48] movsd xmm0, qword ptr [rbp - 56] call rax movss dword ptr [rbp - 64], xmm1 movsd qword ptr [rbp - 72], xmm0 ```
Comment #1
by mrsmith33 — 2020-12-13T21:04:08Z
This happened with 2.092.1. On 2.094.2 there is no bug. Just in case. 2.092.1 didn't handle those correctly too: struct vec5 { i32 x; i32 y; f32 z; f32 w; } struct vec6 { f32 x; f32 y; i32 z; i32 w; }
Comment #2
by kinke — 2020-12-13T22:11:00Z
Let's close it then, as this and similar issues have been fixed by
https://github.com/dlang/dmd/pull/10200.