rs6000: Fix memory leak in rs6000_density_test

Message ID mpth7djep7l.fsf@arm.com
State Committed
Commit 73f34f4d02d72dd61e4573402a202b35cf3cce0f
Headers
Series rs6000: Fix memory leak in rs6000_density_test |

Commit Message

Richard Sandiford Oct. 14, 2021, 1 p.m. UTC
  rs6000_density_test has an early exit test between a call
to get_loop_body and the corresponding free.  This would
lead to a memory leak if the early exit is taken.

Tested on powerpc64le-linux-gnu.  It's obvious that moving the
test avoids the leak, but there are multiple ways to write it,
so: OK to install?

Richard


gcc/
	* config/rs6000/rs6000.c (rs6000_density_test): Move early
	exit test further up the function.
---
 gcc/config/rs6000/rs6000.c | 17 ++++++++---------
 1 file changed, 8 insertions(+), 9 deletions(-)
  

Comments

David Edelsohn Oct. 14, 2021, 2:05 p.m. UTC | #1
On Thu, Oct 14, 2021 at 9:00 AM Richard Sandiford
<richard.sandiford@arm.com> wrote:
>
> rs6000_density_test has an early exit test between a call
> to get_loop_body and the corresponding free.  This would
> lead to a memory leak if the early exit is taken.
>
> Tested on powerpc64le-linux-gnu.  It's obvious that moving the
> test avoids the leak, but there are multiple ways to write it,
> so: OK to install?

Thanks for noticing this and creating a patch.

This is okay.

Thanks, David

>
> Richard
>
>
> gcc/
>         * config/rs6000/rs6000.c (rs6000_density_test): Move early
>         exit test further up the function.
> ---
>  gcc/config/rs6000/rs6000.c | 17 ++++++++---------
>  1 file changed, 8 insertions(+), 9 deletions(-)
>
> diff --git a/gcc/config/rs6000/rs6000.c b/gcc/config/rs6000/rs6000.c
> index acba4d9f26c..01a95591a5d 100644
> --- a/gcc/config/rs6000/rs6000.c
> +++ b/gcc/config/rs6000/rs6000.c
> @@ -5289,20 +5289,19 @@ struct rs6000_cost_data
>  static void
>  rs6000_density_test (rs6000_cost_data *data)
>  {
> -  struct loop *loop = data->loop_info;
> -  basic_block *bbs = get_loop_body (loop);
> -  int nbbs = loop->num_nodes;
> -  loop_vec_info loop_vinfo = loop_vec_info_for_loop (data->loop_info);
> -  int vec_cost = data->cost[vect_body], not_vec_cost = 0;
> -  int i, density_pct;
> -
>    /* This density test only cares about the cost of vector version of the
>       loop, so immediately return if we are passed costing for the scalar
>       version (namely computing single scalar iteration cost).  */
>    if (data->costing_for_scalar)
>      return;
>
> -  for (i = 0; i < nbbs; i++)
> +  struct loop *loop = data->loop_info;
> +  basic_block *bbs = get_loop_body (loop);
> +  int nbbs = loop->num_nodes;
> +  loop_vec_info loop_vinfo = loop_vec_info_for_loop (data->loop_info);
> +  int vec_cost = data->cost[vect_body], not_vec_cost = 0;
> +
> +  for (int i = 0; i < nbbs; i++)
>      {
>        basic_block bb = bbs[i];
>        gimple_stmt_iterator gsi;
> @@ -5322,7 +5321,7 @@ rs6000_density_test (rs6000_cost_data *data)
>      }
>
>    free (bbs);
> -  density_pct = (vec_cost * 100) / (vec_cost + not_vec_cost);
> +  int density_pct = (vec_cost * 100) / (vec_cost + not_vec_cost);
>
>    if (density_pct > rs6000_density_pct_threshold
>        && vec_cost + not_vec_cost > rs6000_density_size_threshold)
  

Patch

diff --git a/gcc/config/rs6000/rs6000.c b/gcc/config/rs6000/rs6000.c
index acba4d9f26c..01a95591a5d 100644
--- a/gcc/config/rs6000/rs6000.c
+++ b/gcc/config/rs6000/rs6000.c
@@ -5289,20 +5289,19 @@  struct rs6000_cost_data
 static void
 rs6000_density_test (rs6000_cost_data *data)
 {
-  struct loop *loop = data->loop_info;
-  basic_block *bbs = get_loop_body (loop);
-  int nbbs = loop->num_nodes;
-  loop_vec_info loop_vinfo = loop_vec_info_for_loop (data->loop_info);
-  int vec_cost = data->cost[vect_body], not_vec_cost = 0;
-  int i, density_pct;
-
   /* This density test only cares about the cost of vector version of the
      loop, so immediately return if we are passed costing for the scalar
      version (namely computing single scalar iteration cost).  */
   if (data->costing_for_scalar)
     return;
 
-  for (i = 0; i < nbbs; i++)
+  struct loop *loop = data->loop_info;
+  basic_block *bbs = get_loop_body (loop);
+  int nbbs = loop->num_nodes;
+  loop_vec_info loop_vinfo = loop_vec_info_for_loop (data->loop_info);
+  int vec_cost = data->cost[vect_body], not_vec_cost = 0;
+
+  for (int i = 0; i < nbbs; i++)
     {
       basic_block bb = bbs[i];
       gimple_stmt_iterator gsi;
@@ -5322,7 +5321,7 @@  rs6000_density_test (rs6000_cost_data *data)
     }
 
   free (bbs);
-  density_pct = (vec_cost * 100) / (vec_cost + not_vec_cost);
+  int density_pct = (vec_cost * 100) / (vec_cost + not_vec_cost);
 
   if (density_pct > rs6000_density_pct_threshold
       && vec_cost + not_vec_cost > rs6000_density_size_threshold)