5.2.5 · D4Processor Datapath & Pipelining

Exercises — Structural hazards

3,797 words17 min readBack to topic

This page is a self-test ladder. Each rung is harder than the last: you start by just recognising a structural hazard, and finish by designing around one. Every problem has a fully worked solution hidden inside a collapsible box — try first, then reveal.

Prerequisites, if any line feels shaky: the parent note, Pipelining Fundamentals, Pipeline Stalls. Contrast partners: Data Hazards, Control Hazards.


The two formulas this whole page runs on

Before any exercise, let's build (from scratch) the only two facts every problem below reuses. Nothing here is assumed from another note.

Recall Edge cases of the model

(read before Level 5)

  • or : no real hazard, ideal pipeline.
  • : every instruction stalls once, throughput halved.
  • What if ? Totally allowed — it just means the average instruction eats more than one bubble (e.g. gives ). CPI can grow up to the pipeline depth (full serialisation); it never goes below because .
  • Negative stalls are impossible: and by definition (you cannot un-stall), so always. If a formula ever hands you you made a sign error.

Level 1 — Recognition

Can you tell a structural hazard apart from the others, and spot the resource conflict?

Exercise 1.1

Four instructions (I1–I4) run in a 5-stage pipeline (IF, ID, EX, MEM, WB) on a machine with one unified memory (a single port shared by IF and MEM). Look at the timing chart below.

Figure — Structural hazards
Figure 1 — Rows are instructions I1–I4; columns are clock cycles 1–8; each labelled box is the stage that instruction occupies that cycle. The two red boxes sit in the same column (cycle 4): I1's MEM and I4's IF. A red arrow marks them as the pair fighting over the single memory port.

Question: In which cycle does a structural hazard occur, and which two stages fight over what resource?

Recall Solution

Read the chart column by column (each column is one clock cycle). A structural hazard is two stages needing the same physical unit in the same column — that is exactly the pair of red boxes in Figure 1.

  • Cycle 4: I1 is in MEM (touching memory for data) and I4 is in IF (touching memory to fetch its instruction).
  • The single memory port can serve only one of them.

Answer: The hazard is in cycle 4; the conflict is between I1's MEM stage and I4's IF stage, both demanding the single memory port.

Exercise 1.2

Classify each situation as Structural, Data, or Control hazard:

  • (a) ADD R1,R2,R3 followed by SUB R4,R1,R5 — the SUB needs R1 before ADD has written it.
  • (b) A single ALU is needed by an EX-stage add and by IF-stage PC+4 in the same cycle.
  • (c) A BEQ (branch) is in EX and the processor does not yet know whether to fetch the next instruction or the branch target.
Recall Solution
  • (a) Data hazard — it is about information/order (a value produced then consumed), not a shared box of hardware.
  • (b) Structural hazard — two stages want the same physical adder in one cycle.
  • (c) Control hazard — the decision of what to fetch next is not resolved.

Rule of thumb: if you can fix it by buying another copy of a hardware unit, it was structural.


Level 2 — Application

Plug numbers into the CPI formula and predict performance.

Exercise 2.1

A processor has . A memory structural hazard stalls the pipeline 1 cycle for every load/store. Loads/stores are of all instructions. Find and the fraction of performance lost versus the ideal pipeline.

Recall Solution

Use the boxed model from the top of this page, . Every symbol first:

  • is the fraction of instructions that trigger the hazard.
  • cycle is the stall penalty per hazard.

Now use the " at equal clock" result derived at the top. Same machine, same clock, so the speed ratio of the hazarded pipeline to the ideal one is:

Answer: ; performance lost slower.

Exercise 2.2

Same machine, but the designer adds a separate data cache, so IF and MEM never collide — the memory hazard is eliminated. But a new hazard appears: a single ALU shared by EX and PC-increment stalls 1 cycle on of instructions. What is the new , and did the redesign help?

Recall Solution

In the boxed model, eliminating the memory hazard sets its to , so that term vanishes. Only the ALU term remains:

Compare with the old . Again same clock, so we use ; the speedup of the new design over the old is the ratio of their CPIs:

Answer: ; the redesign makes the machine about 24% faster. Yes, it helped.


Level 3 — Analysis

Read a pipeline diagram, insert stalls yourself, and count cycles.

Exercise 3.1

On a single-memory-port machine, a load instruction's MEM stage collides with the IF of whichever instruction is 3 slots behind it (because MEM is stage 4 and IF is stage 1: they line up when instructions are 3 apart). We run 8 back-to-back loads.

Assume: each MEM–IF collision forces the younger instruction (the one in IF) to stall exactly 1 cycle. Ignore start-up fill for the moment; count only the extra stall cycles caused by the hazard across the 8 loads.

Figure — Structural hazards
Figure 2 — First five of the eight loads (L1–L5), rows = instructions, columns = clock cycles. Each row's MEM box is drawn in red because that is the memory access that will block a later instruction's IF. The caption line inside the figure states the rule: every red MEM stage stalls its follower's IF by +1 cycle.

Question: How many total stall cycles are inserted, and what is the resulting over these 8 instructions?

Recall Solution

Why the "8" of ideal issue. In a pipeline, a new instruction enters IF every clock and, with no hazards, exactly one instruction finishes every clock. So issuing 8 instructions ideally takes 8 issue slots — one cycle each. That is where the bare comes from (it is the ideal-issue count, before any bubbles; start-up fill is excluded as the problem states).

What causes each stall: when a load reaches MEM (a red box in Figure 2), the instruction entering IF that same cycle must wait. The first load (L1) reaches MEM in cycle 4 while L4 wants IF — so L4 stalls. Every subsequent load likewise pushes one stall onto the instruction behind it.

Counting inside the window: Loads L1…L8 each want to generate one MEM/IF collision with a follower. The last load L8 has no follower inside these 8 — its induced stall would land on an instruction outside the window — so it contributes no stall here. That leaves collisions from L1…L7 → 7 stall cycles.

Answer: 7 stall cycles inside the window; .

Sanity check with the model: in the steady state (infinite stream) every load stalls, so gives . Our windowed is just below because L8's stall spilled out of the window — exactly what we expect. ✓

Exercise 3.2

Now suppose only half of the 8 instructions are loads (the other 4 are register-only ADDs that never touch memory in MEM). Loads sit at positions I1, I3, I5, I7. Each load still forces its follower to stall 1 cycle (and each load's follower is inside the window this time). Compute the CPI over the 8 instructions.

Recall Solution

Loads at I1, I3, I5, I7 → 4 loads, each induces 1 stall on the instruction behind it (I2, I4, I6, I8 respectively — all inside the window). So 4 stall cycles.

Cross-check with the boxed model, , :

Answer: 4 stalls; .


Level 4 — Synthesis

Combine multiple hazard sources and reason about design trade-offs.

Exercise 4.1

A design team measures three independent structural hazards on the same pipeline (they don't overlap in the same instruction, so their stall cycles simply add). In the table, is the frequency of that hazard (fraction of instructions that trigger it) and is its stall penalty in cycles:

Source Frequency Penalty
Unified memory (IF vs MEM) 0.30 1
Shared ALU (EX vs PC+4) 0.05 1
Single register write port 0.02 1

Compute . Then: fixing the memory hazard (split I/D caches) costs 10 units of area; fixing the ALU hazard (add a PC adder) costs 1 unit; fixing the write port costs 4 units. You have a budget of 5 area units. Which fixes maximise performance gain per your budget?

Recall Solution

Baseline CPI — because the three hazards never hit the same instruction, their per-instruction stall contributions just add on top of the ideal (this is the boxed model with several terms):

Why "benefit = ". Removing a hazard deletes exactly its term from the CPI sum — so the CPI you save by fixing hazard is precisely . That is the benefit of the fix. Here every , so benefit .

Why "benefit per area" is the right metric. With a hard area budget you want the most CPI removed per area unit spent — the classic bang-for-buck ratio . Rank the fixes by it:

Fix Area CPI removed () Benefit per area
Memory 10 0.30 0.030
ALU 1 0.05 0.050
Write port 4 0.02 0.005

Budget = 5. The memory fix (area 10) is unaffordable outright. Within 5 units you can take ALU (1) + write port (4) = 5 units exactly:

  • CPI removed → new .

Alternative: ALU alone (1 unit) removes 0.05 → , leaving 4 units unused. Taking both ALU + write port spends the whole budget and reaches , the best achievable within 5 units.

Answer: Buy the ALU adder + register write port (total area 5), reaching . The memory fix, though it removes the biggest single stall, doesn't fit the budget.

Exercise 4.2

The team is deciding between two whole architectures:

  • A (Harvard): separate instruction and data memories — zero memory structural hazards, but 12 area units and higher static power.
  • B (Von Neumann + interlock): one memory + stall logic — memory hazard , , only 2 area units.

If both run at the same clock and , at what memory-hazard frequency would architecture B's performance equal A's? (Assume A has .)

Recall Solution

A: . B: . Same clock, so (from the top-of-page result) equal performance means equal CPI:

Interpretation: with a nonzero hazard frequency, B is always slower than the hazard-free Harvard machine. B only ties A when — i.e. never in practice. So the choice is purely performance (A) vs area/power (B): B is justified only when the 10 extra area units and power matter more than the guaranteed slowdown (at ).

Answer: ; B can never match A on speed, so pick B strictly for area/power reasons.


Level 5 — Mastery

Design and defend; reason about limits and corner cases.

Exercise 5.1

You are handed a superscalar core that issues 2 instructions per cycle. Its register file has 3 read ports and 1 write port. In one cycle the two co-issued instructions are both 2-source, 1-destination ALU ops. (a) Is there a read-port structural hazard? (b) A write-port hazard? (c) State the minimum port counts to never stall on ports for dual 2-source/1-dest issue.

Recall Solution

Demand per cycle for two 2-source, 1-destination instructions issued together:

  • Reads needed: each op reads 2 source registers, two ops → register reads in one cycle.
  • Writes needed: each op writes 1 destination, two ops → register writes when both reach WB together.

(a) Read-port hazard? The file has 3 read ports but the cycle demands 4. , so yes, there is a read-port structural hazard — the file physically cannot deliver 4 source operands at once, so one instruction must stall (or issue must be narrowed to one op that cycle).

(b) Write-port hazard? The file has 1 write port but two destinations want to commit in the same WB cycle. , so yes, there is a write-port structural hazard — the two writes must be serialised, stalling one.

(c) Minimum ports to never stall for dual 2-source/1-dest issue: enough read ports for all sources () and enough write ports for all destinations (). So the register file needs 4 read ports and 2 write ports (in general, read and write ports for -wide issue of 2-source/1-dest ops).

Answer: (a) yes — need 4 reads, have 3; (b) yes — need 2 writes, have 1; (c) 4 read ports, 2 write ports. This is exactly why wider Superscalar Processors pay steeply — port count scales with issue width, and each extra port adds area and power.

Exercise 5.2 (Corner cases — cover every sign)

For the model (with = hazard frequency, = stall penalty), reason about the degenerate/limiting inputs:

  • (a) : what does the machine look like?
  • (b) : what CPI, and what does the pipeline diagram look like?
  • (c) : is this a hazard at all?
  • (d) on a 5-stage pipe: interpret — what has effectively happened to pipelining?
Recall Solution
  • (a) : no hazards ever, ideal pipeline, one instruction finishes per cycle. This is the Harvard/split-cache dream.
  • (b) : every instruction stalls exactly one cycle. The diagram is a bubble after every instruction — throughput halved (matches Ex 3.1's steady-state limit of 2.0 ✓).
  • (c) : a "hazard" that costs zero cycles is no hazard at all — the resource conflict was resolved for free (e.g. a cheap duplicated unit). Frequency becomes irrelevant.
  • (d) on a 5-stage pipe : every instruction waits 4 cycles, so instruction only starts after instruction has fully drained. That is serial execution — pipelining's overlap has been completely destroyed. CPI equals the pipeline depth, the worst case. Note here, which is perfectly valid: the average instruction eats more than one bubble.

Answer: (a) ideal, CPI 1; (b) CPI 2, one bubble per instruction; (c) not a real hazard, CPI 1; (d) CPI 5 = fully serialised, no pipeline benefit remains.


Recall Rapid self-check (cover the right side)

Structural hazard is defined by sharing a … ::: physical hardware resource in the same cycle. Formula for CPI with hazards ::: . Why is performance ::: time per instruction cycle time; performance is its reciprocal, so at equal clock bigger CPI = slower. Ex 2.1 answer ::: CPI = 1.30, about 23% slower. Ex 3.2 answer ::: CPI = 1.5. Ex 4.1 best-within-budget CPI ::: 1.30 (ALU + write-port fixes). Dual 2-source/1-dest issue needs how many ports ::: 4 read, 2 write. Limiting CPI when on 5 stages ::: 5 (fully serial).

Related: Parent: Structural hazards · Pipeline Stalls · Superscalar Processors · Memory Hierarchy · 5.2.05 Structural hazards (Hinglish)