Samira Khan University of Virginia Sep 26, 2018

Slides:



Advertisements
Similar presentations
Branch prediction Titov Alexander MDSP November, 2009.
Advertisements

ILP: IntroductionCSCE430/830 Instruction-level parallelism: Introduction CSCE430/830 Computer Architecture Lecturer: Prof. Hong Jiang Courtesy of Yifeng.
1 Pipelining Part 2 CS Data Hazards Data hazards occur when the pipeline changes the order of read/write accesses to operands that differs from.
Lecture 8 Dynamic Branch Prediction, Superscalar and VLIW Advanced Computer Architecture COE 501.
Mehmet Can Vuran, Instructor University of Nebraska-Lincoln Acknowledgement: Overheads adapted from those provided by the authors of the textbook.
Dynamic Branch PredictionCS510 Computer ArchitecturesLecture Lecture 10 Dynamic Branch Prediction, Superscalar, VLIW, and Software Pipelining.
Pipeline Hazards Pipeline hazards These are situations that inhibit that the next instruction can be processed in the next stage of the pipeline. This.
8 Processing of control transfer instructions TECH Computer Science 8.1 Introduction 8.2 Basic approaches to branch handling 8.3 Delayed branching 8.4.
Chapter 8. Pipelining. Instruction Hazards Overview Whenever the stream of instructions supplied by the instruction fetch unit is interrupted, the pipeline.
Computer Architecture 2011 – Out-Of-Order Execution 1 Computer Architecture Out-Of-Order Execution Lihu Rappoport and Adi Yoaz.
Computer Organization and Architecture The CPU Structure.
1  2004 Morgan Kaufmann Publishers Chapter Six. 2  2004 Morgan Kaufmann Publishers Pipelining The laundry analogy.
Goal: Reduce the Penalty of Control Hazards
Appendix A Pipelining: Basic and Intermediate Concepts
1 Lecture 7: Static ILP and branch prediction Topics: static speculation and branch prediction (Appendix G, Section 2.3)
1 Dynamic Branch Prediction. 2 Why do we want to predict branches? MIPS based pipeline – 1 instruction issued per cycle, branch hazard of 1 cycle. –Delayed.
CS 1104 Help Session IV Five Issues in Pipelining Colin Tan, S
Chapter 6 Pipelined CPU Design. Spring 2005 ELEC 5200/6200 From Patterson/Hennessey Slides Pipelined operation – laundry analogy Text Fig. 6.1.
Computer Architecture: Multithreading (IV) Prof. Onur Mutlu Carnegie Mellon University.
Superscalar - summary Superscalar machines have multiple functional units (FUs) eg 2 x integer ALU, 1 x FPU, 1 x branch, 1 x load/store Requires complex.
Adapted from Computer Organization and Design, Patterson & Hennessy, UCB ECE232: Hardware Organization and Design Part 13: Branch prediction (Chapter 4/6)
COMPSYS 304 Computer Architecture Speculation & Branching Morning visitors - Paradise Bay, Bay of Islands.
CS 352H: Computer Systems Architecture
Computer Architecture: Branch Prediction (II) and Predicated Execution
Advanced Architectures
Computer Architecture Chapter (14): Processor Structure and Function
CS203 – Advanced Computer Architecture
Computer Architecture
Dynamic Branch Prediction
William Stallings Computer Organization and Architecture 8th Edition
PowerPC 604 Superscalar Microprocessor
Part IV Data Path and Control
CIS-550 Advanced Computer Architecture Lecture 10: Precise Exceptions
5.2 Eleven Advanced Optimizations of Cache Performance
Chapter 14 Instruction Level Parallelism and Superscalar Processors
Samira Khan University of Virginia Nov 13, 2017
Improving Program Efficiency by Packing Instructions Into Registers
ECS 154B Computer Architecture II Spring 2009
Samira Khan University of Virginia Dec 4, 2017
Samira Khan University of Virginia Sep 13, 2017
Pipelining: Advanced ILP
Part IV Data Path and Control
Morgan Kaufmann Publishers The Processor
Lecture 6: Advanced Pipelines
Module 3: Branch Prediction
So far we have dealt with control hazards in instruction pipelines by:
Branch statistics Branches occur every 4-6 instructions (16-25%) in integer programs; somewhat less frequently in scientific ones Unconditional branches.
15-740/ Computer Architecture Lecture 24: Control Flow
Computer Architecture: Multithreading (IV)
15-740/ Computer Architecture Lecture 5: Precise Exceptions
Advanced Computer Architecture
Hyesoon Kim Onur Mutlu Jared Stark* Yale N. Patt
Control unit extension for data hazards
So far we have dealt with control hazards in instruction pipelines by:
Pipelining and Exploiting Instruction-Level Parallelism (ILP)
CS203 – Advanced Computer Architecture
So far we have dealt with control hazards in instruction pipelines by:
So far we have dealt with control hazards in instruction pipelines by:
Control unit extension for data hazards
CSC3050 – Computer Architecture
ARM ORGANISATION.
Dynamic Hardware Prediction
So far we have dealt with control hazards in instruction pipelines by:
So far we have dealt with control hazards in instruction pipelines by:
Pipelining and Exploiting Instruction-Level Parallelism (ILP)
So far we have dealt with control hazards in instruction pipelines by:
Control unit extension for data hazards
So far we have dealt with control hazards in instruction pipelines by:
So far we have dealt with control hazards in instruction pipelines by:
CS161 – Design and Architecture of Computer Systems
Presentation transcript:

Samira Khan University of Virginia Sep 26, 2018 COMPUTER ARCHITECTURE CS 6354 Branch Prediction I Samira Khan University of Virginia Sep 26, 2018 The content and concept of this course are adapted from CMU ECE 740

AGENDA Logistics More on multi-core Branch Prediction

LOGISTICS Project proposal due today Project proposal presentation Oct 1 and Oct 3 Each group gets 10 minutes for presentation, 2 minutes for Q&A session

USES OF ASYMMETRY So far: What else can we do with asymmetry? Improvement in serial performance (sequential bottleneck) What else can we do with asymmetry? Energy reduction? Energy/performance tradeoff? Improvement in parallel portion?

USES OF CMPs Can you think about using these ideas to improve single- threaded performance? Implicit parallelization: thread level speculation Slipstream processors Leader-follower architectures Helper threading Prefetching Branch prediction Exception handling Redundant execution to tolerate soft (and hard?) errors

SLIPSTREAM PROCESSORS Goal: use multiple hardware contexts to speed up single thread execution (implicitly parallelize the program) Idea: Divide program execution into two threads: Advanced thread executes a reduced instruction stream, speculatively Redundant thread uses results, prefetches, predictions generated by advanced thread and ensures correctness Benefit: Execution time of the overall program reduces Core idea is similar to many thread-level speculation approaches, except with a reduced instruction stream Sundaramoorthy et al., “Slipstream Processors: Improving both Performance and Fault Tolerance,” ASPLOS 2000.

SLIPSTREAMING “At speeds in excess of 190 m.p.h., high air pressure forms at the front of a race car and a partial vacuum forms behind it. This creates drag and limits the car’s top speed. A second car can position itself close behind the first (a process called slipstreaming or drafting). This fills the vacuum behind the lead car, reducing its drag. And the trailing car now has less wind resistance in front (and by some accounts, the vacuum behind the lead car actually helps pull the trailing car). As a result, both cars speed up by several m.p.h.: the two combined go faster than either can alone.”

SLIPSTREAM PROCESSORS Detect and remove ineffectual instructions; run a shortened “effectual” version of the program (Advanced or A-stream) in one thread context Ensure correctness by running a complete version of the program (Redundant or R-stream) in another thread context Shortened A-stream runs fast; R-stream consumes near- perfect control and data flow outcomes from A-stream and finishes close behind Two streams together lead to faster execution (by helping each other) than a single one alone

SLIPSTREAM IDEA AND POSSIBLE HARDWARE

INSTRUCTION REMOVAL IN SLIPSTREAM IR detector Monitors retired R-stream instructions Detects ineffectual instructions and conveys them to the IR predictor Ineffectual instruction examples: dynamic instructions that repeatedly and predictably have no observable effect (e.g., unreferenced writes, non-modifying writes) dynamic branches whose outcomes are consistently predicted correctly. IR predictor Removes an instruction from A-stream after repeated indications from the IR detector A stream skips ineffectual instructions, executes everything else and inserts their results into delay buffer R stream executes all instructions but uses results from the delay buffer as predictions

WHAT IF A-STREAM DEVIATES FROM CORRECT EXECUTION? Why A-stream deviates due to incorrect removal or stale data access in L1 data cache How to detect it? Branch or value misprediction happens in R-stream (known as an IR misprediction) How to recover? Restore A-stream register state: copy values from R-stream registers using delay buffer or shared-memory exception handler Restore A-stream memory state: invalidate A-stream L1 data cache (or speculatively written blocks by A-stream)

Slipstream Questions How to construct the advanced thread Original proposal: Dynamically eliminate redundant instructions (silent stores, dynamically dead instructions) Dynamically eliminate easy-to-predict branches Other ways: Dynamically ignore long-latency stalls Static based on profiling How to speed up the redundant thread Original proposal: Reuse instruction results (control and data flow outcomes from the A-stream) Other ways: Only use branch results and prefetched data as predictions

BRANCH PREDICTION

Branch Prediction: Guess the Next Instruction to Fetch PC 0x0008 0x0004 0x0007 0x0005 0x0006 ?? I-$ DECD RF WB 0x0001 LD R1, MEM[R0] 0x0002 D-$ ADD R2, R2, #1 0x0003 BRZERO 0x0001 0x0004 ADD R3, R2, #1 12 cycles 0x0005 MUL R1, R2, R3 0x0006 LD R2, MEM[R2] Branch prediction 0x0007 LD R0, MEM[R2] 8 cycles Fetch Decode Execute Memory Writeback

Misprediction Penalty PC Flush!! I-$ DECD RF WB 0x0001 LD R1, MEM[R0] 0x0007 0x0006 0x0005 0x0004 0x0003 0x0002 D-$ ADD R2, R2, #1 0x0003 BRZERO 0x0001 0x0004 ADD R3, R2, #1 0x0005 4 cycles MUL R1, R2, R3 0x0006 LD R2, MEM[R2] 0x0007 LD R0, MEM[R2] Fetch Decode Execute Memory Writeback

Performance Analysis correct guess  no penalty ~86% of the time incorrect guess  2 bubbles Assume no data dependency related stalls 20% control flow instructions 70% of control flow instructions are taken CPI = [ 1 + (0.20*0.7) * 2 ] = = [ 1 + 0.14 * 2 ] = 1.28 probability of a wrong guess penalty for a wrong guess Can we reduce either of the two penalty terms?

BRANCH PREDICTION Idea: Predict the next fetch address (to be used in the next cycle) Requires three things to be predicted at fetch stage: Whether the fetched instruction is a branch (Conditional) branch direction Branch target address (if taken) Observation: Target address remains the same for a conditional direct branch across dynamic instances Idea: Store the target address from previous instance and access it with the PC Called Branch Target Buffer (BTB) or Branch Target Address Cache

Fetch Stage with BTB and Direction Prediction Direction predictor (taken?) taken? PC + inst size Next Fetch Address Program Counter hit? Address of the current branch target address Cache of Target Addresses (BTB: Branch Target Buffer) Always taken CPI = [ 1 + (0.20*0.3) * 2 ] = 1.12 (70% of branches taken)

Three Things to Be Predicted Requires three things to be predicted at fetch stage: 1. Whether the fetched instruction is a branch 2. (Conditional) branch direction 3. Branch target address (if taken) Third (3.) can be accomplished using a BTB Remember target address computed last time branch was executed First (1.) can be accomplished using a BTB If BTB provides a target address for the program counter, then it must be a branch Or, we can store “branch metadata” bits in instruction cache/memory  partially decoded instruction stored in I-cache Second (2.): How do we predict the direction?

HOW TO HANDLE CONTROL DEPENDENCES Critical to keep the pipeline full with correct sequence of dynamic instructions. Potential solutions if the instruction is a control-flow instruction: Stall the pipeline until we know the next fetch address Guess the next fetch address (branch prediction) Employ delayed branching (branch delay slot) Eliminate control-flow instructions (predicated execution) Fetch from both possible paths (if you know the addresses of both possible paths) (multipath execution)

DELAYED BRANCHING Change the semantics of a branch instruction Branch after N instructions Branch after N cycles Idea: Delay the execution of a branch. N instructions (delay slots) that come after the branch are always executed regardless of branch direction. Problem: How do you find instructions to fill the delay slots? Branch must be independent of delay slot instructions Unconditional branch: Easier to find instructions to fill the delay slot Conditional branch: Condition computation should not depend on instructions in delay slots  difficult to fill the delay slot

DELAYED BRANCHING Normal code: Timeline: Delayed branch code: IF EX IF EX B C C A BC X A B A B C A BC X C B D BC C D E BC C E B BC F -- BC F G B X: G G -- X: G 6 cycles 5 cycles

DELAYED BRANCHING (III) Advantages: + Keeps the pipeline full with useful instructions in a simple way assuming 1. Number of delay slots == number of instructions to keep the pipeline full before the branch resolves 2. All delay slots can be filled with useful instructions Disadvantages: -- Not easy to fill the delay slots (even with a 2-stage pipeline) 1. Number of delay slots increases with pipeline depth, superscalar execution width 2. Number of delay slots should be variable with variable latency operations. Why? -- Ties ISA semantics to hardware implementation -- SPARC, MIPS: 1 delay slot -- What if pipeline implementation changes with the next design?

HOW TO HANDLE CONTROL DEPENDENCES Critical to keep the pipeline full with correct sequence of dynamic instructions. Potential solutions if the instruction is a control-flow instruction: Stall the pipeline until we know the next fetch address Guess the next fetch address (branch prediction) Employ delayed branching (branch delay slot) Eliminate control-flow instructions (predicated execution) Fetch from both possible paths (if you know the addresses of both possible paths) (multipath execution)

Predicated Execution Idea: Convert control dependence to data dependence Simple example: Suppose we had a Conditional Move instruction… CMOV condition, R1  R2 R1 = (condition == true) ? R2 : R1 Employed in most modern ISAs (x86, Alpha) Code example with branches vs. CMOVs if (a == 5) {b = 4;} else {b = 3;} CMPEQ condition, a, 5; CMOV condition, b  4; CMOV !condition, b  3;

Predicated Execution Eliminates branches  enables straight line code (i.e., larger basic blocks in code) Advantages Always-not-taken prediction works better (no branches) Compiler has more freedom to optimize code (no branches) control flow does not hinder inst. reordering optimizations code optimizations hindered only by data dependencies Disadvantages Useless work: some instructions fetched/executed but discarded (especially bad for easy-to-predict branches) Requires additional ISA support Can we eliminate all branches this way?

How to Handle Control Dependences Critical to keep the pipeline full with correct sequence of dynamic instructions. Potential solutions if the instruction is a control-flow instruction: Stall the pipeline until we know the next fetch address Guess the next fetch address (branch prediction) Employ delayed branching (branch delay slot) Eliminate control-flow instructions (predicated execution) Fetch from both possible paths (if you know the addresses of both possible paths) (multipath execution)

SIMPLE BRANCH DIRECTION PREDICTION SCHEMES Compile time (static) Always not taken Always taken BTFN (Backward taken, forward not taken) Profile based (likely direction) Run time (dynamic) Last time prediction (single-bit)

MORE SOPHISTICATED DIRECTION PREDICTION Compile time (static) Always not taken Always taken BTFN (Backward taken, forward not taken) Profile based (likely direction) Program analysis based (likely direction) Run time (dynamic) Last time prediction (single-bit) Two-bit counter based prediction Two-level prediction (global vs. local) Hybrid

STATIC BRANCH PREDICTION (I) Always not-taken Simple to implement: no need for BTB, no direction prediction Low accuracy: ~30-40% Compiler can layout code such that the likely path is the “not- taken” path Always taken No direction prediction Better accuracy: ~60-70% Backward branches (i.e. loop branches) are usually taken Backward branch: target address lower than branch PC Backward taken, forward not taken (BTFN) Predict backward (loop) branches as taken, others not-taken

STATIC BRANCH PREDICTION (II) Profile-based Idea: Compiler determines likely direction for each branch using profile run. Encodes that direction as a hint bit in the branch instruction format. + Per branch prediction (more accurate than schemes in previous slide)  accurate if profile is representative! -- Requires hint bits in the branch instruction format -- Accuracy depends on dynamic branch behavior: TTTTTTTTTTNNNNNNNNNN  50% accuracy TNTNTNTNTNTNTNTNTNTN  50% accuracy -- Accuracy depends on the representativeness of profile input set

STATIC BRANCH PREDICTION (III) Program-based (or, program analysis based) Idea: Use heuristics based on program analysis to determine statically- predicted direction Opcode heuristic: Predict BLEZ as NT (negative integers used as error values in many programs) Loop heuristic: Predict a branch guarding a loop execution as taken (i.e., execute the loop) Pointer and FP comparisons: Predict not equal + Does not require profiling -- Heuristics might be not representative or good -- Requires compiler analysis and ISA support Ball and Larus, ”Branch prediction for free,” PLDI 1993. 20% misprediction rate

STATIC BRANCH PREDICTION (III) Programmer-based Idea: Programmer provides the statically-predicted direction Via pragmas in the programming language that qualify a branch as likely- taken versus likely-not-taken + Does not require profiling or program analysis + Programmer may know some branches and their program better than other analysis techniques -- Requires programming language, compiler, ISA support -- Burdens the programmer?

STATIC BRANCH PREDICTION All previous techniques can be combined Profile based Program based Programmer based How would you do that? What are common disadvantages of all three techniques? Cannot adapt to dynamic changes in branch behavior This can be mitigated by a dynamic compiler, but not at a fine granularity (and a dynamic compiler has its overheads…)

Samira Khan University of Virginia Sep 26, 2018 COMPUTER ARCHITECTURE CS 6354 Branch Prediction I Samira Khan University of Virginia Sep 26, 2018 The content and concept of this course are adapted from CMU ECE 740