pipeline front end
play

Pipeline Front-end Instruction Fetch & Branch Prediction - PowerPoint PPT Presentation

Spring 2015 :: CSE 502 Computer Architecture Pipeline Front-end Instruction Fetch & Branch Prediction Instructor: Nima Honarmand Spring 2015 :: CSE 502 Computer Architecture Big Picture Spring 2015 :: CSE 502 Computer


  1. Spring 2015 :: CSE 502 – Computer Architecture Pipeline Front-end Instruction Fetch & Branch Prediction Instructor: Nima Honarmand

  2. Spring 2015 :: CSE 502 – Computer Architecture Big Picture

  3. Spring 2015 :: CSE 502 – Computer Architecture Fetch Rate is an ILP Upper Bound • Instruction fetch limits performance – To sustain IPC of N, must sustain a fetch rate of N per cycle – Need to fetch N on average, not on every cycle • N-wide superscalar ideally fetches N insns. per cycle • This doesn’t happen in practice due to: – Instruction cache organization – Branches – … and interaction between the two

  4. Spring 2015 :: CSE 502 – Computer Architecture Instruction Cache Organization • To fetch N instructions per cycle... – I$ line must be wide enough for N instructions • PC register selects I$ line • A fetch group is the set of insns. starting at PC – For N-wide machine, [PC,PC+N-1] PC Inst Inst Inst Inst Cache Line Tag Inst Inst Inst Inst Tag Inst Inst Inst Inst Tag Decoder Inst Inst Inst Inst Tag Inst Inst Inst Inst Tag

  5. Spring 2015 :: CSE 502 – Computer Architecture Fetch Misalignment (1/2) • If PC = xxx01001, N=4: – Ideal fetch group is xxx01001 through xxx01100 (inclusive) PC: xxx01001 00 01 10 11 000 Inst Inst Inst Inst Tag 001 Inst Inst Inst Inst Tag 010 Inst Inst Inst Inst Tag Decoder 011 Inst Inst Inst Inst Tag 111 Inst Inst Inst Inst Tag Line width Fetch group Misalignment reduces fetch width

  6. Spring 2015 :: CSE 502 – Computer Architecture Fetch Misalignment (2/2) • Fetch block A and A+1 in parallel – Banked I$ + rotator network • To put instructions back in correct order – May add latency (add pipeline stages to avoid slowing down clock) Bank 0: Even Sets Bank 1: Odd Sets • There are other solutions 1020 1021 using advanced data-array 1022 1023 SRAM design techniques… Rotator Inst Inst Inst Inst Aligned fetch group

  7. Spring 2015 :: CSE 502 – Computer Architecture Program Control Flow and Branches Linearly- • Program control flow is CFG Mapped CFG dynamic traversal of Branches static CFG • CFG is mapped to linear memory Basic Blocks

  8. Spring 2015 :: CSE 502 – Computer Architecture Types of Branches • Direction : – Conditional • Conditional branches • Can use Condition code (CC) register or General purpose register – Unconditional • Jumps, subroutine calls, returns • Target : – PC-encoded • PC-relative • Absolute addr – Computed (target derived from register or stack) Need direction and target to find next fetch group

  9. Spring 2015 :: CSE 502 – Computer Architecture What’s Bad About Branches? 1. Cause fragmentation of I$ lines Inst Inst Inst Inst Tag Inst Branch Inst Inst Tag Inst Inst Inst Inst Tag Decoder Inst Inst Inst Inst Tag Inst Inst Inst Inst Tag X X 2. Cause disruption of sequential control flow – Need to determine direction – Need to determine target

  10. Spring 2015 :: CSE 502 – Computer Architecture Branches Disrupt Sequential Control Flow Fetch • Need to determine Instruction/Decode Buffer target Decode  Target prediction Dispatch Buffer Dispatch • Need to determine direction Reservation  Direction prediction Stations Issue Branch Execute Finish Reorder/ Completion Buffer Complete Store Buffer Retire

  11. Spring 2015 :: CSE 502 – Computer Architecture Branch Prediction • Why? – To avoid stalls in fetch stage (due to both unknown direction and target) • Static prediction – Always predict not-taken (pipelines do this naturally) – Based on branch offset (predict backward branch taken) – Use compiler hints – These are all direction prediction, what about target? • Dynamic prediction – Uses special hardware (our focus)

  12. Spring 2015 :: CSE 502 – Computer Architecture Dynamic Branch Prediction • A form of speculation Reorder buffer (ROB) – Integrated with Fetch stage regfile I$ D$ B F D S C R P • Involves three mechanisms: – Prediction – Validation (and training of the predictors) – Misprediction recovery • Prediction uses two hardware predictors – Direction predictor guesses if branch is taken or not-taken • Applies to conditional branches only – Target predictor guesses the destination PC • Applies to all control transfers

  13. Spring 2015 :: CSE 502 – Computer Architecture BP in Superscalars • Fetch group might contain multiple branches • How many branches to predict? (now) – Simple: upto the first one (maybe later) – A bit harder: upto the first taken one (maybe later) – Even harder: multiple taken branches • Only useful if you can fetch multiple fetch groups from I$ in each cycle • How to identify the branch and its target in Fetch stage? – I.e., without executing or decoding?

  14. Spring 2015 :: CSE 502 – Computer Architecture Option 1: Partial Decoding Fetch PC L1-I Target Dir Pred Pred PD PD PD PD + sizeof(inst) Branch’s PC Huge latency (reduces clock frequency)

  15. Spring 2015 :: CSE 502 – Computer Architecture Option 2: Predecoding Predecode branches on fill from L2 L1-I Target Dir Pred Pred Branch’s PC + Store 1 bit per inst, set if inst sizeof(inst) is a branch partial-decode logic removed High latency (L1-I on the critical path)

  16. Spring 2015 :: CSE 502 – Computer Architecture Option 3: Using Fetch group Addr • With one branch in fetch group, does it matter where it is? L1-I Target Dir Pred Pred • Fetch-group addr is stable + – i.e., the same set of instructions are likely to be sizeof(fetch group) if no branch fetched using the same Cache Line address fetch group in the future – Why? Latency determined by branch predictor

  17. Spring 2015 :: CSE 502 – Computer Architecture Target Prediction

  18. Spring 2015 :: CSE 502 – Computer Architecture Target Prediction • Target: 32- or 64-bit value • Turns out targets are generally easier to predict – Don’t need to predict not-taken target – Taken target doesn’t usually change • Only need to predict taken-branch targets Target Pred • Predictor is really just a “cache” + – Called Branch Target Buffer (BTB) sizeof(inst) PC

  19. Spring 2015 :: CSE 502 – Computer Architecture Branch Target Buffer ( BTB ) Branch Instruction Address (Tag) Branch PC BIA BTA V Branch Target Address Valid Bit = Next Fetch PC Hit?

  20. Spring 2015 :: CSE 502 – Computer Architecture Set - Associative BTB PC V tag target V tag target V tag target = = = Next PC

  21. Spring 2015 :: CSE 502 – Computer Architecture Making BTBs Cheaper • Branch prediction is permitted to be wrong – Processor must have ways to detect mispredictions – Correctness of execution is always preserved – Performance may be affected Can tune BTB accuracy based on cost

  22. Spring 2015 :: CSE 502 – Computer Architecture BTB w/Partial Tags v 00000000cfff981 00000000cfff9704 00000000cfff9810 v 00000000cfff982 00000000cfff9830 00000000cfff9824 v 00000000cfff984 00000000cfff9900 00000000cfff984c 00001111beef9810 v f981 00000000cfff9704 00000000cfff9810 v f982 00000000cfff9830 00000000cfff9824 v f984 00000000cfff9900 00000000cfff984c Fewer bits to compare, but prediction may alias

  23. Spring 2015 :: CSE 502 – Computer Architecture BTB w/PC - offset Encoding v f981 00000000cfff9704 v f982 00000000cfff9830 00000000cfff984c v f984 00000000cfff9900 v f981 ff9704 v f982 ff9830 00000000cfff984c v f984 ff9900 00000000cf ff9900 If target too far or PC rolls over, will mispredict

  24. Spring 2015 :: CSE 502 – Computer Architecture BTB Miss? • Dir-Pred says “taken” • Target-Pred (BTB) misses – Could default to fall-through PC (as if Dir-Pred said N-t) • But we know that’s likely to be wrong! • Stall fetch until target known … when’s that? – PC-relative: after decode, we can compute target – Indirect: must wait until register read/exec

  25. Spring 2015 :: CSE 502 – Computer Architecture Subroutine Calls P: 0x1000: (start of printf) 1 FFB 0x1000 A: 0xFC34: CALL printf 1 FC3 0x1000 B: 0xFD08: CALL printf 1 FD0 0x1000 C: 0xFFB0: CALL printf BTB can easily predict target of calls

  26. Spring 2015 :: CSE 502 – Computer Architecture Subroutine Returns P: 0x1000: ST $RA  [$sp] 0x1B98: LD $tmp  [$sp] 0x1B9C: RETN $tmp 0 1 1B9 0xFC38 A: 0xFC34: CALL printf X A’:0xFC38: CMP $ret, 0 B: 0xFD08: CALL printf B’:0xFD0C: CMP $ret, 0 BTB can’t predict return for multiple call sites

  27. Spring 2015 :: CSE 502 – Computer Architecture Return Address Stack ( RAS ) A: 0xFC34: CALL printf FC38 FC38 P: 0x1000: ST $RA  [$sp] D004 BTB … 0x1B9C: RETN $tmp A’:0xFC38: CMP $ret, 0 FC38 Keep track of call stack

  28. Spring 2015 :: CSE 502 – Computer Architecture Return Address Stack Overflow 1. Wrap-around and overwrite • Will lead to eventual misprediction after four pops 2. Do not modify RAS • Will lead to misprediction on next pop • Need to keep track of # of calls that were not pushed 64AC: CALL printf FC90 top of stack 64B0 421C ??? 48C8 7300

  29. Spring 2015 :: CSE 502 – Computer Architecture Direction Prediction

  30. Spring 2015 :: CSE 502 – Computer Architecture Branches Have Locality • If a branch was previously taken… – There’s a good chance it’ll be taken again for(i=0; i < 100000; i++) This branch will be taken { 99,999 times in a row. /* do stuff */ }

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend