pragma HLS loop_merge

Description

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).

TheLOOP_MERGEpragma will seek to merge all loops within the scope it is placed. For example, if you apply aLOOP_MERGEpragma 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.

Syntax

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

#pragma HLS loop_merge force

where

  • 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.

Examples

Merges all consecutive loops in functionfoointo a single loop.

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

All loops insideloop_2(but notloop_2itself) are merged by using theforceoption. Place the pragma in the body ofloop_2.

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

See Also