Upgrade to Pro — share decks privately, control downloads, hide ads and more …

The state of Swift for WebAssembly in 2022

Yuta Saito
September 12, 2022

The state of Swift for WebAssembly in 2022

Yuta Saito

September 12, 2022
Tweet

More Decks by Yuta Saito

Other Decks in Technology

Transcript

  1. The state of Swift for WebAssembly in 2022 iOSDC 2022

    @kateinoigakukun 1
  2. About Me • @kateinoigakukun • Working at GoodNotes • SwiftWasm

    ͷϝϯςφ • ৽ถ Swift / CRuby / LLVM ίϛολ • Love Swift ! 2
  3. About this talk 1. Ϟνϕʔγϣϯ 2. SwiftWasm ͷϢʔεέʔε & σϞ

    3. աڈ2೥ͷਐḿ 4. Future Work 3
  4. Ϟνϕʔγϣϯ 4

  5. Swiftͷ޷͖ͳͱ͜Ζ • ίϯύΠϧ͕௨Ε͹େମಈ͘ • ਖ਼͍͠ίʔυΛ؆୯ʹॻͨ͘ΊͷIDEࢧԉ • ॊೈͳܕγεςϜ • ஋ܕࢦ޲ •

    ConcurrencyͰਅՁΛൃش 5
  6. SwiftͰ΍Γ͍ͨ͜ͱ Run Swift anywhere • Webϒϥ΢βͰಈ͘Swift • AndroidͰಈ͘Swift • Ϋϥ΢υϓϥοτϑΥʔϜͰಈ͘Swift

    • SwiftͰॻ͚ΔϢʔβϓϥάΠϯ (e.g. VSCode Extension) 6
  7. WebAssembly is a game changer A binary instruction format for

    a stack-based machine Designed to be • Portable • Size- and Load-time efficient • Secure by Sandbox 7
  8. WebAssemblyͷ࣮ߦϑϩʔ 8

  9. SwiftWasm Project https://swiftwasm.org SwiftͷWebAssemblyରԠΛਐΊΔϓϩδΣΫτ • ίϯύΠϥΛ௚ͯ͠ύονΛૹͬͨΓɺ • WebAssembly޲͚ͷπʔϧνΣΠϯΛ഑෍ͨ͠Γɺ • JavaScriptKitͳͲͷपลϥΠϒϥϦͷ։ൃ

    9
  10. Demo: Run Swift on browser 10

  11. 11

  12. 12

  13. ϓϩμΫγϣϯͰͷSwiftWasmͷϢʔεέʔε • GoodNotes1 • Note-taking app • Web Viewer for

    sharing • Luminal2 • Python notebook on browser 2 https://getluminal.com/ 1 https://www.goodnotes.com/ 13
  14. GoodNotesͷϨϯμϥ͕Web Ͱಈ͍ͯΔ༷ࢠ 14

  15. WebAssembly is not only for Web 15

  16. WebAssembly is not only for Web • αϯυϘοΫεʹΑΔ҆શੑɺϙʔλϒϧͳΞʔΩςΫνϟɺ ଟݴޠαϙʔτ͸ଞͷ෼໺Ͱ΋خ͍͠ •

    ServerlessϓϥοτϑΥʔϜ • ϓϥάΠϯγεςϜ • and more… 16
  17. WASI (WebAssembly System Interface) WebҎ֎Ͱ΋WasmΛಈ͔ͨ͢Ίͷඪ४γεςϜίʔϧن໿ 17

  18. https://hacks.mozilla.org/2019/03/standardizing-wasi-a-webassembly-system-interface/ 18

  19. https://hacks.mozilla.org/2019/03/standardizing-wasi-a-webassembly-system-interface/ 19

  20. WASI compatible things Platforms • Node.js / Deno / Wasmtime

    • Fastly Compute@Edge • Cloudflare Workers • VSCode Extensions • Fluent Bit • Etc… Languages • C / C++ • Rust • Swift • Ruby • Etc… 20
  21. Wasm͸ಈ͔͢؀ڥ΋ίϯύΠϥ΋خ͍͠ • ಈ͔͢ଆ: WebAssemblyϥϯλΠϜ͑͋͞Ε͹ɺ༷ʑͳݴޠΛ αϙʔτ͢Δ͜ͱ͕Ͱ͖Δ • ίϯύΠϥଆ: WebAssemblyʹίϯύΠϧͰ͖Ε͹ɺ༷ʑͳ؀ ڥͰಈ͔ͤΔ 21

  22. Demo: Swift CLI on WebAssembly 22

  23. 23

  24. Web Ҏ֎ͷSwiftWasmͷϢʔεέʔε • Fastly Compute@Edge3 • Serverless platform based on

    WebAssembly • Enarx4 • WebAssembly + Confidential Computing on TEE (Trusted Execution Environment) 4 https://enarx.dev/docs/webassembly/swift 3 https://www.fastly.com/jp/blog/introducing-serverless-swift-building-on-compute-edge-with-andrew-barba 24
  25. Demo: Swift at Compute@Edge 25

  26. 26

  27. For more info https://swift.cloud/blog/deploy-server-side-swift-to-fastly 27

  28. ٕज़తͳਐḿ (2020~2022) • Relative PointerαϙʔτΛWasmʹ௥Ճ • 70Ҏ্ͷύονΛΞοϓετϦʔϜʹϚʔδ • Swift ConcurrencyʹରԠ

    • Tokamak Fiber Renderer • μWASI 28
  29. Relative Pointer SwiftͰ࢖ΘΕ͍ͯΔϙΠϯλදݱͷςΫχοΫ • 64bit؀ڥͰ΋32bitʹऩ·Δ • → όΠφϦαΠζخ͍͠ • Ґஔಠཱ

    • → ʢେࡶ೺ʹݴ͏ͱʣ ΞϓϦىಈ͕଎ͯ͘خ͠ 29
  30. Relative Pointer • Wasmͷ࢓༷Ͱ͸දݱͰ͖ͳ͔ͬͨ template<typename BasePtrTy, typename Offset> uintptr_t applyRelativeOffset(BasePtrTy

    *basePtr, Offset offset) { #ifdef __wasm__ // WebAssembly: hack: disable relative pointers return (uintptr_t)(intptr_t)offset; #endif auto base = reinterpret_cast<uintptr_t>(basePtr); auto extendOffset = (uintptr_t)(intptr_t)offset; return base + extendOffset; } 30
  31. Relative Pointer ϋοΫ͸ਏ͍ • ίϯύΠϥͱϥϯλΠϜ྆ํʹେྔͷมߋ͕ඞཁ • ΞοϓετϦʔϜͰ৽͘͠Relative Pointerͷ࢖༻͕૿͑Δͨͼ ʹέΞ͕ඞཁ 31

  32. WasmʹRelative PointerαϙʔτΛ௥Ճ • WasmଆʹRelative Pointer ʹඞཁͳػೳΛఏҊ&࣮૷ 32

  33. WebAssemblyͷͨΊʹSwiftΛ௚͢ 33

  34. WebAssemblyͷͨΊʹSwiftΛ௚͢ SwiftͷͨΊʹWebAssemblyΛ௚͢ 34

  35. ϋοΫ͕ফͤͯϋοϐʔ 35

  36. Upstreaming patches • 44 patches for apple/swift • 16 patches

    for apple/swift-corelibs-foundation • 14 patches for apple/swift-package-manager • 4 patches for LLVM They are made by only 2 people! 36
  37. Swift Concurrency on Wasm JavaScriptKit provides a global executor implementation

    based on JS event-loop import JavaScriptEventLoop import JavaScriptKit JavaScriptEventLoop.installGlobalExecutor() func printZen() async throws { let fetch = JSObject.global.fetch.function! let response = try await JSPromise(fetch("https://api.github.com/zen").object!)!.value let text = try await JSPromise(response.text().object!)!.value print(text) } 37
  38. Tokamak Fiber Renderer • React 16Ҏ߱ͱಉ౳ͷΞϧΰϦζϜ • ਂ͘ωετͨ͠ViewͰ΋ελοΫΦʔόʔϑϩʔ͕ى͖ͳ͍ • ͜Ε·ͰͷStack

    Reconciler͸30 viewsͷωετͰ ελοΫΦʔόʔϑϩʔ͍ͯͨ͠ • Fiber ReconcilerͰ͸700·ͰOK https://github.com/TokamakUI/Tokamak/pull/471 38
  39. μWASI (swiftwasm/uwasi) • ܰྔWASI࣮૷ for ϒϥ΢β & Node.js • ैདྷͷ3rd-partyͷ࣮૷͸େ͖͗ͨ͢…

    (65.6 KB) • େ൒ͷػೳ͸࢖ͬͯͳ͔ͬͨ • SwiftWasmͰ৽͍͠ϞϊΛఏڙ: • Minimal: 3 KB • All features enabled: 6 KB https://github.com/swiftwasm/uwasi 39
  40. Help wanted! • ·ͩ·ͩ΍Δ͜ͱ୔ࢁ • ଟछଟ༷ͳ೉қ౓/ϨΠϠ • ॿ͚͍ͯͩ͘͞ λεΫͷྖҬ 40

  41. Future Work • KeyPathݺͼग़͠ن໿ΛSIL্ʹ੍ఆ • LLVM LTOʹΑΔίʔυ࡟ݮΛnon-AppleϓϥοτϑΥʔϜʹҠ ২ • LLVMʹllvm.linker.usedΛ௥Ճͯ͠ɺϦϯΫ࣌GCΛ༗ޮԽ

    • Web APIϕʔεͷFoundationNetworkingޓ׵࣮૷ • ܕ෇͖ͷJS <-> SwiftϒϦοδίʔυੜ੒ث 41
  42. GoodNotes is hiring! https://www.goodnotes.com/careers • Senior Software Engineer, Cross Platform

    (Swift, Wasm) • Senior Software Engineer, iOS • etc... 42
  43. Thank you :) 43