Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Flang][OpenMP] Adjust Flang OpenMP lowering to handle zero sized arrays as mappings#2736

Open
agozillon wants to merge 1 commit into
amd-stagingfrom
amd/dev/agozillo/zero-size-presence-fix
Open

[Flang][OpenMP] Adjust Flang OpenMP lowering to handle zero sized arrays as mappings#2736
agozillon wants to merge 1 commit into
amd-stagingfrom
amd/dev/agozillo/zero-size-presence-fix

Conversation

@agozillon
Copy link
Copy Markdown

In this PR we are teaching the compiler to map the 1-byte that the runtime allocates for zero-sized arrays, so that the data is counted as resident for presence check purposes. This is OpenMP specification compliant.

The 1-byte on device shouldn't matter for later reallocations as users to stay compliant with the speciifcation MUST deallocate mapped data via an exit before modifying it in any way, e.g. resizing it, pointing it elsewhere asn so on. Otherwise it's non conformant code. The Fortran specification also protects us somewhat from this scenario as to resize a zero sized array you have to deallocate and then reallocate it, and assignment resizing also likely goes through the reallocation process under the hood.

We also add presence checking to the descriptor as well as the base address, so we verify if both are present on device now. This should also apply to all presence checking for ref_ptr/ptee/ref_ptr_ptee as well.

@agozillon agozillon requested review from dpalermo and mjklemm May 30, 2026 03:28
Comment on lines +5670 to +5672
llvm::Value *sizeCalc = builder.CreateMul(
elementCount, builder.getInt64(underlyingTypeSzInBits / 8),
"element_count");
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The multiplication does not seem needed. Whether it's a zero-length array can be determined by inspecting the number of elements; their type is not relevant.

Copy link
Copy Markdown
Author

@agozillon agozillon Jun 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whilst entirely true, I think you need to remember that this function isn't specifically for checking zero-length arrays, it's also for calculating every other types size, including regular arrays and allocatable/pointer arrays with an actual defined size, of N-dimensions, and it also needs to continue to function for C/C++ types in the future which do not come packaged with the nice (annoying) descriptor.

So, we could add more complicated logic (if bounds extent is appropriately set to look at the correct descriptor field, which I think it is, but it's been a while) but that would add more instructions to the LLVM-IR and complicate the logic inside of this function specifically for this one case which doesn't seem worth while :-)

Edit: Basically trying to keep the calculation generic and open to C/C++ (CIR) usage, but I think ideally in the future we might let the respective lowering's (CIR/FIR) dictate how the calculations are handled and we just grab an SSA at this level and use it.

…ays as mappings

In this PR we are teaching the compiler to map the 1-byte that the runtime allocates
for zero-sized arrays, so that the data is counted as resident for presence check
purposes. This is OpenMP specification compliant.

The 1-byte on device shouldn't matter for later reallocations as users to stay
compliant with the speciifcation MUST deallocate mapped data via an exit before
modifying it in any way, e.g. resizing it, pointing it elsewhere asn so on. Otherwise
it's non conformant code. The Fortran specification also protects us somewhat from
this scenario as to resize a zero sized array you have to deallocate and then
reallocate it, and assignment resizing also likely goes through the reallocation
process under the hood.

We also add presence checking to the descriptor as well as the base address, so we
verify if both are present on device now. This should also apply to all presence
checking for ref_ptr/ptee/ref_ptr_ptee as well.
@agozillon agozillon force-pushed the amd/dev/agozillo/zero-size-presence-fix branch from 2a873a3 to 1ef5e59 Compare June 4, 2026 04:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants