Field Analysis. Last time Exploit encapsulation to improve memory system performance

Similar documents
Agenda. CSE P 501 Compilers. Java Implementation Overview. JVM Architecture. JVM Runtime Data Areas (1) JVM Data Types. CSE P 501 Su04 T-1

CSE P 501 Compilers. Java Implementation JVMs, JITs &c Hal Perkins Winter /11/ Hal Perkins & UW CSE V-1

Introduction to Visual Basic and Visual C++ Introduction to Java. JDK Editions. Overview. Lesson 13. Overview

Calvin Lin The University of Texas at Austin

Interprocedural Analysis. Motivation. Interprocedural Analysis. Function Calls and Pointers

Running class Timing on Java HotSpot VM, 1

Lecture 15a Persistent Memory & Shared Pointers

Managed runtimes & garbage collection. CSE 6341 Some slides by Kathryn McKinley

Managed runtimes & garbage collection

CS 536 Introduction to Programming Languages and Compilers Charles N. Fischer Lecture 11

Acknowledgements These slides are based on Kathryn McKinley s slides on garbage collection as well as E Christopher Lewis s slides

Run-time Environments

Run-time Environments

Compiler construction 2009

Lecture 9 Dynamic Compilation

USC 227 Office hours: 3-4 Monday and Wednesday CS553 Lecture 1 Introduction 4

CSE 501: Compiler Construction. Course outline. Goals for language implementation. Why study compilers? Models of compilation

CS 231 Data Structures and Algorithms, Fall 2016

Special Topics: Programming Languages

Software Speculative Multithreading for Java

Object Oriented Design

Operational Semantics. One-Slide Summary. Lecture Outline

Chapter 1 GETTING STARTED. SYS-ED/ Computer Education Techniques, Inc.

Flow-sensitive rewritings and Inliner improvements for the Graal JIT compiler

JAVA PERFORMANCE. PR SW2 S18 Dr. Prähofer DI Leopoldseder

Sustainable Memory Use Allocation & (Implicit) Deallocation (mostly in Java)

Escape Analysis. Applications to ML and Java TM

JOVE. An Optimizing Compiler for Java. Allen Wirfs-Brock Instantiations Inc.

Static Semantics. Winter /3/ Hal Perkins & UW CSE I-1

CSE 401 Final Exam. December 16, 2010

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Compaq Interview Questions And Answers

Java Internals. Frank Yellin Tim Lindholm JavaSoft

SSA Based Mobile Code: Construction and Empirical Evaluation

Run-time Environments. Lecture 13. Prof. Alex Aiken Original Slides (Modified by Prof. Vijay Ganesh) Lecture 13

Types. Type checking. Why Do We Need Type Systems? Types and Operations. What is a type? Consensus

A program execution is memory safe so long as memory access errors never occur:

Lecture 3: C Programm

Heap, Variables, References, and Garbage. CS152. Chris Pollett. Oct. 13, 2008.

Dynamic Dispatch and Duck Typing. L25: Modern Compiler Design

Programming II (CS300)

G Programming Languages - Fall 2012

2. Introducing Classes

G Programming Languages - Fall 2012

CS153: Compilers Lecture 15: Local Optimization

Operational Semantics of Cool

CIS 341 Final Examination 3 May 2011

Lecture Outline. COOL operational semantics. Operational Semantics of Cool. Motivation. Lecture 13. Notation. The rules. Evaluation Rules So Far

Compiling Java For High Performance on Servers

Register Allocation. CS 502 Lecture 14 11/25/08

CS201 Some Important Definitions

Example: Count of Points

Compositional Pointer and Escape Analysis for Java Programs

Run-time Environments - 3

Object-Oriented Programming for Scientific Computing

Chapter 5. Names, Bindings, and Scopes

Static Program Analysis Part 9 pointer analysis. Anders Møller & Michael I. Schwartzbach Computer Science, Aarhus University

Properties of an identifier (and the object it represents) may be set at

Lecture Notes on Alias Analysis

One-Slide Summary. Lecture Outine. Automatic Memory Management #1. Why Automatic Memory Management? Garbage Collection.

Cooperative Memory Management in Embedded Systems

Office Hours: Mon/Wed 3:30-4:30 GDC Office Hours: Tue 3:30-4:30 Thu 3:30-4:30 GDC 5.

Chapter 6 Introduction to Defining Classes

CSE 401/M501 Compilers

Garbage Collection. Akim D le, Etienne Renault, Roland Levillain. May 15, CCMP2 Garbage Collection May 15, / 35

Short Notes of CS201

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

Runtime. The optimized program is ready to run What sorts of facilities are available at runtime

Automatic Memory Management

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Q1: /8 Q2: /30 Q3: /30 Q4: /32. Total: /100

Garbage Collection (1)

Write Barrier Removal by Static Analysis

Lecture Outline. COOL operational semantics. Operational Semantics of Cool. Motivation. Notation. The rules. Evaluation Rules So Far.

CS201 - Introduction to Programming Glossary By

Expressing high level optimizations within LLVM. Artur Pilipenko

Alias Analysis. Advanced Topics. What is pointer analysis? Last Time

Lecture Notes on Garbage Collection

1 Epic Test Review 2 Epic Test Review 3 Epic Test Review 4. Epic Test Review 5 Epic Test Review 6 Epic Test Review 7 Epic Test Review 8

CS 241 Honors Memory

The Structure of a Syntax-Directed Compiler

Lecture 13: Garbage Collection

Goals of Program Optimization (1 of 2)

Weeks 6&7: Procedures and Parameter Passing

Untyped Memory in the Java Virtual Machine

Compiler construction 2009

Object-Oriented Programming

Introduction to PThreads and Basic Synchronization

Shenandoah: An ultra-low pause time garbage collector for OpenJDK. Christine Flood Roman Kennke Principal Software Engineers Red Hat

Compiler Construction D7011E

JiST: Java in Simulation Time

Lecture 27. Pros and Cons of Pointers. Basics Design Options Pointer Analysis Algorithms Pointer Analysis Using BDDs Probabilistic Pointer Analysis

Lecture 20 Pointer Analysis

Simply-Typed Lambda Calculus

Lecture 14 Pointer Analysis

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

5. Semantic Analysis!

Intermediate Code & Local Optimizations. Lecture 20

CS553 Lecture Dynamic Optimizations 2

Coping with Immutable Data in a JVM for Embedded Real-Time Systems. Christoph Erhardt, Simon Kuhnle, Isabella Stilkerich, Wolfgang Schröder-Preikschat

Transcription:

Field Analysis Last time Exploit encapsulation to improve memory system performance This time Exploit encapsulation to simplify analysis Two uses of field analysis Escape analysis Object inlining April 8, 2015 Exploiting Encapsulation tosimplify Analysis 1

Motivation Performance Problems with Modern High Level Languages Bounds and type checks for safety Virtual method calls to support object-oriented semantics Heap allocation to provide uniform view of objects Solution Prove facts about array bounds and about types to tighten assumptions e.g. To devirtualize a call, prove that the call has exactly one target class Such analysis typically requires interprocedural analysis Costly Sometimes impossible: dynamic class loading, unavailable source code April 8, 2015 Exploiting Encapsulation tosimplify Analysis 2

Field Analysis A Cheap Form of Interprocedural Analysis Exploits encapsulation to limit the scope of analysis e.g. If an array is indexed by a private variable that is only set by one method, then only that one method needs to be analyzed to determine the index s value Deduce properties about fields based on the properties of all accesses to that field Benefits Efficient (10% overhead in compilation time) Does not require access to the entire program Works well with dynamic class loading Can be applied to any language that supports encapsulation Java, C++, Modula-3, etc. April 8, 2015 Exploiting Encapsulation tosimplify Analysis 3

Field Analysis for Java Today: A specific solution [Ghemawat, Randall, & Scales, PLDI 00] Implemented in the context of Compaq s Swift optimizing Java compiler Swift translates bytecode to native Alpha code Swift performs a number of aggressive optimizations This implementation focuses on reference types Ignores scalar fields April 8, 2015 Exploiting Encapsulation tosimplify Analysis 4

Field Modifiers Dictate Scope of Analysis Java field modifiers Class Field Where can the field be modified? public private containing class public package containing package public protected containing package and subclasses non-public private containing class non-public non-private containing package public public entire program April 8, 2015 Exploiting Encapsulation tosimplify Analysis 5

Example public class Plane { private Point[] points; public Plane() { points = new Point[3]; } } public int GetAverageColor() { return (points[0].getcolor() + points[1].getcolor() + points[2].getcolor())/3; } Since points is private Its properties can be determined by analyzing only the Plane class We can determine the exact type of points So we can inline the GetColor() method April 8, 2015 Exploiting Encapsulation tosimplify Analysis 6

Idea: Create an Enhanced Type System Introduce special types A value is an object of exactly class T (and not a subclass of T) A value is an array of some constant size The value is known to be non-null... Type analysis begins by determining types of Method arguments Loads of fields of objects Loads of global variables Non-null exact types assigned to newly allocated objects Use type propagation to determine types of other nodes in the SSA graph April 8, 2015 Exploiting Encapsulation tosimplify Analysis 7

Basic Approach 1. Initialize Build SSA graph and gather type information SSA provides flow-sensitivity 2. Incrementally update properties Consider all loads and stores and update properties associated with each field Load of a field: Analyze all uses of the load Store of a field: Analyze the value stored into the field and all other uses of the value x = y.f;... x.z(); x = new T;... y.f = x; Type of x? Type of y.f? April 8, 2015 Exploiting Encapsulation tosimplify Analysis 8

Examples of Useful Properties exact_type(field) The field is always assigned a value of the specified type always_init(field) The field is always initialized only_init(field) The field is only modified by constructors April 8, 2015 Exploiting Encapsulation tosimplify Analysis 9

Example Analysis public class Plane { private Point[] points; public Plane() { points = new Point[3]; } points is private, so its properties can be determined by only scanning the Plane class } public void SetPoint(Point p, int i) { points[i] = p; } public Point GetPoint(int i) { return points[i]; } only_init(points) is true exact_types(points) indicates a non-null array with base type Point and a constant size of 3 always_init(points) is true April 8, 2015 Exploiting Encapsulation tosimplify Analysis 10

Example Optimizations Precise type information supports a form of constant folding exact_type(field) If the type is precisely known, we can convert a virtual method call to a static method call Precise type information can be used to statically evaluate type-inclusion tests such as instanceof or array store checks If the type is an array of constant size, some bounds checks can be eliminated and expressions that use the array length (eg. a.length()) can be statically evaluated April 8, 2015 Exploiting Encapsulation tosimplify Analysis 11

Example Optimizations (cont) public class Plane { private Point[] points; public Plane() { points = new Point[3]; } What optimizations are possible in this example? Can eliminate null checks on points } public void SetPoint(Point p, int i) { points[i] = p; } public Point GetPoint(int i) { return points[i]; } Can use the constant 3 in bounds checks on points Can eliminate the array store check for points April 8, 2015 Exploiting Encapsulation tosimplify Analysis 12

Example Optimizations (cont) These properties can enhance other optimizations x = y.f; x.foo(); z = y.f; CSE? x = y.f; x.foo(); z = x; CSE is possible if x.foo does not modify y.f. We know that y.f is only modified by a constructor if only_init(f) = true April 8, 2015 Exploiting Encapsulation tosimplify Analysis 13

Outline Last time Exploit encapsulation to improve memory system performance This time Exploit encapsulation to simplify analysis Two uses of field analysis Escape analysis Object inlining April 8, 2015 Exploiting Encapsulation tosimplify Analysis 14

Escape Analysis Idea Does an object escape the method in which it is allocated? E.g., return, assign to global/heap, pass to another method f() { Point p = new Point(); Stack s = new Stack(100); s.push(p); /* p escapes */... return p; /* p escapes */ } April 8, 2015 Exploiting Encapsulation tosimplify Analysis 15

Escape Analysis Uses Objects that do not escape can be allocated on the stack f() { } Point p = new Point(); return; /* Allocate p on the stack */ Why is this desirable? Less overhead than heap allocation Less work for garbage collector Usually has better cache behavior Synchronization elimination Escape from a thread: Can another thread access the object? If an object cannot escape a thread, it need not be synchronized April 8, 2015 Exploiting Encapsulation tosimplify Analysis 16

Escape Analysis (cont) Heavyweight escape analysis Many proposed variations [Aldrich 99, Blanchet 99, Bogda 99, Choi 99, Whaley 99] Typically expensive interprocedural data-flow analysis Large flow values Connection Graphs represent points-to relationship among objects Simple escape analysis Simplifying assumption: Any object that is assigned into the heap or returned from a method escapes that method April 8, 2015 Exploiting Encapsulation tosimplify Analysis 17

Evaluation of Simple Escape Analysis Pros Extremely simple Inexpensive (analysis time is linear in code size) Cons Inaccurate Assignment to heap does not necessarily imply escape April 8, 2015 Exploiting Encapsulation tosimplify Analysis 18

Limitations of Simple Escape Analysis Consider the following code class Pair { } private Object first; private Object second; Pair p = new Pair(); Integer x = new Integer(5); p.first = x; Questions Is x assigned to the heap? Does x escape? Only if p escapes, since x is only assigned to an encapsulated field of p April 8, 2015 Exploiting Encapsulation tosimplify Analysis 19

Escape Analysis with Field Analysis Idea Identify encapsulated fields If an object does not escape, then the contents of its encapsulated fields do not escape Escape from a thread can be handled similarly by focusing on thread creation routines Conditions for identifying encapsulated fields (1) The value of the field does not escape through a method that accesses the field, and (2) Any value assigned to the field has not already escaped This is trivially true for newly-allocated objects April 8, 2015 Exploiting Encapsulation tosimplify Analysis 20

Field Properties for Escape Analysis Field Property: may_leak(field) Indicates whether the object in the field might escape the containing object Field Property: source_type(field) Describes the kind of values assigned to the field: new only assigned newly allocated objects new/null... or null new/null/param... or method parameters other A field, f, is encapsulated when may_leak(f) = false Condition (1) source_type(f) = new/null Condition (2) April 8, 2015 Exploiting Encapsulation tosimplify Analysis 21

Limitations of Simple Escape Analysis (reprise) Consider the following code class Pair { } private Object first; private Object second; Pair p = new Pair(); Integer x = new Integer(5); p.first = x; Questions Is x assigned to the heap? Yes Does x escape? Only if p escapes, since x is only assigned to an encapsulated field of p Check may_leak(p), source_type(p) April 8, 2015 Exploiting Encapsulation tosimplify Analysis 22

Outline Last time Exploit encapsulation to improve memory system performance This time Exploit encapsulation to simplify analysis Two uses of field analysis Escape analysis Object inlining April 8, 2015 Exploiting Encapsulation tosimplify Analysis 23

Object Inlining Idea Allocate storage for an object inside its containing object Example class Point { int x,y;... } class Ray { Point start; Point end;... } Direct allocation Ray Point header header start x end y Point header x y Inlined allocation Ray header x (start) y (start) x (end) y (end) Benefits? April 8, 2015 Exploiting Encapsulation tosimplify Analysis 24

Object Inlining (cont) Benefits Allows inlined objects to be accessed directly (i.e., without following pointers) Reduces the size of objects Reduces allocation/garbage-collection overheads May improve data cache performance (Inlined objects are likely to be accessed together) Bottom line Object inlining produces code closer to hand-tuned C April 8, 2015 Exploiting Encapsulation tosimplify Analysis 25

Object Representation and Inlining Objects contain headers Type of object Method table Synchronization state Needed for type checking, virtual method calls, synchronization Question: Does the header need to be preserved for inlined objects? Answer: No, if the following hold: There are no virtual method invocations, no synchronization, and no type inclusion checks on the object (i.e., we don t need it), and The object does not escape (i.e, no one else will need it) Otherwise, uses_header(field) = true Question: Can a compiler do this type of inlining in C++? Answer: No April 8, 2015 Exploiting Encapsulation tosimplify Analysis 26

Object Representation and Inlining (cont) Ray start end Point x y Inlining With Headers Ray Ray hdr Point hdr x (start) y (start) Point hdr Point x y Inlining Without Headers Ray Ray hrd x (start) y (start) x (end) x (end) y (end) y (end) April 8, 2015 Exploiting Encapsulation tosimplify Analysis 27

Object Inlining and Garbage Collection Question: What if an inlined object escapes and its enclosing object does not? Answer: Problem: the garbage collector might reclaim the enclosing object, which would also implicitly reclaim the inlined object Two approaches Do not inline objects that may escape Tag inlined objects (in their header) and make sure that the garbage collector does not collect the enclosing object if the inlined object is live April 8, 2015 Exploiting Encapsulation tosimplify Analysis 28

Object Inlining with Field Analysis Recall Field Property: source_type(field) Indicates the kind of values assigned to the field: new only assigned newly allocated objects new/null... or null new/null/param... or method parameters other For inlining we are interested in the first case We need to know the exact type of an object before we can inline it April 8, 2015 Exploiting Encapsulation tosimplify Analysis 29

Object Inlining with Field Analysis (cont) Do we need headers? Use the following properties to determine whether the header for inlined objects must be preserved Field Property: uses_header(field) Indicates whether the header for the object in the field might ever be used Field Property: may_leak(field) Indicates whether the object in the field might escape the containing object April 8, 2015 Exploiting Encapsulation tosimplify Analysis 30

Exploiting Field Analysis Properties A field f can be inlined with a header when always_init(f) = true, only_init(f) = true, The field is always initialized exactly once by a newly allocated object source_type(f) = new, and exact_type(f) = static_type(f) The final condition is a simplification It makes object layout easier for the JVM One layout for all inlined objects of the same static type April 8, 2015 Exploiting Encapsulation tosimplify Analysis 31

Exploiting Field Analysis (cont) A field f can be inlined without a header when It is can be inlined with a header, uses_header(f) = false, and may_leak(f) = false Can also inline arrays when The array satisfies the above constraints, and The array has a constant size April 8, 2015 Exploiting Encapsulation tosimplify Analysis 32

Object Inlining Transformation Transforming references to inlined objects pt = myray.start; Initializations pt = new Point; myray.start = pt; pt = myray + offset(myray, start); No allocation needed Possibly initialize header of myray.start pt = myray + offset(myray, start); Inlined Object myray Ray hdr Point hdr x (start) y (start) Point hdr x (end) y (end) April 8, 2015 Exploiting Encapsulation tosimplify Analysis 33

Limitations of Field Analysis Native methods Cannot analyze native methods Conservative assumption: Assume the native methods read and write all fields that they can access Weak consistency Some optimizations are not legal under weak consistency models on multiprocessors Race conditions may allow a thread to see a null value even if the always_init(field) is true Reflection Field properties can be modified through reflection (setaccessible()) Disable field analysis on such fields April 8, 2015 Exploiting Encapsulation tosimplify Analysis 34

Impact on Performance Run-time check elimination Many null-checks eliminated (0-50%) Some array bounds checks eliminated (0-60%) Not many cast checks eliminated (0-1%) Virtual method calls Significantly reduced Object inlining 0-11% performance improvement Stack allocation Escape information does not significantly assist stack allocation (for the benchmarks considered) April 8, 2015 Exploiting Encapsulation tosimplify Analysis 35

Impact on Performance (cont) Synchronization removal 0-90% reduction in dynamic synchronization Either helps a lot or helps very little Bottom line 0-27% performance improvement Average improvement of 7% April 8, 2015 Exploiting Encapsulation tosimplify Analysis 36

Concepts Escape analysis Useful for optimizing the allocation of objects Useful for removing unnecessary synchronization Object inlining Remove object overhead Improve data locality Field analysis Exploit encapsulation to simplify analysis Many uses De-virtualization Remove runtime checks Perform escape analysis Perform object inlining April 8, 2015 Exploiting Encapsulation tosimplify Analysis 37

Next Time Lecture Traditional uses of compilers April 8, 2015 Exploiting Encapsulation tosimplify Analysis 38