Photo Album - Milwaukee School of Engineering

Photo Album - Milwaukee School of Engineering

Branch Prediction Lecture Objectives: 1) 2) 3) Define branch prediction. Draw a state machine for a 2 bit branch prediction scheme Explain the impact on the compiler of branch delay. Control Hazards Consider: add $t1, $zero, $zero # t1=0 beq $t1, $zero, Ifequal Notequal: addi

$v0, $zero, 4 Ifequal: addi $v0, $zero, 17 Branch determines flow of control Fetching next instruction depends on branch outcome Chapter 4 The Processor 2 Stall on Branch Wait until branch outcome determined before fetching next instruction Pipeline cant determine next instruction until MEM stage of beq

Still working on ID stage of beq when IF should begin! add $t1, $zero, $zero beq $t1, $zero, Ifequal addi $v0, $zero, 4 #Notequal addi $v0, $zero, 17 #Ifequal Chapter 4 The Processor 3 Next instr determined here Deciding earlier helps a little Extra hardware can be designed to test registers and update the PC in the ID stage Then IF of next inst can be done one step earlier

Still have a 1-cycle stall, however add $t1, $zero, $zero beq $t1, $zero, Ifequal addi $v0, $zero, 4 #Notequal addi $v0, $zero, 17 #Ifequal Next instr determined here with extra hardware Chapter 4 The Processor 4 Performance penalty of stalling on branch:

17% of instructions executed in the SPECint2006 benchmark are branch instructions If we always stalled for 1 clock cycle on a branch, what performance penalty would we have? CS2710 Computer Organization 5 Branch Prediction A method of resolving branch hazards that assumes a given outcome for the branch and proceeds from that assumption rather than waiting to ascertain the actual outcome CS2710 Computer Organization

6 1-bit Dynamic Branch Prediction One possibility is to have each branch instruction reserve a bit that retains the history of the last decision 0: branch not taken 1: branch taken To execute a branch Check history bit, expect the same outcome Start fetching from fall-through (next instruction) or branch target If wrong, flush pipeline and flip prediction bit Next actual instr determined here add $t1, $zero, $zero

beq $t1, $zero, Ifequal addi $v0, $zero, 4 #Notequal CS2710 Computer Organization 7 Problems with 1-bit Dynamic Branch Prediction Consider a loop that branches 9 times in a row, then is not taken once (end of loop condition is met) Branch taken 9 times, not taken 1 time At steady state The first branch decision will be incorrect (from previous execution) The final branch decision will be incorrect

Thus, the prediction accuracy would only be 80% CS2710 Computer Organization 8 2-Bit Predictor Only change prediction on two successive mispredictions Chapter 4 The Processor 9 Loops and Static Branch Prediction Consider the following loop of code Which branch might we reliably predict? .text

main: li $t0, 100 loop: addi $t0, $t0, -1 add $t0, $t0, $zero bnez $t0, loop #other instructions follow here CS2710 Computer Organization 10 Example 2: Assembly while-loop .text main: li $t0, 10 Which branch is more probable?

loop: beqz $t0, exitLoop addi $t0, $t0, -1 add $t0, $t0, $zero j loop exitLoop: # Goto main j main CS2710 Computer Organization 11 Static prediction based on code analysis (done by compiler) Assume all branches to a previous address are always taken Assume all branches to a subsequent address are not taken

CS2710 Computer Organization 12 Dynamic Versus static 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 CS2710 Computer Organization 13 Survey The branch prediction methods we just discussed were examples of A. Static Branch Prediction B. Dynamic Branch Prediction C. I havent a clue CS2710 Computer Organization

14 MIPS approach: delayed branching Always assume branch not taken. This means the instruction immediately following the branch instruction will always begin to execute The actual decision to branch will/will not be taken until after that instruction begins to execute! Leaves it to the compiler to insert a useful instruction right after the branch that would have needed to execute whether or not the branch was taken add $t1, $zero, $zero beq $t1, $zero, Ifequal #next inst after beq!!

Next actual instr determined here with extra hardware Chapter 4 The Processor 15 Delayed branching example before after #previous instructions add $s1, $s2, $s3 beq $s2, $zero, Ifequal # no-branch instructions Ifequal: # branch instructions #previous instructions

beq $s2, $zero, Ifequal add $s1, $s2, $s3 # no-branch instructions + Ifequal: # branch instructions MIPS always assumes branch not taken, so the pipeline will automatically begin executing the next instruction following the beq. The actual branch will be delayed until AFTER the next instruction executes The compiler must help out by inserting a useful instruction after the beq to execute while the branch decision is being made by the processor CS2710 Computer Organization 16 Delayed branching pitfall

before not possible #previous instructions add $s2, $s1, $s3 beq $s2, $zero, Ifequal # no-branch instructions Ifequal: # branch instructions #previous instructions beq $s2, $zero, Ifequal add $s2, $s1, $s3 # no-branch instructions + Ifequal: # branch instructions

In this case, the beq instruction depends on $s2 being up-to-date before the branching decision is made If the compiler moves the add instruction until after beq, then $s2 will be updated too late beq would use a stale value of $s2!! The compiler in this case would have to search for a different instruction that it could insert after the beq If no such instruction can be found (which is rare), the pipeline will stall CS2710 Computer Organization 17

Recently Viewed Presentations

  • Getting info from R(r) - Oneonta

    Getting info from R(r) - Oneonta

    3. The shielding constant S for electrons in these groups are determined as follows: a. Each electron in the same group contributes 0.35 to S. (exception: 1s electron contributes 0.30 to another 1s electron) b. Each electron in n-1 groups...
  • Performance Task Overview

    Performance Task Overview

    The tables provide estimated average times for students to complete the Smarter Balanced math and ELA assessmentsand outlines the number and duration of sessions, breaks, total duration of the assessment, and additional resources for the mathematics and ELA CAT items...
  • Phase One Voice of the Customer Branding Research

    Phase One Voice of the Customer Branding Research

    "We trust the pastoral care to Christian Village. They have lots of support and lots of events. They take their job seriously. It is a denominational nursing home." ... There are larger pastor groups that we belong to, they should...
  • PMG Slides - Fermilab

    PMG Slides - Fermilab

    Recall, Keely himself claimed to have a motor which ran on water in 1890. A patent was issued in 1935 for the "Garrett Carburetor", which set the standard for countless water schemes to follow. The principle is quite simple: Invent...
  • Important General Chemistry Lab Policies and Procedures

    Important General Chemistry Lab Policies and Procedures

    The balance room is to be cleaned after each use. A brush and dust pan are available to sweep up any spilled solids. The method of creating a mess and cleaning at the end is NOT ACCEPTABLE and is also...
  • Presentation Title Slide - Energy Star

    Presentation Title Slide - Energy Star

    ENERGY STAR Marks. Brand Recognition. About 85 percent of Americans recognize the ENERGY STAR logo as the symbol for energy efficiency. Thus, the ENERGY STAR identity is a valuable asset that must be properly used and protected.
  • Frequently Asked Questions FAQs Q: What has changed

    Frequently Asked Questions FAQs Q: What has changed

    Q: What types of medical bills does ELAP review? A: They review all medical bills with the exception of retail and mail order prescription drug claims. Your prescription drug claims are handled by ProCare Rx.
  • Medieval Hand Cannon Type: Gun Country of Origin:

    Medieval Hand Cannon Type: Gun Country of Origin:

    Type: Hand to Hand Time Period: Late Medieval and Renaissance. Long Sword