主要内容

Control Inlining of MATLAB Functions in Generated HLS Code

Inlining is an optimization technique that replaces a function call with the body of that function. In the MATLAB® to HLS workflow, inlining can impact latency, throughput, and hardware resource utilization.

By default, the code generator inlines all MATLAB functions into the entry-point function. This default behavior enables further optimization in the generated HLS code. However, you can customize this behavior to preserve function hierarchy or to better control the balance between performance and resource usage.

Control Function Inlining

You can control inlining of MATLAB functions during code generation. When inlining instructions conflict, the code generator follows a hierarchy of inlining controls to determine whether to inline a function in the generated code:

  • Calling a function using coder.inlineCall or coder.nonInlineCall overrides all other inlining controls.

  • The coder.inline("always") or coder.inline("never") directive inside the body of a MATLAB function overrides global inlining settings and code configuration settings.

  • Global inlining settings override code configuration settings.

  • If no other inlining controls conflict, the code generator exhibits the inlining behavior dictated by code configuration settings.

Control Inlining at the Call Site

Override the inlining behavior for a specific call of the function myFunctionby using coder.inlineCall and coder.nonInlineCall:

  • coder.inlineCall(handle,arg1,...,argN) — Forces inlining of the function at this specific call site, regardless of global or function-level inlining settings.

  • coder.nonInlineCall(handle,arg1,...,argN) — Prevents inlining for this specific call site, even if inlining is enabled globally or at the function level.

Control Inlining Within the Function

Control inlining of a particular MATLAB function by using the coder.inline directive inside the function body. This directive overrides global inlining and configuration parameters settings.

  • Force inlining of a function — Use coder.inline("always") directive to insert the function body at the call site, eliminating function call overhead.

  • Prevent inlining of a function — Use coder.inline("never") directive for the function to remain a separate entity in the generated code, preserving hierarchy and potentially reducing resource usage.

Note

Certain built-in MathWorks® functions include predefined coder.inline directives that control whether the function is inlined during code generation. To override this setting at the call site, use coder.inlineCall or coder.nonInlineCall.

Control Inlining Globally

When generating code using the codegen command, you can globally enable or disable inlining inside the configuration object using InstantiateFunction option when generating code using the codegen command.

cfg = coder.config("hdl");
cfg.InstantiateFunctions = true; % Disable inlining

To override global settings for individual functions, use coder.inline, coder.inlineCall, or coder.nonInlineCall.

Improve Latency and Reduce Area by Inlining Functions

Inlining functions can improve latency and reduce area of the generated HLS code. This example shows how inlining affects the performance of a simple addition function.

MATLAB Function Without Inlining

Prevent inlining the compute_no_inline function in the generated code by using the coder.inline("never") directive. The code generator creates a separate hardware block for each call to compute_no_inline instead of inlining its operations into the calling function. Save the following MATLAB code as top_no_inline.m.

function y = top_no_inline(a,b)
    y1 = compute_no_inline(a,coder.ignoreConst(int16(2)));
    y2 = compute_no_inline(b,coder.ignoreConst(int16(3)));
    y = y1 + y2;
end

function y = compute_no_inline(x,y)
    coder.inline("never");
    y = x^y + 1;
end

MATLAB Function With Inlining

Inline the MATLAB function compute_inline in the generated code by using the coder.inline("always") directive. The code generator inserts the operations of compute_inline directly into the calling function, eliminating the function call overhead. Save the following MATLAB code as top_inline.m.

function y = top_inline(a,b)
    y1 = compute_inline(a,coder.ignoreConst(int16(2)));
    y2 = compute_inline(b,coder.ignoreConst(int16(3)));
    y = y1 + y2;
end

function y = compute_inline(x,y)
    coder.inline("always");
    y = x^y + 1;
end

Test Bench

Verify that both implementations produce the same result.

a = int16(3);
b = int16(4);
y1 = top_inline(a,b);
y2 = top_no_inline(a,b);
assert(y1 == y2);
disp("Test passed: Outputs match.");
Test passed: Outputs match.

Synthesis Results with Target Frequency of 300 MHz

Inlining removes function call overhead and lets the HLS tool pipeline the operations directly. Inlining small functions improves latency with minimal impact on hardware area. These synthesis results are achieved by using Xilinx Vitis HLS as the synthesis tool and target frequency of 300 MHz.

Metric

Without Inlining

With Inlining

Latency (cycles)

233.31

173.31

FF

911

839

LUTs

1366

1100

DSPs

4

4

See Also

|

Topics