0% found this document useful (0 votes)
8 views20 pages

Lecture # 8B

Uploaded by

amna995tariq
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views20 pages

Lecture # 8B

Uploaded by

amna995tariq
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

The Processor

Lecture # 8
Course Instructor: Dr. Noshina
Outline
• Pipeline and ISA design
• Pipeline hazards
• Structural hazard
• Data hazard
• Control hazard
• Pipeline summary
Pipelining and ISA Design

• RISC-V ISA designed for pipelining


– All instructions are same length
• Easier to fetch in one stage and decode in second
stage.
• x86: 1- to 15-bytes instructions
– Few and regular instruction formats
• Can decode and read registers in one step
CONTD…

– Load/store addressing
• Can calculate address in 3rd stage, access memory
in 4th stage
– Alignment of memory operands
• Memory access takes only one cycle
Pipeline Hazards

• Situations that prevent starting the next instruction in


the next cycle
• Structural hazard
– A required resource is busy. It means that the
hardware cannot support the combination of
instructions that we want to execute in the same
clock cycle
CONTD…
• Data hazard
– When a planned instruction cannot execute in the
proper clock cycle because data that is needed to
execute the instruction is not yet available
• Control hazard
– When the proper instruction cannot execute in the
proper pipeline clock cycle because the instruction
that was fetched is not the one that is needed.
Structural Hazards
• When a planned instruction cannot execute in the proper
clock cycle because the hardware does not support the
combination of instructions that are set to execute.
• Conflict for use of a resource
• In RISC-V pipeline with a single memory
– Load/store requires data access
– Instruction fetch would have to stall for that cycle
• Hence, pipelined data paths require separate
instruction/data memories.
Structural Hazards
E.g., suppose single – not separate – instruction and data memory in
pipeline below with one read port
– then a structural hazard between first and fourth instructions
P ro g ra m
e x e c u t io n 2 4 6 8 10 12 14
T im e
o rd e r
( i n in s t r u c ti o n s )
I n s tr u c tio n D a ta
ld x7 , 1 0 0 ( x22 )
f e tc h
R eg AL U
access
R eg
Hazard if
ld x8 , 2 0 0 ( x22 ) 2 ns
I n s tr u c tio n
f e tc h
R eg AL U
D a ta
acces s
R eg single
I n s tr u c tio n D a ta
memory
ld x9 , 3 0 0 ( x22
2) 2 ns R eg AL U R eg
f e tc h acces s

I n s tr u c tio n D a ta
ld x9 , 4 0 0 ( x22 ) R eg AL U R eg
2 ns f e tc h acces s

2 ns 2 ns 2 ns 2 ns 2 ns

• RISC-V was designed to be pipelined: structural hazards are easy to avoid!


Data Hazards
• An instruction depends on completion of data access by a previous
instruction
– add x1, x2, x3
sub x4, x1, x5
Forwarding (Bypassing)
• Use result when it is computed
– Don’t wait for it to be stored in a register
– Requires extra connections in the datapath
Load-Use Data Hazard
• Can’t always avoid stalls by forwarding
– If value not computed when needed
– Can’t forward backward in time!
Code Scheduling to Avoid Stalls (Software
Solution)
• Reorder code to avoid use of load result in the next instruction
• C code for a = b + e; c = b + f;

ld x1, 0(x31) ld x1, 0(x31)


ld x2, 8(x31) ld x2, 8(x31)
stall add x3, x1, x2 ld x4,
sd x3, 24(31) 16(x31)
ld x4, add x3, x1, x2
stall 16(x31) sd x3, 24(31)
add x5, x1, x4 add x5, x1, x4
sd x5, sd x5,
32(x31) 32(x31)
13 cycles 11 cycles
CONTD…

Reordering Code to Avoid Pipeline Stall


• Example:
ld x6, 0(x22)
ld x8, 4(x22)
sd x8, 0(x22) Data hazard
sd x9, 4(x22)

• Reordered code:
ld x6, 0(x22)
ld x8, 4(x22)
sd x9, 4(x22)
sd x8, 0(x22) Interchanged
Control Hazards

• 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 RISC-V pipeline
– Need to compare registers and compute target
early in the pipeline
– Add hardware to do it in ID stage
Stall on Branch
• Wait until branch outcome determined
before fetching next instruction
Branch Prediction
• Longer pipelines can’t readily determine branch
outcome early
– Stall penalty becomes unacceptable
• Predict outcome of branch
– Only stall if prediction is wrong
• In RISC-V pipeline
– Can predict branches not taken
– Fetch instruction after branch, with no delay
RISC-V with Predict Not Taken

Prediction
correct

Prediction
incorrect
More-Realistic Branch Prediction
• 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 prediction
– Hardware 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
Pipeline Summary
The BIG Picture

• 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

You might also like