diff mbox series

[og12] OpenMP: Fix checking ICE in "declare target" ctor/dtor support

Message ID 20230405120833.3598320-1-julian@codesourcery.com
State New
Headers show
Series [og12] OpenMP: Fix checking ICE in "declare target" ctor/dtor support | expand

Commit Message

Julian Brown April 5, 2023, 12:08 p.m. UTC
This patch fixes an ICE with checking enabled with the patch:

  abcb5dbac666513c798e574808f849f76a1c0799

There were two problems: first, OMP_CLAUSE_CHAIN was erroneously
used as the chain pointer instead of TREE_CHAIN for a non-OMP clause
list. Secondly, "copy_node" by itself is not sufficient to clone the
initialization statement for use in the on-target constructor/destructor
function. Instead we now use walk_tree with "copy_tree_body_r" and
appropriate configuration parameters.

Tested with offloading to AMD GCN. I will commit (to og12) shortly.

Julian

2023-04-05  Julian Brown  <julian@codesourcery.com>

gcc/cp/
	* decl2.cc (tree-inline.h): Include.
	(do_static_initialization_or_destruction): Change OMP_TARGET parameter
	to pass the host version of the SSDF function decl.  Use
	copy_tree_body_r to clone init stmt.  Update forward declaration.
	(c_parse_final_cleanups): Update calls to
	do_static_initialization_or_destruction.  Use TREE_CHAIN instead of
	OMP_CLAUSE_CHAIN.
---
 gcc/cp/decl2.cc | 44 ++++++++++++++++++++++++++++++++------------
 1 file changed, 32 insertions(+), 12 deletions(-)
diff mbox series

Patch

diff --git a/gcc/cp/decl2.cc b/gcc/cp/decl2.cc
index 9c007043963..4b4b211eb2a 100644
--- a/gcc/cp/decl2.cc
+++ b/gcc/cp/decl2.cc
@@ -50,6 +50,7 @@  along with GCC; see the file COPYING3.  If not see
 #include "asan.h"
 #include "optabs-query.h"
 #include "omp-general.h"
+#include "tree-inline.h"
 
 /* Id for dumping the raw trees.  */
 int raw_dump_id;
@@ -75,7 +76,7 @@  static void finish_objects (int, int, tree);
 static tree start_static_storage_duration_function (unsigned, bool);
 static void finish_static_storage_duration_function (tree);
 static priority_info get_priority_info (int);
-static void do_static_initialization_or_destruction (tree, bool, bool);
+static void do_static_initialization_or_destruction (tree, bool, tree);
 static void one_static_initialization_or_destruction (tree, tree, bool);
 static void generate_ctor_or_dtor_function (bool, int, location_t *, bool);
 static int generate_ctor_and_dtor_functions_for_priority (splay_tree_node,
@@ -4314,9 +4315,11 @@  one_static_initialization_or_destruction (tree decl, tree init, bool initp)
    Whether initialization or destruction is performed is specified by INITP.  */
 
 static void
-do_static_initialization_or_destruction (tree vars, bool initp, bool omp_target)
+do_static_initialization_or_destruction (tree vars, bool initp,
+					 tree host_ssdf_decl = NULL_TREE)
 {
   tree node, init_if_stmt, cond;
+  bool omp_target = host_ssdf_decl != NULL_TREE;
 
   /* Build the outer if-stmt to check for initialization or destruction.  */
   init_if_stmt = begin_if_stmt ();
@@ -4386,7 +4389,25 @@  do_static_initialization_or_destruction (tree vars, bool initp, bool omp_target)
 	/* We will emit 'init' twice, and it is modified in-place during
 	   gimplification.  Make a copy here.  */
 	if (omp_target)
-	  init = copy_node (init);
+	  {
+	    /* We've already emitted INIT in the host version of the ctor/dtor
+	       function.  We need to deep-copy it (including new versions of
+	       local variables introduced, etc.) for use in the target
+	       ctor/dtor function.  */
+	    copy_body_data id;
+	    hash_map<tree, tree> decl_map;
+	    memset (&id, 0, sizeof (id));
+	    id.src_fn = host_ssdf_decl;
+	    id.dst_fn = current_function_decl;
+	    id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
+	    id.decl_map = &decl_map;
+	    id.copy_decl = copy_decl_no_change;
+	    id.transform_call_graph_edges = CB_CGE_DUPLICATE;
+	    id.transform_new_cfg = true;
+	    id.transform_return_to_modify = false;
+	    id.eh_lp_nr = 0;
+	    walk_tree (&init, copy_tree_body_r, &id, NULL);
+	  }
 	/* Do one initialization or destruction.  */
 	one_static_initialization_or_destruction (decl, init, initp);
       }
@@ -5245,8 +5266,7 @@  c_parse_final_cleanups (void)
 
 	  /* First generate code to do all the initializations.  */
 	  if (vars)
-	    do_static_initialization_or_destruction (vars, /*initp=*/true,
-						     /*omp_target=*/false);
+	    do_static_initialization_or_destruction (vars, /*initp=*/true);
 
 	  tree filtered_vars = NULL_TREE;
 
@@ -5262,12 +5282,13 @@  c_parse_final_cleanups (void)
 	  if (!flag_use_cxa_atexit && vars)
 	    {
 	      vars = nreverse (vars);
-	      do_static_initialization_or_destruction (vars, /*initp=*/false,
-						       /*omp_target=*/false);
+	      do_static_initialization_or_destruction (vars, /*initp=*/false);
 	    }
 	  else
 	    vars = NULL_TREE;
 
+	  tree host_ssdf_decl = current_function_decl;
+
 	  /* Finish up the static storage duration function for this
 	     round.  */
 	  input_location = locus_at_end_of_parsing;
@@ -5286,9 +5307,9 @@  c_parse_final_cleanups (void)
 
 		  if (lookup_attribute ("omp declare target",
 					DECL_ATTRIBUTES (decl)))
-		    fvarsp = &OMP_CLAUSE_CHAIN (*fvarsp);
+		    fvarsp = &TREE_CHAIN (*fvarsp);
 		  else
-		    *fvarsp = OMP_CLAUSE_CHAIN (*fvarsp);
+		    *fvarsp = TREE_CHAIN (*fvarsp);
 		}
 
 	      input_location = locus_at_end_of_parsing;
@@ -5318,14 +5339,13 @@  c_parse_final_cleanups (void)
 
 		  do_static_initialization_or_destruction (filtered_vars,
 							   /*initp=*/true,
-							   /*omp_target=*/true);
+							   host_ssdf_decl);
 		  if (!flag_use_cxa_atexit && filtered_vars)
 		    {
 		      filtered_vars = nreverse (filtered_vars);
 		      do_static_initialization_or_destruction (filtered_vars,
 							       /*initp=*/false,
-							       /*omp_target=*/
-							       false);
+							       host_ssdf_decl);
 		    }
 		  else
 		    filtered_vars = NULL_TREE;