Download presentation
Presentation is loading. Please wait.
Published byPernille Kjeldsen Modified over 5 years ago
1
An Example of Program Analysis: Inserting Safe Memory Re-use Commands into ML-like Programs
Oukseh Lee, Hongseok Yang, and Kwangkeun Yi {cookcu; hyang; Research On Program Analysis System, KAIST December 5, 2002 CS520
2
Motivation (1/2) Thanks to many compilation techniques, ML can produce fast code, but still has the overhead for managing the memory. Garbage collection is usually faster than the manual memory management. Then what is the reason of the memory inefficiency?
3
Motivation (2/2) Unnecessary allocation in ML programs: 1 2 3 5 6 1 2
fun insert i l = case l of [] => i::[] | h::t => if i<h then i::l else let z = insert i t in h::z 1 2 3 5 6 1 2 3 4
4
Our Goal To insert safe memory-reuse commands: 1 2 3 5 6 4
fun insert i l = case l of [] => i::[] | h::t => if i<h then i::l else let z = insert i t in (free l; h::z) 1 2 3 5 6 4
5
How to Find it? By an accurate but effective program analysis:
Individual heap-cell as the unit of analysis. Context pruning at the conditional branches. Keeping sharing information using multi-set formula. Parameterized analysis, so poly-variance at function calls. Deallocation inside a function can be conditioned by extra boolean parameters (called dynamic flags).
6
Experimental Result
7
Example: copyright free a? Tree a will not be used later
fun copyright a = case a of Leaf => Leaf | Node(a1,a2) => let r = copyright a2 in Node(a1, r) free a?
8
Key: Precise Separation
Tree a will not be used later fun copyright a = case a of Leaf => Leaf | Node(a1,a2) => let r = copyright a2 in Node(a1, r) # all left subtrees # all result trees free a?
9
Safe Only Under the Assumption
Tree a will not be used later fun copyright a = case a of Leaf => Leaf | Node(a1,a2) => let r = copyright a2 in (free a; Node(a1, r)) Tree a has no shared sub-parts X
10
Passing the Assumption in Run-time
Tree a may have shared sub-parts Tree a will not be used later fun copyright [b,bs] a = case a of Leaf => Leaf | Node(a1,a2) => let r = copyright [bÆ:bs,bs] a2 in (free a when b; Node(a1, r))
11
Input Language
12
Output Language
13
First Step Memory-usage analysis: free-commands insertion:
Computes the memory-type (abstraction of heap objects) and usage for each expression. free-commands insertion: Inserts free-commands and adds dynamic flags using the result from the memory-usage analysis.
14
How to Represent a Set of Heap Objects?
x x x x let x = ... in ...
15
A Set of Locations (NO!) Not enough for sharing information. x x y y
x = Node(Leaf,Leaf) y = Node(x,x) x = Node(Leaf,Leaf) y = Node(x,Leaf) x y x y
16
Multi-set Formula Describes a set of locations with sharing information: note:
17
Memory-Type for a Tree Describes a set of trees: root left right note:
18
Memory-Type for a Function
Describes the behavior of the function. The memory-type of function copyright is: input new result usage
19
Analysis fun copyright a = case a of Leaf => Leaf
| Node(a1,a2) => let r = copyright a2 in Node(a1, r)
20
Second Step Memory-usage analysis:
Computes the memory-type and usage for each expression. free-commands insertion: Inserts free-commands and adds dynamic flags using the result from the memory-usage analysis.
21
Insertion (1/3) fun copyright [b,bs] a = case a of Leaf => Leaf
| Node(a1,a2) => let r = copyright a2 in Node (a1, r) b: it is safe to free the input (excluding the result) bs: the input may have shared sub-parts.
22
Insertion (2/3) b :bs fun copyright [b,bs] a = case a of
Leaf => Leaf | Node(a1,a2) => let r = copyright a2 in Node (a1, r) b :bs
23
Insertion (3/3) a: fun copyright [b,bs] a = case a of Leaf => Leaf
| Node(a1,a2) => let r = copyright [bÆ:bs,bs] a2 in Node (a1, r) b a: true true
24
Result fun copyright [b,bs] a = case a of Leaf => Leaf
| Node(a1,a2) => let r = copyright [bÆ:bs,bs] a2 in (free a when b; Node (a1, r))
25
Conclusion We present a static analysis and source-level transformation for inserting safe memory-reuse commands. Reuse ratio: % Analysis cost: lines/sec Weak for prevalent sharing but enough for usual cases. Plan to apply this analysis to our nML compiler.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.