65

Implemen@ng*Ruby* Using*Truffle*and*Graal*ecoop14.it.uu.se/programme/ecoop-ruby-truffle.pdf · for Profiling Feedback AST Interpreter Rewritten Nodes AST Interpreter Uninitialized

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Implemen@ng*Ruby* Using*Truffle*and*Graal*ecoop14.it.uu.se/programme/ecoop-ruby-truffle.pdf · for Profiling Feedback AST Interpreter Rewritten Nodes AST Interpreter Uninitialized
Page 2: Implemen@ng*Ruby* Using*Truffle*and*Graal*ecoop14.it.uu.se/programme/ecoop-ruby-truffle.pdf · for Profiling Feedback AST Interpreter Rewritten Nodes AST Interpreter Uninitialized

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

Implemen@ng  Ruby  Using  Truffle  and  Graal  

Chris  Seaton  @ChrisGSeaton    ECOOP  Summer  Schools  2014    

2  

Page 3: Implemen@ng*Ruby* Using*Truffle*and*Graal*ecoop14.it.uu.se/programme/ecoop-ruby-truffle.pdf · for Profiling Feedback AST Interpreter Rewritten Nodes AST Interpreter Uninitialized

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

Safe  Harbor  Statement  The  following  is  intended  to  provide  some  insight  into  a  line  of  research  in  Oracle  Labs.  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.  Oracle  reserves  the  right  to  alter  its  development  plans  and  prac@ces  at  any  @me,  and  the  development,  release,  and  @ming  of  any  features  or  func@onality  described  in  connec@on  with  any  Oracle  product  or  service  remains  at  the  sole  discre@on  of  Oracle.    Any  views  expressed  in  this  presenta@on  are  my  own  and  do  not  necessarily  reflect  the  views  of  Oracle.  

3  

Page 4: Implemen@ng*Ruby* Using*Truffle*and*Graal*ecoop14.it.uu.se/programme/ecoop-ruby-truffle.pdf · for Profiling Feedback AST Interpreter Rewritten Nodes AST Interpreter Uninitialized

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

We’re  going  to  talk  about  

Mo@va@on  

Truffle  and  Graal  Theory  

Truffle  and  Graal  in  Prac@ce  

Applying  it  to  Ruby  

1  

2  

3  

4  

4  

Page 5: Implemen@ng*Ruby* Using*Truffle*and*Graal*ecoop14.it.uu.se/programme/ecoop-ruby-truffle.pdf · for Profiling Feedback AST Interpreter Rewritten Nodes AST Interpreter Uninitialized

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

Mo@va@on  

5  

Page 6: Implemen@ng*Ruby* Using*Truffle*and*Graal*ecoop14.it.uu.se/programme/ecoop-ruby-truffle.pdf · for Profiling Feedback AST Interpreter Rewritten Nodes AST Interpreter Uninitialized

Copyright  ©  2014  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   6  

Page 7: Implemen@ng*Ruby* Using*Truffle*and*Graal*ecoop14.it.uu.se/programme/ecoop-ruby-truffle.pdf · for Profiling Feedback AST Interpreter Rewritten Nodes AST Interpreter Uninitialized

Copyright  ©  2014  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   7  

Page 8: Implemen@ng*Ruby* Using*Truffle*and*Graal*ecoop14.it.uu.se/programme/ecoop-ruby-truffle.pdf · for Profiling Feedback AST Interpreter Rewritten Nodes AST Interpreter Uninitialized

Copyright  ©  2014  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   8  

3  

Computer  Language  Benchmarks  Game  

Page 9: Implemen@ng*Ruby* Using*Truffle*and*Graal*ecoop14.it.uu.se/programme/ecoop-ruby-truffle.pdf · for Profiling Feedback AST Interpreter Rewritten Nodes AST Interpreter Uninitialized

Copyright  ©  2014  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   9  

3  

Goal:

Computer  Language  Benchmarks  Game  

Page 10: Implemen@ng*Ruby* Using*Truffle*and*Graal*ecoop14.it.uu.se/programme/ecoop-ruby-truffle.pdf · for Profiling Feedback AST Interpreter Rewritten Nodes AST Interpreter Uninitialized

Copyright  ©  2014  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   10  

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  

Current situation How it should be

Page 11: Implemen@ng*Ruby* Using*Truffle*and*Graal*ecoop14.it.uu.se/programme/ecoop-ruby-truffle.pdf · for Profiling Feedback AST Interpreter Rewritten Nodes AST Interpreter Uninitialized

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

Truffle  and  Graal  Theory  

11  

Page 12: Implemen@ng*Ruby* Using*Truffle*and*Graal*ecoop14.it.uu.se/programme/ecoop-ruby-truffle.pdf · for Profiling Feedback AST Interpreter Rewritten Nodes AST Interpreter Uninitialized

Copyright  ©  2014  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   12  

Guest  Language  

JVM  

Bytecode  

Page 13: Implemen@ng*Ruby* Using*Truffle*and*Graal*ecoop14.it.uu.se/programme/ecoop-ruby-truffle.pdf · for Profiling Feedback AST Interpreter Rewritten Nodes AST Interpreter Uninitialized

Copyright  ©  2014  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   13  

Guest  Language  

Graal  VM  

Java  IR,  machine  code  cache,  invalida@on  and  deop@misa@on,  op@misa@on  phases,  replacements,  etc…  etc…  

Page 14: Implemen@ng*Ruby* Using*Truffle*and*Graal*ecoop14.it.uu.se/programme/ecoop-ruby-truffle.pdf · for Profiling Feedback AST Interpreter Rewritten Nodes AST Interpreter Uninitialized

Copyright  ©  2014  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   14  

Guest  Language  

Graal  VM  

Truffle  

AST  interpreter  

Page 15: Implemen@ng*Ruby* Using*Truffle*and*Graal*ecoop14.it.uu.se/programme/ecoop-ruby-truffle.pdf · for Profiling Feedback AST Interpreter Rewritten Nodes AST Interpreter Uninitialized

Copyright  ©  2014  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   15  

U

U U

U

U I

I I

G

G I

I I

G

G

Node Rewriting for Profiling Feedback

AST InterpreterRewritten Nodes

AST InterpreterUninitialized Nodes

Compilation usingPartial Evaluation

Compiled Code

Node Transitions

S

UI

D

G

Uninitialized Integer

Generic

DoubleString

T.  Würthinger,  C.  Wimmer,  A.  Wöß,  L.  Stadler,  G.  Duboscq,  C.  Humer,  G.  Richards,  D.  Simon,  and  M.  Wolczko.  One  VM  to  rule  them  all.  In  Proceedings  of  Onward!,  2013.  

Page 16: Implemen@ng*Ruby* Using*Truffle*and*Graal*ecoop14.it.uu.se/programme/ecoop-ruby-truffle.pdf · for Profiling Feedback AST Interpreter Rewritten Nodes AST Interpreter Uninitialized

Copyright  ©  2014  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   16  

Frequently executed call  

Page 17: Implemen@ng*Ruby* Using*Truffle*and*Graal*ecoop14.it.uu.se/programme/ecoop-ruby-truffle.pdf · for Profiling Feedback AST Interpreter Rewritten Nodes AST Interpreter Uninitialized

Copyright  ©  2014  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   17  

Page 18: Implemen@ng*Ruby* Using*Truffle*and*Graal*ecoop14.it.uu.se/programme/ecoop-ruby-truffle.pdf · for Profiling Feedback AST Interpreter Rewritten Nodes AST Interpreter Uninitialized

Copyright  ©  2014  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   18  

I

I I

G

G I

I I

G

G

Deoptimizationto AST Interpreter

D

I D

G

G D

I D

G

G

Node Rewriting to Update Profiling Feedback

Recompilation usingPartial Evaluation

T.  Würthinger,  C.  Wimmer,  A.  Wöß,  L.  Stadler,  G.  Duboscq,  C.  Humer,  G.  Richards,  D.  Simon,  and  M.  Wolczko.  One  VM  to  rule  them  all.  In  Proceedings  of  Onward!,  2013.  

   

Page 19: Implemen@ng*Ruby* Using*Truffle*and*Graal*ecoop14.it.uu.se/programme/ecoop-ruby-truffle.pdf · for Profiling Feedback AST Interpreter Rewritten Nodes AST Interpreter Uninitialized

Copyright  ©  2014  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   19  

Guest Language Application

OS

AOT Optimization: using Graal for static analysis and AOT compilation

Language Parser AST InterpreterGuest Language Implementation

Truffle API Framework for Node Rewriting

Truffle Optimizer Partial Evaluation using Graal

VM Runtime Services Garbage Collector Graal CompilerStack Walking Deoptimization

Hosted on any Java VM

Hosted on Graal VM

(slow, for guest language development and debugging only)

(fast, for integration of guest language code with existing Java applications)

T.  Würthinger,  C.  Wimmer,  A.  Wöß,  L.  Stadler,  G.  Duboscq,  C.  Humer,  G.  Richards,  D.  Simon,  and  M.  Wolczko.  One  VM  to  rule  them  all.  In  Proceedings  of  Onward!,  2013.  

Page 20: Implemen@ng*Ruby* Using*Truffle*and*Graal*ecoop14.it.uu.se/programme/ecoop-ruby-truffle.pdf · for Profiling Feedback AST Interpreter Rewritten Nodes AST Interpreter Uninitialized

Copyright  ©  2014  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   20  

Ahead-of-Time Compilation

Static Analysis

Substrate  VM  

Java  Applica@on  

JDK  

Reachable  methods,    fields,  and  classes  

Ini@al  Heap  

Machine  Code  

OS  

All  Java  classes  from    applica@on,  JDK,    and  Substrate  VM  

Applica@on  running    without  compila@on    or  class  loading  

Page 21: Implemen@ng*Ruby* Using*Truffle*and*Graal*ecoop14.it.uu.se/programme/ecoop-ruby-truffle.pdf · for Profiling Feedback AST Interpreter Rewritten Nodes AST Interpreter Uninitialized

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

One  VM  

21  

•  Good  interpreted  performance  on  a  standard  JVM  •  Extremely  good  dynamically  compiled  performance  on  Graal  

•  High  level  representa@on  of  languages  •  Substrate  VM  for  startup  performance,  low  footprint  and  easy  distribu@on  

•  JavaScript,  Ruby,  R,  J,  C,  Python,  SmallTalk    

Page 22: Implemen@ng*Ruby* Using*Truffle*and*Graal*ecoop14.it.uu.se/programme/ecoop-ruby-truffle.pdf · for Profiling Feedback AST Interpreter Rewritten Nodes AST Interpreter Uninitialized

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

Truffle  and  Graal  in  Prac@ce  

22  

Page 23: Implemen@ng*Ruby* Using*Truffle*and*Graal*ecoop14.it.uu.se/programme/ecoop-ruby-truffle.pdf · for Profiling Feedback AST Interpreter Rewritten Nodes AST Interpreter Uninitialized

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

Simple  Language  (SL)  

23  

•  Minimal  language  for  demonstra@on  and  documenta@on  •  Similar  to  JavaScript  

•  Included  in  the  OpenJDK  Graal  repository    

Page 24: Implemen@ng*Ruby* Using*Truffle*and*Graal*ecoop14.it.uu.se/programme/ecoop-ruby-truffle.pdf · for Profiling Feedback AST Interpreter Rewritten Nodes AST Interpreter Uninitialized

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

Setup  

24  

•  hg  clone  http://hg.openjdk.java.net/graal/graal  •  cd  graal  •  ./mx.sh  -­‐-­‐vm  server  build  

•  ./mx.sh  ideinit  

•  Or  just  Google  ‘graal  openjdk’  

Page 25: Implemen@ng*Ruby* Using*Truffle*and*Graal*ecoop14.it.uu.se/programme/ecoop-ruby-truffle.pdf · for Profiling Feedback AST Interpreter Rewritten Nodes AST Interpreter Uninitialized

Copyright  ©  2014  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   25  

public  class  SLIfNode  extends  SLStatementNode  {      @Child  private  SLExpressionNode  conditionNode;      @Child  private  SLStatementNode  thenPartNode;      @Child  private  SLStatementNode  elsePartNode;        public  SLIfNode(SLExpressionNode  conditionNode,          SLStatementNode  thenPartNode,  SLStatementNode  elsePartNode)  {          this.conditionNode  =  conditionNode;          this.thenPartNode  =  thenPartNode;          this.elsePartNode  =  elsePartNode;      }        public  void  executeVoid(VirtualFrame  frame)  {          if  (conditionNode.executeBoolean(frame))  {              thenPartNode.executeVoid(frame);          }  else  {              elsePartNode.executeVoid(frame);          }      }  }  

Page 26: Implemen@ng*Ruby* Using*Truffle*and*Graal*ecoop14.it.uu.se/programme/ecoop-ruby-truffle.pdf · for Profiling Feedback AST Interpreter Rewritten Nodes AST Interpreter Uninitialized

Copyright  ©  2014  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   26  

public  class  SLBlockNode  extends  SLStatementNode  {      @Children  private  final  SLStatementNode[]  bodyNodes;        public  SLBlockNode(SLStatementNode[]  bodyNodes)  {          this.bodyNodes  =  adoptChildren(bodyNodes);      }        @ExplodeLoop      public  void  executeVoid(VirtualFrame  frame)  {          for  (SLStatementNode  statement  :  bodyNodes)  {              statement.executeVoid(frame);          }      }  }  

Page 27: Implemen@ng*Ruby* Using*Truffle*and*Graal*ecoop14.it.uu.se/programme/ecoop-ruby-truffle.pdf · for Profiling Feedback AST Interpreter Rewritten Nodes AST Interpreter Uninitialized

Copyright  ©  2014  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   27  

public  class  SLFunctionBodyNode  extends  SLExpressionNode  {      @Child  private  SLStatementNode  bodyNode;      ...      public  Object  executeGeneric(VirtualFrame  frame)  {          try  {              bodyNode.executeVoid(frame);          }  catch  (SLReturnException  ex)  {              return  ex.getResult();          }          return  SLNull.SINGLETON;      }  }  

public  class  SLReturnNode  extends  SLStatementNode  {      @Child  private  SLExpressionNode  valueNode;      ...      public  void  executeVoid(VirtualFrame  frame)  {          throw  new  SLReturnExceptn(valueNode.executeGeneric(frame));      }  }  

public  class  SLReturnException          extends  ControlFlowException  {      private  final  Object  result;  }  

Page 28: Implemen@ng*Ruby* Using*Truffle*and*Graal*ecoop14.it.uu.se/programme/ecoop-ruby-truffle.pdf · for Profiling Feedback AST Interpreter Rewritten Nodes AST Interpreter Uninitialized

Copyright  ©  2014  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   28  

public  class  SLAddNode  extends  SLExpressionNode  {      @Child  private  SLExpressionNode  leftNode;      @Child  private  SLExpressionNode  rightNode;        @Override      public  Object  executeGeneric(VirtualFrame  frame)  {          Object  left  =  leftNode.executeGeneric(frame);          Object  right  =  rightNode.executeGeneric(frame);            if  (left  instanceof  Long  &&  right  instanceof  Long)  {              try  {                  return  ExactMath.addExact((Long)  left,  (Long)  right);              }  catch  (ArithmeticException  ex)  {  }          }            if  (left  instanceof  Long)  {              left  =  BigInteger.valueOf((Long)  left);          }          if  (right  instanceof  Long)  {              right  =  BigInteger.valueOf((Long)  right);          }          if  (left  instanceof  BigInteger  &&  right  instanceof  BigInteger)  {              return  ((BigInteger)  left).add((BigInteger)  right);          }            if  (left  instanceof  String  ||  right  instanceof  String)  {              return  left.toString()  +  right.toString();          }            throw  new  UnsupportedSpecializationException(this,  ...);      }  }  

Page 29: Implemen@ng*Ruby* Using*Truffle*and*Graal*ecoop14.it.uu.se/programme/ecoop-ruby-truffle.pdf · for Profiling Feedback AST Interpreter Rewritten Nodes AST Interpreter Uninitialized

Copyright  ©  2014  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   29  

   public  Object  executeGeneric(VirtualFrame  frame)  {          Object  left  =  leftNode.executeGeneric(frame);          Object  right  =  rightNode.executeGeneric(frame);            if  (left  instanceof  Long  &&  right  instanceof  Long)  {              try  {                  return  ExactMath.addExact((Long)  left,  (Long)  right);              }  catch  (ArithmeticException  ex)  {  }          }            if  (left  instanceof  Long)  {              left  =  BigInteger.valueOf((Long)  left);          }          if  (right  instanceof  Long)  {              right  =  BigInteger.valueOf((Long)  right);          }          if  (left  instanceof  BigInteger  &&  right  instanceof  BigInteger)  {              return  ((BigInteger)  left).add((BigInteger)  right);          }            if  (left  instanceof  String  ||  right  instanceof  String)  {              return  left.toString()  +  right.toString();          }  ...  

Page 30: Implemen@ng*Ruby* Using*Truffle*and*Graal*ecoop14.it.uu.se/programme/ecoop-ruby-truffle.pdf · for Profiling Feedback AST Interpreter Rewritten Nodes AST Interpreter Uninitialized

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

   @Specialization(rewriteOn  =  ArithmeticException.class)      protected  long  add(long  left,  long  right)  {          return  ExactMath.addExact(left,  right);      }        @Specialization      protected  BigInteger  add(BigInteger  left,  BigInteger  right)  {          return  left.add(right);      }        @Specialization(guards  =  "isString")      protected  String  add(Object  left,  Object  right)  {          return  left.toString()  +  right.toString();      }        protected  boolean  isString(Object  a,  Object  b)  {          return  a  instanceof  String  ||  b  instanceof  String;      }  

Page 31: Implemen@ng*Ruby* Using*Truffle*and*Graal*ecoop14.it.uu.se/programme/ecoop-ruby-truffle.pdf · for Profiling Feedback AST Interpreter Rewritten Nodes AST Interpreter Uninitialized

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

Ruby  in  Truffle  and  Graal  

31  

Page 32: Implemen@ng*Ruby* Using*Truffle*and*Graal*ecoop14.it.uu.se/programme/ecoop-ruby-truffle.pdf · for Profiling Feedback AST Interpreter Rewritten Nodes AST Interpreter Uninitialized

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

Introduc@on  to  Ruby  

32  

•  Impera@ve,  object  oriented,  dynamically  typed  •  Inspira@ons  from  Smalltalk  and  Perl  

• Widely  used  with  the  Rails  framework  for  web  applica@ons  

•  But  also  used  in  graphics,  bioinforma@cs,  systems,  etc    

Ruby  Logo  (Copyright  (c)  2006,  Yukihiro  Matsumoto.  Licensed  under  the  terms  of  Crea@ve  Commons  Asribu@on-­‐ShareAlike  2.5.)  

Page 33: Implemen@ng*Ruby* Using*Truffle*and*Graal*ecoop14.it.uu.se/programme/ecoop-ruby-truffle.pdf · for Profiling Feedback AST Interpreter Rewritten Nodes AST Interpreter Uninitialized

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

Ruby  Implementa@ons  -­‐  MRI  

33  

•  Implemented  in  C  •  Bytecode  interpreter  •  Very  simple  op@misa@ons  –  inline  caches  in  instruc@ons  

•  Probably  the  slowest  commonly  used  interpreter  there  is  

Page 34: Implemen@ng*Ruby* Using*Truffle*and*Graal*ecoop14.it.uu.se/programme/ecoop-ruby-truffle.pdf · for Profiling Feedback AST Interpreter Rewritten Nodes AST Interpreter Uninitialized

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

Ruby  Implementa@ons  -­‐  Rubinius  

34  

•  Implemented  in  C++  and  Ruby  •  Uses  an  LLVM-­‐based  JIT  

Rubinius  logo  copyright  2011  Roger  Bacardit.  Asribu@on-­‐NoDerivs  3.0  Unported  (CC  BY-­‐ND  3.0)  

Page 35: Implemen@ng*Ruby* Using*Truffle*and*Graal*ecoop14.it.uu.se/programme/ecoop-ruby-truffle.pdf · for Profiling Feedback AST Interpreter Rewritten Nodes AST Interpreter Uninitialized

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

Ruby  Implementa@ons  -­‐  Topaz  

35  

•  Implemented  in  RPython  •  Interpreter  is  sta@cally  compiled  to  na@ve  code  via  C  

•  Ruby  code  is  compiled  using  a  tracing  JIT  compiler  

PyPy  logo  hsp://www.pypy.org/    

Page 36: Implemen@ng*Ruby* Using*Truffle*and*Graal*ecoop14.it.uu.se/programme/ecoop-ruby-truffle.pdf · for Profiling Feedback AST Interpreter Rewritten Nodes AST Interpreter Uninitialized

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

Ruby  Implementa@ons  -­‐  JRuby  

36  

•  Implemented  in  Java  •  Driver  and  primary  user  of  JSR  292  (invokedynamic)  un@l  Nashorn  

•  AST  interpreter  -­‐>  bytecode  compiler  -­‐  >  JIT  by  JVM  

•  Now  looking  at  their  own  IR  before  bytecode  

JRuby  Logo  (Copyright  (c)  2011,  Tony  Price.  Licensed  under  the  terms  of  Crea@ve  Commons  Asribu@on-­‐NoDerivs  3.0  Unported  (CC  BY-­‐ND  3.0)).  

Page 37: Implemen@ng*Ruby* Using*Truffle*and*Graal*ecoop14.it.uu.se/programme/ecoop-ruby-truffle.pdf · for Profiling Feedback AST Interpreter Rewritten Nodes AST Interpreter Uninitialized

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

Ruby  Implementa@ons  –  JRuby+Truffle  

37  

•  Uses  JRuby’s  parser  and  limited  parts  of  their  run@me  •  Currently  not  much  more  than  a  tenant  within  JRuby  

•  AST  interpreter,  wrisen  using  Truffle  

• Works  on  a  normal  JVM  

•  Can  implicitly  use  Graal  VM  

Truffle

Page 38: Implemen@ng*Ruby* Using*Truffle*and*Graal*ecoop14.it.uu.se/programme/ecoop-ruby-truffle.pdf · for Profiling Feedback AST Interpreter Rewritten Nodes AST Interpreter Uninitialized

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

Benchmarks  

38  

•  chunky_png  and  psd.rb  •  Real  code,  unmodified  from  the  original  libraries  

•  Range  of  styles  of  Ruby  code:  –  High  performance  @ght  numerical  loops  with  local  variables  –  Object  oriented  code  such  as  method  calls  and  instance  variables  –  Ruby  dynamic  programming  features  such  as  #send  

Page 39: Implemen@ng*Ruby* Using*Truffle*and*Graal*ecoop14.it.uu.se/programme/ecoop-ruby-truffle.pdf · for Profiling Feedback AST Interpreter Rewritten Nodes AST Interpreter Uninitialized

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

Performance  on  chunky_png  and  psd.rb  

39  

0  

2  

4  

6  

8  

10  

12  

Speedu

p  Re

la0v

e  to  M

RI  (s/s)  

chrisseaton.com/rubytruffle/pushing-­‐pixels  

Page 40: Implemen@ng*Ruby* Using*Truffle*and*Graal*ecoop14.it.uu.se/programme/ecoop-ruby-truffle.pdf · for Profiling Feedback AST Interpreter Rewritten Nodes AST Interpreter Uninitialized

Copyright  ©  2014  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   40  

new_r  =  blend_channel(r(bg),  r(fg),  mix_alpha)    ...    def  method_missing(method,  *args,  &block)      return  ChunkyPNG::Color.send(method,  *args)  ↩          if  ChunkyPNG::Color.respond_to?(method)      normal(*args)  end  

Page 41: Implemen@ng*Ruby* Using*Truffle*and*Graal*ecoop14.it.uu.se/programme/ecoop-ruby-truffle.pdf · for Profiling Feedback AST Interpreter Rewritten Nodes AST Interpreter Uninitialized

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

module  Foo      extend  self        def  foo(a,  b,  c)          hash  =  {a:  a,  b:  b,  c:  c}          array  =  hash.map  {  |k,  v|  v  }          x  =  array[0]          y  =  [a,  b,  c].sort[1]          x  +  y      end    end  

class  Bar        def  method_missing(method,  *args)          if  Foo.respond_to?(method)              Foo.send(method,  *args)          else              0          end      end    end  

Bar.new.foo(14,  8,  6)  =>  22  

Page 42: Implemen@ng*Ruby* Using*Truffle*and*Graal*ecoop14.it.uu.se/programme/ecoop-ruby-truffle.pdf · for Profiling Feedback AST Interpreter Rewritten Nodes AST Interpreter Uninitialized

Copyright  ©  2014  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   42  

0  

5  

10  

15  

20  

25  

30  

35  

40  

45  

50  

MRI   Rubinius   JRuby   Topaz   Truffle  

Speedu

p  Re

la0v

e  to  M

RI  (s/s)  

Page 43: Implemen@ng*Ruby* Using*Truffle*and*Graal*ecoop14.it.uu.se/programme/ecoop-ruby-truffle.pdf · for Profiling Feedback AST Interpreter Rewritten Nodes AST Interpreter Uninitialized

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

Hello  World  on  Substrate  VM  

43  

13

353

688

14

0

200

400

600

800

MRI JRuby Truffle onJVM

Truffle onSVM

[msec] Execution Time

5

35

53

9

0

10

20

30

40

50

60

MRI JRuby Truffle onJVM

Truffle onSVM

[MByte] Memory Footprint

Page 44: Implemen@ng*Ruby* Using*Truffle*and*Graal*ecoop14.it.uu.se/programme/ecoop-ruby-truffle.pdf · for Profiling Feedback AST Interpreter Rewritten Nodes AST Interpreter Uninitialized

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

RubySpec  

44  

0%  

20%  

40%  

60%  

80%  

100%  

MRI   JRuby   Rubinius   Topaz   Truffle  

Langau

ge  Spe

cs  Passing  

rubyspec.org  –  thanks  to  Brian  Shirai  et  al  

Page 45: Implemen@ng*Ruby* Using*Truffle*and*Graal*ecoop14.it.uu.se/programme/ecoop-ruby-truffle.pdf · for Profiling Feedback AST Interpreter Rewritten Nodes AST Interpreter Uninitialized

Copyright  ©  2014  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   45  

Language  Feature   Implemented   Notes  

Fixnum  to  Bignum  promo@on   ✓  

Support  for  floa@ng  point   ✓  

Closures   ✓  

Bindings  and  eval   ✓   Works  from  aliased  methods  

callcc  and  Con@nua@on   ✓   Limited  support,  the  same  as  JRuby  

Fibers   ✓   Limited  support,  the  same  as  JRuby  

Frame  local  variables   ✓  

C  extensions   ✓   Early  work,  but  runs  real  C  extensions  

Ruby  1.9  encoding   ✓  

Garbage  collec@on   ✓  

Concurrency  and  parallelism   ✓   We  currently  use  a  GIL  

Tracing  and  debugging   ✓   Always  enabled  

ObjectSpace   ✓   Always  enabled  

Method  invalida@on   ✓  

Constant  invalida@on   ✓  

Ruby  on  Rails  

Charles  Nuser:  ‘So  You  Want  to  Op@mize  Ruby’  hsp://blog.headius.com/2012/10/so-­‐you-­‐want-­‐to-­‐op@mize-­‐ruby.html  

Page 46: Implemen@ng*Ruby* Using*Truffle*and*Graal*ecoop14.it.uu.se/programme/ecoop-ruby-truffle.pdf · for Profiling Feedback AST Interpreter Rewritten Nodes AST Interpreter Uninitialized

Copyright  ©  2014  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   46  

Language  Feature   Implemented   Notes  Fixnum  to  Bignum  promo@on   ✓  Closures   ✓  Bindings  and  eval   ✓  Garbage  collec@on   ✓  Tracing  and  debugging   ✓   Always  enabled  ObjectSpace   ✓   Always  enabled  Method  invalida@on   ✓  Constant  invalida@on   ✓  

Charles  Nuser:  ‘So  You  Want  to  Op@mize  Ruby’  hsp://blog.headius.com/2012/10/so-­‐you-­‐want-­‐to-­‐op@mize-­‐ruby.html  

Page 47: Implemen@ng*Ruby* Using*Truffle*and*Graal*ecoop14.it.uu.se/programme/ecoop-ruby-truffle.pdf · for Profiling Feedback AST Interpreter Rewritten Nodes AST Interpreter Uninitialized

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

Setup  

47  

•  git  clone  https://github.com/jruby/jruby.git  •  cd  jruby  •  mvn  package  

•  Or  just  Google  ‘jruby  truffle  wiki’  

Page 48: Implemen@ng*Ruby* Using*Truffle*and*Graal*ecoop14.it.uu.se/programme/ecoop-ruby-truffle.pdf · for Profiling Feedback AST Interpreter Rewritten Nodes AST Interpreter Uninitialized

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

Fixnum  to  Bignum  Promo@on  

48  

•  Fixnum  –  fixed  integer:  C  int64_t  or  Java  long  •  Bignum  –  arbitrary  integer:  C  mpz_t    or  Java  BigInteger  

•  Fixnum  overflows  to  Bignum  •  Bignum  underflows  (?)  to  Fixnum  •  En@rely  different  classes  –  programmer  can  tell  the  difference  

•  Unlike  JavaScript  and  Python  

Page 49: Implemen@ng*Ruby* Using*Truffle*and*Graal*ecoop14.it.uu.se/programme/ecoop-ruby-truffle.pdf · for Profiling Feedback AST Interpreter Rewritten Nodes AST Interpreter Uninitialized

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

Closures  

49  

•  Anonymous  func@ons  that  capture  a  lexical  scope  •  Called  ‘blocks’  in  Ruby  –  higher  order  methods  

x  =  14    my_array  =  [1,  2,  3,  4]    my_array.each  do  |n|      puts  x  +  n  end  

Page 50: Implemen@ng*Ruby* Using*Truffle*and*Graal*ecoop14.it.uu.se/programme/ecoop-ruby-truffle.pdf · for Profiling Feedback AST Interpreter Rewritten Nodes AST Interpreter Uninitialized

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

Closures  

50  

•  Anonymous  func@ons  that  capture  a  lexical  scope  •  Called  ‘blocks’  in  Ruby  –  higher  order  methods  

x  =  14;    my_array  =  [1,  2,  3,  4];    my_array.each(function(n)  {      console.log(x  +  n);  });  

Page 51: Implemen@ng*Ruby* Using*Truffle*and*Graal*ecoop14.it.uu.se/programme/ecoop-ruby-truffle.pdf · for Profiling Feedback AST Interpreter Rewritten Nodes AST Interpreter Uninitialized

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

Bindings  and  Eval  

51  

•  Binding:  get  an  environment  as  an  object  •  eval:  as  you’d  expect,  also  lets  you  supply  a  Binding  

   def  foo      a  =  1      b  =  2      binding  end    puts  foo.local_variable_get(:a)  

Page 52: Implemen@ng*Ruby* Using*Truffle*and*Graal*ecoop14.it.uu.se/programme/ecoop-ruby-truffle.pdf · for Profiling Feedback AST Interpreter Rewritten Nodes AST Interpreter Uninitialized

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

Bindings  and  Eval  

52  

•  Binding:  get  an  environment  as  an  object  •  eval:  as  you’d  expect,  also  lets  you  supply  a  Binding  

alias  :secret_binding  :binding    def  foo      a  =  1      b  =  2      secret_binding  end    puts  foo.local_variable_get(:a)  

Page 53: Implemen@ng*Ruby* Using*Truffle*and*Graal*ecoop14.it.uu.se/programme/ecoop-ruby-truffle.pdf · for Profiling Feedback AST Interpreter Rewritten Nodes AST Interpreter Uninitialized

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

Method  and  Constant  Invalida@on  

53  

•  Ruby  lets  you  define  methods  –  ‘monkey  patching’  

class  Fixnum      def  *(b)          self  +  b      end  end    puts  14  *  2  =>  16  (not  28)    

Page 54: Implemen@ng*Ruby* Using*Truffle*and*Graal*ecoop14.it.uu.se/programme/ecoop-ruby-truffle.pdf · for Profiling Feedback AST Interpreter Rewritten Nodes AST Interpreter Uninitialized

Copyright  ©  2014  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   54  

class  Fixnum      def  *(b)          eval  "              class  Object::Fixnum                  def  /(b)                      self  -­‐  b                  end              end          "          self  +  b      end  end    puts  14  *  2  /  4  =>  12  (not  4  or  7)    

Page 55: Implemen@ng*Ruby* Using*Truffle*and*Graal*ecoop14.it.uu.se/programme/ecoop-ruby-truffle.pdf · for Profiling Feedback AST Interpreter Rewritten Nodes AST Interpreter Uninitialized

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

Tracing  and  Debugging  

55  

•  set_trace_func  allows  you  to  install  a  method  to  be  run  on  every  line  

•  Behind  a  –debug  flag  in  JRuby,  not  supported  in  Rubinius  

set_trace_func  proc  {  |line,  binding|      puts  “We’re  at  line  number  #{line}”  }    x  =  1    =>  “We’re  at  line  number  6”  y  =  2    =>  “We’re  at  line  number  7”  

Page 56: Implemen@ng*Ruby* Using*Truffle*and*Graal*ecoop14.it.uu.se/programme/ecoop-ruby-truffle.pdf · for Profiling Feedback AST Interpreter Rewritten Nodes AST Interpreter Uninitialized

Copyright  ©  2014  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   56  

inactive

active

Compile: produces partially evaluated

machine code from specialized

AST.

Deoptimize: transfers control

from the machine code back to the AST interpreter.

Replace: the inactive node with an active node to install the debug

action

Compile: produces new machine code from the modified

AST and the installed debug action.

Debug action installed by user Inactive assumption

check completely elided in compiled code

Page 57: Implemen@ng*Ruby* Using*Truffle*and*Graal*ecoop14.it.uu.se/programme/ecoop-ruby-truffle.pdf · for Profiling Feedback AST Interpreter Rewritten Nodes AST Interpreter Uninitialized

Copyright  ©  2014  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   57  

1

10

100

1000

10000

Enabling set_trace_func

Using set_trace_func

Enabling debugger

Breakpoint

Constant conditional

Simple conditional

Self

Rel

ativ

e Ti

me

(s/s

)

MRIRubinius

JRubyTopaz

JRuby+Truffle

n/a

ʜ��

n/a

n/a

ʜ��

n/a

ʜ��

n/a

n/a

Page 58: Implemen@ng*Ruby* Using*Truffle*and*Graal*ecoop14.it.uu.se/programme/ecoop-ruby-truffle.pdf · for Profiling Feedback AST Interpreter Rewritten Nodes AST Interpreter Uninitialized

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

ObjectSpace  

58  

•  ObjectSpace  allows  you  to  enumerate  all  live  objects  •  Behind  a  flag  –X+O  in  JRuby  •  How  to  find  all  live  objects  in  a  JVM?  

ObjectSpace#each_object  do  |o|      puts  o  end  

Page 59: Implemen@ng*Ruby* Using*Truffle*and*Graal*ecoop14.it.uu.se/programme/ecoop-ruby-truffle.pdf · for Profiling Feedback AST Interpreter Rewritten Nodes AST Interpreter Uninitialized

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

Wrap  Up  

59  

Page 60: Implemen@ng*Ruby* Using*Truffle*and*Graal*ecoop14.it.uu.se/programme/ecoop-ruby-truffle.pdf · for Profiling Feedback AST Interpreter Rewritten Nodes AST Interpreter Uninitialized

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

Get  Involved  

60  

•  Now  is  a  great  @me  to  get  involved  in  Truffle  and  Graal  •  Personal  opinion:  I’d  like  to  see  them  in  JDK  9  in  about  2  years  

•  Areas  open  for  research:  concurrency,  parallelism,  heterogeneous  offload,  language  interoperability  

•  Build  your  language  research  on  top  of  Truffle  and  Graal  

•  Implement  a  language:  Haskell,  Erlang,  Swi�,  Clojure,  PHP  

•  Design  and  implement  an  en@rely  new  language  

Page 61: Implemen@ng*Ruby* Using*Truffle*and*Graal*ecoop14.it.uu.se/programme/ecoop-ruby-truffle.pdf · for Profiling Feedback AST Interpreter Rewritten Nodes AST Interpreter Uninitialized

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

Get  Involved  

61  

•  hsp://openjdk.java.net/projects/graal/  •  graal-­‐[email protected]  

•  Documenta@on  admisedly  a  lisle  bit  limited  so  far  

•  Look  at  SL  and  Ruby  •  [email protected]  

• @ChrisGSeaton  

Page 62: Implemen@ng*Ruby* Using*Truffle*and*Graal*ecoop14.it.uu.se/programme/ecoop-ruby-truffle.pdf · for Profiling Feedback AST Interpreter Rewritten Nodes AST Interpreter Uninitialized

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

Many  people  behind  Truffle  and  Graal  Oracle  Labs  Danilo  Ansaloni  Daniele  Bonesa  Laurent  Daynès  Erik  Eckstein  Michael  Haupt  Mick  Jordan  Peter  Kessler  Christos  Kotselidis  David  Leibs  Tom  Rodriguez  Roland  Schatz  Doug  Simon  Lukas  Stadler  Michael  Van  De  Vanter  Chris@an  Wimmer  Chris@an  Wirth  Mario  Wolczko  Thomas  Würthinger  Laura  Hill  (Manager)    Interns  Shams  Imam  Stephen  Kell  

Gregor  Richards  Rifat  Shariyar    JKU  Linz  Prof.  Hanspeter  Mössenböck  Gilles  Duboscq  Mashias  Grimmer  Chris@an  Häubl  Josef  Haider  Chris@an  Humer  Chris@an  Huber  Manuel  Rigger  Bernhard  Urban  Andreas  Wöß    University  of  Manchester  Chris  Seaton    University  of  Edinburgh  Christophe  Dubach  Juan  José  Fumero  Alfonso  Ranjeet  Singh  Toomas  Remmelg  

LaBRI  Floréal  Morandat    University  of  California,  Irvine  Prof.  Michael  Franz  Codrut  Stancu  Gulfem  Savrun  Yeniceri  Wei  Zhang    Purdue  University  Prof.  Jan  Vitek  Tomas  Kalibera  Petr  Maj  Lei  Zhao    T.  U.  Dortmund  Prof.  Peter  Marwedel  Helena  Koshaus  Ingo  Korb    University  of  California,  Davis  Prof.  Duncan  Temple  Lang  Nicholas  Ulle    

62  

Page 63: Implemen@ng*Ruby* Using*Truffle*and*Graal*ecoop14.it.uu.se/programme/ecoop-ruby-truffle.pdf · for Profiling Feedback AST Interpreter Rewritten Nodes AST Interpreter Uninitialized

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

Safe  Harbor  Statement  The  preceding  is  intended  to  provide  some  insight  into  a  line  of  research  in  Oracle  Labs.  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.  Oracle  reserves  the  right  to  alter  its  development  plans  and  prac@ces  at  any  @me,  and  the  development,  release,  and  @ming  of  any  features  or  func@onality  described  in  connec@on  with  any  Oracle  product  or  service  remains  at  the  sole  discre@on  of  Oracle.    Any  views  expressed  in  this  presenta@on  are  my  own  and  do  not  necessarily  reflect  the  views  of  Oracle.  

63  

Page 64: Implemen@ng*Ruby* Using*Truffle*and*Graal*ecoop14.it.uu.se/programme/ecoop-ruby-truffle.pdf · for Profiling Feedback AST Interpreter Rewritten Nodes AST Interpreter Uninitialized

Copyright  ©  2014  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   64  

Page 65: Implemen@ng*Ruby* Using*Truffle*and*Graal*ecoop14.it.uu.se/programme/ecoop-ruby-truffle.pdf · for Profiling Feedback AST Interpreter Rewritten Nodes AST Interpreter Uninitialized