1 / 28

Contification Using Dominators

Contification is a technique used in functional language compilers to turn function calls into local control flow transfers, improving running times. This article explains the concept of contification and its benefits.

Download Presentation

Contification Using Dominators

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. Contification Using Dominators Matthew Fluet Cornell University Stephen Weeks InterTrust STAR Lab

  2. Motivation • use traditional optimizations in functional-language compilers • traditional optimizations require intraprocedural control-flow information • in functional languages, most control-flow information is interprocedural • contification: a technique for turning function calls into local control-flow transfers interprocedural  intraprocedural

  3. main() main() main() even(x) if x = 0 even(x) if x = 0 even(x) if x = 0 even(x) if x = 0 even(x) if x = 0 k k ret(true) odd(x-1) ret(true) k(true) ret(true) ret(true) odd(x-1) odd(x-1) odd(x-1) odd(x-1) l() k(even(3)) l() even(3) l() k(even(3)) k k odd(y) if y = 0 odd(y) if y = 0 odd(y) if y = 0 odd(y) if y = 0 odd(y) if y = 0 k(z) ... k(z) ... k(z) ... ret(false) ret(false) k(false) ret(false) ret(false) even(y-1) even(y-1) even(y-1) even(y-1) even(y-1) An Example

  4. MLton • a whole-program optimizing compiler for Standard ML SML defunctorize polymorphic, higher-order IL monomorphise simply-typed, higher-order IL closure convert optimize simply-typed, first-order IL generate code native x86

  5. Contification • when a function g is contified within a function f • intraprocedural control-flow is exposed • g will share the same stack frame as f • invocations of g can be optimized • goal: contify as many functions as possible • components of contification • analysis: what functions are contified and where • transformation: single-pass rewrite of the program

  6. k m f g k f f g g Contification Analysis I • represent a program by its call graph • functions m, f, g, h and continuations k, l • a distinguished main function m • nontail calls • tail calls

  7. Contification Analysis II • an analysis maps functions to abstract return locations Return = {?}  Cont  Func A Analysis = Func  Return • A(g) = fg always returns to function fg is contified in the body of f • A(g) = kg always returns to continuation kg is contified where k is defined and g is transformed to transfer control to k • A(g) = ?g is not contified in the transformed program

  8. k f f g g Safety • safety ensures a well-defined and correct transformation • an analysis A is safe if the following hold • A(m) =? • if then A(g) {k, ?} • if then A(g) {f, A(f), ?}

  9. ? ? m k1 k2 ? ? f f ? f ? g1 g2 l ? ? l l h1 h2 The Acall and Acont Analyses • Acall– a simple syntactic analysis • Acont– a least fixed-point analysis [Reppy 2001]

  10. ? m k1 k2 m k1 k2 l f f h2 g1 g2 l g1 g2 h1 h2 Call Graph h1 Graph G The Adom Analysis I • build a directed graph G, similar to the call graph Node = Return = {?}  Cont  Func • each edge (l, f) indicates that f returns to location l • if l = ?, then f has no return location

  11. ? ? ? m m k1 f k1 k2 k2 l f f f g1 h1 g2 h2 f l Dominator Tree D g1 g2 l h1 h2 Call Graph The Adom Analysis II • build the dominator tree D of G • l dominates f if f always returns to l in any execution of the program • define Adom(f) to be the dominator of f closest to ? • if the immediate dominator of f is ?, then Adom(f) = ?

  12. The Adom Analysis III • Theorem: Adom is safe. • Theorem: Adom is maximal. • for all safe analyses B and all functions f,if B(f)  ?, then Adom(f)  ?

  13. Compile-time Performance • three rounds of contification in the optimizing cycle • contification quiesces after two rounds • contification takes less than 4% of total compile time • typically less than one second, with a maximum of 13 seconds

  14. Run-time Performance • anomalies • contification may disable size-based inlining • MLton’s optimizer evolved around Acall

  15. Conclusions • a simple, yet general, framework for expressing contification analyses • single transformation • safety condition • maximality criterion • contification is efficient and improves running times Got MLton? You should. http://www.sourcelight.com/MLton

  16. Related Transformations • Local CPS conversion [Reppy 2001] • analyzes a module in isolation • operates over a higher-order IL • escaping functions with unknown control-flow cause imprecision • requires local CPS conversion to apply transformation • Lambda dropping [Danvy and Schultz 2000] • does not approximate the returns of a function • does not change calls from tail to nontail, or vice versa • Loop headers [Appel 1994] • transformation local to a particular function • relies on inlining to expose new control-flow

  17. References • A. W. Appel. Loop headers in -calculus or CPS. Lisp and Symbolic Computation, 7:337-343, 1994. • O. Danvy and U. P. Schultz. Lambda-dropping: Transforming recursive equations into programs with block structure. Theoretical Computer Science, 248(1-2):243-287, 2000. • J. Reppy. Local CPS conversion in a direct-style compiler. In Workshop on Continuations, pages 1-5, Jan. 2001.

  18. m m k k k1 k2 f f g g1 h Comparison to Inlining • no substitution of actual arguments for function parameters • no duplication of code

  19. ? k f k m f g The Acall Analysis • intuition: a function f has one return location if • there is exactly one call to f from outside its body; and • there are only tail calls to f within its body • a simple syntactic analysis • original contification analysis used in MLton

  20. ? m k k ? ? f g ? h But… • f, g, and h always return to continuation k • but, the call analysis fails to contify any of the functions

  21. ? fm k k k k f g k h The Acont Analysis • intuition: a function f returns to continuation k if • all nontail calls to f use k; and • all tail callers of f also return to k • a least fixed point ties the recursion • based on a similar analysis in Moby [Reppy 2001]

  22. ?? fm k1 k2 ?? f f? f? g1 g2 ?? h But… • g1, g2, and h all return to function f • but, the call analysis fails to contify h • and, the continuation analysis fails to contify any function

  23. Root m k1 k2 m k1 f k2 l f g1 h1 g2 h2 g1 g2 g3 g3 l Dominator Tree D h1 h2 Call Graph The Adom Analysis IV • setting Adom(f) equal to the immediate dominator of f violates safety • Adom(h1) = f {g3, A(g3), ?} = {g3, g2, ?}

  24. even(x) if x = 0 ret(true) odd(x-1) An Example I

  25. main() even(x) if x = 0 k ret(true) odd(x-1) l() k(even(3)) odd(y) if y = 0 k(z) ... ret(false) even(y-1) An Example II

  26. main() even(x) if x = 0 ret(true) odd(x-1) l() k(even(3)) k odd(y) if y = 0 k(z) ... ret(false) even(y-1) An Example III

  27. main() main() even(x) if x = 0 even(x) if x = 0 even(x) if x = 0 k ret(true) odd(x-1) ret(true) k(true) odd(x-1) odd(x-1) l() k(even(3)) l() even(3) k odd(y) if y = 0 odd(y) if y = 0 odd(y) if y = 0 k(z) ... k(z) ... ret(false) ret(false) k(false) even(y-1) even(y-1) even(y-1) An Example IV

More Related