Bug 24884 – backend generates wrong 32-bit code after inlining math with double[4]

Status
RESOLVED
Resolution
FIXED
Severity
major
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2024-11-26T12:15:10Z
Last change time
2024-11-29T13:18:46Z
Keywords
industry, pull, wrong-code
Assigned to
No Owner
Creator
Dennis

Comments

Comment #0 by dkorpel — 2024-11-26T12:15:10Z
On Windows, with -m32 -inline this code triggers an Access Violation: ```D void vecdiff(ref double[4] a, ref double[4] b, ref double[4] result) { result[0] = a[0] - b[0]; result[1] = a[1] - b[1]; result[2] = a[2] - b[2]; result[3] = a[3]; } pragma(inline, false) double norm(ref double[4] a) => 0; pragma(inline, false) void outerproduct(ref double[4] a, ref double[4] b, ref double[4] c) {} pragma(inline, false) double innerproduct(ref double[4] a, ref double[4] b) => 0; pragma(inline, false) void inlinebug(ref double[4] point1, ref double[4] point2, ref double[4] point3, ref double[4] abcd) { double[4] v1 = 0.0; double[4] v2 = 0.0; vecdiff(point1, point2, v1); vecdiff(point2, point3, v2); outerproduct(v1, v2, abcd); if (norm(abcd) > 0.0) { abcd[0] = innerproduct(abcd, point1); } } void main() { double[4] a = 0.0; inlinebug(a, a, a, a); } ``` Disassembly with -vasm: ``` _D5testi9inlinebugFKG4dKQeKQhKQkZv: 0000: 83 EC 48 sub ESP,048h 0003: 8B 54 24 50 mov EDX,050h[ESP] 0007: B9 08 00 00 00 mov ECX,8 000c: 89 1C 24 mov [ESP],EBX 000f: 89 C3 mov EBX,EAX 0011: 31 C0 xor EAX,EAX 0013: 89 7C 24 04 mov 4[ESP],EDI 0017: 8D 7C 24 08 lea EDI,8[ESP] 001b: F3 rep 001c: AB stosd 001d: 8B 44 24 4C mov EAX,04Ch[ESP] 0021: 31 C0 xor EAX,EAX 0023: 8D 7C 24 28 lea EDI,028h[ESP] 0027: B9 08 00 00 00 mov ECX,8 002c: F3 rep 002d: AB stosd 002e: 8B 7C 24 54 mov EDI,054h[ESP] 0032: DD 07 fnld qword ptr [EDI] 0034: DC 22 fnsub qword ptr [EDX] 0036: DD 5C 24 08 fnstp qword ptr 8[ESP] 003a: DD 47 08 fnld qword ptr 8[EDI] 003d: DC 62 08 fnsub qword ptr 8[EDX] 0040: DD 5C 24 10 fnstp qword ptr 010h[ESP] 0044: DD 47 10 fnld qword ptr 010h[EDI] 0047: DC 62 10 fnsub qword ptr 010h[EDX] 004a: DD 5C 24 18 fnstp qword ptr 018h[ESP] 004e: DD 47 18 fnld qword ptr 018h[EDI] 0051: DD 5C 24 20 fnstp qword ptr 020h[ESP] 0055: DD 02 fnld qword ptr [EDX] 0057: DC 20 fnsub qword ptr [EAX] <= Access Violation ``` The EAX register is 0 at that point.
Comment #1 by dkorpel — 2024-11-26T12:33:24Z
Reduced a bit further: ``` pragma(inline, false) bool norm(int a) => 0; pragma(inline, false) void inlinebug(ref double[4] point1, ref double[4] point2, ref double[4] point3, ref double[4] abcd) { double[4] v1 = 0.0; double[4] v2 = 0.0; v1[0] = point1[0] - point2[0]; v1[1] = point1[1] - point2[1]; v1[2] = point1[2] - point2[2]; v1[3] = point1[3]; v2[0] = point2[0] - point3[0]; v2[1] = point2[1] - point3[1]; v2[2] = point2[2] - point3[2]; int p = cast(int) &abcd; int q = cast(int) &point1; abcd[0] = norm(p) + p; abcd[1] = q + p; } void main() { double[4] a = 0.0; inlinebug(a, a, a, a); } ```
Comment #2 by dlang-bot — 2024-11-26T15:05:29Z
@dkorpel created dlang/dmd pull request #17097 "Fix bugzilla 24884 - backend generates wrong 32-bit code after inlini…" fixing this issue: - Fix bugzilla 24884 - backend generates wrong 32-bit code after inlining math with double[4] https://github.com/dlang/dmd/pull/17097
Comment #3 by dlang-bot — 2024-11-29T07:35:23Z
dlang/dmd pull request #17097 "Fix bugzilla 24884 - backend generates wrong 32-bit code after inlini…" was merged into stable: - 394719e097d1c758ad252b0f71200714e02142fe by Dennis Korpel: Fix bugzilla 24884 - backend generates wrong 32-bit code after inlining math with double[4] https://github.com/dlang/dmd/pull/17097
Comment #4 by dlang-bot — 2024-11-29T13:18:46Z
dlang/dmd pull request #17098 "Merge stable" was merged into master: - 8b1eb80d25d2945aecab11852895fd8af57b70c1 by Dennis Korpel: Fix bugzilla 24884 - backend generates wrong 32-bit code after inlining math with double[4] https://github.com/dlang/dmd/pull/17098