<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/60106>60106</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [OpenMP] Possible device memory leak when using declared mappers
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            openmp
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          GuilhermeValarini
      </td>
    </tr>
</table>

<pre>
    ## Description

When running a simple program that uses custom mappers (`omp declare mapper`), the base structure being mapped is never deallocated. If the base structure holds a **PTR_AND_OBJ** member (pointers/arrays), the arrays themselves (pointee) are deallocated, but not the structure where the pointer lives.

## Steps to reproduce

```cpp
// RUN: %libomptarget-compile-run-and-check-generic

#include <cstdio>
#include <cstdlib>

#define NUM 1024

class C {
public:declare_mapper_target.cpp
  int *a;
};

#pragma omp declare mapper(id : C s) map(s.a[0 : NUM])

int main() {
  C c;
 c.a = (int *)malloc(sizeof(int) * NUM);
  for (int i = 0; i < NUM; i++) {
    c.a[i] = 1;
  }
#pragma omp target teams distribute parallel for map(mapper(id), tofrom : c)
  for (int i = 0; i < NUM; i++) {
 ++c.a[i];
  }
  int sum = 0;
  for (int i = 0; i < NUM; i++) {
 sum += c.a[i];
  }
  // CHECK: Sum = 2048
  printf("Sum = %d\n", sum);
  return 0;
}
```

One can use the [declare_mapper_target.cpp](https://github.com/llvm/llvm-project/blob/6a9933519dd94e48b59d07c7333dac37186734d0/openmp/libomptarget/test/mapping/declare_mapper_target.cpp) test above to check this.

```bash
# The bug is target independent. `x86_64` can be replace by any other arch.
clang++ -fopenmp -fopenmp-targets=x86_64 -g -Og declare_mapper_target.cpp -o test_case
LIBOMPTARGET_INFO=4 LIBOMPTARGET_DEBUG=1 ./test_case
```

At the end of the program, one can see that the map still contains allocated entries referencing the base structure mapped during the program.

```bash
Libomptarget --> Unloading target library!
Libomptarget --> Unregistered image 0x0000555aa10ad198 from RTL 0x0000555aa11e1b10!
Libomptarget --> Done unregistering images!
Libomptarget --> Removing translation table for descriptor 0x0000555aa10b4070
Libomptarget --> Done unregistering library!
Libomptarget --> Deinit target library!
Libomptarget --> OpenMP Host-Device pointer mappings after block at libomptarget:0:0:
Libomptarget --> Host Ptr           Target Ptr Size (B) DynRefCount HoldRefCount Declaration
Libomptarget --> 0x00007ffca9457ea0 0x0000555aa1217b80 8        1           0            s at declare_mapper_target.cpp:13:33
```

## Environment

- Manjaro Linux 22.0.0 (Sikaris)
- AMD Ryzen 7 3700X / NVIDIA GeForce RTX 3060 Ti
- CUDA 11.1
- clang version 16.0.0 (git@github.com:llvm/llvm-project.git 5c38c6a3aac48e45d8da3f30ff70942f434dfb53)

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJykV1tz2zoO_jXMC8YeipQs6cEPthW32W0ukyZnz1uGoiCbpxKpISlv01-_Q0m-5EzSbnczji0BBPABxIUUzqmdRlySZE2S4kr0fm_s8lOvmj3aFv8QjbBKq6vSVK9LwjhhHAp00qrOK6MJLQhdjd__2qMG22ut9A4EONV2DUJnzc6KFvxeeOgdOpC986aFVnQdWgeEZWRBTdtBhbIRFicOWVDCcsI24PcIpXAIztte-t4ilBiMDAsrUA40HtBChaJpjBQeqznc1O8J7k1TORBA2Iqw1cPT48vqrni5X_9jJECLbYk2gOqM0h6tI2wrrBWv7gLNSAiPrcPmgO4sgITlELy4ABPEyt6DNn4QP8P59x4tDrTJHDTqgG5-Gdcp6l89dg68AYudNVUv8c2iBR0_suuOYlvCtvD4fEf4CghLGlWatvPC7tDPpGk71eDM9nomdDWTe5TfZjvUaJX8m3WlZdNXCIRvpPOVMoRff8RsVHnmHtdUWCuNcPd8CxFl8SVXNsI52ABJ1yOh68tGScJXUza8jNnwMuKen9wDUNqHbRSET6IkLc7PR9udFbtWwDv5xTJVQYjNBsLeBjJhmZsLkqzpwLh7viVJEfb9Qmcw2wqlQ96y_AwcYAPyZB_kXADh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