[llvm] [OpenMP] Add tests for mapping of chained 'containing' structs (PR #156703)

Abhinav Gaba via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 4 10:11:22 PDT 2025


================
@@ -0,0 +1,217 @@
+// RUN: %libomptarget-compilexx-run-and-check-generic
+
+// XFAIL: *
+
+#include <cstdlib>
+#include <cstdio>
+#include <cassert>
+#include <cstring>
+
+#include <omp.h>
+
+struct R {
+  int d;
+  int e;
+  int f;
+};
+
+struct S {
+  int a;
+  int b;
+  struct {
+    int c;
+    R r;
+    R *rp;
+  } sub;
+  int g;
+};
+
+struct T {
+  int a;
+  int *ptr;
+  int b;
+};
+
+int main() {
+  R r;
+  R *rp = new R;
+  S s;
+  S *sp = new S;
+  T t;
+  T *tp = new T;
+
+  memset(&r, 0, sizeof(R));
+  memset(rp, 0, sizeof(R));
+  memset(&s, 0, sizeof(S));
+  memset(sp, 0, sizeof(S));
+  memset(&t, 0, sizeof(T));
+  memset(tp, 0, sizeof(T));
+
+  s.sub.rp = new R;
+  sp->sub.rp = new R;
+
+  memset(s.sub.rp, 0, sizeof(R));
+  memset(sp->sub.rp, 0, sizeof(R));
+
+  t.ptr = new int[10];
+  tp->ptr = new int[10];
+
+  memset(t.ptr, 0, sizeof(int)*10);
+  memset(tp->ptr, 0, sizeof(int)*10);
+
+#pragma omp target map(tofrom: r) map(tofrom: r.e)
+{
+  r.d++;
+  r.e += 2;
+  r.f += 3;
+}
+  printf ("%d\n", r.d); // CHECK: 1
+  printf ("%d\n", r.e); // CHECK-NEXT: 2
+  printf ("%d\n", r.f); // CHECK-NEXT: 3
+
+#pragma omp target map(tofrom: rp[:1]) map(tofrom: rp->e)
+{
+  rp->d++;
+  rp->e += 2;
+  rp->f += 3;
+}
+
+  printf ("%d\n", rp->d); // CHECK-NEXT: 1
+  printf ("%d\n", rp->e); // CHECK-NEXT: 2
+  printf ("%d\n", rp->f); // CHECK-NEXT: 3
+
+  int v;
+  int *orig_addr_v = &v;
+  bool separate_memory_space;
+
+#pragma omp target data map(v)
+  {
+    void *mapped_ptr_v =
+        omp_get_mapped_ptr(orig_addr_v, omp_get_default_device());
+    separate_memory_space = mapped_ptr_v != (void*) orig_addr_v;
----------------
abhinavgaba wrote:

I'm not sure if this is a reliable way to check for `unified shared memory` between device and host. `omp_get_default_device` may return the same value as `omp_get_initial_device`, if no device is available, in which case we're using host-fallback.

@dreachem, is that correct? Is there a better way to check for shared memory?

If not, it might be better to create a copy of the test in unified_shared_memory, and add `pragma omp requires unified_shared_memory`.


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


More information about the llvm-commits mailing list