Docsity

Understanding Speculation and Sentinel Models in Compiler Optimization - Prof. Scott Mahlk, Study notes of Electrical and Electronics Engineering

Partial preview of the text

Download Understanding Speculation and Sentinel Models in Compiler Optimization - Prof. Scott Mahlk and more Study notes Electrical and Electronics Engineering in PDF only on Docsity!

EECS 583 – Class 15 Control Speculation inSuperblocks, Trace Scheduling

University of Michigan March 9, 2005

  • 1 -

Reading Material

Y

Today’s class

“Sentinel Scheduling for VLIW and Superscalar Processors”, S. Mahlke et al., ASPLOS-5, Oct. 1992, pp. 238-

“Trace Scheduling: A Technique for Global MicrocodeCompaction”, J. Fisher, IEEE Transactions on Computers, July 1981

Y

Material for the next lecture

“Iterative Modulo Scheduling: An Algorithm for SoftwarePipelining Loops”, B. Rau, MICRO-27, 1994, pp. 63-74.

  • 3 -

Conservative Approach to Control Dependences

Superblock

  • Make branches barriers, nothing moves above or below branches * Schedule each BB in SB separately * Sequential schedules * Whole purpose of a superblock is lost

1: r1 = r2 + r32: r4 = load(r1)3: p1 = cmpp(r3 == 0)4: branch p1 Exit15: store (r4, -1)6: r2 = r2 – 47: r5 = load(r2)8: p2 = cmpp(r5 > 9)9: branch p2 Exit

Note: Control flow in red bold

  • 4 -

Upward Code Motion Across Branches

Y

Restriction 1a (register op)

»

The destination of op is not inliveout(br)

»

Wrongly kill a live value

Y

Restriction 1b (memory op)

»

Op does not modify the memory

»

Actually live memory is whatmatters, but that is often too hard todetermine

Y

Restriction 2

»

Op must not cause an exception thatmay terminate the program executionwhen br is taken

»

Op is executed more often than it issupposed to (speculated

)

»

Page fault or cache miss are ok

Y

Insert control dep when eitherrestriction is violated

… if (x > 0)

y = z / x

…

control flow graph^ 1: branch x <= 0

2: y = z / x

  • 6 -

Add Control Dependences to a Superblock

Superblock

Assumed liveout sets

1: r1 = r2 + r32: r4 = load(r1)3: p1 = cmpp(r2 == 0)4: branch p1 Exit15: store (r4, -1)6: r2 = r2 – 47: r5 = load(r2)8: p2 = cmpp(r5 > 9)9: branch p2 Exit

{r1}

{r2}

All ops have cdep to op 9!

{r5}

Notes: All branches are control dependent on one another. If no compensation, all ops dependent on last branch

  • 7 -

Relaxing Code Motion Restrictions

Y

Upward code motion is generallymore effective

»

Speculate that an op is useful (justlike an out-of-order processor withbranch pred)

»

Start ops early, hide latency, overlapexecution, more parallelism

Y

Removing restriction 1

»

For register ops – use registerrenaming

»

Could rename memory too, butgenerally not worth it

Y

Removing restriction 2

»

Need hardware support (akaspeculation models

)

y

Some ops don’t cause exceptions

y

Ignore exceptions

y

Delay exceptions

1: branch x <= 0

2: y = z / x

R1: y is not in liveout(1) R2: op 2 will never cause

an exception when op1 is taken

  • 9 -

Class Problem

1: r1 = r7 + 42: branch p1 Exit13: store (r1, -1)4: branch p2 Exit25: r2 = load(r7)6: r3 = r2 – 47: branch p3 Exit38: r4 = r3 / r

{r4} {r1} {r2}

{r4, r8}

Draw the dependence graph assuming the restricted speculation model

  • 10 -

General Speculation Model

Y

2 types of exceptions

Program terminating (traps)

y

Div by 0, illegal address

Fixable (normal and handledat run time)

y

Page fault, TLB miss

Y

General speculation

Processor provides non-trapping versions of alloperations (div, load, etc)

Return some bogus value (0)when error occurs

R2 is completely ignored,only R1 limits speculation

Speculative ops convertedinto non-trapping version

Fixable exceptions handled asusual for non-trapping ops

Remove edge from 4 to 7

  • 12 -

Class Problem

1: r1 = r7 + 42: branch p1 Exit13: store (r1, -1)4: branch p2 Exit25: r2 = load(r7)6: r3 = r2 – 47: branch p3 Exit38: r4 = r3 / r

{r4} {r1} {r2}

{r4, r8}

  1. Starting with the graph assuming restricted speculation, what edges can be removed if general speculation support is provided? 2. With more renaming, what dependences could be removed?
  • 13 -

Sentinel Speculation Model

1: branch x == 0

Y

Ignoring all speculative exceptionsis painful

Debugging issue (is a program everfully correct?)

Y

Also, handling of all fixableexceptions for speculative ops canbe slow

Extra page faults

Y

Sentinel speculation

Mark speculative ops (opcode bit)

Exceptions for speculative ops arenoted, but not handed immediately(return garbage value)

Check for exception conditions inthe “home block” of speculativepotentially excepting ops

2: y = *x 3: z = y + 4

4: *w = z

2’: y = *x 3’: z = y + 4

1: branch x == 0

check exception

4: *w = z

  • 15 -

Delaying Speculative Exceptions (2)

1: branch x == 0

Y

Check for exceptions

Test NAT bit of appropriateregister (last register in dependencechain) in home block

Explicit checks

y

Insert new operation to check NAT

Implicit checks

y

Non-speculative use of registerautomatically serves as NAT check

Y

Regenerate exception

Figure out the exact cause

Handle if possible

Check with NAT conditionbranches to “recovery code”

Compiler generates the recoverycode specific to each check

2: y = *x 3: z = y + 4

4: *w = z

2’: y = *x 3’: z = y + 4

1: branch x == 0

check NAT(z)

4: *w = z

  • 16 -

Delaying Speculative Exceptions (3)

2’: y = *x 3’: z = y + 4

1: branch x == 0 branch NAT(z) fixup

4: *w = z

2’’: y = *x 3’’: z = y + 4

jump done

fixup:

done:

Recovery code

In recovery code, the exception condition will be regenerated as the excepting op is re-executed with the same inputs If the exception can be handled, it is, all dependent ops are re-executed, and execution is returned to point after the check If the exception is a program error, execution is terminated in the recovery code

Recovery code consists of chain of operations starting with a potentially excepting speculative op up to its corresponding check

  • 18 -

Class Problem

1: r1 = r7 + 42: branch p1 Exit13: store (r1, -1)4: branch p2 Exit25: r2 = load(r7)6: r3 = r2 – 47: branch p3 Exit38: r4 = r3 / r

{r4} {r1} {r2}

{r4, r8}

Move ops 5, 6, 8 as far up in the SB as possible assuming sentinel speculation support

Insert the necessary checks and recovery code (assume ld, st, and div can cause exceptions)

  • 19 -

Trace Scheduling

Y

Trace scheduling is one of themore famous VLIW compilertechniques

Invented by Josh Fisher andJohn Ellis in the early 80’s

Bulldog compiler

Multiflow compiler

Y

Recall, traces are sequences ofbasic blocks that are likelyexecution paths

Traces have both sideentrances and exits

Scheduler must worry aboutboth

Trace

1: r1 = r2 + r32: r4 = load(r1)3: p1 = cmpp(r3 == 0)4: branch p1 Exit15: store (r4, -1)6: r2 = r2 – 47: r5 = load(r2)8: p2 = cmpp(r5 > 9)9: branch p2 Exit

{r2}

{r4}

{r4} {r2}

{r5}