the processor pipeline - basavaraj talawar · the processor pipeline chapter 4, patterson and...

40
The Processor Pipeline Chapter 4, Patterson and Hennessy, 4ed. Section 5.3, 5.4: J P Hayes.

Upload: others

Post on 16-Aug-2020

13 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: The Processor Pipeline - Basavaraj Talawar · The Processor Pipeline Chapter 4, Patterson and Hennessy, 4ed. Section 5.3, 5.4: J P Hayes. Pipeline. A Basic MIPS Implementation

The Processor Pipeline

Chapter 4, Patterson and Hennessy, 4ed.Section 5.3, 5.4: J P Hayes.

Page 2: The Processor Pipeline - Basavaraj Talawar · The Processor Pipeline Chapter 4, Patterson and Hennessy, 4ed. Section 5.3, 5.4: J P Hayes. Pipeline. A Basic MIPS Implementation

Pipeline

Page 3: The Processor Pipeline - Basavaraj Talawar · The Processor Pipeline Chapter 4, Patterson and Hennessy, 4ed. Section 5.3, 5.4: J P Hayes. Pipeline. A Basic MIPS Implementation

A Basic MIPS Implementation

● Memory-reference instructions – Load Word (lw) and Store Word (sw)

● ALU instructions – add, sub, AND, OR and slt● Branch on equal (beq)

Page 4: The Processor Pipeline - Basavaraj Talawar · The Processor Pipeline Chapter 4, Patterson and Hennessy, 4ed. Section 5.3, 5.4: J P Hayes. Pipeline. A Basic MIPS Implementation

Instruction Fetch – Elements

Page 5: The Processor Pipeline - Basavaraj Talawar · The Processor Pipeline Chapter 4, Patterson and Hennessy, 4ed. Section 5.3, 5.4: J P Hayes. Pipeline. A Basic MIPS Implementation

Instruction Fetch

Page 6: The Processor Pipeline - Basavaraj Talawar · The Processor Pipeline Chapter 4, Patterson and Hennessy, 4ed. Section 5.3, 5.4: J P Hayes. Pipeline. A Basic MIPS Implementation

ALU Operations – Elements

ADD R1, R2, R3

REGISTERFILE

REGISTERFILE

Addr

Data

Data

Write

Page 7: The Processor Pipeline - Basavaraj Talawar · The Processor Pipeline Chapter 4, Patterson and Hennessy, 4ed. Section 5.3, 5.4: J P Hayes. Pipeline. A Basic MIPS Implementation

ALU Operations – Elements

ADD R1, R2, R3

Page 8: The Processor Pipeline - Basavaraj Talawar · The Processor Pipeline Chapter 4, Patterson and Hennessy, 4ed. Section 5.3, 5.4: J P Hayes. Pipeline. A Basic MIPS Implementation

ALU Operations – Elements

ADD R1, R2, R3

Page 9: The Processor Pipeline - Basavaraj Talawar · The Processor Pipeline Chapter 4, Patterson and Hennessy, 4ed. Section 5.3, 5.4: J P Hayes. Pipeline. A Basic MIPS Implementation

Loads and Stores – Elements

LW R1, -8(R2)

Page 10: The Processor Pipeline - Basavaraj Talawar · The Processor Pipeline Chapter 4, Patterson and Hennessy, 4ed. Section 5.3, 5.4: J P Hayes. Pipeline. A Basic MIPS Implementation

Branches – Elements

BEQ R1, R2, LABEL BEQ R1, R2, -16

Page 11: The Processor Pipeline - Basavaraj Talawar · The Processor Pipeline Chapter 4, Patterson and Hennessy, 4ed. Section 5.3, 5.4: J P Hayes. Pipeline. A Basic MIPS Implementation

Branches – Elements

BEQ R1, R2, LABEL BEQ R1, R2, -16

Page 12: The Processor Pipeline - Basavaraj Talawar · The Processor Pipeline Chapter 4, Patterson and Hennessy, 4ed. Section 5.3, 5.4: J P Hayes. Pipeline. A Basic MIPS Implementation

Memory and R-type Instructions

Page 13: The Processor Pipeline - Basavaraj Talawar · The Processor Pipeline Chapter 4, Patterson and Hennessy, 4ed. Section 5.3, 5.4: J P Hayes. Pipeline. A Basic MIPS Implementation

Memory Instruction – Load

LW R1, -8(R2)

Page 14: The Processor Pipeline - Basavaraj Talawar · The Processor Pipeline Chapter 4, Patterson and Hennessy, 4ed. Section 5.3, 5.4: J P Hayes. Pipeline. A Basic MIPS Implementation

Memory Instruction – Store

SW R1, -8(R2)

Page 15: The Processor Pipeline - Basavaraj Talawar · The Processor Pipeline Chapter 4, Patterson and Hennessy, 4ed. Section 5.3, 5.4: J P Hayes. Pipeline. A Basic MIPS Implementation

R Type Instruction – ADD

ADD R1, R2, R3

Page 16: The Processor Pipeline - Basavaraj Talawar · The Processor Pipeline Chapter 4, Patterson and Hennessy, 4ed. Section 5.3, 5.4: J P Hayes. Pipeline. A Basic MIPS Implementation

The MIPS Datapath

Page 17: The Processor Pipeline - Basavaraj Talawar · The Processor Pipeline Chapter 4, Patterson and Hennessy, 4ed. Section 5.3, 5.4: J P Hayes. Pipeline. A Basic MIPS Implementation

The MIPS Datapath – BEQ

BEQ R1, R2, -16

Page 18: The Processor Pipeline - Basavaraj Talawar · The Processor Pipeline Chapter 4, Patterson and Hennessy, 4ed. Section 5.3, 5.4: J P Hayes. Pipeline. A Basic MIPS Implementation

MIPS Datapath and Control Lines

Page 19: The Processor Pipeline - Basavaraj Talawar · The Processor Pipeline Chapter 4, Patterson and Hennessy, 4ed. Section 5.3, 5.4: J P Hayes. Pipeline. A Basic MIPS Implementation

Pipeline Stages

Instruction Fetch (IF)Instruction Fetch (IF) ID: Instruction decode/Register file read

ID: Instruction decode/Register file read

EX: Execution/Address Calculation

EX: Execution/Address Calculation

MEM: MemoryAccess

MEM: MemoryAccess

WB: WriteBack

WB: WriteBack

Page 20: The Processor Pipeline - Basavaraj Talawar · The Processor Pipeline Chapter 4, Patterson and Hennessy, 4ed. Section 5.3, 5.4: J P Hayes. Pipeline. A Basic MIPS Implementation

Pipelined Datapath

Instruction Fetch (IF)Instruction Fetch (IF)

ID: Instruction decode/Register file read

ID: Instruction decode/Register file read

EX: Execution/Address Calculation

EX: Execution/Address Calculation

MEM: MemoryAccess

MEM: MemoryAccess

WB: WriteBack

WB: WriteBack

Page 21: The Processor Pipeline - Basavaraj Talawar · The Processor Pipeline Chapter 4, Patterson and Hennessy, 4ed. Section 5.3, 5.4: J P Hayes. Pipeline. A Basic MIPS Implementation

Pipelined vs. Nonpipelined Implementation

Page 22: The Processor Pipeline - Basavaraj Talawar · The Processor Pipeline Chapter 4, Patterson and Hennessy, 4ed. Section 5.3, 5.4: J P Hayes. Pipeline. A Basic MIPS Implementation

Pipelined vs. Nonpipelined Implementation

● Ratio of total execution times between the two versions for 10^6 instructions?

● Pipelining increases the instruction throughput opposed to individual instruction execution time.

IF ID EX MEM WB

Page 23: The Processor Pipeline - Basavaraj Talawar · The Processor Pipeline Chapter 4, Patterson and Hennessy, 4ed. Section 5.3, 5.4: J P Hayes. Pipeline. A Basic MIPS Implementation

Speedup of the Pipeline● The speedup of a k stage pipelined processor

over an unpipelined processor

S k=T unpipelinedT pipelined

=n⋅k

k+(n−1)

n: number of instructions in the program.k: number of pipeline stages

Page 24: The Processor Pipeline - Basavaraj Talawar · The Processor Pipeline Chapter 4, Patterson and Hennessy, 4ed. Section 5.3, 5.4: J P Hayes. Pipeline. A Basic MIPS Implementation

Efficiency of the Pipeline● Percentage of stages accomplishing tasks

related to the instruction in execution

η=No. of Instructions

Instruction ExecutionTime

n: number of instructions in the program.k: number of pipeline stages

η=n

k+(n−1)

Page 25: The Processor Pipeline - Basavaraj Talawar · The Processor Pipeline Chapter 4, Patterson and Hennessy, 4ed. Section 5.3, 5.4: J P Hayes. Pipeline. A Basic MIPS Implementation

Throughput of the Pipeline

● Number of tasks completed in unit time (one second)

w=η× f

f: frequency of operation

Page 26: The Processor Pipeline - Basavaraj Talawar · The Processor Pipeline Chapter 4, Patterson and Hennessy, 4ed. Section 5.3, 5.4: J P Hayes. Pipeline. A Basic MIPS Implementation

Pipeline Hazards

● Hazard: n. An unavoidable danger or risk, even though often foreseeable.

● Situations that prevent the next instruction in the instruction stream from being executing during its designated clock cycle

● Reduce the performance from the ideal speedup gained by pipelining

Page 27: The Processor Pipeline - Basavaraj Talawar · The Processor Pipeline Chapter 4, Patterson and Hennessy, 4ed. Section 5.3, 5.4: J P Hayes. Pipeline. A Basic MIPS Implementation

Structural Hazard

MEM ID EX MEM WB

MEM ID EX MEM WB

MEM ID EX MEM WB

MEM ID EX MEM WB

i1

i2

i3

i4

...

1 2 3 4 5 6 7 8 9

MEM ID EX MEM WBi5

HAZARD!!!

● Lack of resources● Solution: Increase resources

– Use of separate Data and Instruction memories in the MIPS pipeline

Page 28: The Processor Pipeline - Basavaraj Talawar · The Processor Pipeline Chapter 4, Patterson and Hennessy, 4ed. Section 5.3, 5.4: J P Hayes. Pipeline. A Basic MIPS Implementation

s

Data Hazard

IF ID EX MEM WBADD R1, R2, R3

1 2 3 4 5 6 7 8 9

IFSUB R4, R1, R5 ID EX MEM WB WRONG!

● Data (input operands) required by the instruction are not ready/available

● Data dependence● RAW, WAR, WAW dependences

ADD R1, R2, R3

SUB R2, R4, R5

ADD R1, R2, R3

SUB R1, R4, R5

Page 29: The Processor Pipeline - Basavaraj Talawar · The Processor Pipeline Chapter 4, Patterson and Hennessy, 4ed. Section 5.3, 5.4: J P Hayes. Pipeline. A Basic MIPS Implementation

Data HazardDADDDSUBANDORXOR

R4,R1,R5R6,R1,R7

R1,R2,R3

R8,R1,R9R10,R1,R11

IM REG DMDADD

DSUB

AND

OR

Time (clock cycles)

XOR

ALU REG

IM REG DMALU REG

IM REG DMALU REG

IM REG DMALU

IM REG ALU

Page 30: The Processor Pipeline - Basavaraj Talawar · The Processor Pipeline Chapter 4, Patterson and Hennessy, 4ed. Section 5.3, 5.4: J P Hayes. Pipeline. A Basic MIPS Implementation

Avoiding Data Hazards – ForwardingDADDDSUBANDORXOR

R4,R1,R5R6,R1,R7

R1,R2,R3

R8,R1,R9R10,R1,R11

IM REG DMDADD

DSUB

AND

OR

Time (clock cycles)

XOR

ALU REG

IM REG DMALU REG

IM REG DMALU REG

IM REG DMALU

IM REG ALU

Page 31: The Processor Pipeline - Basavaraj Talawar · The Processor Pipeline Chapter 4, Patterson and Hennessy, 4ed. Section 5.3, 5.4: J P Hayes. Pipeline. A Basic MIPS Implementation

Pipeline without Forwarding

Page 32: The Processor Pipeline - Basavaraj Talawar · The Processor Pipeline Chapter 4, Patterson and Hennessy, 4ed. Section 5.3, 5.4: J P Hayes. Pipeline. A Basic MIPS Implementation

Pipeline with Forwarding

Page 33: The Processor Pipeline - Basavaraj Talawar · The Processor Pipeline Chapter 4, Patterson and Hennessy, 4ed. Section 5.3, 5.4: J P Hayes. Pipeline. A Basic MIPS Implementation

Data Hazard – Load InstructionLDDSUBANDOR

R4,R1,R5R6,R1,R7

R1,0(R2)

R8,R1,R9

IM REG DMLD

DSUB

AND

OR

Time (clock cycles)

ALU REG

IM REG DMALU REG

IM REG DMALU REG

IM REG DMALU

Page 34: The Processor Pipeline - Basavaraj Talawar · The Processor Pipeline Chapter 4, Patterson and Hennessy, 4ed. Section 5.3, 5.4: J P Hayes. Pipeline. A Basic MIPS Implementation

Data Hazards – StallsLDDSUBANDOR

R4,R1,R5R6,R1,R7

R1,0(R2)

R8,R1,R9

IM REG DMLD

DSUB

AND

OR

Time (clock cycles)

ALU REG

IM REG DMALU REG

IM REG

IM REG

ALU

DMALU ALU

ALU ALU

Page 35: The Processor Pipeline - Basavaraj Talawar · The Processor Pipeline Chapter 4, Patterson and Hennessy, 4ed. Section 5.3, 5.4: J P Hayes. Pipeline. A Basic MIPS Implementation

Data Hazard – Solutions

● Data Forwarding● Instruction Reordering

Page 36: The Processor Pipeline - Basavaraj Talawar · The Processor Pipeline Chapter 4, Patterson and Hennessy, 4ed. Section 5.3, 5.4: J P Hayes. Pipeline. A Basic MIPS Implementation

Control Hazard

● Arise from the pipelining of branches and other instructions that change the PC

● Also called Branch Hazards

Page 37: The Processor Pipeline - Basavaraj Talawar · The Processor Pipeline Chapter 4, Patterson and Hennessy, 4ed. Section 5.3, 5.4: J P Hayes. Pipeline. A Basic MIPS Implementation

Branch Hazards

IF ID EX MEM WB

IF ID EX MEM WB

IF ID EX MEM WB

BEQ

Branch Successor

Branch Successor + 1

Branch Successor + 2

Time(clock cycles)

1 2 3 4 5 6 7 8 9

IF ID EX MEM WB

IFADD ID EX MEM WB

Assumption: Branch condition evaluation completed in the ID stageAssumption: Branch condition evaluation completed in the ID stage

Page 38: The Processor Pipeline - Basavaraj Talawar · The Processor Pipeline Chapter 4, Patterson and Hennessy, 4ed. Section 5.3, 5.4: J P Hayes. Pipeline. A Basic MIPS Implementation

Reducing Pipeline Branch Penalties

● Freeze the pipeline● Predict Taken● Predict Untaken● Fill Branch Delay Slot

IF ID EX MEM WB

IF

IF ID EX MEM WB

IF ID EX MEM WB

BEQ

AND

Branch Successor

Branch Successor + 1

Time(clock cycles)

1 2 3 4 5 6 7 8 9

ID EX MEM WB

i

i-1

i+16

i+17

Page 39: The Processor Pipeline - Basavaraj Talawar · The Processor Pipeline Chapter 4, Patterson and Hennessy, 4ed. Section 5.3, 5.4: J P Hayes. Pipeline. A Basic MIPS Implementation

Dynamic Branch Prediction● Branch prediction buffers

– Single bit predictors

– Change prediction with branch behaviour

– No. of wrong predictions?

PC Prediction

0x0100 1

0x0154 0

0x0210 1

... 1

BRANCH PREDICTION BUFFER

T T T T N T T T T T T T T T T T T

Wrong Predictions

Page 40: The Processor Pipeline - Basavaraj Talawar · The Processor Pipeline Chapter 4, Patterson and Hennessy, 4ed. Section 5.3, 5.4: J P Hayes. Pipeline. A Basic MIPS Implementation

Dynamic Branch Prediction

● 2-bit predictors 00

11

10

11

11

Branch PredictionBuffer

0x0100

0x0154

0x0210

11 10

0100