Docsity

Lecture Notes on Predicated Execution - Compiler Design | CS 6241, Study notes of Computer Science

Partial preview of the text

Download Lecture Notes on Predicated Execution - Compiler Design | CS 6241 and more Study notes Computer Science in PDF only on Docsity!

CS 6241 – Class 4Predicated Execution, If-conversion

Georgia Tech.January 17, 2008

Thanks to Scott Mahlke

  • 1 -

Reading Material



Today’s class

“The Program Dependence Graph and Its Use in Optimization”,J. Ferrante, K. Ottenstein, and J. Warren, ACM TOPLAS, 1987

“On Predicated Execution”, Park and Schlansker, HPL TechnicalReport, 1991.



Material for the next lecture

"Effective Compiler Support for Predicated Execution using theHyperblock", S. Mahlke et al., MICRO-25, 1992.

"Control CPR: A Branch Height Reduction Optimization forEPIC Processors", M. Schlansker et al., PLDI-99, 1999.

  • 3 -

Predicated Execution Example

BB

BB

BB

BB

a = b + cif (a > 0)

e = f + g else

e = f / g h = i - j

add a, b, cbgt a, 0, L1div e, f, gjump L2L1: add e, f, gL2: sub h, i, j

BB1BB1BB3BB3BB2BB

Traditional branching code

BB1BB2BB3BB

add a, b, c if Tp2 = a > 0 if Tp3 = a <= 0 if Tdiv e, f, g if p3add e, f, g if p2sub h, i, j if T

BB1BB1BB1BB3BB2BB4 Predicated code

p



BB

p



BB

  • 4 -

What About Nested If-then-else’s?

BB

BB

BB

BB

a = b + cif (a > 0)

if (a > 25)

e = f + g else

e = f * g

else

e = f / g h = i - j

add a, b, cbgt a, 0, L1div e, f, gjump L2L1: bgt a, 25, L3mpy e, f, gjump L2L3: add e, f, gL2: sub h, i, j

BB1BB1BB3BB3BB2BB6BB6BB5BB

Traditional branching code

BB

BB

  • 6 -

Benefits/Costs of Predicated Execution



Benefits

Remove branches (both conditional and unconditional)

Remove branch mispredictions

Overlap execution of if-then-else statements

^

Branches tend to sequentialize operations ^

Predicates can be computed/used in parallel



Costs

Useless instructions executed

Code size (extra operand, can’t fit into 32-bits)

Possibly longer schedule lengths



The real story

Must be applied selectively or you get worse performance thannot using it at all

  • 7 -

Benefits/Costs of Predicated Execution (2)

BB

BB

BB

BB

BB1BB2BB3BB4BB5BB6BB

BB

BB

BB

Benefits:- No branches, no mispredicts- Can freely reorder independentoperations in the predicated block- Overlap BB2 with BB5 and BB6Costs (execute all paths)-worst case schedule length-worst case resources required

  • 9 -

CMPP Action Specifiers

Guardingpredicate

Compare

Result

UN

UC

ON

      • 1

OC

    • 1 -

AN

    • 0 -

AC

      • 0

UN/UC = Unconditional normal/complement

This is what we used in the earlier examplesguard = 0, both outputs are 0guard = 1, UN = Compare result, UC = opposite

ON/OC = OR-type normal/complementAN/AC = AND-type normal/complement

  • 10 -

OR-type, AND-type Predicates^ p1 = 0p1 = cmpp_ON (r1 < r2) if Tp1 = cmpp_OC (r3 < r4) if Tp1 = cmpp_ON (r5 < r6) if Tp1 = (r1 < r2) | (!(r3 < r4)) |

(r5 < r6)

Wired-OR into p

p1 = 1p1 = cmpp_AN (r1 < r2) if Tp1 = cmpp_AC (r3 < r4) if Tp1 = cmpp_AN (r5 < r6) if Tp1 = (r1 < r2) & (!(r3 < r4)) &

(r5 < r6)

Wired-AND into p1 Talk about these later – usedfor control height reduction

Generating predicated codefor some source code requiresOR-type predicates

  • 12 -

Class Problem

if (a > 0) {

if (b > 0)

r = t + s

else

u = v + 1

y = x + 1

a.

Draw the CFG

b.

Predicate the code removingall branches

  • 13 -

If-conversion



Algorithm for generating predicated code

Automate what we’ve been doing by hand

Handle arbitrary complex graphs

^

But, acyclic subgraph only!! ^

Need a branch to get you back to the top of a loop

Efficient



Roots are from Vector computer days

Vectorize a loop with an if-statement in the body



4 steps

1. Loop backedge coalescing

2. Control dependence analysis

3. Control flow substitution

4. CMPP compaction



My version of Park & Schlansker

  • 15 -

Step 1: Backedge Coalescing



Recall – Loop backedge is branch from inside the loopback to the loop header



This step only applicable for a loop body

If not a loop body

skip this step



Process

Create a new basic block

^

New BB contains an unconditional branch to the loop header

Adjust all other backedges to go to new BB rather than header



Why do this?

Heuristic step – Not essential for correctness

^

If-conversion cannot remove backedges (only forward edges) ^

But this allows the control logic to figure out which backedge youtake to be eliminated

Generally this is a good thing to do

d++ a++ e++ b++ d++ a++ e++ b++

 - BB Running Example – Backedge Coalescing - BB - BB - BB - BB 
  • BB - BB - BB - b < - b >= - c <= - c > - b > - b <= - c <= - c > - e < - e >= - BB c++ - BB - BB - BB - BB - BB
    • BB - BB - BB - b < - b >= - c <= - c > - b > - b <= - c <= - c > - e < - e >=
  • 18 -

Control Dependences



Recall

Post dominator – BBX is post dominated by BBY if every pathfrom BBX to EXIT contains BBY

Immediate post dominator – First breadth first successor of ablock that is a post dominator



Control dependence – BBY is control dependent on BBXiff

1. There exists a directed path P from BBX to BBY with anyBBZ in P (excluding BBX and BBY) post dominated by BBY

2. BBX is not post dominated by BBY



In English,

A BB is control dependent on the closest BB(s) that determine(s)its execution

Its actually not a BB, it’s a control flow edge coming out of a BB

  • 19 -

Control Dependence Example

BB
BB
BB

BB

BB

BB

BB

BB

BB

Control dependences BB1:BB2:BB3:BB4:BB5:BB6:BB7:

T

F

T

F

Notation

positive BB number = fallthru directionnegative BB number = taken direction