Skip to content

feat: Add support for Wasm compilation and CI using the Swift SDK for WebAssembly#190

Merged
kukushechkin merged 1 commit intoapple:mainfrom
PassiveLogic:feat/swift-wasm-support-v3
Feb 12, 2026
Merged

feat: Add support for Wasm compilation and CI using the Swift SDK for WebAssembly#190
kukushechkin merged 1 commit intoapple:mainfrom
PassiveLogic:feat/swift-wasm-support-v3

Conversation

@scottmarchant
Copy link
Copy Markdown
Contributor

@scottmarchant scottmarchant commented Jan 6, 2026

Motivation:

As part of expanding platform and CI coverage of Swift Configuration, Swift Metrics should also build to Wasm using the Swift SDK for WebAssembly.

Modifications:

Brought over locking changes from Swift Log to support Wasm.
Conditionally elided some APIs that require Dispatch when Dispatch isn't available.

Result:

The package now builds against the Wasm SDK, both normal and the test targets.

Important

This PR is a cherry pick of #183 created by @czechboy0. It has been modified to resolve conflicts and is rebased on the latest main.

@scottmarchant
Copy link
Copy Markdown
Contributor Author

@MaxDesiatov @kateinoigakukun Mentioning you on this PR as FYI in case you're interested or have feedback.

@scottmarchant scottmarchant changed the title feat: Add Swift SDK for WebAssembly support including CI feat: Add support for WASM compilation and CI using the Swift SDK for WebAssembly support Jan 6, 2026
@scottmarchant scottmarchant changed the title feat: Add support for WASM compilation and CI using the Swift SDK for WebAssembly support feat: Add support for WASM compilation and CI using the Swift SDK for WebAssembly Jan 6, 2026
#if os(Windows)
internal final class Lock: @unchecked Sendable {
#if canImport(WASILibc)
// WASILibc is single threaded, provides no locks
Copy link
Copy Markdown
Member

@MaxDesiatov MaxDesiatov Jan 6, 2026

Choose a reason for hiding this comment

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

This is technically not true, WASILibc is orthogonal to threading, and it does provide a pthread-compatible API that works in both single-threaded in multi-threaded environments. For compatibility with multi-threaded WASI, I suggest using Mutex from import Synchronization instead, if possible. That would make it work not just for both single-threaded and multi-threaded WASI, but all major platforms supported by Swift.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@ktoso do you think that replacing this ad-hoc Lock type with Mutex from Synchronization across the codebase of swift-metrics is feasible?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Mutex needs to be used carefully on Wasm targets because it will raise an exception when it needs to wait on the main thread. But in this case, the lock is taken only for bootstrapping the factory storage; it is unlikely to fail to acquire, and library users are capable of controlling which thread the lock will be acquired, so I think it makes sense to use Mutex here.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Mutex has high platform requirements, which we can't bump without a major version here, so we need to stick with some custom type here.

We should just copy Swift Log's Locks.swift, which already solved it, as it supports Wasm now.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It does seem to be a copy from swift-log, but my point still stands: the current implementation for WASI there has no support for multi-threading and the comment about "no locking" is not technically correct.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ideally, we should use import wasi_pthread and fix it both here and in swift-log. NIO already has an implementation for wasi_pthread https://github.com/apple/swift-nio/blob/main/Sources/NIOConcurrencyHelpers/lock.swift

Copy link
Copy Markdown
Contributor Author

@scottmarchant scottmarchant Jan 8, 2026

Choose a reason for hiding this comment

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

@MaxDesiatov @czechboy0 @kateinoigakukun It looks like the Lock implementation is a copy paste of the lock.swift in NIO, and the ReadWriteLock is a copy-paste of the implementation in swift-log or swift-distributed tracing.

I'm happy to replicate the implementation in all those places. Is there a preferred order? It looks like I should start in swift-log, get that reviewed and merged, then copy-paste verbatim into swift-metrics and swift-distributed tracing.

For ReadWriteLock, it will have to be refactored to follow the import wasi_pthread pattern, since none of the current implementations have wasi_pthread handling.

For Lock, any preference on the following options:

A) Refactor the current implementation in this PR to follow the import patterns used in the nio lock implementation Max provided?

B) Copy-paste the latest Lock implementation verbatim from NIO, but remove the deprecation?

C) Use the latest NIOLock with a type alias to map NIOLock to Lock.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm happy to replicate the implementation in all those places. Is there a preferred order? It looks like I should start in swift-log, get that reviewed and merged, then copy-paste verbatim into swift-metrics and swift-distributed tracing.

Yes, that's the right order.

For Lock, any preference on the following options:

I think we should stick as closely as possible to whatever NIO is doing, so B), just keep the type internal so no need to deprecate the old name.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@czechboy0 @MaxDesiatov @kateinoigakukun I created the first PR to update the lock implementation for swift-log:

apple/swift-log#398

It's in draft at the moment. Want to run a few more checks.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@czechboy0 apple/swift-log#398 has been merged, and I've updated this PR to match the Locks.swift file in swift-log.

//
//===----------------------------------------------------------------------===//

#if canImport(WASILibc)
Copy link
Copy Markdown
Contributor Author

@scottmarchant scottmarchant Jan 14, 2026

Choose a reason for hiding this comment

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

Note: This implementation is a verbatim copy paste of the change in https://github.com/apple/swift-log/pull/398/changes

The only difference is the project name in the file header comments

@scottmarchant scottmarchant force-pushed the feat/swift-wasm-support-v3 branch from 0fad628 to 2351eab Compare February 3, 2026 23:51
@scottmarchant
Copy link
Copy Markdown
Contributor Author

@czechboy0 I've rebased this PR on the latest main. I believe all issues are resolved. With the note that I'll address removing the // SRWLOCK does not need to be free'd comment separately in swift-log first, then in a follow on PR later (along with other changes from swift-nio and swift-log).

Is there anything else needed for this PR?

@scottmarchant scottmarchant force-pushed the feat/swift-wasm-support-v3 branch 2 times, most recently from 67b652f to ad772d6 Compare February 5, 2026 18:33
… lock implementation to match the latest implementation in swift-log provided by apple/swift-log#408.
@scottmarchant scottmarchant force-pushed the feat/swift-wasm-support-v3 branch from ad772d6 to 95a6857 Compare February 5, 2026 18:35
@scottmarchant
Copy link
Copy Markdown
Contributor Author

Updated this with latest changes in swift-log that will be introduced with apple/swift-log#408

@MaxDesiatov
Copy link
Copy Markdown
Member

Please update PR title to use "Wasm" instead of "WASM" before merging to use correct spelling per the Wasm spec: https://webassembly.github.io/spec/core/intro/introduction.html#wasm

@kukushechkin kukushechkin changed the title feat: Add support for WASM compilation and CI using the Swift SDK for WebAssembly feat: Add support for Wasm compilation and CI using the Swift SDK for WebAssembly Feb 11, 2026
@kukushechkin kukushechkin added the 🔨 semver/patch No public API change. label Feb 11, 2026
@kukushechkin kukushechkin merged commit 00dcdba into apple:main Feb 12, 2026
72 of 76 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🔨 semver/patch No public API change.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants