10. The PyPy translation tool chain Toon Verwaest Thanks to Carl Friedrich Bolz for his kind permission to reuse and adapt his notes.

Slides:



Advertisements
Similar presentations
Runtime Feedback in a Meta-Tracing JIT for Efficient Dynamic Languages Writer: Carl Friedrich Bolz Introduced by Ryotaro IKEDA at 2011/09/06.
Advertisements

8. Static Single Assignment Form Marcus Denker. © Marcus Denker SSA Roadmap  Static Single Assignment Form (SSA)  Converting to SSA Form  Examples.
7. Optimization Prof. O. Nierstrasz Lecture notes by Marcus Denker.
CSI 3120, Implementing subprograms, page 1 Implementing subprograms The environment in block-structured languages The structure of the activation stack.
8. Introduction to Denotational Semantics. © O. Nierstrasz PS — Denotational Semantics 8.2 Roadmap Overview:  Syntax and Semantics  Semantics of Expressions.
12. Common Errors, a few Puzzles. © O. Nierstrasz P2 — Common Errors, a few Puzzles 12.2 Common Errors, a few Puzzles Sources  Cay Horstmann, Computing.
The Assembly Language Level
Program Representations. Representing programs Goals.
ESE Einführung in Software Engineering 7. Modeling Behaviour Prof. O. Nierstrasz.
The Last Lecture Copyright 2011, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 512 at Rice University have explicit permission.
Object-Oriented Reengineering Patterns and Techniques Prof. O. Nierstrasz Prof. S. Ducasse T.
8. Introduction to Denotational Semantics. © O. Nierstrasz PS — Denotational Semantics 8.2 Roadmap  Syntax and Semantics  Semantics of Expressions 
12. Summary, Trends, Research. © O. Nierstrasz PS — Summary, Trends, Research Roadmap  Summary: —Trends in programming paradigms  Research:...
Recap from last time We were trying to do Common Subexpression Elimination Compute expressions that are available at each program point.
12. Common Errors, a few Puzzles. © O. Nierstrasz P2 — Common Errors, a few Puzzles 12.2 Common Errors, a few Puzzles Overview  Common errors … —Typical.
ESE Einführung in Software Engineering N. XXX Prof. O. Nierstrasz Fall Semester 2009.
Representing programs Goals. Representing programs Primary goals –analysis is easy and effective just a few cases to handle directly link related things.
© Oscar Nierstrasz ST — Smalltalk Basics 2.1 Change sets  Make sure your changes are logged to a new change set.
13. Summary, Trends, Research. © O. Nierstrasz PS — Summary, Trends, Research Summary, Trends, Research...  Summary: functional, logic and object-oriented.
CS 536 Spring Intermediate Code. Local Optimizations. Lecture 22.
ESE Einführung in Software Engineering X. CHAPTER Prof. O. Nierstrasz Wintersemester 2005 / 2006.
7. Fixed Points. © O. Nierstrasz PS — Fixed Points 7.2 Roadmap  Representing Numbers  Recursion and the Fixed-Point Combinator  The typed lambda calculus.
1 Intermediate representation Goals: –encode knowledge about the program –facilitate analysis –facilitate retargeting –facilitate optimization scanning.
13. A bit of Smalltalk. © Oscar Nierstrasz 2 Roadmap  The origins of Smalltalk  What is Smalltalk?  Syntax in a nutshell  Seaside — web development.
9. Optimization Marcus Denker. 2 © Marcus Denker Optimization Roadmap  Introduction  Optimizations in the Back-end  The Optimizer  SSA Optimizations.
Metamodeling Seminar X. CHAPTER Prof. O. Nierstrasz Spring Semester 2008.
Java for High Performance Computing Jordi Garcia Almiñana 14 de Octubre de 1998 de la era post-internet.
© Oscar Nierstrasz ST — Smalltalk Basics 2.1 Change sets  Make sure your changes are logged to a new change set.
JVM-1 Introduction to Java Virtual Machine. JVM-2 Outline Java Language, Java Virtual Machine and Java Platform Organization of Java Virtual Machine Garbage.
ESE Einführung in Software Engineering X. CHAPTER Prof. O. Nierstrasz Wintersemester 2005 / 2006.
N. XXX Prof. O. Nierstrasz Thanks to Jens Palsberg and Tony Hosking for their kind permission to reuse and adapt the CS132 and CS502 lecture notes.
7. Fixed Points. © O. Nierstrasz PS — Fixed Points 7.2 Roadmap Overview  Representing Numbers  Recursion and the Fixed-Point Combinator  The typed.
12. Common Errors, a few Puzzles. © O. Nierstrasz P2 — Common Errors, a few Puzzles 12.2 Common Errors, a few Puzzles Sources  Cay Horstmann, Computing.
Intermediate Code. Local Optimizations
12. Summary, Trends, Research. © O. Nierstrasz PS — Summary, Trends, Research Roadmap  Summary: —Trends in programming paradigms  Research:...
Prof. Fateman CS164 Lecture 211 Local Optimizations Lecture 21.
© S. Demeyer, S. Ducasse, O. Nierstrasz Chapter.1 MakeMoney Corp. C*O of MakeMoney Corp. Our Vision  We invest in software  We do not know software 
OORPT Object-Oriented Reengineering Patterns and Techniques X. CHAPTER Prof. O. Nierstrasz.
CP — Concurrent Programming X. CHAPTER Prof. O. Nierstrasz Wintersemester 2005 / 2006.
Building An Interpreter After having done all of the analysis, it’s possible to run the program directly rather than compile it … and it may be worth it.
12. eToys. © O. Nierstrasz PS — eToys 12.2 Denotational Semantics Overview:  … References:  …
JAVA v.s. C++ Programming Language Comparison By LI LU SAMMY CHU By LI LU SAMMY CHU.
Precision Going back to constant prop, in what cases would we lose precision?
Topic #10: Optimization EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
Flex Compiler Compiler Case Study By Mee Ka Chang.
JIT in webkit. What’s JIT See time_compilation for more info. time_compilation.
10/1/2015© Hal Perkins & UW CSEG-1 CSE P 501 – Compilers Intermediate Representations Hal Perkins Autumn 2009.
CSC3315 (Spring 2009)1 CSC 3315 Programming Languages Hamid Harroud School of Science and Engineering, Akhawayn University
Internet of Things with Intel Edison Compiling and running Pierre Collet Intel Software.
7. Just In Time Compilation Prof. O. Nierstrasz Jan Kurs.
Lecture 10 : Introduction to Java Virtual Machine
CSC 310 – Imperative Programming Languages, Spring, 2009 Virtual Machines and Threaded Intermediate Code (instead of PR Chapter 5 on Target Machine Architecture)
1 Comp 104: Operating Systems Concepts Java Development and Run-Time Store Organisation.
SE: CHAPTER 7 Writing The Program
Basic Semantics Associating meaning with language entities.
COMP3190: Principle of Programming Languages
RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions,
CS 598 Scripting Languages Design and Implementation 14. Self Compilers.
FORTRAN History. FORTRAN - Interesting Facts n FORTRAN is the oldest Language actively in use today. n FORTRAN is still used for new software development.
Object Oriented Software Development 4. C# data types, objects and references.
Procedures and Functions Procedures and Functions – subprograms – are named fragments of program they can be called from numerous places  within a main.
By Anand George SourceLens.org Copyright. All rights reserved. Content Owner - Meera R (meera at sourcelens.org)
CS412/413 Introduction to Compilers and Translators April 2, 1999 Lecture 24: Introduction to Optimization.
Optimization of Python-bindings Wim T.L.P. Lavrijsen, LBNL ROOT Meeting, CERN; 01/15/10 C O M P U T A T I O N A L R E S E A R C H D I V I S I O N.
A Single Intermediate Language That Supports Multiple Implemtntation of Exceptions Delvin Defoe Washington University in Saint Louis Department of Computer.
Smalltalk Implementation Harry Porter, October 2009 Smalltalk Implementation: Optimization Techniques Prof. Harry Porter Portland State University 1.
COMP 2100 From Python to Java
Topic 10: Dataflow Analysis
CSE P 501 – Compilers SSA Hal Perkins Autumn /31/2019
JIT Compiler Design Maxine Virtual Machine Dhwani Pandya
Presentation transcript:

10. The PyPy translation tool chain Toon Verwaest Thanks to Carl Friedrich Bolz for his kind permission to reuse and adapt his notes.

© Toon Verwaest The PyPy tool chain 2 2 Roadmap > What is PyPy? > The PyPy interpreter > The PyPy translation tool chain

© Toon Verwaest The PyPy tool chain 3 3 Roadmap > What is PyPy? > The PyPy Interpreter > The PyPy translation tool chain

© Toon Verwaest The PyPy tool chain 4 4 What is PyPy? > Reimplementation of Python in Python > Framework for building interpreters and VMs > L * O * P configurations —L dynamic languages —O optimizations —P platforms

© Toon Verwaest The PyPy tool chain 5 PyPy

© Toon Verwaest The PyPy tool chain 6 Roadmap > What is PyPy? > The PyPy interpreter > The PyPy translation tool chain

© Toon Verwaest The PyPy tool chain 7 The PyPy Interpreter > Python: imperative, object-oriented dynamic language > Stack-based bytecode interpreter (like JVM, Smalltalk)‏ def f(x): return x + 1 >>> dis.dis(f)‏ 2 0 LOAD_FAST 0 (x)‏ 3 LOAD_CONST 1 (1)‏ 6 BINARY_ADD 7 RETURN_VALUE

© Toon Verwaest The PyPy tool chain 8 The PyPy Bytecode Compiler > Written in Python >.py to.pyc > Standard, flexible compiler —Lexer —Parser —AST builder —Bytecode generator > You only have to build this once

© Toon Verwaest The PyPy tool chain 9 Bytecode interpreter > Focuses on language semantics. No low-level details! > Written in RPython —This makes it very slow! About 2000x slower than CPython > PyPy's Python bytecode compiler and interpreter are not the hot topic of the PyPy project!

© Toon Verwaest The PyPy tool chain 10 Roadmap > What is PyPy? > The PyPy interpreter > The PyPy translation tool chain

© Toon Verwaest The PyPy tool chain 11 The PyPy Translation Tool Chain > Model-driven interpreter (VM) development —Focus on language model rather than implementation details —Executable models (meta-circular Python)‏ > Translate models to low-level (LL) back-ends —Considerably lower than Python —Weave in implementation details (GC, JIT)‏ —Allow compilation to different back-ends (OO, procedural)‏

© Toon Verwaest The PyPy tool chain 12 The PyPy Translation Tool Chain

© Toon Verwaest The PyPy tool chain 13 Inside the Translation Tool Chain

© Toon Verwaest The PyPy tool chain 14 PyPy “Parser” > Tool chain starts from loaded Python bytecode > Translator shares Python environment with the target > Relies on Python's reflective capabilities > Allows meta-programming (runtime initialization)‏ def a_decorator(an_f): def g(b): an_f(b+10)‏ return def f(a): print a f(4) -> 14

© Toon Verwaest The PyPy tool chain 15 PyPy Control-Flow Graph

© Toon Verwaest The PyPy tool chain 16 PyPy Control-Flow Graph > Consists of Blocks and Links > Starting from entry_point > “Single Static Information” form def f(n): return 3*n+2 Block(v1): # input argument v2 = mul(Constant(3), v1)‏ v3 = add(v2, Constant(2))‏

© Toon Verwaest The PyPy tool chain 17 PyPy CFG: “Static Single Information” > Remember SSA: PHIs at dominance frontiers

© Toon Verwaest The PyPy tool chain 18 PyPy CFG: “Static Single Information” def test(a): if a > 0: if a > 5: return 10 return 4 if a < - 10: return 3 return 10 > SSI: “PHIs” for all used variables – Blocks as “functions without branches”

© Toon Verwaest The PyPy tool chain 19 Type Inference

© Toon Verwaest The PyPy tool chain 20 Why type inference? > Python is dynamically typed > We want to translate to statically typed code —For efficiency reasons

What do we need to infer? > Type for every variable > Messages sent to an object must be defined in the compile-time type or a supertype © Toon Verwaest The PyPy tool chain

© Toon Verwaest The PyPy tool chain 22 How to infer types? > Starting from entry_point —Can reach the whole program —We know type of arguments and return-value > Forward propagation —Iteratively, until all links in the CFG have been followed at least once —Results in a large dictionary mapping variables to types

© Toon Verwaest The PyPy tool chain 23 Implications of applying type inference Applying type inference restricts type of input programs

© Toon Verwaest The PyPy tool chain 24 RPython: Demo def plus(a, b): return a + b def entry_point(arv=None): print plus(20, 22)‏ print plus(“4”, “2”)‏

© Toon Verwaest The PyPy tool chain 25 RPython: def plus(a, b): return a + b def entry_point(arv=None): print plus(20, 22)‏ print plus(“4”, “2”)‏

RPython is Zen > Subset of Python > Informally: The subset of Python which is type inferable > Actually: type inferable stabilized bytecode —Allows load-time meta-programming (see parser) —Messages sent to an object must be defined in the compile-time type or supertype © Toon Verwaest The PyPy tool chain 26

© Toon Verwaest The PyPy tool chain 27 RTyper

© Toon Verwaest The PyPy tool chain 28 RTyper > Bridge between annotator and low-level code generators > Different low-level models for different target groups —LLTypeSystem C-style (structures, pointers and arrays)‏ —OOTypeSystem JVM, CLI, Squeak (trace-off: single inheritance, )‏ > Does not need to iterate until a fixpoint is reached > Replaces all operations by low-level ones

© Toon Verwaest The PyPy tool chain 29 Back-end Optimizations

© Toon Verwaest The PyPy tool chain 30 Back-end Optimizations > Some general optimizations —Inlining —Constant folding —Escape analysis (allocating objects on the stack)‏ > Partly assume code generation for optimizing back-end

© Toon Verwaest The PyPy tool chain 31 Back-end Optimizations: “Object Explosion” > OO: lots of helper objects > Allocating objects is expensive > Replace unneeded objects with direct calls

© Toon Verwaest The PyPy tool chain 32 Preparation for Source Generation

© Toon Verwaest The PyPy tool chain 33 Exception Handling and Memory Management > C has no support for: —automatic memory management —exception handling > Translate explicit exception handling to flags and if/else > Memory management in PyPy spirit: —not language specific —weave garbage collector in during translation

© Toon Verwaest The PyPy tool chain 34 JIT Compiler > Makes VMs fast —Dynamic information is key > Is an implementation detail > Still under development > “As you surely know, the key idea of PyPy is that we are too lazy to write a JIT of our own: so, instead of passing nights writing a JIT, we pass years coding a JIT generator that writes the JIT for us :-)” Weave in while translating to low-level!

© Toon Verwaest The PyPy tool chain 35 Code Generation

© Toon Verwaest The PyPy tool chain 36 Code Generation > One C-function per Control-Flow Graph > All low-level statements can be translated directly > Gets compiled to binary format with C compiler

© Toon Verwaest The PyPy tool chain 37 Translation Demo

© Toon Verwaest The PyPy tool chain 38 PyPy Performance > Translator —Slow —Uses quite some memory —Produces lots of source code (200 kloc for 5 kloc source)‏ —But: our models are executable (2000x slower than CPython)‏ > Resulting Interpreter —Currently: two times slower to two times faster than CPython —First experiments with JIT: up to 500x faster for special cases —But most importantly: very adaptable!

© Toon Verwaest The PyPy tool chain 39 More PyPy & Getting Involved > > > irc://irc.freenode.org/pypy > PyPy sprints

© Toon Verwaest The PyPy tool chain 40 Summary > PyPy project has two main parts —Language interpreter models —PyPy translation tool chain > PyPy translation tool chain —Has no typical parser —Uses SSI —Applies type inference – Limits input from Python to RPython —Compiles to low-level and object-oriented back-ends —Weaves in implementation details

© Toon Verwaest The PyPy tool chain 41 Summary

© Toon Verwaest The PyPy tool chain 42 What you should know! What is the goal of the PyPy project? What are the main steps of the PyPy toolchain? When is a program RPython?

© Toon Verwaest The PyPy tool chain 43 Can you answer these questions? > Why do we want to keep the language model separated from implementation details? > Why wouldn't we want to keep those details separated? > Why is it not really a problem that the tool chain can only compile RPython code?

© Toon Verwaest The PyPy tool chain 44 xxx 44 License > Attribution-ShareAlike 2.5 You are free: to copy, distribute, display, and perform the work to make derivative works to make commercial use of the work Under the following conditions: Attribution. You must attribute the work in the manner specified by the author or licensor. Share Alike. If you alter, transform, or build upon this work, you may distribute the resulting work only under a license identical to this one. For any reuse or distribution, you must make clear to others the license terms of this work. Any of these conditions can be waived if you get permission from the copyright holder. Your fair use and other rights are in no way affected by the above.