0% found this document useful (0 votes)
34 views

Chapter 4 Part 2

The document discusses pipelining in RISC-V processors. It describes a 5-stage pipeline with stages for instruction fetch, instruction decode, execute, memory access, and write back. Pipelining improves performance by allowing multiple instructions to progress through different stages simultaneously. However, hazards like structural, data, and control hazards can occur and need to be addressed. Common techniques to deal with hazards include forwarding, stalling, branch prediction, and scheduling instructions to avoid stalls. The document provides examples of how load and store instructions progress through each stage of the RISC-V pipeline and how data hazards are handled.

Uploaded by

gioaminefreiha65
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

Chapter 4 Part 2

The document discusses pipelining in RISC-V processors. It describes a 5-stage pipeline with stages for instruction fetch, instruction decode, execute, memory access, and write back. Pipelining improves performance by allowing multiple instructions to progress through different stages simultaneously. However, hazards like structural, data, and control hazards can occur and need to be addressed. Common techniques to deal with hazards include forwarding, stalling, branch prediction, and scheduling instructions to avoid stalls. The document provides examples of how load and store instructions progress through each stage of the RISC-V pipeline and how data hazards are handled.

Uploaded by

gioaminefreiha65
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 50

COMPUTER ORGANIZATION AND DESIGN RISC-V

Edition
The Hardware/Software Interface

Chapter 4
The Processor
Part-2
§4.5 An Overview of Pipelining
Pipelining Analogy
n Pipelined laundry: overlapping execution
n Parallelism improves performance

n Four loads:
n Speedup
= 8/3.5 = 2.3
n Non-stop:
n Speedup
= 2n/0.5n + 1.5 ≈ 4
= number of stages

Chapter 4 — The Processor — 2


RISC-V Pipeline
n Five stages, one step per stage
1. IF: Instruction fetch from memory
2. ID: Instruction decode & register read
3. EX: Execute operation or calculate address
4. MEM: Access memory operand
5. WB: Write result back to register

Chapter 4 — The Processor — 3


Pipeline Performance
n Assume time for stages is
n 100ps for register read or write
n 200ps for other stages
n Compare pipelined Datapath with single-cycle
Datapath

Instruction Instruction Register ALU op Memory Register Total time


fetch read access write

ld 200ps 100 ps 200ps 200ps 100 ps 800ps


sd 200ps 100 ps 200ps 200ps 700ps
R-format 200ps 100 ps 200ps 100 ps 600ps
beq 200ps 100 ps 200ps 500ps

Chapter 4 — The Processor — 4


Pipeline Performance
Single-cycle (Tc= 800ps)

Pipelined (Tc= 200ps)

Chapter 4 — The Processor — 5


Pipeline Speedup
n If all stages are balanced
n i.e., all take the same time
n Time between instructions(pipelined)
= Time between instructions(non-pipelined)
Number of stages
n If not balanced, speedup is less
n Speedup due to increased throughput
n Latency (time for each instruction) does not
decrease

Chapter 4 — The Processor — 6


Pipelining and ISA Design
n RISC-V ISA designed for pipelining
n All instructions are 32-bits
n Easier to fetch and decode in one cycle
n c.f. x86: 1- to 17-byte instructions
n Few and regular instruction formats
n Can decode and read registers in one step
n Load/store addressing
n Can calculate address in 3rd stage, access memory
in 4th stage

Chapter 4 — The Processor — 7


Hazards
n Situations that prevent starting the next
instruction in the next cycle
n Structure hazards
n A required resource is busy
n Data hazard
n Need to wait for previous instruction to
complete its data read/write
n Control hazard
n Deciding on control action depends on
previous instruction

Chapter 4 — The Processor — 8


Structure Hazards
n Conflict for use of a resource
n In RISC-V pipeline with a single memory
n Load/store requires data access
n Instruction fetch would have to stall for that cycle
n Would cause a pipeline “bubble”
n Hence, pipelined Datapaths require separate
instruction/data memories
n Or separate instruction/data caches

Chapter 4 — The Processor — 9


Data Hazards
n An instruction depends on completion of
data access by a previous instruction
n add x19, x0, x1
sub x2, x19, x3 x19 updated

x19 ready
Chapter 4 — The Processor — 10
Forwarding (aka Bypassing)
n Use result when it is computed
n Don’t wait for it to be stored in a register
n Requires extra connections in the Datapath

x1
ready

Chapter 4 — The Processor — 11


Load-Use Data Hazard
n Can’t always avoid stalls by forwarding
n If value not computed when needed
n Can’t forward backward in time!

Chapter 4 — The Processor — 12


Code Scheduling to Avoid Stalls
n Reorder code to avoid use of load result in
the next instruction
n C code for a = b + e; c = b + f;

ld x1, 0(x0) ld x1, 0(x0)


ld x2, 8(x0) ld x2, 8(x0)
stall add x3, x1, x2 ld x4, 16(x0)
sd x3, 24(x0) add x3, x1, x2
ld x4, 16(x0) sd x3, 24(x0)
stall add x5, x1, x4 add x5, x1, x4
sd x5, 32(x0) sd x5, 32(x0)
13 cycles 11 cycles

Chapter 4 — The Processor — 13


Control Hazards
n Branch determines flow of control
n Fetching next instruction depends on branch
outcome
n Pipeline can’t always fetch correct instruction
n Still working on ID stage of branch

n In RISC-V pipeline
n Need to compare registers and compute
target early in the pipeline
n Add hardware to do it in ID stage

Chapter 4 — The Processor — 14


Stall on Branch
n Wait until branch outcome determined
before fetching next instruction

Chapter 4 — The Processor — 15


Branch Prediction
n Longer pipelines can’t readily determine
branch outcome early
n Stall penalty becomes unacceptable
n Predict outcome of branch
n Only stall if prediction is wrong
n In RISC-V pipeline
n Can predict branches not taken
n Fetch instruction after branch, with no delay

Chapter 4 — The Processor — 16


More-Realistic Branch Prediction
n Static branch prediction
n Based on typical branch behavior
n Example: loop and if-statement branches
n Predict backward branches taken
n Predict forward branches not taken
n Dynamic branch prediction
n Hardware measures actual branch behavior
n e.g., record recent history of each branch
n Assume future behavior will continue the trend
n When wrong, stall while re-fetching, and update history

Chapter 4 — The Processor — 17


Pipeline Summary
The BIG Picture

n Pipelining improves performance by


increasing instruction throughput
n Executes multiple instructions in parallel
n Each instruction has the same latency
n Subject to hazards
n Structure, data, control
n Instruction set design affects complexity of
pipeline implementation
Chapter 4 — The Processor — 18
§4.6 Pipelined Datapath and Control
RISC-V Pipelined Datapath
1 2 3 4 5

MEM

Right-to-left WB
flow leads to
hazards
Pipeline registers
n Need registers between stages
n To hold information produced in previous cycle
IF/ID ID/EX EX/MEM MEM/WB
Pipeline Operation
n Cycle-by-cycle flow of instructions through
the pipelined Datapath
n “Single-clock-cycle” pipeline diagram
n Shows pipeline usage in a single cycle
n Highlight resources used
n c.f. “multi-clock-cycle” diagram
n Graph of operation over time
n We’ll look at “single-clock-cycle” diagrams
for load & store

Chapter 4 — The Processor — 21


IF for Load, Store, …
ID for Load, Store, …

Chapter 4 — The Processor — 23


EX for Load

Chapter 4 — The Processor — 24


MEM for Load

Chapter 4 — The Processor — 25


WB for Load

Wrong
register
number

Chapter 4 — The Processor — 26


Corrected Datapath for Load

Send back register number to write to with the data to be written


Chapter 4 — The Processor — 27
EX for Store

Chapter 4 — The Processor — 28


MEM for Store

Chapter 4 — The Processor — 29


WB for Store

Chapter 4 — The Processor — 30


Multi-Cycle Pipeline Diagram
n Form showing resource usage

EX:

Chapter 4 — The Processor — 31


Multi-Cycle Pipeline Diagram
n Traditional form

Chapter 4 — The Processor — 32


Single-Cycle Pipeline Diagram
n State of pipeline in a given cycle
1 2 3
4 5

Chapter 4 — The Processor — 33


Pipelined Control (Simplified)

Chapter 4 — The Processor — 34


Pipelined Control
n Control signals derived from instruction
n As in single-cycle implementation

EX control MEM WB control


Consumed control Consumed
Consumed
Pipelined Control
§4.7 Data Hazards: Forwarding vs. Stalling
Data Hazards in ALU Instructions
n Consider this sequence:
sub x2, x1,x3
and x12,x2,x5
or x13,x6,x2
add x14,x2,x2
sd x15,100(x2)

n We can resolve hazards with forwarding


n How do we detect when to forward?

Chapter 4 — The Processor — 37


Dependencies & Forwarding

Chapter 4 — The Processor — 38


Detecting the Need to Forward
n Pass register numbers along pipeline
n e.g., [ID/EX].RegisterRs1 = register number for Rs1 sitting in
ID/EX pipeline register
n ALU operand register numbers in EX stage
are given by
n [ID/EX].RegisterRs1, [ID/EX].RegisterRs2
n Data hazards when
1a. EX/MEM.RegisterRd = ID/EX.RegisterRs1 Fwd from
EX/MEM
1b. EX/MEM.RegisterRd = ID/EX.RegisterRs2 pipeline reg

2a. MEM/WB.RegisterRd = ID/EX.RegisterRs1 Fwd from


2b. MEM/WB.RegisterRd = ID/EX.RegisterRs2 MEM/WB
pipeline reg

Chapter 4 — The Processor — 39


Detecting the Need to Forward
n But only if forwarding instruction will write
to a register!
n EX/MEM.RegWrite, MEM/WB.RegWrite

n And only if Rd for that instruction is not x0


n EX/MEM.RegisterRd ≠ 0,
MEM/WB.RegisterRd ≠ 0

Chapter 4 — The Processor — 40


Forwarding Paths
00

01

10

00

01

10
Forwarding Conditions
Mux control Source Explanation
ForwardA = 00 ID/EX The first ALU operand comes from the register file.
ForwardA = 10 EX/MEM The first ALU operand is forwarded from the prior
ALU result.
ForwardA = 01 MEM/WB The first ALU operand is forwarded from data
memory or an earlier ALU result.

ForwardB = 00 ID/EX The second ALU operand comes from the register
file.
ForwardB = 10 EX/MEM The second ALU operand is forwarded from the prior
ALU result.
ForwardB = 01 MEM/WB The second ALU operand is forwarded from data
memory or an earlier ALU result.

Chapter 4 — The Processor — 42


Double Data Hazard
n Consider the sequence:
add x1,x1,x2
add x1,x1,x3
add x1,x1,x4
n Both hazards occur
n Want to use the most recent
n Revise MEM hazard condition
n Only forward from MEM if EX hazard condition isn’t
true

Chapter 4 — The Processor — 43


Revised Forwarding Condition
n MEM hazard
n if (MEM/WB.RegWrite If no need to forward from EXE

and (MEM/WB.RegisterRd ≠ 0)
and not(EX/MEM.RegWrite and (EX/MEM.RegisterRd ≠ 0)
and (EX/MEM.RegisterRd ≠ ID/EX.RegisterRs1))
and (MEM/WB.RegisterRd = ID/EX.RegisterRs1)) ForwardA = 01
n 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.RegisterRs2))
and (MEM/WB.RegisterRd = ID/EX.RegisterRs2)) ForwardB = 01

Chapter 4 — The Processor — 44


Datapath with Forwarding

Chapter 4 — The Processor — 45


Load-Use Hazard Detection
n Check when using instruction is decoded
in ID stage
n ALU operand register numbers in ID stage
are given by
n [IF/ID].RegisterRs1, [IF/ID].RegisterRs2
n Load-use hazard when
n ID/EX.MemRead and
((ID/EX.RegisterRd = IF/ID.RegisterRs1) or
(ID/EX.RegisterRd = IF/ID.RegisterRs2))
n If detected, stall and insert bubble

Chapter 4 — The Processor — 46


How to Stall the Pipeline
n Force control values in ID/EX register
to 0, which propagates to all stages
n EX, MEM and WB do nop (no-operation)
n Prevent update of PC and IF/ID register
n Using instruction is decoded again
n Following instruction is fetched again
n 1-cycle stall allows MEM to read data for ld
n Can subsequently forward to EX stage

Chapter 4 — The Processor — 47


Load-Use Data Hazard

Stall inserted
here
Datapath with Hazard Detection

0
Stalls and Performance
The BIG Picture

n Stalls reduce performance


n But are required to get correct results
n Compiler can arrange code to avoid
hazards and stalls
n Requires knowledge of the pipeline structure

Chapter 4 — The Processor — 50

You might also like