Compilers and computer architecture: Just-in-time...

182
Compilers and computer architecture: Just-in-time compilation Martin Berger 1 December 2019 1 Email: [email protected], Office hours: Wed 12-13 in Chi-2R312 1/1

Transcript of Compilers and computer architecture: Just-in-time...

Page 1: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Compilers and computer architecture:Just-in-time compilation

Martin Berger 1

December 2019

1Email: [email protected], Office hours: Wed 12-13 inChi-2R312

1 / 1

Page 2: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Recall the function of compilers

2 / 1

Page 3: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Welcome to the cutting edge

Compilers are used to translate from programming languageshumans can understand to machine code executable bycomputers. Compilers come in two forms:

I Conventional ahead-of-time compilers where translation isdone once, long before program execution.

I Just-in-time (JIT) compilers where translation of programfragments happens at the last possible moment and isinterleaved with program execution.

We spend the whole term learning about the former. Today Iwant to give you a brief introduction to the latter.

3 / 1

Page 4: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Welcome to the cutting edge

Compilers are used to translate from programming languageshumans can understand to machine code executable bycomputers. Compilers come in two forms:

I Conventional ahead-of-time compilers where translation isdone once, long before program execution.

I Just-in-time (JIT) compilers where translation of programfragments happens at the last possible moment and isinterleaved with program execution.

We spend the whole term learning about the former. Today Iwant to give you a brief introduction to the latter.

4 / 1

Page 5: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Welcome to the cutting edge

Compilers are used to translate from programming languageshumans can understand to machine code executable bycomputers. Compilers come in two forms:

I Conventional ahead-of-time compilers where translation isdone once, long before program execution.

I Just-in-time (JIT) compilers where translation of programfragments happens at the last possible moment and isinterleaved with program execution.

We spend the whole term learning about the former. Today Iwant to give you a brief introduction to the latter.

5 / 1

Page 6: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Why learn about JIT compilers?

In the past, dynamically typed languages (e.g. Python,Javascript) were much more slow than statically typedlanguages (factor of 10 or worse). Even OO languages (e.g.Java) were a lot slower than procedural languages like C.

In the last couple of years, this gap has been narrowedconsiderably. JIT compilers where the main cause of thisperformance revolution.

JIT compilers are cutting (bleeding) edge technology andconsiderably more complex than normal compilers, which arealready non-trivial. Hence the presentation today will bemassively simplifying.

6 / 1

Page 7: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Why learn about JIT compilers?

In the past, dynamically typed languages (e.g. Python,Javascript) were much more slow than statically typedlanguages (factor of 10 or worse). Even OO languages (e.g.Java) were a lot slower than procedural languages like C.

In the last couple of years, this gap has been narrowedconsiderably. JIT compilers where the main cause of thisperformance revolution.

JIT compilers are cutting (bleeding) edge technology andconsiderably more complex than normal compilers, which arealready non-trivial. Hence the presentation today will bemassively simplifying.

7 / 1

Page 8: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Why learn about JIT compilers?

In the past, dynamically typed languages (e.g. Python,Javascript) were much more slow than statically typedlanguages (factor of 10 or worse). Even OO languages (e.g.Java) were a lot slower than procedural languages like C.

In the last couple of years, this gap has been narrowedconsiderably. JIT compilers where the main cause of thisperformance revolution.

JIT compilers are cutting (bleeding) edge technology andconsiderably more complex than normal compilers, which arealready non-trivial. Hence the presentation today will bemassively simplifying.

8 / 1

Page 9: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Why learn about JIT compilers?

In the past, dynamically typed languages (e.g. Python,Javascript) were much more slow than statically typedlanguages (factor of 10 or worse). Even OO languages (e.g.Java) were a lot slower than procedural languages like C.

In the last couple of years, this gap has been narrowedconsiderably. JIT compilers where the main cause of thisperformance revolution.

JIT compilers are cutting (bleeding) edge technology andconsiderably more complex than normal compilers, which arealready non-trivial. Hence the presentation today will bemassively simplifying.

9 / 1

Page 10: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

If JIT compilers are the answer ... what is the problem?

Let’s look at two examples. Remember the compilation ofobjects and classes?

a

dptr

Instances of A

Pointer to f_A

Pointer to g_A

dptr

dptr

dptr

Method table for A

Code for f_A

Code for g_A

Method bodies

Pointer to f_B

Method table for B Method bodiesa

Pointer to g_A

Code for f_B

dptr

bdptr

b

Instances of B

To deal with inheritance of methods, invoking a method isindirect via the method table. Each invocation has to follow twopointers. Without inheritance, no need for indirection.

10 / 1

Page 11: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

If JIT compilers are the answer ... what is the problem?Let’s look at two examples. Remember the compilation ofobjects and classes?

a

dptr

Instances of A

Pointer to f_A

Pointer to g_A

dptr

dptr

dptr

Method table for A

Code for f_A

Code for g_A

Method bodies

Pointer to f_B

Method table for B Method bodiesa

Pointer to g_A

Code for f_B

dptr

bdptr

b

Instances of B

To deal with inheritance of methods, invoking a method isindirect via the method table. Each invocation has to follow twopointers. Without inheritance, no need for indirection.

11 / 1

Page 12: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

If JIT compilers are the answer ... what is the problem?Of course an individual indirection takes < 1 nano-second on amodern CPU. So why worry?

Answer: loops!

interface I {int f ( int n ); }

class A implements I {public int f ( int n ) { return n; } }

class B implements I {public int f ( int n ) { return 2*n; } }

class Main {public static void main ( String [] args ) {I o = new A ();for ( int i = 0; i < 1000000; i++ ) {for ( int j = 0; i < 1000000; j++ ) {

o.f ( i+j ); } } } }

Performance penalties add up.

12 / 1

Page 13: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

If JIT compilers are the answer ... what is the problem?Of course an individual indirection takes < 1 nano-second on amodern CPU. So why worry? Answer: loops!

interface I {int f ( int n ); }

class A implements I {public int f ( int n ) { return n; } }

class B implements I {public int f ( int n ) { return 2*n; } }

class Main {public static void main ( String [] args ) {I o = new A ();for ( int i = 0; i < 1000000; i++ ) {

for ( int j = 0; i < 1000000; j++ ) {o.f ( i+j ); } } } }

Performance penalties add up.13 / 1

Page 14: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

If JIT compilers are the answer ... what is the problem?

But, I hear you say, it’s obvious, even at compile time, that theobject o is of class A. A good optimising compiler should beable to work this out, and replace the indirect invocation of fwith a cheaper direct jump.

class Main {public static void main ( String [] args ) {I o = new A ();for ( int i = 0; i < 1000000; i++ ) {for ( int j = 0; i < 1000000; j++ ) {

o.f ( i+j ); } } } }

Yes, in this simple example, a good optimising compiler can dothis. But what about the following?

14 / 1

Page 15: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

If JIT compilers are the answer ... what is the problem?

But, I hear you say, it’s obvious, even at compile time, that theobject o is of class A. A good optimising compiler should beable to work this out, and replace the indirect invocation of fwith a cheaper direct jump.

class Main {public static void main ( String [] args ) {I o = new A ();for ( int i = 0; i < 1000000; i++ ) {

for ( int j = 0; i < 1000000; j++ ) {o.f ( i+j ); } } } }

Yes, in this simple example, a good optimising compiler can dothis. But what about the following?

15 / 1

Page 16: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

If JIT compilers are the answer ... what is the problem?

But, I hear you say, it’s obvious, even at compile time, that theobject o is of class A. A good optimising compiler should beable to work this out, and replace the indirect invocation of fwith a cheaper direct jump.

class Main {public static void main ( String [] args ) {I o = new A ();for ( int i = 0; i < 1000000; i++ ) {

for ( int j = 0; i < 1000000; j++ ) {o.f ( i+j ); } } } }

Yes, in this simple example, a good optimising compiler can dothis. But what about the following?

16 / 1

Page 17: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

If JIT compilers are the answer ... what is the problem?

public static void main ( String [] args ) {I o = null;if ( args [ 0 ] == "hello" )new A ();

elsenew B ();

for ( int i = 0; i < 1000000; i++ ) {for ( int j = 0; i < 1000000; j++ ) {

o.f ( i+j ); } } } }

Now the type of o is determined only at run-time. What is theproblem? Not enough information at compile-time to carryout optimisation! At run-time we do have this information, butthat’s too late (for normal compilers).

(Aside, can you see a hack to deal with this problem in an AOTcompiler?)

17 / 1

Page 18: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

If JIT compilers are the answer ... what is the problem?

public static void main ( String [] args ) {I o = null;if ( args [ 0 ] == "hello" )new A ();

elsenew B ();

for ( int i = 0; i < 1000000; i++ ) {for ( int j = 0; i < 1000000; j++ ) {

o.f ( i+j ); } } } }

Now the type of o is determined only at run-time.

What is theproblem? Not enough information at compile-time to carryout optimisation! At run-time we do have this information, butthat’s too late (for normal compilers).

(Aside, can you see a hack to deal with this problem in an AOTcompiler?)

18 / 1

Page 19: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

If JIT compilers are the answer ... what is the problem?

public static void main ( String [] args ) {I o = null;if ( args [ 0 ] == "hello" )new A ();

elsenew B ();

for ( int i = 0; i < 1000000; i++ ) {for ( int j = 0; i < 1000000; j++ ) {

o.f ( i+j ); } } } }

Now the type of o is determined only at run-time. What is theproblem?

Not enough information at compile-time to carryout optimisation! At run-time we do have this information, butthat’s too late (for normal compilers).

(Aside, can you see a hack to deal with this problem in an AOTcompiler?)

19 / 1

Page 20: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

If JIT compilers are the answer ... what is the problem?

public static void main ( String [] args ) {I o = null;if ( args [ 0 ] == "hello" )new A ();

elsenew B ();

for ( int i = 0; i < 1000000; i++ ) {for ( int j = 0; i < 1000000; j++ ) {

o.f ( i+j ); } } } }

Now the type of o is determined only at run-time. What is theproblem? Not enough information at compile-time to carryout optimisation!

At run-time we do have this information, butthat’s too late (for normal compilers).

(Aside, can you see a hack to deal with this problem in an AOTcompiler?)

20 / 1

Page 21: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

If JIT compilers are the answer ... what is the problem?

public static void main ( String [] args ) {I o = null;if ( args [ 0 ] == "hello" )new A ();

elsenew B ();

for ( int i = 0; i < 1000000; i++ ) {for ( int j = 0; i < 1000000; j++ ) {

o.f ( i+j ); } } } }

Now the type of o is determined only at run-time. What is theproblem? Not enough information at compile-time to carryout optimisation! At run-time we do have this information, butthat’s too late (for normal compilers).

(Aside, can you see a hack to deal with this problem in an AOTcompiler?)

21 / 1

Page 22: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

If JIT compilers are the answer ... what is the problem?

public static void main ( String [] args ) {I o = null;if ( args [ 0 ] == "hello" )new A ();

elsenew B ();

for ( int i = 0; i < 1000000; i++ ) {for ( int j = 0; i < 1000000; j++ ) {

o.f ( i+j ); } } } }

Now the type of o is determined only at run-time. What is theproblem? Not enough information at compile-time to carryout optimisation! At run-time we do have this information, butthat’s too late (for normal compilers).

(Aside, can you see a hack to deal with this problem in an AOTcompiler?)

22 / 1

Page 23: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

If JIT compilers are the answer ... what is the problem?

Dynamically typed languages have a worse problem.

Simplifying a little, variables in dynamically typed languagesstore not just the usual value, e.g. 3, but also the type of thevalue, e.g. Int, and sometimes even more. Whenever youcarry an innocent operation like

x = x + y

under the hood something like the following happens.let tx = typeof ( x )let ty = typeof ( y )if ( tx == Int && ty == Int )

let vx = value ( x )let vy = value ( y )let res = integer_addition ( vx, vy )x_result_part = resx_type_part = Int

else... // even more complicated.

23 / 1

Page 24: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

If JIT compilers are the answer ... what is the problem?Dynamically typed languages have a worse problem.

Simplifying a little, variables in dynamically typed languagesstore not just the usual value, e.g. 3, but also the type of thevalue, e.g. Int, and sometimes even more. Whenever youcarry an innocent operation like

x = x + y

under the hood something like the following happens.let tx = typeof ( x )let ty = typeof ( y )if ( tx == Int && ty == Int )

let vx = value ( x )let vy = value ( y )let res = integer_addition ( vx, vy )x_result_part = resx_type_part = Int

else... // even more complicated.

24 / 1

Page 25: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

If JIT compilers are the answer ... what is the problem?Dynamically typed languages have a worse problem.

Simplifying a little, variables in dynamically typed languagesstore not just the usual value, e.g. 3, but also the type of thevalue, e.g. Int, and sometimes even more.

Whenever youcarry an innocent operation like

x = x + y

under the hood something like the following happens.let tx = typeof ( x )let ty = typeof ( y )if ( tx == Int && ty == Int )

let vx = value ( x )let vy = value ( y )let res = integer_addition ( vx, vy )x_result_part = resx_type_part = Int

else... // even more complicated.

25 / 1

Page 26: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

If JIT compilers are the answer ... what is the problem?Dynamically typed languages have a worse problem.

Simplifying a little, variables in dynamically typed languagesstore not just the usual value, e.g. 3, but also the type of thevalue, e.g. Int, and sometimes even more. Whenever youcarry an innocent operation like

x = x + y

under the hood something like the following happens.let tx = typeof ( x )let ty = typeof ( y )if ( tx == Int && ty == Int )

let vx = value ( x )let vy = value ( y )let res = integer_addition ( vx, vy )x_result_part = resx_type_part = Int

else... // even more complicated.

26 / 1

Page 27: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

If JIT compilers are the answer ... what is the problem?

Imagine this in a nested loop!

for ( int i = 0; i < 1000000; i++ ) {for ( int j = 0; i < 1000000; j++ ) {

let tx = typeof ( x )let ty = typeof ( y )if ( tx == Int && ty == Int )

let vx = value ( x )let vy = value ( y )let res = integer_addition ( vx, vy )x_result_part = resx_type_part = Int

...

This is painful. This is why dynamically typed languages areslow(er).

27 / 1

Page 28: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

If JIT compilers are the answer ... what is the problem?

Imagine this in a nested loop!

for ( int i = 0; i < 1000000; i++ ) {for ( int j = 0; i < 1000000; j++ ) {

let tx = typeof ( x )let ty = typeof ( y )if ( tx == Int && ty == Int )

let vx = value ( x )let vy = value ( y )let res = integer_addition ( vx, vy )x_result_part = resx_type_part = Int

...

This is painful. This is why dynamically typed languages areslow(er).

28 / 1

Page 29: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

If JIT compilers are the answer ... what is the problem?

But ...

in practise, variables usually do not change their typesin inner loops.

Why?

Because typically innermost loops work on big and uniformdata structures (usually big arrays).

So the compiler should move the type-checks outside the loops.

29 / 1

Page 30: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

If JIT compilers are the answer ... what is the problem?

But ... in practise, variables usually do not change their typesin inner loops.

Why?

Because typically innermost loops work on big and uniformdata structures (usually big arrays).

So the compiler should move the type-checks outside the loops.

30 / 1

Page 31: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

If JIT compilers are the answer ... what is the problem?

But ... in practise, variables usually do not change their typesin inner loops.

Why?

Because typically innermost loops work on big and uniformdata structures (usually big arrays).

So the compiler should move the type-checks outside the loops.

31 / 1

Page 32: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

If JIT compilers are the answer ... what is the problem?

But ... in practise, variables usually do not change their typesin inner loops.

Why?

Because typically innermost loops work on big and uniformdata structures (usually big arrays).

So the compiler should move the type-checks outside the loops.

32 / 1

Page 33: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

If JIT compilers are the answer ... what is the problem?

But ... in practise, variables usually do not change their typesin inner loops.

Why?

Because typically innermost loops work on big and uniformdata structures (usually big arrays).

So the compiler should move the type-checks outside the loops.

33 / 1

Page 34: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

If JIT compilers are the answer ... what is the problem?Recall that in dynamically typed languages

for ( int i = 0; i < 1000000; i++ ) {for ( int j = 0; i < 1000000; j++ ) {

a [i, j] = a[i,j] + 1 } }

Is really

for ( int i = 0; i < 1000000; i++ ) {for ( int j = 0; i < 1000000; j++ ) {

let ta = typeof ( a[i, j] ) // always samelet t1 = typeof ( 1 ) // always sameif ( ta == Int && t1 == Int ) {

let va = value ( a[i, j] )let v1 = value ( 1 ) // simplifyinglet res = integer_addition ( va, v1 )a[ i, j ]_result_part = resa[ i, j ] _type_part = Int }

else { ... } } }

34 / 1

Page 35: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

If JIT compilers are the answer ... what is the problem?Recall that in dynamically typed languages

for ( int i = 0; i < 1000000; i++ ) {for ( int j = 0; i < 1000000; j++ ) {

a [i, j] = a[i,j] + 1 } }

Is really

for ( int i = 0; i < 1000000; i++ ) {for ( int j = 0; i < 1000000; j++ ) {

let ta = typeof ( a[i, j] ) // always samelet t1 = typeof ( 1 ) // always sameif ( ta == Int && t1 == Int ) {

let va = value ( a[i, j] )let v1 = value ( 1 ) // simplifyinglet res = integer_addition ( va, v1 )a[ i, j ]_result_part = resa[ i, j ] _type_part = Int }

else { ... } } }

35 / 1

Page 36: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

If JIT compilers are the answer ... what is the problem?

So program from last slide can be

let ta = typeof ( a )let t1 = typeof ( 1 )if ( ta == Array [...] of Int && t1 == Int ) {for ( int i = 0; i < 1000000; i++ ) {

for ( int j = 0; i < 1000000; j++ ) {let va = value ( a[i, j] )let v1 = value ( 1 ) // simplifyinglet res = integer_addition ( va, v1 )a[ i, j ]_result_part = res } } }

else { ... }

Alas, at compile-time, the compiler does not have enoughinformation to make this optimisation safely.

36 / 1

Page 37: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

If JIT compilers are the answer ... what is the problem?

So program from last slide can be

let ta = typeof ( a )let t1 = typeof ( 1 )if ( ta == Array [...] of Int && t1 == Int ) {for ( int i = 0; i < 1000000; i++ ) {

for ( int j = 0; i < 1000000; j++ ) {let va = value ( a[i, j] )let v1 = value ( 1 ) // simplifyinglet res = integer_addition ( va, v1 )a[ i, j ]_result_part = res } } }

else { ... }

Alas, at compile-time, the compiler does not have enoughinformation to make this optimisation safely.

37 / 1

Page 38: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

If JIT compilers are the answer ... what is the problem?

Let’s summarise the situation.I Certain powerful optimisations cannot be done at

compile-time, because the compiler has not got enoughinformation to know they are safe.

I At run-time we have enough information to carry out theseoptimisations.

Hmmm, what could we do ...

38 / 1

Page 39: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

If JIT compilers are the answer ... what is the problem?

Let’s summarise the situation.I Certain powerful optimisations cannot be done at

compile-time, because the compiler has not got enoughinformation to know they are safe.

I At run-time we have enough information to carry out theseoptimisations.

Hmmm, what could we do ...

39 / 1

Page 40: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

If JIT compilers are the answer ... what is the problem?

Let’s summarise the situation.I Certain powerful optimisations cannot be done at

compile-time, because the compiler has not got enoughinformation to know they are safe.

I At run-time we have enough information to carry out theseoptimisations.

Hmmm, what could we do ...

40 / 1

Page 41: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

How about we compile and optimise only at run-time?

But there is no run-time if we don’t have a compilation process,right?

Enter interpreters!

41 / 1

Page 42: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

How about we compile and optimise only at run-time?

But there is no run-time if we don’t have a compilation process,right?

Enter interpreters!

42 / 1

Page 43: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

How about we compile and optimise only at run-time?

But there is no run-time if we don’t have a compilation process,right?

Enter interpreters!

43 / 1

Page 44: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

How about we compile and optimise only at run-time?

But there is no run-time if we don’t have a compilation process,right?

Enter interpreters!

44 / 1

Page 45: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Interpreters

Recall from the beginning of the course, that interpreters are asecond way to run programs.

CompilerSource program Executable

Data

Output

Source program Interpreter

Data

Output

At runtime.

I Compilers generate a programthat has an effect on the world.

I Interpreters effect the worlddirectly.

45 / 1

Page 46: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Interpreters

Recall from the beginning of the course, that interpreters are asecond way to run programs.

CompilerSource program Executable

Data

Output

Source program Interpreter

Data

Output

At runtime.

I Compilers generate a programthat has an effect on the world.

I Interpreters effect the worlddirectly.

46 / 1

Page 47: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Interpreters

Recall from the beginning of the course, that interpreters are asecond way to run programs.

CompilerSource program Executable

Data

Output

Source program Interpreter

Data

Output

At runtime.

I Compilers generate a programthat has an effect on the world.

I Interpreters effect the worlddirectly.

47 / 1

Page 48: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Interpreters

Recall from the beginning of the course, that interpreters are asecond way to run programs.

CompilerSource program Executable

Data

Output

Source program Interpreter

Data

Output

At runtime.

I Compilers generate a programthat has an effect on the world.

I Interpreters effect the worlddirectly.

48 / 1

Page 49: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Interpreters

Recall from the beginning of the course, that interpreters are asecond way to run programs.

CompilerSource program Executable

Data

Output

Source program Interpreter

Data

Output

At runtime.

I The advantage of compilers isthat generated code is faster,because a lot of work has tobe done only once (e.g.lexing, parsing, type-checking,optimisation). And the resultsof this work are shared inevery execution. Theinterpreter has to redo thiswork every time.

I The advantage of interpretersis that they are much simplerthan compilers.

49 / 1

Page 50: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

JIT compiler, key idea

Interpret the program, and compile (parts of) the program atrun-time. This suggests the following questions.

I When shall we compile, and which parts of the program?I How do interpreter and compiled program interact?I But most of all: compilation is really slow, especially

optimising compilation. Don’t we make performance worseif we slow an already slow interpreter down with a lengthycompilation process?

In other words, we are facing the following conundrum:I We want to optimise as much as possible, because

optimised programs run faster.I We want to optimises as little as possible, because running

the optimisers is really slow.

Hmmmm ...

50 / 1

Page 51: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

JIT compiler, key ideaInterpret the program, and compile (parts of) the program atrun-time.

This suggests the following questions.

I When shall we compile, and which parts of the program?I How do interpreter and compiled program interact?I But most of all: compilation is really slow, especially

optimising compilation. Don’t we make performance worseif we slow an already slow interpreter down with a lengthycompilation process?

In other words, we are facing the following conundrum:I We want to optimise as much as possible, because

optimised programs run faster.I We want to optimises as little as possible, because running

the optimisers is really slow.

Hmmmm ...

51 / 1

Page 52: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

JIT compiler, key ideaInterpret the program, and compile (parts of) the program atrun-time. This suggests the following questions.

I When shall we compile, and which parts of the program?I How do interpreter and compiled program interact?I But most of all: compilation is really slow, especially

optimising compilation. Don’t we make performance worseif we slow an already slow interpreter down with a lengthycompilation process?

In other words, we are facing the following conundrum:I We want to optimise as much as possible, because

optimised programs run faster.I We want to optimises as little as possible, because running

the optimisers is really slow.

Hmmmm ...

52 / 1

Page 53: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

JIT compiler, key ideaInterpret the program, and compile (parts of) the program atrun-time. This suggests the following questions.

I When shall we compile, and which parts of the program?

I How do interpreter and compiled program interact?I But most of all: compilation is really slow, especially

optimising compilation. Don’t we make performance worseif we slow an already slow interpreter down with a lengthycompilation process?

In other words, we are facing the following conundrum:I We want to optimise as much as possible, because

optimised programs run faster.I We want to optimises as little as possible, because running

the optimisers is really slow.

Hmmmm ...

53 / 1

Page 54: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

JIT compiler, key ideaInterpret the program, and compile (parts of) the program atrun-time. This suggests the following questions.

I When shall we compile, and which parts of the program?I How do interpreter and compiled program interact?

I But most of all: compilation is really slow, especiallyoptimising compilation. Don’t we make performance worseif we slow an already slow interpreter down with a lengthycompilation process?

In other words, we are facing the following conundrum:I We want to optimise as much as possible, because

optimised programs run faster.I We want to optimises as little as possible, because running

the optimisers is really slow.

Hmmmm ...

54 / 1

Page 55: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

JIT compiler, key ideaInterpret the program, and compile (parts of) the program atrun-time. This suggests the following questions.

I When shall we compile, and which parts of the program?I How do interpreter and compiled program interact?I But most of all: compilation is really slow, especially

optimising compilation. Don’t we make performance worseif we slow an already slow interpreter down with a lengthycompilation process?

In other words, we are facing the following conundrum:I We want to optimise as much as possible, because

optimised programs run faster.I We want to optimises as little as possible, because running

the optimisers is really slow.

Hmmmm ...

55 / 1

Page 56: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

JIT compiler, key ideaInterpret the program, and compile (parts of) the program atrun-time. This suggests the following questions.

I When shall we compile, and which parts of the program?I How do interpreter and compiled program interact?I But most of all: compilation is really slow, especially

optimising compilation. Don’t we make performance worseif we slow an already slow interpreter down with a lengthycompilation process?

In other words, we are facing the following conundrum:

I We want to optimise as much as possible, becauseoptimised programs run faster.

I We want to optimises as little as possible, because runningthe optimisers is really slow.

Hmmmm ...

56 / 1

Page 57: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

JIT compiler, key ideaInterpret the program, and compile (parts of) the program atrun-time. This suggests the following questions.

I When shall we compile, and which parts of the program?I How do interpreter and compiled program interact?I But most of all: compilation is really slow, especially

optimising compilation. Don’t we make performance worseif we slow an already slow interpreter down with a lengthycompilation process?

In other words, we are facing the following conundrum:I We want to optimise as much as possible, because

optimised programs run faster.

I We want to optimises as little as possible, because runningthe optimisers is really slow.

Hmmmm ...

57 / 1

Page 58: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

JIT compiler, key ideaInterpret the program, and compile (parts of) the program atrun-time. This suggests the following questions.

I When shall we compile, and which parts of the program?I How do interpreter and compiled program interact?I But most of all: compilation is really slow, especially

optimising compilation. Don’t we make performance worseif we slow an already slow interpreter down with a lengthycompilation process?

In other words, we are facing the following conundrum:I We want to optimise as much as possible, because

optimised programs run faster.I We want to optimises as little as possible, because running

the optimisers is really slow.

Hmmmm ...

58 / 1

Page 59: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

JIT compiler, key ideaInterpret the program, and compile (parts of) the program atrun-time. This suggests the following questions.

I When shall we compile, and which parts of the program?I How do interpreter and compiled program interact?I But most of all: compilation is really slow, especially

optimising compilation. Don’t we make performance worseif we slow an already slow interpreter down with a lengthycompilation process?

In other words, we are facing the following conundrum:I We want to optimise as much as possible, because

optimised programs run faster.I We want to optimises as little as possible, because running

the optimisers is really slow.

Hmmmm ...59 / 1

Page 60: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Pareto principle and compiler/interpreter ∆ to ourrescue

Compiling

Running

Running

Interpreter Compiler

Time

Paid every time

Paid once

Interpretation is much fasterthan (optimising) compilation.But a compiled program ismuch faster thaninterpretation. And we haveto compile only once.

Combine this with the Pareto principle, and you have a potentweapon at hand.

60 / 1

Page 61: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Pareto principle and compiler/interpreter ∆ to ourrescue

Compiling

Running

Running

Interpreter Compiler

Time

Paid every time

Paid once

Interpretation is much fasterthan (optimising) compilation.But a compiled program ismuch faster thaninterpretation. And we haveto compile only once.

Combine this with the Pareto principle, and you have a potentweapon at hand.

61 / 1

Page 62: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Pareto principle, aka 80-20 rule

Vilfredo Pareto, late 19th, early 20th century Italian economist.Noticed:

I 80% of land in Italy was owned by 20% of the population.I 20% of the pea pods in his garden contained 80% of the

peas.

This principle applies in many other areas of life, includingprogram execution:

The great majority of a program’s execution time is spentrunning in a tiny fragment of the code.

Such code is referred to as hot.

62 / 1

Page 63: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Pareto principle, aka 80-20 rule

Vilfredo Pareto, late 19th, early 20th century Italian economist.Noticed:

I 80% of land in Italy was owned by 20% of the population.I 20% of the pea pods in his garden contained 80% of the

peas.

This principle applies in many other areas of life, includingprogram execution:

The great majority of a program’s execution time is spentrunning in a tiny fragment of the code.

Such code is referred to as hot.

63 / 1

Page 64: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Pareto principle, aka 80-20 rule

Vilfredo Pareto, late 19th, early 20th century Italian economist.Noticed:

I 80% of land in Italy was owned by 20% of the population.I 20% of the pea pods in his garden contained 80% of the

peas.

This principle applies in many other areas of life, includingprogram execution:

The great majority of a program’s execution time is spentrunning in a tiny fragment of the code.

Such code is referred to as hot.

64 / 1

Page 65: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Pareto principle, aka 80-20 rule

Vilfredo Pareto, late 19th, early 20th century Italian economist.Noticed:

I 80% of land in Italy was owned by 20% of the population.I 20% of the pea pods in his garden contained 80% of the

peas.

This principle applies in many other areas of life, includingprogram execution:

The great majority of a program’s execution time is spentrunning in a tiny fragment of the code.

Such code is referred to as hot.

65 / 1

Page 66: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Pareto principle, aka 80-20 rule

Vilfredo Pareto, late 19th, early 20th century Italian economist.Noticed:

I 80% of land in Italy was owned by 20% of the population.I 20% of the pea pods in his garden contained 80% of the

peas.

This principle applies in many other areas of life, includingprogram execution:

The great majority of a program’s execution time is spentrunning in a tiny fragment of the code.

Such code is referred to as hot.

66 / 1

Page 67: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Putting the pieces together

Compiling

Running

Running

Interpreter Compiler

Time

Clearly compiling at run-time code that’s executedinfrequently will slow down execution. Trade-offsare different for hot code.

An innermost loop may be executed billions oftimes. The more often, the more optimisingcompilation pays off.

Pareto’s principle tells us that (typically) aprogram contains some hot code.

With the information available at run-time, we canaggressively optimise such hot code, and get amassive speed-up. The rest is interpreted.Sluggishness of interpretation doesn’t matter,because it’s only a fraction of program executiontime.

67 / 1

Page 68: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Putting the pieces together

Compiling

Running

Running

Interpreter Compiler

Time

Clearly compiling at run-time code that’s executedinfrequently will slow down execution. Trade-offsare different for hot code.

An innermost loop may be executed billions oftimes. The more often, the more optimisingcompilation pays off.

Pareto’s principle tells us that (typically) aprogram contains some hot code.

With the information available at run-time, we canaggressively optimise such hot code, and get amassive speed-up. The rest is interpreted.Sluggishness of interpretation doesn’t matter,because it’s only a fraction of program executiontime.

68 / 1

Page 69: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Putting the pieces together

Compiling

Running

Running

Interpreter Compiler

Time

Clearly compiling at run-time code that’s executedinfrequently will slow down execution. Trade-offsare different for hot code.

An innermost loop may be executed billions oftimes. The more often, the more optimisingcompilation pays off.

Pareto’s principle tells us that (typically) aprogram contains some hot code.

With the information available at run-time, we canaggressively optimise such hot code, and get amassive speed-up. The rest is interpreted.Sluggishness of interpretation doesn’t matter,because it’s only a fraction of program executiontime.

69 / 1

Page 70: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Putting the pieces together

Compiling

Running

Running

Interpreter Compiler

Time

Clearly compiling at run-time code that’s executedinfrequently will slow down execution. Trade-offsare different for hot code.

An innermost loop may be executed billions oftimes. The more often, the more optimisingcompilation pays off.

Pareto’s principle tells us that (typically) aprogram contains some hot code.

With the information available at run-time, we canaggressively optimise such hot code, and get amassive speed-up. The rest is interpreted.Sluggishness of interpretation doesn’t matter,because it’s only a fraction of program executiontime.

70 / 1

Page 71: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Putting the pieces together

Compiling

Running

Running

Interpreter Compiler

Time

Clearly compiling at run-time code that’s executedinfrequently will slow down execution. Trade-offsare different for hot code.

An innermost loop may be executed billions oftimes. The more often, the more optimisingcompilation pays off.

Pareto’s principle tells us that (typically) aprogram contains some hot code.

With the information available at run-time, we canaggressively optimise such hot code, and get amassive speed-up. The rest is interpreted.Sluggishness of interpretation doesn’t matter,because it’s only a fraction of program executiontime.

71 / 1

Page 72: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

There is just one problem ... how do we find hot code?

Remember, at compiler time, the optimiser couldn’t work it out(reliably).

Let’s use counters at run-time!

We instrument the interpreter with counters,that increment every time a method is called,or every time we go round a loop.

Whenever these counters reach a threshold,we assume that the associated code is hot.We compile that hot code, and jump to thecompiled code.

Making this play nice with garbage collection,exceptions, concurrency, debugging isn’t easy...

When the compiled code terminates, we switchback to interpretation.

72 / 1

Page 73: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

There is just one problem ... how do we find hot code?Remember, at compiler time, the optimiser couldn’t work it out(reliably).

Let’s use counters at run-time!

We instrument the interpreter with counters,that increment every time a method is called,or every time we go round a loop.

Whenever these counters reach a threshold,we assume that the associated code is hot.We compile that hot code, and jump to thecompiled code.

Making this play nice with garbage collection,exceptions, concurrency, debugging isn’t easy...

When the compiled code terminates, we switchback to interpretation.

73 / 1

Page 74: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

There is just one problem ... how do we find hot code?Remember, at compiler time, the optimiser couldn’t work it out(reliably).

Let’s use counters at run-time!

We instrument the interpreter with counters,that increment every time a method is called,or every time we go round a loop.

Whenever these counters reach a threshold,we assume that the associated code is hot.We compile that hot code, and jump to thecompiled code.

Making this play nice with garbage collection,exceptions, concurrency, debugging isn’t easy...

When the compiled code terminates, we switchback to interpretation.

74 / 1

Page 75: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

There is just one problem ... how do we find hot code?Remember, at compiler time, the optimiser couldn’t work it out(reliably).

Let’s use counters at run-time!

We instrument the interpreter with counters,that increment every time a method is called,or every time we go round a loop.

Whenever these counters reach a threshold,we assume that the associated code is hot.We compile that hot code, and jump to thecompiled code.

Making this play nice with garbage collection,exceptions, concurrency, debugging isn’t easy...

When the compiled code terminates, we switchback to interpretation.

75 / 1

Page 76: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

There is just one problem ... how do we find hot code?Remember, at compiler time, the optimiser couldn’t work it out(reliably).

Let’s use counters at run-time!

We instrument the interpreter with counters,that increment every time a method is called,or every time we go round a loop.

Whenever these counters reach a threshold,we assume that the associated code is hot.We compile that hot code, and jump to thecompiled code.

Making this play nice with garbage collection,exceptions, concurrency, debugging isn’t easy...

When the compiled code terminates, we switchback to interpretation.

76 / 1

Page 77: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

There is just one problem ... how do we find hot code?Remember, at compiler time, the optimiser couldn’t work it out(reliably).

Let’s use counters at run-time!

We instrument the interpreter with counters,that increment every time a method is called,or every time we go round a loop.

Whenever these counters reach a threshold,we assume that the associated code is hot.We compile that hot code, and jump to thecompiled code.

Making this play nice with garbage collection,exceptions, concurrency, debugging isn’t easy...

When the compiled code terminates, we switchback to interpretation.

77 / 1

Page 78: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

There is just one problem ... how do we find hot code?Remember, at compiler time, the optimiser couldn’t work it out(reliably).

Let’s use counters at run-time!

We instrument the interpreter with counters,that increment every time a method is called,or every time we go round a loop.

Whenever these counters reach a threshold,we assume that the associated code is hot.We compile that hot code, and jump to thecompiled code.

Making this play nice with garbage collection,exceptions, concurrency, debugging isn’t easy...

When the compiled code terminates, we switchback to interpretation.

78 / 1

Page 79: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

There is just one problem ... how do we find hot code?Remember, at compiler time, the optimiser couldn’t work it out(reliably).

Let’s use counters at run-time!

We instrument the interpreter with counters,that increment every time a method is called,or every time we go round a loop.

Whenever these counters reach a threshold,we assume that the associated code is hot.We compile that hot code, and jump to thecompiled code.

Making this play nice with garbage collection,exceptions, concurrency, debugging isn’t easy...

When the compiled code terminates, we switchback to interpretation. 79 / 1

Page 80: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

In a picture

Interpret Incrementcounter

Hot code

Compile hot code and optimise

No

Yes

Execute compiled hot code to termination

Sourcecode

80 / 1

Page 81: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

In a picture

Interpret Incrementcounter

Hot code

Compile hot code and optimise

No

Yes

Execute compiled hot code to termination

Sourcecode

81 / 1

Page 82: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Aside

Have you noticed that Java programs start up quite slowly?

This is because at the beginning, everything is interpreted,hence slow. Then JIT compilation starts, also slow.

Eventually, the hot code is detected and compiled with a greatdeal of optimisation. Then execution gets really fast.

82 / 1

Page 83: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Aside

Have you noticed that Java programs start up quite slowly?

This is because at the beginning, everything is interpreted,hence slow. Then JIT compilation starts, also slow.

Eventually, the hot code is detected and compiled with a greatdeal of optimisation. Then execution gets really fast.

83 / 1

Page 84: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Aside

Have you noticed that Java programs start up quite slowly?

This is because at the beginning, everything is interpreted,hence slow. Then JIT compilation starts, also slow.

Eventually, the hot code is detected and compiled with a greatdeal of optimisation. Then execution gets really fast.

84 / 1

Page 85: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Aside

Have you noticed that Java programs start up quite slowly?

This is because at the beginning, everything is interpreted,hence slow. Then JIT compilation starts, also slow.

Eventually, the hot code is detected and compiled with a greatdeal of optimisation. Then execution gets really fast.

85 / 1

Page 86: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

The devil is in the details

This picture omits many subtleties.

Chief among those is that the handover of control frominterpreter to compiler and back works seamlessly.

Also, we don’t want to recompile code, typically use cache ofalready compiled code.

How actually to do the optimisations, taking informationavailable at run-time into account.

Etc etc.

86 / 1

Page 87: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

The devil is in the details

This picture omits many subtleties.

Chief among those is that the handover of control frominterpreter to compiler and back works seamlessly.

Also, we don’t want to recompile code, typically use cache ofalready compiled code.

How actually to do the optimisations, taking informationavailable at run-time into account.

Etc etc.

87 / 1

Page 88: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

The devil is in the details

This picture omits many subtleties.

Chief among those is that the handover of control frominterpreter to compiler and back works seamlessly.

Also, we don’t want to recompile code, typically use cache ofalready compiled code.

How actually to do the optimisations, taking informationavailable at run-time into account.

Etc etc.

88 / 1

Page 89: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

The devil is in the details

This picture omits many subtleties.

Chief among those is that the handover of control frominterpreter to compiler and back works seamlessly.

Also, we don’t want to recompile code, typically use cache ofalready compiled code.

How actually to do the optimisations, taking informationavailable at run-time into account.

Etc etc.

89 / 1

Page 90: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

The devil is in the details

This picture omits many subtleties.

Chief among those is that the handover of control frominterpreter to compiler and back works seamlessly.

Also, we don’t want to recompile code, typically use cache ofalready compiled code.

How actually to do the optimisations, taking informationavailable at run-time into account.

Etc etc.

90 / 1

Page 91: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

The devil is in the details

This picture omits many subtleties.

Chief among those is that the handover of control frominterpreter to compiler and back works seamlessly.

Also, we don’t want to recompile code, typically use cache ofalready compiled code.

How actually to do the optimisations, taking informationavailable at run-time into account.

Etc etc.

91 / 1

Page 92: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

JIT compilers summary

JIT compilers are the cutting edge of compiler technology. Theywere first conceived (in rudimentary form) in the 1960s, butcame to life in the last 10 years or so.

JIT compilers are very complicated. The JVM, probably thebest known JIT compiler, probably took 1000+ person years tobuild.

So what’s next in compiler technology? Let me introduce you to...

92 / 1

Page 93: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

JIT compilers summary

JIT compilers are the cutting edge of compiler technology. Theywere first conceived (in rudimentary form) in the 1960s, butcame to life in the last 10 years or so.

JIT compilers are very complicated. The JVM, probably thebest known JIT compiler, probably took 1000+ person years tobuild.

So what’s next in compiler technology? Let me introduce you to...

93 / 1

Page 94: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

JIT compilers summary

JIT compilers are the cutting edge of compiler technology. Theywere first conceived (in rudimentary form) in the 1960s, butcame to life in the last 10 years or so.

JIT compilers are very complicated. The JVM, probably thebest known JIT compiler, probably took 1000+ person years tobuild.

So what’s next in compiler technology? Let me introduce you to...

94 / 1

Page 95: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

JIT compilers summary

JIT compilers are the cutting edge of compiler technology. Theywere first conceived (in rudimentary form) in the 1960s, butcame to life in the last 10 years or so.

JIT compilers are very complicated. The JVM, probably thebest known JIT compiler, probably took 1000+ person years tobuild.

So what’s next in compiler technology?

Let me introduce you to...

95 / 1

Page 96: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

JIT compilers summary

JIT compilers are the cutting edge of compiler technology. Theywere first conceived (in rudimentary form) in the 1960s, butcame to life in the last 10 years or so.

JIT compilers are very complicated. The JVM, probably thebest known JIT compiler, probably took 1000+ person years tobuild.

So what’s next in compiler technology? Let me introduce you to...

96 / 1

Page 97: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Tracing JIT compilers

Tracing JIT compilers are a form of JIT compilation whereoptimisation is especially aggressive.

97 / 1

Page 98: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Tracing JIT compilers

Tracing JIT compilers are a form of JIT compilation whereoptimisation is especially aggressive.

98 / 1

Page 99: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Tracing JIT compilers

Hot code can contain code that is not used (much). Imagine thecompilation of:

for ( x = 1 to 1000000 )for ( y = 1 to 1000000 )

trya[ x ][ y ] = a[ x+1 ][ a [ y-1 ][ y+1 ] ]

catch ... // error handling

99 / 1

Page 100: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Tracing JIT compilers

Hot code can contain code that is not used (much).

Imagine thecompilation of:

for ( x = 1 to 1000000 )for ( y = 1 to 1000000 )

trya[ x ][ y ] = a[ x+1 ][ a [ y-1 ][ y+1 ] ]

catch ... // error handling

100 / 1

Page 101: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Tracing JIT compilers

Hot code can contain code that is not used (much). Imagine thecompilation of:

for ( x = 1 to 1000000 )for ( y = 1 to 1000000 )

trya[ x ][ y ] = a[ x+1 ][ a [ y-1 ][ y+1 ] ]

catch ... // error handling

101 / 1

Page 102: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Tracing JIT compilers

Hot code can contain code that is not used (much). Imagine thecompilation of:

for ( x = 1 to 1000000 )for ( y = 1 to 1000000 )

trya[ x ][ y ] = a[ x+1 ][ a [ y-1 ][ y+1 ] ]

catch ... // error handling

Clearly the try-catch block is an innermost loop, sopotentially hot code. But if the programmer does a good job, theexception handling will never be triggered. Yet we have all thisexception handling code (tends to be large) in the hot loop. Thiscauses all manner of problems, e.g. cache locality is destroyed.

102 / 1

Page 103: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Tracing JIT compilers

Hot code can contain code that is not used (much). Imagine thecompilation of:

for ( x = 1 to 1000000 )for ( y = 1 to 1000000 )

trya[ x ][ y ] = a[ x+1 ][ a [ y-1 ][ y+1 ] ]

catch ... // error handling

It is difficult to figure out, even at run-time (!) to find such parts.

103 / 1

Page 104: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Tracing JIT compilers

Hot code can contain code that is not used (much). Imagine thecompilation of:

for ( x = 1 to 1000000 )for ( y = 1 to 1000000 )

trya[ x ][ y ] = a[ x+1 ][ a [ y-1 ][ y+1 ] ]

catch ... // error handling

It is difficult to figure out, even at run-time (!) to find such parts.

Why can’t we use counters?

104 / 1

Page 105: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Tracing JIT compilers

Hot code can contain code that is not used (much). Imagine thecompilation of:

for ( x = 1 to 1000000 )for ( y = 1 to 1000000 )

trya[ x ][ y ] = a[ x+1 ][ a [ y-1 ][ y+1 ] ]

catch ... // error handling

It is difficult to figure out, even at run-time (!) to find such parts.

Why can’t we use counters? Yes but ... counters only give ussome relevant information ... for good optimisation we needmore information. Traces give us this information. What aretraces?

105 / 1

Page 106: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Tracing JIT compilers

Tracing JIT compilers have not one, but several compilers (orinterpreters) inside (simplifying greatly).

After the interpreter has found hot code, the hot code iscompiled and executed once (called tracing execution).

In the tracing execution, the machine code actually executed isrecorded, yielding the trace of the hot code.

Note that if the machine code to be traced is branching, only thebranch taken is in the trace. Traces are linear, no branching.This makes optimisation algorithms much simpler and faster.

Once tracing has finished, e.g. the body of the hot loop hasbeen executed once: then analyse and optimise trace.

Based on the analysis another compiler generates another(highly optimised) executable, which is then run to termination,then control goes back to interpreter.

106 / 1

Page 107: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Tracing JIT compilersTracing JIT compilers have not one, but several compilers (orinterpreters) inside (simplifying greatly).

After the interpreter has found hot code, the hot code iscompiled and executed once (called tracing execution).

In the tracing execution, the machine code actually executed isrecorded, yielding the trace of the hot code.

Note that if the machine code to be traced is branching, only thebranch taken is in the trace. Traces are linear, no branching.This makes optimisation algorithms much simpler and faster.

Once tracing has finished, e.g. the body of the hot loop hasbeen executed once: then analyse and optimise trace.

Based on the analysis another compiler generates another(highly optimised) executable, which is then run to termination,then control goes back to interpreter.

107 / 1

Page 108: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Tracing JIT compilersTracing JIT compilers have not one, but several compilers (orinterpreters) inside (simplifying greatly).

After the interpreter has found hot code, the hot code iscompiled and executed once (called tracing execution).

In the tracing execution, the machine code actually executed isrecorded, yielding the trace of the hot code.

Note that if the machine code to be traced is branching, only thebranch taken is in the trace. Traces are linear, no branching.This makes optimisation algorithms much simpler and faster.

Once tracing has finished, e.g. the body of the hot loop hasbeen executed once: then analyse and optimise trace.

Based on the analysis another compiler generates another(highly optimised) executable, which is then run to termination,then control goes back to interpreter.

108 / 1

Page 109: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Tracing JIT compilersTracing JIT compilers have not one, but several compilers (orinterpreters) inside (simplifying greatly).

After the interpreter has found hot code, the hot code iscompiled and executed once (called tracing execution).

In the tracing execution, the machine code actually executed isrecorded, yielding the trace of the hot code.

Note that if the machine code to be traced is branching, only thebranch taken is in the trace. Traces are linear, no branching.This makes optimisation algorithms much simpler and faster.

Once tracing has finished, e.g. the body of the hot loop hasbeen executed once: then analyse and optimise trace.

Based on the analysis another compiler generates another(highly optimised) executable, which is then run to termination,then control goes back to interpreter.

109 / 1

Page 110: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Tracing JIT compilersTracing JIT compilers have not one, but several compilers (orinterpreters) inside (simplifying greatly).

After the interpreter has found hot code, the hot code iscompiled and executed once (called tracing execution).

In the tracing execution, the machine code actually executed isrecorded, yielding the trace of the hot code.

Note that if the machine code to be traced is branching, only thebranch taken is in the trace. Traces are linear, no branching.This makes optimisation algorithms much simpler and faster.

Once tracing has finished, e.g. the body of the hot loop hasbeen executed once: then analyse and optimise trace.

Based on the analysis another compiler generates another(highly optimised) executable, which is then run to termination,then control goes back to interpreter.

110 / 1

Page 111: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Tracing JIT compilersTracing JIT compilers have not one, but several compilers (orinterpreters) inside (simplifying greatly).

After the interpreter has found hot code, the hot code iscompiled and executed once (called tracing execution).

In the tracing execution, the machine code actually executed isrecorded, yielding the trace of the hot code.

Note that if the machine code to be traced is branching, only thebranch taken is in the trace. Traces are linear, no branching.This makes optimisation algorithms much simpler and faster.

Once tracing has finished, e.g. the body of the hot loop hasbeen executed once: then analyse and optimise trace.

Based on the analysis another compiler generates another(highly optimised) executable, which is then run to termination,then control goes back to interpreter.

111 / 1

Page 112: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Tracing JIT compilersTracing JIT compilers have not one, but several compilers (orinterpreters) inside (simplifying greatly).

After the interpreter has found hot code, the hot code iscompiled and executed once (called tracing execution).

In the tracing execution, the machine code actually executed isrecorded, yielding the trace of the hot code.

Note that if the machine code to be traced is branching, only thebranch taken is in the trace. Traces are linear, no branching.This makes optimisation algorithms much simpler and faster.

Once tracing has finished, e.g. the body of the hot loop hasbeen executed once: then analyse and optimise trace.

Based on the analysis another compiler generates another(highly optimised) executable, which is then run to termination,then control goes back to interpreter. 112 / 1

Page 113: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Tracing JIT compilers

Analysing and optimising the trace:I Find out if variables change type in the loop, if not, move

type-checking out of the loop. (For dynamically typedlanguages.)

I Find out if object change type in the loop, if not, useshort-cut method invocations, no need to go via methodtable.

I Let the interpreter handle the rarely used parts of the hotloop (e.g. error handling).

I ...I Finally, enter the third phase, the ’normal’ execution of the

optimised trace.

113 / 1

Page 114: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

A tracing JIT compiler in a picture

Interpret Incrementcounter

Hot code

Compile

No

Yes

Execute compiled code and

record trace

Analyse trace

Optimise trace

Execute optimised

trace

Sourcecode

114 / 1

Page 115: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Difficulties

As with normal JIT compilers, we have to orchestrate theinterplay of all these compiler phases, e.g.: Handover of controlfrom interpreter to compiler, to tracing, to execution ofoptimised trace, and back. Garbage collection, exceptions,concurrency etc must all also work.

Typical optimisations: type-specialisation, bypassing methodinvocation, function inlining, register allocation, dead codeelimination.

Etc etc.

115 / 1

Page 116: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Difficulties

As with normal JIT compilers, we have to orchestrate theinterplay of all these compiler phases, e.g.: Handover of controlfrom interpreter to compiler, to tracing, to execution ofoptimised trace, and back. Garbage collection, exceptions,concurrency etc must all also work.

Typical optimisations: type-specialisation, bypassing methodinvocation, function inlining, register allocation, dead codeelimination.

Etc etc.

116 / 1

Page 117: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Difficulties

As with normal JIT compilers, we have to orchestrate theinterplay of all these compiler phases, e.g.: Handover of controlfrom interpreter to compiler, to tracing, to execution ofoptimised trace, and back. Garbage collection, exceptions,concurrency etc must all also work.

Typical optimisations: type-specialisation, bypassing methodinvocation, function inlining, register allocation, dead codeelimination.

Etc etc.

117 / 1

Page 118: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Example compilers

The JVM (from Oracle). It is a method based JIT compiler,meaning that methods are the units of compilation. It is nottracing.

The first implementation of a tracing JIT was HPs Dynamo. Itdoes not compile from a high-level language to a low-levellanguage. Instead it optimises machine-code.

HotpathVM was the first tracing JIT for a high-level language(Java).

TraceMonkey, one of Firefox’s JavaScript implementations wasfirst JIT compiler for Javascript. (NB: Current Firefox’sSpiderMonkey is not tracing.)

Hard to say exactly who uses what (e.g. Apple Safari) sincecompanies rarely say what they’re using. They can use morethan one. Trade secrets.

118 / 1

Page 119: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Example compilersThe JVM (from Oracle). It is a method based JIT compiler,meaning that methods are the units of compilation. It is nottracing.

The first implementation of a tracing JIT was HPs Dynamo. Itdoes not compile from a high-level language to a low-levellanguage. Instead it optimises machine-code.

HotpathVM was the first tracing JIT for a high-level language(Java).

TraceMonkey, one of Firefox’s JavaScript implementations wasfirst JIT compiler for Javascript. (NB: Current Firefox’sSpiderMonkey is not tracing.)

Hard to say exactly who uses what (e.g. Apple Safari) sincecompanies rarely say what they’re using. They can use morethan one. Trade secrets.

119 / 1

Page 120: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Example compilersThe JVM (from Oracle). It is a method based JIT compiler,meaning that methods are the units of compilation. It is nottracing.

The first implementation of a tracing JIT was HPs Dynamo. Itdoes not compile from a high-level language to a low-levellanguage. Instead it optimises machine-code.

HotpathVM was the first tracing JIT for a high-level language(Java).

TraceMonkey, one of Firefox’s JavaScript implementations wasfirst JIT compiler for Javascript. (NB: Current Firefox’sSpiderMonkey is not tracing.)

Hard to say exactly who uses what (e.g. Apple Safari) sincecompanies rarely say what they’re using. They can use morethan one. Trade secrets.

120 / 1

Page 121: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Example compilersThe JVM (from Oracle). It is a method based JIT compiler,meaning that methods are the units of compilation. It is nottracing.

The first implementation of a tracing JIT was HPs Dynamo. Itdoes not compile from a high-level language to a low-levellanguage. Instead it optimises machine-code.

HotpathVM was the first tracing JIT for a high-level language(Java).

TraceMonkey, one of Firefox’s JavaScript implementations wasfirst JIT compiler for Javascript. (NB: Current Firefox’sSpiderMonkey is not tracing.)

Hard to say exactly who uses what (e.g. Apple Safari) sincecompanies rarely say what they’re using. They can use morethan one. Trade secrets.

121 / 1

Page 122: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Example compilersThe JVM (from Oracle). It is a method based JIT compiler,meaning that methods are the units of compilation. It is nottracing.

The first implementation of a tracing JIT was HPs Dynamo. Itdoes not compile from a high-level language to a low-levellanguage. Instead it optimises machine-code.

HotpathVM was the first tracing JIT for a high-level language(Java).

TraceMonkey, one of Firefox’s JavaScript implementations wasfirst JIT compiler for Javascript. (NB: Current Firefox’sSpiderMonkey is not tracing.)

Hard to say exactly who uses what (e.g. Apple Safari) sincecompanies rarely say what they’re using. They can use morethan one. Trade secrets.

122 / 1

Page 123: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Example compilersThe JVM (from Oracle). It is a method based JIT compiler,meaning that methods are the units of compilation. It is nottracing.

The first implementation of a tracing JIT was HPs Dynamo. Itdoes not compile from a high-level language to a low-levellanguage. Instead it optimises machine-code.

HotpathVM was the first tracing JIT for a high-level language(Java).

TraceMonkey, one of Firefox’s JavaScript implementations wasfirst JIT compiler for Javascript. (NB: Current Firefox’sSpiderMonkey is not tracing.)

Hard to say exactly who uses what (e.g. Apple Safari) sincecompanies rarely say what they’re using. They can use morethan one. Trade secrets.

123 / 1

Page 124: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Example compilers

Open source: PyPy, a meta-tracing framework for Python.

Meta-tracing, what’s that?

124 / 1

Page 125: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Example compilers

Open source: PyPy, a meta-tracing framework for Python.Meta-tracing, what’s that?

125 / 1

Page 126: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Meta-tracing?

126 / 1

Page 127: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Meta-tracingBackground:

Writing compilers is hard, writing optimisingcompilers is harder, writing JIT compilers is harder still, butwriting tracing JIT compilers is the hardest.

Designers of new programming languages cannot reallyproduce a good code generator for a new language. Typicallylanguage designers write interpreters for new languages. Butthat means the new language is hampered. This impedesprogress in programming languages.

Great idea: how about using a JIT compiler to compile theinterpreter, hoping that JITing will speed up interpreter, hencenew PL.

This idea is ingenious, simple, old and ... wrong!

The problem is that interpreter loops are the kinds of loops thatJITers do not optimise well. Let’s explain this in detail.

127 / 1

Page 128: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Meta-tracingBackground: Writing compilers is hard, writing optimisingcompilers is harder, writing JIT compilers is harder still, butwriting tracing JIT compilers is the hardest.

Designers of new programming languages cannot reallyproduce a good code generator for a new language. Typicallylanguage designers write interpreters for new languages. Butthat means the new language is hampered. This impedesprogress in programming languages.

Great idea: how about using a JIT compiler to compile theinterpreter, hoping that JITing will speed up interpreter, hencenew PL.

This idea is ingenious, simple, old and ... wrong!

The problem is that interpreter loops are the kinds of loops thatJITers do not optimise well. Let’s explain this in detail.

128 / 1

Page 129: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Meta-tracingBackground: Writing compilers is hard, writing optimisingcompilers is harder, writing JIT compilers is harder still, butwriting tracing JIT compilers is the hardest.

Designers of new programming languages cannot reallyproduce a good code generator for a new language. Typicallylanguage designers write interpreters for new languages. Butthat means the new language is hampered. This impedesprogress in programming languages.

Great idea: how about using a JIT compiler to compile theinterpreter, hoping that JITing will speed up interpreter, hencenew PL.

This idea is ingenious, simple, old and ... wrong!

The problem is that interpreter loops are the kinds of loops thatJITers do not optimise well. Let’s explain this in detail.

129 / 1

Page 130: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Meta-tracingBackground: Writing compilers is hard, writing optimisingcompilers is harder, writing JIT compilers is harder still, butwriting tracing JIT compilers is the hardest.

Designers of new programming languages cannot reallyproduce a good code generator for a new language. Typicallylanguage designers write interpreters for new languages. Butthat means the new language is hampered. This impedesprogress in programming languages.

Great idea: how about using a JIT compiler to compile theinterpreter, hoping that JITing will speed up interpreter, hencenew PL.

This idea is ingenious, simple, old and ... wrong!

The problem is that interpreter loops are the kinds of loops thatJITers do not optimise well. Let’s explain this in detail.

130 / 1

Page 131: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Meta-tracingBackground: Writing compilers is hard, writing optimisingcompilers is harder, writing JIT compilers is harder still, butwriting tracing JIT compilers is the hardest.

Designers of new programming languages cannot reallyproduce a good code generator for a new language. Typicallylanguage designers write interpreters for new languages. Butthat means the new language is hampered. This impedesprogress in programming languages.

Great idea: how about using a JIT compiler to compile theinterpreter, hoping that JITing will speed up interpreter, hencenew PL.

This idea is ingenious, simple, old and ...

wrong!

The problem is that interpreter loops are the kinds of loops thatJITers do not optimise well. Let’s explain this in detail.

131 / 1

Page 132: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Meta-tracingBackground: Writing compilers is hard, writing optimisingcompilers is harder, writing JIT compilers is harder still, butwriting tracing JIT compilers is the hardest.

Designers of new programming languages cannot reallyproduce a good code generator for a new language. Typicallylanguage designers write interpreters for new languages. Butthat means the new language is hampered. This impedesprogress in programming languages.

Great idea: how about using a JIT compiler to compile theinterpreter, hoping that JITing will speed up interpreter, hencenew PL.

This idea is ingenious, simple, old and ... wrong!

The problem is that interpreter loops are the kinds of loops thatJITers do not optimise well. Let’s explain this in detail.

132 / 1

Page 133: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Meta-tracingBackground: Writing compilers is hard, writing optimisingcompilers is harder, writing JIT compilers is harder still, butwriting tracing JIT compilers is the hardest.

Designers of new programming languages cannot reallyproduce a good code generator for a new language. Typicallylanguage designers write interpreters for new languages. Butthat means the new language is hampered. This impedesprogress in programming languages.

Great idea: how about using a JIT compiler to compile theinterpreter, hoping that JITing will speed up interpreter, hencenew PL.

This idea is ingenious, simple, old and ... wrong!

The problem is that interpreter loops are the kinds of loops thatJITers do not optimise well. Let’s explain this in detail.

133 / 1

Page 134: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Why JIT compilers can’t optimise interpreter loops

An interpreter is a big loop that gets the next command andacts on it, e.g.

while true do:cmd = getNextCommandif cmd is:

"x := E" then ..."if C then M else N" then ..."while C do M" then ..."repeat M until C" then ..."print(M)" then ......

Now JIT compilers are really good at optimising loops, why dothey fail with interpreter loops?

134 / 1

Page 135: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Why JIT compilers can’t optimise interpreter loops

An interpreter is a big loop that gets the next command andacts on it, e.g.

while true do:cmd = getNextCommandif cmd is:

"x := E" then ..."if C then M else N" then ..."while C do M" then ..."repeat M until C" then ..."print(M)" then ......

Now JIT compilers are really good at optimising loops, why dothey fail with interpreter loops?

135 / 1

Page 136: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Why JIT compilers can’t optimise interpreter loops

An interpreter is a big loop that gets the next command andacts on it, e.g.

while true do:cmd = getNextCommandif cmd is:

"x := E" then ..."if C then M else N" then ..."while C do M" then ..."repeat M until C" then ..."print(M)" then ......

Now JIT compilers are really good at optimising loops, why dothey fail with interpreter loops?

136 / 1

Page 137: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Why JIT compilers can’t optimise interpreter loops

An interpreter is a big loop that gets the next command andacts on it, e.g.

while true do:cmd = getNextCommandif cmd is:

"x := E" then ..."if C then M else N" then ..."while C do M" then ..."repeat M until C" then ..."print(M)" then ......

Now JIT compilers are really good at optimising loops, why dothey fail with interpreter loops?

137 / 1

Page 138: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Key requirements for good JIT optimisation of loops

The essence of JIT compilation are tight inner loops that areexecuted a large number of times. This insight can be split intoseparate parts.

I Because they are executed a large number of times theeffect of the optimisation is magnified. X

I Optimising these inner loops heavily gives substantialperformace benefits. X

I Each iteration (or at least most of them) do the same thing.

Last requirement is violated in interpreter loops.

138 / 1

Page 139: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Key requirements for good JIT optimisation of loops

The essence of JIT compilation are tight inner loops that areexecuted a large number of times. This insight can be split intoseparate parts.

I Because they are executed a large number of times theeffect of the optimisation is magnified. X

I Optimising these inner loops heavily gives substantialperformace benefits. X

I Each iteration (or at least most of them) do the same thing.

Last requirement is violated in interpreter loops.

139 / 1

Page 140: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Key requirements for good JIT optimisation of loops

The essence of JIT compilation are tight inner loops that areexecuted a large number of times. This insight can be split intoseparate parts.

I Because they are executed a large number of times theeffect of the optimisation is magnified. X

I Optimising these inner loops heavily gives substantialperformace benefits. X

I Each iteration (or at least most of them) do the same thing.

Last requirement is violated in interpreter loops.

140 / 1

Page 141: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Key requirements for good JIT optimisation of loops

The essence of JIT compilation are tight inner loops that areexecuted a large number of times. This insight can be split intoseparate parts.

I Because they are executed a large number of times theeffect of the optimisation is magnified. X

I Optimising these inner loops heavily gives substantialperformace benefits. X

I Each iteration (or at least most of them) do the same thing.

Last requirement is violated in interpreter loops.

141 / 1

Page 142: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Key requirements for good JIT optimisation of loops

The essence of JIT compilation are tight inner loops that areexecuted a large number of times. This insight can be split intoseparate parts.

I Because they are executed a large number of times theeffect of the optimisation is magnified. X

I Optimising these inner loops heavily gives substantialperformace benefits. X

I Each iteration (or at least most of them) do the same thing.

Last requirement is violated in interpreter loops.

142 / 1

Page 143: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Why can’t interpreter loops be JITed?

The problem is that the source language to be interpreted hasloops too.

Let’s assume this is the programm we are interpreting.

while i > 0:j = j+ii = i-1

This gives rise to something like the following bytecode

loop:br r17 exitadd r21 r33 r21subabs r33 1 r33jump loop

exit:...

143 / 1

Page 144: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Why can’t interpreter loops be JITed?

The problem is that the source language to be interpreted hasloops too.

Let’s assume this is the programm we are interpreting.

while i > 0:j = j+ii = i-1

This gives rise to something like the following bytecode

loop:br r17 exitadd r21 r33 r21subabs r33 1 r33jump loop

exit:...

144 / 1

Page 145: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Why can’t interpreter loops be JITed?

The problem is that the source language to be interpreted hasloops too.

Let’s assume this is the programm we are interpreting.

while i > 0:j = j+ii = i-1

This gives rise to something like the following bytecode

loop:br r17 exitadd r21 r33 r21subabs r33 1 r33jump loop

exit:...

145 / 1

Page 146: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Why can’t interpreter loops be JITed?Let’s have bytecode and bytecode interpreter side-by-side:

loop:br r17 exitadd r21 r33 r21subabs r33 1 r33jump loop

exit:...

while true:op = mem [ pc ]pc = pc+1case op = br:

r = mem [ pc ]pc = pc+1if mem [ r ] == 0:

pc := mem [ pc ]case op = add:

r1 = mem [ pc ]pc = pc+1

...

Now every round of the interpreter takes a different branch.The tracing JIT can just optimise one branch through the loop.This is the worst case scenario: we pay the price of tracing,optimisation (since loop is executed a lot), only to throw awaythe optimisation and go back to interpretation.

146 / 1

Page 147: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Why can’t interpreter loops be JITed?Let’s have bytecode and bytecode interpreter side-by-side:

loop:br r17 exitadd r21 r33 r21subabs r33 1 r33jump loop

exit:...

while true:op = mem [ pc ]pc = pc+1case op = br:

r = mem [ pc ]pc = pc+1if mem [ r ] == 0:

pc := mem [ pc ]case op = add:

r1 = mem [ pc ]pc = pc+1

...

Now every round of the interpreter takes a different branch.

The tracing JIT can just optimise one branch through the loop.This is the worst case scenario: we pay the price of tracing,optimisation (since loop is executed a lot), only to throw awaythe optimisation and go back to interpretation.

147 / 1

Page 148: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Why can’t interpreter loops be JITed?Let’s have bytecode and bytecode interpreter side-by-side:

loop:br r17 exitadd r21 r33 r21subabs r33 1 r33jump loop

exit:...

while true:op = mem [ pc ]pc = pc+1case op = br:

r = mem [ pc ]pc = pc+1if mem [ r ] == 0:

pc := mem [ pc ]case op = add:

r1 = mem [ pc ]pc = pc+1

...

Now every round of the interpreter takes a different branch.The tracing JIT can just optimise one branch through the loop.

This is the worst case scenario: we pay the price of tracing,optimisation (since loop is executed a lot), only to throw awaythe optimisation and go back to interpretation.

148 / 1

Page 149: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Why can’t interpreter loops be JITed?Let’s have bytecode and bytecode interpreter side-by-side:

loop:br r17 exitadd r21 r33 r21subabs r33 1 r33jump loop

exit:...

while true:op = mem [ pc ]pc = pc+1case op = br:

r = mem [ pc ]pc = pc+1if mem [ r ] == 0:

pc := mem [ pc ]case op = add:

r1 = mem [ pc ]pc = pc+1

...

Now every round of the interpreter takes a different branch.The tracing JIT can just optimise one branch through the loop.This is the worst case scenario: we pay the price of tracing,optimisation (since loop is executed a lot), only to throw awaythe optimisation and go back to interpretation. 149 / 1

Page 150: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Why can’t interpreter loops be JITed?

150 / 1

Page 151: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Why can’t interpreter loops be JITed?

Profiling detects the wrong loop as hot code!

We want profiling to detect the (code corresponding to the)user loop, not the interpreter loop.

Note that the (code corresponding to the) user loop consists ofseveral rounds of the interpreter loop.

This is too difficult to detect for profiling, since user programscan vary greatly.

151 / 1

Page 152: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Why can’t interpreter loops be JITed?

Profiling detects the wrong loop as hot code!

We want profiling to detect the (code corresponding to the)user loop, not the interpreter loop.

Note that the (code corresponding to the) user loop consists ofseveral rounds of the interpreter loop.

This is too difficult to detect for profiling, since user programscan vary greatly.

152 / 1

Page 153: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Why can’t interpreter loops be JITed?

Profiling detects the wrong loop as hot code!

We want profiling to detect the (code corresponding to the)user loop, not the interpreter loop.

Note that the (code corresponding to the) user loop consists ofseveral rounds of the interpreter loop.

This is too difficult to detect for profiling, since user programscan vary greatly.

153 / 1

Page 154: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Why can’t interpreter loops be JITed?

Profiling detects the wrong loop as hot code!

We want profiling to detect the (code corresponding to the)user loop, not the interpreter loop.

Note that the (code corresponding to the) user loop consists ofseveral rounds of the interpreter loop.

This is too difficult to detect for profiling, since user programscan vary greatly.

154 / 1

Page 155: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Why can’t interpreter loops be JITed?

The interpreter writer knows what the user loops are like:

while true do:cmd = getNextCommandif cmd is:

"x := E" then ..."if C then M else M" then ..."while C do M" then ..."repeat M until C" then ..."print(M)" then ......

The idea of meta-tracing is to let the interpreter writer annotatethe interpreter code with ’hooks’ that tell the tracing JITcompiler where user loops start and end. The profiler can thenidentify the hot loops in (the interpretation of) user code.

155 / 1

Page 156: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Why can’t interpreter loops be JITed?

The interpreter writer knows what the user loops are like:

while true do:cmd = getNextCommandif cmd is:

"x := E" then ..."if C then M else M" then ..."while C do M" then ..."repeat M until C" then ..."print(M)" then ......

The idea of meta-tracing is to let the interpreter writer annotatethe interpreter code with ’hooks’ that tell the tracing JITcompiler where user loops start and end. The profiler can thenidentify the hot loops in (the interpretation of) user code.

156 / 1

Page 157: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Why can’t interpreter loops be JITed?while true do:

beginInterpreterLoopcmd = getNextCommandif cmd is:

"x := E" then ..."while C do M" then

beginUserLoop...endUserLoop

"repeat M until C" thenbeginUserLoop...endUserLoop

...endInterpreterLoop

Annotations are used by profiler for finding hot user loops.Then user loops are traced & optimised.

Result: Speedup!It is simple to annotate an interpreter.

157 / 1

Page 158: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Why can’t interpreter loops be JITed?while true do:

beginInterpreterLoopcmd = getNextCommandif cmd is:

"x := E" then ..."while C do M" then

beginUserLoop...endUserLoop

"repeat M until C" thenbeginUserLoop...endUserLoop

...endInterpreterLoop

Annotations are used by profiler for finding hot user loops.Then user loops are traced & optimised. Result: Speedup!

It is simple to annotate an interpreter.

158 / 1

Page 159: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Why can’t interpreter loops be JITed?while true do:

beginInterpreterLoopcmd = getNextCommandif cmd is:

"x := E" then ..."while C do M" then

beginUserLoop...endUserLoop

"repeat M until C" thenbeginUserLoop...endUserLoop

...endInterpreterLoop

Annotations are used by profiler for finding hot user loops.Then user loops are traced & optimised. Result: Speedup!It is simple to annotate an interpreter.

159 / 1

Page 160: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Meta-tracing as game changer in PL developmentThe real advantage of this is that it divides the problem ofdeveloping high-performance JIT compilers for a language intoseveral parts, each of which separately is much moremangable:

1. Develop a (meta-)tracing JIT compiler. Hard, but needs tobe done only once.

2. Develop an interpreter for the given source language.Easy!

3. Add annotations in the interpreter to expose user loops.Easy!

4. Run the interpreter using the tracing JIT from (1). Easy!The tracing JIT from (1) can be reused for an unlimited numberof language interpreters. Once a meta-tracing JIT is available,we can easily develop new languages and havehigh-performance compilers for them (almost) for free.The PyPy meta-tracing framework runs Python substantiallyfaster than e.g. the CPython framework.

160 / 1

Page 161: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Meta-tracing as game changer in PL developmentThe real advantage of this is that it divides the problem ofdeveloping high-performance JIT compilers for a language intoseveral parts, each of which separately is much moremangable:

1. Develop a (meta-)tracing JIT compiler. Hard, but needs tobe done only once.

2. Develop an interpreter for the given source language.Easy!

3. Add annotations in the interpreter to expose user loops.Easy!

4. Run the interpreter using the tracing JIT from (1). Easy!The tracing JIT from (1) can be reused for an unlimited numberof language interpreters. Once a meta-tracing JIT is available,we can easily develop new languages and havehigh-performance compilers for them (almost) for free.The PyPy meta-tracing framework runs Python substantiallyfaster than e.g. the CPython framework.

161 / 1

Page 162: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Meta-tracing as game changer in PL developmentThe real advantage of this is that it divides the problem ofdeveloping high-performance JIT compilers for a language intoseveral parts, each of which separately is much moremangable:

1. Develop a (meta-)tracing JIT compiler. Hard, but needs tobe done only once.

2. Develop an interpreter for the given source language.Easy!

3. Add annotations in the interpreter to expose user loops.Easy!

4. Run the interpreter using the tracing JIT from (1). Easy!The tracing JIT from (1) can be reused for an unlimited numberof language interpreters. Once a meta-tracing JIT is available,we can easily develop new languages and havehigh-performance compilers for them (almost) for free.The PyPy meta-tracing framework runs Python substantiallyfaster than e.g. the CPython framework.

162 / 1

Page 163: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Meta-tracing as game changer in PL developmentThe real advantage of this is that it divides the problem ofdeveloping high-performance JIT compilers for a language intoseveral parts, each of which separately is much moremangable:

1. Develop a (meta-)tracing JIT compiler. Hard, but needs tobe done only once.

2. Develop an interpreter for the given source language.Easy!

3. Add annotations in the interpreter to expose user loops.Easy!

4. Run the interpreter using the tracing JIT from (1). Easy!The tracing JIT from (1) can be reused for an unlimited numberof language interpreters. Once a meta-tracing JIT is available,we can easily develop new languages and havehigh-performance compilers for them (almost) for free.The PyPy meta-tracing framework runs Python substantiallyfaster than e.g. the CPython framework.

163 / 1

Page 164: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Meta-tracing as game changer in PL developmentThe real advantage of this is that it divides the problem ofdeveloping high-performance JIT compilers for a language intoseveral parts, each of which separately is much moremangable:

1. Develop a (meta-)tracing JIT compiler. Hard, but needs tobe done only once.

2. Develop an interpreter for the given source language.Easy!

3. Add annotations in the interpreter to expose user loops.Easy!

4. Run the interpreter using the tracing JIT from (1). Easy!

The tracing JIT from (1) can be reused for an unlimited numberof language interpreters. Once a meta-tracing JIT is available,we can easily develop new languages and havehigh-performance compilers for them (almost) for free.The PyPy meta-tracing framework runs Python substantiallyfaster than e.g. the CPython framework.

164 / 1

Page 165: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Meta-tracing as game changer in PL developmentThe real advantage of this is that it divides the problem ofdeveloping high-performance JIT compilers for a language intoseveral parts, each of which separately is much moremangable:

1. Develop a (meta-)tracing JIT compiler. Hard, but needs tobe done only once.

2. Develop an interpreter for the given source language.Easy!

3. Add annotations in the interpreter to expose user loops.Easy!

4. Run the interpreter using the tracing JIT from (1). Easy!The tracing JIT from (1) can be reused for an unlimited numberof language interpreters.

Once a meta-tracing JIT is available,we can easily develop new languages and havehigh-performance compilers for them (almost) for free.The PyPy meta-tracing framework runs Python substantiallyfaster than e.g. the CPython framework.

165 / 1

Page 166: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Meta-tracing as game changer in PL developmentThe real advantage of this is that it divides the problem ofdeveloping high-performance JIT compilers for a language intoseveral parts, each of which separately is much moremangable:

1. Develop a (meta-)tracing JIT compiler. Hard, but needs tobe done only once.

2. Develop an interpreter for the given source language.Easy!

3. Add annotations in the interpreter to expose user loops.Easy!

4. Run the interpreter using the tracing JIT from (1). Easy!The tracing JIT from (1) can be reused for an unlimited numberof language interpreters. Once a meta-tracing JIT is available,we can easily develop new languages and havehigh-performance compilers for them (almost) for free.

The PyPy meta-tracing framework runs Python substantiallyfaster than e.g. the CPython framework.

166 / 1

Page 167: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Meta-tracing as game changer in PL developmentThe real advantage of this is that it divides the problem ofdeveloping high-performance JIT compilers for a language intoseveral parts, each of which separately is much moremangable:

1. Develop a (meta-)tracing JIT compiler. Hard, but needs tobe done only once.

2. Develop an interpreter for the given source language.Easy!

3. Add annotations in the interpreter to expose user loops.Easy!

4. Run the interpreter using the tracing JIT from (1). Easy!The tracing JIT from (1) can be reused for an unlimited numberof language interpreters. Once a meta-tracing JIT is available,we can easily develop new languages and havehigh-performance compilers for them (almost) for free.The PyPy meta-tracing framework runs Python substantiallyfaster than e.g. the CPython framework.

167 / 1

Page 168: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Brief remarks on performance

JIT compilers are built upon many trade-offs.

Although JIT compilers can give lightning fast execution ontypical programs, their worst-case execution time can bedreadful.

JIT compilers work best for languages that do a lot of stuff atrun-time (e.g. type-checking).

For bare-bones languages like C, there is little to optimise atrun-time, and code generated by a conventional C compilerwith heavy (hence slow) optimisation will almost always beat amodern JIT compiler.

168 / 1

Page 169: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Brief remarks on performance

JIT compilers are built upon many trade-offs.

Although JIT compilers can give lightning fast execution ontypical programs, their worst-case execution time can bedreadful.

JIT compilers work best for languages that do a lot of stuff atrun-time (e.g. type-checking).

For bare-bones languages like C, there is little to optimise atrun-time, and code generated by a conventional C compilerwith heavy (hence slow) optimisation will almost always beat amodern JIT compiler.

169 / 1

Page 170: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Brief remarks on performance

JIT compilers are built upon many trade-offs.

Although JIT compilers can give lightning fast execution ontypical programs, their worst-case execution time can bedreadful.

JIT compilers work best for languages that do a lot of stuff atrun-time (e.g. type-checking).

For bare-bones languages like C, there is little to optimise atrun-time, and code generated by a conventional C compilerwith heavy (hence slow) optimisation will almost always beat amodern JIT compiler.

170 / 1

Page 171: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Brief remarks on performance

JIT compilers are built upon many trade-offs.

Although JIT compilers can give lightning fast execution ontypical programs, their worst-case execution time can bedreadful.

JIT compilers work best for languages that do a lot of stuff atrun-time (e.g. type-checking).

For bare-bones languages like C, there is little to optimise atrun-time, and code generated by a conventional C compilerwith heavy (hence slow) optimisation will almost always beat amodern JIT compiler.

171 / 1

Page 172: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Brief remarks on performance

JIT compilers are built upon many trade-offs.

Although JIT compilers can give lightning fast execution ontypical programs, their worst-case execution time can bedreadful.

JIT compilers work best for languages that do a lot of stuff atrun-time (e.g. type-checking).

For bare-bones languages like C, there is little to optimise atrun-time, and code generated by a conventional C compilerwith heavy (hence slow) optimisation will almost always beat amodern JIT compiler.

172 / 1

Page 173: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Compiler development in industry

Lot’s of research going on into compilers, both conventionaland (tracing) JITs. It’s super high-tech.

Big companies (Google, Microsoft, Oracle, Intel, Arm, Apple)compete heavily on quality (e.g. speed, energy usage) of theircompilers. They have large teams working on this. Find itdifficult to hire, because advanced compiler knowledge is rare.

Much work left to be done.

173 / 1

Page 174: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Compiler development in industry

Lot’s of research going on into compilers, both conventionaland (tracing) JITs. It’s super high-tech.

Big companies (Google, Microsoft, Oracle, Intel, Arm, Apple)compete heavily on quality (e.g. speed, energy usage) of theircompilers. They have large teams working on this. Find itdifficult to hire, because advanced compiler knowledge is rare.

Much work left to be done.

174 / 1

Page 175: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Compiler development in industry

Lot’s of research going on into compilers, both conventionaland (tracing) JITs. It’s super high-tech.

Big companies (Google, Microsoft, Oracle, Intel, Arm, Apple)compete heavily on quality (e.g. speed, energy usage) of theircompilers. They have large teams working on this. Find itdifficult to hire, because advanced compiler knowledge is rare.

Much work left to be done.

175 / 1

Page 176: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Compiler development in industry

Lot’s of research going on into compilers, both conventionaland (tracing) JITs. It’s super high-tech.

Big companies (Google, Microsoft, Oracle, Intel, Arm, Apple)compete heavily on quality (e.g. speed, energy usage) of theircompilers. They have large teams working on this. Find itdifficult to hire, because advanced compiler knowledge is rare.

Much work left to be done.

176 / 1

Page 177: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Interested?

Compilers (and related subjects) great subject for final yearprojects.

JRA (Junior Research Assistant) in the summer 2019.

Feel free to talk to me about this.

177 / 1

Page 178: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Interested?

Compilers (and related subjects) great subject for final yearprojects.

JRA (Junior Research Assistant) in the summer 2019.

Feel free to talk to me about this.

178 / 1

Page 179: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Interested?

Compilers (and related subjects) great subject for final yearprojects.

JRA (Junior Research Assistant) in the summer 2019.

Feel free to talk to me about this.

179 / 1

Page 180: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

Interested?

Compilers (and related subjects) great subject for final yearprojects.

JRA (Junior Research Assistant) in the summer 2019.

Feel free to talk to me about this.

180 / 1

Page 181: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

181 / 1

Page 182: Compilers and computer architecture: Just-in-time compilationusers.sussex.ac.uk/~mfb21/compilers/slides/16.pdfCompilers and computer architecture: Just-in-time compilation Martin Berger

182 / 1