Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compile Swift to WebAssembly

The LLVM infrastructure now supports compiling from LLVM IR to WebAssembly (at least experimentally). Swift uses the LLVM compiler infrastructure and can easily be compiled to LLVM IR. So I thought it would be straightforward to compile some Swift code to LLVM IR and then to WebAssembly.

It turned out not to be that easy, however. It looks like LLVM IR is not entirely platform independent? Whatever the reason behind the scenes, when compiling Swift to LLVM IR, a target architecture must be specified and WebAssembly is not available.

I have two questions then:

1) Am I correct that there is currently (as of October 2017) no way to compile Swift to WebAssembly?

2) What would it take to make WebAssembly a supported target for Swift to LLVM IR compilation?

like image 570
Rob Mecham Avatar asked Oct 04 '17 18:10

Rob Mecham


People also ask

What can compile to WebAssembly?

Binaryen. Binaryen is a compiler toolchain infrastructure library for WebAssembly. Written in C++, Binaryen is intended to make compiling to WebAssembly easy, effective, and fast. It has a C API in a single header, and it can be used from JavaScript.

Can go compile to WebAssembly?

WebAssembly can be compiled by Golang itself or with TinyGo. At the time of writing Go version 1.18 is the latest version and does not support WASI, if you wish to compile to WASI you need to use TinyGo.

What languages work with WebAssembly?

Since it's a well-defined format, any language can have Wasm as a compilation target. Consequently, there are now around 40 high-level programming languages that support WebAssembly, including C and C++, Python, Go, Rust, Java, and PHP.

Do mobile browsers support WebAssembly?

You can use WebAssembly in Safari, Chrome, Firefox, Edge and in mobile browsers like iOS Safari, Firefox for Android, and Chrome.


2 Answers

1) To the best of my knowledge as of early Nov, 2017 you are correct: there is no commonly available way to compile Swift to WebAssembly. Maybe some enterprising hacker somewhere has made it happen but if so she hasn't shared her code with us yet.

2) In order to enable Wasm support you will probably need to hack on a few different parts. I think you could do it without knowing much of anything about the internals of the compiler (e.g. the parser & optimizers), but you'd need to learn about how the toolchain works and how it integrates with the platform at runtime.

You can learn a ton about what you'd need to do by studying how Swift was ported to Android. Luckily, Brian Gesiak posted a really detailed blog post about exactly how that port worked (warning: small Patreon donation required):

https://modocache.io/how-to-port-the-swift-runtime-to-android

Seriously, you would be nuts to embark on this project without reading that article.

Though I'm NOT an expert, based on that port and my (basic) understanding of Swift, I think the rough overview of where you'd need to hack would be:

  • The Swift compiler
    • You'll need to teach it about the Wasm "triple" used by LLVM, so it knows how to integrate with the rest of its toolchain
    • You'll need to set up a WebAssembly platform so that people can write #if os(WebAssembly) in places that require conditional compilation
    • You'll also need to set up similar build-time macros. The Android article explains this sort of thing really well.
  • The Swift runtime
    • This is written in C++ and it needs to run on Wasm
    • Since Wasm is an unusual platform there will probably be some work here. You might need to provide compatibility shims for various system calls and the like.
    • Projects like Emscripten have demonstrated lots of success compiling C++ to Wasm.
  • The Swift standard library
    • In theory you can write & run Swift code that doesn't use the standard library, but who would want to?
    • Also in theory this should "just work" if the runtime works, but you will likely need to use your #if os(WebAssembly) feature here to work around platform irregularities
  • Bonus: The Foundation and Dispatch libraries
    • If you want to use existing Swift code these two libraries will be essential.

Links:

  • Brian Gesiak's awesome blog post: https://modocache.io/how-to-port-the-swift-runtime-to-android
  • Link to the Android port's pull request: https://github.com/apple/swift/pull/1442
  • Interesting article about the challenges and rewards of cross-platform Swift: https://medium.com/@ephemer/how-we-put-an-app-in-the-android-play-store-using-swift-67bd99573e3c
like image 150
n8gray Avatar answered Sep 30 '22 11:09

n8gray


It looks like there is a commercial offering that supports compilation of Swift to WebAssembly. RemObjects, the developer tooling company, has just announced support for WebAssembly with their Elements compiler, which can compile Java, Swift, C# and Oxygene.

like image 20
ColinE Avatar answered Sep 30 '22 09:09

ColinE