Slide 1

Slide 1 text

Copyright  ©  2015  Oracle  and/or  its  affiliates.  All  rights  reserved.     One VM To Rule Them All! And With Interoperability Bind Them Matthias Grimmer, @grimmer_m, JKU Linz Thomas Wuerthinger, @thomaswue, Oracle Labs

Slide 2

Slide 2 text

Copyright  ©  2015  Oracle  and/or  its  affiliates.  All  rights  reserved.       2   Let’s ask a search engine… One Language to Rule Them All?

Slide 3

Slide 3 text

Copyright  ©  2015  Oracle  and/or  its  affiliates.  All  rights  reserved.       3   One Language to Rule Them All? Let’s ask Stack Overflow…

Slide 4

Slide 4 text

Copyright  ©  2015  Oracle  and/or  its  affiliates.  All  rights  reserved.       4   C  

Slide 5

Slide 5 text

Copyright  ©  2015  Oracle  and/or  its  affiliates.  All  rights  reserved.       5   3   Goal: One VM for all languages means interoperability and being able to choose the best language for the task! Lower is better

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

Copyright  ©  2015  Oracle  and/or  its  affiliates.  All  rights  reserved.       Everything is a dynamic map Everything is a map and every operation a function call Everything is a map and every operation a function call and everything is a vector C   Everything is an address

Slide 8

Slide 8 text

Copyright  ©  2015  Oracle  and/or  its  affiliates.  All  rights  reserved.       8 Parser and language work to build syntax tree (AST), AST Interpreter Write a “real” VM In C/C++, still using AST interpreter, spend a lot of time implementing runtime system, GC, … Define a bytecode format and write bytecode interpreter People complain about performance Write a JIT compiler Improve the garbage collector Performance is still bad Prototype a new language

Slide 9

Slide 9 text

Copyright  ©  2015  Oracle  and/or  its  affiliates.  All  rights  reserved.       9 Parser and language work to build syntax tree (AST), AST Interpreter Write a “real” VM In C/C++, still using AST interpreter, spend a lot of time implementing runtime 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 still bad Prototype a new language

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

Copyright  ©  2015  Oracle  and/or  its  affiliates.  All  rights  reserved.       11   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

Slide 12

Slide 12 text

Copyright  ©  2015  Oracle  and/or  its  affiliates.  All  rights  reserved.       12   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 int main() { struct complex *a = …; struct complex *b = …; add(a, b); }

Slide 13

Slide 13 text

Copyright  ©  2015  Oracle  and/or  its  affiliates.  All  rights  reserved.       13   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 int main() { struct complex *a = …; struct complex *b = …; add(a, b); }

Slide 14

Slide 14 text

Copyright  ©  2015  Oracle  and/or  its  affiliates.  All  rights  reserved.       14   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 C  

Slide 15

Slide 15 text

Copyright  ©  2015  Oracle  and/or  its  affiliates.  All  rights  reserved.       15   a + b Object int String

Slide 16

Slide 16 text

Copyright  ©  2015  Oracle  and/or  its  affiliates.  All  rights  reserved.       16   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); } }

Slide 17

Slide 17 text

Copyright  ©  2015  Oracle  and/or  its  affiliates.  All  rights  reserved.       17   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; }

Slide 18

Slide 18 text

Copyright  ©  2015  Oracle  and/or  its  affiliates.  All  rights  reserved.       18   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; }

Slide 19

Slide 19 text

Copyright  ©  2015  Oracle  and/or  its  affiliates.  All  rights  reserved.       19   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); } } Object add(Object a, Object b) { return genericAdd(a, b); } int add(int a, int b) { return a + b; } String add(String a, String b) { return a + b; }

Slide 20

Slide 20 text

Copyright  ©  2015  Oracle  and/or  its  affiliates.  All  rights  reserved.       20   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

Slide 21

Slide 21 text

Copyright  ©  2015  Oracle  and/or  its  affiliates.  All  rights  reserved.       21   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

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

Copyright  ©  2015  Oracle  and/or  its  affiliates.  All  rights  reserved.       Graal VM § A new JIT compiler for Java written in Java. § Designed for multi-language performance. § Downloadable from the Oracle Technology Network (OTN) with Java and JavaScript support. C

Slide 25

Slide 25 text

Copyright  ©  2015  Oracle  and/or  its  affiliates.  All  rights  reserved.       25   Graal   Truffle   R   Ruby   Java   Scala   Smalltalk   C   J   Python   JavaScript   Parallel  Graph  AnalyLcs   C  

Slide 26

Slide 26 text

Copyright  ©  2015  Oracle  and/or  its  affiliates.  All  rights  reserved.       26 Performance – Java and Scala 0   0.5   1   1.5   2   2.5   Java   Scala   server   graal   DaCapo benchmarks - Normalized versus client compiler Higher is better

Slide 27

Slide 27 text

Copyright  ©  2015  Oracle  and/or  its  affiliates.  All  rights  reserved.       27 0   1   2   3   4   5   6   7   V8   Graal/JS   Performance – JavaScript Higher is better

Slide 28

Slide 28 text

Copyright  ©  2015  Oracle  and/or  its  affiliates.  All  rights  reserved.       28 Performance – C 0   50   100   150   200   250   300   350   400   450   500   Composite   NB   FA   SN   FK   MB   FFT   SOR   MC   SM   LU   GCC  O0   GCC  best   TruffleC   Higher is better

Slide 29

Slide 29 text

Copyright  ©  2015  Oracle  and/or  its  affiliates.  All  rights  reserved.       29   C  

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

Copyright  ©  2015  Oracle  and/or  its  affiliates.  All  rights  reserved.       32   CRuby FFI

Slide 33

Slide 33 text

Copyright  ©  2015  Oracle  and/or  its  affiliates.  All  rights  reserved.       33   CRuby FFI NOT SUPPORTED

Slide 34

Slide 34 text

Copyright  ©  2015  Oracle  and/or  its  affiliates.  All  rights  reserved.       34   1 typedef VALUE void*; 2 typedef ID void*; 3 4 // Define a C function as a Ruby method 5 void rb_define_method 6 (VALUE class, const char* name, 7 VALUE(*func)(), int argc); 8 9 // Store an array element into a Ruby array 10 void rb_ary_store 11 (VALUE ary, long idx, VALUE val); 12 13 // Get the Ruby internal representation of an 14 // identifier 15 ID rb_intern(const char* name); 16 17 // Get instance variables of a Ruby object 18 VALUE rb_iv_get(VALUE object, 19 const char* iv_name) 20 21 // Invoke a Ruby method from C 22 VALUE rb_funcall(VALUE receiver ID method_id, 23 int argc, ...); 24 25 // Convert a Ruby Fixnum to C long 26 long FIX2INT(VALUE value); Well designed API? è Internal functions of CRuby

Slide 35

Slide 35 text

Copyright  ©  2015  Oracle  and/or  its  affiliates.  All  rights  reserved.       35   CRuby MRI API *.rb *.c executes extension

Slide 36

Slide 36 text

Copyright  ©  2015  Oracle  and/or  its  affiliates.  All  rights  reserved.       36   VALUE rb_ary_write(VALUE ary, long idx, VALUE value); rubyArray = Array.new 42 #include “ruby.h” // … rb_ary_write(rubyArray, 0, DBL2NUM(42)); // … TruffleRuby Java *.rb executes Graal/Ruby *.c TruffleC Java executes Graal/C

Slide 37

Slide 37 text

Copyright  ©  2015  Oracle  and/or  its  affiliates.  All  rights  reserved.       37   VALUE rb_ary_write(VALUE ary, long idx, VALUE value); *.c TruffleC Java executes TruffleRuby Java *.rb executes rubyArray = Array.new 42 #include “ruby.h” // … rb_ary_write(rubyArray, 0, DBL2NUM(42)); // … rubyArray.[] = 0, 42 Graal/Ruby Graal/C

Slide 38

Slide 38 text

Copyright  ©  2015  Oracle  and/or  its  affiliates.  All  rights  reserved.       38   1x   8x   11x   0   5   10   15   20   25   30   35   CRuby   Graal/Ruby   CRuby+C   Graal/Ruby+C   Image  Processing  Composite  Speedup   Pure Ruby Higher is better

Slide 39

Slide 39 text

Copyright  ©  2015  Oracle  and/or  its  affiliates.  All  rights  reserved.       39   1x   8x   11x   0   5   10   15   20   25   30   35   CRuby   Graal/Ruby   CRuby+C   Graal/Ruby+C   Image  Processing  Composite  Speedup   Pure Ruby Ruby + C Higher is better

Slide 40

Slide 40 text

Copyright  ©  2015  Oracle  and/or  its  affiliates.  All  rights  reserved.       40   1x   8x   11x   32x   0   5   10   15   20   25   30   35   CRuby   Graal/Ruby   CRuby+C   Graal/Ruby+C   Image  Processing  Composite  Speedup   Pure Ruby Ruby + C Higher is better

Slide 41

Slide 41 text

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

Slide 42

Slide 42 text

Copyright  ©  2015  Oracle  and/or  its  affiliates.  All  rights  reserved.       42   Let’s write a small part of a program in a different language! Ø There is no interface to the language I would need Ø 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

Slide 43

Slide 43 text

Copyright  ©  2015  Oracle  and/or  its  affiliates.  All  rights  reserved.       43   def function(v1, v2) sum = [] for i in 0..n do sum[i] = v1[i] + v2[i] end end function(v1, v2) { v1 + v2 } v1 v2 Let’s write a small part of a program in a different language! Ø There is no interface to the language I would need Ø I don’t want to write a lot of wrapper code Ø Performance will be bad

Slide 44

Slide 44 text

Copyright  ©  2015  Oracle  and/or  its  affiliates.  All  rights  reserved.       44   Programmers want to write programs in multiple languages Use the most suitable language for a given problem §  Existing approaches are often pairwise efforts Gradually migrate existing projects §  Programmers need to write wrapper code Reusing existing source code §  Language borders introduce a performance bottleneck

Slide 45

Slide 45 text

Copyright  ©  2015  Oracle  and/or  its  affiliates.  All  rights  reserved.       45   #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; } add(a, b) a->r b->r a->i b->i main.c complex.js mx graalML main.c complex.js

Slide 46

Slide 46 text

Copyright  ©  2015  Oracle  and/or  its  affiliates.  All  rights  reserved.       46   Programmers want to write programs in multiple languages Use the most suitable language for a given problem §  Existing approaches are often pairwise efforts Gradually migrate existing projects §  Programmers need to write wrapper code Reusing existing source code §  Language borders introduce a performance bottleneck

Slide 47

Slide 47 text

Copyright  ©  2015  Oracle  and/or  its  affiliates.  All  rights  reserved.       47   Programmers want to write programs in multiple languages Use the most suitable language for a given problem §  Existing approaches are often pairwise efforts è The Graal VM allows composing arbitrary languages Gradually migrate existing projects §  Programmers need to write wrapper code Reusing existing source code §  Language borders introduce a performance bottleneck

Slide 48

Slide 48 text

Copyright  ©  2015  Oracle  and/or  its  affiliates.  All  rights  reserved.       48   Programmers want to write programs in multiple languages Use the most suitable language for a given problem §  Existing approaches are often pairwise efforts è The Graal VM allows composing arbitrary languages Gradually migrate existing projects §  Programmers need to write wrapper code è Without writing boilerplate code Reusing existing source code §  Language borders introduce a performance bottleneck

Slide 49

Slide 49 text

Copyright  ©  2015  Oracle  and/or  its  affiliates.  All  rights  reserved.       49   Programmers want to write programs in multiple languages Use the most suitable language for a given problem §  Existing approaches are often pairwise efforts è The Graal VM allows composing arbitrary languages Gradually migrate existing projects §  Programmers need to write wrapper code è Without writing boilerplate code Reusing existing source code §  Language borders introduce a performance bottleneck è Cross-language inlining

Slide 50

Slide 50 text

Copyright  ©  2015  Oracle  and/or  its  affiliates.  All  rights  reserved.       High-Performance Language Interoperability 0   200   400   600   800   1000   1200   Ruby  data   JS  data   C  data   Ruby  program   JS  program   C  program   C Scores for mixing Ruby/JS/C executing the scimark benchmarks. Higher is better

Slide 51

Slide 51 text

Copyright  ©  2015  Oracle  and/or  its  affiliates.  All  rights  reserved.       §  51 §  Graal VM Binaries for Java and JavaScript! http://www.oracle.com/technetwork/oracle-labs/program-languages §  Graal and Truffle API! http://openjdk.java.net/projects/graal/ §  R! https://bitbucket.org/allr/fastr §  Ruby! https://github.com/jruby/jruby/wiki/Truffle §  Python! https://bitbucket.org/ssllab/zippy §  Other projects for Smalltalk, Clojure, Lua, …

Slide 52

Slide 52 text

Copyright  ©  2015  Oracle  and/or  its  affiliates.  All  rights  reserved.       52   Your Language? http://openjdk.java.net/projects/graal/ graal-dev@openjdk.java.net $  hg  clone  hZp://hg.openjdk.java.net/graal/graal   $  cd  graal   $  ./mx.sh  -­‐-­‐vm  server  build   $  ./mx.sh  ideinit   $  ./mx.sh  -­‐-­‐vm  server  uniZest  SumTest  

Slide 53

Slide 53 text

Copyright  ©  2015  Oracle  and/or  its  affiliates.  All  rights  reserved.       53 Safe Harbor Statement The preceding is intended to provide some insight into a line of research in Oracle Labs. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, and timing of any features or functionality described in connection with any Oracle product or service remains at the sole discretion of Oracle. Any views expressed in this presentation are my own and do not necessarily reflect the views of Oracle.

Slide 54

Slide 54 text

Copyright  ©  2015  Oracle  and/or  its  affiliates.  All  rights  reserved.     Q/A

Slide 55

Slide 55 text

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

Slide 56

Slide 56 text

No content