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.

Graal in GraalVM - A New JIT Compiler

1,496 views

Published on

2018 Jul 25th LINE Developer Meetup #41 in Fukuoka
Session Slide in English / セッションスライドです。

Graal in GraalVM - A New JIT Compiler

オラクル社からGraalVMというものが発表され、話題を呼んでいます。GraalVMはHotSpot VM上に新しいJITコンパイラGraalと言語実装用フレームワーク/ASTインタプリタであるTruffle、さらにネイティブイメージ作成機能とその実行に使われるSubstrateVMを併せ持ったものです。すでにTruffleを使用したJavaScriptやRuby、R、Pythonの実装も提供されており、これらの言語とJavaはコードから相互に呼び出しができます。このセッションではGraalVMを概観したあと、JITコンパイラGraalにとくに注力して解説します。GraalとTruffleはOracle Labsとヨハネス・ケプラー大学で共同研究されており、多くの論文が発表されています。HotSpotのJITコンパイラとパフォーマンスや構造などを比較しつつ、GraalのJITコンパイルのテクニックについてもいくつか触れます。とにかく、私がGraalをとても好きなのです。デモも実施しつつ、Graalのすごさを伝えられればと考えています。

Published in: Technology
  • Be the first to comment

Graal in GraalVM - A New JIT Compiler

  1. 1. Graal in GraalVM - A New JIT Compiler - KanJava JUG PONOS Corporation Koichi Sakata(@jyukutyo) #LINE_DM
  2. 2. About Me • Koichi Sakata / 阪田 浩一 – @jyukutyo • JUG Leader (KanJava JUG) • JVM Devotee – Want to Be A JVM / JVMになりたい • PONOS Corporation – Kyoto
  3. 3. About KanJava • Kansai Java Engineer Group – Kansai Region (Osaka/Kyoto/Kobe) – JCP (Java Community Process) Member • Founded in 2009 • Over 800 Members • Event Every Two Month
  4. 4. Today’s Theme New Java JIT Compiler: Graal
  5. 5. Before That
  6. 6. http://www.graalvm.org/
  7. 7. http://www.graalvm.org/
  8. 8. GraalVM • Graal – JIT Compiler • Truffle – Language Implementation Framework • Substrate VM – Runtime Library and a Set of Tools for Building Java AOT Compiled Code
  9. 9. Top 10 Things To Do With GraalVM 1. High-performance modern Java 2. Low-footprint, fast- startup Java 3. Combine JavaScript, Java, Ruby, and R 4. Run native languages on the JVM 5. Tools that work across all languages 6. Extend a JVM-based application 7. Extend a native application 8. Java code as a native library 9. Polyglot in the database 10.Create your own language
  10. 10. http://www.graalvm.org/
  11. 11. Top 10 Things To Do With GraalVM 1. High-performance modern Java 2. Low-footprint, fast- startup Java 3. Combine JavaScript, Java, Ruby, and R 4. Run native languages on the JVM 5. Tools that work across all languages 6. Extend a JVM-based application 7. Extend a native application 8. Java code as a native library 9. Polyglot in the database 10.Create your own language
  12. 12. Polyglot HotSpot VM JVMCI Graal JVM lang Truffle LLVMJS R Ruby C C++ Fortran Interpreter
  13. 13. Interoperability
  14. 14. Demo: Polyglot Shell Polyglot.eval('ruby', '"100".to_i')
  15. 15. Polyglot on the JVM with Graal
  16. 16. Create Your Own Language • https://github.com/jyukutyo/JVM-Math-Language
  17. 17. GraalVM ≠ Graal
  18. 18. Today’s Theme New Java JIT Compiler: Graal
  19. 19. What is a JIT Compiler?
  20. 20. Run Java Code on JVM
  21. 21. JVM interprets Java Bytecode
  22. 22. Interpreting is very slow...
  23. 23. JIT Compiler Java Bytecode ↓ Machine Code
  24. 24. Speculation Without Regret: Reducing Deoptimization Meta-data in the Graal compiler
  25. 25. JIT Compilation Fast Compilation Speed vs Better Optimization
  26. 26. JIT Compilation • Case: Fast Compilation Speed – Fast Compilation – Not Efficient • Case: Better Optimization – Slow Compilation – Efficient
  27. 27. HotSpot VM Compiler Interface C2C1 HotSpot VM C++
  28. 28. GraalVM Compiler Interface GraalC1 HotSpot VM JVMCI Java
  29. 29. JVMCI http://openjdk.java.net/jeps/243
  30. 30. JVMCI • JVM Compiler Interface –Write our own JIT compiler in Java –Use another JIT compiler at runtime
  31. 31. Graal Uses JVMCI!
  32. 32. We can use Graal only in GraalVM?
  33. 33. Not Only in GraalVM http://openjdk.java.net/jeps/295
  34. 34. In Java 9 Graal is used for AOT compilation (jaotc) * only supports Linux/x64
  35. 35. JIT Compiler Java Bytecode ↓ Machine Code
  36. 36. Not Only in GraalVM http://openjdk.java.net/jeps/317
  37. 37. In Java 10 Java-Based JIT Compiler = Graal
  38. 38. Java 10 Demo • java -XX:+UnlockExperimentalVMOptions -XX:+UseJVMCICompiler -XX:+PrintCompilation Demo
  39. 39. Graal is written in Java!
  40. 40. It’s Human-readable
  41. 41. HotSpot VM Compiler Interface C2C1 HotSpot VM C++
  42. 42. Real-World Example
  43. 43. Twitter uses Graal in PRODUCTION!
  44. 44. Twitter & Graal • Twitter’s JDK – Based on OpenJDK 8u – JEP 243 (JVMCI) Backport, Graal etc. • Twitter's Quest for a Wholly Graal Runtime – YouTube • Improve Performance & Save $$$
  45. 45. TWITTER'S QUEST FOR A WHOLLY GRAAL RUNTIME
  46. 46. TWITTER'S QUEST FOR A WHOLLY GRAAL RUNTIME
  47. 47. TWITTER'S QUEST FOR A WHOLLY GRAAL RUNTIME
  48. 48. Performance Compiling Scala Faster with GraalVM Scala Compiler Benchmark with Scala 2.12.6
  49. 49. Graal • Actively Developed/Researched by – Oracle Labs – Johannes Kepler University of Linz • Institute for System Software • Compiler and JVM Research at JKU
  50. 50. JIT Compilation Process
  51. 51. Linear Scan Register Allocation for the Java HotSpot™ Client Compiler
  52. 52. IR (中間表現) • HIR – High-level Intermediate Representation – Platform Independent • LIR – Low-level Intermediate Representation – Platform Dependent
  53. 53. Graal IR Partial Escape Analysis and Scalar Replacement for Java
  54. 54. Graal Graph int average(int a, int b) { return (a + b) / 2; }
  55. 55. Graal Graph int average(int[] values) { int sum = 0; for (int n = 0; n < values.length; n++) { sum += values[n]; } return sum / values.length; }
  56. 56. Graal Graph
  57. 57. Partial Escape Analysis and Scalar Replacement for Java
  58. 58. Snippets: Taking the High Road to a Low Level
  59. 59. Many Optimizations • Assumptions • Type-Checked Inlining • Polymorphic Inlining • Intrinsification • Read Optimization • Loop Refactoring • Loop Unrolling • Tail Duplication • Partial Escape Analysis • Graph Caching • Use Exception Probability • Conditional Elimination • Simulation Based Path Duplication
  60. 60. Optimazaions More Inlining As the Result
  61. 61. Partial Escape Analysis
  62. 62. Escape Analysis • Analyze where references to new objects flow – Look for “escapes” • Method call parameter • Static field • Return value • Throw • etc.
  63. 63. Escape Analysis static Object field; static Object foo() { Object a = new Object(); field = a; Object b = new Object(); method(b); Object c = new Object(); return c; } static void method(Object o) {...} Object a,b,c escapes Optimizing Allocations with Partial Escape Analysis
  64. 64. If the object doesn’t escape, there might be optimization opportunities
  65. 65. Escape Analysis public Person get(String name, int age) { Person p = new Person(name, age); Person cachedPerson = null; for (int i = 0; i < cache.length(); i++) { Person c = cache[i]; if (p.name.equals(c.name) && p.age == c.age) { cachedPerson = c; break; } } if (cachedPerson != null) { return cachedPerson; } return null; } Object p doesn’t escape Optimizing Allocations with Partial Escape Analysis
  66. 66. Scalar Replacement public Person get(String name, int age) { Person cachedPerson = null; for (int i = 0; i < cache.length(); i++) { Person c = cache[i]; if (name.equals(c.name) && age == c.age) { cachedPerson = c; break; } } if (cachedPerson != null) { return cachedPerson; } return null; } • Field loads replaced with local variables • Allocation was removed Optimizing Allocations with Partial Escape Analysis
  67. 67. Partial Escape Analysis public Person get(String name, int age) { Person p = new Person(name, age); Person cachedPerson = null; for (int i = 0; i < cache.length(); i++) { Person c = cache[i]; if (p.name.equals(c.name) && p.age == c.age) { cachedPerson = c; break; } } if (cachedPerson != null) { return cachedPerson; } addToCache(p); return p; } Object p escapes... Optimizing Allocations with Partial Escape Analysis
  68. 68. Partial Escape Analysis public Person get(String name, int age) { Person p = new Person(name, age); Person cachedPerson = null; for (int i = 0; i < cache.length(); i++) { Person c = cache[i]; if (p.name.equals(c.name) && p.age == c.age) { cachedPerson = c; break; } } if (cachedPerson != null) { return cachedPerson; } addToCache(p); return p; } If the if statement is true, object p doesn’t escape Optimizing Allocations with Partial Escape Analysis
  69. 69. Partial Escape Analysis public Person get(String name, int age) { Person p = new Person(name, age); Person cachedPerson = null; for (int i = 0; i < cache.length(); i++) { Person c = cache[i]; if (name.equals(c.name) && age == c.age) { cachedPerson = c; break; } } if (cachedPerson != null) { return cachedPerson; } Person p = new Person(name, age); addToCache(p); return p; } Optimizing Allocations with Partial Escape Analysis No allocation in this path (It might be a frequent path)
  70. 70. Partial Escape Analysis Analyzing the Escapability of Objects for Individual Branches

×