Static single-assignment form explained

In compiler design, static single assignment form (often abbreviated as SSA form or simply SSA) is a type of intermediate representation (IR) where each variable is assigned exactly once. SSA is used in most high-quality optimizing compilers for imperative languages, including LLVM, the GNU Compiler Collection, and many commercial compilers.

There are efficient algorithms for converting programs into SSA form. To convert to SSA, existing variables in the original IR are split into versions, new variables typically indicated by the original name with a subscript, so that every definition gets its own version. Additional statements that assign to new versions of variables may also need to be introduced at the join point of two control flow paths. Converting from SSA form to machine code is also efficient.

SSA makes numerous analyses needed for optimizations easier to perform, such as determining use-define chains, because when looking at a use of a variable there is only one place where that variable may have received a value. Most optimizations can be adapted to preserve SSA form, so that one optimization can be performed after another with no additional analysis. The SSA based optimizations are usually more efficient and more powerful than their non-SSA form prior equivalents.

In functional language compilers, such as those for Scheme and ML, continuation-passing style (CPS) is generally used. SSA is formally equivalent to a well-behaved subset of CPS excluding non-local control flow, so optimizations and transformations formulated in terms of one generally apply to the other. Using CPS as the intermediate representation is more natural for higher-order functions and interprocedural analysis. CPS also easily encodes call/cc, whereas SSA does not.[1]

History

SSA was developed in the 1980s by several researchers at IBM. Kenneth Zadeck, a key member of the team, moved to Brown University as development continued.[2] A 1986 paper introduced birthpoints, identity assignments, and variable renaming such that variables had a single static assignment.[3] A subsequent 1987 paper by Jeanne Ferrante and Ronald Citron[4] proved that the renaming done in the previous paper removes all false dependencies for scalars.[2] In 1988, Barry Rosen, Mark N. Wegman, and Kenneth Zadeck replaced the identity assignments with Φ-functions, introduced the name "static single-assignment form", and demonstrated a now-common SSA optimization.[5] The name Φ-function was chosen by Rosen to be a more publishable version of "phony function".[2] Alpern, Wegman, and Zadeck presented another optimization, but using the name "static single assignment".[6] Finally, in 1989, Rosen, Wegman, Zadeck, Cytron, and Ferrante found an efficient means of converting programs to SSA form.[7]

Benefits

The primary usefulness of SSA comes from how it simultaneously simplifies and improves the results of a variety of compiler optimizations, by simplifying the properties of variables. For example, consider this piece of code:

y := 1 y := 2 x := y

Humans can see that the first assignment is not necessary, and that the value of y being used in the third line comes from the second assignment of y. A program would have to perform reaching definition analysis to determine this. But if the program is in SSA form, both of these are immediate:

y1 := 1 y2 := 2 x1 := y2

Compiler optimization algorithms that are either enabled or strongly enhanced by the use of SSA include:

Converting to SSA

Converting ordinary code into SSA form is primarily a matter of replacing the target of each assignment with a new variable, and replacing each use of a variable with the "version" of the variable reaching that point. For example, consider the following control-flow graph:

Changing the name on the left hand side of "x

\leftarrow

x - 3" and changing the following uses of x to that new name would leave the program unaltered. This can be exploited in SSA by creating two new variables: x1 and x2, each of which is assigned only once. Likewise, giving distinguishing subscripts to all the other variables yields:

It is clear which definition each use is referring to, except for one case: both uses of y in the bottom block could be referring to either y1 or y2, depending on which path the control flow took.

To resolve this, a special statement is inserted in the last block, called a Φ (Phi) function. This statement will generate a new definition of y called y3 by "choosing" either y1 or y2, depending on the control flow in the past.

Now, the last block can simply use y3, and the correct value will be obtained either way. A Φ function for x is not needed: only one version of x, namely x2 is reaching this place, so there is no problem (in other words, Φ(x2,x2)=x2).

Given an arbitrary control-flow graph, it can be difficult to tell where to insert Φ functions, and for which variables. This general question has an efficient solution that can be computed using a concept called dominance frontiers (see below).

Φ functions are not implemented as machine operations on most machines. A compiler can implement a Φ function by inserting "move" operations at the end of every predecessor block. In the example above, the compiler might insert a move from y1 to y3 at the end of the middle-left block and a move from y2 to y3 at the end of the middle-right block. These move operations might not end up in the final code based on the compiler's register allocation procedure. However, this approach may not work when simultaneous operations are speculatively producing inputs to a Φ function, as can happen on wide-issue machines. Typically, a wide-issue machine has a selection instruction used in such situations by the compiler to implement the Φ function.

Computing minimal SSA using dominance frontiers

In a control-flow graph, a node A is said to a different node B if it is impossible to reach B without passing through A first. In other words, if node B is reached, then it can be assumed that A has run. A is said to dominate B (or B to be dominated by A) if either A strictly dominates B or A = B.

A node which transfers control to a node A is called an of A.

The of node A is the set of nodes B where A strictly dominate B, but does dominate some immediate predecessor of B. These are the points at which multiple control paths merge back together into a single path.

For example, in the following code:

[1] x = random
if x < 0.5
    [2] result = "heads"
else
    [3] result = "tails"
end
[4] print(result)

node 1 strictly dominates 2, 3, and 4 and the immediate predecessors of node 4 are nodes 2 and 3.

Dominance frontiers define the points at which Φ functions are needed. In the above example, when control is passed to node 4, the definition of result used depends on whether control was passed from node 2 or 3. Φ functions are not needed for variables defined in a dominator, as there is only one possible definition that can apply.

There is an efficient algorithm for finding dominance frontiers of each node. This algorithm was originally described in "Efficiently Computing Static Single Assignment Form and the Control Graph" by Ron Cytron, Jeanne Ferrante, et al. in 1991.[9]

Keith D. Cooper, Timothy J. Harvey, and Ken Kennedy of Rice University describe an algorithm in their paper titled A Simple, Fast Dominance Algorithm:[10]

for each node b dominance_frontier(b) := for each node b if the number of immediate predecessors of b ≥ 2 for each p in immediate predecessors of b runner := p while runner ≠ idom(b) dominance_frontier(runner) := dominance_frontier(runner) ∪ runner := idom(runner)

In the code above, idom(b) is the of b, the unique node that strictly dominates b but does not strictly dominate any other node that strictly dominates b.

Variations that reduce the number of Φ functions

"Minimal" SSA inserts the minimal number of Φ functions required to ensure that each name is assigned a value exactly once and that each reference (use) of a name in the original program can still refer to a unique name. (The latter requirement is needed to ensure that the compiler can write down a name for each operand in each operation.)

However, some of these Φ functions could be dead. For this reason, minimal SSA does not necessarily produce the fewest Φ functions that are needed by a specific procedure. For some types of analysis, these Φ functions are superfluous and can cause the analysis to run less efficiently.

Pruned SSA

Pruned SSA form is based on a simple observation: Φ functions are only needed for variables that are "live" after the Φ function. (Here, "live" means that the value is used along some path that begins at the Φ function in question.) If a variable is not live, the result of the Φ function cannot be used and the assignment by the Φ function is dead.

Construction of pruned SSA form uses live-variable information in the Φ function insertion phase to decide whether a given Φ function is needed. If the original variable name isn't live at the Φ function insertion point, the Φ function isn't inserted.

Another possibility is to treat pruning as a dead-code elimination problem. Then, a Φ function is live only if any use in the input program will be rewritten to it, or if it will be used as an argument in another Φ function. When entering SSA form, each use is rewritten to the nearest definition that dominates it. A Φ function will then be considered live as long as it is the nearest definition that dominates at least one use, or at least one argument of a live Φ.

Semi-pruned SSA

Semi-pruned SSA form[11] is an attempt to reduce the number of Φ functions without incurring the relatively high cost of computing live-variable information. It is based on the following observation: if a variable is never live upon entry into a basic block, it never needs a Φ function. During SSA construction, Φ functions for any "block-local" variables are omitted.

Computing the set of block-local variables is a simpler and faster procedure than full live-variable analysis, making semi-pruned SSA form more efficient to compute than pruned SSA form. On the other hand, semi-pruned SSA form will contain more Φ functions.

Block arguments

Block arguments are an alternative to Φ functions that is representationally identical but in practice can be more convenient during optimization. Blocks are named and take a list of block arguments, notated as function parameters. When calling a block the block arguments are bound to specified values. Swift SIL and MLIR use block arguments.[12]

Converting out of SSA form

SSA form is not normally used for direct execution (although it is possible to interpret SSA[13]), and it is frequently used "on top of" another IR with which it remains in direct correspondence. This can be accomplished by "constructing" SSA as a set of functions that map between parts of the existing IR (basic blocks, instructions, operands, etc.) and its SSA counterpart. When the SSA form is no longer needed, these mapping functions may be discarded, leaving only the now-optimized IR.

Performing optimizations on SSA form usually leads to entangled SSA-Webs, meaning there are Φ instructions whose operands do not all have the same root operand. In such cases color-out algorithms are used to come out of SSA. Naive algorithms introduce a copy along each predecessor path that caused a source of different root symbol to be put in Φ than the destination of Φ. There are multiple algorithms for coming out of SSA with fewer copies, most use interference graphs or some approximation of it to do copy coalescing.[14]

Extensions

Extensions to SSA form can be divided into two categories.

Renaming scheme extensions alter the renaming criterion. Recall that SSA form renames each variable when it is assigned a value. Alternative schemes include static single use form (which renames each variable at each statement when it is used) and static single information form (which renames each variable when it is assigned a value, and at the post-dominance frontier).

Feature-specific extensions retain the single assignment property for variables, but incorporate new semantics to model additional features. Some feature-specific extensions model high-level programming language features like arrays, objects and aliased pointers. Other feature-specific extensions model low-level architectural features like speculation and predication.

Compilers using SSA form

Open-source

Commercial

Research and abandoned

References

General references

External links

Notes and References

  1. Book: Richard A. . Kelsey. Papers from the 1995 ACM SIGPLAN workshop on Intermediate representations . A correspondence between continuation passing style and static single assignment form . 1995 . 13–22. 0897917545 . 10.1145/202529.202532 . 6207179 . https://www.cs.purdue.edu/homes/suresh/502-Fall2008/papers/kelsey-ssa-cps.pdf.
  2. The Development of Static Single Assignment Form. Static Single-Assignment Form Seminar. Kenneth. Zadeck. https://compilers.cs.uni-saarland.de/ssasem/. Autrans, France. April 2009.
  3. Cytron . Ron . Lowry . Andy . Zadeck . F. Kenneth . Code motion of control structures in high-level languages . Proceedings of the 13th ACM SIGACT-SIGPLAN Symposium on Principles of Programming Languages - POPL '86 . 1986 . 70–85 . 10.1145/512644.512651. 9099471 .
  4. Cytron . Ronald Kaplan . Jeanne . Ferrante . What's in a name? Or, the value of renaming for parallelism detection and storage allocation . International Conference on Parallel Processing, ICPP'87 1987 . 19–27.
  5. Barry Rosen. Mark N. Wegman. F. Kenneth Zadeck. Global value numbers and redundant computations. 1988. Proceedings of the 15th ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages.
  6. Book: Alpern . B. . Wegman . M. N. . Zadeck . F. K. . Proceedings of the 15th ACM SIGPLAN-SIGACT symposium on Principles of programming languages - POPL '88 . Detecting equality of variables in programs . 1988 . 1–11 . 10.1145/73560.73561. 0897912527 . 18384941 .
  7. Efficiently computing static single assignment form and the control dependence graph . Cytron, Ron . Ferrante, Jeanne . Rosen, Barry K. . Wegman, Mark N. . Zadeck, F. Kenneth . amp . ACM Transactions on Programming Languages and Systems . 13 . 1991 . 451 - 490 . 4 . 10.1145/115372.115320 . 10.1.1.100.6361 . 13243943 .
  8. http://llvm.org/devmtg/2007-05/05-Lewycky-Predsimplify.pdf value range propagation
  9. Cytron . Ron . Ferrante . Jeanne . Rosen . Barry K. . Wegman . Mark N. . Zadeck . F. Kenneth . Efficiently computing static single assignment form and the control dependence graph . ACM Transactions on Programming Languages and Systems . 1 October 1991 . 13 . 4 . 451–490 . 10.1145/115372.115320. 13243943 . free .
  10. A Simple, Fast Dominance Algorithm . Rice University, CS Technical Report 06-33870. Cooper . Keith D. . Harvey . Timothy J. . Kennedy . Ken . 2001 . https://web.archive.org/web/20220326234549/https://www.cs.rice.edu/~keith/EMBED/dom.pdf . 2022-03-26 . dead.
  11. Practical Improvements to the Construction and Destruction of Static Single Assignment Form . 1998 . Briggs . Preston . Cooper . Keith D. . Harvey . Timothy J. . Simpson . L. Taylor . dead . https://web.archive.org/web/20100607003509/http://www.cs.rice.edu/~harv/my_papers/ssa.pdf . 2010-06-07 .
  12. Web site: Block Arguments vs PHI nodes - MLIR Rationale . mlir.llvm.org . 4 March 2022.
  13. Book: von Ronne, Jeffery . Interpreting programs in static single assignment form . 2004 . Ning Wang . Michael Franz . Proceedings of the 2004 workshop on Interpreters, virtual machines and emulators - IVME '04 . 23 . 10.1145/1059579.1059585 . 1581139098 . 451410 . http://dl.acm.org/citation.cfm?doid=1059579.1059585 .
  14. Boissinot . Benoit . Darte . Alain . Rastello . Fabrice . Dinechin . Benoît Dupont de . Guillon . Christophe . Revisiting Out-of-SSA Translation for Correctness, Code Quality, and Efficiency . HAL-Inria Cs.DS . 2008 . 14 . en.
  15. Web site: Introducing the WebKit FTL JIT. 13 May 2014.
  16. Web site: Introducing the B3 JIT Compiler. 15 February 2016.
  17. Web site: Swift Intermediate Language (GitHub). GitHub. 30 October 2021.
  18. Web site: Swift's High-Level IR: A Case Study of Complementing LLVM IR with Language-Specific Optimization, LLVM Developers Meetup 10/2015. https://ghostarchive.org/varchive/youtube/20211221/Ntj8ab-5cvE . 2021-12-21 . live. YouTube. 9 November 2015 .
  19. Web site: OTP 22.0 Release Notes.
  20. Web site: Go 1.7 Release Notes - The Go Programming Language. golang.org. 2016-08-17.
  21. Web site: Go 1.8 Release Notes - The Go Programming Language. golang.org. 2017-02-17.
  22. Web site: IonMonkey Overview.,
  23. The Evolution of ART - Google I/O 2016 . 3m47s . 25 May 2016 . Google.
  24. Web site: Bytecode Optimizations. the LuaJIT project.
  25. Web site: HipHop Intermediate Representation (HHIR). GitHub. 30 October 2021.
  26. Web site: Firm - Optimization and Machine Code Generation.
  27. Web site: Reintroducing NIR, a new IR for mesa. Ekstrand. Jason. 16 December 2014 .
  28. Web site: The Java HotSpot Performance Engine Architecture. Oracle Corporation.
  29. Web site: Introducing a new, advanced Visual C++ code optimizer. 4 May 2016.
  30. Web site: SPIR-V spec.
  31. Automatic selection of high-order transformations in the IBM XL FORTRAN compilers. V.. Sarkar. IBM Journal of Research and Development. 41. 3. 233–264. May 1997. IBM. 10.1147/rd.413.0233 .
  32. CUDA: Compiling and optimizing for a GPU platform. 2012 . 10.1016/j.procs.2012.04.209 . Chakrabarti . Gautam . Grover . Vinod . Aarts . Bastiaan . Kong . Xiangyun . Kudlur . Manjunath . Lin . Yuan . Marathe . Jaydeep . Murphy . Mike . Wang . Jian-Zhong . Procedia Computer Science . 9 . 1910–1919 . free .
  33. Web site: Illinois Concert Project. https://web.archive.org/web/20140313140417/http://www-csag.ucsd.edu/projects/concert.html. 2014-03-13. dead.
  34. Static Single Information Form . Ananian . C. Scott . Rinard . Martin . 1999. 10.1.1.1.9976.
  35. Book: Encyclopedia of Parallel Computing.