1 / 74

Incrementalized Pointer and Escape Analysis

Incrementalized Pointer and Escape Analysis. Martin Rinard MIT LCS. Context. Unsafe languages (C, C++, …) Safe languages (Java, ML, Scheme, …) Big difference: garbage collection Goal: analyze program to safely allocate objects on the call stack instead of in garbage-collected heap.

kim-sherman
Download Presentation

Incrementalized Pointer and Escape Analysis

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Incrementalized Pointer and Escape Analysis Martin Rinard MIT LCS

  2. Context • Unsafe languages (C, C++, …) • Safe languages (Java, ML, Scheme, …) • Big difference: garbage collection Goal: analyze program to safely allocate objects on the call stack instead of in garbage-collected heap Advantages • No dangling references • No memory leaks Disadvantages • Collection overhead • Collection pauses

  3. Program With Allocation Sites void main(i,j) ——————— ——————— ——————— void compute(d,e) ———— ———— ———— void evaluate(i,j) —————— —————— —————— void multiplyAdd(a,b,c) ————————— ————————— ————————— void abs(r) ———— ———— ———— void scale(n,m) —————— —————— void multiply(m) ———— ———— ———— void add(u,v) —————— ——————

  4. Program With Allocation Sites When program runs void main(i,j) ——————— ——————— ——————— void compute(d,e) ———— ———— ———— void evaluate(i,j) —————— —————— —————— void multiplyAdd(a,b,c) ————————— ————————— ————————— void abs(r) ———— ———— ———— void scale(n,m) —————— —————— void multiply(m) ———— ———— ———— void add(u,v) —————— ——————

  5. Program With Allocation Sites When program runs void main(i,j) ——————— ——————— ——————— void compute(d,e) ———— ———— ———— void evaluate(i,j) —————— —————— —————— void multiplyAdd(a,b,c) ————————— ————————— ————————— void abs(r) ———— ———— ———— void scale(n,m) —————— —————— void multiply(m) ———— ———— ———— void add(u,v) —————— ——————

  6. Program With Allocation Sites When program runs It allocates objects in heap void main(i,j) ——————— ——————— ——————— void compute(d,e) ———— ———— ———— void evaluate(i,j) —————— —————— —————— void multiplyAdd(a,b,c) ————————— ————————— ————————— void abs(r) ———— ———— ———— void scale(n,m) —————— —————— void multiply(m) ———— ———— ———— void add(u,v) —————— ——————

  7. Program With Allocation Sites Program With Allocation Sites When program runs It allocates objects in heap void main(i,j) ——————— ——————— ——————— void compute(d,e) ———— ———— ———— void evaluate(i,j) —————— —————— —————— void multiplyAdd(a,b,c) ————————— ————————— ————————— void abs(r) ———— ———— ———— void scale(n,m) —————— —————— void multiply(m) ———— ———— ———— void add(u,v) —————— ——————

  8. Program With Allocation Sites Program With Allocation Sites When program runs It allocates objects in heap void main(i,j) ——————— ——————— ——————— void compute(d,e) ———— ———— ———— void evaluate(i,j) —————— —————— —————— void multiplyAdd(a,b,c) ————————— ————————— ————————— void abs(r) ———— ———— ———— void scale(n,m) —————— —————— void multiply(m) ———— ———— ———— void add(u,v) —————— ——————

  9. Program With Allocation Sites Program With Allocation Sites When program runs It allocates objects in heap void main(i,j) ——————— ——————— ——————— void compute(d,e) ———— ———— ———— void evaluate(i,j) —————— —————— —————— void multiplyAdd(a,b,c) ————————— ————————— ————————— void abs(r) ———— ———— ———— void scale(n,m) —————— —————— void multiply(m) ———— ———— ———— void add(u,v) —————— ——————

  10. Program With Allocation Sites When objects become unreachable void main(i,j) ——————— ——————— ——————— void compute(d,e) ———— ———— ———— void evaluate(i,j) —————— —————— —————— void multiplyAdd(a,b,c) ————————— ————————— ————————— void abs(r) ———— ———— ———— void scale(n,m) —————— —————— void multiply(m) ———— ———— ———— void add(u,v) —————— ——————

  11. Program With Allocation Sites When objects become unreachable Garbage collector (eventually) reclaims memory void main(i,j) ——————— ——————— ——————— void compute(d,e) ———— ———— ———— void evaluate(i,j) —————— —————— —————— void multiplyAdd(a,b,c) ————————— ————————— ————————— void abs(r) ———— ———— ———— void scale(n,m) —————— —————— void multiply(m) ———— ———— ———— void add(u,v) —————— ——————

  12. Stack Allocation Correlate lifetimes of objects with lifetimes of procedures void main(i,j) ——————— ——————— ——————— void compute(d,e) ———— ———— ———— void evaluate(i,j) —————— —————— —————— void multiplyAdd(a,b,c) ————————— ————————— ————————— void abs(r) ———— ———— ———— void scale(n,m) —————— —————— void multiply(m) ———— ———— ———— void add(u,v) —————— ——————

  13. Stack Allocation Allocate object on activation record of procedure void main(i,j) ——————— ——————— ——————— void compute(d,e) ———— ———— ———— void evaluate(i,j) —————— —————— —————— void multiplyAdd(a,b,c) ————————— ————————— ————————— void abs(r) ———— ———— ———— void scale(n,m) —————— —————— void multiply(m) ———— ———— ———— void add(u,v) —————— ——————

  14. Stack Allocation When procedure returns void main(i,j) ——————— ——————— ——————— void compute(d,e) ———— ———— ———— void evaluate(i,j) —————— —————— —————— void multiplyAdd(a,b,c) ————————— ————————— ————————— void abs(r) ———— ———— ———— void scale(n,m) —————— —————— void multiply(m) ———— ———— ———— void add(u,v) —————— ——————

  15. Stack Allocation When procedure returns Object automatically deallocated void main(i,j) ——————— ——————— ——————— void compute(d,e) ———— ———— ———— void evaluate(i,j) —————— —————— —————— void multiplyAdd(a,b,c) ————————— ————————— ————————— void abs(r) ———— ———— ———— void scale(n,m) —————— —————— void multiply(m) ———— ———— ———— void add(u,v) —————— ——————

  16. Problem • How does compiler determine if it is safe to allocate objects on stack? • Classic problem in program analysis • Standard approach • Analyze whole program • Use information to find “captured” objects • Allocate captured objects on stack

  17. Stack Allocation Percentage of Memory Allocated on Stack 100 80 60 40 20 0 barnes water jlex db raytrace compress Whole-Program Analysis

  18. Normalized Execution Times Reference: execution time without optimization 100 80 60 40 20 0 barnes water jlex db raytrace compress Whole-Program Analysis

  19. Analysis Times 223 645 150 125 100 75 Analysis Time (seconds) 50 25 0 barnes water jlex db raytrace compress Whole-Program Analysis

  20. Key Observation Number One: void main(i,j) ——————— ——————— ——————— Most optimizations require only the analysis of a small part of program surrounding the object allocation site void compute(d,e) ———— ———— ———— void evaluate(i,j) —————— —————— —————— void multiplyAdd(a,b,c) ————————— ————————— ————————— void abs(r) ———— ———— ———— void scale(n,m) —————— —————— void multiply(m) ———— ———— ———— void add(u,v) —————— ——————

  21. Key Observation Number Two: void main(i,j) ——————— ——————— ——————— Most of the optimization benefit comes from a small percentage of the allocation sites void compute(d,e) ———— ———— ———— void evaluate(i,j) —————— —————— —————— void multiplyAdd(a,b,c) ————————— ————————— ————————— void abs(r) ———— ———— ———— void scale(n,m) —————— —————— void multiply(m) ———— ———— ———— void add(u,v) —————— —————— 99% of objects allocated at these two sites

  22. void main(i,j) ——————— ——————— ——————— void evaluate(i,j) —————— —————— —————— void compute(d,e) ———— ———— ———— void abs(r) ———— ———— ———— void multiplyAdd(a,b,c) ————————— ————————— ————————— void scale(n,m) —————— —————— void multiply(m) ———— ———— ———— void add(u,v) —————— —————— 99% of objects allocated at these two sites Intuition for Better Analysis Locate important allocation sites Use demand-driven approach to analyze region surrounding site Somehow avoid sinking analysis resources into unprofitable sites

  23. Example

  24. Vector John Doe Ben Bit Jane Roe $45,000 $30,000 $55,000 Employee Database Example Traverse database, extract max salary Name Salary max salary = $55,000 highest paid = Jane Roe

  25. Coding Max Computation (in Java) class EmployeeDatabase { Vector database = new Vector(); Employee highestPaid; void computeMax() { int max = 0; Enumeration enum = database.elements(); while (enum.hasMoreElements()) { Employee e = enum.nextElement(); if (max < e.salary()) { max = e.salary(); highestPaid = e; } } } }

  26. Coding Max Computation (in Java) class EmployeeDatabase { Vector database = new Vector(); Employee highestPaid; void computeMax() { int max = 0; Enumeration enum = database.elements(); while (enum.hasMoreElements()) { Employee e = enum.nextElement(); if (max < e.salary()) { max = e.salary(); highestPaid = e; } } } } Would like to allocate enum object on stack, not on the heap

  27. Whole Program Analysis

  28. Bottom Up and Compositional void printStatistics() ——————— ——————— ——————— Currently analyzed procedure void computeMax() ———— ———— ———— Enumeration elements() ——— ——— ——— boolean hasMoreElements() —————— —————— —————— Employee nextElement() —————— —————— int salary() —————— —————— Vector elementData() ———— ———— ———— Currently analyzed part of the program

  29. Bottom Up and Compositional void printStatistics() ——————— ——————— ——————— Currently analyzed procedure void computeMax() ———— ———— ———— Enumeration elements() ——— ——— ——— boolean hasMoreElements() —————— —————— —————— Employee nextElement() —————— —————— int salary() —————— —————— Vector elementData() ———— ———— ———— Currently analyzed part of the program

  30. Points-to Graph in Example void computeMax() { int max = 0; Enumeration enum = database.elements(); while (enum.hasMoreElements()) { Employee e = enum.nextElement(); if (max < e.salary()) { max = e.salary(); highestPaid = e; } } } [ ] vector elementData enum database highestPaid this e

  31. Escape Information • Escaped nodes • parameter nodes • returned nodes • nodes reachable from other escaped nodes • Captured is the opposite of escaped [ ] vector elementData enum database highestPaid green = escaped this e white = captured

  32. Stack Allocation Optimization • Examine graph from end of procedure • If a node is captured in this graph • Allocate corresponding objects on stack (may need to inline procedures to apply optimization) Can allocate enum object on stack [ ] vector elementData enum database highestPaid green = escaped this e white = captured

  33. Whole Program Analysis void printStatistics() ——————— ——————— ——————— void computeMax() ———— ———— ———— Enumeration elements() ——— ——— ——— boolean hasMoreElements() —————— —————— —————— Employee nextElement() —————— —————— int salary() —————— —————— Vector elementData() ———— ———— ————

  34. Whole Program Analysis void printStatistics() ——————— ——————— ——————— void computeMax() ———— ———— ———— Enumeration elements() ——— ——— ——— boolean hasMoreElements() —————— —————— —————— Employee nextElement() —————— —————— int salary() —————— —————— Vector elementData() ———— ———— ————

  35. Whole Program Analysis void printStatistics() ——————— ——————— ——————— void computeMax() ———— ———— ———— Enumeration elements() ——— ——— ——— boolean hasMoreElements() —————— —————— —————— Employee nextElement() —————— —————— int salary() —————— —————— Vector elementData() ———— ———— ————

  36. Whole Program Analysis void printStatistics() ——————— ——————— ——————— void computeMax() ———— ———— ———— Enumeration elements() ——— ——— ——— boolean hasMoreElements() —————— —————— —————— Employee nextElement() —————— —————— int salary() —————— —————— Vector elementData() ———— ———— ————

  37. Whole Program Analysis void printStatistics() ——————— ——————— ——————— void computeMax() ———— ———— ———— Enumeration elements() ——— ——— ——— boolean hasMoreElements() —————— —————— —————— Employee nextElement() —————— —————— int salary() —————— —————— Vector elementData() ———— ———— ————

  38. Whole Program Analysis void printStatistics() ——————— ——————— ——————— void computeMax() ———— ———— ———— Enumeration elements() ——— ——— ——— boolean hasMoreElements() —————— —————— —————— Employee nextElement() —————— —————— int salary() —————— —————— Vector elementData() ———— ———— ————

  39. Whole Program Analysis void printStatistics() ——————— ——————— ——————— void computeMax() ———— ———— ———— Enumeration elements() ——— ——— ——— boolean hasMoreElements() —————— —————— —————— Employee nextElement() —————— —————— int salary() —————— —————— Vector elementData() ———— ———— ————

  40. Whole Program Analysis void printStatistics() ——————— ——————— ——————— void computeMax() ———— ———— ———— Enumeration elements() ——— ——— ——— boolean hasMoreElements() —————— —————— —————— Employee nextElement() —————— —————— int salary() —————— —————— Vector elementData() ———— ———— ————

  41. Incrementalized Analysis

  42. Incrementalized Analysis Requirements Must be able to • Analyze procedure independently of callers • Whole-program analysis is compositional • Already does this • Skip analysis of invoked procedures • But later incrementally integrate analysis results if desirable to do so

  43. First Extension to Whole-Program Analysis • Skip the analysis of invoked procedures • Parameters are marked as escaping into skipped call site Assume analysis skips enum.nextElement() 1 vector enum database highestPaid Node 1 escapes into enum.nextElement() this e

  44. First Extension Almost Works • Can skip analysis of invoked procedures • If allocation site is captured, great! • If not, escape information tells you what procedures you should have analyzed… Should have analyzed enum.nextElement() 1 vector enum database highestPaid Node 1 escapes into enum.nextElement() this e

  45. Second Extension to Base Algorithm • Record enough information to undo skip and incorporate analysis into existing result • Parameter mapping at call site • Ordering information for call sites

  46. Incrementalized Analysis void printStatistics() ——————— ——————— ——————— void computeMax() ———— ———— ———— Enumeration elements() ——— ——— ——— boolean hasMoreElements() —————— —————— —————— Employee nextElement() —————— —————— int salary() —————— —————— Vector elementData() ———— ———— ————

  47. Incrementalized Analysis void printStatistics() ——————— ——————— ——————— Attempt to stack allocate Enumeration object from elements void computeMax() ———— ———— ———— Enumeration elements() ——— ——— ——— boolean hasMoreElements() —————— —————— —————— Employee nextElement() —————— —————— int salary() —————— —————— Vector elementData() ———— ———— ————

  48. Incrementalized Analysis void printStatistics() ——————— ——————— ——————— Analyze elements (intraprocedurally) void computeMax() ———— ———— ———— Enumeration elements() ——— ——— ——— boolean hasMoreElements() —————— —————— —————— Employee nextElement() —————— —————— int salary() —————— —————— Vector elementData() ———— ———— ————

  49. Incrementalized Analysis void printStatistics() ——————— ——————— ——————— Analyze elements (intraprocedurally) void computeMax() ———— ———— ———— Enumeration elements() ——— ——— ——— boolean hasMoreElements() —————— —————— —————— Employee nextElement() —————— —————— int salary() —————— —————— Vector elementData() ———— ———— ————

  50. Incrementalized Analysis void printStatistics() ——————— ——————— ——————— Analyze elements (intraprocedurally) Escapes only into the caller void computeMax() ———— ———— ———— Enumeration elements() ——— ——— ——— boolean hasMoreElements() —————— —————— —————— Employee nextElement() —————— —————— int salary() —————— —————— Vector elementData() ———— ———— ————

More Related