tree-inline: Fix up multiversioning with vector arguments [PR105554]

Message ID ZBSnqPiQgZmSLO29@tucnak
State Unresolved
Headers
Series tree-inline: Fix up multiversioning with vector arguments [PR105554] |

Checks

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

Commit Message

Jakub Jelinek March 17, 2023, 5:47 p.m. UTC
  Hi!

The following testcase ICEs, because we call tree_function_versioning from
old_decl which has target attributes not supporting V4DImode and so
DECL_MODE of DECL_ARGUMENTS is BLKmode, while new_decl supports those.
tree_function_versioning initially copies DECL_RESULT and DECL_ARGUMENTS
from old_decl to new_decl, then calls initialize_cfun to create cfun
and only when the cfun is created it can later actually remap_decl
DECL_RESULT and DECL_ARGUMENTS etc.
The problem is that initialize_cfun -> push_struct_function ->
allocate_struct_function calls relayout_decl on DECL_RESULT and
DECL_ARGUMENTS, which clobbers DECL_MODE of old_decl and we then ICE because
of it.
In particular, allocate_struct_function does:
      if (!abstract_p)
        {
          /* Now that we have activated any function-specific attributes
             that might affect layout, particularly vector modes, relayout
             each of the parameters and the result.  */
          relayout_decl (result);
          for (tree parm = DECL_ARGUMENTS (fndecl); parm;
               parm = DECL_CHAIN (parm))
            relayout_decl (parm);

          /* Similarly relayout the function decl.  */
          targetm.target_option.relayout_function (fndecl);
        }

      if (!abstract_p && aggregate_value_p (result, fndecl))
        {
#ifdef PCC_STATIC_STRUCT_RETURN
          cfun->returns_pcc_struct = 1;
#endif
          cfun->returns_struct = 1;
        }
Now, in the case of tree_function_versioning, I believe all that we need
from these is possibly the
targetm.target_option.relayout_function (fndecl);
call (arm only), we will remap DECL_RESULT and DECL_ARGUMENTS later on
and copy_decl_for_dup_finish in that case will handle all we need:
  /* For vector typed decls make sure to update DECL_MODE according
     to the new function context.  */
  if (VECTOR_TYPE_P (TREE_TYPE (copy)))
    SET_DECL_MODE (copy, TYPE_MODE (TREE_TYPE (copy)));
We don't need the cfun->returns_*struct either, because we override it
in initialize_cfun a few lines later:
  /* Copy items we preserve during cloning.  */
...
  cfun->returns_struct = src_cfun->returns_struct;
  cfun->returns_pcc_struct = src_cfun->returns_pcc_struct;

So, to avoid the clobbering of DECL_RESULT/DECL_ARGUMENTS of old_decl,
the following patch arranges allocate_struct_function to be called with
abstract_p true and calls targetm.target_option.relayout_function (fndecl);
by hand.

The removal of DECL_RESULT/DECL_ARGUMENTS copying at the start of
initialize_cfun is removed because the only caller -
tree_function_versioning, does that unconditionally before.

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

2023-03-17  Jakub Jelinek  <jakub@redhat.com>

	PR target/105554
	* function.h (push_struct_function): Add ABSTRACT_P argument defaulted
	to false.
	* function.cc (push_struct_function): Add ABSTRACT_P argument, pass it
	to allocate_struct_function instead of false.
	* tree-inline.cc (initialize_cfun): Don't copy DECL_ARGUMENTS
	nor DECL_RESULT here.  Pass true as ABSTRACT_P to
	push_struct_function.  Call targetm.target_option.relayout_function
	after it.
	(tree_function_versioning): Formatting fix.

	* gcc.target/i386/pr105554.c: New test.


	Jakub
  

Comments

Richard Biener March 17, 2023, 5:52 p.m. UTC | #1
> Am 17.03.2023 um 18:48 schrieb Jakub Jelinek <jakub@redhat.com>:
> 
> Hi!
> 
> The following testcase ICEs, because we call tree_function_versioning from
> old_decl which has target attributes not supporting V4DImode and so
> DECL_MODE of DECL_ARGUMENTS is BLKmode, while new_decl supports those.
> tree_function_versioning initially copies DECL_RESULT and DECL_ARGUMENTS
> from old_decl to new_decl, then calls initialize_cfun to create cfun
> and only when the cfun is created it can later actually remap_decl
> DECL_RESULT and DECL_ARGUMENTS etc.
> The problem is that initialize_cfun -> push_struct_function ->
> allocate_struct_function calls relayout_decl on DECL_RESULT and
> DECL_ARGUMENTS, which clobbers DECL_MODE of old_decl and we then ICE because
> of it.
> In particular, allocate_struct_function does:
>      if (!abstract_p)
>        {
>          /* Now that we have activated any function-specific attributes
>             that might affect layout, particularly vector modes, relayout
>             each of the parameters and the result.  */
>          relayout_decl (result);
>          for (tree parm = DECL_ARGUMENTS (fndecl); parm;
>               parm = DECL_CHAIN (parm))
>            relayout_decl (parm);
> 
>          /* Similarly relayout the function decl.  */
>          targetm.target_option.relayout_function (fndecl);
>        }
> 
>      if (!abstract_p && aggregate_value_p (result, fndecl))
>        {
> #ifdef PCC_STATIC_STRUCT_RETURN
>          cfun->returns_pcc_struct = 1;
> #endif
>          cfun->returns_struct = 1;
>        }
> Now, in the case of tree_function_versioning, I believe all that we need
> from these is possibly the
> targetm.target_option.relayout_function (fndecl);
> call (arm only), we will remap DECL_RESULT and DECL_ARGUMENTS later on
> and copy_decl_for_dup_finish in that case will handle all we need:
>  /* For vector typed decls make sure to update DECL_MODE according
>     to the new function context.  */
>  if (VECTOR_TYPE_P (TREE_TYPE (copy)))
>    SET_DECL_MODE (copy, TYPE_MODE (TREE_TYPE (copy)));
> We don't need the cfun->returns_*struct either, because we override it
> in initialize_cfun a few lines later:
>  /* Copy items we preserve during cloning.  */
> ...
>  cfun->returns_struct = src_cfun->returns_struct;
>  cfun->returns_pcc_struct = src_cfun->returns_pcc_struct;
> 
> So, to avoid the clobbering of DECL_RESULT/DECL_ARGUMENTS of old_decl,
> the following patch arranges allocate_struct_function to be called with
> abstract_p true and calls targetm.target_option.relayout_function (fndecl);
> by hand.
> 
> The removal of DECL_RESULT/DECL_ARGUMENTS copying at the start of
> initialize_cfun is removed because the only caller -
> tree_function_versioning, does that unconditionally before.
> 
> Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

Ok.

Thanks,
Richard 

> 2023-03-17  Jakub Jelinek  <jakub@redhat.com>
> 
>    PR target/105554
>    * function.h (push_struct_function): Add ABSTRACT_P argument defaulted
>    to false.
>    * function.cc (push_struct_function): Add ABSTRACT_P argument, pass it
>    to allocate_struct_function instead of false.
>    * tree-inline.cc (initialize_cfun): Don't copy DECL_ARGUMENTS
>    nor DECL_RESULT here.  Pass true as ABSTRACT_P to
>    push_struct_function.  Call targetm.target_option.relayout_function
>    after it.
>    (tree_function_versioning): Formatting fix.
> 
>    * gcc.target/i386/pr105554.c: New test.
> 
> --- gcc/function.h.jj    2023-01-12 21:04:08.647239183 +0100
> +++ gcc/function.h    2023-03-17 10:47:27.489571469 +0100
> @@ -687,7 +687,7 @@ extern void pop_cfun (void);
> extern int get_next_funcdef_no (void);
> extern int get_last_funcdef_no (void);
> extern void allocate_struct_function (tree, bool);
> -extern void push_struct_function (tree fndecl);
> +extern void push_struct_function (tree fndecl, bool = false);
> extern void push_dummy_function (bool);
> extern void pop_dummy_function (void);
> extern void init_dummy_function_start (void);
> --- gcc/function.cc.jj    2023-01-14 10:00:20.308560059 +0100
> +++ gcc/function.cc    2023-03-17 10:48:10.009945483 +0100
> @@ -4891,7 +4891,7 @@ allocate_struct_function (tree fndecl, b
>    instead of just setting it.  */
> 
> void
> -push_struct_function (tree fndecl)
> +push_struct_function (tree fndecl, bool abstract_p)
> {
>   /* When in_dummy_function we might be in the middle of a pop_cfun and
>      current_function_decl and cfun may not match.  */
> @@ -4900,7 +4900,7 @@ push_struct_function (tree fndecl)
>          || (cfun && current_function_decl == cfun->decl));
>   cfun_stack.safe_push (cfun);
>   current_function_decl = fndecl;
> -  allocate_struct_function (fndecl, false);
> +  allocate_struct_function (fndecl, abstract_p);
> }
> 
> /* Reset crtl and other non-struct-function variables to defaults as
> --- gcc/tree-inline.cc.jj    2023-01-19 09:58:51.037226790 +0100
> +++ gcc/tree-inline.cc    2023-03-17 10:56:50.321285330 +0100
> @@ -2781,16 +2781,12 @@ initialize_cfun (tree new_fndecl, tree c
> {
>   struct function *src_cfun = DECL_STRUCT_FUNCTION (callee_fndecl);
> 
> -  if (!DECL_ARGUMENTS (new_fndecl))
> -    DECL_ARGUMENTS (new_fndecl) = DECL_ARGUMENTS (callee_fndecl);
> -  if (!DECL_RESULT (new_fndecl))
> -    DECL_RESULT (new_fndecl) = DECL_RESULT (callee_fndecl);
> -
>   /* Register specific tree functions.  */
>   gimple_register_cfg_hooks ();
> 
>   /* Get clean struct function.  */
> -  push_struct_function (new_fndecl);
> +  push_struct_function (new_fndecl, true);
> +  targetm.target_option.relayout_function (new_fndecl);
> 
>   /* We will rebuild these, so just sanity check that they are empty.  */
>   gcc_assert (VALUE_HISTOGRAMS (cfun) == NULL);
> @@ -6235,8 +6231,7 @@ tree_function_versioning (tree old_decl,
>   id.transform_return_to_modify = false;
>   id.transform_parameter = false;
> 
> -  old_entry_block = ENTRY_BLOCK_PTR_FOR_FN
> -    (DECL_STRUCT_FUNCTION (old_decl));
> +  old_entry_block = ENTRY_BLOCK_PTR_FOR_FN (DECL_STRUCT_FUNCTION (old_decl));
>   DECL_RESULT (new_decl) = DECL_RESULT (old_decl);
>   DECL_ARGUMENTS (new_decl) = DECL_ARGUMENTS (old_decl);
>   initialize_cfun (new_decl, old_decl,
> --- gcc/testsuite/gcc.target/i386/pr105554.c.jj    2023-03-17 10:11:38.998223934 +0100
> +++ gcc/testsuite/gcc.target/i386/pr105554.c    2023-03-17 10:11:38.998223934 +0100
> @@ -0,0 +1,10 @@
> +/* PR target/105554 */
> +/* { dg-do compile } */
> +/* { dg-options "-O2 -Wno-psabi -mno-sse3" } */
> +
> +typedef long long v4di __attribute__((__vector_size__(32)));
> +
> +__attribute__((target_clones ("arch=core-avx2", "default"))) void
> +foo (v4di x)
> +{
> +}
> 
>    Jakub
>
  

Patch

--- gcc/function.h.jj	2023-01-12 21:04:08.647239183 +0100
+++ gcc/function.h	2023-03-17 10:47:27.489571469 +0100
@@ -687,7 +687,7 @@  extern void pop_cfun (void);
 extern int get_next_funcdef_no (void);
 extern int get_last_funcdef_no (void);
 extern void allocate_struct_function (tree, bool);
-extern void push_struct_function (tree fndecl);
+extern void push_struct_function (tree fndecl, bool = false);
 extern void push_dummy_function (bool);
 extern void pop_dummy_function (void);
 extern void init_dummy_function_start (void);
--- gcc/function.cc.jj	2023-01-14 10:00:20.308560059 +0100
+++ gcc/function.cc	2023-03-17 10:48:10.009945483 +0100
@@ -4891,7 +4891,7 @@  allocate_struct_function (tree fndecl, b
    instead of just setting it.  */
 
 void
-push_struct_function (tree fndecl)
+push_struct_function (tree fndecl, bool abstract_p)
 {
   /* When in_dummy_function we might be in the middle of a pop_cfun and
      current_function_decl and cfun may not match.  */
@@ -4900,7 +4900,7 @@  push_struct_function (tree fndecl)
 	      || (cfun && current_function_decl == cfun->decl));
   cfun_stack.safe_push (cfun);
   current_function_decl = fndecl;
-  allocate_struct_function (fndecl, false);
+  allocate_struct_function (fndecl, abstract_p);
 }
 
 /* Reset crtl and other non-struct-function variables to defaults as
--- gcc/tree-inline.cc.jj	2023-01-19 09:58:51.037226790 +0100
+++ gcc/tree-inline.cc	2023-03-17 10:56:50.321285330 +0100
@@ -2781,16 +2781,12 @@  initialize_cfun (tree new_fndecl, tree c
 {
   struct function *src_cfun = DECL_STRUCT_FUNCTION (callee_fndecl);
 
-  if (!DECL_ARGUMENTS (new_fndecl))
-    DECL_ARGUMENTS (new_fndecl) = DECL_ARGUMENTS (callee_fndecl);
-  if (!DECL_RESULT (new_fndecl))
-    DECL_RESULT (new_fndecl) = DECL_RESULT (callee_fndecl);
-
   /* Register specific tree functions.  */
   gimple_register_cfg_hooks ();
 
   /* Get clean struct function.  */
-  push_struct_function (new_fndecl);
+  push_struct_function (new_fndecl, true);
+  targetm.target_option.relayout_function (new_fndecl);
 
   /* We will rebuild these, so just sanity check that they are empty.  */
   gcc_assert (VALUE_HISTOGRAMS (cfun) == NULL);
@@ -6235,8 +6231,7 @@  tree_function_versioning (tree old_decl,
   id.transform_return_to_modify = false;
   id.transform_parameter = false;
 
-  old_entry_block = ENTRY_BLOCK_PTR_FOR_FN
-    (DECL_STRUCT_FUNCTION (old_decl));
+  old_entry_block = ENTRY_BLOCK_PTR_FOR_FN (DECL_STRUCT_FUNCTION (old_decl));
   DECL_RESULT (new_decl) = DECL_RESULT (old_decl);
   DECL_ARGUMENTS (new_decl) = DECL_ARGUMENTS (old_decl);
   initialize_cfun (new_decl, old_decl,
--- gcc/testsuite/gcc.target/i386/pr105554.c.jj	2023-03-17 10:11:38.998223934 +0100
+++ gcc/testsuite/gcc.target/i386/pr105554.c	2023-03-17 10:11:38.998223934 +0100
@@ -0,0 +1,10 @@ 
+/* PR target/105554 */
+/* { dg-do compile } */
+/* { dg-options "-O2 -Wno-psabi -mno-sse3" } */
+
+typedef long long v4di __attribute__((__vector_size__(32)));
+
+__attribute__((target_clones ("arch=core-avx2", "default"))) void
+foo (v4di x)
+{
+}