cling - llvm.org · cling? c++* interpreter* *: not really interactive, i.e. prompt used like...

43
cling Axel Naumann (CERN), Philippe Canal (Fermilab), Paul Russo (Fermilab), Vassil Vassilev (CERN) Thursday, November 4, 2010

Upload: others

Post on 17-Jul-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: cling - llvm.org · cling? C++* interpreter* *: not really interactive, i.e. prompt used like python, bash and php, but C++ cling: “C++ LLVM-based Interpreter g” 3 Thursday, November

clingAxel Naumann (CERN), Philippe Canal (Fermilab),

Paul Russo (Fermilab), Vassil Vassilev (CERN)

Thursday, November 4, 2010

Page 2: cling - llvm.org · cling? C++* interpreter* *: not really interactive, i.e. prompt used like python, bash and php, but C++ cling: “C++ LLVM-based Interpreter g” 3 Thursday, November

Creating cling, an interactive interpreter interface for clang

Thursday, November 4, 2010

Page 3: cling - llvm.org · cling? C++* interpreter* *: not really interactive, i.e. prompt used like python, bash and php, but C++ cling: “C++ LLVM-based Interpreter g” 3 Thursday, November

Axel Naumann / CERN, llvm dev workshop 2010

cling?

C++* interpreter*

*: not really

interactive, i.e. prompt

used like python, bash and php, but C++

cling: “C++ LLVM-based Interpreterg”

3Thursday, November 4, 2010

Page 4: cling - llvm.org · cling? C++* interpreter* *: not really interactive, i.e. prompt used like python, bash and php, but C++ cling: “C++ LLVM-based Interpreter g” 3 Thursday, November

Axel Naumann / CERN, llvm dev workshop 2010

context:

CERN’s LHC: petabytes of serialized C++ objects / year

analyzed by >10,000physicists world-wide

performance counts

approx 20M LOC C++

4Thursday, November 4, 2010

Page 5: cling - llvm.org · cling? C++* interpreter* *: not really interactive, i.e. prompt used like python, bash and php, but C++ cling: “C++ LLVM-based Interpreter g” 3 Thursday, November

Axel Naumann / CERN, llvm dev workshop 2010

legacy:

experience through CINT:C++ interpreter, 15 years old

main use in data analysis framework @ http://root.cern.ch

limitations, limitations... (parsing, design)

re-write with clang the obvious solution!

5Thursday, November 4, 2010

Page 6: cling - llvm.org · cling? C++* interpreter* *: not really interactive, i.e. prompt used like python, bash and php, but C++ cling: “C++ LLVM-based Interpreter g” 3 Thursday, November

Thursday, November 4, 2010

Page 7: cling - llvm.org · cling? C++* interpreter* *: not really interactive, i.e. prompt used like python, bash and php, but C++ cling: “C++ LLVM-based Interpreter g” 3 Thursday, November

Axel Naumann / CERN, llvm dev workshop 2010

C++ dialect:

statement at translation unit scope

implicit #includes

automatic loading of dynamic libraries

implicit auto keyword

dynamic scopes

f();

void  f(){Klass  o;  o.f();}

void  f(){    File  f(“f.root”);    hist-­‐>Draw();}

7

i=12;

Thursday, November 4, 2010

Page 8: cling - llvm.org · cling? C++* interpreter* *: not really interactive, i.e. prompt used like python, bash and php, but C++ cling: “C++ LLVM-based Interpreter g” 3 Thursday, November

Axel Naumann / CERN, llvm dev workshop 2010

“interpreter”?

not lli, not a traditional interpreter

instead:

runtime evaluation

pseudo-instantaneous response(c.f. compile + link + load + disk I/O)

ahead of time compilation where possible

8Thursday, November 4, 2010

Page 9: cling - llvm.org · cling? C++* interpreter* *: not really interactive, i.e. prompt used like python, bash and php, but C++ cling: “C++ LLVM-based Interpreter g” 3 Thursday, November

Axel Naumann / CERN, llvm dev workshop 2010

big picture:

9

clang

AST/Sema ExeEng

“C++”  code  with  cling  extensions

Thursday, November 4, 2010

Page 10: cling - llvm.org · cling? C++* interpreter* *: not really interactive, i.e. prompt used like python, bash and php, but C++ cling: “C++ LLVM-based Interpreter g” 3 Thursday, November

Axel Naumann / CERN, llvm dev workshop 2010

big picture:

9

clang

AST/Sema

cling

ExeEng“C++”  code  with  cling  extensions

Thursday, November 4, 2010

Page 11: cling - llvm.org · cling? C++* interpreter* *: not really interactive, i.e. prompt used like python, bash and php, but C++ cling: “C++ LLVM-based Interpreter g” 3 Thursday, November

Axel Naumann / CERN, llvm dev workshop 2010

big picture:

9

clang

AST/Sema

cling

ExeEng“C++”  code  with  cling  extensions

Thursday, November 4, 2010

Page 12: cling - llvm.org · cling? C++* interpreter* *: not really interactive, i.e. prompt used like python, bash and php, but C++ cling: “C++ LLVM-based Interpreter g” 3 Thursday, November

use cases!

Thursday, November 4, 2010

Page 13: cling - llvm.org · cling? C++* interpreter* *: not really interactive, i.e. prompt used like python, bash and php, but C++ cling: “C++ LLVM-based Interpreter g” 3 Thursday, November

Axel Naumann / CERN, llvm dev workshop 2010

use case: development

explorative development of algorithms:

edit, run, unhappy about output, edit, run,

in CINT, re-interpret 300x faster than full recompile, link, load!

unhappy about output

11

$  .x  myCode.C(47,  ”argument”)

void  f()  {}

//  myCode.C:void  myCode(int,  const  char*){}

Thursday, November 4, 2010

Page 14: cling - llvm.org · cling? C++* interpreter* *: not really interactive, i.e. prompt used like python, bash and php, but C++ cling: “C++ LLVM-based Interpreter g” 3 Thursday, November

Axel Naumann / CERN, llvm dev workshop 2010

use case: dev’t (2)

code looks the same wherever it is (C++!)

easy migration from physicist to framework

simple transition interpreted / compiled:

12

$  .x  myCode.C $  .x  myCode.C+

Thursday, November 4, 2010

Page 15: cling - llvm.org · cling? C++* interpreter* *: not really interactive, i.e. prompt used like python, bash and php, but C++ cling: “C++ LLVM-based Interpreter g” 3 Thursday, November

Axel Naumann / CERN, llvm dev workshop 2010

use case: signal / slot

intuitive function call:

runtime binding

runtime parameter values

easily extensible (a string!)

fCheck-­‐>Connect("Toggled(Bool_t)",      "MyClass",this,"CallMe(12)");

13Thursday, November 4, 2010

Page 16: cling - llvm.org · cling? C++* interpreter* *: not really interactive, i.e. prompt used like python, bash and php, but C++ cling: “C++ LLVM-based Interpreter g” 3 Thursday, November

Axel Naumann / CERN, llvm dev workshop 2010

use case: plugins

function call through string

can depend on runtime

loose, optional coupling of libraries

void  P110_THDFSFile()  {      gPluginMgr-­‐>AddHandler("TFile",  "^hdfs:",  "THDFSFile",            "THDFSFile(const  char*,Option_t*,const  char*,Int_t)");}

14Thursday, November 4, 2010

Page 17: cling - llvm.org · cling? C++* interpreter* *: not really interactive, i.e. prompt used like python, bash and php, but C++ cling: “C++ LLVM-based Interpreter g” 3 Thursday, November

Axel Naumann / CERN, llvm dev workshop 2010

use case: python binding

C++ reflection + interpreter: control over objects and calls

runtime discovery of types + functions:no stubs

both ways: python <-> C++

15Thursday, November 4, 2010

Page 18: cling - llvm.org · cling? C++* interpreter* *: not really interactive, i.e. prompt used like python, bash and php, but C++ cling: “C++ LLVM-based Interpreter g” 3 Thursday, November

Axel Naumann / CERN, llvm dev workshop 2010

use case: python binding

use the same (C++) library

//  Create  a  one  dimensional  function  and  draw  itfun1  =  new  TF1("fun1",  "abs(sin(x)/x)",  0,  10);fun1-­‐>Draw();

from  ROOT  import  TF1#  Create  a  one  dimensional  function  and  draw  itfun1  =  TF1('fun1',  'abs(sin(x)/x)',  0,  10)fun1.Draw()

16Thursday, November 4, 2010

Page 19: cling - llvm.org · cling? C++* interpreter* *: not really interactive, i.e. prompt used like python, bash and php, but C++ cling: “C++ LLVM-based Interpreter g” 3 Thursday, November

Axel Naumann / CERN, llvm dev workshop 2010

and more use cases:

configuration management

reflection for serialization, documentation

beyond high energy physics:

AI for computer game

remote configuration of integrated devices

add your own (e.g. debugger?)

17Thursday, November 4, 2010

Page 20: cling - llvm.org · cling? C++* interpreter* *: not really interactive, i.e. prompt used like python, bash and php, but C++ cling: “C++ LLVM-based Interpreter g” 3 Thursday, November

dissecting cling!

Thursday, November 4, 2010

Page 21: cling - llvm.org · cling? C++* interpreter* *: not really interactive, i.e. prompt used like python, bash and php, but C++ cling: “C++ LLVM-based Interpreter g” 3 Thursday, November

Axel Naumann / CERN, llvm dev workshop 2010

parts:

interpreter: parses, JITs, executes (C++)

meta processor: e.g. unload source file, debug (steer)

user interface: e.g. prompt loop, exception handling

19Thursday, November 4, 2010

Page 22: cling - llvm.org · cling? C++* interpreter* *: not really interactive, i.e. prompt used like python, bash and php, but C++ cling: “C++ LLVM-based Interpreter g” 3 Thursday, November

$  

Axel Naumann / CERN, llvm dev workshop 2010

prompt $

think of

20Thursday, November 4, 2010

Page 23: cling - llvm.org · cling? C++* interpreter* *: not really interactive, i.e. prompt used like python, bash and php, but C++ cling: “C++ LLVM-based Interpreter g” 3 Thursday, November

$      int  i(17)$  

Axel Naumann / CERN, llvm dev workshop 2010

prompt $

think of

20Thursday, November 4, 2010

Page 24: cling - llvm.org · cling? C++* interpreter* *: not really interactive, i.e. prompt used like python, bash and php, but C++ cling: “C++ LLVM-based Interpreter g” 3 Thursday, November

$      int  i(17)$      i=42$  

Axel Naumann / CERN, llvm dev workshop 2010

prompt $

think of

20Thursday, November 4, 2010

Page 25: cling - llvm.org · cling? C++* interpreter* *: not really interactive, i.e. prompt used like python, bash and php, but C++ cling: “C++ LLVM-based Interpreter g” 3 Thursday, November

$      int  i(17)$      i=42$      int  f()  {    ...

Axel Naumann / CERN, llvm dev workshop 2010

prompt $

think of

20Thursday, November 4, 2010

Page 26: cling - llvm.org · cling? C++* interpreter* *: not really interactive, i.e. prompt used like python, bash and php, but C++ cling: “C++ LLVM-based Interpreter g” 3 Thursday, November

$      int  i(17)$      i=42$      int  f()  {    ...

   

           return    ...

Axel Naumann / CERN, llvm dev workshop 2010

prompt $

think of

20Thursday, November 4, 2010

Page 27: cling - llvm.org · cling? C++* interpreter* *: not really interactive, i.e. prompt used like python, bash and php, but C++ cling: “C++ LLVM-based Interpreter g” 3 Thursday, November

$      int  i(17)$      i=42$      int  f()  {    ...

   

           return    ...            ++i;}$

Axel Naumann / CERN, llvm dev workshop 2010

prompt $

think of

20Thursday, November 4, 2010

Page 28: cling - llvm.org · cling? C++* interpreter* *: not really interactive, i.e. prompt used like python, bash and php, but C++ cling: “C++ LLVM-based Interpreter g” 3 Thursday, November

$      int  i(17)$      i=42$      int  f()  {    ...

   

           return    ...            ++i;}$    f();

Axel Naumann / CERN, llvm dev workshop 2010

prompt $

think of

20Thursday, November 4, 2010

Page 29: cling - llvm.org · cling? C++* interpreter* *: not really interactive, i.e. prompt used like python, bash and php, but C++ cling: “C++ LLVM-based Interpreter g” 3 Thursday, November

Axel Naumann / CERN, llvm dev workshop 2010

prompt $

1. transform input: declarations vs. statements

2.add to existing AST (“ever-growing AST”)

3.remap globals

4.run only new initializers

5.call statement stub

21

$  int  i(17)$  i=42$  int  f()  {    ...  return    ...  ++i;}$  f();

using  namespace  __cling;namespace  __cling  {    int  i={17};    int  f()  {  return  ++i;  }}void  cling_prompt_0()  {i=42;}void  cling_prompt_1()  {f();}

Thursday, November 4, 2010

Page 30: cling - llvm.org · cling? C++* interpreter* *: not really interactive, i.e. prompt used like python, bash and php, but C++ cling: “C++ LLVM-based Interpreter g” 3 Thursday, November

Axel Naumann / CERN, llvm dev workshop 2010

library auto-loading:

1. intercept on unresolved symbol

2.look up in our symbol -> library map

3.dlopen

4.rewire global mapping to symbol

22Thursday, November 4, 2010

Page 31: cling - llvm.org · cling? C++* interpreter* *: not really interactive, i.e. prompt used like python, bash and php, but C++ cling: “C++ LLVM-based Interpreter g” 3 Thursday, November

Axel Naumann / CERN, llvm dev workshop 2010

#includes optional:

PCH for everything

multiple PCHs

need to prune PCH overlaps / vetoed types via AST manipulation and dependency analysis

23Thursday, November 4, 2010

Page 32: cling - llvm.org · cling? C++* interpreter* *: not really interactive, i.e. prompt used like python, bash and php, but C++ cling: “C++ LLVM-based Interpreter g” 3 Thursday, November

Axel Naumann / CERN, llvm dev workshop 2010

compiled <-> interpreted

recursive* ExecutionEngine / JIT invocation

* yes, perfect recursion!

symbol resolution into libraries

24

void  f()  {  cling::Interpret(“f();”);  }

$  .x  f.C

Thursday, November 4, 2010

Page 33: cling - llvm.org · cling? C++* interpreter* *: not really interactive, i.e. prompt used like python, bash and php, but C++ cling: “C++ LLVM-based Interpreter g” 3 Thursday, November

Axel Naumann / CERN, llvm dev workshop 2010

dynamic scoping:

file opens a dynamic scope

unknown id: query file’s objects

but need valid AST:

mark unknown identifiers dependent

transform intodelayed evaluation:

25

void  f(){    File  f(“f.root”);    hist-­‐>Draw();}

Thursday, November 4, 2010

Page 34: cling - llvm.org · cling? C++* interpreter* *: not really interactive, i.e. prompt used like python, bash and php, but C++ cling: “C++ LLVM-based Interpreter g” 3 Thursday, November

Axel Naumann / CERN, llvm dev workshop 2010

dynamic scoping:

file opens a dynamic scope

unknown id: query file’s objects

but need valid AST:

mark unknown identifiers dependent

transform intodelayed evaluation:

25

void  f(){    File  f(“f.root”);    hist-­‐>Draw();}

void  f(){    File  f(“f.root”);    cling::eval(“hist-­‐>Draw();”);}

Thursday, November 4, 2010

Page 35: cling - llvm.org · cling? C++* interpreter* *: not really interactive, i.e. prompt used like python, bash and php, but C++ cling: “C++ LLVM-based Interpreter g” 3 Thursday, November

Axel Naumann / CERN, llvm dev workshop 2010

un-/reloading

26

//  Struct.h,  v1.0struct  S  {    int  f()  {  return  17;  }};

$  .L  Struct.h$  S  o$  o.f()17$  .U  Struct.h

//  edit  Struct.h

$  .L  Struct.h$  S  o$  o.f()42

//  Struct.h,  v2.0struct  S  {    int  f()  {  return  42;  }};

Thursday, November 4, 2010

Page 36: cling - llvm.org · cling? C++* interpreter* *: not really interactive, i.e. prompt used like python, bash and php, but C++ cling: “C++ LLVM-based Interpreter g” 3 Thursday, November

Axel Naumann / CERN, llvm dev workshop 2010

un-/reloading

checkpoint ever-growing AST (undo points)

prune top level decls if dependency analysis allows

global mapping magic

27Thursday, November 4, 2010

Page 37: cling - llvm.org · cling? C++* interpreter* *: not really interactive, i.e. prompt used like python, bash and php, but C++ cling: “C++ LLVM-based Interpreter g” 3 Thursday, November

conclusion

Thursday, November 4, 2010

Page 38: cling - llvm.org · cling? C++* interpreter* *: not really interactive, i.e. prompt used like python, bash and php, but C++ cling: “C++ LLVM-based Interpreter g” 3 Thursday, November

Axel Naumann / CERN, llvm dev workshop 2010

cling:

ahead-of-time compiler

extends C++ for ease of use as interpreter

interfaces clang

isn’t finished yet!

29Thursday, November 4, 2010

Page 39: cling - llvm.org · cling? C++* interpreter* *: not really interactive, i.e. prompt used like python, bash and php, but C++ cling: “C++ LLVM-based Interpreter g” 3 Thursday, November

Axel Naumann / CERN, llvm dev workshop 2010

main ingredients:

interactive prompt

library auto-loading

#includes optional

dynamic scoping

calling compiled <-> interpreted

un- / reloading of libraries and source

30Thursday, November 4, 2010

Page 40: cling - llvm.org · cling? C++* interpreter* *: not really interactive, i.e. prompt used like python, bash and php, but C++ cling: “C++ LLVM-based Interpreter g” 3 Thursday, November

Axel Naumann / CERN, llvm dev workshop 2010

wish list:

growing MemoryBuffer with “top level decl not finished” callback

AST dependency analysis:

AST still valid after removal of node N?

shortcut JIT -> shared lib

31Thursday, November 4, 2010

Page 41: cling - llvm.org · cling? C++* interpreter* *: not really interactive, i.e. prompt used like python, bash and php, but C++ cling: “C++ LLVM-based Interpreter g” 3 Thursday, November

Axel Naumann / CERN, llvm dev workshop 2010

santa:

can anybody help us with AST dependency graph?need to do it ourselves?

possible use cases: code dependency / dead code analysis, refactoring

32Thursday, November 4, 2010

Page 42: cling - llvm.org · cling? C++* interpreter* *: not really interactive, i.e. prompt used like python, bash and php, but C++ cling: “C++ LLVM-based Interpreter g” 3 Thursday, November

Axel Naumann / CERN, llvm dev workshop 2010

status:

some things work: simple prompt, library <-> interpreter (even recursion), auto-loading libs

currently attacking dynamic scope, extending prompt, interface to high energy physics data analysis software

for the rest, you have seen the plan!

33Thursday, November 4, 2010

Page 43: cling - llvm.org · cling? C++* interpreter* *: not really interactive, i.e. prompt used like python, bash and php, but C++ cling: “C++ LLVM-based Interpreter g” 3 Thursday, November

Axel Naumann / CERN, llvm dev workshop 2010

where we are:

svn co http://root.cern.ch/svn/root/branches/dev/cling

code’s copyright / license points to clang’s

would like to move into clang repo once cling is a bit more mature / usable - hopefully within this calendar year (santa!)

34Thursday, November 4, 2010