Bladeren bron

gcc: Fix support of the `heap_allocated' attribute.

This partly reverts r4728 ("gcc: Make `heap_allocated' arrays really
have an array type.").

* gcc-plugin/src/starpu.c (handle_heap_allocated_attribute): Turn var
  into a pointer, instead of a hacked array.  Use `_starpu_free_unref'
  as the cleanup handler.

* gcc-plugin/tests/heap-allocated.c (foo): Make sure &m[0][0] != &m, and
  m[0][0] == m.  Remove wrong assertion.

* gcc-plugin/tests/mocks.h (starpu_free): Remove.
  (_starpu_free_unref): New function.

* include/starpu_data.h (_starpu_free_unref)[STARPU_GCC_PLUGIN]: New
  declaration.

* src/util/malloc.c (_starpu_free_unref): New function.
Ludovic Courtès 13 jaren geleden
bovenliggende
commit
54d104d38c
5 gewijzigde bestanden met toevoegingen van 30 en 11 verwijderingen
  1. 7 5
      gcc-plugin/src/starpu.c
  2. 9 2
      gcc-plugin/tests/heap-allocated.c
  3. 3 4
      gcc-plugin/tests/mocks.h
  4. 4 0
      include/starpu_data.h
  5. 7 0
      src/util/malloc.c

+ 7 - 5
gcc-plugin/src/starpu.c

@@ -971,12 +971,14 @@ handle_heap_allocated_attribute (tree *node, tree name, tree args,
     }
   else
     {
+      /* Turn VAR into a pointer that feels like an array.  This is what's
+	 done for PARM_DECLs that have an array type.  */
+
       tree array_type = TREE_TYPE (var);
-      tree pointer_type = build_pointer_type (strip_array_types (array_type));
+      tree element_type = TREE_TYPE (array_type);
+      tree pointer_type = build_pointer_type (element_type);
 
-      /* We want VAR to feel like an array, but to really be a pointer.  So
-	 the hack consists in keeping its array type, but giving it the
-	 storage of a pointer.  (XXX) */
+      TREE_TYPE (var) = pointer_type;
       DECL_SIZE (var) = TYPE_SIZE (pointer_type);
       DECL_SIZE_UNIT (var) = TYPE_SIZE_UNIT (pointer_type);
       DECL_ALIGN (var) = TYPE_ALIGN (pointer_type);
@@ -996,7 +998,7 @@ handle_heap_allocated_attribute (tree *node, tree name, tree args,
 	 TODO: Provide a way to disable this.  */
       DECL_ATTRIBUTES (var) =
 	tree_cons (get_identifier ("cleanup"),
-		   lookup_name (get_identifier ("starpu_free")),
+		   lookup_name (get_identifier ("_starpu_free_unref")),
 		   DECL_ATTRIBUTES (var));
     }
 

+ 9 - 2
gcc-plugin/tests/heap-allocated.c

@@ -1,5 +1,5 @@
 /* GCC-StarPU
-   Copyright (C) 2011 Institut National de Recherche en Informatique et Automatique
+   Copyright (C) 2011, 2012 Institut National de Recherche en Informatique et Automatique
 
    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
@@ -26,12 +26,14 @@ foo (size_t size)
 
   float *test_array_parm (float m[size][23], int x, int y)
   {
+    assert ((char *) &m[0][0] != (char *) &m);
     return &m[x][y];
   }
 
   size_t minus_one = size - 1;
   float *test_array_static_parm (float m[static minus_one][23], int x, int y)
   {
+    assert ((char *) &m[0][0] != (char *) &m);
     return &m[x][y];
   }
 
@@ -48,6 +50,12 @@ foo (size_t size)
 
   assert (malloc_calls == 1);
 
+  /* `&m' points to the on-stack area that stores the underlying pointer,
+     whereas `m' and `m[0][0]' should point to the heap-allocated area.  */
+  assert ((char *) &m != (char *) m);
+  assert ((char *) &m[0][0] != (char *) &m);
+  assert ((char *) &m[0][0] == (char *) m);
+
   /* Make sure "array arithmetic" works.  */
   assert ((char *) &m[0][1] - (char *) &m[0][0] == sizeof m[0][0]);
   assert ((char *) &m[1][0] - (char *) &m[0][22] == sizeof m[0][0]);
@@ -59,7 +67,6 @@ foo (size_t size)
 	assert (&m[x][y] == test_array_parm (m, x, y));
 	assert (&m[x][y] == test_array_static_parm (m, x, y));
 	assert (&m[x][y] == test_pointer_parm ((float *) m, x, y));
-	assert (&m[x][y] == test_pointer_parm ((float *) &m, x, y));
       }
 
   /* Freed when going out of scope.  */

+ 3 - 4
gcc-plugin/tests/mocks.h

@@ -292,12 +292,11 @@ starpu_malloc (void **ptr, size_t size)
   return 0;
 }
 
-int
-starpu_free (void *ptr)
+void
+_starpu_free_unref (void *ptr)
 {
-  assert (ptr == expected_free_argument);
+  assert (* (void **) ptr == expected_free_argument);
   free_calls++;
-  return 0;
 }
 
 

+ 4 - 0
include/starpu_data.h

@@ -79,6 +79,10 @@ void starpu_data_release(starpu_data_handle_t handle);
 int starpu_malloc(void **A, size_t dim);
 int starpu_free(void *A);
 
+#ifdef STARPU_GCC_PLUGIN
+void _starpu_free_unref(void *p);
+#endif
+
 /* XXX These macros are provided to avoid breaking old codes. But consider
  * these function names as deprecated. */
 #define starpu_data_malloc_pinned_if_possible	starpu_malloc

+ 7 - 0
src/util/malloc.c

@@ -224,3 +224,10 @@ int starpu_free(void *A)
 
 	return 0;
 }
+
+/* Internal convenience function, used by code generated by the GCC
+ * plug-in.  */
+void _starpu_free_unref(void *p)
+{
+	(void)starpu_free(* (void **)p);
+}