aboutsummaryrefslogtreecommitdiff
path: root/gcc/cp
diff options
context:
space:
mode:
authorKwok Cheung Yeung <kcy@codesourcery.com>2020-12-18 08:26:34 -0800
committerKwok Cheung Yeung <kcy@codesourcery.com>2020-12-18 08:38:30 -0800
commit3af02d32cce2ff1ff11d078cf8094305f57ca179 (patch)
treea9affc2bf477cd04d393cc9b5aa6392b54074a42 /gcc/cp
parent7ff5706fcd732b671afb2d308e8dab7e23050823 (diff)
downloadgcc-3af02d32cce2ff1ff11d078cf8094305f57ca179.zip
gcc-3af02d32cce2ff1ff11d078cf8094305f57ca179.tar.gz
gcc-3af02d32cce2ff1ff11d078cf8094305f57ca179.tar.bz2
openmp: Implicitly add 'declare target' directives for dynamic initializers in C++
2020-12-18 Kwok Cheung Yeung <kcy@codesourcery.com> gcc/ * langhooks-def.h (lhd_get_decl_init): New. (lhd_finish_decl_inits): New. (LANG_HOOKS_GET_DECL_INIT): New. (LANG_HOOKS_OMP_FINISH_DECL_INITS): New. (LANG_HOOKS_DECLS): Add LANG_HOOKS_GET_DECL_INIT and LANG_HOOKS_OMP_FINISH_DECL_INITS. * langhooks.c (lhd_omp_get_decl_init): New. (lhd_omp_finish_decl_inits): New. * langhooks.h (struct lang_hooks_for_decls): Add omp_get_decl_init and omp_finish_decl_inits. * omp-offload.c (omp_discover_declare_target_var_r): Use get_decl_init langhook in place of DECL_INITIAL. Call omp_finish_decl_inits langhook at end of function. gcc/cp/ * cp-lang.c (cxx_get_decl_init): New. (cxx_omp_finish_decl_inits): New. (LANG_HOOKS_GET_DECL_INIT): New. (LANG_HOOKS_OMP_FINISH_DECL_INITS): New. * cp-tree.h (dynamic_initializers): New. * decl.c (dynamic_initializers): New. * decl2.c (c_parse_final_cleanups): Add initializer entries from vars to dynamic_initializers. gcc/testsuite/ * g++.dg/gomp/declare-target-3.C: New.
Diffstat (limited to 'gcc/cp')
-rw-r--r--gcc/cp/cp-lang.c32
-rw-r--r--gcc/cp/cp-tree.h4
-rw-r--r--gcc/cp/decl.c4
-rw-r--r--gcc/cp/decl2.c7
4 files changed, 47 insertions, 0 deletions
diff --git a/gcc/cp/cp-lang.c b/gcc/cp/cp-lang.c
index 5d2aef4..0c3be0f 100644
--- a/gcc/cp/cp-lang.c
+++ b/gcc/cp/cp-lang.c
@@ -34,6 +34,8 @@ static tree cp_eh_personality (void);
static tree get_template_innermost_arguments_folded (const_tree);
static tree get_template_argument_pack_elems_folded (const_tree);
static tree cxx_enum_underlying_base_type (const_tree);
+static tree *cxx_omp_get_decl_init (tree);
+static void cxx_omp_finish_decl_inits (void);
/* Lang hooks common to C++ and ObjC++ are declared in cp/cp-objcp-common.h;
consequently, there should be very few hooks below. */
@@ -92,6 +94,12 @@ static tree cxx_enum_underlying_base_type (const_tree);
#undef LANG_HOOKS_GET_SUBSTRING_LOCATION
#define LANG_HOOKS_GET_SUBSTRING_LOCATION c_get_substring_location
+#undef LANG_HOOKS_OMP_GET_DECL_INIT
+#define LANG_HOOKS_OMP_GET_DECL_INIT cxx_omp_get_decl_init
+
+#undef LANG_HOOKS_OMP_FINISH_DECL_INITS
+#define LANG_HOOKS_OMP_FINISH_DECL_INITS cxx_omp_finish_decl_inits
+
/* Each front end provides its own lang hook initializer. */
struct lang_hooks lang_hooks = LANG_HOOKS_INITIALIZER;
@@ -233,6 +241,30 @@ tree cxx_enum_underlying_base_type (const_tree type)
return underlying_type;
}
+/* The C++ version of the omp_get_decl_init langhook returns the static
+ initializer for a variable declaration if present, otherwise it
+ tries to find and return the dynamic initializer. If not present,
+ it returns NULL. */
+
+static tree *
+cxx_omp_get_decl_init (tree decl)
+{
+ if (DECL_INITIAL (decl))
+ return &DECL_INITIAL (decl);
+
+ return hash_map_safe_get (dynamic_initializers, decl);
+}
+
+/* The C++ version of the omp_finish_decl_inits langhook allows GC to
+ reclaim the memory used by the hash-map used to hold dynamic initializer
+ information. */
+
+static void
+cxx_omp_finish_decl_inits (void)
+{
+ dynamic_initializers = NULL;
+}
+
#if CHECKING_P
namespace selftest {
diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h
index b4fb3deb..6cce9e2 100644
--- a/gcc/cp/cp-tree.h
+++ b/gcc/cp/cp-tree.h
@@ -5631,6 +5631,10 @@ extern GTY(()) tree static_aggregates;
/* Likewise, for thread local storage. */
extern GTY(()) tree tls_aggregates;
+/* A hash-map mapping from variable decls to the dynamic initializer for
+ the decl. This is currently only used by OpenMP. */
+extern GTY(()) decl_tree_map *dynamic_initializers;
+
enum overload_flags { NO_SPECIAL = 0, DTOR_FLAG, TYPENAME_FLAG };
/* These are uses as bits in flags passed to various functions to
diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c
index 6e8dd0b..8e63667 100644
--- a/gcc/cp/decl.c
+++ b/gcc/cp/decl.c
@@ -146,6 +146,10 @@ tree static_aggregates;
/* Like static_aggregates, but for thread_local variables. */
tree tls_aggregates;
+/* A hash-map mapping from variable decls to the dynamic initializer for
+ the decl. This is currently only used by OpenMP. */
+decl_tree_map *dynamic_initializers;
+
/* -- end of C++ */
/* A node for the integer constant 2. */
diff --git a/gcc/cp/decl2.c b/gcc/cp/decl2.c
index 6d8158a..af88e7f 100644
--- a/gcc/cp/decl2.c
+++ b/gcc/cp/decl2.c
@@ -5006,6 +5006,13 @@ c_parse_final_cleanups (void)
loop. */
if (tree vars = prune_vars_needing_no_initialization (&static_aggregates))
{
+ if (flag_openmp)
+ /* Add initializer information from VARS into
+ DYNAMIC_INITIALIZERS. */
+ for (t = vars; t; t = TREE_CHAIN (t))
+ hash_map_safe_put<hm_ggc> (dynamic_initializers,
+ TREE_VALUE (t), TREE_PURPOSE (t));
+
/* We need to start a new initialization function each time
through the loop. That's because we need to know which
vtables have been referenced, and TREE_SYMBOL_REFERENCED