Resolve Issue: coder.inline("never") and coder.nonInlineCall Do Not Prevent Function Inlining - MATLAB & Simulink (original) (raw)

Main Content

Issue

In certain cases, the code generator inlines a MATLABĀ® function, replacing the function call with a constant value, even though you call the function using coder.nonInlineCall or you include thecoder.inline("never") directive in the function body.

For example, create a function test_inline that calls local function local with a constant input. Instruct the code generator not to inline local by using thecoder.inline("never") directive.

function y = test_inline() x = local(0); if(x == 0) y = 0; else y = 1; end end

function y = local(x) coder.inline("never"); y = x; end

Generate C code for test_inline and inspect the entry-point function in the generated code. The code generator inlineslocal, replacing the function call with a constant value, even though the MATLAB definition contains the coder.inline("never") directive.

double test_inline(void) { return 0.0; }

Possible Solutions

To prevent the code generator from inlining a function, use coder.ignoreConst on one of the inputs at the function call site in your MATLAB code. The coder.ignoreConst function prevents the code generator from returning the function output as a constant value.

Modify test_inline to instruct the code generator to callcoder.ignoreConst on the input tolocal.

function y = test_inline() x = local(coder.ignoreConst(0)); if(x == 0) y = 0; else y = 1; end end

function y = local(x) coder.inline("never"); y = x; end

Generate C code for test_inline and inspect the entry-point function in the generated code. The code generator does not inlinelocal.

static double local(void) { return 0.0; } double test_inline(void) { return !(local() == 0.0); }

See Also

coder.ignoreConst | coder.inline | coder.inlineCall | coder.nonInlineCall