From 0ef8b6609cfb399343a897cb12e9dbc51889b4fc Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Fri, 3 Feb 2023 21:05:21 +0900 Subject: [PATCH 1/7] gh-101538: Add experimental wasi-threads build eg. ``` autoconf export WASI_SDK_PATH=/opt/wasi-sdk-19.5g0236e959edbc ./Tools/wasm/wasm_build.py wasi-threads build ~/git/toywasm/b.thread/toywasm --wasi --wasi-dir . --wasi-dir ~/git/garbage/py/th2 -- builddir/wasi-threads/python.wasm ~/git/garbage/py/th2/thread.py ``` * I used an unreleased version of wasi-sdk * I used toywasm to test because wasmtime doesn't have necessary functionality yet. --- ...2023-02-03-21-36-42.gh-issue-101538.sF5F6S.rst | 1 + Python/thread_pthread.h | 7 +++++++ Tools/wasm/config.site-wasm32-wasi | 2 ++ Tools/wasm/wasm_build.py | 8 +++++++- configure.ac | 15 ++++++++++++++- 5 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 Misc/NEWS.d/next/Build/2023-02-03-21-36-42.gh-issue-101538.sF5F6S.rst diff --git a/Misc/NEWS.d/next/Build/2023-02-03-21-36-42.gh-issue-101538.sF5F6S.rst b/Misc/NEWS.d/next/Build/2023-02-03-21-36-42.gh-issue-101538.sF5F6S.rst new file mode 100644 index 00000000000000..4b83c303b3d2c5 --- /dev/null +++ b/Misc/NEWS.d/next/Build/2023-02-03-21-36-42.gh-issue-101538.sF5F6S.rst @@ -0,0 +1 @@ +Add experimental wasi-threads support. Patch by Takashi Yamamoto. diff --git a/Python/thread_pthread.h b/Python/thread_pthread.h index 76d6f3bcdf9c40..540d3bceb19b9c 100644 --- a/Python/thread_pthread.h +++ b/Python/thread_pthread.h @@ -356,7 +356,14 @@ PyThread_exit_thread(void) { if (!initialized) exit(0); +#if defined(__wasi__) + /* + * wasi-threads doesn't have pthread_exit right now + */ + abort(); +#else pthread_exit(0); +#endif } #ifdef USE_SEMAPHORES diff --git a/Tools/wasm/config.site-wasm32-wasi b/Tools/wasm/config.site-wasm32-wasi index 5e98775400f6ea..065a7f3baed8b0 100644 --- a/Tools/wasm/config.site-wasm32-wasi +++ b/Tools/wasm/config.site-wasm32-wasi @@ -40,3 +40,5 @@ ac_cv_header_netpacket_packet_h=no # Disable int-conversion for wask-sdk as it triggers an error from version 17. ac_cv_disable_int_conversion=yes + +ac_cv_pthread=yes diff --git a/Tools/wasm/wasm_build.py b/Tools/wasm/wasm_build.py index 493682c5b138a3..4a37ec20895ee1 100755 --- a/Tools/wasm/wasm_build.py +++ b/Tools/wasm/wasm_build.py @@ -480,7 +480,6 @@ def configure_cmd(self) -> List[str]: cmd.append(f"--{opt}-wasm-dynamic-linking") if self.pthreads is not None: - assert self.host.is_emscripten opt = "enable" if self.pthreads else "disable" cmd.append(f"--{opt}-wasm-pthreads") @@ -745,6 +744,13 @@ def build_emports(self, force: bool = False): support_level=SupportLevel.supported, host=Host.wasm32_wasi, ), + # wasm32-wasi-threads + BuildProfile( + "wasi-threads", + support_level=SupportLevel.experimental, + host=Host.wasm32_wasi, + pthreads=True, + ), # no SDK available yet # BuildProfile( # "wasm64-wasi", diff --git a/configure.ac b/configure.ac index 5eee4586680dbb..4e0717fbcea7f8 100644 --- a/configure.ac +++ b/configure.ac @@ -1053,7 +1053,11 @@ cat > conftest.c <> confdefs.h + AS_VAR_IF([enable_wasm_pthreads], [yes], [ + # Note: update CFLAGS because ac_compile/ac_link needs this too + CFLAGS="$CFLAGS -target wasm32-wasi-threads -pthread" + AS_VAR_APPEND([CFLAGS_NODIST], [" -target wasm32-wasi-threads -pthread"]) + AS_VAR_APPEND([LDFLAGS_NODIST], [" -target wasm32-wasi-threads -pthread"]) + AS_VAR_APPEND([LDFLAGS_NODIST], [" -Wl,--import-memory"]) + AS_VAR_APPEND([LDFLAGS_NODIST], [" -Wl,--max-memory=10485760"]) + ]) + dnl increase initial memory and stack size, move stack first dnl https://github.com/WebAssembly/wasi-libc/issues/233 AS_VAR_APPEND([LDFLAGS_NODIST], [" -z stack-size=524288 -Wl,--stack-first -Wl,--initial-memory=10485760"]) From c4543e1b3df3cec0c5135a17ecaa06bf41f5c9c7 Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Fri, 3 Feb 2023 22:42:31 +0900 Subject: [PATCH 2/7] Regen configure ``` docker run --rm -v $(pwd):/src quay.io/tiran/cpython_autoconf:269 ``` --- configure | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/configure b/configure index 8b707cda62129f..d054f5c3d9ad46 100755 --- a/configure +++ b/configure @@ -6232,7 +6232,11 @@ cat > conftest.c <>confdefs.h LIBS="$LIBS -lwasi-emulated-signal -lwasi-emulated-getpid -lwasi-emulated-process-clocks" echo "#define _WASI_EMULATED_SIGNAL 1" >> confdefs.h + if test "x$enable_wasm_pthreads" = xyes; then : + + # Note: update CFLAGS because ac_compile/ac_link needs this too + CFLAGS="$CFLAGS -target wasm32-wasi-threads -pthread" + as_fn_append CFLAGS_NODIST " -target wasm32-wasi-threads -pthread" + as_fn_append LDFLAGS_NODIST " -target wasm32-wasi-threads -pthread" + as_fn_append LDFLAGS_NODIST " -Wl,--import-memory" + as_fn_append LDFLAGS_NODIST " -Wl,--max-memory=10485760" + +fi + as_fn_append LDFLAGS_NODIST " -z stack-size=524288 -Wl,--stack-first -Wl,--initial-memory=10485760" ;; #( From 0980549ab5a5fe8dd003c9f8fd3af25696aff1ed Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Sat, 4 Feb 2023 16:35:33 +0900 Subject: [PATCH 3/7] revert unnecessary config override --- Tools/wasm/config.site-wasm32-wasi | 2 -- 1 file changed, 2 deletions(-) diff --git a/Tools/wasm/config.site-wasm32-wasi b/Tools/wasm/config.site-wasm32-wasi index 065a7f3baed8b0..5e98775400f6ea 100644 --- a/Tools/wasm/config.site-wasm32-wasi +++ b/Tools/wasm/config.site-wasm32-wasi @@ -40,5 +40,3 @@ ac_cv_header_netpacket_packet_h=no # Disable int-conversion for wask-sdk as it triggers an error from version 17. ac_cv_disable_int_conversion=yes - -ac_cv_pthread=yes From a1959a366490ca4880b107eb936eae9bdc74ed1a Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Sat, 4 Feb 2023 16:36:00 +0900 Subject: [PATCH 4/7] add a comment --- Python/thread_pthread.h | 1 + 1 file changed, 1 insertion(+) diff --git a/Python/thread_pthread.h b/Python/thread_pthread.h index 540d3bceb19b9c..f96c57da64636d 100644 --- a/Python/thread_pthread.h +++ b/Python/thread_pthread.h @@ -359,6 +359,7 @@ PyThread_exit_thread(void) #if defined(__wasi__) /* * wasi-threads doesn't have pthread_exit right now + * cf. https://github.com/WebAssembly/wasi-threads/issues/7 */ abort(); #else From ce7d5c2c0d512bb965236b6fd41370b8228a14b6 Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Sat, 11 Feb 2023 03:01:32 +0900 Subject: [PATCH 5/7] configure.ac: add a comment --- configure.ac | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/configure.ac b/configure.ac index 4e0717fbcea7f8..9773a2d80e4c6c 100644 --- a/configure.ac +++ b/configure.ac @@ -2204,7 +2204,10 @@ AS_CASE([$ac_sys_system], echo "#define _WASI_EMULATED_SIGNAL 1" >> confdefs.h AS_VAR_IF([enable_wasm_pthreads], [yes], [ - # Note: update CFLAGS because ac_compile/ac_link needs this too + # Note: update CFLAGS because ac_compile/ac_link needs this too. + # without this, configure fails to find pthread_create, sem_init, + # etc because they are only available in the sysroot for + # wasm32-wasi-threads. CFLAGS="$CFLAGS -target wasm32-wasi-threads -pthread" AS_VAR_APPEND([CFLAGS_NODIST], [" -target wasm32-wasi-threads -pthread"]) AS_VAR_APPEND([LDFLAGS_NODIST], [" -target wasm32-wasi-threads -pthread"]) From bb26aa22469433bc109d45a3f998c6824d27925b Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Sat, 11 Feb 2023 03:02:11 +0900 Subject: [PATCH 6/7] Regen configure --- configure | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/configure b/configure index d054f5c3d9ad46..cc0c7cdc79875b 100755 --- a/configure +++ b/configure @@ -8553,7 +8553,10 @@ $as_echo "#define _WASI_EMULATED_PROCESS_CLOCKS 1" >>confdefs.h if test "x$enable_wasm_pthreads" = xyes; then : - # Note: update CFLAGS because ac_compile/ac_link needs this too + # Note: update CFLAGS because ac_compile/ac_link needs this too. + # without this, configure fails to find pthread_create, sem_init, + # etc because they are only available in the sysroot for + # wasm32-wasi-threads. CFLAGS="$CFLAGS -target wasm32-wasi-threads -pthread" as_fn_append CFLAGS_NODIST " -target wasm32-wasi-threads -pthread" as_fn_append LDFLAGS_NODIST " -target wasm32-wasi-threads -pthread" From dd60e0a88322760d7d5f6111542fe36b32c70482 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Wed, 14 Jun 2023 10:36:59 +0200 Subject: [PATCH 7/7] Style: use AS_VAR_APPEND throughout the PR --- configure | 5 +++-- configure.ac | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/configure b/configure index d82574eb3aa6b5..73d5fcb864063e 100755 --- a/configure +++ b/configure @@ -9398,13 +9398,14 @@ printf "%s\n" "#define _WASI_EMULATED_PROCESS_CLOCKS 1" >>confdefs.h LIBS="$LIBS -lwasi-emulated-signal -lwasi-emulated-getpid -lwasi-emulated-process-clocks" echo "#define _WASI_EMULATED_SIGNAL 1" >> confdefs.h - if test "x$enable_wasm_pthreads" = xyes; then : + if test "x$enable_wasm_pthreads" = xyes +then : # Note: update CFLAGS because ac_compile/ac_link needs this too. # without this, configure fails to find pthread_create, sem_init, # etc because they are only available in the sysroot for # wasm32-wasi-threads. - CFLAGS="$CFLAGS -target wasm32-wasi-threads -pthread" + as_fn_append CFLAGS " -target wasm32-wasi-threads -pthread" as_fn_append CFLAGS_NODIST " -target wasm32-wasi-threads -pthread" as_fn_append LDFLAGS_NODIST " -target wasm32-wasi-threads -pthread" as_fn_append LDFLAGS_NODIST " -Wl,--import-memory" diff --git a/configure.ac b/configure.ac index d45c99d06abd7f..326b08ebd02317 100644 --- a/configure.ac +++ b/configure.ac @@ -2318,7 +2318,7 @@ AS_CASE([$ac_sys_system], # without this, configure fails to find pthread_create, sem_init, # etc because they are only available in the sysroot for # wasm32-wasi-threads. - CFLAGS="$CFLAGS -target wasm32-wasi-threads -pthread" + AS_VAR_APPEND([CFLAGS], [" -target wasm32-wasi-threads -pthread"]) AS_VAR_APPEND([CFLAGS_NODIST], [" -target wasm32-wasi-threads -pthread"]) AS_VAR_APPEND([LDFLAGS_NODIST], [" -target wasm32-wasi-threads -pthread"]) AS_VAR_APPEND([LDFLAGS_NODIST], [" -Wl,--import-memory"])