Main Content

coder.inline

Control inlining of current function in generated code

Description

coder.inline(option) controls the inlining of the current function in the generated C/C++ code, as specified by option.

  • coder.inline("always") instructs the code generator to replace a function call with the contents (body) of the called function in the generated code.

  • coder.inline("never") prevents inlining of the current function in the generated code.

Inlining eliminates the overhead of a function call and can create opportunities for further optimization of the generated code, but can generate larger, more complex code. Conversely, preventing inlining can simplify the mapping between the MATLAB® code and the generated code. By default, the code generator uses internal heuristics to determine whether to inline the current function. Usually, these heuristics produce highly optimized code. Use the coder.inline optimization directive explicitly in your MATLAB functions only when you need to fine-tune these optimizations.

Examples

collapse all

You can use coder.inline in flow-control code. If the software detects contradictory coder.inline directives, the generated code uses the default inlining heuristic and issues a warning.

The inline_division function controls inlining based on whether it performs scalar division or vector division:

function out = inline_example(a)
  if (a > 10)
    out = inline_division(a,2);
  else
    out = 5;
  end
end

function y = inline_division(dividend, divisor)

% For scalar division, inlining produces smaller code
% than the function call itself.  
if isscalar(dividend) && isscalar(divisor)
   coder.inline("always");
else
% Vector division produces a for-loop.
% Prohibit inlining to reduce code size.
   coder.inline("never");
end

if any(divisor == 0)
   error("Cannot divide by 0");
end

y = dividend / divisor;

Use coder.inline("never") in a function to prevent inlining in the generated code. In the generated code, calls to the subfcn1 function remains as function calls.

function out = inline_example(a)
  if (a > 10)
    out = subfcn1(a);
  else
    out = 5;
  end
end

function out = subfcn1(a)
  coder.inline("never");
  out = a + 10;
end

Input Arguments

collapse all

Control inlining of the current MATLAB function, specified as "always" or "never".

  • coder.inline("always") instructs the code generator to replace a function call with the contents (body) of the called function in the generated code. The coder.inline("always") directive does not support the inlining of:

    • Entry-point functions

    • Recursive functions

    • Functions that contain parfor-loops

    • Functions called from parfor-loops

  • coder.inline("never") prevents inlining of the current function in the generated code. The coder.inline("never") optimization directive does not prevent the inlining of:

    • Empty functions

    • Functions that return constant output

    To prevent inlining even in these situations, use the coder.ignoreConst function on an input at the function call site in your MATLAB code. See Resolve Issue: coder.inline("never") and coder.nonInlineCall Do Not Prevent Function Inlining.

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

expand all