Docsity

Static Single Assignment (SSA) Form in Programming Languages - Prof. Jeremy G. Siek, Assignments of Electrical and Electronics Engineering

Toggle sidebar
bg1
Assignment 5(G): static single assignment
form (SSA)
ECEN 4553 & 5013, CSCI 4555 & 5525
Prof. Jeremy G. Siek
September 9, 2008
Consider again the following example. In the previous assignment we
assigned the variable xthe type pyobj because it needs to store an integer
and a floating point number.
x=3
y=x+1
x = x + 0.14159
z=x/2
However, in cases like this we can do better because we can statically deter-
mine when xneeds to store an integer and when it needs to store a floating
point number. We can split xinto two separate variable, giving one the
type int and the other the type float. The following shows xsplit into the
variables x0and x 1.
x_0 = 3
y=x_0+1
x_1 = x_0 + 0.14159
z=x_1/2
The variable x0can be given the type int and x 1 can be given the type
float. This resulting code is in Static Single Assignment (SSA) form [1, 2, 3,
4] because each variable is only assigned to at a single place in the code.
1 Converting to SSA form
To convert a program to SSA form you traverse the program, and when
you encounter an assignment, you replace it with an assignment to a new
variable with its version number incremented by one. For each variable,
you maintain a dictionary that maps each variable to its current version
1
pf3
pf4
pf5

Partial preview of the text

Download Static Single Assignment (SSA) Form in Programming Languages - Prof. Jeremy G. Siek and more Assignments Electrical and Electronics Engineering in PDF only on Docsity!

Assignment 5(G): static single assignment

form (SSA)

ECEN 4553 & 5013, CSCI 4555 & 5525

Prof. Jeremy G. Siek

September 9, 2008

Consider again the following example. In the previous assignment we assigned the variable x the type pyobj because it needs to store an integer and a floating point number.

x = 3 y = x + 1 x = x + 0. z = x / 2

However, in cases like this we can do better because we can statically deter- mine when x needs to store an integer and when it needs to store a floating point number. We can split x into two separate variable, giving one the type int and the other the type float. The following shows x split into the variables x 0 and x 1.

x_0 = 3 y = x_0 + 1 x_1 = x_0 + 0. z = x_1 / 2

The variable x 0 can be given the type int and x 1 can be given the type float. This resulting code is in Static Single Assignment (SSA) form [1, 2, 3, 4] because each variable is only assigned to at a single place in the code.

1 Converting to SSA form

To convert a program to SSA form you traverse the program, and when you encounter an assignment, you replace it with an assignment to a new variable with its version number incremented by one. For each variable, you maintain a dictionary that maps each variable to its current version

number. When you encounter a variable in an expression, you append the current version number to its name. So, for example, when you see x = 3 you transform it to x_0 = 3 and add x:0 to the dictionary. When you see x

  • 1 you transform it to x_0 + 1. The language P 2 also includes if statements and while loops, which complicates the SSA form. Consider the following program in which the variable x is assigned values of different types in two branches of the if.

if randint(0,1): x = 42 else: x = 3. print x

SSA form deals with if statements by inserting a φ node assignment fol- lowing the if for each variable that is assigned in the different branches.

if randint(0,1): x_0 = 42 else: x_1 = 3. x_2 = φ(x_0,x_1) print x_

The semantics of the φ expression is to return the value of the variable corresponding to the branch that was actually taken at runtime. Think of the φ node being magically connected to the if statement. The mystery will disappear when we translate back out of SSA form. Out intermediate representation can store φ nodes simply as an assignment where the right- hand side is a PrimitiveOp node with ’phi’ is the name of the operation. In the translation out of SSA form we will need to find all the of φ as- signments for a particular If. Thus, instead of placing the φ assignments af- ter the If, we modify the If AST node to include another attribute, named phis, that contains the list of φ assignment statements. The semantics of this new If node is the same as the old except that the phis statements are executed after the rest of the If is finished. The translation of While AST nodes to SSA form follows similar ideas to that of the If: φ nodes are inserted just after a join in the control flow. For a while loop, the join is just before the test of the loop because control can enter the test of the loop from the preceding statement or from the end of the loop. The left-hand side of Figure 1 shows the control-flow graph of a while loop. The right-hand side of Figure 1 shows where the φ nodes need to be inserted. The control-flow is somewhat tricky in the that φ nodes need to

test

body x_2 = 7 y_3 = 5

else_ x_1 = 4 + 5

phis x_3 = phi(x_2, x_1) y_4 = phi(y_3, y_2)

y_2 = 4

test

body x_2 = 7 y_3 = 5 x_3 = x_ y_4 = y_

else_ x_1 = 4 + 5 x_3 = x_ y_4 = y_

y_2 = 4

If statement in SSA (^) Converted to normal If statement

Figure 2: Converting and If statement out of SSA form.

Generate C

Lex & Parse AST

Python text

C text

Simplify Primitive Operations IR 0

IR (^0) Specialization^ Type IR 0 Type Analysis

IR 0 with VarDecl

Insert variable declarations

Convert to SSA form IR^0

Convert out of IR (^0) SSA form

Figure 3: Compiler organization with conversion to SSA form.

analysis more precise. The convertion out of SSA form should come just before generating C code. Note that the passes in between the conversion into and out of SSA must be updated to handle If and While AST nodes to process the list of statements in their phis attribute.

References

[1] B. Alpern, M. N. Wegman, and F. K. Zadeck. Detecting equality of vari- ables in programs. In POPL ’88: Proceedings of the 15th ACM SIGPLAN- SIGACT Symposium on Principles of Programming Languages, pages 1–11. ACM Press, 1988.

[2] A. W. Appel and J. Palsberg. Modern Compiler Implementation in Java. Cambridge University Press, 2003.

[3] M. M. Brandis and H. M ¨ossenb ¨ock. Single-pass generation of static single-assignment form for structured languages. ACM Trans. Program. Lang. Syst., 16(6):1684–1698, 1994.

[4] R. Cytron, J. Ferrante, B. K. Rosen, M. N. Wegman, and F. K. Zadeck. Efficiently computing static single assignment form and the control de- pendence graph. ACM Trans. Program. Lang. Syst., 13(4):451–490, 1991.