pragma HLS loop_merge


Merge consecutive loops into a single loop to reduce overall latency, increase sharing, and improve logic optimization. Merging loops:

  • Reduces the number of clock cycles required in the RTL to transition between the loop-body implementations.
  • Allows the loops be implemented in parallel (if possible).

The LOOP_MERGE pragma will seek to merge all loops within the scope it is placed. For example, if you apply a LOOP_MERGE pragma in the body of a loop, Vivado® HLS applies the pragma to any sub-loops within the loop but not to the loop itself.

The rules for merging loops are:

  • If the loop bounds are variables, they must have the same value (number of iterations).
  • If the loop bounds are constants, the maximum constant value is used as the bound of the merged loop.
  • Loops with both variable bounds and constant bounds cannot be merged.
  • The code between loops to be merged cannot have side effects. Multiple execution of this code should generate the same results (a=b is allowed, a=a+1 is not).
  • Loops cannot be merged when they contain FIFO reads. Merging changes the order of the reads. Reads from a FIFO or FIFO interface must always be in sequence.


Place the pragma in the C source within the required scope or region of code:

#pragma HLS loop_merge force


  • force: An optional keyword to force loops to be merged even when Vivado HLS issues a warning.
    IMPORTANT!: In this case, you must manually insure that the merged loop will function correctly.


Merges all consecutive loops in function foo into a single loop.

void foo (num_samples, ...) {
  #pragma HLS loop_merge
  int i;
  loop_1: for(i=0;i< num_samples;i++) {

All loops inside loop_2 (but not loop_2 itself) are merged by using the force option. Place the pragma in the body of loop_2.

loop_2: for(i=0;i< num_samples;i++) {
#pragma HLS loop_merge force

See Also