pipelinig hazardous

18
1 Pipeline Hazards Pipeline Hazards

Upload: jasscheema

Post on 06-Dec-2014

571 views

Category:

Education


0 download

DESCRIPTION

computer architecture

TRANSCRIPT

Page 1: Pipelinig hazardous

1

Pipeline HazardsPipeline Hazards

Page 2: Pipelinig hazardous

2

Pipeline HazardsPipeline Hazards

• Hazards are situations in pipelining where one instruction cannot immediately follow another.

• Hazards reduce the ideal speedup gained from pipelining and are classified into three classes:

– Structural hazards: Arise from hardware resource conflicts when the available hardware cannot support all possible combinations of instructions.

– Data hazards: Arise when an instruction depends on the results of a previous instruction in a way that is exposed by the overlapping of instructions in the pipeline

– Control hazards: Arise from the pipelining of conditional branches and other instructions that change the PC

• Can always resolve hazards by waitingCan always resolve hazards by waiting

Page 3: Pipelinig hazardous

3

Structural HazardsStructural Hazards

• In pipelined processors, overlapped instruction execution requires pipelining of functional units and duplication of resources to allow all possible combinations of instructions in the pipeline.

• If a resource conflict arises due to a hardware resource being required by more than one instruction in a single cycle, and one or more such instructions cannot be accommodated, then a structural hazard has occurred, for example:– when a machine has only one register file write port – or when a pipelined machine has a shared single-memory

pipeline for data and instructions.

Page 4: Pipelinig hazardous

4

Register File/Structural HazardsRegister File/Structural Hazards

Instr.

Order

Time (clock cycles)

Load

Instr 1

Instr 2

Instr 3

Instr 4

ALU DMemIfetch

ALU DMemIfetch

ALU DMemIfetch

ALU DMemIfetch

Cycle 1 Cycle 2 Cycle 3 Cycle 4 Cycle 6 Cycle 7Cycle 5

ALU DMemIfetch

Operation on register setOperation on register setby 2 different instructionsby 2 different instructionsin the same clock cyclein the same clock cycle

Reg Reg

Reg

Reg

Reg

Reg

Reg Reg

Reg Reg

Page 5: Pipelinig hazardous

5

Register File/Structural HazardsRegister File/Structural Hazards

Instr.

Order

Time (clock cycles)

Load

Instr 1

Instr 2

Instr 3

Instr 4

ALU DMemIfetch

ALU DMemIfetch

ALU DMemIfetch

Cycle 1 Cycle 2 Cycle 3 Cycle 4 Cycle 6 Cycle 7Cycle 5

We need 3 stall cycles We need 3 stall cycles In order to solve this hazardIn order to solve this hazard

Reg Reg

Reg

Reg

Reg

Reg

ALUIfetch Reg

Ifetch Reg

3 stalls cycles3 stalls cycles

Page 6: Pipelinig hazardous

6

Register File/Structural HazardsRegister File/Structural Hazards

Instr.

Order

Time (clock cycles)

Load

Instr 1

Instr 2

Instr 3

Instr 4

ALU DMemIfetch

ALU DMemIfetch

ALU DMemIfetch

ALU DMemIfetch

Cycle 1 Cycle 2 Cycle 3 Cycle 4 Cycle 6 Cycle 7Cycle 5

ALU DMemIfetch

No stalls are requiredNo stalls are required Reg Reg

Reg

Reg

Reg

Reg

Reg Reg

Reg Reg

Allow writing registers in first ½ of cycle Allow writing registers in first ½ of cycle and reading in 2and reading in 2ndnd ½ of cycle ½ of cycle

Page 7: Pipelinig hazardous

7

1 Memory Port/Structural Hazards1 Memory Port/Structural Hazards

Mem AL

U

Reg Mem Reg

Mem AL

U

Reg Mem Reg

Mem AL

U

Reg Mem Reg

Mem AL

U

Reg Mem Reg

Mem AL

U

Reg Mem Reg

Time (in Cycles)

Load

Instruction1

Instruction2

Instruction3

Instruction4

Inst

ruct

ion

Ord

er Operation on MemoryOperation on Memoryby 2 different instructionsby 2 different instructionsin the same clock cycle in the same clock cycle

Page 8: Pipelinig hazardous

8

Inserting Bubbles (Stalls)Inserting Bubbles (Stalls)

Mem AL

U

Reg Mem Reg

Mem AL

U

Reg Mem Reg

Mem AL

U

Reg Mem Reg

Time (in Cycles)

Load

Instruction1

Instruction2

Mem AL

U

Reg MemInstruction3

Stall BubbleBubble Bubble Bubble Bubble

Stall BubbleBubble Bubble Bubble Bubble

Stall BubbleBubble Bubble Bubble Bubble

3 stall cycles3 stall cycleswith 1-port memorywith 1-port memory

Page 9: Pipelinig hazardous

9

2 Memory Port/Structural Hazards2 Memory Port/Structural Hazards(Read & Write at the same time)(Read & Write at the same time)

Mem AL

U

Reg Mem Reg

Mem AL

U

Reg Mem Reg

Mem AL

U

Reg Mem Reg

Mem AL

U

Reg Mem Reg

Mem AL

U

Reg Mem Reg

Time (in Cycles)

Load

Instruction1

Instruction2

Instruction3

Instruction4

Inst

ruct

ion

Ord

er

No stall with 2-memory No stall with 2-memory ports ports

Page 10: Pipelinig hazardous

10

Pipeline HazardsPipeline Hazards

• Hazards reduce the ideal speedup gained from pipelining and are classified into three classes:

– Structural hazards: Arise from hardware resource conflicts when the available hardware cannot support all possible combinations of instructions.

– Data hazards: Arise when an instruction depends on the results of a previous instruction in a way that is exposed by the overlapping of instructions in the pipeline

– Control hazards: Arise from the pipelining of conditional branches and other instructions that change the PC

• Can always resolve hazards by waitingCan always resolve hazards by waiting

Page 11: Pipelinig hazardous

11

Instr.

Order

add r1,r2,r3

sub r4,r1,r3

and r6,r1,r7

or r8,r1,r9

xor r10,r1,r11

Reg ALU DMemIfetch Reg

Reg ALU DMemIfetch Reg

Reg ALU DMemIfetch Reg

Reg ALU DMemIfetch Reg

Reg ALU DMemIfetch Reg

Data Hazard on R1Data Hazard on R1

Time (clock cycles)

IF ID/RF EX MEM WB

Page 12: Pipelinig hazardous

12

Data Hazard ClassificationData Hazard Classification Given two instructions I, J, with I occurring before J

in an instruction stream:

• RAW (read after write): A true data dependence J tried to read a source before I writes to it, so J

incorrectly gets the old value.• WAW (write after write): A name dependence J tries to write an operand before it is written by

I The writes end up being performed in the wrong

order.• WAR (write after read): A name dependence J tries to write to a destination before it is read by

I, so I incorrectly gets the new value.

• RAR (read after read): Not a hazard.

Page 13: Pipelinig hazardous

13

Data Hazard ClassificationData Hazard ClassificationI (Write)

Shared Operand

J (Read)

Read after Write (RAW)

I (Read)

Shared Operand

J (Write)

Write after Read (WAR)

I (Write)

Shared Operand

J (Write)

Write after Write (WAW)

I (Read)

Shared Operand

J (Read)

Read after Read (RAR) not a hazard

Page 14: Pipelinig hazardous

14

Data Hazards Present in Current MIPS PipelineData Hazards Present in Current MIPS Pipeline

• Read after Write (RAW) Hazards:Read after Write (RAW) Hazards: Possible?– Caused by a “Dependence” (in compiler nomenclature). This

hazard results from an actual need for communication. – Yes possible, when an instruction requires an operand generated by

a preceding instruction with distance less than four.

– Resolved by:• Forwarding or Stalling.

I: add r1,r2,r3J: sub r4,r1,r3

1 2 543

1 2 543

Write

Read

Inst i

Inst j

read the old data.

Page 15: Pipelinig hazardous

15

– Write After Read (WAR) – not possibleError if InstrJ tries to write operand before InstrI reads it

– Called an “anti-dependence” by compiler writers.This results from reuse of the name “r1”.

I: sub r4,r1,r3 J: add r1,r2,r3K: mul r6,r1,r7

Data Hazards Present in Current MIPS PipelineData Hazards Present in Current MIPS Pipeline

1 2 543

1 2 543

Read

WriteInst i

Inst j

Always read the correct data.

Page 16: Pipelinig hazardous

16

Data Hazards Present in Current MIPS Data Hazards Present in Current MIPS PipelinePipeline

– Write After Write (WAW) - not possible- not possible– Error if InstrJ tries to write operand before InstrI writes it.

– Called an “output dependence” by compiler writersThis also results from the reuse of name “r1”.

I: sub r1,r4,r3 J: add r1,r2,r3K: mul r6,r1,r7

1 2 543

1 2 543Write

Inst i

Inst j

Write

Page 17: Pipelinig hazardous

17

Data Hazards

• Solutions for Data Hazards– Stalling

– Forwarding:• connect new value directly to next stage

– Reordering

Page 18: Pipelinig hazardous

18

Minimizing Data Hazard Stalls by Minimizing Data Hazard Stalls by ForwardingForwarding

• Forwarding is a hardware-based technique (also called register bypassing or short-circuiting) used to eliminate or minimize data hazard stalls.

• Using forwarding hardware, the result of an instruction is copied directly from where it is produced (ALU, memory read port etc.), to where subsequent instructions need it (ALU, input register, memory write port etc.)

MU

X

MU

X

Zero?

DataMemory

ALU

D/A

B

uffer

A/M

B

uffer

M/W

B

uffer