Docsity

Control Hazards - Intro to Computer Architecture - Lecture Notes, Study notes of Computer Architecture and Organization

Toggle sidebar
bg1
Control Hazards
- branching causes problems since the pipeline can be filled with the wrong
instructions.
Example: Two possible “streams” of instruction
BEQ R3, R8, ELSE
ADD R4, R5, R6 /* ADD should not be executed if the branch is taken */
SUB R8, R5, R6
.
.
ELSE: OR R3, R3, R2
FI/DI
latch
DI/CO
latch
CO/FO
latch
FO/EI
latch
EI/WO
latch
FI DI CO FO EI WO
Decoder
ALU
ALU
ALU
Data Data
Memory Memory
Register
Register
Register
File
File
File
Instr.
Memory
Copy of
Instr.
Decoded
CO-ALU
do +
Opcode
operand
1 addr.
operand
2 addr
(or reg#)
operand
1 value
operand
2 value
opcode
dest. dest.
addr/reg addr/reg
dest
addr/reg
result
value
EI-ALU
do *
Reg.s
for EAs
During which stage is the target address (address of the instruction to branch to) likely to be
calculated?
During which stage is the comparison between registers (R3 and R8) likely to be performed?
Lecture 5 - 1
Docsity.com
pf3
pf4
pf5
pf8

Partial preview of the text

Download Control Hazards - Intro to Computer Architecture - Lecture Notes and more Study notes Computer Architecture and Organization in PDF only on Docsity!

Control Hazards - branching causes problems since the pipeline can be filled with the wrong

instructions. Example: Two possible “streams” of instruction

BEQ R3, R8, ELSE ADD R4, R5, R6 /* ADD should not be executed if the branch is taken */ SUB R8, R5, R . . ELSE: OR R3, R3, R

FI/DI latch

DI/CO latch

CO/FO latch

FO/EI latch

EI/WO latch

FI DI CO^ FO^ EI^ WO

Decoder ALU

ALU

ALU

Data (^) Data Memory (^) Memory

Register

Register

Register File

File

File

Instr. Memory

Copy of Instr.

Decoded CO-ALU do +

Opcode operand 1 addr. operand 2 addr (or reg#)

operand 1 value operand 2 value

opcode

dest. dest. addr/reg addr/reg

dest addr/reg

result value

EI-ALU do *

Reg.s for EAs

During which stage is the target address (address of the instruction to branch to) likely to be calculated?

During which stage is the comparison between registers (R3 and R8) likely to be performed?

Lecture 5 - 1

Write WO Write operand to memory or register file Operand

Perform the indicated operation ( on conditional branch, compare register values and set PC to target address if comparison true )

Execute EI Instruction

Fetch FO Fetch operands from memory or register file Operands

Calculate the effective addresses of all operands ( on branch, calculate target address of branch )

Calculate CO Operands

Decode DI Determine opcode and operand specifiers Instruction

Fetch FI Read next instruction into CPU Instruction

Stage Abbreviation Actions

Assume the branch is taken:

ELSE: OR R3, R3, R2 FI DI CO FO EI WO

FI

FI DI

SUB R8, R5, R6 FI DI CO

ADD R4, R5, R6 FI DI CO FO

BEQ R3, R8, ELSE FI DI CO FO EI WO

Instructions 1 2 3 4 5 6 7 8 9 10 11 12

Time d

If the branch is taken, then there is a branch penalty of ____ cycles.

If the branch is not taken and we continue to fetch instructions sequentially, then there is a branch penalty of _____ cycles.

Possible approaches to reducing the branch penalty:

  1. Multiple streams - replicate initial pipeline stages and fetch from both streams when a branch is detected Problems:  causes contention for memory, registers, etc. since duplicate stages need the same hardware  additional branch instructions enter the pipeline before the original branch decision is resolved e.g., BEQ R3, R8, ELSE

ELSE: BNEZ R5, ELSE_

  1. Prefetch Branch of Target - (a limited form of multiple streams) save the instruction(s) from the target until the outcome of the branch is known; This saves on fetching if the branch is taken.

Lecture 5 - 2

Solution: Branch-History Table (BHT)- small, fully-associative cache to store information about most recently executed branch instructions. With a fully-associative cache, you supply a tag/key value to search for across the whole cache in parallel. In a BHT, the Branch instruction address acts as the tag since that’s what you know at IF.

During the IF stage, the Branch-History Table is checked to see if the instruction being fetched is a branch (if the addresses match) instruction.

If the instruction is a branch instruction and it is in the Branch-History Table, then the target address and prediction can be supplied by the BHT by the end of the IF for the branch instruction.

If the branch instruction is in the Branch-History Table, will the target address supplied correspond to the correct instruction to be execute next?

What if the instruction is a branch instruction and it is not in the Branch-History Table?

Should the Branch-History Table contain entries for unconditional as well as conditional branch instructions?

Lecture 5 - 4

Table below shows the advantage of using a Branch-history table to improve accuracy of the branch prediction. It shows the impact of past n branches on prediction accuracy.

Notice:

  1. the big jump in using the knowledge of just 1 past branch to predict the branch
  2. notice the big jump in going from using 1 to 2 past branches to predict the branch for scientific applications. What types of data do scientific applications spend most of their time processing? What would be true about the code for processing this type of data?

Typically, two prediction bits are use so that two wrong predictions in a row are need to change the prediction -- see Figure 12.

Predict Predict

Predict Predict

taken taken

not taken not taken

(branch) (branch)

(don't branch) (don't branch)

Not taken

Not taken

Taken

Taken

Taken

Taken

Not taken

Not taken

How does this help for nested loops?

Lecture 5 - 5

Type of mix

n Compiler Business Scientific

Consider the nested loops: for (i = 1; i <= 100; i++) { for (j = 1; j <= 100; j++) { } }

Nested Loop Unconditional Branch Penalty With 2 History Bit

i = 1

i = 2

j = 1

j = 1

j = 2

j = 2

j = 100

j = 100

j = 101

j = 101

miss in BHT

NOT TAKEN

NOT TAKEN

NOT TAKEN

NOT TAKEN

NOT TAKEN

NOT TAKEN

NOT TAKEN

NOT TAKEN

NOT TAKEN

NOT TAKEN

NOT TAKEN

NOT TAKEN

NOT TAKEN

TAKEN

TAKEN

NOT TAKEN

NOT TAKEN

NOT TAKEN

NOT TAKEN

NOT TAKEN

NOT TAKEN

NOT TAKEN

NOT TAKEN

PREDICTION ACTUAL NEXT PREDICTION PENALTY

Penalties associated with conditional branches = 1 * 100 +

miss in BHT NOT TAKEN NOT TAKEN

NOT TAKEN NOT TAKEN NOT TAKEN

outer loop

inner loop

Lecture 5 - 7

  1. Delayed Branching - redefine the branch such that one (or two) instruction(s) after the branch will always be executed.

Compiler automatically rearranges code to fill the delayed-branch slot(s) with instructions that can always be executed. Instructions in the delayed-branch slot(s) do not need to be flushed after branching. If no instruction can be found to fill the delayed-branch slot(s), then a NOOP instruction is inserted.

BEQZ R3, ELSE

SUB R8, R2, R1 # delay slot alway done ADD R4, R5, R . . ELSE: ADD R3, R3, R

SUB R8, R2, R

BEQZ R3, ELSE

ADD R4, R5, R

ELSE: ADD R3, R3, R

Without Delayed Branching With Delayed Branching

Due to data dependences, the instruction before the branch cannot always be moved into the branch-delay slot. Other alternative to consider are:

ADD R7, R8, R

LOOP:.

SUB R3, R2, R

BEQZ R3, LOOP

ADD R7, R8, R9 #delay slot MUL R4, R5, R

LOOP: ADD R7, R8, R

SUB R3, R2, R

BEQZ R3, LOOP

MUL R4, R5, R

Without Delayed Branching With Delayed Branching

The Instruction at the Target of the Branch

Can this technique always be used?

SUB R3, R2, R

BEQZ R3, ELSE

ADD R8, R5, R6 # delay slot . . ELSE: ADD R3, R3, R

SUB R3, R2, R

BEQZ R3, ELSE

ADD R8, R5, R

ELSE: ADD R3, R3, R

Without Delayed Branching With Delayed Branching

The Instruction From the Fall-Through of the Branch

Can this technique always be used?

Lecture 5 - 8