Pipeline Hazards

94
 Pipeline Hazards CSCE430/830 Pipeline: Hazards CSCE430/830 Computer Architecture Lecturer: Prof Hon! "ian! Courtes# of Prof $ifen! %hu& ' of (aine )all& *00+ Portions of these slides are deri,ed from: -a,e Patterson . 'C

description

PH

Transcript of Pipeline Hazards

  • 5/18/2018 Pipeline Hazards

    1/94Pipeline HazardsCSCE430/830

    Pipeline: Hazards

    CSCE430/830 Computer Architecture

    Lecturer: Prof Hon! "ian!

    Courtes# of Prof $ifen! %hu& ' of (aine

    )all& *00+

    Portions of these slides are deri,ed from:-a,e Patterson . 'C

  • 5/18/2018 Pipeline Hazards

    2/94Pipeline HazardsCSCE430/830

    Pipelinin! utline

    1ntroduction -efinin! Pipelinin!

    Pipelinin! 1nstructions

    Hazards

    Structural hazards -ata Hazards

    Control Hazards

    Performance

    Controller implementation

  • 5/18/2018 Pipeline Hazards

    3/94Pipeline HazardsCSCE430/830

    Pipeline Hazards

    2here one instruction cannot immediatel#follo another

    #pes of hazards Structural hazards5 attempt to use the same resource 6#

    to or more instructions Control hazards5 attempt to ma7e 6ranchin! decisions

    6efore 6ranch condition is e,aluated

    -ata hazards5 attempt to use data 6efore it is read#

    Can ala#s resol,e hazards 6# aitin!

  • 5/18/2018 Pipeline Hazards

    4/94Pipeline HazardsCSCE430/830

    Structural Hazards

    Attempt to use the same resource 6# to ormore instructions at the same time

    Eample: Sin!le (emor# for instructions anddata

    Accessed 6# 1) sta!e Accessed at same time 6# (E( sta!e

    Solutions -ela# the second access 6# one cloc7 c#cle& 9

    Pro,ide separate memories for instructions data

    his is hat the 6oo7 does

    his is called a ;Har,ard Architecture

    L2

    5

    RD1

    RD2

    RN1

    RN2

    WN

    WD

    RegisterFile

    ALU

    EXTND

    16 32

    RD

    WD

    Data

    Memory

    ADDR

    32

    MUX

  • 5/18/2018 Pipeline Hazards

    7/94Pipeline HazardsCSCE430/830

    5

    RD1

    RD2

    RN1

    RN2

    WN

    WD

    RegisterFile

    ALU

    EXTND

    16 32

    RD

    WD

    Data

    Memory

    ADDR

    32

    MUX

  • 5/18/2018 Pipeline Hazards

    8/94

    Pipeline HazardsCSCE430/830

    5

    RD1

    RD2

    RN1

    RN2

    WN

    WD

    RegisterFile

    ALU

    EXTND

    16 32

    RD

    WD

    Data

    Memory

    ADDR

    32

    MUX

  • 5/18/2018 Pipeline Hazards

    9/94

    Pipeline HazardsCSCE430/830

    5

    RD1

    RD2

    RN1

    RN2

    WN

    WD

    RegisterFile

    ALU

    EXTND

    16 32

    RD

    WD

    Data

    Memory

    ADDR

    32

    MUX

  • 5/18/2018 Pipeline Hazards

    10/94

    Pipeline HazardsCSCE430/830

    Eecutin! (ultiple 1nstructionsCloc7 C#cle ?

    5

    RD1

    RD2

    RN1

    RN2

    WN

    WD

    RegisterFile

    ALU

    EXTND

    16 32

    RD

    WD

    Data

    Memory

    ADDR

    32

    MUX

  • 5/18/2018 Pipeline Hazards

    11/94

    Pipeline HazardsCSCE430/830

    Eecutin! (ultiple 1nstructionsCloc7 C#cle +

    5

    RD1

    RD2

    RN1

    RN2

    WN

    WD

    RegisterFile

    ALU

    EXTND

    16 32

    RD

    WD

    Data

    Memory

    ADDR

    32

    MUX

  • 5/18/2018 Pipeline Hazards

    12/94

    Pipeline HazardsCSCE430/830

    Eecutin! (ultiple 1nstructionsCloc7 C#cle @

    5

    RD1

    RD2

    RN1

    RN2

    WN

    WD

    RegisterFile

    ALU

    EXTND

    16 32

    RD

    WD

    Data

    Memory

    ADDR

    32

    MUX

  • 5/18/2018 Pipeline Hazards

    13/94

    Pipeline HazardsCSCE430/830

    Eecutin! (ultiple 1nstructionsCloc7 C#cle 8

    5

    RD1

    RD2

    RN1

    RN2

    WN

    WD

    RegisterFile

    ALU

    EXTND

    16 32

    RD

    WD

    Data

    Memory

    ADDR

    32

    MUX

  • 5/18/2018 Pipeline Hazards

    14/94

    Pipeline HazardsCSCE430/830

    Alternati,e ie 5 (ultic#cle -ia!ram

    IM REG ALU DM REGlw $r0, 10($r1)

    sw $r3, 20($r4)

    add $r5, $r6, $r7

    CC 1 CC 2 CC 3 CC 4 CC 5 CC 6 CC 7

    IM REG ALU DM REG

    IM REG ALU DM REG

    sub $r8, $r9, $r10 IM REG ALU DM REG

    CC 8

  • 5/18/2018 Pipeline Hazards

    15/94

    Pipeline HazardsCSCE430/830

    Alternati,e ie 5 (ultic#cle -ia!ram

    IM REG ALU DM REGlw $r0, 10($r1)

    sw $r3, 20($r4)

    add $r5, $r6, $r7

    CC 1 CC 2 CC 3 CC 4 CC 5 CC 6 CC 7

    IM REG ALU DM REG

    IM REG ALU DM REG

    sub $r8, $r9, $r10 IM REG ALU DM REG

    CC 8

    (emor# Conflict

  • 5/18/2018 Pipeline Hazards

    16/94

    Pipeline HazardsCSCE430/830

    ne (emor# Port Structural Hazards

    I

    nstr.

    Order

    Time (clock cycles)

    Load

    Instr 1

    Instr 2

    Stall

    Instr 3

    Reg

    ALU DMemIfetch Reg

    Reg

    ALU DMemIfetch Reg

    Reg

    ALU DMemIfetch Reg

    Cycle 1 Cycle Cycle ! Cycle " Cycle # Cycle $Cycle %

    Reg

    ALU DMemIfetch Reg

    &'le &'le &'le &'le&'le

  • 5/18/2018 Pipeline Hazards

    17/94

    Pipeline HazardsCSCE430/830

    Structural Hazards

    Some common Structural Hazards: (emor#:

    eB,e alread# mentioned this one

    )loatin! point: Since man# floatin! point instructions re=uire man# c#cles& itBs eas# for

    them to interfere ith each other Startin! up more of one t#pe of instruction than there are

    resources )or instance& the PA58+00 can support to AL' to load/store

    instructions per c#cle 5 thatBs ho much hardare it has a,aila6le

  • 5/18/2018 Pipeline Hazards

    18/94

    Pipeline HazardsCSCE430/830

    Structural Hazards

    -ealin! ith Structural HazardsStall

    low cost, simple

    Increases CPI

    use for rare case since stalling has performance effect

    Pipeline hardare resource

    useful for multi-cycle resources

    good performance

    sometimes complex e.g., R!

    9eplicate resource good performance

    increases cost "# may$e interconnect delay%

    useful for cheap or di&isi$le resources

  • 5/18/2018 Pipeline Hazards

    19/94

    Pipeline HazardsCSCE430/830

    Structural Hazards

    Structural hazards are reduced ith these rules: Each instruction uses a resource at most once

    Ala#s use the resource in the same pipeline sta!e

    'se the resource for one c#cle onl#

    (an# 91SC 1SAs are desi!ned ith this in mind

    Sometimes ,er# difficult to do this )or eample& memor# of necessit# is used in the 1) and (E(

    sta!es

  • 5/18/2018 Pipeline Hazards

    20/94

    Pipeline HazardsCSCE430/830

    Structural Hazards

    2e ant to compare the performance of to machines 2hich machine is fasterD

    (achine A: -ual ported memor# 5 so there are no memor# stalls (achine : Sin!le ported memor#& 6ut its pipelined implementation has a cloc7

    rate that is >0? times faster

    Assume:

    1deal CP1 > for 6oth

    Loads are 40F of instructions eecuted

  • 5/18/2018 Pipeline Hazards

    21/94

    Pipeline HazardsCSCE430/830

    Speed 'p E=uations for Pipelinin!

    ielined

    d'nieline

    TimeCycleTimeCycle

    C*Ist+ll*ielineC*IIde+ldeth*ielineC*IIde+l,eed'

    ielined

    d'nieline

    TimeCycle

    TimeCycle

    C*Ist+ll*ieline1

    deth*ieline,eed'

    Inst)ercycles,t+llA-er+geC*IIde+lC*I)i)elined

    or simle RI,C ieline/ C*I 0 1

  • 5/18/2018 Pipeline Hazards

    22/94

    Pipeline HazardsCSCE430/830

    Structural Hazards

    2e ant to compare the performance of to machines 2hich machine is fasterD

    (achine A: -ual ported memor# 5 so there are no memor# stalls (achine : Sin!le ported memor#& 6ut its pipelined implementation has a >0?

    times faster cloc7 rate

    Assume:

    1deal CP1 > for 6oth

    Loads are 40F of instructions eecuted

    Speedp!" #pelne %ept&'(1 0) (*lo*+unp$pe'*lo*+p$pe)

    " #pelne %ept&

    Speedp," #pelne %ept&'(1 0-4 1)

    (*lo*+unp$pe'(*lo*+unp$pe ' 1-05)

    " (#pelne %ept&'1-4) 1-05 " 0-75 #pelne %ept&

    Speed'pA/ Speed'p/ Pipeline -epth / G0@? Pipeline -epth >33

    (achine A is >33 times faster

  • 5/18/2018 Pipeline Hazards

    23/94

    Pipeline HazardsCSCE430/830

    Pipelinin! Summar#

    Speed 'p I Pipeline -epthJ if ideal CP1 is >& then:

    Hazards limit performance on computers: Structural: need more H2 resources

    -ata G9A2&2A9&2A2

    Control

    Speedup Pipeline -epth

    > Pipeline stall CP1

    KCloc7 C#cle 'npipelined

    Cloc7 C#cle Pipelined

  • 5/18/2018 Pipeline Hazards

    24/94

    Pipeline HazardsCSCE430/830

    9e,ie

    Speedup Pipeline -epth> Pipeline stall CP1

    K Cloc7 C#cle 'npipelinedCloc7 C#cle Pipelined

    ,eed' of ieline

  • 5/18/2018 Pipeline Hazards

    25/94

    Pipeline HazardsCSCE430/830

    Pipelinin! utline

    1ntroduction -efinin! Pipelinin!

    Pipelinin! 1nstructions

    Hazards

    Structural hazards -ata Hazards

    Control Hazards

    Performance

    Controller implementation

  • 5/18/2018 Pipeline Hazards

    26/94

    Pipeline HazardsCSCE430/830

    Pipeline Hazards

    2here one instruction cannot immediatel#follo another

    #pes of hazards Structural hazards5 attempt to use same resource tice

    Control hazards5 attempt to ma7e decision 6eforecondition is e,aluated

    -ata hazards5 attempt to use data 6efore it is read#

    Can ala#s resol,e hazards 6# aitin!

  • 5/18/2018 Pipeline Hazards

    27/94

    Pipeline HazardsCSCE430/830

    -ata Hazards

    -ata hazards occur hen data is used 6efore

    it is read#

    I! Reg

    I! Reg

    CC ' CC ( CC ) CC * CC + CC

    ime "in cloc cycles%

    su$ /(, /', /)

    Programexecutionorder"in instructions%

    and /'(, /(, /+

    I! Reg 0! Reg

    I! 0! Reg

    I! 0! Reg

    CC 1 CC 2 CC 3

    '4 '4 '4 '4 '45 (4 (4 (4 (4 (4

    or /'), /, /(

    add /'*, /(, /(

    sw /'+, '44"/(%

    6alue ofregister /(7

    0! Reg

    Reg

    Reg

    Reg

    0!

    he use of the result of the S' instruction in the net three instructions causes adata hazard& since the re!ister * is not ritten until after those instructions read it

  • 5/18/2018 Pipeline Hazards

    28/94

    Pipeline HazardsCSCE430/830

    -ata Hazards9ead After 2rite G9A2

    1nstr"tries to read operand 6efore 1nstr1 rites it

    Caused 6# a ;-ependence< Gin compiler nomenclature hishazard results from an actual need for communication

    8xecution 9rder is71nstr1

    1nstr"

    I. add r1,r2,r3

    /. sub r4,r1,r3

  • 5/18/2018 Pipeline Hazards

    29/94

    Pipeline HazardsCSCE430/830

    -ata Hazards2rite After 9ead G2A9

    1nstr"tries to rite operand before1nstr1 reads i Mets ron! operand

    Called an ;anti5dependence< 6# compiler ritershis results from reuse of the name ;r>

  • 5/18/2018 Pipeline Hazards

    30/94

    Pipeline HazardsCSCE430/830

    -ata Hazards2rite After 2rite G2A2

    1nstr"tries to rite operand before1nstr1rites it Lea,es ron! result G 1nstr1not 1nstr"

    Called an ;output dependence< 6# compiler ritershis also results from the reuse of name ;r>

  • 5/18/2018 Pipeline Hazards

    31/94

    Pipeline HazardsCSCE430/830

    -ata Hazard -etection in (1PS G>

    I! Reg

    I! Reg

    CC ' CC ( CC ) CC * CC + CC

    ime "in cloc cycles%

    su$ /(, /', /)

    Programexecutionorder"in instructions%

    and /'(, /(, /+

    I! Reg 0! Reg

    I! 0! Reg

    I! 0! Reg

    CC 1 CC 2 CC 3

    '4 '4 '4 '4 '45 (4 (4 (4 (4 (4

    or /'), /, /(

    add /'*, /(, /(

    sw /'+, '44"/(%

    6alue of

    register /(7

    0! Reg

    Reg

    Reg

    Reg

    0!

    I2ID ID234 342M3M M3M25&

    1+342M3M.RegisterRd0 ID234.RegisterRs

    1342M3M.RegisterRd 0 ID234.RegisterRt

    +M3M25&.RegisterRd 0 ID234.RegisterRsM3M25&.RegisterRd 0 ID234.RegisterRt

    Re+d +fter 5rite

    34 h+6+rd

    M3M h+6+rd

  • 5/18/2018 Pipeline Hazards

    32/94

    Pipeline HazardsCSCE430/830

    -ata Hazards

    Solutions for -ata Hazards Stallin!

    )orardin!:

    connect ne ,alue directl# to net sta!e

    9eorderin!

  • 5/18/2018 Pipeline Hazards

    33/94

    Pipeline HazardsCSCE430/830

    -ata Hazard 5 Stallin!

    0 2 4 6 8 10 12

    IF ID EX MEM

    16

    add$s0,$t0,$t1

    STALL

    18

    sub $t2,$s0,$t3 IF EX MEM

    STALL

    BUBBLE BUBBLE BUBBLE BUBBLE

    BUBBLEBUBBLE BUBBLE BUBBLE BUBBLE

    $s0writtenhere

    Ws0

    WB

    $s0 readhere

    Rs0

    BUBBLE

  • 5/18/2018 Pipeline Hazards

    34/94

    Pipeline HazardsCSCE430/830

    -ata Hazards 5 Stallin!

    Simple Solution to 9A2

    :ardware detects R; and stalls ssumes register written then read each cycle

    # low cost to implement, simple-- reduces IPC

    ry to minimi

  • 5/18/2018 Pipeline Hazards

    35/94

    Pipeline HazardsCSCE430/830

    -ata Hazards 5 )orardin!

    Ne# idea: connect ne ,alue directl# to net sta!e

    Still read s0& 6ut i!nore in fa,or of ne result

    Pro6lem: hat a6out load instructionsD

    ID

    0 2 4 6 8 10 12

    IF ID EX MEM

    16

    add$s0,$t0,$t1

    18

    sub $t2,$s0,$t3 IF EX MEM

    Ws0

    WBRs0

    new valueof s0

  • 5/18/2018 Pipeline Hazards

    36/94

    Pipeline HazardsCSCE430/830

    -ata Hazards 5 )orardin!

    SALL still re=uired for load 5 data a,ail after (E(

    (1PS architecture calls this dela#ed load& initialimplementations re=uired compiler to deal ith this

    ID

    0 2 4 6 8 10 12

    IF ID EX MEM

    16

    lw$s0,20($t1)

    18

    sub $t2,$s0,$t3 IF EX MEM

    Ws0

    WBRs0

    new valueof s0

    STALL

    BUBBLE BUBBLE BUBBLE BUBBLE BUBBLE

  • 5/18/2018 Pipeline Hazards

    37/94

    Pipeline HazardsCSCE430/830

    -ata Hazardshis is anotherrepresentation

    of the stall

    L2 9>& 0G9* 1) 1- EK (E( 2

    S' 94& 9>& 9? 1) 1- EK (E( 2

    AO- 9+& 9>& 9@ 1) 1- EK (E( 2

    9 98& 9>& 9 1) 1- EK (E( 2

    L2 9>& 0G9* 1) 1- EK (E( 2

    S' 94& 9>& 9? 1) 1- stall EK (E( 2

    AO- 9+& 9>& 9@ 1) stall 1- EK (E( 2

    9 98& 9>& 9 stall 1) 1- EK (E( 2

  • 5/18/2018 Pipeline Hazards

    38/94

    Pipeline HazardsCSCE430/830

    )orardin!

    I! Reg

    I! Reg

    CC ' CC ( CC ) CC * CC + CC

    -ime "in cloc. cycles%

    su$ /(, /', /)

    Programexecutionorder"in instructions%

    and /'(, /(, /+

    I! Reg 0! Reg

    I! 0! Reg

    I! 0! Reg

    CC 1 CC 2 CC 3

    '4 '4 '4 '4 '45 (4 (4 (4 (4 (4

    or /'), /, /(

    add /'*, /(, /(

    sw /'+, '44"/(%

    6alue ofregister /(7

    0! Reg

    Reg

    Reg

    Reg

    0!

    I2ID ID234 342M3M M3M25&

    7o8 8o'ld yo' design the for8+rding9

    :ey ide+ connect d+t+ intern+lly efore it;s stored

  • 5/18/2018 Pipeline Hazards

    39/94

    Pipeline HazardsCSCE430/830

    Oo )orardin!

  • 5/18/2018 Pipeline Hazards

    40/94

    Pipeline HazardsCSCE430/830

    -ata Hazard Solution: )orardin!

    Ne# idea: connect data internall# 6efore itQs

    stored

    I! Reg

    I! Reg

    CC ' CC ( CC ) CC * CC + CC

    ime "in cloc cycles%

    su$ /(, /', /)

    Programexecution order"in instructions%

    and /'(, /(, /+

    I! Reg 0! Reg

    I! 0! Reg

    I! 0! Reg

    CC 1 CC 2 CC 3

    '4 '4 '4 '4 '45 (4 (4 (4 (4 (4

    or /'), /, /(

    add /'*, /(, /(

    sw /'+, '44"/(%

    6alue of register /( 7

    0! Reg

    Reg

    Reg

    Reg

    A A A (4 A A A A A6alue of 8A5!8! 7

    A A A A (4 A A A A6alue of !8!5;= 7

    0!

    Assumption:he re!ister file forards ,alues that are read

    and ritten durin! the same c#cle

  • 5/18/2018 Pipeline Hazards

    41/94

    Pipeline HazardsCSCE430/830

    -ata Hazard Summar#

    hree t#pes of data hazards 9A2 G(1PS 2A2 Gnot in (1PS

    2A9 Gnot in (1PS

    Solution to 9A2 in (1PS Stall

    )orardin!

    -etection Control EK hazard

    (E( hazard

    A stall is needed if read a re!ister after a loadinstruction that rites the same re!ister

    9eorderin!

  • 5/18/2018 Pipeline Hazards

    42/94

    Pipeline HazardsCSCE430/830

    9e,ie

    Speedup Pipeline -epth> Pipeline stall CP1

    K Cloc7 C#cle 'npipelinedCloc7 C#cle Pipelined

    ,eed' of ieline

  • 5/18/2018 Pipeline Hazards

    43/94

    Pipeline HazardsCSCE430/830

    Pipelinin! utline

    1ntroduction -efinin! Pipelinin!

    Pipelinin! 1nstructions

    Hazards

    Structural hazards -ata Hazards

    Control Hazards

    Performance

    Controller implementation

  • 5/18/2018 Pipeline Hazards

    44/94

    Pipeline HazardsCSCE430/830

    -ata Hazard 9e,ie

    hree t#pes of data hazards 9A2 Gin (1PS and all others 2A2 Gnot in (1PS 6ut man# others

    2A9 Gnot in (1PS 6ut man# others

    )orardin!

    9 i - t H d ) di

  • 5/18/2018 Pipeline Hazards

    45/94

    Pipeline HazardsCSCE430/830

    9e,ie: -ata Hazards )orardin!

    S' s0& t0& t> Js0 t0 5 t>

    A-- t*& s0& t3 Jt* s0 t3

    S'

    A--

    1) 1- EK (E( 2

    1) 1- EK (E( 2

    EK Hazard: S' result not ritten until its 2& read# at end

    of its EK& needed at start of A--Bs EK

    EK/(E( )orardin!: forard s0 from EK/(E( to AL' inputin A-- EK sta!e GCC4

    Note: can occur in sequentialinstructions

    1 ! " % #

    9 i - t H d ) di

  • 5/18/2018 Pipeline Hazards

    46/94

    Pipeline HazardsCSCE430/830

    9e,ie: -ata Hazards )orardin!

    S' s0& t0& t> Js0 t0 5 t>

    A-- t*& s0& t3 Jt* s0 t3

    S'

    A--

    1) 1- EK (E( 2

    1) 1- EK (E( 2

    EK Hazard -etection 5 EK/(E( )orardin! Conditions:

    If ((EX/MEM.RegWrite = 1) & (EX/MEM.RegRD= ID/EX.RegRS))

    If ((EX/MEM.RegWrite = 1) & (EX/MEM.RegRD = ID/EX.RegRT))

    Then forward EX/MEM resut to EX stage

    Note: In PH3, also check that EX/MEM.RegRD 0

    1 ! " % #

    9 i - t H d ) di

  • 5/18/2018 Pipeline Hazards

    47/94

    Pipeline HazardsCSCE430/830

    9e,ie: -ata Hazards )orardin!

    S' s0& t4& s3 Js0 t4 s3

    A-- t*& s>& t> Jt* s0 t>9 s*& t3& s0 Js* t3 9 s0

    S'

    A--

    9

    1) 1- EK (E( 2

    1) 1- EK (E( 2

    1) 1- EK (E( 2

    (E( Hazard: S' result not ritten until its 2& stored in

    (E(/2& needed at start of 9Bs EK

    (E(/2 )orardin!: forard s0 from (E(/2 to AL'input in 9 EK sta!e GCC?

    Note: can occur in instructions In& In+2

    1 ! " % #

    9 i - t H d ) di

  • 5/18/2018 Pipeline Hazards

    48/94

    Pipeline HazardsCSCE430/830

    9e,ie: -ata Hazards )orardin!

    S' s0& t4& s3 Js0 t4 s3

    A-- t*& s>& t> Jt* s0 t>9 s*& t3& s0 Js* t3 9 s0

    S'

    A--

    9

    1) 1- EK (E( 2

    1) 1- EK (E( 2

    1) 1- EK (E( 2

    (E( Hazard -etection 5 (E(/2 )orardin! Conditions:If ((MEM/W!.RegWrite = 1) & (MEM/W!.RegRD = ID/EX.RegRS))

    If ((EX/MEM.RegWrite = 1) & (EX/MEM.RegRD = ID/EX.RegRT))

    Then forward MEM/W! resut to EX stage

    Note: In PH3, also check that MEM/WB.RegRD 0

    1 ! " % #

  • 5/18/2018 Pipeline Hazards

    49/94

    Pipeline HazardsCSCE430/830

    -ata Hazard -etection in (1PS

    I! Reg

    I! Reg

    CC ' CC ( CC ) CC * CC + CC

    ime "in cloc cycles%

    su$ /(, /', /)

    Programexecution

    order"in instructions%

    and /'(, /(, /+

    I! Reg 0! Reg

    I! 0! Reg

    I! 0! Reg

    CC 1 CC 2 CC 3

    '4 '4 '4 '4 '45 (4 (4 (4 (4 (4

    or /'), /, /(

    add /'*, /(, /(

    sw /'+, '44"/(%

    6alue ofregister /(7

    0! Reg

    Reg

    Reg

    Reg

    0!

    I2ID ID234 342M3M M3M25&

    1+342M3M.RegisterRd 0 ID234.RegisterRs

    1342M3M.RegisterRd 0 ID234.RegisterRt+M3M25&.RegisterRd 0 ID234.RegisterRsM3M25&.RegisterRd 0 ID234.RegisterRt

    *rolem9

    342M3M.Reg5rite m'st e +sserted00Gt0 Js0 memor# ,alue

    A-- t*& s0& t3 Jt* s0 t3

    L2

    A--

    1) 1- EK (E( 2

    1) 1- EK (E( 2

    L2 doesnBt rite s0 to 9e! )ileuntil the end of CC?& 6utA-- reads s0 from 9e! )ile in CC3

    1 ! " % #

    -ata Hazards Stalls

  • 5/18/2018 Pipeline Hazards

    60/94

    Pipeline HazardsCSCE430/830

    -ata Hazards Stalls

    L2 s0& >00Gt0 Js0 memor# ,alueA-- t*& s0& t3 Jt* s0 t3

    L2

    A--

    1) 1- EK (E( 2

    1) 1- EK (E( 2

    EK/(E( forardin! onBt or7& 6ecause the data isnBtloaded from memor# until CC4 Gso itBs not in EK/(E(re!ister

    1 ! " % #

    -ata Hazards Stalls

  • 5/18/2018 Pipeline Hazards

    61/94

    Pipeline HazardsCSCE430/830

    -ata Hazards Stalls

    L2 s0& >00Gt0 Js0 memor# ,alueA-- t*& s0& t3 Jt* s0 t3

    L2

    A--

    1) 1- EK (E( 2

    1) 1- EK (E( 2

    (E(/2 forardin! onBt or7 either& 6ecause A--eecutes in CC4

    1 ! " % #

    -ata Hazards Stalls: implementation

  • 5/18/2018 Pipeline Hazards

    62/94

    Pipeline HazardsCSCE430/830

    -ata Hazards Stalls: implementation

    L2 s0& >00Gt0 Js0 memor# ,alueA-- t*& s0& t3 Jt* s0 t3

    L2

    A--

    1) 1- EK (E( 2

    1) 1- 1- EK (E( 2

    2e must handle this hazard 6# ;stallin!< the pipelinefor > Cloc7 C#cle G6u66le

    "u""

    e

    1 ! " % #

    -ata Hazards Stalls: implementation

  • 5/18/2018 Pipeline Hazards

    63/94

    Pipeline HazardsCSCE430/830

    -ata Hazards Stalls: implementation

    L2 s0& >00Gt0 Js0 memor# ,alueA-- t*& s0& t3 Jt* s0 t3

    L2

    A--

    1) 1- EK (E( 2

    1) 1- 1- EK (E( 2

    2e can then use (E(/2 forardin!& 6ut of course thereis still a performance loss

    "u""

    e

    1 ! " % #

    -ata Hazards Stalls: implementation

  • 5/18/2018 Pipeline Hazards

    64/94

    Pipeline HazardsCSCE430/830

    -ata Hazards Stalls: implementation

    Stall 1mplementation T>: Compiler detects hazard and

    inserts a OP Gno re! chan!es GSLL 0& 0& 0

    L2 s0& >00Gt0 Js0 memor# ,alue

    OP Jdumm# instruction

    A-- t*& s0& t3 Jt* s0 t3

    L2

    OP

    A--

    1) 1- EK (E( 2

    1) 1- EK (E( 2

    1) 1- EK (E( 2

    "u""e

    "u""e

    "u""e

    "u""e

    "u""e

    Pro6lem: e ha,e to rel# on the compiler

    1 ! " % #

    -ata Hazards Stalls: implementation

  • 5/18/2018 Pipeline Hazards

    65/94

    Pipeline HazardsCSCE430/830

    -ata Hazards Stalls: implementation

    Stall 1mplementation T*: Add a ;hazard detection unit< tostall current instructionfor > CC if:

    1-5Sta!e Hazard -etection and Stall Condition:

    If ((ID/EX.Me#Read = 1) & $on% a W reads #e#

    ((ID/EX.RegRT = I'/ID.RegRS) $RS wi read oad dest (RT)(ID/EX.RegRT = I'/ID.RegRT))) $RT wi read oad dest

    L2 s0& >00Gt0 Js0 memor# ,alue

    A-- t*& s0& t3 Jt* s0 t3

    L2

    A--

    1) 1- EK (E( 2

    1) 1- EK (E( 2

    -ata Hazards Stalls: implementation

  • 5/18/2018 Pipeline Hazards

    66/94

    Pipeline HazardsCSCE430/830

    -ata Hazards Stalls: implementation

    he effect of this stall ill 6e to repeat the 1- Sta!e of thecurrent instruction hen e do the (E(/2 forardin! onthe net Cloc7 C#cle

    L2

    A--

    1) 1- EK (E( 2

    1) 1- 1- EK (E( 2

    2e do this 6# preser,in! the current ,alues in 1)/1- for useon the net Cloc7 C#cle

    -ata Hazards: A Classic Eample

  • 5/18/2018 Pipeline Hazards

    67/94

    Pipeline HazardsCSCE430/830

    -ata Hazards: A Classic Eample

    1dentif# the data dependencies in thefolloin! code 2hich of them can 6eresol,ed throu!h forardin!D

    S' *& >& 39 >*& *& ?

    S2 >3& >00G*

    A-- >4& *& *

    L2 >?& >00G*

    A-- 4& @& >?

    -ata Hazards 5 9eorderin!

  • 5/18/2018 Pipeline Hazards

    68/94

    Pipeline HazardsCSCE430/830

    !1nstructions

    Assumin! e ha,e data forardin!& hat arethe hazards in this codeD

    lw $t0, 0($t1)lw $t2, 4($t1)sw $t2, 0($t1)sw $t0, 4($t1)

    9eorder instructions to remo,e hazard:lw $t0, 0($t1)lw $t2, 4($t1)sw $t0, 4($t1)sw $t2, 0($t1)

    -ata Hazard Summar#

  • 5/18/2018 Pipeline Hazards

    69/94

    Pipeline HazardsCSCE430/830

    -ata Hazard Summar#

    hree t#pes of data hazards 9A2 G(1PS

    2A2 Gnot in (1PS

    2A9 Gnot in (1PS

    Solution to 9A2 in (1PS

    Stall )orardin!

    -etection Control EK hazard

    (E( hazard

    A stall is needed if read a re!ister after a loadinstruction that rites the same re!ister

    9eorderin!

    Pipelinin! utline

  • 5/18/2018 Pipeline Hazards

    70/94

    Pipeline HazardsCSCE430/830

    p !Oet class

    1ntroduction -efinin! Pipelinin!

    Pipelinin! 1nstructions

    Hazards

    Structural hazards -ata Hazards

    Control Hazards

    Performance

    Controller implementation

    Pipeline Hazards

  • 5/18/2018 Pipeline Hazards

    71/94

    Pipeline HazardsCSCE430/830

    Pipeline Hazards

    2here one instruction cannot immediatel#follo another

    #pes of hazards Structural hazards5 attempt to use same resource tice

    Control hazards5 attempt to ma7e decision 6eforecondition is e,aluated

    -ata hazards5 attempt to use data 6efore it is read#

    Can ala#s resol,e hazards 6# aitin!

    Control Hazards

  • 5/18/2018 Pipeline Hazards

    72/94

    Pipeline HazardsCSCE430/830

    Control Hazards

    A control hazard is hen e need to find the

    destination of a 6ranch& and canBt fetch an# neinstructions until e 7no that destination

    A 6ranch is either

    a7en: PC I PC 4 1mmediate Oot a7en: PC I PC 4

    Control Hazard on ranchesControl Hazards

  • 5/18/2018 Pipeline Hazards

    73/94

    Pipeline HazardsCSCE430/830

    Control Hazard on rancheshree Sta!e Stall

    Control Hazards

    >0: 6e=r>&r3&3+

    >4: and r*&r3&r?

    >8: or r+&r>&r@

    **: add r8&r>&r

    3+: or r>0&r>&r>>

    9e!

    AL' -(em1fetch 9e!

    9e!

    AL' -(em1fetch 9e!

    9e!

    AL' -(em1fetch 9e!

    9e!

    AL' -(em1fetch 9e!

    9e!

    AL' -(em1fetch 9e!

    The en+lty 8hen r+nch t+ke is ! cycles