Sub topic 7
Structural hazards: resource conflicts
This chapter assumes you know the five stages (IF, ID, EX, MEM, WB), how a pipeline timing diagram is drawn, and that an ideal pipeline finishes n instructions in k + n − 1 cycles. It covers the first of the three hazard classes.
What a structural hazard is
A structural hazard occurs when two instructions need the same piece of hardware in the same clock cycle, and the hardware cannot serve both. The cause is the hardware design, not the program. Data hazards depend on the values instructions pass to each other. Control hazards depend on branches. A structural hazard depends only on what the processor has too few of.
The pipeline cannot do both things at once, so it lets the older instruction proceed and holds the younger one back. The younger instruction waits one or more cycles, and the empty slot that travels down the pipeline is a bubble.
The classic case: one memory for code and data
Suppose instructions and data share one memory with a single access port. Two stages touch memory:
- IF reads the next instruction.
- MEM reads or writes data for a load or store.
A load in MEM and a fetch in IF can land in the same cycle. The load is in MEM in cycle 4, and the instruction three places behind it is ready to fetch in cycle 4.
The table below shows a load followed by three instructions. The three that follow are two adds and a fourth instruction i4. Adds pass through MEM without using memory, so only the load conflicts.
| Instruction | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
|---|---|---|---|---|---|---|---|---|---|
| lw | IF | ID | EX | MEM | WB | ||||
| add (i2) | IF | ID | EX | MEM | WB | ||||
| add (i3) | IF | ID | EX | MEM | WB | ||||
| i4 | stall | IF | ID | EX | MEM | WB |
In cycle 4 the load uses the memory, so i4 cannot fetch. It fetches in cycle 5. Everything behind i4 is delayed by one cycle too, because the pipeline is in order. The program takes 9 cycles instead of the ideal 4 + 4 = 8.
Why the older instruction wins
Giving priority to the older instruction is the standard policy. If the younger instruction took the resource, the older one would have to stall, and everything behind the older one would stall with it. Stalling the younger one holds back only instructions that were already behind it.
Other places structural hazards appear
- Register file ports. WB writes a register while ID reads two registers in the same cycle. The usual fix is to write in the first half of the clock cycle and read in the second half, so the two accesses do not overlap. The same trick appears in the data-hazard chapter.
- A single ALU. If one ALU had to compute both branch targets or addresses and arithmetic results, the two would collide. Classic designs add a separate adder for the program counter.
- Multi-cycle functional units. A floating-point divider that takes 4 cycles and is not pipelined can accept a new operation only every 4 cycles. Two divides in a row conflict on the divider.
- Non-pipelined units in general. A unit is a hazard source if it is slower than one cycle and cannot start a new operation each cycle.
Remedies
| Remedy | How it works | Cost |
|---|---|---|
| Duplicate the resource | Separate instruction and data memories (a Harvard design), or split L1 instruction and data caches | More hardware and area |
| Pipeline the unit | A pipelined divider accepts a new divide each cycle | More latches and design effort |
| Stall the younger instruction | Insert bubbles until the resource is free | Lower throughput |
| Add ports | Multi-ported register file or memory | Area and power grow with the number of ports |
Duplication is the common answer. Modern processors have separate L1 instruction and data caches, so IF and MEM do not compete. Stalling is the cheapest in hardware, which is why GATE questions use it as the baseline.
Measuring the cost
A structural hazard adds stall cycles per instruction, and the effective CPI is the ideal CPI plus the average stall cycles per instruction. With a single memory port, each load or store makes one fetch wait one cycle. If 30% of instructions access data memory:
CPI = 1 + 0.30 × 1 = 1.30The program below simulates this. It uses the model above: fetches are in order, and a fetch cannot use a cycle in which an earlier instruction is in MEM using the memory.
def total_cycles(program, unified=True):
# Cycle numbering starts at 1. An instruction fetched in cycle f
# finishes WB in cycle f + 4 if it never stalls after fetch.
busy_memory = set() # cycles in which a load/store uses the memory in MEM
last_fetch = 0
for op in program:
c = last_fetch + 1
while unified and c in busy_memory:
c += 1 # the fetch waits: older load/store has the port
last_fetch = c
if op in ("lw", "sw"):
busy_memory.add(c + 3) # MEM happens 3 cycles after IF
return last_fetch + 4
program = ["lw", "add", "sub"] * 33 + ["add"] # 100 instructions, 33 loads
print(total_cycles(program), total_cycles(program, unified=False)) # 137 104With split memories the run takes 104 cycles (5 + 100 − 1). With one memory it takes 137: 33 extra cycles, one per load. The measured CPI is 1.37. That is the formula's 1.33 plus the 0.04 contributed by filling the pipeline, which is why GATE questions usually ask for the steady-state CPI.
A trade-off question
Suppose splitting the memory removes all stalls but slows the clock because of the extra hardware. At equal clock periods, the split design is faster by CPIunified / CPIsplit = 1.30. The split design still wins as long as its clock period is less than 1.30 times the unified period. With a 10% slower clock, the speedup is 1.30 / 1.10 ≈ 1.18.
Multi-cycle units
If a divider needs 4 cycles per operation and is not pipelined, the second of two back-to-back independent divides cannot start EX until the first has finished its fourth EX cycle. It waits 3 cycles. If the divider were fully pipelined, it would wait none. The general rule is:
stall between two operations = (initiation interval) − (cycles between their EX starts)The initiation interval is how many cycles a unit needs before it can accept a new operation. It is 1 for a fully pipelined unit and equals the latency for a non-pipelined one.
Common mistakes
- Counting a stall for every instruction. Only instructions that really use the shared resource conflict. Adds go through MEM without touching memory.
- Stalling the load. The younger fetch waits, not the load.
- Forgetting the end of the program. A load near the end may have no later fetch to collide with, so the stall count can be lower than the number of loads.
- Treating structural hazards as software-caused. They are fixed by hardware duplication or by pipelining a unit, not by reordering code, although a scheduler may reduce their effect.
- Mixing up hazards that look alike. If the problem says the register file is written in the first half and read in the second half, there is no structural hazard on it and no stall.
Summary of the method
- List the shared resources and the stages that use each.
- Mark which instructions actually use them.
- Find the cycles in which two users coincide.
- Delay the younger instruction and shift everything behind it.
- Convert to CPI: ideal CPI + (fraction conflicting × stall cycles per conflict).
Exercise
A five-stage pipeline has one memory for both instructions and data. 25% of instructions are loads or stores, and each causes a one-cycle stall of the fetch behind it. A redesign with separate instruction and data memories removes all stalls but lengthens the clock period by 10%. Which design is faster per instruction, and by what factor?
Show answer
The unified design has CPI = 1 + 0.25 × 1 = 1.25 at period T, so 1.25 T per instruction. The split design has CPI = 1 at period 1.1 T, so 1.1 T per instruction. The split design is faster by 1.25 / 1.1 ≈ 1.14.
Exercise
A non-pipelined floating-point divider needs 4 cycles in EX. Three independent divide instructions are issued back to back, with no other hazards. How many stall cycles in total does the third divide see relative to the ideal pipeline, and why?
Show answer
The second divide cannot enter EX until the first finishes its fourth EX cycle, so it waits 3 cycles. The third divide must wait for the second to finish its four cycles, which adds another 3 cycles on top of the delay it already inherited. The third divide is delayed by 6 cycles in total.