Issue 243147
Summary [flang] Large arrays declared in a BLOCK construct in the main program are always placed on the stack
Labels flang:fir-hlfir
Assignees
Reporter eugeneepshteyn
    flang puts main-program variables larger than 32 bytes in static memory, so a large array declared in the main program does not use the stack. The same array declared inside a BLOCK construct in the main program becomes a stack allocation, and the program crashes at the default 8 MB stack limit. Neither `-fsave-main-program` nor `-fno-automatic` changes this. Since 239263, `-mllvm -fstack-array-size` no longer applies either, so no option moves the array off the stack.

```fortran
program block_main
  implicit none
 integer :: i
  real(8) :: top(4000000)
  do i = 1, size(top)
    top(i) = i
  end do
  block
    real(8) :: inner(4000000)
    do i = 1, size(inner)
      inner(i) = 2 * i
    end do
    print *, top(size(top)), inner(size(inner))
  end block
end program
```

```console
$ flang -O2 block_main.f90 -o block_main
$ (ulimit -s 8192; ./block_main); echo $?
... Segmentation fault      ( ulimit -s 8192; ./block_main )
139
$ flang -fc1 -emit-fir block_main.f90 -o - | grep -E 'fir.global .*Etop|fir.alloca .*inner'
    %18 = fir.alloca !fir.array<4000000xf64> <{bindc_name = "inner", uniq_name = "_QFB1Einner"}>
  fir.global internal @_QFEtop <{alignment = 64 : i64}> : !fir.array<4000000xf64> {
```

Measured with flang at 2f6c791287848c44fbfd85e724fd4f8728aad7d4 on x86-64 Linux:

- `-O0` and `-O2` both crash, and adding `-fsave-main-program`, `-fno-automatic` or `-mllvm -fstack-array-size=1000` doesn't help. The emitted LLVM IR still has the `alloca [4000000 x double]` and no `malloc`.
- The program passes if `inner` is moved out of the BLOCK, or declared with an explicit `save`.
- gfortran 13.3 runs it at `-O0` and `-O2`, with both arrays in `.bss`.

`evaluate::IsSaved` (flang/lib/Evaluate/tools.cpp) checks the kind of the scope that owns the symbol. A BLOCK local is owned by its BLOCK construct's scope, so the main-program branches (`SaveBigMainProgramVariables`, `-fsave-main-program`) never apply to it. The `-fno-automatic` branch has the same restriction (`MainProgram` or `Subprogram`), so it skips BLOCK locals in subprograms too:

```fortran
subroutine s()
  implicit none
  real(8) :: a(1000)
 a = 1
  block
    real(8) :: b(1000)
    b = 2
    print *, a(1), b(1)
 end block
end subroutine
```

```console
$ flang -fc1 -emit-fir -fno-automatic s.f90 -o - | grep -E 'fir.global .*Ea|fir.alloca .*"b"'
 %5 = fir.alloca !fir.array<1000xf64> <{bindc_name = "b", uniq_name = "_QFsB1Eb"}>
  fir.global internal @_QFsEa <{alignment = 64 : i64}> : !fir.array<1000xf64> {
```

Treating BLOCK locals as saved in `IsSaved` would be wrong, because the standard does not give them implicit SAVE: F2023 8.5.16 p4 covers the scoping unit of a main program, and a BLOCK construct is a scoping unit of its own. Saved status also controls deallocation and finalization at END BLOCK (F2023 9.7.3.2 p3, F2023 7.5.6.3 p4). Two ways to fix it without that problem:

- A storage-only decision. A large BLOCK local whose enclosing scopes up to the main program are all BLOCK constructs gets static storage, as long as it is not allocatable, a pointer or finalizable, and has no default initialization. Locals of a BLOCK inside an OpenMP or OpenACC construct, a DO CONCURRENT or an internal procedure are excluded, because those can have more than one live instance.
- A byte-size threshold for large constant-size user variables in allocation-placement, moving them to the heap. This would also cover large locals in subprograms (192932), which gfortran places in static memory above `-fmax-stack-var-size`.

Related: 192932, 239263, and the RFC at https://discourse.llvm.org/t/rfc-flang-improving-temporary-and-local-array-allocations-unify-stackarray-memoryallocation/91353.

(This issue was filed with the help of AI)

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to