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

わいわいswiftc#17Genericsの特殊化

Iceman
January 17, 2020

 わいわいswiftc#17Genericsの特殊化

Iceman

January 17, 2020
Tweet

More Decks by Iceman

Other Decks in Programming

Transcript

  1. 特殊化される様⼦を観察する // generics.swift struct A<T> { var value: T }

    @inline(never) func f() -> UInt16 { let a = A(value: UInt16(6)) // ← これが特殊化される return a.value } f() $ swift -O -Xllvm -sil-print-all -Xllvm -sil-print-only-functions=s8generics1fs6UInt16VyF generics.swift 5
  2. *** SIL module before Guaranteed Passes *** // f() sil

    hidden [noinline] [ossa] @$s8generics1fs6UInt16VyF : $@convention(thin) () - > UInt16 { bb0: %0 = alloc_stack $A<UInt16> // users: %13, %11, %9 %1 = metatype $@thin A<UInt16>.Type // user: %9 %2 = integer_literal $Builtin.IntLiteral, 6 // user: %5 %3 = metatype $@thin UInt16.Type // user: %5 // function_ref UInt16.init(_builtinIntegerLiteral:) %4 = function_ref @$ss6UInt16V22_builtinIntegerLiteralABBI_tcfC : $@convention (method) (Builtin.IntLiteral, @thin UInt16.Type) -> UInt16 // user: %5 %5 = apply %4(%2, %3) : $@convention(method) (Builtin.IntLiteral, @thin UInt16 .Type) -> UInt16 // user: %7 %6 = alloc_stack $UInt16 // users: %10, %9, %7 store %5 to [trivial] %6 : $*UInt16 // id: %7 // function_ref A.init(value:) %8 = function_ref @$s8generics1AV5valueACyxGx_tcfC : $@convention(method) <τ_0 _0> (@in τ_0_0, @thin A<τ_0_0>.Type) -> @out A<τ_0_0> // user: %9 %9 = apply %8<UInt16>(%0, %6, %1) : $@convention(method) <τ_0_0> (@in τ_0_0, @ thin A<τ_0_0>.Type) -> @out A<τ_0_0> dealloc_stack %6 : $*UInt16 // id: %10 %11 = load [trivial] %0 : $*A<UInt16> // users: %14, %12 debug_value %11 : $A<UInt16>, let, name "a" // id: %12 dealloc_stack %0 : $*A<UInt16> // id: %13 %14 = struct_extract %11 : $A<UInt16>, #A.value // user: %15 return %14 : $UInt16 // id: %15 } // end sil function '$s8generics1fs6UInt16VyF' 6
  3. ⼀番最初のフェーズではUInt16のメタタイプを渡して呼出 (↓前ページの⼀部を抜粋) %8 = function_ref @$s8generics1AV5valueACyxGx_tcfC : $@convention(method) <τ_0_0 >

    (@in τ_0_0, @thin A<τ_0_0>.Type) -> @out A<τ_0_0> // user: %9 %9 = apply %8<UInt16>(%0, %6, %1) : $@convention(method) <τ_0_0> (@in τ_0_0, @th in A<τ_0_0>.Type) -> @out A<τ_0_0> $ swift demangle s8generics1AV5valueACyxGx_tcfC $s8generics1AV5valueACyxGx_tcfC ---> generics.A.init(value: A) -> generics.A<A> 7
  4. *** SIL function after #69, stage HighLevel+EarlyLoopOpt, pass 13: GenericSpecializer

    (generic-specializer) // f() sil hidden [noinline] @$s8generics1fs6UInt16VyF : $@convention(thin) () -> UInt16 { bb0: %0 = alloc_stack $A<UInt16> // users: %9, %11, %13 %1 = metatype $@thin A<UInt16>.Type // user: %8 %2 = integer_literal $Builtin.Int16, 6 // user: %3 %3 = struct $UInt16 (%2 : $Builtin.Int16) // user: %5 %4 = alloc_stack $UInt16 // users: %7, %5, %10 store %3 to %4 : $*UInt16 // id: %5 // function_ref specialized A.init(value:) %6 = function_ref @$s8generics1AV5valueACyxGx_tcfCs6UInt16V_Tg5 : $@convention(me thod) (UInt16, @thin A<UInt16>.Type) -> A<UInt16> // user: %8 %7 = load %4 : $*UInt16 // user: %8 %8 = apply %6(%7, %1) : $@convention(method) (UInt16, @thin A<UInt16>.Type) -> A<UInt16> // user: %9 store %8 to %0 : $*A<UInt16> // id: %9 dealloc_stack %4 : $*UInt16 // id: %10 %11 = struct_element_addr %0 : $*A<UInt16>, #A.value // user: %12 %12 = load %11 : $*UInt16 // user: %14 dealloc_stack %0 : $*A<UInt16> // id: %13 return %12 : $UInt16 // id: %14 } // end sil function '$s8generics1fs6UInt16VyF' 8
  5. GenericSpecializerを通過すると特殊化された実装が⽣える // function_ref specialized A.init(value:) %6 = function_ref @$s8generics1AV5valueACyxGx_tcfCs6UInt16V_Tg5 :

    $@convention(method) (UInt16, @thin A<UInt16>.Type) -> A<UInt16> // user: %8 %7 = load %4 : $*UInt16 // user: %8 %8 = apply %6(%7, %1) : $@convention(method) (UInt16, @thin A<UInt16>.Type) -> A<UInt16> // user: %9 store %8 to %0 : $*A<UInt16> // id: %9 $ swift demangle s8generics1AV5valueACyxGx_tcfCs6UInt16V_Tg5 $s8generics1AV5valueACyxGx_tcfCs6UInt16V_Tg5 ---> generic specialization <Swift.UInt16> of generics.A.init(value: A) -> generics.A<A> 9
  6. *** SIL module after #2, stage IRGen Preparation, pass 1:

    LoadableByAddress (loadable-address) // f() sil hidden [noinline] @$s8generics1fs6UInt16VyF : $@conventi on(thin) () -> UInt16 { bb0: %0 = integer_literal $Builtin.Int16, 6 // user: %1 %1 = struct $UInt16 (%0 : $Builtin.Int16) // user: %2 return %1 : $UInt16 // id: %2 } // end sil function '$s8generics1fs6UInt16VyF' 最終的には全部消える 10
  7. 特殊化の流れ 1. 型パラつきの apply 命令を集める 2. 特殊化できないものを除外する 3. 集めた apply

    ごとに特殊化 ここでも精査され特殊化に失敗しうる 4. 特殊化に成功した apply の呼び出し先を新しい関数に置き換 えて、既存の呼び出しを削除 14
  8. 型が複雑すぎる struct A<T> { var v: T init(_ v: T)

    { self.v = v } } func use<T>(_ v: T) -> T { v } let a49 = A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A( A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A( A(A(A(A(A(A(A(A(A(Int16(9))))))))))))))) ))))))))))))))))))))))))))))))))))) let a50 = A(a49) use(a49) // ← 特殊化される use(a50) // ← されない 19
  9. 型⾃体の特殊化は⾏われない struct A<T> {} に対して struct A_Int {} みたいな型パラ 埋め込み済みの型は⽣成されない

    GenericSpecializer は apply 命令にしか処理を⾏わない 他に特殊化を⾏ってる箇所がなさそう、実際のSILを⾒てもそ れっぽい動きがなさそう そもそも型⾃体を特殊化するメリットはほとんど無いのか も?意⾒募集 21
  10. 特殊化後のコードのイメージ // @_specialize(where T == Int) func f<T: CustomStringConvertible>(_ v:

    T) -> String { if let v = v as? Int { return v.description } else { return v.description } } 同じ .description 呼び出しだが後者はwitness tableの参 照を⾏うのでオーバーヘッドがある 23
  11. 24