Main Content

coder.hdl.loopspec

Unroll or stream loops in generated HDL and SystemC code

Description

example

For HDL Code Generation: coder.hdl.loopspec('unroll') % HDL Code fully unrolls a loop in the generated HDL code. Instead of a loop statement, the generated code contains multiple instances of the loop body, with one loop body instance per loop iteration.

The coder.hdl.loopspec pragma does not affect MATLAB® simulation behavior.

Note

If you specify the coder.unroll pragma, this pragma takes precedence over coder.hdl.loopspec. coder.hdl.loopspec has no effect.

example

For HDL Code Generation: coder.hdl.loopspec('unroll',unroll_factor) % HDL Code unrolls a loop by the specified unrolling factor, unroll_factor, in the generated HDL code.

The generated HDL code is a loop statement that contains unroll_factor instances of the original loop body. The number of loop iterations in the generated code is (original_loop_iterations / unroll_factor). If (original_loop_iterations / unroll_factor) has a remainder, the remaining iterations are fully unrolled as loop body instances outside the loop.

This pragma does not affect MATLAB simulation behavior.

example

For HDL Code Generation: coder.hdl.loopspec('stream') % HDL Code generates a single instance of the loop body in the HDL code. Instead of using a loop statement, the generated code implements local oversampling and added logic to match the functionality of the original loop.

You can specify this pragma for loops at the top level of your MATLAB design.

This pragma does not affect MATLAB simulation behavior.

example

For HDL Code Generation: coder.hdl.loopspec('stream',stream_factor) % HDL Code unrolls the loop with unroll_factor set to original_loop_iterations / stream_factor rounded down to the nearest integer, and also oversamples the loop. If (original_loop_iterations / stream_factor) has a remainder, the remainder loop body instances outside the loop are not oversampled, and run at the original rate.

You can specify this pragma for loops at the top level of your MATLAB design.

This pragma does not affect MATLAB simulation behavior.

example

For SystemC Code Generation: coder.hdl.loopspec('unroll') % SystemC Code inserts the pragma at the first line of the for-loop body in the generated SystemC code. This pragma indicates that the loop should be completely unrolled during synthesis.

This pragma does not affect MATLAB simulation behavior.

example

For SystemC Code Generation: coder.hdl.loopspec('unroll',unroll_factor) % SystemC Code inserts the pragma at the first line of the for-loop body in the generated SystemC code. This pragma indicates that the loop should be unrolled by a given unroll_factor during synthesis.

This pragma does not affect MATLAB simulation behavior.

Examples

collapse all

Unroll loop in generated code.

function y = hdltest
    pv = uint8(1);
    y = uint8(zeros(1,10));
  
    coder.hdl.loopspec('unroll');
    % Optional comment between pragma and loop statement
    for i = 1:10
        y(i) = pv + i;
    end
end

Generate a loop statement in the HDL code that has two iterations and contains five instances of the original loop body.

function y = hdltest
    pv = uint8(1);
    y = uint8(zeros(1,10));
  
    coder.hdl.loopspec('unroll', 5);
    % Optional comment between pragma and loop statement
    for i = 1:10
        y(i) = pv + i;
    end
end

In the generated code, implement the 10-iteration MATLAB loop as a single instance of the original loop body that is oversampled by a factor of 10.

function y = hdltest
    pv = uint8(1);
    y = uint8(zeros(1,10));
  
    coder.hdl.loopspec('stream');
    % Optional comment between pragma and loop statement
    for i = 1:10
        y(i) = pv + i;
    end
end

In the generated code, implement the 10-iteration MATLAB loop as five instances of the original loop body that are oversampled by a factor of 2.

function y = hdltest
    pv = uint8(1);
    y = uint8(zeros(1,10));
  
    coder.hdl.loopspec('stream', 2);
    % Optional comment between pragma and loop statement
    for i = 1:10
        y(i) = pv + i;
    end
end

Unroll loop in generated SystemC code.

function y = hdltest
    pv = uint8(1);
    y = uint8(zeros(1,10));
    coder.hdl.loopspec('unroll');
    % Optional comment between pragma and loop statement
    for i = 1:10
        y(i) = pv + i;
    end
end

Generated SystemC Code:

#pragma once

#include "rtwtypes.hpp"

class hdltestClass
{
 public:

  void hdltest(uint8_T (&y)[10])
  {
    /*  Optional comment between pragma and loop statement */
   L1:
    for (int32_T i = 0; i < 10; i = i + 1) {
      HLS_UNROLL_LOOP( ON, "L1" );
      y[i] = (uint8_T)(i + 2);
    }
  }
};

Generate a loop statement in the SystemC code that has two iterations and contains five instances of the original loop body.

function y = hdltest
    pv = uint8(1);
    y = uint8(zeros(1,10));
    coder.hdl.loopspec('unroll',5);
    % Optional comment between pragma and loop statement
    for i = 1:10
        y(i) = pv + i;
    end
end

Generated SystemC Code:

#pragma once

#include "rtwtypes.hpp"

class hdltestClass
{
 public: 

  void hdltest(uint8_T (&y)[10])
  {
    /*  Optional comment between pragma and loop statement */
   L1:
    for (int32_T i = 0; i < 10; i = i + 1) {
      HLS_UNROLL_LOOP( COMPLETE, 5, "L1" );
      y[i] = (uint8_T)(i + 2);
    }
  }
};

Input Arguments

collapse all

Loop streaming factor, specified as a positive integer.

Setting stream_factor to the number of original loop iterations is equivalent to fully streaming the loop, or using coder.hdl.loopspec('stream').

Example: 4

Number of loop body instances, specified as a positive integer.

Setting unroll_factor to the number of original loop iterations is equivalent to fully unrolling the loop, or using coder.hdl.loopspec('unroll').

Example: 10

Version History

Introduced in R2015a

See Also