$30 off During Our Annual Pro Sale. View Details »

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

    View Slide

  2. About Me
    • @kateinoigakukun
    • Working at GoodNotes
    • SwiftWasm ͷϝϯςφ
    • ৽ถ Swift / CRuby / LLVM
    ίϛολ
    • Love Swift
    !
    2

    View Slide

  3. About this talk
    1. Ϟνϕʔγϣϯ
    2. SwiftWasm ͷϢʔεέʔε & σϞ
    3. աڈ2೥ͷਐḿ
    4. Future Work
    3

    View Slide

  4. Ϟνϕʔγϣϯ
    4

    View Slide

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

    View Slide

  6. SwiftͰ΍Γ͍ͨ͜ͱ
    Run Swift anywhere
    • Webϒϥ΢βͰಈ͘Swift
    • AndroidͰಈ͘Swift
    • Ϋϥ΢υϓϥοτϑΥʔϜͰಈ͘Swift
    • SwiftͰॻ͚ΔϢʔβϓϥάΠϯ (e.g. VSCode Extension)
    6

    View Slide

  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

    View Slide

  8. WebAssemblyͷ࣮ߦϑϩʔ
    8

    View Slide

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

    View Slide

  10. Demo: Run Swift on browser
    10

    View Slide

  11. 11

    View Slide

  12. 12

    View Slide

  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

    View Slide

  14. GoodNotesͷϨϯμϥ͕Web
    Ͱಈ͍ͯΔ༷ࢠ
    14

    View Slide

  15. WebAssembly is not only for Web
    15

    View Slide

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

    View Slide

  17. WASI (WebAssembly System Interface)
    WebҎ֎Ͱ΋WasmΛಈ͔ͨ͢Ίͷඪ४γεςϜίʔϧن໿
    17

    View Slide

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

    View Slide

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

    View Slide

  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

    View Slide

  21. Wasm͸ಈ͔͢؀ڥ΋ίϯύΠϥ΋خ͍͠
    • ಈ͔͢ଆ: WebAssemblyϥϯλΠϜ͑͋͞Ε͹ɺ༷ʑͳݴޠΛ
    αϙʔτ͢Δ͜ͱ͕Ͱ͖Δ
    • ίϯύΠϥଆ: WebAssemblyʹίϯύΠϧͰ͖Ε͹ɺ༷ʑͳ؀
    ڥͰಈ͔ͤΔ
    21

    View Slide

  22. Demo: Swift CLI on WebAssembly
    22

    View Slide

  23. 23

    View Slide

  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

    View Slide

  25. Demo: Swift at Compute@Edge
    25

    View Slide

  26. 26

    View Slide

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

    View Slide

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

    View Slide

  29. Relative Pointer
    SwiftͰ࢖ΘΕ͍ͯΔϙΠϯλදݱͷςΫχοΫ
    • 64bit؀ڥͰ΋32bitʹऩ·Δ
    • → όΠφϦαΠζخ͍͠
    • Ґஔಠཱ
    • → ʢେࡶ೺ʹݴ͏ͱʣ
    ΞϓϦىಈ͕଎ͯ͘خ͠
    29

    View Slide

  30. Relative Pointer
    • Wasmͷ࢓༷Ͱ͸දݱͰ͖ͳ͔ͬͨ
    template
    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(basePtr);
    auto extendOffset = (uintptr_t)(intptr_t)offset;
    return base + extendOffset;
    }
    30

    View Slide

  31. Relative Pointer
    ϋοΫ͸ਏ͍
    • ίϯύΠϥͱϥϯλΠϜ྆ํʹେྔͷมߋ͕ඞཁ
    • ΞοϓετϦʔϜͰ৽͘͠Relative Pointerͷ࢖༻͕૿͑Δͨͼ
    ʹέΞ͕ඞཁ
    31

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  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

    View Slide

  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

    View Slide

  38. Tokamak Fiber Renderer
    • React 16Ҏ߱ͱಉ౳ͷΞϧΰϦζϜ
    • ਂ͘ωετͨ͠ViewͰ΋ελοΫΦʔόʔϑϩʔ͕ى͖ͳ͍
    • ͜Ε·ͰͷStack Reconciler͸30 viewsͷωετͰ
    ελοΫΦʔόʔϑϩʔ͍ͯͨ͠
    • Fiber ReconcilerͰ͸700·ͰOK
    https://github.com/TokamakUI/Tokamak/pull/471 38

    View Slide

  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

    View Slide

  40. Help wanted!
    • ·ͩ·ͩ΍Δ͜ͱ୔ࢁ
    • ଟछଟ༷ͳ೉қ౓/ϨΠϠ
    • ॿ͚͍ͯͩ͘͞
    λεΫͷྖҬ
    40

    View Slide

  41. Future Work
    • KeyPathݺͼग़͠ن໿ΛSIL্ʹ੍ఆ
    • LLVM LTOʹΑΔίʔυ࡟ݮΛnon-AppleϓϥοτϑΥʔϜʹҠ

    • LLVMʹllvm.linker.usedΛ௥Ճͯ͠ɺϦϯΫ࣌GCΛ༗ޮԽ
    • Web APIϕʔεͷFoundationNetworkingޓ׵࣮૷
    • ܕ෇͖ͷJS <-> SwiftϒϦοδίʔυੜ੒ث
    41

    View Slide

  42. GoodNotes is hiring!
    https://www.goodnotes.com/careers
    • Senior Software Engineer, Cross Platform (Swift, Wasm)
    • Senior Software Engineer, iOS
    • etc...
    42

    View Slide

  43. Thank you :)
    43

    View Slide