Type Systems for Region-based Memory Management

133
Type Systems for Region- based Memory Management Matthew Fluet Greg Morrisett & Amal Ahmed Harvard University

description

Type Systems for Region-based Memory Management. Matthew Fluet Greg Morrisett & Amal Ahmed Harvard University. Memory Management. Dynamic allocation pervasive in computation. Memory Management. Dynamic allocation pervasive in computation Range of methods for managing memory. - PowerPoint PPT Presentation

Transcript of Type Systems for Region-based Memory Management

Page 1: Type Systems for Region-based Memory Management

Type Systems for Region-based Memory Management

Matthew Fluet

Greg Morrisett & Amal Ahmed

Harvard University

Page 2: Type Systems for Region-based Memory Management

2

Memory Management

Dynamic allocation pervasive in computation

Page 3: Type Systems for Region-based Memory Management

3

Memory Management

Dynamic allocation pervasive in computation Range of methods for managing memory

Page 4: Type Systems for Region-based Memory Management

4

Memory Management

Dynamic allocation pervasive in computation Range of methods for managing memory

– malloc/free efficient, but tedious and error prone

Page 5: Type Systems for Region-based Memory Management

5

Memory Management

Dynamic allocation pervasive in computation Range of methods for managing memory

– malloc/free efficient, but tedious and error prone

– garbage collection transparent and safe, but (can be) inefficient

Page 6: Type Systems for Region-based Memory Management

6

Memory Management

Dynamic allocation pervasive in computation Range of methods for managing memory

– malloc/free efficient, but tedious and error prone

– regions

– garbage collection transparent and safe, but (can be) inefficient

Page 7: Type Systems for Region-based Memory Management

7

Region-based Memory Management

Operationally– Memory is divided regions (denoted by r, , …)– Objects are individually allocated in a region– All objects in a region are deallocated together

Page 8: Type Systems for Region-based Memory Management

8

Region-based Memory Management

Runtime Organization– Regions are linked lists of pages– Arbitrary intra- and inter-region references– Similar to arena-style allocators

r1

r2

r3

Region handles

Page 9: Type Systems for Region-based Memory Management

9

Application: Cyclone

Cyclone Safe-C Project– type-safety– with the “virtues” of C

low-level interface with manifest cost model

Page 10: Type Systems for Region-based Memory Management

10

Application: Cyclone

Cyclone Safe-C Project– type-safety– with the “virtues” of C

low-level interface with manifest cost model

– range of memory management options regions are an organizing principle

Page 11: Type Systems for Region-based Memory Management

11

Application: Cyclone

MediaNET– TCP benchmark (packet forwarding)– Cyclone v.0.1

High water mark: 840 KB 130 collections Basic throughput: 50 MB/s

– Cyclone v.0.5 High water mark: 8 KB 0 collections Basic throughput: 74MB/s

Page 12: Type Systems for Region-based Memory Management

12

Cyclone: Regions

Region varietyAllocation

(objects)

DeallocationAliasing

(objects)(what) (when)

Stack static

whole region

exit of lexical scope

unrestricted

Lexical

dynamic

Dynamic manual

Heap (`H)

single objects

automatic(BDW GC)

Unique (`U)manual restricted

Ref-counted (`RC)

Page 13: Type Systems for Region-based Memory Management

13

Cyclone: Regions

Region varietyAllocation

(objects)

DeallocationAliasing

(objects)(what) (when)

Stack static

whole region

exit of lexical scope

unrestricted

Lexical

dynamic

Dynamic manual

Heap (`H)

single objects

automatic (BDW GC)

Unique (`U)manual restricted

Ref-counted (`RC)

Meta-theory of Cyclone is a nightmare!!

Page 14: Type Systems for Region-based Memory Management

14

Cyclone: Regions

Region varietyAllocation

(objects)

DeallocationAliasing

(objects)(what) (when)

Stack static

whole region

exit of lexical scope

unrestricted

Lexical

dynamic

Dynamic manual

Heap (`H)

single objects

automatic (BDW GC)

Unique (`U)manual restricted

Ref-counted (`RC)

Ultimate Goal: simple model where we can easily encode the key features of Cyclone in a target

language with simpler meta-theory

Page 15: Type Systems for Region-based Memory Management

15

Cyclone: Regions

Region varietyAllocation

(objects)

DeallocationAliasing

(objects)(what) (when)

Stack static

whole region

exit of lexical scope

unrestricted

Lexical

dynamic

Dynamic manual

Heap (`H)

single objects

automatic (BDW GC)

Unique (`U)manual restricted

Ref-counted (`RC)

Today’s Goal: Three type systems for region-based languages,

culminating with a fairly good approximation of Cyclone’s features

Page 16: Type Systems for Region-based Memory Management

16

Outline

Introduction Type-and-Effect System (Tofte-Talpin) Monadic Type System (FRGN)

– Translation Sketch

Substructural Type System (rgnURAL)– Translation Sketch

Conclusion

Page 17: Type Systems for Region-based Memory Management

17

Type Systems for Regions

Memory is divided into regions– type of handle for region r

hnd r

Page 18: Type Systems for Region-based Memory Management

18

Type Systems for Regions

Memory is divided into regions– type of handle for region r

hnd r

Objects are individually allocated in a region– operations: new, read, write– type of object of type allocated in region r

ref r

Page 19: Type Systems for Region-based Memory Management

19

Tofte-Talpin Region Calculus [’94]

Regions are created and destroyedwith a lexically scoped construct:

letregion ,h in e All objects in region are deallocated

together at the end of ’s scope

Page 20: Type Systems for Region-based Memory Management

20

Tofte-Talpin Region Calculus [’94]

Regions are created and destroyedwith a lexically scoped construct:

letregion ,h in e All objects in region are deallocated

together at the end of ’s scope

Regions have LIFO lifetimes Live regions can be organized as a stack

Page 21: Type Systems for Region-based Memory Management

21

Tofte-Talpin Region Calculus [’94]

Regions are created and destroyedwith a lexically scoped construct

Page 22: Type Systems for Region-based Memory Management

22

Tofte-Talpin Region Calculus [’94]

Regions are created and destroyedwith a lexically scoped construct

1

letregion 1,h1 in

let a = new h1 1 in

let c = letregion 2,h2 in

let b = new h2 7 in

new h1 (read a + read b) in

… c …

Page 23: Type Systems for Region-based Memory Management

23

Tofte-Talpin Region Calculus [’94]

Regions are created and destroyedwith a lexically scoped construct

1 a : 1

letregion 1,h1 in

let a = new h1 1 in

let c = letregion 2,h2 in

let b = new h2 7 in

new h1 (read a + read b) in

… c … input allocated in first region

Page 24: Type Systems for Region-based Memory Management

24

Tofte-Talpin Region Calculus [’94]

Regions are created and destroyedwith a lexically scoped construct

1 a : 1

2

letregion 1,h1 in

let a = new h1 1 in

let c = letregion 2,h2 in

let b = new h2 7 in

new h1 (read a + read b) in

… c … input allocated in first region

Page 25: Type Systems for Region-based Memory Management

25

Tofte-Talpin Region Calculus [’94]

Regions are created and destroyedwith a lexically scoped construct

1 a : 1

2 b : 7letregion 1,h1 in

let a = new h1 1 in

let c = letregion 2,h2 in

let b = new h2 7 in

new h1 (read a + read b) in

… c …

temporary allocated in second region

inputallocated in first region

Page 26: Type Systems for Region-based Memory Management

26

Tofte-Talpin Region Calculus [’94]

Regions are created and destroyedwith a lexically scoped construct

1

2

a : 1

c : 8

b : 7letregion 1,h1 in

let a = new h1 1 in

let c = letregion 2,h2 in

let b = new h2 7 in

new h1 (read a + read b) in

… c …

temporary allocated in second region

input and outputallocated in first region

Page 27: Type Systems for Region-based Memory Management

27

Tofte-Talpin Region Calculus [’94]

Regions are created and destroyedwith a lexically scoped construct

1 a : 1

c : 8

temporary allocated in second region

input and outputallocated in first region

letregion 1,h1 in

let a = new h1 1 in

let c = letregion 2,h2 in

let b = new h2 7 in

new h1 (read a + read b) in

… c …

Page 28: Type Systems for Region-based Memory Management

28

Type-and-Effect System

Track the set of regions accessed by a computation:

` e : ,

Function types include a latent effect:

1 ! 2

The role of is to tell us when it is not safe to deallocate a region

Page 29: Type Systems for Region-based Memory Management

29

Type-and-Effect System

Typing rule for letregion is subtle:

,h:hnd ` e : , ∉ frv(,) ` letregion ,h in e : , \ {}

Page 30: Type Systems for Region-based Memory Management

30

Type-and-Effect System

Typing rule for letregion is subtle:

,h:hnd ` e : , ∉ frv(,) ` letregion ,h in e : , \ {}

Typing rule for effect weakening:

` e : , µ ’

` e : , ’

Page 31: Type Systems for Region-based Memory Management

31

Type-and-Effect System

Effects are pervasive in typing rules:

` e1 : int, 1 ` e2 : int, 2

` e1 + e2 : int, 1 [ 2

` eh : hnd , h ` e : ,

` new eh e : ref , h [ [ {}

Page 32: Type Systems for Region-based Memory Management

32

Type-and-Effect System

Type-and-effects system ensures safety

Page 33: Type Systems for Region-based Memory Management

33

Type-and-Effect System

Type-and-effects system ensures safety But adds complications:

– Typing rule for letregion is subtle(due to the interplay of dangling pointers and effects)

– Effect weakening and region subtyping– Effects correspond to sets of regions

(term equality no longer suffices for type checking)

Page 34: Type Systems for Region-based Memory Management

34

Monadic Type Systems

Monadic encapsulation of effects [L-PJ 94]– Embed imperative features in pure languages

Page 35: Type Systems for Region-based Memory Management

35

Monadic Type Systems

Monadic encapsulation of effects [L-PJ 94]– Embed imperative features in pure languages

Types

ST s STRef s Operations

returnST :: 8s,. ! ST s thenST :: 8s,, ST s ! ( ! ST s !ST s newSTRef :: 8s,. ! ST s (STRef s )

readSTRef :: 8s,. STRef s ! ST s writeSTRef :: 8s,. STRef s ! ! ST s 1

Page 36: Type Systems for Region-based Memory Management

36

Monadic Type Systems

Monadic encapsulation of effects [L-PJ 94]– Embed imperative features in pure languages

runST :: 8. (8s. ST s ) !

Polymorphism over store index type ensures that the computation (and the result) are independent of the initial (and final) store

Page 37: Type Systems for Region-based Memory Management

37

Monadic Type Systems

Monadic encapsulation of effects [L-PJ 94]– Embed imperative features in pure languages– Polymorphic type system ensures safety

Well understood meta-theory Simplicity of System F type system

Page 38: Type Systems for Region-based Memory Management

38

FRGN = System F + RGN monad

System F

Monadic sub-language

Page 39: Type Systems for Region-based Memory Management

39

RGN monad: Types

Monadic types

Page 40: Type Systems for Region-based Memory Management

40

RGN monad: Types

Monadic types

RGN –

computations in stack of regions returning values of type ;a “stack” transformer

Page 41: Type Systems for Region-based Memory Management

41

RGN monad: Types

Monadic types

Hnd –

handles for the region

at the top of the stack of regions

Page 42: Type Systems for Region-based Memory Management

42

RGN monad: Types

Monadic types

Ref –

values of type allocated in region

at the top of the stack of regions

Page 43: Type Systems for Region-based Memory Management

43

RGN monad: Operations

Monadic unit and bind

returnRGN ::

8,. ! RGN

thenRGN ::

8,,. RGN ! ( ! RGN ) ! RGN

Page 44: Type Systems for Region-based Memory Management

44

RGN monad: Operations

Monadic unit and bind

returnRGN ::

8,. ! RGN

thenRGN ::

8,,. RGN ! ( ! RGN ) ! RGN

Page 45: Type Systems for Region-based Memory Management

45

RGN monad: Operations

Monadic unit and bind

returnRGN ::

8,. ! RGN

thenRGN ::

8,,. RGN ! ( ! RGN ) ! RGN

Page 46: Type Systems for Region-based Memory Management

46

RGN monad: Operations

Create and read region allocated values

new ::

8,. Hnd ! ! RGN (Ref )

read ::

8,. Ref ! RGN

Page 47: Type Systems for Region-based Memory Management

47

RGN monad: Operations

Create and read region allocated values

new ::

8,. Hnd ! ! RGN (Ref )

read ::

8,. Ref ! RGN

Page 48: Type Systems for Region-based Memory Management

48

RGN monad: Encapsulation

Encapsulate and run a monadic computation

runRGN ::

8. (8. RGN ) !

Page 49: Type Systems for Region-based Memory Management

49

RGN monad: Encapsulation

Encapsulate and run a monadic computation

runRGN ::

8. (8. RGN ) !

Page 50: Type Systems for Region-based Memory Management

50

RGN monad: Encapsulation

Encapsulate and run a monadic computation

runRGN ::

8. (8. RGN ) !

“for all stacks” ) no assumptions about

stack of regions

Page 51: Type Systems for Region-based Memory Management

51

RGN monad: Encapsulation

Encapsulate and run a monadic computation

runRGN ::

8. (8. RGN ) !

“for all stacks” ) no assumptions about

stack of regions

Page 52: Type Systems for Region-based Memory Management

52

RGN monad: Encapsulation

Encapsulate and run a monadic computation

runRGN ::

8. (8. RGN ) !

result is independent of stack ) 62 frv() )

region values don’t escape

“for all stacks” ) no assumptions about

stack of regions

Page 53: Type Systems for Region-based Memory Management

53

RGN monad: Regions

Regions are created and destroyedwith a lexically scoped construct

letRGN ::

81,. (82. Hnd 2 ! RGN 2 ) ! RGN 1

Page 54: Type Systems for Region-based Memory Management

54

RGN monad: Regions

Regions are created and destroyedwith a lexically scoped construct

letRGN ::

81,. (82. Hnd 2 ! RGN 2 ) ! RGN 1

result is independent of stack ) 2 62 frv(RGN 1 ) )

region values don’t escape

“for all stacks” ) no assumptions about

stack of regions

Page 55: Type Systems for Region-based Memory Management

55

RGN monad: Regions

Regions are created and destroyedwith a lexically scoped construct

letRGN ::

81,. (82. Hnd 2 ! RGN 2 ) ! RGN 1

result is independent of stack ) 2 62 frv(RGN 1 ) )

region values don’t escape

“for all stacks” ) no assumptions about

stack of regions

But, want to assume that 1 · 2 (1:: == 2)

Page 56: Type Systems for Region-based Memory Management

56

RGN monad: Witnesses

Witness type

Pf(1 · 2) –

proof that the stack of regions 1

is a substack of the stack of regions 2

Page 57: Type Systems for Region-based Memory Management

57

RGN monad: Witnesses

Witness operations

coerceRGN ::

81,2,. Pf(1 · 2) ! RGN 1 ! RGN 2

transSub ::

81,2,3. Pf(1 · 2) ! Pf(2 · 3)

! Pf(1 · 3)

Page 58: Type Systems for Region-based Memory Management

58

RGN monad: Regions

Regions are created and destroyedwith a lexically scoped construct

letRGN ::

81,. (82. Pf(1 · 2) ! Hnd 2 ! RGN 2 )! RGN 1

Page 59: Type Systems for Region-based Memory Management

59

RGN monad: Regions

Regions are created and destroyedwith a lexically scoped construct

letRGN ::

81,. (82. Pf(1 · 2) ! Hnd 2 ! RGN 2 )! RGN 1

Page 60: Type Systems for Region-based Memory Management

60

Translation: TTRC to FRGN

Type- and meaning-preserving translation from Tofte-Talpin Region Calculus to FRGN

Page 61: Type Systems for Region-based Memory Management

61

Translation: TTRC to FRGN

Type- and meaning-preserving translation from Tofte-Talpin Region Calculus to FRGN

« (ref 1 int) ! (ref 3 int) ¬ )

8. (Pf(1 · ) £ Pf(2 · ) £ Pf(3 · )) !

Ref 1 int ! RGN (Ref 3 int)

{1,2,3}

Page 62: Type Systems for Region-based Memory Management

62

Translation: TTRC to FRGN

Type- and meaning-preserving translation from Tofte-Talpin Region Calculus to FRGN

« (ref 1 int) ! (ref 3 int) ¬ )

8. (Pf(1 · ) £ Pf(2 · ) £ Pf(3 · )) !

Ref 1 int ! RGN (Ref 3 int)

{1,2,3}

Page 63: Type Systems for Region-based Memory Management

63

Translation: TTRC to FRGN

Type- and meaning-preserving translation from Tofte-Talpin Region Calculus to FRGN

« (ref 1 int) ! (ref 3 int) ¬ )

8. (Pf(1 · ) £ Pf(2 · ) £ Pf(3 · )) !

Ref 1 int ! RGN (Ref 3 int)

{1,2,3}

Page 64: Type Systems for Region-based Memory Management

64

Translation: TTRC to FRGN

Type- and meaning-preserving translation from Tofte-Talpin Region Calculus to FRGN

« (ref 1 int) ! (ref 3 int) ¬ )

8. (Pf(1 · ) £ Pf(2 · ) £ Pf(3 · )) !

Ref 1 int ! RGN (Ref 3 int)

{1,2,3}

Page 65: Type Systems for Region-based Memory Management

65

Translation: TTRC to FRGN

Type- and meaning-preserving translation from Tofte-Talpin Region Calculus to FRGN

« (ref 1 int) ! (ref 3 int) ¬ )

8. (Pf(1 · ) £ Pf(2 · ) £ Pf(3 · )) !

Ref 1 int ! RGN (Ref 3 int)

{1,2,3}

?

Page 66: Type Systems for Region-based Memory Management

66

Translation: TTRC to FRGN

Type- and meaning-preserving translation from Tofte-Talpin Region Calculus to FRGN

« (ref 1 int) ! (ref 3 int) ¬ )

8. (Pf(1 · ) £ Pf(2 · ) £ Pf(3 · )) !

Ref 1 int ! RGN (Ref 3 int)

{1,2,3}

Page 67: Type Systems for Region-based Memory Management

67

Translation: TTRC to FRGN

Type- and meaning-preserving translation from Tofte-Talpin Region Calculus to FRGN

« (ref 1 int) ! (ref 3 int) ¬ )

8. (Pf(1 · ) £ Pf(2 · ) £ Pf(3 · )) !

Ref 1 int ! RGN (Ref 3 int)

{1,2,3}

Page 68: Type Systems for Region-based Memory Management

68

Translation: TTRC to FRGN

Type- and meaning-preserving translation from Tofte-Talpin Region Calculus to FRGN

«letregion ,h in e¬ )

Page 69: Type Systems for Region-based Memory Management

69

Translation: TTRC to FRGN

Type- and meaning-preserving translation from Tofte-Talpin Region Calculus to FRGN

«letregion ,h in e¬ )

letRGN (.w.h. «e¬)

Page 70: Type Systems for Region-based Memory Management

70

Limitations of LIFO Regions

Lexical scope is ill-suited for– iterative computations

Conway’s Game of Life; copying GC

– CPS-based computations– event-based computations

Page 71: Type Systems for Region-based Memory Management

71

Limitations of LIFO Regions

Lexical scope is ill-suited for– iterative computations

Conway’s Game of Life; copying GC

– CPS-based computations– event-based computations

But, lexical scope was ensuring thatthe stack of regions was threaded linearly

Page 72: Type Systems for Region-based Memory Management

72

Substructural Type Systems

Provide core mechanisms to restrict the number and order of uses of data and operations– generalization of linear type systems

Page 73: Type Systems for Region-based Memory Management

73

Structural Properties

Conventional type systems satisfy

– Exchange use typing assumptions in any order

– Contraction use typing assumptions more than once

– Weakening use typing assumptions less than once

Page 74: Type Systems for Region-based Memory Management

74

Structural Properties

Conventional type systems satisfy

– Exchange use typing assumptions in any order

– Contraction – Copy use typing assumptions more than once

– Weakening – Drop use typing assumptions less than once

Page 75: Type Systems for Region-based Memory Management

75

Structural Properties

Substructural type systems fail to satisfy

– Exchange use typing assumptions in any order

– Contraction – Copy use typing assumptions more than once

– Weakening – Drop use typing assumptions less than once

Page 76: Type Systems for Region-based Memory Management

76

Structural Properties

Substructural type systems fail to satisfy

– Exchange use typing assumptions in any order

– Contraction – Copy use typing assumptions more than once

– Weakening – Drop use typing assumptions less than once

Page 77: Type Systems for Region-based Memory Management

77

Substructural Qualifiers

AffineDrop

RelevantCopy

UnrestrictedDrop Copy

Linear

Page 78: Type Systems for Region-based Memory Management

78

Substructural Qualifiers

AffineDrop

RelevantCopy

UnrestrictedDrop Copy

Linear

Unique objects – may be “used”at most once

Shared objects –may be “used” more than once

Page 79: Type Systems for Region-based Memory Management

79

Substructural Qualifiers

AffineDrop

RelevantCopy

UnrestrictedDrop Copy

Linear

Unique objects – may be “used”at most once

Shared objects –may be “used” more than once

Page 80: Type Systems for Region-based Memory Management

80

Substructural Qualifiers

AffineDrop

RelevantCopy

UnrestrictedDrop Copy

Linear

Unique objects – may be “used”at most once

Shared objects –may be “used” more than once

Page 81: Type Systems for Region-based Memory Management

81

Substructural Qualifiers

AffineDrop

RelevantCopy

UnrestrictedDrop Copy

Linear

Unique objects – may be “used”at most once

Shared objects –may be “used” more than once

Page 82: Type Systems for Region-based Memory Management

82

Substructural Qualifiers

AffineDrop

RelevantCopy

UnrestrictedDrop Copy

Linear

Essential objects – must be “used”at least once

Inessential objects –may be “used” less than once

Page 83: Type Systems for Region-based Memory Management

83

Substructural Qualifiers

AffineDrop

RelevantCopy

UnrestrictedDrop Copy

Linear

Essential objects – must be “used”at least once

Inessential objects –may be “used” less than once

Page 84: Type Systems for Region-based Memory Management

84

Substructural Qualifiers

AffineDrop

RelevantCopy

UnrestrictedDrop Copy

Linear

Essential objects – must be “used”at least once

Inessential objects –may be “used” less than once

Page 85: Type Systems for Region-based Memory Management

85

Substructural Qualifiers

AffineDrop

RelevantCopy

UnrestrictedDrop Copy

Linear

Essential objects – must be “used”at least once

Inessential objects –may be “used” less than once

Page 86: Type Systems for Region-based Memory Management

86

Substructural Type System: URAL

Qualifiers

q ::= U j R j A j L

PreTypes

::= 1 j 1 2 j 1 ( 2 j 8. j 9.

Types

::= q

Page 87: Type Systems for Region-based Memory Management

87

Substructural Type System: URAL

Qualifiers

q ::= U j R j A j L

PreTypes

::= 1 j 1 2 j 1 ( 2 j 8. j 9.

Types

::= q

How maythe value be used?

Page 88: Type Systems for Region-based Memory Management

88

Substructural Type System: URAL

Qualifiers

q ::= U j R j A j L

PreTypes

::= 1 j 1 2 j 1 ( 2 j 8. j 9.

Types

::= q

How maythe value be used?

How often maythe value be used?

Page 89: Type Systems for Region-based Memory Management

89

rgnURAL = URAL + Regions

PreTypes

::= … j cap j hnd j ref j 8. j 9.

“capability” for region

Page 90: Type Systems for Region-based Memory Management

90

rgnURAL: Region Primitives

Regions are created and destroyedwith separate operations

newrgn ::U(U1 ( L(9.L(Lcap Uhnd ))

freergn ::U8.U(L(Lcap Uhnd ) ( U1)

Page 91: Type Systems for Region-based Memory Management

91

rgnURAL: Region Primitives

Regions are created and destroyedwith separate operations

newrgn ::U(U1 ( L(9.L(Lcap Uhnd ))

freergn ::U8.U(L(Lcap Uhnd ) ( U1)

Page 92: Type Systems for Region-based Memory Management

92

rgnURAL: Region Primitives

Regions are created and destroyedwith separate operations

newrgn ::U(U1 ( L(9.L(Lcap Uhnd ))

freergn ::U8.U(L(Lcap Uhnd ) ( U1)

Page 93: Type Systems for Region-based Memory Management

93

rgnURAL: Region Primitives

Regions are created and destroyedwith separate operations

newrgn ::U(U1 ( L(9.L(Lcap Uhnd ))

freergn ::U8.U(L(Lcap Uhnd ) ( U1)

Page 94: Type Systems for Region-based Memory Management

94

rgnURAL: Region Primitives

new ::U8,.U(L(Lcap Uhnd U) (

L(Lcap Uref U)

read ::U8,.U(L(Lcap Uref U) (

L(Lcap U)

Page 95: Type Systems for Region-based Memory Management

95

rgnURAL: Region Primitives

new ::U8,.U(L(Lcap Uhnd U) (

L(Lcap Uref U)

read ::U8,.U(L(Lcap Uref U) (

L(Lcap U)

Page 96: Type Systems for Region-based Memory Management

96

rgnURAL: Region Primitives

new ::U8,.U(L(Lcap Uhnd U) (

L(Lcap Uref U)

read ::U8,.U(L(Lcap Uref U) (

L(Lcap U)

Page 97: Type Systems for Region-based Memory Management

97

rgnURAL: Region Primitives

Regions are created and destroyedwith separate operations

newrgn ::U(U1 ( L(9.L(Lcap Uhnd ))

freergn ::U8.U(L(Lcap Uhnd ) ( U1)

Page 98: Type Systems for Region-based Memory Management

98

rgnURAL: Region Primitives

Regions are created and destroyedwith separate operations

newrgn ::U(U1 ( q(9.q(qcap Uhnd ))

freergn ::U8.U(q(qcap Uhnd ) ( U1)

Page 99: Type Systems for Region-based Memory Management

99

rgnURAL: Region Primitives

Regions are created and destroyedwith separate operations

newrgn ::U(U1 ( q(9.q(qcap Uhnd ))

freergn :: A ¹qU8.U(q(qcap Uhnd ) ( U1)

Page 100: Type Systems for Region-based Memory Management

100

rgnURAL: Region Primitives

new ::U8,.U(q(qcap Uhnd U) (

q(qcap Uref U)

read ::U8,.U(q(qcap Uref U) (

q(qcap U)

Page 101: Type Systems for Region-based Memory Management

101

Translation: FRGN to rgnURAL, Types

« RGN ¬ = U( ( L( «¬))

Page 102: Type Systems for Region-based Memory Management

102

Translation: FRGN to rgnURAL, Types

« RGN ¬ = U( ( L( «¬))

– operational behavior of monad is store-passing

Page 103: Type Systems for Region-based Memory Management

103

Translation: FRGN to rgnURAL, Types

« RGN ¬ = U( ( L( «¬))

– operational behavior of monad is store-passing

Page 104: Type Systems for Region-based Memory Management

104

Translation: FRGN to rgnURAL, Types

« RGN ¬ = U( ( L( «¬))

– operational behavior of monad is store-passing

Page 105: Type Systems for Region-based Memory Management

105

Translation: FRGN to rgnURAL, Types

« Pf(1 · 2) ¬ = U(9’. U(U(2 ( L(1 ’))

« Pf(1 · 2) ¬ = U(9’. U( U(L(1 ’) ( 2))

– Isomorphism between 2 and L(1 ’), for some “slack” ’

Page 106: Type Systems for Region-based Memory Management

106

Translation: FRGN to rgnURAL, Types

« Pf(1 · 2) ¬ = U(9’. U(U(2 ( L(1 ’))

« Pf(1 · 2) ¬ = U(9’. U( U(L(1 ’) ( 2))

– Isomorphism between 2 and L(1 ’), for some “slack” ’

– Proof that 1 is a substack of 2 is persistent

– Existence of 1 and 2 is ephemeral

Page 107: Type Systems for Region-based Memory Management

107

Translation: FRGN to rgnURAL, Types

« Pf(1 · 2) ¬ = U(9’. U(U(2 ( L(1 ’))

« Pf(1 · 2) ¬ = U(9’. U( U(L(1 ’) ( 2))

– Isomorphism between 2 and L(1 ’), for some “slack” ’

– Proof that 1 is a substack of 2 is persistent

– Existence of 1 and 2 is ephemeral

Page 108: Type Systems for Region-based Memory Management

108

Translation: FRGN to rgnURAL, Types

« Pf(1 · 2) ¬ = U(9’. Iso(2, L(1 ’))) (

« Pf(1 · 2) ¬ = U(9’. U(U(L(1 ’) ( 2))

– Isomorphism between 2 and L(1 ’), for some “slack” ’

– Proof that 1 is a substack of 2 is persistent

– Existence of 1 and 2 is ephemeral

Page 109: Type Systems for Region-based Memory Management

109

Translation: FRGN to rgnURAL, Types

« Hnd ¬ = U(9. U(U(9’. Iso(, L(’ Lcap )))« Hnd ¬ = U(9. U( Uhnd ))

« Ref ¬ = U(9. U(U(9’. Iso(, L(’ Lcap )))« Ref ¬ = U(9. U( Uref «¬))

Page 110: Type Systems for Region-based Memory Management

110

Translation: FRGN to rgnURAL, Types

« Hnd ¬ = U(9. U(U(9’. Iso(, L(’ Lcap )))« Hnd ¬ = U(9. U( Uhnd ))

« Ref ¬ = U(9. U(U(9’. Iso(, L(’ Lcap )))« Ref ¬ = U(9. U( Uref «¬))

Existential fixes region

Page 111: Type Systems for Region-based Memory Management

111

Translation: FRGN to rgnURAL, Types

« Hnd ¬ = U(9. U(U(9’. Iso(, L(’ Lcap )))« Hnd ¬ = U(9. U( Uhnd ))

« Ref ¬ = U(9. U(U(9’. Iso(, L(’ Lcap )))« Ref ¬ = U(9. U( Uref «¬))

Existential fixes region Isomorphism witnesses membership of in

Page 112: Type Systems for Region-based Memory Management

112

Translation: FRGN to rgnURAL, Ops

« returnRGN [] [] e ¬ =let res : «¬ = «e¬ inUstk:. Lhstk,resi

« thenRGN [] [a] [b] e1 e2 ¬ =let f : «RGN a¬= «e1¬ inlet g : «a ! RGN b¬ = «e2¬ inUstk:. let hstk,resi = f stk in g res stk

Page 113: Type Systems for Region-based Memory Management

113

Translation: FRGN to rgnURAL, Ops

« returnRGN [] [] e ¬ =let res : «¬ = «e¬ inUstk:. Lhstk,resi

« thenRGN [] [a] [b] e1 e2 ¬ =let f : «RGN a¬= «e1¬ inlet g : «a ! RGN b¬ = «e2¬ inUstk:. let hstk,resi = f stk in g res stk

Store-passing

encoding

Page 114: Type Systems for Region-based Memory Management

114

Translation: FRGN to rgnURAL, Ops

« letRGN [1] [] e ¬ = let f : «82. Pf(1·2) ! Hnd 2 ! RGN 2 ¬ = «e¬ inUstk1:1.let pack(,hcap,hndi) = newrgn Lhi inUstk1:1.let stk2 = Lhstk1,capi inUstk1:1.let id = Ustk: L(1 Lcap ).stk inUstk1:1.let pwit = Upack(Lcap ,Uhid,idi) inUstk1:1.let phnd = Upack(,UhUpack(1,Uhid,idi),hndi) inUstk1:1.let hstk2,resi = f [L(1 Lcap )] pwit phnd stk2 inUstk1:1.let hstk1,capi = stk2 inUstk1:1.let hi = freergn [] Lhcap,hndi inUstk1:1.Lhstk1,resi

Page 115: Type Systems for Region-based Memory Management

115

Translation: FRGN to rgnURAL, Ops

« letRGN [1] [] e ¬ = let f : «82. Pf(1·2) ! Hnd 2 ! RGN 2 ¬ = «e¬ inUstk1:1.let pack(,hcap,hndi) = newrgn Lhi inUstk1:1.let stk2 = Lhstk1,capi inUstk1:1.let id = Ustk: L(1 Lcap ).stk inUstk1:1.let pwit = Upack(Lcap ,Uhid,idi) inUstk1:1.let phnd = Upack(,UhUpack(1,Uhid,idi),hndi) inUstk1:1.let hstk2,resi = f [L(1 Lcap )] pwit phnd stk2 inUstk1:1.let hstk1,capi = stk2 inUstk1:1.let hi = freergn [] Lhcap,hndi inUstk1:1.Lhstk1,resi

Page 116: Type Systems for Region-based Memory Management

116

Translation: FRGN to rgnURAL, Ops

« letRGN [1] [] e ¬ = let f : «82. Pf(1·2) ! Hnd 2 ! RGN 2 ¬ = «e¬ inUstk1:1.let pack(,hcap,hndi) = newrgn Uhi inUstk1:1.let stk2 = Lhstk1,capi inUstk1:1.let id = Ustk: L(1 Lcap ).stk inUstk1:1.let pwit = Upack(Lcap ,Uhid,idi) inUstk1:1.let phnd = Upack(,UhUpack(1,Uhid,idi),hndi) inUstk1:1.let hstk2,resi = f [L(1 Lcap )] pwit phnd stk2 inUstk1:1.let hstk1,capi = stk2 inUstk1:1.let hi = freergn [] Lhcap,hndi inUstk1:1.Lhstk1,resi

Page 117: Type Systems for Region-based Memory Management

117

Translation: FRGN to rgnURAL, Ops

« letRGN [1] [] e ¬ = let f : «82. Pf(1·2) ! Hnd 2 ! RGN 2 ¬ = «e¬ inUstk1:1.let pack(,hcap,hndi) = newrgn Uhi inUstk1:1.let stk2 = Lhstk1,capi inUstk1:1.let id = Ustk: L(1 Lcap ).stk inUstk1:1.let pwit = Upack(Lcap ,Uhid,idi) inUstk1:1.let phnd = Upack(,UhUpack(1,Uhid,idi),hndi) inUstk1:1.let hstk2,resi = f [L(1 Lcap )] pwit phnd stk2 inUstk1:1.let hstk1,capi = stk2 inUstk1:1.let hi = freergn [] Lhcap,hndi inUstk1:1.Lhstk1,resi

Page 118: Type Systems for Region-based Memory Management

118

Translation: FRGN to rgnURAL, Ops

« letRGN [1] [] e ¬ = let f : «82. Pf(1·2) ! Hnd 2 ! RGN 2 ¬ = «e¬ inUstk1:1.let pack(,hcap,hndi) = newrgn Uhi inUstk1:1.let stk2 = Lhstk1,capi inUstk1:1.let id = Ustk: L(1 Lcap ).stk inUstk1:1.let pwit = Upack(Lcap ,Uhid,idi) inUstk1:1.let phnd = Upack(,UhUpack(1,Uhid,idi),hndi) inUstk1:1.let hstk2,resi = f [L(1 Lcap )] pwit phnd stk2 inUstk1:1.let hstk1,capi = stk2 inUstk1:1.let hi = freergn [] Lhcap,hndi inUstk1:1.Lhstk1,resi

Page 119: Type Systems for Region-based Memory Management

119

Translation: FRGN to rgnURAL, Ops

« letRGN [1] [] e ¬ = let f : «82. Pf(1·2) ! Hnd 2 ! RGN 2 ¬ = «e¬ inUstk1:1.let pack(,hcap,hndi) = newrgn Uhi inUstk1:1.let stk2 = Lhstk1,capi inUstk1:1.let id = Ustk: L(1 Lcap ).stk inUstk1:1.let pwit = Upack(Lcap ,Uhid,idi) inUstk1:1.let phnd = Upack(,UhUpack(1,Uhid,idi),hndi) inUstk1:1.let hstk2,resi = f [L(1 Lcap )] pwit phnd stk2 inUstk1:1.let hstk1,capi = stk2 inUstk1:1.let hi = freergn [] Lhcap,hndi inUstk1:1.Lhstk1,resi

Page 120: Type Systems for Region-based Memory Management

120

Translation: FRGN to rgnURAL, Ops

« new [] [] eh e ¬ =let phnd : «Hnd ¬ = «eh¬ in let x : «¬ = «e¬ in Ustk:.let pack(,hpack(’,hprj,inji),hndi) = phnd in Ustk:.let hstk’,capi = prj stk inUstk:.let hcap,refi = new [] [«¬] Lhcap,hnd,xi in Ustk:.let pref = Upack(,UhUpack(’,Uhprj,inji),refi) in Ustk:.let stk = inj Lhstk’,capi inUstk:.Lhstk,prefi

Page 121: Type Systems for Region-based Memory Management

121

Cyclone Features

Dynamic Regions– 1st class regions without LIFO restriction– represented by a linear/affine key:

L/Akey = L/A(L/Acap Uhnd )– must be opened to allocate or access

openDRgn :: 8,,. key ( 8,,. (hnd ( rgn ( cap ) ) ( 8,,. rgn ( key )

Page 122: Type Systems for Region-based Memory Management

122

Cyclone Features

Heap– globally scoped, garbage collected region

Ucap heap Uhnd heap

Page 123: Type Systems for Region-based Memory Management

123

Cyclone Features

Heap– globally scoped, garbage collected region

Ucap heap Uhnd heap

Reaps– regions with individual object deallocation

L/Aref

Page 124: Type Systems for Region-based Memory Management

124

Cyclone Features

Unique Pointers– anonymous dynamic regions without the handle

Luptr = L(9. L(Lcap Uref ))

– or, unique references in the heapLuptr = Lref heap

Page 125: Type Systems for Region-based Memory Management

125

Future Work

In practice, need to phase-split capabilities

Encode results of region analyses– Aiken et.al. [PLDI’95], Henglein et.al. [PPDP’01]

Modeling other language features– Cyclone, Cqual, Vault

Page 126: Type Systems for Region-based Memory Management

126

Final Thoughts

Type-and-effect · Monadic · Substructural– would be a nice story

Page 127: Type Systems for Region-based Memory Management

127

Final Thoughts

Type-and-effect · Monadic · Substructural– would be a nice story– better picture (??)

Type-and effect Monadic

Substructural

Region-basedmemory management

Page 128: Type Systems for Region-based Memory Management

128

Final Thoughts

Advantage of Substructural TS for RBMM– Encode results of region analyses

Aiken et.al. [PLDI’95], Henglein et.al. [PPDP’01]

– Previous work introduced features in type-system exclusively for supporting non-LIFO regions

– This work introduces primitives, but reuses features of type-system to encode non-LIFO regions

Page 129: Type Systems for Region-based Memory Management

129

Final Thoughts

Advantage of Substructural TS for RBMM– Encode results of region analyses

Aiken et.al. [PLDI’95], Henglein et.al. [PPDP’01]

– Previous work introduced features in type-system exclusively for supporting non-LIFO regions

– This work introduces primitives, but reuses features of type-system to encode non-LIFO regions

– Substructural type-systems are good for supporting other features, so it is an economical use

Page 130: Type Systems for Region-based Memory Management

130

Final Thoughts

Scope vs. Lifetime– Lexical scope of region name

universal and existential quantification

– Un-scoped lifetime of region capability late allocation / early deallocation

Page 131: Type Systems for Region-based Memory Management

131

Final Thoughts

Scope vs. Lifetime– Lexical scope of region name

universal and existential quantification

– Un-scoped lifetime of region capability late allocation / early deallocation

newrgn ::U(U1 ( L(9.L(Lcap Uhnd ))

Page 132: Type Systems for Region-based Memory Management

132

Final Thoughts

Scope vs. Lifetime– Lexical scope of region name

universal and existential quantification

– Un-scoped lifetime of region capability late allocation / early deallocation

newrgn ::U(U1 ( L(9.L(U1 ( L(Lcap Uhnd )))

Page 133: Type Systems for Region-based Memory Management

133

References

http://www.cs.cornell.edu/People/fluet

– FRGN : ICFP’04, JFP’06

– URAL and refURAL: ICFP’05

– rgnURAL : ESOP’06 (submitted)