+ All Categories
Home > Documents > cse431-06mipspipeline

cse431-06mipspipeline

Date post: 02-Jun-2018
Category:
Upload: dilshad-sabir
View: 213 times
Download: 0 times
Share this document with a friend

of 25

Transcript
  • 8/11/2019 cse431-06mipspipeline

    1/25

    CSE431 L06 Basic MIPS Pipelining.1 Irwin, PSU, 2005

    CSE 431Computer ArchitectureFall 2005

    Lecture 06: Basic MIPS Pipelining Review

    Mary Jane Irwin ( www.cse.psu.edu/~mji)

    www.cse.psu.edu/~cg431

    [Adapted from Computer Organization and Design,

    Patterson & Hennessy, 2005, UCB]

    http://www.cse.psu.edu/~mjihttp://www.cse.psu.edu/~http://www.cse.psu.edu/~http://www.cse.psu.edu/~mji
  • 8/11/2019 cse431-06mipspipeline

    2/25

    CSE431 L06 Basic MIPS Pipelining.2 Irwin, PSU, 2005

    Review: Single Cycle vs. Multiple Cycle Timing

    Clk Cycle 1

    Multiple Cycle Implementation:

    IFetch Dec Exec Mem WB

    Cycle 2 Cycle 3 Cycle 4 Cycle 5 Cycle 6 Cycle 7 Cycle 8 Cycle 9

    Cycle 10

    IFetch Dec Exec Mem

    lw sw

    IFetch

    R-type

    Clk

    Single Cycle Implementation:

    lw sw Waste

    Cycle 1 Cycle 2

    multicycle clock

    slower than 1/5thofsingle cycle clockdue to stage registeroverhead

  • 8/11/2019 cse431-06mipspipeline

    3/25

    CSE431 L06 Basic MIPS Pipelining.3 Irwin, PSU, 2005

    How Can We Make It Even Faster?

    Split the multiple instruction cycle into smaller and

    smaller steps There is a point of diminishing returns where as much time is

    spent loading the state registers as doing the work

    Start fetching and executing the next instruction before

    the current one has completed Pipelining(all?) modern processors are pipelined for

    performance

    Remember theperformance equation:

    CPU time = CPI * CC * IC

    Fetch (and execute) more than one instruction at a time

    Superscalar processingstay tuned

  • 8/11/2019 cse431-06mipspipeline

    4/25

    CSE431 L06 Basic MIPS Pipelining.4 Irwin, PSU, 2005

    A Pipelined MIPS Processor

    Start the nextinstruction before the current one hascompleted

    improves throughput- total amount of work done in a given time

    instruction latency(execution time, delay time, response time -time from the start of an instruction to its completion) is notreduced

    Cycle 1 Cycle 2 Cycle 3 Cycle 4 Cycle 5

    IFetch Dec Exec Mem WBlw

    Cycle 7Cycle 6 Cycle 8

    sw IFetch Dec Exec Mem WB

    R-type IFetch Dec Exec Mem WB

    - clock cycle (pipeline stage time) is limited by the slowest stage

    - for some instructions, some stages are wastedcycles

  • 8/11/2019 cse431-06mipspipeline

    5/25

    CSE431 L06 Basic MIPS Pipelining.5 Irwin, PSU, 2005

    Single Cycle, Multiple Cycle, vs. Pipeline

    Multiple Cycle Implementation:

    Clk

    Cycle 1

    IFetch Dec Exec Mem WB

    Cycle 2 Cycle 3 Cycle 4 Cycle 5 Cycle 6 Cycle 7 Cycle 8 Cycle 9

    Cycle 10

    IFetch Dec Exec Mem

    lw sw

    IFetch

    R-type

    lw IFetch Dec Exec Mem WB

    Pipeline Implementation:

    IFetch Dec Exec Mem WBsw

    IFetch Dec Exec Mem WBR-type

    Clk

    Single Cycle Implementation:

    lw sw Waste

    Cycle 1 Cycle 2

  • 8/11/2019 cse431-06mipspipeline

    6/25

    CSE431 L06 Basic MIPS Pipelining.6 Irwin, PSU, 2005

    MIPS Pipeline Datapath Modifications What do we need to add/modify in our MIPS datapath?

    State registersbetween each pipeline stage to isolatethem

    ReadAddress

    Instruction

    Memory

    Add

    PC

    4

    Write Data

    Read Addr 1

    Read Addr 2

    Write Addr

    Register

    File

    ReadData 1

    ReadData 2

    16 32

    ALU

    Shift

    left 2

    Add

    Data

    Memory

    Address

    Write Data

    Read

    DataIFetch/Dec

    De

    c/Exec

    Exe

    c/Mem

    Mem/WB

    IF:IFetch ID:Dec EX:Execute MEM:

    MemAccess

    WB:

    WriteBack

    System Clock

    Sign

    Extend

  • 8/11/2019 cse431-06mipspipeline

    7/25CSE431 L06 Basic MIPS Pipelining.7 Irwin, PSU, 2005

    Pipelining the MIPS ISA

    What makes it easy all instructions are the same length (32 bits)

    - can fetch in the 1ststage and decode in the 2ndstage

    few instruction formats (three) with symmetryacross formats

    - can begin reading register file in 2ndstage

    memory operations can occur only in loads and stores

    - can use the execute stage to calculate memory addresses

    each MIPS instruction writes at most one result (i.e.,changes the machine state) and does so near the end of thepipeline (MEM and WB)

    What makes it hard structural hazards: what if we had only one memory?

    control hazards: what about branches?

    data hazards: what if an instructions input operands dependon the output of a previous instruction?

  • 8/11/2019 cse431-06mipspipeline

    8/25CSE431 L06 Basic MIPS Pipelining.8 Irwin, PSU, 2005

    Graphically Representing MIPS Pipeline

    Can help with answering questions like:

    How many cycles does it take to execute this code?

    What is the ALU doing during cycle 4?

    Is there a hazard, why does it occur, and how can it be fixed?

    ALUIM Reg DM Reg

  • 8/11/2019 cse431-06mipspipeline

    9/25CSE431 L06 Basic MIPS Pipelining.9 Irwin, PSU, 2005

    Why Pipeline? For Performance!

    I

    n

    s

    t

    r.

    O

    r

    d

    e

    r

    Time (clock cycles)

    Inst 0

    Inst 1

    Inst 2

    Inst 4

    Inst 3

    ALU

    IM Reg DM Reg

    ALU

    IM Reg DM Reg

    ALU

    IM Reg DM Reg

    ALU

    IM Reg DM Reg

    ALU

    IM Reg DM Reg

    Once thepipeline is full,one instruction

    is completedevery cycle, so

    CPI = 1

    Time to fill the pipeline

  • 8/11/2019 cse431-06mipspipeline

    10/25CSE431 L06 Basic MIPS Pipelining.10 Irwin, PSU, 2005

    Can Pipelining Get Us Into Trouble?

    Yes: Pipeline Hazards

    structural hazards: attempt to use the same resource by twodifferent instructions at the same time

    data hazards: attempt to use data before it is ready

    - An instructions source operand(s) are produced by a priorinstruction still in the pipeline

    control hazards: attempt to make a decision about programcontrol flow before the condition has been evaluated and thenew PC target address calculated

    - branch instructions

    Can always resolve hazards by waiting

    pipeline control must detect the hazard

    and take action to resolve hazards

  • 8/11/2019 cse431-06mipspipeline

    11/25CSE431 L06 Basic MIPS Pipelining.11 Irwin, PSU, 2005

    I

    n

    s

    t

    r.

    O

    r

    d

    e

    r

    Time (clock cycles)

    lw

    Inst 1

    Inst 2

    Inst 4

    Inst 3

    ALU

    Mem Reg Mem Reg

    ALU

    Mem Reg Mem Reg

    ALU

    Mem Reg Mem Reg

    ALU

    Mem Reg Mem Reg

    ALU

    Mem Reg Mem Reg

    A Single Memory Would Be a Structural Hazard

    Reading data frommemory

    Reading instructionfrom memory

    Fix with separate instr and data memories (I$ and D$)

  • 8/11/2019 cse431-06mipspipeline

    12/25CSE431 L06 Basic MIPS Pipelining.13 Irwin, PSU, 2005

    How About Register File Access?

    I

    n

    s

    t

    r.

    O

    r

    d

    e

    r

    Time (clock cycles)

    Inst 1

    Inst 2

    ALU

    IM Reg DM Reg

    ALU

    IM Reg DM Reg

    ALU

    IM Reg DM Reg

    ALU

    IM Reg DM Reg

    Fix register fileaccess hazard bydoing reads in thesecond half of the

    cycle and writes in

    the first half

    add $1,

    add $2,$1,

    clock edge that controlsregister writing

    clock edge that controlsloading of pipeline state

    registers

  • 8/11/2019 cse431-06mipspipeline

    13/25CSE431 L06 Basic MIPS Pipelining.15 Irwin, PSU, 2005

    Register Usage Can Cause Data Hazards

    ALU

    IM Reg DM Reg

    ALU

    IM Reg DM Reg

    ALU

    IM Reg DM Reg

    ALU

    IM Reg DM Reg

    ALU

    IM Reg DM Reg

    Dependencies backward in time cause hazards

    add $1,

    sub $4,$1,$5

    and $6,$1,$7

    xor $4,$1,$5

    or $8,$1,$9

    Read before writedata hazard

  • 8/11/2019 cse431-06mipspipeline

    14/25

    CSE431 L06 Basic MIPS Pipelining.16 Irwin, PSU, 2005

    Loads Can Cause Data Hazards

    I

    n

    s

    t

    r.

    O

    r

    d

    e

    r

    lw $1,4($2)

    sub $4,$1,$5

    and $6,$1,$7

    xor $4,$1,$5

    or $8,$1,$9

    ALU

    IM Reg DM Reg

    ALU

    IM Reg DM Reg

    ALU

    IM Reg DM Reg

    ALU

    IM Reg DM Reg

    ALUIM Reg DM Reg

    Dependencies backward in time cause hazards

    Load-usedata hazard

  • 8/11/2019 cse431-06mipspipeline

    15/25

    CSE431 L06 Basic MIPS Pipelining.17 Irwin, PSU, 2005

    stall

    stall

    One Way to Fix a Data Hazard

    I

    n

    s

    t

    r.

    O

    r

    d

    e

    r

    add $1,ALU

    IM Reg DM Reg

    sub $4,$1,$5

    and $6,$1,$7

    ALU

    IM Reg DM Reg

    ALU

    IM Reg DM Reg

    Can fix data

    hazard bywaitingstallbut impacts CPI

  • 8/11/2019 cse431-06mipspipeline

    16/25

    CSE431 L06 Basic MIPS Pipelining.19 Irwin, PSU, 2005

    Another Way to Fix a Data Hazard

    A

    LU

    IM Reg DM Reg

    ALU

    IM Reg DM Reg

    ALU

    IM Reg DM Reg

    Fix data hazards

    by forwardingresults as soon asthey are availableto where they are

    needed

    ALU

    IM Reg DM Reg

    ALU

    IM Reg DM Reg

    I

    n

    s

    t

    r.

    O

    r

    d

    e

    r

    add $1,

    sub $4,$1,$5

    and $6,$1,$7

    xor $4,$1,$5

    or $8,$1,$9

  • 8/11/2019 cse431-06mipspipeline

    17/25

    CSE431 L06 Basic MIPS Pipelining.21 Irwin, PSU, 2005

    Forwarding with Load-use Data Hazards

    ALU

    IM Reg DM Reg

    ALU

    IM Reg DM Reg

    ALU

    IM Reg DM Reg

    ALU

    IM Reg DM Reg

    ALUIM Reg DM Reg

    Will still need one stall cycleeven with forwarding

    I

    n

    s

    t

    r.

    O

    r

    d

    e

    r

    lw $1,4($2)

    sub $4,$1,$5

    and $6,$1,$7

    xor $4,$1,$5

    or $8,$1,$9

  • 8/11/2019 cse431-06mipspipeline

    18/25

    CSE431 L06 Basic MIPS Pipelining.22 Irwin, PSU, 2005

    Branch Instructions Cause Control Hazards

    I

    n

    s

    t

    r.

    O

    r

    d

    e

    r

    lw

    Inst 4

    Inst 3

    beqALUIM Reg DM Reg

    ALU

    IM Reg DM Reg

    ALU

    IM Reg DM Reg

    ALU

    IM Reg DM Reg

    Dependencies backward in time cause hazards

  • 8/11/2019 cse431-06mipspipeline

    19/25

    CSE431 L06 Basic MIPS Pipelining.23 Irwin, PSU, 2005

    stall

    stall

    stall

    One Way to Fix a Control Hazard

    In

    s

    t

    r.

    Or

    d

    e

    r

    beq

    A

    LUIM Reg DM Reg

    lw

    AL

    UIM Reg DM Reg

    ALUInst 3

    IM Reg DM

    Fix branch

    hazard bywaiting

    stallbutaffects CPI

  • 8/11/2019 cse431-06mipspipeline

    20/25

    CSE431 L06 Basic MIPS Pipelining.25 Irwin, PSU, 2005

    Corrected Datapath to Save RegWrite Addr Need to preserve the destination register address in

    the pipeline state registers

    ReadAddress

    Instruction

    Memory

    Add

    PC

    4

    Write Data

    Read Addr 1

    Read Addr 2

    Write Addr

    Register

    File

    ReadData 1

    ReadData 2

    16 32

    ALU

    Shiftleft 2

    Add

    Data

    Memory

    Address

    Write Data

    ReadData

    IF/ID

    Sign

    Extend

    ID/EX EX/MEM

    MEM/WB

  • 8/11/2019 cse431-06mipspipeline

    21/25

    CSE431 L06 Basic MIPS Pipelining.26 Irwin, PSU, 2005

    MIPS Pipeline Control Path ModificationsAll control signals can be determined during Decode

    and held in thestate registersbetween pipeline stages

    ReadAddress

    Instruction

    Memory

    Add

    PC

    4

    Write Data

    Read Addr 1

    Read Addr 2

    Write Addr

    Register

    File

    ReadData 1

    Read

    Data 2

    16 32

    ALU

    Shift

    left 2

    Add

    Data

    Memory

    Address

    Write Data

    ReadData

    IF/ID

    Sign

    Extend

    ID/EX

    EX/MEM

    MEM/WB

    Control

  • 8/11/2019 cse431-06mipspipeline

    22/25

    CSE431 L06 Basic MIPS Pipelining.27 Irwin, PSU, 2005

    Other Pipeline Structures Are Possible

    What about the (slow) multiply operation?

    Make the clock twice as slow or

    let it take two cycles (since it doesnt use the DM stage)

    ALU

    IM Reg DM Reg

    MUL

    ALU

    IM Reg DM1 RegDM2

    What if the data memory access is twice as slow asthe instruction memory?

    make the clock twice as slow or

    let data memory access take two cycles (and keep the sameclock rate)

  • 8/11/2019 cse431-06mipspipeline

    23/25

    CSE431 L06 Basic MIPS Pipelining.28 Irwin, PSU, 2005

    Sample Pipeline Alternatives

    ARM7

    StrongARM-1

    XScale

    A

    LUIM1 IM2 DM1

    Reg

    DM2

    IM Reg EX

    PC updateIM access

    decodereg

    access

    ALU opDM accessshift/rotatecommit result

    (write back)AL

    UIM Reg DM Reg

    Reg SHFT

    PC updateBTB access

    start IM access

    IM access

    decodereg 1 access

    shift/rotatereg 2 access

    ALU op

    start DM accessexception

    DM writereg write

  • 8/11/2019 cse431-06mipspipeline

    24/25

    CSE431 L06 Basic MIPS Pipelining.29 Irwin, PSU, 2005

    Summary

    All modern day processors use pipelining

    Pipelining doesnt help latencyof single task, it helpsthroughputof entire workload

    Potential speedup: a CPI of 1 and fast a CC

    Pipeline rate limited by slowestpipeline stage

    Unbalanced pipe stages makes for inefficiencies

    The time to fill pipeline and time to drain it can impactspeedup for deep pipelines and short code runs

    Must detect and resolve hazards

    Stalling negatively affects CPI (makes CPI less than the idealof 1)

  • 8/11/2019 cse431-06mipspipeline

    25/25

    CSE431 L06 Basic MIPS Pipelining 30 Irwin PSU 2005

    Next Lecture and Reminders

    Next lecture

    Overcoming data hazards

    - Reading assignmentPH, Chapter 6.4-6.5

    Reminders

    HW2 due September 29th

    SimpleScalar tutorials scheduled

    - Thursday, Sept 22, 5:30-6:30 pm in 218 IST

    Evening midterm exam scheduled

    - Tuesday, October 18th, 20:15 to 22:15, Location 113 IST- You should have let me know by now if you have a conflict


Recommended