Slide 1

Slide 1 text

Scala to WebAssembly: How and Why Rikito Taniguchi (@tanishiking) Scala Compiler Engineer @ VirtusLab

Slide 2

Slide 2 text

2 Scala の Wasm サポートがくる! WebAssembly support is coming!

Slide 3

Slide 3 text

3 ● What is Wasm ● Current Wasm Support in Scala ● Why Wasm (in future) ● How to Wasm ● Future

Slide 4

Slide 4 text

4 What is WebAssembly (Wasm) WebAssembly は安全・高速・環境や言語に非依存なプログラム形式 WebAssembly はバイナリのプログラム形式 高速な起動と、サンドボックス実行などによる安全性 “WebAssembly is a binary instruction format for a stack-based virtual machine…” Fast (startup) ● Decoding Wasm binary is faster than parsing JS ● Decode, validate, compile in stream / in parallel Secure ● Runs in a sandboxed environment ● Wasm design eliminates dangerous features from its execution semantic

Slide 5

Slide 5 text

5 What is WebAssembly (Wasm) WebAssembly は環境や言語に非依存なプログラム形式

Slide 6

Slide 6 text

6 ● What is Wasm ● Current Wasm Support in Scala ● Why Wasm (in future) ● How to Wasm ● Future

Slide 7

Slide 7 text

7 ScalaのWasmサポートは Scala.js のバックエンドとして実装 Scala.js の中間言語から JS ではなく Wasm + JS helper を生成する Dialect of Scala targeting JavaScript platform ● Frontend compiles Scala code to Scala.js IR (SJSIR) ● Linker backend generates Wasm from SJSIR (instead of JS) Wasm Linker backend in Scala.js NEW!

Slide 8

Slide 8 text

8 Scala.js により生成される Wasm は JS ヘルパーも含まれる JS 環境で実行できるが、non-js runtime では実行できない Generated Wasm runs on JS environment, but it doesn’t run on non-JS runtimes (wasmedge, wasmtime…) Wasm Linker backend in Scala.js NEW! Doesn’t work on non-JS runtime yet

Slide 9

Slide 9 text

9 ● Fast startup time ● Fast execution (depends on app) ● Small code size ● Sandboxed environment that’s all? WasmはScala.jsに 高速起動・(アプリの性質によるが)実行速度・小さいコート サイズ・サンドボックス環境を提供。それだけ? Wasm provides Scala.js…

Slide 10

Slide 10 text

10 ● What is Wasm ● Current Wasm Support in Scala ● Why Wasm (in future) ● Wasm is beyond browsers ● Wasm component Model ● Evolving Landsacape ● How to Wasm ● Future

Slide 11

Slide 11 text

11 ● Serverless/FaaS (Fastly edge, Fermyon Spin, CF workers) ● Containers/k8s (containerd-wasm-shim) ● Plugins/Extensions (VSCode, Envoy, UDF in TiDB) Wasm is beyond browser 近年では Serverless, containers, plugins の実行環境など ブラウザに限らない環境で活躍 Why? - Wasm’s fast startup time, sandboxing, and portability

Slide 12

Slide 12 text

12 Wasm module access host environment through a Standardized interfaces (WASI) to access system resources WASI (WebAssembly System Interface) WASI はシステムリソースにアクセスするための標準化されたインタフェース

Slide 13

Slide 13 text

13 WebAssembly Component Model Wasm module は i32 などの限られた型のみを使って import / export 文字列などの高度な型を表現する標準的なABIがない There’s no standard way to serialize high-level types across modules Wasm modules import/export functions only with i32, i64, f32, f64

Slide 14

Slide 14 text

14 WebAssembly Component Model Wasm component model は Wasm component 間で高度な型を使って直接 やり取りする方法を提供 Defines standardized way to directly communicate between Wasm components using high-level types.

Slide 15

Slide 15 text

15 Canonical ABI A standardized way to represent data types in binary WIT (Wasm Interface Types) A language for describing the interfaces and data types. Tools generate glue code that conforms to the ABI from WIT. Component Model under the hood ABI とインターフェース定義言語により構成される

Slide 16

Slide 16 text

16 Proposals ● WebAssembly SIMD proposal ● Stack Switching proposal ○ Native Delimited Control support (for green threads, etc) Tools ● Warg (Wasm component registry) ● wasm-opt Evolving LandScape SIMD サポート・軽量スレッドなど component model 周りのツールの進化

Slide 17

Slide 17 text

17 ● What is Wasm ● Current Wasm Support in Scala ● Why Wasm (in future) ● How to Wasm ● Future

Slide 18

Slide 18 text

18 Wasm native GC was not well supported in 2019 Without Wasm native GC, there’s a problem with interoperability between JavaScript and WebAssembly. (cycle collection problem) Why Scala hadn’t support Wasm? G “Scala.js and WebAssembly a tale of the dangers of the sea” from ScalaDays 2019 Lausanne, by Sébastien Doeraene (author of Scala.js) https://www.youtube.com/watch?v=QsOHofFJpig Wasmは2019年から考えられていたが、当時WasmGCが未成熟。適切にJSと の相互呼び出しができない

Slide 19

Slide 19 text

19 Compile VM to Wasm ● Module size tends to huge ● Good for legacy applications Embed own GC ● GC need to inspect the stack to find GC roots, which isn’t possible in Wasm, requires a shadow stack. ● Cycle-collection problem Compile Scala to Wasm in 2019 WasmGC以前にWasm対応するとしたら? JVM on Wasm・GCをWasm moduleに埋め込む。どちらにも問題がある

Slide 20

Slide 20 text

20 GC-managed (by Wasm runtime) data structures and instructions Wasm Garbage Collection (WasmGC) Directly compile GC-languages to Wasm without embedding GC Kotlin, Java, OCaml, Dart Wasm1.0には数値型のみ。WasmGCはstructやarrayなどのWasm runtime にGC管理されたデータ型を導入する。

Slide 21

Slide 21 text

21 Many Wasm runtimes support WasmGC by default in 2024 ● JS engines ○ V8 (Deno 1.38 / Node.js 22 / Chrome 119) ○ SpiderMonkey (Firefox 120) ○ 🚧 JavaScriptCore (Safari, soon?) ● Stand-alone Wasm VMs ○ WasmEdge (0.14.0) ○ 🚧 Wasmtime ○ 🚧 WAMR WasmGC support status 2019年当時はWasmGCはearly stageだった。 今では多くの Wasm runtime が WasmGC をサポートしている。 It’s right time to support Wasm in Scala!

Slide 22

Slide 22 text

22 Scala の Wasm サポートがくる! We compile Scala.js to Wasm with WasmGC

Slide 23

Slide 23 text

23 ● Support non-JS Wasm ○ Get rid of JS interop semantics ○ WASI support ○ Re-implement JS-dependent libs ● Wasm Component Model Support ● More optimizations ○ Scala.js optimizer ○ Better code generation ○ wasm-opt Future 現状の実装はJS環境でのみ動作。将来的にはクラウド環境での利用・Wasm Component による任意の言語との interoperability を考えている

Slide 24

Slide 24 text

24 ● Wasm is fast, safe, polyglot, and portable binary program format ● Scala is compiling to WasmGC via Scala.js IR ○ Currently, Scala supports Wasm on browser ● Wasm has potential in non-JS environment + inter-communicate with different source languages. Conclusion Wasmはブラウザ外でも活躍し言語間のinteropのための新しいレイヤ Scala.jsからWasmGCを使って効率の良いバイナリを生成

Slide 25

Slide 25 text

25 ScalaNative compiles Scala to LLVM IR ● Relies on LLVM, Emscripten, and WASI-SDK to support Wasm! ● ScalaNative libs work out of the box in stand-alone Wasm runtime but… ● LLVM doesn’t support WasmGC ○ Need to embed GC to Wasm module, that is expensive ○ GC cycle-collection problem Appendix: How about Scala Native? ScalaNative は LLVM を活用、LLVM経由でWasmサポートの検討 LLVM は WasmGC 対応しておらず、GCを組み込むことになる。