Graal and Truffle

Total Page:16

File Type:pdf, Size:1020Kb

Graal and Truffle Self-Specialising Interpreters and Start P:object Partial Evaluation IsNull Graal and Truffle If true false P:profiledHubs-0 Begin Begin guard Chris Seaton Deoptimize LoadHub Research Manager Oracle Labs == 9 August 2016 P:hubIsPositive-0 If true false IR Node Begin Begin Control-flow Edge Data-flow Edge Return Deoptimize 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. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | Compilers are, of course, metaprogramming systems Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | Writing languages that target the JVM Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | Hotspot Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | Hotspot Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | Hotspot JIT Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | Hotspot JIT Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | JIT Hotspot (Graal) JIT Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | Truffle JIT Hotspot (Graal) JIT Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | Two levels of program representation • Truffle – ASTs • Graal – compiler IR Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | Truffle Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | Node Rewriting Compilation using U for Profiling Feedback G Partial Evaluation G U U Node Transitions I G I G Uninitialized Integer I I U U U I I I S D AST Interpreter AST Interpreter Compiled Code Uninitialized Nodes String Double Rewritten Nodes G Generic 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. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | Node Rewriting Compilation using U for Profiling Feedback G Partial Evaluation G U U Node Transitions I G I G Uninitialized Integer I I U U U I I I S D AST Interpreter AST Interpreter Compiled Code Uninitialized Nodes String Double Rewritten Nodes G Generic 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. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | Node Rewriting Compilation using U for Profiling Feedback G Partial Evaluation G U U Node Transitions I G I G Uninitialized Integer I I U U U I I I S D AST Interpreter AST Interpreter Compiled Code Uninitialized Nodes String Double Rewritten Nodes G Generic 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. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | Node Rewriting Compilation using U for Profiling Feedback G Partial Evaluation G U U Node Transitions I G I G Uninitialized Integer I I U U U I I I S D AST Interpreter AST Interpreter Compiled Code Uninitialized Nodes String Double Rewritten Nodes G Generic 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. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | codon.com/compilers-for-free Presentation, by Tom Stuart, licensed under a Creative Commons Attribution ShareAlike 3.0 Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 19 Deoptimization Node Rewriting to Update Recompilation using to AST Interpreter G Profiling Feedback G Partial Evaluation G G I G I G D G D G I I I D I I I D 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/08/2016 Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | Deoptimization Node Rewriting to Update Recompilation using to AST Interpreter G Profiling Feedback G Partial Evaluation G G I G I G D G D G I I I D I I I D 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/08/2016 Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | Oracle Confidential – Internal/Restricted/Highly Restricted Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 22 Frequently executed call Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 23 Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 24 double BigInteger int Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 25 double BigInteger int Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 26 double BigInteger int Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 27 Partial Evaluation and Transfer to Interpreter Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 28 Example: Partial Evaluation class ExampleNode { // parameter this in rsi @CompilationFinal boolean flag; normal compilation cmpb [rsi + 16], 0 of method foo() jz L1 int foo() { mov eax, 42 if (this.flag) { ret return 42; L1: mov eax, -1 } else { ret return -1; } } mov rax, 42 Object value of this ret ExampleNode flag: true partial evaluation of method foo() with known parameter this Memory access is eliminated and condition is constant folded during partial evaluation @CompilationFinal field is treated like a final field during partial evaluation Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 29 Example: Transfer to Interpreter class ExampleNode { int foo(boolean flag) { compilation of method // parameter flag in edi if (flag) { foo() cmp edi, 0 return 42; jz L1 } else { mov eax, 42 throw new IllegalArgumentException( ret "flag: " + flag); L1: ... } // lots of code here } class ExampleNode { // parameter flag in edi int foo(boolean flag) { compilation of method foo() cmp edi, 0 if (flag) { jz L1 return 42; mov eax, 42 } else { ret transferToInterpreter(); L1: mov [rsp + 24], edi throw new IllegalArgumentException( call transferToInterpreter "flag: " + flag); // no more code, this point is unreachable } } transferToInterpreter() is a call into the VM runtime that does not return to its caller, because execution continues in the interpreter Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 30 Example: Partial Evaluation and Transfer to Interpreter class ExampleNode { Expected behavior: method negate() only partial evaluation called with allowed values @CompilationFinal boolean minValueSeen; of method negate() int negate(int value) { with known parameter this if (value == Integer.MIN_VALUE) { // parameter value in eax if (!minValueSeen) { cmp eax, 0x80000000 transferToInterpreterAndInvalidate(); ExampleNode jz L1 minValueSeen = true; minValueSeen: false neg eax } ret throw new ArithmeticException() L1: mov [rsp + 24], eax } call transferToInterpreterAndInvalidate // no more code, this point is unreachable return -value; } } if compiled code is invoked with minimum int value: 1) transfer back to the interpreter 2) invalidate the compiled code // parameter value in eax cmp eax, 0x80000000 jz L1 ExampleNode neg eax minValueSeen: true ret second L1: ... partial evaluation // lots of code here to throw exception Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 31 Branch Profiles class ExampleNode { Truffle profile API provides high-level API that final BranchProfile minValueSeen = BranchProfile.create(); hides complexity and is easier to use int negate(int value) { if (value == Integer.MIN_VALUE) { minValueSeen.enter(); throw new ArithmeticException(); } return -value; } } Best Practice: Use classes in com.oracle.truffle.api.profiles when possible, instead of @CompilationFinal Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 32 Condition Profiles for Branch Probability class ExampleNode { final ConditionProfile positive = ConditionProfile.createCountingProfile(); final BranchProfile minValueSeen = BranchProfile.create(); int abs(int value) { if (positive.profile(value >= 0)) { Counting ConditionProfile: add branch probability return value; for code paths with different execution frequencies } else if (value == Integer.MIN_VALUE) { minValueSeen.enter(); throw new ArithmeticException(); BranchProfile: remove unlikely code paths } else { return -value; } } } Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 33 Profiles: Summary • BranchProfile to speculate on unlikely branches – Benefit: remove code of unlikely code paths Profiles
Recommended publications
  • Compiler Error Messages Considered Unhelpful: the Landscape of Text-Based Programming Error Message Research
    Working Group Report ITiCSE-WGR ’19, July 15–17, 2019, Aberdeen, Scotland Uk Compiler Error Messages Considered Unhelpful: The Landscape of Text-Based Programming Error Message Research Brett A. Becker∗ Paul Denny∗ Raymond Pettit∗ University College Dublin University of Auckland University of Virginia Dublin, Ireland Auckland, New Zealand Charlottesville, Virginia, USA [email protected] [email protected] [email protected] Durell Bouchard Dennis J. Bouvier Brian Harrington Roanoke College Southern Illinois University Edwardsville University of Toronto Scarborough Roanoke, Virgina, USA Edwardsville, Illinois, USA Scarborough, Ontario, Canada [email protected] [email protected] [email protected] Amir Kamil Amey Karkare Chris McDonald University of Michigan Indian Institute of Technology Kanpur University of Western Australia Ann Arbor, Michigan, USA Kanpur, India Perth, Australia [email protected] [email protected] [email protected] Peter-Michael Osera Janice L. Pearce James Prather Grinnell College Berea College Abilene Christian University Grinnell, Iowa, USA Berea, Kentucky, USA Abilene, Texas, USA [email protected] [email protected] [email protected] ABSTRACT of evidence supporting each one (historical, anecdotal, and empiri- Diagnostic messages generated by compilers and interpreters such cal). This work can serve as a starting point for those who wish to as syntax error messages have been researched for over half of a conduct research on compiler error messages, runtime errors, and century. Unfortunately, these messages which include error, warn- warnings. We also make the bibtex file of our 300+ reference corpus ing, and run-time messages, present substantial difficulty and could publicly available.
    [Show full text]
  • Truffle/Graal: from Interpreters to Optimizing Compilers Via Partial Evaluation
    Truffle/Graal: From Interpreters to Optimizing Compilers via Partial Evaluation Jonathan Aldrich 17-396/17-696/17-960: Language Design and Prototyping Carnegie Mellon University Many slides from Oracle’s PLDI 2016 and 2017 tutorials on Truffle/Graal—marked where they occur. 1 Interpreters for Prototyping • Writing optimizing compilers is hard • Many complex optimization algorithms • Relies on knowledge of architecture and performance characteristics • See 15-411/15-611, 15-745 • So when we prototype with interpreters • Easy to write • Easy to change when the language changes • Unfortunately, interpreters are slow • Especially if you make them simple! • Could be 2 orders of magnitude slower than an optimizing compiler • Is there any way to make an interpreter faster? 2 From Interpreter to Compiler • Given • An interpreter for guest language G written in host language H • “if you see an add expression, add the two arguments” • A program in language G • “add(add(x, y), z)” • What if we “run” the interpreter on the program? • Whenever we get to actual input data, we’ll stop evaluating there (but keep going elsewhere) • “add(add(x, y), z)” x+y+z • We have essentially “compiled” the language • This is called Partial Evaluation • When applied in the context of interpreters and programs, it’s called the First Futamura Projection (after Futamura, who proposed it in 1971) 3 Example: Partial Evaluation class ExampleNode { // parameter this in rsi @CompilationFinal boolean flag; normal compilation cmpb [rsi + 16], 0 of method foo() jz L1 int foo() { mov eax, 42 if (this.flag) { ret return 42; L1: mov eax, -1 } else { ret return -1; } } mov rax, 42 Object value of this ret ExampleNode flag: true partial evaluation of method foo() with known parameter this Memory access is eliminated and condition is constant folded during partial evaluation @CompilationFinal field is treated like a final field during partial evaluation Copyright © 2016, Oracle and/or its affiliates.
    [Show full text]
  • Rule-Based Program Specialization to Optimize Gradually Typed Code
    Rule-based Program Specialization to Optimize Gradually Typed Code Francisco Ortina,∗, Miguel Garciaa, Seán McSweeneyb aUniversity of Oviedo, Computer Science Department, Calvo Sotelo s/n, 33007, Oviedo, Spain bCork Institute of Technology, Department of Computer Science, Rossa Avenue, Bishopstown, Cork, Ireland Abstract Both static and dynamic typing provide different benefits to the programmer. Stati- cally typed languages support earlier type error detection and more opportunities for compiler optimizations. Dynamically typed languages facilitate the development of runtime adaptable applications and rapid prototyping. Since both approaches provide benefits, gradually typed languages support both typing approaches in the very same programming language. Gradual typing has been an active research field in the last years, turning out to be a strong influence on commercial languages. However, one important drawback of gradual typing is the runtime performance cost of the additional type checks performed at runtime. In this article, we propose a rule-based program specialization mechanism to provide significant performance optimizations of gradually typed code. Our system gathers dynamic type information of the application by simulating its execution. That type information is used to optimize the generated code, reducing the number of type checks performed at runtime. Moreover, program specialization allows the early detection of compile-time type errors, providing static type safety. To ensure the correctness of the proposed approach, we prove its soundness and efficiency properties. The specialization system has been implemented as part of a full-fledged programming language, measuring the runtime performance gain. The generated code performs significantly better than the state-of-the-art techniques to optimize dynamically typed code.
    [Show full text]
  • An Integrated Approach to Source Level Debugging and Compile Error Reporting in Metaprograms
    Journal of Object Technology Published by AITO — Association Internationale pour les Technologies Objets, c JOT 2011 Online at http://www.jot.fm. An Integrated Approach to Source Level Debugging and Compile Error Reporting in Metaprograms Yannis Lilisa Anthony Savidisab a. Institute of Computer Science, FORTH b. Department of Computer Science, University of Crete Abstract Metaprogramming is an advanced language feature enabling to mix programs with definitions that may be executed either at compile- time or at runtime to generate source code to be put in their place. Such definitions are called metaprograms and their actual evaluation constitutes a compilation stage. As metaprograms are also programs, programmers should be supported in handling compile-time and runtime errors, something introducing challenges to the entire tool chain along two lines. Firstly, the source point of a compile error may well be the outcome of a series of compilation stages, thus never appearing within the original program. Effectively, the latter requires a compiler to track down the error chain across all involved stages so as to provide a meaningful, descriptive and precise error report. Secondly, every compilation stage is instantiated by the execution of the respective staged program. Thus, typical full-fledged source-level debugging for any particular stage should be facilitated during the compilation process. Existing implementations suffer in both terms, overall providing poor error messages, while lacking the required support for debugging metaprograms of any staging depth. In this paper, we outline the implementation of a compile-time metaprogramming system offering all aforementioned facilities. Then, we detail the required amendments to the compilation process and the necessary interaction between the compiler and the tool-chain (IDE).
    [Show full text]
  • Introduction to Java
    Class 9 Unit 2: Introduction to Java Answer the following questions Q 1- Who developed Java? What was it initially called? James Gosling developed Java programming language. It was initially called Oak. Q 2- Mention at least four features of Java. Four features of Java are: 1. It is an Object Oriented Programming Language. 2. It is platform independent. It provides us Write Once, Run Anywhere (WORA) feature. 3. It uses a compiler as well as an interpreter. 4. It is case sensitive. Q 3- Define the following: (a) A compiler A compiler is a program that translates a source program written in some high-level programming language into a target program in another low-level programming language without changing the meaning of the program. The compiler processes the complete source program at once and if there are compilation errors, they are all reported together at once. (b) An interpreter An interpreter is a program that reads a source program line by line, converts each line into its equivalent machine code and executes it. As it reads the program line by line so the errors are reported one by one. (c) Byte code Java compiler converts Java source code into an intermediate binary code called Bytecode. Bytecode can't be executed directly on the processor. It needs to be converted into Machine Code first. Q 4- What is Java Virtual Machine (JVM)? Java Virtual Machine (JVM) is a software that takes Bytecode as input, converts it into Machine code of the specific platform it is running on and executes it.
    [Show full text]
  • Declaration Syntax Error in C Programming
    Declaration Syntax Error In C Programming newfangledly,Ulick bisects his posticous Stonehenge and Nazi.mishearing Litho and proverbially unionized or Pincus purposely never after drabbed Elliot shedcumbrously and snort when Stevie distressfullylibrates his squiredom. but ail her evangelicalismSorbed Chester appealingly. still wobbles: unfailing and appointive Adolph outgun quite Ensure that they make sure not both good programmers for taking it is due to guarantee that it returns Note how do android to declare and programming languages, declarations to a declaration. Errors which thought be corrected by either human programmer who knew of about life source language. Syntax Errors. After reading and programming language syntax error message when this program without actually no similar functionality wherever a function? If between do and error had occur A const field requires a scholar to data provided. Insert a program in many times in a computer terms for example. Check dynamic sql syntax errorreference may want to. If you like this box to break the declaration syntax in error occurred. F Find with bug s in depth following program s 46 64 0 1. When a program to be additional behavior of a c programs or not be sure you must appear only useful if we get some key. C Sharp Program To pass Database Using Datagridview C Sharp. The declaration provides both protocols. 12 parsing restarts here on previous syntax error 13 expected a file. Python breaks are trademarks of. Object-Oriented Programming Using C. Comments in programming languages. Outermost function in programming language syntax in computer to. As in C you can mix declarations with other statements in your code.
    [Show full text]
  • 6172216 BASIC Compiler Mar
    --- - ----- ------- -------_.--- - --- Personal Computer Computer Language Series BASIC Compiler by Microsoft First Edition Revised (March 1982) Changes are periodically made to the information herein; these changes will be incorporated in new editions of this publication. Products are not stocked at the address below. Requests for copies of this product and for technical information about the system should he made to your authorized IBN! Personal Computer Dealer. A Product Comment Form is provided at the back of this publication. If this form has been removed, address comment to: IBNI Corp., Personal Computer, P.O. Box 1328-C, Boca Raton, Florida 33132. IBJ\;I may usc or distribute any of the information you supply in any way it believes appropriate without incurring any obligations whatever. ~-~Copyright International Business l\lachincs Corporation 1982 Preface How to Use this Book This book explains what you need to know about the IBM Personal Computer BASIC Compiler. It is designed for people who are unfamiliar with the compiler as a programming tool. Therefore, this book provides both a step-by-step introduction and a detailed technical guide to the BASIC Compiler and its use. After a few compila­ tions, you can use this book both as a refresher on procedures and as a technical reference. You should have a working knowledge of the BASIC language to use this book. For reference information on IBM Personal Computer BASIC, consult the IBM Personal Computer BASIC manual. Organization This book contains the following sections: Introducing the BASIC Compiler: Gives you an intro­ duction to compilers in general and a comparison of interpreters and compilers.
    [Show full text]
  • Sample Chapter
    Languages and CHAPTER Translators Objective When a particular problem is to be solved, it is necessary to design the instructions for computer to carry out and the art of writing instructions for a computer to solve the specific task is known as language and translator. Computer understands the instructions written in machine language. Thus, the program written in any other language needs to be translated into machine language. This task is done by translators which are special programs to check user programs grammatically and produce a corresponding set of machine language. This chapter on programming will help the users to make the programs. In this section we will enquire, why do we use the language and what are the roles of computer language. This chapter contains all steps by which user will be able to understand about data type, translator and steps to write and run C++ programs. • It is a medium by which a computer can communicate with the machine. • Language of computer is used to control the behavior/activity of the machine. • In computer science and engineering, human language is different from computer languages. • Unfortunately, computer cannot understand human languages. • Computers use special language such as C++ language, etc. • We must communicate with computer using special languages. Languages and Translators (b) Assembly language: • It is also known as a symbolic language. • In assembly language specified symbols are used. • It is a low level language. • Easier to correct the codes. • Easy to modify the instructions. • It has a demerit that it is machine dependent. • Translator is used for translating the input language to output language.
    [Show full text]
  • Usability of Error Messages for Introductory Students
    Scholarly Horizons: University of Minnesota, Morris Undergraduate Journal Volume 2 Issue 2 Article 5 September 2015 Usability of Error Messages for Introductory Students Paul A. Schliep University of Minnesota, Morris Follow this and additional works at: https://digitalcommons.morris.umn.edu/horizons Part of the Software Engineering Commons Recommended Citation Schliep, Paul A. (2015) "Usability of Error Messages for Introductory Students," Scholarly Horizons: University of Minnesota, Morris Undergraduate Journal: Vol. 2 : Iss. 2 , Article 5. Available at: https://digitalcommons.morris.umn.edu/horizons/vol2/iss2/5 This Article is brought to you for free and open access by the Journals at University of Minnesota Morris Digital Well. It has been accepted for inclusion in Scholarly Horizons: University of Minnesota, Morris Undergraduate Journal by an authorized editor of University of Minnesota Morris Digital Well. For more information, please contact [email protected]. Schliep: Usability of Error Messages for Introductory Students Usability of Error Messages for Introductory Students Paul A. Schliep Division of Science and Mathematics University of Minnesota, Morris Morris, Minnesota, USA 56267 [email protected] ABSTRACT that students struggle with various elements of error mes- Error messages are an important tool programmers use to sages such as terminology and source highlighting [1, 7]. help find and fix mistakes or issues in their code. When Several tools and heuristics are being developed to help ad- an error message is unhelpful, it can be difficult to find the dress issues in error message usability. The goals of these issue and may impose additional challenges in learning the methodologies are to help introductory programmers locate language and concepts.
    [Show full text]