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

iOS エンジニアのための LLVM Optimiazation

iOS エンジニアのための LLVM Optimiazation

横浜へなちょこiOS勉強会#34.1 での発表資料です

0a86666ca90016b2a28289dda921b0d7?s=128

Toshihiro Morimoto

November 29, 2014
Tweet

More Decks by Toshihiro Morimoto

Other Decks in Technology

Transcript

  1. iOS ΤϯδχΞͷͨΊͷ LLVM Optimiazation 2014/11/29 ԣ඿΁ͳͪΐ͜iOSษڧձ#34.1 @dealforest Toshihro Morimoto

  2. ࣗݾ঺հ http://about.me/dealforest ! ! iOS App / Web Application(perl, ruby)

    @dealforest Toshihro Morimoto
  3. Έͳ͞Μ ͜Μͳܦݧ͋Γ·ͤΜ͔ʁ

  4. Debug ϏϧυͰ͔֬͠ೝͤͣ ৹ࠪʹͩͯ͠͠·ͬͨ͜ͱ

  5. ͍͟ϦϦʔε͞ΕΔͱ...

  6. ʊਓਓਓਓਓਓਓਓʊ ʼɹಥવͷ Crashɹʻ ʉY^Y^Y^Y^Y^Y^Yʉ

  7. ͳͥͦΜͳ͜ͱ͕͓͖Δͷ͔ʁ

  8. LLVM ͷ࠷దԽʹΑΓ ͜ͷΑ͏ͳ͜ͱ͕ ى͖ͯ͠·͏έʔε͕͋Γ·͢

  9. LLVM ͷ࠷దԽ͸ԿΛ͍ͯ͠Δͷ͔ʁ

  10. όΠφϦαΠζΛখͨ͘͞͠Γ ॲཧΛߴ଎Խͨ͠Γ͠·͢ (಺༰͸ઃఆʹΑͬͯมΘΓ·͢)

  11. ͪΌΜͱ Release ϏϧυͰ ςετ͠Ζͬͯݴ͑͹ ͦ͏ͳΜͰ͚͢ͲͶɻɻɻɻ

  12. Ͳ͏ͯ͠࠷దԽͰ Crash ͢ΔΑ͏ʹ ͳͬͯ͠·ͬͨͷ͔

  13. ࠷దԽͰԿ͕ߦΘΕ͍ͯΔͷ͔

  14. ࠓճ͸ͦΕΛௐ΂ͯΈ·ͨ͠

  15. ஫ҙࣄ߲ ༻ޠ / ༻๏ڞʹؒҧ͍͕͋ΔՄೳੑ͕ ͋Γ·͢ͷͰ͔͋͠Βͣ

  16. ͦ΋ͦ΋Ͳ͜Ͱઃఆ͢Δͷʁ

  17. Build Settings

  18. ࠷దԽͷछྨ • O0 : ࠷΋଎͘ίϯύΠϧग़དྷɺ࠷΋σόοΨϒϧͳίʔυΛ ੜ੒͢Δɻ࠷దԽͳ͠ • O1 : جຊతͳ࠷దԽΛ࣮ߦ͢Δ

    • O2 : ΄ͱΜͲ͢΂ͯͷ࠷దԽΛ࣮ߦ͢Δ • O3 : O2 ͱࣅ͍ͯΔ͕ίϯύΠϧʹΑΓ͕͔͔࣌ؒΓ αΠζ͕େ͖͘ͳΔɻ͔͠͠ੑೳͷྑ͍ίʔυΛੜ੒͢Δ • Os : O2ͷ࠷దԽʹՃ͑ͯίʔυαΠζΛ཈͑Δ • Ofast : ن໿ΛҰ෦ແࢹͨ͠࠷దԽΛߦ͏Α͏ͳͷͰ࢖Θͳ͍΄͏ ͕͍͍Ͱ͢ɻ by Compiler-Level Optimizations
  19. ͍·͍ͪϐϯͱ͖·ͤΜͶ

  20. Ξό΢τ͗͢Δ…

  21. ͜ΕΛ஌ΔͨΊʹ͸ LLVM ͷਂ෵Λ ೷͔ͳ͍ͱ͍͚·ͤΜͰͨ͠

  22. ͱ͍ͬͯ΋ LLVM ͷ֓೦ࣗମ͕ γϯϓϧͰͱͯ΋෼͔Γ΍͍͢ʂ

  23. ͦ΋ͦ΋ LLVM ͬͯԿʁ

  24. LLVM

  25. LLVM LLVM ͱ͸ɺίϯύΠϧ࣌ɺϦϯΫ࣌ɺ࣮ߦ࣌ͳͲ ͋ΒΏΔ࣌఺ͰϓϩάϥϜΛ࠷దԽ͢ΔΑ͏ઃܭ ͞Εͨɺ೚ҙͷϓϩάϥϛϯάݴޠʹରԠՄೳͳ ίϯύΠϥج൫Ͱ͋Δɻ౰ॳ͸ɺLLVM ͷ໊শͷ༝ དྷ͸ɺLow Level Virtual

    Machine (௿ਫ४Ծ૝ػց) ͷུͰ͋Δͱ͍͕ͯͨ͠ɺݱࡏ͸ɺԿͷ಄จࣈͰ ΋ͳ͍ͱ͍ͯ͠Δɻ by Wikipekia
  26. LLVM - Phase- by LLVM and NVVM

  27. LLVM - Phase- • Front-end • Middle-end • Back-end

  28. LLVM - Phase Front-end - • ࣈ۟ղੳ • ߏจղੳ •

    தؒίʔυ(LLVM IR)࡞੒
  29. LLVM - Phase Middle-end - தؒίʔυͷ࠷దԽΛ͠ɺ ࠷దԽ͞ΕͨதؒίʔυΛग़ྗ͢Δ

  30. LLVM - Phase Back-end - ࠷దԽ͞Εͨதؒίʔυ͔Βλʔήοτʹ ߹ΘͤͨίʔυΛग़ྗ͢Δ ಛఆͷ CPU ͷωΠςΟϒίʔυͱ͔

    Javascript ͱ͔
  31. ;Ή;Ή

  32. ࠓճ͸࠷దԽͷ͜ͱΛ஌Γ͍ͨͷͰ Middle-end Λௐ΂Ε͹Αͦ͞͏Ͱ͢Ͷ

  33. Middle-end(LLVM optimiser) Λ ௐ΂͍ͯ͘ͱ Pass ͱ͍͏࢓૊Έʹ ͍͖͖ͭ·͢

  34. Pass Middle-end Ͱ࠷దԽ͢ΔॲཧΛ͍ͯ͠Δ LLVM IR ͷίʔυΛର৅ʹɺͦͷ಺༰Λղ ੳ͠ɺ࠷దԽ΍ɺ໋ྩͷஔ׵ͳͲΛߦ͏ͨ ΊͷϑϨʔϜϫʔΫ

  35. ͭ·Γ͸ͲΜͳ Pass ͕࣮ߦ͞Ε͍ͯΔ͔Λ ௐ΂Ε͹ͲΜͳ࠷దԽ͕ߦΘΕ͍ͯΔ͔͕ ෼͔Δͱ͍͏Θ͚Ͱ͢Ͷ

  36. ؀ڥߏங $ brew install llvm ! `/usr/local/Cellar/llvm/3.5.0/bin/opt` ͱ࣮ߦ͢ Δͷ͕ΊΜͲ͍͘͞৔߹͸ `brew

    link llvm` Λ࣮ߦ͢Δ
  37. ௐࠪϑϩʔ 1. ࠷దԽͳ͠ͷ .o ϑΝΠϧΛ࡞੒͢Δઃ ఆΛௐ΂Δ 2. ͦͷઃఆΛ༻͍ͯதؒίʔυ(LLVM IR)Λ ࡞੒

    3. ֤࠷దԽΛ࣮ߦͯ͠ϑΝΠϧʹग़ྗ 4. diff ΛͱΓ Pass ͷࠩ෼Λௐ΂Δ
  38. ࠷దԽͳ͠ͷ .o ϑΝΠϧΛ ࡞੒͢ΔઃఆΛௐ΂Δ xcodebuild build -configuration Debug

  39. தؒίʔυ(LLVM IR)Λ࡞੒ • ઌ΄Ͳௐ΂ͨઃఆΛར༻ • `-S -emit-llvm` ΦϓγϣϯΛ௥Ճͯ͠த ؒίʔυΛग़ྗ •

    `-o` ͷ஋Λมߋͯ͠தؒίʔυͷग़ྗઌ Λมߋ(.o -> .ll)
  40. ֤࠷దԽΛ࣮ߦͯ͠ϑΝΠϧʹग़ྗ $ /usr/local/Cellar/llvm/3.5.0/bin/opt \ -debug-pass=Structure \ -O1 [தؒίʔυ].ll > /dev/null

    ! ඞཁͳ৘ใ͸ඪ४Τϥʔग़ྗʹදࣔ͞ΕΔ
  41. ֤࠷దԽΛ࣮ߦͯ͠ϑΝΠϧʹग़ྗ

  42. ݟʹ͍͘ͷͰ gist ʹ·ͱΊ·ͨ͠ https://gist.github.com/dealforest/ 718df40a09d8e27758c7

  43. ͦ΋ͦ΋ Debug ࣌΋ Os ΍Ε͹͍͍Μ͡Ό...

  44. ! ͱࢥ͚ͬͨͲɺίϯύΠϧ͕͔͔࣌ؒΔͷͱɺC, C++ ͷίʔυͰ BreakPoint Λ࢓ֻ͚ͨΓ͢Δͱશ͘ ݟ౰ҧ͍ͳߦͰఀࢭͨ͠Γ͠·͢ ͳͷͰ։ൃαΠΫϧʹӨڹ͕Ͱͯ͠·͏ͷͰ O0 ͕

    ࠾༻͞Ε͍ͯΔͱΘ͚Ͱ͢Ͷɻೲಘ @niwatako ͞Μᐌ͘ɺObjective-C Ͱ΋ͣΕΔΒ͍͠ Ͱ͢
  45. ·ͱΊ ࠷దԽ͸ࢥͬͯΔҎ্ʹ৭ʑ͍ͬͯ͡ΔͷͰͪΌ Μͱ֬ೝ͠·͠ΐ͏ Ή͠Ζ֬ೝ͔ͯ͠Β͡Όͳ͍ͱɺ৹ࠪʹͩͤͳ͍ εΩʔϜΛͭ͘Δ͜ͱ͕େࣄ

  46. http://www.amazon.co.jp/dp/4883379310/

  47. ͓·͚ Xcode6 ͔Β௥Ճ͞Εͨ Profile Guided Optimization(PGO) ͱ͍͏࠷దԽ͕͋Δ ؆୯ʹ͍͏ͱɺ࢖༻ස౓ͷߴ͍ॱʹίʔυϨΠΞ ΢τΛ࠷దԽ͢ΔΒ͍͠ ͸΍͘ͳΔΑʂ

  48. ͓·͚ Xcode6 ͔Β௥Ճ͞Εͨ࠷దԽ Profile Guided Optimization(PGO) ͕͋Δ ؆୯ʹ͍͏ͱΑ͘࢖͏ॱ൪ʹίʔυϨΠΞ΢τΛ ࠷దԽ͢ΔΒ͍͠ ͸΍͘ͳΔΑʂ

  49. ͓·͚ ৄ͘͠͸ʮ What's New in LLVMʯΛΈ͍ͯͩ͘͞

  50. ͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠