Best Practices for Inlining MATLAB Functions for HLS Code Generation
Function inlining is an optimization technique that replaces a function call with the body of the function. Inlining eliminates the overhead of a function call and can create opportunities for further optimization of the generated code. However, inlining might also generate larger, more complex code. Therefore, inlining functions can impact the performance and area of your generated HLS code. To generate efficient HLS code, apply these best practices when deciding whether to inline MATLAB® functions.
Inline Small, Frequently Used Functions
Small functions that are called often — especially those performing simple arithmetic or logic—are ideal for inlining. Inlining these functions allows the HLS tool to optimize the logic more effectively.
Benefits
Reduces latency
Enables better pipelining
Has minimal impact on area due to small function size
Example
A MATLAB function that adds two numbers or performs a simple comparison.
Inline Functions with Simple Arithmetic
Functions that contain arithmetic operations like addition and multiplication, or logic operations like conditional checks benefit from inlining because the HLS tool can merge them into the surrounding logic, enabling more optimizations.
Benefits
Simplifies control logic
Improves throughput
Reduces FSM complexity
Example
A MATLAB function that implements an absolute value operation or a basic mathematical formula.
Avoid Inlining Large or Complex Functions
Inlining large or complex functions can lead to code bloat, increased synthesis time, and excessive resource usage. These functions may contain loops, nested conditionals, or large data structures that are better handled modularly.
Drawbacks of inlining large functions
Increases area due to logic duplication
Makes debugging and maintenance harder
May not yield significant performance gains
Example
A MATLAB function that performs matrix operations or contains multiple nested loops.
Inline Selectively When Functions Are Reused
If a function is reused in multiple places, inlining the function duplicates its logic at each call site. This can improve performance by enabling parallelism but it increases the area. Inline functions that are reused only if performance is critical and area is not a constraint. Otherwise, keep the function modular to share logic.
Example
A MATLAB function that computes a polynomial expression used in multiple parts of the design.
Use coder.inlineCall or coder.nonInlineCall for Fine-Grained Control
The coder.inlineCall and coder.nonInlineCall functions allow you to override all other
inlining settings at the call site. This is useful when you want to inline a
function in one place but not in another.
Benefits
Provides maximum flexibility
Overrides global and local inlining directives
Example
Pass a function call in a performance-critical path to the
coder.inlineCall function to inline the function call at
the call site, but retain default inlining behavior elsewhere.
Use coder.inline("always") or coder.inline("never") Inside Functions
The coder.inline("always") and
coder.inline("never") directives control whether a function
should always or never be inlined, regardless of global settings. See coder.inline. Place the directive inside the function
definition.
Benefits
Consistent behavior across all call sites
Enforces design constraints
Example
Use coder.inline("never") to prevent inlining of a utility
function to preserve hierarchy in the generated HLS code.