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

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

Yuta Saito
September 21, 2020

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

Yuta Saito

September 21, 2020
Tweet

More Decks by Yuta Saito

Other Decks in Programming

Transcript

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  6. Swift x WebAssembly?
    6

    View full-size slide

  7. Swift ͕WebAssemblyʹରԠ͢Δͱخ͍͜͠ͱ
    • ΞϓϦΛॻ͖΍͍͢SwiftͰWebϑϩϯτΤϯυ։ൃ͕Մೳʹ
    • Server Side Swift΍iOS, macOSͱίʔυڞ༗Ͱ͖Δ
    • Swift͕ରԠ͍ͯ͠ͳ͍؀ڥͰ΋WasmϥϯλΠϜ͑͋͞Ε͹
    ಈ͘
    7

    View full-size slide

  8. SwiftWasm Project
    https://swiftwasm.org
    SwiftͷWebAssemblyରԠΛਐΊΔϓϩδΣΫτ
    • ϝϯςφ: @zhuowei, @MaxDesiatov, @kateinoigakukun
    • swiftwasm/swift
    • swiftwasm/llvm-project
    • etc...
    8

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  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

    View full-size slide

  19. swiftwasm/carton
    !
    SwiftWasm੡WebΞϓϦͷϏϧυπʔϧ
    • Zero-config
    • πʔϧνΣʔϯΠϯετʔϧͷࣗಈԽ
    • WebΞϓϦ։ൃ࣌ͷAuto reload
    19

    View full-size slide

  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

    View full-size slide

  21. ݱঢ়ͷ՝୊
    21

    View full-size slide

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

    View full-size slide

  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

    View full-size slide

  24. όΠφϦαΠζ໰୊ (ඪ४ϥΠϒϥϦ)
    • Swiftͷඪ४ϥΠϒϥϦ͸ඇৗʹେ͖͍
    • ࢖ΘΕͳ͍ػೳ΋όΠφϦʹؚ·Εͯ͠·͏
    24

    View full-size slide

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

    View full-size slide

  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(_ value: T) {
    value.foo()
    }
    useP(S())
    26

    View full-size slide

  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(_ value: T) {
    value.foo()
    }
    useP(S())
    26

    View full-size slide

  28. ৽͍͠όΠφϦαΠζ࠷దԽ
    • SILϨϕϧͰSwiftͷηϚϯςΟοΫΛ࢖ͬͯ࠷దԽ
    • LLVMϨϕϧΑΓੵۃతͳ࠷దԽ͕ߦ͑Δ
    27

    View full-size slide

  29. ࠷దԽޙ
    Language Size
    Swift 8.5 MB
    Swift (Optimized) 3.5 MB
    Go 2.4 MB
    Rust 70 KB
    28

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide