Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wasm workers #12833

Merged
merged 67 commits into from
Mar 7, 2022
Merged
Show file tree
Hide file tree
Changes from 48 commits
Commits
Show all changes
67 commits
Select commit Hold shift + click to select a range
0f55bf4
Add Wasm Workers
juj Feb 7, 2022
fa21c46
Add TLS test, ES6ify.
juj Feb 7, 2022
f466e62
Update test
juj Feb 7, 2022
bd1e261
Add TLS support to Wasm Workers
juj Feb 15, 2022
ead8aa5
Add C++11 thread_local keyword test.
juj Feb 15, 2022
9545aeb
Add test for C11 _Thread_local.
juj Feb 15, 2022
29bed03
Add emscripten_malloc_wasm_worker and rename creation API a little.
juj Feb 15, 2022
0e3e328
Add documentation for Wasm Workers.
juj Feb 15, 2022
83fef04
Flake and lint and fix build error
juj Feb 17, 2022
57b2ff6
Remove deps_info dependency that does not work in current setup
juj Feb 17, 2022
559551f
__builtin_wasm_tls_align() can be zero
juj Feb 18, 2022
9effd63
Add more notes about __builtin_wasm_tls_align() being zero
juj Feb 18, 2022
177f29b
Add test for GCC __thread keyword.
juj Feb 18, 2022
3e0cdb3
Fix test_wasm_worker_malloc
juj Feb 18, 2022
883e539
Fix emscripten_lock_async_acquire()
juj Feb 18, 2022
a403133
Fix thread stack creation.
juj Feb 18, 2022
67d0bbb
Fix wasm64 build
juj Feb 18, 2022
ebc4319
Add slack to lock_busyspin_wait_acquire
juj Feb 18, 2022
da84c92
Fix typo in setting
juj Feb 18, 2022
ffdc68e
Remove removal of TextDecoder in threads.
juj Feb 20, 2022
2a0f5e4
Fix non-Wasm Workers build
juj Feb 20, 2022
19904b4
Fix file system case sensitivity
juj Feb 20, 2022
0300d02
Fix Wasm Workers proxying mode generation.
juj Feb 20, 2022
9f87d16
Skip TLS tests on Linux, they produce an internal compiler error.
juj Feb 20, 2022
df81c82
Fix typo
juj Feb 20, 2022
fd5ed59
Fix wasm_worker.h include from C code.
juj Feb 20, 2022
8c94a36
Add library_wasm_worker_stub.c.
juj Feb 20, 2022
1876f14
Wasm Workers working on default runtime.
juj Feb 21, 2022
3fa9bac
flake
juj Feb 21, 2022
fdb8c92
Disable most wasm workers tests to debug CI
juj Feb 21, 2022
37fba96
Fix non-minimal runtime wasm workers startup. Add test for WASM_WORKE…
juj Feb 22, 2022
a9e47d6
Simplify in MINIMAL_RUNTIME preamble assignment for wasm maximum memory.
juj Feb 22, 2022
4091a56
Fix USE_PTHREADS+WASM_WORKERS line.
juj Feb 22, 2022
90543ec
Add support for simultaneous pthreads + Wasm workers.
juj Feb 22, 2022
ce6040e
Do not pass redundant TLS size to Wasm Worker creation side.
juj Feb 22, 2022
81b0543
Update emcc.py wasm worker deps
juj Feb 22, 2022
fbc4fdc
Remove special handling of .S files in system_libs build
juj Feb 23, 2022
75d51c8
Update documentation
juj Feb 23, 2022
553dd6b
Add code size test.
juj Feb 23, 2022
7f35de8
flake
juj Feb 23, 2022
b0f04f0
Update tests and wasm worker MT build
juj Feb 23, 2022
ba81dd2
Fix mt build
juj Feb 23, 2022
59d11ad
Adjust mt build
juj Feb 23, 2022
05d0dc9
Update code size test
juj Feb 23, 2022
98cc721
Update hello worker wasm
juj Feb 24, 2022
28f37d9
flake
juj Feb 24, 2022
4dff3b7
Merge remote-tracking branch 'origin/main' into wasm_workers
juj Mar 3, 2022
76a4e82
Address review: Allow building with -sSHARED_MEMORY and add a test. M…
juj Mar 3, 2022
34a766e
Remove unnecessary dynCall statements
juj Mar 4, 2022
b45a7b7
Update mention of C11 and C++11 Atomics APIs
juj Mar 4, 2022
acdce95
Merge branch 'main' into wasm_workers
juj Mar 6, 2022
ee31eed
Remove old code.
juj Mar 6, 2022
08041f1
Utilize runOnMainThread() in MINIMAL_RUNTIME ready handler.
juj Mar 6, 2022
1f5d6b0
Simplify code
juj Mar 6, 2022
9e56c85
#error quotes
juj Mar 6, 2022
bca824e
Clean typo
juj Mar 6, 2022
07a3585
Cleanup tests
juj Mar 6, 2022
7fa8dd7
Update ChangeLog
juj Mar 6, 2022
33079fd
Fixes
juj Mar 6, 2022
4f8dc6c
Add test files.
juj Mar 6, 2022
c78609d
Fix pthreads
juj Mar 6, 2022
552e0a7
Remove moved test
juj Mar 7, 2022
7cfeb98
Address review
juj Mar 7, 2022
b7ac44d
Small code size optimization
juj Mar 7, 2022
5839916
Small code size opt
juj Mar 7, 2022
a570999
Flake
juj Mar 7, 2022
8d53ac4
Update Wasm Workers code size test
juj Mar 7, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ ignorePatterns:
- "src/embind/"
- "src/emrun_postjs.js"
- "src/worker.js"
- "src/wasm_worker.js"
- "src/wasm2js.js"
- "src/webGLClient.js"
- "src/webGLWorker.js"
Expand Down
53 changes: 43 additions & 10 deletions emcc.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ def setup_environment_settings():
if not settings.ENVIRONMENT_MAY_BE_WORKER and settings.PROXY_TO_WORKER:
exit_with_error('If you specify --proxy-to-worker and specify a "-s ENVIRONMENT=" directive, it must include "worker" as a target! (Try e.g. -s ENVIRONMENT=web,worker)')

if not settings.ENVIRONMENT_MAY_BE_WORKER and settings.USE_PTHREADS:
if not settings.ENVIRONMENT_MAY_BE_WORKER and settings.SHARED_MEMORY:
exit_with_error('When building with multithreading enabled and a "-s ENVIRONMENT=" directive is specified, it must include "worker" as a target! (Try e.g. -s ENVIRONMENT=web,worker)')


Expand Down Expand Up @@ -830,6 +830,12 @@ def get_cflags(user_args):
if settings.EMSCRIPTEN_TRACING:
cflags.append('-D__EMSCRIPTEN_TRACING__=1')

if settings.SHARED_MEMORY:
cflags.append('-D__EMSCRIPTEN_SHARED_MEMORY__=1')

if settings.WASM_WORKERS:
cflags.append('-D__EMSCRIPTEN_WASM_WORKERS__=1')

if not settings.STRICT:
# The preprocessor define EMSCRIPTEN is deprecated. Don't pass it to code
# in strict mode. Code should use the define __EMSCRIPTEN__ instead.
Expand Down Expand Up @@ -1432,7 +1438,11 @@ def phase_setup(options, state, newargs, user_settings):
if settings.MAIN_MODULE or settings.SIDE_MODULE:
settings.RELOCATABLE = 1

if settings.USE_PTHREADS and '-pthread' not in newargs:
# Pthreads and Wasm Workers require targeting shared Wasm memory (SAB).
if settings.USE_PTHREADS or settings.WASM_WORKERS:
settings.SHARED_MEMORY = 1

if settings.SHARED_MEMORY and '-pthread' not in newargs:
juj marked this conversation as resolved.
Show resolved Hide resolved
newargs += ['-pthread']

if 'DISABLE_EXCEPTION_CATCHING' in user_settings and 'EXCEPTION_CATCHING_ALLOWED' in user_settings:
Expand Down Expand Up @@ -1687,7 +1697,7 @@ def phase_linker_setup(options, state, newargs, user_settings):
# https://github.com/whatwg/encoding/issues/172
# When supporting shell environments, do not do this as TextDecoder is not
# widely supported there.
if settings.SHRINK_LEVEL >= 2 and not settings.USE_PTHREADS and \
if settings.SHRINK_LEVEL >= 2 and not settings.SHARED_MEMORY and \
not settings.ENVIRONMENT_MAY_BE_SHELL:
default_setting(user_settings, 'TEXTDECODER', 2)

Expand Down Expand Up @@ -2075,6 +2085,18 @@ def phase_linker_setup(options, state, newargs, user_settings):
if settings.INCLUDE_FULL_LIBRARY and not settings.DISABLE_EXCEPTION_CATCHING:
settings.EXPORTED_FUNCTIONS += ['_emscripten_format_exception', '_free']

if settings.WASM_WORKERS:
# TODO: After #15982 is resolved, these dependencies can be declared in library_wasm_worker.js
# instead of having to record them here.
wasm_worker_imports = ['_emscripten_wasm_worker_initialize']
settings.EXPORTED_FUNCTIONS += wasm_worker_imports
juj marked this conversation as resolved.
Show resolved Hide resolved
building.user_requested_exports.update(wasm_worker_imports)
settings.DEFAULT_LIBRARY_FUNCS_TO_INCLUDE += ['_wasm_worker_initializeRuntime']
# set location of Wasm Worker bootstrap JS file
if settings.WASM_WORKERS == 1:
settings.WASM_WORKER_FILE = unsuffixed(os.path.basename(target)) + '.ww.js'
settings.JS_LIBRARIES.append((0, shared.path_from_root('src', 'library_wasm_worker.js')))

if settings.FORCE_FILESYSTEM and not settings.MINIMAL_RUNTIME:
# when the filesystem is forced, we export by default methods that filesystem usage
# may need, including filesystem usage from standalone file packager output (i.e.
Expand Down Expand Up @@ -2214,16 +2236,16 @@ def check_memory_setting(setting):
exit_with_error(f'Due to collision in variable name "Module", the shell file "{options.shell_path}" is not compatible with build options "-s MODULARIZE=1 -s EXPORT_NAME=Module". Either provide your own shell file, change the name of the export to something else to avoid the name collision. (see https://github.com/emscripten-core/emscripten/issues/7950 for details)')

if settings.STANDALONE_WASM:
if settings.USE_PTHREADS:
exit_with_error('STANDALONE_WASM does not support pthreads yet')
if settings.SHARED_MEMORY:
exit_with_error('STANDALONE_WASM does not support shared memories yet')
if settings.MINIMAL_RUNTIME:
exit_with_error('MINIMAL_RUNTIME reduces JS size, and is incompatible with STANDALONE_WASM which focuses on ignoring JS anyhow and being 100% wasm')
# the wasm must be runnable without the JS, so there cannot be anything that
# requires JS legalization
settings.LEGALIZE_JS_FFI = 0

# TODO(sbc): Remove WASM2JS here once the size regression it would introduce has been fixed.
if settings.USE_PTHREADS or settings.RELOCATABLE or settings.ASYNCIFY_LAZY_LOAD_CODE or settings.WASM2JS:
if settings.SHARED_MEMORY or settings.RELOCATABLE or settings.ASYNCIFY_LAZY_LOAD_CODE or settings.WASM2JS:
settings.IMPORTED_MEMORY = 1

if settings.WASM_BIGINT:
Expand Down Expand Up @@ -2258,9 +2280,9 @@ def check_memory_setting(setting):
# can use a .mem file like asm.js used to.
# generally we follow what the options tell us to do (which is to use
# a .mem file in most cases, since it is binary & compact). however, for
# pthreads we must keep the memory segments in the wasm as they will be
# passive segments which the .mem format cannot handle.
settings.MEM_INIT_IN_WASM = not options.memory_init_file or settings.SINGLE_FILE or settings.USE_PTHREADS
# shared memory builds we must keep the memory segments in the wasm as
# they will be passive segments which the .mem format cannot handle.
settings.MEM_INIT_IN_WASM = not options.memory_init_file or settings.SINGLE_FILE or settings.SHARED_MEMORY
else:
# wasm includes the mem init in the wasm binary. The exception is
# wasm2js, which behaves more like js.
Expand Down Expand Up @@ -2821,8 +2843,8 @@ def phase_final_emitting(options, state, target, wasm_target, memfile):
# src = re.sub(r'\n+[ \n]*\n+', '\n', src)
# write_file(final_js, src)

target_dir = os.path.dirname(os.path.abspath(target))
if settings.USE_PTHREADS:
target_dir = os.path.dirname(os.path.abspath(target))
worker_output = os.path.join(target_dir, settings.PTHREAD_WORKER_FILE)
contents = shared.read_and_preprocess(utils.path_from_root('src/worker.js'), expand_macros=True)
write_file(worker_output, contents)
Expand All @@ -2832,6 +2854,17 @@ def phase_final_emitting(options, state, target, wasm_target, memfile):
minified_worker = building.acorn_optimizer(worker_output, ['minifyWhitespace'], return_output=True)
write_file(worker_output, minified_worker)

# Deploy the Wasm Worker bootstrap file as an output file (*.ww.js)
if settings.WASM_WORKERS == 1:
worker_output = os.path.join(target_dir, settings.WASM_WORKER_FILE)
with open(worker_output, 'w') as f:
f.write(shared.read_and_preprocess(shared.path_from_root('src', 'wasm_worker.js'), expand_macros=True))

# Minify the wasm_worker.js file in optimized builds
if (settings.OPT_LEVEL >= 1 or settings.SHRINK_LEVEL >= 1) and not settings.DEBUG_LEVEL:
minified_worker = building.acorn_optimizer(worker_output, ['minifyWhitespace'], return_output=True)
open(worker_output, 'w').write(minified_worker)

# track files that will need native eols
generated_text_files_with_native_eols = []

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions site/source/docs/api_reference/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ This section lists Emscripten's public API, organised by header file. At a very
- :ref:`Fetch-API`:
API for managing accesses to network XHR and IndexedDB.

- :ref:`wasm_workers`:
Enables writing multithreaded programs using a web-like API.

- :ref:`Module`:
Global JavaScript object that can be used to control code execution and access exported methods.

Expand Down Expand Up @@ -53,6 +56,7 @@ This section lists Emscripten's public API, organised by header file. At a very
bind.h
trace.h
fiber.h
wasm_workers
advanced-apis


Loading