This is an archive of the discontinued LLVM Phabricator instance.

[flang][runtime] Add dynamically allocated temporary storage
ClosedPublic

Authored by tblah on May 6 2023, 1:56 PM.

Details

Summary

These functions will be used as part of the HLFIR lowering for
forall/where. The contents of the API were requested by @jeanPerier.

The API is designed around that use case, assuming that the caller knows
through some side channel what size to allocate for boxes returned from
the pop() function.

Diff Detail

Event Timeline

tblah created this revision.May 6 2023, 1:56 PM
Herald added projects: Restricted Project, Restricted Project. · View Herald Transcript
Herald added a subscriber: sunshaoce. · View Herald Transcript
tblah requested review of this revision.May 6 2023, 1:56 PM

The llvm-libc has the same constraints. They cannot link against libc++. They have implemented optional and vector. They even have type traits. I didn't say copy and paste, but maybe you get inspired by some of their ideas.

flang/runtime/temporary-stack.cpp
19

Thanks a lot for delivering this so fast!

flang/runtime/temporary-stack.cpp
57

Shouldn't the old storage be freed here?

113–114

Instead of per push allocation for values, I think a single vector like storage for values the could be used too (you can get the size to allocate in the vector like storage via the bytes computed below).

You cannot use the ArrayConstructorVector here because it assumes the types parameters of all elements are the same (like for array ctor) which is not true here.

Now, I am also OK to start with alloc for each value/descriptor and to improve that in a later patch when this runtime is plugged and we can measure things.

116

Memcpy is likely not enough because deep copies should be made for allocatable component. Using
AssignTemporary like in flang/runtime/array-constructor.cpp is probably more robust (it can deal with deep copies/allocation, and will not call user defined assignments).
It can also deal with the fact that the input box may not be contiguous, which the memcpy below would require to be valid. The array values that are saved may bot be contiguous (e.g: forall(i=1,10) x(i,:) = x(10-i,:) where x is an assumed shape).

128

I describe the interface a bit badly here. We do not need to pop, we need to be able to fetch the values that were pushed in the same order again, and we need to be able to do it several times. So the memory cannot be freed here yet (and we want to do something like fetch_position +=1 rather than size_ -= 1).

It is more a FIFO structure that we can empty several times until we are done.

forall (xxx)
 where(some_expression_modified_by_assignment_1)
  assignment_1
  assignment_2_that_can_only_be_done_once_1_is_fully_evaluated
 end where
end forall

We will need to loop three times here:

  • The first time, push values for each where mask expression

(set stack position to zero again)

  • The second time, fetch the saved array values to mask assignment 1

(set stack position to zero again)

  • The third time, fetch the saved array values to mask assignment 2

Then only, the storage for the values can be freed. So we also need some "ResetFetchPosition(opaque_ptr)" entry point to reset the fetching position between two runs.

vzakhari added inline comments.May 8 2023, 4:18 PM
flang/include/flang/Runtime/temporary-stack.h
29

May I recommend returning the stack pointer via the return value rather than the argument? This way we can use LLVM IR's noalias attribute for the return value at the call site to say that the returned memory is "a new allocation" (https://llvm.org/docs/LangRef.html#parameter-attributes).

33

typo: adendum -> addendum

38

It is probably worth mentioning somewhere in this file whether the same opaquePtr can be used for both Value and Descriptor flavors of the runtime functions OR explicitly document that the opaquePtr intialized by CreateValueStack can only be used by Value functions, and same for CreateDescriptorStack pointer.

tblah updated this revision to Diff 520710.May 9 2023, 8:29 AM
tblah marked 7 inline comments as done.

Thanks for review everyone.

I haven't yet had time to fix the code style, I will do that when I get back
from EuroLLVM.

Changes:

  • fix typo
  • Add an accesor similar to std::vector::at()
  • return opaque pointer from constructor function
  • add missing free
  • use AssignTemporary instead of memcpy to duplicate fortran value
tblah updated this revision to Diff 522585.May 16 2023, 6:28 AM

Fixed code style

tblah marked an inline comment as done.May 16 2023, 6:29 AM
tblah added inline comments.
flang/runtime/temporary-stack.cpp
57

Good spot! Thanks

113–114

Lets merge this as it is and see (mostly because I'm in a rush)

128

I've added an accessor to read an element at position i

jeanPerier accepted this revision.May 17 2023, 6:42 AM

Thanks a lot, this this the hammer I need to save all kinds of values inside forall!

This revision is now accepted and ready to land.May 17 2023, 6:42 AM
This revision was landed with ongoing or failed builds.May 18 2023, 4:04 AM
This revision was automatically updated to reflect the committed changes.