emscripten-core / emscripten

Emscripten: An LLVM-to-WebAssembly Compiler
Other
25.62k stars 3.28k forks source link

Loop optimization error with SIMD instructions #5406

Closed bruceceng closed 5 years ago

bruceceng commented 7 years ago

I am experimenting with SIMD intrinsics and running into a compiler issue.

The following program successfully compiles with this command:

emcc simd-test.c -msse2 -s WASM=1 -o simd-test.html -O1

#include <xmmintrin.h>
#include <emscripten/emscripten.h>
int main(int argc, char ** argv) {
    __m128 SSE_accum = _mm_set_ps(0,100,200,300);
    __m128 SSE_one = _mm_set_ps(1,1,1,1);
    for (int i=0; i<74; i++) {
        SSE_accum = _mm_add_ps(SSE_accum, SSE_one);
    }   
    float result [4];
    _mm_store_ps (result, SSE_accum);
    printf("SIMD Result: %f, %f, %f, %f\n", result[0], result[1], result[2], result[3]);
}

However if I change the loop to for (int i=0; i<75; i++) or anything higher than 75, then when I compile I get the following compile error:

build executables save the before js process input run polyfill packer on fib1.js.temp2.js [MYPATH].emscripten_cache\pack-asmjs.js:warning: Successfully compiled asm.js code (total compilation time 276ms; caching disabled by missing command-line arguments) uncaught exception: Assertion failed: dot.base.as().name.equals("Math"), at: C:\Program Files\Emscripten\emscripten\1.35.0\third_party\wasm-polyfill\src\pack-asmjs.cpp,1327,analyze_import at jsStackTrace ...

(This goes on for a while).

I assume it has to do with some attempt to optimize the bigger loop which is only triggered above 74 iterations.

bruceceng commented 7 years ago

Apparently with loops less than 74, it was actually evaluating the entire loop and optimizing out any SIMD instructions. When there were SIMD instructions it successfully compiled to asm.js but failed to created webassembly.

kripken commented 7 years ago

In general, wasm doesn't have simd support yet, so things probably won't work (unless it can lower it to non-simd instructions).

stale[bot] commented 5 years ago

This issue has been automatically marked as stale because there has been no activity in the past 2 years. It will be closed automatically if no further activity occurs in the next 7 days. Feel free to re-open at any time if this issue is still relevant.