Pro Yearly is on sale from $80 to $50! »

An Introduction to Truffle and Graal

An Introduction to Truffle and Graal

or how to get a just-in-time compiler by writing an interpreter.

Truffle is framework to write a programming language implementation as an Abstract-Syntax-Tree Interpreter. Together with the Graal Just-in-Time compiler, it achieve competitive performance with the best just-in-time compilers such as V8 for JavaScript. In particular, the Ruby implementation I am working on, TruffleRuby achieves speedups of more than 10x faster than the standard implementation on classic benchmarks.
We also observed impressive speedups for Mozart-Graal, a new implementation of the Oz language using Truffle, also the subject of a master thesis at UCL. Graal and Truffle can be seen as a way to obtain a Just-in-Time compiler by only writing an AST interpreter for the language, which is itself the simplest way to implement a language. This is possible in great parts thanks to an application of Partial Evaluation, an idea described in 1983 and now made a reality.

0ea7f61aec8fee539be0cf39b7bab77c?s=128

Benoit Daloze

May 22, 2017
Tweet

Transcript

  1. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.

          An  Introduc@on  to   Truffle  and  Graal   Or  how  to  get  a  just-­‐in-­‐1me  compiler   by  wri1ng  an  interpreter   Benoit  Daloze   Johannes  Kepler  University   22  May  2017     1  
  2. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.

          Safe  Harbor  Statement   The  following  is  intended  to  outline  our  general  product  direc@on.  It  is  intended  for   informa@on  purposes  only,  and  may  not  be  incorporated  into  any  contract.  It  is  not  a   commitment  to  deliver  any  material,  code,  or  func@onality,  and  should  not  be  relied  upon   in  making  purchasing  decisions.  The  development,  release,  and  @ming  of  any  features  or   func@onality  described  for  Oracle’s  products  remains  at  the  sole  discre@on  of  Oracle.   2  
  3. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.

          Who  am  I  ?   •  Benoit  Daloze   •  PhD  student  at  Johannes  Kepler  University   •  Research  with  TruffleRuby  on  concurrency   •  MRI  and  JRuby  commiXer   •  Maintainer  of  the  Ruby  Spec  Suite  (ruby/spec)   3   TwiXer:  @eregontp   GitHub:  @eregon  
  4. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.

            Agenda   GraalVM   The  Truffle  Approach   Performance   Language  Interoperability     1   2   3   4   4  
  5. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.

          GraalVM   5  
  6. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.

          Graal  and  GraalVM   •  Graal:  A  new  JIT  compiler  for  Java   wriXen  in  Java.   •  GraalVM  is  based  on  the  HotSpotVM.   •  Designed  for  mul@-­‐language   performance.   •  Graal  is  open  source,  as  are  many  of   the  language  projects.   •  Binary  release  available  from  the   Oracle  Technology  Network  (OTN)   with  support  for  Java,  JavaScript,   Ruby,  and  R.   6  
  7. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.

          Guest Language Application Guest Language Implementation Host Services OS Written by: Application Developer VM Expert OS Expert Language Developer Guest Language Java Java / C++ Unmanaged Language (typically C or C++) Java Written in: Graal VM Truffle 7   7  
  8. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.

          Status  of  Graal   •  The  Graal  compiler  is  open  source   – hXps://github.com/graalvm/graal   – Full  support  of  the  Java  specifica@on   •  JEP  243:  Java-­‐Level  JVM  Compiler  Interface   – hXp://openjdk.java.net/jeps/243     – Will  be  in  JDK9   •  Graal  JIT  compiler  can  used  with  any  JDK9  VM.   8  
  9. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.

          The  Truffle  Approach   9  
  10. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.

          Computer  Language  Benchmarks  Game  
  11. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.

          Goal: Computer  Language  Benchmarks  Game  
  12. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.

          “Write  Your  Own  Language”   12   Prototype  a  new  language   Parser  and  language  work  to  build  syntax  tree  (AST),     AST  Interpreter   Write  a  “real”  VM   In  C/C++,  s@ll  using  AST  interpreter,  spend  a  lot  of  @me     implemen@ng  run@me  system,  GC,  …   People  start  using  it   Define  a  bytecode  format  and  write  bytecode  interpreter   People  complain  about  performance   Write  a  JIT  compiler,  improve  the  garbage  collector   Performance  is  s@ll  bad   Prototype  a  new  language  in  Java   Parser  and  language  work  to  build  syntax  tree  (AST)   Execute  using  AST  interpreter   People  start  using  it   And  it  is  already  fast   And  it  integrates  with  other  languages   And  it  has  tool  support,  e.g.,  a  debugger   Current situation How it should be
  13. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.

          (a + b) * c   •  Abstract  syntax  tree  IS  the  interpreter   •  Every  node  has  an  execute  method   •  Java  program  that  interprets  the  guest  language   * + c a b Tradi@onal  AST  interpreter   13   Object execute(VirtualFrame frame) { Object a = left.execute(frame); Object b = right.execute(frame); return add(a, b); } Object add(Object a, Object b) { if (a instanceof Integer && b instanceof Integer) { return (int)a + (int)b; } else if (a instanceof String && b instanceof String) { return (String)a + (String)b; } else { return genericAdd(a, b); } } 13  
  14. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.

          Object add(Object a, Object b) {   if (a instanceof Integer && b instanceof Integer) {   return (int)a + (int)b;   } else if (a instanceof String && b instanceof String) {   return (String)a + (String)b;   } else {   return genericAdd(a, b);   }   }   int add(int a,   int b) {   return a + b;   }   String add(String a,   String b) {   return a + b;   }   14   14   Object add(Object a,   Object b) {   return genericAdd(a, b);   }  
  15. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.

          U U U U U I I I G G I I I G G Node Rewriting for Profiling Feedback AST Interpreter Rewritten Nodes AST Interpreter Uninitialized Nodes Compilation using Partial Evaluation Compiled Code Node Transitions S U I D G Uninitialized Integer Generic Double String 15   15  
  16. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.

          16   Par@al  Evalua@on  and  the  1st  Futamura  projec@on   Par@allyEvaluate(      Ruby  interpreter,      known  Ruby  program/method)  =>  compiler  graph  =>  machine  code   Very  similar  to  a  Just-­‐in-­‐Time  Compiler  wriXen  for  Ruby  specifically!     •  Par@al  evalua@on  inlines  the  AST  execute()  methods  and   Graal  op@mizes  and  translates  the  resul@ng  graph  to  machine  code   •  Par@al  evalua@on  brings  context  to  the  compila@on:  The  AST  of  a  method   is  a  set  of  nodes  instances  which  are  compiled  together  
  17. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.

          I I I G G I I I G G Deoptimization to AST Interpreter D I D G G D I D G G Node Rewriting to Update Profiling Feedback Recompilation using Partial Evaluation 17   17  
  18. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.

          Performance   18  
  19. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.

          Performance  Disclaimers   •  All  Truffle  numbers  reflect  a  development  snapshot   –  Subject  to  change  at  any  @me  (hopefully  improve)   –  You  have  to  know  a  benchmark  to  understand  why  it  is  slow  or  fast   •  We  are  not  claiming  to  have  complete  language  implementa@ons   –  JavaScript:  passes  100%  of  ECMAscript  standard  tests   •  Working  on  full  compa@bility  with  V8  for  Node.JS   –  Ruby:  passing  100%  of  RubySpec  language  tests   •  Passing  around  94%  of  the  core  library  tests   –  R:  prototype,  but  already  complete  enough  and  fast  for  a  few  selected  workloads   •  Benchmarks  that  are  not  shown   –  may  not  run  at  all,  or   –  may  not  run  fast   19  
  20. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.

          Performance:  JavaScript   20   0   0.2   0.4   0.6   0.8   1   1.2   1.4   box2d   Deltablue   Crypto   EarleyBoyer   Gameboy   NavierStokes   Richards   Raytrace   Splay   Geomean   Speedup,  higher  is  beBer   Performance  rela1ve  to  V8   JavaScript performance: similar to V8  
  21. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.

          Performance:  R  with  Scalar  Code   21   0   10   20   30   40   50   60   70   80   90   100   Speedup,  higher  is  beBer   Performance  rela1ve  to  GNU  R  with  bytecode  interpreter   660x   Huge speedups on scalar code, GNU R is only optimized for vector operations  
  22. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.

          Performance:  Ruby  Compute-­‐Intensive  Kernels   22   0   50   100   150   200   250   300   350   Speedup,  higher  is  beBer   Performance  rela1ve  to  JRuby  running  with  Java  HotSpot  server  compiler     Huge speedup because Truffle can optimize through Ruby metaprogramming  
  23. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.

          Speedup  rela@ve  to  MRI  
  24. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.

          • • • • MRI 2.3 JRuby 9.0.4 Node.js JRuby+Truffle Java 1.8.0u66 1 5 10 25 50 75 12  Benchmarks  using  Objects,  Closures  and  Arrays        from  hXps://github.com/smarr/are-­‐we-­‐fast-­‐yet                TruffleRuby  
  25. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.

          25   Optcarrot:  NES  emulator  benchmark  
  26. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.

          Optcarrot:  NES  emulator  benchmark  –  9x  faster  than  MRI   26  
  27. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.

          27   Mozart-­‐Graal:  {Pow  X  7}  
  28. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.

          28   Mozart-­‐Graal:  {Map  Xs  fun  {$  X}  X  +  3  end}  
  29. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.

          Language  interoperability   PuMng  the  pieces  together   29  
  30. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.

          29   You  can  execute  any  language  on  the  JVM  /  CLR   -­‐  as  long  as  it  looks  like  Java  /  C#.     30  
  31. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.

          31   Let’s  write  a  small  part  of  a  program  in  a  different  language!   Ø There  is  no  interface  to  the  language  I  can  use             Ø I  don’t  want  to  write  a  lot  of  wrapper  code             Ø Performance  will  be  bad             #include<stdio.h> struct complex { double r; double i; } int main() { struct complex *a = …; struct complex *b = …; add(a, b); } function add(a, b) { var result = {r:0, i:0}; result.r = a.r + b.r; result.i = a.i + b.i; return result; } struct complex *a struct complex *b C   JavaScript   31  
  32. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.

          High-­‐Performance  Language  Interoperability   Dynamic Compilation = a . obj obj is a C struct = a obj !!" ! JS Msg = a is C? -> Message Resolution Msg !! ! C JS-specific object access Language-independent object access C-specific object access var a = obj.value; value value Read obj value Map JS access to a message Map message to a C access JS JS JS JS C C JS JS Machine Code 32   32   Dynamic Compilation = a . obj obj is a C struct = a obj !!" ! JS Msg = a is C? -> Message Resolution Msg !! ! C JS-specific object access Language-independent object access C-specific object access var a = obj.value; value value Read obj value Map JS access to a message Map message to a C access JS JS JS JS C C JS JS Machine Code
  33. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.

          High-­‐Performance  Language  Interoperability   obj is a Ruby object Msg Rb !!" ! C-specific object access Map message to Rb access Dynamic Compilation = a is C? -> obj value = a is C? -> obj value -> obj value is Rb? JS JS JS JS C C JS Rb JS Rb JS Machine Code C-specific and Rb-specific object accesses var a = obj.value; 33   33  
  34. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.

          34   Demo  Time  !  
  35. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.

          Conclusion   35  
  36. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.

          There  would  be  much  more  to  talk  about!   Language   interoperability   Paralleliza@on   Database  Driver   Op@miza@ons   Debuggability,   Analyzability   36  
  37. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.

          Resources   •  Graal  on  GitHub   – hXps://github.com/graalvm/graal   – Mailing  list:  graal-­‐dev@openjdk.java.net   •  Technology  preview  release  of  GraalVM   – hXp://www.oracle.com/technetwork/oracle-­‐labs/program-­‐languages/   37