Successfully reported this slideshow.
Your SlideShare is downloading. ×

Tutorial for developing SILOptimizer Pass

Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Upcoming SlideShare
Future of PHP
Future of PHP
Loading in …3
×

Check these out next

1 of 61 Ad

More Related Content

Slideshows for you (20)

Similar to Tutorial for developing SILOptimizer Pass (20)

Advertisement

Recently uploaded (20)

Advertisement

Tutorial for developing SILOptimizer Pass

  1. 1. Tutorial for developing SILOptimizer Pass freddi - Yuki.Aki at SwiftcKaigi #1 (16th.Nov)
  2. 2. About Me - I’m freddi - iOS Engineer @ Fukuoka - twitter: _ _ _freddi_ _ _
  3. 3. Before Start - This presentation is written in English - This session will be talked in Japanese
  4. 4. Before Start - This session is mainly for Beginner - Better to listen previous SIL session - Better to be familiar with C++ grammar
  5. 5. Today’s Aim - Understand What SILOptimizer and Pass are. - Understand How Pass optmize SIL Code - Understand How Can we Play developing Pass
  6. 6. Flow of Compiling Swift Code, with SILOptimizer
  7. 7. Flow of Compiling Swift Code
  8. 8. Flow of Compiling Swift Code SILGen generates SIL from type-checked AST
  9. 9. SIL - "Swift Intermediate Language” = SIL - between type-checked AST and LLVM IR - SIL Code is used for optimization of program
  10. 10. Between SILGen and IRGen canonical SIL raw SIL SILOptimizer
  11. 11. raw SIL - raw SIL code is generated from SILGen - raw SIL has not been optimized and diagnosed - optimized and diagnosed on SILOptimizer phase raw SIL
  12. 12. canonical SIL - Optimized and diagnosed SIL code is canonical SIL - on SILOptimizer phase canonical SIL
  13. 13. canonical SIL raw SIL Flow of raw SIL to canonical SIL SILOptimizer
  14. 14. SILOptimizer - SILOptimizer is one of phase in swiftc - raw SIL is optimized and diagnosed on this phase - It is consist of many module which called Pass SILOptimizer
  15. 15. canonical SIL raw SIL SILOptimizer Flow from raw SIL to canonical SIL
  16. 16. SILOptimizer canonical SIL raw SIL Pass Pass Pass Pass Flow from raw SIL to canonical SIL = “PipeLine of Pass”
  17. 17. Pass in SILOptimizer - Pass diagnoses and optimizes received ras SIL - Some of Passes are called definitely Pass
  18. 18. Pass Pipeline - Pass Pipeline has info which Passes must be called - Pipeline is created before starting optimizing - Pass in Pipeline is decided by … - swiftc Option (e.g.-O,-Onone) etc …
  19. 19. Pass in SILOptimizer - Example of Optimizer Pass - Memory Promotion - Assuming Single Thread (Optional Optimizer) Pass
  20. 20. Pass in SILOptimizer - Example of Diagnostic Pass - Overflow checking of Numeric Type Pass
  21. 21. Pass in SILOptimizer - Example of Diagnostic Pass - Overflow checking of Numeric Type Let's check it by Demo! by using -Xllvm -sil-print-pass-name option Pass
  22. 22. Pass in SILOptimizer -Xllvm -sil-print-pass-name
  23. 23. SIL - SIL is generated from our Swift Code - via type-checked AST - raw SIL and canonical SIL - canonical SIL is optimized and diagnosed SIL
  24. 24. SILOptimizer and Pass - SILOptimizer is phase of optimizing and diagnosing - SILOptimizer is consist of many Pass - raw SIL is optimized and diagnosed by Pass
  25. 25. How can we add optimize Pass to compiler?
  26. 26. To add original Pass to swiftc 1. Create your C++ file (because Pass is C++ class) @ lib/SILOptimizer/Transforms/*.cpp
  27. 27. To add original Pass to swiftc 2. Add location of C++ source for Pass Class to lib/SILOptimizer/Transforms/CMakeLists.txt 3. Generate swiftc Xcode Project ※ You must do 2.before generating Xcode Project
  28. 28. 4. Register Pass info @ include/swift/SILOptimizer/PassManager/Passes.def 5. Rebuild on Xcode 6. Add code which adding Pass to Pipeline @ lib/SILOptimizer/PassManager/PassPipeline.cpp To add original Pass to swiftc
  29. 29. 7. Write Pass Class and Creating Pass object func @ on your C++ file which is added on 1. To add original Pass to swiftc
  30. 30. Demo
  31. 31. Get Swift Compiler Code $ mkdir swift-source $ cd swift-source $ git clone https://github.com/apple/swift
  32. 32. Create your C++ file (because Pass is C++ class)
  33. 33. Add location of C++ source for Pass Class
  34. 34. Generate swiftc Xcode Project $ cd swift-source $ utils/update-checkout --clone $ utils/build-script —debug —xcode --skip-build-benchmarks
  35. 35. Register Pass info and Rebuild on Xcode
  36. 36. Add code which adding Pass to Pipeline
  37. 37. Write Pass Class and Creating Pass object func
  38. 38. Demo
  39. 39. Understand structural unit of SIL Code treated by SILOptimizer Pass
  40. 40. SIL Component - SILModule - SILGlobalVariable-s - SILFunction-s - SILBasicBlock-s - SILInstruction-s - etc …
  41. 41. Demo again :)
  42. 42. SILModule
  43. 43. SILFunction-sSILModule
  44. 44. SILBasicBlock-sSILFunctions
  45. 45. SILBasicBlock-s SILInstruction-s
  46. 46. Basic Structure of SILInstruction What instruction is using it (or Instruction’s id) Instruction’s id (Some instruction has no id on left of =)
  47. 47. Kind of SILInstruction Integer Literal Instruction Function Ref Instruction Apply Literal Instruction
  48. 48. Understand structural unit of SIL Code treated by SILOptimizer Pass
  49. 49. Understand structural unit of SIL Code treated by SILOptimizer Pass == We can use these unit as C++ Class on writing Pass
  50. 50. Demo time XD
  51. 51. AssumeSingleThreaded
  52. 52. SILFunctionTransform
  53. 53. Check Option
  54. 54. getFunction = Collection of BasicBlock
  55. 55. Basic Block = Collection of Instruction
  56. 56. Checking the Instruction is kind of Ref Count
  57. 57. Set Instruction non atomic
  58. 58. Invalidate Analysis
  59. 59. Now,we can read easy Pass!
  60. 60. Let’s play Pass by Demo!

×