Loop Flattening and Unrolling - 2022.1 English

AI Engine Kernel Coding Best Practices Guide (UG1079)

Document ID
Release Date
2022.1 English

Loops can be flattened completely with the chess_flatten_loop pragma. This can be useful for small loops that are not optimally automated by the AI Engine compiler. The flattening done in the final scheduling phase, such that the code generation, is still done based on the loop construct.

For loop flattening, the loop count can be determined by the compiler. In cases where the loop count cannot be determined by the tool automatically, you can set the loop count using the chess_loop_count pragma. For example:
for(int i=0;i<6;i++) chess_flatten_loop {...}
for(...) chess_loop_count(6) chess_flatten_loop {...}

With chess_unroll_loop(N), the loop body can be duplicated N-1 times, and the loop count is divided by N. The loop can also be completely unrolled by chess_unroll_loop(*). The loop is unrolled and rewritten as a repeated sequence of similar independent statements.