Skip to content
VLSI

RISC-V Pipeline Hazards: Data, Control, and Structural Hazards

  • 13 min read

RISC-V is an open-standard instruction set architecture (ISA) used in a wide range of processor designs, from embedded systems to more advanced computing platforms. One of the important techniques used to improve processor performance is instruction pipelining. Instead of completing one instruction before starting another, a pipelined processor divides instruction execution into stages and allows multiple instructions to progress through different stages at the same time.

However, overlapping instruction execution introduces situations where an instruction cannot proceed in its expected clock cycle. These situations are known as pipeline hazards. A hazard may occur because an instruction needs a result that is not yet available, a branch instruction changes the execution path, or multiple operations require the same hardware resource simultaneously.

In a typical five-stage RISC-V pipeline, instruction execution is divided into the following stages:

Pipeline Stage

Function

IF – Instruction Fetch

Retrieves the instruction from instruction memory or cache.

ID – Instruction Decode

Decodes the instruction and reads the required registers.

EX – Execute

Performs arithmetic, logical operations, or address calculations.

MEM – Memory Access

Reads data from or writes data to memory when required.

WB – Write Back

Writes the result into the destination register.


These stages allow several instructions to be processed simultaneously. For example, while one instruction accesses memory, another may execute an arithmetic operation and a third may be decoded.

The five-stage pipeline is a common educational model, not a requirement imposed by the RISC-V ISA. Actual RISC-V processors may use different pipeline structures, execution units, and hazard-management techniques.

The three major categories of pipeline hazards are data hazards, control hazards, and structural hazards. Understanding each category helps explain how processors maintain correct instruction execution while improving performance.

What Is a Data Hazard in RISC-V?

A data hazard occurs when an instruction depends on the result of an earlier instruction that has not yet completed. Since multiple instructions are active in the pipeline, a later instruction may attempt to use a register value before the previous instruction has written the updated result.

This situation can cause incorrect execution if the processor does not manage the dependency properly.

Example of a Data Hazard

Consider two consecutive RISC-V instructions. The first instruction adds the values stored in registers x1 and x2 and stores the result in register x5. The second instruction subtracts the value in register x3 from the value in x5 and stores the result in x6.

The second instruction depends on the result produced by the first instruction because it needs the updated value of x5. However, the first instruction may not have written its result into the register file when the second instruction reaches its execution stage.

If the second instruction reads the old value of x5, it may calculate an incorrect result. This situation is called a data hazard because the execution of one instruction depends on data produced by an earlier instruction.

Types of Data Hazards

Data hazards are commonly classified into three categories:

1. Read After Write (RAW)

A Read After Write hazard occurs when an instruction attempts to read a register before an earlier instruction has written its new value.

For example, an ADD instruction calculates a result and stores it in register x5. The next SUB instruction uses x5 as one of its input values. If the SUB instruction reads the register before the ADD instruction updates it, the processor may use outdated data. RAW is the most common data hazard in a basic in-order RISC-V pipeline.

2. Write After Read (WAR)

A Write After Read hazard occurs when a later instruction writes to a register before an earlier instruction has read its required value.

For example, an earlier instruction needs to read the original value of register x5, while a later instruction is scheduled to write a new value into the same register. If the later write happens first, the earlier instruction may receive the wrong value.

This type of hazard is generally not present in a simple in-order five-stage pipeline with fixed stage ordering. It can become relevant in processors that execute instructions out of order.

3. Write After Write (WAW)

A Write After Write hazard occurs when two instructions write to the same destination register, but their results are committed in the wrong order.

For example, two instructions independently calculate values for register x5. If the later instruction writes its result first and the earlier instruction writes afterward, the final register value may not match the program's intended execution order.

Like WAR, WAW is generally avoided in a basic in-order pipeline. It may occur in more complex processors that allow instructions to complete out of order.

How Are Data Hazards Resolved?

Processors use several techniques to handle data dependencies and prevent incorrect results.

Data Forwarding (Bypassing)

Forwarding allows a processor to send a result directly from an internal pipeline stage to another instruction that needs it. The value does not have to wait until it is written into the register file and read again.

For example, if an ADD instruction produces a result needed by the following SUB instruction, the processor can forward the result from an appropriate pipeline register to the ALU input. This reduces unnecessary waiting and helps maintain pipeline performance.

Pipeline Stalling

Sometimes, the required value is not available even through forwarding. In such situations, the processor temporarily pauses the dependent instruction by inserting a stall, commonly represented as a bubble in the pipeline.

Consider a situation in which a load instruction retrieves data from memory and stores it in register x5. The instruction immediately following it performs an addition using x5 and another register. The ADD instruction needs the value loaded from memory, but that value may not be ready when the ADD instruction reaches the execution stage.

In a conventional five-stage pipeline, forwarding alone may not be sufficient to eliminate this dependency. The hazard detection unit typically inserts a one-cycle stall before allowing the dependent instruction to proceed.

Compiler Instruction Scheduling

A compiler may rearrange independent instructions to reduce the time spent waiting for dependent operations. This technique is known as instruction scheduling.

For example, an independent arithmetic operation can sometimes be placed between a load instruction and the instruction that uses its result. This may reduce pipeline stalls without changing the program's intended behaviour.

What Is a Control Hazard in RISC-V?

A control hazard occurs when the processor does not immediately know which instruction should be fetched next because the program's execution path depends on the result of a control-flow instruction.

In RISC-V, control hazards commonly arise from conditional branches and jump instructions.

Example of a Control Hazard

Consider a RISC-V program in which a BEQ instruction compares the values stored in registers x1 and x2. If both values are equal, the processor must jump to a specific label in the program. If they are not equal, execution continues with the next sequential instruction.

While the processor is evaluating the branch condition, it may already have fetched instructions from the sequential execution path. If the branch is taken, those instructions may not belong to the correct execution path.

The processor must therefore determine the branch outcome and ensure that only the correct instructions affect the program state.

Causes of Control Hazards

Control hazards can occur due to several types of instruction behaviour:

  • Conditional branches: Instructions such as BEQ, BNE, BLT, and BGE depend on a comparison result to decide the next program address.
  • Unconditional jumps: The JAL instruction changes the program counter to a target address.
  • Indirect jumps: The JALR instruction calculates the next instruction address using a register value and an immediate.

The delay in determining the correct next program counter can interrupt the normal flow of instruction fetching.

How Are Control Hazards Resolved?

Processors use different techniques to reduce the performance impact of control hazards.

Branch Stalling

The processor may temporarily stop fetching new instructions until the branch outcome and target address are known. This approach prevents incorrect instructions from progressing but can reduce performance.

Branch Prediction

Branch prediction allows the processor to estimate whether a conditional branch will be taken or not taken.

There are two common approaches:

  • Static branch prediction: Uses a fixed rule or compiler-provided information to predict branch behaviour.
  • Dynamic branch prediction: Uses information from previous branch outcomes to make future predictions.

If the prediction is correct, instruction execution can continue with fewer interruptions. If it is incorrect, the processor must recover by redirecting instruction fetching to the correct path.

Pipeline Flushing

When a processor has fetched instructions from an incorrect path, it may remove those instructions from the pipeline. This process is called pipeline flushing.

For example, if a branch is predicted as not taken but is later found to be taken, the processor discards the incorrectly fetched instructions and begins fetching from the branch target.

Flushing maintains program correctness, although it introduces a performance penalty because the discarded instructions represent wasted pipeline work.

Early Branch Resolution

Some processor designs resolve branch conditions and calculate branch targets earlier in the pipeline. This can reduce the number of instructions that need to be discarded after a branch decision.

The effectiveness of this technique depends on the processor's pipeline design and branch-handling hardware.

What Is a Structural Hazard in RISC-V?

A structural hazard occurs when two or more instructions need the same hardware resource during the same clock cycle, but the processor does not have enough resources to serve all of them simultaneously.

Unlike a data hazard, which is caused by an instruction dependency, a structural hazard results from a hardware resource conflict.

Example of a Structural Hazard

Imagine a processor that uses a single memory unit for both instruction fetching and data access.

Suppose one instruction has reached the memory access stage and needs to read data from memory. At the same time, another instruction has reached the instruction fetch stage and needs to retrieve the next instruction.

Both instructions require access to the same memory resource during the same clock cycle. If the memory supports only one access at a time, the processor cannot perform both operations simultaneously. One instruction must wait, creating a structural hazard.

Common Causes of Structural Hazards

Shared Memory Resources

A processor with a single memory resource for both instruction and data access may experience a conflict when instruction fetching and data access occur simultaneously.

Limited Functional Units

If a processor has only one arithmetic unit, multiple instructions requiring that unit at the same time may compete for access. This is more relevant in processors with multiple execution paths or overlapping operations.

Memory Port Limitations

Some processor designs use memories with a limited number of read and write ports. When several operations request more access than the memory can support in one cycle, a structural conflict may occur.

How Are Structural Hazards Resolved?

Separate Instruction and Data Memories

One common solution is to provide separate instruction and data memory resources. This arrangement allows instruction fetching and data access to happen simultaneously.

Many educational RISC-V pipeline diagrams use this organisation to avoid the basic single-memory conflict.

Multi-Port Memory

A multi-port memory allows multiple accesses during the same clock cycle, subject to the capabilities of the memory design. This can reduce conflicts between instructions that need to access shared storage.

Additional Functional Units

A processor may include multiple functional units so that independent operations can execute simultaneously. For example, separate arithmetic and multiplication units can reduce competition for execution resources.

Pipeline Stalling

If a hardware conflict cannot be avoided, the processor may temporarily delay one instruction until the required resource becomes available. This preserves correct execution but reduces throughput.

Difference Between Data, Control, and Structural Hazards

Although all three hazards can interrupt pipeline execution, they occur for different reasons and require different handling techniques.

Comparison

Data Hazard

Control Hazard

Structural Hazard

Main cause

Data dependency between instructions

Uncertainty about the next instruction address

Competition for hardware resources

Common example

An instruction uses a result that is not ready

A conditional branch changes the execution path

Instruction fetch and data access need the same memory

Main concern

Availability of correct data

Correct instruction flow

Availability of hardware resources

Common solutions

Forwarding, stalling, scheduling

Branch prediction, flushing, early resolution

Resource duplication, multi-port hardware, stalling

Performance impact

Delays dependent instructions

May require discarding incorrectly fetched instructions

Delays operations competing for a resource

Understanding these differences is important because a processor cannot use one universal solution for every hazard. Each type requires a suitable detection and handling mechanism.

Role of Hazard Detection and Pipeline Control

A pipelined processor needs control logic to identify situations that may lead to incorrect execution. Hazard detection and pipeline control mechanisms help determine when instructions can proceed, when data must be forwarded, and when a stall or flush is required.

In a typical five-stage pipeline, the hazard detection unit checks register dependencies between instructions. For example, it can identify when a load instruction is followed by an instruction that immediately uses the loaded register.

Forwarding control logic selects the correct data source for an instruction's operands. Instead of using an outdated value from the register file, the processor can select a recently generated result from a pipeline register.

Branch control logic manages changes in the program counter. When a branch decision is made, the processor continues with the correct instruction path and removes invalid instructions if necessary.

These mechanisms work together to maintain correct program execution while reducing unnecessary pipeline interruptions. Their exact implementation varies across RISC-V processor designs.

Impact of Pipeline Hazards on Processor Performance

Pipeline hazards affect how efficiently a processor can execute instructions. A pipeline is designed to improve instruction throughput by allowing several instructions to be active at once. However, hazards can prevent the processor from using all its stages effectively.

Data hazards may introduce stalls when an instruction waits for an earlier result. Control hazards can interrupt instruction fetching and require the processor to discard instructions from an incorrect path. Structural hazards can leave pipeline stages waiting because a required hardware resource is unavailable.

The impact depends on the frequency of hazards and the processor's ability to manage them.

For example, a workload with many dependent load and arithmetic instructions may experience more data-related stalls. A program containing frequent conditional branches may be affected by branch prediction accuracy. Similarly, a processor with limited memory access resources may encounter structural conflicts more often.

Processor designers therefore consider hazard handling when selecting pipeline depth, memory organisation, execution units, forwarding paths, and branch-control mechanisms.

The goal is not necessarily to eliminate every possible hazard. Instead, the design aims to minimise unnecessary delays while maintaining correct instruction execution and balancing hardware complexity, power consumption, and performance.

Why Understanding RISC-V Pipeline Hazards Matters

Understanding pipeline hazards is useful for students and engineers working with computer architecture, processor design, RTL development, and functional verification.

For processor designers, hazard knowledge helps in developing forwarding paths, hazard detection logic, branch handling, and pipeline control mechanisms. For verification engineers, it provides a foundation for creating test cases that check instruction dependencies, branch behaviour, memory access conflicts, and correct pipeline recovery.

It is also important when studying processor performance. Knowing why an instruction stalls or why a pipeline is flushed helps engineers understand the relationship between instruction sequences and execution efficiency.

RISC-V provides a useful architecture for learning these concepts because its instruction set is open and supports different processor implementations. Studying hazards through a five-stage pipeline gives learners a practical starting point before exploring more advanced processor microarchitectures.

Conclusion

RISC-V pipeline hazards are important challenges in designing efficient pipelined processors. Data hazards occur when instructions depend on results that are not yet available, control hazards arise when the next execution path is uncertain, and structural hazards happen when multiple instructions compete for the same hardware resource.

Techniques such as data forwarding, pipeline stalling, branch prediction, pipeline flushing, and resource duplication help manage these situations. Understanding how these mechanisms work provides a strong foundation in computer architecture, RTL design, and processor verification.

For anyone learning RISC-V, studying the three types of pipeline hazards is an important step towards understanding how modern processors execute instructions correctly and efficiently.

Share LinkedIn X WhatsApp