Pooja Vashisth
Pooja Vashisth
Pooja Vashisth
CSC
CSC 258
258
2
This Week’s Learning Goals
3
Today’s Plan
• Data Hazards and forwarding
• Scheduling
• Control Hazards and Prediction
4
CSC 258
5
RISC-V Instructions
6
The Pipelined Processor
7
Hazards
Structure hazards
A required resource is busy
Data hazards
A previous instruction must complete its data read/write
Control hazards
A previous (branch) will determine the next instruction to
execute
8
Structural Hazards
Structural hazards are caused by conflicts for a resource
Example:
Instruction fetch requires access to memory to get an instruction.
A load or store instruction requires access to memory to manipulate data
If there was a single point of access to memory, it could not handle both simultaneously: a
structural hazard
Good design can avoid structural hazards. But what about control and data hazards?
9
CSC 258
10
Data Hazards
11
Performance Penalty
12
Forwarding (Bypassing)
13
What do we need for forwarding?
First, we need to detect the need to forward.
14
Detecting the Need to Forward
15
Quick Check
Using this breakdown …
1a. EX/MEM.RegisterRd = ID/EX.RegisterRs1
1b. EX/MEM.RegisterRd = ID/EX.RegisterRs2
2a. MEM/WB.RegisterRd = ID/EX.RegisterRs1
2b. MEM/WB.RegisterRd = ID/EX.RegisterRs2
16
Forwarding Paths
It emits a control
signal to change
the value selected
by the multiplexer.
17
Activity: Analyzing Hazards
Identify all the data hazards in the following code, categorize it, and
explain how it might be resolved.
18
CSC 258
19
Revisiting the activity…
The last hazard in this sequence is difficult to resolve! We can’t easily
forward the value …
20
Load-Use Data Hazard
21
How to Stall the Pipeline
22
Datapath with Hazard Detection
23
How to Stall the Pipeline
This reduces throughput. One stage of the pipeline is now empty, not
doing productive work. Is there a better answer?
24
Code Scheduling to Avoid Stalls
Can reorder code to avoid use of load result in the next instruction
C code for a = b + e; c = b + f;
25
Revisiting the activity…
Go back to your answer to the previous activity. Can you provide a
different solution to resolving the lw-addi hazard?
26
The Bottom Line:
Stalls and Performance
27
CSC 258
28
Control Hazards
29
Reducing Branch Delay
30
Branch Prediction
31
Can we do better?
32
(More) Advanced Branch Prediction
33
Branch Prediction Impact
The following two textbook problems are in group homework as well.
34
Problem Text
35
Next topic: Interrupts and Exceptions
36
CSC
CSC 258
258
37
This Week’s Learning Goals
1. Describe how the RISC-V architecture services an exception/interrupt.
2. Explain how interrupts are used to interact with external devices (typically
I/O).
3. Explain how interrupts are used to implement system calls.
38
Today’s Plan
• Handling Exceptions
• Pipelining and Exceptions
• Exceptions and System Calls, I/O
39
Why?
40
Exceptions and Interrupts
41
How to Handle an Exception
42
How to Handle an Exception
1. Pause and save current location in running user process
Also need to make sure user state is not modified
2. Store cause of the exception/interrupt
3. Invoke a handler that will deal with the issue
Quick challenge: How would you do this?
43
How to Handle an Exception (in RISC-V)
44
One Handler vs. Many
RISC-V uses a single exception handler, but that’s not the only possibility
45
Single Handler Duties
46
CSC 258
47
Exceptions … and Pipelining
48
Exceptions: a Control Hazard
Consider malfunction on add in EX stage …
add x1, x2, x1
Must prevent x1 from being clobbered
Must complete previous instructions
To do so, flush add and subsequent instructions – but keep previous
The steps required are similar to a mispredicted branch
49
Pipeline with Exceptions
50
Exception Example
Exception on add …
40 sub x11, x2, x4
44 and x12, x2, x5
48 orr x13, x2, x6
4c add x1, x2, x1
50 sub x15, x6, x7
54 ld x16, 100(x7)
…
Handler
1C090000 sd x26, 1000(x10)
1c090004 sd x27, 1008(x10)
…
51
Exception Example
52
Exception Example
53
What about multiple exceptions?
54
CSC 258
55
Activity: Branch Prediction Impact
We’re going to explore how exceptions are handled by the pipelined
processor.
56
Exercise Text
57
CSC 258
58
We’ve seen how to support exceptions …
• System calls
• Handling I/O
59
System Calls
A question for you: How does ecall differ from a procedure call?
60
System Calls
61
Handling System Calls
62
Other Examples?
Timers: the OS can set a timer in the hardware which causes an interrupt when
the timer expires
Signals: signals (sent from one process to another) are implemented as interrupts
Virtual memory: the OS needs to provide the illusion that every process has
access to all of its memory (but also needs to store some of that memory on disk).
Interrupts are used when memory that is not loaded is accessed.
63
Coming Up
64
Don’t Forget!
65
Don’t Forget!
66
Next week: Memory and Storage
67
See you next
week!
68