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