Docsity

Code Generation and Speculation Models for Loop Optimization - Prof. Scott Mahlke, Assignments of Electrical and Electronics Engineering

Partial preview of the text

Download Code Generation and Speculation Models for Loop Optimization - Prof. Scott Mahlke and more Assignments Electrical and Electronics Engineering in PDF only on Docsity!

  • EECS 583 – Class 16 Control Speculation and Intro to Software Pipelining University of Michigan March 13,
  • 1 -

Reading Material^ Y^ Today’s class^ »^ “Sentinel Scheduling for VLIW and Superscalar Processors”,^ S. Mahlke et al., ASPLOS-5, Oct. 1992, pp.238-247^ »^ “Iterative Modulo Scheduling: An Algorithm for SoftwarePipelining Loops”, B. Rau, MICRO-27, 1994, pp. 63-74.^ Y^ Material for the next lecture^ »^ "Code Generation Schemas for Modulo Scheduled DO-Loopsand WHILE-Loops", B. Rau, M. Schlansker, and P. Tirumalai,^ MICRO-25, Dec. 1992.

  • 3 -

Review: Code Motion Across Branches^ Y^ Upward code motion^ Y^ Restriction 1 (liveness)^ »^ The destination of op is not inliveout(br)^ »^ All memory considered “live”^ Y^ Restriction 2 (exception)^ »^ Op must not cause anexception that may terminatethe program execution when bris taken^ »^ Page fault or cache miss are ok

Y^ Downward code motion Y^ Restriction 1 (liveness)^ »^ If no compensation code^ y^ Destination of op is notliveout^ »^ Else, no restrictions^ y^ Duplicate operation alongboth directions of branch ifdestination is liveout^ 2: y = z / x^ 1: branch x <= 0

1: branch x <= 02: y = z / x

{ a } Insert control dependence to subsequent branch

Insert control dependence to prior branch

  • 4 -

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

  • 6 -

Restricted Speculation Model^ Y^ Most processors have 2 classesof opcodes^ »^ Potentially exception causing^ y^ load, store, integer divide,floating-point^ »^ Never excepting^ y^ Integer add, multiply, etc.^ y^ Overflow is detected, butdoes not terminate programexecution^ Y^ Restricted model^ »^ R2 only applies to potentiallyexception causing operations^ »^ Can freely speculate all neverexception ops (still limited byR1 however)

1 2 We assumed 3 restricted^ speculation 4 when this^ graph was 5 drawn. 6 This is why^ there is no 7 cdep between^4 Æ 8

6 and 4 Æ 8 9

  • 7 -

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

1 2 Remove 3 edge from^ 4 to 7 4 5 6 7 8 9

  • 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} 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?

  • 10 -

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 exception4: *w = z

  • 12 -

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

  • 13 -

Delaying Speculative Exceptions (3)

2’: y = *x 3’: z = y + 4 1: branch x == 0 branch NAT(z) fixup4: *w = z

fixup:^ 2’’: y = *x3’’: z = y + 4jump done

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

  • 15 -

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. Move ops 5, 6, 8 as far up in the SB as possible assuming sentinel speculation support 2. Insert the necessary checks and recovery code (assume ld, st, and div can cause exceptions)

  • 16 -

Scalar Scheduling Wrap Up^ Y^ SB scheduling has no bookkeeping, so its simpler^ »^ Replicate code during SB formation thus eliminating need forbookkeeping^ Y^ Trace scheduling^ »^ In general has less code expansion than SB^ »^ But, it can be quite messy due to compensation code^ Y^ Elcor/Impact^ »^ Uses SB/HB scheduling^ »^ Restricted or general speculation models supported^ y^ General is default^ »^ Sentinel speculation not supported (though it should be)^ Y^ Next topic – Modulo scheduling for loops

  • 18 -

Basic Approach – List Schedule the Loop Body^ time Iteration^

n Schedule each iteration resources: 4 issue, 2 alu, 1 mem, 1 br latencies: add=1, mpy=3, ld = 2, st = 1, br = 1 1: r3 = load(r1)2: r4 = r3 * 263: store (r2, r4)4: r1 = r1 + 45: r2 = r2 + 46: p1 = cmpp (r1 < r9)7: brct p1 Loop

time^ ops 0 1, 4 1 6 2 2 3 - 4 - 5 3, 5, 7

Total time = 6 * n

  • 19 -

Unroll Then Schedule Larger Body^ time Iteration^ 1,

3,^

5,^

n-1,n

Schedule each iteration resources: 4 issue, 2 alu, 1 mem, 1 br latencies: add=1, cmpp = 1, mpy=3, ld = 2, st = 1, br = 1 1: r3 = load(r1)2: r4 = r3 * 263: store (r2, r4)4: r1 = r1 + 45: r2 = r2 + 46: p1 = cmpp (r1 < r9)7: brct p1 Loop

time^ ops 0 1, 4 1 1’, 6, 4’ 2 2, 6’ 3 2’ 4 - 5 3, 5, 7 6 3’,5’,7’

Total time = 7 * n/