coder.hdl.loopspec - Unroll or stream loops in generated HDL and High-Level Synthesis (HLS) code - MATLAB (original) (raw)
Unroll or stream loops in generated HDL and High-Level Synthesis (HLS) code
Syntax
Description
For HDL Code Generation:coder.hdl.loopspec('unroll')
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.
For HDL Code Generation:coder.hdl.loopspec('unroll',[unroll_factor](#burt1i6-1%5Fsep%5Fburt1i6-1-unroll%5Ffactor))
unrolls a loop by the specified unrolling factor,unroll_factor
, in the generated HDL code.
The generated HDL code is a loop statement that containsunroll_factor
instances of the original loop body. The number of loop iterations in the generated code is (originalloopiterations
/unroll_factor
). If (originalloopiterations
/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.
For HDL Code Generation:coder.hdl.loopspec('stream')
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.
For HDL Code Generation:coder.hdl.loopspec('stream',[stream_factor](#burt1i6-1%5Fsep%5Fburt1i6-1-stream%5Ffactor))
unrolls the loop with unroll_factor set to_originalloopiterations
_ /stream_factor
rounded down to the nearest integer, and also oversamples the loop. If (originalloopiterations
/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.
For HLS Code Generation:coder.hdl.loopspec('unroll')
fully unrolls thefor
-loop in the generated HLS code during synthesis. You must insert this pragma at the first line of the for
-loop body in your MATLAB code.
This pragma does not affect MATLAB simulation behavior.
For HLS Code Generation:coder.hdl.loopspec('unroll',[unroll_factor](#burt1i6-1%5Fsep%5Fburt1i6-1-unroll%5Ffactor))
unrolls a for
-loop by the specified unrolling factor,unroll_factor
, in the generated HLS code during synthesis. You must insert this pragma at the first line of thefor
-loop body in your MATLAB code.
This pragma does not affect MATLAB simulation behavior.
For HLS Code Generation:coder.hdl.loopspec('pipeline')
pipelines thefor
-loop with the initiation interval of 1, in the generated HLS code. You must insert this pragma before thefor
-loop to be pipelined.
This pragma does not affect MATLAB simulation behavior.
For HLS Code Generation:coder.hdl.loopspec('pipeline',[initiation_interval](#mw%5Ff8aac755-8a93-4271-9485-e902d6eeaba2))
pipelines the for
-loop by the specified initiation interval,initiation_interval
, in the generated HLS code. You must insert the pragma before the for
-loop to be pipelined. Theinitiation_interval
represents the number of clock cycles before the start of the next iteration of thefor
-loop.
This pragma does not affect MATLAB simulation behavior.
Examples
Completely unroll MATLAB loop in generated HDL code
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
Partially unroll MATLAB loop in generated HDL code
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
Completely stream MATLAB loop in generated HDL code
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
Partially stream MATLAB loop in generated HDL code
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
Completely unroll MATLAB loop in generated HLS code
Unroll loop in generated HLS 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
Partially unroll MATLAB loop in generated HLS code
Generate a loop statement in the HLS 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
Pipeline for
-loop in generated HLS code
In the generated HLS code, the for
-loop is pipelined during synthesis.
function y = myFun(a, b, c) y = int16(zeros(1,10)); coder.hdl.loopspec('pipeline'); for i = 1:10 y(i) = i + a*b + c; end end
Pipeline for
-loop with initiation_interval
in generated HLS code
In the generated HLS code, the for
-loop is pipelined with the initiation_interval
as 2
during synthesis.
function y = myFun(a, b, c) y = int16(zeros(1,10)); coder.hdl.loopspec('pipeline',2); for i = 1:10 y(i) = i + a*b + c; end end
Input Arguments
stream_factor
— Loop streaming factor
positive integer
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
unroll_factor
— Loop unrolling factor
positive integer
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
initiation_interval
— Initiation interval
positive integer
Initiation interval to pipeline for
-loops, specified as a positive integer.
The default value of initiation interval is 1
.
Example: 3
Version History
Introduced in R2015a
R2022a: Support for HLS Code Generation
This function supports HLS code generation.