diff mbox series

Fix PR middle-end/116933

Message ID 9767203.eNJFYEL58v@fomalhaut
State New
Headers show
Series Fix PR middle-end/116933 | expand

Commit Message

Eric Botcazou Oct. 5, 2024, 8:21 a.m. UTC
Hi,

this polishes a few rough edges that prevent -ftrivial-auto-var-init=zero from 
working in Ada:

  - build_common_builtin_nodes declares BUILT_IN_CLEAR_PADDING with 3 instead 
of 2 parameters, now gimple_fold_builtin_clear_padding contains the assertion:

  gcc_assert (gimple_call_num_args (stmt) == 2)

This causes gimple_builtin_call_types_compatible_p to always return false in 
Ada (this works in C/C++ because yet another declaration is used AFAICS).

  - gimple_add_init_for_auto_var uses EXPR_LOCATION to fetch the location of a 
DECL notes, which always returns UNKNOWN_LOCATION.

  - the machinery attempts to initialize Out parameters, which is problematic.

Tested on x86-64/Linux, OK for the mainline?


2024-10-05  Eric Botcazou  <ebotcazou@adacore.com>

	PR middle-end/116933
	* gimplify.cc (gimple_add_init_for_auto_var): Use the correct macro
	to fetch the source location of the variable.
	* tree.c (common_builtin_nodes): Remove the 3rd parameter in the
	type of BUILT_IN_CLEAR_PADDING.


2024-10-05  Eric Botcazou  <ebotcazou@adacore.com>

ada/
	PR middle-end/116933
	* gcc-interface/decl.cc (gnat_to_gnu_entity) <E_Out_Parameter>: Add
	the "uninitialized" attribute on Out parameters.
	* gcc-interface/utils.cc (gnat_internal_attributes): Add entry for
	the "uninitialized" attribute.
	(handle_uninitialized_attribute): New function.


2024-10-05  Eric Botcazou  <ebotcazou@adacore.com>

testsuite/
	* gnat.dg/auto_var_init.adb: New test.
diff mbox series

Patch

diff --git a/gcc/ada/gcc-interface/decl.cc b/gcc/ada/gcc-interface/decl.cc
index 4252e627b0c..880eaff8d0b 100644
--- a/gcc/ada/gcc-interface/decl.cc
+++ b/gcc/ada/gcc-interface/decl.cc
@@ -1563,6 +1563,13 @@  gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, bool definition)
 	  prepend_one_attribute_pragma (&attr_list,
 					Linker_Section_Pragma (gnat_entity));
 
+	/* Do not initialize Out parameters with -ftrivial-auto-var-init.  */
+	if (kind == E_Out_Parameter)
+	  prepend_one_attribute
+	    (&attr_list, ATTR_MACHINE_ATTRIBUTE,
+	     get_identifier ("uninitialized"), NULL_TREE,
+	     gnat_entity);
+
 	/* Now create the variable or the constant and set various flags.  */
 	gnu_decl
 	  = create_var_decl (gnu_entity_name, gnu_ext_name, gnu_type,
diff --git a/gcc/ada/gcc-interface/utils.cc b/gcc/ada/gcc-interface/utils.cc
index 60f36b1e50d..a88a23860d3 100644
--- a/gcc/ada/gcc-interface/utils.cc
+++ b/gcc/ada/gcc-interface/utils.cc
@@ -107,6 +107,7 @@  static tree handle_malloc_attribute (tree *, tree, tree, int, bool *);
 static tree handle_type_generic_attribute (tree *, tree, tree, int, bool *);
 static tree handle_flatten_attribute (tree *, tree, tree, int, bool *);
 static tree handle_used_attribute (tree *, tree, tree, int, bool *);
+static tree handle_uninitialized_attribute (tree *, tree, tree, int, bool *);
 static tree handle_cold_attribute (tree *, tree, tree, int, bool *);
 static tree handle_hot_attribute (tree *, tree, tree, int, bool *);
 static tree handle_simd_attribute (tree *, tree, tree, int, bool *);
@@ -214,6 +215,8 @@  static const attribute_spec gnat_internal_attributes[] =
     handle_flatten_attribute, NULL },
   { "used",         0, 0,  true,  false, false, false,
     handle_used_attribute, NULL },
+  { "uninitialized",0, 0,  true,  false, false, false,
+    handle_uninitialized_attribute, NULL },
   { "cold",         0, 0,  true,  false, false, false,
     handle_cold_attribute, attr_cold_hot_exclusions },
   { "hot",          0, 0,  true,  false, false, false,
@@ -7171,6 +7174,30 @@  handle_used_attribute (tree *pnode, tree name, tree ARG_UNUSED (args),
   return NULL_TREE;
 }
 
+/* Handle an "uninitialized" attribute; arguments as in
+   struct attribute_spec.handler.  */
+
+static tree
+handle_uninitialized_attribute (tree *node, tree name, tree ARG_UNUSED (args),
+				int ARG_UNUSED (flags), bool *no_add_attrs)
+{
+  tree decl = *node;
+  if (!VAR_P (decl))
+    {
+      warning (OPT_Wattributes, "%qE attribute ignored because %qD "
+	       "is not a variable", name, decl);
+      *no_add_attrs = true;
+    }
+  else if (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
+    {
+      warning (OPT_Wattributes, "%qE attribute ignored because %qD "
+	       "is not a local variable", name, decl);
+      *no_add_attrs = true;
+    }
+
+  return NULL_TREE;
+}
+
 /* Handle a "cold" and attribute; arguments as in
    struct attribute_spec.handler.  */
 
diff --git a/gcc/gimplify.cc b/gcc/gimplify.cc
index ceb53e5d5bb..935f1251846 100644
--- a/gcc/gimplify.cc
+++ b/gcc/gimplify.cc
@@ -1993,17 +1993,14 @@  gimple_add_init_for_auto_var (tree decl,
 {
   gcc_assert (auto_var_p (decl));
   gcc_assert (init_type > AUTO_INIT_UNINITIALIZED);
-  location_t loc = EXPR_LOCATION (decl);
-  tree decl_size = TYPE_SIZE_UNIT (TREE_TYPE (decl));
 
-  tree init_type_node
-    = build_int_cst (integer_type_node, (int) init_type);
+  const location_t loc = DECL_SOURCE_LOCATION (decl);
+  tree decl_size = TYPE_SIZE_UNIT (TREE_TYPE (decl));
+  tree init_type_node = build_int_cst (integer_type_node, (int) init_type);
+  tree decl_name;
 
-  tree decl_name = NULL_TREE;
   if (DECL_NAME (decl))
-
     decl_name = build_string_literal (DECL_NAME (decl));
-
   else
     {
       char decl_name_anonymous[3 + (HOST_BITS_PER_INT + 2) / 3];
diff --git a/gcc/tree.cc b/gcc/tree.cc
index bc50afca9a3..095c02c5474 100644
--- a/gcc/tree.cc
+++ b/gcc/tree.cc
@@ -9848,7 +9848,6 @@  build_common_builtin_nodes (void)
       ftype = build_function_type_list (void_type_node,
 					ptr_type_node,
 					ptr_type_node,
-					integer_type_node,
 					NULL_TREE);
       local_define_builtin ("__builtin_clear_padding", ftype,
 			    BUILT_IN_CLEAR_PADDING,