llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[Flang] Should the MemoryAllocation pass be enabled by default?

Open jonathonpenix opened this issue 4 years ago • 12 comments

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

jonathonpenix avatar Jun 13 '22 22:06 jonathonpenix

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.

kiranchandramohan avatar Jun 14 '22 09:06 kiranchandramohan

@llvm/issue-subscribers-flang-ir

llvmbot avatar Jun 14 '22 12:06 llvmbot

-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.

banach-space avatar Jun 14 '22 14:06 banach-space

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!

jonathonpenix avatar Jun 14 '22 20:06 jonathonpenix

Sorry, I'm realising that I spoke too fast without doing enough digging. So,

  • -fdynamic-heap-array is defined in CLOptions.inc and is used to configure the MemoryAllocation pass when it's created,
  • MemoryAllocation pass is effectively always created, but dynamicArrayOnHeap is by default set to false (so you need to use -fdynamic-heap-array to 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!

banach-space avatar Jun 15 '22 10:06 banach-space

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.

rouson avatar Jun 17 '22 15:06 rouson

Hi @jonathonpenix , @kiranchandramohan , @banach-space , @rouson ,

Sorry for spamming, but you might be interested in https://github.com/llvm/llvm-project/issues/88344 .

Thanks.

zaikunzhang avatar Apr 11 '24 02:04 zaikunzhang

Hi, anyone has any idea about how to deal with https://github.com/llvm/llvm-project/issues/88344 ? Thank you.

zaikunzhang avatar Apr 16 '24 01:04 zaikunzhang

This was discussed briefly in today's flang call. You can see the minutes here: https://docs.google.com/document/d/1Z2U5UAtJ-Dag5wlMaLaW1KRmNgENNAYynJqLW2j2AZQ

clementval avatar Apr 16 '24 04:04 clementval

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

zaikunzhang avatar Apr 16 '24 07:04 zaikunzhang

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.

clementval avatar Apr 16 '24 16:04 clementval

Thank you @clementval . I do look forward to seeing and trying the improved version. Many thanks to the efforts of the llvm/flang community.

zaikunzhang avatar Apr 19 '24 01:04 zaikunzhang