emscripten-core / emscripten-core/emscripten
Too many local (according to nodejs) in generated Wasm module
- Dominant language
- C++
- Stars
- 27.6k
- Forks
- 3.6k
- Avg merge
- 1d 1h
- Merged PRs (30d)
- 105
Description
I have some C code generated through the [CertiCoq project](https://github.com/CertiCoq/certicoq), specifically a verified implementation of sha256.
Emscripten produces a Wasm module that contains a function that fails when trying to run the module via the generated js scaffolding and nodejs (v.20.12.2).
I have attached a zip containing the C files needed for running the `emcc` command:
[sha_fast.zip](https://github.com/emscripten-core/emscripten/files/15148012/sha_fast.zip)
Error message:
```
failed to asynchronously prepare wasm: CompileError: WebAssembly.instantiate(): Compiling function #141:"body" failed: local count too large @+339007
Aborted(CompileError: WebAssembly.instantiate(): Compiling function #141:"body" failed: local count too large @+339007)
/home/martin/Documents/sha_fast/sha_fast-opt_O0.js:132
throw ex;
^
RuntimeError: Aborted(CompileError: WebAssembly.instantiate(): Compiling function #141:"body" failed: local count too large @+339007)
at abort (/home/martin/Documents/sha_fast/sha_fast-opt_O0.js:593:11)
at /home/martin/Documents/sha_fast/sha_fast-opt_O0.js:707:5
Node.js v20.12.2
```
I haven't tested other Wasm runtimes (so disregard this if it is a nodejs related issue).
If i add the `-O1` optimization flag, the module instantiates and runs, but the behavior (printed result) differs from that of the x86-binary, similar to [this issue](https://github.com/emscripten-core/emscripten/issues/21846) (let me know if you need the details for this specific instance of the difference in behavior)
Emscripten Version
```
emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.58 (a41843e0860e52c948c1fce20307933c6631c800)
clang version 19.0.0git (https:/github.com/llvm/llvm-project 0a8cd1ed1f4f35905df318015b0dbcb69d81d7c2)
Target: wasm32-unknown-emscripten
Thread model: posix
```
Full command:
```
emcc -v -O0 -o sha_fast-opt_O0.js -w -Wno-everything -I./ sha_fast_main_wasm.c gc_stack.c CertiCoq.Benchmarks.tests.sha_fast.c glue_sha_fast.c -sALLOW_MEMORY_GROWTH=1 -sSTACK_SIZE=1000000 --profiling
```
Output:
```
/home/martin/code/emsdk/upstream/bin/clang -target wasm32-unknown-emscripten -fignore-exceptions -fvisibility=default -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr --sysroot=/home/martin/code/emsdk/upstream/emscripten/cache/sysroot -DEMSCRIPTEN -Werror=implicit-function-declaration -Xclang -iwithsysroot/include/fakesdl -Xclang -iwithsysroot/include/compat -v -O0 -w -Wno-everything -I./ sha_fast_main_wasm.c -c -o /tmp/emscripten_temp_g7hbpg60/sha_fast_main_wasm_0.o
clang version 19.0.0git (https:/github.com/llvm/llvm-project 0a8cd1ed1f4f35905df318015b0dbcb69d81d7c2)
Target: wasm32-unknown-emscripten
Thread model: posix
InstalledDir: /home/martin/code/emsdk/upstream/bin
(in-process)
"/home/martin/code/emsdk/upstream/bin/clang-19" -cc1 -triple wasm32-unknown-emscripten -emit-obj -mrelax-all -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name sha_fast_main_wasm.c -mrelocation-model static -mframe-pointer=none -ffp-contract=on -fno-rounding-math -mconstructor-aliases -target-cpu generic -debugger-tuning=gdb -fdebug-compilation-dir=/home/martin/Documents/sha_fast -v -fcoverage-compilation-dir=/home/martin/Documents/sha_fast -resource-dir /home/martin/code/emsdk/upstream/lib/clang/19 -D EMSCRIPTEN -I ./ -isysroot /home/martin/code/emsdk/upstream/emscripten/cache/sysroot -internal-isystem /home/martin/code/emsdk/upstream/lib/clang/19/include -internal-isystem /home/martin/code/emsdk/upstream/emscripten/cache/sysroot/include/wasm32-emscripten -internal-isystem /home/martin/code/emsdk/upstream/emscripten/cache/sysroot/include -O0 -Werror=implicit-function-declaration -Wno-everything -w -ferror-limit 19 -fvisibility=default -fgnuc-version=4.2.1 -fskip-odr-check-in-gmf -fignore-exceptions -fcolor-diagnostics -iwithsysroot/include/fakesdl -iwithsysroot/include/compat -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr -o /tmp/emscripten_temp_g7hbpg60/sha_fast_main_wasm_0.o -x c sha_fast_main_wasm.c
clang -cc1 version 19.0.0git based upon LLVM 19.0.0git default target x86_64-unknown-linux-gnu
ignoring nonexistent directory "/home/martin/code/emsdk/upstream/emscripten/cache/sysroot/include/wasm32-emscripten"
#include "..." search starts here:
#include <...> search starts here:
.
/home/martin/code/emsdk/upstream/emscripten/cache/sysroot/include/fakesdl
/home/martin/code/emsdk/upstream/emscripten/cache/sysroot/include/compat
/home/martin/code/emsdk/upstream/lib/clang/19/include
/home/martin/code/emsdk/upstream/emscripten/cache/sysroot/include
End of search list.
/home/martin/code/emsdk/upstream/bin/clang -target wasm32-unknown-emscripten -fignore-exceptions -fvisibility=default -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr --sysroot=/home/martin/code/emsdk/upstream/emscripten/cache/sysroot -DEMSCRIPTEN -Werror=implicit-function-declaration -Xclang -iwithsysroot/include/fakesdl -Xclang -iwithsysroot/include/compat -v -O0 -w -Wno-everything -I./ gc_stack.c -c -o /tmp/emscripten_temp_g7hbpg60/gc_stack_1.o
clang version 19.0.0git (https:/github.com/llvm/llvm-project 0a8cd1ed1f4f35905df318015b0dbcb69d81d7c2)
Target: wasm32-unknown-emscripten
Thread model: posix
InstalledDir: /home/martin/code/emsdk/upstream/bin
(in-process)
"/home/martin/code/emsdk/upstream/bin/clang-19" -cc1 -triple wasm32-unknown-emscripten -emit-obj -mrelax-all -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name gc_stack.c -mrelocation-model static -mframe-pointer=none -ffp-contract=on -fno-rounding-math -mconstructor-aliases -target-cpu generic -debugger-tuning=gdb -fdebug-compilation-dir=/home/martin/Documents/sha_fast -v -fcoverage-compilation-dir=/home/martin/Documents/sha_fast -resource-dir /home/martin/code/emsdk/upstream/lib/clang/19 -D EMSCRIPTEN -I ./ -isysroot /home/martin/code/emsdk/upstream/emscripten/cache/sysroot -internal-isystem /home/martin/code/emsdk/upstream/lib/clang/19/include -internal-isystem /home/martin/code/emsdk/upstream/emscripten/cache/sysroot/include/wasm32-emscripten -internal-isystem /home/martin/code/emsdk/upstream/emscripten/cache/sysroot/include -O0 -Werror=implicit-function-declaration -Wno-everything -w -ferror-limit 19 -fvisibility=default -fgnuc-version=4.2.1 -fskip-odr-check-in-gmf -fignore-exceptions -fcolor-diagnostics -iwithsysroot/include/fakesdl -iwithsysroot/include/compat -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr -o /tmp/emscripten_temp_g7hbpg60/gc_stack_1.o -x c gc_stack.c
clang -cc1 version 19.0.0git based upon LLVM 19.0.0git default target x86_64-unknown-linux-gnu
ignoring nonexistent directory "/home/martin/code/emsdk/upstream/emscripten/cache/sysroot/include/wasm32-emscripten"
#include "..." search starts here:
#include <...> search starts here:
.
/home/martin/code/emsdk/upstream/emscripten/cache/sysroot/include/fakesdl
/home/martin/code/emsdk/upstream/emscripten/cache/sysroot/include/compat
/home/martin/code/emsdk/upstream/lib/clang/19/include
/home/martin/code/emsdk/upstream/emscripten/cache/sysroot/include
End of search list.
/home/martin/code/emsdk/upstream/bin/clang -target wasm32-unknown-emscripten -fignore-exceptions -fvisibility=default -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr --sysroot=/home/martin/code/emsdk/upstream/emscripten/cache/sysroot -DEMSCRIPTEN -Werror=implicit-function-declaration -Xclang -iwithsysroot/include/fakesdl -Xclang -iwithsysroot/include/compat -v -O0 -w -Wno-everything -I./ CertiCoq.Benchmarks.tests.sha_fast.c -c -o /tmp/emscripten_temp_g7hbpg60/CertiCoq.Benchmarks.tests.sha_fast_2.o
clang version 19.0.0git (https:/github.com/llvm/llvm-project 0a8cd1ed1f4f35905df318015b0dbcb69d81d7c2)
Target: wasm32-unknown-emscripten
Thread model: posix
InstalledDir: /home/martin/code/emsdk/upstream/bin
(in-process)
"/home/martin/code/emsdk/upstream/bin/clang-19" -cc1 -triple wasm32-unknown-emscripten -emit-obj -mrelax-all -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name CertiCoq.Benchmarks.tests.sha_fast.c -mrelocation-model static -mframe-pointer=none -ffp-contract=on -fno-rounding-math -mconstructor-aliases -target-cpu generic -debugger-tuning=gdb -fdebug-compilation-dir=/home/martin/Documents/sha_fast -v -fcoverage-compilation-dir=/home/martin/Documents/sha_fast -resource-dir /home/martin/code/emsdk/upstream/lib/clang/19 -D EMSCRIPTEN -I ./ -isysroot /home/martin/code/emsdk/upstream/emscripten/cache/sysroot -internal-isystem /home/martin/code/emsdk/upstream/lib/clang/19/include -internal-isystem /home/martin/code/emsdk/upstream/emscripten/cache/sysroot/include/wasm32-emscripten -internal-isystem /home/martin/code/emsdk/upstream/emscripten/cache/sysroot/include -O0 -Werror=implicit-function-declaration -Wno-everything -w -ferror-limit 19 -fvisibility=default -fgnuc-version=4.2.1 -fskip-odr-check-in-gmf -fignore-exceptions -fcolor-diagnostics -iwithsysroot/include/fakesdl -iwithsysroot/include/compat -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr -o /tmp/emscripten_temp_g7hbpg60/CertiCoq.Benchmarks.tests.sha_fast_2.o -x c CertiCoq.Benchmarks.tests.sha_fast.c
clang -cc1 version 19.0.0git based upon LLVM 19.0.0git default target x86_64-unknown-linux-gnu
ignoring nonexistent directory "/home/martin/code/emsdk/upstream/emscripten/cache/sysroot/include/wasm32-emscripten"
#include "..." search starts here:
#include <...> search starts here:
.
/home/martin/code/emsdk/upstream/emscripten/cache/sysroot/include/fakesdl
/home/martin/code/emsdk/upstream/emscripten/cache/sysroot/include/compat
/home/martin/code/emsdk/upstream/lib/clang/19/include
/home/martin/code/emsdk/upstream/emscripten/cache/sysroot/include
End of search list.
/home/martin/code/emsdk/upstream/bin/clang -target wasm32-unknown-emscripten -fignore-exceptions -fvisibility=default -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr --sysroot=/home/martin/code/emsdk/upstream/emscripten/cache/sysroot -DEMSCRIPTEN -Werror=implicit-function-declaration -Xclang -iwithsysroot/include/fakesdl -Xclang -iwithsysroot/include/compat -v -O0 -w -Wno-everything -I./ glue_sha_fast.c -c -o /tmp/emscripten_temp_g7hbpg60/glue_sha_fast_3.o
clang version 19.0.0git (https:/github.com/llvm/llvm-project 0a8cd1ed1f4f35905df318015b0dbcb69d81d7c2)
Target: wasm32-unknown-emscripten
Thread model: posix
InstalledDir: /home/martin/code/emsdk/upstream/bin
(in-process)
"/home/martin/code/emsdk/upstream/bin/clang-19" -cc1 -triple wasm32-unknown-emscripten -emit-obj -mrelax-all -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name glue_sha_fast.c -mrelocation-model static -mframe-pointer=none -ffp-contract=on -fno-rounding-math -mconstructor-aliases -target-cpu generic -debugger-tuning=gdb -fdebug-compilation-dir=/home/martin/Documents/sha_fast -v -fcoverage-compilation-dir=/home/martin/Documents/sha_fast -resource-dir /home/martin/code/emsdk/upstream/lib/clang/19 -D EMSCRIPTEN -I ./ -isysroot /home/martin/code/emsdk/upstream/emscripten/cache/sysroot -internal-isystem /home/martin/code/emsdk/upstream/lib/clang/19/include -internal-isystem /home/martin/code/emsdk/upstream/emscripten/cache/sysroot/include/wasm32-emscripten -internal-isystem /home/martin/code/emsdk/upstream/emscripten/cache/sysroot/include -O0 -Werror=implicit-function-declaration -Wno-everything -w -ferror-limit 19 -fvisibility=default -fgnuc-version=4.2.1 -fskip-odr-check-in-gmf -fignore-exceptions -fcolor-diagnostics -iwithsysroot/include/fakesdl -iwithsysroot/include/compat -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr -o /tmp/emscripten_temp_g7hbpg60/glue_sha_fast_3.o -x c glue_sha_fast.c
clang -cc1 version 19.0.0git based upon LLVM 19.0.0git default target x86_64-unknown-linux-gnu
ignoring nonexistent directory "/home/martin/code/emsdk/upstream/emscripten/cache/sysroot/include/wasm32-emscripten"
#include "..." search starts here:
#include <...> search starts here:
.
/home/martin/code/emsdk/upstream/emscripten/cache/sysroot/include/fakesdl
/home/martin/code/emsdk/upstream/emscripten/cache/sysroot/include/compat
/home/martin/code/emsdk/upstream/lib/clang/19/include
/home/martin/code/emsdk/upstream/emscripten/cache/sysroot/include
End of search list.
/home/martin/code/emsdk/upstream/bin/clang --version
/home/martin/code/emsdk/upstream/bin/wasm-ld -o sha_fast-opt_O0.wasm -L/home/martin/code/emsdk/upstream/emscripten/cache/sysroot/lib/wasm32-emscripten /tmp/emscripten_temp_g7hbpg60/sha_fast_main_wasm_0.o /tmp/emscripten_temp_g7hbpg60/gc_stack_1.o /tmp/emscripten_temp_g7hbpg60/CertiCoq.Benchmarks.tests.sha_fast_2.o /tmp/emscripten_temp_g7hbpg60/glue_sha_fast_3.o -lGL-getprocaddr -lal -lhtml5 -lstubs-debug -lnoexit -lc-debug -ldlmalloc -lcompiler_rt -lc++-noexcept -lc++abi-debug-noexcept -lsockets -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr /tmp/tmpj5x0rc3qlibemscripten_js_symbols.so --export=emscripten_stack_get_end --export=emscripten_stack_get_free --export=emscripten_stack_get_base --export=emscripten_stack_get_current --export=emscripten_stack_init --export=_emscripten_stack_alloc --export=__get_temp_ret --export=__set_temp_ret --export=__wasm_call_ctors --export=_emscripten_stack_restore --export-if-defined=__start_em_asm --export-if-defined=__stop_em_asm --export-if-defined=__start_em_lib_deps --export-if-defined=__stop_em_lib_deps --export-if-defined=__start_em_js --export-if-defined=__stop_em_js --export-if-defined=main --export-if-defined=__main_argc_argv --export-if-defined=fflush --export-table -z stack-size=1000000 --max-memory=2147483648 --initial-heap=16777216 --no-entry --stack-first --table-base=1
/home/martin/code/emsdk/upstream/bin/llvm-objcopy sha_fast-opt_O0.wasm sha_fast-opt_O0.wasm --remove-section=.debug* --remove-section=producers
/home/martin/code/emsdk/upstream/bin/wasm-emscripten-finalize -g --dyncalls-i64 --pass-arg=legalize-js-interface-exported-helpers sha_fast-opt_O0.wasm -o sha_fast-opt_O0.wasm --detect-features
/home/martin/code/emsdk/node/16.20.0_64bit/bin/node /home/martin/code/emsdk/upstream/emscripten/src/compiler.mjs /tmp/tmp9r6txfje.json
```
Contributor guide
Assessment
This issue has not been assessed yet.