[flang-commits] [flang] [flang][OpenMP] Allocate array reduction variables on the heap (PR #87773)

Tom Eccles via flang-commits flang-commits at lists.llvm.org
Mon Apr 8 04:06:27 PDT 2024


tblah wrote:

> Allocating on the stack vs heap has some tradeoffs with OpenMP. If there are a large number of threads and if the memory required is high then there is a possibility that we run out of stack space. On the other hand, allocating on the heap probably takes more time. The runtime probably uses locks so I guess it is safe, but please check.
> 
> > Reductions might be inlined inside of a loop so stack allocations are not safe.
> 
> We could use stacksave and stackrestore as well. This was the original suggestion in the patch. https://llvm.org/docs/LangRef.html#llvm-stacksave-intrinsic https://llvm.org/docs/LangRef.html#llvm-stackrestore-intrinsic

Stacksave/stackrestore would be awkward to implement because there isn't a convenient way to pass the saved stack pointer into the cleanup region. Adding an additional argument to the OpenMP operation definition feels very flang-specific. Maybe the stack save/restore should be added in OpenMPIRBuilder?

`fir.allocmem` is lowered to a call to `malloc`, which is thread safe: https://en.cppreference.com/w/c/memory/malloc

https://github.com/llvm/llvm-project/pull/87773


More information about the flang-commits mailing list