coder.inline - Control inlining of current function in generated code - MATLAB (original) (raw)

Control inlining of current function in generated code

Syntax

Description

coder.inline([option](#mw%5Ffab04dc5-af49-4d64-b27c-a6d88a486708)) controls the inlining of the current function in the generated C/C++ code, as specified byoption.

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.

To control the inlining behavior of the code generator at each individual call site, use coder.inlineCall andcoder.nonInlineCall.

example

Examples

collapse all

Control Function Inlining

Create an entry-point function inliningEntryPoint that calls two local functions, local_Inline and local_NoInline. Both local functions return the square of the input value, but local_Inline uses the directive coder.inline("always"), while local_NoInline uses the directive coder.inline("never").

type inliningEntryPoint.m

function [x,y] = inliningEntryPoint(n) %#codegen arguments n (1,1) double end x = local_Inline(n); y = local_NoInline(n); end

function y = local_Inline(x) coder.inline("always"); y = x^2; end

function y = local_NoInline(x) coder.inline("never"); y = x^2; end

Generate C code for inliningEntryPoint and inspect the entry-point function in the generated code. The code generator inlines the call to local_Inline but does not inline the call to local_NoInline.

codegen -config:lib inliningEntryPoint

Warning: Code generation is using a coder.EmbeddedCodeConfig object. Because Embedded Coder is not installed, this might cause some Embedded Coder features to fail.

Code generation successful (with warnings): To view the report, open('codegen/lib/inliningEntryPoint/html/report.mldatx')

type(fullfile("codegen","lib","inliningEntryPoint","inliningEntryPoint.c"))

/*

/* Include Files */ #include "inliningEntryPoint.h"

/* Function Declarations */ static double local_NoInline(double x);

/* Function Definitions / /

/*

/*

Use coder.inline in Flow-Control Statements

You can use multiple coder.inline directives to control function inlining based on parameters such as input arguments.

Create the entry-point function conditionalInlining, which calls the local function simpleDivision. Use multiple coder.inline directives to instruct the code generator to inline simpleDivision only if both input arguments are scalar.

type conditionalInlining.m

function out = conditionalInlining(x,y) %#codegen out = simpleDivision(x,y); end

function y = simpleDivision(dividend, divisor) if isscalar(dividend) && isscalar(divisor) forceInlining = "always"; else forceInlining = "default"; end coder.inline(forceInlining) y = dividend / divisor; end

Generate C code for conditionalInlining, specifying scalar inputs, and examine the entry-point function in the generated code. The code generator inlines simpleDivision in the generated code.

codegen -config:lib conditionalInlining -args {3 4}

Warning: Code generation is using a coder.EmbeddedCodeConfig object. Because Embedded Coder is not installed, this might cause some Embedded Coder features to fail.

Code generation successful (with warnings): To view the report, open('codegen/lib/conditionalInlining/html/report.mldatx')

type(fullfile("codegen","lib","conditionalInlining","conditionalInlining.c"))

/*

/* Include Files */ #include "conditionalInlining.h"

/* Function Definitions / /

/*

Generate C code for conditionalInlining, specifying vector inputs, and examine the entry-point function in the generated code. The code generator uses internal heuristics to determine whether or not to inline simpleDivision in the generated code.

codegen -config:lib conditionalInlining -args {1:10 11:20}

Warning: Code generation is using a coder.EmbeddedCodeConfig object. Because Embedded Coder is not installed, this might cause some Embedded Coder features to fail.

Code generation successful (with warnings): To view the report, open('codegen/lib/conditionalInlining/html/report.mldatx')

type(fullfile("codegen","lib","conditionalInlining","conditionalInlining.c"))

/*

/* Include Files */ #include "conditionalInlining.h" #include "mrdivide_helper.h" #include "rt_nonfinite.h"

/* Function Definitions / /

/*

Input Arguments

collapse all

option — Control inlining

"always" | "never" | "default"

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

Tips

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™.

HDL Code Generation

Generate VHDL, Verilog and SystemVerilog code for FPGA and ASIC designs using HDL Coder™.

The coder.inline function supports MATLAB to High-Level Synthesis code generation.

Version History

Introduced in R2011a

expand all

R2024a: Support for High-Level Synthesis Code Generation

You can inline MATLAB functions in the generated HLS code.