ECE 5755: Modern Computer Systems and Architecture
Lecture 4: Pipeline Processors
Douglas
Mcllroy
Seymour
Cray
Douglas Mcllroy invented the concept of pipelining in the Version 3 Unix pages. The concept was later implemented and commercialized in 1970’s supercomputer by Cray Research led by Seymour Cray
Logistics
Photos, illustrations, graphics here.
Today’s agenda
Photos, illustrations, graphics here.
Terminology for pipelined processor
PC
I$
Register
File
s1
s2
d
D$
+
4
PC
PC
A
B
IR
IR
IR
IR
B
O
O
D
Fetch
Decode
Execute
Memory
Write-
back
Photos, illustrations, graphics here.
Pipelining example
PC
I$
Register
File
s1
s2
d
D$
+
4
PC
PC
A
B
IR
IR
IR
IR
B
O
O
D
add $3,$2,$1
Photos, illustrations, graphics here.
Pipelining example
PC
I$
Register
File
s1
s2
d
D$
+
4
PC
A
B
IR
IR
IR
B
O
O
D
add $3,$2,$1
lw $4,0($5)
PC
IR
Photos, illustrations, graphics here.
Pipelining example
PC
I$
Register
File
s1
s2
d
D$
+
4
IR
IR
B
O
O
D
add $3,$2,$1
lw $4,0($5)
sw $6, 4($7)
PC
PC
A
B
IR
IR
Photos, illustrations, graphics here.
Pipelining example
PC
I$
Register
File
s1
s2
d
D$
+
4
PC
IR
IR
O
D
add $3,$2,$1
lw $4,0($5)
sw $6, 4($7)
PC
A
B
IR
IR
B
O
Photos, illustrations, graphics here.
Pipelining example
PC
I$
Register
File
s1
s2
d
D$
+
4
PC
PC
A
B
IR
IR
add
lw $4,0($5)
sw $6, 4($7)
IR
O
D
IR
B
O
Photos, illustrations, graphics here.
Pipelining example
PC
I$
s1
s2
d
D$
+
4
PC
PC
A
B
IR
IR
IR
IR
B
O
O
D
lw $4
sw $6, 4($7)
Register
File
Photos, illustrations, graphics here.
Pipelining example
PC
I$
Register
File
s1
s2
d
D$
+
4
PC
PC
A
B
IR
IR
IR
IR
B
O
O
D
sw $6
Photos, illustrations, graphics here.
Understanding CPI for pipelined processors
Photos, illustrations, graphics here.
Pipeline Hazards
Photos, illustrations, graphics here.
How to resolve hazards?
Photos, illustrations, graphics here.
Structural hazards
Fetch
ALU (Execute)
Registers
Cache
Decode
Photos, illustrations, graphics here.
Structural hazards
Fetch
ALU (Execute)
Registers
Cache
lw R10, 10(R1) | IF | | | | | | | |
| | | | | | | | |
| | | | | | | | |
| | | | | | | | |
Decode
Photos, illustrations, graphics here.
Structural hazards
Fetch
ALU (Execute)
Registers
Cache
lw R10, 10(R1) | IF | ID | | | | | | |
sub R11, R2, R3 | | IF | | | | | | |
| | | | | | | | |
| | | | | | | | |
Decode
Photos, illustrations, graphics here.
Structural hazards
Fetch
ALU (Execute)
Registers
Cache
lw R10, 10(R1) | IF | ID | EX | | | | | |
sub R11, R2, R3 | | IF | ID | | | | | |
add R12, R4, R5 | | | IF | | | | | |
| | | | | | | | |
Decode
Photos, illustrations, graphics here.
Structural hazards
Fetch
ALU (Execute)
Registers
Cache
lw R10, 10(R1) | IF | ID | EX | MEM | | | | |
sub R11, R2, R3 | | IF | ID | EX | | | | |
add R12, R4, R5 | | | IF | ID | | | | |
add R13, R6, R7 | | | | IF | | | | |
Decode
Photos, illustrations, graphics here.
Solution 1: Stall
Fetch
ALU (Execute)
Registers
Cache
lw R10, 10(R1) | IF | ID | EX | MEM | WB | | | |
sub R11, R2, R3 | | IF | ID | EX | MEM | WB | | |
add R12, R4, R5 | | | IF | ID | EX | MEM | WB | |
add R13, R6, R7 | | | | <stall> | IF | ID | EX | MEM |
Decode
Photos, illustrations, graphics here.
Solution 1: duplicate resources
Fetch
ALU (Execute)
Registers
Inst $$
lw R10, 10(R1) | IF | ID | EX | MEM | WB | | | |
sub R11, R2, R3 | | IF | ID | EX | MEM | WB | | |
add R12, R4, R5 | | | IF | ID | EX | MEM | WB | |
add R13, R6, R7 | | | | IF | ID | EX | MEM | WB |
Decode
Data $$
Photos, illustrations, graphics here.
Other solutions for structural hazards
Fetch
ALU (Execute)
Registers
Inst $$
Decode
Data $$
Photos, illustrations, graphics here.
In the real world… High-level synthesis
Photos, illustrations, graphics here.
Pipelining example
PC
I$
Register
File
s1
s2
d
D$
+
4
PC
PC
A
B
IR
IR
IR
IR
B
O
O
D
add $3,$2,$1
Photos, illustrations, graphics here.
Pipelining example
PC
I$
Register
File
s1
s2
d
D$
+
4
PC
A
B
IR
IR
IR
B
O
O
D
add $3,$2,$1
lw $4,0($5)
PC
IR
Photos, illustrations, graphics here.
Pipelining example
PC
I$
Register
File
s1
s2
d
D$
+
4
IR
IR
B
O
O
D
add $3,$2,$1
lw $4,0($5)
sw $6, 4($7)
PC
PC
A
B
IR
IR
Photos, illustrations, graphics here.
Pipelining example
PC
I$
Register
File
s1
s2
d
D$
+
4
PC
IR
IR
O
D
add $3,$2,$1
lw $4,0($5)
sw $6, 4($7)
PC
A
B
IR
IR
B
O
Photos, illustrations, graphics here.
Pipelining example
PC
I$
Register
File
s1
s2
d
D$
+
4
PC
PC
A
B
IR
IR
add
lw $4,0($5)
sw $6, 4($7)
IR
O
D
IR
B
O
Photos, illustrations, graphics here.
Pipelining example
PC
I$
s1
s2
d
D$
+
4
PC
PC
A
B
IR
IR
IR
IR
B
O
O
D
lw $4
sw $6, 4($7)
Register
File
Photos, illustrations, graphics here.
Pipelining example
PC
I$
Register
File
s1
s2
d
D$
+
4
PC
PC
A
B
IR
IR
IR
IR
B
O
O
D
sw $6
Photos, illustrations, graphics here.
Pipelining example
PC
I$
Register
File
s1
s2
d
D$
+
4
PC
PC
A
B
IR
IR
IR
IR
B
O
O
D
Okay but that isn’t a real program…
Photos, illustrations, graphics here.
Dependent Operations
add $3 <- $2,$1
add $6 <- $5,$4
add $3 <- $2,$1
add $6 <- $5,$3
add $3 <- $2,$1
lw $4 <- 0($3)
addi $6,1,$3
sw $3 -> 0($7)
Photos, illustrations, graphics here.
Data Hazards
add $3,$2,$1
lw $4,0($3)
sw $3,0($7)
addi $6,1,$3
Register
File
S
X
s1
s2
d
IR
A
B
IR
O
B
IR
Data
Mem
a
d
O
D
IR
Photos, illustrations, graphics here.
Data Hazards
add $3,$2,$1
lw $4,0($3)
sw $3,0($7)
addi $6,1,$3
Register
File
S
X
s1
s2
d
IR
A
B
IR
O
B
IR
Data
Mem
a
d
O
D
IR
Photos, illustrations, graphics here.
Data Hazards
add $3,$2,$1
lw $4,0($3)
sw $3,0($7)
addi $6,1,$3
Register
File
S
X
s1
s2
d
IR
A
B
IR
O
B
IR
Data
Mem
a
d
O
D
IR
Photos, illustrations, graphics here.
Data Hazards
add $3,$2,$1
lw $4,0($3)
sw $3,0($7)
addi $6,1,$3
Register
File
S
X
s1
s2
d
IR
A
B
IR
O
B
IR
Data
Mem
a
d
O
D
IR
Photos, illustrations, graphics here.
Memory Hazards
sw $5, 0($1)
lw $4,0($1)
Register
File
S
X
s1
s2
d
IR
A
B
IR
O
B
IR
Data
Mem
a
d
O
D
IR
Photos, illustrations, graphics here.
Memory Hazards
sw $5, 0($1)
lw $4,0($1)
Register
File
S
X
s1
s2
d
IR
A
B
IR
O
B
IR
Data
Mem
a
d
O
D
IR
Photos, illustrations, graphics here.
Observation!
Register
File
S
X
s1
s2
d
IR
A
B
IR
O
B
IR
Data
Mem
a
d
O
D
IR
add $3,$2,$1
lw $4,0($3)
Photos, illustrations, graphics here.
Observation!
Register
File
S
X
s1
s2
d
IR
A
B
IR
O
B
IR
Data
Mem
a
d
O
D
IR
add $3,$2,$1
lw $4,0($3)
Photos, illustrations, graphics here.
Solutions for RAW hazards
Photos, illustrations, graphics here.
Solutions for RAW hazards
Photos, illustrations, graphics here.
Solution: Bypassing/Forwarding
Register
File
S
X
s1
s2
d
IR
A
B
IR
O
B
IR
F/D
D/X
X/M
add $3,$2,$1
lw $4,0($3)
Data
Mem
a
d
O
D
IR
M/W
Photos, illustrations, graphics here.
What about this case?
Register
File
S
X
s1
s2
d
IR
A
B
IR
O
B
IR
F/D
D/X
X/M
add $3,$2,$1
lw $4,0($3)
Data
Mem
a
d
O
D
IR
M/W
Photos, illustrations, graphics here.
What about this case?
Register
File
S
X
s1
s2
d
IR
A
B
IR
O
B
IR
F/D
D/X
X/M
add $3,$2,$1
lw $4,0($3)
Data
Mem
a
d
O
D
IR
M/W
Photos, illustrations, graphics here.
Bypassing for operand B in X stage for ALU
Register
File
S
X
s1
s2
d
IR
A
B
IR
O
B
IR
F/D
D/X
X/M
add $3,$2,$1
add $4,$2,$3
Data
Mem
a
d
O
D
IR
M/W
Photos, illustrations, graphics here.
What about Bypassing/Forward from W to M?
Register
File
S
X
s1
s2
d
Data
Mem
a
d
IR
A
B
IR
O
B
IR
O
D
IR
F/D
D/X
X/M
M/W
Photos, illustrations, graphics here.
What about Bypassing/Forward from W to M?
Register
File
S
X
s1
s2
d
Data
Mem
a
d
IR
A
B
IR
O
B
IR
O
D
IR
F/D
D/X
X/M
M/W
lw $3,0($2)
sw $3,0($4)
Photos, illustrations, graphics here.
Load Use Hazards
Cycle | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
lw R3, 10(R1) | IF | ID | EX | MEM | WB | | | |
Add R4, R3, R5 | | IF | ID | EX | MEM | WB | | |
Add R6, R3, R5 | | | IF | ID | EX | MEM | WB | |
Cycle | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
lw R3, 10(R1) | IF | ID | EX | MEM | WB | | | |
Add R4, R3, R5 | | IF | ID | stall | EX | MEM | WB | |
Add R6, R3, R5 | | | IF | stall | ID | EX | MEM | WB |
Photos, illustrations, graphics here.
Load Use Hazards
Photos, illustrations, graphics here.
Instruction scheduling example
a = b +c;
d = e – f;
LW Rb, b
LW Rc, c
ADD Ra, Rb, Rc
SW a, Ra
LW Re, e
LW Rf, f
SUB Rd, Re, Rf
SW d, Rd
Unoptimized schedule
Photos, illustrations, graphics here.
Instruction scheduling example
a = b +c;
d = e – f;
LW Rb, b LW Rb, b
LW Rc, c LW Rc, c
ADD Ra, Rb, Rc LW Re, e
SW a, Ra ADD Ra, Rb, Rc
LW Re, e LW Rf, f
LW Rf, f SW a, Ra
SUB Rd, Re, Rf SUB Rd, Re, Rf
SW d, Rd SW d, Rd
Unoptimized schedule
Optimized schedule
Photos, illustrations, graphics here.
Shifting gears to Multicycle Operations
Photos, illustrations, graphics here.
Multicycle Operations
Photos, illustrations, graphics here.
Terminology
Functional Unit | Latency | Initiation Interval |
Integer ALU | 0 | 1 |
Data Memory | 1 | 1 |
FP Add | 3 | 1 |
FP Multiply | 6 | 1 |
FP Divide | 24 | 24 |
Photos, illustrations, graphics here.
Multicycle example
ADD R1, R2, R3 | IF | ID | EX | MEM | WB | | | | | |
DIVD F2, F2, F3 | | IF | ID | E1 | E2 | E3 | … | E25 | MEM | WB |
ADDD F10, F2, F8 | | | IF | ID | ------Stall------- | E1 | E2 | |||
Photos, illustrations, graphics here.
Multicycle hazards
Photos, illustrations, graphics here.
Multi-cycle summary
Photos, illustrations, graphics here.
Pipeline Hazards
Photos, illustrations, graphics here.
Instruction level parallelism (ILP)
Photos, illustrations, graphics here.
Control Hazards
If (1 == a) {� b = 5;
…
} else {
b = func(x, y);
}
Photos, illustrations, graphics here.
Control Hazards
If (1 == a) {� b = 5;
…
} else {
b = func(x, y);
}
Photos, illustrations, graphics here.
Control hazards in pipelining
PC
Insn
Mem
Register
File
s1
s2
d
+
4
<<
2
F/D
D/X
X/M
PC
A
B
IR
O
B
IR
PC
IR
S
X
When are branches resolved?
Photos, illustrations, graphics here.
Control hazards in pipelining
PC
Insn
Mem
Register
File
s1
s2
d
+
4
<<
2
F/D
D/X
X/M
PC
A
B
IR
O
B
IR
PC
IR
S
X
Photos, illustrations, graphics here.
Branch recovery: Flushing
+ They haven’t written permanent state (regfile, DMEM)
- Two cycle penalty for taken branches
PC
Insn
Mem
Register
File
s1
s2
d
+
4
<<
2
F/D
D/X
X/M
nop
nop
PC
A
B
IR
O
B
IR
PC
IR
S
X
Photos, illustrations, graphics here.
Branch performance
Photos, illustrations, graphics here.
Why predict? Speculative Execution
Photos, illustrations, graphics here.
Why are branches predictable?
for (i=0; i<1000000; i++) { // Highly biased
if (i % 3 == 0) { // and this one?
// whatever
}
if (random() % 2 == 0) { // how about this one?
…
}
}
Photos, illustrations, graphics here.
Branch Prediction Performance
Photos, illustrations, graphics here.
Branch prediction
PC
Insn
Mem
Register
File
S
X
s1
s2
d
+
4
<<
2
TG
PC
IR
TG
PC
A
B
IR
O
B
IR
F/D
D/X
X/M
nop
nop
BP
<>
Hardware guesses outcome
Start fetching from guessed address
Photos, illustrations, graphics here.
Dynamic hardware branch prediction
Photos, illustrations, graphics here.
Branch direction prediction
Photos, illustrations, graphics here.
Branch direction prediction in fetch stage
Photos, illustrations, graphics here.
Example 1-bit branch prediction
Branch Outcome | T | T | T | N | T | T | T | N | T | T | T | N | % predict rate |
1-bit Prediction | N | | | | | | | | | | | | |
1-bit Mis-Predict? | Y | | | | | | | | | | | | |
| | | | | | | | | | | | | |
| | | | | | | | | | | | | |
Photos, illustrations, graphics here.
Example 1-bit branch prediction
Branch Outcome | T | T | T | N | T | T | T | N | T | T | T | N | % predict rate |
1-bit Prediction | N | T | | | | | | | | | | | |
1-bit Mis-Predict? | Y | | | | | | | | | | | | |
| | | | | | | | | | | | | |
| | | | | | | | | | | | | |
Photos, illustrations, graphics here.
Example 1-bit branch prediction
Branch Outcome | T | T | T | N | T | T | T | N | T | T | T | N | % predict rate |
1-bit Prediction | N | T | T | T | | | | | | | | | |
1-bit Mis-Predict? | Y | | | Y | | | | | | | | | |
| | | | | | | | | | | | | |
| | | | | | | | | | | | | |
Photos, illustrations, graphics here.
Example 1-bit branch prediction
Branch Outcome | T | T | T | N | T | T | T | N | T | T | T | N | % predict rate |
1-bit Prediction | N | T | T | T | N | | | | | | | | |
1-bit Mis-Predict? | Y | | | Y | Y | | | | | | | | |
| | | | | | | | | | | | | |
| | | | | | | | | | | | | |
Photos, illustrations, graphics here.
Example 1-bit branch prediction
Branch Outcome | T | T | T | N | T | T | T | N | T | T | T | N | % predict rate |
1-bit Prediction | N | T | T | T | N | T | T | T | | | | | |
1-bit Mis-Predict? | Y | | | Y | Y | | | Y | | | | | |
| | | | | | | | | | | | | |
| | | | | | | | | | | | | |
Photos, illustrations, graphics here.
Example 1-bit branch prediction
Branch Outcome | T | T | T | N | T | T | T | N | T | T | T | N | % predict rate |
1-bit Prediction | N | T | T | T | N | T | T | T | N | | | | |
1-bit Mis-Predict? | Y | | | Y | Y | | | Y | Y | | | | |
| | | | | | | | | | | | | |
| | | | | | | | | | | | | |
Photos, illustrations, graphics here.
Example 1-bit branch prediction
Branch Outcome | T | T | T | N | T | T | T | N | T | T | T | N | % predict rate |
1-bit Prediction | N | T | T | T | N | T | T | T | N | T | T | T | |
1-bit Mis-Predict? | Y | | | Y | Y | | | Y | Y | | | Y | ~50% |
| | | | | | | | | | | | | |
| | | | | | | | | | | | | |
Photos, illustrations, graphics here.
Two bit saturating counter
Predict Not Taken
Predict Taken
Predict Not Taken
Predict Taken
11
10
01
00
Taken
Taken
Taken
Taken
Not Taken
Not Taken
Not Taken
Not Taken
“strongly
taken”
“strongly
not taken”
Photos, illustrations, graphics here.
Example 2-bit vs. 1-bit branch prediction
Branch Outcome | T | T | T | N | T | T | T | N | T | T | T | N | % predict rate |
1-bit Prediction | N | T | T | T | N | T | T | T | N | T | T | T | |
1-bit Mis-Predict? | Y | | | Y | Y | | | Y | Y | | | Y | ~50% |
2-bit Prediction | n | | | | | | | | | | | | |
2-bit Mis-Predict? | Y | | | | | | | | | | | | |
Photos, illustrations, graphics here.
Example 2-bit vs. 1-bit branch prediction
Branch Outcome | T | T | T | N | T | T | T | N | T | T | T | N | % predict rate |
1-bit Prediction | N | T | T | T | N | T | T | T | N | T | T | T | |
1-bit Mis-Predict? | Y | | | Y | Y | | | Y | Y | | | Y | ~50% |
2-bit Prediction | n | T | T | t | | | | | | | | | |
2-bit Mis-Predict? | Y | | | Y | | | | | | | | | |
Photos, illustrations, graphics here.
Example 2-bit vs. 1-bit branch prediction
Branch Outcome | T | T | T | N | T | T | T | N | T | T | T | N | % predict rate |
1-bit Prediction | N | T | T | T | N | T | T | T | N | T | T | T | |
1-bit Mis-Predict? | Y | | | Y | Y | | | Y | Y | | | Y | ~50% |
2-bit Prediction | n | T | T | t | T | T | T | t | | | | | |
2-bit Mis-Predict? | Y | | | Y | | | | Y | | | | | |
Photos, illustrations, graphics here.
Example 2-bit vs. 1-bit branch prediction
Branch Outcome | T | T | T | N | T | T | T | N | T | T | T | N | % predict rate |
1-bit Prediction | N | T | T | T | N | T | T | T | N | T | T | T | |
1-bit Mis-Predict? | Y | | | Y | Y | | | Y | Y | | | Y | ~50% |
2-bit Prediction | n | T | T | t | T | T | T | t | T | T | T | t | |
2-bit Mis-Predict? | Y | | | Y | | | | Y | | | | Y | ~75% |
Photos, illustrations, graphics here.
Branch prediction buffer (branch history table, BHT)
PC
12-bits
212 = 4K Entries
Taken or
Not-taken?
Photos, illustrations, graphics here.
Correlating predictors
if (aa==2)aa=0;
if (bb==2)bb=0;
if (aa!=bb){..}
// Branch #1
// Branch #2
// Branch #3
Photos, illustrations, graphics here.
Two level global branch prediction
Photos, illustrations, graphics here.
Variations on the basics
PC
12-bits
BHR
XOR
Photos, illustrations, graphics here.
Review: 1-level branch predictor
Photos, illustrations, graphics here.
Review: 2-level Global History Branch Predictor
Photos, illustrations, graphics here.
Review: 2-level Gshare Branch Predictor
Photos, illustrations, graphics here.
Hybrid branch predictors
Local
Predictor
Global
Predictor
Chooser
Predictor
Taken or
Not-taken?
Could be
G-share
Could be
2-bit BHT
Photos, illustrations, graphics here.
Branch Target Prediction
Photos, illustrations, graphics here.
Branch Target Buffer
Photos, illustrations, graphics here.
Branch Target Buffer
Photos, illustrations, graphics here.
Real world: Using ML for Branch Prediction
Photos, illustrations, graphics here.
Real world: Security vulnerabilities with speculation
“if the pattern of memory accesses performed by such speculative execution depends on private data, the resulting state of the data cache constitutes a side channel through which an attacker may be able to extract information about the private data using a timing attack”
Discovered in 2018 and
affects all processors before 2019
Photos, illustrations, graphics here.
Logistics
Photos, illustrations, graphics here.