Thread overview
[Issue 6354] New: Optimizer bug on x86_64: Bitshift optimized out when foreach and scope(failure) are used
Jul 20, 2011
Jonathan M Davis
Aug 01, 2011
Brad Roberts
Nov 30, 2011
Walter Bright
July 20, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=6354

           Summary: Optimizer bug on x86_64: Bitshift optimized out when
                    foreach and scope(failure) are used
           Product: D
           Version: unspecified
          Platform: x86_64
        OS/Version: Linux
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: jmdavisProg@gmx.com


--- Comment #0 from Jonathan M Davis <jmdavisProg@gmx.com> 2011-07-20 01:03:12 PDT ---
Okay. This is a weird one, but it only happens on x86_64 when compiling with -O, so presumably it's a bug in the optmizer for x86_64. This code

import std.stdio;

ushort swapEndian(ushort val)
{
    return ((val & 0xff00U) >> 8) |
           ((val & 0x00ffU) << 8);
}

void main()
{
    foreach(j; 0 .. 2)
    {
        scope(failure) writefln("j: %s", j);

        ushort left = 0xffU;
        left <<= (ushort.sizeof - 1) * 8;
        ushort right = 0xffU;

        writefln("%s %s %s %s", swapEndian(left), right, swapEndian(right),
left);
        assert(swapEndian(left) == right);
    }
}

does this:

255 65280 255 65280
j: 0
core.exception.AssertError@q(25): Assertion failure
----------------
----------------

Thee writefln at the bottom can be removed, but it helps show what's going on, since the correct output for this program would be

255 255 65280 65280
255 255 65280 65280

While the program does fail on the first iteration, removing the loop makes it
so that it succeeds, so the foreach somehow helps cause the bug. Removing the
scope(failure) also helps contribute, since removing _it_ gets rid of the bug.
However, it fails regardless of whether it's a scope(failure), scope(success),
or scope(exit). It also fails regardless of what's in the scope statement (e.g.
it fails with scope(failure) int i;). You can also get rid of swapEndian and
replace the call to it with its body and have the failure occur

assert((((left & 0xff00U) >> 8) | ((left & 0x00ffU) << 8)) == right);

so the function call isn't part of the problem (but it's easier to read with swapEndian in there, so I left in there). I can narrow it down to this at it still fails

void main()
{
    foreach(j; 0 .. 2)
    {
        scope(failure) int i = 0;

        ushort left = 0xffU;
        left <<= (ushort.sizeof - 1) * 8;

        assert((((left & 0xff00U) >> 8) | ((left & 0x00ffU) << 8)) == 0xffu);
    }
}

However, sometimes, some combination of changes in between those two states succeeds - probably depending on what the optimizer decides that it can optimize out.

In any case, it seems that something is causing the optimizer to think that it can optimize out the bitshifts. And it's something that's going to cause test failures if the swapEndian stuff that I'm currently working on for Phobos gets merged in, so it would be nice if it could be fixed soon.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 01, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=6354


Brad Roberts <braddr@puremagic.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |wrong-code
                 CC|                            |braddr@puremagic.com
           Severity|normal                      |critical


-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
November 30, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=6354


Walter Bright <bugzilla@digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |bugzilla@digitalmars.com
         Resolution|                            |FIXED


--- Comment #1 from Walter Bright <bugzilla@digitalmars.com> 2011-11-30 13:43:39 PST ---
https://github.com/D-Programming-Language/dmd/commit/c468e89347d2f7306a64bfaa46e46fc3bf96b612

https://github.com/D-Programming-Language/dmd/commit/062351c39c6337eb09338a209d8e945c489852de

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
December 27, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=6354



--- Comment #2 from github-bugzilla@puremagic.com 2012-12-27 14:58:21 PST ---
Commits pushed to master at https://github.com/D-Programming-Language/phobos

https://github.com/D-Programming-Language/phobos/commit/3bb400d4920a3d611fbe8b7b2500d815eb69d13e Decommented unittest that was blocked by bug 6354, which has since been fixed.

https://github.com/D-Programming-Language/phobos/commit/901500b1605209638c4deadc38b2d7236be06880 Merge pull request #1039 from mnembrini/decommentbug

Decommented unittest that was blocked by bug 6354, which has since been ...

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------