Upload
others
View
1
Download
0
Embed Size (px)
Citation preview
M. C. Felipe Santiago Espinosa Abril/2018
Segmentación del Path de Datos
(Parte II)
Maestría en ElectrónicaArquitectura de Computadoras
Unidad 5
Pipelining Analogy
Segmentación del Path de Datos2
Pipelined laundry: overlapping execution Parallelism improves performance
Four loads: Speedup = 8/3.5 = 2.3
Non-stop: Speedup
= 2*n/0.5 + 1.5 ≈ 4= number of stages
MIPS Pipeline
Segmentación del Path de Datos3
Five stages, one step per stage1. IF: Instruction fetch from memory2. ID: Instruction decode & register read3. EX: Execute operation or calculate address4. MEM: Access memory operand5. WB: Write result back to register
Pipeline Performance
Segmentación del Path de Datos4
Assume time for stages is 100ps for register read or write 200ps for other stages
Compare pipelined datapath with single-cycle datapath
Instr Instr fetch Register read
ALU op Memory access
Register write
Total time
lw 200ps 100 ps 200ps 200ps 100 ps 800ps
sw 200ps 100 ps 200ps 200ps 700ps
R-format 200ps 100 ps 200ps 100 ps 600ps
beq 200ps 100 ps 200ps 500ps
Pipeline Performance
Segmentación del Path de Datos
Single-cycle (Tc= 800ps)
Pipelined (Tc= 200ps)
5
Pipeline Speedup
Segmentación del Path de Datos6
If all stages are balanced i.e., all take the same time Time between instructionspipelined =
Time between instructionsnonpipelined
Number of stages If not balanced, speedup is less Speedup due to increased throughput Latency (time for each instruction) does not decrease
Pipelining and ISA Design
Segmentación del Path de Datos7
MIPS ISA designed for pipeliningAll instructions are 32-bits Easier to fetch and decode in one cycle c.f. x86: 1- to 17-byte instructions
Few and regular instruction formats Can decode and read registers in one step
Load/store addressing Can calculate address in 3rd stage, access memory in 4th stage
Alignment of memory operands Memory access takes only one cycle
Hazards
Segmentación del Path de Datos
Situations that prevent starting the next instruction in the next cycle
Structure hazardsA required resource is busy
Data hazardNeed to wait for previous instruction to complete its
data read/write Control hazardDeciding on control action depends on previous
instruction
8
Structure Hazards
Segmentación del Path de Datos9
Conflict for use of a resource In MIPS pipeline with a single memory Load/store requires data access Instruction fetch would have to stall for that cycle Would cause a pipeline “bubble”
Hence, pipelined datapaths require separate instruction/data memoriesOr separate instruction/data caches
Data Hazards
Segmentación del Path de Datos
An instruction depends on completion of data access by a previous instruction add $s0, $t0, $t1sub $t2, $s0, $t3
10
Forwarding (aka Bypassing)
Segmentación del Path de Datos
Use result when it is computed Don’t wait for it to be stored in a register Requires extra connections in the datapath
11
Load-Use Data Hazard
Segmentación del Path de Datos
Can’t always avoid stalls by forwarding If value not computed when needed Can’t forward backward in time!
12
Code Scheduling to Avoid Stalls
Segmentación del Path de Datos
Reorder code to avoid use of load result in the next instruction C code for A = B + E; C = B + F;
lw $t1, 0($t0)
lw $t2, 4($t0)
add $t3, $t1, $t2
sw $t3, 12($t0)
lw $t4, 8($t0)
add $t5, $t1, $t4
sw $t5, 16($t0)
stall
stall
lw $t1, 0($t0)
lw $t2, 4($t0)
lw $t4, 8($t0)
add $t3, $t1, $t2
sw $t3, 12($t0)
add $t5, $t1, $t4
sw $t5, 16($t0)
11 cycles13 cycles
13
Control Hazards
Segmentación del Path de Datos14
Branch 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
In MIPS pipelineNeed to compare registers and compute target early in
the pipelineAdd hardware to do it in ID stage
Stall on Branch
Segmentación del Path de Datos
Wait until branch outcome determined before fetching next instruction
15
Branch Prediction
Segmentación del Path de Datos
Longer pipelines can’t readily determine branch outcome early Stall penalty becomes unacceptable
Predict outcome of branchOnly stall if prediction is wrong
In MIPS pipelineCan predict branches not taken Fetch instruction after branch, with no delay
16
MIPS with Predict Not Taken
Segmentación del Path de Datos
Prediction correct
Prediction incorrect
17
More-Realistic Branch Prediction
Segmentación del Path de Datos
Static branch prediction Based on typical branch behavior Example: loop and if-statement branches Predict backward branches taken Predict forward branches not taken
Dynamic branch predictionHardware measures actual branch behavior e.g., record recent history of each branch
Assume future behavior will continue the trend When wrong, stall while re-fetching, and update history
18
Pipeline Summary
Segmentación del Path de Datos
Pipelining improves performance by increasing instruction throughput Executes multiple instructions in parallel Each instruction has the same latency
Subject to hazards Structure, data, control
Instruction set design affects complexity of pipeline implementation
The BIG Picture
19
MIPS Pipelined Datapath
Segmentación del Path de Datos
WB
MEM
Right-to-left flow leads to hazards
20
Pipeline registers
Segmentación del Path de Datos
Need registers between stages To hold information produced in previous cycle
21
Pipeline Operation
Segmentación del Path de Datos
Cycle-by-cycle flow of instructions through the pipelined datapath “Single-clock-cycle” pipeline diagram Shows pipeline usage in a single cycle Highlight resources used
c.f. “multi-clock-cycle” diagram Graph of operation over time
We’ll look at “single-clock-cycle” diagrams for load & store
22
IF for Load, Store, …
Segmentación del Path de Datos23
ID for Load, Store, …
Segmentación del Path de Datos24
EX for Load
Segmentación del Path de Datos25
MEM for Load
Segmentación del Path de Datos26
WB for Load
Segmentación del Path de Datos
Wrongregisternumber
27
Corrected Datapath for Load
Segmentación del Path de Datos28
EX for Store
Segmentación del Path de Datos29
MEM for Store
Segmentación del Path de Datos30
WB for Store
Segmentación del Path de Datos31
Multi-Cycle Pipeline Diagram
Segmentación del Path de Datos32
Traditional form
Multi-Cycle Pipeline Diagram
Segmentación del Path de Datos
Form showing resource usage
33
Single-Cycle Pipeline Diagram
Segmentación del Path de Datos
State of pipeline in a given cycle
34
Pipelined Control (Simplified)
Segmentación del Path de Datos35
Pipelined Control
Segmentación del Path de Datos
Control signals derived from instruction As in single-cycle implementation
36
Pipelined Control
Segmentación del Path de Datos37
Ejemplo:
Segmentación del Path de Datos38
Evaluar la ejecución de las instrucciones siguientes a través de un procesador segmentado:
lw $10, 20 ($1)sub $11, $2, $3 and $12, $4, $5 or $13, $6, $7 add $14, $8, $9
Respuesta: Se requieren 5 ciclos de reloj para concluir con la instrucción lw Cuando lw termina su ejecución, las siguientes instrucciones se
producen una por ciclo de reloj. En total se requiere de 9 ciclos para la ejecución de las cinco
instrucciones.
Ciclo de reloj no. 1
Segmentación del Path de Datos39
Ciclo de reloj no. 2
Segmentación del Path de Datos40
Ciclo de reloj no. 3
Segmentación del Path de Datos41
Ciclo de reloj no. 4
Segmentación del Path de Datos42
Ciclo de reloj no. 5
Segmentación del Path de Datos43
Ciclo de reloj no. 6
Segmentación del Path de Datos44
Ciclo de reloj no. 7
Segmentación del Path de Datos45
Ciclo de reloj no. 8
Segmentación del Path de Datos46
Ciclo de reloj no. 9
Segmentación del Path de Datos47
Data Hazards in ALU Instructions
Segmentación del Path de Datos48
Consider this sequence:sub $2, $1,$3and $12,$2,$5or $13,$6,$2add $14,$2,$2sw $15,100($2)
We can resolve hazards with forwarding How do we detect when to forward?
Dependencies & Forwarding
Segmentación del Path de Datos49
Detecting the Need to Forward
Segmentación del Path de Datos
ALU operand register numbers in EX stage are given by ID/EX.RegisterRs, ID/EX.RegisterRt
Data hazards when1a. EX/MEM.RegisterRd = ID/EX.RegisterRs1b. EX/MEM.RegisterRd = ID/EX.RegisterRt2a. MEM/WB.RegisterRd = ID/EX.RegisterRs2b. MEM/WB.RegisterRd = ID/EX.RegisterRt
Fwd fromEX/MEMpipeline reg
Fwd fromMEM/WBpipeline reg
50
Detecting the Need to Forward
Segmentación del Path de Datos
But only if forwarding instruction will write to a register! EX/MEM.RegWrite, MEM/WB.RegWrite
And only if Rd for that instruction is not $zero EX/MEM.RegisterRd ≠ 0,
MEM/WB.RegisterRd ≠ 0
51
Forwarding Paths
Segmentación del Path de Datos52
Forwarding Conditions
Segmentación del Path de Datos
EX hazard if (EX/MEM.RegWrite and (EX/MEM.RegisterRd ≠ 0)
and (EX/MEM.RegisterRd = ID/EX.RegisterRs)) ForwardA = 10
if (EX/MEM.RegWrite and (EX/MEM.RegisterRd ≠ 0) and (EX/MEM.RegisterRd = ID/EX.RegisterRt)) ForwardB = 10
MEM hazard if (MEM/WB.RegWrite and (MEM/WB.RegisterRd ≠ 0)
and (MEM/WB.RegisterRd = ID/EX.RegisterRs)) ForwardA = 01
if (MEM/WB.RegWrite and (MEM/WB.RegisterRd ≠ 0) and (MEM/WB.RegisterRd = ID/EX.RegisterRt)) ForwardB = 01
53
Double Data Hazard
Segmentación del Path de Datos
Consider the sequence:add $1,$1,$2add $1,$1,$3add $1,$1,$4
Both hazards occur Want to use the most recent
Revise MEM hazard condition Only fwd if EX hazard condition isn’t true
54
Revised Forwarding Condition
Segmentación del Path de Datos
MEM hazard if (MEM/WB.RegWrite and (MEM/WB.RegisterRd ≠ 0)
and not (EX/MEM.RegWrite and (EX/MEM.RegisterRd ≠ 0) and (EX/MEM.RegisterRd = ID/EX.RegisterRs)) and (MEM/WB.RegisterRd = ID/EX.RegisterRs)) ForwardA = 01
if (MEM/WB.RegWrite and (MEM/WB.RegisterRd ≠ 0) and not (EX/MEM.RegWrite and (EX/MEM.RegisterRd ≠ 0) and (EX/MEM.RegisterRd = ID/EX.RegisterRt)) and (MEM/WB.RegisterRd = ID/EX.RegisterRt)) ForwardB = 01
55
Datapath with Forwarding
Segmentación del Path de Datos56
Load-Use Data Hazard
Segmentación del Path de Datos
Need to stall for one cycle
57
Load-Use Hazard Detection
Segmentación del Path de Datos
Check when using instruction is decoded in ID stage ALU operand register numbers in ID stage are given by IF/ID.RegisterRs, IF/ID.RegisterRt
Load-use hazard when ID/EX.MemRead and
((ID/EX.RegisterRt = IF/ID.RegisterRs) or (ID/EX.RegisterRt = IF/ID.RegisterRt))
If detected, stall and insert bubble
58
How to Stall the Pipeline
Segmentación del Path de Datos
Force control values in ID/EX registerto 0 EX, MEM and WB do nop (no-operation)
Prevent update of PC and IF/ID register Using instruction is decoded again Following instruction is fetched again 1-cycle stall allows MEM to read data for lw Can subsequently forward to EX stage
59
Stall/Bubble in the Pipeline
Segmentación del Path de Datos
Stall inserted here
60
Stall/Bubble in the Pipeline
Segmentación del Path de Datos
Or, more accurately…
61
Datapath with Hazard Detection
Segmentación del Path de Datos62
Ejemplo:
Segmentación del Path de Datos63
Con la unidad de anticipación y la unidad de detección de riesgos incorporadas en el procesador segmentando, analice su comportamiento ciclo a ciclo durante la ejecución del código:
lw $2, 20($1) and $4, $2, $5 or $4, $4, $2 add $9, $4, $2
Se revisarán los ciclos del 2 al 7 para observar cómo se inserta la burbuja y cómo avanza sin afectar la ejecución del programa. También se notará que no hay conflictos entre ambas unidades.
Desarrolle la representación Multi-ciclos para observar el comportamiento global de la ejecución.
Segmentación del Path de Datos64
Segmentación del Path de Datos65
Segmentación del Path de Datos66
Segmentación del Path de Datos67
Segmentación del Path de Datos68
Segmentación del Path de Datos69
Stalls and Performance
Segmentación del Path de Datos
Stalls reduce performance But are required to get correct results
Compiler can arrange code to avoid hazards and stalls Requires knowledge of the pipeline structure
The BIG Picture
70
Branch Hazards
Segmentación del Path de Datos
If branch outcome determined in MEM
PC
Flush theseinstructions(Set controlvalues to 0)
71
Incorporando Saltos Incondicionales
Segmentación del Path de Datos72
Los saltos incondicionales (J) se pueden ejecutar tan pronto se identifica a la instrucción.
Esto se hace en la etapa ID. Dos acciones deben realizarse: Anular la instrucción que está en la etapa IF limpiando el
registro IF/ID. Remplazar el valor del PC por el destino del salto.
Segmentación del Path de Datos73
Implementación que resuelve a BEQ en la etapa MEM y a J en la etapa ID
Reducing Branch Delay
Segmentación del Path de Datos
Move hardware to determine outcome to ID stage Target address adder Register comparator
Example: branch taken36: sub $10, $4, $840: beq $1, $3, 744: and $12, $2, $548: or $13, $2, $652: add $14, $4, $256: slt $15, $6, $7 ...72: lw $4, 50($7)
74
Example: Branch Taken
Segmentación del Path de Datos75
Example: Branch Taken
Segmentación del Path de Datos76
Data Hazards for Branches
Segmentación del Path de Datos
If a comparison register is a destination of 2nd or 3rd preceding ALU instruction
…
IF ID EX MEM WB
IF ID EX MEM WB
IF ID EX MEM WB
IF ID EX MEM WB
add $4, $5, $6
add $1, $2, $3
beq $1, $4, target
n Can resolve using forwarding
77
Data Hazards for Branches
Segmentación del Path de Datos
If a comparison register is a destination of preceding ALU instruction or 2nd preceding load instruction Need 1 stall cycle
beq stalled
IF ID EX MEM WB
IF ID EX MEM WB
IF ID
ID EX MEM WB
add $4, $5, $6
lw $1, addr
beq $1, $4, target
78
Data Hazards for Branches
Segmentación del Path de Datos
If a comparison register is a destination of immediately preceding load instruction Need 2 stall cycles
beq stalled
IF ID EX MEM WB
IF ID
ID
ID EX MEM WB
beq stalled
lw $1, addr
beq $1, $0, target
79
Ejercicio: Comparación de Implementaciones
Segmentación del Path de Datos80
Comparar el rendimiento de una implementación de un solo ciclo de reloj (PSC) con una implementación segmentada (PSEG). Considerando un programa de prueba con la siguiente distribución de instrucciones:
Tiempos de operación para las unidades funcionales: 2 ns - acceso a memoria, 2 ns - operación de la ALU, 1 ns – acceso a registros.
Para la implementación segmentada suponer que: la mitad de las instrucciones de carga están inmediatamente seguidas por una instrucción que usará el valor obtenido de la memoria, que una cuarta parte de los brincos condicionales si se realizan y sólo producen un retraso de 1 ciclo de reloj y que los saltos incondicionales también requieren de 1 ciclo de reloj adicional.
Tipo Frecuencia
Cargas 20 %
Almacenamientos 15 %
Brincos 20 %
Saltos 5 %
Aritmético – Lógicas 40 %
Ejercicios:
Segmentación del Path de Datos81
1. Por medio de una representación de múltiples ciclos de la segmentación, representar la ejecución de las siguientes instrucciones:
Add $2, $5, $4 Add $4, $2, $5 Lw $5, 100($2) Add $3, $5, $4 Beq $8, $8, s1 And $1, $2, $3 Or $4, $5, $6
s1: Sub $7, $8, $9
Mostrar las líneas por medio de las cuales se resuelven las dependencias de datos y/o mostrar la inserción de burbujas si es que se requieren. Suponga que los brincos se resuelven en la etapa ID.
Ejercicios (Comprensión y ampliación de la implementación).
Segmentación del Path de Datos82
Utilizando como base la figura de la diapositiva 73, analice como avanzan las instrucciones durante su ejecución:
add $t5, $t1, $t0 beq $t2, $t3, et1
Suponga que están ubicadas en la dirección 4000diez, que los registros $t0 y $t1 tienen los valores 30 y 40 respectivamente, que $t2 y $t3 tienen el dato 33 y que la etiqueta está ubicada 10 instrucciones después de la que se está evaluando. Resalte las líneas que estarán activas durante la ejecución y escriba el valor de cada una de esas líneas.
A la misma figura, agregue el hardware necesario (camino de datos y control) para que soporte la instrucción addi.
De manera similar, agregue el hardware necesario para que el procesador soporte la instrucción jal.