Faulty optimisation brings pre-loop into loop body
Hi Alon,
Hope you are well! Someone reported hangs with emscripten-qt when using recent emscripten. I can't offer a reduced test case at this time, but it might be worth your while to build emscripten-qt + "chips" demo (which reproduces the hang) yourself using the instructions in the Wiki ( http://vps2.etotheipiplusone.com:30176/redmine/projects/emscripten-qt/wiki ).
From digging through the code, the problem seems to be with the following code from QPngHandlerPrivate::readPngImage in src/gui/image/qpnghandler.cpp (https://qt.gitorious.org/qt/emscripten-qt/source/863ac718d600c6a7e818bc020c335f1dabc452e3:src/gui/image/qpnghandler.cpp#L544) :
// sanity check palette entries
if (color_type == PNG_COLOR_TYPE_PALETTE
&& outImage->format() == QImage::Format_Indexed8) {
int color_table_size = outImage->colorCount();
for (int y=0; y<(int)height; ++y) {
uchar *p = FAST_SCAN_LINE(data, bpl, y);
uchar *end = p + width;
while (p < end) {
if (*p >= color_table_size)
*p = 0;
++p;
}
}
}
Emscripten appears to me to have attempted incorrectly to remove the "end" variable, by "inlining" it this way:
// sanity check palette entries
if (color_type == PNG_COLOR_TYPE_PALETTE
&& outImage->format() == QImage::Format_Indexed8) {
int color_table_size = outImage->colorCount();
for (int y=0; y<(int)height; ++y) {
uchar *p = FAST_SCAN_LINE(data, bpl, y);
while (p < p + width) {
if (*p >= color_table_size)
*p = 0;
++p;
}
}
}
I'm basing this on the generated code for QPngHandlerPrivate::readPngImage:
https://gist.github.com/anonymous/8397466
Note in particular case 29 which (i think) has removed the "end" temporary variable by "inlining" it, making the condition "p < p + width", which can never be fulfilled.
I hope this is enough for you to go on - if not, please let me know :)
Update: This bug does not occur if I pass the "-O0" flag to emscripten.
I don't have time to try to build that right now, but here is some advice. It is possible you are hitting an LLVM bug. Can you try with the settings mentioned at
https://github.com/kripken/emscripten/wiki/Debugging
- disable llvm opts specifically, disable js opts specifically, etc.?
You can also build with EMCC_DEBUG=2 in the env and send me all the temp files, with a pointer to the problem area, and I can take a look.
Hi Alon,
I tried it with -O2 --lvm-opts 0, and it still hung, so I don't think the problem is with llvm (or at least, not just with it).
I did a git bisect and ended up with 7e179d8c63f43ade6d6345e9bb118e1974a1c992 ("make EMCC_OPTIMIZE_NORMALLY the default"), which wasn't very useful ;) I then repeated, this time with the EMCC_OPTIMIZE_NORMALLY environment variable set, and got the much more promising 2275f51a55a91b4a989a7f044f70144dbee8bab6 ("eliminate into switch cases").
I ran with EMCC_DEBUG=2, and the full set of data is a bit too much for me to upload - is just emcc-2-original.js enough? If so, you can find it here. As expected from the bisection, the "eliminate" pass seems to cause the problem: look out for
if ($p >>> 0 < ($p + (HEAP32[$width >> 2] | 0) | 0) >>> 0)
inside __ZN18QPngHandlerPrivate12readPngImageEP6QImage.
Thanks, that's all I need. The issue was we let variables be eliminated between switch cases, which is fine unless they have a dependency on a non-safe variable, which is the case here with $p. Fixed on incoming, added testcase.