`-frecursive`, `-assume recursion`, and recursion/thread-safety

I have the following questions regarding -frecursive for gfortran and -assume recursion for ifort.

Q1. Does -frecursive conflict with -fno-stack-arrays for gfortran?

According to Code Gen Options (The GNU Fortran Compiler)

-frecursive
    Allow indirect recursion by forcing all local arrays to be allocated on the stack. This flag cannot be used together with -fmax-stack-var-size= or -fno-automatic.

But I did not find any documentation about -fno-stack-arrays. I tried gfortran -frecursive -fno-stack-arrays with gfortran-13 and received no warning or error.

Q2. What is the default behavior of gfortran regarding -frecursive? Is it -frecursive or -fno-recursive?

Q3. Is -assume recursion the ifort correspondent of -frecursive?

Q4. Similar to Q1, does -assume recursion conflict with -heap-arrays for ifort?

Q5. Is it possible to compile a program so that all automatic arrays and local arrays are allocated on the heap (to avoid stack overflows) while ensuring recursion-safety and thread-safety? If yes, how should we specify the compiler flags for gfortran and ifort? If no, why? See a previous related discussion.

N.B.: I do understand that deep recursion can cause stack overflows. Let us assume that the recursion depth is small, e.g., 2 or 3.

Thank you.

1 Like

What is important for recursion AND thread safety is to NOT have static local variable. All local variables have to be dynamically allocated upon entering the routine (unless you really know what you are doing, then you can force some variable to be static with the save attribute if this is really what you want), but whether they are allocated on the stack or on the heap doesn’t matter.

So my understanding is:

  • -frecursive forces the local variables to be dynamically allocated, but the compiler has always the choice between the stack and the heap (even if the stack is generally the first choice of the compilers)
  • -fno-stack-arrays forces all dynamic allocations of arrays to be on the heap and not on the stack

To me, there’s no conflict between these options.

1 Like

Hi, @PierU , thank you for the response.

  • -frecursive forces the local variables to be dynamically allocated, but the compiler has always the choice between the stack and the heap (even if the stack is generally the first choice of the compilers)
  • -fno-stack-arrays forces all dynamic allocations of arrays to be on the heap and not on the stack

Then the following documentation does not seem precise, does it?

Thanks.

Probably not.

1 Like

For ifort, use assume recursion or -standard-semantics. You can also use -heap-arrays with either of these.

1 Like

Thank you Dr. Fortran @sblionel for the response, which is reassuring.

Thanks to Dr. Fortran’s explanation, I am 100% confident that -assume recursion -heap-arrays is the flag to make automatic arrays and local arrays allocated on the heap while ensuring recursion-safety and thread-safety.

What about gfortran? Is -frecursive --fno-stack-arrays the answer? Is this combination already the default?

What about other popular compilers like the NAG compiler and various flangs?

Thank you.