Browse Source

gcc: Factorize OpenCL file-loading code.

* gcc-plugin/src/starpu.c (build_variable_from_file_contents): New
  function.
  (define_opencl_task_implementation): Remove file-loading code to the
  above function, and use it.
Ludovic Courtès 13 years ago
parent
commit
d7f1fb7e2e
1 changed files with 73 additions and 54 deletions
  1. 73 54
      gcc-plugin/src/starpu.c

+ 73 - 54
gcc-plugin/src/starpu.c

@@ -1043,64 +1043,26 @@ build_string_variable (location_t loc, const char *name_seed,
   return decl;
 }
 
-/* Return the type corresponding to OPENCL_PROGRAM_STRUCT_TAG.  */
+/* 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
-opencl_program_type (void)
+build_variable_from_file_contents (location_t loc,
+				   const char *name_seed,
+				   const char *file,
+				   const_tree search_path)
 {
-  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;
-}
-
-/* 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)
-{
-  gcc_assert (task_implementation_p (task_impl)
-	      && task_implementation_where (task_impl) == STARPU_OPENCL);
-  gcc_assert (TREE_CODE (kernel) == STRING_CST);
-
-  if (!verbose_output_p)
-    /* No further warnings for this node.  */
-    TREE_NO_WARNING (task_impl) = true;
-
-  static tree load_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;
-	}
-    }
-
-  if (verbose_output_p)
-    inform (loc, "defining %qE, with OpenCL kernel %qs from file %qs",
-	    DECL_NAME (task_impl), TREE_STRING_POINTER (kernel), file);
+  gcc_assert (search_path != NULL_TREE
+	      && TREE_CODE (search_path) == STRING_CST);
 
   int err, dir_fd;
   struct stat st;
-  tree source_var = NULL_TREE;
-  tree dirs;
+  const_tree dirs;
+  tree var = NULL_TREE;
 
-  /* Look for FILE in each OPENCL_INCLUDE_DIRS, and pick the first one that
-     matches.  */
+  /* Look for FILE in each directory in SEARCH_PATH, and pick the first one
+     that matches.  */
   for (err = ENOENT, dir_fd = -1, dirs = opencl_include_dirs;
        (err != 0 || err == ENOENT) && dirs != NULL_TREE;
        dirs = TREE_CHAIN (dirs))
@@ -1146,9 +1108,9 @@ define_opencl_task_implementation (location_t loc, tree task_impl,
 	    error_at (loc, "failed to map contents of %qs: %m", file);
 	  else
 	    {
-	      source_var = build_string_variable (loc, "opencl_source",
-						  contents, st.st_size);
-	      pushdecl (source_var);
+	      var = build_string_variable (loc, name_seed,
+					   contents, st.st_size);
+	      pushdecl (var);
 	      munmap (contents, st.st_size);
 	    }
 
@@ -1156,6 +1118,63 @@ define_opencl_task_implementation (location_t loc, tree task_impl,
 	}
     }
 
+  return var;
+}
+
+/* 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;
+}
+
+/* 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)
+{
+  gcc_assert (task_implementation_p (task_impl)
+	      && task_implementation_where (task_impl) == STARPU_OPENCL);
+  gcc_assert (TREE_CODE (kernel) == STRING_CST);
+
+  if (!verbose_output_p)
+    /* No further warnings for this node.  */
+    TREE_NO_WARNING (task_impl) = true;
+
+  static tree load_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;
+	}
+    }
+
+  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)
     {
       tree prog_var, prog_loaded_var;