Skip to content

wasm: Add support for WASI threads in CMake build system #71978

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

Conversation

kateinoigakukun
Copy link
Member

@kateinoigakukun kateinoigakukun commented Feb 29, 2024

This patch adds a new CMake option, SWIFT_ENABLE_WASI_THREADS, to enable building the Standard Library using WASI threads primitives (https://github.com/WebAssembly/wasi-threads). With this option, the Standard Library will be built for the new "wasm32-unknown-wasi-threads" target. We add the new triple with "-threads" environment field because the WASI threads proposal requires extra syscalls and object files compiled to "wasm32-unknown-wasi" and "wasm32-unknown-wasi-threads" are not compatible and cannot be linked together.

I chose wasm32-unknown-wasi-threads by following wasi-sdk: https://github.com/WebAssembly/wasi-sdk/blob/f1ebc52a74394cdf885d03bfde13899b3d5c6d2d/wasi-sdk-pthread.cmake#L9

This patch adds a new CMake option, `SWIFT_ENABLE_WASI_THREADS`, to
enable building the Standard Library using WASI threads primitives
(https://github.com/WebAssembly/wasi-threads). With this option, the
Standard Library will be built for the new "wasm32-unknown-wasi-threads"
target. We add the new triple because the WASI threads proposal requires
extra syscalls and object files compiled to "wasm32-unknown-wasi" and
"wasm32-unknown-wasi-threads" are not compatible and cannot be linked
together.
@kateinoigakukun
Copy link
Member Author

@swift-ci smoke test

@kateinoigakukun
Copy link
Member Author

@swift-ci test WebAssembly

@@ -418,7 +422,11 @@ macro(configure_sdk_unix name architectures)
message(FATAL_ERROR "unsupported arch for WebAssembly: ${arch}")
endif()
set(SWIFT_SDK_WASI_ARCH_wasm32_PATH "${SWIFT_WASI_SYSROOT_PATH}")
set(SWIFT_SDK_WASI_ARCH_wasm32_TRIPLE "wasm32-unknown-wasi")
if(SWIFT_ENABLE_WASI_THREADS)
set(SWIFT_SDK_WASI_ARCH_wasm32_TRIPLE "wasm32-unknown-wasi-threads")
Copy link
Contributor

@MaxDesiatov MaxDesiatov Feb 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there no other way to express this other than using the last triple component? Is this the triple that Clang uses for WASI threads as well? I find it confusing that we have wasm32-unknown-none-wasm with the last triple component denoting the object format, and wasm32-unknown-wasi-threads for denoting a platform feature. OTOH this is not much different from armv7-unknown-linux-musl?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I followed how wasi-sdk and wasi-libc call the target: https://github.com/WebAssembly/wasi-sdk/blob/f1ebc52a74394cdf885d03bfde13899b3d5c6d2d/wasi-sdk-pthread.cmake#L9

Target triple parser is very complicated... The fourth component of triple can be interpreted as environment or object file format.
https://github.com/llvm/llvm-project/blob/a872a35251b833aaddec2172710ff234236afbd8/llvm/lib/TargetParser/Triple.cpp#L1071-L1076

@kateinoigakukun kateinoigakukun marked this pull request as ready for review February 29, 2024 17:44
@kateinoigakukun kateinoigakukun merged commit f127c17 into swiftlang:main Mar 8, 2024
@kateinoigakukun kateinoigakukun deleted the pr-23387a5d8e024a6c8c879f6de94023a575d00ee6 branch March 8, 2024 07:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants