Slide 1

Slide 1 text

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  

Slide 2

Slide 2 text

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  

Slide 3

Slide 3 text

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  

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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  

Slide 7

Slide 7 text

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  

Slide 8

Slide 8 text

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  

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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  

Slide 14

Slide 14 text

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);   }  

Slide 15

Slide 15 text

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  

Slide 16

Slide 16 text

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  

Slide 17

Slide 17 text

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  

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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  

Slide 20

Slide 20 text

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  

Slide 21

Slide 21 text

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  

Slide 22

Slide 22 text

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  

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

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  

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

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  

Slide 31

Slide 31 text

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 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  

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

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  

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

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  

Slide 37

Slide 37 text

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.       Resources   •  Graal  on  GitHub   – hXps://github.com/graalvm/graal   – Mailing  list:  graal-­‐[email protected]   •  Technology  preview  release  of  GraalVM   – hXp://www.oracle.com/technetwork/oracle-­‐labs/program-­‐languages/   37