+ All Categories
Home > Documents > Pipelining and Hazards - University of California, Berkeleycs61c/resources/su18_lec/... ·...

Pipelining and Hazards - University of California, Berkeleycs61c/resources/su18_lec/... ·...

Date post: 09-Jun-2020
Category:
Upload: others
View: 0 times
Download: 0 times
Share this document with a friend
65
Pipelining and Hazards Instructor: Steven Ho
Transcript
Page 1: Pipelining and Hazards - University of California, Berkeleycs61c/resources/su18_lec/... · 2018-07-11 · Pipelining Hazards A hazard is a situation that prevents starting the next

Pipelining and HazardsInstructor: Steven Ho

Page 2: Pipelining and Hazards - University of California, Berkeleycs61c/resources/su18_lec/... · 2018-07-11 · Pipelining Hazards A hazard is a situation that prevents starting the next

• Parallel RequestsAssigned to computere.g. search “Garcia”

• Parallel ThreadsAssigned to coree.g. lookup, ads

• Parallel Instructions> 1 instruction @ one timee.g. 5 pipelined instructions

• Parallel Data> 1 data item @ one timee.g. add of 4 pairs of words

• Hardware descriptionsAll gates functioning in

parallel at same time

Great Idea #4: Parallelism

7/12/2017 CS61C Su18 - Lecture 13 2

SmartPhone

Warehouse Scale

Computer

LeverageParallelism &Achieve HighPerformance

Core …

Memory

Input/Output

Computer

Core

Software Hardware

Cache Memory

Core

Instruction Unit(s) FunctionalUnit(s)

A0+B

0A

1+B

1A

2+B

2A

3+B

3

Logic Gates

Page 3: Pipelining and Hazards - University of California, Berkeleycs61c/resources/su18_lec/... · 2018-07-11 · Pipelining Hazards A hazard is a situation that prevents starting the next

Review of Last Lecture• Implementing controller for your datapath

– Take decoded signals from instruction and generate control signals

• Pipelining improves performance by exploiting Instruction Level Parallelism– 5-stage pipeline for RISC-V: IF, ID, EX, MEM, WB– Executes multiple instructions in parallel– Each instruction has the same latency– What can go wrong???

37/12/2017 CS61C Su18 - Lecture 13

Page 4: Pipelining and Hazards - University of California, Berkeleycs61c/resources/su18_lec/... · 2018-07-11 · Pipelining Hazards A hazard is a situation that prevents starting the next

Agenda

• RISC-V Pipeline• Hazards

– Structural– Data

• R-type instructions• Load

– Control• Superscalar processors

47/12/2017 CS61C Su18 - Lecture 13

Page 5: Pipelining and Hazards - University of California, Berkeleycs61c/resources/su18_lec/... · 2018-07-11 · Pipelining Hazards A hazard is a situation that prevents starting the next

Recap: Pipelining with RISC-V

7/11/2018 5

add t0, t1, t2

or t3, t4, t5

sll t6, t0, t3

tcycle

instru

ction

sequ

ence

tinstruction

Single Cycle Pipelining

Timing tstep

= 100 … 200 ps tcycle

= 200 ps

Register access only 100 ps All cycles same length

Instruction time, tinstruction

= tcycle

= 800 ps 1000 ps

Clock rate, fs

1/800 ps = 1.25 GHz 1/200 ps = 5 GHz

Relative speed 1 x 4 x

Page 6: Pipelining and Hazards - University of California, Berkeleycs61c/resources/su18_lec/... · 2018-07-11 · Pipelining Hazards A hazard is a situation that prevents starting the next

RISC-V Pipeline

add t0, t1, t2

or t3, t4, t5

slt t6, t0, t3

tcycle

= 200 ps

instru

ction

sequ

ence

tinstruction

= 1000 ps

sw t0, 4(t3)

lw t0, 8(t3)

addi t2, t2, 1

Resource use of instruction over time

Resource use in a particular time slot

7/11/2018 CS61C Su18 - Lecture 13 6

Page 7: Pipelining and Hazards - University of California, Berkeleycs61c/resources/su18_lec/... · 2018-07-11 · Pipelining Hazards A hazard is a situation that prevents starting the next

Single-Cycle RISC-V RV32I Datapath

7/11/2018 7

IMEM

ALU

Imm.Gen

+4

DMEM

Branch Comp.

Reg[]

AddrA

AddrB

DataA

AddrD

DataB

DataD

Addr

DataW

DataR

1

0

0

1

21

0pc

0

1

inst[11:7]

inst[19:15]

inst[24:20]

inst[31:7]

pc+4

alu

mem

wb

alu

pc+4

Reg[rs1]

pc

imm[31:0]

Reg[rs2]

inst[31:0] ImmSel RegWEn BrUn BrEq BrLT ASelBSel ALUSel MemRW WBSelPCSel

wb

Page 8: Pipelining and Hazards - University of California, Berkeleycs61c/resources/su18_lec/... · 2018-07-11 · Pipelining Hazards A hazard is a situation that prevents starting the next

Pipelining RISC-V RV32I Datapath

7/11/2018 8

IMEM

ALU

Imm.Gen

+4

DMEM

Branch Comp.

Reg[]

AddrA

AddrB

DataA

AddrD

DataB

DataD

Addr

DataW

DataR

1

0

0

1

21

0pc

0

1

inst[11:7]

inst[19:15]

inst[24:20]

inst[31:7]

pc+4

alu

mem

wb

alu

pc+4

Reg[rs1]

pc

imm[31:0]

Reg[rs2]

wb

Instruction Fetch(F)

Instruction Decode/Register Read

(D)

ALU Execute(X)

Memory Access(M)

Write Back(W)

Page 9: Pipelining and Hazards - University of California, Berkeleycs61c/resources/su18_lec/... · 2018-07-11 · Pipelining Hazards A hazard is a situation that prevents starting the next

Pipelined RISC-V RV32I Datapath

7/11/20189

IMEM

ALU

+4

DMEM

Branch Comp.

Reg[]

AddrA

AddrB

DataA

AddrD

DataB

DataD

Addr

DataW

DataR

1

0

aluX

pcF+4

+4pcD

pcF

pcX

pcM

instD

instX

rs1X

rs2X

aluM

rs2Mimm

XImm.

Recalculate PC+4 in M stage to avoid sending both PC and PC+4 down pipeline

instM inst

W

Must pipeline instruction along with data, so control operates correctly in each stage

Page 10: Pipelining and Hazards - University of California, Berkeleycs61c/resources/su18_lec/... · 2018-07-11 · Pipelining Hazards A hazard is a situation that prevents starting the next

Each stage operates on different instruction

7/11/201810

IMEM

ALU

+4

DMEM

Branch Comp.

Reg[]

AddrA

AddrB

DataA

AddrD

DataB

DataD

Addr

DataW

DataR

1

0

aluX

pcF+4

+4pcD

pcF

pcX

pcM

instD

instX

rs1X

rs2X

aluM

rs2Mimm

XImm.

instM inst

W

add t0, t1, t2

or t3, t4, t5slt t6, t0, t3sw t0, 4(t3)lw t0, 8(t3)

Pipeline registers separate stages, hold data for each instruction in flight

Page 11: Pipelining and Hazards - University of California, Berkeleycs61c/resources/su18_lec/... · 2018-07-11 · Pipelining Hazards A hazard is a situation that prevents starting the next

Pipelined Control• Control signals derived from instruction

− As in single-cycle implementation− Information is stored in pipeline registers for use by later stages

7/11/2018 11

Page 12: Pipelining and Hazards - University of California, Berkeleycs61c/resources/su18_lec/... · 2018-07-11 · Pipelining Hazards A hazard is a situation that prevents starting the next

Instr

Order

Load

Add

Store

Sub

Or

I$

Time (clock cycles)

I$

AL

U

Reg

Reg

I$

D$

AL

U

AL

U

Reg

D$

Reg

I$

D$

Reg

AL

U

Reg Reg

Reg

D$

Reg

D$

AL

U

• RegFile: right half is read, left half is write

Reg

I$

Graphical Pipeline Representation

127/12/2017 CS61C Su18 - Lecture 13

Page 13: Pipelining and Hazards - University of California, Berkeleycs61c/resources/su18_lec/... · 2018-07-11 · Pipelining Hazards A hazard is a situation that prevents starting the next

Question: Which of the following signals (buses or control signals) for RISC-V does NOT need to be passed into the EX pipeline stage for a beq instruction?

BrUn(A)

MemWr(B)

RegWr(C)

WBSel(D)

IF ID EX Mem WBA

LU I$ Reg D$ Reg

13

beq t0 t1 Label

Page 14: Pipelining and Hazards - University of California, Berkeleycs61c/resources/su18_lec/... · 2018-07-11 · Pipelining Hazards A hazard is a situation that prevents starting the next

Agenda

• RISC-V Pipeline• Hazards

– Structural– Data

• R-type instructions• Load

– Control• Superscalar processors

147/12/2017 CS61C Su18 - Lecture 13

Hazards Ahead!

Page 15: Pipelining and Hazards - University of California, Berkeleycs61c/resources/su18_lec/... · 2018-07-11 · Pipelining Hazards A hazard is a situation that prevents starting the next

Pipelining HazardsA hazard is a situation that prevents starting the

next instruction in the next clock cycle1) Structural hazard

– A required resource is busy(e.g. needed in multiple stages)

2) Data hazard– Data dependency between instructions– Need to wait for previous instruction to

complete its data write

3) Control hazard– Flow of execution depends on previous instruction

157/12/2017 CS61C Su18 - Lecture 13

Page 16: Pipelining and Hazards - University of California, Berkeleycs61c/resources/su18_lec/... · 2018-07-11 · Pipelining Hazards A hazard is a situation that prevents starting the next

Agenda

• RISC-V Pipeline• Hazards

– Structural– Data

• R-type instructions• Load

– Control• Superscalar processors

167/12/2017 CS61C Su18 - Lecture 13

Page 17: Pipelining and Hazards - University of California, Berkeleycs61c/resources/su18_lec/... · 2018-07-11 · Pipelining Hazards A hazard is a situation that prevents starting the next

Structural Hazard

• Problem: Two or more instructions in the pipeline compete for access to a single physical resource

• Solution 1: Instructions take it in turns to use resource, some instructions have to stall

• Solution 2: Add more hardware to machine

• Can always solve a structural hazard by adding more hardware

7/11/2018 CS61C Su18 - Lecture 13 17

Page 18: Pipelining and Hazards - University of California, Berkeleycs61c/resources/su18_lec/... · 2018-07-11 · Pipelining Hazards A hazard is a situation that prevents starting the next

1. Structural Hazards

18

I$

Load

Instr 1

Instr 2

Instr 3

Instr 4

AL

U I$ Reg D$ Reg

AL

U I$ Reg D$ RegA

LU I$ Reg D$ Reg

AL

UReg D$ Reg

AL

U I$ Reg D$ Reg

Instr

Order

Time (clock cycles)

Can we read and write to registers simultaneously?

• Conflict for use of a resource

7/12/2017 CS61C Su18 - Lecture 13

Page 19: Pipelining and Hazards - University of California, Berkeleycs61c/resources/su18_lec/... · 2018-07-11 · Pipelining Hazards A hazard is a situation that prevents starting the next

Regfile Structural Hazards

• Each instruction:− can read up to two operands in decode stage− can write one value in writeback stage

• Avoid structural hazard by having separate “ports”− two independent read ports and one independent write port

• Three accesses per cycle can happen simultaneously

7/11/2018 CS61C Su18 - Lecture 13 19

Page 20: Pipelining and Hazards - University of California, Berkeleycs61c/resources/su18_lec/... · 2018-07-11 · Pipelining Hazards A hazard is a situation that prevents starting the next

• Two alternate solutions:1) Build RegFile with independent read and write

ports (what you will do in the project; good for single-stage)

2) Double Pumping: split RegFile access in two! Prepare to write during 1st half, write on falling edge, read during 2nd half of each clock cycle• Will save us a cycle later...

• Possible because RegFile access is VERY fast (takes less than half the time of ALU stage)

• Conclusion: Read and Write to registers during same clock cycle is okay

207/12/2017 CS61C Su18 - Lecture 13

Regfile Structural Hazards

Page 21: Pipelining and Hazards - University of California, Berkeleycs61c/resources/su18_lec/... · 2018-07-11 · Pipelining Hazards A hazard is a situation that prevents starting the next

I$

Load

Instr 1

Instr 2

Instr 3

Instr 4

AL

U I$ Reg D$ Reg

AL

U I$ Reg D$ RegA

LU I$ Reg D$ Reg

AL

UReg D$ Reg

AL

U I$ Reg D$ Reg

Instr

Order

Time (clock cycles)

Memory Structural Hazards

21

Trying to read (and maybe write) same memory twice in same clock cycle

• Conflict for use of a resource

7/12/2017 CS61C Su18 - Lecture 13

Page 22: Pipelining and Hazards - University of California, Berkeleycs61c/resources/su18_lec/... · 2018-07-11 · Pipelining Hazards A hazard is a situation that prevents starting the next

Instruction and Data Caches

227/11/2018 CS61C Su18 - Lecture 13

Processor

Control

Datapath

PC

Registers

Arithmetic & Logic Unit(ALU)

Memory (DRAM)

Bytes

Program

Data

Instruction Cache

DataCache

Caches: small and fast “buffer” memories

Page 23: Pipelining and Hazards - University of California, Berkeleycs61c/resources/su18_lec/... · 2018-07-11 · Pipelining Hazards A hazard is a situation that prevents starting the next

CS61C Su18 - Lecture 13

Structural Hazards – Summary

• Conflict for use of a resource• In RISC-V pipeline with a single memory

− Load/store requires data access− Without separate memories, instruction fetch would have to stall

for that cycle▪ All other operations in pipeline would have to wait

• Pipelined datapaths require separate instruction/data memories− Or separate instruction/data caches

• RISC ISAs (including RISC-V) designed to avoid structural hazards− e.g. at most one memory access/instruction

23

Page 24: Pipelining and Hazards - University of California, Berkeleycs61c/resources/su18_lec/... · 2018-07-11 · Pipelining Hazards A hazard is a situation that prevents starting the next

Administrivia

• Proj2-2 due 7/13, HW3/4 due 7/16– 2-2 autograder being run

• Guerilla Session Tonight! 4-6pm• HW0-2 grades should now be accurate on

glookup• Project 3 released tomorrow night!• Supplementary review sessions starting

– First one this Sat. (7/14) 12-2p, Cory 540AB

247/12/2017 CS61C Su18 - Lecture 13

Page 25: Pipelining and Hazards - University of California, Berkeleycs61c/resources/su18_lec/... · 2018-07-11 · Pipelining Hazards A hazard is a situation that prevents starting the next

Agenda

• RISC-V Pipeline• Hazards

– Structural– Data

• R-type instructions• Load

– Control• Superscalar processors

257/12/2017 CS61C Su18 - Lecture 13

Page 26: Pipelining and Hazards - University of California, Berkeleycs61c/resources/su18_lec/... · 2018-07-11 · Pipelining Hazards A hazard is a situation that prevents starting the next

2. Data Hazards (1/2)

• Consider the following sequence of instructions:

26

add t0, t1, t2sub t4, t0, t3and t5, t0, t6or t7, t0, t8xor t9, t0, t10

Storedduring WB

Read during ID

7/12/2017 CS61C Su18 - Lecture 13

Page 27: Pipelining and Hazards - University of California, Berkeleycs61c/resources/su18_lec/... · 2018-07-11 · Pipelining Hazards A hazard is a situation that prevents starting the next

2. Data Hazards (2/2)

• Data-flow backward in time are hazards

27

sub t4, t0, t3A

LUI$ Reg D$ Reg

and t5, t0, t6

AL

UI$ Reg D$ Reg

or t7, t0, t8 I$

AL

UReg D$ Reg

xor t9, t0, t10

AL

UI$ Reg D$ Reg

add t0, t1, t2IF ID/RF EX MEM WBA

LUI$ Reg D$ Reg

Instr

Order

Time (clock cycles)

7/12/2017 CS61C Su18 - Lecture 13

Hazard if no double pumping

Page 28: Pipelining and Hazards - University of California, Berkeleycs61c/resources/su18_lec/... · 2018-07-11 · Pipelining Hazards A hazard is a situation that prevents starting the next

Solution 1: Stalling

• Problem: Instruction depends on result from previous instruction− add t0, t1, t2

sub t4, t0, t3

• Bubble: − effectively NOP: affected pipeline stages do “nothing”

Page 29: Pipelining and Hazards - University of California, Berkeleycs61c/resources/su18_lec/... · 2018-07-11 · Pipelining Hazards A hazard is a situation that prevents starting the next

Stalls and Performance

• Stalls reduce performance− But stalls are required to get correct results

• Compiler can arrange code to avoid hazards and stalls− Requires knowledge of the pipeline structure

7/11/2018 29

Page 30: Pipelining and Hazards - University of California, Berkeleycs61c/resources/su18_lec/... · 2018-07-11 · Pipelining Hazards A hazard is a situation that prevents starting the next

Data Hazard Solution: Forwarding• Forward result as soon as it is available

– OK that it’s not stored in RegFile yet

30

sub t4, t0, t3A

LUI$ Reg D$ Reg

and t5, t0, t6

AL

UI$ Reg D$ Reg

or t7, t0, t8 I$

AL

UReg D$ Reg

xor t9, t0, t10

AL

UI$ Reg D$ Reg

add t0, t1, t2IF ID/RF EX MEM WBA

LUI$ Reg D$ Reg

Arithmetic resultavailable in EX

7/12/2017

Forwarding: grab operand from pipeline stage, rather than register file

Page 31: Pipelining and Hazards - University of California, Berkeleycs61c/resources/su18_lec/... · 2018-07-11 · Pipelining Hazards A hazard is a situation that prevents starting the next

Forwarding (aka Bypassing)• Use result when it is computed− Don’t wait for it to be stored in a register− Requires extra connections in the datapath

7/11/2018 31CS61C Su18 - Lecture 13

Page 32: Pipelining and Hazards - University of California, Berkeleycs61c/resources/su18_lec/... · 2018-07-11 · Pipelining Hazards A hazard is a situation that prevents starting the next

Detect Need for Forwarding (example)

add t0, t1, t2

or t3, t0, t5

sub t6, t0, t3

X M WD

instX.rd

instD

.rs1

7/11/2018 32

Compare destination of older instructions in pipeline with sources of new instruction in decode stage.Must ignore writes to x0!

Page 33: Pipelining and Hazards - University of California, Berkeleycs61c/resources/su18_lec/... · 2018-07-11 · Pipelining Hazards A hazard is a situation that prevents starting the next

Forwarding Path

7/11/201833

IMEM

ALU

+4

DMEM

Branch Comp.

Reg[]

AddrA

AddrB

DataA

AddrD

DataB

DataD

Addr

DataW

DataR

1

0

aluX

pcF+4

+4pcD

pcF

pcX

pcM

instD

instX

rs1X

rs2X

aluM

rs2Mimm

XImm.

instM inst

W

Forwarding Control Logic

Page 34: Pipelining and Hazards - University of California, Berkeleycs61c/resources/su18_lec/... · 2018-07-11 · Pipelining Hazards A hazard is a situation that prevents starting the next

Agenda

• RISC-V Pipeline• Hazards

– Structural– Data

• R-type instructions• Load

– Control• Superscalar processors

347/12/2017 CS61C Su18 - Lecture 13

Page 35: Pipelining and Hazards - University of California, Berkeleycs61c/resources/su18_lec/... · 2018-07-11 · Pipelining Hazards A hazard is a situation that prevents starting the next

Data Hazard: Loads (1/4)

• Recall: Dataflow backwards in time are hazards

• Can’t solve all cases with forwarding– Must stall instruction dependent on load, then

forward (more hardware)35

sub t3, t0, t2

AL

UI$ Reg D$ Reg

lw t0, 0(t1)IF ID/RF EX MEM WBA

LUI$ Reg D$ Reg

7/12/2017 CS61C Su18 - Lecture 13

Page 36: Pipelining and Hazards - University of California, Berkeleycs61c/resources/su18_lec/... · 2018-07-11 · Pipelining Hazards A hazard is a situation that prevents starting the next

Data Hazard: Loads (2/4)

• Hardware stalls pipeline– Called “hardware interlock”

36

sub t3, t0, t2

AL

UI$ Reg D$ Regbubble

and t5, t0, t4

AL

UI$ Reg D$ Regbubble

or t7, t0, t6 I$

AL

UReg D$bubble

lw t0, 0(t1)IF ID/RF EX MEM WBA

LUI$ Re

g D$ Reg

This is what happens in hardware in a “hardware interlock”

Must stall entire pipeline

7/12/2017 CS61C Su18 - Lecture 13

Page 37: Pipelining and Hazards - University of California, Berkeleycs61c/resources/su18_lec/... · 2018-07-11 · Pipelining Hazards A hazard is a situation that prevents starting the next

Data Hazard: Loads (3/4)

• Stall is equivalent to nop

37

sub t3, t0, t2

and t5, t0, t4

or t7, t0, t6 I$

AL

UReg D$

lw t0, 0(t1) AL

UI$ Reg D$ Reg

bubble

bubble

bubble

bubble

bubble

AL

UI$ Reg D$ Reg

AL

UI$ Reg D$ Reg

nop

7/12/2017 CS61C Su18 - Lecture 13

Page 38: Pipelining and Hazards - University of California, Berkeleycs61c/resources/su18_lec/... · 2018-07-11 · Pipelining Hazards A hazard is a situation that prevents starting the next

Data Hazard: Loads (4/4)

• Slot after a load is called a load delay slot– If that instruction uses the result of the load,

then the hardware will stall for one cycle– Equivalent to inserting an explicit nop in the

slot• except the latter uses more code space

– Performance loss• Idea: Let the compiler/assembler put an

unrelated instruction in that slot → no stall!

387/12/2017 CS61C Su18 - Lecture 13

Page 39: Pipelining and Hazards - University of California, Berkeleycs61c/resources/su18_lec/... · 2018-07-11 · Pipelining Hazards A hazard is a situation that prevents starting the next

Code Scheduling to Avoid Stalls

• Reorder code to avoid use of load result in the next instruction!

• RISC-V code for D=A+B; E=A+C;

39

Original Order:lw t1, 0(t0)lw t2, 4(t0)add t3, t1, t2sw t3, 12(t0)lw t4, 8(t0)add t5, t1, t4sw t5, 16(t0)

Alternative:lw t1, 0(t0)lw t2, 4(t0)lw t4, 8(t0)add t3, t1, t2sw t3, 12(t0)add t5, t1, t4sw t5, 16(t0)

Stall!

Stall!

13 cycles11 cycles

7/11/2018

Page 40: Pipelining and Hazards - University of California, Berkeleycs61c/resources/su18_lec/... · 2018-07-11 · Pipelining Hazards A hazard is a situation that prevents starting the next

Break!

7/09/2018 40CS61C Su18 - Lecture 11

Page 41: Pipelining and Hazards - University of California, Berkeleycs61c/resources/su18_lec/... · 2018-07-11 · Pipelining Hazards A hazard is a situation that prevents starting the next

Agenda

• RISC-V Pipeline• Hazards

– Structural– Data

• R-type instructions• Load

– Control• Superscalar processors

417/12/2017 CS61C Su18 - Lecture 13

Page 42: Pipelining and Hazards - University of California, Berkeleycs61c/resources/su18_lec/... · 2018-07-11 · Pipelining Hazards A hazard is a situation that prevents starting the next

3. Control Hazards

• Branch (beq, bne) determines flow of control– Fetching next instruction depends on branch

outcome– Pipeline can’t always fetch correct instruction

• Still working on ID stage of branch

• Simple Solution: Stall on every branch until we have the new PC value– How long must we stall?

7/13/2016 CS61C Su16 - Lecture 13 42

Page 43: Pipelining and Hazards - University of California, Berkeleycs61c/resources/su18_lec/... · 2018-07-11 · Pipelining Hazards A hazard is a situation that prevents starting the next

Branch Stall

7/13/2016 CS61C Su16 - Lecture 13 43

• When is comparison result available?

I$

beq

Instr 1

Instr 2

Instr 3

Instr 4

AL

U I$ Reg D$ Reg

AL

U I$ Reg D$ RegA

LU I$ Reg D$ Reg

AL

UReg D$ Reg

AL

U I$ Reg D$ Reg

Instr

Order

Time (clock cycles)

TWO bubbles required per branch!

Page 44: Pipelining and Hazards - University of California, Berkeleycs61c/resources/su18_lec/... · 2018-07-11 · Pipelining Hazards A hazard is a situation that prevents starting the next

3. Control Hazard: Branching

• Option #1: Moving branch comparator to ID stage– As soon as instruction is decoded, immediately

make a decision and set the new value of the PC

– Benefit: Branch decision made in 2nd stage, so only one nop is needed instead of two

– Side Note: Have to compute new PC value in ID instead of EX

• Adds extra hardware and reduces redundancy

• Branches are idle in EX, MEM, and WB7/13/2016 CS61C Su16 - Lecture 13 44

Page 45: Pipelining and Hazards - University of California, Berkeleycs61c/resources/su18_lec/... · 2018-07-11 · Pipelining Hazards A hazard is a situation that prevents starting the next

Improved Branch Stall

7/13/2016 CS61C Su16 - Lecture 13 45

• When is comparison result available?

I$

beq

Instr 1

Instr 2

Instr 3

Instr 4

AL

U I$ Reg D$ Reg

AL

U I$ Reg D$ RegA

LU I$ Reg D$ Reg

AL

UReg D$ Reg

AL

U I$ Reg D$ Reg

Instr

Order

Time (clock cycles)

Only one bubble required now

Page 46: Pipelining and Hazards - University of California, Berkeleycs61c/resources/su18_lec/... · 2018-07-11 · Pipelining Hazards A hazard is a situation that prevents starting the next

Data Hazard: Branches!

• Recall: Dataflow backwards in time are hazards

• Now that t0 is needed earlier (ID instead of EX), we can’t forward it to the beq’s ID stage– Must stall after add, then forward (more hardware)

46

beq x0, t0, foo

AL

UI$ Reg D$ Reg

add t0, t0, t1IF ID/RF EX MEM WBA

LUI$ Reg D$ Reg

7/12/2017 CS61C Su18 - Lecture 13

Page 47: Pipelining and Hazards - University of California, Berkeleycs61c/resources/su18_lec/... · 2018-07-11 · Pipelining Hazards A hazard is a situation that prevents starting the next

•Takeaway: Moving branch comparator to ID stage would add extra hardware, reduce redundancy, and introduce new problems

•Can we work with the nature of branches?− If branch not taken, then instructions

fetched sequentially after branch are correct

− If branch or jump taken, then need to flush incorrect instructions from pipeline by converting to NOPs

Observations

7/11/2018 CS61C Su18 - Lecture 13 47

Page 48: Pipelining and Hazards - University of California, Berkeleycs61c/resources/su18_lec/... · 2018-07-11 · Pipelining Hazards A hazard is a situation that prevents starting the next

3. Control Hazard: Branching

• RISC-V Solution: Branch Prediction – guess outcome of a branch, fix afterwards if necessary– Must cancel (flush) all instructions in pipeline that

depended on guess that was wrong

– How many instructions do we end up flushing?

7/13/2016 CS61C Su16 - Lecture 13 48

Page 49: Pipelining and Hazards - University of California, Berkeleycs61c/resources/su18_lec/... · 2018-07-11 · Pipelining Hazards A hazard is a situation that prevents starting the next

Kill Instructions after Branch if Taken

beq t0, t1, label

sub t2, s0, t5

or t6, s0, t3

label: xxxxxxPC updated reflecting branch outcome

7/11/2018 CS61C Su18 - Lecture 13 49

Taken branch

Convert to NOP

Convert to NOP

Page 50: Pipelining and Hazards - University of California, Berkeleycs61c/resources/su18_lec/... · 2018-07-11 · Pipelining Hazards A hazard is a situation that prevents starting the next

Branch Prediction

beq t0, t1, label

label: …..

…..

7/11/2018 CS61C Su18 - Lecture 13 50

Taken branch

Guess next PC!

Check guess correct

Page 51: Pipelining and Hazards - University of California, Berkeleycs61c/resources/su18_lec/... · 2018-07-11 · Pipelining Hazards A hazard is a situation that prevents starting the next

Dynamic Branch Prediction

• Branch penalty is more significant in deeper

pipelines

• Use dynamic branch prediction

– Have branch prediction buffer (a.k.a. branch history table)

that stores outcomes (taken/not taken) indexed by recent

branch instruction addresses

– To execute a branch

• Check table and predict the same outcome for next fetch

• If wrong, flush pipeline and flip prediction

7/13/2016 CS61C Su16 - Lecture 13 51

Page 52: Pipelining and Hazards - University of California, Berkeleycs61c/resources/su18_lec/... · 2018-07-11 · Pipelining Hazards A hazard is a situation that prevents starting the next

1-Bit Predictor: Shortcoming• Examine the code below, assuming both loops will be

executed multiple times:

7/13/2016 CS61C Su16 - Lecture 13 52

outer: … …inner: … … beq …, …, inner … beq …, …, outer

• Inner loop branches are predicted wrong twice!– Predict as taken on last iteration of inner loop

– Then predict as not taken on first iteration of inner loop next time around

Page 53: Pipelining and Hazards - University of California, Berkeleycs61c/resources/su18_lec/... · 2018-07-11 · Pipelining Hazards A hazard is a situation that prevents starting the next

Agenda

• Structural Hazards• Data Hazards

– Forwarding

• Administrivia• Data Hazards (Continued)

– Load Delay Slot

• Control Hazards– Branch and Jump Delay Slots– Branch Prediction

7/13/2016 CS61C Su16 - Lecture 13 53

Page 54: Pipelining and Hazards - University of California, Berkeleycs61c/resources/su18_lec/... · 2018-07-11 · Pipelining Hazards A hazard is a situation that prevents starting the next

Question: For each code sequences below, choose one of the statements below:

No stalls as isA

No stalls with forwardingB

Must stallC

1: lw$t0,0($t0) add $t1,$t0,$t0

2: add $t1,$t0,$t0 addi $t2,$t0,5 addi $t4,$t1,5

3: addi $t1,$t0,1 addi $t2,$t0,2 addi $t3,$t0,2 addi $t3,$t0,4 addi $t5,$t1,5

54

Page 55: Pipelining and Hazards - University of California, Berkeleycs61c/resources/su18_lec/... · 2018-07-11 · Pipelining Hazards A hazard is a situation that prevents starting the next

Code Sequence 1

7/13/2016 CS61C Su16 - Lecture 13 55

I$

lw

add

instr

instr

instrA

LU I$ Reg D$ Reg

AL

U I$ Reg D$ Reg

AL

U I$ Reg D$ Reg

AL

UReg D$ Reg

AL

U I$ Reg D$ Reg

Instr

Order

Time (clock cycles)

Must stall

Page 56: Pipelining and Hazards - University of California, Berkeleycs61c/resources/su18_lec/... · 2018-07-11 · Pipelining Hazards A hazard is a situation that prevents starting the next

Code Sequence 2

7/13/2016 CS61C Su16 - Lecture 13 56

I$

add

addi

addi

instr

instrA

LU I$ Reg D$ Reg

AL

U I$ Reg D$ Reg

AL

U I$ Reg D$ Reg

AL

UReg D$ Reg

AL

U I$ Reg D$ Reg

Instr

Order

Time (clock cycles)forwarding

no forwarding

No stalls with forwarding

Page 57: Pipelining and Hazards - University of California, Berkeleycs61c/resources/su18_lec/... · 2018-07-11 · Pipelining Hazards A hazard is a situation that prevents starting the next

Code Sequence 3

7/13/2016 CS61C Su16 - Lecture 13 57

I$

addi

addi

addi

addi

addiA

LU I$ Reg D$ Reg

AL

U I$ Reg D$ Reg

AL

U I$ Reg D$ Reg

AL

UReg D$ Reg

AL

U I$ Reg D$ Reg

Instr

Order

Time (clock cycles)

No stalls as is

Page 58: Pipelining and Hazards - University of California, Berkeleycs61c/resources/su18_lec/... · 2018-07-11 · Pipelining Hazards A hazard is a situation that prevents starting the next

Agenda

• RISC-V Pipeline• Hazards

– Structural– Data

• R-type instructions• Load

– Control• Superscalar processors

587/12/2017 CS61C Su18 - Lecture 13

Page 59: Pipelining and Hazards - University of California, Berkeleycs61c/resources/su18_lec/... · 2018-07-11 · Pipelining Hazards A hazard is a situation that prevents starting the next

Increasing Processor Performance

1. Clock rate− Limited by technology and power dissipation

2. Pipelining− “Overlap” instruction execution− Deeper pipeline: 5 => 10 => 15 stages

▪ Less work per stage → shorter clock cycle▪ But more potential for hazards (CPI > 1)

3. Multi-issue ”super-scalar” processor− Multiple execution units (ALUs)

▪ Several instructions executed simultaneously▪ CPI < 1 (ideally)

7/11/2018 CS61C Su18 - Lecture 13 59

Page 60: Pipelining and Hazards - University of California, Berkeleycs61c/resources/su18_lec/... · 2018-07-11 · Pipelining Hazards A hazard is a situation that prevents starting the next

Superscalar Processor

7/11/2018 CS61C Su18 - Lecture 13 60

P&H p. 340

Page 61: Pipelining and Hazards - University of California, Berkeleycs61c/resources/su18_lec/... · 2018-07-11 · Pipelining Hazards A hazard is a situation that prevents starting the next

Benchmark: CPI of Intel Core i7

7/11/2018 CS61C Su18 - Lecture 13 61

CPI = 1

P&H p. 350

Page 62: Pipelining and Hazards - University of California, Berkeleycs61c/resources/su18_lec/... · 2018-07-11 · Pipelining Hazards A hazard is a situation that prevents starting the next

• Hazards reduce effectiveness of pipelining– Cause stalls/bubbles

• Structural Hazards– Conflict in use of a datapath component

• Data Hazards– Need to wait for result of a previous instruction

• Control Hazards– Address of next instruction uncertain/unknown

• Superscalar processors use multiple execution units for additional instruction level parallelism– Performance benefit highly code dependent

Summary

7/13/2016 CS61C Su16 - Lecture 13 62

Page 63: Pipelining and Hazards - University of California, Berkeleycs61c/resources/su18_lec/... · 2018-07-11 · Pipelining Hazards A hazard is a situation that prevents starting the next

Extra Slides

7/11/2018 CS61C Su18 - Lecture 13 63

Page 64: Pipelining and Hazards - University of California, Berkeleycs61c/resources/su18_lec/... · 2018-07-11 · Pipelining Hazards A hazard is a situation that prevents starting the next

CS61C Su18 - Lecture 13

Pipelining and ISA Design

• RISC-V ISA designed for pipelining− All instructions are 32-bits

▪ Easy to fetch and decode in one cycle▪ Versus x86: 1- to 15-byte instructions

− Few and regular instruction formats▪ Decode and read registers in one step

− Load/store addressing▪ Calculate address in 3rd stage, access memory in 4th stage

− Alignment of memory operands▪ Memory access takes only one cycle

7/11/2018 64

Page 65: Pipelining and Hazards - University of California, Berkeleycs61c/resources/su18_lec/... · 2018-07-11 · Pipelining Hazards A hazard is a situation that prevents starting the next

Superscalar Processor

• Multiple issue “superscalar”− Replicate pipeline stages ⇒ multiple pipelines− Start multiple instructions per clock cycle− CPI < 1, so use Instructions Per Cycle (IPC)− E.g., 4GHz 4-way multiple-issue

▪ 16 BIPS, peak CPI = 0.25, peak IPC = 4− Dependencies reduce this in practice

• “Out-of-Order” execution− Reorder instructions dynamically in hardware to reduce impact of

hazards

• CS152 discusses these techniques!

7/11/2018 CS61C Su18 - Lecture 13 65


Recommended