Intel® High Level Synthesis Compiler Pro Edition: Best Practices Guide

ID 683152
Date 10/04/2021
Public

A newer version of this document is available. Customers should click here to go to the newest version.

Document Table of Contents

5.7. Place if-Statements in the Lowest Possible Scope in a Loop Nest

If you have a nests of loops, avoid placing loops within conditional statements.

These conditions can cause the outer loop to take different paths (divergent loops), which can reduce the QoR of your component because these condition prevent the Intel® HLS Compiler from pipelining the loops.

For example, the following code example results in divergent loops:
for (int row = 0; row < outerTripCount; row++) {
  if (loopCondition)  {
    for (int col = 0; col < innerTripCount; col++) {
      foo();
      }
    }
  else {
    for (int col = 0; col < innerTripCount; col++) {
      bar();
    }
  }
}
This code example is better rewritten as follows:
for (int row = 0; row < outerTripCount; row++) {
  for (int col = 0; col < innerTripCount; col++) {
    if (loopCondition) {
      foo();
      }
    else {
      bar();
      }
   }
}
For more details, review the divergent loops tutorial available in the following location:
<quartus_installdir>/hls/examples/tutorials/best_practices/divergent_loops