Sub topic 7
Structural Hazards: Resource Conflicts
This chapter assumes you know the CPU clock, registers, memory, and the five pipeline stages: IF, ID, EX, MEM, and WB. You should also understand that pipeline stages from different instructions can run during the same clock cycle.
What Is a Structural Hazard?
A structural hazard occurs when two instructions need the same hardware resource during the same clock cycle, but the processor has only one copy of that resource.
For example, suppose instruction fetch and data memory access both use one shared memory. During the same cycle:
- One instruction is in the IF stage and needs memory to fetch its instruction.
- Another instruction is in the MEM stage and needs memory to load or store data.
Both operations request the same memory. The processor cannot perform both operations at once. One instruction must wait, creating a pipeline stall.
Structural hazards depend on the processor design. The same instruction sequence may have a structural hazard in one processor and no hazard in another.
A Five-Stage Example
Consider this sequence in a five-stage pipeline:
lw r1, 0(r2)
add r3, r1, r4
sub r5, r6, r7The lw instruction uses the memory in its MEM stage. At the same time, a later instruction may need memory in its IF stage. If instruction and data accesses share one memory port, these operations conflict.
A timing view looks like this:
The processor can resolve this conflict by inserting a bubble, delaying one pipeline stage by one or more cycles.
Common Hardware Solutions
| Design choice | Structural hazard | Cost or trade-off |
|---|---|---|
| Separate instruction and data caches | Usually removed | Requires extra hardware |
| Multiported memory | Multiple accesses can occur together | More complex and expensive |
| Stall the pipeline | Hazard is always safe | Reduces throughput |
| Schedule instructions differently | May avoid the conflict | Requires compiler or programmer support |
A common design uses separate instruction and data caches. This is called a Harvard-style organization. With separate access paths, the IF stage can fetch an instruction while the MEM stage accesses data.
Another solution is a multiported memory. A port is an independent access path. Two ports allow two memory operations in the same cycle, although the hardware becomes more expensive.
Counting Stalls
When solving a numerical problem, use this method:
- List the resources used by every pipeline stage.
- Place instructions in a timing diagram.
- Check each cycle for simultaneous use of a single resource.
- Insert a stall wherever the resource cannot serve all requests.
- Recount the completion cycle and total execution time.
Do not confuse a structural hazard with a data hazard. A structural hazard is caused by a shortage of hardware resources. A data hazard is caused by an instruction needing a value produced by another instruction.
Common Error
A frequent mistake is to count a conflict merely because two instructions use the word “memory.” The relevant question is whether they use the same resource at the same time. A load in MEM does not conflict with an instruction fetch if the processor provides separate instruction and data memories.
Exercise
A five-stage pipeline has one shared memory for instructions and data. In one cycle, instruction I1 is in MEM performing a load, and instruction I2 is in IF. What type of hazard exists, and what is the usual basic solution?
Show answer
This is a structural hazard because both stages require the same memory resource. The basic solution is to stall one operation, usually by inserting a one-cycle bubble. Separate instruction and data memories could remove the conflict.