h4 the processor
TRANSCRIPT
![Page 1: H4 The processor](https://reader030.vdocuments.site/reader030/viewer/2022032610/62394856dc9a79299913d934/html5/thumbnails/1.jpg)
Computer architectureELECTA01
H4The processor
ELECTA01Hogeschool Rotterdam
Opleiding ElektrotechniekMinor Embedded Systems
J.W. Peltenburg / J.Z.M. Broeders ([email protected])
![Page 2: H4 The processor](https://reader030.vdocuments.site/reader030/viewer/2022032610/62394856dc9a79299913d934/html5/thumbnails/2.jpg)
Planning CTA01
• Week 1: Introduction, Performance & Parallelism
• Week 2: Instructions for arithmetic and memory
• Week 3: Instructions for decisions and procedures
• Week 4: Computer arithmetic
• Week 5: Single cycle LEGv8, intro pipelining
• Week 6: Pipelined LEGv8, hazards and forwarding
• Week 7: Memory architecture
• Week 8: Guest lecture (multicore and parallelism)
CTA01 H4 V 2.3 2
![Page 3: H4 The processor](https://reader030.vdocuments.site/reader030/viewer/2022032610/62394856dc9a79299913d934/html5/thumbnails/3.jpg)
COMPUTERORGANIZATION ANDDESIGNThe Hardware/Software Interface
ARM
Edition
Chapter 4
The Processor
![Page 4: H4 The processor](https://reader030.vdocuments.site/reader030/viewer/2022032610/62394856dc9a79299913d934/html5/thumbnails/4.jpg)
Introduction
◼ CPU performance factors◼ Instruction count
◼ Determined by ISA and compiler
◼ CPI and Cycle time◼ Determined by CPU hardware
◼ We will examine two LEGv8 implementations◼ A simplified version
◼ A more realistic pipelined version
◼ Simple subset, shows most aspects◼ Memory reference: LDUR, STUR
◼ Arithmetic/logical: ADD, SUB, AND, ORR
◼ Control transfer: CBZ
§4.1
Intro
ductio
n
CTA01 H4 V 2.34
![Page 5: H4 The processor](https://reader030.vdocuments.site/reader030/viewer/2022032610/62394856dc9a79299913d934/html5/thumbnails/5.jpg)
Instruction Execution
◼ PC → instruction memory, fetch instruction
◼ Register numbers → register file, read registers
◼ Depending on instruction type
◼ Use ALU to calculate
◼ Arithmetic or logic result for R-type (ADD, SUB, AND, ORR)
◼ Memory address for D-type instructions (LDUR, STUR)
◼ Register equals zero for CB-type (CBZ)
◼ For CB-type (CBZ) calculate branch target address
◼ Access data memory for D-type (LDUR, STUR)
◼ Write to register file for R-type (ADD, SUB, AND, ORR)
and load (LDUR)
◼ PC PC + 4 or PC Branch target addressCTA01 H4 V 2.35
In
parallel
In
parallel
![Page 6: H4 The processor](https://reader030.vdocuments.site/reader030/viewer/2022032610/62394856dc9a79299913d934/html5/thumbnails/6.jpg)
CPU Overview
CTA01 H4 V 2.36
![Page 7: H4 The processor](https://reader030.vdocuments.site/reader030/viewer/2022032610/62394856dc9a79299913d934/html5/thumbnails/7.jpg)
Multiplexers
CTA01 H4 V 2.37
◼ Can’t just join
wires together
◼ Use multiplexers
![Page 8: H4 The processor](https://reader030.vdocuments.site/reader030/viewer/2022032610/62394856dc9a79299913d934/html5/thumbnails/8.jpg)
Control
CTA01 H4 V 2.38
![Page 9: H4 The processor](https://reader030.vdocuments.site/reader030/viewer/2022032610/62394856dc9a79299913d934/html5/thumbnails/9.jpg)
Logic Design Basics§4.2
Logic
Desig
n C
onventio
ns
◼ Information encoded in binary
◼ Low voltage = 0, High voltage = 1
◼ One wire per bit
◼ Multi-bit data encoded on multi-wire buses
◼ Combinational element
◼ Operate on data
◼ Output is a function of input
◼ State (sequential) elements
◼ Store information
>>
CTA01 H4 V 2.39
![Page 10: H4 The processor](https://reader030.vdocuments.site/reader030/viewer/2022032610/62394856dc9a79299913d934/html5/thumbnails/10.jpg)
Combinational Elements
◼ AND-gate
◼ Y = A & B
A
BY
I0
I1Y
M
u
x
S
◼ Multiplexer
◼ Y = S ? I1 : I0
A
B
Y+
A
B
YALU
F
◼ Adder
◼ Y = A + B
◼ Arithmetic/Logic Unit
◼ Y = F(A, B)
>>
CTA01 H4 V 2.310
![Page 11: H4 The processor](https://reader030.vdocuments.site/reader030/viewer/2022032610/62394856dc9a79299913d934/html5/thumbnails/11.jpg)
Sequential Elements
◼ Register: stores data in a circuit
◼ Uses a clock signal to determine when to
update the stored value
◼ Edge-triggered: update when Clk changes
from 0 to 1
D
Clk
Q
Clk
D
Q
>>
CTA01 H4 V 2.311
![Page 12: H4 The processor](https://reader030.vdocuments.site/reader030/viewer/2022032610/62394856dc9a79299913d934/html5/thumbnails/12.jpg)
Sequential Elements
◼ Register with write control
◼ Only updates on clock edge when write
control input is 1
◼ Used when stored value is required later
D
Clk
Q
Write
Write
D
Q
Clk
>>
CTA01 H4 V 2.312
![Page 13: H4 The processor](https://reader030.vdocuments.site/reader030/viewer/2022032610/62394856dc9a79299913d934/html5/thumbnails/13.jpg)
Clocking Methodology
◼ Combinational logic transforms data during clock cycles
◼ Between clock edges
◼ Input from state elements, output to state element
◼ Longest delay determines clock period
>>
CTA01 H4 V 2.313
![Page 14: H4 The processor](https://reader030.vdocuments.site/reader030/viewer/2022032610/62394856dc9a79299913d934/html5/thumbnails/14.jpg)
Building a Datapath
◼ Datapath
◼ Elements that process data and addresses
in the CPU
◼ Registers, ALUs, mux’s, memories, …
◼ We will build a LEGv8 datapath
incrementally
◼ Refining the overview design
§4.3
Build
ing a
Data
path
CTA01 H4 V 2.314
![Page 15: H4 The processor](https://reader030.vdocuments.site/reader030/viewer/2022032610/62394856dc9a79299913d934/html5/thumbnails/15.jpg)
Instruction Fetch
64-bit
register
Increment by
4 for next
instruction
CTA01 H4 V 2.315
![Page 16: H4 The processor](https://reader030.vdocuments.site/reader030/viewer/2022032610/62394856dc9a79299913d934/html5/thumbnails/16.jpg)
R-type Instructions ADD, SUB, AND, ORR
◼ Read two register operands
◼ Perform arithmetic/logical operation
◼ Write register result
CTA01 H4 V 2.316
Multiport register file:
2 read ports, 1 write port
![Page 17: H4 The processor](https://reader030.vdocuments.site/reader030/viewer/2022032610/62394856dc9a79299913d934/html5/thumbnails/17.jpg)
◼ Read register operands
◼ Calculate address using 9-bit offset◼ Use ALU, but sign-extend offset
◼ LDUR: Read memory and update register
◼ STUR: Write register value to memory
CTA01 H4 V 2.317
D-type Instructions LDUR, STUR
![Page 18: H4 The processor](https://reader030.vdocuments.site/reader030/viewer/2022032610/62394856dc9a79299913d934/html5/thumbnails/18.jpg)
CB-type Instruction CBZ
◼ Read register operand
◼ Compare operand to zero
◼ Use ALU pass input and check Zero output
◼ Calculate target address
◼ Sign-extend 19-bit displacement field
◼ Shift left 2 places (word displacement)
◼ Add to PC
CTA01 H4 V 2.318
![Page 19: H4 The processor](https://reader030.vdocuments.site/reader030/viewer/2022032610/62394856dc9a79299913d934/html5/thumbnails/19.jpg)
Branch Instructions
Just
re-routes
wires
CTA01 H4 V 2.319
Corrected version of Figure 4.9 page 268.
![Page 20: H4 The processor](https://reader030.vdocuments.site/reader030/viewer/2022032610/62394856dc9a79299913d934/html5/thumbnails/20.jpg)
Composing the Elements
◼ First-cut data path does an instruction in
one clock cycle
◼ Each datapath element can only do one
function at a time
◼ Hence, we need separate instruction and data
memories
◼ Use multiplexers where alternate data
sources are used for different instructions
CTA01 H4 V 2.320
![Page 21: H4 The processor](https://reader030.vdocuments.site/reader030/viewer/2022032610/62394856dc9a79299913d934/html5/thumbnails/21.jpg)
R-Type/D-Type Datapath
CTA01 H4 V 2.321
ADD, SUB, AND, ORR, LDUR, STUR
![Page 22: H4 The processor](https://reader030.vdocuments.site/reader030/viewer/2022032610/62394856dc9a79299913d934/html5/thumbnails/22.jpg)
Full Datapath
CTA01 H4 V 2.322
ADD, SUB, AND, ORR, LDUR, STUR, CBZ
![Page 23: H4 The processor](https://reader030.vdocuments.site/reader030/viewer/2022032610/62394856dc9a79299913d934/html5/thumbnails/23.jpg)
ALU Control
◼ ALU used for
◼ D-type LDUR, STUR: Function = add
◼ CB-type CBZ: Function = pass
◼ R-type ADD, SUB, AND, ORR: Function
depends on opcode
§4.4
A S
imple
Imple
menta
tion S
chem
e
ALU control F = Function
0000 AND
0001 OR
0010 add
0110 subtract
XX11 pass input b
CTA01 H4 V 2.323
See ALU design Figure A.5.12 page A-35
![Page 24: H4 The processor](https://reader030.vdocuments.site/reader030/viewer/2022032610/62394856dc9a79299913d934/html5/thumbnails/24.jpg)
ALU Control
◼ Assume 2-bit ALUOp derived from opcode
◼ Combinational logic derives ALU control
opcode ALUOp Operation Opcode field ALU function
ALU
control
LDUR 00 load register XXXXXXXXXXX add 0010
STUR 00 store register XXXXXXXXXXX add 0010
CBZ 01 compare and
branch on zero
XXXXXXXXXXX pass input b XX11
R-type
ADD, SUB,
AND, ORR
10 add 10001011000 add 0010
subtract 11001011000 subtract 0110
AND 10001010000 AND 0000
OR 10101010000 OR 0001
CTA01 H4 V 2.324
![Page 25: H4 The processor](https://reader030.vdocuments.site/reader030/viewer/2022032610/62394856dc9a79299913d934/html5/thumbnails/25.jpg)
The Main Control Unit
◼ Control signals derived from instruction
CTA01 H4 V 2.325
![Page 26: H4 The processor](https://reader030.vdocuments.site/reader030/viewer/2022032610/62394856dc9a79299913d934/html5/thumbnails/26.jpg)
Datapath With Control
CTA01 H4 V 2.326
![Page 27: H4 The processor](https://reader030.vdocuments.site/reader030/viewer/2022032610/62394856dc9a79299913d934/html5/thumbnails/27.jpg)
Control signals
CTA01 H4 V 2.327
![Page 28: H4 The processor](https://reader030.vdocuments.site/reader030/viewer/2022032610/62394856dc9a79299913d934/html5/thumbnails/28.jpg)
Implementing B-type (B)
◼ Branch uses word offset
◼ PC PC + (sign extended offset) x 4
◼ Need an extra control signal decoded from
opcode
000101 offset
31:26 25:0
B
CTA01 H4 V 2.328
![Page 29: H4 The processor](https://reader030.vdocuments.site/reader030/viewer/2022032610/62394856dc9a79299913d934/html5/thumbnails/29.jpg)
Datapath With B Added
CTA01 H4 V 2.329
![Page 30: H4 The processor](https://reader030.vdocuments.site/reader030/viewer/2022032610/62394856dc9a79299913d934/html5/thumbnails/30.jpg)
Performance Issues
◼ Longest delay determines clock period
◼ Critical path: load instruction
◼ Instruction memory → register file → ALU →
data memory → register file
◼ Not feasible to vary period for different
instructions
◼ Violates design principle
◼ Making the common case fast
◼ We will improve performance by pipelining
CTA01 H4 V 2.330
![Page 31: H4 The processor](https://reader030.vdocuments.site/reader030/viewer/2022032610/62394856dc9a79299913d934/html5/thumbnails/31.jpg)
Pipelining Analogy
◼ Pipelined laundry: overlapping execution
◼ Parallelism improves performance
§4.5
An O
verv
iew
of P
ipelin
ing◼ Four loads:
◼ Speedup
= 8/3.5 = 2.3
◼ Non-stop:
◼ Speedup
= 2n/0.5n + 1.5 ≈ 4
= number of stages
CTA01 H4 V 2.331
Speedup?
![Page 32: H4 The processor](https://reader030.vdocuments.site/reader030/viewer/2022032610/62394856dc9a79299913d934/html5/thumbnails/32.jpg)
LEGv8 Pipeline
◼ 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
CTA01 H4 V 2.332
![Page 33: H4 The processor](https://reader030.vdocuments.site/reader030/viewer/2022032610/62394856dc9a79299913d934/html5/thumbnails/33.jpg)
Pipeline Performance
◼ Assume time for stages is
◼ 100 ps for register read or write
◼ 200 ps for other stages
◼ Compare pipelined datapath with single-cycle
datapath
Instr Instr fetch Register
read
ALU op Memory
access
Register
write
Total time
LDUR 200ps 100 ps 200ps 200ps 100 ps 800ps
STUR 200ps 100 ps 200ps 200ps 700ps
R-format 200ps 100 ps 200ps 100 ps 600ps
CBZ 200ps 100 ps 200ps 500ps
CTA01 H4 V 2.333
![Page 34: H4 The processor](https://reader030.vdocuments.site/reader030/viewer/2022032610/62394856dc9a79299913d934/html5/thumbnails/34.jpg)
Pipeline Performance
Single-cycle (Tc= 800ps)
Pipelined (Tc= 200ps)
CTA01 H4 V 2.334
![Page 35: H4 The processor](https://reader030.vdocuments.site/reader030/viewer/2022032610/62394856dc9a79299913d934/html5/thumbnails/35.jpg)
Pipeline Speedup
◼ 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
CTA01 H4 V 2.335
![Page 36: H4 The processor](https://reader030.vdocuments.site/reader030/viewer/2022032610/62394856dc9a79299913d934/html5/thumbnails/36.jpg)
Pipelining and ISA Design
◼ LEGv8 ISA designed for pipelining
◼ All 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◼ 64-bit Memory access takes only one cycle
CTA01 H4 V 2.336
![Page 37: H4 The processor](https://reader030.vdocuments.site/reader030/viewer/2022032610/62394856dc9a79299913d934/html5/thumbnails/37.jpg)
Hazards
◼ Situations that prevent starting the next instruction in the next cycle
◼ Structure hazards
◼ A required resource is busy
◼ Data hazard
◼ Need to wait for previous instruction to complete its data read/write
◼ Control hazard
◼ Deciding on control action depends on previous instruction
CTA01 H4 V 2.337
![Page 38: H4 The processor](https://reader030.vdocuments.site/reader030/viewer/2022032610/62394856dc9a79299913d934/html5/thumbnails/38.jpg)
Structure Hazards
◼ Conflict for use of a resource
◼ In LEGv8 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 memories
◼ Or separate instruction/data caches
CTA01 H4 V 2.338
![Page 39: H4 The processor](https://reader030.vdocuments.site/reader030/viewer/2022032610/62394856dc9a79299913d934/html5/thumbnails/39.jpg)
Structure Hazards
◼ Conflict for use of a resource
◼ In LEGv8 the registers are used in stages
ID (read) and WB (write)
◼ Assumed time for stages is
◼ 100 ps for register read or write
◼ 200 ps for other stages
◼ Hence, write and read can be combined in
one pipeline slot
◼ write in first 100 ps, read in last 100 ps
CTA01 H4 V 2.339
![Page 40: H4 The processor](https://reader030.vdocuments.site/reader030/viewer/2022032610/62394856dc9a79299913d934/html5/thumbnails/40.jpg)
Data Hazards
◼ An instruction depends on completion of
data access by a previous instruction
◼ ADD X1, X2, X3SUB X4, X1, X5
CTA01 H4 V 2.340
Data can not move backward in time!
![Page 41: H4 The processor](https://reader030.vdocuments.site/reader030/viewer/2022032610/62394856dc9a79299913d934/html5/thumbnails/41.jpg)
Stalling (aka insert bubbles)
◼ wait for the data to be stored in a register
◼ Requires extra control logic
CTA01 H4 V 2.341
Data now moves forward in time!
![Page 42: H4 The processor](https://reader030.vdocuments.site/reader030/viewer/2022032610/62394856dc9a79299913d934/html5/thumbnails/42.jpg)
Forwarding (aka Bypassing)
◼ Use result when it is computed
◼ Don’t wait for it to be stored in a register
◼ Requires extra connections in the datapath
and extra control logic
CTA01 H4 V 2.342
![Page 43: H4 The processor](https://reader030.vdocuments.site/reader030/viewer/2022032610/62394856dc9a79299913d934/html5/thumbnails/43.jpg)
Load-Use Data Hazard
◼ Can’t always avoid stalls by forwarding
◼ If value not computed when needed
◼ Can’t forward backward in time!
CTA01 H4 V 2.343
![Page 44: H4 The processor](https://reader030.vdocuments.site/reader030/viewer/2022032610/62394856dc9a79299913d934/html5/thumbnails/44.jpg)
Load-Use Data Hazard
◼ Can’t avoid stalls by forwarding
◼ Stall can be limited to one cycle by using
forwarding.
CTA01 H4 V 2.344
![Page 45: H4 The processor](https://reader030.vdocuments.site/reader030/viewer/2022032610/62394856dc9a79299913d934/html5/thumbnails/45.jpg)
Code Scheduling to Avoid Stalls
◼ Reorder code to avoid use of load result in
the next instruction
◼ C code for A[3]=A[0]+A[1]; A[4]=A[0]+A[2];
LDUR X1, [X0,#0]
LDUR X2, [X0,#8]
ADD X3, X1, X2
STUR X3, [X0,#24]
LDUR X4, [X0,#16]
ADD X5, X1, X4
STUR X5, [X0,#32]
stall
stall
LDUR X1, [X0,#0]
LDUR X2, [X0,#8]
LDUR X4, [X0,#16]
ADD X3, X1, X2
STUR X3, [X0,#24]
ADD X5, X1, X4
STUR X5, [X0,#32]
11 cycles13 cycles
CTA01 H4 V 2.345
![Page 46: H4 The processor](https://reader030.vdocuments.site/reader030/viewer/2022032610/62394856dc9a79299913d934/html5/thumbnails/46.jpg)
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 LEGv8 pipeline
◼ Need to compare registers and compute target early in the pipeline
◼ Add hardware to do it in ID stage
CTA01 H4 V 2.346
![Page 47: H4 The processor](https://reader030.vdocuments.site/reader030/viewer/2022032610/62394856dc9a79299913d934/html5/thumbnails/47.jpg)
Stall on Branch
◼ Wait until branch outcome determined
before fetching next instruction
CTA01 H4 V 2.347
![Page 48: H4 The processor](https://reader030.vdocuments.site/reader030/viewer/2022032610/62394856dc9a79299913d934/html5/thumbnails/48.jpg)
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 LEGv8 pipeline
◼ Can predict branches not taken
◼ Fetch instruction after branch, with no delay
CTA01 H4 V 2.348
![Page 49: H4 The processor](https://reader030.vdocuments.site/reader030/viewer/2022032610/62394856dc9a79299913d934/html5/thumbnails/49.jpg)
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
CTA01 H4 V 2.349
![Page 50: H4 The processor](https://reader030.vdocuments.site/reader030/viewer/2022032610/62394856dc9a79299913d934/html5/thumbnails/50.jpg)
Pipeline Summary
◼ 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
CTA01 H4 V 2.350
![Page 51: H4 The processor](https://reader030.vdocuments.site/reader030/viewer/2022032610/62394856dc9a79299913d934/html5/thumbnails/51.jpg)
Planning CTA01
• Week 1: Introduction, Performance & Parallelism
• Week 2: Instructions for arithmetic and memory
• Week 3: Instructions for decisions and procedures
• Week 4: Computer arithmetic
• Week 5: Single cycle LEGv8, intro pipelining
• Week 6: Pipelined LEGv8, hazards and forwarding
• Week 7: Memory architecture
• Week 8: Guest lecture (multicore and parallelism)
CTA01 H4 V 2.3 51
![Page 52: H4 The processor](https://reader030.vdocuments.site/reader030/viewer/2022032610/62394856dc9a79299913d934/html5/thumbnails/52.jpg)
LEGv8 Pipelined Datapath§4.6
Pip
elin
ed D
ata
path
and C
ontro
l
WB
MEM
Right-to-left
flow leads to
hazards
CTA01 H4 V 2.352
![Page 53: H4 The processor](https://reader030.vdocuments.site/reader030/viewer/2022032610/62394856dc9a79299913d934/html5/thumbnails/53.jpg)
Pipeline registers
◼ Need registers between stages
◼ To hold information produced in previous cycle
CTA01 H4 V 2.353
![Page 54: H4 The processor](https://reader030.vdocuments.site/reader030/viewer/2022032610/62394856dc9a79299913d934/html5/thumbnails/54.jpg)
Pipeline Operation
◼ 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
◼ “Multi-clock-cycle” diagram
◼ Graph of operation over time
◼ We’ll look at “single-clock-cycle” diagrams
for load & store
CTA01 H4 V 2.354
![Page 55: H4 The processor](https://reader030.vdocuments.site/reader030/viewer/2022032610/62394856dc9a79299913d934/html5/thumbnails/55.jpg)
IF for Load, Store, …
CTA01 H4 V 2.355
![Page 56: H4 The processor](https://reader030.vdocuments.site/reader030/viewer/2022032610/62394856dc9a79299913d934/html5/thumbnails/56.jpg)
ID for Load, Store, …
CTA01 H4 V 2.356
![Page 57: H4 The processor](https://reader030.vdocuments.site/reader030/viewer/2022032610/62394856dc9a79299913d934/html5/thumbnails/57.jpg)
EX for Load
CTA01 H4 V 2.357
![Page 58: H4 The processor](https://reader030.vdocuments.site/reader030/viewer/2022032610/62394856dc9a79299913d934/html5/thumbnails/58.jpg)
MEM for Load
CTA01 H4 V 2.358
![Page 59: H4 The processor](https://reader030.vdocuments.site/reader030/viewer/2022032610/62394856dc9a79299913d934/html5/thumbnails/59.jpg)
WB for Load
Wrong
register
number
CTA01 H4 V 2.359
![Page 60: H4 The processor](https://reader030.vdocuments.site/reader030/viewer/2022032610/62394856dc9a79299913d934/html5/thumbnails/60.jpg)
Corrected Datapath for Load
CTA01 H4 V 2.360
![Page 61: H4 The processor](https://reader030.vdocuments.site/reader030/viewer/2022032610/62394856dc9a79299913d934/html5/thumbnails/61.jpg)
EX for Store
CTA01 H4 V 2.361
![Page 62: H4 The processor](https://reader030.vdocuments.site/reader030/viewer/2022032610/62394856dc9a79299913d934/html5/thumbnails/62.jpg)
MEM for Store
CTA01 H4 V 2.362
![Page 63: H4 The processor](https://reader030.vdocuments.site/reader030/viewer/2022032610/62394856dc9a79299913d934/html5/thumbnails/63.jpg)
WB for Store
CTA01 H4 V 2.363
![Page 64: H4 The processor](https://reader030.vdocuments.site/reader030/viewer/2022032610/62394856dc9a79299913d934/html5/thumbnails/64.jpg)
Multi-Cycle Pipeline Diagram
◼ Form showing resource usage
CTA01 H4 V 2.364
![Page 65: H4 The processor](https://reader030.vdocuments.site/reader030/viewer/2022032610/62394856dc9a79299913d934/html5/thumbnails/65.jpg)
Multi-Cycle Pipeline Diagram
◼ Traditional form
CTA01 H4 V 2.365
![Page 66: H4 The processor](https://reader030.vdocuments.site/reader030/viewer/2022032610/62394856dc9a79299913d934/html5/thumbnails/66.jpg)
Single-Cycle Pipeline Diagram
◼ State of pipeline in a given cycle, e.g. CC 5
CTA01 H4 V 2.366
![Page 67: H4 The processor](https://reader030.vdocuments.site/reader030/viewer/2022032610/62394856dc9a79299913d934/html5/thumbnails/67.jpg)
Pipelined Control (Simplified)
CTA01 H4 V 2.367
![Page 68: H4 The processor](https://reader030.vdocuments.site/reader030/viewer/2022032610/62394856dc9a79299913d934/html5/thumbnails/68.jpg)
Pipelined Control
◼ Control signals derived from instruction
◼ As in single-cycle implementation
CTA01 H4 V 2.368
![Page 69: H4 The processor](https://reader030.vdocuments.site/reader030/viewer/2022032610/62394856dc9a79299913d934/html5/thumbnails/69.jpg)
Pipelined Control
CTA01 H4 V 2.369
![Page 70: H4 The processor](https://reader030.vdocuments.site/reader030/viewer/2022032610/62394856dc9a79299913d934/html5/thumbnails/70.jpg)
Data Hazards in ALU Instructions
◼ Consider this sequence:
SUB X2,X1,X3AND X12,X2,X5OR X13,X6,X2ADD X14,X2,X2STUR X15,[X2,#100]
◼ We can resolve hazards with forwarding
◼ How do we detect when to forward?
§4.7
Data
Hazard
s: F
orw
ard
ing v
s. S
tallin
g
CTA01 H4 V 2.370
![Page 71: H4 The processor](https://reader030.vdocuments.site/reader030/viewer/2022032610/62394856dc9a79299913d934/html5/thumbnails/71.jpg)
Dependencies & Forwarding
CTA01 H4 V 2.371
![Page 72: H4 The processor](https://reader030.vdocuments.site/reader030/viewer/2022032610/62394856dc9a79299913d934/html5/thumbnails/72.jpg)
Detecting the Need to Forward
◼ Pass register numbers along pipeline◼ e.g., ID/EX.RegisterRs = register number for Rs
sitting in ID/EX pipeline register
◼ ALU operand register numbers in EX stage are given by◼ ID/EX.RegisterRn1, ID/EX.RegisterRm2
◼ Data hazards when1a. EX/MEM.RegisterRd = ID/EX.RegisterRn1
1b. EX/MEM.RegisterRd = ID/EX.RegisterRm2
2a. MEM/WB.RegisterRd = ID/EX.RegisterRn1
2b. MEM/WB.RegisterRd = ID/EX.RegisterRm2
Fwd from
EX/MEM
pipeline reg
Fwd from
MEM/WB
pipeline reg
CTA01 H4 V 2.372
![Page 73: H4 The processor](https://reader030.vdocuments.site/reader030/viewer/2022032610/62394856dc9a79299913d934/html5/thumbnails/73.jpg)
Detecting the Need to Forward
◼ 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
XZR
◼ EX/MEM.RegisterRd ≠ 31,
MEM/WB.RegisterRd ≠ 31
CTA01 H4 V 2.373
![Page 74: H4 The processor](https://reader030.vdocuments.site/reader030/viewer/2022032610/62394856dc9a79299913d934/html5/thumbnails/74.jpg)
Forwarding Paths
CTA01 H4 V 2.374
![Page 75: H4 The processor](https://reader030.vdocuments.site/reader030/viewer/2022032610/62394856dc9a79299913d934/html5/thumbnails/75.jpg)
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.
CTA01 H4 V 2.375
![Page 76: H4 The processor](https://reader030.vdocuments.site/reader030/viewer/2022032610/62394856dc9a79299913d934/html5/thumbnails/76.jpg)
Forwarding Condition EX hazard
◼ EX hazard
◼ if (EX/MEM.RegWrite
and (EX/MEM.RegisterRd ≠ 31)
and (EX/MEM.RegisterRd = ID/EX.RegisterRn1)) ForwardA = 10
◼ if (EX/MEM.RegWrite
and (EX/MEM.RegisterRd ≠ 31)
and (EX/MEM.RegisterRd = ID/EX.RegisterRm2)) ForwardB = 10
CTA01 H4 V 2.376
![Page 77: H4 The processor](https://reader030.vdocuments.site/reader030/viewer/2022032610/62394856dc9a79299913d934/html5/thumbnails/77.jpg)
Forwarding Condition MEM hazard
◼ MEM hazard
◼ if (MEM/WB.RegWrite
and (MEM/WB.RegisterRd ≠ 31)
and (MEM/WB.RegisterRd = ID/EX.RegisterRn1)) ForwardA = 01
◼ if (MEM/WB.RegWrite
and (MEM/WB.RegisterRd ≠ 31)
and (MEM/WB.RegisterRd = ID/EX.RegisterRm2)) ForwardB = 01
CTA01 H4 V 2.377
Dit is nog niet helemaal correct!
Zie volgende slide.
![Page 78: H4 The processor](https://reader030.vdocuments.site/reader030/viewer/2022032610/62394856dc9a79299913d934/html5/thumbnails/78.jpg)
Double Data Hazard
◼ Consider the sequence:
add X1,X1,X2add X1,X1,X3add X1,X1,X4
◼ Both hazards occur
◼ Want to use the most recent
◼ Revise MEM hazard condition
◼ Only fwd if EX hazard condition isn’t true
CTA01 H4 V 2.378
![Page 79: H4 The processor](https://reader030.vdocuments.site/reader030/viewer/2022032610/62394856dc9a79299913d934/html5/thumbnails/79.jpg)
Revised Forwarding Condition
◼ MEM hazard
◼ if (MEM/WB.RegWrite
and (MEM/WB.RegisterRd ≠ 31)
and not(EX/MEM.RegWrite and (EX/MEM.RegisterRd ≠ 31)
and (EX/MEM.RegisterRd = ID/EX.RegisterRn1))
and (MEM/WB.RegisterRd = ID/EX.RegisterRn1)) ForwardA = 01
◼ if (MEM/WB.RegWrite
and (MEM/WB.RegisterRd ≠ 31)
and not(EX/MEM.RegWrite and (EX/MEM.RegisterRd ≠ 31)
and (EX/MEM.RegisterRd = ID/EX.RegisterRm2))
and (MEM/WB.RegisterRd = ID/EX.RegisterRm2)) ForwardB = 01
CTA01 H4 V 2.379
![Page 80: H4 The processor](https://reader030.vdocuments.site/reader030/viewer/2022032610/62394856dc9a79299913d934/html5/thumbnails/80.jpg)
Datapath with Forwarding
CTA01 H4 V 2.380
![Page 81: H4 The processor](https://reader030.vdocuments.site/reader030/viewer/2022032610/62394856dc9a79299913d934/html5/thumbnails/81.jpg)
Datapath Detail
CTA01 H4 V 2.381
![Page 82: H4 The processor](https://reader030.vdocuments.site/reader030/viewer/2022032610/62394856dc9a79299913d934/html5/thumbnails/82.jpg)
Load-Use Hazard Detection
◼ Check when using instruction is decoded in ID stage
◼ ALU operand register numbers in ID stage are given by
◼ IF/ID.RegisterRn1, IF/ID.RegisterRm2
◼ Load-use hazard when
◼ ID/EX.MemRead and((ID/EX.RegisterRd = IF/ID.RegisterRn1) or(ID/EX.RegisterRd = IF/ID.RegisterRm2))
◼ If detected, stall and insert bubble
CTA01 H4 V 2.382
![Page 83: H4 The processor](https://reader030.vdocuments.site/reader030/viewer/2022032610/62394856dc9a79299913d934/html5/thumbnails/83.jpg)
How to Stall the Pipeline
◼ Force control values in ID/EX register
to 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
LDUR
◼ Can subsequently forward to EX stage
CTA01 H4 V 2.383
![Page 84: H4 The processor](https://reader030.vdocuments.site/reader030/viewer/2022032610/62394856dc9a79299913d934/html5/thumbnails/84.jpg)
Load-Use Data Hazard
Stall inserted
here
CTA01 H4 V 2.384
![Page 85: H4 The processor](https://reader030.vdocuments.site/reader030/viewer/2022032610/62394856dc9a79299913d934/html5/thumbnails/85.jpg)
Datapath with Hazard Detection
CTA01 H4 V 2.385
![Page 86: H4 The processor](https://reader030.vdocuments.site/reader030/viewer/2022032610/62394856dc9a79299913d934/html5/thumbnails/86.jpg)
Stalls and Performance
◼ 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
CTA01 H4 V 2.386
![Page 87: H4 The processor](https://reader030.vdocuments.site/reader030/viewer/2022032610/62394856dc9a79299913d934/html5/thumbnails/87.jpg)
Branch Hazards
◼ If branch outcome determined in MEM
§4.8
Contro
l Hazard
s
PC
Flush these
instructions
(Set control
values to 0)
CTA01 H4 V 2.387
![Page 88: H4 The processor](https://reader030.vdocuments.site/reader030/viewer/2022032610/62394856dc9a79299913d934/html5/thumbnails/88.jpg)
Reducing Branch Delay
◼ Move hardware to determine outcome to ID
stage
◼ Target address adder
◼ Register comparator
◼ Example: branch taken36: SUB X10, X4, X840: CBZ X1, X3, 844: AND X12, X2, X548: ORR X13, X2, X652: ADD X14, X4, X256: SUB X15, X6, X7
...72: LDUR X4, [X7,#50]
CTA01 H4 V 2.388
![Page 89: H4 The processor](https://reader030.vdocuments.site/reader030/viewer/2022032610/62394856dc9a79299913d934/html5/thumbnails/89.jpg)
Example: Branch Taken
CTA01 H4 V 2.389
![Page 90: H4 The processor](https://reader030.vdocuments.site/reader030/viewer/2022032610/62394856dc9a79299913d934/html5/thumbnails/90.jpg)
Example: Branch Taken
CTA01 H4 V 2.390
![Page 91: H4 The processor](https://reader030.vdocuments.site/reader030/viewer/2022032610/62394856dc9a79299913d934/html5/thumbnails/91.jpg)
Dynamic Branch Prediction
◼ In deeper and superscalar pipelines, branch
penalty is more significant
◼ Use dynamic prediction
◼ Branch prediction buffer (aka branch history table)
◼ Indexed by recent branch instruction addresses
◼ Stores outcome (taken/not taken)
◼ To execute a branch
◼ Check table, expect the same outcome
◼ Start fetching from fall-through or target
◼ If wrong, flush pipeline and flip prediction
CTA01 H4 V 2.391
![Page 92: H4 The processor](https://reader030.vdocuments.site/reader030/viewer/2022032610/62394856dc9a79299913d934/html5/thumbnails/92.jpg)
1-Bit Predictor: Shortcoming
◼ Inner loop branches mispredicted twice!
outer: ……
inner: ……CBZ …, …, inner…CBZ …, …, outer
◼ Mispredict as taken on last iteration of
inner loop
◼ Then mispredict as not taken on first
iteration of inner loop next time around
CTA01 H4 V 2.392
![Page 93: H4 The processor](https://reader030.vdocuments.site/reader030/viewer/2022032610/62394856dc9a79299913d934/html5/thumbnails/93.jpg)
2-Bit Predictor
◼ Only change prediction on two successive
mispredictions
CTA01 H4 V 2.393
![Page 94: H4 The processor](https://reader030.vdocuments.site/reader030/viewer/2022032610/62394856dc9a79299913d934/html5/thumbnails/94.jpg)
Calculating the Branch Target
◼ Even with predictor, still need to calculate
the target address
◼ 1-cycle penalty for a taken branch
◼ Branch target buffer
◼ Cache of target addresses
◼ Indexed by PC when instruction fetched
◼ If hit and instruction is branch predicted taken, can
fetch target immediately
CTA01 H4 V 2.394
![Page 95: H4 The processor](https://reader030.vdocuments.site/reader030/viewer/2022032610/62394856dc9a79299913d934/html5/thumbnails/95.jpg)
Fallacies
◼ Pipelining is easy (!)
◼ The basic idea is easy
◼ The devil is in the details
◼ e.g., detecting data hazards
◼ Pipelining is independent of technology
◼ So why haven’t we always done pipelining?
◼ More transistors make more advanced techniques
feasible
◼ Pipeline-related ISA design needs to take account of
technology trends
◼ e.g., predicated instructions
§4.1
4 F
alla
cie
s a
nd P
itfalls
CTA01 H4 V 2.395
![Page 96: H4 The processor](https://reader030.vdocuments.site/reader030/viewer/2022032610/62394856dc9a79299913d934/html5/thumbnails/96.jpg)
Concluding Remarks
◼ ISA influences design of datapath and control
◼ Datapath and control influence design of ISA
◼ Pipelining improves instruction throughput
using parallelism
◼ More instructions completed per second
◼ Latency for each instruction not reduced
◼ Hazards: structural, data, control
§4.1
4 C
onclu
din
g R
em
ark
s
CTA01 H4 V 2.396