edited by
777 views
13 votes
13 votes

Consider a $5$-stage pipeline - IF (Instruction Fetch), ID (Instruction Decode and register read), EX (Execute), MEM (Memory) and WB (Write Back). All register reads take place in the second phase of a clock cycle and all register writes occur in the first phase. Consider the execution of the following instruction sequence:

  • $I_1: R_1 \leftarrow R_2 + R_3$
  • $I_2: R_3 \leftarrow R_1 - R_2$
  • $I_3: M[R_1+1000] \leftarrow R_1$
  • $I_4: R_2 \leftarrow R_3 * R_1$

If the number of RAW (Read after write) hazards is denoted by $A,$ WAR (Write after read) hazards by $B$ and WAW (Write after write) hazards by $C,$ then $A+B+C = $_____

edited by

1 Answer

Best answer
16 votes
16 votes
$$\begin{array}{|c|c c c c c c c c|}
        \hline
        &1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 \\
        \hline
        I_1&\text{F}&\text{D}  &\text{E} &\text{M} &\textbf{W} & & &   \\
        I_2&&\text{F}  &\boxed{\text{D}} &\text{E} &\text{M} &\textbf{W} & &    \\
        I_3&& &\text{F} &\boxed{\text{D}} &\text{E}  &\text{M}  &\text{W} & \\
        I_4&& & & \text{F}   &\boxed{\text{D}} &\text{E}  &\text{M} &\text{W} \\
        \hline
    \end{array}$$
RAW (Read After Write) Dependencies are: $I_1\rightarrow I_2,I_1\rightarrow I_3,I_1 \rightarrow I_4,I_2 \rightarrow I_4.$

Of these $4$ dependencies, only $I_1 \rightarrow I_4$ is not a hazard, as the order of R/W here follows the same as in a non-pipelined sequential execution. ($W$ of $I_1$ is happening in first phase of the cycle and $R$ of $I_4$ is happening in the second phase). Thus, we have $3$ RAW hazards here. So, $A = 3.$
    
The given pipeline is the simplistic one -- where each stage is used only for one cycle -- and in such a pipeline with inorder execution we can never have WAR or WAW hazards. So, $B = C = 0.$
    
Thus, $A +B+C = 3.$
selected by
Answer:

Related questions