Intel®高层次综合编译器专业版: 最佳实践指南

ID 683152
日期 12/04/2023
Public
文档目录

5.7. 将if-Statements放置在循环嵌套中尽可能最低的范围

如果有嵌套循环,请避免将循环放置在条件语句中。

这些条件可能会导致外部循环采用不同的路径(发散循环),这会降低组件的QoR,因为这些条件会阻止 Intel® HLS Compiler将循环流水线化。

例如,以下代码示例会导致发散式循环:
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();
    }
  }
}
最好重写该代码示例,如下:
for (int row = 0; row < outerTripCount; row++) {
  for (int col = 0; col < innerTripCount; col++) {
    if (loopCondition) {
      foo();
      }
    else {
      bar();
      }
   }
}
请在以下位置查看发散式循环的教程:
<quartus_installdir>/hls/examples/tutorials/best_practices/divergent_loops
了解更多详情。