This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: RFA (GGC): PATCH to support GGC finalizers with PCH


On 11/17/2015 09:39 AM, Richard Biener wrote:
On Tue, Nov 17, 2015 at 3:09 PM, Jason Merrill <jason@redhat.com> wrote:
While I was looking at the interaction of delayed folding with GGC, I
noticed that ggc_handle_finalizers currently runs no finalizers if
G.context_depth != 0.  So any GC objects in a greater depth will still be
collected, but they won't have their finalizers run.  This specifically
affects compiles that use a PCH file, since G.context_depth is set to 1
after loading the PCH.

This patch fixes ggc_handle_finalizers to look at the depth of each
finalizer so that we still don't try to run finalizers for non-collectable
objects loaded from the PCH, but we do run finalizers for collectable
objects allocated after loading the PCH.

I ended up not relying on this for delayed folding, but it still seems like
a good bug fix.

Tested x86_64-pc-linux-gnu.  OK for trunk?

Hmm, this enlarges finalizer/vec_finalizer.  Wouldn't it be better to
add separate finalizer vectors for context_depth != 0?  (I'm proposing
to add one for exactly context_depth == 1)

When is context_depth increased other than for PCH?

That seems to be the only place it's changed currently. I was assuming that the generalized way ggc-page handles context_depth was intended to support more depths in the future (perhaps for collecting after processing a nested function?), so my patch was following that model.

How about this?

Jason

commit afb196cd7fc176736f9ff2abf92690a7c4ae4f94
Author: Jason Merrill <jason@redhat.com>
Date:   Fri Nov 13 09:39:15 2015 -0500

    	Support GGC finalizers with PCH.
    
    	* ggc-page.c (ggc_globals): Change finalizers and vec_finalizers
    	to be vecs of vecs.
    	(add_finalizer): Split out from ggc_internal_alloc.
    	(ggc_handle_finalizers): Run finalizers for the current depth.
    	(init_ggc, ggc_pch_read): Reserve space for finalizers.

diff --git a/gcc/ggc-page.c b/gcc/ggc-page.c
index deb21bb..c3af3c8 100644
--- a/gcc/ggc-page.c
+++ b/gcc/ggc-page.c
@@ -361,7 +361,7 @@ private:
   void (*m_function)(void *);
   size_t m_object_size;
   size_t m_n_objects;
-  };
+};
 
 #ifdef ENABLE_GC_ALWAYS_COLLECT
 /* List of free objects to be verified as actually free on the
@@ -456,11 +456,11 @@ static struct ggc_globals
      better runtime data access pattern.  */
   unsigned long **save_in_use;
 
-  /* Finalizers for single objects.  */
-  vec<finalizer> finalizers;
+  /* Finalizers for single objects.  The first index is collection_depth.  */
+  vec<vec<finalizer> > finalizers;
 
   /* Finalizers for vectors of objects.  */
-  vec<vec_finalizer> vec_finalizers;
+  vec<vec<vec_finalizer> > vec_finalizers;
 
 #ifdef ENABLE_GC_ALWAYS_COLLECT
   /* List of free objects to be verified as actually free on the
@@ -1240,6 +1240,23 @@ ggc_round_alloc_size (size_t requested_size)
   return size;
 }
 
+static void
+add_finalizer (void *result, void (*f)(void *), size_t s, size_t n)
+{
+  if (f == NULL)
+    ;
+  else if (n == 1)
+    {
+      finalizer fin (result, f);
+      G.finalizers[G.context_depth].safe_push (fin);
+    }
+  else
+    {
+      vec_finalizer fin (reinterpret_cast<uintptr_t> (result), f, s, n);
+      G.vec_finalizers[G.context_depth].safe_push (fin);
+    }
+}
+
 /* Allocate a chunk of memory of SIZE bytes.  Its contents are undefined.  */
 
 void *
@@ -1387,11 +1404,8 @@ ggc_internal_alloc (size_t size, void (*f)(void *), size_t s, size_t n
   /* For timevar statistics.  */
   timevar_ggc_mem_total += object_size;
 
-  if (f && n == 1)
-    G.finalizers.safe_push (finalizer (result, f));
-  else if (f)
-    G.vec_finalizers.safe_push
-      (vec_finalizer (reinterpret_cast<uintptr_t> (result), f, s, n));
+  if (f)
+    add_finalizer (result, f, s, n);
 
   if (GATHER_STATISTICS)
     {
@@ -1788,6 +1802,9 @@ init_ggc (void)
   G.by_depth_max = INITIAL_PTE_COUNT;
   G.by_depth = XNEWVEC (page_entry *, G.by_depth_max);
   G.save_in_use = XNEWVEC (unsigned long *, G.by_depth_max);
+
+  G.finalizers.safe_grow_cleared (1);
+  G.vec_finalizers.safe_grow_cleared (1);
 }
 
 /* Merge the SAVE_IN_USE_P and IN_USE_P arrays in P so that IN_USE_P
@@ -1875,36 +1892,42 @@ clear_marks (void)
 static void
 ggc_handle_finalizers ()
 {
-  if (G.context_depth != 0)
-    return;
-
-  unsigned length = G.finalizers.length ();
-  for (unsigned int i = 0; i < length;)
+  unsigned dlen = G.finalizers.length();
+  for (unsigned d = G.context_depth; d < dlen; ++d)
     {
-      finalizer &f = G.finalizers[i];
-      if (!ggc_marked_p (f.addr ()))
+      vec<finalizer> &v = G.finalizers[d];
+      unsigned length = v.length ();
+      for (unsigned int i = 0; i < length;)
 	{
-	  f.call ();
-	  G.finalizers.unordered_remove (i);
-	  length--;
+	  finalizer &f = v[i];
+	  if (!ggc_marked_p (f.addr ()))
+	    {
+	      f.call ();
+	      v.unordered_remove (i);
+	      length--;
+	    }
+	  else
+	    i++;
 	}
-      else
-	i++;
     }
 
-
-  length = G.vec_finalizers.length ();
-  for (unsigned int i = 0; i < length;)
+  gcc_assert (dlen == G.vec_finalizers.length());
+  for (unsigned d = G.context_depth; d < dlen; ++d)
     {
-      vec_finalizer &f = G.vec_finalizers[i];
-      if (!ggc_marked_p (f.addr ()))
+      vec<vec_finalizer> &vv = G.vec_finalizers[d];
+      unsigned length = vv.length ();
+      for (unsigned int i = 0; i < length;)
 	{
-	  f.call ();
-	  G.vec_finalizers.unordered_remove (i);
-	  length--;
+	  vec_finalizer &f = vv[i];
+	  if (!ggc_marked_p (f.addr ()))
+	    {
+	      f.call ();
+	      vv.unordered_remove (i);
+	      length--;
+	    }
+	  else
+	    i++;
 	}
-      else
-	i++;
     }
 }
 
@@ -2545,6 +2568,8 @@ ggc_pch_read (FILE *f, void *addr)
      pages to be 1 too.  PCH pages will have depth 0.  */
   gcc_assert (!G.context_depth);
   G.context_depth = 1;
+  G.finalizers.safe_grow_cleared (2);
+  G.vec_finalizers.safe_grow_cleared (2);
   for (i = 0; i < NUM_ORDERS; i++)
     {
       page_entry *p;

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]