public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc/devel/omp/gcc-12] Handle operator new with alignment in usm transform.
@ 2022-10-26  8:53 Hafiz Abid Qadeer
  0 siblings, 0 replies; only message in thread
From: Hafiz Abid Qadeer @ 2022-10-26  8:53 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:9c461798d5d2dadb5e74357ae0eae95a4478d968

commit 9c461798d5d2dadb5e74357ae0eae95a4478d968
Author: Hafiz Abid Qadeer <abidh@codesourcery.com>
Date:   Wed Oct 26 09:51:01 2022 +0100

    Handle operator new with alignment in usm transform.
    
    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.

Diff:
---
 gcc/ChangeLog.omp                     |  4 ++++
 gcc/omp-low.cc                        | 16 ++++++++++++----
 gcc/testsuite/ChangeLog.omp           |  5 +++++
 gcc/testsuite/g++.dg/gomp/usm-4.C     | 32 ++++++++++++++++++++++++++++++++
 gcc/testsuite/g++.dg/gomp/usm-5.C     | 30 ++++++++++++++++++++++++++++++
 libgomp/ChangeLog.omp                 |  4 ++++
 libgomp/testsuite/libgomp.c++/usm-2.C | 33 +++++++++++++++++++++++++++++++++
 7 files changed, 120 insertions(+), 4 deletions(-)

diff --git a/gcc/ChangeLog.omp b/gcc/ChangeLog.omp
index 9b2a2007ac1..53cbfbab603 100644
--- a/gcc/ChangeLog.omp
+++ b/gcc/ChangeLog.omp
@@ -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
diff --git a/gcc/omp-low.cc b/gcc/omp-low.cc
index f171181e2c4..b5b2681b654 100644
--- a/gcc/omp-low.cc
+++ b/gcc/omp-low.cc
@@ -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));
diff --git a/gcc/testsuite/ChangeLog.omp b/gcc/testsuite/ChangeLog.omp
index 677a4c6ee12..d5548ebff3b 100644
--- a/gcc/testsuite/ChangeLog.omp
+++ b/gcc/testsuite/ChangeLog.omp
@@ -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
index 00000000000..9096c445363
--- /dev/null
+++ b/gcc/testsuite/g++.dg/gomp/usm-4.C
@@ -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
index 00000000000..2cc8a625f64
--- /dev/null
+++ b/gcc/testsuite/g++.dg/gomp/usm-5.C
@@ -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"  } } */
diff --git a/libgomp/ChangeLog.omp b/libgomp/ChangeLog.omp
index ee0f09ead13..f31fdfe373c 100644
--- a/libgomp/ChangeLog.omp
+++ b/libgomp/ChangeLog.omp
@@ -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
index 00000000000..d12e550d507
--- /dev/null
+++ b/libgomp/testsuite/libgomp.c++/usm-2.C
@@ -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;
+}

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-10-26  8:53 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-10-26  8:53 [gcc/devel/omp/gcc-12] Handle operator new with alignment in usm transform Hafiz Abid Qadeer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).