20010903-ICFP01.ppt

Download Report

Transcript 20010903-ICFP01.ppt

Contification Using Dominators
Matthew Fluet
Cornell University
Stephen Weeks
InterTrust STAR Lab
Motivation
• use traditional optimizations in functionallanguage 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
An Example
main()
k
l()
even(3)
k(even(3))
k
even(x)
if x = 0
ret(true)
k(true)
odd(x-1)
odd(y)
if y = 0
k(z)
...
ret(false)
k(false)
even(y-1)
MLton
• a whole-program optimizing compiler for Standard ML
SML
defunctorize
polymorphic, higher-order IL
monomorphise
simply-typed, higher-order IL
closure convert
simply-typed, first-order IL
generate code
native x86
optimize
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
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 f k g
g
– tail calls f
m
k
f
g
Contification Analysis II
• an analysis maps functions to abstract return locations
Return = {?}  Cont  Func
A  Analysis = Func  Return
• A(g) = f
• A(g) = k
• A(g) = ?
g always returns to function f
g is contified in the body of f
g always returns to continuation k
g is contified where k is defined
and g is transformed to transfer control to k
g is not contified in the transformed program
Safety
•
•
safety ensures a well-defined and correct transformation
an analysis A is safe if the following hold
1. A(m) = ?
2. if
f
3. if
f
k
g
then A(g)  {k, ?}
g
then A(g)  {f, A(f), ?}
The Acall and Acont Analyses
• Acall – a simple syntactic analysis
• Acont – a least fixed-point analysis [Reppy 2001]
??
m
k1
k2
??
f
f?
f?
g1
g2
l
??
l l
h1
h2
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
m
k1
?
k2
f
g1
m
k1
g2
k2
f
h2
l
h1
h2
g1
l
g2
Call Graph
h1
Graph G
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) = ?
m
?
k1
m
k1
g1
f
h1
k2
g2
Dominator Tree D
h2
k2
f
l
f
g1
?
?
g2
f
l
f
h1
h2
Call Graph
l
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)  ?
Compile-time Performance
total functions (normalized)
0.7
Call
Cont
Dom
0.6
0.5
0.4
0.3
0.2
0.1
0
barneshut
countgraphs
lexgen
mlton
mlyacc
raytrace
tensor
• 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
Run-time Performance
run-time (normalized)
1.2
Call
Cont
Dom
1
0.8
0.6
0.4
0.2
0
barneshut
countgraphs
lexgen
mlton
mlyacc
raytrace
• anomalies
– contification may disable size-based inlining
– MLton’s optimizer evolved around Acall
tensor
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
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
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):243287, 2000.
• J. Reppy. Local CPS conversion in a direct-style compiler.
In Workshop on Continuations, pages 1-5, Jan. 2001.
Comparison to Inlining
• no substitution of actual arguments for function parameters
• no duplication of code
m
k1
m
k2
f
g1
k
k
f
g
h
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
?
m
k
k
f
f
g
• original contification analysis used in MLton
But…
?
k
m
k
?
?
f
g
?
h
• f, g, and h always return to continuation k
• but, the call analysis fails to contify any of the functions
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
?
k
fm
k
k
k
f
g
k
h
• based on a similar analysis in Moby [Reppy 2001]
But…
??
k1
fm
f
k2
??
f?
f?
g1
g2
??
h
• 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
The Adom Analysis IV
• setting Adom(f) equal to the immediate dominator of f violates safety
– Adom(h1) = f  {g3, A(g3), ?} = {g3, g2, ?}
m
k1
Root
k2
f
g1
m
g2
g3
l
k1
f
k2
l
g1
h1
g2
h2
g3
Dominator Tree D
h1
h2
Call Graph
An Example I
even(x)
if x = 0
ret(true)
odd(x-1)
An Example II
main()
k
l()
k(even(3))
even(x)
if x = 0
ret(true)
odd(x-1)
odd(y)
if y = 0
k(z)
...
ret(false)
even(y-1)
An Example III
main()
even(x)
if x = 0
l()
k(even(3))
k
ret(true)
odd(x-1)
odd(y)
if y = 0
k(z)
...
ret(false)
even(y-1)
An Example IV
main()
k
l()
even(3)
k(even(3))
k
even(x)
if x = 0
ret(true)
k(true)
odd(x-1)
odd(y)
if y = 0
k(z)
...
ret(false)
k(false)
even(y-1)