Reginf pldi3

47
11th June 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

Transcript of Reginf pldi3

11th June 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

2

Background

• Basic problem:• how to recover memory of dead objects?

• Two Approaches• garbage collection• explicit memory management

• Our Focus: • Explicit memory regions with different lifetimes

3

Current Status

Region Inference for a functional language • -Calculus [Tofte-Talpin POPL94]

Previous Works on Region Type Checkers: • Java [DIKU98], • Real-Time Java : [MIT-PLDI03], • Cyclone [Cornell-PLDI02].

Our Goal: automatic region inference for object-oriented programs

4

Two Major Assumptions

• Lexically-Scoped Regions: Stack-like lifetimes construct for local region :

letreg r in e

top (younger)

bottom (older)

r1

r2

r3

r4

• No Dangling References: Regions of components should have longer lifetimes than its object.

5

Main Contributions

• Region Inference for Core-Java

• Constraint-Based Type System.

• Region Polymorphism & Region Subtyping

• Handles Inheritance, Overriding, Downcast

• Implementation

6

Region-Annotated Core-Javaregion parameters for

polymorphism

constraint abstractions

class invariant

method precondition

region constraints

7

Inferring for Class

Step 1 : Reserve first region for the receiver object

(this).Step 2 : Keep regions of fields

distinct for precision.

Step 3 : No dangling property as class invariant.

Step 4 : Inherit constraint of superclass.

8

Inferring for Class

Step 1 : Reserve first region for the receiver object

(this).Step 2 : Keep regions of fields

distinct for precision.

Step 3 : No dangling requirement as class invariant.

9

Inferring for Methods

Step 3 : Localise regions that do not escape.

Step 4 : Fix-point analysis for recursion.

method precondition

Step 2 : Gather constraints from method body.

Step 1 : Region parameters from method

signature.

10

Outline

• Background• Key Techniques

• Inferring Class• Inferring Methods• Region Subtyping

• Region Inference• Method Overriding + Downcast• Experimental Results

11

(Object) Region Subtypingconcept pioneered in Cyclone [PLDI02]

an object w of type c<r,..> can be passed to a location v of type c<s,..> provided r º s (e.g. in assignment and parameter/result passing)

12

Example

Without region subtyping, regions of a and b are forced to be equal.

With region subtyping, these regions may be distinct.

13

Problem – Recursive Fields

Recursive nodes being forced into the same region

Some programs work better with recursive nodes in different regions …

14

(Field) Region Subtyping

SOLUTION : Covariance for Read-Only Fields.

Example:

read-onlymutable

15

Outline

• Background

• Key Techniques

• Region Inference

• Method Overriding + Downcast

• Experimental Results

16

Region Inference

• Main type inference rules

• Apply inference according to global dependency graph (class,call,override dependencies).

17

Inference Rule : Sequence

Inference is flow-insensitive but can be improved by SSA form

18

Inference Rule : Expression Block

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

Regions of rs coalesced into a single localised region r

19

Example – Localised Region

20

Example – Localised Region

localised region r

21

Example : Recursive Function

22

Outline

• Background

• Key Techniques

• Region Inference

• Overriding + Downcast

• Experimental Results

23

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 { }

Dynamic Dispatch : Allows B.mn may be invoked where A.mn is expected

Method Override Rule: CB Æ MA ) MB

24

Override Conflict Resolution

Derived constraints CB MA MB may conflict with method override rule.

Solution : strengthen CB , MA to C’B , M’A

Until: C’B Æ M’A ) MB

25

Downcast Safety

• Compile-time guarantee to downcast safety.

• Problem : regions are lost during upcast that have to be recovered for downcast.

Our Solution:• Selectively maintain extra regions that are lost

during upcast and may be needed by downcast.• Use flow analysis

26

Experiments

• Prototype was built using Haskell.• Inference is fast and takes <5s for the

Olden benchmark.

27

Experiments

• Inference is competitive to hand-annotation (RegJava benchmark)• similar localised regions • same space recovery

• Region subtyping gives better space recovery for some examples.

28

Concluding Remarks

• Constraint-based region inference system:• Region subtyping.

• Region polymorphism.• Polymorphic recursion

• Handles OO features:• class subtyping• method overriding• downcast safety

• Implementation is fast and precise.

29

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

30

Correctness

31

Core-Java

32

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.

33

No Region Subtyping

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

34

No Region Subtyping

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

35

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.

36

Example - Reynolds3

With field region subtyping, the List(x,p) object can be allocated in a local region of the recursive call.

37

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

38

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<a1,a2,a3> p = (Pair) o

• Key technique : maintain extra regions that were lost during upcasting and may be needed!

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

• Solution : Flow analysis

39

Region Subtyping

concept from [Cyclone PLDI02]

allows v=w where location v::c<s,..> can be passed an object w of type c<r,..> provided r

s

40

Experiments

• Inference is competitive hand-annotation.• Prototype is fast (< 5s for upto 1000 lines)• http://loris-4.ddns.comp.nus.edu.sg/~craciunm

41

Experiments

42

Inference of Methods

class invariant

method precondition

43

Inferring for Class

class invariant to prevent dangling references

44

List Example

Key Principles• First region is for only object of the class.• Keep regions of fields distinct for precision.

45

Inference of Methods

Key Principles• Region polymorphism from parameters/result• Method precondition is applicable to only

those objects that may invoke the method.

46

Example – Cyclic Structure

p1 p2

47

Inference of Methods

method precondition