Hi Jakub,

Here is a patch for generation of tables containing omp-functions addresses.
It is just a first step, as it lacks generation of similar tables for globals,
but having it in the branch would ease our further development, as we would be
able to base on this.

This patch introduces new function OMP_FINISH_FILE which goes through cgraph and
saves address of every non-external function with 'omp declare target' attribute
to a new symbol.  Then this symbol is stored to a dedicated section
'.omp_fn_table_section'.  When several files are linked together, these sections
are concatenated, so we have an aggregate table with function address.  In a
further patches I'm going to 1) extend this functionality to globals, 2) add a
special symbol at the beginning of the aggregated section (for that I'd add a
dedicated file with this symbol and pass it to the linker as a first input
file).

Is this patch ok for gomp4-branch?

Changelog:

2013-11-15  Michael Zolotukhin  <michael.v.zolotuk...@gmail.com>

        * omp-low.c (omp_finish_file): New.
        * omp-low.h (omp_finish_file): Declare new function.
        * toplev.c: include "omp-low.h"
        (compile_file): Call omp_finish_file.


---
 gcc/omp-low.c | 42 ++++++++++++++++++++++++++++++++++++++++++
 gcc/omp-low.h |  1 +
 gcc/toplev.c  |  3 +++
 3 files changed, 46 insertions(+)

diff --git a/gcc/omp-low.c b/gcc/omp-low.c
index 797a492..8dfdc92 100644
--- a/gcc/omp-low.c
+++ b/gcc/omp-low.c
@@ -12101,4 +12101,46 @@ make_pass_omp_simd_clone (gcc::context *ctxt)
   return new pass_omp_simd_clone (ctxt);
 }
 
+/* Create new symbol containing addresses of functions.  */
+void
+omp_finish_file (void)
+{
+  struct cgraph_node *node;
+  const char *section_name = ".omp_fn_table_section";
+  tree new_decl = build_decl (UNKNOWN_LOCATION,
+      VAR_DECL, get_identifier (".omp_fn_table"), void_type_node);
+  vec<constructor_elt, va_gc> *v;
+  tree ctor;
+  int num = 0;
+
+  /* TODO: we need to create similar table for global variables.  */
+  vec_alloc (v, 0);
+  FOR_EACH_FUNCTION (node)
+    {
+      if (DECL_EXTERNAL (node->decl)
+         || !lookup_attribute ("omp declare target",
+                               DECL_ATTRIBUTES (node->decl)))
+       continue;
+      CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_fold_addr_expr (node->decl));
+      num++;
+    }
+  ctor
+    = build_constructor (build_array_type_nelts (pointer_sized_int_node, num), 
v);
+  TREE_CONSTANT (ctor) = 1;
+  TREE_STATIC (ctor) = 1;
+  TREE_STATIC (new_decl) = 1;
+  DECL_INITIAL (new_decl) = ctor;
+  TREE_TYPE (new_decl) = build_array_type_nelts (pointer_sized_int_node,
+                                                num);
+  DECL_SIZE (new_decl) = build_int_cst (integer_type_node,
+                                       int_size_in_bytes 
(pointer_sized_int_node)
+                                       * num * BITS_PER_UNIT);
+  DECL_SIZE_UNIT (new_decl)
+    = build_int_cst (integer_type_node,
+                    int_size_in_bytes (pointer_sized_int_node) * num);
+  DECL_SECTION_NAME (new_decl) = build_string (strlen (section_name), 
section_name);
+
+  varpool_assemble_decl ( varpool_node_for_decl (new_decl));
+}
+
 #include "gt-omp-low.h"
diff --git a/gcc/omp-low.h b/gcc/omp-low.h
index 6b5a2ff..813189d 100644
--- a/gcc/omp-low.h
+++ b/gcc/omp-low.h
@@ -27,5 +27,6 @@ extern void omp_expand_local (basic_block);
 extern void free_omp_regions (void);
 extern tree omp_reduction_init (tree, tree);
 extern bool make_gimple_omp_edges (basic_block, struct omp_region **);
+extern void omp_finish_file (void);
 
 #endif /* GCC_OMP_LOW_H */
diff --git a/gcc/toplev.c b/gcc/toplev.c
index f78912e..595705a 100644
--- a/gcc/toplev.c
+++ b/gcc/toplev.c
@@ -76,6 +76,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "diagnostic-color.h"
 #include "context.h"
 #include "pass_manager.h"
+#include "omp-low.h"
 
 #if defined(DBX_DEBUGGING_INFO) || defined(XCOFF_DEBUGGING_INFO)
 #include "dbxout.h"
@@ -574,6 +575,8 @@ compile_file (void)
       if (flag_sanitize & SANITIZE_THREAD)
        tsan_finish_file ();
 
+      omp_finish_file ();
+
       output_shared_constant_pool ();
       output_object_blocks ();
       finish_tm_clone_pairs ();
-- 
1.8.3.1

Reply via email to