Top Banner
11th Nov 2004 PLDI 2004 1 Region Inference for an Object-Oriented Language Wei Ngan Chin 1,2 Joint work with Florin Craciun 1 , Shengchao Qin 1,2 , Martin Rinard 3 1 National University of Singapore 2 Singapore-MIT Alliance 3 MIT
36

11th Nov 2004PLDI 20041 Region Inference for an Object-Oriented Language Wei Ngan Chin 1,2 Joint work with Florin Craciun 1, Shengchao Qin 1,2, Martin.

Jan 17, 2016

Download

Documents

Cameron Stokes
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 11th Nov 2004PLDI 20041 Region Inference for an Object-Oriented Language Wei Ngan Chin 1,2 Joint work with Florin Craciun 1, Shengchao Qin 1,2, Martin.

11th Nov 2004 PLDI 2004 1

Region Inference for an Object-Oriented Language

Wei Ngan Chin1,2

Joint work with

Florin Craciun1, Shengchao Qin1,2, Martin Rinard3

1 National University of Singapore2 Singapore-MIT Alliance 3 MIT

Page 2: 11th Nov 2004PLDI 20041 Region Inference for an Object-Oriented Language Wei Ngan Chin 1,2 Joint work with Florin Craciun 1, Shengchao Qin 1,2, Martin.

2

Background

• Problem:• objects have different lifetimes• garbage collection can be expensive

• One Solution: • Use regions with different lifetimes• Avoid dangling references.

• Previous Works : Region checkers for Java [DIKU98], Real-Time Java : [Boyapati et al PLDI03], C [Cyclone PLDI02].

• Our Goal: auto. region inference for OO paradigm

Page 3: 11th Nov 2004PLDI 20041 Region Inference for an Object-Oriented Language Wei Ngan Chin 1,2 Joint work with Florin Craciun 1, Shengchao Qin 1,2, Martin.

3

Assumptions

• Lexically-Scoped Regions:• LIFO behaviour construct : letreg r in e

• No-Dangling References: • Regions of fields must outlive the region of its enclosing object.

top (younger)

bottom (older)

Page 4: 11th Nov 2004PLDI 20041 Region Inference for an Object-Oriented Language Wei Ngan Chin 1,2 Joint work with Florin Craciun 1, Shengchao Qin 1,2, Martin.

4

Main Contributions

• Region Inference for Core-Java• Constraint-Based Type System.• Region Polymorphism & Region Subtyping• Handles Inheritance, Overriding, Downcast• Implementation

Page 5: 11th Nov 2004PLDI 20041 Region Inference for an Object-Oriented Language Wei Ngan Chin 1,2 Joint work with Florin Craciun 1, Shengchao Qin 1,2, Martin.

5

Core-Java

Page 6: 11th Nov 2004PLDI 20041 Region Inference for an Object-Oriented Language Wei Ngan Chin 1,2 Joint work with Florin Craciun 1, Shengchao Qin 1,2, Martin.

6

Region-Annotated Core-Java

region types, parameters, constraints, letreg

Page 7: 11th Nov 2004PLDI 20041 Region Inference for an Object-Oriented Language Wei Ngan Chin 1,2 Joint work with Florin Craciun 1, Shengchao Qin 1,2, Martin.

7

Inferring for Class

Page 8: 11th Nov 2004PLDI 20041 Region Inference for an Object-Oriented Language Wei Ngan Chin 1,2 Joint work with Florin Craciun 1, Shengchao Qin 1,2, Martin.

8

Key Principles : Classes

• Region type : chr1,…, rni

• r1 : (region for current object of this type)

• r2,…,rn (regions for components/fields)

• rk º r1, 8 k 2 2..n (no dangling references)

• First region is special.

• Keep regions of components distinct.

Page 9: 11th Nov 2004PLDI 20041 Region Inference for an Object-Oriented Language Wei Ngan Chin 1,2 Joint work with Florin Craciun 1, Shengchao Qin 1,2, Martin.

9

List Example

Page 10: 11th Nov 2004PLDI 20041 Region Inference for an Object-Oriented Language Wei Ngan Chin 1,2 Joint work with Florin Craciun 1, Shengchao Qin 1,2, Martin.

10

Key Principles : Methods

• Region polymorphism from the region types of parameters and output • t mn hr*i ((t v)*) where rc e• r* captures region polymorphism

• Keep region constraints of classes and methods separate.

Page 11: 11th Nov 2004PLDI 20041 Region Inference for an Object-Oriented Language Wei Ngan Chin 1,2 Joint work with Florin Craciun 1, Shengchao Qin 1,2, Martin.

11

Inference of Methods

class invariant

method precondition

Page 12: 11th Nov 2004PLDI 20041 Region Inference for an Object-Oriented Language Wei Ngan Chin 1,2 Joint work with Florin Craciun 1, Shengchao Qin 1,2, Martin.

12

Outline

• Background• Key Principles• Region Subtyping• Region Inference• Method Overriding + Downcast• Experimental Results

Page 13: 11th Nov 2004PLDI 20041 Region Inference for an Object-Oriented Language Wei Ngan Chin 1,2 Joint work with Florin Craciun 1, Shengchao Qin 1,2, Martin.

13

No Region Subtyping

[Boyapati et al PLDI03] supports class subtyping but not region subtyping

Page 14: 11th Nov 2004PLDI 20041 Region Inference for an Object-Oriented Language Wei Ngan Chin 1,2 Joint work with Florin Craciun 1, Shengchao Qin 1,2, Martin.

14

(Object) Region Subtyping

concept from [Cyclone PLDI02]

A variable location v::c<s,..> can be passed an object of type c<r,..> provided r s

Page 15: 11th Nov 2004PLDI 20041 Region Inference for an Object-Oriented Language Wei Ngan Chin 1,2 Joint work with Florin Craciun 1, Shengchao Qin 1,2, Martin.

15

Example

Variables a and b are assigned to tmp.

Without region subtyping, their regions are equal.

With region subtyping, their regions may be distinct.

Page 16: 11th Nov 2004PLDI 20041 Region Inference for an Object-Oriented Language Wei Ngan Chin 1,2 Joint work with Florin Craciun 1, Shengchao Qin 1,2, Martin.

16

Problem – Recursive Fields

Recursive nodes being forced into the same region

Some programs permit recursive nodes to be in different regions …..

Page 17: 11th Nov 2004PLDI 20041 Region Inference for an Object-Oriented Language Wei Ngan Chin 1,2 Joint work with Florin Craciun 1, Shengchao Qin 1,2, Martin.

17

Example - Reynolds3

Escape Analysis [Deutsch:POPL97] allows the List(x,p) object to be build on runtime stack. This effect is equivalent to local region at each recursive call.

Page 18: 11th Nov 2004PLDI 20041 Region Inference for an Object-Oriented Language Wei Ngan Chin 1,2 Joint work with Florin Craciun 1, Shengchao Qin 1,2, Martin.

18

(Field) Region SubtypingOur Extension : Covariance for Read-Only Fields.

Page 19: 11th Nov 2004PLDI 20041 Region Inference for an Object-Oriented Language Wei Ngan Chin 1,2 Joint work with Florin Craciun 1, Shengchao Qin 1,2, Martin.

19

Outline

• Background• Key Principles• Region Subtyping• Region Inference• Method Overriding + Downcast• Experimental Results

Page 20: 11th Nov 2004PLDI 20041 Region Inference for an Object-Oriented Language Wei Ngan Chin 1,2 Joint work with Florin Craciun 1, Shengchao Qin 1,2, Martin.

20

Region Inference

• Format of type inference rules

• Flow-insensitive but context sensitive.• Modular inference according to dependency

graph.

Page 21: 11th Nov 2004PLDI 20041 Region Inference for an Object-Oriented Language Wei Ngan Chin 1,2 Joint work with Florin Craciun 1, Shengchao Qin 1,2, Martin.

21

Inference Rule for Block

rs: the set of regions that do not escape the block

They are coalesced into a single localised region r

Page 22: 11th Nov 2004PLDI 20041 Region Inference for an Object-Oriented Language Wei Ngan Chin 1,2 Joint work with Florin Craciun 1, Shengchao Qin 1,2, Martin.

22

Example – Localised Region

Page 23: 11th Nov 2004PLDI 20041 Region Inference for an Object-Oriented Language Wei Ngan Chin 1,2 Joint work with Florin Craciun 1, Shengchao Qin 1,2, Martin.

23

Example – Localised Region

Page 24: 11th Nov 2004PLDI 20041 Region Inference for an Object-Oriented Language Wei Ngan Chin 1,2 Joint work with Florin Craciun 1, Shengchao Qin 1,2, Martin.

24

Example – Cyclic Structure

p1 p2

Page 25: 11th Nov 2004PLDI 20041 Region Inference for an Object-Oriented Language Wei Ngan Chin 1,2 Joint work with Florin Craciun 1, Shengchao Qin 1,2, Martin.

25

Outline

• Background• Key Principles• Region Subtyping• Region Inference• Overriding + Downcast• Experimental Results

Page 26: 11th Nov 2004PLDI 20041 Region Inference for an Object-Oriented Language Wei Ngan Chin 1,2 Joint work with Florin Craciun 1, Shengchao Qin 1,2, Martin.

26

Method Overriding

class Bhr1 .. rni extends Ahr1 .. rmi where CB

In class A: Y mnhr’1,..,r’pi (X) where MA { }

In class B: Y mnhr’1,..,r’pi (X) where MB { }

Function Subtyping Rule:

Method Override Rule: CB Æ MA ) MB

argument resultselection

Page 27: 11th Nov 2004PLDI 20041 Region Inference for an Object-Oriented Language Wei Ngan Chin 1,2 Joint work with Florin Craciun 1, Shengchao Qin 1,2, Martin.

27

Override Conflict Resolution

If method override rule do not hold for: CB MA MB

Can strengthen CB , MA to C’B , M’A using:

Until: C’B Æ M’A ) MB

CB, MA, MB ` C’B, M’A

Page 28: 11th Nov 2004PLDI 20041 Region Inference for an Object-Oriented Language Wei Ngan Chin 1,2 Joint work with Florin Craciun 1, Shengchao Qin 1,2, Martin.

28

Downcast Safety

• In our framework :• regions may be lost during upcast

e.g. Object<r1> o = new Pair<s1,s2,s3> (…)

• lost regions must be recovered during downcast.e.g. Pair<t1,t2,t3> p = (Pair) o

• Key technique : maintain extra regions that were supposedly lost during upcasting!

e.g. Object<r1>[r2,r3] p = new Pair<s1,s2,s3> (…)

• Solution : Flow analysis

Page 29: 11th Nov 2004PLDI 20041 Region Inference for an Object-Oriented Language Wei Ngan Chin 1,2 Joint work with Florin Craciun 1, Shengchao Qin 1,2, Martin.

29

Experiments

• Region annotation needed in 12% of the code.• Comparable space reuse for all programs. • Inference is as good as hand-annotation.• On Olden benchmark, inference takes < 5s.

Page 30: 11th Nov 2004PLDI 20041 Region Inference for an Object-Oriented Language Wei Ngan Chin 1,2 Joint work with Florin Craciun 1, Shengchao Qin 1,2, Martin.

30

Concluding Remarks

• Automatic region inference for CoreJava.• Support classes and methods with region

polymorphism.• Supports region subtyping.• Supports OO features : class subtyping,

method overriding, and downcast safety.• Implementation

Page 31: 11th Nov 2004PLDI 20041 Region Inference for an Object-Oriented Language Wei Ngan Chin 1,2 Joint work with Florin Craciun 1, Shengchao Qin 1,2, Martin.

31

Future Work

• Better Lifetime Precision• variable liveness analysis• flow sensitivity via SSA• treat null as a primitive value

• Memory Efficiency : Sized Regions + Reuse

• Extensions : Genericity + RTSJ

Page 32: 11th Nov 2004PLDI 20041 Region Inference for an Object-Oriented Language Wei Ngan Chin 1,2 Joint work with Florin Craciun 1, Shengchao Qin 1,2, Martin.

32

Inference Rule for Sequence

Inference rule is flow-insensitive.

Improvement possible via SSA.

Page 33: 11th Nov 2004PLDI 20041 Region Inference for an Object-Oriented Language Wei Ngan Chin 1,2 Joint work with Florin Craciun 1, Shengchao Qin 1,2, Martin.

33

Correctness

Page 34: 11th Nov 2004PLDI 20041 Region Inference for an Object-Oriented Language Wei Ngan Chin 1,2 Joint work with Florin Craciun 1, Shengchao Qin 1,2, Martin.

34

Example : Fixed-Point Analysis

Page 35: 11th Nov 2004PLDI 20041 Region Inference for an Object-Oriented Language Wei Ngan Chin 1,2 Joint work with Florin Craciun 1, Shengchao Qin 1,2, Martin.

35

Inference of Methods

Page 36: 11th Nov 2004PLDI 20041 Region Inference for an Object-Oriented Language Wei Ngan Chin 1,2 Joint work with Florin Craciun 1, Shengchao Qin 1,2, Martin.

36

Experiments

• Region inference is fast for large programs too.

• Our prototype was built using Glasgow Haskell Compiler.