More CFG Static Single Assignmentcode transformations – Primarily due to explicit & simplified...

31
More CFG Static Single Assignment ECE 5775 High-Level Digital Design Automation Fall 2018

Transcript of More CFG Static Single Assignmentcode transformations – Primarily due to explicit & simplified...

Page 1: More CFG Static Single Assignmentcode transformations – Primarily due to explicit & simplified (sparse) def-use chains Here we show two simple examples – Dead code elimination

More CFGStatic Single Assignment

ECE 5775High-Level Digital Design Automation

Fall 2018

Page 2: More CFG Static Single Assignmentcode transformations – Primarily due to explicit & simplified (sparse) def-use chains Here we show two simple examples – Dead code elimination

▸ HW 1 due next Monday (9/17)

▸ Lab 2 will be released tonight (due 9/24)

▸ Instructor OH cancelled this afternoon

1

Announcements

Page 3: More CFG Static Single Assignmentcode transformations – Primarily due to explicit & simplified (sparse) def-use chains Here we show two simple examples – Dead code elimination

▸ More on control flow analysis – Dominance frontier

▸ Dataflow analysis – static single assignment (SSA)– SSA Definition– PHI node (F-node) placement– Code optimizations with SSA

2

Outline

Page 4: More CFG Static Single Assignmentcode transformations – Primarily due to explicit & simplified (sparse) def-use chains Here we show two simple examples – Dead code elimination

▸ What becomes a leader statement of a basic block? (three cases)

▸ Does a node strictly (or properly) dominate itself?

▸ Does a predecessor of a node B always dominate B?

▸ Suppose A that dominates all of B’s predecessors. Does A always dominate B?

3

Revisiting Control Flow Analysis

Page 5: More CFG Static Single Assignmentcode transformations – Primarily due to explicit & simplified (sparse) def-use chains Here we show two simple examples – Dead code elimination

Review: Finding Loops

▸ Loop identification algorithm– Find an edge B®H where H dominates B;

This edge is called a back-edge

– Find all nodes that (1) are dominated by H and (2) can reach B through nodes dominated by H; add them to the loop • H and B are naturally included

4

Page 6: More CFG Static Single Assignmentcode transformations – Primarily due to explicit & simplified (sparse) def-use chains Here we show two simple examples – Dead code elimination

5

Finding Loops (1)

1

2

3

5 6

8

9 10

Find all back edges in this graphand the natural loop associated with each back edge

(9,1)4

7

Page 7: More CFG Static Single Assignmentcode transformations – Primarily due to explicit & simplified (sparse) def-use chains Here we show two simple examples – Dead code elimination

6

Finding Loops (1)

1

2

3

5 6

8

9 10

Find all back edges in this graphand the natural loop associated with each back edge

(9,1) Entire graph4

7

Page 8: More CFG Static Single Assignmentcode transformations – Primarily due to explicit & simplified (sparse) def-use chains Here we show two simple examples – Dead code elimination

7

Finding Loops (2)

1

2

5 6

8

9 10

Find all back edges in this graphand the natural loop associated with each back edge

(9,1) Entire graph(10,7)

3

4

7

Page 9: More CFG Static Single Assignmentcode transformations – Primarily due to explicit & simplified (sparse) def-use chains Here we show two simple examples – Dead code elimination

Intuition of Dominance Relation

8

Imagine a source of lightat the entry node, and thatthe edges are optical fibers

To find which nodes are dominated by a given node, place an opaque barrier at that node and observe which nodes become dark

1

2

5 6

8

9 10

3

4

7

entry

Page 10: More CFG Static Single Assignmentcode transformations – Primarily due to explicit & simplified (sparse) def-use chains Here we show two simple examples – Dead code elimination

9

Finding Loops (2)

1

2

3

4

7

5 6

8

9 10

Find all back edges in this graphand the natural loop associated with each back edge

(9,1) Entire graph(10,7)

Page 11: More CFG Static Single Assignmentcode transformations – Primarily due to explicit & simplified (sparse) def-use chains Here we show two simple examples – Dead code elimination

10

Finding Loops (2)

1

2

3

4

7

5 6

8

9 10

Find all back edges in this graphand the natural loop associated with each back edge

(9,1) Entire graph(10,7) {7,8,10}

Page 12: More CFG Static Single Assignmentcode transformations – Primarily due to explicit & simplified (sparse) def-use chains Here we show two simple examples – Dead code elimination

(7,4)

11

Finding Loops (3)

1

2

3

4

5 6

8

9 10

Find all back edges in this graphand the natural loop associated with each back edge

(9,1) Entire graph(10,7) {7,8,10}

7

Page 13: More CFG Static Single Assignmentcode transformations – Primarily due to explicit & simplified (sparse) def-use chains Here we show two simple examples – Dead code elimination

(7,4)

12

Finding Loops (3)

1

2

3

4

7

5 6

8

9 10

Find all back edges in this graphand the natural loop associated with each back edge

(9,1) Entire graph(10,7) {7,8,10}

Page 14: More CFG Static Single Assignmentcode transformations – Primarily due to explicit & simplified (sparse) def-use chains Here we show two simple examples – Dead code elimination

13

Finding Loops (3)

1

2

3

4

7

5 6

8

9 10

Find all back edges in this graphand the natural loop associated with each back edge

(9,1) Entire graph(10,7) {7,8,10}(7,4) {4,5,6,7,8,10}

Page 15: More CFG Static Single Assignmentcode transformations – Primarily due to explicit & simplified (sparse) def-use chains Here we show two simple examples – Dead code elimination

Static Single Assignment

▸ Static single assignment (SSA) form is a restricted IR where– Each variable definition has a unique name– Each variable use refers to a single definition

▸ SSA simplifies data flow analysis & many compiler optimizations – Eliminates artificial dependences (on scalars)

• Write-after-write• Write-after-read

14

Page 16: More CFG Static Single Assignmentcode transformations – Primarily due to explicit & simplified (sparse) def-use chains Here we show two simple examples – Dead code elimination

SSA within a Basic Block

▸ Assign each variable definition a unique name▸ Update the uses accordingly

x = read()x = x * 5x = x + 1y = x * 9

x0 = read()x1 = x0 * 5x2 = x1 + 1y = x2 * 9

15

×

+

×

5

x1

x0

x2

1

9

yOriginal code SSA form

Corresponding data flow graph

Page 17: More CFG Static Single Assignmentcode transformations – Primarily due to explicit & simplified (sparse) def-use chains Here we show two simple examples – Dead code elimination

SSA with Control Flow

▸ Consider a situation where two control-flow paths merge– e.g., due to an if-then-else statement or a loop

x = read()if (x > 0)

y = 5else

y = 10x = y

y = 5 y = 10

x = y

x = read()if (x > 0)

y0 = 5 y1 = 10

x1 = y

x0 = read()if (x0 > 0)

should this be y0 or y1?

16

Page 18: More CFG Static Single Assignmentcode transformations – Primarily due to explicit & simplified (sparse) def-use chains Here we show two simple examples – Dead code elimination

Introducing ϕ-Node

▸ Inserts special join functions (called ϕ-nodes or PHI nodes) at points where different control flow paths converge

y0 = 5 y1 = 10

y2 = ϕ(y0, y1)x1 = y2

if (x0 > 0) Note: ϕ is not an executable function!To generate executable code from this form, appropriate copy statements need to be generated in the predecessors (in other words, reversing the SSA process for code generation)

17

Page 19: More CFG Static Single Assignmentcode transformations – Primarily due to explicit & simplified (sparse) def-use chains Here we show two simple examples – Dead code elimination

SSA in a Loop

▸ Insert ϕ-nodes in the loop header block

x = 0 i = 1 while (i<10) {

x = x+ii = i+1

}

if (i < 10)

Outsidex = x + 1i = i + 1

x = 0i = 1

x1 = ϕ(x0, x2)i1 = ϕ(i0, i2)if (i1 < 10)

Outsidex2 = x1 + 1i2 = i1 + 1

x0 = 0i0 = 1

18

Page 20: More CFG Static Single Assignmentcode transformations – Primarily due to explicit & simplified (sparse) def-use chains Here we show two simple examples – Dead code elimination

ϕ-Node Placement

▸ When and where to add ϕ-nodes?– If two control paths A à C and B à C

converge at a node C, and both A and B contain assignments to variable X, then ϕ-node for X must be placed at C • We often call C a join node or

convergence point• Can be generalized to more than two

converging control paths

▸ Objective: Minimize the number of ϕ-nodes– Need to compute dominance frontier

sets

19

x1 = ϕ(x0, x2)i1 = ϕ(i0, i2)if (i1 < 10)

Outsidex2 = x1 + 1i2 = i1 + 1

x0 = 0i0 = 1

Page 21: More CFG Static Single Assignmentcode transformations – Primarily due to explicit & simplified (sparse) def-use chains Here we show two simple examples – Dead code elimination

Dominance Frontier

▸ A basic block F is in the dominance frontier set (DF)of basic block B if and only if– B does NOT strictly dominate F– B dominates some predecessor(s) of FIf above two conditions hold, F Î DF(B)

▸ Intuitively, the basic blocks in the dominance frontier set of B are almost strictly dominated by B

▸ Useful for efficiently computing the SSA form

20

Page 22: More CFG Static Single Assignmentcode transformations – Primarily due to explicit & simplified (sparse) def-use chains Here we show two simple examples – Dead code elimination

21

Dominance Frontier and SSA

B0

B1

B2 B3

B4

B6

B7

B5

Area dominated by B3

▸ B7 is in the dominance frontier set of B3– B3 does not dominate B7,

but dominates one of its predecessors (i.e., B6)

▸ For each variable definition in B3, a ϕnode is needed in B7– B7 is the destination of

some edge(s) leaving an area dominated by B3

Page 23: More CFG Static Single Assignmentcode transformations – Primarily due to explicit & simplified (sparse) def-use chains Here we show two simple examples – Dead code elimination

DF(B)–1776671

22

Example: Dominance Frontiers

B0

B1

B2 B3

B4

B6

B7

B5

CFG

B01234567

Dominance frontiers of B

▸ F is in the dominance frontier set (DF) of B iff– B does NOT strictly dominate F– B dominates some predecessor(s) of FIf above two conditions hold, F Î DF(B)

Page 24: More CFG Static Single Assignmentcode transformations – Primarily due to explicit & simplified (sparse) def-use chains Here we show two simple examples – Dead code elimination

23

Iterative ϕ-Node InsertionB DF0 -1 12 73 74 65 66 77 1

b =c =

c =

b =

a =b =c =B0

B1

B2 B3

B4

B6

B7

B5

a = ϕ(…)b = ϕ(…)c = ϕ(…)

c = ϕ(…)

a = ϕ(…)b = ϕ(…)c = ϕ(…)

a =

• a is defined in 0, 3 => need ϕ in 7, then a defined in 7=> need ϕ in 1

• b is defined in 0, 2, 6=> need ϕ in 7then b defined in 7 => need ϕ in 1

• c is defined in 0,2,5=> need ϕ in 6,7then c defined in 7=> need ϕ in 1

Page 25: More CFG Static Single Assignmentcode transformations – Primarily due to explicit & simplified (sparse) def-use chains Here we show two simple examples – Dead code elimination

▸ SSA form simplifies data flow analysis and many code transformations– Primarily due to explicit & simplified (sparse) def-use

chains

▸ Here we show two simple examples – Dead code elimination– Loop induction variable detection

24

SSA Applications

Page 26: More CFG Static Single Assignmentcode transformations – Primarily due to explicit & simplified (sparse) def-use chains Here we show two simple examples – Dead code elimination

Dead Code in CDFG

▸ Dead code is either– Unreachable code– Definitions never used

▸ Dead statements?

x = a + by = c + d

x = a – b

z = z + 1

y = c – d z = x + y

f(x, y)

B1

B2

B4

B5

B6

25

Page 27: More CFG Static Single Assignmentcode transformations – Primarily due to explicit & simplified (sparse) def-use chains Here we show two simple examples – Dead code elimination

26

Dead Code Elimination with SSA

x1 = a + by1 = c + d

x2 = a – b

z2 = z1 + 1

x3 = ϕ(x1, x2)y2 = c – d

z1 = x3 + y2

f(x3, y2)

B1

B2

B3

B4

B5

x1 = a + b

x2 = a – b

x3 = ϕ(x1, x2)y2 = c – d

f(x3, y2)

Iteratively remove unused definitions: remove y1, z2 (and B4) à then remove z1

Page 28: More CFG Static Single Assignmentcode transformations – Primarily due to explicit & simplified (sparse) def-use chains Here we show two simple examples – Dead code elimination

▸ An induction variable is a variable that

– Gets increased or decreased by a fixed amount (loop invariant) on every iteration of a loop (basic induction variable)• i = i + c

– or is an affine function of another induction variable (mutual induction variable)• j = a * i + b

27

Loop Induction Variables

Page 29: More CFG Static Single Assignmentcode transformations – Primarily due to explicit & simplified (sparse) def-use chains Here we show two simple examples – Dead code elimination

▸ Find basic loop induction variable(s)1. Inspect back edges in the loop

2. Each back edge points to a ϕ node in the loop header, which may indicate a basic induction variable

3. ϕ is a function of an initialized variable and a definition in the form of “i + c” (i.e., increment operation)

28

Identifying Basic Loop Induction Variable

i1 = ϕ(0, i2)if (i1 < 10)

Outsidei2 = i1 + 1

Page 30: More CFG Static Single Assignmentcode transformations – Primarily due to explicit & simplified (sparse) def-use chains Here we show two simple examples – Dead code elimination

▸ Importance of compilers– Essential component of SoC software development flow– Essential component of high-level synthesis

▸ A good intermediate representation (IR) enables efficient and effective analysis and optimization– Dominance relation helps effective CFG analysis– SSA form facilitates efficient IR-level optimization

29

Summary

Page 31: More CFG Static Single Assignmentcode transformations – Primarily due to explicit & simplified (sparse) def-use chains Here we show two simple examples – Dead code elimination

▸ These slides contain/adapt materials developed by– Prof. Scott Mahlke (UMich)

30

Acknowledgements