[PATCH] tree-optimization/97630 - fix SLP cycle memory leak

Richard Biener rguenther@suse.de
Wed Dec 2 13:48:12 GMT 2020


This fixes SLP cycles leaking memory by maintaining a double-linked
list of allocatd SLP nodes we can zap when we free the alloc pool.

Bootstrap & regtest running on x86_64-unknown-linux-gnu.

2020-12-02  Richard Biener  <rguenther@suse.de>

	PR tree-optimization/97630
	* tree-vectorizer.h (_slp_tree::next_node,
	_slp_tree::prev_node): New.
	(vect_slp_init): Declare.
	(vect_slp_fini): Likewise.
	* tree-vectorizer.c (vectorize_loops): Call vect_slp_init/fini.
	(pass_slp_vectorize::execute): Likewise.
	* tree-vect-slp.c (vect_slp_init): New.
	(vect_slp_fini): Likewise.
	(slp_first_node): New global.
	(_slp_tree::_slp_tree): Link node into the SLP tree list.
	(_slp_tree::~_slp_tree): Delink node from the SLP tree list.
---
 gcc/tree-vect-slp.c   | 31 ++++++++++++++++++++++++++++++-
 gcc/tree-vectorizer.c | 10 ++++------
 gcc/tree-vectorizer.h |  9 ++++++---
 3 files changed, 40 insertions(+), 10 deletions(-)

diff --git a/gcc/tree-vect-slp.c b/gcc/tree-vect-slp.c
index 55e0a23253d..be87475092d 100644
--- a/gcc/tree-vect-slp.c
+++ b/gcc/tree-vect-slp.c
@@ -48,12 +48,30 @@ along with GCC; see the file COPYING3.  If not see
 #include "cfganal.h"
 #include "tree-eh.h"
 #include "tree-cfg.h"
+#include "alloc-pool.h"
 
 static bool vectorizable_slp_permutation (vec_info *, gimple_stmt_iterator *,
 					  slp_tree, stmt_vector_for_cost *);
 static void vect_print_slp_tree (dump_flags_t, dump_location_t, slp_tree);
+static void vect_free_slp_tree (slp_tree);
 
-object_allocator<_slp_tree> *slp_tree_pool;
+static object_allocator<_slp_tree> *slp_tree_pool;
+static slp_tree slp_first_node;
+
+void
+vect_slp_init (void)
+{
+  slp_tree_pool = new object_allocator<_slp_tree> ("SLP nodes");
+}
+
+void
+vect_slp_fini (void)
+{
+  while (slp_first_node)
+    delete slp_first_node;
+  delete slp_tree_pool;
+  slp_tree_pool = NULL;
+}
 
 void *
 _slp_tree::operator new (size_t n)
@@ -74,6 +92,11 @@ _slp_tree::operator delete (void *node, size_t n)
 
 _slp_tree::_slp_tree ()
 {
+  this->prev_node = NULL;
+  if (slp_first_node)
+    slp_first_node->prev_node = this;
+  this->next_node = slp_first_node;
+  slp_first_node = this;
   SLP_TREE_SCALAR_STMTS (this) = vNULL;
   SLP_TREE_SCALAR_OPS (this) = vNULL;
   SLP_TREE_VEC_STMTS (this) = vNULL;
@@ -96,6 +119,12 @@ _slp_tree::_slp_tree ()
 
 _slp_tree::~_slp_tree ()
 {
+  if (this->prev_node)
+    this->prev_node->next_node = this->next_node;
+  else
+    slp_first_node = this->next_node;
+  if (this->next_node)
+    this->next_node->prev_node = this->prev_node;
   SLP_TREE_CHILDREN (this).release ();
   SLP_TREE_SCALAR_STMTS (this).release ();
   SLP_TREE_SCALAR_OPS (this).release ();
diff --git a/gcc/tree-vectorizer.c b/gcc/tree-vectorizer.c
index b63dda31a08..f9e26422018 100644
--- a/gcc/tree-vectorizer.c
+++ b/gcc/tree-vectorizer.c
@@ -1171,7 +1171,7 @@ vectorize_loops (void)
   if (vect_loops_num <= 1)
     return 0;
 
-  slp_tree_pool = new object_allocator<_slp_tree> ("SLP nodes for vect");
+  vect_slp_init ();
 
   if (cfun->has_simduid_loops)
     note_simd_array_uses (&simd_array_to_simduid_htab);
@@ -1295,8 +1295,7 @@ vectorize_loops (void)
     shrink_simd_arrays (simd_array_to_simduid_htab, simduid_to_vf_htab);
   delete simduid_to_vf_htab;
   cfun->has_simduid_loops = false;
-  delete slp_tree_pool;
-  slp_tree_pool = NULL;
+  vect_slp_fini ();
 
   if (num_vectorized_loops > 0)
     {
@@ -1432,12 +1431,11 @@ pass_slp_vectorize::execute (function *fun)
 	}
     }
 
-  slp_tree_pool = new object_allocator<_slp_tree> ("SLP nodes for slp");
+  vect_slp_init ();
 
   vect_slp_function (fun);
 
-  delete slp_tree_pool;
-  slp_tree_pool = NULL;
+  vect_slp_fini ();
 
   if (!in_loop_pipeline)
     {
diff --git a/gcc/tree-vectorizer.h b/gcc/tree-vectorizer.h
index c9f95bc6ffe..661444e3dac 100644
--- a/gcc/tree-vectorizer.h
+++ b/gcc/tree-vectorizer.h
@@ -26,7 +26,6 @@ typedef class _stmt_vec_info *stmt_vec_info;
 #include "tree-data-ref.h"
 #include "tree-hash-traits.h"
 #include "target.h"
-#include "alloc-pool.h"
 
 
 /* Used for naming of new temporaries.  */
@@ -116,8 +115,6 @@ typedef hash_map<tree_operand_hash,
  ************************************************************************/
 typedef struct _slp_tree *slp_tree;
 
-extern object_allocator<_slp_tree> *slp_tree_pool;
-
 /* A computation tree of an SLP instance.  Each node corresponds to a group of
    stmts to be packed in a SIMD stmt.  */
 struct _slp_tree {
@@ -177,6 +174,10 @@ struct _slp_tree {
 
   /* Return memory to slp_tree_pool.  */
   static void operator delete (void *, size_t);
+
+  /* Linked list of nodes to release when we free the slp_tree_pool.  */
+  slp_tree next_node;
+  slp_tree prev_node;
 };
 
 /* The enum describes the type of operations that an SLP instance
@@ -1968,6 +1969,8 @@ extern int vect_get_known_peeling_cost (loop_vec_info, int, int *,
 extern tree cse_and_gimplify_to_preheader (loop_vec_info, tree);
 
 /* In tree-vect-slp.c.  */
+extern void vect_slp_init (void);
+extern void vect_slp_fini (void);
 extern void vect_free_slp_instance (slp_instance);
 extern bool vect_transform_slp_perm_load (vec_info *, slp_tree, vec<tree>,
 					  gimple_stmt_iterator *, poly_uint64,
-- 
2.26.2


More information about the Gcc-patches mailing list