[Issue 6354] New: Optimizer bug on x86_64: Bitshift optimized out when foreach and scope(failure) are used

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Jul 20 01:08:32 PDT 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 at puremagic.com
        ReportedBy: jmdavisProg at gmx.com


--- Comment #0 from Jonathan M Davis <jmdavisProg at 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 at 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: -------


More information about the Digitalmars-d-bugs mailing list