public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] c++: Fix cp_genericize_target_expr for TARGET_EXPRs created for global initialization [PR104031]
@ 2022-01-17  8:56 Jakub Jelinek
  2022-01-17 17:01 ` Jason Merrill
  0 siblings, 1 reply; 2+ messages in thread
From: Jakub Jelinek @ 2022-01-17  8:56 UTC (permalink / raw)
  To: Jason Merrill; +Cc: gcc-patches

Hi!

The following patch is miscompiled, cp_genericize_target_expr expects
that for the constant part split_nonconstant_init will emit an INIT_EXPR
that will initialize it, but that doesn't happen and instead we get
DECL_INITIAL on the TARGET_EXPR_SLOT that isn't initialized anywhere
in the IL.

The problem is that the TARGET_EXPR has been created while
current_function_decl was NULL, it is inside a global var initializer.
That means the build_local_temp created VAR_DECL has NULL DECL_CONTEXT.
Later on when genericizing the ssdf (current_function_decl is already
non-NULL), the new cp_genericize_target_expr is called and during
split_nonconstant_init it checks is_local_temp, but that due to the NULL
DECL_CONTEXT returns false.  DECL_CONTEXT is set only later on during
gimplification.

The following patch fixes it by setting DECL_CONTEXT also inside of
cp_genericize_target_expr, which fixes the testcase.  But if there are
better spots to do that, please let me know...

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

2022-01-17  Jakub Jelinek  <jakub@redhat.com>

	PR c++/104031
	* cp-gimplify.c (cp_genericize_target_expr): Set DECL_CONTEXT of
	TARGET_EXPR_SLOT to current_function_decl if it was NULL.

	* g++.dg/cpp1y/pr104031.C: New test.

--- gcc/cp/cp-gimplify.c.jj	2022-01-14 12:09:13.003250834 +0100
+++ gcc/cp/cp-gimplify.c	2022-01-16 21:34:52.300937996 +0100
@@ -930,6 +930,11 @@ cp_genericize_init_expr (tree *stmt_p)
 static void
 cp_genericize_target_expr (tree *stmt_p)
 {
+  /* If TARGET_EXPR is created for some global var initializer, the slot
+     will have NULL and so is_local_temp will return false for it.  If
+     this is a ssdf, set DECL_CONTEXT now.  */
+  if (DECL_CONTEXT (TARGET_EXPR_SLOT (*stmt_p)) == NULL_TREE)
+    DECL_CONTEXT (TARGET_EXPR_SLOT (*stmt_p)) = current_function_decl;
   cp_genericize_init (&TARGET_EXPR_INITIAL (*stmt_p),
 		      TARGET_EXPR_INITIAL (*stmt_p),
 		      TARGET_EXPR_SLOT (*stmt_p));
--- gcc/testsuite/g++.dg/cpp1y/pr104031.C.jj	2022-01-16 21:30:14.977838079 +0100
+++ gcc/testsuite/g++.dg/cpp1y/pr104031.C	2022-01-16 21:30:46.028401294 +0100
@@ -0,0 +1,23 @@
+// PR c++/104031
+// { dg-do run { target c++14 } }
+// { dg-options "-O2" }
+
+struct A {
+  A () {}
+  ~A () {}
+};
+struct B {
+  A a;
+  int b = 0;
+};
+struct C
+{
+  [[gnu::noipa]]
+  C (B x) { if (x.b != 42) __builtin_abort (); }
+};
+static C c ({ .a = A{}, .b = 42 });
+
+int
+main ()
+{
+}

	Jakub


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] c++: Fix cp_genericize_target_expr for TARGET_EXPRs created for global initialization [PR104031]
  2022-01-17  8:56 [PATCH] c++: Fix cp_genericize_target_expr for TARGET_EXPRs created for global initialization [PR104031] Jakub Jelinek
@ 2022-01-17 17:01 ` Jason Merrill
  0 siblings, 0 replies; 2+ messages in thread
From: Jason Merrill @ 2022-01-17 17:01 UTC (permalink / raw)
  To: Jakub Jelinek; +Cc: gcc-patches

On 1/17/22 03:56, Jakub Jelinek wrote:
> Hi!
> 
> The following patch is miscompiled, cp_genericize_target_expr expects
> that for the constant part split_nonconstant_init will emit an INIT_EXPR
> that will initialize it, but that doesn't happen and instead we get
> DECL_INITIAL on the TARGET_EXPR_SLOT that isn't initialized anywhere
> in the IL.
> 
> The problem is that the TARGET_EXPR has been created while
> current_function_decl was NULL, it is inside a global var initializer.
> That means the build_local_temp created VAR_DECL has NULL DECL_CONTEXT.
> Later on when genericizing the ssdf (current_function_decl is already
> non-NULL), the new cp_genericize_target_expr is called and during
> split_nonconstant_init it checks is_local_temp, but that due to the NULL
> DECL_CONTEXT returns false.  DECL_CONTEXT is set only later on during
> gimplification.
> 
> The following patch fixes it by setting DECL_CONTEXT also inside of
> cp_genericize_target_expr, which fixes the testcase.  But if there are
> better spots to do that, please let me know...
> 
> Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?
> 
> 2022-01-17  Jakub Jelinek  <jakub@redhat.com>
> 
> 	PR c++/104031
> 	* cp-gimplify.c (cp_genericize_target_expr): Set DECL_CONTEXT of
> 	TARGET_EXPR_SLOT to current_function_decl if it was NULL.
> 
> 	* g++.dg/cpp1y/pr104031.C: New test.
> 
> --- gcc/cp/cp-gimplify.c.jj	2022-01-14 12:09:13.003250834 +0100
> +++ gcc/cp/cp-gimplify.c	2022-01-16 21:34:52.300937996 +0100
> @@ -930,6 +930,11 @@ cp_genericize_init_expr (tree *stmt_p)
>   static void
>   cp_genericize_target_expr (tree *stmt_p)
>   {
> +  /* If TARGET_EXPR is created for some global var initializer, the slot
> +     will have NULL and so is_local_temp will return false for it.  If
> +     this is a ssdf, set DECL_CONTEXT now.  */
> +  if (DECL_CONTEXT (TARGET_EXPR_SLOT (*stmt_p)) == NULL_TREE)
> +    DECL_CONTEXT (TARGET_EXPR_SLOT (*stmt_p)) = current_function_decl;
>     cp_genericize_init (&TARGET_EXPR_INITIAL (*stmt_p),
>   		      TARGET_EXPR_INITIAL (*stmt_p),
>   		      TARGET_EXPR_SLOT (*stmt_p));

We could use a local variable for TARGET_EXPR_SLOT (*stmt_p) instead of 
repeating it.  OK either way.

> --- gcc/testsuite/g++.dg/cpp1y/pr104031.C.jj	2022-01-16 21:30:14.977838079 +0100
> +++ gcc/testsuite/g++.dg/cpp1y/pr104031.C	2022-01-16 21:30:46.028401294 +0100
> @@ -0,0 +1,23 @@
> +// PR c++/104031
> +// { dg-do run { target c++14 } }
> +// { dg-options "-O2" }
> +
> +struct A {
> +  A () {}
> +  ~A () {}
> +};
> +struct B {
> +  A a;
> +  int b = 0;
> +};
> +struct C
> +{
> +  [[gnu::noipa]]
> +  C (B x) { if (x.b != 42) __builtin_abort (); }
> +};
> +static C c ({ .a = A{}, .b = 42 });
> +
> +int
> +main ()
> +{
> +}
> 
> 	Jakub
> 


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-01-17 17:01 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-17  8:56 [PATCH] c++: Fix cp_genericize_target_expr for TARGET_EXPRs created for global initialization [PR104031] Jakub Jelinek
2022-01-17 17:01 ` Jason Merrill

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).