Docsity

Programming Language Compilers - Instruction Scheduling | CS 5363, Study notes of Computer Science

Partial preview of the text

Download Programming Language Compilers - Instruction Scheduling | CS 5363 and more Study notes Computer Science in PDF only on Docsity!

Instruction Scheduling

Instruction scheduling

 Reorder operations to decrease running time

 Different operations take different no. of cycles

 Referencing values not yet ready causes operation pipeline to stall

 Processors can issue multiple instructions every cycle

 VLIW processors: can issue one operation per functional unit in each cycle  Superscalar processors: tries to issue the next k instructions if possible Instruction Scheduler Original code Reordered code

Instruction scheduling Dependence graph

 Instruction reordering must maintain original result

 Dependence/precedence graph G = (N,E)

 Each node n ∈ N is a single operation  type(n) : type of functional-unit that can execute n  delay(n): number of cycles required to complete n  Edge (n1,n2) ∈ N indicates n2 uses result of n1 as operand  G is acyclic within each basic block a: loadAI rarp, @w  r b: add r1, r1  r c: loadAI rarp, @x  r d: mult r1, r2  r e: loadAi rarp, @y  r f: mult r1, r2  r g: loadAI rarp, @z  r h: mult r1, r2  r i: storeAI r1  rarp, 0 a b c d e f g h i Dependence graph

The scheduling problem

 Given a dependence graph D = (N,E), a schedule S maps each node n ∈ N to the cycle number that n should be issued.  Each schedule S must satisfy three constraints  Well-formed: for each node n ∈ N, S(n) >= 1; there is at least one node n ∈ N such that S(n) = 1  Correctness: if (n1,n2) ∈ E, then S(n1) + delay(n1) <= S(n2)  Feasibility: for each cycle i >= 1 and each functional-unit type t, (number of node n where type(n)=t and S(n)=i) ≤ (number of functional-unit t on the target machine)  Given a well-formed schedule S that is both correct and feasible, the length of the schedule is L(s) = max(S(n) + delay(n)) n ∈ N  A schedule S is time-optimal if it is the shortest  For all other schedules Sj (which contain the same set of operations), L(S) <= L(Sj) (S has shorter length than Sj)

Anti dependences

 Node (operation) e cannot be issued before d even if e does

not use result of d

 e overwrites the value of r2 that d uses  There is an anti-dependence from d to e

 To handle anti-dependences, schedulers can

 Add anti-dependences as new edges in dependence graph; or  Rename registers to eliminate anti-dependences a: loadAI rarp, @w  r b: add r1, r1  r c: loadAI rarp, @x  r d: mult r1, r2  r e: loadAi rarp, @y  r f: mult r1, r2  r g: loadAI rarp, @z  r h: mult r1, r2  r i: storeAI r1  rarp, 0 a b c d e f g h i Dependence graph

Critical path of dependence graph

 Given a dependence graph D

 Each node ni can start only if all other nodes that ni depend on have finished  Length of any dependence path n1n2…ni (any path in D) is delay(n1)+delay(n2)+…+delay(ni)

 Critical path: the longest path in the dependence graph

 should schedule nodes on critical path as early as possible a: loadAI rarp, @w  r b: add r1, r1  r c: loadAI rarp, @x  r d: mult r1, r2  r e: loadAi rarp, @y  r f: mult r1, r2  r g: loadAI rarp, @z  r h: mult r1, r2  r i: storeAI r1  rarp, 0 a b c d e f g h i Dependence graph

cs5363 10

List scheduling algorithm

Cycle := 1 Ready := leaves of D Active := ∅ While (Ready ∪ Active ≠ ∅) if Ready ≠ ∅ then remove an opi (top priority) from Ready S(opi) := Cycle add opi to Active Cycle ++ for each opi ∈ Active if S(opi) + delay(opi) <= Cycle then remove opi from Active for each successor opj of opi in D Mark edge (opi,opj) ready if all edges to opj are ready then add opj to Ready a: loadAI rarp, @w  r b: add r1, r1  r c: loadAI rarp, @x  r d: mult r2, r3  r e: loadAi rarp, @y  r f: mult r4, r5  r g: loadAI rarp, @z  r h: mult r6, r7  r i: storeAI r8  rarp, 0 Example: a b c d e f g h i Dependence graph 3

Example: list scheduling

9 h h

11 i i

7 f f

6 g g

5 g d d

4 g b b

3 g e e

2 eg c c

1 ceg a a

cycle Ready active integer memory loadAI rarp, @w  r loadAI rarp, @x  r loadAi rarp, @y  r add r11, r1  r mult r1, r2  r loadAI rarp, @z  r mult r1, r3  r mult r1, r2  r storeAI r1  rarp, 0 start 1 2 3 4 5 6 7 9 11

Complications in list scheduling

 Wait time at basic block boundaries

 Wait for all operations in one block to complete before proceeding to the next block  Improvement: trace scheduling (across block boundaries)

 Scheduling functional units in VLIW instructions

 Must allocate operations on specific functional units

 Uncertainty of memory operations

 Memory access may take different number of cycles depending on whether it is in the cache  Advanced study: reordering computations to improve the performance memory access operations

 Sensitivity to priority ranking of operations

 Unstability in tie breaking of same-ranking operations  Can use different priority ranking, e.g., length of longest path, number of immediate successors, number of descendents, …