Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// `-O`あり
// Factory.init(() -> A) -> A
sil hidden @(extension in main):main.Factory.init (() -> A) -> A : $@convention(method) <Self where Self : Factory> (@owned @callee_owned () -> @out Self, @thick Self.Type) -> @out Self {
// %0 // user: %4
// %1 // users: %4, %3
bb0(%0 : $*Self, %1 : $@callee_owned () -> @out Self, %2 : $@thick Self.Type):
debug_value %1 : $@callee_owned () -> @out Self, let, name "f", argno 1, loc "<stdin>":3:12, scope 13 // id: %3
%4 = apply %1(%0) : $@callee_owned () -> @out Self, loc "<stdin>":4:18, scope 14
%5 = tuple (), scope 14 // user: %6
return %5 : $(), loc "<stdin>":5:5, scope 14 // id: %6
} // end sil function '(extension in main):main.Factory.init (() -> A) -> A'
// `-O`なし
// Factory.init(() -> A) -> A
sil hidden @(extension in main):main.Factory.init (() -> A) -> A : $@convention(method) <Self where Self : Factory> (@owned @callee_owned () -> @out Self, @thick Self.Type) -> @out Self {
// %0 // user: %10
// %1 // users: %11, %7, %5, %4
bb0(%0 : $*Self, %1 : $@callee_owned () -> @out Self, %2 : $@thick Self.Type):
%3 = alloc_stack $Self, var, name "self", loc "<stdin>":3:5, scope 2 // users: %8, %10, %12, %14
debug_value %1 : $@callee_owned () -> @out Self, let, name "f", argno 1, loc "<stdin>":3:12, scope 2 // id: %4
strong_retain %1 : $@callee_owned () -> @out Self, loc "<stdin>":4:16, scope 3 // id: %5
%6 = alloc_stack $Self, loc "<stdin>":4:18, scope 3 // users: %9, %8, %7
%7 = apply %1(%6) : $@callee_owned () -> @out Self, loc "<stdin>":4:18, scope 3
copy_addr [take] %6 to [initialization] %3 : $*Self, loc "<stdin>":4:14, scope 3 // id: %8
dealloc_stack %6 : $*Self, loc "<stdin>":4:18, scope 3 // id: %9
copy_addr %3 to [initialization] %0 : $*Self, loc "<stdin>":5:5, scope 3 // id: %10
strong_release %1 : $@callee_owned () -> @out Self, loc "<stdin>":5:5, scope 3 // id: %11
destroy_addr %3 : $*Self, loc "<stdin>":3:5, scope 3 // id: %12
%13 = tuple (), scope 3 // user: %15
dealloc_stack %3 : $*Self, loc "<stdin>":3:5, scope 3 // id: %14
return %13 : $(), loc "<stdin>":5:5, scope 3 // id: %15
} // end sil function '(extension in main):main.Factory.init (() -> A) -> A'
$ cat <<EOF|swiftc -emit-sil -|swift demangle
protocol Factory {}
extension Factory {
init(_ f: () -> Self) {
self = f()
}
}
extension Int: Factory {}
let foo = Int {
return 1
}
EOF
sil_stage canonical
import Builtin
import Swift
import SwiftShims
// foo
sil_global hidden [let] @main.foo : Swift.Int : $Int
sil_scope 1 { parent @main : $@convention(c) (Int32, UnsafeMutablePointer<Optional<UnsafeMutablePointer<Int8>>>) -> Int32 }
// main
sil @main : $@convention(c) (Int32, UnsafeMutablePointer<Optional<UnsafeMutablePointer<Int8>>>) -> Int32 {
bb0(%0 : $Int32, %1 : $UnsafeMutablePointer<Optional<UnsafeMutablePointer<Int8>>>):
alloc_global @main.foo : Swift.Int, loc "<stdin>":9:5, scope 1 // id: %2
%3 = global_addr @main.foo : Swift.Int : $*Int, loc "<stdin>":9:5, scope 1 // user: %11
// function_ref Factory.init(() -> A) -> A
%4 = function_ref @(extension in main):main.Factory.init (() -> A) -> A : $@convention(method) <τ_0_0 where τ_0_0 : Factory> (@owned @callee_owned () -> @out τ_0_0, @thick τ_0_0.Type) -> @out τ_0_0, loc "<stdin>":9:11, scope 1 // user: %11
%5 = metatype $@thin Int.Type, loc "<stdin>":9:11, scope 1
%6 = metatype $@thick Int.Type, loc "<stdin>":9:11, scope 1 // user: %11
// function_ref (closure #1)
%7 = function_ref @main.(closure #1) : $@convention(thin) () -> Int, loc "<stdin>":9:15, scope 1 // user: %8
%8 = thin_to_thick_function %7 : $@convention(thin) () -> Int to $@callee_owned () -> Int, loc "<stdin>":9:15, scope 1 // user: %10
// function_ref thunk
%9 = function_ref @reabstraction thunk helper from @callee_owned () -> (@unowned Swift.Int) to @callee_owned () -> (@out Swift.Int) : $@convention(thin) (@owned @callee_owned () -> Int) -> @out Int, loc "<stdin>":9:15, scope 1 // user: %10
%10 = partial_apply %9(%8) : $@convention(thin) (@owned @callee_owned () -> Int) -> @out Int, loc "<stdin>":9:15, scope 1 // user: %11
%11 = apply %4<Int>(%3, %10, %6) : $@convention(method) <τ_0_0 where τ_0_0 : Factory> (@owned @callee_owned () -> @out τ_0_0, @thick τ_0_0.Type) -> @out τ_0_0, loc "<stdin>":11:1, scope 1
%12 = integer_literal $Builtin.Int32, 0, scope 1 // user: %13
%13 = struct $Int32 (%12 : $Builtin.Int32), scope 1 // user: %14
return %13 : $Int32, scope 1 // id: %14
} // end sil function 'main'
sil_scope 2 { loc "<stdin>":3:5 parent @(extension in main):main.Factory.init (() -> A) -> A : $@convention(method) <τ_0_0 where τ_0_0 : Factory> (@owned @callee_owned () -> @out τ_0_0, @thick τ_0_0.Type) -> @out τ_0_0 }
sil_scope 3 { loc "<stdin>":5:5 parent 2 }
// Factory.init(() -> A) -> A
sil hidden @(extension in main):main.Factory.init (() -> A) -> A : $@convention(method) <Self where Self : Factory> (@owned @callee_owned () -> @out Self, @thick Self.Type) -> @out Self {
// %0 // user: %10
// %1 // users: %11, %7, %5, %4
bb0(%0 : $*Self, %1 : $@callee_owned () -> @out Self, %2 : $@thick Self.Type):
%3 = alloc_stack $Self, var, name "self", loc "<stdin>":3:5, scope 2 // users: %8, %10, %12, %14
debug_value %1 : $@callee_owned () -> @out Self, let, name "f", argno 1, loc "<stdin>":3:12, scope 2 // id: %4
strong_retain %1 : $@callee_owned () -> @out Self, loc "<stdin>":4:16, scope 3 // id: %5
%6 = alloc_stack $Self, loc "<stdin>":4:18, scope 3 // users: %9, %8, %7
%7 = apply %1(%6) : $@callee_owned () -> @out Self, loc "<stdin>":4:18, scope 3
copy_addr [take] %6 to [initialization] %3 : $*Self, loc "<stdin>":4:14, scope 3 // id: %8
dealloc_stack %6 : $*Self, loc "<stdin>":4:18, scope 3 // id: %9
copy_addr %3 to [initialization] %0 : $*Self, loc "<stdin>":5:5, scope 3 // id: %10
strong_release %1 : $@callee_owned () -> @out Self, loc "<stdin>":5:5, scope 3 // id: %11
destroy_addr %3 : $*Self, loc "<stdin>":3:5, scope 3 // id: %12
%13 = tuple (), scope 3 // user: %15
dealloc_stack %3 : $*Self, loc "<stdin>":3:5, scope 3 // id: %14
return %13 : $(), loc "<stdin>":5:5, scope 3 // id: %15
} // end sil function '(extension in main):main.Factory.init (() -> A) -> A'
sil_scope 4 { loc "<stdin>":9:15 parent @main.(closure #1) : $@convention(thin) () -> Int }
sil_scope 5 { loc "<stdin>":11:1 parent 4 }
// (closure #1)
sil shared @main.(closure #1) : $@convention(thin) () -> Int {
bb0:
%0 = integer_literal $Builtin.Int64, 1, loc "<stdin>":10:12, scope 5 // user: %1
%1 = struct $Int (%0 : $Builtin.Int64), loc "<stdin>":10:12, scope 5 // user: %2
return %1 : $Int, loc "<stdin>":10:5, scope 5 // id: %2
} // end sil function 'main.(closure #1)'
sil_scope 6 { parent @Swift.Int.init (_builtinIntegerLiteral : Builtin.Int2048) -> Swift.Int : $@convention(method) (Builtin.Int2048, @thin Int.Type) -> Int }
// Int.init(_builtinIntegerLiteral : Builtin.Int2048) -> Int
sil public_external [transparent] [fragile] @Swift.Int.init (_builtinIntegerLiteral : Builtin.Int2048) -> Swift.Int : $@convention(method) (Builtin.Int2048, @thin Int.Type) -> Int {
// %0 // user: %2
bb0(%0 : $Builtin.Int2048, %1 : $@thin Int.Type):
%2 = builtin "s_to_s_checked_trunc_Int2048_Int64"(%0 : $Builtin.Int2048) : $(Builtin.Int64, Builtin.Int1), scope 6 // user: %3
%3 = tuple_extract %2 : $(Builtin.Int64, Builtin.Int1), 0, scope 6 // user: %4
%4 = struct $Int (%3 : $Builtin.Int64), scope 6 // user: %5
return %4 : $Int, scope 6 // id: %5
} // end sil function 'Swift.Int.init (_builtinIntegerLiteral : Builtin.Int2048) -> Swift.Int'
sil_scope 7 { parent @reabstraction thunk helper from @callee_owned () -> (@unowned Swift.Int) to @callee_owned () -> (@out Swift.Int) : $@convention(thin) (@owned @callee_owned () -> Int) -> @out Int }
// thunk
sil shared [transparent] [reabstraction_thunk] @reabstraction thunk helper from @callee_owned () -> (@unowned Swift.Int) to @callee_owned () -> (@out Swift.Int) : $@convention(thin) (@owned @callee_owned () -> Int) -> @out Int {
// %0 // user: %3
// %1 // user: %2
bb0(%0 : $*Int, %1 : $@callee_owned () -> Int):
%2 = apply %1() : $@callee_owned () -> Int, scope 7 // user: %3
store %2 to %0 : $*Int, scope 7 // id: %3
%4 = tuple (), scope 7 // user: %5
return %4 : $(), scope 7 // id: %5
} // end sil function 'reabstraction thunk helper from @callee_owned () -> (@unowned Swift.Int) to @callee_owned () -> (@out Swift.Int)'
sil_witness_table hidden Int: Factory module main {
}
sil_default_witness_table hidden Factory {
}
$ cat <<EOF|swiftc -emit-sil -O -|swift demangle
protocol Factory {}
extension Factory {
init(_ f: () -> Self) {
self = f()
}
}
extension Int: Factory {}
let foo = Int {
return 1
}
EOF
sil_stage canonical
import Builtin
import Swift
import SwiftShims
// foo
sil_global hidden [let] @main.foo : Swift.Int : $Int
sil_scope 1 { parent @main : $@convention(c) (Int32, UnsafeMutablePointer<Optional<UnsafeMutablePointer<Int8>>>) -> Int32 }
sil_scope 2 { loc "<stdin>":9:15 parent @main.(closure #1) : $@convention(thin) () -> Int }
sil_scope 3 { loc "<stdin>":11:1 parent 2 }
sil_scope 4 { parent @reabstraction thunk helper from @callee_owned () -> (@unowned Swift.Int) to @callee_owned () -> (@out Swift.Int) : $@convention(thin) (@owned @callee_owned () -> Int) -> @out Int }
sil_scope 5 { loc "<stdin>":3:5 parent @generic specialization <Swift.Int with Swift.Int : main.Factory in main> of (extension in main):main.Factory.init (() -> A) -> A : $@convention(method) (@owned @callee_owned () -> @out Int, @thick Int.Type) -> Int }
sil_scope 6 { loc "<stdin>":5:5 parent 5 }
sil_scope 7 { loc "<stdin>":11:1 parent 1 }
sil_scope 8 { loc "<stdin>":5:5 parent 6 inlined_at 7 }
sil_scope 9 { loc "<stdin>":4:18 parent 8 }
sil_scope 10 { parent 4 inlined_at 9 }
sil_scope 11 { parent 10 }
sil_scope 12 { loc "<stdin>":11:1 parent 3 inlined_at 11 }
// main
sil @main : $@convention(c) (Int32, UnsafeMutablePointer<Optional<UnsafeMutablePointer<Int8>>>) -> Int32 {
bb0(%0 : $Int32, %1 : $UnsafeMutablePointer<Optional<UnsafeMutablePointer<Int8>>>):
alloc_global @main.foo : Swift.Int, loc "<stdin>":9:5, scope 1 // id: %2
%3 = global_addr @main.foo : Swift.Int : $*Int, loc "<stdin>":9:5, scope 1 // user: %6
%4 = integer_literal $Builtin.Int64, 1, loc "<stdin>":10:12, scope 12 // user: %5
%5 = struct $Int (%4 : $Builtin.Int64), loc "<stdin>":10:12, scope 12 // user: %6
store %5 to %3 : $*Int, loc "<stdin>":11:1, scope 1 // id: %6
%7 = integer_literal $Builtin.Int32, 0, scope 1 // user: %8
%8 = struct $Int32 (%7 : $Builtin.Int32), scope 1 // user: %9
return %8 : $Int32, scope 1 // id: %9
} // end sil function 'main'
sil_scope 13 { loc "<stdin>":3:5 parent @(extension in main):main.Factory.init (() -> A) -> A : $@convention(method) <τ_0_0 where τ_0_0 : Factory> (@owned @callee_owned () -> @out τ_0_0, @thick τ_0_0.Type) -> @out τ_0_0 }
sil_scope 14 { loc "<stdin>":5:5 parent 13 }
// Factory.init(() -> A) -> A
sil hidden @(extension in main):main.Factory.init (() -> A) -> A : $@convention(method) <Self where Self : Factory> (@owned @callee_owned () -> @out Self, @thick Self.Type) -> @out Self {
// %0 // user: %4
// %1 // users: %4, %3
bb0(%0 : $*Self, %1 : $@callee_owned () -> @out Self, %2 : $@thick Self.Type):
debug_value %1 : $@callee_owned () -> @out Self, let, name "f", argno 1, loc "<stdin>":3:12, scope 13 // id: %3
%4 = apply %1(%0) : $@callee_owned () -> @out Self, loc "<stdin>":4:18, scope 14
%5 = tuple (), scope 14 // user: %6
return %5 : $(), loc "<stdin>":5:5, scope 14 // id: %6
} // end sil function '(extension in main):main.Factory.init (() -> A) -> A'
sil_witness_table hidden Int: Factory module main {
}
sil_default_witness_table hidden Factory {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment