pipeline - unicamppannain/mc542/aulas/ch6_arq.pdf · 2006. 8. 2. · – problemas devido à...

48
1 Ch6.a-1 ? 1998 Morgan Kaufmann Publishers Paulo C. Centoducatte Pipeline Ch6.a-2 ? 1998 Morgan Kaufmann Publishers Paulo C. Centoducatte Lavanderia analogia com o pipelining Time 7 6 PM 8 9 10 11 12 1 2 AM A B C D Task order Time 7 6 PM 8 9 10 11 12 1 2 AM A B C D Task order

Upload: others

Post on 07-Aug-2021

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Pipeline - Unicamppannain/mc542/aulas/ch6_arq.pdf · 2006. 8. 2. · – Problemas devido à execução de instruções de desvio • Ex.: Quando umbranch é tomado, como tratar a(s)

1

Ch6.a-1? 1998 Morgan Kaufmann PublishersPaulo C. Centoducatte

Pipeline

Ch6.a-2? 1998 Morgan Kaufmann PublishersPaulo C. Centoducatte

Lavanderia – analogia com o pipeliningTime

76 PM 8 9 10 11 12 1 2 AM

A

B

C

D

Taskorder

Time76 PM 8 9 10 11 12 1 2 AM

A

B

C

D

Taskorder

Page 2: Pipeline - Unicamppannain/mc542/aulas/ch6_arq.pdf · 2006. 8. 2. · – Problemas devido à execução de instruções de desvio • Ex.: Quando umbranch é tomado, como tratar a(s)

2

Ch6.a-3? 1998 Morgan Kaufmann PublishersPaulo C. Centoducatte

Pipeline de instruções no MIPS

• Fetch da instrução

• Leitura dos registradores e decodificação

• Execução da operação ou cálculo de endereço

• Acesso ao operando na memória

• Escrita do resultado em um registrador

Ch6.a-4? 1998 Morgan Kaufmann PublishersPaulo C. Centoducatte

Exemplo

• Compare o tempo médio entre instruções da implementação em single-cycle (uma instrução por ciclo) com uma implementação com pipeline. Supor maior tempo de operação para acesso à memória = 2ns, operação da ULA = 2ns e acesso ao register file = 1ns. (Instrs lw, sw, add, sub, and, or slt e beq).

• Inicialmente suponha a execução de 3 instruções lw (tempo entre o início da 1ª instrução e o início da 4ª instrução)

Page 3: Pipeline - Unicamppannain/mc542/aulas/ch6_arq.pdf · 2006. 8. 2. · – Problemas devido à execução de instruções de desvio • Ex.: Quando umbranch é tomado, como tratar a(s)

3

Ch6.a-5? 1998 Morgan Kaufmann PublishersPaulo C. Centoducatte

Tempo total para as oito instruções calculado a partir do tempo de cada componente

Ch6.a-6? 1998 Morgan Kaufmann PublishersPaulo C. Centoducatte

Execução não-pipeline X pipeline

Instructionfetch Reg ALU Data

access Reg

8 ns Instructionfetch Reg ALU Data

access Reg

8 nsInstruction

fetch

8 ns

Time

lw $1, 100($0)

lw $2, 200($0)

lw $3, 300($0)

2 4 6 8 10 12 14 16 18

2 4 6 8 10 12 14

...

Programexecutionorder(in instructions)

Instructionfetch Reg ALU Data

access Reg

Time

lw $1, 100($0)

lw $2, 200($0)

lw $3, 300($0)

2 ns Instructionfetch Reg ALU Data

access Reg

2 nsInstruction

fetch Reg ALU Dataaccess Reg

2 ns 2 ns 2 ns 2 ns 2 ns

Programexecutionorder(in instructions)

Page 4: Pipeline - Unicamppannain/mc542/aulas/ch6_arq.pdf · 2006. 8. 2. · – Problemas devido à execução de instruções de desvio • Ex.: Quando umbranch é tomado, como tratar a(s)

4

Ch6.a-7? 1998 Morgan Kaufmann PublishersPaulo C. Centoducatte

OBS.:

• Sob condições ideais, com estágios balanceados, o speedup do pipeline é igual ao número de estágios do pipeline ( 5 estágios , 5 vezes mais rápido)

• Na realidade o tempo de execução de uma instrução é um pouco superior (overheads) ? speedup é menor que o número de estágios do pipeline

Suponha a execução de 1003 instruções

com pipeline?1000 X 2ns + 14 = 2014 (para cada instrução adiciono 2ns)

sem pipeline? 1000 X 8ns + 24 = 8024

spedup = 8024 / 2014 = 3.98 ~~ 8 / 2

Desempenho do pipelineé devido ao aumento dothroughput.

Ch6.a-8? 1998 Morgan Kaufmann PublishersPaulo C. Centoducatte

Projeto de um conjunto de instruções para pipeline

? O que torna a implementação mais fácil

– Instruções de mesmo tamanho– Poucos formatos, com campos de registradores

sempre dispostos no mesmo lugar (Simetria, no 2º

estágio podemos ler registradores e decodificar ao mesmo tempo).

– Acesso à memória apenas com as instruções lw e sw.– Operandos alinhados na memória: o dado pode ser

transferido da memória para a CPU e CPU para amemória em um único estágio do pipeline.

Page 5: Pipeline - Unicamppannain/mc542/aulas/ch6_arq.pdf · 2006. 8. 2. · – Problemas devido à execução de instruções de desvio • Ex.: Quando umbranch é tomado, como tratar a(s)

5

Ch6.a-9? 1998 Morgan Kaufmann PublishersPaulo C. Centoducatte

Projeto de um conjunto de instruções para pipeline

? O que torna a implementação mais dificil

– Hazard

• Hazard Estrural

• Hazard de Controle

• Hazard de Dados

Ch6.a-10? 1998 Morgan Kaufmann PublishersPaulo C. Centoducatte

Pipeline Hazards

• Hazard Estrutural

– O hardware não suporta uma combinação de instruções que queremos executar em um único período de clock

• Ex.: escrever e ler da memória em um mesmo ciclo

Page 6: Pipeline - Unicamppannain/mc542/aulas/ch6_arq.pdf · 2006. 8. 2. · – Problemas devido à execução de instruções de desvio • Ex.: Quando umbranch é tomado, como tratar a(s)

6

Ch6.a-11? 1998 Morgan Kaufmann PublishersPaulo C. Centoducatte

Pipeline Hazards

• Hazard de Controle

– Problemas devido à execução de instruções de desvio

• Ex.: Quando um branch é tomado, como tratar a(s) instruções que seguem (fisicamente) o branch no programa e que já estão no pipeline

Ch6.a-12? 1998 Morgan Kaufmann PublishersPaulo C. Centoducatte

Pipelining stalling para instruções branch

Instructionfetch

Reg ALU Dataaccess

Reg

Time

beq $1, $2, 40

add $4, $5, $6

lw $3, 300($0)4 ns

Instructionfetch

Reg ALU Dataaccess

Reg2ns

Instructionfetch

Reg ALU Dataaccess

Reg

2ns

2 4 6 8 10 12 14 16Programexecutionorder(in instructions)

Page 7: Pipeline - Unicamppannain/mc542/aulas/ch6_arq.pdf · 2006. 8. 2. · – Problemas devido à execução de instruções de desvio • Ex.: Quando umbranch é tomado, como tratar a(s)

7

Ch6.a-13? 1998 Morgan Kaufmann PublishersPaulo C. Centoducatte

Branch prediction: Tentar “adivinhar” qual dos caminhos do branchserá tomado

Instructionfetch Reg ALU Data

access Reg

Time

beq $1, $2, 40

add $4, $5, $6

lw $3, 300($0)

Instructionfetch

Reg ALU Dataaccess

Reg2 ns

Instructionfetch Reg ALU Data

access Reg2 ns

Programexecutionorder(in instructions)

Instructionfetch Reg ALU Data

access Reg

Time

beq $1, $2, 40

add $4, $5 ,$6

or $7, $8, $9

Instructionfetch Reg ALU Data

access Reg

2 4 6 8 10 12 14

2 4 6 8 10 12 14

Instructionfetch Reg ALU Data

access Reg

2 ns

4 ns

bubble bubble bubble bubble bubble

Programexecutionorder(in instructions)

O branch não será tomado

Ch6.a-14? 1998 Morgan Kaufmann PublishersPaulo C. Centoducatte

Pipeline delayed branch

Instructionfetch Reg ALU

Dataaccess Reg

Time

beq $1, $2, 40

add $4, $5, $6

lw $3, 300($0)

Instructionfetch Reg ALU

Dataaccess Reg

2 ns

Instructionfetch Reg ALU

Dataaccess Reg

2 ns

2 4 6 8 10 12 14

2 ns

(Delayed branch slot)

Programexecutionorder(in instructions)

Page 8: Pipeline - Unicamppannain/mc542/aulas/ch6_arq.pdf · 2006. 8. 2. · – Problemas devido à execução de instruções de desvio • Ex.: Quando umbranch é tomado, como tratar a(s)

8

Ch6.a-15? 1998 Morgan Kaufmann PublishersPaulo C. Centoducatte

Hazard de Dados

• Quando uma instrução necessita de um dado que ainda não foi calculado

– Ex.: add $s0,$t0,$t1

sub $t2,$s0,$t3

Soluções : Compilador (programador) gera código livre de data hazard (introduzindo, por ex., instruções nopno código; alterando a ordem das instruções; ...)

Stall; Forwarding ou bypassing

Ch6.a-16? 1998 Morgan Kaufmann PublishersPaulo C. Centoducatte

Hazard de Dados

Time2 4 6 8 10

add $s0, $t0, $t1 IF ID WBEX MEM

add $s0, $t0, $t1

sub $t2, $s0, $t3

Programexecutionorder(in instructions)

IF ID WBEX

IF ID MEMEX

Time2 4 6 8 10

MEM

WBMEM

Page 9: Pipeline - Unicamppannain/mc542/aulas/ch6_arq.pdf · 2006. 8. 2. · – Problemas devido à execução de instruções de desvio • Ex.: Quando umbranch é tomado, como tratar a(s)

9

Ch6.a-17? 1998 Morgan Kaufmann PublishersPaulo C. Centoducatte

Hazard de Dados

Time2 4 6 8 10 12 14

lw $s0, 20($t1)

sub $t2, $s0, $t3

Programexecutionorder(in instructions)

IF ID WBMEMEX

IF ID WBMEMEX

bubble bubble bubble bubble bubble

Stall

Ch6.a-18? 1998 Morgan Kaufmann PublishersPaulo C. Centoducatte

Exemplo

• Encontre o hazard no código abaixo e resolva-o:

# $t1 tem o end. de v[k]lw $t0, 0($t1) # $t0 = v[k]lw $t2,4($t1) # $t2 = v[k+1]sw $t2, 0($t1) # v[k] = $t2sw $t0, 4($t1) # v[k+1] = $t0

Solução:# $t1 tem o end. de v[k]

lw $t0, 0($t1) # $t0 = v[k]lw $t2,4($t1) # $t2 = v[k+1]sw $t0, 4($t1) # v[k+1] = $t0sw $t2, 0($t1) # v[k] = $t2

Page 10: Pipeline - Unicamppannain/mc542/aulas/ch6_arq.pdf · 2006. 8. 2. · – Problemas devido à execução de instruções de desvio • Ex.: Quando umbranch é tomado, como tratar a(s)

10

Ch6.a-19? 1998 Morgan Kaufmann PublishersPaulo C. Centoducatte

Pipeline: Idéia Básica • 5 estágios: Fetch; Decodificação e leitura dos regs; execução ou cálculo

de end. ; acesso à memória; escrita no reg. destino

Instructionmemory

Address

4

32

0

Add Addresult

Shiftleft 2

Instruction

Mux

0

1

Add

PC

0Writedata

Mux

1Registers

Readdata 1

Readdata 2

Readregister 1

Readregister 2

16Sign

extend

Writeregister

Writedata

ReaddataAddress

Datamemory

1

ALUresult

Mux

ALUZero

IF: Instruction fetch ID: Instruction decode/register file read

EX: Execute/address calculation

MEM: Memory access WB: Write back

O que é necessário para tornar cada divisão em estágios?

Ch6.a-20? 1998 Morgan Kaufmann PublishersPaulo C. Centoducatte

Instruções sendo executadas pelo datapath

IM Reg DM RegALU

IM Reg DM RegALU

CC 1 CC 2 CC 3 CC 4 CC 5 CC 6 CC 7

Time (in clock cycles)

lw $2, 200($0)

lw $3, 300($0)

Programexecutionorder(in instructions)

lw $1, 100($0) IM Reg DM RegALU

Page 11: Pipeline - Unicamppannain/mc542/aulas/ch6_arq.pdf · 2006. 8. 2. · – Problemas devido à execução de instruções de desvio • Ex.: Quando umbranch é tomado, como tratar a(s)

11

Ch6.a-21? 1998 Morgan Kaufmann PublishersPaulo C. Centoducatte

Pipelined Datapath

Instructionmemory

Address

4

32

0

Add Addresult

Shiftleft 2

Inst

ruct

ion

IF/ID EX/MEM MEM/WB

Mux

0

1

Add

PC

0Writedata

Mux

1Registers

Readdata 1

Readdata 2

Readregister 1

Readregister 2

16Sign

extend

Writeregister

Writedata

Readdata

1

ALUresult

Mux

ALUZero

ID/EX

Datamemory

Address

Pode acontecer algum problema nesta solução se não existir dependência de dados?A execução de qual instrução causa o problema?

Ch6.a-22? 1998 Morgan Kaufmann PublishersPaulo C. Centoducatte

Pipelined Datapath

Instructionmemory

Address

4

32

0

Add Addresult

Shiftleft 2

Inst

ruct

ion

IF/ID EX/MEM MEM/WB

Mux

0

1

Add

PC

0Writedata

Mux

1Registers

Readdata 1

Readdata 2

Readregister 1

Readregister 2

16Sign

extend

Writeregister

Writedata

Readdata

1

ALUresult

Mux

ALUZero

ID/EX

Instruction fetch

lw

Address

Datamemory

Page 12: Pipeline - Unicamppannain/mc542/aulas/ch6_arq.pdf · 2006. 8. 2. · – Problemas devido à execução de instruções de desvio • Ex.: Quando umbranch é tomado, como tratar a(s)

12

Ch6.a-23? 1998 Morgan Kaufmann PublishersPaulo C. Centoducatte

Pipelined Datapath

Instructionmemory

Address

4

32

0

Add Addresult

Shiftleft 2

Inst

ruct

ion

IF/ID EX/MEM

Mux

0

1

Add

PC

0Writedata

Mux

1Registers

Readdata 1

Readdata 2

Readregister 1

Readregister 2

16Sign

extend

Writeregister

Writedata

Readdata

1

ALUresult

Mux

ALUZero

ID/EX MEM/WB

Instruction decode

lw

Address

Datamemory

Ch6.a-24? 1998 Morgan Kaufmann PublishersPaulo C. Centoducatte

Pipelined Datapath

Instructionmemory

Address

4

32

0

Add Addresult

Shiftleft 2

Inst

ruct

ion

IF/ID EX/MEM

Mux

0

1

Add

PC

0Writedata

Mux

1Registers

Readdata 1

Readdata 2

Readregister 1

Readregister 2

16Sign

extend

Writeregister

Writedata

Readdata

1

ALUresult

Mux

ALUZero

ID/EX MEM/WB

Executionlw

Address

D atamemory

Page 13: Pipeline - Unicamppannain/mc542/aulas/ch6_arq.pdf · 2006. 8. 2. · – Problemas devido à execução de instruções de desvio • Ex.: Quando umbranch é tomado, como tratar a(s)

13

Ch6.a-25? 1998 Morgan Kaufmann PublishersPaulo C. Centoducatte

Pipelined Datapath

Instructionmemory

Address

4

32

0

Add Addresult

Shiftleft 2

Inst

ruct

ion

IF/ID EX/MEM

Mux

0

1

Add

PC

0Writedata

Mux

1Registers

Readdata 1

Readdata 2

Readregister 1

Readregister 2

16Sign

extend

Writeregister

Writedata

Readdata

Datamemory

1

ALUresult

Mux

ALUZero

ID/EX MEM/WB

Memory

lw

Address

Ch6.a-26? 1998 Morgan Kaufmann PublishersPaulo C. Centoducatte

Pipelined Datapath

Instructionmemory

Address

4

32

0

Add Addresult

Shiftleft 2

Inst

ruct

ion

IF/ID EX/MEM

Mux

0

1

Add

PC

0Writedata

Mux

1Registers

Readdata 1

Readdata 2

Readregister 1

Readregister 2

16Sign

extend

Writedata

ReaddataData

memory

1

ALUresult

Mux

ALUZero

ID/EX MEM/WB

Write backlw

Writeregister

Address

Page 14: Pipeline - Unicamppannain/mc542/aulas/ch6_arq.pdf · 2006. 8. 2. · – Problemas devido à execução de instruções de desvio • Ex.: Quando umbranch é tomado, como tratar a(s)

14

Ch6.a-27? 1998 Morgan Kaufmann PublishersPaulo C. Centoducatte

Pipelined Datapath

Instructionmemory

Address

4

32

0

Add Addresult

Shiftleft 2

Inst

ruct

ion

IF/ID EX/MEM

Mux

0

1

Add

PC

0Writedata

Mux

1Registers

Readdata 1

Readdata 2

Readregister 1

Readregister 2

16Sign

extend

Writeregister

Writedata

Readdata

Datamemory

1

ALUresult

Mux

ALUZero

ID/EX MEM/WB

Execution

sw

Address

Ch6.a-28? 1998 Morgan Kaufmann PublishersPaulo C. Centoducatte

Pipelined Datapath

Instructionmemory

Address

4

32

0

Add Addresult

Shiftleft 2

Inst

ruct

ion

IF/ID EX/MEM

Mux

0

1

Add

PC

0Writedata

Mux

1Registers

Readdata 1

Readdata 2

Readregister 1

Readregister 2

16Sign

extend

Writeregister

Writedata

Readdata

Datamemory

1

ALUresult

Mux

ALUZero

ID/EX MEM/WB

Memory

sw

Address

Page 15: Pipeline - Unicamppannain/mc542/aulas/ch6_arq.pdf · 2006. 8. 2. · – Problemas devido à execução de instruções de desvio • Ex.: Quando umbranch é tomado, como tratar a(s)

15

Ch6.a-29? 1998 Morgan Kaufmann PublishersPaulo C. Centoducatte

Pipelined Datapath

Instructionmemory

Address

4

32

0

Add Addresult

Shiftleft 2

Inst

ruct

ion

IF/ID EX/MEM

Mux

0

1

Add

PC

0

Address

Writedata

Mux

1Registers

Readdata 1

Readdata 2

Readregister 1

Readregister 2

16Sign

extend

Writeregister

Writedata

Readdata

Datamemory

1

ALUresult

Mux

ALUZero

ID/EX MEM/WB

Write back

sw

Ch6.a-30? 1998 Morgan Kaufmann PublishersPaulo C. Centoducatte

Datapath Correto

Instructionmemory

Address

4

32

0

Add Addresult

Shiftleft 2

Inst

ruct

ion

IF/ID EX/MEM MEM/WB

Mux

0

1

Add

PC

0

Address

Writedata

Mux

1Registers

Readdata 1

Readdata 2

Readregister 1

Readregister 2

16Sign

extend

Writeregister

Writedata

Readdata

Datamemory

1

ALUresult

Mux

ALUZero

ID/EX

Problema na execução da instrução load.Qual é o erro?

Page 16: Pipeline - Unicamppannain/mc542/aulas/ch6_arq.pdf · 2006. 8. 2. · – Problemas devido à execução de instruções de desvio • Ex.: Quando umbranch é tomado, como tratar a(s)

16

Ch6.a-31? 1998 Morgan Kaufmann PublishersPaulo C. Centoducatte

Datapath com os estágios usados para um instrução lw

Instructionmemory

Address

4

32

0

Add Addresult

Shiftleft 2

Inst

ruct

ion

IF/ID EX/MEM MEM/WB

Mux

0

1

Add

PC

0Writedata

Mux

1Registers

Readdata 1

Readdata 2

Readregister 1

Readregister 2

16Sign

extend

Writeregister

Writedata

Readdata

1

ALUresult

Mux

ALUZero

ID/EX

Address

Datamemory

Ch6.a-32? 1998 Morgan Kaufmann PublishersPaulo C. Centoducatte

Representações Gráfica do Pipeline

IM R eg D M R e g

IM R e g D M R eg

C C 1 C C 2 C C 3 C C 4 C C 5 C C 6

T im e (in c lo c k c y cles )

lw $1 0 , 2 0 ( $1 )

P rog ra me x e c u t io no rd e r( in in s tr uc tio n s )

s ub $ 11 , $ 2 , $3

A L U

A L U

Ajuda a responder perguntas como:Quantos ciclos são gasto para executar este código?O que a ALU está fazendo durante o ciclo 10?

Ajuda a entender os datapaths

Page 17: Pipeline - Unicamppannain/mc542/aulas/ch6_arq.pdf · 2006. 8. 2. · – Problemas devido à execução de instruções de desvio • Ex.: Quando umbranch é tomado, como tratar a(s)

17

Ch6.a-33? 1998 Morgan Kaufmann PublishersPaulo C. Centoducatte

Representações Gráfica do Pipeline

Programexecutionorder(in instructions)

Time ( in clock cycles)

CC 1 CC 2 CC 3 CC 4 CC 5 CC 6

Instructionfetch

Instructiondecode

Instructionfetch

Instructiondecode Execution Write back

Execution

Dataaccess

Dataaccess Write backlw $10, $20($1)

sub $11, $2, $3

Ch6.a-34? 1998 Morgan Kaufmann PublishersPaulo C. Centoducatte

Instructionmemory

Address

4

32

0

Add Addresult

Shiftleft 2

Inst

ruct

ion

IF/ID EX/MEM MEM/WB

Mux

0

1

Add

PC

0Writedata

Mux

1Registers

Readdata 1

Readdata 2

Readregister 1

Readregister 2

16Sign

extend

Writeregister

Writedata

Readdata

1

ALUresult

Mux

ALUZero

ID/EX

Instruction fetch

lw $10, 20($1)

Address

Datamemory

Clock 1

Page 18: Pipeline - Unicamppannain/mc542/aulas/ch6_arq.pdf · 2006. 8. 2. · – Problemas devido à execução de instruções de desvio • Ex.: Quando umbranch é tomado, como tratar a(s)

18

Ch6.a-35? 1998 Morgan Kaufmann PublishersPaulo C. Centoducatte

Instructionmemory

Address

4

32

0

Add Addresult

Shiftleft 2

Inst

ruct

ion

IF/ID EX/MEM MEM/WB

Mux

0

1

Add

PC

0Writedata

Mux

1Registers

Readdata 1

Readdata 2

Readregister 1

Readregister 2

16Sign

extend

Writeregister

Writedata

Readdata

1

ALUresult

Mux

ALUZero

ID/EX

Instruction decode

lw $10, 20($1)

Instruction fetch

sub $11, $2, $3

Address

Datamemory

Clock 2

Ch6.a-36? 1998 Morgan Kaufmann PublishersPaulo C. Centoducatte

Instructionmemory

Address

4

0

Add Addresult

Shiftleft 2

Inst

ruct

ion

IF/ID EX/MEM MEM/WB

Mux

0

1

Add

PC

0Writedata

Mux

1Registers

Readdata 1

Readdata 2

Readregister 1

Readregister 2

Writeregister

Writedata

Readdata

1

ALUresult

Mux

ALUZero

ID/EX

Execution

lw $10, 20($1)

Instruction decode

sub $11, $2, $3

3216Sign

extend

Address

Datamemory

Clock 3

Page 19: Pipeline - Unicamppannain/mc542/aulas/ch6_arq.pdf · 2006. 8. 2. · – Problemas devido à execução de instruções de desvio • Ex.: Quando umbranch é tomado, como tratar a(s)

19

Ch6.a-37? 1998 Morgan Kaufmann PublishersPaulo C. Centoducatte

Instructionmemory

Address

4

0

Add Addresult

Shiftleft 2

Inst

ruct

ion

IF/ID EX/MEM MEM/WB

Mux

0

1

Add

PC

0Writedata

Mux

1Registers

Readdata 1

Readdata 2

Readregister 1

Readregister 2

3216Sign

extend

Writeregister

Writedata

Memory

lw $10, 20($1)

Readdata

1

ALUresult

Mux

ALUZero

ID/EX

Execution

sub $11, $2, $3

Datamemory

Address

Clock 4

Ch6.a-38? 1998 Morgan Kaufmann PublishersPaulo C. Centoducatte

Instructionmemory

Address

4

32

0

Add Addresult

1

ALUresult

Zero

Shiftleft 2

Inst

ruct

ion

IF/ID EX/MEMID/EX MEM/WB

Write backMux

0

1

Add

PC

0Writedata

Mux

1Registers

Readdata 1

Readdata 2

Readregister 1

Readregister 2

16Sign

extend

Mux

ALUReaddata

Writeregister

Writedata

lw $10, 20($1)

Memory

sub $11, $2, $3

Address

Datamemory

Clock 5

Page 20: Pipeline - Unicamppannain/mc542/aulas/ch6_arq.pdf · 2006. 8. 2. · – Problemas devido à execução de instruções de desvio • Ex.: Quando umbranch é tomado, como tratar a(s)

20

Ch6.a-39? 1998 Morgan Kaufmann PublishersPaulo C. Centoducatte

Instructionmemory

Address

4

32

0

Add Addresult

1

ALUresult

Zero

Shiftleft 2

Inst

ruct

ion

IF/ID EX/MEMID/EX MEM/WB

Write backMux

0

1

Add

PC

0Writedata

Mux

1Registers

Readdata 1

Readdata 2

Readregister 1

Readregister 2

16Sign

extend

Mux

ALUReaddata

Writeregister

Writedata

sub $11, $2, $3

Address

Datamemory

Clock 6

Ch6.a-40? 1998 Morgan Kaufmann PublishersPaulo C. Centoducatte

Controle do Pipeline

PC

Instructionmemory

Address

Inst

ruct

ion

Instruction[20– 16]

MemtoReg

ALUOp

Branch

RegDst

ALUSrc

4

16 32Instruction[15– 0]

0

0Registers

Writeregister

Writedata

Readdata 1

Readdata 2

Readregister 1

Readregister 2

Signextend

Mux

1Write

data

Read

data Mux

1

ALUcontrol

RegWrite

MemRead

Instruction[15– 11]

6

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

MemWrite

Address

Datamemory

PCSrc

Zero

Add Addresult

Shiftleft 2

ALUresult

ALUZero

Add

0

1

Mux

0

1

Mux

Page 21: Pipeline - Unicamppannain/mc542/aulas/ch6_arq.pdf · 2006. 8. 2. · – Problemas devido à execução de instruções de desvio • Ex.: Quando umbranch é tomado, como tratar a(s)

21

Ch6.a-41? 1998 Morgan Kaufmann PublishersPaulo C. Centoducatte

Controle do Pipeline

• 5 estágios. O que deve ser controlado em cada estágio?

– 10: Fetch da instrução e incremento do PC– 20: Decodificação da instrução e Fetch dos

registradores– 30: Execução– 40: Acesso à memória– 50: Write back

Ch6.a-42? 1998 Morgan Kaufmann PublishersPaulo C. Centoducatte

Sinais de controle

Page 22: Pipeline - Unicamppannain/mc542/aulas/ch6_arq.pdf · 2006. 8. 2. · – Problemas devido à execução de instruções de desvio • Ex.: Quando umbranch é tomado, como tratar a(s)

22

Ch6.a-43? 1998 Morgan Kaufmann PublishersPaulo C. Centoducatte

Sinais de controle

Ch6.a-44? 1998 Morgan Kaufmann PublishersPaulo C. Centoducatte

Sinais de controle

Execution/Address Calculation stage

control linesMemory access stage

control lines

Write-back stage control

lines

InstructionReg Dst

ALU Op1

ALU Op0

ALU Src Branch

Mem Read

Mem Write

Reg write

Mem to Reg

R-format 1 1 0 0 0 0 0 1 0lw 0 0 0 1 0 1 0 1 1sw X 0 0 1 0 0 1 0 Xbeq X 0 1 0 1 0 0 0 X

Page 23: Pipeline - Unicamppannain/mc542/aulas/ch6_arq.pdf · 2006. 8. 2. · – Problemas devido à execução de instruções de desvio • Ex.: Quando umbranch é tomado, como tratar a(s)

23

Ch6.a-45? 1998 Morgan Kaufmann PublishersPaulo C. Centoducatte

Control

EX

M

WB

M

WB

WB

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

Instruction

Ch6.a-46? 1998 Morgan Kaufmann PublishersPaulo C. Centoducatte

Datapath com Controle

PC

Instruc tionmemory

Inst

ruct

i on

Add

Instruction[20– 16]

Mem

t oR

e g

ALU Op

Branch

RegD st

ALUSrc

4

16 32Instruction[15– 0]

0

0

Mux

0

1

Add Addresult

R egistersW riteregister

W ritedata

Readdata 1

Readdata 2

Readregister 1

Readregister 2

Signextend

Mux

1

ALUresult

Zero

Writedata

Readdata

Mux

1

ALUcontrol

Shiftleft 2

Reg

Wr it

e

M emRead

Control

ALU

Instruction[15– 11]

6

EX

M

W B

M

WB

WBIF/ID

PCSrc

ID/EX

EX/MEM

MEM/WB

Mux

0

1

Mem

Wri t

e

AddressData

memory

Address

Page 24: Pipeline - Unicamppannain/mc542/aulas/ch6_arq.pdf · 2006. 8. 2. · – Problemas devido à execução de instruções de desvio • Ex.: Quando umbranch é tomado, como tratar a(s)

24

Ch6.a-47? 1998 Morgan Kaufmann PublishersPaulo C. Centoducatte

lw $10, 20 ($1)sub $11, $2, $3and $12, $4, $5or $13, $6, $7add $14, $8, $9

Instructionmemory

Instruction[20–16]

Mem

toR

eg

ALUOp

Branch

RegDst

ALUSrc

4

Instruction[15– 0]

0

Mux

0

1

Add Addresult

RegistersWriteregister

Writedata

Readdata 1

Readdata 2

Readregister 1

Readregister 2

Signextend

Mux

1

ALUresult

Zero

ALUcontrol

Shiftleft 2

Reg

Writ

e

MemRead

Control

ALU

Instruction[15–11]

EX

M

WB

M

WB

WBIn

stru

ctio

n

IF/ID EX/MEMID/EX

ID: before<1> EX: before<2> MEM: before<3> WB: before<4>

MEM/WB

IF: lw $10, 20($1)

000

00

0000

000

00

000

0

00

00

0

00

Mux

0

1

Add

PC

0

Datamemory

Address

Writedata

Readdata

Mux

1

Mem

Writ

e

Address

Clock 1

1o ciclo

Ch6.a-48? 1998 Morgan Kaufmann PublishersPaulo C. Centoducatte

lw $10, 20 ($1)sub $11, $2, $3and $12, $4, $5or $13, $6, $7add $14, $8, $9

WB

EX

M

Instructionmemory

Mem

toR

eg

ALUOp

Branch

RegDst

ALUSrc

4

0

Mux

0

1

Add Addresult

Writeregister

Writedata

Mux

1

ALUresult

Zero

ALUcontrol

Shiftleft 2

Reg

Writ

e

ALU

M

WB

WB

Inst

ruct

ion

IF/ID EX/MEMID/EX

ID: lw $10, 20($1) EX: before<1> MEM: before<2> WB: before<3>

MEM/WB

IF: sub $11, $2, $3

010

11

0001

000

00

000

0

00

00

0

00

Mux

0

1

Add

PC

0Writedata

Readdata

Mux

1

lwControl

Registers

Readdata 1

Readdata 2

Readregister 1

Readregister 2

X

10

20

X

1

Instruction[20– 16]

Instruction[15– 0] Sign

extend

Instruction[15– 11]

20

$X

$1

10

X

MemRead

Mem

Writ

e

Datamemory

Address

Address

Clock 2

2o ciclo

Page 25: Pipeline - Unicamppannain/mc542/aulas/ch6_arq.pdf · 2006. 8. 2. · – Problemas devido à execução de instruções de desvio • Ex.: Quando umbranch é tomado, como tratar a(s)

25

Ch6.a-49? 1998 Morgan Kaufmann PublishersPaulo C. Centoducatte

Instructionmemory

Address

Instruction[20– 16]

Mem

toR

eg

Branch

ALUSrc

4

Instruction[15– 0]

0

1

Add Addresult

RegistersWriteregister

Writedata

Readdata 1

Readdata 2

Readregister 1

Readregister 2

ALUresult

Shiftleft 2

Reg

Writ

e

MemRead

Control

ALU

Instruction[15– 11]

EX

M

WB

WB

Inst

ruct

ion

IF/ID EX/MEMID/EX

ID: sub $11, $2, $3 EX: lw $10, . . . MEM: before<1> WB: before<2>

MEM/WB

IF: and $12, $4, $5

000

10

1100

010

11

000

1

00

00

0

00

Mux

0

1

Add

PC

0Writedata

Readdata

Mux

1

Mem

Writ

e

sub

11

X

X

3

2

X

$3

$2

X

11

$1

20

10

Mux

0

Mux

1

ALUOp

RegDst

ALUcontrol

M

WB

Zero

Signextend

Datamemory

Address

Clock 3

lw $10, 20 ($1)sub $11, $2, $3and $12, $4, $5or $13, $6, $7add $14, $8, $9

3o ciclo

Ch6.a-50? 1998 Morgan Kaufmann PublishersPaulo C. Centoducatte

WB

EX

M

Instructionmemory

Address

Mem

toR

eg

ALUOp

Branch

RegDst

ALUSrc

4

0

0

1

Add Addresult

Writeregister

Writedata 1

ALUresult

ALUcontrol

Shiftleft 2

Reg

Writ

e

M

WB

Inst

ruct

ion

IF/ID EX/MEMID/EX

ID: and $12, $2, $3 EX: sub $11, . . . MEM: lw $10, . . . WB: before<1>

MEM/WB

IF: or $13, $6, $7

000

10

1100

000

10

101

0

11

10

0

00

Mux

0

1

Add

PC

0Writedata

Mux

1

andControl

Registers

Readdata 1

Readdata 2

Readregister 1

Readregister 2

12

X

X

5

4

Instruction[20– 16]

Instruction[15– 0]

Instruction[15– 11]

X

$5

$4

X

12

MemRead

Mem

Writ

e

$3

$2

11

Mux

Mux

ALUAddress Read

dataData

memory

10

WB

Zero

Signextend

Clock 4

lw $10, 20 ($1)sub $11, $2, $3and $12, $4, $5or $13, $6, $7add $14, $8, $9

4o ciclo

Page 26: Pipeline - Unicamppannain/mc542/aulas/ch6_arq.pdf · 2006. 8. 2. · – Problemas devido à execução de instruções de desvio • Ex.: Quando umbranch é tomado, como tratar a(s)

26

Ch6.a-51? 1998 Morgan Kaufmann PublishersPaulo C. Centoducatte

Instructionmemory

Address

Instruction[20– 16]

Branch

ALUSrc

4

Instruction[15– 0]

0

1

Add Addresult

RegistersWriteregister

Writedata

Readdata 1

Readdata 2

Readregister 1

Readregister 2

ALUresult

Shiftleft 2

Reg

Writ

e

MemRead

Control

ALU

Instruction[15– 11]

EX

M

WB

Inst

ruct

ion

IF/ID EX/MEMID/EX

ID: or $13, $6, $7 EX: and $12, . . . MEM: sub $11, . . . WB: lw $10, . . .

MEM/WB

IF: add $14, $8, $9

000

10

1100

000

10

101

0

10

00

0

Mux

0

1

Add

PC

0Writedata

Readdata

Mux

1

Mem

Writ

e

or

13

X

X

7

6

X

$7

$6

X

13

$4

Mux

0

Mux

1

ALUOp

RegDst

ALUcontrol

M

WB

11 10

10$5

12

WB

Mem

toR

eg

11

Zero

Datamemory

Address

Signextend

Clock 5

lw $10, 20 ($1)sub $11, $2, $3and $12, $4, $5or $13, $6, $7add $14, $8, $9

5o ciclo

Ch6.a-52? 1998 Morgan Kaufmann PublishersPaulo C. Centoducatte

WB

EX

M

Instructionmemory

Address

Mem

toR

eg

ALUOp

Branch

RegDst

ALUSrc

4

0

0

1

Add Addresult

1

ALUresult

ALUcontrol

Shiftleft 2

Reg

Writ

e

M

WB

Inst

ruct

ion

IF/ID EX/MEMID/EX

ID: add $14, $8, $9 EX: or $13, . . . MEM: and $12, . . . WB: sub $11, . . .

MEM/WB

IF: after<1>

000

10

1100

000

10

101

0

10

00

0

10

Mux

0

1

Add

PC

0Writedata

Mux

1

addControl

Registers

Readdata 1

Readdata 2

Readregister 1

Readregister 2

14

X

X

9

8

Instruction[20– 16]

Instruction[15– 0]

Instruction[15– 11]

X

$9

$8

X

14

MemRead

Mem

Writ

e

$7

$6

13

Mux

Mux

ALUReaddata

12

WB

11

11

Writeregister

Writedata

Zero

Datamemory

Address

Signextend

Clock 6

lw $10, 20 ($1)sub $11, $2, $3and $12, $4, $5or $13, $6, $7add $14, $8, $9

6o ciclo

Page 27: Pipeline - Unicamppannain/mc542/aulas/ch6_arq.pdf · 2006. 8. 2. · – Problemas devido à execução de instruções de desvio • Ex.: Quando umbranch é tomado, como tratar a(s)

27

Ch6.a-53? 1998 Morgan Kaufmann PublishersPaulo C. Centoducatte

Instructionmemory

Address

Instruction[20–16]

Branch

ALUSrc

4

Instruction[15–0]

0

1

Add Addresult

RegistersWriteregister

Writedata

ALUresult

Shiftleft 2

Reg

Writ

e

MemRead

Control

ALU

Instruction[15–11]

Signextend

EX

M

WB

Inst

ruct

ion

IF/ID EX/MEMID/EX

ID: after<1> EX: add $14, . . . MEM: or $13, . . . WB: and $12, . . .

MEM/WB

IF: after<2>

000

00

0000

000

10

101

0

10

00

0

Mux

0

1

Add

PC

0Writedata

Readdata

Mux

1

ID: after<2> EX: after<1> MEM: add $14, . . . WB: or $13, . . .IF: after<3>M

emW

rite

$8

Mux

0

Mux

1

ALUOp

RegDst

ALUcontrol

M

WB

13 12

12$9

14

WB

Mem

toR

eg

10

Readdata 1

Readdata 2

Readregister 1

Readregister 2 Zero

Datamemory

Address

Clock 7

lw $10, 20 ($1)sub $11, $2, $3and $12, $4, $5or $13, $6, $7add $14, $8, $9

7o ciclo

Ch6.a-54? 1998 Morgan Kaufmann PublishersPaulo C. Centoducatte

WB

EX

M

Instructionmemory

Address

Mem

toR

eg

ALUOp

Branch

RegDst

ALUSrc

4

0

0

1

Add Addresult

1

ALUresult

Zero

ALUcontrol

Shiftleft 2

Reg

Writ

e

M

WB

Inst

ruct

ion

IF/ID EX/MEMID/EX

ID: after<2> EX: after<1> MEM: add $14, . . . WB: or $13, . . .

MEM/WB

IF: after<3>

000

00

0000

000

00

000

0

10

00

0

10

Mux

0

1

Add

PC

0Writedata

Mux

1

Control

Registers

Readdata 1

Readdata 2

Readregister 1

Readregister 2

Instruction[20–16]

Instruction[15–0] Sign

extend

Instruction[15–11]

MemRead

Mem

Writ

e

Mux

Mux

ALUReaddata

14

WB

13

13

Writeregister

Writedata

Datamemory

Address

Clock 8

lw $10, 20 ($1)sub $11, $2, $3and $12, $4, $5or $13, $6, $7add $14, $8, $9

8o ciclo

Page 28: Pipeline - Unicamppannain/mc542/aulas/ch6_arq.pdf · 2006. 8. 2. · – Problemas devido à execução de instruções de desvio • Ex.: Quando umbranch é tomado, como tratar a(s)

28

Ch6.a-55? 1998 Morgan Kaufmann PublishersPaulo C. Centoducatte

WB

EX

M

Instructionmemory

Address

Mem

toR

eg

ALUOp

Branch

RegDst

ALUSrc

4

0

0

1

Add Addresult

1

ALUresult

Zero

ALUcontrol

Shiftleft 2

Reg

Writ

e

M

WB

Inst

ruct

ion

IF/ID EX/MEMID/EX

ID: after<3> EX: after<2> MEM: after<1> WB: add $14, . . .

MEM/WB

IF: after<4>

000

00

0000

000

00

000

0

00

00

0

10

Mux

0

1

Add

PC

0Writedata

Mux

1

Control

Registers

Readdata 1

Readdata 2

Readregister 1

Readregister 2

Instruction[20– 16]

Instruction[15– 0] Sign

extend

Instruction[15– 11]

MemRead

Mem

Writ

e

Mux

Mux

ALUReaddata

WB

14

14

Writeregister

Writedata

Datamemory

Address

Clock 9

lw $10, 20 ($1)sub $11, $2, $3and $12, $4, $5or $13, $6, $7add $14, $8, $9

9o ciclo

Ch6.a-56? 1998 Morgan Kaufmann PublishersPaulo C. Centoducatte

Dependências

• Problema com iniciar a execução de uma instrução antes do término da anterior

– Exemplo: Instruções com dependências

sub $2, $1, $3 # reg $2 modificadoand $12, $2, $5 # valor (1º operando) de $2

# depende do subor $13, $6, $2 # idem (2º operando)add $14, $2, $2 # idem (1º e 2º operando)sw $15, 100($2) # idem (base do endereçamento)

Page 29: Pipeline - Unicamppannain/mc542/aulas/ch6_arq.pdf · 2006. 8. 2. · – Problemas devido à execução de instruções de desvio • Ex.: Quando umbranch é tomado, como tratar a(s)

29

Ch6.a-57? 1998 Morgan Kaufmann PublishersPaulo C. Centoducatte

IM Reg

IM Reg

CC 1 CC 2 CC 3 CC 4 CC 5 CC 6Time (in clock cycles)

sub $2, $1, $3

Programexecutionorder(in instructions)

and $12, $2, $5

IM Reg DM Reg

IM DM Reg

IM DM Reg

CC 7 CC 8 CC 910 10 10 10 10/–20 –20 –20 –20 –20

or $13, $6, $2

add $14, $2, $2

sw $15, 100($2)

Value ofregister $2:

DM Reg

Reg

Reg

Reg

DM

Se a escrita no banco de registradores é feita no 1o semi-cicloe a leitura no 2o, em CC5 não há hazard.

CC4 CC5 CC6

Reg WR Reg RD

Ch6.a-58? 1998 Morgan Kaufmann PublishersPaulo C. Centoducatte

Solução por Software

• Compilador garante um código sem Hazard inserindo nops– Onde inserir os nops?

sub $2, $1, $3and $12, $2, $5or $13, $6, $2add $14, $2, $2sw $15, 100($2)

sub $2, $1, $3nopnopand $12, $2, $5or $13, $6, $2add $14, $2, $2sw $15, 100($2)

Problema: reduz o desempenho

Page 30: Pipeline - Unicamppannain/mc542/aulas/ch6_arq.pdf · 2006. 8. 2. · – Problemas devido à execução de instruções de desvio • Ex.: Quando umbranch é tomado, como tratar a(s)

30

Ch6.a-59? 1998 Morgan Kaufmann PublishersPaulo C. Centoducatte

Solução por Hardware

• Usar o resultado assim que calculado, não esperar que ele seja escrito.

• Neste caso é necessário mecanismo para detectar o hazard. Qual a condição para que haja hazard?

Quando uma instrução tenta ler um registrador (estágio EX) e esse registrador será escrito poruma instrução anterior no estágio WB

Ch6.a-60? 1998 Morgan Kaufmann PublishersPaulo C. Centoducatte

Forwarding

O que ocorre se $13 no lugar de $2

IM Reg

IM Reg

CC 1 CC 2 CC 3 CC 4 CC 5 CC 6

Time (in clock cycles)

sub $2, $1, $3

Programexecution order(in instructions)

and $12, $2, $5

IM Reg DM Reg

IM DM Reg

IM DM Reg

CC 7 CC 8 CC 9

10 10 10 10 10/– 20 – 20 – 20 – 20 – 20

or $13, $6, $2

add $14, $2, $2

sw $15, 100($2)

Value of register $2 :

DM Reg

Reg

Reg

Reg

X X X – 20 X X X X XValue of EX/MEM :X X X X – 20 X X X XValue of MEM/W B :

DM

Page 31: Pipeline - Unicamppannain/mc542/aulas/ch6_arq.pdf · 2006. 8. 2. · – Problemas devido à execução de instruções de desvio • Ex.: Quando umbranch é tomado, como tratar a(s)

31

Ch6.a-61? 1998 Morgan Kaufmann PublishersPaulo C. Centoducatte

Condições que determinam Hazards de dados

• EX/MEM

– EX/MEM.RegisterRd = ID/EX.RegisterRs

– EX/MEM.RegisterRd = ID/EX.RegisterRt

• MEM/WB

– MEM/WB.RegisterRd = ID/EX.RegisterRs

– MEM/WB.RegisterRd = ID/EX.RegisterRt

Ch6.a-62? 1998 Morgan Kaufmann PublishersPaulo C. Centoducatte

Condições que determinam Hazard de dados

• Exemplo:

– sub–and: EX/MEM.RegisterRd = ID/EX.RegisterRs = $2

– sub-or: MEM/WB.RegisterRd = ID/EX.RegisterRt = $2

– sub-add: não tem hazard– sub-sw: não tem hazard

sub $2, $1, $3 # reg $2 modificadoand $12, $2, $5 # valor de $2 depende do subor $13, $6, $2 # idem (2º operando)add $14,$2, $2 # idem (1º e 2º operandos)sw $15, 100($2) # idem (base do endereçamento)

Page 32: Pipeline - Unicamppannain/mc542/aulas/ch6_arq.pdf · 2006. 8. 2. · – Problemas devido à execução de instruções de desvio • Ex.: Quando umbranch é tomado, como tratar a(s)

32

Ch6.a-63? 1998 Morgan Kaufmann PublishersPaulo C. Centoducatte

Condições que determinam Hazard de dados

• Esta não é uma política precisa, pois existem instruções que não escrevem no register file.– solução seria verificar o sinal RegWrite

? MIPS usa $0 para operandos de valor 0. Para instruções onde $0 é destino?? sll $0, $1, 2

? valor diferente de zero nos regs de pipeline

? add $3, $0, $2? se houver fowarding, $3 terá valor errado no fim da

instrução add

? Para isto temos que incluir as condições ? EX/MEM.registerRd <> 0 (1º hazard) ? MEM/WB.registerRd <> 0 (2º hazard)

Ch6.a-64? 1998 Morgan Kaufmann PublishersPaulo C. Centoducatte

Datapath sem Forwarding

Mux

ALU

ID/EX MEM/WB

Datamemory

EX/MEM

a. No forwarding

Registers

Page 33: Pipeline - Unicamppannain/mc542/aulas/ch6_arq.pdf · 2006. 8. 2. · – Problemas devido à execução de instruções de desvio • Ex.: Quando umbranch é tomado, como tratar a(s)

33

Ch6.a-65? 1998 Morgan Kaufmann PublishersPaulo C. Centoducatte

Datapath com Forwarding ( para add, sub, and e or )

Registers

Mux M

ux

ALU

ID/EX MEM/WB

Datamemory

Mux

Forwardingunit

EX/MEM

b. With forwarding

ForwardB

RdEX/MEM.RegisterRd

MEM/WB.RegisterRd

RtRtRs

ForwardA

Mux

Ch6.a-66? 1998 Morgan Kaufmann PublishersPaulo C. Centoducatte

Valores dos sinais ForwardA e ForwardB

Mux Control Source ExplanaçãoForwardA = 00 ID/EX Primeiro operando da ULA ?

register fileForwardA = 10 EX/MEM Primeiro operando da ULA ?

resultado anteiror da ULAForwardA = 01 MEM/WB Primeiro operando da ULA é

antecipado da memória de dadosou um resultado anterior da ULA

ForwardB = 00 ID/EX Segundo operando da ULA ?register file

ForwardB = 10 EX/MEM Segundo operando da ULA ?resultado anteiror da ULA

ForwardB = 01 MEM/WB Segundo operando da ULA éantecipado da memória de dadosou um resultado anterior da ULA

Page 34: Pipeline - Unicamppannain/mc542/aulas/ch6_arq.pdf · 2006. 8. 2. · – Problemas devido à execução de instruções de desvio • Ex.: Quando umbranch é tomado, como tratar a(s)

34

Ch6.a-67? 1998 Morgan Kaufmann PublishersPaulo C. Centoducatte

Detecção e Controle de hazard

? O controle de fowarding será no estágio EX, pois é neste estágio que se encontram os multiplexadores de fowarding da ULA. Portanto devemos passar o númerodo registrador operando do estágio ID via registrador depipeline ID/EX ? adicionar campo rs (bits 25-21).

Ch6.a-68? 1998 Morgan Kaufmann PublishersPaulo C. Centoducatte

Condições para detecção de hazardEX hazard

if (EX/MEM.RegWrite and (EX/MEM.RegisterRd <> 0 )and (EX/MEM.RegisterRd = ID/EX.RegisterRs))

FowardA = 10if (EX/MEM.RegWrite and (EX/MEM.RegisterRd <> 0 )

and (EX/MEM.RegisterRd = ID/EX.RegisterRt)) FowardB = 10

MEM hazardif (MEM/WB.RegWrite and (MEM/WB.RegisterRd <> 0 )

and (MEM/WB.RegisterRd = ID/EX.RegisterRs)) FowardA = 01

if (MEM/WB.RegWrite and (MEM/WB.RegisterRd <> 0 ) and (MEM/WB.RegisterRd = ID/EX.RegisterRt))

FowardB = 01

Page 35: Pipeline - Unicamppannain/mc542/aulas/ch6_arq.pdf · 2006. 8. 2. · – Problemas devido à execução de instruções de desvio • Ex.: Quando umbranch é tomado, como tratar a(s)

35

Ch6.a-69? 1998 Morgan Kaufmann PublishersPaulo C. Centoducatte

Observações:

• Não existe data hazard no estágio WB, pois estamos assumindo que o register file supre o resultado correto se a instrução no estágio ID é o mesmo registrador escrito pela instrução no estágio WB ? forwarding no registerfile

– Na 1a edição do livro P&H tem hazard

Ch6.a-70? 1998 Morgan Kaufmann PublishersPaulo C. Centoducatte

Observações:

• Um data hazard potencial pode ocorrer entre o resultado de uma instrução em WB, o resultado de uma instrução em MEM e o operando fonte da instrução no estágio da ULA

• Neste caso o resultado é antecipado do estágio MEM porque o resultado neste estágio é mais recente

add $1, $1, $2add $1, $1, $3add $1, $1, $4

if (MEM/WB.RegWrite and (MEM/WB.RegisterRd <> 0 ) and (EX/MEM.RegisterRd <> ID/EX.registerRs)and (MEM/WB.RegisterRd = ID/EX.RegisterRs)) FowardA = 01

if (MEM/WB.RegWrite and (MEM/WB.RegisterRd <> 0 ) and (EX/MEM.RegisterRd <> ID/EX.registerRt)and (MEM/WB.RegisterRd = ID/EX.RegisterRt)) FowardB = 01

Page 36: Pipeline - Unicamppannain/mc542/aulas/ch6_arq.pdf · 2006. 8. 2. · – Problemas devido à execução de instruções de desvio • Ex.: Quando umbranch é tomado, como tratar a(s)

36

Ch6.a-71? 1998 Morgan Kaufmann PublishersPaulo C. Centoducatte

Datapath modificado para fowarding

PCInstruction

memory

Registers

Mux

Mux

Control

ALU

EX

M

WB

M

WB

WB

ID/EX

EX/MEM

MEM/WB

Datamemory

Mux

Forwardingunit

IF/ID

Ins t

ruct

ion

Mux

RdEX/MEM.RegisterRd

MEM/WB.RegisterRd

Rt

Rt

Rs

IF/ID.RegisterRd

IF/ID.RegisterRt

IF/ID.RegisterRt

IF/ID.RegisterRs

Ch6.a-72? 1998 Morgan Kaufmann PublishersPaulo C. Centoducatte

Forwarding

PC Instructionmemory

Registers

Mux

Mux

Mux

EX

M

WB

WB

Datamemory

Mux

Forwardingunit

Inst

ruct

ion

IF/ID

and $4, $2, $5 sub $2, $1, $3

ID/EX

before<1>

EX/MEM

before<2>

MEM/WB

or $4, $4, $2

Clock 3

2

5

10 10

$2

$5

5

2

4

$1

$3

3

1

2

Control

ALU

M

WB

sub $2, $1, $3and $4, $2, $5or $4, $4, $2add $9, $4, $2

Page 37: Pipeline - Unicamppannain/mc542/aulas/ch6_arq.pdf · 2006. 8. 2. · – Problemas devido à execução de instruções de desvio • Ex.: Quando umbranch é tomado, como tratar a(s)

37

Ch6.a-73? 1998 Morgan Kaufmann PublishersPaulo C. Centoducatte

Forwarding

PC Instructionmemory

Registers

Mux

Mux

Mux

EX

M

WB

M

WB

Datamemory

Mux

Forwardingunit

Inst

ruct

ion

IF/ID

or $4, $4, $2 and $4, $2, $5

ID/EX

sub $2, . . .

EX/MEM

before<1>

MEM/WB

add $9, $4, $2

Clock 4

4

6

10 10

$4

$2

6

2

4

$2

$5

5

2

4

Control

ALU

10

2

WB

sub $2, $1, $3and $4, $2, $5or $4, $4, $2add $9, $4, $2

Ch6.a-74? 1998 Morgan Kaufmann PublishersPaulo C. Centoducatte

Forwarding

PC Instructionmemory

Registers

Mux

Mux

Mux

EX

M

WB

M

WB

Datamemory

Mux

Forwardingunit

Inst

ruct

ion

IF/ID

add $9, $4, $2 or $4, $4, $2

ID/EX

and $4, . . .

EX/MEM

sub $2, . . .

MEM/WB

after<1>

Clock 5

4

2

10 10

$4

$2

2

4

9

$4

$2

4

2

24

Control

ALU

10

WB

2

1

4

sub $2, $1, $3and $4, $2, $5or $4, $4, $2add $9, $4, $2

Page 38: Pipeline - Unicamppannain/mc542/aulas/ch6_arq.pdf · 2006. 8. 2. · – Problemas devido à execução de instruções de desvio • Ex.: Quando umbranch é tomado, como tratar a(s)

38

Ch6.a-75? 1998 Morgan Kaufmann PublishersPaulo C. Centoducatte

Forwarding

PC Instructionmemory

Mux

Mux

Mux

EX

M

WB

M

WB

Datamemory

Mux

Forwardingunit

after<1>after<2> add $9, $4, $2 or $4, . . .

EX/MEM

and $4, . . .

MEM/WB

Clock 6

10

$4

$2

2

4

9

ALU

10

4

WB

4

1

Registers

Inst

ruct

ion

IF/ID

ID/EX

4

Control

sub $2, $1, $3and $4, $2, $5or $4, $4, $2add $9, $4, $2

Ch6.a-76? 1998 Morgan Kaufmann PublishersPaulo C. Centoducatte

Forwarding: Datapath para entrada signed-imediatenecessária para lw e sw

ALUSrcRegisters

Mux

Mux

Mux

ALU

ID/EX MEM/WB

Datamemory

Mux

Forwardingunit

EX/MEM

Mux

Page 39: Pipeline - Unicamppannain/mc542/aulas/ch6_arq.pdf · 2006. 8. 2. · – Problemas devido à execução de instruções de desvio • Ex.: Quando umbranch é tomado, como tratar a(s)

39

Ch6.a-77? 1998 Morgan Kaufmann PublishersPaulo C. Centoducatte

Data Hazard e Stalls

Reg

IM

Reg

Reg

IM

CC 1 CC 2 CC 3 CC 4 CC 5 CC 6

Time (in clock cycles)

lw $2, 20($1)

Programexecutionorder(in instructions)

and $4, $2, $5

IM Reg DM Reg

IM DM Reg

IM DM Reg

CC 7 CC 8 CC 9

or $8, $2, $6

add $9, $4, $2

slt $1, $6, $7

DM Reg

Reg

Reg

DM

Ch6.a-78? 1998 Morgan Kaufmann PublishersPaulo C. Centoducatte

Data Hazards e Stalls

? Quando uma instrução tenta ler um registrador precedida por uma instrução de load, que escreve no mesmo registrador ? o dado tem que ser mantido (ciclo 4) enquanto a ULA executa a operação ? atrasar o pipelinepara que a instrução leia o valor correto.

? Condição de detecção de hazard para atraso no pipeline

# testa se é um load:

if (ID/EX.MemRead and

# verifica se registrador destino da instrução load em EX é o registrador fonte da instrução em ID:

((ID/EX.RegisterRt = IF/ID.RegisterRs) or( ID/EX.RegisterRt = IF/ID.RegisterRt)))

stall pipeline

Page 40: Pipeline - Unicamppannain/mc542/aulas/ch6_arq.pdf · 2006. 8. 2. · – Problemas devido à execução de instruções de desvio • Ex.: Quando umbranch é tomado, como tratar a(s)

40

Ch6.a-79? 1998 Morgan Kaufmann PublishersPaulo C. Centoducatte

Data Hazards e Stalls

lw $2, 20($1)

Programexecutionorder(in instructions)

and $4, $2, $5

or $8, $2, $6

add $9, $4, $2

slt $1, $6, $7

Reg

IM

Reg

Reg

IM DM

CC 1 CC 2 CC 3 CC 4 CC 5 CC 6Time (in clock cycles)

IM Reg DM RegIM

IM DM Reg

IM DM Reg

CC 7 CC 8 CC 9 CC 10

DM Reg

RegReg

Reg

bubble

Ch6.a-80? 1998 Morgan Kaufmann PublishersPaulo C. Centoducatte

Data Hazards e Stalls

? Se a instrução no estágio ID é atrasada, a instrução no estágio IF também deve ser atrasada

? Como fazer?Impedir a mudança no PC e no registrador IF/IF. A instrução em IF continua sendo lida e em ID continuam sendo lido os mesmos campos da instrução.

? Stall pipeline: mesmo efeito da instrução nop começandopelo estágio EX ? desativar os 9 sinais de controle dos estágios EX, MEM e WB

Page 41: Pipeline - Unicamppannain/mc542/aulas/ch6_arq.pdf · 2006. 8. 2. · – Problemas devido à execução de instruções de desvio • Ex.: Quando umbranch é tomado, como tratar a(s)

41

Ch6.a-81? 1998 Morgan Kaufmann PublishersPaulo C. Centoducatte

Datapath com forwarding e data hazard detection

PCInstruction

memory

Registers

Mux

Mux

Mux

Control

ALU

EX

M

WB

M

WB

WB

ID/EX

EX/MEM

MEM/WB

Datamemory

Mux

Hazarddetection

unit

Forwardingunit

0

Mux

IF/ID

Inst

r uct

ion

ID/EX.MemRead

IF/ID

Wr i

te

PC

Wri

te

ID/EX.RegisterRt

IF/ID.RegisterRd

IF/ID.RegisterRt

IF/ID.RegisterRt

IF/ID.RegisterRs

RtRs

Rd

Rt EX/MEM.RegisterRd

MEM/WB.RegisterRd

Ch6.a-82? 1998 Morgan Kaufmann PublishersPaulo C. Centoducatte

Seqüência de execução do exemplo anterior

Hazarddetection

unit

0

MuxIF

/IDW

rite

PC

Writ

e

ID/EX.RegisterRt

ID/EX.MemRead

M

WB

$1

$X

X

1

2

before<3>

PC Instructionmemory

Registers

Mux

Mux

Mux

EX WB

Datamemory

Mux

Forwardingunit

Inst

ruct

ion

IF/ID

ID/EX

EX/MEM

MEM/WB

and $4, $2, $5 lw $2, 20($1) before<1> before<2>

Clock 2

1

1

X

X11

Control

ALU

M

WB

Page 42: Pipeline - Unicamppannain/mc542/aulas/ch6_arq.pdf · 2006. 8. 2. · – Problemas devido à execução de instruções de desvio • Ex.: Quando umbranch é tomado, como tratar a(s)

42

Ch6.a-83? 1998 Morgan Kaufmann PublishersPaulo C. Centoducatte

Seqüência de execução do exemplo anterior

Hazarddetection

unit

0

MuxIF

/IDW

rite

PC

Writ

e

ID/EX.RegisterRt

lw $2, 20($1)

PC Instructionmemory

Registers

Mux

Mux

Mux

EX

M

WB

WB

Datamemory

Mux

Forwardingunit

Inst

ruct

ion

IF/ID

and $4, $2, $5

ID/EX

before<1>

EX/MEM

before<2>

MEM/WB

or $4, $4, $2

Clock 3

2

5

2

500 11

$2

$5

5

2

4

$1

$X

X

1

2

Control

ALU

M

WB

ID/EX.MemRead

Ch6.a-84? 1998 Morgan Kaufmann PublishersPaulo C. Centoducatte

Seqüência de execução do exemplo anterior

$2

$5

5

2

24

WB

Hazarddetection

unit

0

MuxIF

/IDW

rite

PC

Writ

e

ID/EX.RegisterRt

PCInstruction

memory

Registers

Mux

Mux

Mux

EX

M

WB

Datamemory

Mux

Inst

ruct

ion

IF/ID

and $4, $2, $5 bubble

ID/EX

lw $2, . . .

EX/MEM

before<1>

MEM/WB

Clock 4

2

2

5

510

11

00

$2

$5

5

2

4

Control

ALU

M

WB

Forwardingunit

ID/EX.MemRead

or $4, $4, $2

Page 43: Pipeline - Unicamppannain/mc542/aulas/ch6_arq.pdf · 2006. 8. 2. · – Problemas devido à execução de instruções de desvio • Ex.: Quando umbranch é tomado, como tratar a(s)

43

Ch6.a-85? 1998 Morgan Kaufmann PublishersPaulo C. Centoducatte

Seqüência de execução do exemplo anterior

Hazarddetection

unit

0

MuxIF

/IDW

rite

PC

Writ

e

ID/EX.RegisterRt

2

bubble lw $2, . . .

PCInstruction

memory

Registers

Mux

Mux

Mux

EX

M

WB

M

WB

Datamemory

Mux

Forwardingunit

Inst

ruct

ion

IF/ID

and $4, $2, $5

ID/EX

EX/MEM

MEM/WB

add $9, $4, $2

Clock 5

2

210 10

11

$4

$2

2

4

4

4

2

4

$2

$5

5

2

4

Control

ALU

0

WB

ID/EX.MemRead

or $4, $4, $2

Ch6.a-86? 1998 Morgan Kaufmann PublishersPaulo C. Centoducatte

Seqüência de execução do exemplo anterior

PC Instructionmemory

Hazarddetection

unit

0

MuxIF

/IDW

rite

PC

Writ

e

ID/EX.RegisterRt

bubble

Registers

Mux

Mux

EX

M

WB

M

WB

Datamemory

Mux

Inst

ruct

ion

IF/ID

add $9, $4, $2

ID/EX

and $4, . . .

EX/MEM

MEM/WB

Clock 6

4

4

2

210 10

$4

$2

2

4

49

$2

2

Control

ALU

10

WB0

after<1>

Forwardingunit

$4

4

4

or $4, $4, $2

ID/EX.MemRead

Mux

Page 44: Pipeline - Unicamppannain/mc542/aulas/ch6_arq.pdf · 2006. 8. 2. · – Problemas devido à execução de instruções de desvio • Ex.: Quando umbranch é tomado, como tratar a(s)

44

Ch6.a-87? 1998 Morgan Kaufmann PublishersPaulo C. Centoducatte

Seqüência de execução do exemplo anterior

Registers

Inst

ruct

ion

ID/EX

4

Control

PC Instructionmemory

IF/ID

Writ

e

PC

Writ

e

add $9, $4, $2 or $4, . . . and $4, . . .after<2> after<1>

Clock 7

Mux

Mux

Mux

EX

M

WB

M

WB

Datamemory

Mux

Forwardingunit

EX/MEM

MEM/WB

10 10

$4

$2

2

4

9

ALU

10

WB

44

1

Hazarddetection

unit

0

Mux

ID/EX.RegisterRt

ID/EX.MemRead

IF/ID

Ch6.a-88? 1998 Morgan Kaufmann PublishersPaulo C. Centoducatte

Branch Hazards

• Devemos ter um fetch de instrução por ciclo de clock, decisão:qual caminho de um branch deve ocorrer até o estágio MEM.

Reg

Reg

CC 1

Time (in clock cycles)

40 beq $1, $3, 7

Programexecutionorder(in instructions)

IM Reg

IM DM

IM DM

IM DM

DM

DM Reg

Reg Reg

Reg

Reg

RegIM

44 and $12, $2, $5

48 or $13, $6, $2

52 add $14, $2, $2

72 lw $4, 50($7)

CC 2 CC 3 CC 4 CC 5 CC 6 CC 7 CC 8 CC 9

Reg

Page 45: Pipeline - Unicamppannain/mc542/aulas/ch6_arq.pdf · 2006. 8. 2. · – Problemas devido à execução de instruções de desvio • Ex.: Quando umbranch é tomado, como tratar a(s)

45

Ch6.a-89? 1998 Morgan Kaufmann PublishersPaulo C. Centoducatte

Branch Hazards

• Tês esquemas para resolver control hazard :

• Branch-delay Slots

• Assume Branch Not Taken

• Dynamic Branch Prediction

• Assume Branch Not Taken

• continua a execução seqüencialmente e se o branch for tomado, descarta as instruções entre a instrução de branch e a instrução no endereço alvo, fazendo seus sinais de controle iguais a zero

Ch6.a-90? 1998 Morgan Kaufmann PublishersPaulo C. Centoducatte

Branch Hazards

? Redução do atraso de branches

? reduzir o custo se o branch for tomado

? adiantar a execução da instrução de branch.

? O next PC para uma instrução de branch é selecionado no estágio MEM

? executar o branch no estágio ID(apenas uma instrução será descartada)

? deslocar o cálculo do endereço de branch (branch adder) do MEM para o ID e comparando os registradores lidos do register file.

Page 46: Pipeline - Unicamppannain/mc542/aulas/ch6_arq.pdf · 2006. 8. 2. · – Problemas devido à execução de instruções de desvio • Ex.: Quando umbranch é tomado, como tratar a(s)

46

Ch6.a-91? 1998 Morgan Kaufmann PublishersPaulo C. Centoducatte

Branch Hazards

PCInstruction

memory

4

Registers

Mux

Mux

Mux

ALU

EX

M

WB

M

WB

WB

ID/EX

0

EX/MEM

MEM/WB

Datamemory

Mux

Hazarddetection

unit

Forwardingunit

IF.Flush

IF/ID

Signextend

Control

Mux

=

Shiftleft 2

Mux

Ch6.a-92? 1998 Morgan Kaufmann PublishersPaulo C. Centoducatte

Branch Hazards (exemplo)

36 sub $10, $4, $840 beq $1, $3, 7 # PC ? 40 + 4 +7*4 = 7244 and $12, $2, $548 or $13, $2, $652 add $14, $4, $256 slt $15, $6, $7.... ................ ............72 lw $4, 50($7)

Page 47: Pipeline - Unicamppannain/mc542/aulas/ch6_arq.pdf · 2006. 8. 2. · – Problemas devido à execução de instruções de desvio • Ex.: Quando umbranch é tomado, como tratar a(s)

47

Ch6.a-93? 1998 Morgan Kaufmann PublishersPaulo C. Centoducatte

Branch Hazards (exemplo)

PC Instructionmemory

4

Registers

Signextend

Mux

Mux

Control

EX

M

WB

M

WB

WB

Mux

Hazarddetection

unit

Forwardingunit

Mux

IF.Flush

IF/ID

and $12, $2, $5 beq $1, $3, 7 sub $10, $4, $8

MEM/WB

EX/MEM

ID/EX

Clock 3

72 44

48 44

28

7

$1

$3

10

48

72

72

0

$4

$8

ALU Datamemory

Mux

Shiftleft 2

before<1> before<2>

=

Ch6.a-94? 1998 Morgan Kaufmann PublishersPaulo C. Centoducatte

Branch Hazards (exemplo)

Mux

0

bubble (nop)lw $4, 50($7)

Clock 4

beq $1, $3, 7 sub $10, . . . before<1>

PC Instructionmemory

4

Registers

Signextend

Mux

Mux

Control

EX

M

WB

M

WB

WB

Mux

Hazarddetection

unit

Forwardingunit

IF.Flush

IF/ID

MEM/WB

EX/MEM

ID/EX

76 72

76 72

$1

$3

10

76

ALU Datamemory

Mux

Shiftleft 2

=

Page 48: Pipeline - Unicamppannain/mc542/aulas/ch6_arq.pdf · 2006. 8. 2. · – Problemas devido à execução de instruções de desvio • Ex.: Quando umbranch é tomado, como tratar a(s)

48

Ch6.a-95? 1998 Morgan Kaufmann PublishersPaulo C. Centoducatte

Dymanic Branch Prediction

? Branch not taken

? forma de branch predicton que assume que obranch não será tomado.

? Dymanic Branch Prediction

? descobre se o branch foi tomado ou não na última vezque foi executado e faz o fetch das instruções pelomesmo local da última vez.

Ch6.a-96? 1998 Morgan Kaufmann PublishersPaulo C. Centoducatte

Dymanic Branch Prediction

? Implementação? branch prediction buffer ? branch history table

? Branch prediction buffer: pequena memória indexada por bits menos significativos do endereço da instrução de branch. Ela contém um bit que diz se o branch foi recentemente tomado ou não (Neste esquema não sabemos se a previsão é correta ou não, pois este buffer pode ser alterado por outra instrução de branch que tem os mesmos bits menos significativos de endereço).