Upgrade to Pro — share decks privately, control downloads, hide ads and more …

FOSDEM'16: Sulong: Fast LLVM IR Execution on the JVM with Truffle and Graal

FOSDEM'16: Sulong: Fast LLVM IR Execution on the JVM with Truffle and Graal

Talk at FOSDEM 2016

Manuel Rigger

January 31, 2016

More Decks by Manuel Rigger

Other Decks in Programming


  1. 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
  2. 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.
  3. 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; }
  4. 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
  5. Truffle Multi-Language Environment Graal Truffle R Ruby Java Scala C

    JavaScript C LLVM http://www.github.com/graalvm [1]
  6. 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
  7. 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
  8. 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
  9. 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];
  10. 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
  11. Function Pointer Call Inlining descending ascending bubble_sort bubble_sort descending ascending

  12. Demo

  13. 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
  14. 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
  15. 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
  16. Example: Truffle Graph

  17. 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
  18. 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
  19. 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
  20. Q/A @RiggerManuel • Thanks for listening!

  21. 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)