Files
llvm/offload/test/mapping/attach_host_ptr_from_basic.c
Abhinav GabaandClaude Opus 5.5 78a3aee439 Reland "[OpenMP][OMPTarget] Restore attached host pointers to their value before the FROM transfer" (#243980)
Reland of #233650, reverted in #243904 after two buildbot failures. The
PR has
two commits, so that the fix can be reviewed on its own:

1. `406b85fd7023`: revert of the revert PR 243904.
2. `dd7d863ac958`: the fix and new tests for the buildbot failures.

The fix resolved the nvptx/sycl buildbot fails. Verified via
https://lab.llvm.org/buildbot/#/builders/239/builds/1050,
https://lab.llvm.org/buildbot/#/builders/225/builds/26443.

-----

#### Original description

Since OpenMP 6.1, an attached pointer may be re-pointed on the host. A
FROM of its
containing storage must then keep the host's current value:

```c
S s = {&a[0], 0};
#pragma omp target enter data map(to : s, s.p[0 : 1])
s.p = &a[1];                                  // host only
#pragma omp target exit data map(from : s)
// main: s.p == &a[0], the value saved at attach time
// now:  s.p == &a[1]
```

The runtime now saves the host pointers that a FROM transfer overwrites
right before
the transfer, and writes them back once the transfer completes.
Previously it wrote
the attach-time value back after every FROM entry, even when the FROM
was skipped
(ref count > 1) or did not cover the pointer.

Tests: `offload/test/mapping/attach_host_ptr_from_*.c`, plus two Fortran
tests from
#229966 (co-authored with @agozillon). On a forced `amdgpu-clang-flang`
build
([#9094](https://lab.llvm.org/buildbot/#/builders/234/builds/9094)), the
C tests pass on
amdgpu and x86_64 and `derived_type_pointer_repointed_host_kept.f90`
passes on amdgpu;
`derived_type_allocatable_skipped_from_no_restore.f90` needs a
`libomptarget-debug`
runtime and was skipped. The only two `check-offload` failures there
also fail on
main
([#9093](https://lab.llvm.org/buildbot/#/builders/234/builds/9093)).

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-19 07:32:54 -08:00

112 lines
3.5 KiB
C

// RUN: %libomptarget-compile-generic -fopenmp-version=61
// RUN: %libomptarget-run-generic 2>&1 | %fcheck-generic
// UNSUPPORTED: apu
// Since OpenMP 6.1, the host pointer of an attached pointer may be re-pointed
// while its device pointer stays attached. A FROM transfer must then leave
// the host pointer with its value before the transfer, and a skipped FROM
// transfer must not touch it at all:
//
// enter data map(to: s, s.p[0:1], ...) ; device s.p -> device a[0]
// s.p = &a[1] ; host only
// <construct with FROM of s> ; host s.p must stay &a[1]
#include <stdio.h>
typedef struct {
int *p;
int x;
int *q;
} S;
int a[2], b[2];
int *ptrs[4];
int main() {
#pragma omp target enter data map(to : a, b)
// FROM skipped: the target region's exit takes the ref count of s 2 -> 1.
{
S s = {&a[0], 0, &b[0]};
#pragma omp target enter data map(to : s, s.p[0 : 1])
s.p = &a[1];
#pragma omp target map(tofrom : s)
{
s.x = 1;
}
// CHECK: skipped FROM: s.p kept: 1, s.x not copied back: 1
printf("skipped FROM: s.p kept: %d, s.x not copied back: %d\n",
s.p == &a[1], s.x == 0);
s.p = &a[0];
#pragma omp target exit data map(delete : s) map(release : s.p[0 : 1])
}
// FROM performed, covering the re-pointed s.p.
{
S s = {&a[0], 0, &b[0]};
#pragma omp target enter data map(to : s, s.p[0 : 1])
s.p = &a[1];
#pragma omp target map(alloc : s)
{
s.x = 2;
}
#pragma omp target exit data map(from : s) map(release : s.p[0 : 1])
// CHECK: real FROM: s.p kept: 1, s.x: 2
printf("real FROM: s.p kept: %d, s.x: %d\n", s.p == &a[1], s.x);
}
// FROM of only s.p: s.p keeps its re-pointed value; the re-pointed s.q is
// not touched.
{
S s = {&a[0], 0, &b[0]};
#pragma omp target enter data map(to : s, s.p[0 : 1], s.q[0 : 1])
s.p = &a[1];
s.q = &b[1];
#pragma omp target map(always, from : s.p)
{
}
// CHECK: partial FROM: s.p kept: 1, s.q kept: 1
printf("partial FROM: s.p kept: %d, s.q kept: %d\n", s.p == &a[1],
s.q == &b[1]);
s.p = &a[0];
s.q = &b[0];
#pragma omp target exit data map(delete : s) \
map(release : s.p[0 : 1], s.q[0 : 1])
}
// target update from of a member that does not cover the pointer.
{
S s = {&a[0], 0, &b[0]};
#pragma omp target enter data map(to : s, s.p[0 : 1])
s.p = &a[1];
#pragma omp target map(alloc : s)
{
s.x = 3;
}
#pragma omp target update from(s.x)
// CHECK: update from(s.x): s.p kept: 1, s.x: 3
printf("update from(s.x): s.p kept: %d, s.x: %d\n", s.p == &a[1], s.x);
s.p = &a[0];
#pragma omp target exit data map(delete : s) map(release : s.p[0 : 1])
}
// Strided target update from: ptrs[0] and ptrs[2] are transferred, ptrs[2]
// is attached and restored, the re-pointed ptrs[1] is not touched.
{
for (int i = 0; i < 4; ++i)
ptrs[i] = i < 2 ? &a[i] : &b[i - 2];
#pragma omp target enter data map(to : ptrs, ptrs[1][0 : 1], ptrs[2][0 : 1])
ptrs[1] = &a[0];
#pragma omp target update from(ptrs[0 : 2 : 2])
// CHECK: strided update from: ptrs[1] kept: 1, ptrs[2] restored: 1
printf("strided update from: ptrs[1] kept: %d, ptrs[2] restored: %d\n",
ptrs[1] == &a[0], ptrs[2] == &b[0]);
ptrs[1] = &a[1];
#pragma omp target exit data map(delete : ptrs) \
map(release : ptrs[1][0 : 1], ptrs[2][0 : 1])
}
#pragma omp target exit data map(delete : a, b)
return 0;
}