Fixing PR107411

Message ID 20230217222603.2485714-1-qing.zhao@oracle.com
State Accepted
Headers
Series Fixing PR107411 |

Checks

Context Check Description
snail/gcc-patch-check success Github commit url

Commit Message

Qing Zhao Feb. 17, 2023, 10:26 p.m. UTC
  This is a bug in tree-ssa-uninit.cc.
When doing the following:

  /* Ignore the call to .DEFERRED_INIT that define the original
     var itself as the following case:
       temp = .DEFERRED_INIT (4, 2, “alt_reloc");
       alt_reloc = temp;
     In order to avoid generating warning for the fake usage
     at alt_reloc = temp.
  */

We need to compare the var name inside the .DEFERRED_INIT call
(the 3rd argument) and the name for the LHS variable. if they are the same,
we will NOT report the warning.

There is one issue when we get the name for the LHS variable. when the
variable doesn't have a DECL_NAME (it's not a user declared variable,
which is the case for this bug):

  _1 = .DEFERRED_INIT (4, 2, &"D.2389"[0]);
  D.2389 = _1;

The current checking just ignores this case, and still report the warning.

The fix is very simple, when getting the name for the LHS variable, we should
consider this case and come up with the name the same way as we construct the
3rd argument for the call to .DEFERRED_INIT (please refer to the routine
"gimple_add_init_for_auto_var")

The patch has been bootstrapped and regression tested on both x86 and aarch64.
Okay for committing?

thanks.

Qing

	PR middle-end/107411

gcc/ChangeLog:

	PR middle-end/107411
	* tree-ssa-uninit.cc (warn_uninit): Handle the case when the
	LHS varaible of a .DEFERRED_INIT call doesn't have a DECL_NAME.

gcc/testsuite/ChangeLog:

	PR middle-end/107411
	* g++.dg/pr107411.C: New test.
---
 gcc/testsuite/g++.dg/pr107411.C | 10 ++++++++++
 gcc/tree-ssa-uninit.cc          | 27 ++++++++++++++++++++-------
 2 files changed, 30 insertions(+), 7 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/pr107411.C
  

Comments

Jakub Jelinek Feb. 17, 2023, 10:35 p.m. UTC | #1
On Fri, Feb 17, 2023 at 10:26:03PM +0000, Qing Zhao via Gcc-patches wrote:
> +	      else if (!DECL_NAME (lhs_var))
> +		{
> +		  char *lhs_var_name_str
> +		    = xasprintf ("D.%u", DECL_UID (lhs_var));

Why xasprintf?  D.%u can be sprintfed into a fixed size automatic buffer,
say 3 + (HOST_BITS_PER_INT + 2) / 3 would be a good upper bound for the size
of the buffer.  Then you don't need to free it...

> +		  if (strcmp (lhs_var_name_str, var_name_str) == 0)
> +		    {
> +		      free (lhs_var_name_str);
> +		      return;
> +		    }
> +		  free (lhs_var_name_str);
> +		}
> +	    }
>  	  gcc_assert (var_name_str && var_def_stmt);
>  	}
>      }
> -- 
> 2.31.1

	Jakub
  
Qing Zhao Feb. 20, 2023, 3:04 p.m. UTC | #2
> On Feb 17, 2023, at 5:35 PM, Jakub Jelinek <jakub@redhat.com> wrote:
> 
> On Fri, Feb 17, 2023 at 10:26:03PM +0000, Qing Zhao via Gcc-patches wrote:
>> +	      else if (!DECL_NAME (lhs_var))
>> +		{
>> +		  char *lhs_var_name_str
>> +		    = xasprintf ("D.%u", DECL_UID (lhs_var));
> 
> Why xasprintf?

Just emulated the code in “gimple_add_init_for_auto_var” without thinking too much. -:)
>  D.%u can be sprintfed into a fixed size automatic buffer,
> say 3 + (HOST_BITS_PER_INT + 2) / 3 would be a good upper bound for the size
> of the buffer.  Then you don't need to free it...

xasprintf is "like a sprintf but provided a pointer to malloc’d storage (without fail)”. If free the pointer properly, then it should be okay, right?
In addition to “no need to free”, what other benefit to use sprintf other than xasprintf?

Qing
> 
>> +		  if (strcmp (lhs_var_name_str, var_name_str) == 0)
>> +		    {
>> +		      free (lhs_var_name_str);
>> +		      return;
>> +		    }
>> +		  free (lhs_var_name_str);
>> +		}
>> +	    }
>> 	  gcc_assert (var_name_str && var_def_stmt);
>> 	}
>>     }
>> -- 
>> 2.31.1
> 
> 	Jakub
  
Jakub Jelinek Feb. 20, 2023, 3:17 p.m. UTC | #3
On Mon, Feb 20, 2023 at 03:04:51PM +0000, Qing Zhao via Gcc-patches wrote:
> 
> 
> > On Feb 17, 2023, at 5:35 PM, Jakub Jelinek <jakub@redhat.com> wrote:
> > 
> > On Fri, Feb 17, 2023 at 10:26:03PM +0000, Qing Zhao via Gcc-patches wrote:
> >> +	      else if (!DECL_NAME (lhs_var))
> >> +		{
> >> +		  char *lhs_var_name_str
> >> +		    = xasprintf ("D.%u", DECL_UID (lhs_var));
> > 
> > Why xasprintf?
> 
> Just emulated the code in “gimple_add_init_for_auto_var” without thinking too much. -:)
> >  D.%u can be sprintfed into a fixed size automatic buffer,
> > say 3 + (HOST_BITS_PER_INT + 2) / 3 would be a good upper bound for the size
> > of the buffer.  Then you don't need to free it...
> 
> xasprintf is "like a sprintf but provided a pointer to malloc’d storage (without fail)”. If free the pointer properly, then it should be okay, right?
> In addition to “no need to free”, what other benefit to use sprintf other than xasprintf?

xasprintf+free being significantly slower, exactly because it needs to
malloc and free later, where both are fairly expensive functions.
The glibc asprintf for short strings like the above uses a ~ 200 byte
static buffer, stores in there, later mallocs the needed amount of memory
and copies it there (so again, another waste because the string needs to be
copied around), while for longer it can do perhaps many allocations and
realloc at the end to the right size.
The libiberty function actually performs the printing twice, once without
writing result anywhere to compute size, then malloc, then again into the
malloced buffer.

	Jakub
  
Qing Zhao Feb. 20, 2023, 5:45 p.m. UTC | #4
> On Feb 20, 2023, at 10:17 AM, Jakub Jelinek <jakub@redhat.com> wrote:
> 
> On Mon, Feb 20, 2023 at 03:04:51PM +0000, Qing Zhao via Gcc-patches wrote:
>> 
>> 
>>> On Feb 17, 2023, at 5:35 PM, Jakub Jelinek <jakub@redhat.com> wrote:
>>> 
>>> On Fri, Feb 17, 2023 at 10:26:03PM +0000, Qing Zhao via Gcc-patches wrote:
>>>> +	      else if (!DECL_NAME (lhs_var))
>>>> +		{
>>>> +		  char *lhs_var_name_str
>>>> +		    = xasprintf ("D.%u", DECL_UID (lhs_var));
>>> 
>>> Why xasprintf?
>> 
>> Just emulated the code in “gimple_add_init_for_auto_var” without thinking too much. -:)
>>> D.%u can be sprintfed into a fixed size automatic buffer,
>>> say 3 + (HOST_BITS_PER_INT + 2) / 3 would be a good upper bound for the size
>>> of the buffer.  Then you don't need to free it...
>> 
>> xasprintf is "like a sprintf but provided a pointer to malloc’d storage (without fail)”. If free the pointer properly, then it should be okay, right?
>> In addition to “no need to free”, what other benefit to use sprintf other than xasprintf?
> 
> xasprintf+free being significantly slower, exactly because it needs to
> malloc and free later, where both are fairly expensive functions.
> The glibc asprintf for short strings like the above uses a ~ 200 byte
> static buffer, stores in there, later mallocs the needed amount of memory
> and copies it there (so again, another waste because the string needs to be
> copied around), while for longer it can do perhaps many allocations and
> realloc at the end to the right size.
> The libiberty function actually performs the printing twice, once without
> writing result anywhere to compute size, then malloc, then again into the
> malloced buffer.

Okay, thanks a lot for the info.
I will replace xasprintf with sprintf for this patch.

Qing
> 
> 	Jakub
>
  

Patch

diff --git a/gcc/testsuite/g++.dg/pr107411.C b/gcc/testsuite/g++.dg/pr107411.C
new file mode 100644
index 00000000000..7eefecae4f3
--- /dev/null
+++ b/gcc/testsuite/g++.dg/pr107411.C
@@ -0,0 +1,10 @@ 
+/* { dg-do compile } */
+/* { dg-options "-Werror=uninitialized -ftrivial-auto-var-init=zero"  } */
+int t();
+void f(int);
+
+void j()
+{
+  const int& e = t();
+  f(e);
+}
diff --git a/gcc/tree-ssa-uninit.cc b/gcc/tree-ssa-uninit.cc
index c555cf5cd50..b4218900f66 100644
--- a/gcc/tree-ssa-uninit.cc
+++ b/gcc/tree-ssa-uninit.cc
@@ -224,8 +224,6 @@  warn_uninit (opt_code opt, tree t, tree var, gimple *context,
 	     at alt_reloc = temp.
 	  */
 	  tree lhs_var = NULL_TREE;
-	  tree lhs_var_name = NULL_TREE;
-	  const char *lhs_var_name_str = NULL;
 
 	  /* Get the variable name from the 3rd argument of call.  */
 	  tree var_name = gimple_call_arg (var_def_stmt, 2);
@@ -239,11 +237,26 @@  warn_uninit (opt_code opt, tree t, tree var, gimple *context,
 	      else if (TREE_CODE (gimple_assign_lhs (context)) == SSA_NAME)
 		lhs_var = SSA_NAME_VAR (gimple_assign_lhs (context));
 	    }
-	  if (lhs_var
-	      && (lhs_var_name = DECL_NAME (lhs_var))
-	      && (lhs_var_name_str = IDENTIFIER_POINTER (lhs_var_name))
-	      && (strcmp (lhs_var_name_str, var_name_str) == 0))
-	    return;
+	  if (lhs_var)
+	    {
+	      /* Get the name string for the LHS_VAR.
+		 Refer to routine gimple_add_init_for_auto_var.  */
+	      if (DECL_NAME (lhs_var)
+		  && (strcmp (IDENTIFIER_POINTER (DECL_NAME (lhs_var)),
+		      var_name_str) == 0))
+		return;
+	      else if (!DECL_NAME (lhs_var))
+		{
+		  char *lhs_var_name_str
+		    = xasprintf ("D.%u", DECL_UID (lhs_var));
+		  if (strcmp (lhs_var_name_str, var_name_str) == 0)
+		    {
+		      free (lhs_var_name_str);
+		      return;
+		    }
+		  free (lhs_var_name_str);
+		}
+	    }
 	  gcc_assert (var_name_str && var_def_stmt);
 	}
     }