Docsity

Instruction Scheduling for Pipelined Architectures: Minimizing Pipeline Stalls - Prof. Jin, Study notes of Computer Science

Toggle sidebar
bg1
Instruction Scheduling
Jingke Li
Portland State University
Jingke Li (Portland State University) CS322 Instruction Scheduling 1 / 17
Instruction Scheduling (for Pipelined Architectures)
Goal: Minimizing pipeline stalls by reordering instructions.
Assumptions:
•Only consider data hazards
•Perform instruction selection and register allocation first
Approaches:
•Hennessy & Gross (’83) — O(n4) algorithm (where nis the number
of instructions in the basic block). It uses lookahead to avoid
deadlocking the scheduling algorithm. (Deadlock can occur b ecause
they do not represent write-write dependences in their DAG).
•Gibbons & Muchnick (’86) — O(n2) worst case, O(n) in practice,
with nearly the same results as Hennessy & Gross.
Jingke Li (Portland State University) CS322 Instruction Scheduling 2 / 17
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Instruction Scheduling for Pipelined Architectures: Minimizing Pipeline Stalls - Prof. Jin and more Study notes Computer Science in PDF only on Docsity!

Instruction Scheduling

Jingke Li

Portland State University

Jingke Li (Portland State University) CS322 Instruction Scheduling 1 / 17

Instruction Scheduling (for Pipelined Architectures)

Goal: Minimizing pipeline stalls by reordering instructions.

Assumptions:

  • Only consider data hazards
  • Perform instruction selection and register allocation first

Approaches:

  • Hennessy & Gross (’83) — O(n^4 ) algorithm (where n is the number of instructions in the basic block). It uses lookahead to avoid deadlocking the scheduling algorithm. (Deadlock can occur because they do not represent write-write dependences in their DAG).
  • Gibbons & Muchnick (’86) — O(n^2 ) worst case, O(n) in practice, with nearly the same results as Hennessy & Gross.

Pipelining

Begin executing an instruction before completing the previous one. Concretely, the execution of an instruction is decomposed into multiple stages, each stage is executed in a different hardware unit.

The following is a 5-stage pipeline:

IF ID EX MEM WB IF ID EX MEM WB IF ID EX MEM WB IF ID EX MEM WB IF ID EX MEM WB IF ID EX MEM WB

time inst

fetch — fetch the instruction decode — decode the instruction execute — execute the instruction (for reg-to-reg instructions) or perform address calculation (for memory operations) memory access — for memory operations writeback — write back data to registers Jingke Li (Portland State University) CS322 Instruction Scheduling 3 / 17

Hazards and Stalls

  • Data Hazards — Instruction depends on result of prior instruction that is still in the pipe: add $r1,$r2,$r3 // $r1 is the destination mul $r4,$r1,$r1 // $r1’s value is needed here

IF ID EX MEM WB IF × × × ID EX MEM WB

time inst

  • (^) Structural hazards — Hardware cannot support certain instruction sequences because of limited hardware resources: mul $r1,$r2,$r3 // suppose mult takes 2 cycles mul $r4,$r5,$r

IF ID EX MEM WB IF ID ×EX MEM WB

time inst

Dependence Graph

  • (^) Nodes represent instructions
  • (^) Edges (s 1 , s 2 ) represent dependences between instructions instruction s 1 must execute before s 2.

1 addi $r2,1,$r 2 addi $sp,12,$sp 3 st a, $r 4 ld $r3,-4($sp) 5 ld $r4,-8($sp) 6 addi $sp,8,$sp 7 st 0($sp),$r 8 ld $r5,a 9 addi $r4,1,$r

1 2 3

4 5

6

7

8

9

Hazards in current schedule: (3,4), (5,6), (7,8), (8,9)

Jingke Li (Portland State University) CS322 Instruction Scheduling 7 / 17

Scheduling Heuristics

Consider these questions:

  • Does an instruction interlock with any immediate successors in the dependence graph?
  • How many immediate successors does an instruction have?
  • Is an instruction on the critical path?

Idea: Schedule an instruction earlier when ...

  • It does not interlock with the previously scheduled instruction (avoid interlocks)
  • It interlocks with its successors (may enable successors to be scheduled without interlock)
  • It has many successors (may enable successors to be scheduled with greater flexibility)
  • It is on the critical path (the goal is to minimize time, after all)

List Scheduling Assumptions

  • Instructions from a single basic block
  • Hardware can detect and handle hazards — algorithm need not introduce no-ops
  • Pointers can refer to any memory address (i.e., no alias analysis)
  • load followed by an ALU op produces a hazard
  • store followed by a load produces a hazard
  • Hazards take a single cycle

Jingke Li (Portland State University) CS322 Instruction Scheduling 9 / 17

List Scheduling Algorithm

  1. Build dependence graph G (with a forward pass over the basic block, comparing each instruction to the nodes in the DAG so far).
  2. Put all the roots (nodes with no predecessors) in G to a candidates set.
  3. While candidates 6 = φ, do 3.1 Select an instruction s from candidates (using heuristics in the given order). 3.2 Schedule s. 3.3 Remove s from candidates and add to candidates those nodes whose predecessors have all been scheduled.

Complexity: Quadratic in the number of instructions.

  • Building dependence graph is O(n^2 )
  • May need to inspect each instruction at each scheduling step: O(n^2 )

In practice: closer to linear

Instruction Scheduling vs. Register Allocation

The Phase Ordering Problem:

  • (^) Register allocation tries to reuse registers, artificially constrains instruction schedule.
  • (^) Instruction scheduling can dramatically increase register pressure (e.g. schedule all the loads at the beginning).

Which should be done first?

The Delayed-Load-Scheduling Algorithm: [Proebsting & Fischer ’91]

  • Extends Sethi-Ullman register allocation algorithm for expression trees to include instruction scheduling
  • Assume RISC architectures with delayed-loads: 1 cycle per non-memory instruction, 2 cycle per non-blocking load

Jingke Li (Portland State University) CS322 Instruction Scheduling 13 / 17

The DLS Algorithm

  • Run a pre-pass over the expression tree to compute the minimum number of registers (minReg) needed to evaluate every subtree.
  • During the main pass, at each tree node, generate code for the subtree with a higher minReg value first.
  1. Run Sethi-Ullman algorithm
  • calculate minReg for each subtree
  • create an ordering of the operations
  1. Put loads into canonical order (assume k registers and l loads)
  • schedule k loads
  • schedule a series of l − k (operation, load) pairs
  • schedule the remaining operations Preserves relative order of operations and relative order of loads. This arrangement keeps register pressure down.

Property: If k ≥ minReg + 1, there will be no stalls.

An Example

× / + m1 m2 − + m3 m4 m5 m

Canonical Ordering: Operators: −, +, +, /, × Loads: m3, m4, m5, m6, m1, m

Sethi-Ullman DLS(3) DLS(4) 1 load m3, r1 load m3, r1 load m3, r 2 load m4, r2 load m4, r2 load m4, r 3 – stall – load m5, r3 load m5, r 4 sub r1,r2,r2 sub r1,r2,r2 load m6, r 5 load m5, r1 load m6, r1 sub r1,r2,r 6 load m6, r2 – stall – load m1, r 7 – stall – add r1,r3,r3 add r3,r4,r 8 add r1,r3,r3 load m1,r3 load m2, r 9 add r2,r3,r3 add r2,r1,r1 add r2,r4,r 10 load m1, r1 load m2,r2 div r1,r3,r 11 load m2, r2 – stall – mult r4,r3,r 12 – stall – div r3,r2,r 13 div r1,r2,r2 mult r1,r2,r 14 mult r3,r2,r Jingke Li (Portland State University) CS322 Instruction Scheduling 15 / 17

Spilling vs. Stalls

If fewer than minReg+1 regs are available, should we spill or stall? Stalls May be Better:

m1 m2 m3 m4 m5 m6 m7 m

No Spill No Stalls 1 load m1, r1 load m1, r 2 load m2, r2 load m2, r 3 load m3, r3 load m3, r 4 load m4, r4 load m4, r 5 add r1,r2,r2 add r1,r2,r 6 load m5, r1 load m5, r 7 add r3,r4,r4 add r3,r4,r 8 load m6, r3 load m6, r 9 add r2,r4,r4 add r2,r4,r 10 load m7, r2 load m7, r 11 add r1,r3,r3 store r4, TEMP 12 load m8, r1 load m8, r 13 – stall – add r1,r3,r 14 add r2,r1,r1 load TEMP, r 15 add r3,r1,r1 add r2,r4,r 16 add r4,r1,r1 add r3,r4,r 17 add r1,r4,r