[clang] [llvm] [Clang][OpenMP] Capture mapped pointers on `target` by reference. (PR #145454)

Abhinav Gaba via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 23 11:40:37 PDT 2025


abhinavgaba wrote:

> Even for cases where both the pointer, pointee are mapped, the expectation is for the pointer to still be captured by-ref.
>Why?

Because we don't know if the pointer/pointee were both already existing on the device, in which case, there should be no pointer-attachment for them.

```c
  int x = 111;
  #pragma omp target enter data map(x) // map the pointee
  int *p = &x;
  #pragma omp target enter data map(p) // map the pointer

  #pragma omp target map(p, p[0:1]) // No pointer-attachment between p and p[0], since both
                                    // already existed on the device
  {
    p = (int*) 111;
  }

  printf("%ld\n", (long) p); // CHECK: 111
```

And as I mentioend, even if there is a pointer attachment, and the value of p cannot be modified in the region, a user may still assert that the address of p in the device matches its mapped address, like:

```c
int x;
int *p = &x;
#pragma omp target enter data map(p)
int64_t mapped_p = (int64_t) omp_get_mapped_ptr(&p, omp_get_default_device());

#pragma omp target map(p, p[0:1]) // pointer-attachment succeeds here
{
 assert(mapped_p == (int64_t) &p && "address of p in device should match the mapped address before the region.");
}
```


https://github.com/llvm/llvm-project/pull/145454


More information about the llvm-commits mailing list