According to information on GitHub, flang -mmlir -fdynamic-heap-array
should force dynamic arrays to be allocated on the heap and hence avoid stackoverflows when large dynamic arrays are present. The example mentioned on GitHub works but the following does not.
! array.f90
module outprod_mod
contains
function outprod(y, z) result(x)
implicit none
real, intent(in) :: y(:), z(:)
real :: x(size(y), size(z))
integer :: i, j, n, m
m = size(y)
n = size(z)
do i = 1, m
do j = 1, n
x(i, j) = y(i) * z(j)
end do
end do
end function
end module outprod_mod
program prog
use outprod_mod, only : outprod
implicit none
integer, parameter :: k = 10000
integer :: i
real :: y(k)
y = 1.0
do i = 1, k
write (*, *) i, size(outprod(y(1:i), y(1:i)))
end do
end
With flang-new, it crashes with a segfault as follows.
$ uname -a && flang --version && flang -fno-stack-arrays -mmlir -fdynamic-heap-array -o array array.f90 && ./array
Linux 6.5.0-26-generic #26~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Tue Mar 12 10:22:43 UTC 2 x86_64 x86_64 x86_64 GNU/Linux
flang-new version 19.0.0git (git@github.com:llvm/llvm-project.git 37c175af955f0aeab67e8c553a0a47b2ed0fdba2)
Target: x86_64-unknown-linux-gnu
Thread model: posix
... ...
1445 2088025
1446 2090916
Segmentation fault (core dumped)
Questions:
- What are the correct flags to effectively force dynamic arrays to be allocated on the heap?
-fdynamic-heap-array
alone cannot be recognized by the compiler. It must be-mmlir -fdynamic-heap-array
. Is this intended?
Notes:
ulimit -s unlimited
seems to help, but we need a solution on flang’s side.- Compiled with Classic Flang 15.0.3 without any flags, the code runs fine.