Theoremadvanced
Correspondence of SSA and functional programs
Also called SSA is functional programming.
SSA blocks correspond to local functions and phi operands to arguments of jumps, so SSA is a restricted form of CPS or ANF.
A procedure in static single assignment form is a functional program in disguise. Each basic block is a local function, the phi-functions at the start of a block are its parameters, and a jump to the block is a call that passes the phi operands of that edge as arguments. Nest each block function inside the block that immediately dominates it, and every variable is in lexical scope wherever SSA allows a use: dominance becomes scope. Every call of a block function is a tail call, and no block function escapes, so the result is a program in continuation-passing style whose local continuations are only called, or a program in A-normal form. Kelsey (1995) gave the translations in both directions, and Appel (1998) stated the correspondence in the paper "SSA is functional programming".
Statement
Let be a procedure with parameters in strict SSA form: every name has one definition, and the definition of each name dominates each of its uses, where a phi operand for the edge counts as a use at the end of . Let be the dominator tree of its control flow graph. Define, for each block with phi-functions , instructions and children in :
where each instruction becomes , and is for a jump to , for a branch, and for return v. Here lists the operands of the phi-functions of for the edge . The translation of is
Theorem. For every procedure in strict SSA form:
- Dominance is scope. Every variable of other than and is bound around each of its uses. A name defined in block is in scope in the body of exactly when dominates ; each block name is in scope at every jump to .
- Blocks are local functions. Every call of a block function is a tail call, and block functions occur only as the operator of such calls. So is a CPS term in which the continuation is the only continuation that is passed, and the block functions are local continuations. Replacing by gives a term in A-normal form.
- Phi operands are call arguments. On every input, and applied to the same arguments and to the continuation give the same result, or both run forever. The parallel assignment of the phi-functions of on the edge is the binding of the arguments to the parameters of .
The translation has size linear in the size of plus the number of phi operands.
Example. A loop that sums , in SSA form and translated. The dominator tree has entry above loop, and loop above body and exit.
entry: i0 = 0; s0 = 0; goto loop fun n k ->
loop: i1 = φ(i0, i2); s1 = φ(s0, s2) letrec entry () =
c = i1 < n; if c goto body else exit let i0 = 0 in let s0 = 0 in
body: s2 = s1 + i1; i2 = i1 + 1; goto loop letrec loop (i1, s1) =
exit: return s1 let c = i1 < n in
letrec body () = let s2 = s1 + i1 in
let i2 = i1 + 1 in loop (i2, s2)
and exit () = k s1
in if c then body () else exit ()
in loop (i0, s0)
in entry ()
The phi-functions of loop are its parameters. The jump from entry passes (i0, s0), and the back edge from body passes (i2, s2). The body of body reads i1 and s1 from the enclosing function loop, because loop dominates body.
In plain words
Each block of the program becomes a small local function. Its phi-functions become its parameters. A jump to a block becomes a call of that function, and the values that the phi-functions would pick for that jump become the arguments. Each block function is written inside the function of the block that comes before it on every path, so it can read every name that SSA lets it read. No block function ever returns to the place that called it, so all of these calls are jumps. The result is an ordinary functional program that does the same thing.
Proof
Part 1, scope. The variables bound around a point in the body of are: the parameters ; the phi targets and instruction targets of every proper ancestor of in , because is nested in the bodies of these blocks after their instructions; the phi targets of ; and the instruction targets of before the point. These are exactly the names whose definitions dominate the point, since a block dominates if and only if is an ancestor of in . Strict SSA says that each use is dominated by its definition, so each use is in scope. For a phi operand on the edge , the use is at the end of , where is, and the same argument applies. For a jump from to : every dominator of other than dominates , because a path to followed by the edge is a path to . So is or an ancestor of , and the letrec of the children of , which binds , encloses . If dominates (a back edge), is bound by the letrec that encloses its own body, which also encloses . In both cases the name is in scope at the jump.
Part 2, tail calls. A block function appears only in , as the operator of a call that is the whole term or a whole branch of the final if. Both are tail positions, and nothing else uses the name. The term is the only use of , also in tail position. So the term meets the grammar of CPS terms with the block functions as continuations that are bound and called but never passed.
Part 3, same results. Use the operational semantics of SSA: a state is the current block and a map from names to values. Entering from first evaluates all operands in and then assigns them to the phi targets of at once; then the instructions run in order and update ; then the terminator picks the next block or returns. For the functional program use call-by-value evaluation with environments and closures. Each run of the body of a block creates new closures for the children of in .
Invariant. When the SSA program enters block , the functional program calls , and the call uses a closure of whose chain of enclosing activations contains, for each proper ancestor of in , the most recent run of the body of .
The invariant holds at the start, where is called with no enclosing block. Suppose it holds when the program enters , and the program then goes to . In the functional program, the body of runs and calls . The name refers to the closure made by the most recent run of that encloses the current activation, because is or an ancestor of (Part 1). By the invariant for , plus the run of that has just happened, that run is the most recent run of , and its chain gives the most recent runs of the ancestors of . So the invariant holds for .
Values agree. Let be a name used in and defined in , a dominator of . In SSA, is the value from the most recent run of , since has no other definition. In the functional program, the use of refers to the binding in the activation of on the closure chain, which is the most recent run of by the invariant, or to the current run if . So the two values are equal. This needs that no other block assigns in between, which single assignment gives, and that a run of after the closure was made would have created a new closure: if control leaves the blocks dominated by and comes back to , it must pass again, since otherwise a path from the entry to would avoid .
Phi-functions. A call evaluates all arguments in the environment of , where they have the values , and only then binds them to the parameters. That is the parallel assignment of the phi-functions. Instructions are lets in the same order and compute the same values, a branch tests the same value, and return v calls with . So the sequences of blocks visited are the same, both programs return the same value, and if one runs forever so does the other.
The size bound holds because each block, instruction and phi operand gives one binding, one let or one argument.
Consequences
- Optimizations transfer. An SSA optimization is a transformation of the functional program, and the other way round. Chakravarty, Keller and Zadarnowski (2003) do sparse conditional constant propagation on the A-normal form translation. Copy propagation in SSA is substitution of a variable for a variable, which is always safe because names are not reassigned.
- Compiling CPS to machine code. Block functions are known, never escape and are only called in tail position. A compiler can give them no closures, keep their free variables in registers, and compile each call as a move of arguments followed by a jump. That is what CPS compilers do with local continuations, as Kennedy (2007) describes.
- The converse. Kelsey (1995) also translates CPS programs to SSA: if the local continuations of a CPS program are only bound and called, never passed as arguments, each of them becomes a block, and its parameters become phi-functions.
- Out of SSA as argument passing. A call binds its arguments to its parameters at once, so the lost-copy and swap problems of out-of-SSA translation do not arise in the functional reading. They come back when the parallel binding is turned into a sequence of moves.
- Lambda lifting gives the flat form. Applying lambda lifting to moves every block to the top level and passes its free variables as extra parameters. The result is a program in which every live variable is passed at every jump, a form with many more phi-functions than minimal SSA.
Tightness
- Strictness is needed. If a use is not dominated by its definition, as in non-strict SSA where a variable may be undefined on some path, the use lies outside the scope of its binding in . The functional program then has a free variable.
- Nesting by dominance is needed. With all block functions in one flat
letrecat the top, blocks could not read the names of other blocks. Then every name used outside its block must be passed explicitly, which is what lambda lifting does. - CPS is strictly more general. A CPS program can pass a continuation as an argument, store it, or call it from a deeper procedure, as with exceptions or
call/cc. Such a program has no SSA form within one procedure: the correspondence covers exactly the CPS programs whose local continuations do not escape. - Phi-functions only at block entry. The translation uses the fact that phi-functions sit at the start of blocks and read their operands at the end of predecessors. Forms that also split at branches, such as SSI form, translate the same way, with the split names as parameters of the successor blocks.
History
Kelsey (1995) presented the correspondence at the ACM SIGPLAN workshop on intermediate representations, with translations in both directions between SSA and CPS with local continuations. Appel (1998) made the same point for a wider audience in "SSA is functional programming", with blocks as nested functions and dominance as scope, and described functional intermediate forms in his compiler textbook of the same year. Chakravarty, Keller and Zadarnowski (2003) formalized the translation into A-normal form and carried an SSA optimization over to it. Kennedy (2007) argued for a CPS intermediate language with local continuations that are compiled as jumps.
References
- Richard A. Kelsey, A correspondence between continuation passing style and static single assignment form, ACM SIGPLAN Workshop on Intermediate Representations (IR 1995), ACM SIGPLAN Notices 30(3), 1995, pages 13-22, the translations from SSA to CPS and from CPS with non-escaping local continuations to SSA
- Andrew W. Appel, SSA is functional programming, ACM SIGPLAN Notices 33(4), 1998, pages 17-20, the reading of SSA blocks as nested functions, of phi-functions as parameters, and of dominance as lexical scope
- Manuel M. T. Chakravarty, Gabriele Keller and Patryk Zadarnowski, A functional perspective on SSA optimisation algorithms, Compiler Optimization Meets Compiler Verification (COCV 2003), Electronic Notes in Theoretical Computer Science 82(2), pages 347-361, the formal translation of SSA programs into A-normal form, and sparse conditional constant propagation done on the translated programs
- Ron Cytron, Jeanne Ferrante, Barry K. Rosen, Mark N. Wegman and F. Kenneth Zadeck, Efficiently computing static single assignment form and the control dependence graph, ACM Transactions on Programming Languages and Systems 13(4), 1991, pages 451-490, the definition of SSA form, with phi-functions and the property that each definition dominates its uses
- Andrew W. Appel, Modern Compiler Implementation in ML, Cambridge University Press, 1998, the chapter on static single-assignment form, the section on functional intermediate forms
- Andrew Kennedy, Compiling with continuations, continued, Proceedings of the 12th ACM SIGPLAN International Conference on Functional Programming (ICFP 2007), pages 177-190, a CPS intermediate language with local continuations that are bound by a letcont form and compiled as jumps
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W4 complete |
|
Tag M0C7VW Edit this page History Report an error Markdown