Slide 1

Slide 1 text

Sulong: Fast LLVM IR Execution on the JVM with Truffle and Graal FOSDEM 2016: 31. January 2016 Manuel Rigger @RiggerManuel PhD student at Johannes Kepler University Linz, Austria

Slide 2

Slide 2 text

Why Do We Need A(nother) LLVM IR Interpreter? Speculative optimizations? Compile- time Link- time Run- time Offline 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.

Slide 3

Slide 3 text

Motivation Example: Function Pointer Calls 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 4

Slide 4 text

Sulong • LLVM IR interpreter running on the JVM • With dynamic optimizations and JIT compilation! • Available under a BSD 3-Clause License • https://github.com/graalvm/sulong • Contributions are welcome! • Sulong: Chinese for velocisaurus • 速: fast, rapid • 龙: dragon

Slide 5

Slide 5 text

Truffle Multi-Language Environment Graal Truffle R Ruby Java Scala C JavaScript C LLVM http://www.github.com/graalvm [1]

Slide 6

Slide 6 text

AST Interpreter define i32 @ascending(i32 %a, i32 %b) { %1 = sub nsw i32 %a, %b ret i32 %1 } Function Node WriteI32 Node %1 SubI32 Node ReadI32 Node %a ReadI32 Node %b ReadI32 Node %1 ReturnI32 Node

Slide 7

Slide 7 text

Truffle and Graal U U U U U I I I G G I I I G G Node Rewriting for Profiling Feedback AST Interpreter Rewritten Nodes AST Interpreter Uninitialized Nodes Compilation using Partial Evaluation Compiled Code Node Transitions S U I D G Uninitialized Integer Generic Double String

Slide 8

Slide 8 text

Truffle and Graal I I I G G I I I G G Deoptimization to AST Interpreter D I D G G D I D G G Node Rewriting to Update Profiling Feedback Recompilation using Partial Evaluation

Slide 9

Slide 9 text

Example 1: Value Profiling expectedValue = memory[ptr]; deoptimizeAndRewrite(); Uninitialized MemoryRead Node Profiling MemoryRead Node MemoryRead Node currentValue = memory[ptr]; if (currentValue == expectedValue) { return expectedValue; } else { deoptimizeAndRewrite(); } return memory[ptr];

Slide 10

Slide 10 text

Example 2: Polymorphic Function Pointer Inline Caches No call 2 calls if (compare == &ascending) { return ascending(a, b); } else if (compare == &descending) { return descending(a, b); } else { deoptimizeAndRewrite(); } Direct CallNode Direct CallNode Uninitialized CallNode Indirect CallNode Direct CallNode Uninitialized CallNode Uninitialized CallNode compare(a, b); >2 calls 1 call compare(a, b) > 0

Slide 11

Slide 11 text

Function Pointer Call Inlining descending ascending bubble_sort bubble_sort descending ascending

Slide 12

Slide 12 text

Demo

Slide 13

Slide 13 text

Getting started • Download the mx build tool • Clone the repo and build the project • Compile and run a program $ hg clone https://bitbucket.org/allr/mx $ export PATH=$PWD/mx:$PATH $ git clone https://github.com/graalvm/sulong $ cd sulong $ mx build $ mx su-clang -S -emit-llvm -o test.ll test.c $ mx su-run test.ll

Slide 14

Slide 14 text

Developing with mx • Generate Eclipse project files (also available for other IDEs) • Quality tools • run Sulong tests • Eclipse remote debugging (port 5005) $ mx eclipseinit $ mx checkstyle/findbugs/pylint/... $ mx su-tests $ mx su-debug test.ll

Slide 15

Slide 15 text

Compilation • Textual information about which LLVM functions are compiled • View Truffle and Graal graphs $ mx su-run test.ll -Dgraal.TraceTruffleCompilation=true $ mx igv $ mx su-run test.ll -Dgraal.Dump=Truffle

Slide 16

Slide 16 text

Example: Truffle Graph

Slide 17

Slide 17 text

Implementation of Memory • Unmanaged mode • Heap allocation: by native standard libraries • Stack allocation: Java Unsafe API • Graal Native Function Interface for library interoperability Graal Foreign Function Interface malloc

Slide 18

Slide 18 text

Current State • Performance: room for improvement on most benchmarks • Completeness: mostly focused on C so far • Missing: longjmp/setjmp, inline assembly, full support of 80 bit floats • Can execute most of the gcc.c-torture/execute benchmarks

Slide 19

Slide 19 text

Outlook • Low overhead security-related instrumentations  Graal is specialized to perform optimizations for operations like bounds or type checks • Memory safety via allocating on the Java heap • Tracking of integer overflows • Full Truffle integration • Debugger with source code highlighting • Language interoperability

Slide 20

Slide 20 text

Q/A @RiggerManuel • Thanks for listening!

Slide 21

Slide 21 text

Attributions • [1] The JRuby logo is copyright (c) Tony Price 2011, licensed under the terms of Creative Commons Attribution-NoDerivs 3.0 Unported (CC BY-ND 3.0)