emscripten icon indicating copy to clipboard operation
emscripten copied to clipboard

ASM_CONSTS mismatch when building two wasm modules: one with -msimd128 and one without

Open andyb1979 opened this issue 1 month ago • 2 comments

Hi all,

I'm attempting to build a wasm module with Emscripten once with -msimd128 -msse2 and have a fallback without. This uses the makeflags

ifeq ($(P_SIMD),1)
LINKFLAGS += -msimd128 -msse2 // later passed to emcc
COMPILEFLAGS += -msimd128 -msse2 // later passed to emcc 
endif

Here are our complete emcc flags sent to the compiler:

// SIMD version
emcc   -s WASM=1 --bind -s STACK_SIZE=8388608 -s GL_WORKAROUND_SAFARI_GETCONTEXT_BUG=0 -s NO_DYNAMIC_EXECUTION=1 --lembind --closure 0 --no-heap-copy -s USE_SDL=2 -s FETCH=1 -s ALLOW_MEMORY_GROWTH=1 -s MAX_WEBGL_VERSION=2 -Oz -msimd128 -msse2 -s MODULARIZE=1 -s NODEJS_CATCH_REJECTION=0 -s ENVIRONMENT=web -s EXPORTED_FUNCTIONS="['_main', '_setThrew', '_malloc', '_free']" -s EXPORTED_RUNTIME_METHODS="['ccall', 'cwrap','lengthBytesUTF8','stringToUTF8','UTF8ToString','callMain']" @allobjfiles2d.list obj/GL2_SIMD/Release/Core.Cpp/src/module.o   -o module.js --post-js postJsOverrides.js
// Non-SIMD version
emcc   -s WASM=1 --bind -s STACK_SIZE=8388608 -s GL_WORKAROUND_SAFARI_GETCONTEXT_BUG=0 -s NO_DYNAMIC_EXECUTION=1 --lembind --closure 0 --no-heap-copy -s USE_SDL=2 -s FETCH=1 -s ALLOW_MEMORY_GROWTH=1 -s MAX_WEBGL_VERSION=2 -Oz -s MODULA
RIZE=1 -s NODEJS_CATCH_REJECTION=0 -s ENVIRONMENT=web -s EXPORTED_FUNCTIONS="['_main', '_setThrew', '_malloc', '_free']" -s EXPORTED_RUNTIME_
METHODS="['ccall', 'cwrap','lengthBytesUTF8','stringToUTF8','UTF8ToString','callMain']" @allobjfiles2d.list obj/GL2_NOSIMD/Release/Core.Cpp/src/module.o   -o module-nosimd.js --post-js postJsOverrides.js

This outputs two modules: module.wasm and module-nosimd.wasm

Two JS glue codes are generated: module.js and module-nosimd.js

One problem, I only want to include one set of glue code as it will bloat the final bundle size. However there are minor differences in ASM_CONSTS between the two versions:

Image

This is causing the runtime initialization to fail when loading one wasm or another with the error

   at runEmAsmFunction (module.js:9:141722)
   at _emscripten_asm_const_int (module.js:9:141756)
   at module-nosimd.wasm:0xb24e2 ...

Question: Is there a way to unify the ASM_CONSTS generated in the two builds to ensure that I can use one module glue code for the two separate wasms (-nosimd and simd variants)?

Thank you in advance,

andyb1979 avatar Oct 23 '25 10:10 andyb1979

Sadly its not possible today to use the JS glue code generated for one Wasm file with another. The JS has a lot of baked in assumptions about the specific Wasm file it was built for.

We have has some place in the past about some kind of universal JS loader, but it has not materialized. The ASM_CONST list is one example of where specific data addresses are hardcoded into the generated JS, meaning there is no way it can work with more than one specific wasm file.

sbc100 avatar Oct 23 '25 15:10 sbc100

I managed to work around this by excluding EM_ASM calls from c++ and passing required values into cpp from js, but it’s a fragile solution.

It would be great if we could provide a lookup table or similar for ASM Consts generated

andyb1979 avatar Oct 23 '25 20:10 andyb1979