ECE 4750 Computer Architecture, Fall 2014 T03 Pipelined Processors

Similar documents
ECE 4750 Computer Architecture, Fall 2016 T02 Fundamental Processor Microarchitecture

ECE 4750 Computer Architecture, Fall 2014 T01 Single-Cycle Processors

EITF20: Computer Architecture Part2.2.1: Pipeline-1

EITF20: Computer Architecture Part2.2.1: Pipeline-1

CS 110 Computer Architecture. Pipelining. Guest Lecture: Shu Yin. School of Information Science and Technology SIST

EITF20: Computer Architecture Part2.2.1: Pipeline-1

Appendix C: Pipelining: Basic and Intermediate Concepts

Lecture 3. Pipelining. Dr. Soner Onder CS 4431 Michigan Technological University 9/23/2009 1

ECE 4750 Computer Architecture, Fall 2014 T05 FSM and Pipelined Cache Memories

Instruction Pipelining Review

Computer Architecture. Lecture 6.1: Fundamentals of

Instruction Level Parallelism. Appendix C and Chapter 3, HP5e

Minimizing Data hazard Stalls by Forwarding Data Hazard Classification Data Hazards Present in Current MIPS Pipeline

COMPUTER ORGANIZATION AND DESI

Modern Computer Architecture

Overview. Appendix A. Pipelining: Its Natural! Sequential Laundry 6 PM Midnight. Pipelined Laundry: Start work ASAP

Pipelining. Maurizio Palesi

LECTURE 10. Pipelining: Advanced ILP

Instruction Level Parallelism. ILP, Loop level Parallelism Dependences, Hazards Speculation, Branch prediction

ECE/CS 552: Pipeline Hazards

ECE 4750 Computer Architecture, Fall 2017 T05 Integrating Processors and Memories

Appendix A. Overview

Pipeline Overview. Dr. Jiang Li. Adapted from the slides provided by the authors. Jiang Li, Ph.D. Department of Computer Science

Pipelining. Principles of pipelining. Simple pipelining. Structural Hazards. Data Hazards. Control Hazards. Interrupts. Multicycle operations

Some material adapted from Mohamed Younis, UMBC CMSC 611 Spr 2003 course slides Some material adapted from Hennessy & Patterson / 2003 Elsevier

Lecture 7: Pipelining Contd. More pipelining complications: Interrupts and Exceptions

CSE 141 Computer Architecture Spring Lectures 11 Exceptions and Introduction to Pipelining. Announcements

Page 1. Pipelining: Its Natural! Chapter 3. Pipelining. Pipelined Laundry Start work ASAP. Sequential Laundry A B C D. 6 PM Midnight

L19 Pipelined CPU I 1. Where are the registers? Study Chapter 6 of Text. Pipelined CPUs. Comp 411 Fall /07/07

Lecture 3: The Processor (Chapter 4 of textbook) Chapter 4.1

Thomas Polzer Institut für Technische Informatik

ECE 4750 Computer Architecture, Fall 2015 T16 Advanced Processors: VLIW Processors

Computer Architecture Computer Science & Engineering. Chapter 4. The Processor BK TP.HCM

LECTURE 3: THE PROCESSOR

Chapter 4 The Processor 1. Chapter 4A. The Processor

Basic Pipelining Concepts

Pipelined CPUs. Study Chapter 4 of Text. Where are the registers?

Determined by ISA and compiler. We will examine two MIPS implementations. A simplified version A more realistic pipelined version

CMCS Mohamed Younis CMCS 611, Advanced Computer Architecture 1

Predict Not Taken. Revisiting Branch Hazard Solutions. Filling the delay slot (e.g., in the compiler) Delayed Branch

Computer Architecture

There are different characteristics for exceptions. They are as follows:

MIPS Pipelining. Computer Organization Architectures for Embedded Computing. Wednesday 8 October 14

COMPUTER ORGANIZATION AND DESIGN

Department of Computer and IT Engineering University of Kurdistan. Computer Architecture Pipelining. By: Dr. Alireza Abdollahpouri

EI338: Computer Systems and Engineering (Computer Architecture & Operating Systems)

Full Datapath. Chapter 4 The Processor 2

ECE 486/586. Computer Architecture. Lecture # 12

COSC4201 Pipelining. Prof. Mokhtar Aboelaze York University

CS 61C: Great Ideas in Computer Architecture Pipelining and Hazards

The Processor: Improving the performance - Control Hazards

CS 252 Graduate Computer Architecture. Lecture 4: Instruction-Level Parallelism

Chapter 4. The Processor

Lecture 7 Pipelining. Peng Liu.

3/12/2014. Single Cycle (Review) CSE 2021: Computer Organization. Single Cycle with Jump. Multi-Cycle Implementation. Why Multi-Cycle?

Pipelining, Instruction Level Parallelism and Memory in Processors. Advanced Topics ICOM 4215 Computer Architecture and Organization Fall 2010

Pipelining: Basic and Intermediate Concepts

Chapter 4 The Processor (Part 4)

The Big Picture Problem Focus S re r g X r eg A d, M lt2 Sub u, Shi h ft Mac2 M l u t l 1 Mac1 Mac Performance Focus Gate Source Drain BOX

Data Hazards Compiler Scheduling Pipeline scheduling or instruction scheduling: Compiler generates code to eliminate hazard

Chapter 4. The Processor

Chapter 4. The Processor

ECE260: Fundamentals of Computer Engineering

CSEE 3827: Fundamentals of Computer Systems

MIPS An ISA for Pipelining

Pipelining. Principles of pipelining. Simple pipelining. Structural Hazards. Data Hazards. Control Hazards. Interrupts. Multicycle operations

Chapter 4 The Processor 1. Chapter 4B. The Processor

EIE/ENE 334 Microprocessors

CO Computer Architecture and Programming Languages CAPL. Lecture 18 & 19

Chapter 8. Pipelining

CSC 631: High-Performance Computer Architecture

CPE Computer Architecture. Appendix A: Pipelining: Basic and Intermediate Concepts

EECS 151/251A Fall 2017 Digital Design and Integrated Circuits. Instructor: John Wawrzynek and Nicholas Weaver. Lecture 13 EE141

COMPUTER ORGANIZATION AND DESIGN

Working on the Pipeline

Chapter 4. The Processor

Advanced Parallel Architecture Lessons 5 and 6. Annalisa Massini /2017

The Processor: Instruction-Level Parallelism

CS 61C: Great Ideas in Computer Architecture Control and Pipelining

Instr. execution impl. view

(Basic) Processor Pipeline

ECE 505 Computer Architecture

Pipeline Architecture RISC

Spring 2014 Midterm Exam Review

Pipelining. CSC Friday, November 6, 2015

These actions may use different parts of the CPU. Pipelining is when the parts run simultaneously on different instructions.

COMPUTER ORGANIZATION AND DESIGN. 5 th Edition. The Hardware/Software Interface. Chapter 4. The Processor

Advanced issues in pipelining

COMPUTER ORGANIZATION AND DESIGN The Hardware/Software Interface. 5 th. Edition. Chapter 4. The Processor

EN2910A: Advanced Computer Architecture Topic 02: Review of classical concepts

CPS104 Computer Organization and Programming Lecture 19: Pipelining. Robert Wagner

Outline Marquette University

Lecture 4 Pipelining Part II

Computer and Information Sciences College / Computer Science Department Enhancing Performance with Pipelining

CISC 662 Graduate Computer Architecture Lecture 6 - Hazards

Processor (IV) - advanced ILP. Hwansoo Han

Some material adapted from Mohamed Younis, UMBC CMSC 611 Spr 2003 course slides Some material adapted from Hennessy & Patterson / 2003 Elsevier

Chapter 3. Pipelining. EE511 In-Cheol Park, KAIST

Computer Architecture ELEC3441

Multi-cycle Instructions in the Pipeline (Floating Point)

Transcription:

ECE 4750 Computer Architecture, Fall 2014 T03 Pipelined Processors School of Electrical and Computer Engineering Cornell University revision: 2014-09-24-12-26 1 Pipelined Processors 3 2 RAW Data Hazards 10 2.1. Software Scheduling........................... 12 2.2. Hardware Stalling............................. 13 2.3. Hardware Bypassing........................... 16 2.4. RAW Data Hazards Through Memory................. 20 3 Control Hazards 22 3.1. Software Scheduling........................... 24 3.2. Hardware Stalling............................. 25 3.3. Hardware Speculation.......................... 26 3.4. Interrupts and Exceptions........................ 29 4 Structual Hazards 34 4.1. Software Scheduling........................... 35 4.2. Hardware Stalling............................. 36 4.3. Hardware Duplication.......................... 37

5 WAW and WAR Name Hazards 38 5.1. Software Renaming............................ 39 5.2. Hardware Stalling............................. 40 6 Analyzing Processor Performance 41 7 Case Study: MIPS R2K 44 2

1 Pipelined Processors 1. Pipelined Processors Time Program = Instructions Program Cycles Instruction Time Cycles Instructions / program depends on source code, compiler, ISA Cycles / instruction (CPI) depends on ISA, microarchitecture Time / cycle depends upon microarchitecture and implementation Microarchitecture CPI Cycle Time Topic 01 Single-Cycle Processor 1 long Topic 02 FSM Processor >1 short this topic Pipelined Processor 1 short Technology Constraints Assume modern technology where logic is cheap and fast (e.g., fast integer ALU) Assume multi-ported register files with a reasonable number of ports are feasible Assume small amount of very fast memory (caches) backed by large, slower memory imreq Control Memory Control Unit imresp dmreq <1 cycle combinational Status Datapath regfile dmresp 3

1 Pipelined Processors Pipelining laundry analogy Anne, Brian, Cathy, and Dave each have one load of clothes Washing, drying, folding, and storing each take 30 minutes Sequential Laundry 7pm 8pm 9pm 10pm 11pm 12am 1am 2am 3am Anne's Load Ben's Load Cathy's Load Dave's Load Pipelined Laundry 7pm 8pm 9pm 10pm Pipelined Laundry with Slow Dryers 7pm 8pm 9pm 10pm 11pm 12am Anne's Load Ben's Load Cathy's Load Dave's Load Anne's Load Ben's Load Cathy's Load Dave's Load Pipelining lessons Multiple transactions operate simultaneously using different resources Pipelining does not help the transaction latency Pipelining does help the transaction throughput Potential speedup is proportional to the number of pipeline stages Potential speedup is limited by the slowest pipeline stage Potential speedup is reduced by time to fill the pipeline 4

1 Pipelined Processors Visualizing space, time, and transactions with pipeline diagrams 5

1 Pipelined Processors Comparing single-cycle, FSM, and pipelined processors addu addiu mul lw sw j jal jr bne Fetch Instruction Decode Instruction Read Registers Register Arithmetic Read Memory Write Memory Write Registers Update PC Single-Cycle Processor Fetch Inst lw Decode Inst Read Reg Reg Arith Update PC Read Mem Write Reg Fetch Inst addu Decode Inst Read Reg Reg Arith Update PC Write Reg Fetch Inst j Decode Inst Update PC FSM Processor Fetch Inst lw Decode Inst Read Reg Reg Arith Update PC Read Mem Write Reg Fetch Inst addu Decode Inst Read Reg Reg Arith Update PC Write Reg Fetch Inst j Decode Inst Update PC Pipelined Processor Fetch Inst lw Decode Inst Read Reg Reg Arith Update PC Read Mem Fetch Inst addu Write Reg Decode Inst Read Reg Reg Arith Update PC Write Reg Fetch Inst j Decode Inst Update PC 6

1 Pipelined Processors Pipelining a PARCv1 processor Incrementally develop an unpipelined datapath Keep data flowing from left to right Position control signal table early in the diagram Divided datapath/control into stages by inserting pipeline registers Keep the pipeline stages roughly balanced Forward arrows should avoid skipping pipeline registers Backward arrows will need careful consideration Control Signal Table pc_f pc _sel_f br_targ jr j_targ pc_plus4 +4 ir[25:0] ir[15:0] j_tgen br_tgen mul wb_sel_x wb_sel_m rf _w_w rf _wen_w ir[25:21] ir[20:16] regfile (read) alu_ func_x regfile (write) ir[15:0] sext alu op1 _sel_d eq_x imreq imresp data dmreq data dmreq dmresp 7

1 Pipelined Processors Adding a new auto-incrementing load instruction Draw on the above datapath diagram what paths we need to use as well as any new paths we will need to add in order to implement the following auto-incrementing load instruction. lw.ai rt, imm(rs) R[rt] M[ R[rs] + sext(imm) ]; R[rs] R[rs] + 4 Control Signal Table cs_dx cs_xm cs_mw pc_f F Stage D Stage X Stage M Stage W Stage always pc pc_plus4 _sel_f br_targ jr j_targ pc_plus4 +4 pc_plus4 _FD ir_fd ir[25:0] ir[15:0] ir[25:21] ir[20:16] j_tgen br_tgen regfile (read) btarg_dx op0_dx op1_dx mul alu_ func_x wb_sel_x result _XM rf _w_w wb_sel_m rf _wen_w result _MW regfile (write) ir[15:0] sext alu sd_dx op1 _sel_d eq_x sd_xm imreq imresp data dmreq data dmreq dmresp 8

1 Pipelined Processors Pipeline diagrams F D X M W addiu r3, r4, 1 F D X M W addiu r5, r6, 1 F D X M W addiu r3, r4, 1 addiu r5, r6, 1 What would be the total execution time if these three instructions were repeated 10 times? Hazards occur when instructions interact with each other in pipeline RAW Data Hazards: An instruction depends on a data value produced by an earlier instruction Control Hazards: Whether or not an instruction should be executed depends on a control decision made by an earlier instruction Structural Hazards: An instruction in the pipeline needs a resource being used by another instruction in the pipeline WAW and WAR Name Hazards: An instruction in the pipeline is writing a register that an earlier instruction in the pipeline is either writing or reading 9

2 RAW Data Hazards 2. RAW Data Hazards RAW data hazards occur when one instruction depends on a data value produced by a preceding instruction still in the pipeline. We use architectural dependency arrows to illustrate RAW dependencies in assembly code sequences. addiu r3, r1, 1 addiu r4, r3, 1 Using pipeline diagrams to illustrate RAW hazards We use microarchitectural dependency arrows to illustrate RAW hazards on pipeline diagrams. F D X M W addiu r3, r1, 1 F D X M W addiu r4, r3, 1 F D X M W addiu r3, r1, 1 addiu r4, r3, 1 10

2 RAW Data Hazards Approaches to resolving data hazards Software Scheduling: Programmer or compiler explicitly avoids scheduling instructions that would create data hazards Hardware Scheduling: Hardware dynamically schedules instructions to avoid RAW hazards, potentially allowing instructions to execute out of order Hardware Stalling: Hardware includes control logic that freezes later instructions until earlier instruction has finished producing data value Hardware Bypassing: Hardware allows values to be sent from an earlier instruction to a later instruction before the earlier instruction has left the pipeline Hardware Speculation: Hardware guesses that there is no hazard and allows later instructions to potentially read invalid data; detects when there is a problem and re-executes instructions that operated on invalid data 11

2 RAW Data Hazards 2.1. Software Scheduling 2.1. Software Scheduling Insert s to delay read of earlier write. These s count as real instructions increasing instructions per program. addiu r3, r1, 1 addiu r4, r3, 1 Insert independent instructions to delay read of earlier write, and only use s if there is not enough useful work addiu r6, r7, 1 addiu r8, r9, 1 addiu r3, r1, 1 addiu r4, r3, 1 Pipeline diagram showing software scheduling for RAW data hazards addiu r6, r7, 1 addiu r8, r9, 1 addiu r3, r1, 1 addiu r4, r3, 1 12

2 RAW Data Hazards 2.2. Hardware Stalling 2.2. Hardware Stalling Hardware includes control logic that freezes later instructions (in front of pipeline) until earlier instruction (in back of pipeline) has finished producing data value. Pipeline diagram showing hardware stalling for RAW data hazards F D X M W addiu r3, r1, 1 F D addiu r3, r1, 1 addiu r4, r3, 1 13

2 RAW Data Hazards 2.2. Hardware Stalling Modifications to datapath/control to support hardware stalling Stall Signal Control Signal Table cs_dx cs_xm cs_mw pc_f reg_ en_f F Stage D Stage X Stage M Stage W Stage always pc pc_plus4 _sel_f br_targ jr j_targ pc_plus4 +4 pc_plus4 _FD ir_fd reg_ en_d ir[25:0] ir[15:0] ir[25:21] ir[20:16] j_tgen br_tgen regfile (read) btarg_dx op0_dx op1_dx mul alu_ func_x wb_sel_x result _XM rf _w_w wb_sel_m rf _wen_w result _MW regfile (write) insert D ir[15:0] sext sd_dx op1 _sel_d alu eq_x sd_xm imreq imresp data dmreq data dmreq dmresp Deriving the stall signal ren0 r0 ren1 r1 wen w addu addiu mul lw sw j jal jr bne 14

2 RAW Data Hazards 2.2. Hardware Stalling stall_w_x_r0_d = ren0_d && wen_x && (r0_d == w_x) && (w_x!= 0) stall_w_m_r0_d = ren0_d && wen_m && (r0_d == w_m) && (w_m!= 0) stall_w_w_r0_d = ren0_d && wen_w && (r0_d == w_w) && (w_w!= 0) stall_w_x_r1_d = ren1_d && wen_x && (r1_d == w_x) && (w_x!= 0) stall_w_m_r1_d = ren1_d && wen_m && (r1_d == w_m) && (w_m!= 0) stall_w_w_r1_d = ren1_d && wen_w && (r1_d == w_w) && (w_w!= 0) stall = stall_w_x_r0_d stall_w_x_r1_d stall_w_m_r0_d stall_w_m_r1_d stall_w_w_r0_d stall_w_w_r1_d reg_en_f =!stall reg_en_d =!stall insert D = stall 15

2 RAW Data Hazards 2.3. Hardware Bypassing 2.3. Hardware Bypassing Hardware allows values to be sent from an earlier instruction (in back of pipeline) to a later instruction (in front of pipeline) before the earlier instruction has left the pipeline. Sometimes called forwarding. Pipeline diagram showing hardware bypassing for RAW data hazards F D X M W addiu r3, r1, 1 F D X M W addiu r4, r3, 1 F D X M W addiu r3, r1, 1 addiu r4, r3, 1 16

2 RAW Data Hazards 2.3. Hardware Bypassing Adding single bypass path to support limited hardware bypassing Stall & Stall Bypass Signals Control Signal Table cs_dx cs_xm cs_mw pc_f reg_ en_f F Stage D Stage X Stage M Stage W Stage always pc pc_plus4 _sel_f br_targ jr j_targ pc_plus4 +4 pc_plus4 _FD ir_fd reg_ en_d ir[25:0] ir[15:0] ir[25:21] ir[20:16] j_tgen br_tgen regfile (read) op0_byp _sel_d btarg_dx op0_dx op1_dx mul alu_ func_x wb_sel_x result _XM rf _w_w wb_sel_m rf _wen_w result _MW regfile (write) insert D ir[15:0] sext sd_dx op1 _sel_d alu eq_x sd_xm bypass_from_x imreq imresp data dmreq data dmreq dmresp Deriving the bypass and stall signals stall_w_x_r0_d = 0 bypass_w_x_r0_d = ren0_d && wen_x && (r0_d == w_x) && (w_x!= 0) stall = stall_w_x_r0_d stall_w_x_r1_d stall_w_m_r0_d stall_w_m_r1_d stall_w_w_r0_d stall_w_w_r1_d reg_en_f =!stall reg_en_d =!stall insert D = stall 17

2 RAW Data Hazards 2.3. Hardware Bypassing Handling load-use RAW dependencies ALU-use latency is only one cycle, but load-use latency is two cycles. lw r1, 0(r2) F D X M W addiu r3, r1, 1 F D X M W lw r1, 0(r2) addiu r3, r1, 1 lw r1, 0(r2) addiu r3, r1, 1 stall_w_x_r0_d = ren0_d && wen_x && (r0_d == w_x) && (w_x!= 0) && (op_x == lw) bypass_w_x_r0_d = ren0_d && wen_x && (r0_d == w_x) && (w_x!= 0) && (op_x!= lw) stall = stall_w_x_r0_d stall_w_x_r1_d stall_w_m_r0_d stall_w_m_r1_d stall_w_w_r0_d stall_w_w_r1_d reg_en_f =!stall reg_en_d =!stall insert D = stall 18

2 RAW Data Hazards 2.3. Hardware Bypassing Pipeline diagram showing multiple hardware bypass paths addiu r3, r4, 1 addiu r5, r3, 1 addu r6, r1, r3 sw r5, 0(r7) jr r6 Adding all bypass path to support full hardware bypassing Stall & Stall Bypass Signals Control Signal Table cs_dx cs_xm cs_mw pc_f reg_ en_f F Stage D Stage X Stage M Stage W Stage always pc pc_plus4 _sel_f br_targ jr j_targ pc_plus4 +4 pc_plus4 _FD ir_fd reg_ en_d ir[25:0] ir[15:0] ir[25:21] ir[20:16] j_tgen br_tgen regfile (read) op0_byp _sel_d btarg_dx op0_dx op1_dx mul alu_ func_x wb_sel_x result _XM rf _w_w wb_sel_m rf _wen_w result _MW regfile (write) insert D ir[15:0] sext sd_dx op1_ op1 byp sel_d sel_d alu eq_x sd_xm bypass_from_x bypass_from_m bypass_from_w imreq imresp data dmreq data dmreq dmresp 19

2 RAW Data Hazards 2.4. RAW Data Hazards Through Memory 2.4. RAW Data Hazards Through Memory So far we have only studied RAW data hazards through registers, but we must also carefully consider RAW data hazards through memory. sw r1, 0(r2) lw r3, 0(r4) # RAW dependency occurs if R[r2] == R[r4] Pipeline diagrams showing RAW dependency through memory sw r1, 0(r2) F D X M W lw r3, 0(r4) F D X M W sw r1, 0(r2) lw r3, 0(r4) 20

2 RAW Data Hazards 2.4. RAW Data Hazards Through Memory Pipeline diagram for simple assembly sequence Draw a pipeline diagram illustrating how the following assembly sequence would execute on a fully bypassed pipelined PARCv1 processor. Include microarchitectural dependency arrows to illustrate how data is transferred along various bypass paths. lw r1, 0(r2) lw r3, 0(r4) addu r5, r1, r3 sw r5, 0(r6) addiu r2, r2, 4 addiu r4, r4, 4 addiu r6, r6, 4 addiu r7, r7, -1 bne r7, r0, loop 21

3 Control Hazards 3. Control Hazards Control hazards occur when whether or not an instruction should be executed depends on a control decision made by an earlier instruction We use architectural dependency arrows to illustrate control dependencies in assembly code sequences. j foo # Jumps are always taken addiu r3, r4, 1 foo: addiu r5, r6, 1 bne r0, r0, bar # This branch is not taken addiu r7, r8, 1 bar: addiu r9, r10, 1 Using pipeline diagrams to illustrate control hazards We use microarchitectural dependency arrows to illustrate control hazards on pipeline diagrams. Jump resolution latency is two cycles, and branch resolution latency is three cycles. j foo F D X M W addiu r5, r6, 1 F D X M W bne r0, r0, bar F D X M W addiu r7, r8, 1 F D X M W 22

3 Control Hazards j foo addiu r5, r6, 1 bne r0, r0, bar addiu r7, r8, 1 addiu r9, r10, 1 Approaches to resolving control hazards Software Scheduling: Programmer or compiler explicitly avoids scheduling instructions that would create control hazards Software Predication: Programmer or compiler converts control flow into data flow by using instructions that conditionally execute based on a data value Hardware Stalling: Hardware includes control logic that freezes later instructions until earlier instruction have finished determining the correct control flow Hardware Speculation: Hardware guesses which way the control flow will go and potentially fetches incorrect instructions; detects when there is a problem and re-executes instructions the instructions that are along the correct control flow Software Hints: Programmer or compiler provides hints about whether a conditional branch will be taken or not taken, and hardware can use these hints for more efficient hardware speculation 23

3 Control Hazards 3.1. Software Scheduling 3.1. Software Scheduling Expose branch delay slots as part of the instruction set. Branch delay slots are instructions that follow a jump or branch and are always executed regardless of whether a jump or branch is taken or not taken. Compiler tries to insert useful instructions, otherwise inserts s. j foo addiu r3, r4, 1 foo: addiu r5, r6, 1 bne r0, r0, bar addiu r7, r8, 1 bar: addiu r9, r10, 1 Assume we modify the PARCv1 instruction set to specify that J, JAL, and JR instructions have a single-instruction branch delay slot (i.e., one instruction after a J, JAL, and JR is always executed) and the BNE instruction has a two-instruction branch delay slot (i.e., two instructions after a BNE are always executed). Pipeline diagram showing software scheduling for control hazards j foo addiu r5, r6, 1 bne r0, r0, bar addiu r7, r8, 1 addiu r9, r10, 1 24

3 Control Hazards 3.2. Hardware Stalling 3.2. Hardware Stalling Although we could stall to resolve control hazards, the issue is that every instruction essentially creates a control hazard until we know if that instruction is a jump, branch, or other instruction. Stalling will seriously degrade the performance of the common case when we have a sequence of instructions that are not control flow instructions. j foo addiu r5, r6, 1 bne r0, r0, bar addiu r7, r8, 1 addiu r9, r10, 1 25

3 Control Hazards 3.3. Hardware Speculation 3.3. Hardware Speculation Hardware guesses which way the control flow will go and potentially fetches incorrect instructions; detects when there is a problem and re-executes instructions the instructions that are along the correct control flow. For now, we will only consider a simple branch prediction scheme where the hardware always predicts not taken. Pipeline diagram when branch is not taken j foo addiu r3, r4, 1 addiu r5, r6, 1 bne r0, r0, bar addiu r7, r8, 1 addiu r9, r10, 1 Pipeline diagram when branch is taken j foo addiu r3, r4, 1 addiu r5, r6, 1 bne r0, r0, bar addiu r7, r8, 1 addiu r9, r10, 1 addiu r9, r10, 1 26

3 Control Hazards 3.3. Hardware Speculation Modifications to datapath/control to support hardware speculation Stall, & Bypass, Stall & Squash Signals Signals Control Signal Table cs_dx cs_xm cs_mw pc_f reg_ en_f F Stage D Stage X Stage M Stage W Stage always pc pc_plus4 _sel_f br_targ jr j_targ pc_plus4 +4 pc_plus4 _FD ir_fd reg_ en_d ir[25:0] ir[15:0] ir[25:21] ir[20:16] j_tgen br_tgen regfile (read) op0_byp _sel_d btarg_dx op0_dx op1_dx mul alu_ func_x wb_sel_x result _XM rf _w_w wb_sel_m rf _wen_w result _MW regfile (write) insert F insert D ir[15:0] sext sd_dx op1_ op1 byp sel_d sel_d alu eq_x sd_xm bypass_from_x bypass_from_m bypass_from_w imreq imresp data dmreq data dmreq dmresp Deriving the squash signals squash_f = squash_d = br_taken_x (op_d == j) (op_d == jal) (op_d == jr) br_taken_x reg_en_f =!stall squash_f reg_en_d =!stall squash_d insert D = stall squash_d insert F = squash_f 27

3 Control Hazards 3.3. Hardware Speculation Pipeline diagram for simple assembly sequence Draw a pipeline diagram illustrating how the following assembly sequence would execute on a fully bypassed pipelined PARCv1 processor that uses hardware speculation which always predicts not-taken. Unlike the standard PARCv1 processor, you should also assume that we add a single-instruction branch delay slot to the instruction set. So this processor will leverage both software scheduling and hardware speculation. Include microarchitectural dependency arrows to illustrate both data and control flow. bne r0, r3, foo # assume R[rs]!= 0 addiu r4, r5, 1 # instruction in branch delay slot addiu r6, r7, 1... foo: addu r8, r1, r4 addiu r9, r1, 1 28

3 Control Hazards 3.4. Interrupts and Exceptions 3.4. Interrupts and Exceptions Interrupts and exceptions alter the normal control flow of the program. They are caused by an external or internal event that needs to be processed by the system, and these events are usually unexpected or rare from the program s point of view. Asynchronous Interrupts Input/output device needs to be serviced Timer has expired Power distruption or hardware failure Synchronous Exceptions Undefined opcode, privileged instruction Arithmetic overflow, floating-point exception Misaligned memory access for instruction fetch or data access Memory protection violation Virtual memory page faults System calls (traps) to jump into the operating system kernel Interrupts and Exception Semantics Interrupts are asynchronous with respect to the program, so the microarchitecture can decide when to service the interrupt Exceptions are synchronous with respect to the program, so they must be handled immediately 29

3 Control Hazards 3.4. Interrupts and Exceptions To handle an interrupt or exception the hardware/software must: Stop program at current instruction (I), ensure previous insts finished Save cause of interrupt or exception in privileged arch state Save the PC of the instruction I in a special register (EPC) Switch to privileged mode Set the PC to the ess of either the interrupt or the exception handler Disable interrupts Save the user architectural state Check the type of interrupt or exception Handle the interrupt or exception Enable interrupts Switch to user mode Set the PC to EPC if I should be restarted Potentially set PC to EPC+4 if we should skip I Handling a misaligned data ess and syscall exceptions Static code sequence addiu r1, r0, 0x2001 lw r2, 0(r1) syscall opb opc... exception_hander: opd # disable interrupts ope # save user registers opf # check exception type opg # handle exception oph # enable interrupts addiu EPC, EPC, 4 eret Dynamic code sequence addiu r1, r0, 0x2001 lw r2, 0(r1) (excep) opd ope opf opg oph addiu EPC, EPC, 4 eret syscall (excep) opd ope opf 30

3 Control Hazards 3.4. Interrupts and Exceptions Interrupts and Exceptions in a PARCv4 Pipelined Processor F D X M W Inst Address Exceptions Illegal Instruction Arithmetic Overflow Data Address Exceptions How should we handle a single instruction which generates multiple exceptions in different stages as it goes down the pipeline? Exceptions in earlier pipeline stages override later exceptions for a given instruction How should we handle multiple instructions generating exceptions in different stages at the same or different times? We always want the execution to appear as if we have completed executed one instruction before going onto the next instruction So we want to process the exception corresponding to the earliest instruction in program order first Hold exception flags in pipeline until commit point Commit point is after all exceptions could be generated but before any architectural state has been updated To handle an exception at the commit point: update cause and EPC, squash all stages before the commit point, and set PC to exception handler How and where to handle external asynchronous interrupts? Inject asynchronous interrupts at the commit point Asynchronous interrupts will then naturally override exceptions caused by instructions earlier in the pipeline 31

3 Control Hazards 3.4. Interrupts and Exceptions Modifications to datapath/control to support exceptions exception_fd exception_dx exception_xm pc_f reg_ en_f ehandler br_targ jr j_targ pc_plus4 +4 Control Signal Table F Stage D Stage X Stage M Stage W Stage pc _sel_f pc_plus4 _FD ir_fd reg_ en_d Stall & Stall Bypass Signals ir[25:0] ir[15:0] ir[25:21] ir[20:16] j_tgen br_tgen regfile (read) op0_byp _sel_d cs_dx btarg_dx op0_dx op1_dx mul alu_ func_x insert X wb_sel_x cs_xm result _XM Exception Handling Logic cs_mw insert M rf _w_w wb_sel_m rf _wen_w result _MW regfile (write) insert D ir[15:0] sext sd_dx op1_ op1 byp sel_d sel_d alu eq_x sd_xm bypass_from_x bypass_from_m bypass_from_w imreq imresp data dmreq data dmreq dmresp Deriving the squash signals squash_f = (op_d == j) (op_d == jal) (op_d == jr) br_taken_x exception_m squash_d = br_taken_x exception_m squash_x = exception_m squash_m = exception_m 32

3 Control Hazards 3.4. Interrupts and Exceptions Pipeline diagram of exception handling 33

4 Structual Hazards 4. Structual Hazards Structural hazards occur when an instruction in the pipeline needs a resource being used by another instruction in the pipeline. The PARCv1 processor pipeline is specifically designed to avoid any structural hazards. Let s introduce a structural hazard by allowing ADDU, ADDIU, MUL, and JAL instructions to write to the register file in the M stage instead of waiting until the W stage. We would need to add another writeback mux in the W stage and carefully handle bypassing. Using pipeline diagrams to illustrate structural hazards We use structual dependency arrows to illustrate structual hazards on pipeline diagrams. addiu r3, r4, 1 lw r5, 0(r6) addiu r7, r8, 1 Approaches to resolving structural hazards Software Scheduling: Programmer or compiler explicitly avoids scheduling instructions that would create structural hazards Hardware Stalling: Hardware includes control logic that freezes later instructions until earlier instruction has finished using the shared resource Hardware Duplication: Add more hardware so that each instruction can access separate resources at the same time 34

4 Structual Hazards 4.1. Software Scheduling 4.1. Software Scheduling Insert independent instructions or s to delay an ADDU, ADDIU, MUL, or JAL instructions if they follow a LW instruction. addiu r3, r4, 1 lw r5, 0(r6) addiu r7, r8, 1 Pipeline diagram showing software scheduling for structural hazards addiu r3, r4, 1 lw r5, 0(r6) addiu r7, r8, 1 35

4 Structual Hazards 4.2. Hardware Stalling 4.2. Hardware Stalling Hardware includes control logic that freezes an ADDU, ADDIU, MUL, or JAL instruction if a LW instruction is ahead in the pipeline. Pipeline diagram showing hardware stalling for structural hazards addiu r3, r4, 1 lw r5, 0(r6) addiu r7, r8, 1 Deriving the stall signal stall_wport_hazard_d = (op_x == lw) && ( (op_d == ADDU) (op_d == ADDIU) (op_d == MUL) (op_d == JAL) ) stall = stall_w_x_r0_d stall_w_x_r1_d stall_w_m_r0_d stall_w_m_r1_d stall_w_w_r0_d stall_w_w_r1_d stall_wport_hazard_d Notice that we stall far before the point when the structural hazard actually occurs, because we know exactly how instructions move down the pipeline. Also possible to use dynamic arbitration in the back of the pipeline. 36

4 Structual Hazards 4.3. Hardware Duplication 4.3. Hardware Duplication Add a second write port so that an ADDU, ADDIU, MUL, or JAL instruction can writeback to the register file at the same time as a LW. Stall, & Bypass, Stall & Squash Signals Signals Control Signal Table cs_dx cs_xm cs_mw pc_f reg_ en_f F Stage D Stage X Stage M Stage W Stage always pc pc_plus4 _sel_f br_targ jr j_targ pc_plus4 +4 pc_plus4 _FD ir_fd reg_ en_d ir[25:0] ir[15:0] ir[25:21] ir[20:16] j_tgen br_tgen regfile (read) op0_byp _sel_d btarg_dx op0_dx op1_dx mul alu_ func_x wb_sel_x result _XM rf _w_w wb_sel_m rf _wen_w result _MW regfile (write) insert F insert D ir[15:0] sext sd_dx op1_ op1 byp sel_d sel_d alu eq_x sd_xm rf _wen1_w rf _w1_w bypass_from_x bypass_from_m bypass_from_w imreq imresp data dmreq data dmreq dmresp Does allowing early writeback help performance in the first place? addiu r3, r1, 1 addiu r4, r3, 1 addiu r5, r4, 1 addiu r6, r5, 1 addiu r7, r6, 1 37

5 WAW and WAR Name Hazards 5. WAW and WAR Name Hazards WAW dependencies occur when an instruction overwrites a register than an earlier instruction has already written. WAR dependencies occur when an instruction writes a register than an earlier instruction needs to read. We use architectural dependency arrows to illustrate WAW and WAR dependencies in assembly code sequences. mul r1, r2, r3 addiu r4, r1, 1 addiu r1, r5, 1 WAW name hazards occur when an instruction in the pipeline writes a register before an earlier instruction (in back of the pipeline) has had a chance to write that same register. WAR name hazards occur when an instruction in the pipeline writes a register before an earlier instuction (in back of pipeline) has had a chance to read that same register. The PARCv1 processor pipeline is specifically designed to avoid any WAW or WAR name hazards. Instructions always write the registerfile in-order in the same stage, and instructions always read registers in the front of the pipeline and write registers in the back of the pipeline. Let s introduce a WAW name hazard by using an iterative variable latency multiplier, and allowing other instructions to continue executing while the multiplier is working. 38

5 WAW and WAR Name Hazards 5.1. Software Renaming Using pipeline diagrams to illustrate WAW name hazards We use microarchitectural dependency arrows to illustrate WAW hazards on pipeline diagrams. mul r1, r2, r3 addiu r4, r1, 1 addiu r1, r5, 1 Approaches to resolving structural hazards Software Renaming: Programmer or compiler changes the register names to avoid creating name hazards Hardware Renaming: Hardware dynamically changes the register names to avoid creating name hazards Hardware Stalling: Hardware includes control logic that freezes later instructions until earlier instruction has finished either writing or reading the problematic register name 5.1. Software Renaming As long as we have enough architectural registers, renaming registers in software is easy. WAW and WAR dependencies occur because we have a finite number of architectural registers. mul r1, r2, r3 addiu r4, r1, 1 addiu r6, r5, 1 39

5 WAW and WAR Name Hazards 5.2. Hardware Stalling 5.2. Hardware Stalling Simplest approach is to add stall logic in the decode stage similar to what the approach used to resolve other hazards. mul r1, r2, r3 addiu r4, r1, 1 addiu r1, r5, 1 Deriving the stall signal stall_struct_hazard_d = (op_d == MUL) &&!imul_rdy_d stall_waw_hazard_d = wen_d && wen_z && (w_d == w_z) && (w_z!= 0) stall = stall_w_x_r0_d stall_w_x_r1_d stall_w_m_r0_d stall_w_m_r1_d stall_w_w_r0_d stall_w_w_r1_d stall_struct_hazard_d stall_waw_hazard_d 40

6 Analyzing Processor Performance 6. Analyzing Processor Performance Time Program = Instructions Program Cycles Instruction Time Cycles Estimating cycle time Stall, & Bypass, Stall & Squash Signals Signals Control Signal Table cs_dx cs_xm cs_mw pc_f reg_ en_f F Stage D Stage X Stage M Stage W Stage always pc pc_plus4 _sel_f br_targ jr j_targ pc_plus4 +4 pc_plus4 _FD ir_fd reg_ en_d ir[25:0] ir[15:0] ir[25:21] ir[20:16] j_tgen br_tgen regfile (read) op0_byp _sel_d btarg_dx op0_dx op1_dx mul alu_ func_x wb_sel_x result _XM rf _w_w wb_sel_m rf _wen_w result _MW regfile (write) insert F insert D ir[15:0] sext sd_dx op1_ op1 byp sel_d sel_d alu eq_x sd_xm bypass_from_x bypass_from_m bypass_from_w imreq imresp data dmreq data dmreq dmresp register read = 1τ register write = 1τ regfile read = 10τ regfile write = 10τ memory read = 20τ memory write = 20τ +4 unit sext unit br_tgen j_tgen mux multiplier alu = 4τ = 1τ = 8τ = 1τ = 3τ = 20τ = 10τ 41

6 Analyzing Processor Performance Estimating execution time Using our first-order equation for processor performance, how long in τ will it take to execute the vvadd example assuming n is 64? loop: lw r12, 0(r4) lw r13, 0(r5) addu r14, r12, r13 sw r14, 0(r6) addiu r4, r4, 4 addiu r5, r5, 4 addiu r6, r6, 4 addiu r7, r7, -1 bne r7, r0, loop jr r31 lw lw addu sw addiu addiu addiu addiu bne opa opb lw 42

6 Analyzing Processor Performance Using our first-order equation for processor performance, how long in τ will it take to execute the mystery program assuming n is 64 and that we find a match on the last element. addiu r12, r0, 0 loop: lw r13, 0(r4) bne r13, r6, foo addiu r2, r12, 0 jr r31 foo: addiu r4, r4, 4 addiu r12, r12, 1 bne r12, r5, loop addiu r2, r0, -1 jr r31 43

7 Case Study: MIPS R2K 7. Case Study: MIPS R2K MIPSI MIPS R2K is one of the first popular pipelined RISC processors MIPS R2K implements the MIPS I instruction set MIPS = Microprocessor without Interlocked Pipeline Stages MIPSIII MIPSIV MIPS64 MIPSII MIPS32 MIPS16 MIPS I used software scheduling to avoid some RAW hazards by including a single-instruction load-use delay slot MIPS I used software scheduling to avoid some control hazards by including a single-instruction branch delay slot One-Instr Branch Delay Slot j foo addiu r3, r4, 1 # BDS... foo: addiu r5, r6, 1 bne r7, r8, bar addiu r9, r10, 1 # BDS... bar: Present in all MIPS instruction sets; not possible to depricate and still enable legacy code to execute on new microarchitectures One-Instr Load-Use Delay Slot lw r1, 0(r2) lw r3, 0(r4) addiu r2, r2, 4 # LDS addu r5, r1, r3 Deprecated in MIPS II instruction set; legacy code can still execute on new microarchitectures, but code using the MIPS II instruction set can rely in hardware stalling 44

7 Case Study: MIPS R2K MIPS R2K Microarchitecture The pipelined datapath and control were located on a single die. Cache control and memory management unit were also integrated on-die, but the actual tag and data storage for the cache was located off-chip. Control Unit Datapath I$ Controller D$ Controller I$ (4-32KB) D$ (4-32KB) Used two-phase clocking to enable five pipeline stages to fit into four clock cycles. This avoided the need for explicit bypassing from the W 1.2 The MIPS Five-Stage Pipeline 5 stage to the end of the D stage. Instruction sequence Instruction 1 Instruction 2 IF from I-cache RD from register file ALU MEM from D-cache WB to register file IF RD ALU MEM WB Instruction 3 IF RD ALU MEM WB Time FIGURE 1.2 MIPS five-stage pipeline. Two-phase clocking enabled a single-cycle branch resolution latency since register read, branch ess generation, and branch comparison can fit in a single cycle. Branch instruction Branch delay Branch target IF fetch data from the cache; 1.5 a cache MIPSmiss Compared is a relatively with CISC rare event Architectures and we can just 27 stop the CPU when it happens (though cleverer CPUs find more useful things to do). The MIPS architecture was planned with separate instruction and data caches, so it can fetch an instruction and read or write a memory variable simultaneously. Branch RD CISC ess MEM WB architectures have caches too, but they re most often afterthoughts, fitted in as a feature of the memory system. A RISC architecture makes more sense if you regard the caches as very much part of the CPU and tied firmly into the pipeline. IF RD ALU MEM WB 1.2 The MIPS Five-Stage Pipeline FIGURE 1.3 The pipeline and branch delays. IF RD MEM The MIPS architecture is made for pipelining, and Figure 1.2 is close to the earliest MIPS CPUs and typical of many. So long as the CPU runs from the cache, the execution of every MIPS instruction is divided into five phases, called pipestages, with each pipestage taking a fixed amount of time. The fixed amount of time is usually a processor clock cycle (though some actions take only half a clock, so the MIPS five-stage pipeline actually occupies only four clock cycles). on a MIPS CPU without using some registers). For a program running 45 Allininstructions any systemare that rigidly takes defined interrupts so or they traps, can the follow values the of same these sequence registers of pipestages, may change even at where any time, the instruction so you d better does nothing use at them. some stage. The net ALU WB

7 Case Study: MIPS R2K MIPS R2K VLSI Design Process: 2 µm, two metal layers Clock Frequency: 8 15 MHz Size: 110K transistors, 80 mm 2 46