Bug 6354 – Optimizer bug on x86_64: Bitshift optimized out when foreach and scope(failure) are used
Status
RESOLVED
Resolution
FIXED
Severity
critical
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2011-07-20T01:03:00Z
Last change time
2015-06-09T05:15:14Z
Keywords
wrong-code
Assigned to
nobody
Creator
issues.dlang
Comments
Comment #0 by issues.dlang — 2011-07-20T01:03:12Z
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.