jit compilation and dynamically typed languages › ~kulkarni › teaching › eecs768 › 19-spring...

28
JIT Compilation and Dynamically Typed Languages TJ Barclay University of Kansas April 26, 2019 1 / 28

Upload: others

Post on 06-Jul-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: JIT Compilation and Dynamically Typed Languages › ~kulkarni › teaching › EECS768 › 19-Spring › TJ... · 2019-04-26 · Object unboxing 11/28. Low Level Optimization Low-Level

JIT Compilation and Dynamically Typed Languages

TJ Barclay

University of Kansas

April 26, 2019

1 / 28

Page 2: JIT Compilation and Dynamically Typed Languages › ~kulkarni › teaching › EECS768 › 19-Spring › TJ... · 2019-04-26 · Object unboxing 11/28. Low Level Optimization Low-Level

Outline

1 Dynamic Programming Languages

2 JIT Compilation

3 Compiler Optimizations

4 ExamplesActionScriptJulia

2 / 28

Page 3: JIT Compilation and Dynamically Typed Languages › ~kulkarni › teaching › EECS768 › 19-Spring › TJ... · 2019-04-26 · Object unboxing 11/28. Low Level Optimization Low-Level

Dynamic Programming Languages

3 / 28

Page 4: JIT Compilation and Dynamically Typed Languages › ~kulkarni › teaching › EECS768 › 19-Spring › TJ... · 2019-04-26 · Object unboxing 11/28. Low Level Optimization Low-Level

Dynamic Programming Languages - Introduction

Static

”Stuff” happens at/before compile time

Dynamic

”Stuff” happens at runtime

”Stuff” includes:

Method binding

Typing

Program extension

Modifying objects/classes

4 / 28

Page 5: JIT Compilation and Dynamically Typed Languages › ~kulkarni › teaching › EECS768 › 19-Spring › TJ... · 2019-04-26 · Object unboxing 11/28. Low Level Optimization Low-Level

Dynamic Typing vs Static Typing

Static typing: types are checked before runtime, for example Java checkswhile compiling to bytecodeDynamic typing: types are checked during runtime, Julia/Actionscript

5 / 28

Page 6: JIT Compilation and Dynamically Typed Languages › ~kulkarni › teaching › EECS768 › 19-Spring › TJ... · 2019-04-26 · Object unboxing 11/28. Low Level Optimization Low-Level

Optional Typing

Dynamic typing but the programmer can force the type of a variable to besomething

6 / 28

Page 7: JIT Compilation and Dynamically Typed Languages › ~kulkarni › teaching › EECS768 › 19-Spring › TJ... · 2019-04-26 · Object unboxing 11/28. Low Level Optimization Low-Level

JIT Compilation

7 / 28

Page 8: JIT Compilation and Dynamically Typed Languages › ~kulkarni › teaching › EECS768 › 19-Spring › TJ... · 2019-04-26 · Object unboxing 11/28. Low Level Optimization Low-Level

What is JIT compilation?

Just-In-Time (JIT) compilation is compilation to machine code thathappens at runtime.

8 / 28

Page 9: JIT Compilation and Dynamically Typed Languages › ~kulkarni › teaching › EECS768 › 19-Spring › TJ... · 2019-04-26 · Object unboxing 11/28. Low Level Optimization Low-Level

JIT Tradeoffs

Compilation speed vs. generating performant code

9 / 28

Page 10: JIT Compilation and Dynamically Typed Languages › ~kulkarni › teaching › EECS768 › 19-Spring › TJ... · 2019-04-26 · Object unboxing 11/28. Low Level Optimization Low-Level

Compiler Optimizations

10 / 28

Page 11: JIT Compilation and Dynamically Typed Languages › ~kulkarni › teaching › EECS768 › 19-Spring › TJ... · 2019-04-26 · Object unboxing 11/28. Low Level Optimization Low-Level

High-Level Optimization

High-Level Optimization

Optimization that requires knowledge about the language semantics andruntime environment

Examples:

Type inference

Method inlining

Type speculation

Method specialization

Object unboxing

11 / 28

Page 12: JIT Compilation and Dynamically Typed Languages › ~kulkarni › teaching › EECS768 › 19-Spring › TJ... · 2019-04-26 · Object unboxing 11/28. Low Level Optimization Low-Level

Low Level Optimization

Low-Level Optimization

Optimization that happens in any context, simply by observing thestructure of low-level IR

Examples:

Redundant load/store removal

Common subexpression elimination

Dead code elimination

Register allocation

12 / 28

Page 13: JIT Compilation and Dynamically Typed Languages › ~kulkarni › teaching › EECS768 › 19-Spring › TJ... · 2019-04-26 · Object unboxing 11/28. Low Level Optimization Low-Level

Type Inference

Iterative data flow problem

Start from the most specific type and generalize(contrast with Hindley-Milner)

Example in Julia

13 / 28

Page 14: JIT Compilation and Dynamically Typed Languages › ~kulkarni › teaching › EECS768 › 19-Spring › TJ... · 2019-04-26 · Object unboxing 11/28. Low Level Optimization Low-Level

Examples

14 / 28

Page 15: JIT Compilation and Dynamically Typed Languages › ~kulkarni › teaching › EECS768 › 19-Spring › TJ... · 2019-04-26 · Object unboxing 11/28. Low Level Optimization Low-Level

ActionScript

Dynamic programming language

Optionally typed

Programmer can specify type of variable or it has Any type

Tamarin VM

NanoJITType Enriched Static Single Assignment (TESSA)

15 / 28

Page 16: JIT Compilation and Dynamically Typed Languages › ~kulkarni › teaching › EECS768 › 19-Spring › TJ... · 2019-04-26 · Object unboxing 11/28. Low Level Optimization Low-Level

NanoJIT

Designed for fast compilation

ActionScript Bytecode (ABC)

Few optimizations

Common subexpression eliminationRedundant load/store removal

Untyped variables given the Any Type

requires C++ conversion code to beinlined

16 / 28

Page 17: JIT Compilation and Dynamically Typed Languages › ~kulkarni › teaching › EECS768 › 19-Spring › TJ... · 2019-04-26 · Object unboxing 11/28. Low Level Optimization Low-Level

TESSA

Designed to produce faster code

Performs heavier optimizations

Type inferenceMethod inliningLLVM low-level optimizations

17 / 28

Page 18: JIT Compilation and Dynamically Typed Languages › ~kulkarni › teaching › EECS768 › 19-Spring › TJ... · 2019-04-26 · Object unboxing 11/28. Low Level Optimization Low-Level

Evaluation

Comparing:

Generated code performance

Differing amounts of type informationDifferent backend optimization levels

JIT compilation time

18 / 28

Page 19: JIT Compilation and Dynamically Typed Languages › ~kulkarni › teaching › EECS768 › 19-Spring › TJ... · 2019-04-26 · Object unboxing 11/28. Low Level Optimization Low-Level

Typed Code

19 / 28

Page 20: JIT Compilation and Dynamically Typed Languages › ~kulkarni › teaching › EECS768 › 19-Spring › TJ... · 2019-04-26 · Object unboxing 11/28. Low Level Optimization Low-Level

Untyped Code

20 / 28

Page 21: JIT Compilation and Dynamically Typed Languages › ~kulkarni › teaching › EECS768 › 19-Spring › TJ... · 2019-04-26 · Object unboxing 11/28. Low Level Optimization Low-Level

Partially Typed Code

21 / 28

Page 22: JIT Compilation and Dynamically Typed Languages › ~kulkarni › teaching › EECS768 › 19-Spring › TJ... · 2019-04-26 · Object unboxing 11/28. Low Level Optimization Low-Level

Compilation Time

22 / 28

Page 23: JIT Compilation and Dynamically Typed Languages › ~kulkarni › teaching › EECS768 › 19-Spring › TJ... · 2019-04-26 · Object unboxing 11/28. Low Level Optimization Low-Level

Julia

Dynamic programming language

Optionally typed

Multiple dispatch

Designed for fast development that can be later sped up

Can control memory layout of datatypes

23 / 28

Page 24: JIT Compilation and Dynamically Typed Languages › ~kulkarni › teaching › EECS768 › 19-Spring › TJ... · 2019-04-26 · Object unboxing 11/28. Low Level Optimization Low-Level

Julia’s Optimizations

Method specialization

Type inference

Method inlining (In Julia methods are function implementations thatare ad hoc polymorphic)

Object unboxing

24 / 28

Page 25: JIT Compilation and Dynamically Typed Languages › ~kulkarni › teaching › EECS768 › 19-Spring › TJ... · 2019-04-26 · Object unboxing 11/28. Low Level Optimization Low-Level

Evaluation

25 / 28

Page 26: JIT Compilation and Dynamically Typed Languages › ~kulkarni › teaching › EECS768 › 19-Spring › TJ... · 2019-04-26 · Object unboxing 11/28. Low Level Optimization Low-Level

Conclusions

Conclusions:

High-level optimizations are key to performance gains

Large amounts of low-level optimization often takes too long tojustify the speedup

26 / 28

Page 27: JIT Compilation and Dynamically Typed Languages › ~kulkarni › teaching › EECS768 › 19-Spring › TJ... · 2019-04-26 · Object unboxing 11/28. Low Level Optimization Low-Level

References

Mason Chang, Bernd Mathiske, Edwin Smith, Avik Chaudhuri,Andreas Gal, Michael Bebenita, Christian Wimmer, and MichaelFranz.2011. The impact of optional type information on jit compilation ofdynamically typed languages.SIGPLAN Not. 47, 2 (October 2011), 13-24. DOI:https://doi.org/10.1145/2168696.2047853

Jeff Bezanson, Jiahao Chen, Benjamin Chung, Stefan Karpinski, ViralB. Shah, Jan Vitek, and Lionel Zoubritzky.2018. Julia: dynamism and performance reconciled by design.Proc. ACM Program. Lang. 2, OOPSLA, Article 120 (October 2018),23 pages. DOI: https://doi.org/10.1145/3276490

27 / 28

Page 28: JIT Compilation and Dynamically Typed Languages › ~kulkarni › teaching › EECS768 › 19-Spring › TJ... · 2019-04-26 · Object unboxing 11/28. Low Level Optimization Low-Level

Questions?

28 / 28