21
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

Sulong: Fast LLVM IR Execution on the JVM with Truffle and ... · Sulong: Fast LLVM IR Execution on the JVM with Truffle and Graal FOSDEM 2016: 31. January 2016 ... Deo pti miz ati

Embed Size (px)

Citation preview

Page 1: Sulong: Fast LLVM IR Execution on the JVM with Truffle and ... · Sulong: Fast LLVM IR Execution on the JVM with Truffle and Graal FOSDEM 2016: 31. January 2016 ... Deo pti miz ati

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

Page 2: Sulong: Fast LLVM IR Execution on the JVM with Truffle and ... · Sulong: Fast LLVM IR Execution on the JVM with Truffle and Graal FOSDEM 2016: 31. January 2016 ... Deo pti miz ati

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.

Page 3: Sulong: Fast LLVM IR Execution on the JVM with Truffle and ... · Sulong: Fast LLVM IR Execution on the JVM with Truffle and Graal FOSDEM 2016: 31. January 2016 ... Deo pti miz ati

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; }

Page 4: Sulong: Fast LLVM IR Execution on the JVM with Truffle and ... · Sulong: Fast LLVM IR Execution on the JVM with Truffle and Graal FOSDEM 2016: 31. January 2016 ... Deo pti miz ati

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

Page 5: Sulong: Fast LLVM IR Execution on the JVM with Truffle and ... · Sulong: Fast LLVM IR Execution on the JVM with Truffle and Graal FOSDEM 2016: 31. January 2016 ... Deo pti miz ati

Truffle Multi-Language Environment

Graal

Truffle

RRuby

Java Scala

CJavaScript

CLLVM

http://www.github.com/graalvm

[1]

Page 6: Sulong: Fast LLVM IR Execution on the JVM with Truffle and ... · Sulong: Fast LLVM IR Execution on the JVM with Truffle and Graal FOSDEM 2016: 31. January 2016 ... Deo pti miz ati

AST Interpreter

define i32 @ascending(i32 %a, i32 %b) {

%1 = sub nsw i32 %a, %b

ret i32 %1

}

FunctionNode

WriteI32Node%1

SubI32Node

ReadI32Node%a

ReadI32Node%b

ReadI32Node%1

ReturnI32Node

Page 7: Sulong: Fast LLVM IR Execution on the JVM with Truffle and ... · Sulong: Fast LLVM IR Execution on the JVM with Truffle and Graal FOSDEM 2016: 31. January 2016 ... Deo pti miz ati

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

DoubleString

Page 8: Sulong: Fast LLVM IR Execution on the JVM with Truffle and ... · Sulong: Fast LLVM IR Execution on the JVM with Truffle and Graal FOSDEM 2016: 31. January 2016 ... Deo pti miz ati

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

Page 9: Sulong: Fast LLVM IR Execution on the JVM with Truffle and ... · Sulong: Fast LLVM IR Execution on the JVM with Truffle and Graal FOSDEM 2016: 31. January 2016 ... Deo pti miz ati

Example 1: Value Profiling

expectedValue = memory[ptr];

deoptimizeAndRewrite();

UninitializedMemoryRead

Node

ProfilingMemoryRead

Node

MemoryReadNode

currentValue = memory[ptr];

if (currentValue == expectedValue) {

return expectedValue;

} else {

deoptimizeAndRewrite();

}

return memory[ptr];

Page 10: Sulong: Fast LLVM IR Execution on the JVM with Truffle and ... · Sulong: Fast LLVM IR Execution on the JVM with Truffle and Graal FOSDEM 2016: 31. January 2016 ... Deo pti miz ati

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();

}

DirectCallNode

DirectCallNode

UninitializedCallNode

IndirectCallNode

DirectCallNode

UninitializedCallNode

UninitializedCallNode

compare(a, b);

>2 calls1 call

compare(a, b) > 0

Page 11: Sulong: Fast LLVM IR Execution on the JVM with Truffle and ... · Sulong: Fast LLVM IR Execution on the JVM with Truffle and Graal FOSDEM 2016: 31. January 2016 ... Deo pti miz ati

Function Pointer Call Inlining

descending

ascending

bubble_sort

bubble_sort

descending ascending

Page 12: Sulong: Fast LLVM IR Execution on the JVM with Truffle and ... · Sulong: Fast LLVM IR Execution on the JVM with Truffle and Graal FOSDEM 2016: 31. January 2016 ... Deo pti miz ati

Demo

Page 13: Sulong: Fast LLVM IR Execution on the JVM with Truffle and ... · Sulong: Fast LLVM IR Execution on the JVM with Truffle and Graal FOSDEM 2016: 31. January 2016 ... Deo pti miz ati

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

Page 14: Sulong: Fast LLVM IR Execution on the JVM with Truffle and ... · Sulong: Fast LLVM IR Execution on the JVM with Truffle and Graal FOSDEM 2016: 31. January 2016 ... Deo pti miz ati

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

Page 15: Sulong: Fast LLVM IR Execution on the JVM with Truffle and ... · Sulong: Fast LLVM IR Execution on the JVM with Truffle and Graal FOSDEM 2016: 31. January 2016 ... Deo pti miz ati

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

Page 16: Sulong: Fast LLVM IR Execution on the JVM with Truffle and ... · Sulong: Fast LLVM IR Execution on the JVM with Truffle and Graal FOSDEM 2016: 31. January 2016 ... Deo pti miz ati

Example: Truffle Graph

Page 17: Sulong: Fast LLVM IR Execution on the JVM with Truffle and ... · Sulong: Fast LLVM IR Execution on the JVM with Truffle and Graal FOSDEM 2016: 31. January 2016 ... Deo pti miz ati

Implementation of Memory

• Unmanaged mode• Heap allocation: by native standard libraries

• Stack allocation: Java Unsafe API

• Graal Native Function Interface for library interoperability

Graal ForeignFunction Interface

malloc

Page 18: Sulong: Fast LLVM IR Execution on the JVM with Truffle and ... · Sulong: Fast LLVM IR Execution on the JVM with Truffle and Graal FOSDEM 2016: 31. January 2016 ... Deo pti miz ati

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

Page 19: Sulong: Fast LLVM IR Execution on the JVM with Truffle and ... · Sulong: Fast LLVM IR Execution on the JVM with Truffle and Graal FOSDEM 2016: 31. January 2016 ... Deo pti miz ati

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

Page 20: Sulong: Fast LLVM IR Execution on the JVM with Truffle and ... · Sulong: Fast LLVM IR Execution on the JVM with Truffle and Graal FOSDEM 2016: 31. January 2016 ... Deo pti miz ati

Q/A @RiggerManuel

• Thanks for listening!

Page 21: Sulong: Fast LLVM IR Execution on the JVM with Truffle and ... · Sulong: Fast LLVM IR Execution on the JVM with Truffle and Graal FOSDEM 2016: 31. January 2016 ... Deo pti miz ati

Attributions

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