Last active
January 19, 2017 17:01
-
-
Save hamishknight/600a05ddf86bacccc132e87a70593340 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
For the folllowing code: | |
var v = 5 | |
@inline(never) func foo() { | |
v += 10 | |
} | |
class C<T> { | |
let t: T | |
@inline(never) init(t: T) { | |
self.t = t | |
} | |
} | |
let c = C(t: foo) | |
c.t() | |
The canonical SIL emitted for the main function in a -O build (xcrun swiftc -O -emit-sil main.swift | xcrun swift-demangle > main.silgen): | |
// main | |
sil @main : $@convention(c) (Int32, UnsafeMutablePointer<Optional<UnsafeMutablePointer<Int8>>>) -> Int32 { | |
// %0 // user: %3 | |
// %1 // user: %9 | |
bb0(%0 : $Int32, %1 : $UnsafeMutablePointer<Optional<UnsafeMutablePointer<Int8>>>): | |
%2 = global_addr @static Swift.CommandLine._argc : Swift.Int32 : $*Int32, scope 1 // user: %3 | |
store %0 to %2 : $*Int32, scope 1 // id: %3 | |
%4 = global_addr @globalinit_33_FD9A49A256BEB6AF7C48013347ADC3BA_token4 : $*Builtin.Word, scope 1 // user: %5 | |
%5 = address_to_pointer %4 : $*Builtin.Word to $Builtin.RawPointer, scope 1 // user: %7 | |
// function_ref globalinit_33_FD9A49A256BEB6AF7C48013347ADC3BA_func4 | |
%6 = function_ref @globalinit_33_FD9A49A256BEB6AF7C48013347ADC3BA_func4 : $@convention(thin) () -> (), scope 1 // user: %7 | |
%7 = builtin "once"(%5 : $Builtin.RawPointer, %6 : $@convention(thin) () -> ()) : $(), scope 1 | |
%8 = global_addr @static Swift.CommandLine._unsafeArgv : Swift.UnsafeMutablePointer<Swift.UnsafeMutablePointer<Swift.Int8>?> : $*UnsafeMutablePointer<Optional<UnsafeMutablePointer<Int8>>>, scope 1 // user: %9 | |
store %1 to %8 : $*UnsafeMutablePointer<Optional<UnsafeMutablePointer<Int8>>>, scope 1 // id: %9 | |
// Allocate new global variable "v", and initialise it with the Int 5. | |
alloc_global @main.v : Swift.Int, loc "main.swift":33:5, scope 1 // id: %10 | |
%11 = global_addr @main.v : Swift.Int : $*Int, loc "main.swift":33:5, scope 1 // user: %14 | |
%12 = integer_literal $Builtin.Int64, 5, loc "main.swift":33:9, scope 1 // user: %13 | |
%13 = struct $Int (%12 : $Builtin.Int64), loc "main.swift":33:9, scope 1 // user: %14 | |
store %13 to %11 : $*Int, loc "main.swift":33:9, scope 1 // id: %14 | |
// Allocate new global variable "c". | |
alloc_global @main.c : main.C<() -> ()>, loc "main.swift":48:5, scope 1 // id: %15 | |
%16 = global_addr @main.c : main.C<() -> ()> : $*C<() -> ()>, loc "main.swift":48:5, scope 1 // user: %21 | |
// function_ref specialized thunk | |
// Get a reference to the specialised thunk helper for the function foo(). | |
%17 = function_ref @function signature specialization <Arg[0] = [Constant Propagated Function : main.foo () -> ()]> of reabstraction thunk helper from @callee_owned () -> () to @callee_owned (@in ()) -> (@out ()) : $@convention(thin) (@in ()) -> @out (), loc "main.swift":48:14, scope 1 // user: %18 | |
// Convert the thunk helper from a thin to thick function. | |
%18 = thin_to_thick_function %17 : $@convention(thin) (@in ()) -> @out () to $@callee_owned (@in ()) -> @out (), loc "main.swift":48:14, scope 1 // user: %20 | |
// function_ref specialized specialized C.__allocating_init(t : A) -> C<A> | |
// Get C<() -> ()>'s specialised init. | |
%19 = function_ref @function signature specialization <Arg[1] = Dead> of generic specialization <() -> ()> of main.C.__allocating_init (t : A) -> main.C<A> : $@convention(thin) (@owned @callee_owned (@in ()) -> @out ()) -> @owned C<() -> ()>, loc "main.swift":43:20, scope 4 // user: %20 | |
// Apply it with the specialised thunk helper, and store the result in the global variable "c". | |
%20 = apply %19(%18) : $@convention(thin) (@owned @callee_owned (@in ()) -> @out ()) -> @owned C<() -> ()>, loc "main.swift":43:20, scope 4 // users: %21, %22 | |
store %20 to %16 : $*C<() -> ()>, loc "main.swift":48:17, scope 1 // id: %21 | |
// Call c.t()... | |
%22 = ref_element_addr %20 : $C<() -> ()>, #C.t, loc "main.swift":49:3, scope 1 // user: %23 | |
%23 = load %22 : $*@callee_owned (@in ()) -> @out (), loc "main.swift":49:3, scope 1 // users: %26, %27 | |
%24 = alloc_stack $(), loc "main.swift":49:5, scope 1 // users: %29, %27 | |
%25 = alloc_stack $(), loc "main.swift":49:5, scope 1 // users: %28, %27 | |
strong_retain %23 : $@callee_owned (@in ()) -> @out (), scope 1 // id: %26 | |
%27 = apply %23(%25, %24) : $@callee_owned (@in ()) -> @out (), loc "main.swift":49:5, scope 1 | |
dealloc_stack %25 : $*(), loc "main.swift":49:5, scope 1 // id: %28 | |
dealloc_stack %24 : $*(), loc "main.swift":49:5, scope 1 // id: %29 | |
%30 = integer_literal $Builtin.Int32, 0, scope 1 // user: %31 | |
%31 = struct $Int32 (%30 : $Builtin.Int32), scope 1 // user: %32 | |
return %31 : $Int32, scope 1 // id: %32 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment