Using shared memory (-s IMPORTED_MEMORY=1)

1,990 views
Skip to first unread message

Александр Гурьянов

unread,
Mar 4, 2022, 3:44:01 AM3/4/22
to emscripte...@googlegroups.com
Hi, I want to use shared memory for my WebAssembly module. It's needed for js-dos. Currently emulation works in worker and frame updates are send using postMessage. I want to use SharedArrayBuffer to avoid copying/sending big rgba data.

I dont' need to use pthread or threading, I just want to share memory between worker and main process. How I can say emscripten to generate "shared" memory imort.

As suggested I tried wasm2wat and then wat2wasm, but it not working for me. Looks like it affects  -s INVOKE_RUN=0, because after wasm2wat my module immediately starts and I can't provide own memory.

Without wasm2wat I have this error:

LinkError: WebAssembly.instantiate(): mismatch in shared state of memory, declared = 0, imported = 1

After wasm2wat this:

LinkError: WebAssembly.instantiate(): mismatch in shared state of memory, declared = 1, imported = 0

Can we add some pass to binaryen for turning non-shared memory into shared?

Александр Гурьянов

unread,
Mar 4, 2022, 1:29:19 PM3/4/22
to emscripte...@googlegroups.com

пт, 4 мар. 2022 г. в 15:43, Александр Гурьянов <caii...@gmail.com>:

Sam Clegg

unread,
Mar 4, 2022, 2:49:22 PM3/4/22
to emscripte...@googlegroups.com
Historically the `-pthead` flag (or `-s USE_PTHREADS`) is the way to get shared memory.   Is there some reason you have not wanted to use that flag?  (is it code size 
of the resulting JS.. if so we are working on reducing that).

As it happens we just today landed a new `-s SHARED_MEMORY` flag I think gives you exactly what you want without the overhead of the pthread JS library code: https://github.com/emscripten-core/emscripten/pull/16419.

cheers,
sam

--
You received this message because you are subscribed to the Google Groups "emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-disc...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/emscripten-discuss/CAKOm%3DVE24Q4%2Bewi5QBSdmuuRf7F-FCk_1TM7tZxexwrMOzXdqw%40mail.gmail.com.

Thomas Lively

unread,
Mar 4, 2022, 3:27:39 PM3/4/22
to emscripte...@googlegroups.com
Also note that you probably do want to use atomic operations to read and write to the shared memory, even if you're using JS on one thread and Wasm on another. If you don't use atomics, there's no guarantee that the values written on one thread will actually become visible on the other thread when you expect them to.

Александр Гурьянов

unread,
Mar 5, 2022, 1:06:26 AM3/5/22
to emscripte...@googlegroups.com
I tried set SHARED_MEMORY=1 both for compile and linking, but now I have this error from linker:
wasm-ld: error: --shared-memory is disallowed by lto.tmp because it was not compiled with 'atomics' or 'bulk-memory' features

Seems that I need to set PTHREADS anyway. I don't want use PTHREADS because I don't know how it affects performance of wasm, can it slowdown the overall performance?


сб, 5 мар. 2022 г. в 03:27, 'Thomas Lively' via emscripten-discuss <emscripte...@googlegroups.com>:

Александр Гурьянов

unread,
Mar 5, 2022, 1:50:50 AM3/5/22
to emscripte...@googlegroups.com
It compiles with flags -sSHARED_MEMORY=1 -sUSE_PTHREADS=1, but it didn't start in worker:

wdosbox.js:6935 Uncaught (in promise) LinkError: WebAssembly.instantiate(): Import #46 module="env" function="emscripten_unwind_to_js_event_loop" error: function import requires a callable

I checked there is no such function on info.env, maybe it's incompatible with -s ASYNCIFY=1?

сб, 5 мар. 2022 г. в 13:06, Александр Гурьянов <caii...@gmail.com>:

Sam Clegg

unread,
Mar 5, 2022, 12:24:14 PM3/5/22
to emscripte...@googlegroups.com
On Fri, Mar 4, 2022 at 10:06 PM Александр Гурьянов <caii...@gmail.com> wrote:
I tried set SHARED_MEMORY=1 both for compile and linking, but now I have this error from linker:
wasm-ld: error: --shared-memory is disallowed by lto.tmp because it was not compiled with 'atomics' or 'bulk-memory' features

Seems that I need to set PTHREADS anyway. I don't want use PTHREADS because I don't know how it affects performance of wasm, can it slowdown the overall performance?

No it shouldn't slow down anything.. there could be come JS code size bloat.. but performance should not suffer significantly.
 

Sam Clegg

unread,
Mar 5, 2022, 12:25:15 PM3/5/22
to emscripte...@googlegroups.com
On Fri, Mar 4, 2022 at 10:50 PM Александр Гурьянов <caii...@gmail.com> wrote:
It compiles with flags -sSHARED_MEMORY=1 -sUSE_PTHREADS=1, but it didn't start in worker:

wdosbox.js:6935 Uncaught (in promise) LinkError: WebAssembly.instantiate(): Import #46 module="env" function="emscripten_unwind_to_js_event_loop" error: function import requires a callable

That function is defined in `library/library_eventloop.js` are you using the emcc-generated JS library code?   Can you share your entire link command that you are using the build module?

Александр Гурьянов

unread,
Mar 8, 2022, 11:34:32 PM3/8/22
to emscripte...@googlegroups.com
I found error in my build system, so now everything works fine. Thank you!

Sam Clegg

unread,
Nov 29, 2023, 1:21:28 PM11/29/23
to emscripten-discuss
I'm considering removing the public facing `-sSHARED_MEMORY` flag: https://github.com/emscripten-core/emscripten/issues/20803

The upshot would be that users who want shared memory would need to choose to use either wasm workers or pthreads.   The use cases for shared memory but without either wasm workers or pthreads seem very limited.  If your program doesn't actually use any pthread APIs then IIUC that code size and performance impacts of using `-pthread` should be very minimal.  

Alexander, WDTY, if we can show minimal impact would your be OK with switching to `-pthread`?

cheers,
sam
Reply all
Reply to author
Forward
0 new messages