c++: Don't clear TREE_READONLY for -fmerge-all-constants for non-aggregates [PR107558]

Message ID Y3810xiZcwOyI+7f@tucnak
State Unresolved
Headers
Series c++: Don't clear TREE_READONLY for -fmerge-all-constants for non-aggregates [PR107558] |

Checks

Context Check Description
snail/gcc-patch-check warning Git am fail log

Commit Message

Jakub Jelinek Nov. 24, 2022, 9:13 a.m. UTC
  Hi!

The following testcase ICEs, because OpenMP lowering for shared clause
on l variable with REFERENCE_TYPE creates POINTER_TYPE to REFERENCE_TYPE.
The reason is that the automatic variable has non-trivial construction
(reference to a lambda) and -fmerge-all-constants is on and so TREE_READONLY
isn't set - omp-low will handle automatic TREE_READONLY vars in shared
specially and only copy to the construct and not back, while !TREE_READONLY
are assumed to be changeable.
The PR91529 change rationale was that the gimplification can change
some non-addressable automatic variables to TREE_STATIC with
-fmerge-all-constants and therefore TREE_READONLY on them is undesirable.
But, the gimplifier does that only for aggregate variables:
  switch (TREE_CODE (type))
    {      
    case RECORD_TYPE:
    case UNION_TYPE:
    case QUAL_UNION_TYPE:
    case ARRAY_TYPE:
and not for anything else.  So, I think clearing TREE_READONLY for
automatic integral or reference or pointer etc. vars for
-fmerge-all-constants only is unnecessary.

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

2022-11-24  Jakub Jelinek  <jakub@redhat.com>

	PR c++/107558
	* decl.cc (cp_finish_decl): Don't clear TREE_READONLY on
	automatic non-aggregate variables just because of
	-fmerge-all-constants.

	* g++.dg/gomp/pr107558.C: New test.


	Jakub
  

Comments

Jakub Jelinek March 10, 2023, 9:02 a.m. UTC | #1
Hi!

I'd like to ping this patch:

https://gcc.gnu.org/pipermail/gcc-patches/2022-November/607145.html
  - PR107558 - P2 - c++: Don't clear TREE_READONLY for -fmerge-all-constants for non-aggregates

Thanks
	Jakub

On Thu, Nov 24, 2022 at 10:13:55AM +0100, Jakub Jelinek via Gcc-patches wrote:
> The following testcase ICEs, because OpenMP lowering for shared clause
> on l variable with REFERENCE_TYPE creates POINTER_TYPE to REFERENCE_TYPE.
> The reason is that the automatic variable has non-trivial construction
> (reference to a lambda) and -fmerge-all-constants is on and so TREE_READONLY
> isn't set - omp-low will handle automatic TREE_READONLY vars in shared
> specially and only copy to the construct and not back, while !TREE_READONLY
> are assumed to be changeable.
> The PR91529 change rationale was that the gimplification can change
> some non-addressable automatic variables to TREE_STATIC with
> -fmerge-all-constants and therefore TREE_READONLY on them is undesirable.
> But, the gimplifier does that only for aggregate variables:
>   switch (TREE_CODE (type))
>     {      
>     case RECORD_TYPE:
>     case UNION_TYPE:
>     case QUAL_UNION_TYPE:
>     case ARRAY_TYPE:
> and not for anything else.  So, I think clearing TREE_READONLY for
> automatic integral or reference or pointer etc. vars for
> -fmerge-all-constants only is unnecessary.
> 
> Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?
> 
> 2022-11-24  Jakub Jelinek  <jakub@redhat.com>
> 
> 	PR c++/107558
> 	* decl.cc (cp_finish_decl): Don't clear TREE_READONLY on
> 	automatic non-aggregate variables just because of
> 	-fmerge-all-constants.
> 
> 	* g++.dg/gomp/pr107558.C: New test.
> 
> --- gcc/cp/decl.cc.jj	2022-11-19 09:21:14.662439877 +0100
> +++ gcc/cp/decl.cc	2022-11-23 13:12:31.866553152 +0100
> @@ -8679,8 +8679,10 @@ cp_finish_decl (tree decl, tree init, bo
>  
>        if (var_definition_p
>  	  /* With -fmerge-all-constants, gimplify_init_constructor
> -	     might add TREE_STATIC to the variable.  */
> -	  && (TREE_STATIC (decl) || flag_merge_constants >= 2))
> +	     might add TREE_STATIC to aggregate variables.  */
> +	  && (TREE_STATIC (decl)
> +	      || (flag_merge_constants >= 2
> +		  && AGGREGATE_TYPE_P (type))))
>  	{
>  	  /* If a TREE_READONLY variable needs initialization
>  	     at runtime, it is no longer readonly and we need to
> --- gcc/testsuite/g++.dg/gomp/pr107558.C.jj	2022-11-23 13:13:27.260736525 +0100
> +++ gcc/testsuite/g++.dg/gomp/pr107558.C	2022-11-23 13:15:22.271041005 +0100
> @@ -0,0 +1,14 @@
> +// PR c++/107558
> +// { dg-do compile { target c++11 } }
> +// { dg-additional-options "-fmerge-all-constants" }
> +// { dg-additional-options "-flto" { target lto } }
> +
> +int a = 15;
> +
> +void
> +foo ()
> +{
> +  auto &&l = [&]() { return a; };
> +#pragma omp target parallel
> +  l ();
> +}
  
Jason Merrill March 10, 2023, 6:36 p.m. UTC | #2
On 11/24/22 04:13, Jakub Jelinek wrote:
> Hi!
> 
> The following testcase ICEs, because OpenMP lowering for shared clause
> on l variable with REFERENCE_TYPE creates POINTER_TYPE to REFERENCE_TYPE.
> The reason is that the automatic variable has non-trivial construction
> (reference to a lambda) and -fmerge-all-constants is on and so TREE_READONLY
> isn't set - omp-low will handle automatic TREE_READONLY vars in shared
> specially and only copy to the construct and not back, while !TREE_READONLY
> are assumed to be changeable.
> The PR91529 change rationale was that the gimplification can change
> some non-addressable automatic variables to TREE_STATIC with
> -fmerge-all-constants and therefore TREE_READONLY on them is undesirable.
> But, the gimplifier does that only for aggregate variables:
>    switch (TREE_CODE (type))
>      {
>      case RECORD_TYPE:
>      case UNION_TYPE:
>      case QUAL_UNION_TYPE:
>      case ARRAY_TYPE:
> and not for anything else.  So, I think clearing TREE_READONLY for
> automatic integral or reference or pointer etc. vars for
> -fmerge-all-constants only is unnecessary.
> 
> Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

OK.

> 2022-11-24  Jakub Jelinek  <jakub@redhat.com>
> 
> 	PR c++/107558
> 	* decl.cc (cp_finish_decl): Don't clear TREE_READONLY on
> 	automatic non-aggregate variables just because of
> 	-fmerge-all-constants.
> 
> 	* g++.dg/gomp/pr107558.C: New test.
> 
> --- gcc/cp/decl.cc.jj	2022-11-19 09:21:14.662439877 +0100
> +++ gcc/cp/decl.cc	2022-11-23 13:12:31.866553152 +0100
> @@ -8679,8 +8679,10 @@ cp_finish_decl (tree decl, tree init, bo
>   
>         if (var_definition_p
>   	  /* With -fmerge-all-constants, gimplify_init_constructor
> -	     might add TREE_STATIC to the variable.  */
> -	  && (TREE_STATIC (decl) || flag_merge_constants >= 2))
> +	     might add TREE_STATIC to aggregate variables.  */
> +	  && (TREE_STATIC (decl)
> +	      || (flag_merge_constants >= 2
> +		  && AGGREGATE_TYPE_P (type))))
>   	{
>   	  /* If a TREE_READONLY variable needs initialization
>   	     at runtime, it is no longer readonly and we need to
> --- gcc/testsuite/g++.dg/gomp/pr107558.C.jj	2022-11-23 13:13:27.260736525 +0100
> +++ gcc/testsuite/g++.dg/gomp/pr107558.C	2022-11-23 13:15:22.271041005 +0100
> @@ -0,0 +1,14 @@
> +// PR c++/107558
> +// { dg-do compile { target c++11 } }
> +// { dg-additional-options "-fmerge-all-constants" }
> +// { dg-additional-options "-flto" { target lto } }
> +
> +int a = 15;
> +
> +void
> +foo ()
> +{
> +  auto &&l = [&]() { return a; };
> +#pragma omp target parallel
> +  l ();
> +}
> 
> 	Jakub
>
  

Patch

--- gcc/cp/decl.cc.jj	2022-11-19 09:21:14.662439877 +0100
+++ gcc/cp/decl.cc	2022-11-23 13:12:31.866553152 +0100
@@ -8679,8 +8679,10 @@  cp_finish_decl (tree decl, tree init, bo
 
       if (var_definition_p
 	  /* With -fmerge-all-constants, gimplify_init_constructor
-	     might add TREE_STATIC to the variable.  */
-	  && (TREE_STATIC (decl) || flag_merge_constants >= 2))
+	     might add TREE_STATIC to aggregate variables.  */
+	  && (TREE_STATIC (decl)
+	      || (flag_merge_constants >= 2
+		  && AGGREGATE_TYPE_P (type))))
 	{
 	  /* If a TREE_READONLY variable needs initialization
 	     at runtime, it is no longer readonly and we need to
--- gcc/testsuite/g++.dg/gomp/pr107558.C.jj	2022-11-23 13:13:27.260736525 +0100
+++ gcc/testsuite/g++.dg/gomp/pr107558.C	2022-11-23 13:15:22.271041005 +0100
@@ -0,0 +1,14 @@ 
+// PR c++/107558
+// { dg-do compile { target c++11 } }
+// { dg-additional-options "-fmerge-all-constants" }
+// { dg-additional-options "-flto" { target lto } }
+
+int a = 15;
+
+void
+foo ()
+{
+  auto &&l = [&]() { return a; };
+#pragma omp target parallel
+  l ();
+}