[Issue 16189] Optimizer bug, with simple test case
https://issues.dlang.org/show_bug.cgi?id=16189 --- Comment #8 from github-bugzi...@puremagic.com --- Commits pushed to master at https://github.com/dlang/dmd https://github.com/dlang/dmd/commit/27dd8373a06cafa7e6038bf853cf15ab9575518d fix Issue 16189 - Optimizer bug, with simple test case https://github.com/dlang/dmd/commit/88a963a8ce44d062d45c34677a697ff2bd1fe2fa Merge pull request #8074 from WalterBright/fix16189 fix Issue 16189 - Optimizer bug, with simple test case --
[Issue 16189] Optimizer bug, with simple test case
https://issues.dlang.org/show_bug.cgi?id=16189 github-bugzi...@puremagic.com changed: What|Removed |Added Status|NEW |RESOLVED Resolution|--- |FIXED --
[Issue 16189] Optimizer bug, with simple test case
https://issues.dlang.org/show_bug.cgi?id=16189 --- Comment #7 from Walter Bright--- https://github.com/dlang/dmd/pull/8074 --
[Issue 16189] Optimizer bug, with simple test case
https://issues.dlang.org/show_bug.cgi?id=16189 Walter Brightchanged: What|Removed |Added Hardware|x86_64 |All --
[Issue 16189] Optimizer bug, with simple test case
https://issues.dlang.org/show_bug.cgi?id=16189 Walter Brightchanged: What|Removed |Added Keywords||industry CC||bugzi...@digitalmars.com --
[Issue 16189] Optimizer bug, with simple test case
https://issues.dlang.org/show_bug.cgi?id=16189 bitter.ta...@gmx.com changed: What|Removed |Added CC||bitter.ta...@gmx.com --- Comment #6 from bitter.ta...@gmx.com --- The LICM pass is too eager and given this small snippet ``` void main() { ubyte[9][1] data; size_t a = 0; loop: data[0] = data[a]; a--; bool b = false; if (b) goto loop; assert(a == -1); // Fails with -O } ``` `a' is considered to be as an "index" variable into `data' with starting value 0 and that's decremented until `a < 1', this condition is most likely derived by the bound-checking condition. As a result we get ``` Adding (a(1) = ((_TMP1(3) - #data(0)) / 9LL )) to exit block B4 ``` that doesn't play well with the constant folding pass ``` const prop (_TMP1(3) replaced by #data(0).xfff7) ``` as shown by ``` const prop (a(1) replaced by 2049638230412172400LL ) ``` --
[Issue 16189] Optimizer bug, with simple test case
https://issues.dlang.org/show_bug.cgi?id=16189 Sebchanged: What|Removed |Added CC||greensunn...@gmail.com --- Comment #5 from Seb --- See also the discussion in the forum: https://forum.dlang.org/post/jguphfhffgtdtfitt...@forum.dlang.org My summary: The optimizer seems to be confused and wrongly precomputes a. Note that if you remove e.g. "if (b) goto loop;", a will be correctly statically set in the printf too: --- mov RSI,0h; look ma - a is now -1 lea RDI,FLAT:.rodata[00h][RIP] xor EAX,EAX ; set eax to 0 call printf@PLT32 --- For comparison the assembly of -O. Here with printf and a different value to be compared with a because it's easier to read: --- main: pushRBP mov RBP,RSP sub RSP,010h lea RAX,-010h[RBP] xor ECX,ECX mov [RAX],RCX mov 8[RAX],CL lea RSI,-010h[RBP] lea RDI,-010h[RBP] movsd movsb mov RSI,01C71C71C71C71C70h ; 2 function argument (value of a) lea RDI,FLAT:.rodata[00h][RIP] ; "%d" (1st function argument) xor EAX,EAX ; set eax to 0 call printf@PLT32 ; printf("%d", a) mov EDX,0Ch lea RSI,_TMP0@PC32[RIP] ; load function arguments (in reverse order) lea RDI,_TMP0@PC32[RIP] call __assert@PLT32; values load, let's call assert add [RAX],AL .text.main ends end --
[Issue 16189] Optimizer bug, with simple test case
https://issues.dlang.org/show_bug.cgi?id=16189 --- Comment #4 from Kirill Kryukov--- I thought this might be a regression, so I tested dmd versions down to 2.050. But no, the bug is there in each and every one of them. --
[Issue 16189] Optimizer bug, with simple test case
https://issues.dlang.org/show_bug.cgi?id=16189 Ketmar Darkchanged: What|Removed |Added CC||ket...@ketmar.no-ip.org --- Comment #3 from Ketmar Dark --- funny thing: adding `asm { nop; }` anywhere in `main` seems to turn the optimizer off, and the code works again. a hackish way to control optimizer on per-function basis. ;-) --
[Issue 16189] Optimizer bug, with simple test case
https://issues.dlang.org/show_bug.cgi?id=16189 --- Comment #2 from Kirill Kryukov--- A possible workaround: change "a--;" into "{ auto a2 = a - 1; a = a2; }". (This is NOT to suggest that the bug does not need fixing, as it's annoying as hell that even simplest C-like code does not work correctly.) As for previous reduction - it hurts my eyes to see size_t (unsigned type) compared for equality with -1, so I suggest to at least use ptrdiff_t. --
[Issue 16189] Optimizer bug, with simple test case
https://issues.dlang.org/show_bug.cgi?id=16189 ag0ae...@gmail.com changed: What|Removed |Added Keywords||wrong-code CC||ag0ae...@gmail.com OS|Windows |All --- Comment #1 from ag0ae...@gmail.com --- Slightly more reduced: void main() { ubyte[9][1] data; size_t a = 0; loop: data[0] = data[a]; a--; bool b = false; if (b) goto loop; assert(a == -1); // Fails with -O } Also happens on Linux. --
[Issue 16189] Optimizer bug, with simple test case
https://issues.dlang.org/show_bug.cgi?id=16189 Kirill Kryukovchanged: What|Removed |Added CC||kkryu...@gmail.com --