SwiftのWebAssembly対応の軌跡 / The State of Swift WebAssembly

7a4968fbcd56e81f95a4f3c186141b52?s=47 Yuta Saito
September 21, 2020

SwiftのWebAssembly対応の軌跡 / The State of Swift WebAssembly

7a4968fbcd56e81f95a4f3c186141b52?s=128

Yuta Saito

September 21, 2020
Tweet

Transcript

  1. SwiftͷWebAssemblyରԠͷي੻ iOSDC 2020 @kateinoigakukun 1

  2. ࣗݾ঺հ @kateinoigakukun • Intern at Merpay Expert Team • Love

    Swift ! 2
  3. What is WebAssembly? WebAssembly (Wasm) ͱ͸ελοΫϕʔεͷԾ૝Ϛγϯ্Ͱಈ ໋͘ྩηοτͷҰͭͰ͢ɻWebAssembly͸C΍C++΍RustͳͲ ͔Βϙʔλϒϧͳλʔήοτͱͯ͠ίϯύΠϧ͞ΕΔΑ͏ʹઃ ܭ͞Ε͓ͯΓɺΫϥΠΞϯταΠυɺαʔόʔαΠυͷΞϓϦ έʔγϣϯΛಈ͔͢͜ͱ͕Ͱ͖·͢ɻ1.

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

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

  6. Swift x WebAssembly? 6

  7. Swift ͕WebAssemblyʹରԠ͢Δͱخ͍͜͠ͱ • ΞϓϦΛॻ͖΍͍͢SwiftͰWebϑϩϯτΤϯυ։ൃ͕Մೳʹ • Server Side Swift΍iOS, macOSͱίʔυڞ༗Ͱ͖Δ •

    Swift͕ରԠ͍ͯ͠ͳ͍؀ڥͰ΋WasmϥϯλΠϜ͑͋͞Ε͹ ಈ͘ 7
  8. SwiftWasm Project https://swiftwasm.org SwiftͷWebAssemblyରԠΛਐΊΔϓϩδΣΫτ • ϝϯςφ: @zhuowei, @MaxDesiatov, @kateinoigakukun •

    swiftwasm/swift • swiftwasm/llvm-project • etc... 8
  9. Demo https://swiftwasm-pad.netlify.app SwiftͷΦϯϥΠϯ࣮ߦ؀ڥ • ϒϥ΢β্ͰSwiftΛ࣮ߦ • ΠϯλϥΫςΟϒͳૢ࡞͕ Մೳ 9

  10. https://swiftwasm-pad.netlify.app 10

  11. SwiftWasmϓϩδΣΫτͷಓͷΓ 11

  12. Compiler Pipeline http://llvm.org/devmtg/2016-10/slides/GroffLattner-SILHighLevelIR.pdf 12

  13. LLVM for WebAssembly • SwiftWasmϓϩδΣΫτҎલ͔ΒWebAssemblyΛαϙʔτ • ʢجຊతʹʣίϯύΠϥ͸ग़ྗλʔήοτΛมߋ͢Δ͚ͩͰ WebAssemblyରԠՄೳ • Swiftݻ༗ͷݺͼग़͠ن໿͸Wasm্Ͱಈ͔ͳ͍

    13
  14. Wasm্Ͱ্ख͘ಈ͘Swiftݺͼग़͠ن໿Λ੍ఆ https://reviews.llvm.org/D76049 14

  15. Swift Compiler • ϥϯλΠϜϥΠϒϥϦ&ඪ४ϥΠϒϥϦͷWasmαϙʔτ • ඪ४ϥΠϒϥϦͷςετέʔε100% Pass 15

  16. طଘίϯϙʔωϯτͷWasm ରԠ·ͱΊ ίϯϙʔωϯτ WebAssemblyରԠ LLVM Swift Compiler (WIP) SwiftPM Foundation

    16
  17. Swift for WebAssemblyͷΤίγεςϜ 17

  18. swiftwasm/JavaScriptKit JavaScriptͱͷόΠϯσΟϯάϥΠϒϥϦ • JavaScript likeͳΦϒδΣ Ϋτૢ࡞APIΛఏڙ • JavaScriptͱSwiftͷGCͷ ࠩΛҰ෦ٵऩ import

    JavaScriptKit let alert = JSObject.global.alert.function! let document = JSObject.global.document.object! let div = document.createElement!("div").object! div.innerText = "Hello, world" let body = document.body.object! body.appendChild!(div) alert("Swift is running on browser!") 18
  19. swiftwasm/carton ! SwiftWasm੡WebΞϓϦͷϏϧυπʔϧ • Zero-config • πʔϧνΣʔϯΠϯετʔϧͷࣗಈԽ • WebΞϓϦ։ൃ࣌ͷAuto reload

    19
  20. TokamakUI/Tokamak SwiftUIޓ׵ͷUIϥΠϒϥϦ • ΫϩεϓϥοτϑΥʔϜΛ લఏʹઃܭ • WebAssembly/DOMɺ Static HTMLΛαϙʔτ import

    TokamakDOM struct Counter: View { @State var count: Int = 0 var body: some View { VStack { Text("\(count)") HStack { Button("Reset") { count = 0 } Button("Increment") { count += 1 } } } } } 20
  21. ݱঢ়ͷ՝୊ 21

  22. όΠφϦαΠζ໰୊ ଞͷݴޠͱൺ΂Wasmʹͨ͠ͱ͖ͷαΠζ͕େ͖͍ e.g. Hello, World ͷόΠφϦαΠζ Language Size Swift 8.5

    MB Go 2.4 MB Rust 70 KB 22
  23. όΠφϦαΠζ໰୊ (ICU) • Swift͸Unicodeσʔλϕʔ ε΍ਖ਼نԽͷͨΊʹICUʹ ґଘ͍ͯ͠Δ • ICU͚ͩͰ4MBҎ্ let emoji:

    UnicodeScalar = " " emoji.properties.isEmoji // true // Pokémon let str1 = "Poke\u{0301}mon" // Pokémon let str2 = "Pok\u{00e9}mon" str1 == str2 // true ICU = International Components for Unicode 23
  24. όΠφϦαΠζ໰୊ (ඪ४ϥΠϒϥϦ) • Swiftͷඪ४ϥΠϒϥϦ͸ඇৗʹେ͖͍ • ࢖ΘΕͳ͍ػೳ΋όΠφϦʹؚ·Εͯ͠·͏ 24

  25. ৽͍͠όΠφϦαΠζ࠷దԽ SwiftͷGoogle Summer of Code ϓϩδΣΫτͷҰͭ https://summerofcode.withgoogle.com/projects/#6691362033893376 25

  26. ৽͍͠όΠφϦαΠζ࠷దԽ ϞδϡʔϧΛލ͍Ͱ࢖ΘΕͳ͍ίʔυΛ࡟আ͢Δ͜ͱͰόΠφ ϦαΠζΛ࡟ݮ // LibX.swift public protocol P { func

    foo() func unusedMethod() } public struct S: P { public func foo() {} public func unusedMethod() {} } // main.swift import LibX func useP<T: P>(_ value: T) { value.foo() } useP(S()) 26
  27. ৽͍͠όΠφϦαΠζ࠷దԽ ϞδϡʔϧΛލ͍Ͱ࢖ΘΕͳ͍ίʔυΛ࡟আ͢Δ͜ͱͰόΠφ ϦαΠζΛ࡟ݮ // LibX.swift public protocol P { func

    foo() func unusedMethod() } public struct S: P { public func foo() {} public func unusedMethod() {} } // main.swift import LibX func useP<T: P>(_ value: T) { value.foo() } useP(S()) 26
  28. ৽͍͠όΠφϦαΠζ࠷దԽ • SILϨϕϧͰSwiftͷηϚϯςΟοΫΛ࢖ͬͯ࠷దԽ • LLVMϨϕϧΑΓੵۃతͳ࠷దԽ͕ߦ͑Δ 27

  29. ࠷దԽޙ Language Size Swift 8.5 MB Swift (Optimized) 3.5 MB

    Go 2.4 MB Rust 70 KB 28
  30. ৄ͘͠஌Γ͍ͨํ޲͚ • kateinoigakukun/swift-lto-benchmark • Swift Forum: https://forums.swift.org/t/refactoring-plan-of- silvisitor-for-lto/37678/14 29

  31. ·ͱΊ • ࣮ݧஈ֊ͱͯ͠ར༻Մೳ • पลπʔϧ΍ϥΠϒϥϦͷରԠ΋ਐΜͰ͍Δ • όΠφϦαΠζ͸վળͷ༨஍͋Γ 30

  32. ؔ࿈ϦϯΫ • kateinoigakukun/swiftwasm-pad • https://swiftwasm.github.io/swiftwasm-book/ 31