linear regions are all you need

75
Linear Regions Are All You Need Matthew Fluet Cornell University Greg Morrisett & Amal Ahmed Harvard University

Upload: noreen

Post on 08-Jan-2016

41 views

Category:

Documents


2 download

DESCRIPTION

Linear Regions Are All You Need. Matthew Fluet Cornell University Greg Morrisett & Amal Ahmed Harvard University. Memory Management. Dynamic allocation pervasive in computation. Memory Management. Dynamic allocation pervasive in computation Region-based Memory Management - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Linear Regions Are All You Need

Linear Regions Are All You Need

Matthew Fluet

Cornell University

Greg Morrisett & Amal Ahmed

Harvard University

Page 2: Linear Regions Are All You Need

2

Memory Management

Dynamic allocation pervasive in computation

Page 3: Linear Regions Are All You Need

3

Memory Management

Dynamic allocation pervasive in computation

Region-based Memory Management– Memory is divided into regions– Objects are individually allocated in a region

constant-time operation

– All objects in a region are deallocated together constant-time operation

Page 4: Linear Regions Are All You Need

4

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 5: Linear Regions Are All You Need

5

Cyclone: Regions

Region varietyAllocation

(objects)

DeallocationAliasing

(objects)(what) (when)

Stack static

whole region

exit of lexical scope

unrestricted

Lexical

dynamic

Dynamicmanual

Dynamic seq.

Heap (`H)

single objects

automatic(BDW GC)

Unique (`U)

manual restrictedRef-counted (`RC)

Page 6: Linear Regions Are All You Need

6

Application: Cyclone

MediaNET– TCP benchmark (packet forwarding)– Cyclone v.0.1 (lexical regions & garbage collector)

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

– Cyclone v.0.5 (unique pointers & dynamic regions) High water mark: 8 KB 0 collections Basic throughput: 74MB/s

Page 7: Linear Regions Are All You Need

7

Application: Cyclone

MediaNET– TCP benchmark (packet forwarding)– Cyclone v.0.1 (lexical regions & garbage collector)

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

– Cyclone v.0.5 (unique pointers & dynamic regions) High water mark: 8 KB 0 collections Basic throughput: 74MB/s

Page 8: Linear Regions Are All You Need

8

Cyclone: Regions

Region varietyAllocation

(objects)

DeallocationAliasing

(objects)(what) (when)

Stack static

whole region

exit of lexical scope

unrestricted

Lexical

dynamic

Dynamicmanual

Dynamic seq.

Heap (`H)

single objects

automatic(BDW GC)

Unique (`U)

manual restrictedRef-counted (`RC)

Proving type safety of Cyclone is a nightmare!!

Page 9: Linear Regions Are All You Need

9

Cyclone: Regions

Region varietyAllocation

(objects)

DeallocationAliasing

(objects)(what) (when)

Stack static

whole region

exit of lexical scope

unrestricted

Lexical

dynamic

Dynamicmanual

Dynamic seq.

Heap (`H)

single objects

automatic(BDW GC)

Unique (`U)

manual restrictedRef-counted (`RC)

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

a simpler type system.

Page 10: Linear Regions Are All You Need

10

Cyclone: Regions

Region varietyAllocation

(objects)

DeallocationAliasing

(objects)(what) (when)

Stack static

whole region

exit of lexical scope

unrestricted

Lexical

dynamic

Dynamicmanual

Dynamic seq.

Heap (`H)

single objects

automatic(BDW GC)

Unique (`U)

manual restrictedRef-counted (`RC)

Linear RegionsAre All You Need

Page 11: Linear Regions Are All You Need

11

Outline

Introduction

Monadic Type System (FRGN) [ICFP’04]

Substructural Type System (rgnUL)– Translation Sketch

Conclusion

Page 12: Linear Regions Are All You Need

12

Monadic Type System for Regions [ICFP’04]

Extend the runST “trick” to nested regions [L-PJ ’94]

– Polymorphic type system ensures safety

Key insights (FRGN):– Effects map to an indexed monadic type– Region subtyping witnessed by types– Sufficient for encoding Tofte-Talpin region calculus

and “core” Cyclone region features

Page 13: Linear Regions Are All You Need

13

RGN monad: Types

Monadic type

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

Page 14: Linear Regions Are All You Need

14

RGN monad: Operations

Monadic unit and bind

returnRGN ::

8,. ! RGN

thenRGN ::

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

Page 15: Linear Regions Are All You Need

15

RGN monad: Operations

Monadic unit and bind

returnRGN ::

8,. ! RGN

thenRGN ::

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

Page 16: Linear Regions Are All You Need

16

RGN monad: Operations

Monadic unit and bind

returnRGN ::

8,. ! RGN

thenRGN ::

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

Page 17: Linear Regions Are All You Need

17

RGN monad: Types

Reference type

Ref values of type allocated in region

at the top of the stack of regions

Page 18: Linear Regions Are All You Need

18

RGN monad: Operations

Create and read region allocated values

new ::

8,. ! RGN (Ref )

read ::

8,. Ref ! RGN

Page 19: Linear Regions Are All You Need

19

RGN monad: Operations

Create and read region allocated values

new ::

8,. ! RGN (Ref )

read ::

8,. Ref ! RGN

Page 20: Linear Regions Are All You Need

20

RGN monad: Encapsulation

Encapsulate and run a monadic computation

runRGN ::

8. (8. RGN ) !

Page 21: Linear Regions Are All You Need

21

RGN monad: Encapsulation

Encapsulate and run a monadic computation

runRGN ::

8. (8. RGN ) !

Page 22: Linear Regions Are All You Need

22

RGN monad: Encapsulation

Encapsulate and run a monadic computation

runRGN ::

8. (8. RGN ) !

“for all stacks” ) no assumptions about

stack of regions

Page 23: Linear Regions Are All You Need

23

RGN monad: Encapsulation

Encapsulate and run a monadic computation

runRGN ::

8. (8. RGN ) !

“for all stacks” ) no assumptions about

stack of regions

Page 24: Linear Regions Are All You Need

24

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 25: Linear Regions Are All You Need

25

RGN monad: Example

runRGN ( 1.

do a à new [1] 1

c à runRGN ( 2.

do b à new [2] 7

… z = …

new [1] z )

… c … )

1

Page 26: Linear Regions Are All You Need

26

RGN monad: Example

runRGN ( 1.

do a à new [1] 1

c à runRGN ( 2.

do b à new [2] 7

… z = …

new [1] z )

… c … )

1 a : 1

inputallocated in first region

Page 27: Linear Regions Are All You Need

27

RGN monad: Example

runRGN ( 1.

do a à new [1] 1

c à runRGN ( 2.

do b à new [2] 7

… z = …

new [1] z )

… c … )

1

2

a : 1

input allocated in first region

Page 28: Linear Regions Are All You Need

28

RGN monad: Example

runRGN ( 1.

do a à new [1] 1

c à runRGN ( 2.

do b à new [2] 7

… z = …

new [1] z )

… c … )

1

2

a : 1

b : 7

temporary allocated in second region

inputallocated in first region

Page 29: Linear Regions Are All You Need

29

RGN monad: Example

runRGN ( 1.

do a à new [1] 1

c à runRGN ( 2.

do b à new [2] 7

… z = …

new [1] z )

… c … )

1

2

a : 1

c : 8

b : 7

temporary allocated in second region

input and outputallocated in first region

Page 30: Linear Regions Are All You Need

30

RGN monad: Example

runRGN ( 1.

do a à new [1] 1

c à runRGN ( 2.

do b à new [2] 7

… z = …

new [1] z )

… c … )

1 a : 1

c : 8

temporary allocated in second region

input and outputallocated in first region

Page 31: Linear Regions Are All You Need

31

RGN monad: Example

runRGN ( 1.

do a à new [1] 1

c à runRGN ( 2.

do b à new [2] 7

… z = …

new [1] z )

… c … )

allocating in older region

requires RGN 1 type

allocating in younger region

requires RGN 2 type

Page 32: Linear Regions Are All You Need

32

RGN monad: Witnesses

Witness type

Pf(1 · 2) –

type-level proof that the stack of regions 1

is a substack of the stack of regions 2

Page 33: Linear Regions Are All You Need

33

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 34: Linear Regions Are All You Need

34

RGN monad: Regions

Regions are created and destroyedwith a lexically scoped construct

letRGN ::

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

Page 35: Linear Regions Are All You Need

35

RGN monad: Regions

Regions are created and destroyedwith a lexically scoped construct

letRGN ::

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

Page 36: Linear Regions Are All You Need

36

RGN monad: Example

letRGN ( 1. pf1.

do a à new [1] 1

c à letRGN ( 2. pf2.

do b à new [2] 7

… z = …

coerceRgn pf (new [1] z ))

… c … )

1

2

a : 1

c : 8

b : 7

Page 37: Linear Regions Are All You Need

37

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 38: Linear Regions Are All You Need

38

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 that the stack of regions was used in a single-threaded manner

Page 39: Linear Regions Are All You Need

39

Substructural Type Systems

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

Page 40: Linear Regions Are All You Need

40

Substructural Type System: UL

Qualifiers

q ::= U j L

PreTypes

::= 1 j 1 £ 2 j 1 ! 2 j 8. j 9.

Types

::= q

Page 41: Linear Regions Are All You Need

41

Substructural Type System: UL

Qualifiers

q ::= U j L

PreTypes

::= 1 j 1 £ 2 j 1 ! 2 j 8. j 9.

Types

::= q

How maythe value be used?

Page 42: Linear Regions Are All You Need

42

Substructural Type System: UL

Qualifiers

q ::= U 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 43: Linear Regions Are All You Need

43

Substructural Qualifiers

UnrestrictedDrop Copy

Linear

must be “used” exactly once

may be “used” an arbitrary # of times

Page 44: Linear Regions Are All You Need

44

Substructural Type System for Regions

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

Key insights (rgnUL):– Separate region names from region liveness– Region liveness witnessed by types– Sufficient for encoding FRGN calculus

and “advanced” Cyclone region features

Page 45: Linear Regions Are All You Need

45

rgnUL = UL + Regions

PreTypes

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

“capability” for region ;mediates all access to a region

for allocating, reading, and writing

Page 46: Linear Regions Are All You Need

46

rgnUL: Region Primitives

Regions are created and destroyedwith separate operations

newrgn ::U1 ! (9. Lcap )

freergn ::

8. (Lcap ! U1)

Page 47: Linear Regions Are All You Need

47

rgnUL: Region Primitives

Regions are created and destroyedwith separate operations

newrgn ::U1 ! (9. Lcap )

freergn ::

8. (Lcap ! U1)

Produces a capability.

Consumes a capability.

Page 48: Linear Regions Are All You Need

48

rgnUL: Region Primitives

Regions are created and destroyedwith separate operations

newrgn ::U1 ! (9. Lcap )

freergn ::

8. (Lcap ! U1)

Page 49: Linear Regions Are All You Need

49

rgnUL: Region Primitives

new ::

8,. ((Lcap £ U) !(Lcap £ Uref U))

read ::

8,. ((Lcap £ Uref U) !(Lcap £ U))

Page 50: Linear Regions Are All You Need

50

rgnUL: Region Primitives

new ::

8,. ((Lcap £ U) !(Lcap £ Uref U))

read ::

8,. ((Lcap £ Uref U) !(Lcap £ U))

Returns a capability.

Requires a capability.

Page 51: Linear Regions Are All You Need

51

rgnUL: Region Primitives

new ::

8,. ((Lcap £ U) !(Lcap £ Uref U)

read ::

8,. ((Lcap £ Uref U) !(Lcap £ U)

Page 52: Linear Regions Are All You Need

52

Translation: FRGN to rgnUL, Types

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

Page 53: Linear Regions Are All You Need

53

Translation: FRGN to rgnUL, Types

« RGN ¬ = ! ( £ «¬)

– operational behavior of monad is store/stack-passing

Page 54: Linear Regions Are All You Need

54

Translation: FRGN to rgnUL, Types

« RGN ¬ = ! ( £ «¬)

– operational behavior of monad is store/stack-passing

Page 55: Linear Regions Are All You Need

55

Translation: FRGN to rgnUL, Types

« RGN ¬ = ! ( £ «¬)

– operational behavior of monad is store/stack-passing– represent “stack of regions”

as a sequence of linear capabilities,formed out of nested linear tuples

Page 56: Linear Regions Are All You Need

56

Translation: FRGN to rgnUL, Types

« RGN ¬ = ! ( £ «¬)

– operational behavior of monad is store/stack-passing– represent “stack of regions”

as a sequence of linear capabilities,formed out of nested linear tuples

Page 57: Linear Regions Are All You Need

57

Translation: FRGN to rgnUL, 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 58: Linear Regions Are All You Need

58

Translation: FRGN to rgnUL, 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 59: Linear Regions Are All You Need

59

Translation: FRGN to rgnUL, Types

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

Page 60: Linear Regions Are All You Need

60

Translation: FRGN to rgnUL, Types

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

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

Page 61: Linear Regions Are All You Need

61

Translation: FRGN to rgnUL, Types

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

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

– Proof that 1 is a substack of 2 is persistent

– Liveness of 1 and 2 is ephemeral

Page 62: Linear Regions Are All You Need

62

Translation: FRGN to rgnUL, Types

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

Page 63: Linear Regions Are All You Need

63

Translation: FRGN to rgnUL, Types

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

Existential fixes region

Page 64: Linear Regions Are All You Need

64

Translation: FRGN to rgnUL, Types

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

Existential fixes region Isomorphism witnesses membership of in

Page 65: Linear Regions Are All You Need

65

Translation: FRGN to rgnUL, 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 66: Linear Regions Are All You Need

66

Translation: FRGN to rgnUL, Ops

« letRGN [1] [] e ¬ = let f : «82. Pf(1·2) ! RGN 2 ¬ = «e¬ inUstk1:1.let pack(,cap) = 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 hstk2,resi = f [L(1 ­ Lcap )] pwit stk2 inUstk1:1.let hstk1,capi = stk2 inUstk1:1.let hi = freergn [] cap inUstk1:1.Lhstk1,resi

Stack-passing encoding

Page 67: Linear Regions Are All You Need

67

Translation: FRGN to rgnUL, Ops

« letRGN [1] [] e ¬ = let f : «82. Pf(1·2) ! RGN 2 ¬ = «e¬ inUstk1:1.let pack(,cap) = 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 hstk2,resi = f [L(1 ­ Lcap )] pwit stk2 inUstk1:1.let hstk1,capi = stk2 inUstk1:1.let hi = freergn [] cap inUstk1:1.Lhstk1,resi

Create & destroy a new region

Page 68: Linear Regions Are All You Need

68

Translation: FRGN to rgnUL, Ops

« letRGN [1] [] e ¬ = let f : «82. Pf(1·2) ! RGN 2 ¬ = «e¬ inUstk1:1.let pack(,cap) = 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 hstk2,resi = f [L(1 ­ Lcap )] pwit stk2 inUstk1:1.let hstk1,capi = stk2 inUstk1:1.let hi = freergn [] cap inUstk1:1.Lhstk1,resi

Construct rep.of new stack

Page 69: Linear Regions Are All You Need

69

Translation: FRGN to rgnUL, Ops

« letRGN [1] [] e ¬ = let f : «82. Pf(1·2) ! RGN 2 ¬ = «e¬ inUstk1:1.let pack(,cap) = 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 hstk2,resi = f [L(1 ­ Lcap )] pwit stk2 inUstk1:1.let hstk1,capi = stk2 inUstk1:1.let hi = freergn [] cap inUstk1:1.Lhstk1,resi

Run comp. and recover old stack

and new cap

Page 70: Linear Regions Are All You Need

70

Translation: FRGN to rgnUL, Ops

« letRGN [1] [] e ¬ = let f : «82. Pf(1·2) ! RGN 2 ¬ = «e¬ inUstk1:1.let pack(,cap) = 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 hstk2,resi = f [L(1 ­ Lcap )] pwit stk2 inUstk1:1.let hstk1,capi = stk2 inUstk1:1.let hi = freergn [] cap inUstk1:1.Lhstk1,resi

Construct isomorphism

Page 71: Linear Regions Are All You Need

71

Translation: FRGN to rgnUL, Ops

« letRGN [1] [] e ¬ = let f : «82. Pf(1·2) ! RGN 2 ¬ = «e¬ inUstk1:1.let pack(,cap) = 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 hstk2,resi = f [L(1 ­ Lcap )] pwit stk2 inUstk1:1.let hstk1,capi = stk2 inUstk1:1.let hi = freergn [] cap inUstk1:1.Lhstk1,resi

Page 72: Linear Regions Are All You Need

72

Encoding Cyclone Features

Many of Cyclone’s features fit into this framework– Lexical Regions– Dynamic Regions– Heap– Reaps– Unique Pointers

See paper for more details.

Page 73: Linear Regions Are All You Need

73

Future Work

In practice, capabilities shouldn’t be values

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

Combine convenience of monadic encapsulation with power of substructural threading

Page 74: Linear Regions Are All You Need

74

Conclusion

Substructural type systems applicable to encoding region-based memory management– target-level language exposes commonalities

in source-level language features

Scope vs. Lifetime– Lexical scope of region name– Un-scoped lifetime of region capability

Unrestricted witnesses vs. Linear capabilities

Page 75: Linear Regions Are All You Need

75