Procházet zdrojové kódy

gcc: Further split source code in several files.

* gcc-plugin/src/opencl.c, gcc-plugin/src/opencl.h,
  gcc-plugin/src/tasks.c, gcc-plugin/src/tasks.h: New files.

* gcc-plugin/src/starpu.c (opencl_include_dirs,
  opencl_program_struct_tag, build_string_variable,
  build_variable_from_file_contents, opencl_program_type,
  opencl_kernel_type, opencl_command_queue_type, opencl_event_type,
  build_opencl_error_string, build_opencl_set_kernel_arg_call,
  build_opencl_set_kernel_arg_calls, define_opencl_task_implementation,
  handle_pragma_opencl, validate_opencl_argument_type): Move to
  `opencl.[ch]'.
  (task_implementation_attribute_name, codelet_struct_tag, define_task,
  task_implementation_p, task_implementation_where,
  task_implementation_task, task_implementation_target_to_int,
  declare_codelet, build_codelet_identifier, codelet_type,
  task_codelet_attribute_name, task_implementation_list_attribute_name,
  task_implementation_wrapper_attribute_name, build_codelet_declaration,
  taskify_function, add_task_implementation, task_codelet_declaration,
  task_implementation_list, task_pointer_parameter_types, task_where,
  task_implementation_wrapper, output_type_p, access_mode,
  build_codelet_declaration, build_codelet_initializer):
  Move to `tasks.[ch]'.
  (build_function_arguments, type_decl_for_struct_tag,
  LOOKUP_STARPU_FUNCTION, build_call_expr_loc_array,
  build_call_expr_loc_vec, build_zero_cst, VEC_qsort,
  builtin_decl_explicit, void_type_p, pointer_type_p,
  build_error_statements, chain_trees, filter, list_remove, map,
  for_each, count, build_pointer_lookup, build_constructor_from_list,
  build_starpu_error_string): Move to `utils.[ch]'.

* gcc-plugin/src/Makefile.am (starpu_la_SOURCES): Add `opencl.c' and
  `tasks.c'.
  (noinst_HEADERS): Add `opencl.h' and `tasks.h'.
Ludovic Courtès před 12 roky
rodič
revize
9dd5a841aa

+ 4 - 0
gcc-plugin/src/Makefile.am

@@ -19,7 +19,9 @@ gccplugin_LTLIBRARIES = starpu.la
 
 starpu_la_SOURCES =				\
   c-expr.y					\
+  opencl.c					\
   starpu.c					\
+  tasks.c					\
   utils.c
 
 if HAVE_PTR_DEREFS_MAY_ALIAS_P
@@ -30,6 +32,8 @@ starpu_la_SOURCES += warn-unregistered.c
 endif
 
 noinst_HEADERS =				\
+  opencl.h					\
+  tasks.h					\
   utils.h					\
   warn-unregistered.h
 

+ 695 - 0
gcc-plugin/src/opencl.c

@@ -0,0 +1,695 @@
+/* GCC-StarPU
+   Copyright (C) 2012 Inria
+
+   GCC-StarPU 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 of the License, or
+   (at your option) any later version.
+
+   GCC-StarPU 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 PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with GCC-StarPU.  If not, see <http://www.gnu.org/licenses/>.  */
+
+#include <starpu-gcc-config.h>
+
+#include <stdlib.h>
+#include <unistd.h>
+
+#include <starpu.h>
+
+#include <gcc-plugin.h>
+#include <plugin-version.h>
+#include <plugin.h>
+#include <tree.h>
+#include <tree-iterator.h>
+#include <gimple.h>
+#include <cgraph.h>
+#include <toplev.h>
+#include <langhooks.h>
+
+#ifdef HAVE_C_FAMILY_C_COMMON_H
+# include <c-family/c-common.h>
+#elif HAVE_C_COMMON_H
+# include <c-common.h>
+#endif
+
+#include <utils.h>
+#include <tasks.h>
+
+
+/* Search path for OpenCL source files for the `opencl' pragma, as a
+   `TREE_LIST'.  */
+tree opencl_include_dirs = NULL_TREE;
+
+/* Names of data structures defined in <starpu.h>.  */
+static const char opencl_program_struct_tag[] = "starpu_opencl_program";
+
+
+/* Return the type corresponding to OPENCL_PROGRAM_STRUCT_TAG.  */
+
+static tree
+opencl_program_type (void)
+{
+  tree t = TREE_TYPE (type_decl_for_struct_tag (opencl_program_struct_tag));
+
+  if (TYPE_SIZE (t) == NULL_TREE)
+    {
+      /* Incomplete type definition, for instance because <starpu_opencl.h>
+	 wasn't included.  */
+      error_at (UNKNOWN_LOCATION, "StarPU OpenCL support is lacking");
+      t = error_mark_node;
+    }
+
+  return t;
+}
+
+static tree
+opencl_kernel_type (void)
+{
+  tree t = lookup_name (get_identifier ("cl_kernel"));
+  gcc_assert (t != NULL_TREE);
+  if (TREE_CODE (t) == TYPE_DECL)
+    t = TREE_TYPE (t);
+  gcc_assert (TYPE_P (t));
+  return t;
+}
+
+static tree
+opencl_command_queue_type (void)
+{
+  tree t = lookup_name (get_identifier ("cl_command_queue"));
+  gcc_assert (t != NULL_TREE);
+  if (TREE_CODE (t) == TYPE_DECL)
+    t = TREE_TYPE (t);
+  gcc_assert (TYPE_P (t));
+  return t;
+}
+
+static tree
+opencl_event_type (void)
+{
+  tree t = lookup_name (get_identifier ("cl_event"));
+  gcc_assert (t != NULL_TREE);
+  if (TREE_CODE (t) == TYPE_DECL)
+    t = TREE_TYPE (t);
+  gcc_assert (TYPE_P (t));
+  return t;
+}
+
+
+
+/* Return a private global string literal VAR_DECL, whose contents are the
+   LEN bytes at CONTENTS.  */
+
+static tree
+build_string_variable (location_t loc, const char *name_seed,
+		       const char *contents, size_t len)
+{
+  tree decl;
+
+  decl = build_decl (loc, VAR_DECL, create_tmp_var_name (name_seed),
+		     string_type_node);
+  TREE_PUBLIC (decl) = false;
+  TREE_STATIC (decl) = true;
+  TREE_USED (decl) = true;
+
+  DECL_INITIAL (decl) =				  /* XXX: off-by-one? */
+    build_string_literal (len + 1, contents);
+
+  DECL_ARTIFICIAL (decl) = true;
+
+  return decl;
+}
+
+/* Return a VAR_DECL for a string variable containing the contents of FILE,
+   which is looked for in each of the directories listed in SEARCH_PATH.  If
+   FILE could not be found, return NULL_TREE.  */
+
+static tree
+build_variable_from_file_contents (location_t loc,
+				   const char *name_seed,
+				   const char *file,
+				   const_tree search_path)
+{
+  gcc_assert (search_path != NULL_TREE
+	      && TREE_CODE (search_path) == TREE_LIST);
+
+  int err, dir_fd;
+  struct stat st;
+  const_tree dirs;
+  tree var = NULL_TREE;
+
+  /* Look for FILE in each directory in SEARCH_PATH, and pick the first one
+     that matches.  */
+  for (err = ENOENT, dir_fd = -1, dirs = search_path;
+       (err != 0 || err == ENOENT) && dirs != NULL_TREE;
+       dirs = TREE_CHAIN (dirs))
+    {
+      gcc_assert (TREE_VALUE (dirs) != NULL_TREE
+		  && TREE_CODE (TREE_VALUE (dirs)) == STRING_CST);
+
+      dir_fd = open (TREE_STRING_POINTER (TREE_VALUE (dirs)),
+		     O_DIRECTORY | O_RDONLY);
+      if (dir_fd < 0)
+	err = ENOENT;
+      else
+	{
+	  err = fstatat (dir_fd, file, &st, 0);
+	  if (err != 0)
+	    close (dir_fd);
+	  else
+	    /* Leave DIRS unchanged so it can be referred to in diagnostics
+	       below.  */
+	    break;
+	}
+    }
+
+  if (err != 0 || dir_fd < 0)
+    error_at (loc, "failed to access %qs: %m", file);
+  else if (st.st_size == 0)
+    {
+      error_at (loc, "source file %qs is empty", file);
+      close (dir_fd);
+    }
+  else
+    {
+      if (verbose_output_p)
+	inform (loc, "found file %qs in %qs",
+		file, TREE_STRING_POINTER (TREE_VALUE (dirs)));
+
+      int fd;
+
+      fd = openat (dir_fd, file, O_RDONLY);
+      close (dir_fd);
+
+      if (fd < 0)
+	error_at (loc, "failed to open %qs: %m", file);
+      else
+	{
+	  void *contents;
+
+	  contents = mmap (NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
+	  if (contents == NULL)
+	    error_at (loc, "failed to map contents of %qs: %m", file);
+	  else
+	    {
+	      var = build_string_variable (loc, name_seed,
+					   (char *) contents, st.st_size);
+	      pushdecl (var);
+	      munmap (contents, st.st_size);
+	    }
+
+	  close (fd);
+	}
+    }
+
+  return var;
+}
+
+/* Return an expression that, given the OpenCL error code in ERROR_VAR,
+   returns a string.  */
+
+static tree
+build_opencl_error_string (tree error_var)
+{
+  static tree clstrerror_fn;
+  LOOKUP_STARPU_FUNCTION (clstrerror_fn, "starpu_opencl_error_string");
+
+  return build_call_expr (clstrerror_fn, 1, error_var);
+}
+
+/* Return an error-checking `clSetKernelArg' call for argument ARG, at
+   index IDX, of KERNEL.  */
+
+static tree
+build_opencl_set_kernel_arg_call (location_t loc, tree fn,
+				  tree kernel, unsigned int idx,
+				  tree arg)
+{
+  gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
+	      && TREE_TYPE (kernel) == opencl_kernel_type ());
+
+  static tree setkernarg_fn;
+  LOOKUP_STARPU_FUNCTION (setkernarg_fn, "clSetKernelArg");
+
+  tree call = build_call_expr (setkernarg_fn, 4, kernel,
+			       build_int_cst (integer_type_node, idx),
+			       size_in_bytes (TREE_TYPE (arg)),
+			       build_addr (arg, fn));
+  tree error_var = build_decl (loc, VAR_DECL,
+			       create_tmp_var_name ("setkernelarg_error"),
+			       integer_type_node);
+  DECL_ARTIFICIAL (error_var) = true;
+  DECL_CONTEXT (error_var) = fn;
+
+  tree assignment = build2 (INIT_EXPR, TREE_TYPE (error_var),
+			    error_var, call);
+
+  /* Build `if (ERROR_VAR != 0) error ();'.  */
+  tree cond;
+  cond = build3 (COND_EXPR, void_type_node,
+		 build2 (NE_EXPR, boolean_type_node,
+			 error_var, integer_zero_node),
+		 build_error_statements (loc, error_var,
+					 build_opencl_error_string,
+					 "failed to set OpenCL kernel "
+					 "argument %d", idx),
+		 NULL_TREE);
+
+  tree stmts = NULL_TREE;
+  append_to_statement_list (assignment, &stmts);
+  append_to_statement_list (cond, &stmts);
+
+  return build4 (TARGET_EXPR, void_type_node, error_var,
+		 stmts, NULL_TREE, NULL_TREE);
+}
+
+/* Return the sequence of `clSetKernelArg' calls for KERNEL.  */
+
+static tree
+build_opencl_set_kernel_arg_calls (location_t loc, tree task_impl,
+				   tree kernel)
+{
+  gcc_assert (task_implementation_p (task_impl));
+
+  size_t n;
+  tree arg, stmts = NULL_TREE;
+
+  for (arg = DECL_ARGUMENTS (task_impl), n = 0;
+       arg != NULL_TREE;
+       arg = TREE_CHAIN (arg), n++)
+    {
+      tree call = build_opencl_set_kernel_arg_call (loc, task_impl,
+						    kernel, n, arg);
+      append_to_statement_list (call, &stmts);
+    }
+
+  return stmts;
+}
+
+/* Define a body for TASK_IMPL that loads OpenCL source from FILE and calls
+   KERNEL.  */
+
+static void
+define_opencl_task_implementation (location_t loc, tree task_impl,
+				   const char *file, const_tree kernel,
+				   tree groupsize)
+{
+  gcc_assert (task_implementation_p (task_impl)
+	      && task_implementation_where (task_impl) == STARPU_OPENCL);
+  gcc_assert (TREE_CODE (kernel) == STRING_CST);
+  gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (groupsize)));
+
+  local_define (tree, local_var, (tree type))
+  {
+    tree var = build_decl (loc, VAR_DECL,
+			   create_tmp_var_name ("opencl_var"),
+			   type);
+    DECL_ARTIFICIAL (var) = true;
+    DECL_CONTEXT (var) = task_impl;
+    return var;
+  };
+
+  if (!verbose_output_p)
+    /* No further warnings for this node.  */
+    TREE_NO_WARNING (task_impl) = true;
+
+  static tree load_fn, load_kern_fn, enqueue_kern_fn, wid_fn, devid_fn, clfinish_fn,
+    collect_stats_fn, release_ev_fn;
+
+  if (load_fn == NULL_TREE)
+    {
+      load_fn =
+	lookup_name (get_identifier ("starpu_opencl_load_opencl_from_string"));
+      if (load_fn == NULL_TREE)
+	{
+	  inform (loc, "no OpenCL support, task implementation %qE "
+		  "not generated", DECL_NAME (task_impl));
+	  return;
+	}
+    }
+
+  LOOKUP_STARPU_FUNCTION (load_kern_fn, "starpu_opencl_load_kernel");
+  LOOKUP_STARPU_FUNCTION (wid_fn, "starpu_worker_get_id");
+  LOOKUP_STARPU_FUNCTION (devid_fn, "starpu_worker_get_devid");
+  LOOKUP_STARPU_FUNCTION (enqueue_kern_fn, "clEnqueueNDRangeKernel");
+  LOOKUP_STARPU_FUNCTION (clfinish_fn, "clFinish");
+  LOOKUP_STARPU_FUNCTION (collect_stats_fn, "starpu_opencl_collect_stats");
+  LOOKUP_STARPU_FUNCTION (release_ev_fn, "clReleaseEvent");
+
+  if (verbose_output_p)
+    inform (loc, "defining %qE, with OpenCL kernel %qs from file %qs",
+	    DECL_NAME (task_impl), TREE_STRING_POINTER (kernel), file);
+
+  tree source_var;
+  source_var = build_variable_from_file_contents (loc, "opencl_source",
+						  file, opencl_include_dirs);
+  if (source_var != NULL_TREE)
+    {
+      /* Give TASK_IMPL an actual argument list.  */
+      DECL_ARGUMENTS (task_impl) = build_function_arguments (task_impl);
+
+      tree prog_var, prog_loaded_var;
+
+      /* Global variable to hold the `starpu_opencl_program' object.  */
+
+      prog_var = build_decl (loc, VAR_DECL,
+			     create_tmp_var_name ("opencl_program"),
+			     opencl_program_type ());
+      TREE_PUBLIC (prog_var) = false;
+      TREE_STATIC (prog_var) = true;
+      TREE_USED (prog_var) = true;
+      DECL_ARTIFICIAL (prog_var) = true;
+      pushdecl (prog_var);
+
+      /* Global variable indicating whether the program has already been
+	 loaded.  */
+
+      prog_loaded_var = build_decl (loc, VAR_DECL,
+				    create_tmp_var_name ("opencl_prog_loaded"),
+				    boolean_type_node);
+      TREE_PUBLIC (prog_loaded_var) = false;
+      TREE_STATIC (prog_loaded_var) = true;
+      TREE_USED (prog_loaded_var) = true;
+      DECL_ARTIFICIAL (prog_loaded_var) = true;
+      DECL_INITIAL (prog_loaded_var) = build_zero_cst (boolean_type_node);
+      pushdecl (prog_loaded_var);
+
+      /* Build `starpu_opencl_load_opencl_from_string (SOURCE_VAR,
+	                                               &PROG_VAR, "")'.  */
+      tree load = build_call_expr (load_fn, 3, source_var,
+				   build_addr (prog_var, task_impl),
+				   build_string_literal (1, ""));
+
+      tree load_stmts = NULL_TREE;
+      append_to_statement_list (load, &load_stmts);
+      append_to_statement_list (build2 (MODIFY_EXPR, boolean_type_node,
+					prog_loaded_var,
+					build_int_cst (boolean_type_node, 1)),
+				&load_stmts);
+
+      /* Build `if (!PROG_LOADED_VAR) { ...; PROG_LOADED_VAR = true; }'.  */
+
+      tree load_cond = build3 (COND_EXPR, void_type_node,
+			       prog_loaded_var,
+			       NULL_TREE,
+			       load_stmts);
+
+      /* Local variables.  */
+      tree kernel_var, queue_var, event_var, group_size_var, ngroups_var,
+	error_var;
+
+      kernel_var = local_var (opencl_kernel_type ());
+      queue_var = local_var (opencl_command_queue_type ());
+      event_var = local_var (opencl_event_type ());
+      group_size_var = local_var (size_type_node);
+      ngroups_var = local_var (size_type_node);
+      error_var = local_var (integer_type_node);
+
+      /* Build `starpu_opencl_load_kernel (...)'.
+         TODO: Check return value.  */
+      tree devid =
+	build_call_expr (devid_fn, 1, build_call_expr (wid_fn, 0));
+
+      tree load_kern = build_call_expr (load_kern_fn, 5,
+					build_addr (kernel_var, task_impl),
+					build_addr (queue_var, task_impl),
+					build_addr (prog_var, task_impl),
+					build_string_literal
+					(TREE_STRING_LENGTH (kernel) + 1,
+					 TREE_STRING_POINTER (kernel)),
+					devid);
+
+      tree enqueue_kern =
+	build_call_expr (enqueue_kern_fn, 9,
+			 queue_var, kernel_var,
+			 build_int_cst (integer_type_node, 1),
+			 null_pointer_node,
+			 build_addr (group_size_var, task_impl),
+			 build_addr (ngroups_var, task_impl),
+			 integer_zero_node,
+			 null_pointer_node,
+			 build_addr (event_var, task_impl));
+      tree enqueue_err =
+	build2 (INIT_EXPR, TREE_TYPE (error_var), error_var, enqueue_kern);
+
+      tree enqueue_cond =
+	build3 (COND_EXPR, void_type_node,
+		build2 (NE_EXPR, boolean_type_node,
+			error_var, integer_zero_node),
+		build_error_statements (loc, error_var,
+					build_opencl_error_string,
+					"failed to enqueue kernel"),
+		NULL_TREE);
+
+      tree clfinish =
+	build_call_expr (clfinish_fn, 1, queue_var);
+
+      tree collect_stats =
+	build_call_expr (collect_stats_fn, 1, event_var);
+
+      tree release_ev =
+	build_call_expr (release_ev_fn, 1, event_var);
+
+      tree enqueue_stmts = NULL_TREE;
+      append_to_statement_list (enqueue_err, &enqueue_stmts);
+      append_to_statement_list (enqueue_cond, &enqueue_stmts);
+
+
+      /* TODO: Build `clFinish', `clReleaseEvent', & co.  */
+      /* Put it all together.  */
+      tree stmts = NULL_TREE;
+      append_to_statement_list (load_cond, &stmts);
+      append_to_statement_list (load_kern, &stmts);
+      append_to_statement_list (build_opencl_set_kernel_arg_calls (loc,
+								   task_impl,
+								   kernel_var),
+				&stmts);
+
+      /* TODO: Support user-provided values.  */
+      append_to_statement_list (build2 (INIT_EXPR, TREE_TYPE (group_size_var),
+					group_size_var,
+					fold_convert (TREE_TYPE (group_size_var),
+						      groupsize)),
+				&stmts);
+      append_to_statement_list (build2 (INIT_EXPR, TREE_TYPE (ngroups_var),
+					ngroups_var,
+					build_int_cst (TREE_TYPE (ngroups_var),
+						       1)),
+				&stmts);
+      append_to_statement_list (build4 (TARGET_EXPR, void_type_node,
+					error_var, enqueue_stmts,
+					NULL_TREE, NULL_TREE),
+				&stmts);
+      append_to_statement_list (clfinish, &stmts);
+      append_to_statement_list (collect_stats, &stmts);
+      append_to_statement_list (release_ev, &stmts);
+
+      /* Bind the local vars.  */
+      tree vars = chain_trees (kernel_var, queue_var, event_var,
+			       group_size_var, ngroups_var, NULL_TREE);
+      tree bind = build3 (BIND_EXPR, void_type_node, vars, stmts,
+			  build_block (vars, NULL_TREE, task_impl, NULL_TREE));
+
+      TREE_USED (task_impl) = true;
+      TREE_STATIC (task_impl) = true;
+      DECL_EXTERNAL (task_impl) = false;
+      DECL_ARTIFICIAL (task_impl) = true;
+      DECL_SAVED_TREE (task_impl) = bind;
+      DECL_INITIAL (task_impl) = BIND_EXPR_BLOCK (bind);
+      DECL_RESULT (task_impl) =
+	build_decl (loc, RESULT_DECL, NULL_TREE, void_type_node);
+
+      /* Compile TASK_IMPL.  */
+      rest_of_decl_compilation (task_impl, true, 0);
+      allocate_struct_function (task_impl, false);
+      cgraph_finalize_function (task_impl, false);
+      cgraph_mark_needed_node (cgraph_get_node (task_impl));
+
+      /* Generate a wrapper for TASK_IMPL, and possibly the body of its task.
+	 This needs to be done explicitly here, because otherwise
+	 `handle_pre_genericize' would never see TASK_IMPL's task.  */
+      tree task = task_implementation_task (task_impl);
+      if (!TREE_STATIC (task))
+	{
+	  declare_codelet (task);
+	  define_task (task);
+
+	  /* Compile TASK's body.  */
+	  rest_of_decl_compilation (task, true, 0);
+	  allocate_struct_function (task, false);
+	  cgraph_finalize_function (task, false);
+	  cgraph_mark_needed_node (cgraph_get_node (task));
+	}
+    }
+  else
+    DECL_SAVED_TREE (task_impl) = error_mark_node;
+
+  return;
+}
+
+/* Handle the `opencl' pragma, which defines an OpenCL task
+   implementation.  */
+
+void
+handle_pragma_opencl (struct cpp_reader *reader)
+{
+  tree args;
+  location_t loc;
+
+  loc = cpp_peek_token (reader, 0)->src_loc;
+
+  if (current_function_decl != NULL_TREE)
+    {
+      error_at (loc, "%<starpu opencl%> pragma can only be used "
+		"at the top-level");
+      return;
+    }
+
+  args = read_pragma_expressions ("opencl", loc);
+  if (args == NULL_TREE)
+    return;
+
+  /* TODO: Add "number of groups" arguments.  */
+  if (list_length (args) < 4)
+    {
+      error_at (loc, "wrong number of arguments for %<starpu opencl%> pragma");
+      return;
+    }
+
+  if (task_implementation_p (TREE_VALUE (args)))
+    {
+      tree task_impl = TREE_VALUE (args);
+      if (task_implementation_where (task_impl) == STARPU_OPENCL)
+  	{
+  	  args = TREE_CHAIN (args);
+  	  if (TREE_CODE (TREE_VALUE (args)) == STRING_CST)
+  	    {
+  	      tree file = TREE_VALUE (args);
+  	      args = TREE_CHAIN (args);
+  	      if (TREE_CODE (TREE_VALUE (args)) == STRING_CST)
+  		{
+  		  tree kernel = TREE_VALUE (args);
+		  args = TREE_CHAIN (args);
+
+		  if (TREE_TYPE (TREE_VALUE (args)) != NULL_TREE &&
+		      INTEGRAL_TYPE_P (TREE_TYPE (TREE_VALUE (args))))
+		    {
+		      tree groupsize = TREE_VALUE (args);
+		      if (TREE_CHAIN (args) == NULL_TREE)
+			define_opencl_task_implementation (loc, task_impl,
+							   TREE_STRING_POINTER (file),
+							   kernel, groupsize);
+		      else
+			error_at (loc, "junk after %<starpu opencl%> pragma");
+		    }
+		  else
+		    error_at (loc, "%<groupsize%> argument must be an integral type");
+  		}
+  	      else
+  		error_at (loc, "%<kernel%> argument must be a string constant");
+	    }
+	  else
+	    error_at (loc, "%<file%> argument must be a string constant");
+	}
+      else
+	error_at (loc, "%qE is not an OpenCL task implementation",
+		  DECL_NAME (task_impl));
+    }
+  else
+    error_at (loc, "%qE is not a task implementation", TREE_VALUE (args));
+}
+
+/* Diagnose use of C types that are either nonexistent or different in
+   OpenCL.  */
+
+void
+validate_opencl_argument_type (location_t loc, const_tree type)
+{
+  /* When TYPE is a pointer type, get to the base element type.  */
+  for (; POINTER_TYPE_P (type); type = TREE_TYPE (type));
+
+  if (!RECORD_OR_UNION_TYPE_P (type) && !VOID_TYPE_P (type))
+    {
+      tree decl = TYPE_NAME (type);
+
+      if (DECL_P (decl))
+	{
+	  static const struct { const char *c; const char *cl; }
+	  type_map[] =
+	    {
+	      /* Scalar types defined in OpenCL 1.2.  See
+		 <http://www.khronos.org/files/opencl-1-2-quick-reference-card.pdf>.  */
+	      { "char", "cl_char" },
+	      { "signed char", "cl_char" },
+	      { "unsigned char", "cl_uchar" },
+	      { "uchar", "cl_uchar" },
+	      { "short int", "cl_short" },
+	      { "unsigned short", "cl_ushort" },
+	      { "int", "cl_int" },
+	      { "unsigned int", "cl_uint" },
+	      { "uint", "cl_uint" },
+	      { "long int", "cl_long" },
+	      { "long unsigned int", "cl_ulong" },
+	      { "ulong", "cl_ulong" },
+	      { "float", "cl_float" },
+	      { "double", "cl_double" },
+	      { NULL, NULL }
+	    };
+
+	  const char *c_name = IDENTIFIER_POINTER (DECL_NAME (decl));
+	  const char *cl_name =
+	    ({
+	      size_t i;
+	      for (i = 0; type_map[i].c != NULL; i++)
+		{
+		  if (strcmp (type_map[i].c, c_name) == 0)
+		    break;
+		}
+	      type_map[i].cl;
+	    });
+
+	  if (cl_name != NULL)
+	    {
+	      tree cl_type = lookup_name (get_identifier (cl_name));
+
+	      if (cl_type != NULL_TREE)
+		{
+		  if (DECL_P (cl_type))
+		    cl_type = TREE_TYPE (cl_type);
+
+		  if (!lang_hooks.types_compatible_p ((tree) type, cl_type))
+		    {
+		      tree st, sclt;
+
+		      st = c_common_signed_type ((tree) type);
+		      sclt = c_common_signed_type (cl_type);
+
+		      if (st == sclt)
+			warning_at (loc, 0, "C type %qE differs in signedness "
+				    "from the same-named OpenCL type",
+				    DECL_NAME (decl));
+		      else
+			/* TYPE should be avoided because the it differs from
+			   CL_TYPE, and thus cannot be used safely in
+			   `clSetKernelArg'.  */
+			warning_at (loc, 0, "C type %qE differs from the "
+				    "same-named OpenCL type",
+				    DECL_NAME (decl));
+		    }
+		}
+
+	      /* Otherwise we can't conclude.  It could be that <CL/cl.h>
+		 wasn't included in the program, for instance.  */
+	    }
+	  else
+	    /* Recommend against use of `size_t', etc.  */
+	    warning_at (loc, 0, "%qE does not correspond to a known "
+			"OpenCL type", DECL_NAME (decl));
+	}
+    }
+}

+ 28 - 0
gcc-plugin/src/opencl.h

@@ -0,0 +1,28 @@
+/* GCC-StarPU
+   Copyright (C) 2012 Inria
+
+   GCC-StarPU 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 of the License, or
+   (at your option) any later version.
+
+   GCC-StarPU 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 PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with GCC-StarPU.  If not, see <http://www.gnu.org/licenses/>.  */
+
+#pragma once
+
+#include <gcc-plugin.h>
+#include <tree.h>
+#include <cpplib.h>
+
+#include <utils.h>
+
+extern tree opencl_include_dirs;
+
+extern void handle_pragma_opencl (struct cpp_reader *reader);
+extern void validate_opencl_argument_type (location_t loc, const_tree type);

Rozdílová data souboru nebyla zobrazena, protože soubor je příliš velký
+ 75 - 1753
gcc-plugin/src/starpu.c


+ 669 - 0
gcc-plugin/src/tasks.c

@@ -0,0 +1,669 @@
+/* GCC-StarPU
+   Copyright (C) 2012 Inria
+
+   GCC-StarPU 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 of the License, or
+   (at your option) any later version.
+
+   GCC-StarPU 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 PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with GCC-StarPU.  If not, see <http://www.gnu.org/licenses/>.  */
+
+#include <starpu-gcc-config.h>
+
+#include <gcc-plugin.h>
+#include <plugin-version.h>
+
+#include <plugin.h>
+#include <cpplib.h>
+#include <tree.h>
+#include <tree-iterator.h>
+#include <gimple.h>
+
+#ifdef HAVE_C_FAMILY_C_COMMON_H
+# include <c-family/c-common.h>
+#elif HAVE_C_COMMON_H
+# include <c-common.h>
+#endif
+
+#include <tasks.h>
+#include <utils.h>
+#include <opencl.h>
+#include <starpu.h>
+
+
+/* Task-related functions.  */
+
+/* Name of public attributes.  */
+const char task_attribute_name[] = "task";
+const char task_implementation_attribute_name[] = "task_implementation";
+const char output_attribute_name[] = "output";
+
+/* Names of attributes used internally.  */
+static const char task_codelet_attribute_name[] = ".codelet";
+const char task_implementation_list_attribute_name[] =
+  ".task_implementation_list";
+const char task_implementation_wrapper_attribute_name[] =
+  ".task_implementation_wrapper";
+
+/* Names of data structures defined in <starpu.h>.  */
+static const char codelet_struct_tag[] = "starpu_codelet";
+
+
+/* Return true if DECL is a task.  */
+
+bool
+task_p (const_tree decl)
+{
+  return (TREE_CODE (decl) == FUNCTION_DECL &&
+	  lookup_attribute (task_attribute_name,
+			    DECL_ATTRIBUTES (decl)) != NULL_TREE);
+}
+
+/* Return true if DECL is a task implementation.  */
+
+bool
+task_implementation_p (const_tree decl)
+{
+  return (TREE_CODE (decl) == FUNCTION_DECL &&
+	  lookup_attribute (task_implementation_attribute_name,
+			    DECL_ATTRIBUTES (decl)) != NULL_TREE);
+}
+
+/* Return a value indicating where TASK_IMPL should execute (`STARPU_CPU',
+   `STARPU_CUDA', etc.).  */
+
+int
+task_implementation_where (const_tree task_impl)
+{
+  tree impl_attr, args, where;
+
+  gcc_assert (TREE_CODE (task_impl) == FUNCTION_DECL);
+
+  impl_attr = lookup_attribute (task_implementation_attribute_name,
+				DECL_ATTRIBUTES (task_impl));
+  gcc_assert (impl_attr != NULL_TREE);
+
+  args = TREE_VALUE (impl_attr);
+  where = TREE_VALUE (args);
+
+  return task_implementation_target_to_int (where);
+}
+
+/* Return the StarPU integer constant corresponding to string TARGET.  */
+
+int
+task_implementation_target_to_int (const_tree target)
+{
+  gcc_assert (TREE_CODE (target) == STRING_CST);
+
+  int where_int;
+
+  if (!strncmp (TREE_STRING_POINTER (target), "cpu",
+		TREE_STRING_LENGTH (target)))
+    where_int = STARPU_CPU;
+  else if (!strncmp (TREE_STRING_POINTER (target), "opencl",
+		     TREE_STRING_LENGTH (target)))
+    where_int = STARPU_OPENCL;
+  else if (!strncmp (TREE_STRING_POINTER (target), "cuda",
+		     TREE_STRING_LENGTH (target)))
+    where_int = STARPU_CUDA;
+  else if (!strncmp (TREE_STRING_POINTER (target), "gordon",
+		     TREE_STRING_LENGTH (target)))
+    where_int = STARPU_GORDON;
+  else
+    where_int = 0;
+
+  return where_int;
+}
+
+/* Return the task implemented by TASK_IMPL.  */
+
+tree
+task_implementation_task (const_tree task_impl)
+{
+  tree impl_attr, args, task;
+
+  gcc_assert (TREE_CODE (task_impl) == FUNCTION_DECL);
+
+  impl_attr = lookup_attribute (task_implementation_attribute_name,
+				DECL_ATTRIBUTES (task_impl));
+  gcc_assert (impl_attr != NULL_TREE);
+
+  args = TREE_VALUE (impl_attr);
+
+  task = TREE_VALUE (TREE_CHAIN (args));
+  if (task_implementation_p (task))
+    /* TASK is an implicit CPU task implementation, so return its real
+       task.  */
+    return task_implementation_task (task);
+
+  return task;
+}
+
+/* Return the declaration of the `struct starpu_codelet' variable associated with
+   TASK_DECL.  */
+
+tree
+task_codelet_declaration (const_tree task_decl)
+{
+  tree cl_attr;
+
+  cl_attr = lookup_attribute (task_codelet_attribute_name,
+			      DECL_ATTRIBUTES (task_decl));
+  gcc_assert (cl_attr != NULL_TREE);
+
+  return TREE_VALUE (cl_attr);
+}
+
+/* Return the list of implementations of TASK_DECL.  */
+
+tree
+task_implementation_list (const_tree task_decl)
+{
+  tree attr;
+
+  attr = lookup_attribute (task_implementation_list_attribute_name,
+			   DECL_ATTRIBUTES (task_decl));
+  return TREE_VALUE (attr);
+}
+
+/* Return the list of pointer parameter types of TASK_DECL.  */
+
+tree
+task_pointer_parameter_types (const_tree task_decl)
+{
+  return filter (pointer_type_p, TYPE_ARG_TYPES (TREE_TYPE (task_decl)));
+}
+
+/* Return a bitwise-or of the supported targets of TASK_DECL.  */
+
+int
+task_where (const_tree task_decl)
+{
+  gcc_assert (task_p (task_decl));
+
+  int where;
+  const_tree impl;
+
+  for (impl = task_implementation_list (task_decl), where = 0;
+       impl != NULL_TREE;
+       impl = TREE_CHAIN (impl))
+    where |= task_implementation_where (TREE_VALUE (impl));
+
+  return where;
+}
+
+/* Return the FUNCTION_DECL of the wrapper generated for TASK_IMPL.  */
+
+tree
+task_implementation_wrapper (const_tree task_impl)
+{
+  tree attr;
+
+  gcc_assert (TREE_CODE (task_impl) == FUNCTION_DECL);
+
+  attr = lookup_attribute (task_implementation_wrapper_attribute_name,
+			   DECL_ATTRIBUTES (task_impl));
+  gcc_assert (attr != NULL_TREE);
+
+  return TREE_VALUE (attr);
+}
+
+tree
+codelet_type (void)
+{
+  /* XXX: Hack to allow the type declaration to be accessible at lower
+     time.  */
+  static tree type_decl = NULL_TREE;
+
+  if (type_decl == NULL_TREE)
+    /* Lookup the `struct starpu_codelet' struct type.  This should succeed since
+       we push <starpu.h> early on.  */
+    type_decl = type_decl_for_struct_tag (codelet_struct_tag);
+
+  return TREE_TYPE (type_decl);
+}
+
+/* Return the access mode for POINTER, a PARM_DECL of a task.  */
+
+enum starpu_access_mode
+access_mode (const_tree type)
+{
+  gcc_assert (POINTER_TYPE_P (type));
+
+  /* If TYPE points to a const-qualified type, then mark the data as
+     read-only; if is has the `output' attribute, then mark it as write-only;
+     otherwise default to read-write.  */
+  return ((TYPE_QUALS (TREE_TYPE (type)) & TYPE_QUAL_CONST)
+	  ? STARPU_R
+	  : (output_type_p (type) ? STARPU_W : STARPU_RW));
+}
+
+/* Return true if TYPE is `output'-qualified.  */
+
+bool
+output_type_p (const_tree type)
+{
+  return (lookup_attribute (output_attribute_name,
+			    TYPE_ATTRIBUTES (type)) != NULL_TREE);
+}
+
+
+/* Code generation.  */
+
+/* Turn FN into a task, and push its associated codelet declaration.  */
+
+void
+taskify_function (tree fn)
+{
+  gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
+
+  /* Add a `task' attribute and an empty `task_implementation_list'
+     attribute.  */
+  DECL_ATTRIBUTES (fn) =
+    tree_cons (get_identifier (task_implementation_list_attribute_name),
+	       NULL_TREE,
+	       tree_cons (get_identifier (task_attribute_name), NULL_TREE,
+			  DECL_ATTRIBUTES (fn)));
+
+  /* Push a declaration for the corresponding `struct starpu_codelet' object and
+     add it as an attribute of FN.  */
+  tree cl = build_codelet_declaration (fn);
+  DECL_ATTRIBUTES (fn) =
+    tree_cons (get_identifier (task_codelet_attribute_name), cl,
+	       DECL_ATTRIBUTES (fn));
+
+  pushdecl (cl);
+}
+
+
+/* Return a NODE_IDENTIFIER for the variable holding the `struct starpu_codelet'
+   structure associated with TASK_DECL.  */
+
+tree
+build_codelet_identifier (tree task_decl)
+{
+  static const char suffix[] = ".codelet";
+
+  tree id;
+  char *cl_name;
+  const char *task_name;
+
+  id = DECL_NAME (task_decl);
+  task_name = IDENTIFIER_POINTER (id);
+
+  cl_name = (char *) alloca (IDENTIFIER_LENGTH (id) + strlen (suffix) + 1);
+  memcpy (cl_name, task_name, IDENTIFIER_LENGTH (id));
+  strcpy (&cl_name[IDENTIFIER_LENGTH (id)], suffix);
+
+  return get_identifier (cl_name);
+}
+
+/* Return a VAR_DECL that declares a `struct starpu_codelet' structure for
+   TASK_DECL.  */
+
+tree
+build_codelet_declaration (tree task_decl)
+{
+  tree name, cl_decl;
+
+  name = build_codelet_identifier (task_decl);
+
+  cl_decl = build_decl (DECL_SOURCE_LOCATION (task_decl),
+			VAR_DECL, name,
+			/* c_build_qualified_type (type, TYPE_QUAL_CONST) */
+			codelet_type ());
+
+  DECL_ARTIFICIAL (cl_decl) = true;
+  TREE_PUBLIC (cl_decl) = TREE_PUBLIC (task_decl);
+  TREE_STATIC (cl_decl) = false;
+  TREE_USED (cl_decl) = true;
+  DECL_EXTERNAL (cl_decl) = true;
+  DECL_CONTEXT (cl_decl) = NULL_TREE;
+
+  return cl_decl;
+}
+
+/* Return a `struct starpu_codelet' initializer for TASK_DECL.  */
+
+tree
+build_codelet_initializer (tree task_decl)
+{
+  tree fields;
+
+  fields = TYPE_FIELDS (codelet_type ());
+  gcc_assert (TREE_CODE (fields) == FIELD_DECL);
+
+  local_define (tree, lookup_field, (const char *name))
+  {
+    tree fdecl, fname;
+
+    fname = get_identifier (name);
+    for (fdecl = fields;
+	 fdecl != NULL_TREE;
+	 fdecl = TREE_CHAIN (fdecl))
+      {
+	if (DECL_NAME (fdecl) == fname)
+	  return fdecl;
+      }
+
+    /* Field NAME wasn't found.  */
+    gcc_assert (false);
+  };
+
+  local_define (tree, field_initializer, (const char *name, tree value))
+  {
+    tree field, init;
+
+    field = lookup_field (name);
+    init = make_node (TREE_LIST);
+    TREE_PURPOSE (init) = field;
+    TREE_CHAIN (init) = NULL_TREE;
+
+    if (TREE_CODE (TREE_TYPE (value)) != ARRAY_TYPE)
+      TREE_VALUE (init) = fold_convert (TREE_TYPE (field), value);
+    else
+      TREE_VALUE (init) = value;
+
+    return init;
+  };
+
+  local_define (tree, codelet_name, ())
+  {
+    const char *name = IDENTIFIER_POINTER (DECL_NAME (task_decl));
+    return build_string_literal (strlen (name) + 1, name);
+  };
+
+  local_define (tree, where_init, (tree impls))
+  {
+    tree impl;
+    int where_int = 0;
+
+    for (impl = impls;
+	 impl != NULL_TREE;
+	 impl = TREE_CHAIN (impl))
+      {
+	tree impl_decl;
+
+	impl_decl = TREE_VALUE (impl);
+	gcc_assert (TREE_CODE (impl_decl) == FUNCTION_DECL);
+
+	if (verbose_output_p)
+	  /* List the implementations of TASK_DECL.  */
+	  inform (DECL_SOURCE_LOCATION (impl_decl),
+		  "   %qE", DECL_NAME (impl_decl));
+
+	where_int |= task_implementation_where (impl_decl);
+      }
+
+    return build_int_cstu (integer_type_node, where_int);
+  };
+
+  local_define (tree, implementation_pointers, (tree impls, int where))
+  {
+    size_t len;
+    tree impl, pointers;
+
+    for (impl = impls, pointers = NULL_TREE, len = 0;
+	 impl != NULL_TREE;
+	 impl = TREE_CHAIN (impl))
+      {
+	tree impl_decl;
+
+	impl_decl = TREE_VALUE (impl);
+	if (task_implementation_where (impl_decl) == where)
+	  {
+	    /* Return a pointer to the wrapper of IMPL_DECL.  */
+	    tree addr = build_addr (task_implementation_wrapper (impl_decl),
+				    NULL_TREE);
+	    pointers = tree_cons (size_int (len), addr, pointers);
+	    len++;
+
+	    if (len > STARPU_MAXIMPLEMENTATIONS)
+	      error_at (DECL_SOURCE_LOCATION (impl_decl),
+			"maximum number of per-target task implementations "
+			"exceeded");
+	  }
+      }
+
+    /* POINTERS must be null-terminated.  */
+    pointers = tree_cons (size_int (len), build_zero_cst (ptr_type_node),
+			  pointers);
+    len++;
+
+    /* Return an array initializer.  */
+    tree index_type = build_index_type (size_int (list_length (pointers)));
+
+    return build_constructor_from_list (build_array_type (ptr_type_node,
+							  index_type),
+					nreverse (pointers));
+  };
+
+  local_define (tree, pointer_arg_count, (void))
+  {
+    size_t len;
+
+    len = list_length (task_pointer_parameter_types (task_decl));
+    return build_int_cstu (integer_type_node, len);
+  };
+
+  local_define (tree, access_mode_array, (void))
+  {
+    const_tree type;
+    tree modes;
+    size_t index;
+
+    for (type = task_pointer_parameter_types (task_decl),
+	   modes = NULL_TREE, index = 0;
+	 type != NULL_TREE && index < STARPU_NMAXBUFS;
+	 type = TREE_CHAIN (type), index++)
+      {
+	tree value = build_int_cst (integer_type_node,
+				    access_mode (TREE_VALUE (type)));
+
+	modes = tree_cons (size_int (index), value, modes);
+      }
+
+    tree index_type = build_index_type (size_int (list_length (modes)));
+
+    return build_constructor_from_list (build_array_type (integer_type_node,
+							  index_type),
+					nreverse (modes));
+  };
+
+  if (verbose_output_p)
+    inform (DECL_SOURCE_LOCATION (task_decl),
+	    "implementations for task %qE:", DECL_NAME (task_decl));
+
+  tree impls, inits;
+
+  impls = task_implementation_list (task_decl);
+
+  inits =
+    chain_trees (field_initializer ("name", codelet_name ()),
+		 field_initializer ("where", where_init (impls)),
+		 field_initializer ("nbuffers", pointer_arg_count ()),
+		 field_initializer ("modes", access_mode_array ()),
+		 field_initializer ("cpu_funcs",
+				    implementation_pointers (impls,
+							     STARPU_CPU)),
+		 field_initializer ("opencl_funcs",
+		 		    implementation_pointers (impls,
+							     STARPU_OPENCL)),
+		 field_initializer ("cuda_funcs",
+		 		    implementation_pointers (impls,
+							     STARPU_CUDA)),
+		 NULL_TREE);
+
+  return build_constructor_from_unsorted_list (codelet_type (), inits);
+}
+
+/* Return the VAR_DECL that defines a `struct starpu_codelet' structure for
+   TASK_DECL.  The VAR_DECL is assumed to already exists, so it must not be
+   pushed again.  */
+
+tree
+declare_codelet (tree task_decl)
+{
+  /* Retrieve the declaration of the `struct starpu_codelet' object.  */
+  tree cl_decl;
+  cl_decl = lookup_name (build_codelet_identifier (task_decl));
+  gcc_assert (cl_decl != NULL_TREE && TREE_CODE (cl_decl) == VAR_DECL);
+
+  /* Turn the codelet declaration into a definition.  */
+  TREE_TYPE (cl_decl) = codelet_type ();
+  TREE_PUBLIC (cl_decl) = TREE_PUBLIC (task_decl);
+
+  return cl_decl;
+}
+
+/* Build the body of TASK_DECL, which will call `starpu_insert_task'.  */
+
+void
+define_task (tree task_decl)
+{
+  /* First of all, give TASK_DECL an argument list.  */
+  DECL_ARGUMENTS (task_decl) = build_function_arguments (task_decl);
+
+  VEC(tree, gc) *args = NULL;
+  location_t loc = DECL_SOURCE_LOCATION (task_decl);
+  tree p, params = DECL_ARGUMENTS (task_decl);
+
+  /* The first argument will be a pointer to the codelet.  */
+
+  VEC_safe_push (tree, gc, args,
+		 build_addr (task_codelet_declaration (task_decl),
+			     current_function_decl));
+
+  for (p = params; p != NULL_TREE; p = TREE_CHAIN (p))
+    {
+      gcc_assert (TREE_CODE (p) == PARM_DECL);
+
+      tree type = TREE_TYPE (p);
+
+      if (POINTER_TYPE_P (type))
+	{
+	  /* A pointer: the arguments will be:
+	     `STARPU_RW, ptr' or similar.  */
+
+	  VEC_safe_push (tree, gc, args,
+			 build_int_cst (integer_type_node,
+					access_mode (type)));
+	  VEC_safe_push (tree, gc, args, build_pointer_lookup (p));
+	}
+      else
+	{
+	  /* A scalar: the arguments will be:
+	     `STARPU_VALUE, &scalar, sizeof (scalar)'.  */
+
+	  mark_addressable (p);
+
+	  VEC_safe_push (tree, gc, args,
+			 build_int_cst (integer_type_node, STARPU_VALUE));
+	  VEC_safe_push (tree, gc, args,
+			 build_addr (p, current_function_decl));
+	  VEC_safe_push (tree, gc, args,
+			 size_in_bytes (type));
+	}
+    }
+
+  /* Push the terminating zero.  */
+
+  VEC_safe_push (tree, gc, args,
+		 build_int_cst (integer_type_node, 0));
+
+  /* Introduce a local variable to hold the error code.  */
+
+  tree error_var = build_decl (loc, VAR_DECL,
+  			       create_tmp_var_name (".insert_task_error"),
+  			       integer_type_node);
+  DECL_CONTEXT (error_var) = task_decl;
+  DECL_ARTIFICIAL (error_var) = true;
+
+  /* Build this:
+
+       err = starpu_insert_task (...);
+       if (err != 0)
+         { printf ...; abort (); }
+   */
+
+  static tree insert_task_fn;
+  LOOKUP_STARPU_FUNCTION (insert_task_fn, "starpu_insert_task");
+
+  tree call = build_call_expr_loc_vec (loc, insert_task_fn, args);
+
+  tree assignment = build2 (INIT_EXPR, TREE_TYPE (error_var),
+  			    error_var, call);
+
+  tree name = DECL_NAME (task_decl);
+  tree cond = build3 (COND_EXPR, void_type_node,
+		      build2 (NE_EXPR, boolean_type_node,
+			      error_var, integer_zero_node),
+		      build_error_statements (loc, error_var,
+					      build_starpu_error_string,
+					      "failed to insert task `%s'",
+					      IDENTIFIER_POINTER (name)),
+		      NULL_TREE);
+
+  tree stmts = NULL;
+  append_to_statement_list (assignment, &stmts);
+  append_to_statement_list (cond, &stmts);
+
+  tree bind = build3 (BIND_EXPR, void_type_node, error_var, stmts,
+  		      NULL_TREE);
+
+  /* Put it all together.  */
+
+  DECL_SAVED_TREE (task_decl) = bind;
+  TREE_STATIC (task_decl) = true;
+  DECL_EXTERNAL (task_decl) = false;
+  DECL_ARTIFICIAL (task_decl) = true;
+  DECL_INITIAL (task_decl) =
+    build_block (error_var, NULL_TREE, task_decl, NULL_TREE);
+  DECL_RESULT (task_decl) =
+    build_decl (loc, RESULT_DECL, NULL_TREE, void_type_node);
+  DECL_CONTEXT (DECL_RESULT (task_decl)) = task_decl;
+}
+
+/* Add FN to the list of implementations of TASK_DECL.  */
+
+void
+add_task_implementation (tree task_decl, tree fn, const_tree where)
+{
+  location_t loc;
+  tree attr, impls;
+
+  attr = lookup_attribute (task_implementation_list_attribute_name,
+			   DECL_ATTRIBUTES (task_decl));
+  gcc_assert (attr != NULL_TREE);
+
+  gcc_assert (TREE_CODE (where) == STRING_CST);
+
+  loc = DECL_SOURCE_LOCATION (fn);
+
+  impls = tree_cons (NULL_TREE, fn, TREE_VALUE (attr));
+  TREE_VALUE (attr) = impls;
+
+  TREE_USED (fn) = true;
+
+  /* Check the `where' argument to raise a warning if needed.  */
+  if (task_implementation_target_to_int (where) == 0)
+    warning_at (loc, 0,
+		"unsupported target %E; task implementation won't be used",
+		where);
+  else if (task_implementation_target_to_int (where) == STARPU_OPENCL)
+    {
+      local_define (void, validate, (tree t))
+	{
+	  validate_opencl_argument_type (loc, t);
+	};
+
+      for_each (validate, TYPE_ARG_TYPES (TREE_TYPE (fn)));
+    }
+}

+ 55 - 0
gcc-plugin/src/tasks.h

@@ -0,0 +1,55 @@
+/* GCC-StarPU
+   Copyright (C) 2012 Inria
+
+   GCC-StarPU 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 of the License, or
+   (at your option) any later version.
+
+   GCC-StarPU 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 PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with GCC-StarPU.  If not, see <http://www.gnu.org/licenses/>.  */
+
+/* Various utilities.  */
+
+#pragma once
+
+#include <starpu-gcc-config.h>
+
+#include <utils.h>
+#include <starpu.h>
+
+
+extern const char task_attribute_name[];
+extern const char task_implementation_attribute_name[];
+extern const char output_attribute_name[];
+
+extern const char task_implementation_wrapper_attribute_name[];
+extern const char task_implementation_list_attribute_name[];
+
+extern bool task_p (const_tree decl);
+extern bool task_implementation_p (const_tree decl);
+extern int task_implementation_where (const_tree task_impl);
+extern int task_implementation_target_to_int (const_tree target);
+extern tree task_implementation_task (const_tree task_impl);
+extern tree task_codelet_declaration (const_tree task_decl);
+extern tree task_implementation_list (const_tree task_decl);
+extern tree task_pointer_parameter_types (const_tree task_decl);
+extern int task_where (const_tree task_decl);
+extern tree task_implementation_wrapper (const_tree task_impl);
+extern enum starpu_access_mode access_mode (const_tree type);
+extern bool output_type_p (const_tree type);
+
+extern tree codelet_type (void);
+extern void taskify_function (tree fn);
+extern tree build_codelet_identifier (tree task_decl);
+extern tree build_codelet_declaration (tree task_decl);
+extern tree build_codelet_initializer (tree task_decl);
+extern tree declare_codelet (tree task_decl);
+extern void define_task (tree task_decl);
+extern void add_task_implementation (tree task_decl, tree fn,
+				     const_tree where);

+ 373 - 8
gcc-plugin/src/utils.c

@@ -22,27 +22,150 @@
 #include <plugin.h>
 #include <cpplib.h>
 #include <tree.h>
+#include <tree-iterator.h>
 #include <gimple.h>
 
-#include <utils.h>
+#ifdef HAVE_C_FAMILY_C_COMMON_H
+# include <c-family/c-common.h>
+#elif HAVE_C_COMMON_H
+# include <c-common.h>
+#endif
 
+#include <utils.h>
+#include <starpu.h>
 
 /* Whether to enable verbose output.  */
 bool verbose_output_p = false;
 
-/* Name of the `task' attribute.  */
-const char task_attribute_name[] = "task";
+
+/* Various helpers.  */
+
+/* Return a TYPE_DECL for the RECORD_TYPE with tag name TAG.  */
+
+tree
+type_decl_for_struct_tag (const char *tag)
+{
+  tree type_decl = xref_tag (RECORD_TYPE, get_identifier (tag));
+  gcc_assert (type_decl != NULL_TREE
+	      && TREE_CODE (type_decl) == RECORD_TYPE);
+
+  /* `build_decl' expects a TYPE_DECL, so give it what it wants.  */
+
+  type_decl = TYPE_STUB_DECL (type_decl);
+  gcc_assert (type_decl != NULL && TREE_CODE (type_decl) == TYPE_DECL);
+
+  return type_decl;
+}
+
+/* Given ERROR_VAR, an integer variable holding a StarPU error code, return
+   statements that print out the error message returned by
+   BUILD_ERROR_MESSAGE (ERROR_VAR) and abort.  */
+
+tree
+build_error_statements (location_t loc, tree error_var,
+			function_parm (tree, build_error_message, (tree)),
+			const char *fmt, ...)
+{
+  expanded_location xloc = expand_location (loc);
+
+  tree print;
+  char *str, *fmt_long;
+  va_list args;
+
+  va_start (args, fmt);
+
+  /* Build a longer format.  Since FMT itself contains % escapes, this needs
+     to be done in two steps.  */
+
+  vasprintf (&str, fmt, args);
+
+  if (error_var != NULL_TREE)
+    {
+      /* ERROR_VAR is an error code.  */
+      gcc_assert (TREE_CODE (error_var) == VAR_DECL
+		  && TREE_TYPE (error_var) == integer_type_node);
+
+      asprintf (&fmt_long, "%s:%d: error: %s: %%s\n",
+		xloc.file, xloc.line, str);
+
+      print =
+	build_call_expr (builtin_decl_explicit (BUILT_IN_PRINTF), 2,
+			 build_string_literal (strlen (fmt_long) + 1,
+					       fmt_long),
+			 build_error_message (error_var));
+    }
+  else
+    {
+      /* No error code provided.  */
+
+      asprintf (&fmt_long, "%s:%d: error: %s\n",
+		xloc.file, xloc.line, str);
+
+      print =
+	build_call_expr (builtin_decl_explicit (BUILT_IN_PUTS), 1,
+			 build_string_literal (strlen (fmt_long) + 1,
+					       fmt_long));
+    }
+
+  free (fmt_long);
+  free (str);
+  va_end (args);
+
+  tree stmts = NULL;
+  append_to_statement_list (print, &stmts);
+  append_to_statement_list (build_call_expr
+			    (builtin_decl_explicit (BUILT_IN_ABORT), 0),
+			    &stmts);
+
+  return stmts;
+}
+
+/* Return a fresh argument list for FN.  */
+
+tree
+build_function_arguments (tree fn)
+{
+  gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
+	      && DECL_ARGUMENTS (fn) == NULL_TREE);
+
+  local_define (tree, build_argument, (const_tree lst))
+    {
+      tree param, type;
+
+      type = TREE_VALUE (lst);
+      param = build_decl (DECL_SOURCE_LOCATION (fn), PARM_DECL,
+			  create_tmp_var_name ("argument"),
+			  type);
+      DECL_ARG_TYPE (param) = type;
+      DECL_CONTEXT (param) = fn;
 
-/* Return true if DECL is a task.  */
+      return param;
+    };
+
+  return map (build_argument,
+	      list_remove (void_type_p,
+			   TYPE_ARG_TYPES (TREE_TYPE (fn))));
+}
+
+/* Return true if LST holds the void type.  */
 
 bool
-task_p (const_tree decl)
+void_type_p (const_tree lst)
 {
-  return (TREE_CODE (decl) == FUNCTION_DECL &&
-	  lookup_attribute (task_attribute_name,
-			    DECL_ATTRIBUTES (decl)) != NULL_TREE);
+  gcc_assert (TREE_CODE (lst) == TREE_LIST);
+  return VOID_TYPE_P (TREE_VALUE (lst));
 }
 
+/* Return true if LST holds a pointer type.  */
+
+bool
+pointer_type_p (const_tree lst)
+{
+  gcc_assert (TREE_CODE (lst) == TREE_LIST);
+  return POINTER_TYPE_P (TREE_VALUE (lst));
+}
+
+
 /* C expression parser, possibly with C++ linkage.  */
 
 extern int yyparse (location_t, const char *, tree *);
@@ -62,3 +185,245 @@ read_pragma_expressions (const char *pragma, location_t loc)
 
   return expr;
 }
+
+
+/* List and vector utilities, à la SRFI-1.  */
+
+tree
+chain_trees (tree t, ...)
+{
+  va_list args;
+
+  va_start (args, t);
+
+  tree next, prev = t;
+  for (prev = t, next = va_arg (args, tree);
+       next != NULL_TREE;
+       prev = next, next = va_arg (args, tree))
+    TREE_CHAIN (prev) = next;
+
+  va_end (args);
+
+  return t;
+}
+
+tree
+filter (function_parm (bool, pred, (const_tree)), tree t)
+{
+  tree result, lst;
+
+  gcc_assert (TREE_CODE (t) == TREE_LIST);
+
+  result = NULL_TREE;
+  for (lst = t; lst != NULL_TREE; lst = TREE_CHAIN (lst))
+    {
+      if (pred (lst))
+	result = tree_cons (TREE_PURPOSE (lst), TREE_VALUE (lst),
+			    result);
+    }
+
+  return nreverse (result);
+}
+
+tree
+list_remove (function_parm (bool, pred, (const_tree)), tree t)
+{
+  local_define (bool, opposite, (const_tree t))
+  {
+    return !pred (t);
+  };
+
+  return filter (opposite, t);
+}
+
+/* Map FUNC over chain T.  T does not have to be `TREE_LIST'; it can be a
+   chain of arbitrary tree objects.  */
+
+tree
+map (function_parm (tree, func, (const_tree)), tree t)
+{
+  tree result, tail, lst;
+
+  result = tail = NULL_TREE;
+  for (lst = t; lst != NULL_TREE; lst = TREE_CHAIN (lst))
+    {
+      tree r = func (lst);
+      if (tail != NULL_TREE)
+	TREE_CHAIN (tail) = r;
+      else
+	result = r;
+
+      tail = r;
+    }
+
+  return result;
+}
+
+void
+for_each (function_parm (void, func, (tree)), tree t)
+{
+  tree lst;
+
+  gcc_assert (TREE_CODE (t) == TREE_LIST);
+
+  for (lst = t; lst != NULL_TREE; lst = TREE_CHAIN (lst))
+    func (TREE_VALUE (lst));
+}
+
+size_t
+count (function_parm (bool, pred, (const_tree)), const_tree t)
+{
+  size_t result;
+  const_tree lst;
+
+  for (lst = t, result = 0; lst != NULL_TREE; lst = TREE_CHAIN (lst))
+    if (pred (lst))
+      result++;
+
+  return result;
+}
+
+
+/* Useful code backported from GCC 4.6.  */
+
+#if !HAVE_DECL_BUILD_CALL_EXPR_LOC_ARRAY
+
+static tree
+build_call_expr_loc_array (location_t loc, tree fndecl, int n, tree *argarray)
+{
+  tree fntype = TREE_TYPE (fndecl);
+  tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
+
+  return fold_builtin_call_array (loc, TREE_TYPE (fntype), fn, n, argarray);
+}
+
+#endif
+
+#if !HAVE_DECL_BUILD_CALL_EXPR_LOC_VEC
+
+static tree
+build_call_expr_loc_vec (location_t loc, tree fndecl, VEC(tree,gc) *vec)
+{
+  return build_call_expr_loc_array (loc, fndecl, VEC_length (tree, vec),
+				    VEC_address (tree, vec));
+}
+
+#endif
+
+#if !HAVE_DECL_BUILD_ZERO_CST
+
+tree
+build_zero_cst (tree type)
+{
+  switch (TREE_CODE (type))
+    {
+    case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
+    case POINTER_TYPE: case REFERENCE_TYPE:
+    case OFFSET_TYPE:
+      return build_int_cst (type, 0);
+
+    default:
+      abort ();
+    }
+}
+
+#endif
+
+/* Build a "conversion" from a raw C pointer to its data handle.  The
+   assumption is that the programmer should have already registered the
+   pointer by themselves.  */
+
+tree
+build_pointer_lookup (tree pointer)
+{
+  static tree data_lookup_fn;
+
+  /* Make sure DATA_LOOKUP_FN is valid.  */
+  LOOKUP_STARPU_FUNCTION (data_lookup_fn, "starpu_data_lookup");
+
+  location_t loc;
+
+  if (DECL_P (pointer))
+    loc = DECL_SOURCE_LOCATION (pointer);
+  else
+    loc = UNKNOWN_LOCATION;
+
+  /* Introduce a local variable to hold the handle.  */
+
+  tree result_var = build_decl (loc, VAR_DECL,
+  				create_tmp_var_name (".data_lookup_result"),
+  				ptr_type_node);
+  DECL_CONTEXT (result_var) = current_function_decl;
+  DECL_ARTIFICIAL (result_var) = true;
+  DECL_SOURCE_LOCATION (result_var) = loc;
+
+  tree call = build_call_expr (data_lookup_fn, 1, pointer);
+  tree assignment = build2 (INIT_EXPR, TREE_TYPE (result_var),
+  			    result_var, call);
+
+  /* Build `if (RESULT_VAR == NULL) error ();'.  */
+
+  tree cond = build3 (COND_EXPR, void_type_node,
+		      build2 (EQ_EXPR, boolean_type_node,
+			      result_var, null_pointer_node),
+		      build_error_statements (loc, NULL_TREE,
+					      build_starpu_error_string,
+					      "attempt to use unregistered "
+					      "pointer"),
+		      NULL_TREE);
+
+  tree stmts = NULL;
+  append_to_statement_list (assignment, &stmts);
+  append_to_statement_list (cond, &stmts);
+  append_to_statement_list (result_var, &stmts);
+
+  return build4 (TARGET_EXPR, ptr_type_node, result_var, stmts, NULL_TREE, NULL_TREE);
+}
+
+/* Build an error string for the StarPU return value in ERROR_VAR.  */
+
+tree
+build_starpu_error_string (tree error_var)
+{
+  static tree strerror_fn;
+  LOOKUP_STARPU_FUNCTION (strerror_fn, "strerror");
+
+  tree error_code =
+    build1 (NEGATE_EXPR, TREE_TYPE (error_var), error_var);
+
+  return build_call_expr (strerror_fn, 1, error_code);
+}
+
+/* Like `build_constructor_from_list', but sort VALS according to their
+   offset in struct TYPE.  Inspired by `gnat_build_constructor'.  */
+
+tree
+build_constructor_from_unsorted_list (tree type, tree vals)
+{
+  local_define (int, compare_elmt_bitpos, (const void *rt1, const void *rt2))
+  {
+    const constructor_elt *elmt1 = (constructor_elt *) rt1;
+    const constructor_elt *elmt2 = (constructor_elt *) rt2;
+    const_tree field1 = elmt1->index;
+    const_tree field2 = elmt2->index;
+    int ret
+      = tree_int_cst_compare (bit_position (field1), bit_position (field2));
+
+    return ret ? ret : (int) (DECL_UID (field1) - DECL_UID (field2));
+  };
+
+  tree t;
+  VEC(constructor_elt,gc) *v = NULL;
+
+  if (vals)
+    {
+      v = VEC_alloc (constructor_elt, gc, list_length (vals));
+      for (t = vals; t; t = TREE_CHAIN (t))
+	CONSTRUCTOR_APPEND_ELT (v, TREE_PURPOSE (t), TREE_VALUE (t));
+    }
+
+  /* Sort field initializers by field offset.  */
+  VEC_qsort (constructor_elt, v, compare_elmt_bitpos);
+
+  return build_constructor (type, v);
+}

+ 73 - 4
gcc-plugin/src/utils.h

@@ -18,6 +18,10 @@
 
 #pragma once
 
+#include <starpu-gcc-config.h>
+
+#include <unistd.h>
+
 /* GCC 4.7 requires compilation with `g++', and C++ lacks a number of GNU C
    features, so work around that.  */
 
@@ -47,20 +51,85 @@
 #endif /* !__cplusplus */
 
 
-extern bool verbose_output_p;
-extern const char task_attribute_name[];
-extern bool task_p (const_tree decl);
+/* List and vector utilities, à la SRFI-1.  */
 
-extern tree read_pragma_expressions (const char *pragma, location_t loc);
+extern tree chain_trees (tree t, ...)
+  __attribute__ ((sentinel));
+
+extern tree filter (function_parm (bool, pred, (const_tree)), tree t);
+extern tree list_remove (function_parm (bool, pred, (const_tree)), tree t);
+extern tree map (function_parm (tree, func, (const_tree)), tree t);
+extern void for_each (function_parm (void, func, (tree)), tree t);
+extern size_t count (function_parm (bool, pred, (const_tree)), const_tree t);
 
 
+/* Compatibility tricks & workarounds.  */
+
 #include <tree.h>
+#include <vec.h>
 
 /* This declaration is from `c-tree.h', but that header doesn't get
    installed.  */
 
 extern tree xref_tag (enum tree_code, tree);
 
+#if !HAVE_DECL_BUILTIN_DECL_EXPLICIT
+
+/* This function was introduced in GCC 4.7 as a replacement for the
+   `built_in_decls' array.  */
+
+static inline tree
+builtin_decl_explicit (enum built_in_function fncode)
+{
+  return built_in_decls[fncode];
+}
+
+#endif
+
+#if !HAVE_DECL_BUILD_ZERO_CST
+
+extern tree build_zero_cst (tree type);
+
+#endif
+
+#ifndef VEC_qsort
+
+/* This macro is missing in GCC 4.5.  */
+
+# define VEC_qsort(T,V,CMP) qsort(VEC_address (T,V), VEC_length(T,V),	\
+				  sizeof (T), CMP)
+
+#endif
+
+
+/* Helpers.  */
+
+extern bool verbose_output_p;
+
+extern tree build_pointer_lookup (tree pointer);
+extern tree build_starpu_error_string (tree error_var);
+extern tree build_constructor_from_unsorted_list (tree type, tree vals);
+extern tree read_pragma_expressions (const char *pragma, location_t loc);
+extern tree type_decl_for_struct_tag (const char *tag);
+extern tree build_function_arguments (tree fn);
+extern tree build_error_statements (location_t, tree,
+				    function_parm (tree, f, (tree)),
+				    const char *, ...)
+  __attribute__ ((format (printf, 4, 5)));
+
+extern bool void_type_p (const_tree lst);
+extern bool pointer_type_p (const_tree lst);
+
+/* Lookup the StarPU function NAME in the global scope and store the result
+   in VAR (this can't be done from `lower_starpu'.)  */
+
+#define LOOKUP_STARPU_FUNCTION(var, name)				\
+  if ((var) == NULL_TREE)						\
+    {									\
+      (var) = lookup_name (get_identifier (name));			\
+      gcc_assert ((var) != NULL_TREE && TREE_CODE (var) == FUNCTION_DECL); \
+    }
+
 
 /* Don't warn about the unused `gcc_version' variable, from
    <plugin-version.h>.  */

+ 1 - 0
gcc-plugin/src/warn-unregistered.c

@@ -31,6 +31,7 @@
 #include <cgraph.h>
 
 #include <utils.h>
+#include <tasks.h>
 
 /* Return true if there exists a `starpu_vector_data_register' call for VAR
    before GSI in its basic block.  */