[Flang] Should the MemoryAllocation pass be enabled by default?
503.bwaves fails on a segmentation fault when being built without specifying the -mmlir -fdynamic-heap-array flag. It seems like this is due to allocating arrays with sizes determined at runtime on the stack by default, resulting in an invalid access given large enough arrays.
When additionally passing the -mmlir -fdynamic-heap-array flag during compilation to allocate the arrays on the heap, 503.bwaves passes. (Additionally, increasing the stack size to accommodate the large arrays and running 503.bwaves compiled without the -fdynamic-heap-array flag also resulted in a pass for me.)
Should the default behavior for allocating dynamic arrays be changed so that 503.bwaves (and presumably other programs) passes without needing to specify extra flags? From what I can tell, gfortran is allocating these arrays on the heap by default.
Small reproducer:
PROGRAM prog
IMPLICIT NONE
call sr(14, 14, 14, 14, 14)
END
SUBROUTINE sr(v, w, x, y, z)
IMPLICIT NONE
INTEGER, INTENT(IN) :: v,w,x,y,z
REAL*8 a(v,w,x,y,z), b(v,w,x,y,z), c(v,w,x,y,z)
REAL*8 arr(x, y, z)
a(1, 1, 1, 1, 1) = 42
b(1, 1, 1, 1, 1) = 42
c(1, 1, 1, 1, 1) = 42
arr(1, 1, 1) = 42
END
The benchmark also works with the unlimited stack setting (ulimit -s unlimited). I think it is best to ask a question in discourse/slack channel with a pointer to this issue on what is the best course of action.
@llvm/issue-subscribers-flang-ir
-fdynamic-heap-array controls whether to enable the MemoryAllocation pass. The way that it's implemented right now makes it an internal compiler option, i.e. "hidden" from end users (probably still considered experimental).
To me, this question is more about whether to have the MemoryAllocation pass enabled by default or not. bwaves is just one particular example in which it would be beneficial. Let me update the title accordingly. Also, CC @schweitzpgi who wrote this pass.
The new title is definitely more fitting to what I was hoping to get at--thank you!
@banach-space and @schweitzpgi I also posted a question about this in the slack channel, but if the pass is still considered experimental, do either of you have suggestions for steps I could take towards improving the pass (assuming there are no major concerns about making it a default or more readily found/seen by users)? I see the TODO mentioned in the file, but am not entirely sure what to do with it. Thanks!
Sorry, I'm realising that I spoke too fast without doing enough digging. So,
-fdynamic-heap-arrayis defined in CLOptions.inc and is used to configure theMemoryAllocationpass when it's created,MemoryAllocationpass is effectively always created, but dynamicArrayOnHeap is by default set tofalse(so you need to use-fdynamic-heap-arrayto change that).
Here's a hint as why dynamicArrayOnHeap is false by default:
// Always move dynamic array allocations to the heap. This may result in more // heap fragmentation, so may impact performance negatively.
Hopefully I got it all right this time!
Side note: I don't think real* has ever been in a Fortran standard so it's a compiler extension. Apparently every compiler accepts it so there's little risk in using it, but it should produce an error when compiled flags that enforce standard-conformance, which can be very helpful flags to use.
Hi @jonathonpenix , @kiranchandramohan , @banach-space , @rouson ,
Sorry for spamming, but you might be interested in https://github.com/llvm/llvm-project/issues/88344 .
Thanks.
Hi, anyone has any idea about how to deal with https://github.com/llvm/llvm-project/issues/88344 ? Thank you.
This was discussed briefly in today's flang call. You can see the minutes here: https://docs.google.com/document/d/1Z2U5UAtJ-Dag5wlMaLaW1KRmNgENNAYynJqLW2j2AZQ
This was discussed briefly in today's flang call. You can see the minutes here: https://docs.google.com/document/d/1Z2U5UAtJ-Dag5wlMaLaW1KRmNgENNAYynJqLW2j2AZQ
Hi @clementval ,
Thank you and the community very much for the response.
Any suggestions about https://github.com/llvm/llvm-project/issues/88344, namely how to force flang-new to allocate all arrays on the heap (not necessarily by default but by some flags)?
Thanks.
Zaikun
As mentioned in the minutes, the pass is not meant to be production ready and it seems that it does not work on your example. Someone would have to spend some effort to make the pass better.
Thank you @clementval . I do look forward to seeing and trying the improved version. Many thanks to the efforts of the llvm/flang community.