emscripten-core / emscripten-core/emscripten

Issue with O3-optimized main module and side module using emval

Open
#21,036 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
27.6k
Forks
3.6k
Avg merge
1d 1h
Merged PRs (30d)
105

Description

Hi,

Somewhat related to https://github.com/emscripten-core/emscripten/issues/20612, I am trying to build my project with `O3` optimizations enabled, but it seems that some symbols are being pruned too aggressively, while they are not up to `O2` level.

Essentially I am trying to load a side module that uses the `emscripten::val` function (while the main module doesn't).

I was able to compile a minimal list of `EXPORTED_FUNCTIONS` to be able to compile with `MAIN_MODULE=2` (rather than `MAIN_MODULE=1`) and it works at all optimization level until `O2` included. But when I use `O3` it breaks.

My main module build command looks like this:
```
emcc -v ../main.cc \
-s WASM=1 \
-lembind \
-sMAIN_MODULE=2 \
-O3 \
-sEXPORTED_FUNCTIONS='[_main,_malloc,_free,_printf,__ZTVN10__cxxabiv117__class_type_infoE,__emval_take_value,__emval_decref,__ZTIv,_pthread_self,__ZTIN10emscripten11memory_viewIhEE,__ZTSN10emscripten11memory_viewIhEE]' \
-sDEFAULT_LIBRARY_FUNCS_TO_INCLUDE=_emval_take_value \
-o hello.html \
--shell-file ../template.html
```

I've put together a small branch that demonstrates the problem [here](https://github.com/Y--/wasm-dlopen-embind/tree/emval_take_value_o3) (branch `emval_take_value_o3`): when you build with `O3` in the `build.sh` script and open the file in a browser, the console displays:

```
Starting side_func
hello.js:1 Got typed_memory_view
hello.js:1 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'apply')
at stubs. (hello.js:1:19547)
at d2c5c7ca:0xa56
at stubs. (hello.js:1:19547)
at d2c5c7ca:0x971
at hello.wasm:0x5a4f
at callMain (hello.js:1:95427)
at doRun (hello.js:1:95813)
at run (hello.js:1:95985)
at runCaller (hello.js:1:95061)
at removeRunDependency (hello.js:1:5486)
```

And a quick look at the missing symbol where the exception occurs shows `_emval_take_value` to be missing.
Changing `O3` to `O2` in the main module compilation solves the issue.

Is there a way to force this symbol to stay in the `wasmImports` collection?

**Version of emscripten/emsdk:**

```
$ emcc -v
emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.52-git (249cf8c650c7fa27e358cfd759dbc30483b6038f)
clang version 18.0.0git (https://github.com/llvm/llvm-project f2464ca317bfeeedddb7cbdea3c2c8ec487890bb)
Target: wasm32-unknown-emscripten
Thread model: posix
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.