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

Using LLVM and Sulong for Language C Extensions

Chris Seaton
September 01, 2016

Using LLVM and Sulong for Language C Extensions

At the LLVM Cauldron. 2016.

Chris Seaton

September 01, 2016
Tweet

More Decks by Chris Seaton

Other Decks in Research

Transcript

  1. Copyright © 2016, Oracle and/or its affiliates. All rights reserved.

    | Using LLVM and Sulong for Language C Extensions Chris Seaton Research Manager VM Research Group Oracle Labs Manuel Rigger, Matthias Grimmer JKU Linz
  2. Copyright © 2016, Oracle and/or its affiliates. All rights reserved.

    | Safe Harbor Statement The following is intended to provide some insight into a line of research in Oracle Labs. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. Oracle reserves the right to alter its development plans and practices at any time, and the development, release, and timing of any features or functionality described in connection with any Oracle product or service remains at the sole discretion of Oracle. Any views expressed in this presentation are my own and do not necessarily reflect the views of Oracle. 3
  3. Copyright © 2016, Oracle and/or its affiliates. All rights reserved.

    | 6 Graal Truffle R Ruby Java Scala JavaScript LLVM The Ruby Logo is Copyright (c) 2006, Yukihiro Matsumoto. It is licensed under the terms of the Creative Commons Attribution-ShareAlike 2.5 agreement JS Logo Copyright (c) 2011 Christopher Williams <[email protected]>, MIT licence You can distribute the logo under the terms of the Creative Commons Attribution-ShareAlike 4.0 International license (CC-BY-SA 4.0) or (at your option) the GNU General Public License version 2 (GPL-2). JVM
  4. Copyright © 2016, Oracle and/or its affiliates. All rights reserved.

    | 8 The Ruby Logo is Copyright (c) 2006, Yukihiro Matsumoto. It is licensed under the terms of the Creative Commons Attribution-ShareAlike 2.5 agreement JS Logo Copyright (c) 2011 Christopher Williams <[email protected]>, MIT licence The Python logo is a trademark of the Python Software Foundation
  5. Copyright © 2016, Oracle and/or its affiliates. All rights reserved.

    | Performance on Ruby C Extensions Oily PNG and PSD Native 12 M. Grimmer, C. Seaton, T. Würthinger, H. Mössenböck. Dynamically Composing Languages in a Modular Way: Supporting C Extensions for Dynamic Languages. In Proceedings of the 14th International Conference on Modularity, 2015. 0 2 4 6 8 10 12 MRI Pure Ruby MRI With C Extension
  6. Copyright © 2016, Oracle and/or its affiliates. All rights reserved.

    | 14 Structs declared in the public API Implementation details like embedded strings and sharing exposed
  7. Copyright © 2016, Oracle and/or its affiliates. All rights reserved.

    | 15 Ruby String object Exposes inner char* String object with char* already exposed now returned to Ruby Call to arbitrary C code Managed Ruby value on the C stack
  8. Copyright © 2016, Oracle and/or its affiliates. All rights reserved.

    | 16 Values need to be converted as they go from Ruby to native Call from Ruby to native is extremely hot
  9. Copyright © 2016, Oracle and/or its affiliates. All rights reserved.

    | 17 Array implementation pointer taken and stored for later When they’re used there’s no indication someone else is managing them
  10. Copyright © 2016, Oracle and/or its affiliates. All rights reserved.

    | • Interpret both the Ruby and the C • Actually, interpret the LLVM IR of the C to simplify • JIT compile the Ruby and the C • Use a single high and low level IR for both • Forget which language the IR came from and optimise them together • Give virtualised pointers to the C program 21
  11. Copyright © 2016, Oracle and/or its affiliates. All rights reserved.

    | U U U U U Node Rewriting for Profiling Feedback AST Interpreter Uninitialized Nodes Node Transitions S U I D G Uninitialized Integer Generic Double String T. Würthinger, C. Wimmer, A. Wöß, L. Stadler, G. Duboscq, C. Humer, G. Richards, D. Simon, and M. Wolczko. One VM to rule them all. In Proceedings of Onward!, 2013.
  12. Copyright © 2016, Oracle and/or its affiliates. All rights reserved.

    | U U U U U I I I G G Node Rewriting for Profiling Feedback AST Interpreter Rewritten Nodes AST Interpreter Uninitialized Nodes Node Transitions S U I D G Uninitialized Integer Generic Double String T. Würthinger, C. Wimmer, A. Wöß, L. Stadler, G. Duboscq, C. Humer, G. Richards, D. Simon, and M. Wolczko. One VM to rule them all. In Proceedings of Onward!, 2013.
  13. Copyright © 2016, Oracle and/or its affiliates. All rights reserved.

    | I I I G G I I I G G Rewriting ng Feedback AST Interpreter Rewritten Nodes Compilation using Partial Evaluation Compiled Code I D Uninitialized Integer Generic Double T. Würthinger, C. Wimmer, A. Wöß, L. Stadler, G. Duboscq, C. Humer, G. Richards, D. Simon, and M. Wolczko. One VM to rule them all. In Proceedings of Onward!, 2013.
  14. Copyright © 2016, Oracle and/or its affiliates. All rights reserved.

    | 32 codon.com/compilers-for-free Presentation, by Tom Stuart, licensed under a Creative Commons Attribution ShareAlike 3.0
  15. Copyright © 2016, Oracle and/or its affiliates. All rights reserved.

    | 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 T. Würthinger, C. Wimmer, A. Wöß, L. Stadler, G. Duboscq, C. Humer, G. Richards, D. Simon, and M. Wolczko. One VM to rule them all. In Proceedings of Onward!, 2013.
  16. Copyright © 2016, Oracle and/or its affiliates. All rights reserved.

    | 13/09/2016 T. Würthinger, C. Wimmer, A. Wöß, L. Stadler, G. Duboscq, C. Humer, G. Richards, D. Simon, and M. Wolczko. One VM to rule them all. In Proceedings of Onward!, 2013. I I I G G I I I G G Deoptimization to AST Interpreter D I Node Rewriting to Update Profiling Feedback
  17. Copyright © 2016, Oracle and/or its affiliates. All rights reserved.

    | 13/09/2016 Oracle Confidential – Internal/Restricted/Highly Restricted T. Würthinger, C. Wimmer, A. Wöß, L. Stadler, G. Duboscq, C. Humer, G. Richards, D. Simon, and M. Wolczko. One VM to rule them all. In Proceedings of Onward!, 2013. I I G G D I D G G D I D G G Node Rewriting to Update Profiling Feedback Recompilation using Partial Evaluation
  18. Copyright © 2016, Oracle and/or its affiliates. All rights reserved.

    | 13/09/2016 Oracle Confidential – Internal/Restricted/Highly Restricted T. Würthinger, C. Wimmer, A. Wöß, L. Stadler, G. Duboscq, C. Humer, G. Richards, D. Simon, and M. Wolczko. One VM to rule them all. In Proceedings of Onward!, 2013. 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
  19. Copyright © 2016, Oracle and/or its affiliates. All rights reserved.

    | Hotspot Graal Truffle JS Ruby R Java C++ JVMCI (JVM Compiler Interface)
  20. Copyright © 2016, Oracle and/or its affiliates. All rights reserved.

    | Hotspot Graal Truffle JS Ruby R via Maven etc Java 9
  21. Copyright © 2016, Oracle and/or its affiliates. All rights reserved.

    | Completeness – language and core library Oracle Confidential – Internal Ruby language JRuby passes 94% 95% Ruby core library JRuby passes 95% 99%
  22. Copyright © 2016, Oracle and/or its affiliates. All rights reserved.

    | Completeness – the basic Rails stack Oracle Confidential – Internal Active Model Active Support Active Record Action View Action Pack Action Mailer Railties Sprockets-Rails Active Job Spring 100% 100% 98% Basic functionality works 37%
  23. Copyright © 2016, Oracle and/or its affiliates. All rights reserved.

    | Classic research benchmarks – 10-20x faster Confidential – Oracle Internal/Restricted/Highly Restricted 48 0 5 10 15 20 25 30 35 40 45 Speedup Compared to Ruby GraalVM JRuby+invokedynamic Ruby Benchmarks bound by allocation or BigInteger performance
  24. Copyright © 2016, Oracle and/or its affiliates. All rights reserved.

    | 53 cmyk_to_rgb psd_native_util_clamp FIX2INT
  25. Copyright © 2016, Oracle and/or its affiliates. All rights reserved.

    | 54 cmyk_to_rgb psd_native_util_clamp FIX2INT
  26. Copyright © 2016, Oracle and/or its affiliates. All rights reserved.

    | 56 Instead of RARRAY_PTR returning a pointer (a number), return a proper Java object Operations like getelementptr can return a new Java object that remembers the original object, and what offset to use Let SSA names store Java objects as well as numbers The load can then use whatever logic we want to actually read a value from the Java object – reuse normal Ruby array logic
  27. Copyright © 2016, Oracle and/or its affiliates. All rights reserved.

    | 58 Virtualised pointer – really points to a Java object which represents our Ruby string The C program has no way of knowing it’s not a real char * Intrinsic operations on the pointer are redirected to be method calls
  28. Copyright © 2016, Oracle and/or its affiliates. All rights reserved.

    | Evaluation is based on earlier work • We used to have a C interpreter – TruffleC • We’ve moved on from this, because we want to support more languages • But we aren’t able to run all the same benchmarks yet • So we’ve showing results from our old implementation in the mean time • We’re pretty sure results will be similar, as the compiled code is similar 60
  29. Copyright © 2016, Oracle and/or its affiliates. All rights reserved.

    | Performance on Ruby C Extensions Oily PNG and PSD Native 61 0 5 10 15 20 25 30 35 MRI Pure Ruby MRI With C Extension Rubinius With C Extension JRuby With C Extension JRuby+Truffle With C Extension JRuby+Truffle With C Extension (No Inline) M. Grimmer, C. Seaton, T. Würthinger, H. Mössenböck. Dynamically Composing Languages in a Modular Way: Supporting C Extensions for Dynamic Languages. In Proceedings of the 14th International Conference on Modularity, 2015.
  30. Copyright © 2016, Oracle and/or its affiliates. All rights reserved.

    | Performance on Ruby C Extensions Oily PNG and PSD Native 62 0 5 10 15 20 25 30 35 MRI Pure Ruby MRI With C Extension Rubinius With C Extension JRuby With C Extension JRuby+Truffle With C Extension JRuby+Truffle With C Extension (No Inline) M. Grimmer, C. Seaton, T. Würthinger, H. Mössenböck. Dynamically Composing Languages in a Modular Way: Supporting C Extensions for Dynamic Languages. In Proceedings of the 14th International Conference on Modularity, 2015. Native C extensions give an order of magnitude performance boost
  31. Copyright © 2016, Oracle and/or its affiliates. All rights reserved.

    | Performance on Ruby C Extensions Oily PNG and PSD Native 63 0 5 10 15 20 25 30 35 MRI Pure Ruby MRI With C Extension Rubinius With C Extension JRuby With C Extension JRuby+Truffle With C Extension JRuby+Truffle With C Extension (No Inline) M. Grimmer, C. Seaton, T. Würthinger, H. Mössenböck. Dynamically Composing Languages in a Modular Way: Supporting C Extensions for Dynamic Languages. In Proceedings of the 14th International Conference on Modularity, 2015. Existing attempt to mix managed/native are very disappointing
  32. Copyright © 2016, Oracle and/or its affiliates. All rights reserved.

    | Performance on Ruby C Extensions Oily PNG and PSD Native 64 0 5 10 15 20 25 30 35 MRI Pure Ruby MRI With C Extension Rubinius With C Extension JRuby With C Extension JRuby+Truffle With C Extension JRuby+Truffle With C Extension (No Inline) M. Grimmer, C. Seaton, T. Würthinger, H. Mössenböck. Dynamically Composing Languages in a Modular Way: Supporting C Extensions for Dynamic Languages. In Proceedings of the 14th International Conference on Modularity, 2015. Our solution is 3x faster than native!
  33. Copyright © 2016, Oracle and/or its affiliates. All rights reserved.

    | Performance on Ruby C Extensions Oily PNG and PSD Native 65 0 5 10 15 20 25 30 35 MRI Pure Ruby MRI With C Extension Rubinius With C Extension JRuby With C Extension JRuby+Truffle With C Extension JRuby+Truffle With C Extension (No Inline) M. Grimmer, C. Seaton, T. Würthinger, H. Mössenböck. Dynamically Composing Languages in a Modular Way: Supporting C Extensions for Dynamic Languages. In Proceedings of the 14th International Conference on Modularity, 2015. It’s clear that cross-language inlining is a key part of the performance
  34. Copyright © 2016, Oracle and/or its affiliates. All rights reserved.

    | Open Source • https://github.com/graalvm/graal-core – Graal compiler • https://github.com/graalvm/truffle – Truffle language implementation framework • https://github.com/graalvm/fastr – Fast R runtime • https://github.com/graalvm/sulong – Dynamic runtime for LLVM bitcode • https://github.com/jruby/jruby/wiki/Truffle – Fast Ruby runtime 68
  35. Copyright © 2016, Oracle and/or its affiliates. All rights reserved.

    | Acknowledgements 69 Oracle Danilo Ansaloni Stefan Anzinger Cosmin Basca Daniele Bonetta Matthias Brantner Petr Chalupa Jürgen Christ Laurent Daynès Gilles Duboscq Martin Entlicher Brandon Fish Bastian Hossbach Christian Humer Mick Jordan Vojin Jovanovic Peter Kessler David Leopoldseder Kevin Menard Jakub Podlešák Aleksandar Prokopec Tom Rodriguez Oracle (continued) Roland Schatz Chris Seaton Doug Simon Štěpán Šindelář Zbyněk Šlajchrt Lukas Stadler Codrut Stancu Jan Štola Jaroslav Tulach Michael Van De Vanter Adam Welc Christian Wimmer Christian Wirth Paul Wögerer Mario Wolczko Andreas Wöß Thomas Würthinger JKU Linz Prof. Hanspeter Mössenböck Benoit Daloze Josef Eisl Thomas Feichtinger Matthias Grimmer Christian Häubl Josef Haider Christian Huber Stefan Marr Manuel Rigger Stefan Rumzucker Bernhard Urban University of Edinburgh Christophe Dubach Juan José Fumero Alfonso Ranjeet Singh Toomas Remmelg LaBRI Floréal Morandat University of California, Irvine Prof. Michael Franz Gulfem Savrun Yeniceri Wei Zhang Purdue University Prof. Jan Vitek Tomas Kalibera Petr Maj Lei Zhao T. U. Dortmund Prof. Peter Marwedel Helena Kotthaus Ingo Korb University of California, Davis Prof. Duncan Temple Lang Nicholas Ulle University of Lugano, Switzerland Prof. Walter Binder Sun Haiyang Yudi Zheng Oracle Interns Brian Belleville Miguel Garcia Shams Imam Alexey Karyakin Stephen Kell Andreas Kunft Volker Lanting Gero Leinemann Julian Lettner Joe Nash David Piorkowski Gregor Richards Robert Seilbeck Rifat Shariyar Alumni Erik Eckstein Michael Haupt Christos Kotselidis Hyunjin Lee David Leibs Chris Thalinger Till Westmann
  36. Copyright © 2016, Oracle and/or its affiliates. All rights reserved.

    | Safe Harbor Statement The preceding is intended to provide some insight into a line of research in Oracle Labs. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. Oracle reserves the right to alter its development plans and practices at any time, and the development, release, and timing of any features or functionality described in connection with any Oracle product or service remains at the sole discretion of Oracle. Any views expressed in this presentation are my own and do not necessarily reflect the views of Oracle. 70