The Processor: CPU Performance Factors

Download as pdf or txt
Download as pdf or txt
You are on page 1of 66

COMPUTER ORGANIZATION AND DESIGN ARM

Edition
The Hardware/Software Interface

Chapter 4
The Processor

§4.1 Introduction
Introduction
n CPU performance factors
n Instruction count
n Determined by ISA and compiler
n CPI and Cycle time
n Determined by CPU hardware
n We will examine two LEGv8 implementations
n A simplified version
n A more realistic pipelined version
n Simple subset, shows most aspects
n Memory reference: LDUR, STUR
n Arithmetic/logical: add, sub, and, or, slt
n Control transfer: beq, j

Chapter 4 — The Processor — 2


Instruction Execution
n PC ® instruction memory, fetch instruction
n Register numbers ® register file, read registers
n Depending on instruction class
n Use ALU to calculate
n Arithmetic result
n Memory address for load/store
n Branch target address
n Access data memory for load/store
n PC ¬ target address or PC + 4

Chapter 4 — The Processor — 3

CPU Overview

Chapter 4 — The Processor — 4


Multiplexers
n Can’t just join
wires together
n Use multiplexers

Chapter 4 — The Processor — 5

Control

Chapter 4 — The Processor — 6


§4.2 Logic Design Conventions
Logic Design Basics
n Information encoded in binary
n Low voltage = 0, High voltage = 1
n One wire per bit
n Multi-bit data encoded on multi-wire buses
n Combinational element
n Operate on data
n Output is a function of input
n State (sequential) elements
n Store information

Chapter 4 — The Processor — 7

Combinational Elements

n AND-gate n Adder A
Y
+
n Y=A&B n Y=A+B B

A
Y
B

n Arithmetic/Logic Unit
n Multiplexer n Y = F(A, B)
n Y = S ? I1 : I0
A
I0 M
u Y ALU Y
I1 x
B
S F

Chapter 4 — The Processor — 8


Sequential Elements
n Register: stores data in a circuit
n Uses a clock signal to determine when to
update the stored value
n Edge-triggered: update when Clk changes
from 0 to 1

Clk
D Q
D

Clk
Q

Chapter 4 — The Processor — 9

Sequential Elements
n Register with write control
n Only updates on clock edge when write
control input is 1
n Used when stored value is required later

Clk

D Q Write

Write D
Clk
Q

Chapter 4 — The Processor — 10


Clocking Methodology
n Combinational logic transforms data during
clock cycles
n Between clock edges
n Input from state elements, output to state
element
n Longest delay determines clock period

Chapter 4 — The Processor — 11

§4.3 Building a Datapath


Building a Datapath
n Datapath
n Elements that process data and addresses
in the CPU
n Registers, ALUs, mux’s, memories, …
n We will build a LEGv8 datapath
incrementally
n Refining the overview design

Chapter 4 — The Processor — 12


Instruction Fetch

Increment by
4 for next
32-bit instruction
register

Chapter 4 — The Processor — 13

R-Format Instructions
n Read two register operands
n Perform arithmetic/logical operation
n Write register result

Chapter 4 — The Processor — 14


Load/Store Instructions
n Read register operands
n Calculate address using 16-bit offset
n Use ALU, but sign-extend offset
n Load: Read memory and update register
n Store: Write register value to memory

Chapter 4 — The Processor — 15

Branch Instructions
n Read register operands
n Compare operands
n Use ALU, subtract and check Zero output
n Calculate target address
n Sign-extend displacement
n Shift left 2 places (word displacement)
n Add to PC + 4
n Already calculated by instruction fetch

Chapter 4 — The Processor — 16


Branch Instructions
Just
re-routes
wires

Sign-bit wire
replicated

Chapter 4 — The Processor — 17

Composing the Elements


n First-cut data path does an instruction in
one clock cycle
n Each datapath element can only do one
function at a time
n Hence, we need separate instruction and data
memories
n Use multiplexers where alternate data
sources are used for different instructions

Chapter 4 — The Processor — 18


R-Type/Load/Store Datapath

Chapter 4 — The Processor — 19

Full Datapath

Chapter 4 — The Processor — 20


§4.4 A Simple Implementation Scheme
ALU Control
n ALU used for
n Load/Store: F = add
n Branch: F = subtract
n R-type: F depends on opcode
ALU control Function
0000 AND
0001 OR
0010 add
0110 subtract
0111 pass input b
1100 NOR

Chapter 4 — The Processor — 21

ALU Control
n Assume 2-bit ALUOp derived from opcode
n Combinational logic derives ALU control

ALU
opcode ALUOp Operation Opcode field ALU function control
LDUR 00 load register XXXXXXXXXXX add 0010
STUR 00 store register XXXXXXXXXXX add 0010
CBZ 01 compare and XXXXXXXXXXX pass input b 0111
branch on zero
R-type 10 add 100000 add 0010
subtract 100010 subtract 0110
AND 100100 AND 0000
ORR 100101 OR 0001

Chapter 4 — The Processor — 22


The Main Control Unit
n Control signals derived from instruction

Chapter 4 — The Processor — 23

Datapath With Control

Chapter 4 — The Processor — 24


R-Type Instruction

Chapter 4 — The Processor — 25

Load Instruction

Chapter 4 — The Processor — 26


CBZ Instruction

Chapter 4 — The Processor — 27

Implementing Uncnd’l Branch


Jump 2 address
31:26 25:0

n Jump uses word address


n Update PC with concatenation of
n Top 4 bits of old PC
n 26-bit jump address
n 00
n Need an extra control signal decoded from
opcode
Chapter 4 — The Processor — 28
Datapath With B Added

Chapter 4 — The Processor — 29

Performance Issues
n Longest delay determines clock period
n Critical path: load instruction
n Instruction memory ® register file ® ALU ®
data memory ® register file
n Not feasible to vary period for different
instructions
n Violates design principle
n Making the common case fast
n We will improve performance by pipelining

Chapter 4 — The Processor — 30


§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 — 31

LEGv8 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 — 32


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

Instr Instr fetch Register ALU op Memory Register Total time


read access write
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

Chapter 4 — The Processor — 33

Pipeline Performance
Single-cycle (Tc= 800ps)

Pipelined (Tc= 200ps)

Chapter 4 — The Processor — 34


Pipeline Speedup
n If all stages are balanced
n i.e., all take the same time
n Time between instructionspipelined
= Time between instructionsnonpipelined
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 — 35

Pipelining and ISA Design


n LEGv8 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
n Alignment of memory operands
n Memory access takes only one cycle

Chapter 4 — The Processor — 36


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 — 37

Structure Hazards
n Conflict for use of a resource
n In LEGv8 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 — 38


Data Hazards
n An instruction depends on completion of
data access by a previous instruction
n ADD X19, X0, X1
SUB X2, X19, X3

Chapter 4 — The Processor — 39

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

Chapter 4 — The Processor — 40


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 — 41

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;

LDUR X1, [X0,#0] LDUR X1, [X0,#0]


LDUR X2, [X0,#8] LDUR X2, [X0,#8]
stall ADD X3, X1, X2 LDUR X4, [X0,#16]
STUR X3, [X0,#24] ADD X3, X1, X2
LDUR X4, [X0,#16] STUR X3, [X0,#24]
stall ADD X5, X1, X4 ADD X5, X1, X4
STUR X5, [X0,#32] STUR X5, [X0,#32]
13 cycles 11 cycles

Chapter 4 — The Processor — 42


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 LEGv8 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 — 43

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

Chapter 4 — The Processor — 44


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 LEGv8 pipeline
n Can predict branches not taken
n Fetch instruction after branch, with no delay

Chapter 4 — The Processor — 45

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 — 46


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 — 47

§4.6 Pipelined Datapath and Control


LEGv8 Pipelined Datapath

MEM

Right-to-left WB
flow leads to
hazards

Chapter 4 — The Processor — 48


Pipeline registers
n Need registers between stages
n To hold information produced in previous cycle

Chapter 4 — The Processor — 49

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 — 50


IF for Load, Store, …

Chapter 4 — The Processor — 51

ID for Load, Store, …

Chapter 4 — The Processor — 52


EX for Load

Chapter 4 — The Processor — 53

MEM for Load

Chapter 4 — The Processor — 54


WB for Load

Wrong
register
number

Chapter 4 — The Processor — 55

Corrected Datapath for Load

Chapter 4 — The Processor — 56


EX for Store

Chapter 4 — The Processor — 57

MEM for Store

Chapter 4 — The Processor — 58


WB for Store

Chapter 4 — The Processor — 59

Multi-Cycle Pipeline Diagram


n Form showing resource usage

Chapter 4 — The Processor — 60


Multi-Cycle Pipeline Diagram
n Traditional form

Chapter 4 — The Processor — 61

Single-Cycle Pipeline Diagram


n State of pipeline in a given cycle

Chapter 4 — The Processor — 62


Pipelined Control (Simplified)

Chapter 4 — The Processor — 63

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

Chapter 4 — The Processor — 64


Pipelined Control

Chapter 4 — The Processor — 65

§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
STUR X15,[X2,#100]

n We can resolve hazards with forwarding


n How do we detect when to forward?

Chapter 4 — The Processor — 66


Dependencies & Forwarding

Chapter 4 — The Processor — 67

Detecting the Need to Forward


n Pass register numbers along pipeline
n e.g., ID/EX.RegisterRs = register number for Rs
sitting in ID/EX pipeline register
n ALU operand register numbers in EX stage
are given by
n ID/EX.RegisterRn1, ID/EX.RegisterRm2
n Data hazards when
Fwd from
1a. EX/MEM.RegisterRd = ID/EX.RegisterRn1 EX/MEM
pipeline reg
1b. EX/MEM.RegisterRd = ID/EX.RegisterRm2
2a. MEM/WB.RegisterRd = ID/EX.RegisterRn1 Fwd from
MEM/WB
2b. MEM/WB.RegisterRd = ID/EX.RegisterRm2 pipeline reg

Chapter 4 — The Processor — 68


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


XZR
n EX/MEM.RegisterRd ≠ 31,
MEM/WB.RegisterRd ≠ 31

Chapter 4 — The Processor — 69

Forwarding Paths

Chapter 4 — The Processor — 70


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 — 71

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 fwd if EX hazard condition isn’t true

Chapter 4 — The Processor — 72


Revised Forwarding Condition
n MEM hazard
n 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
n 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

Chapter 4 — The Processor — 73

Datapath with Forwarding

Chapter 4 — The Processor — 74


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.RegisterRn1, IF/ID.RegisterRm2
n Load-use hazard when
n ID/EX.MemRead and
((ID/EX.RegisterRd = IF/ID.RegisterRn1) or
(ID/EX.RegisterRd = IF/ID.RegisterRm1))
n If detected, stall and insert bubble

Chapter 4 — The Processor — 75

How to Stall the Pipeline


n Force control values in ID/EX register
to 0
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
LDUI
n Can subsequently forward to EX stage

Chapter 4 — The Processor — 76


Load-Use Data Hazard

Stall inserted
here

Chapter 4 — The Processor — 77

Datapath with Hazard Detection

Chapter 4 — The Processor — 78


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 — 79

§4.8 Control Hazards


Branch Hazards
n If branch outcome determined in MEM

Flush these
instructions
(Set control
values to 0)

PC

Chapter 4 — The Processor — 80


Reducing Branch Delay
n Move hardware to determine outcome to ID
stage
n Target address adder
n Register comparator
n Example: branch taken
36: SUB X10, X4, X8
40: CBZ X1, X3, 8
44: AND X12, X2, X5
48: ORR X13, X2, X6
52: ADD X14, X4, X2
56: SUB X15, X6, X7
...
72: LDUR X4, [X7,#50]

Chapter 4 — The Processor — 81

Example: Branch Taken

Chapter 4 — The Processor — 82


Example: Branch Taken

Chapter 4 — The Processor — 83

Dynamic Branch Prediction


n In deeper and superscalar pipelines, branch
penalty is more significant
n Use dynamic prediction
n Branch prediction buffer (aka branch history table)
n Indexed by recent branch instruction addresses
n Stores outcome (taken/not taken)
n To execute a branch
n Check table, expect the same outcome
n Start fetching from fall-through or target
n If wrong, flush pipeline and flip prediction

Chapter 4 — The Processor — 84


1-Bit Predictor: Shortcoming
n Inner loop branches mispredicted twice!
outer: …

inner: …

CBZ …, …, inner

CBZ …, …, outer

n Mispredict as taken on last iteration of


inner loop
n Then mispredict as not taken on first
iteration of inner loop next time around
Chapter 4 — The Processor — 85

2-Bit Predictor
n Only change prediction on two successive
mispredictions

Chapter 4 — The Processor — 86


Calculating the Branch Target
n Even with predictor, still need to calculate
the target address
n 1-cycle penalty for a taken branch
n Branch target buffer
n Cache of target addresses
n Indexed by PC when instruction fetched
n If hit and instruction is branch predicted taken, can
fetch target immediately

Chapter 4 — The Processor — 87

§4.9 Exceptions
Exceptions and Interrupts
n “Unexpected” events requiring change
in flow of control
n Different ISAs use the terms differently
n Exception
n Arises within the CPU
n e.g., undefined opcode, overflow, syscall, …
n Interrupt
n From an external I/O controller
n Dealing with them without sacrificing
performance is hard

Chapter 4 — The Processor — 88


Handling Exceptions
n Save PC of offending (or interrupted) instruction
n In LEGv8: Exception Link Register (ELR)

n Save indication of the problem


n In LEGv8: Exception Syndrome Rregister (ESR)
n We’ll assume 1-bit
n 0 for undefined opcode, 1 for overflow

Chapter 4 — The Processor — 89

An Alternate Mechanism
n Vectored Interrupts
n Handler address determined by the cause
n Exception vector address to be added to a
vector table base register:
n Unknown Reason: 00 0000two
n Overflow: 10 1100two
n …: 11 1111two
n Instructions either
n Deal with the interrupt, or
n Jump to real handler

Chapter 4 — The Processor — 90


Handler Actions
n Read cause, and transfer to relevant
handler
n Determine action required
n If restartable
n Take corrective action
n use EPC to return to program
n Otherwise
n Terminate program
n Report error using EPC, cause, …

Chapter 4 — The Processor — 91

Exceptions in a Pipeline
n Another form of control hazard
n Consider overflow on add in EX stage
ADD X1, X2, X1
n Prevent X1 from being clobbered

n Complete previous instructions

n Flush add and subsequent instructions

n Set ESR and ELR register values

n Transfer control to handler

n Similar to mispredicted branch


n Use much of the same hardware

Chapter 4 — The Processor — 92


Pipeline with Exceptions

Chapter 4 — The Processor — 93

Exception Properties
n Restartable exceptions
n Pipeline can flush the instruction
n Handler executes, then returns to the
instruction
n Refetched and executed from scratch
n PC saved in ELR register
n Identifies causing instruction
n Actually PC + 4 is saved
n Handler must adjust

Chapter 4 — The Processor — 94


Exception Example
n Exception on ADD in
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 LDUR X16, [X7,#100]

n Handler
80000180 STUR X26, [X0,#1000]
80000184 STUR X27, [X0,#1008]

Chapter 4 — The Processor — 95

Exception Example

Chapter 4 — The Processor — 96


Exception Example

Chapter 4 — The Processor — 97

Multiple Exceptions
n Pipelining overlaps multiple instructions
n Could have multiple exceptions at once
n Simple approach: deal with exception from
earliest instruction
n Flush subsequent instructions
n “Precise” exceptions
n In complex pipelines
n Multiple instructions issued per cycle
n Out-of-order completion
n Maintaining precise exceptions is difficult!

Chapter 4 — The Processor — 98


Imprecise Exceptions
n Just stop pipeline and save state
n Including exception cause(s)
n Let the handler work out
n Which instruction(s) had exceptions
n Which to complete or flush
n May require “manual” completion
n Simplifies hardware, but more complex handler
software
n Not feasible for complex multiple-issue
out-of-order pipelines

Chapter 4 — The Processor — 99

§4.10 Parallelism via Instructions


Instruction-Level Parallelism (ILP)
n Pipelining: executing multiple instructions in
parallel
n To increase ILP
n Deeper pipeline
n Less work per stage Þ shorter clock cycle
n Multiple issue
n Replicate pipeline stages Þ multiple pipelines
n Start multiple instructions per clock cycle
n CPI < 1, so use Instructions Per Cycle (IPC)
n E.g., 4GHz 4-way multiple-issue
n 16 BIPS, peak CPI = 0.25, peak IPC = 4
n But dependencies reduce this in practice

Chapter 4 — The Processor — 100


Multiple Issue
n Static multiple issue
n Compiler groups instructions to be issued together
n Packages them into “issue slots”
n Compiler detects and avoids hazards
n Dynamic multiple issue
n CPU examines instruction stream and chooses
instructions to issue each cycle
n Compiler can help by reordering instructions
n CPU resolves hazards using advanced techniques at
runtime

Chapter 4 — The Processor — 101

Speculation
n “Guess” what to do with an instruction
n Start operation as soon as possible
n Check whether guess was right
n If so, complete the operation
n If not, roll-back and do the right thing
n Common to static and dynamic multiple issue
n Examples
n Speculate on branch outcome
n Roll back if path taken is different
n Speculate on load
n Roll back if location is updated

Chapter 4 — The Processor — 102


Compiler/Hardware Speculation
n Compiler can reorder instructions
n e.g., move load before branch
n Can include “fix-up” instructions to recover
from incorrect guess
n Hardware can look ahead for instructions
to execute
n Buffer results until it determines they are
actually needed
n Flush buffers on incorrect speculation

Chapter 4 — The Processor — 103

Speculation and Exceptions


n What if exception occurs on a
speculatively executed instruction?
n e.g., speculative load before null-pointer
check
n Static speculation
n Can add ISA support for deferring exceptions
n Dynamic speculation
n Can buffer exceptions until instruction
completion (which may not occur)

Chapter 4 — The Processor — 104


Static Multiple Issue
n Compiler groups instructions into “issue
packets”
n Group of instructions that can be issued on a
single cycle
n Determined by pipeline resources required
n Think of an issue packet as a very long
instruction
n Specifies multiple concurrent operations
n Þ Very Long Instruction Word (VLIW)

Chapter 4 — The Processor — 105

Scheduling Static Multiple Issue


n Compiler must remove some/all hazards
n Reorder instructions into issue packets
n No dependencies with a packet
n Possibly some dependencies between
packets
n Varies between ISAs; compiler must know!
n Pad with nop if necessary

Chapter 4 — The Processor — 106


LEGv8 with Static Dual Issue
n Two-issue packets
n One ALU/branch instruction
n One load/store instruction
n 64-bit aligned
n ALU/branch, then load/store
n Pad an unused instruction with nop

Address Instruction type Pipeline Stages


n ALU/branch IF ID EX MEM WB
n+4 Load/store IF ID EX MEM WB
n+8 ALU/branch IF ID EX MEM WB
n + 12 Load/store IF ID EX MEM WB
n + 16 ALU/branch IF ID EX MEM WB
n + 20 Load/store IF ID EX MEM WB

Chapter 4 — The Processor — 107

LEGv8 with Static Dual Issue

Chapter 4 — The Processor — 108


Hazards in the Dual-Issue LEGv8
n More instructions executing in parallel
n EX data hazard
n Forwarding avoided stalls with single-issue
n Now can’t use ALU result in load/store in same packet
n ADD X0, X0, X1
LDUR X2, [X0,#0]
n Split into two packets, effectively a stall
n Load-use hazard
n Still one cycle use latency, but now two instructions
n More aggressive scheduling required

Chapter 4 — The Processor — 109

Scheduling Example
n Schedule this for dual-issue LEGv8
Loop: LDUR X0, [X20,#0] // X0=array element
ADD X0, X0,X21 // add scalar in X21
STUR X0, [X20,#0] // store result
SUBI X20, X20,#4 // decrement pointer
CMP X20, X22 // branch $s1!=0
BGT Loop
ALU/branch Load/store cycle
Loop: nop LDUR X0, [X20,#0] 1
SUBI X20, X20,#4 nop 2
ADD X0, X0,X21 nop 3
CMP X20, X22 sw $t0, 4($s1) 4
BGT Loop STUR X0, [X20,#0] 5

n IPC = 7/6 = 1.17 (c.f. peak IPC = 2)


Chapter 4 — The Processor — 110
Loop Unrolling
n Replicate loop body to expose more
parallelism
n Reduces loop-control overhead
n Use different registers per replication
n Called “register renaming”
n Avoid loop-carried “anti-dependencies”
n Store followed by a load of the same register
n Aka “name dependence”
n Reuse of a register name

Chapter 4 — The Processor — 111

Loop Unrolling Example


ALU/branch Load/store cycle
Loop: SUBI X20, X20,#32 LDUR X0, [X20,#0] 1
nop LDUR X1, [X20,#24] 2
ADD X0, X0, X21 LDUR X2, [X20,#16] 3
ADD X1, X1, X21 LDUR X3, [X20,#8] 4
ADD X2, X2, X21 STUR X0, [X20,#32] 5
ADD X3, X3, X21 sw X1, [X20,#24] 6
CMP X20,X22 sw X2, [X20,#16] 7
BGT Loop sw X3, [X20,#8] 8

n IPC = 15/8 = 1.875


n Closer to 2, but at cost of registers and code size

Chapter 4 — The Processor — 112


Dynamic Multiple Issue
n “Superscalar” processors
n CPU decides whether to issue 0, 1, 2, …
each cycle
n Avoiding structural and data hazards
n Avoids the need for compiler scheduling
n Though it may still help
n Code semantics ensured by the CPU

Chapter 4 — The Processor — 113

Dynamic Pipeline Scheduling


n Allow the CPU to execute instructions out
of order to avoid stalls
n But commit result to registers in order
n Example
LDUR X0, [X21,#20]
ADD X1, X0, X2
SUB X23,X23,X3
ANDI X5, X23,#20
n Can start sub while ADD is waiting for LDUI

Chapter 4 — The Processor — 114


Dynamically Scheduled CPU
Preserves
dependencies

Hold pending
operands

Results also sent


to any waiting
reservation stations

Reorders buffer for


register writes
Can supply
operands for
issued instructions

Chapter 4 — The Processor — 115

Register Renaming
n Reservation stations and reorder buffer
effectively provide register renaming
n On instruction issue to reservation station
n If operand is available in register file or
reorder buffer
n Copied to reservation station
n No longer required in the register; can be
overwritten
n If operand is not yet available
n It will be provided to the reservation station by a
function unit
n Register update may not be required
Chapter 4 — The Processor — 116
Speculation
n Predict branch and continue issuing
n Don’t commit until branch outcome
determined
n Load speculation
n Avoid load and cache miss delay
n Predict the effective address
n Predict loaded value
n Load before completing outstanding stores
n Bypass stored values to load unit
n Don’t commit load until speculation cleared

Chapter 4 — The Processor — 117

Why Do Dynamic Scheduling?


n Why not just let the compiler schedule
code?
n Not all stalls are predicable
n e.g., cache misses
n Can’t always schedule around branches
n Branch outcome is dynamically determined
n Different implementations of an ISA have
different latencies and hazards

Chapter 4 — The Processor — 118


Does Multiple Issue Work?
The BIG Picture

n Yes, but not as much as we’d like


n Programs have real dependencies that limit ILP
n Some dependencies are hard to eliminate
n e.g., pointer aliasing
n Some parallelism is hard to expose
n Limited window size during instruction issue
n Memory delays and limited bandwidth
n Hard to keep pipelines full
n Speculation can help if done well
Chapter 4 — The Processor — 119

Power Efficiency
n Complexity of dynamic scheduling and
speculations requires power
n Multiple simpler cores may be better
Microprocessor Year Clock Rate Pipeline Issue Out-of-order/ Cores Power
Stages width Speculation
i486 1989 25MHz 5 1 No 1 5W
Pentium 1993 66MHz 5 2 No 1 10W
Pentium Pro 1997 200MHz 10 3 Yes 1 29W
P4 Willamette 2001 2000MHz 22 3 Yes 1 75W
P4 Prescott 2004 3600MHz 31 3 Yes 1 103W
Core 2006 2930MHz 14 4 Yes 2 75W
UltraSparc III 2003 1950MHz 14 4 No 1 90W
UltraSparc T1 2005 1200MHz 6 1 No 8 70W

Chapter 4 — The Processor — 120


§4.11 Real Stuff: The ARM Cortex-A8 and Intel Core i7 Pipelines
Cortex A53 and Intel i7
Processor ARM A53 Intel Core i7 920
Market Personal Mobile Device Server, cloud
Thermal design power 100 milliWatts 130 Watts
(1 core @ 1 GHz)
Clock rate 1.5 GHz 2.66 GHz
Cores/Chip 4 (configurable) 4
Floating point? Yes Yes
Multiple issue? Dynamic Dynamic
Peak instructions/clock cycle 2 4
Pipeline stages 8 14
Pipeline schedule Static in-order Dynamic out-of-order
with speculation
Branch prediction Hybrid 2-level
1st level caches/core 16-64 KiB I, 16-64 KiB D 32 KiB I, 32 KiB D
2nd level caches/core 128-2048 KiB 256 KiB (per core)
3rd level caches (shared) (platform dependent) 2-8 MB

Chapter 4 — The Processor — 121

ARM Cortex-A53 Pipeline

Chapter 4 — The Processor — 122


ARM Cortex-A53 Performance

Chapter 4 — The Processor — 123

Core i7 Pipeline

Chapter 4 — The Processor — 124


Core i7 Performance

Chapter 4 — The Processor — 125

§4.12 Instruction-Level Parallelism and Matrix Multiply


Matrix Multiply
n Unrolled C code
1 #include <x86intrin.h>
2 #define UNROLL (4)
3
4 void dgemm (int n, double* A, double* B, double* C)
5 {
6 for ( int i = 0; i < n; i+=UNROLL*4 )
7 for ( int j = 0; j < n; j++ ) {
8 __m256d c[4];
9 for ( int x = 0; x < UNROLL; x++ )
10 c[x] = _mm256_load_pd(C+i+x*4+j*n);
11
12 for( int k = 0; k < n; k++ )
13 {
14 __m256d b = _mm256_broadcast_sd(B+k+j*n);
15 for (int x = 0; x < UNROLL; x++)
16 c[x] = _mm256_add_pd(c[x],
17 _mm256_mul_pd(_mm256_load_pd(A+n*k+x*4+i), b));
18 }
19
20 for ( int x = 0; x < UNROLL; x++ )
21 _mm256_store_pd(C+i+x*4+j*n, c[x]);
22 }
23 }

Chapter 4 — The Processor — 126


§4.12 Instruction-Level Parallelism and Matrix Multiply
Matrix Multiply
n Assembly code:
1 vmovapd (%r11),%ymm4 # Load 4 elements of C into %ymm4
2 mov %rbx,%rax # register %rax = %rbx
3 xor %ecx,%ecx # register %ecx = 0
4 vmovapd 0x20(%r11),%ymm3 # Load 4 elements of C into %ymm3
5 vmovapd 0x40(%r11),%ymm2 # Load 4 elements of C into %ymm2
6 vmovapd 0x60(%r11),%ymm1 # Load 4 elements of C into %ymm1
7 vbroadcastsd (%rcx,%r9,1),%ymm0 # Make 4 copies of B element
8 add $0x8,%rcx # register %rcx = %rcx + 8
9 vmulpd (%rax),%ymm0,%ymm5 # Parallel mul %ymm1,4 A elements
10 vaddpd %ymm5,%ymm4,%ymm4 # Parallel add %ymm5, %ymm4
11 vmulpd 0x20(%rax),%ymm0,%ymm5 # Parallel mul %ymm1,4 A elements
12 vaddpd %ymm5,%ymm3,%ymm3 # Parallel add %ymm5, %ymm3
13 vmulpd 0x40(%rax),%ymm0,%ymm5 # Parallel mul %ymm1,4 A elements
14 vmulpd 0x60(%rax),%ymm0,%ymm0 # Parallel mul %ymm1,4 A elements
15 add %r8,%rax # register %rax = %rax + %r8
16 cmp %r10,%rcx # compare %r8 to %rax
17 vaddpd %ymm5,%ymm2,%ymm2 # Parallel add %ymm5, %ymm2
18 vaddpd %ymm0,%ymm1,%ymm1 # Parallel add %ymm0, %ymm1
19 jne 68 <dgemm+0x68> # jump if not %r8 != %rax
20 add $0x1,%esi # register % esi = % esi + 1
21 vmovapd %ymm4,(%r11) # Store %ymm4 into 4 C elements
22 vmovapd %ymm3,0x20(%r11) # Store %ymm3 into 4 C elements
23 vmovapd %ymm2,0x40(%r11) # Store %ymm2 into 4 C elements
24 vmovapd %ymm1,0x60(%r11) # Store %ymm1 into 4 C elements

Chapter 4 — The Processor — 127

Performance Impact

Chapter 4 — The Processor — 128


§4.14 Fallacies and Pitfalls
Fallacies
n Pipelining is easy (!)
n The basic idea is easy
n The devil is in the details
n e.g., detecting data hazards
n Pipelining is independent of technology
n So why haven’t we always done pipelining?
n More transistors make more advanced techniques
feasible
n Pipeline-related ISA design needs to take account of
technology trends
n e.g., predicated instructions

Chapter 4 — The Processor — 129

Pitfalls
n Poor ISA design can make pipelining
harder
n e.g., complex instruction sets (VAX, IA-32)
n Significant overhead to make pipelining work
n IA-32 micro-op approach
n e.g., complex addressing modes
n Register update side effects, memory indirection
n e.g., delayed branches
n Advanced pipelines have long delay slots

Chapter 4 — The Processor — 130


§4.14 Concluding Remarks
Concluding Remarks
n ISA influences design of datapath and control
n Datapath and control influence design of ISA
n Pipelining improves instruction throughput
using parallelism
n More instructions completed per second
n Latency for each instruction not reduced
n Hazards: structural, data, control
n Multiple issue and dynamic scheduling (ILP)
n Dependencies limit achievable parallelism
n Complexity leads to the power wall

Chapter 4 — The Processor — 131

You might also like