Resolve Error: Compile-Time Recursion Limit Reached
R2026bIssue
When the code generator encounters a recursive function in the MATLAB® code, it attempts to determine the number of recursive calls. If it can determine recursion depth, it does not implement the recursive MATLAB function as a recursive function in the C or C++ code. Instead, the code generator produces multiple versions of the function in the generated code, each of which corresponds to a recursive call. By default, the maximum number of recursive function copies in the generated code is 50. If the number of recursive calls exceeds this limit, the code generator produces one of these errors:
Compile-time recursion limit
reached.
Compile-time recursion limit
reached. Value of input #argument_number of function
'function_name' may change at every
call.
Compile-time recursion limit
reached. Size or type of input #argument_number of
function 'function_name' may change at every
call.
Possible Solutions
To resolve this error, force the code generator to use run-time recursion by using one of these approaches:
If the error message states that the value of an input argument may change at every call, instruct the code generator to treat the input argument as nonconstant.
If the error message states that the size or type of an input argument may change at every call, instruct the code generator to treat the input argument as variable size.
Alternatively, increase the maximum allowed number of function specializations.
Force Run-Time Recursion by Specifying Depth Variable as Nonconstant
When the MATLAB code calls a recursive function a constant number of times, you can
force the code generator to use run-time recursion by instructing it to treat the
constant value as nonconstant. For example, consider this function, which calls the
local function recurSumElements a constant number times. Code
generation fails because the code generator produces a function specialization for
each recursive call, and the number of function specializations exceeds the
recursion limit.
function out = constValue_error(in) A = ones(1,in); maxDepth = 100; out = recurSumElements(A,maxDepth); end function out = recurSumElements(A,depthRemaining) if depthRemaining == 1 out = A(1); else out = A(1)+recurSumElements(A(2:end),depthRemaining-1); end end
To resolve this error, force the code generator to use run-time recursion by using
coder.ignoreConst to instruct the code
generator to ignore the constant value in the recursive function call.
function out = constValue_example(in) A = ones(1,in); maxDepth = 100; out = recurSumElements(A,coder.ignoreConst(maxDepth)); end function out = recurSumElements(A,depthRemaining) if depthRemaining == 1 out = A(1); else out = A(1)+recurSumElements(A(2:end),depthRemaining-1); end end
If the code generator cannot determine that the number of recursive calls is finite, it produces a run-time recursive function.
Force Run-Time Recursion by Specifying Input Array as Variable Size
When the MATLAB code calls a recursive function a constant number of times, you can
force the code generator to use run-time recursion by instructing it to treat the
constant value as nonconstant. For example, consider this function, which calls the
local function recurSumElements a constant number times. If the
input array A is fixed size, the code generator produces a
function specialization for each recursive call. Code generation fails if the number
of recursive calls exceeds the recursion limit.
function out = constSize_error(A) out = recurSumElements(A); end function out = recurSumElements(A) if isscalar(A) out = A(1); else out = A(1)+recurSumElements(A(2:end)); end end
To resolve this error, force the code generator to use run-time recursion by using
coder.varsize to explicitly specify
that the input array is variable size. Because code generation does not support
calling coder.varsize on an input argument, call
coder.varsize on a copy of the input argument.
function out = constSize_example(tmpA) A = tmpA; coder.varsize("A") out = recurSumElements(A); end function out = recurSumElements(A) if isscalar(A) out = A(1); else out = A(1)+recurSumElements(A(2:end)); end end
Alternatively, specify that the input argument is variable size when you call the
function by using coder.typeof. For example:
codegen constSize_error -args {coder.typeof(0,[1 100],[false true])}
Increase the Recursion Limit
Large numbers of function specializations can make the generated code harder to analyze and manage. However, if you prefer the generated code to use function specializations instead of run-time recursion, you can increase the recursion limit by using one of these approaches:
In a MATLAB Coder™ code configuration object, increase the value of the
CompileTimeRecursionLimitproperty.In the MATLAB Coder Code Generation Settings dialog box, increase the value of the Compile-time recursion limit parameter.
In the Simulink® Model Configuration dialog box, increase the value of the Compile-time recursion limit for MATLAB functions (Simulink) parameter.
See Also
codegen | coder.ignoreConst | coder.varsize