Jump to page: 1 2
Thread overview
[Issue 16189] Optimizer bug, with simple test case
Jun 21, 2016
Kirill Kryukov
Jun 21, 2016
ag0aep6g@gmail.com
Jun 22, 2016
Kirill Kryukov
Jun 23, 2016
Ketmar Dark
Aug 02, 2016
Kirill Kryukov
Feb 07, 2018
Seb
Mar 23, 2018
Walter Bright
Mar 24, 2018
Walter Bright
Mar 24, 2018
Walter Bright
June 21, 2016
https://issues.dlang.org/show_bug.cgi?id=16189

Kirill Kryukov <kkryukov@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |kkryukov@gmail.com

--
June 21, 2016
https://issues.dlang.org/show_bug.cgi?id=16189

ag0aep6g@gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |wrong-code
                 CC|                            |ag0aep6g@gmail.com
                 OS|Windows                     |All

--- Comment #1 from ag0aep6g@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.

--
June 22, 2016
https://issues.dlang.org/show_bug.cgi?id=16189

--- Comment #2 from Kirill Kryukov <kkryukov@gmail.com> ---
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.

--
June 23, 2016
https://issues.dlang.org/show_bug.cgi?id=16189

Ketmar Dark <ketmar@ketmar.no-ip.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |ketmar@ketmar.no-ip.org

--- Comment #3 from Ketmar Dark <ketmar@ketmar.no-ip.org> ---
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. ;-)

--
August 02, 2016
https://issues.dlang.org/show_bug.cgi?id=16189

--- Comment #4 from Kirill Kryukov <kkryukov@gmail.com> ---
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.

--
February 07, 2018
https://issues.dlang.org/show_bug.cgi?id=16189

Seb <greensunny12@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |greensunny12@gmail.com

--- Comment #5 from Seb <greensunny12@gmail.com> ---
See also the discussion in the forum: https://forum.dlang.org/post/jguphfhffgtdtfittkyw@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,0FFFFFFFFh                ; 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:
                push    RBP
                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
----

--
February 20, 2018
https://issues.dlang.org/show_bug.cgi?id=16189

bitter.taste@gmx.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bitter.taste@gmx.com

--- Comment #6 from bitter.taste@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).xfffffffffffffff7)
```

as shown by

```
const prop (a(1) replaced by 2049638230412172400LL )
```

--
March 23, 2018
https://issues.dlang.org/show_bug.cgi?id=16189

Walter Bright <bugzilla@digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |industry
                 CC|                            |bugzilla@digitalmars.com

--
March 24, 2018
https://issues.dlang.org/show_bug.cgi?id=16189

--- Comment #7 from Walter Bright <bugzilla@digitalmars.com> ---
https://github.com/dlang/dmd/pull/8074

--
March 24, 2018
https://issues.dlang.org/show_bug.cgi?id=16189

Walter Bright <bugzilla@digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Hardware|x86_64                      |All

--
« First   ‹ Prev
1 2