Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emscripten SIMD intrinsics `invalid conversion` error

Tags:

emscripten

According to https://emscripten.org/docs/porting/simd.html, GCC/Clang SIMD Vector Extensions can be used. However, I can't compile the following:

#include <emmintrin.h>
#include <stdint.h>

int stub_sse(void) {
    __m128i v1 = _mm_set1_epi32(42);
    __m128i v2 = _mm_set1_epi32(86);
    union { __m128i v; int32_t x[4]; } v3;
    v3.v = _mm_add_epi32(v1, v2);
    return (int) v3.x[0];
}

int main(void) { if (stub_sse() != 128) return 1; else return 0; }

Running emcc -msimd128 simd.c gives lots of errors like

/emsdk/upstream/lib/clang/10.0.0/include/mmintrin.h:525:12: error:  
invalid conversion between vector type '__m64' (vector of 1 'long long' value)  
and integer type 'int' of different size
    return (__m64)__builtin_ia32_psubw((__v4hi)__m1, (__v4hi)__m2);
           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
fatal error: too many errors emitted, stopping now [-ferror-limit=]
20 errors generated.

I am using emcc upstream 1.39.1, clang 10.0.0, gcc 9.2.0 on Linux. Am I missing something?

like image 694
Eric Avatar asked Mar 09 '26 14:03

Eric


1 Answers

The docs that mention the GCC/Clang SIMD Vector Extensions link here: https://gcc.gnu.org/onlinedocs/gcc/Vector-Extensions.html. Those docs explain how to use __attribute__((vector_size(16))) to name vector types and explain the operations you can use to work with those types. Those operations are generally the same as operations you can use on normal scalar types such as +, -, * as well as logical operations and subscripting ([]). Notably, using these extensions does not require including any headers or calling any special functions such as _mm_set1_epi32.

Your code tries to use emmintrin.h, which is a platform-specific SIMD intrinsics header for x86. If you look inside emmintrin.h you will see that it implements types like __m128i and functions like _mm_set1_epi32 in terms of the GCC/Clang SIMD Vector Extensions, but the header is not itself part of the vector extensions. The only SIMD intrinsics header currently available when using Emscripten is wasm_simd128.h.

like image 94
tlively Avatar answered Mar 16 '26 10:03

tlively