]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
Handle operator new with alignment in usm transform.
authorHafiz Abid Qadeer <abidh@codesourcery.com>
Wed, 26 Oct 2022 08:51:01 +0000 (09:51 +0100)
committerHafiz Abid Qadeer <abidh@codesourcery.com>
Wed, 26 Oct 2022 08:51:01 +0000 (09:51 +0100)
Since C++17, the is a variant of operator new with alignment. This
patch converts it to omp_aligned_alloc when unified shared memory is
being used.

gcc/ChangeLog:

* omp-low.cc (usm_transform): Handle operator new with alignment.

libgomp/ChangeLog:

* testsuite/libgomp.c++/usm-2.C: New test.

gcc/testsuite/ChangeLog:

* g++.dg/gomp/usm-4.C: New test.
* g++.dg/gomp/usm-5.C: New test.

gcc/ChangeLog.omp
gcc/omp-low.cc
gcc/testsuite/ChangeLog.omp
gcc/testsuite/g++.dg/gomp/usm-4.C [new file with mode: 0644]
gcc/testsuite/g++.dg/gomp/usm-5.C [new file with mode: 0644]
libgomp/ChangeLog.omp
libgomp/testsuite/libgomp.c++/usm-2.C [new file with mode: 0644]

index 9b2a2007ac12cf349a15f9aed1dcaa201277d6e1..53cbfbab6039052500bf87c79b90b63222bec11c 100644 (file)
@@ -1,3 +1,7 @@
+2022-10-25  Abid Qadeer  <abidh@codesourcery.com>
+
+       * omp-low.cc (usm_transform): Handle operator new with alignment.
+
 2022-10-25  Marcel Vollweiler  <marcel@codesourcery.com>
 
        * omp-offload.cc (oacc_loop_auto_partitions): Removed OLF reduction
index f171181e2c4231360524967f70d233fdadee9288..b5b2681b6541fe7ca56403d867a9f1d3fdf9cded 100644 (file)
@@ -16339,6 +16339,7 @@ usm_transform (gimple_stmt_iterator *gsi_p, bool *,
       {
        gcall *gs = as_a <gcall *> (stmt);
        tree fndecl = gimple_call_fndecl (gs);
+       unsigned int args = gimple_call_num_args (gs);
        if (fndecl)
          {
            tree allocator = build_int_cst (pointer_sized_int_node,
@@ -16347,7 +16348,8 @@ usm_transform (gimple_stmt_iterator *gsi_p, bool *,
            if ((strcmp (name, "malloc") == 0)
                 || (fndecl_built_in_p (fndecl, BUILT_IN_NORMAL)
                     && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_MALLOC)
-                || DECL_IS_REPLACEABLE_OPERATOR_NEW_P (fndecl)
+                || (DECL_IS_REPLACEABLE_OPERATOR_NEW_P (fndecl)
+                    && args == 1)
                 || strcmp (name, "omp_target_alloc") == 0)
              {
                  tree omp_alloc_type
@@ -16361,7 +16363,9 @@ usm_transform (gimple_stmt_iterator *gsi_p, bool *,
                gimple_set_location (g, gimple_location (stmt));
                gsi_replace (gsi_p, g, true);
              }
-           else if (strcmp (name, "aligned_alloc") == 0)
+           else if ((strcmp (name, "aligned_alloc") == 0)
+                     || (DECL_IS_REPLACEABLE_OPERATOR_NEW_P (fndecl)
+                         && args == 2))
              {
                /*  May be we can also use this for new operator with
                    std::align_val_t parameter.  */
@@ -16372,8 +16376,12 @@ usm_transform (gimple_stmt_iterator *gsi_p, bool *,
                                              NULL_TREE);
                tree repl = build_fn_decl ("omp_aligned_alloc",
                                           omp_alloc_type);
-               tree align = gimple_call_arg (gs, 0);
-               tree size = gimple_call_arg (gs, 1);
+               int align_arg
+                 = DECL_IS_REPLACEABLE_OPERATOR_NEW_P (fndecl) ? 1: 0;
+               int size_arg
+                 = DECL_IS_REPLACEABLE_OPERATOR_NEW_P (fndecl) ? 0: 1;
+               tree align = gimple_call_arg (gs, align_arg);
+               tree size = gimple_call_arg (gs, size_arg);
                gimple *g = gimple_build_call (repl, 3, align, size,
                                               allocator);
                gimple_call_set_lhs (g, gimple_call_lhs (gs));
index 677a4c6ee12ab164ffd28254607fed324f6eb17c..d5548ebff3b479c5c59b891cc5065e5231c50826 100644 (file)
@@ -1,3 +1,8 @@
+2022-10-25  Abid Qadeer  <abidh@codesourcery.com>
+
+       * g++.dg/gomp/usm-4.C: New test.
+       * g++.dg/gomp/usm-5.C: New test.
+
 2022-10-24  Tobias Burnus  <tobias@codesourcery.com>
 
        * g++.dg/ext/unroll-1.C: Change 'cunrolli' to 'cunrolli1' in
diff --git a/gcc/testsuite/g++.dg/gomp/usm-4.C b/gcc/testsuite/g++.dg/gomp/usm-4.C
new file mode 100644 (file)
index 0000000..9096c44
--- /dev/null
@@ -0,0 +1,32 @@
+// { dg-do compile { target c++17 } }
+// { dg-options "-fopenmp -fdump-tree-usm_transform" }
+
+#pragma omp requires unified_shared_memory
+
+struct t1
+{
+  int a;
+  int b;
+};
+
+typedef unsigned char uint8_t;
+
+void
+foo (__SIZE_TYPE__ x, __SIZE_TYPE__ y)
+{
+  uint8_t *p1 = new (std::align_val_t(128)) uint8_t;
+  uint8_t *p2 = new (std::align_val_t(128)) uint8_t[40];
+  t1 *p3 = new (std::align_val_t(128)) t1;
+  t1 *p4 = new (std::align_val_t(128)) t1[y];
+  delete p1;
+  delete p3;
+  delete [] p2;
+  delete [] p4;
+}
+
+/* { dg-final { scan-tree-dump-times "omp_aligned_alloc \\(128, 1, 10\\)" 1 "usm_transform"  } } */
+/* { dg-final { scan-tree-dump-times "omp_aligned_alloc \\(128, 40, 10\\)" 1 "usm_transform"  } } */
+/* { dg-final { scan-tree-dump-times "omp_aligned_alloc" 4 "usm_transform"  } } */
+/* { dg-final { scan-tree-dump-times "omp_free" 4 "usm_transform"  } } */
+/* { dg-final { scan-tree-dump-not "operator new"  "usm_transform"  } } */
+/* { dg-final { scan-tree-dump-not "operator delete"  "usm_transform"  } } */
diff --git a/gcc/testsuite/g++.dg/gomp/usm-5.C b/gcc/testsuite/g++.dg/gomp/usm-5.C
new file mode 100644 (file)
index 0000000..2cc8a62
--- /dev/null
@@ -0,0 +1,30 @@
+// { dg-do compile { target c++17 } }
+// { dg-options "-fopenmp -foffload-memory=unified -fdump-tree-usm_transform" }
+
+struct t1
+{
+  int a;
+  int b;
+};
+
+typedef unsigned char uint8_t;
+
+void
+foo (__SIZE_TYPE__ x, __SIZE_TYPE__ y)
+{
+  uint8_t *p1 = new (std::align_val_t(128)) uint8_t;
+  uint8_t *p2 = new (std::align_val_t(128)) uint8_t[40];
+  t1 *p3 = new (std::align_val_t(128)) t1;
+  t1 *p4 = new (std::align_val_t(128)) t1[y];
+  delete p1;
+  delete p3;
+  delete [] p2;
+  delete [] p4;
+}
+
+/* { dg-final { scan-tree-dump-times "omp_aligned_alloc \\(128, 1, 10\\)" 1 "usm_transform"  } } */
+/* { dg-final { scan-tree-dump-times "omp_aligned_alloc \\(128, 40, 10\\)" 1 "usm_transform"  } } */
+/* { dg-final { scan-tree-dump-times "omp_aligned_alloc" 4 "usm_transform"  } } */
+/* { dg-final { scan-tree-dump-times "omp_free" 4 "usm_transform"  } } */
+/* { dg-final { scan-tree-dump-not "operator new"  "usm_transform"  } } */
+/* { dg-final { scan-tree-dump-not "operator delete"  "usm_transform"  } } */
index ee0f09ead133cd843eda456e347e11c4d2130015..f31fdfe373caffbe651ca27fee55b50e7d0a4b2b 100644 (file)
@@ -1,3 +1,7 @@
+2022-10-25  Abid Qadeer  <abidh@codesourcery.com>
+
+       * testsuite/libgomp.c++/usm-2.C: New test.
+
 2022-10-24  Tobias Burnus  <tobias@codesourcery.com>
 
        Backport from mainline:
diff --git a/libgomp/testsuite/libgomp.c++/usm-2.C b/libgomp/testsuite/libgomp.c++/usm-2.C
new file mode 100644 (file)
index 0000000..d12e550
--- /dev/null
@@ -0,0 +1,33 @@
+/* { dg-do run } */
+/* { dg-additional-options "-std=c++17" } */
+/* { dg-require-effective-target omp_usm } */
+#include <stdint.h>
+
+#pragma omp requires unified_shared_memory
+
+struct s1
+{
+  int a;
+};
+
+int
+main ()
+{
+  s1 *p1 = new s1;
+  s1 *p2 = new s1[10];
+
+  if (!p1 || !p2)
+    __builtin_abort ();
+
+  uintptr_t pp1 = (uintptr_t)p1;
+  uintptr_t pp2 = (uintptr_t)p2;
+  if (pp1 & 0x7f != 0)
+    __builtin_abort ();
+
+  if (pp2 & 0x7f != 0)
+    __builtin_abort ();
+
+  delete [] p2;
+  delete p1;
+  return 0;
+}