diff mbox

Fix memory leak in the vectorizer (PR tree-optimization/79340)

Message ID 20170203222407.GQ14051@tucnak
State New
Headers show

Commit Message

Jakub Jelinek Feb. 3, 2017, 10:24 p.m. UTC
Hi!

As mentioned in the PR, on the gfortran.dg/forall_7.f90 testcase
we leak memory starting with r239542.  vect_get_slp_defs allocates
a vector of vectors, elements corresponding to NULL in slp_ops
are .create (0) vectors that don't need freeing, but the rest
is safe_spliced into other vectors.  We need to release those
vectors afterwards, before the vector containing them is released.

Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

2017-02-03  Jakub Jelinek  <jakub@redhat.com>

	PR tree-optimization/79340
	* tree-vect-loop.c (vectorizable_reduction): Release
	vec_defs elements after safe_splicing them into other vectors.
	Formatting fixes.


	Jakub

Comments

Jeff Law Feb. 3, 2017, 10:33 p.m. UTC | #1
On 02/03/2017 03:24 PM, Jakub Jelinek wrote:
> Hi!
>
> As mentioned in the PR, on the gfortran.dg/forall_7.f90 testcase
> we leak memory starting with r239542.  vect_get_slp_defs allocates
> a vector of vectors, elements corresponding to NULL in slp_ops
> are .create (0) vectors that don't need freeing, but the rest
> is safe_spliced into other vectors.  We need to release those
> vectors afterwards, before the vector containing them is released.
>
> Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?
>
> 2017-02-03  Jakub Jelinek  <jakub@redhat.com>
>
> 	PR tree-optimization/79340
> 	* tree-vect-loop.c (vectorizable_reduction): Release
> 	vec_defs elements after safe_splicing them into other vectors.
> 	Formatting fixes.
OK.
jeff
diff mbox

Patch

--- gcc/tree-vect-loop.c.jj	2017-02-02 08:44:13.000000000 +0100
+++ gcc/tree-vect-loop.c	2017-02-03 13:19:00.087499820 +0100
@@ -6172,20 +6172,24 @@  vectorizable_reduction (gimple *stmt, gi
 	  if (slp_node)
 	    {
 	      /* Get vec defs for all the operands except the reduction index,
-		ensuring the ordering of the ops in the vector is kept.  */
+		 ensuring the ordering of the ops in the vector is kept.  */
 	      auto_vec<tree, 3> slp_ops;
 	      auto_vec<vec<tree>, 3> vec_defs;
 
-	      slp_ops.quick_push ((reduc_index == 0) ? NULL : ops[0]);
-	      slp_ops.quick_push ((reduc_index == 1) ? NULL : ops[1]);
+	      slp_ops.quick_push (reduc_index == 0 ? NULL : ops[0]);
+	      slp_ops.quick_push (reduc_index == 1 ? NULL : ops[1]);
 	      if (op_type == ternary_op)
-		slp_ops.quick_push ((reduc_index == 2) ? NULL : ops[2]);
+		slp_ops.quick_push (reduc_index == 2 ? NULL : ops[2]);
 
 	      vect_get_slp_defs (slp_ops, slp_node, &vec_defs, -1);
 
-	      vec_oprnds0.safe_splice (vec_defs[(reduc_index == 0) ? 1 : 0]);
+	      vec_oprnds0.safe_splice (vec_defs[reduc_index == 0 ? 1 : 0]);
+	      vec_defs[reduc_index == 0 ? 1 : 0].release ();
 	      if (op_type == ternary_op)
-		vec_oprnds1.safe_splice (vec_defs[(reduc_index == 2) ? 1 : 2]);
+		{
+		  vec_oprnds1.safe_splice (vec_defs[reduc_index == 2 ? 1 : 2]);
+		  vec_defs[reduc_index == 2 ? 1 : 2].release ();
+		}
 	    }
           else
 	    {
@@ -6194,7 +6198,7 @@  vectorizable_reduction (gimple *stmt, gi
               vec_oprnds0.quick_push (loop_vec_def0);
               if (op_type == ternary_op)
                {
-		 op1 = (reduc_index == 0) ? ops[2] : ops[1];
+		 op1 = reduc_index == 0 ? ops[2] : ops[1];
                  loop_vec_def1 = vect_get_vec_def_for_operand (op1, stmt);
                  vec_oprnds1.quick_push (loop_vec_def1);
                }