Main Content

coder.inline

Control inlining of a specific function in generated code

Description

example

coder.inline('always')forcesinliningof the current function in the generated code. Place thecoder.inlinedirective inside the function that you want to inline. The code generator does not inline entry-point functions and recursive functions. Also, the code generator does not inline functions intoparforloops, or inline functions called fromparforloops.

example

coder.inline('never')prevents inlining of the current function in the generated code. Prevent inlining when you want to simplify the mapping between the MATLAB®source code and the generated code.

Note

If you use thecodegen(MATLAB Coder)or thefiaccel(Fixed-Point Designer)command, you can disable inlining for all functions by using the- o禁用:内联option.

If you generate C/C++ code by using thecodegencommand or theMATLAB Coder™app, you might have different speed and readability requirements for the code generated for functions that you write and the code generated for MathWorks®功能。某些额外的全局设置enable you to separately control the inlining behavior for these two parts of the generated code base and at the boundary between them. See .

coder.inline('default')instructs the code generator to use internal heuristics to determine whether to inline the current function. Usually, the heuristics produce highly optimized code. Usecoder.inlineexplicitly in your MATLAB functions only when you need to fine-tune these optimizations.

Examples

collapse all

In this example, functionfoois not inlined in the generated code:

functiony = foo(x) coder.inline('never'); y = x;end

You can usecoder.inlinein control flow code. If the software detects contradictorycoder.inlinedirectives, the generated code uses the default inlining heuristic and issues a warning.

Suppose that you want to generate code for a division function that runs on a system with limited memory. To optimize memory use in the generated code, theinline_divisionfunction manually controls inlining based on whether it performs scalar division or vector division:

functiony = inline_division(dividend, divisor)%为scalar division, inlining produces smaller code% than the function call itself.ifisscalar(dividend) && isscalar(divisor) coder.inline('always');else% Vector division produces a for-loop.% Prohibit inlining to reduce code size.coder.inline('never');endifany(divisor == 0) error('Cannot divide by 0');endy = dividend / divisor;

More About

collapse all

Inlining

Technique that replaces a function call with the contents (body) of that function. Inlining eliminates the overhead of a function call, but can produce larger C/C++ code. Inlining can create opportunities for further optimization of the generated C/C++ code.

Extended Capabilities

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

GPU Code Generation
Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.

Version History

Introduced in R2011a