# Wasm Support

**URL:** <https://forums.swift.org/t/wasm-support/16087>\
**Category:** Compiler\
**Created:** [September 11, 2018, 10:23pm UTC](https://forums.swift.org/t/wasm-support/16087 "2018-09-11T22:23:35Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![kateinoigakukun](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/kateinoigakukun/32/16706_2.png) [@kateinoigakukun](https://forums.swift.org/u/kateinoigakukun)\
**Post date:** [February 26, 2020, 4:37am UTC](https://forums.swift.org/t/wasm-support/16087/21 "2020-02-26T04:37:06Z")

</div>

@Joe_Groff Hi!  
While I was fixing the rest of failing test suites, I found that another issue for ABI.  
As described [here](https://github.com/apple/swift/blob/9f5bf22ee3a8cbfe4c090c6c9f2652f5a1f726dd/lib/IRGen/GenFunc.cpp#L44-L47), Swift ABI allows using non-generic function as generic function directly.

On WebAssembly runtime, this ABI causes signature mismatch between callee and caller.

Unlike throws and non-throws, this extra generic parameter doesn't have special attribute, so it's hard to lower them as I did for swifterror.

However as far as I investigated, there is only one use case for this ABI, only KeyPath uses it. I've patched to add extra parameter always to match signatures and this patch makes `test/stdlib/KeyPath.swift` succeeded.

[https://github.com/swiftwasm/swift/pull/252](https://github.com/swiftwasm/swift/pull/252)

Do you know other use cases of this optional generic parameter?

In addition, I found extra subscript indices are also passed optionally in KeyPath getter and setter, as same as generic parameter.  
Does Swift5 ABI allow additional argument generally? Is this additional argument ABI used in other places?

---

<div class="post-metadata">

**Author:** ![Joe\_Groff](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/joe_groff/32/1929_2.png) [@Joe\_Groff](https://forums.swift.org/u/Joe_Groff)\
**Post date:** [February 26, 2020, 6:18pm UTC](https://forums.swift.org/t/wasm-support/16087/22 "2020-02-26T18:18:11Z")

</div>

Adding an extra argument to nongeneric key path entry points is the right thing to do.

---

<div class="post-metadata">

**Author:** ![Joe\_Groff](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/joe_groff/32/1929_2.png) [@Joe\_Groff](https://forums.swift.org/u/Joe_Groff)\
**Post date:** [February 26, 2020, 10:59pm UTC](https://forums.swift.org/t/wasm-support/16087/23 "2020-02-26T22:59:55Z")

</div>

Regarding key paths, their weird calling convention is also an issue for optimization, so we may introduce a SIL convention for key path accessors, which would be another marker you could use to lower them to a consistent LLVM function type for wasm:

> <https://github.com/apple/swift/pull/28799#issuecomment-570299845>
>
> We have an optimization in SILCombiner (introduced in #24929) that inlines comp…ile-time constant key paths by performing the property access directly instead of calling a runtime function (leading to huge performance gains e.g. for heavy use of \`@dynamicMemberLookup\`). However, this optimization previously only supported key paths which solely access stored properties, so computed properties, optional chaining, etc. still had to call a runtime function. This commit generalizes the optimization to support all types of key paths.
> 
> This is my first time contributing to any sort of compiler development, so it's likely I've overlooked something. The previous iteration of this optimzation essentially computed the stored property offsets and returned the resulting address. I still used with a design that projects the key path to an address since that's what's returned by the runtime functions that we're replacing, but my implementation is more complex because projections for most types of key path components need both setup and teardown logic to e.g. deallocate temporaries, call getters and setters, or re-wrap optionals.
> 
> I created an abstract class called \`KeyPathProjector\`. It has a method called \`project\`, which accepts an access type (get, set, or modify) and a callback lambda. This method inserts SIL instructions to project the key path to an address, invokes the callback, and then inserts instructions to tear down the projection. A complete key path is handled (internally to \`KeyPathProjector.cpp\`) by creating a "chained" projector: each projector in the chain is responsible for projecting a single component on top of the previously projected result. For each type of key path component, there is one subclass of \`KeyPathProjector\` (again, all of these are implementation details of \`KeyPathProjector.cpp\`).
> 
> There are a few special cases (such as access enforcement and optional chaining), but I think my logic works, and it passes all the tests I've tried. I do have a couple concerns though:
> \- Subscripts in key paths are treated as computed properties with indices, and they require an additional context pointer to be passed to the property accessor. The key path projector needs to create this context object with the correct memory layout; I implemented this by creating a tuple which contains the context operands. Is a tuple guaranteed to have the correct memory layout? Will this work in all cases, and is it guaranteed to work in the future?
> \- Some code in IRGen, along with the key path ABI documentation, seemed to make references to including a "generic environment" along with these context pointers. However, SILVerifier \[doesn't seem to know about this\](https://github.com/apple/swift/blob/master/lib/SIL/SILVerifier.cpp#L248), and I haven't been able to figure out anything more. Is this something I need to account for? Under what circumstances would I need to pass a generic environment to an accessor, and what exactly would that involve?

---

<div class="post-metadata">

**Author:** ![kateinoigakukun](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/kateinoigakukun/32/16706_2.png) [@kateinoigakukun](https://forums.swift.org/u/kateinoigakukun)\
**Post date:** [February 27, 2020, 12:08am UTC](https://forums.swift.org/t/wasm-support/16087/24 "2020-02-27T00:08:29Z")

</div>

Thanks! I vote that SIL convention for key path accessors.

Just to confirm, do you mean that extra generic argument and extra indices are only used for KeyPath?

---

<div class="post-metadata">

**Author:** ![Joe\_Groff](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/joe_groff/32/1929_2.png) [@Joe\_Groff](https://forums.swift.org/u/Joe_Groff)\
**Post date:** [February 27, 2020, 2:11am UTC](https://forums.swift.org/t/wasm-support/16087/25 "2020-02-27T02:11:46Z")

</div>

Yeah, I don't know of anywhere else that we expect generic and nongeneric functions to have compatible ABIs. If we add such cases in the future, they should be thunkable for wasm targets.

---

<div class="post-metadata">

**Author:** ![Max\_Desiatov](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/max_desiatov/32/20654_2.png) [@Max\_Desiatov](https://forums.swift.org/u/Max_Desiatov)\
**Post date:** [April 24, 2020, 10:03am UTC](https://forums.swift.org/t/wasm-support/16087/26 "2020-04-24T10:03:43Z")

</div>

An update on the current status: if you don't care about binary size, it's more or less usable right now as [in this demo](https://github.com/kateinoigakukun/life-game-with-swiftwasm). Swift can't run dead code elimination on unused protocol conformances yet, so the minimum produced binary size is ~10M raw and ~1.5M when optimized and compressed with 3rd-party tools, as tracked in [swiftwasm/swift#7](https://github.com/swiftwasm/swift/issues/7). Building with SwiftPM works, as long as you don't use Foundation or Dispatch due to a few technical reasons ([swiftwasm/swift#592](https://github.com/swiftwasm/swift/issues/592), [swiftwasm/swift#658](https://github.com/swiftwasm/swift/issues/658), [swiftwasm/swift#647](https://github.com/swiftwasm/swift/issues/647)), but I hope that [a minimalistic bare-bones subset of Foundation](https://github.com/MaxDesiatov/WASIFoundation/) can alleviate that pain in the meantime.

Interacting with DOM works through [JavaScriptKit](https://github.com/kateinoigakukun/JavaScriptKit) built by @kateinoigakukun, also big thanks to him for the Game of Life demo. I honestly didn't expect that we actually have SwiftPM and DOM interaction working at this point before I saw the demo in action 🤩

`swift test` doesn't work yet, but I was able to get [a subset of XCTest without expectations, waiters and parallel testing](https://github.com/swiftwasm/swift-corelibs-xctest) compiling, but not working yet due to [a library path issue on Linux](https://github.com/swiftwasm/swift/issues/713). I think I got it working on macOS, but need to verify that with a proper snapshot build. XCTest is one of my top priorities right now, as lack of XCTest blocks people from verifying how well their libraries work when compiled by SwiftWasm.

Overall, I think it's in the state now where any help from the community would be greatly appreciated. Everyone can now [install the SwiftWasm toolchain and SDK locally with `swiftenv`](https://swiftwasm.github.io/swiftwasm-book/GettingStarted.html) and check if their package works. I hope to get [a GitHub action with the toolchain preinstalled](https://github.com/swiftwasm/swiftwasm-action) working ASAP so that people can start adding this to their CI. Now that upstream 5.3 has been branched, I'd like to start making 5.3 SwiftWasm snapshots, which hopefully converge to something stable by the time upstream Swift 5.3 toolchain is released. [Upstreaming the changes](https://github.com/swiftwasm/swift/issues/187) would also tremendously help, as it would reduce the maintenance burden we have due to re-appearing merge conflicts. Technical writing and doing basic "DevRel tasks" is also something that would help a lot. I personally work on this full-time right now, but would be deeply grateful for any help and contributions.

Thanks for reading this 🙂

---

<div class="post-metadata">

**Author:** ![ddunbar](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ddunbar/32/1933_2.png) [@ddunbar](https://forums.swift.org/u/ddunbar)\
**Post date:** [April 24, 2020, 4:48pm UTC](https://forums.swift.org/t/wasm-support/16087/27 "2020-04-24T16:48:05Z")

</div>

Thanks for the update, this is amazing progress!

---

<div class="post-metadata">

**Author:** ![Douglas\_Gregor](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/douglas_gregor/32/1362_2.png) [@Douglas\_Gregor](https://forums.swift.org/u/Douglas_Gregor)\
**Post date:** [April 24, 2020, 5:27pm UTC](https://forums.swift.org/t/wasm-support/16087/28 "2020-04-24T17:27:39Z")

</div>

This is fantastic! Is there any low-hanging fruit that interested folks could tackle in the area of code size? It seems like the kind of thing where having many eyes on it could make some easy "wins" possible.

Doug

---

<div class="post-metadata">

**Author:** ![Max\_Desiatov](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/max_desiatov/32/20654_2.png) [@Max\_Desiatov](https://forums.swift.org/u/Max_Desiatov)\
**Post date:** [April 24, 2020, 5:35pm UTC](https://forums.swift.org/t/wasm-support/16087/29 "2020-04-24T17:35:09Z")

</div>

> [@Douglas\_Gregor](#):
>
> Is there any low-hanging fruit that interested folks could tackle in the area of code size?

This has been previously discussed to some extent in [the LTO GSoC thread](https://forums.swift.org/t/gsoc2020-lto-support-for-swift/34397/7), but I myself mostly have been focusing on the infrastructure part of SwiftWasm, so I'd rather avoid retelling things I don't know well. I saw @kateinoigakukun have been doing some work and research on this, you can see his posts in that thread. I also know he recently submitted [a PR](https://github.com/apple/swift/pull/31146) that to me seems to be related to this, but again, I don't have enough expertise in that area to provide more details.

---

<div class="post-metadata">

**Author:** ![Joe\_Groff](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/joe_groff/32/1929_2.png) [@Joe\_Groff](https://forums.swift.org/u/Joe_Groff)\
**Post date:** [April 24, 2020, 6:25pm UTC](https://forums.swift.org/t/wasm-support/16087/30 "2020-04-24T18:25:20Z")

</div>

Great work! The topic of dead protocol conformances has come up in our own performance analysis of Swift binaries as a general problem for code size of Swift binaries, so progress on that front would not only benefit wasm but all targets Swift supports.

---

<div class="post-metadata">

**Author:** ![unnanego](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/unnanego/32/12042_2.png) [@unnanego](https://forums.swift.org/u/unnanego)\
**Post date:** [June 11, 2020, 12:19pm UTC](https://forums.swift.org/t/wasm-support/16087/31 "2020-06-11T12:19:49Z")

</div>

Is it possible to use WASM to access ARKit from existing browsers (Safari and Chrome)?

---

<div class="post-metadata">

**Author:** ![Max\_Desiatov](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/max_desiatov/32/20654_2.png) [@Max\_Desiatov](https://forums.swift.org/u/Max_Desiatov)\
**Post date:** [June 11, 2020, 12:23pm UTC](https://forums.swift.org/t/wasm-support/16087/32 "2020-06-11T12:23:11Z")

</div>

Hi @unnanego, as I've answered your question [here](https://github.com/swiftwasm/swift/issues/1180#issuecomment-640280573) a couple of days ago, there is no way to access ARKit from any browser, neither from WebAssembly, nor from JavaScript. ARKit is not a browser API, but a native API. Additionally, there would be no benefit in using it from browsers, since you can already build a native Swift app and call ARKit directly with all the added benefits of native apps, such as better performance, access to multi-threading, ability to optimize for a specific device, access to the rest of the native APIs etc.

---

<div class="post-metadata">

**Author:** ![unnanego](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/unnanego/32/12042_2.png) [@unnanego](https://forums.swift.org/u/unnanego)\
**Post date:** [June 11, 2020, 12:45pm UTC](https://forums.swift.org/t/wasm-support/16087/33 "2020-06-11T12:45:21Z")

</div>

oh, sorry, couldn't find that topic for some reason, so asked again. thanks for the answer. There is huge benefit in using it in a browser, because nobody wants to install the app for a 30 seconds experience. This is why 8th wall, for example, charges its users an astonishing $3000 per month.

---

<div class="post-metadata">

**Author:** ![Max\_Desiatov](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/max_desiatov/32/20654_2.png) [@Max\_Desiatov](https://forums.swift.org/u/Max_Desiatov)\
**Post date:** [June 11, 2020, 12:55pm UTC](https://forums.swift.org/t/wasm-support/16087/34 "2020-06-11T12:55:37Z")

</div>

If the experience is so time-limited, I doubt there's anything that ARKit can do that [the USDZ format](https://developer.apple.com/augmented-reality/quick-look/) can't in 30 seconds (usually plane detection on an average device takes about 30 seconds to settle in a random environment). Also, I highly doubt that ARKit (or a similar API) will ever become available in browsers before there's a widely adopted camera API for browsers. How would one operate with a hypothetical AR API if there's no API whatsoever to even receive any data from a camera? Even the most basic API to acquire a user permission to start the camera would be required.

---

<div class="post-metadata">

**Author:** ![unnanego](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/unnanego/32/12042_2.png) [@unnanego](https://forums.swift.org/u/unnanego)\
**Post date:** [June 11, 2020, 1:00pm UTC](https://forums.swift.org/t/wasm-support/16087/35 "2020-06-11T13:00:57Z")

</div>

lol: [WebXR Device API - Web APIs | MDN](https://developer.mozilla.org/en-US/docs/Web/API/WebXR_Device_API)

Quick Look doesn't allow any interactivity

---

<div class="post-metadata">

**Author:** ![Max\_Desiatov](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/max_desiatov/32/20654_2.png) [@Max\_Desiatov](https://forums.swift.org/u/Max_Desiatov)\
**Post date:** [June 11, 2020, 1:06pm UTC](https://forums.swift.org/t/wasm-support/16087/36 "2020-06-11T13:06:50Z")

</div>

I'm aware of WebXR, but it doesn't look like any browsers other than those based on the Chromium engine support it, which makes it completely impractical for any adoption. Even if/when it gains adoption, there is a camera access/privacy API discussed as you can see in [this article](https://blog.mozvr.com/experimenting-with-computer-vision-in-webxr/) in "A Note About Privacy" section.

Anyway, this is not only off-topic with regards to WebAssembly, but also not related to Swift, especially as ARKit is not a Swift-specific API. Unless you have a question about WebAssembly or SwiftWasm, I would advise against posting about AR in this topic.

---

<div class="post-metadata">

**Author:** ![jsoneaday](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jsoneaday/32/12870_2.png) [@jsoneaday](https://forums.swift.org/u/jsoneaday)\
**Post date:** [August 5, 2020, 8:44pm UTC](https://forums.swift.org/t/wasm-support/16087/37 "2020-08-05T20:44:45Z")

</div>

What are the limits of this library. Will this library convert any swift code into swiftwasm? For example if I wrote an app in swiftui would it convert that into webassembly? Probably not right.

But that would be super cool. Right once run almost anywhere ...

---

<div class="post-metadata">

**Author:** ![Max\_Desiatov](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/max_desiatov/32/20654_2.png) [@Max\_Desiatov](https://forums.swift.org/u/Max_Desiatov)\
**Post date:** [August 6, 2020, 9:11am UTC](https://forums.swift.org/t/wasm-support/16087/38 "2020-08-06T09:11:25Z")

</div>

The only limits are imposed by your WebAssembly host (which in most cases are just web browsers) and the software ecosystem available. Thus, as was discussed above, if you'd like to port your ARKit app you need comparable browser support (probably based on WebXR, which is not available in Safari yet), and some library that wraps it in an ARKit-compatible API.

We already provide a subset of the SwiftUI API that you can check out at [tokamak.dev](https://tokamak.dev), and the end goal is to get as close as possible (however unrealistic it may be to get there 100%) to the "tick the checkbox and recompile" experience. But instead of "tick the checkbox" you need to replace `import SwiftUI` with `import TokamakShim`, and obviously check if your app makes sense as a browser app and make navigation/layout adjustments for it to work. The latter is applicable to all cross-platform frameworks and apps after all.

---

<div class="post-metadata">

**Author:** ![kateinoigakukun](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/kateinoigakukun/32/16706_2.png) [@kateinoigakukun](https://forums.swift.org/u/kateinoigakukun)\
**Post date:** [March 24, 2022, 6:25am UTC](https://forums.swift.org/t/wasm-support/16087/39 "2022-03-24T06:25:34Z")

</div>

Hello, everyone. I want to share the current status of Wasm support of the compiler.

### Swift Calling Convention

We sent several patches to LLVM for supporting swiftcc on Wasm, and we've finally removed all workarounds for swiftcc in Swift compiler.

> **[⚙ D76049 \[WebAssembly\] Support swiftself and swifterror for WebAssembly target](https://reviews.llvm.org/D76049)**

### Relative Pointer

As I talked with Joe Groff above, we had decided to use absolute pointer instead of relative pointer at that time due to missing a location relative relocation type.

However, this is a hacky solution and it will break metadata memory layout on 64bit arch, and also fragile for dynamic linking situation.

So we added a location relative relocation type to Wasm obejct file spec, and implemented it in LLVM and lld. This enabled Swift compiler to emit relative pointer to data symbols.

> **[⚙ D96659 \[WebAssembly\] Add new relocation for location relative data](https://reviews.llvm.org/D96659)**

But there is still a problem when emitting relative function pointer.  
Since WebAssembly is a harvard architecture and its data and code address spaces are completely isolated, so relative offsets between relative pointer itself address and the referent function address couldn't be represented.

To address this, I propose forcing relative function to always be indirect on harvard architectures. This is similar to GOTed indirectable relative pointer, but it emits indirect pointer even though the reference and referent are in the same image.  
Of course, this indirection would add some amount of runtime performance and size overheads for extra loading and trampoline indirect pointers in data, but as far as I understand, those relative function pointer resolutions are not in performance-critical areas (correct?).

WDYT this approach, runtime people? it would be appreciated if someone could review the change.  
CC: @Joe_Groff, @John_McCall

> <https://github.com/swiftlang/swift/pull/41995/>
>
> Harvard architectures like WebAssembly cannot make direct relative function poin…ter because the referent function and the metadata are in different address spaces and the relative offset between them is not representable.
> So we need to force such relative function pointers to always be indirect for those archs.
> 
> This change shouldn't affect other than WebAssembly, and Runtime changes will come in another PR
> 
> See also: https://forums.swift.org/t/wasm-support/16087/39

---

<div class="post-metadata">

**Author:** ![Joe\_Groff](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/joe_groff/32/1929_2.png) [@Joe\_Groff](https://forums.swift.org/u/Joe_Groff)\
**Post date:** [March 25, 2022, 2:25pm UTC](https://forums.swift.org/t/wasm-support/16087/40 "2022-03-25T14:25:08Z")

</div>

Is it possible to take the addresses of functions relative to some other per-module reference point? Using the GOT unconditionally would maintain consistency with other platforms, but is overall less efficient than using absolute addresses in the first place, so if it's possible to still get some benefit from the 32-bit direct relative representation.

[Previous page](https://forums.swift.org/t/wasm-support/16087.md?page=1)

[Next page](https://forums.swift.org/t/wasm-support/16087.md?page=3)
