libgomp: Add helper functions for memory handling.

Message ID 20200720221952.3133728-1-y2s1982@gmail.com
State Superseded
Headers show
Series
  • libgomp: Add helper functions for memory handling.
Related show

Commit Message

Dimitrij Mijoski via Gcc-patches July 20, 2020, 10:19 p.m.
This patch adds few helper functions aims to improve readability of
fetching addresses, sizes, and values. It also proposes a syntax for
querying these information through the callback functions, similar to
that of LLVM implementation. The syntax format is <query type>_<varaible
name>, or <query type>_<variable type>_<member type>. '_' is the
delimiter between fields. '<query type>', as currently defined in the
enum, is either gompd_query_address or gompd_query_size: the first
handles address or offset queries while the second handles the size of
the variable/member. '<variable type>' refers to the variable type, and
'<member type>' refers to the data type of the member of the variable.
This code is incomplete: in particular, it currently lacks CUDA support,
as well as segment handling, and inlining of the functions.

2020-07-20  Tony Sim  <y2s1982@gmail.com>

libgomp/ChangeLog:

	* Makefile.am (libgompd_la_SOURCES): Add ompd-helper.c.
	* Makefile.in: Regenerate.
	* libgompd.h (FOREACH_QUERYTYPE): Add macro for query types.
	(query_type): Add enum for query types.
	(gompd_getQueryStringSize): Add declaration.
	(gompd_getQueryString): Add declaration.
	(gompd_getAddress): Add declaration.
	(gompd_getSize): Add declaration.
	(gompd_getValue): Add declaration.
	(gompd_getVariableAddress): Add declaration.
	(gompd_getVariableSize): Add declaration.
	(gompd_getVariableValue): Add declaration.
	(gompd_getMemberAddress): Add declaration.
	(gompd_getMemberSize): Add declaration.
	(gompd_getMemberValue): Add declaration.
	* ompd-helper.c: New file.

---
 libgomp/Makefile.am   |   2 +-
 libgomp/Makefile.in   |   5 +-
 libgomp/libgompd.h    |  53 ++++++++++
 libgomp/ompd-helper.c | 228 ++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 285 insertions(+), 3 deletions(-)
 create mode 100644 libgomp/ompd-helper.c

-- 
2.27.0

Patch

diff --git a/libgomp/Makefile.am b/libgomp/Makefile.am
index fe0a92122ea..d126bc655fc 100644
--- a/libgomp/Makefile.am
+++ b/libgomp/Makefile.am
@@ -90,7 +90,7 @@  libgomp_la_SOURCES = alloc.c atomic.c barrier.c critical.c env.c error.c \
 	oacc-mem.c oacc-async.c oacc-plugin.c oacc-cuda.c priority_queue.c \
 	affinity-fmt.c teams.c allocator.c oacc-profiling.c oacc-target.c
 
-libgompd_la_SOURCES = ompd-lib.c ompd-proc.c
+libgompd_la_SOURCES = ompd-lib.c ompd-proc.c ompd-helper.c
 
 include $(top_srcdir)/plugin/Makefrag.am
 
diff --git a/libgomp/Makefile.in b/libgomp/Makefile.in
index f74d5f3ac8e..fdd488ca98e 100644
--- a/libgomp/Makefile.in
+++ b/libgomp/Makefile.in
@@ -235,7 +235,7 @@  am_libgomp_la_OBJECTS = alloc.lo atomic.lo barrier.lo critical.lo \
 	$(am__objects_1)
 libgomp_la_OBJECTS = $(am_libgomp_la_OBJECTS)
 libgompd_la_LIBADD =
-am_libgompd_la_OBJECTS = ompd-lib.lo ompd-proc.lo
+am_libgompd_la_OBJECTS = ompd-lib.lo ompd-proc.lo ompd-helper.lo
 libgompd_la_OBJECTS = $(am_libgompd_la_OBJECTS)
 AM_V_P = $(am__v_P_@AM_V@)
 am__v_P_ = $(am__v_P_@AM_DEFAULT_V@)
@@ -593,7 +593,7 @@  libgomp_la_SOURCES = alloc.c atomic.c barrier.c critical.c env.c \
 	oacc-async.c oacc-plugin.c oacc-cuda.c priority_queue.c \
 	affinity-fmt.c teams.c allocator.c oacc-profiling.c \
 	oacc-target.c $(am__append_4)
-libgompd_la_SOURCES = ompd-lib.c ompd-proc.c
+libgompd_la_SOURCES = ompd-lib.c ompd-proc.c ompd-helper.c
 
 # Nvidia PTX OpenACC plugin.
 @PLUGIN_NVPTX_TRUE@libgomp_plugin_nvptx_version_info = -version-info $(libtool_VERSION)
@@ -817,6 +817,7 @@  distclean-compile:
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/oacc-plugin.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/oacc-profiling.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/oacc-target.Plo@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ompd-helper.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ompd-lib.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ompd-proc.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ordered.Plo@am__quote@
diff --git a/libgomp/libgompd.h b/libgomp/libgompd.h
index 495995e00d3..69bf3573d3c 100644
--- a/libgomp/libgompd.h
+++ b/libgomp/libgompd.h
@@ -30,12 +30,18 @@ 
 #define LIBGOMPD_H 1
 
 #include "omp-tools.h"
+#include <string.h>
 
 #define ompd_stringify(x) ompd_str2(x)
 #define ompd_str2(x) #x
 
 #define OMPD_VERSION 201811
 
+#define FOREACH_QUERYTYPE(TYPE)\
+	TYPE (gompd_query_address)\
+	TYPE (gompd_query_size)\
+
+
 extern ompd_callbacks_t gompd_callbacks;
 
 typedef struct _ompd_aspace_handle {
@@ -47,4 +53,51 @@  typedef struct _ompd_aspace_handle {
   ompd_size_t ref_count;
 } ompd_address_space_handle_t;
 
+typedef enum gompd_query_type {
+#define GENERATE_ENUM(ENUM) ENUM,
+  FOREACH_QUERYTYPE (GENERATE_ENUM)
+#undef GENERATE_ENUM
+} query_type;
+
+ompd_rc_t gompd_getQueryStringSize (size_t *, query_type, const char*,
+				    const char *);
+
+ompd_rc_t gompd_getQueryString (char **, query_type, const char*, const char *);
+
+ompd_rc_t gompd_getAddress (ompd_address_space_context_t *,
+			    ompd_thread_context_t *, ompd_address_t *,
+			    const char *, const char *, ompd_addr_t);
+
+ompd_rc_t gompd_getSize (ompd_address_space_context_t *,
+			 ompd_thread_context_t *, ompd_size_t *, const char *,
+			 const char *);
+
+ompd_rc_t gompd_getValue (ompd_address_space_context_t *,
+			  ompd_thread_context_t *,
+			  void *, ompd_address_t *, const char *, const char *);
+
+ompd_rc_t gompd_getVariableAddress (ompd_address_space_context_t *,
+				    ompd_thread_context_t *, ompd_address_t *,
+				    const char *, ompd_addr_t);
+
+ompd_rc_t gompd_getVariableSize (ompd_address_space_context_t *,
+				 ompd_thread_context_t *, ompd_size_t *,
+				 const char *);
+
+ompd_rc_t gompd_getVariableValue (ompd_address_space_context_t *,
+				  ompd_thread_context_t *,
+				  void *, ompd_address_t *, const char *);
+
+ompd_rc_t gompd_getMemberAddress (ompd_address_space_context_t *,
+				  ompd_thread_context_t *, ompd_address_t *,
+				  const char *, const char *, ompd_addr_t);
+
+ompd_rc_t gompd_getMemberSize (ompd_address_space_context_t *,
+			       ompd_thread_context_t *, ompd_size_t *,
+			       const char *, const char *);
+
+ompd_rc_t gompd_getMemberValue (ompd_address_space_context_t *,
+				ompd_thread_context_t *, void *,
+				ompd_address_t *, const char *, const char *);
+
 #endif /* LIBGOMPD_H */
diff --git a/libgomp/ompd-helper.c b/libgomp/ompd-helper.c
new file mode 100644
index 00000000000..dc8ea58c08c
--- /dev/null
+++ b/libgomp/ompd-helper.c
@@ -0,0 +1,228 @@ 
+/* Copyright (C) 2020 Free Software Foundation, Inc.
+   Contributed by Yoosuk Sim <y2s1982@gmail.com>.
+
+   This file is part of the GNU Offloading and Multi Processing Library
+   (libgomp).
+
+   Libgomp is free software; you can redistribute it and/or modify it
+   under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3, or (at your option)
+   any later version.
+
+   Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY
+   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+   FOR A PARTICULAR PURoffsetE.  See the GNU General Public License for
+   more details.
+
+   Under Section 7 of GPL version 3, you are granted additional
+   permissions described in the GCC Runtime Library Exception, version
+   3.1, as published by the Free Software Foundation.
+
+   You should have received a copy of the GNU General Public License and
+   a copy of the GCC Runtime Library Exception along with this program;
+   see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
+   <http://www.gnu.org/licenses/>.  */
+
+/* This file defines custom helper functions for OMPD functions.  */
+
+#include "libgompd.h"
+
+static const char *gompd_query_type_string[] = {
+#define GENERATE_STRING(STRING) ompd_stringify (STRING),
+  FOREACH_QUERYTYPE (GENERATE_STRING)
+#undef GENERATE_STRING
+};
+
+ompd_rc_t
+gompd_getQueryStringSize (size_t *size, query_type type,
+			  const char* variableType, const char *memberType)
+{
+  const char *sep = "_";
+  const char **typeString = &gompd_query_type_string[type];
+
+  *size = strlen (*typeString) + strlen (sep) + strlen (variableType);
+  if (memberType)
+    *size += strlen (sep) + strlen (memberType);
+
+  return ompd_rc_ok;
+}
+ompd_rc_t
+gompd_getQueryString (char **queryString, query_type type,
+		      const char* variableType, const char *memberType)
+{
+  if (!type || !variableType)
+    return ompd_rc_bad_input;
+
+  const char *sep = "_";
+  const char **typeString = &gompd_query_type_string[type];
+
+  size_t offset = 0;
+  strcpy (*queryString, *typeString);
+  offset += strlen (*typeString);
+  strcpy (*queryString + offset, sep);
+  offset += strlen (sep);
+  strcpy (*queryString + offset, variableType);
+  if (memberType)
+    {
+      offset += strlen (variableType);
+      strcpy (*queryString + offset, sep);
+      offset += strlen (sep);
+      strcpy (*queryString + offset, memberType);
+    }
+
+  return ompd_rc_ok;
+}
+
+ompd_rc_t
+gompd_getAddress (ompd_address_space_context_t *ah, ompd_thread_context_t *th,
+		  ompd_address_t *addr, const char *variableType,
+		  const char *memberType, ompd_addr_t seg)
+{
+  if (!ah || !th)
+    return ompd_rc_stale_handle;
+  ompd_rc_t ret = ompd_rc_ok;
+  char *queryString = NULL;
+  size_t querySize = 0;
+  ret = gompd_getQueryStringSize (&querySize, gompd_query_address, variableType,
+				  memberType);
+  if (ret != ompd_rc_ok)
+    return ret;
+  ret = gompd_callbacks.alloc_memory (querySize + 1, (void *) queryString);
+  if (ret != ompd_rc_ok)
+    return ret;
+  ret = gompd_getQueryString (&queryString, gompd_query_address, variableType,
+			      memberType);
+  if (ret != ompd_rc_ok)
+    return ret;
+
+  if (!memberType)
+    {
+      ret = gompd_callbacks.symbol_addr_lookup (ah, th, queryString, addr,
+						NULL);
+      if (ret != ompd_rc_ok)
+	return ret;
+    }
+  else if (addr)
+    {
+      ompd_size_t offset;
+      ompd_address_t offsetAddr;
+      ret = gompd_callbacks.symbol_addr_lookup (ah, th, queryString,
+						&offsetAddr, NULL);
+      if (ret != ompd_rc_ok)
+	return ret;
+      ret = gompd_callbacks.read_memory (ah, th, &offsetAddr,
+					 sizeof (ompd_size_t),
+					 (void *) &offset);
+      if (ret != ompd_rc_ok)
+	return ret;
+      addr->address += offset;
+    }
+  else
+    {
+      return ompd_rc_bad_input;
+    }
+
+  ret = gompd_callbacks.free_memory (queryString);
+  return ret;
+}
+
+ompd_rc_t
+gompd_getSize (ompd_address_space_context_t *ah, ompd_thread_context_t *th,
+	       ompd_size_t *size, const char *variableType,
+	       const char *memberType)
+{
+  if (!ah || !th)
+    return ompd_rc_stale_handle;
+  ompd_rc_t ret;
+  ompd_address_t sizeAddr;
+  char *queryString = NULL;
+  ret = gompd_getQueryString (&queryString, gompd_query_size, variableType,
+			      memberType);
+
+  if (ret != ompd_rc_ok)
+    return ret;
+
+  ret = gompd_callbacks.symbol_addr_lookup (ah, th, queryString, &sizeAddr,
+					    NULL);
+  if (ret != ompd_rc_ok)
+    return ret;
+
+  ret = gompd_callbacks.read_memory (ah, th, &sizeAddr, sizeof (ompd_size_t),
+				     (void *) size);
+  return ret;
+}
+
+ompd_rc_t gompd_getValue (ompd_address_space_context_t *ah,
+			  ompd_thread_context_t *th, void *value,
+			  ompd_address_t *addr, const char *variableType,
+			  const char *memberType)
+{
+  if (!ah || !th)
+    return ompd_rc_stale_handle;
+  if (!addr)
+    return ompd_rc_bad_input;
+
+  ompd_rc_t ret;
+  ompd_size_t size;
+
+  ret = gompd_getSize (ah, th, &size, variableType, memberType);
+  if (ret != ompd_rc_ok)
+    return ret;
+
+  ret = gompd_callbacks.read_memory (ah, th, addr, size, value);
+
+  return ompd_rc_ok;
+}
+
+ompd_rc_t
+gompd_getVariableAddress (ompd_address_space_context_t *ah,
+			  ompd_thread_context_t *th, ompd_address_t *addr,
+			  const char *variableType, ompd_addr_t seg)
+{
+  return gompd_getAddress (ah, th, addr, variableType, NULL, seg);
+}
+
+ompd_rc_t
+gompd_getVariableSize (ompd_address_space_context_t *ah,
+		       ompd_thread_context_t * th, ompd_size_t *size,
+		       const char *variableType)
+{
+  return gompd_getSize (ah, th, size, variableType, NULL);
+}
+
+ompd_rc_t
+gompd_getVariableValue (ompd_address_space_context_t *ah,
+			ompd_thread_context_t *th, void *value,
+			ompd_address_t *variableAddress,
+			const char *variableType)
+{
+  return gompd_getValue (ah, th, value, variableAddress, variableType, NULL);
+}
+
+ompd_rc_t
+gompd_getMemberAddress (ompd_address_space_context_t *ah,
+			ompd_thread_context_t *th, ompd_address_t *variableAddr,
+			const char *variableType, const char *memberType,
+			ompd_addr_t seg)
+{
+  if (!variableAddr)
+    return ompd_rc_bad_input;
+  return gompd_getAddress (ah, th, variableAddr, variableType, memberType, seg);
+}
+
+ompd_rc_t
+gompd_getMemberSize (ompd_address_space_context_t *ah,
+		     ompd_thread_context_t *th, ompd_size_t *size,
+		     const char *variableType, const char *memberType)
+{
+  return gompd_getSize (ah, th, size, variableType, memberType);
+}
+
+ompd_rc_t
+gompd_getMemberValue (ompd_address_space_context_t *ah,
+		      ompd_thread_context_t *th, void *value,
+		      ompd_address_t *addr, const char *variableType,
+		      const char *memberType)
+{
+  return gompd_getValue (ah, th, value, addr, variableType, memberType);
+}