Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

SIL for the first time

1,167 views

Published on

DEPRECATED!!! Please see below instead.
https://www.slideshare.net/kitasuke/sil-for-first-time-leaners

potatotips #47/iPhone Dev Sapporo

Published in: Technology
  • Be the first to comment

SIL for the first time

  1. 1. SIL for the first time Yusuke Kita @kitasuke
  2. 2. SIL
  3. 3. Swift Intermediate Language
  4. 4. Why SIL?
  5. 5. Better understanding of how optimizations work
  6. 6. What is SIL?
  7. 7. SIL is an SSA-form IR with high-level semantic information designed to implement the Swift programming language
  8. 8. Swift Programming Language
  9. 9. Swift Compiler
  10. 10. Where all the magic happens !
  11. 11. How it works?
  12. 12. Swift Compiler
  13. 13. Swift Compiler • Front end
  14. 14. Swift Compiler • Front end • Middle end
  15. 15. Swift Compiler • Front end • Middle end • Back end
  16. 16. Swift Compiler • Front end • Middle end • Back end
  17. 17. Parsing ➡ AST
  18. 18. Semantic analysis ➡ type checked AST
  19. 19. SIL generation ➡ raw SIL
  20. 20. SIL guaranteed transformations ➡ canonical SIL
  21. 21. Swift Compiler • Front end • Middle end • Back end
  22. 22. LLVM IR Generation ➡ LLVM IR
  23. 23. Swift Compiler • Front end • Middle end • Back end
  24. 24. LLVM ➡ .o
  25. 25. Recap
  26. 26. Swift Compiler
  27. 27. source ➡ executable
  28. 28. How SIL works?
  29. 29. SIL Stage
  30. 30. SIL Stage • Raw SIL
  31. 31. SIL Stage • Raw SIL • Canonical SIL
  32. 32. Raw SIL • May not have a fully-constructed SSA graph • May contain dataflow errors • Should not be used for native code generation or distribution
  33. 33. Canonical SIL • Dataflow errors must be eliminated • Certain instructions must be canonicalized to simpler forms. • Performance optimization and native code generation are derived from this form • A module can be distributed containing SIL in this (or later) forms.
  34. 34. Optimizations
  35. 35. Optimizations • Guaranteed Optimization Passes and Diagnostic Passes
  36. 36. Optimizations • Guaranteed Optimization Passes and Diagnostic Passes • General Optimization Passes (not running at -Onone)
  37. 37. Guaranteed Optimization Passes and Diagnostic Passes • Mandatory inlining • Memory promotion • Constant propagation • Return analysis • Critical edge splitting
  38. 38. General Optimization Passes • Generic Specialization • Witness and VTable Devirtualization • Performance Inlining • Reference Counting Optimizations • Memory Promotion/Optimizations • High-level domain specific optimizations
  39. 39. Lots of optimizations happening here !
  40. 40. SIL Gen
  41. 41. SIL Gen • Raw SIL $swiftc -emit-silgen sample.swift
  42. 42. SIL Gen • Raw SIL $swiftc -emit-silgen sample.swift • Canonical SIL $swiftc -emit-sil sample.swift
  43. 43. How .sil file looks like?
  44. 44. sample.swift func number() -> Int { let x: Int x = 1 return x }
  45. 45. sample.sil sil_stage canonical import Builtin import Swift import SwiftShims // main ... // number() sil hidden @_T06sample6numberSiyF : $@convention(thin) () -> Int { bb0: %0 = alloc_stack $Int, let, name "x" // users: %3, %4 %1 = integer_literal $Builtin.Int64, 1 // user: %2 %2 = struct $Int (%1 : $Builtin.Int64) // users: %5, %3 store %2 to %0 : $*Int // id: %3 dealloc_stack %0 : $*Int // id: %4 return %2 : $Int // id: %5 } // end sil function '_T06sample6numberSiyF' // Int.init(_builtinIntegerLiteral:) ...
  46. 46. Let's take a look from the top on down
  47. 47. SIL Stage sil_stage canonical SIL files declare the processing stage of the included SIL with one of the declarations sil_stage raw or sil_stage canonical at top level.
  48. 48. Import import Builtin import Swift import SwiftShims SIL is reliant on Swift's type system and declarations. In a .sil file, there are no implicit imports. The swift and/or Builtin standard modules must be imported explicitly if used.
  49. 49. Legal SIL Types • A loadable legal SIL type, $T • The address of a legal SIL type, $*T
  50. 50. Linkage sil hidden @_T06sample6numberSiyF : $@convention(thin) () -> Int hidden definitions are unique and visible only within the current Swift module. In LLVM IR, they will be emitted with external linkage and hidden visibility.
  51. 51. Name Mangling sil hidden @_T06sample6numberSiyF : $@convention(thin) () -> Int The name @_T06sample6numberSiyF is the mangled name of the number Swift function.
  52. 52. Calling Convention How Swift functions are emitted in SIL • @convention(swift) • @convention(method) • @convention(witness_method) • @convention(c) • @convention(objc_method)
  53. 53. Type Lowering sil hidden @_T06sample6numberSiyF : $@convention(thin) () -> Int A formal type is the type of a value in Swift, such as an expression result.Type lowering is the process of turning a formal type into its lowered type. The lowered type of a declaration will usually be thin.
  54. 54. Basic Blocks bb0: In SIL, basic blocks take arguments, which are used as an alternative to LLVM's phi nodes.
  55. 55. alloc_stack %0 = alloc_stack $Int, let, name "x" Allocates uninitialized memory that is sufficiently aligned on the stack to contain a value of type Int.
  56. 56. integer_literal %1 = integer_literal $Builtin.Int64, 1 Creates an integer literal value. The result will be of type Builtin.Int64, which must be a builtin integer type. The literal value is specified using Swift’s integer literal syntax.
  57. 57. struct %2 = struct $Int (%1 : $Builtin.Int64) Creates a value of a loadable struct type by aggregating multiple loadable values.
  58. 58. store store %2 to %0 : $*Int Stores the value %2 to memory at address %0. The type of %0 is *Int and the type of %2 is Int.
  59. 59. dealloc_stack dealloc_stack %0 : $*Int Deallocates memory previously allocated by alloc_stack. The allocated value in memory must be uninitialized or destroyed prior to being deallocated.
  60. 60. return return %2 : $Int Exits the current function and returns control to the calling function.
  61. 61. sample.sil sil_stage canonical import Builtin import Swift import SwiftShims // main ... // number() sil hidden @_T06sample6numberSiyF : $@convention(thin) () -> Int { bb0: %0 = alloc_stack $Int, let, name "x" // users: %3, %4 %1 = integer_literal $Builtin.Int64, 1 // user: %2 %2 = struct $Int (%1 : $Builtin.Int64) // users: %5, %3 store %2 to %0 : $*Int // id: %3 dealloc_stack %0 : $*Int // id: %4 return %2 : $Int // id: %5 } // end sil function '_T06sample6numberSiyF' // Int.init(_builtinIntegerLiteral:) ...
  62. 62. SIL with optimizations swiftc -emit-sil sample.swift -O Compile with optimizations
  63. 63. sample.swift func number() -> Int { let x: Int x = 1 return x }
  64. 64. sil_stage canonical import Builtin import Swift import SwiftShims // main ... // number() sil hidden @_T06sample6numberSiyF : $@convention(thin) () -> Int { bb0: %0 = integer_literal $Builtin.Int64, 1 // user: %1 %1 = struct $Int (%0 : $Builtin.Int64) // user: %2 return %1 : $Int // id: %2 } // end sil function '_T06sample6numberSiyF'
  65. 65. Summary • Not so difficult than expected • Good to know how Swift Compiler works as Swift developer • Especially Swift optimizations
  66. 66. References • swift/docs/SIL.rst • Swiftコンパイラのアーキテクチャ • Swiftコンパイラの構造と基盤テクニック
  67. 67. Thank you

×