environment analysis via cfa
play

Environment Analysis via CFA Matthew Might Olin Shivers Georgia - PowerPoint PPT Presentation

Environment Analysis via CFA Matthew Might Olin Shivers Georgia Tech POPL 2006 Control-flow analysis is not enough Problem Closure = term + environment; e.g. , ( () x) + [ x 3 ] CFA: good with control (what


  1. Environment Analysis via ∆ CFA Matthew Might Olin Shivers Georgia Tech POPL 2006

  2. Control-flow analysis is not enough Problem ◮ Closure = λ term + environment; ◮ e.g. , ( λ () x) + [ x �→ 3 ] ◮ CFA: good with control (what λ invoked from which call sites); ◮ . . . not so good with environments.

  3. Control-flow analysis is not enough (let ((f ( λ (x h) (if (zero? x) (h) ( λ () x))))) (f 0 (f 3 #f))) Fact: ( λ () x) flows to (h) . Question: Safe to inline?

  4. Control-flow analysis is not enough (let ((f ( λ (x h) (if (zero? x) (h) ( λ () x))))) (f 0 (f 3 #f))) Fact: ( λ () x) flows to (h) . Question: Safe to inline? Answer: No. Why: Only one variable x in program; but multiple dynamic bindings . ( λ () x) + [ x �→ 0 ] v . ( λ () x) + [ x �→ 3 ]

  5. Control-flow analysis is not enough Folding infinite set of binding contours down to finite set causes merging. Can lead to unsound conclusions. Problem: | x | = | y | does not imply x = y

  6. Why it matters We frequently use closures as general “carriers” of data: ◮ Create closure at point a . ◮ Ship to point b and invoke. a & b have same static scope and same dynamic bindings ⇒ ◮ inline closure’s code at b (super- β inlining), ◮ communicate data via shared context. Avoid heap allocating & fetching data.

  7. Why it matters We frequently use closures as general “carriers” of data: ◮ Create closure at point a . ◮ Ship to point b and invoke. a & b have same static scope and same dynamic bindings ⇒ ◮ inline closure’s code at b (super- β inlining), ◮ communicate data via shared context. Avoid heap allocating & fetching data. Need to reason about environment relationships between two control points.

  8. Tool 1: Procedure strings Classic model (Sharir & Pnueli, Harrison) ◮ Program trace at procedure level ◮ String of procedure activation/deactivation actions Actions control: call/return Intuition: call extends environment; return restores environment.

  9. Tool 1: Procedure strings Classic model (Sharir & Pnueli, Harrison) ◮ Program trace at procedure level ◮ String of procedure activation/deactivation actions Actions control: call/return Intuition: call extends environment; return restores environment. (fact 1) call fact / call zero? / return zero? / call - / return - / call fact / call zero? / return zero? / return fact / call * / return * / return fact Note: Call/return items nest like parens.

  10. Problems with procedure strings ◮ In functional languages, not all calls have matching returns. ( e.g. , iteration) ◮ Procedure strings designed for “large-grain” procedures. ◮ What about other control/env operators? (loops, conditionals, coroutines, continuations, . . . )

  11. Tool 2: CPS λ is universal representation of control & env. Construct encoding fun call call to λ

  12. Tool 2: CPS λ is universal representation of control & env. Construct encoding fun call call to λ fun return call to λ

  13. Tool 2: CPS λ is universal representation of control & env. Construct encoding fun call call to λ fun return call to λ iteration call to λ

  14. Tool 2: CPS λ is universal representation of control & env. Construct encoding fun call call to λ fun return call to λ iteration call to λ sequencing call to λ

  15. Tool 2: CPS λ is universal representation of control & env. Construct encoding fun call call to λ fun return call to λ iteration call to λ sequencing call to λ conditional call to λ

  16. Tool 2: CPS λ is universal representation of control & env. Construct encoding fun call call to λ fun return call to λ iteration call to λ sequencing call to λ conditional call to λ exception call to λ

  17. Tool 2: CPS λ is universal representation of control & env. Construct encoding fun call call to λ fun return call to λ iteration call to λ sequencing call to λ conditional call to λ exception call to λ coroutine call to λ . . . . . . Now λ is fine-grained construct. Adapt procedure-string models to CPS ⇒ have universal analysis.

  18. CPS & stacks But wait! CPS is all calls, no returns! Procedure strings won’t nest properly: call a / call b / call c / call d / . . .

  19. CPS & stacks But wait! CPS is all calls, no returns! Procedure strings won’t nest properly: call a / call b / call c / call d / . . . Unless...

  20. CPS & stacks Solution ◮ Syntactically partition CPS language into “user” & “continuation” world. We still have calls & returns; have just decoupled them somewhat. ◮ Shift from call/return view to push/pop view. Calls & returns no longer nest, but pushes & pops always nest.

  21. Example: recursive factorial ( λ (n) (letrec ((f ( λ (m) (if0 m 1 (* m (f (- m 1))))))) (f n)))

  22. Example: recursive factorial ( λ t (n ktop) (letrec ((f ( λ f (m k) (%if0 m ( λ 1 () (k 1)) ( λ 2 () (- m 1 ( λ 3 (m2) (f m2 ( λ 4 (a) (* m a k) ))))))))) (f n ktop)))

  23. Example: recursive factorial ( λ t (n ktop) (letrec ((f ( λ f (m k) (%if0 m ( λ 1 () (k 1)) ( λ 2 () (- m 1 ( λ 3 (m2) (f m2 ( λ 4 (a) (* m a k) ))))))))) (f n ktop))) But. . . Blue � = call/push Red � = return/pop

  24. Putting it all together: frame strings Frame strings, F ◮ Record push/pop sequences. ◮ Each character: push or pop. ◮ Calls push frames. ◮ Continuations restore stacks.

  25. Anatomy of a push character � � ψ � � � t

  26. Anatomy of a push character Procedure � � ψ � � � t

  27. Anatomy of a push character Procedure � � ψ � � � t Timestamp

  28. Anatomy of a pop character Procedure � � � ψ � � t Timestamp

  29. Net & inverse operators Net Examples ◮ Written ⌊ p ⌋ . ◮ ⌊� a 6 |� b 7 | b 7 �| a 6 �⌋ = ǫ ◮ Cancels opposite neighbors. ◮ ⌊| q 38 �� q 38 |⌋ = ǫ ◮ ⌊� r 21 | r 21 �� a 71 |⌋ = � a 71 |

  30. Net & inverse operators Net Examples ◮ Written ⌊ p ⌋ . ◮ ⌊� a 6 |� b 7 | b 7 �| a 6 �⌋ = ǫ ◮ Cancels opposite neighbors. ◮ ⌊| q 38 �� q 38 |⌋ = ǫ ◮ ⌊� r 21 | r 21 �� a 71 |⌋ = � a 71 | Two views Absolute ⌊ p t ⌋ is picture of stack at time t . Relative ⌊ p t ′ t ⌋ is summary of stack change.

  31. Net & inverse operators p − 1 = reverse ⌊ p ⌋ and swap “push” & “pop”: Example � � − 1 = | c � a 4 |� b 5 | b 5 �� c 6 �| a 6 | 4 � Frame strings mod ⌊·⌋ is group: p + p − 1 ≡ p − 1 + p ≡ ǫ . ( + is concatenation)

  32. The inverse operator Use: restoring stack to previous state Time Frame string Stack t 1 p ⌊ p ⌋ t 2 p + q ⌊ p + q ⌋ t 3 p + q + ??? ⌊ p ⌋

  33. The inverse operator Use: restoring stack to previous state Time Frame string Stack t 1 p ⌊ p ⌋ t 2 p + q ⌊ p + q ⌋ p + q + q − 1 t 3 ⌊ p ⌋ This is what continuations do in CPS. . . but expressed in terms of change .

  34. Iterative factorial example ( λ t (n ktop) (letrec ((f ( λ f (m a k) (%if0 m ( λ 1 () (k a)) ( λ 2 () (- m 1 ( λ 3 (m2) (* m a ( λ 4 (a2) (f m2 a2 k) ))))))))) (f n 1 ktop))) Call site Description Stack ∆ Stack

  35. Iterative factorial example ( λ t (n ktop) (letrec ((f ( λ f (m a k) (%if0 m ( λ 1 () (k a)) ( λ 2 () (- m 1 ( λ 3 (m2) (* m a ( λ 4 (a2) (f m2 a2 k) ))))))))) (f n 1 ktop))) Call site Description Stack ∆ Stack � t 1 |

  36. Iterative factorial example ( λ t (n ktop) (letrec ((f ( λ f (m a k) (%if0 m ( λ 1 () (k a)) ( λ 2 () (- m 1 ( λ 3 (m2) (* m a ( λ 4 (a2) (f m2 a2 k) ))))))))) (f n 1 ktop))) Call site Description Stack ∆ Stack � t 1 | | t 1 �� f � f tail call to λ f 2 | 2 | (f n 1 ktop)

  37. Iterative factorial example ( λ t (n ktop) (letrec ((f ( λ f (m a k) (%if0 m ( λ 1 () (k a)) ( λ 2 () (- m 1 ( λ 3 (m2) (* m a ( λ 4 (a2) (f m2 a2 k) ))))))))) (f n 1 ktop))) Call site Description Stack ∆ Stack � t 1 | | t 1 �� f � f tail call to λ f 2 | 2 | (f n 1 ktop) � %if0 � f 2 |� %if0 | | (%if0 m ...) call to %if0 3 3

  38. Iterative factorial example ( λ t (n ktop) (letrec ((f ( λ f (m a k) (%if0 m ( λ 1 () (k a)) ( λ 2 () (- m 1 ( λ 3 (m2) (* m a ( λ 4 (a2) (f m2 a2 k) ))))))))) (f n 1 ktop))) Call site Description Stack ∆ Stack � t 1 | | t 1 �� f � f tail call to λ f 2 | 2 | (f n 1 ktop) � %if0 � f 2 |� %if0 | | (%if0 m ...) call to %if0 3 3 �� 2 2 |� 2 | %if0 � f %if0 internal return to λ 2 4 | 4 | 3

  39. Iterative factorial example ( λ t (n ktop) (letrec ((f ( λ f (m a k) (%if0 m ( λ 1 () (k a)) ( λ 2 () (- m 1 ( λ 3 (m2) (* m a ( λ 4 (a2) (f m2 a2 k) ))))))))) (f n 1 ktop))) Call site Description Stack ∆ Stack � t 1 | | t 1 �� f � f tail call to λ f 2 | 2 | (f n 1 ktop) � %if0 � f 2 |� %if0 | | (%if0 m ...) call to %if0 3 3 �� 2 2 |� 2 | %if0 � f %if0 internal return to λ 2 4 | 4 | 3 � - 4 |� - � f 2 |� 2 (- m 1 ...) call to - 5 | 5 |

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend