Slide 1

Slide 1 text

The state of Swift for WebAssembly in 2022 iOSDC 2022 @kateinoigakukun 1

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

Ϟνϕʔγϣϯ 4

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

WebAssemblyͷ࣮ߦϑϩʔ 8

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

Demo: Run Swift on browser 10

Slide 11

Slide 11 text

11

Slide 12

Slide 12 text

12

Slide 13

Slide 13 text

ϓϩμΫγϣϯͰͷSwiftWasmͷϢʔεέʔε • GoodNotes1 • Note-taking app • Web Viewer for sharing • Luminal2 • Python notebook on browser 2 https://getluminal.com/ 1 https://www.goodnotes.com/ 13

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

WebAssembly is not only for Web 15

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

Demo: Swift CLI on WebAssembly 22

Slide 23

Slide 23 text

23

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

Demo: Swift at Compute@Edge 25

Slide 26

Slide 26 text

26

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

WebAssemblyͷͨΊʹSwiftΛ௚͢ 33

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

μ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

Slide 40

Slide 40 text

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

Slide 41

Slide 41 text

Future Work • KeyPathݺͼग़͠ن໿ΛSIL্ʹ੍ఆ • LLVM LTOʹΑΔίʔυ࡟ݮΛnon-AppleϓϥοτϑΥʔϜʹҠ ২ • LLVMʹllvm.linker.usedΛ௥Ճͯ͠ɺϦϯΫ࣌GCΛ༗ޮԽ • Web APIϕʔεͷFoundationNetworkingޓ׵࣮૷ • ܕ෇͖ͷJS <-> SwiftϒϦοδίʔυੜ੒ث 41

Slide 42

Slide 42 text

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

Slide 43

Slide 43 text

Thank you :) 43