Slide 1

Slide 1 text

Sulong - Execution of LLVM-Based Languages on the JVM Manuel Rigger Matthias Grimmer Hanspeter Mössenböck Johannes Kepler University Linz ICOOOLPS 18. July, 2016

Slide 2

Slide 2 text

Java Platform JVM [1] Rose. "Bytecodes meet combinators: invokedynamic on the JVM." [2] Würthinger et al. "One VM to rule them all." 2

Slide 3

Slide 3 text

Java Platform: Native Languages? JVM Java Native Interface Native side 3

Slide 4

Slide 4 text

Disadvantages • Slow • Transitions between Java and native code • Conversions/marshaling • Language boundaries are compilation boundaries • Difficult to use • boiler plate code • Breaks Java‘s safety guarantees Java Native Interface Native side JVM 4

Slide 5

Slide 5 text

Goals Native Interface Static + Dynamic Optimizations Memory Safety 5

Slide 6

Slide 6 text

System Overview LLVM IR Interpreter Truffle LLVM IR Clang C C++ GCC Fortran Other LLVM frontend ... JVM + Graal tooling 6

Slide 7

Slide 7 text

System Overview LLVM IR Interpreter Truffle LLVM IR Clang C C++ GCC Fortran Other LLVM frontend ... JVM + Graal tooling Use LLVM front ends and LLVM IR 6

Slide 8

Slide 8 text

System Overview LLVM IR Interpreter • Only component that has to be implemented LLVM IR Interpreter Truffle LLVM IR Clang C C++ GCC Fortran Other LLVM frontend ... JVM + Graal tooling 6

Slide 9

Slide 9 text

System Overview Use Truffle and Graal • Language Interoperability • Dynamic compilation LLVM IR Interpreter Truffle LLVM IR Clang C C++ GCC Fortran Other LLVM frontend ... JVM + Graal tooling 6

Slide 10

Slide 10 text

7

Slide 11

Slide 11 text

Static + Dynamic Optimizations Can we combine static with dynamic optimizations to top peak performance of static compilers? 7

Slide 12

Slide 12 text

Static + Dynamic Optimizations LLVM: advanced static optimizations Truffle and Graal: speculative optimizations based on profiling Compile- time Link- time Run- time 8

Slide 13

Slide 13 text

LLVM IR Interpreter Truffle LLVM IR JVM+Graal opt Optimization Pipeline 9

Slide 14

Slide 14 text

LLVM IR Interpreter Truffle LLVM IR JVM+Graal opt Optimization Pipeline Static LLVM optimizations 9

Slide 15

Slide 15 text

LLVM IR Interpreter Truffle LLVM IR JVM+Graal opt Optimization Pipeline Profiling-based optimizations • Polymorphic inline caches for function pointer calls • Function inlining Static LLVM optimizations 9

Slide 16

Slide 16 text

LLVM IR Interpreter Truffle LLVM IR JVM+Graal opt Optimization Pipeline Profiling-based optimizations • Polymorphic inline caches for function pointer calls • Function inlining Compilation of the AST Static LLVM optimizations 9

Slide 17

Slide 17 text

Dynamic Optimization: Example 10 void bubble_sort(int *numbers, int count, (*compare)(int a, int b)) { for (int i = 0; i < count; i++) { for (int j = 0; j < count - 1; j++) { if (compare(numbers[j], numbers[j+1]) > 0) { swap(&numbers[j], &numbers[j+1]); } } } } int ascending(int a, int b){ return a - b; } int descending(int a, int b){ return b - a; }

Slide 18

Slide 18 text

Dynamic Optimization: Example 11 bubble_sort bubble_sort ascending ascending ~1.3x speedup Polymorphic inline cache + Function inlining

Slide 19

Slide 19 text

Performance: C Benchmarks Lower is better, run-time ratio relative to Clang O3 12

Slide 20

Slide 20 text

13 Static + Dynamic Optimizations

Slide 21

Slide 21 text

Native Interface We can use Sulong as an alternative to JNI and to implement other language‘s native interfaces 13 Static + Dynamic Optimizations

Slide 22

Slide 22 text

Sulong as an Alternative to JNI Use Sulong as a Java library! Java C/C++/ Fortran Sulong 14

Slide 23

Slide 23 text

Case Studies: Sulong to Implement NFIs JRuby+Truffle FastR Graal.JS C/C++/ Fortran 15

Slide 24

Slide 24 text

Case Studies: Sulong to Implement NFIs JRuby+Truffle FastR Graal.JS C/C++/ Fortran 15

Slide 25

Slide 25 text

Truffle as a Multi Language Runtime [4] Grimmer, et al. "High-performance cross-language interoperability in a multi-language runtime." JVM Truffle Language Interoperability 16

Slide 26

Slide 26 text

Truffle as a Multi Language Runtime #include struct complex { double r; double i; }; int main() { struct complex *a = …; struct complex *b = …; add(a, b); } function add(a, b) { var result = {r:0, i:0}; result.r = a.r + b.r; result.i = a.i + b.i; return result; } main.c complex.js 17

Slide 27

Slide 27 text

Truffle as a Multi Language Runtime #include struct complex { double r; double i; }; int main() { struct complex *a = …; struct complex *b = …; add(a, b); } function add(a, b) { var result = {r:0, i:0}; result.r = a.r + b.r; result.i = a.i + b.i; return result; } add(a, b) main.c complex.js 17

Slide 28

Slide 28 text

Truffle as a Multi Language Runtime #include struct complex { double r; double i; }; int main() { struct complex *a = …; struct complex *b = …; add(a, b); } function add(a, b) { var result = {r:0, i:0}; result.r = a.r + b.r; result.i = a.i + b.i; return result; } add(a, b) a->r b->r a->i b->i main.c complex.js 17

Slide 29

Slide 29 text

Case Studies: Sulong to Implement NFIs 18 program.c vm.h Program JRuby+Truffle Written against Interop call Interop call vm.c [5] Grimmer, et al. "Dynamically composing languages in a modular way: supporting C extensions for dynamic languages."

Slide 30

Slide 30 text

Native Interface Static + Dynamic Optimizations 19

Slide 31

Slide 31 text

Native Interface Static + Dynamic Optimizations Memory Safety Can we provide memory safety for C/C++ and Fortran? 19

Slide 32

Slide 32 text

Memory Errors in C int *arr = malloc(4 * sizeof(int)) 20 [6] Szekeres, et al. "Sok: Eternal war in memory."

Slide 33

Slide 33 text

Memory Errors in C int *arr = malloc(4 * sizeof(int)) … = arr[5] arr[5] = … 20 [6] Szekeres, et al. "Sok: Eternal war in memory."

Slide 34

Slide 34 text

Memory Errors in C int *arr = malloc(4 * sizeof(int)) … = arr[5] arr[5] = … Spatial memory safety error 20 [6] Szekeres, et al. "Sok: Eternal war in memory."

Slide 35

Slide 35 text

Memory Errors in C int *arr = malloc(4 * sizeof(int)) … = arr[5] arr[5] = … free(arr); … = arr[0] arr[0] = … Spatial memory safety error 20 [6] Szekeres, et al. "Sok: Eternal war in memory."

Slide 36

Slide 36 text

Memory Errors in C int *arr = malloc(4 * sizeof(int)) … = arr[5] arr[5] = … free(arr); … = arr[0] arr[0] = … Spatial memory safety error Temporal memory safety error 20 [6] Szekeres, et al. "Sok: Eternal war in memory."

Slide 37

Slide 37 text

Memory Errors in C int *arr = malloc(4 * sizeof(int)) … = arr[5] arr[5] = … free(arr); … = arr[0] arr[0] = … Spatial memory safety error Temporal memory safety error 20 • Segmentation faults • (Silent) data corruption • Reading of sensitive data • Code injection [6] Szekeres, et al. "Sok: Eternal war in memory."

Slide 38

Slide 38 text

Current Sulong implementation • Relies on unmanaged memory (sun.misc.Unsafe) • https://github.com/graalvm/sulong • Goal: use managed Java objects 21

Slide 39

Slide 39 text

Prevent Spatial Errors … = arr[5] arr[5] = … 22 (arr[5] ≈ arr + sizeof(int) * 5) ManagedAddress offset=20 data I32Array elementSize=4 contents {1, 2, 3, 4}

Slide 40

Slide 40 text

Prevent Spatial Errors … = arr[5] arr[5] = … contents[20 / 4]  ArrayOutOfBoundsException 22 (arr[5] ≈ arr + sizeof(int) * 5) ManagedAddress offset=20 data I32Array elementSize=4 contents {1, 2, 3, 4}

Slide 41

Slide 41 text

Prevent Temporal Errors free(arr); … = arr[0] arr[0] = … 23 ManagedAddress offset=0 data I32Array elementSize=4 contents=null

Slide 42

Slide 42 text

Prevent Temporal Errors free(arr); … = arr[0] arr[0] = … contents[0]  NullPointerException 23 ManagedAddress offset=0 data I32Array elementSize=4 contents=null

Slide 43

Slide 43 text

Limitations • Warm-up and start-up • Current limitations: multithreading, 80 bit floats, inline assembler support • Support of all library functions • Conundrum between memory safety and programmer freedom 24

Slide 44

Slide 44 text

Summary 25 Native Interface Static + Dynamic Optimizations Memory Safety JRuby+Truffle FastR Graal.JS C/C++/ Fortran … = arr[5] arr[5] = … contents[20 / 4]  ArrayOutOfBoundsException ManagedAddress offset=20 data I32Array elementSize=4 contents {1, 2, 3, 4}

Slide 45

Slide 45 text

References [1] Rose, John R. "Bytecodes meet combinators: invokedynamic on the JVM." Proceedings of the Third Workshop on Virtual Machines and Intermediate Languages. ACM, 2009. [2] Würthinger, Thomas, et al. "One VM to rule them all." Proceedings of the 2013 ACM international symposium on New ideas, new paradigms, and reflections on programming & software. ACM, 2013. [3] Lattner, Chris, and Vikram Adve. "LLVM: A compilation framework for lifelong program analysis & transformation." Code Generation and Optimization, 2004. CGO 2004. International Symposium on. IEEE, 2004. [4] Grimmer, Matthias, et al. "High-performance cross-language interoperability in a multi-language runtime." Proceedings of the 11th Symposium on Dynamic Languages. ACM, 2015. [5] Grimmer, Matthias, et al. "Dynamically composing languages in a modular way: supporting C extensions for dynamic languages." Proceedings of the 14th International Conference on Modularity. ACM, 2015. [6] Szekeres, Laszlo, et al. "Sok: Eternal war in memory." Security and Privacy (SP), 2013 IEEE Symposium on. IEEE, 2013. 26