Presentation is loading. Please wait.

Presentation is loading. Please wait.

534 534534 534 534 534 534 A Type Theory for Memory Allocation and Data Layout Leaf Petersen, Robert Harper, Karl Crary and Frank Pfenning Carnegie Mellon.

Similar presentations


Presentation on theme: "534 534534 534 534 534 534 A Type Theory for Memory Allocation and Data Layout Leaf Petersen, Robert Harper, Karl Crary and Frank Pfenning Carnegie Mellon."— Presentation transcript:

1 534 534534 534 534 534 534 A Type Theory for Memory Allocation and Data Layout Leaf Petersen, Robert Harper, Karl Crary and Frank Pfenning Carnegie Mellon University 534

2 2 High-level languages –Abstract view of data, characterized by operations –e.g. pairs: Introduction: (e1,e2) : t1 x t2 Elimination: fst e : t1, snd e : t2 Low-level languages – Concrete view of data, characterized by layout in memory –e.g. C structs: Contiguous layout Memory size determined by type Views of data

3 Carnegie Mellon University3 Usually programmers don’t care –But sometimes have to –Marshalling, interaction with low-level devices, precise control of initialization, interoperability –Generally no type safety Compilers have to care –Represent high-level data abstractions –Allocation and initialization code Data layout

4 Carnegie Mellon University4 (3,(4,5)) : int x (int x int) 534 534 534

5 Carnegie Mellon University5 Type theory for data layout Expose the fine structure –Expose memory layout in types –Implementation choices explicit –High-level object types defined in terms of low-level memory types –High-level operations on objects broken down into low-level operations on memory What is the fine structure of memory?

6 Carnegie Mellon University6 Initialization Data objects –Created by initializing raw memory. Initialization changes types –e.g. from ns to int Commonly dealt with via linearity –New memory is linear –No aliases –Linear type theory handles re-typing

7 Carnegie Mellon University7 Adjacency Memory provides a primitive notion of adjacent items: e.g. 3 next to 4. Large objects composed of adjacent smaller objects Sub-objects referenced by offsets or interior pointers. 34

8 Carnegie Mellon University8 Associativity Adjacency is associative: the same memory layout is described by: –(3 next to 4) next to 5 –3 next to (4 next to 5) But not commutative! –3 next to 4  4 next to 3 345

9 Carnegie Mellon University9 Indirection Not all objects are adjacent Memory supports a notion of indirection (pointers or labels). –Refer to non-adjacent data via indirection – 3 next to (pointer to (4 next to 5)) 3 45

10 Carnegie Mellon University10 Ordered Type Theory Linear type theory handles initialization –Doesn’t capture other memory properties Ordered type theory –Variables used exactly once (linear) –Variables may not be permuted. –Adjacent variables remain adjacent –No weakening, contraction, or exchange. Claim: Ordered constructs admit a natural interpretation as adjacency and indirection.

11 Carnegie Mellon University11 Variables and Resources Typing judgments: Ordering of x’s does not matter. –Unrestricted variables, bound to small objects Ordering and usage of a’s does matter. –Bound to memory –Adjacent variables bound to adjacent memory

12 Carnegie Mellon University12 Ordered product Ordered product (fuse): Ordered products model adjacency

13 Carnegie Mellon University13 3 next to 4 –3 ² 4 : int ² int 3 next to 4 next to 5 –3 ² (4 ² 5) : int ² (int ² int) –(3 ² 4) ² 5 : (int ² int) ² int 34 Adjacency 345

14 Carnegie Mellon University14 Memory properties Associativity: –(  1 ²  2 ) ²  3 and  1 ² (  2 ²  3 ) are isomorphic –Functions witness isomorphism Non-commutativity: –  1 ²  2 and  2 ²  1 are not isomorphic –No function mapping one to the other (in general)

15 Carnegie Mellon University15 Indirection Ordered modality models indirection –!M : !  corresponds to a pointer to M –Non-linear, un-ordered term

16 Carnegie Mellon University16 (3,(4,5)) : int x (int x int) 534 534 534

17 Carnegie Mellon University17 (3,(4,5)) : int x (int x int) int x (int x int) Ã !(int ² !(int ² int)) (3,(4,5)) Ã !(3 ² ! (4 ² 5) ) 534

18 Carnegie Mellon University18 (3,(4,5)) : int x (int x int) int x (int x int) Ã ! (! int ² !(! int ² ! int) (3,(4,5)) Ã !(!3 ² ! (!4 ² !5)) 534

19 Carnegie Mellon University19 (3,(4,5)) : int x (int x int) int x (int x int) Ã ! (int ² (int ² int)) (3,(4,5)) Ã !(3 ² (4 ² 5)) 534

20 Carnegie Mellon University20 Explicit Allocation Ordered type theory –Fine structure of data layout –But not allocation For example: !(x ² x) –Each time x is instantiated, new object –Initialized atomically Make allocation explicit –Remove !M from syntax –Add allocation primitives to introduce ! 

21 Carnegie Mellon University21 Memory Allocation A well-known GC allocation protocol for copying garbage collectors: –Reserve: obtain raw, un-initialized space. –Initialize: assign values to individual locations. –Allocate: baptize some or all as valid objects.

22 Carnegie Mellon University22 Example: Memory Allocation Heap APLPAP ???? 120 ReserveInitializeAllocate x = (0,(1,2)) x

23 Carnegie Mellon University23 Memory Allocation Type system separates terms and expressions –Terms M: no effects –Expressions E: have effects Allocation is an effect –Allocation primitives are expressions

24 Carnegie Mellon University24 Resource a is used up!Create names for parts.Reserve space at a. Allocating a Pair Allocate (1,2) : Initialize a 1, using it up. Re-introduce b 1 :int Fuse parts and allocate.

25 Carnegie Mellon University25 Coalescing Reservation Allocate two pairs: (1,2) and (3,4)

26 Carnegie Mellon University26 Coalescing Reservation Allocate two pairs: (1,2) and (3,4)

27 Carnegie Mellon University27 Coalescing Reservation Allocate two pairs: (1,2) and (3,4)

28 Carnegie Mellon University28 Coalescing Reservation Allocate two pairs: (1,2) and (3,4)

29 Carnegie Mellon University29 Summary Type theory for describing data layout –Adjacency requirements. –Precise control over representations. Type system for allocation: –Allocate raw memory. –Initialize, destructively changing types. –Ensures correct use of allocation protocol. –Permits code motion optimizations.

30 Carnegie Mellon University30 What I’m not telling you It’s more subtle than it seems. –Plain ordered –calculus doesn’t work. –Need notion of size preserving terms, other refinements. For details see the paper –Technical presentation and examples. –Interpretation of a  calculus with pairs.

31 Carnegie Mellon University31 Current and Future Work POPL paper –Only finite products Technical Report: –Sums, recursive types, ordered functions. –Extended coercion language. Ongoing –Dynamic extent (arrays) –Other allocation models

32 Carnegie Mellon University32 Conclusion Ordered type theory is a natural framework for modeling data layout. –Low level issues dealt with entirely realistically in a -calculus setting. –Correctness of allocation and initialization protocols can be captured in the type system

33 Carnegie Mellon University33 Ordered product (fuse): Models adjacency –3 next to 4 –3 ² 4 : int ² int 34 Adjacency

34 Carnegie Mellon University34 Ordered Type Theory –Adjacency tracked by context Ordered-ness preserves adjacency –Permits pointers into the middle of objects. –Supports in-place, piecemeal initialization –Worldview no longer object-based, but rather memory based

35 Carnegie Mellon University35 Future Work Extend to handle dynamic extent (arrays) –Probably using dependent types Pointers to partially initialized data Demonstrate use of framework for other allocation models

36 Carnegie Mellon University36 Example: Allocating (3,(4,5))

37 Carnegie Mellon University37 Size Matters Types uniquely determine the size of the values they classify |  1 ²  2 | = |  1 |+|  2 | –The size of a composite object is the sum of the sizes of its components |!  | = 1, |int| = 1 –Pointers and constants are unit size –Pointers “encapsulate” (refer to) large objects Size is crucial for allocation.

38 Carnegie Mellon University38 Memory Allocation High level type systems allocate atomically –Allocate and initialize in “one step”. Our calculus exposes the implementation of high-level objects in terms of memory –Support code motion such as coalescing. –Allow incremental initialization. –But nevertheless ensure safety!

39 Carnegie Mellon University39 Judgements:  ;  ` E :   is an unrestricted context –Maps unrestricted variables x to types  –Corresponds roughly to the register file –If x:  2  then |  |=1 –Types of variables cannot change (mysterious at this point)

40 Carnegie Mellon University40 Judgements (2):  ;  ` E :   is an ordered context –Maps ordered variables a to types  –Describes the reservation area –Adjacent variables in the  are adjacent in memory a 1 :int,a 2 :int a:(int ² int)

41 Carnegie Mellon University41 Memory Allocation Allocation model based on common copying garbage collector implementation Three phases –Reserve raw memory in a designated area (the frontier) –Initialize some or all of the reserved memory –Allocate the memory into the managed heap

42 Carnegie Mellon University42 Reservation Reservation primitive –introduce new uninitialized memory –(This version is a derived form)

43 Carnegie Mellon University43 Initialization Ordered variables are linearl –Linearity handles initialization Initialization primitive –Consumes an ordered variable and introduces a new one –(This version is also a derived form)

44 Carnegie Mellon University44 Allocation May allocate any prefix of memory –Prefix because of particular allocation model –“Move” piece of frontier into the heap –Return a pointer to the new object Q := a | Q 1 ² Q 2 (roughly)

45 Carnegie Mellon University45 Example: calculus pairs A function to allocate and initialize a pair

46 Carnegie Mellon University46 Size Making data layout explicit exposes size (memory requirements Adjacency is additive Indirection is unit Constants are unit An indirection is a small reference to a (potentially) large object

47 Carnegie Mellon University47 Types for Low-Level Languages What is a good type system for a low-level language? –Should expose data representations. –Should allow for low-level “hacks”. –Should be verifiably safe. –Should not compromise efficiency. Current systems make serious compromises. –Very weak safety properties. –Force atomicity of complex operations.

48 Carnegie Mellon University48 Example: Memory Allocation Most type systems take an atomic view of constructors. –Allocate and initialize in “one step”. –Even HLL’s like Java impose restrictions. We’d like to expose the “fine structure”. –Support code motion such as coalescing. –Allow incremental initialization. –But nevertheless ensure safety!

49 Carnegie Mellon University49 Example: Memory Allocation An allocation protocol (used in TILT): –Reserve: obtain raw, un-initialized space. –Initialize: assign values to the parts. –Allocate: baptize as a valid object. Current type systems cannot handle this. –Partially initialized objects. –In-place modification of parts. –Interaction with collected heap.

50 Carnegie Mellon University50 Example: Memory Allocation Heap HP AP ???? LP AP 120 HP

51 Carnegie Mellon University51 Explicit representations Need a type theory for data layout. –A language in which to make representation explicit. –Provide types primitive concepts of data layout. High-level types (such as pairs) given a concrete implementation via an interpretation into the language of data layout.

52 Carnegie Mellon University52 A Low-Level Type System Borrow two ideas from linear logic. –Restricted and unrestricted variables. –A modality to mediate between them. Restricted variables are resources. –Think: pointer into middle of an object. Unrestricted variables are standard. –Think: heap pointer.

53 Carnegie Mellon University53 A Low-Level Type System Variables are bound to valid objects. –Can be used freely. –Garbage collected when inaccessible. Resources are bound to parts of objects-in-creation. –Cannot be passed around freely. –Explicitly allocated and disposed.

54 Carnegie Mellon University54 Restrictions on Resources Linearity: use resources exactly once. –Admits re-typing after initialization. –Ensure allocation before general usage. Ordering: resource adjacency matters. –Admit “pointers into the middle” of objects. –Supports in-place, piecemeal initialization.

55 Carnegie Mellon University55 Variables and Resources Typing judgments: Ordering of x’s does not matter. –Abstract “mobile” locations. Ordering and usage of a’s does matter. –Abstract “pinned” locations, with (a form of) pointer arithmetic.

56 Carnegie Mellon University56 Low-Level Type Constructors Contiguous data:  1 ²  2. –Two contiguous values. –Two adjacent words: int ² int. Mobile data object: ! . –A fully initialized object of type . Example:  1 £  2 := ! (  1 ²  2 ). –A pointer to an adjacent pair of values.

57 Carnegie Mellon University57 Allocating a Pair Allocate (1,2) : Initialize a 1, using it up. Re-introduce a 1. Must be empty on return.h Reserve space at a.Create names for parts. Fuse parts and allocate. Resource a is used up!

58 Carnegie Mellon University58 Coalescing Reservation Allocate (0,(1,2)) :

59 Carnegie Mellon University59 What Have We Gained? The ordered type system ensures: –All reserved data is eventually allocated. –Initialization can happen in any order. –Cannot read un-initialized memory. May also be used for inter-operability. –Adjacency requirements. –Marshalling and un-marshalling. –Precise control over representations.


Download ppt "534 534534 534 534 534 534 A Type Theory for Memory Allocation and Data Layout Leaf Petersen, Robert Harper, Karl Crary and Frank Pfenning Carnegie Mellon."

Similar presentations


Ads by Google