[Issue 6505] Wrong code for expression involving 8 floats, only with -O
https://issues.dlang.org/show_bug.cgi?id=6505 Andrei Alexandrescu changed: What|Removed |Added Version|D1 & D2 |D2 --
[Issue 6505] Wrong code for expression involving 8 floats, only with -O
http://d.puremagic.com/issues/show_bug.cgi?id=6505 Walter Bright changed: What|Removed |Added Status|NEW |RESOLVED CC||bugzi...@digitalmars.com Resolution||FIXED --- Comment #4 from Walter Bright 2011-08-23 15:23:43 PDT --- https://github.com/D-Programming-Language/dmd/commit/628640f45bbc2bed3e84e835e19cf03ee2c4eafe https://github.com/D-Programming-Language/dmd/commit/a92f6bf42e547c6978e4e317b4feb2c6610cf518 -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email --- You are receiving this mail because: ---
[Issue 6505] Wrong code for expression involving 8 floats, only with -O
http://d.puremagic.com/issues/show_bug.cgi?id=6505 --- Comment #3 from Don 2011-08-23 12:19:44 PDT --- Here's the problem. The scheduler keeps track of the number of used x87 registers via the "fpustackused" variable. Each instruction has a "fpuadjust" value which says how many x87 registers it uses. The problem is CALL instructions. They have fpuadjust = 0. But, a function returning float returns it in ST(0) --- so fpuadjust should be +1. And if it's a complex result, fpuadjust should be +2. I think it should be possible to put an assert in cgsched.c, line 2491: if (tbl[j]) { fpu += tbl[j]->fpuadjust; + assert(fpu >= 0); if (fpu >= 8) // if FPU stack overflow but this would fail at the moment, because the total goes negative after a call followed by an FSTP. So it needs to distinguish between each type of call, depending on how it affects the FPU stack. I think the bug lies in funccall() in cod1.c; it should be setting something in 'code' to record how many x87 registers are returned. Then, there'll be another minor problem: the number of FPU values CAN reach 8. This is because of the code in push87() in cg87.c, which does fdecstp; fstp; when the stack is full. So the condition above will need to change to: if (fpu > 8) // if FPU stack overflow This is too invasive a change for me to make and test. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email --- You are receiving this mail because: ---
[Issue 6505] Wrong code for expression involving 8 floats, only with -O
http://d.puremagic.com/issues/show_bug.cgi?id=6505 --- Comment #2 from Don 2011-08-23 01:52:36 PDT --- Thanks bearophile, that reduction is very helpful. The code loads 8 values on the x87 stack. The eighth one would cause an overflow, so in cg87.c, push87(), it inserts: fdecstp; fstp [EBP - 24]; The next line is: fld [EBP - 18]; The code passes to the scheduler, where there's a bug: it converts the code to: fdecstp; fld [EBP - 18]; fxch ST(1); fstp [EBP - 24]; This is an x87 stack overflow, since the load happens before space has been made in the save. The x87 returns a NaN. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email --- You are receiving this mail because: ---
[Issue 6505] Wrong code for expression involving 8 floats, only with -O
http://d.puremagic.com/issues/show_bug.cgi?id=6505 bearophile_h...@eml.cc changed: What|Removed |Added CC||bearophile_h...@eml.cc --- Comment #1 from bearophile_h...@eml.cc 2011-08-16 06:37:50 PDT --- Simplified a little: double foo() { return 1.0; } void main() { double a = foo(); double b = foo(); double x = a*a + a*a + a*a + a*a + a*a + a*a + a*a + a*b + a*b; assert(x > 0); } --- Asm normal compilation: _D4test3fooFZdcomdat fld1 ret __Dmaincomdat L0:enter024h,0 callnear ptr _D4test3fooFZd fstpqword ptr -018h[EBP] callnear ptr _D4test3fooFZd fstpqword ptr -010h[EBP] fldqword ptr -018h[EBP] fmulqword ptr -010h[EBP] fldqword ptr -018h[EBP] fmulqword ptr -010h[EBP] fldqword ptr -018h[EBP] fmulqword ptr -018h[EBP] fldqword ptr -018h[EBP] fmulqword ptr -018h[EBP] fldqword ptr -018h[EBP] fmulqword ptr -018h[EBP] fldqword ptr -018h[EBP] fmulqword ptr -018h[EBP] fldqword ptr -018h[EBP] fmulqword ptr -018h[EBP] fldqword ptr -018h[EBP] fmulqword ptr -018h[EBP] fdecstp fstpqword ptr -024h[EBP] fldqword ptr -018h[EBP] fmulqword ptr -018h[EBP] faddpST(1),ST faddpST(1),ST faddpST(1),ST faddpST(1),ST faddpST(1),ST faddpST(1),ST faddpST(1),ST fldqword ptr -024h[EBP] faddpST(1),ST fstpqword ptr -8[EBP] fldqword ptr -8[EBP] ftst fstswAX sahf fstpST jaL7B movEAX,9 callnear ptr _D4test8__assertFiZv L7B:xorEAX,EAX leave ret Asm compilation with -O: _D4test3fooFZdcomdat fldqword ptr FLAT:_DATA[00h] ret __Dmaincomdat L0:subESP,034h callnear ptr _D4test3fooFZd fstpqword ptr 0Ch[ESP] callnear ptr _D4test3fooFZd fldqword ptr 0Ch[ESP] fldqword ptr 0Ch[ESP] fxchST2 fstpqword ptr 014h[ESP] fmulqword ptr 014h[ESP] fxchST1 fldqword ptr 0Ch[ESP] fxchST1 fmulqword ptr 014h[ESP] fxchST1 fmulST,ST(0) fldqword ptr 0Ch[ESP] fmulST,ST(0) fldqword ptr 0Ch[ESP] fmulST,ST(0) fldqword ptr 0Ch[ESP] fmulST,ST(0) fldqword ptr 0Ch[ESP] fmulST,ST(0) fldqword ptr 0Ch[ESP] fmulST,ST(0) fdecstp fldqword ptr 0Ch[ESP] fxchST1 fstpqword ptr [ESP] fmulST,ST(0) faddpST(1),ST faddpST(1),ST faddpST(1),ST faddpST(1),ST faddpST(1),ST faddpST(1),ST faddpST(1),ST fldqword ptr [ESP] faddpST(1),ST ftst fstswAX fstpST sahf jaL84 movEAX,9 callnear ptr _D4test8__assertFiZv L84addESP,034h xorEAX,EAX ret -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email --- You are receiving this mail because: ---