This is an archive of the discontinued LLVM Phabricator instance.

[OpenMP] Fix delete map type in ref count debug messages
ClosedPublic

Authored by jdenny on Jun 18 2021, 11:14 AM.

Details

Summary

For example, without this patch:

$ cat test.c
int main() {
  int x;
  #pragma omp target enter data map(alloc: x)
  #pragma omp target enter data map(alloc: x)
  #pragma omp target enter data map(alloc: x)
  #pragma omp target exit data map(delete: x)
  ;
  return 0;
}
$ clang -fopenmp -fopenmp-targets=nvptx64-nvidia-cuda test.c
$ LIBOMPTARGET_DEBUG=1 ./a.out |& grep 'Creating\|Mapping exists\|last'
Libomptarget --> Creating new map entry with HstPtrBegin=0x00007ffddf1eaea8, TgtPtrBegin=0x00000000013bb040, Size=4, RefCount=1, Name=unknown
Libomptarget --> Mapping exists with HstPtrBegin=0x00007ffddf1eaea8, TgtPtrBegin=0x00000000013bb040, Size=4, RefCount=2 (incremented), Name=unknown
Libomptarget --> Mapping exists with HstPtrBegin=0x00007ffddf1eaea8, TgtPtrBegin=0x00000000013bb040, Size=4, RefCount=3 (incremented), Name=unknown
Libomptarget --> Mapping exists with HstPtrBegin=0x00007ffddf1eaea8, TgtPtrBegin=0x00000000013bb040, Size=4, RefCount=2 (decremented)
Libomptarget --> There are 4 bytes allocated at target address 0x00000000013bb040 - is not last

RefCount is reported as decremented to 2, but it ought to be reset
because of the delete map type, and is not last is incorrect.

This patch migrates the reset of reference counts from
DeviceTy::deallocTgtPtr to DeviceTy::getTgtPtrBegin, which then
correctly reports the reset. Based on the IsLast result from
DeviceTy::getTgtPtrBegin, targetDataEnd then correctly reports `is
last` for any deletion. DeviceTy::deallocTgtPtr is responsible only
for the final reference count decrement and mapping removal.

An obscure side effect of this patch is that a delete map type when
the reference count is infinite yields DelEntry=IsLast=false in
targetDataEnd and so no longer results in a
DeviceTy::deallocTgtPtr call. Without this patch, that call is a
no-op anyway besides some unnecessary locking and mapping table
lookups.

Diff Detail

Event Timeline

jdenny created this revision.Jun 18 2021, 11:14 AM
jdenny requested review of this revision.Jun 18 2021, 11:14 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 18 2021, 11:14 AM
Herald added a subscriber: sstefan1. · View Herald Transcript
grokos added inline comments.Jun 22 2021, 2:38 PM
openmp/libomptarget/src/device.cpp
361–362

It seems that after this patch ForceDelete is no longer necessary in the signature of deallocTgtPtr. There is only one instance where this variable is used and it's not a functional one; it is only used inside INFO to report whether the removal of a map entry is a forced one. I think that at this point such information is of no value since getTgtPtrBegin will already have printed out whether the entry is forcefully removed. Right?

jdenny added inline comments.Jun 22 2021, 2:44 PM
openmp/libomptarget/src/device.cpp
361–362

Good point. The "forcing" (the reset) doesn't happen here anymore. All that's left is a single decrement. I'll fix it. Thanks.

jdenny updated this revision to Diff 353818.Jun 22 2021, 4:37 PM

Applied grokos's suggestion: Removed ForceDelete from deallocTgtPtr.

Rebased.

jdenny marked an inline comment as done.Jun 22 2021, 4:37 PM
grokos accepted this revision.Jun 23 2021, 1:05 AM

LGTM, thanks for the patch!

This revision is now accepted and ready to land.Jun 23 2021, 1:05 AM
This revision was landed with ongoing or failed builds.Jun 23 2021, 6:59 AM
This revision was automatically updated to reflect the committed changes.

Thanks for the review.