Browse Source

gcc: Pass field initializers to the codelet constructor in the right order.

* gcc-plugin/src/starpu.c (build_constructor_from_unsorted_list): New
  function.
  (build_codelet_initializer): Use it.  Uncomment the `cuda_func' initializer.
Ludovic Courtès 14 years ago
parent
commit
91e09797ef
1 changed files with 40 additions and 4 deletions
  1. 40 4
      gcc-plugin/src/starpu.c

+ 40 - 4
gcc-plugin/src/starpu.c

@@ -99,6 +99,42 @@ build_call_expr_loc_vec (location_t loc, tree fndecl, VEC(tree,gc) *vec)
 #endif
 
 
+/* Helpers.  */
+
+/* Like `build_constructor_from_list', but sort VALS according to their
+   offset in struct TYPE.  Inspired by `gnat_build_constructor'.  */
+
+static tree
+build_constructor_from_unsorted_list (tree type, tree vals)
+{
+  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);
+}
+
 /* Debugging helpers.  */
 
 static tree build_printf (const char *, ...)
@@ -815,12 +851,12 @@ build_codelet_initializer (tree task_decl)
 		 field_initializer ("opencl_func",
 		 		    implementation_pointer (impls,
 		 					    STARPU_OPENCL)),
-		 /* field_initializer ("cuda_func", */
-		 /* 		    implementation_pointer (impls, */
-		 /* 					    STARPU_CUDA)), */
+		 field_initializer ("cuda_func",
+		 		    implementation_pointer (impls,
+		 					    STARPU_CUDA)),
 		 NULL_TREE);
 
-  return build_constructor_from_list (codelet_type (), inits);
+  return build_constructor_from_unsorted_list (codelet_type (), inits);
 }
 
 /* Return the VAR_DECL that defines a `starpu_codelet' structure for