public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* Updated musttail patchkit
@ 2024-06-02 17:16 Andi Kleen
  2024-06-02 17:16 ` [PATCH v7 1/9] Improve must tail in RTL backend Andi Kleen
                   ` (8 more replies)
  0 siblings, 9 replies; 23+ messages in thread
From: Andi Kleen @ 2024-06-02 17:16 UTC (permalink / raw)
  To: gcc-patches; +Cc: richard.guenther, nathan, josmyers, richard.sandiford, jason

This version fixes C++ template propagation of musttail, avoids ICEs for
ARM (and probably some other targets) generation of -O0 tail calls,
and improves the error messages in tree-musttail again, as well as
the documentation.

I bootstrapped/tested it on x86_64-linux, and checked the musttail tests
work on arm and riscv targets.

-O0 is still not as good as clang (e.g. it doesn't handle struct
returns), but I believe it's good enough for now to be usable.


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

* [PATCH v7 1/9] Improve must tail in RTL backend
  2024-06-02 17:16 Updated musttail patchkit Andi Kleen
@ 2024-06-02 17:16 ` Andi Kleen
  2024-06-10  0:55   ` [PING] " Andi Kleen
  2024-06-02 17:16 ` [PATCH v7 2/9] Fix pro_and_epilogue for sibcalls at -O0 Andi Kleen
                   ` (7 subsequent siblings)
  8 siblings, 1 reply; 23+ messages in thread
From: Andi Kleen @ 2024-06-02 17:16 UTC (permalink / raw)
  To: gcc-patches
  Cc: richard.guenther, nathan, josmyers, richard.sandiford, jason, Andi Kleen

- Give error messages for all causes of non sibling call generation
- When giving error messages clear the musttail flag to avoid ICEs
- Error out when tree-tailcall failed to mark a must-tail call
sibcall. In this case it doesn't know the true reason and only gives
a vague message.

	PR83324

gcc/ChangeLog:

	* calls.cc (expand_call): Fix mustcall implementation.
	(maybe_complain_about_tail_call): Clear must tail flag on error.
---
 gcc/calls.cc | 30 ++++++++++++++++++++++++------
 1 file changed, 24 insertions(+), 6 deletions(-)

diff --git a/gcc/calls.cc b/gcc/calls.cc
index 21d78f9779fe..161e36839654 100644
--- a/gcc/calls.cc
+++ b/gcc/calls.cc
@@ -1249,6 +1249,7 @@ maybe_complain_about_tail_call (tree call_expr, const char *reason)
     return;
 
   error_at (EXPR_LOCATION (call_expr), "cannot tail-call: %s", reason);
+  CALL_EXPR_MUST_TAIL_CALL (call_expr) = 0;
 }
 
 /* Fill in ARGS_SIZE and ARGS array based on the parameters found in
@@ -2650,7 +2651,11 @@ expand_call (tree exp, rtx target, int ignore)
   /* The type of the function being called.  */
   tree fntype;
   bool try_tail_call = CALL_EXPR_TAILCALL (exp);
-  bool must_tail_call = CALL_EXPR_MUST_TAIL_CALL (exp);
+  /* tree-tailcall decided not to do tail calls. Error for the musttail case,
+     unfortunately we don't know the reason so it's fairly vague.
+     When tree-tailcall reported an error it already cleared the flag.  */
+  if (!try_tail_call)
+      maybe_complain_about_tail_call (exp, "other reasons");
   int pass;
 
   /* Register in which non-BLKmode value will be returned,
@@ -3022,10 +3027,21 @@ expand_call (tree exp, rtx target, int ignore)
      pushed these optimizations into -O2.  Don't try if we're already
      expanding a call, as that means we're an argument.  Don't try if
      there's cleanups, as we know there's code to follow the call.  */
-  if (currently_expanding_call++ != 0
-      || (!flag_optimize_sibling_calls && !CALL_FROM_THUNK_P (exp))
-      || args_size.var
-      || dbg_cnt (tail_call) == false)
+  if (currently_expanding_call++ != 0)
+    {
+      maybe_complain_about_tail_call (exp, "inside another call");
+      try_tail_call = 0;
+    }
+  if (!flag_optimize_sibling_calls
+	&& !CALL_FROM_THUNK_P (exp)
+	&& !CALL_EXPR_MUST_TAIL_CALL (exp))
+    try_tail_call = 0;
+  if (args_size.var)
+    {
+      maybe_complain_about_tail_call (exp, "variable size arguments");
+      try_tail_call = 0;
+    }
+  if (dbg_cnt (tail_call) == false)
     try_tail_call = 0;
 
   /* Workaround buggy C/C++ wrappers around Fortran routines with
@@ -3046,13 +3062,15 @@ expand_call (tree exp, rtx target, int ignore)
 	    if (MEM_P (*iter))
 	      {
 		try_tail_call = 0;
+		maybe_complain_about_tail_call (exp,
+				"hidden string length argument passed on stack");
 		break;
 	      }
 	}
 
   /* If the user has marked the function as requiring tail-call
      optimization, attempt it.  */
-  if (must_tail_call)
+  if (CALL_EXPR_MUST_TAIL_CALL (exp))
     try_tail_call = 1;
 
   /*  Rest of purposes for tail call optimizations to fail.  */
-- 
2.44.0


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

* [PATCH v7 2/9] Fix pro_and_epilogue for sibcalls at -O0
  2024-06-02 17:16 Updated musttail patchkit Andi Kleen
  2024-06-02 17:16 ` [PATCH v7 1/9] Improve must tail in RTL backend Andi Kleen
@ 2024-06-02 17:16 ` Andi Kleen
  2024-06-02 17:30   ` Andrew Pinski
  2024-06-02 17:16 ` [PATCH v7 3/9] Add a musttail generic attribute to the c-attribs table Andi Kleen
                   ` (6 subsequent siblings)
  8 siblings, 1 reply; 23+ messages in thread
From: Andi Kleen @ 2024-06-02 17:16 UTC (permalink / raw)
  To: gcc-patches
  Cc: richard.guenther, nathan, josmyers, richard.sandiford, jason, Andi Kleen

Some of the cfg fixups in pro_and_epilogue for sibcalls were dependent on "optimize".
Make them check cfun->tail_call_marked instead to handle the -O0 musttail
case. This fixes the musttail test cases on arm targets.

	PR115255

gcc/ChangeLog:

	* function.cc (thread_prologue_and_epilogue_insns): Check
	  cfun->tail_call_marked for sibcalls too.
	(rest_of_handle_thread_prologue_and_epilogue): Dito.
---
 gcc/function.cc | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/gcc/function.cc b/gcc/function.cc
index 4edd4da12474..f949c38b916c 100644
--- a/gcc/function.cc
+++ b/gcc/function.cc
@@ -6261,6 +6261,7 @@ thread_prologue_and_epilogue_insns (void)
   /* Threading the prologue and epilogue changes the artificial refs in the
      entry and exit blocks, and may invalidate DF info for tail calls.  */
   if (optimize
+      || cfun->tail_call_marked
       || flag_optimize_sibling_calls
       || flag_ipa_icf_functions
       || in_lto_p)
@@ -6557,7 +6558,7 @@ rest_of_handle_thread_prologue_and_epilogue (function *fun)
 {
   /* prepare_shrink_wrap is sensitive to the block structure of the control
      flow graph, so clean it up first.  */
-  if (optimize)
+  if (cfun->tail_call_marked || optimize)
     cleanup_cfg (0);
 
   /* On some machines, the prologue and epilogue code, or parts thereof,
@@ -6579,7 +6580,7 @@ rest_of_handle_thread_prologue_and_epilogue (function *fun)
 
   /* Shrink-wrapping can result in unreachable edges in the epilogue,
      see PR57320.  */
-  cleanup_cfg (optimize ? CLEANUP_EXPENSIVE : 0);
+  cleanup_cfg ((cfun->tail_call_marked || optimize) ? CLEANUP_EXPENSIVE : 0);
 
   /* The stack usage info is finalized during prologue expansion.  */
   if (flag_stack_usage_info || flag_callgraph_info)
-- 
2.44.0


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

* [PATCH v7 3/9] Add a musttail generic attribute to the c-attribs table
  2024-06-02 17:16 Updated musttail patchkit Andi Kleen
  2024-06-02 17:16 ` [PATCH v7 1/9] Improve must tail in RTL backend Andi Kleen
  2024-06-02 17:16 ` [PATCH v7 2/9] Fix pro_and_epilogue for sibcalls at -O0 Andi Kleen
@ 2024-06-02 17:16 ` Andi Kleen
  2024-06-02 17:16 ` [PATCH v7 4/9] C++: Support clang compatible [[musttail]] (PR83324) Andi Kleen
                   ` (5 subsequent siblings)
  8 siblings, 0 replies; 23+ messages in thread
From: Andi Kleen @ 2024-06-02 17:16 UTC (permalink / raw)
  To: gcc-patches
  Cc: richard.guenther, nathan, josmyers, richard.sandiford, jason, Andi Kleen

It does nothing currently since statement attributes are handled
directly in the parser.

gcc/c-family/ChangeLog:

	* c-attribs.cc (handle_musttail_attribute): Add.
	* c-common.h (handle_musttail_attribute): Add.
---
 gcc/c-family/c-attribs.cc | 15 +++++++++++++++
 gcc/c-family/c-common.h   |  1 +
 2 files changed, 16 insertions(+)

diff --git a/gcc/c-family/c-attribs.cc b/gcc/c-family/c-attribs.cc
index f9b229aba7fc..5adc7b775eaf 100644
--- a/gcc/c-family/c-attribs.cc
+++ b/gcc/c-family/c-attribs.cc
@@ -340,6 +340,8 @@ const struct attribute_spec c_common_gnu_attributes[] =
   { "common",                 0, 0, true,  false, false, false,
 			      handle_common_attribute,
 	                      attr_common_exclusions },
+  { "musttail",		      0, 0, false, false, false,
+			      false, handle_musttail_attribute, NULL },
   /* FIXME: logically, noreturn attributes should be listed as
      "false, true, true" and apply to function types.  But implementing this
      would require all the places in the compiler that use TREE_THIS_VOLATILE
@@ -1222,6 +1224,19 @@ handle_common_attribute (tree *node, tree name, tree ARG_UNUSED (args),
   return NULL_TREE;
 }
 
+/* Handle a "musttail" attribute; arguments as in
+   struct attribute_spec.handler.  */
+
+tree
+handle_musttail_attribute (tree ARG_UNUSED (*node), tree name, tree ARG_UNUSED (args),
+			   int ARG_UNUSED (flags), bool *no_add_attrs)
+{
+  /* Currently only a statement attribute, handled directly in parser.  */
+  warning (OPT_Wattributes, "%qE attribute ignored", name);
+  *no_add_attrs = true;
+  return NULL_TREE;
+}
+
 /* Handle a "noreturn" attribute; arguments as in
    struct attribute_spec.handler.  */
 
diff --git a/gcc/c-family/c-common.h b/gcc/c-family/c-common.h
index 3e0eed0548b0..f08b92e420b6 100644
--- a/gcc/c-family/c-common.h
+++ b/gcc/c-family/c-common.h
@@ -1638,6 +1638,7 @@ extern tree find_tm_attribute (tree);
 extern const struct attribute_spec::exclusions attr_cold_hot_exclusions[];
 extern const struct attribute_spec::exclusions attr_noreturn_exclusions[];
 extern tree handle_noreturn_attribute (tree *, tree, tree, int, bool *);
+extern tree handle_musttail_attribute (tree *, tree, tree, int, bool *);
 extern bool has_attribute (location_t, tree, tree, tree (*)(tree));
 extern tree build_attr_access_from_parms (tree, bool);
 
-- 
2.44.0


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

* [PATCH v7 4/9] C++: Support clang compatible [[musttail]] (PR83324)
  2024-06-02 17:16 Updated musttail patchkit Andi Kleen
                   ` (2 preceding siblings ...)
  2024-06-02 17:16 ` [PATCH v7 3/9] Add a musttail generic attribute to the c-attribs table Andi Kleen
@ 2024-06-02 17:16 ` Andi Kleen
  2024-06-03 14:42   ` Jason Merrill
  2024-06-02 17:16 ` [PATCH v7 5/9] C: Implement musttail attribute for returns Andi Kleen
                   ` (4 subsequent siblings)
  8 siblings, 1 reply; 23+ messages in thread
From: Andi Kleen @ 2024-06-02 17:16 UTC (permalink / raw)
  To: gcc-patches
  Cc: richard.guenther, nathan, josmyers, richard.sandiford, jason, Andi Kleen

This patch implements a clang compatible [[musttail]] attribute for
returns.

musttail is useful as an alternative to computed goto for interpreters.
With computed goto the interpreter function usually ends up very big
which causes problems with register allocation and other per function
optimizations not scaling. With musttail the interpreter can be instead
written as a sequence of smaller functions that call each other. To
avoid unbounded stack growth this requires forcing a sibling call, which
this attribute does. It guarantees an error if the call cannot be tail
called which allows the programmer to fix it instead of risking a stack
overflow. Unlike computed goto it is also type-safe.

It turns out that David Malcolm had already implemented middle/backend
support for a musttail attribute back in 2016, but it wasn't exposed
to any frontend other than a special plugin.

This patch adds a [[gnu::musttail]] attribute for C++ that can be added
to return statements. The return statement must be a direct call
(it does not follow dependencies), which is similar to what clang
implements. It then uses the existing must tail infrastructure.

For compatibility it also detects clang::musttail

One problem is that tree-tailcall usually fails when optimization
is disabled, which implies the attribute only really works with
optimization on. But that seems to be a reasonable limitation.

Passes bootstrap and full test

	PR83324

gcc/cp/ChangeLog:

	* parser.cc (cp_parser_statement): Handle musttail.
	(cp_parser_jump_statement): Dito.
	(cp_parser_std_attribute): Dito.
	* pt.cc (tsubst_expr): Copy CALL_EXPR_MUST_TAIL_CALL.
---
 gcc/cp/parser.cc | 42 +++++++++++++++++++++++++++++++++++++-----
 gcc/cp/pt.cc     |  4 +++-
 2 files changed, 40 insertions(+), 6 deletions(-)

diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc
index 779625144db4..c2cb304bac7d 100644
--- a/gcc/cp/parser.cc
+++ b/gcc/cp/parser.cc
@@ -2467,7 +2467,7 @@ static tree cp_parser_perform_range_for_lookup
 static tree cp_parser_range_for_member_function
   (tree, tree);
 static tree cp_parser_jump_statement
-  (cp_parser *);
+  (cp_parser *, tree &);
 static void cp_parser_declaration_statement
   (cp_parser *);
 
@@ -12747,13 +12747,17 @@ cp_parser_statement (cp_parser* parser, tree in_statement_expr,
 						     NULL_TREE, false);
 	  break;
 
+	case RID_RETURN:
+	  std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
+	  statement = cp_parser_jump_statement (parser, std_attrs);
+	  break;
+
 	case RID_BREAK:
 	case RID_CONTINUE:
-	case RID_RETURN:
 	case RID_CO_RETURN:
 	case RID_GOTO:
 	  std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
-	  statement = cp_parser_jump_statement (parser);
+	  statement = cp_parser_jump_statement (parser, std_attrs);
 	  break;
 
 	  /* Objective-C++ exception-handling constructs.  */
@@ -14813,10 +14817,11 @@ cp_parser_init_statement (cp_parser *parser, tree *decl)
    jump-statement:
      goto * expression ;
 
+   STD_ATTRS are the statement attributes. They can be modified.
    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
 
 static tree
-cp_parser_jump_statement (cp_parser* parser)
+cp_parser_jump_statement (cp_parser* parser, tree &std_attrs)
 {
   tree statement = error_mark_node;
   cp_token *token;
@@ -14893,6 +14898,33 @@ cp_parser_jump_statement (cp_parser* parser)
 	  /* If the next token is a `;', then there is no
 	     expression.  */
 	  expr = NULL_TREE;
+
+	if (keyword == RID_RETURN && expr)
+	  {
+	    bool musttail_p = false;
+	    if (lookup_attribute ("gnu", "musttail", std_attrs))
+	      {
+		musttail_p = true;
+		std_attrs = remove_attribute ("gnu", "musttail", std_attrs);
+	      }
+	    // support this for compatibility
+	    if (lookup_attribute ("clang", "musttail", std_attrs))
+	      {
+		musttail_p = true;
+		std_attrs = remove_attribute ("clang", "musttail", std_attrs);
+	      }
+	    if (musttail_p)
+	      {
+		tree t = expr;
+		if (t && TREE_CODE (t) == TARGET_EXPR)
+		  t = TARGET_EXPR_INITIAL (t);
+		if (t && TREE_CODE (t) != CALL_EXPR)
+		  error_at (token->location, "cannot tail-call: return value must be a call");
+		else
+		  CALL_EXPR_MUST_TAIL_CALL (t) = 1;
+	      }
+	  }
+
 	/* Build the return-statement, check co-return first, since type
 	   deduction is not valid there.  */
 	if (keyword == RID_CO_RETURN)
@@ -30316,7 +30348,7 @@ cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
     /* Maybe we don't expect to see any arguments for this attribute.  */
     const attribute_spec *as
       = lookup_attribute_spec (TREE_PURPOSE (attribute));
-    if (as && as->max_length == 0)
+    if ((as && as->max_length == 0) || is_attribute_p ("musttail", attr_id))
       {
 	error_at (token->location, "%qE attribute does not take any arguments",
 		  attr_id);
diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
index dfce1b3c3591..f6af8e1a81e4 100644
--- a/gcc/cp/pt.cc
+++ b/gcc/cp/pt.cc
@@ -21085,12 +21085,14 @@ tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
 	    bool op = CALL_EXPR_OPERATOR_SYNTAX (t);
 	    bool ord = CALL_EXPR_ORDERED_ARGS (t);
 	    bool rev = CALL_EXPR_REVERSE_ARGS (t);
-	    if (op || ord || rev)
+	    bool mtc = CALL_EXPR_MUST_TAIL_CALL (t);
+	    if (op || ord || rev || mtc)
 	      if (tree call = extract_call_expr (ret))
 		{
 		  CALL_EXPR_OPERATOR_SYNTAX (call) = op;
 		  CALL_EXPR_ORDERED_ARGS (call) = ord;
 		  CALL_EXPR_REVERSE_ARGS (call) = rev;
+		  CALL_EXPR_MUST_TAIL_CALL (call) = mtc;
 		}
 	    if (warning_suppressed_p (t, OPT_Wpessimizing_move))
 	      /* This also suppresses -Wredundant-move.  */
-- 
2.44.0


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

* [PATCH v7 5/9] C: Implement musttail attribute for returns
  2024-06-02 17:16 Updated musttail patchkit Andi Kleen
                   ` (3 preceding siblings ...)
  2024-06-02 17:16 ` [PATCH v7 4/9] C++: Support clang compatible [[musttail]] (PR83324) Andi Kleen
@ 2024-06-02 17:16 ` Andi Kleen
  2024-06-02 17:16 ` [PATCH v7 6/9] Add tests for C/C++ musttail attributes Andi Kleen
                   ` (3 subsequent siblings)
  8 siblings, 0 replies; 23+ messages in thread
From: Andi Kleen @ 2024-06-02 17:16 UTC (permalink / raw)
  To: gcc-patches
  Cc: richard.guenther, nathan, josmyers, richard.sandiford, jason, Andi Kleen

Implement a C23 clang compatible musttail attribute similar to the earlier
C++ implementation in the C parser.

	PR83324

gcc/c/ChangeLog:

	* c-parser.cc (struct attr_state): Define with musttail_p.
	(c_parser_statement_after_labels): Handle [[musttail]]
	(c_parser_std_attribute): Dito.
	(c_parser_handle_musttail): Dito.
	(c_parser_compound_statement_nostart): Dito.
	(c_parser_all_labels): Dito.
	(c_parser_statement): Dito.
	* c-tree.h (c_finish_return): Add musttail_p flag.
	* c-typeck.cc (c_finish_return): Handle musttail_p flag.
---
 gcc/c/c-parser.cc | 61 +++++++++++++++++++++++++++++++++++++----------
 gcc/c/c-tree.h    |  2 +-
 gcc/c/c-typeck.cc | 15 ++++++++++--
 3 files changed, 63 insertions(+), 15 deletions(-)

diff --git a/gcc/c/c-parser.cc b/gcc/c/c-parser.cc
index 2d9e9c0969f0..6158d1d8ba4b 100644
--- a/gcc/c/c-parser.cc
+++ b/gcc/c/c-parser.cc
@@ -1616,6 +1616,11 @@ struct omp_for_parse_data {
   bool fail : 1;
 };
 
+struct attr_state
+{
+  bool musttail_p; // parsed a musttail for return
+};
+
 static bool c_parser_nth_token_starts_std_attributes (c_parser *,
 						      unsigned int);
 static tree c_parser_std_attribute_specifier_sequence (c_parser *);
@@ -1660,7 +1665,7 @@ static location_t c_parser_compound_statement_nostart (c_parser *);
 static void c_parser_label (c_parser *, tree);
 static void c_parser_statement (c_parser *, bool *, location_t * = NULL);
 static void c_parser_statement_after_labels (c_parser *, bool *,
-					     vec<tree> * = NULL);
+					     vec<tree> * = NULL, attr_state = {});
 static tree c_parser_c99_block_statement (c_parser *, bool *,
 					  location_t * = NULL);
 static void c_parser_if_statement (c_parser *, bool *, vec<tree> *);
@@ -5756,6 +5761,8 @@ c_parser_std_attribute (c_parser *parser, bool for_tm)
 	}
       goto out;
     }
+  else if (is_attribute_p ("musttail", name))
+    error ("%<musttail%> attribute has arguments");
   {
     location_t open_loc = c_parser_peek_token (parser)->location;
     matching_parens parens;
@@ -6941,6 +6948,28 @@ c_parser_handle_directive_omp_attributes (tree &attrs,
     }
 }
 
+/* Check if STD_ATTR contains a musttail attribute and handle it
+   PARSER is the parser and A is the output attr_state.  */
+
+static tree
+c_parser_handle_musttail (c_parser *parser, tree std_attrs, attr_state &a)
+{
+  if (c_parser_next_token_is_keyword (parser, RID_RETURN))
+    {
+      if (lookup_attribute ("gnu", "musttail", std_attrs))
+	{
+	  std_attrs = remove_attribute ("gnu", "musttail", std_attrs);
+	  a.musttail_p = true;
+	}
+      if (lookup_attribute ("clang", "musttail", std_attrs))
+	{
+	  std_attrs = remove_attribute ("clang", "musttail", std_attrs);
+	  a.musttail_p = true;
+	}
+    }
+  return std_attrs;
+}
+
 /* Parse a compound statement except for the opening brace.  This is
    used for parsing both compound statements and statement expressions
    (which follow different paths to handling the opening).  */
@@ -6957,6 +6986,7 @@ c_parser_compound_statement_nostart (c_parser *parser)
   bool in_omp_loop_block
     = omp_for_parse_state ? omp_for_parse_state->want_nested_loop : false;
   tree sl = NULL_TREE;
+  attr_state a = {};
 
   if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
     {
@@ -7095,7 +7125,10 @@ c_parser_compound_statement_nostart (c_parser *parser)
 	= c_parser_nth_token_starts_std_attributes (parser, 1);
       tree std_attrs = NULL_TREE;
       if (have_std_attrs)
-	std_attrs = c_parser_std_attribute_specifier_sequence (parser);
+	{
+	  std_attrs = c_parser_std_attribute_specifier_sequence (parser);
+	  std_attrs = c_parser_handle_musttail (parser, std_attrs, a);
+	}
       if (c_parser_next_token_is_keyword (parser, RID_CASE)
 	  || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
 	  || (c_parser_next_token_is (parser, CPP_NAME)
@@ -7243,7 +7276,7 @@ c_parser_compound_statement_nostart (c_parser *parser)
 	  last_stmt = true;
 	  mark_valid_location_for_stdc_pragma (false);
 	  if (!omp_for_parse_state)
-	    c_parser_statement_after_labels (parser, NULL);
+	    c_parser_statement_after_labels (parser, NULL, NULL, a);
 	  else
 	    {
 	      /* In canonical loop nest form, nested loops can only appear
@@ -7285,15 +7318,18 @@ c_parser_compound_statement_nostart (c_parser *parser)
 /* Parse all consecutive labels, possibly preceded by standard
    attributes.  In this context, a statement is required, not a
    declaration, so attributes must be followed by a statement that is
-   not just a semicolon.  */
+   not just a semicolon.  Returns an attr_state.  */
 
-static void
+static attr_state
 c_parser_all_labels (c_parser *parser)
 {
+  attr_state a = {};
   bool have_std_attrs;
   tree std_attrs = NULL;
   if ((have_std_attrs = c_parser_nth_token_starts_std_attributes (parser, 1)))
-    std_attrs = c_parser_std_attribute_specifier_sequence (parser);
+    std_attrs = c_parser_handle_musttail (parser,
+		    c_parser_std_attribute_specifier_sequence (parser), a);
+
   while (c_parser_next_token_is_keyword (parser, RID_CASE)
 	 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
 	 || (c_parser_next_token_is (parser, CPP_NAME)
@@ -7315,6 +7351,7 @@ c_parser_all_labels (c_parser *parser)
     }
   else if (have_std_attrs && c_parser_next_token_is (parser, CPP_SEMICOLON))
     c_parser_error (parser, "expected statement");
+  return a;
 }
 
 /* Parse a label (C90 6.6.1, C99 6.8.1, C11 6.8.1).
@@ -7558,11 +7595,11 @@ c_parser_label (c_parser *parser, tree std_attrs)
 static void
 c_parser_statement (c_parser *parser, bool *if_p, location_t *loc_after_labels)
 {
-  c_parser_all_labels (parser);
+  attr_state a = c_parser_all_labels (parser);
   if (loc_after_labels)
     *loc_after_labels = c_parser_peek_token (parser)->location;
   parser->omp_attrs_forbidden_p = false;
-  c_parser_statement_after_labels (parser, if_p, NULL);
+  c_parser_statement_after_labels (parser, if_p, NULL, a);
 }
 
 /* Parse a statement, other than a labeled statement.  CHAIN is a vector
@@ -7571,11 +7608,11 @@ c_parser_statement (c_parser *parser, bool *if_p, location_t *loc_after_labels)
 
    IF_P is used to track whether there's a (possibly labeled) if statement
    which is not enclosed in braces and has an else clause.  This is used to
-   implement -Wparentheses.  */
+   implement -Wparentheses. A has an earlier parsed attribute state.  */
 
 static void
 c_parser_statement_after_labels (c_parser *parser, bool *if_p,
-				 vec<tree> *chain)
+				 vec<tree> *chain, attr_state a)
 {
   location_t loc = c_parser_peek_token (parser)->location;
   tree stmt = NULL_TREE;
@@ -7643,7 +7680,7 @@ c_parser_statement_after_labels (c_parser *parser, bool *if_p,
 	  c_parser_consume_token (parser);
 	  if (c_parser_next_token_is (parser, CPP_SEMICOLON))
 	    {
-	      stmt = c_finish_return (loc, NULL_TREE, NULL_TREE);
+	      stmt = c_finish_return (loc, NULL_TREE, NULL_TREE, a.musttail_p);
 	      c_parser_consume_token (parser);
 	    }
 	  else
@@ -7652,7 +7689,7 @@ c_parser_statement_after_labels (c_parser *parser, bool *if_p,
 	      struct c_expr expr = c_parser_expression_conv (parser);
 	      mark_exp_read (expr.value);
 	      stmt = c_finish_return (EXPR_LOC_OR_LOC (expr.value, xloc),
-				      expr.value, expr.original_type);
+				      expr.value, expr.original_type, a.musttail_p);
 	      goto expect_semicolon;
 	    }
 	  break;
diff --git a/gcc/c/c-tree.h b/gcc/c/c-tree.h
index 56a33b8156c6..754117bccd0e 100644
--- a/gcc/c/c-tree.h
+++ b/gcc/c/c-tree.h
@@ -827,7 +827,7 @@ extern tree c_begin_stmt_expr (void);
 extern tree c_finish_stmt_expr (location_t, tree);
 extern tree c_process_expr_stmt (location_t, tree);
 extern tree c_finish_expr_stmt (location_t, tree);
-extern tree c_finish_return (location_t, tree, tree);
+extern tree c_finish_return (location_t, tree, tree, bool = false);
 extern tree c_finish_bc_stmt (location_t, tree, bool);
 extern tree c_finish_goto_label (location_t, tree);
 extern tree c_finish_goto_ptr (location_t, c_expr val);
diff --git a/gcc/c/c-typeck.cc b/gcc/c/c-typeck.cc
index a0e7dbe1b481..5ec92900d50a 100644
--- a/gcc/c/c-typeck.cc
+++ b/gcc/c/c-typeck.cc
@@ -11692,10 +11692,10 @@ c_finish_goto_ptr (location_t loc, c_expr val)
    to return, or a null pointer for `return;' with no value.  LOC is
    the location of the return statement, or the location of the expression,
    if the statement has any.  If ORIGTYPE is not NULL_TREE, it
-   is the original type of RETVAL.  */
+   is the original type of RETVAL.  MUSTTAIL_P indicates a musttail attribute.  */
 
 tree
-c_finish_return (location_t loc, tree retval, tree origtype)
+c_finish_return (location_t loc, tree retval, tree origtype, bool musttail_p)
 {
   tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
   bool no_warning = false;
@@ -11709,6 +11709,17 @@ c_finish_return (location_t loc, tree retval, tree origtype)
     warning_at (xloc, 0,
 		"function declared %<noreturn%> has a %<return%> statement");
 
+  if (retval && musttail_p)
+    {
+      tree t = retval;
+      if (TREE_CODE (t) == TARGET_EXPR)
+	t = TARGET_EXPR_INITIAL (t);
+      if (TREE_CODE (t) != CALL_EXPR)
+	error_at (xloc, "cannot tail-call: return value must be call");
+      else
+	CALL_EXPR_MUST_TAIL_CALL (t) = 1;
+    }
+
   if (retval)
     {
       tree semantic_type = NULL_TREE;
-- 
2.44.0


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

* [PATCH v7 6/9] Add tests for C/C++ musttail attributes
  2024-06-02 17:16 Updated musttail patchkit Andi Kleen
                   ` (4 preceding siblings ...)
  2024-06-02 17:16 ` [PATCH v7 5/9] C: Implement musttail attribute for returns Andi Kleen
@ 2024-06-02 17:16 ` Andi Kleen
  2024-06-03 14:46   ` Jason Merrill
  2024-06-02 17:16 ` [PATCH v7 7/9] Enable musttail tail conversion even when not optimizing Andi Kleen
                   ` (2 subsequent siblings)
  8 siblings, 1 reply; 23+ messages in thread
From: Andi Kleen @ 2024-06-02 17:16 UTC (permalink / raw)
  To: gcc-patches
  Cc: richard.guenther, nathan, josmyers, richard.sandiford, jason, Andi Kleen

Mostly adopted from the existing C musttail plugin tests.

gcc/testsuite/ChangeLog:

	* c-c++-common/musttail1.c: New test.
	* c-c++-common/musttail2.c: New test.
	* c-c++-common/musttail3.c: New test.
	* c-c++-common/musttail4.c: New test.
	* c-c++-common/musttail7.c: New test.
	* c-c++-common/musttail8.c: New test.
	* g++.dg/musttail6.C: New test.
	* g++.dg/musttail9.C: New test.
	* g++.dg/musttail10.C: New test.
---
 gcc/testsuite/c-c++-common/musttail1.c | 14 +++++++
 gcc/testsuite/c-c++-common/musttail2.c | 33 +++++++++++++++
 gcc/testsuite/c-c++-common/musttail3.c | 29 +++++++++++++
 gcc/testsuite/c-c++-common/musttail4.c | 17 ++++++++
 gcc/testsuite/c-c++-common/musttail5.c | 28 +++++++++++++
 gcc/testsuite/c-c++-common/musttail7.c | 14 +++++++
 gcc/testsuite/c-c++-common/musttail8.c | 17 ++++++++
 gcc/testsuite/g++.dg/musttail10.C      | 20 +++++++++
 gcc/testsuite/g++.dg/musttail6.C       | 58 ++++++++++++++++++++++++++
 gcc/testsuite/g++.dg/musttail9.C       | 10 +++++
 10 files changed, 240 insertions(+)
 create mode 100644 gcc/testsuite/c-c++-common/musttail1.c
 create mode 100644 gcc/testsuite/c-c++-common/musttail2.c
 create mode 100644 gcc/testsuite/c-c++-common/musttail3.c
 create mode 100644 gcc/testsuite/c-c++-common/musttail4.c
 create mode 100644 gcc/testsuite/c-c++-common/musttail5.c
 create mode 100644 gcc/testsuite/c-c++-common/musttail7.c
 create mode 100644 gcc/testsuite/c-c++-common/musttail8.c
 create mode 100644 gcc/testsuite/g++.dg/musttail10.C
 create mode 100644 gcc/testsuite/g++.dg/musttail6.C
 create mode 100644 gcc/testsuite/g++.dg/musttail9.C

diff --git a/gcc/testsuite/c-c++-common/musttail1.c b/gcc/testsuite/c-c++-common/musttail1.c
new file mode 100644
index 000000000000..74efcc2a0bc6
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/musttail1.c
@@ -0,0 +1,14 @@
+/* { dg-do compile { target { tail_call && { c || c++11 } } } } */
+/* { dg-additional-options "-fdelayed-branch" { target sparc*-*-* } } */
+
+int __attribute__((noinline,noclone,noipa))
+callee (int i)
+{
+  return i * i;
+}
+
+int __attribute__((noinline,noclone,noipa))
+caller (int i)
+{
+  [[gnu::musttail]] return callee (i + 1);
+}
diff --git a/gcc/testsuite/c-c++-common/musttail2.c b/gcc/testsuite/c-c++-common/musttail2.c
new file mode 100644
index 000000000000..86f2c3d77404
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/musttail2.c
@@ -0,0 +1,33 @@
+/* { dg-do compile { target { tail_call && { c || c++11 } } } } */
+
+struct box { char field[256]; int i; };
+
+int __attribute__((noinline,noclone,noipa))
+test_2_callee (int i, struct box b)
+{
+  if (b.field[0])
+    return 5;
+  return i * i;
+}
+
+int __attribute__((noinline,noclone,noipa))
+test_2_caller (int i)
+{
+  struct box b;
+  [[gnu::musttail]] return test_2_callee (i + 1, b); /* { dg-error "cannot tail-call: " } */
+}
+
+extern void setjmp (void);
+void
+test_3 (void)
+{
+  [[gnu::musttail]] return setjmp (); /* { dg-error "cannot tail-call: " } */
+}
+
+extern float f7(void);
+
+int
+test_6 (void)
+{
+  [[gnu::musttail]] return f7(); /* { dg-error "cannot tail-call: " } */
+}
diff --git a/gcc/testsuite/c-c++-common/musttail3.c b/gcc/testsuite/c-c++-common/musttail3.c
new file mode 100644
index 000000000000..ea9589c59ef2
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/musttail3.c
@@ -0,0 +1,29 @@
+/* { dg-do compile { target { tail_call && { c || c++11 } } } } */
+
+extern int foo2 (int x, ...);
+
+struct str
+{
+  int a, b;
+};
+
+struct str
+cstruct (int x)
+{
+  if (x < 10)
+    [[clang::musttail]] return cstruct (x + 1);
+  return ((struct str){ x, 0 });
+}
+
+int
+foo (int x)
+{
+  if (x < 10)
+    [[clang::musttail]] return foo2 (x, 29);
+  if (x < 100)
+    {
+      int k = foo (x + 1);
+      [[clang::musttail]] return k;	/* { dg-error "cannot tail-call: " } */
+    }
+  return x;
+}
diff --git a/gcc/testsuite/c-c++-common/musttail4.c b/gcc/testsuite/c-c++-common/musttail4.c
new file mode 100644
index 000000000000..23f4b5e1cd68
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/musttail4.c
@@ -0,0 +1,17 @@
+/* { dg-do compile { target { tail_call && { c || c++11 } } } } */
+
+struct box { char field[64]; int i; };
+
+struct box __attribute__((noinline,noclone,noipa))
+returns_struct (int i)
+{
+  struct box b;
+  b.i = i * i;
+  return b;
+}
+
+int __attribute__((noinline,noclone))
+test_1 (int i)
+{
+  [[gnu::musttail]] return returns_struct (i * 5).i; /* { dg-error "cannot tail-call: " } */
+}
diff --git a/gcc/testsuite/c-c++-common/musttail5.c b/gcc/testsuite/c-c++-common/musttail5.c
new file mode 100644
index 000000000000..234da0d3f2a9
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/musttail5.c
@@ -0,0 +1,28 @@
+/* { dg-do compile } */
+/* { dg-options "-std=c23" { target c } } */
+/* { dg-options "-std=gnu++11" { target c++ } } */
+
+[[musttail]] int j; /* { dg-warning "attribute" } */
+__attribute__((musttail)) int k; /* { dg-warning "attribute" } */
+
+void foo(void)
+{
+  [[gnu::musttail]] j++; /* { dg-warning "attribute" } */
+  [[gnu::musttail]] if (k > 0) /* { dg-warning "attribute" } */
+    [[gnu::musttail]] k++; /* { dg-warning "attribute" } */
+}
+
+int foo2(int p)
+{
+  [[gnu::musttail(1)]] return foo2(p + 1); /* { dg-error "\(before numeric constant|attribute\)" } */
+}
+
+int i;
+
+int foo3(void)
+{
+  [[musttail]] i++; /* { dg-warning "attribute" } */
+  [[musttail]] if (i > 10) /* { dg-warning "attribute" } */
+    [[musttail]] return foo2(i); /* { dg-warning "attribute" } */
+  return 0;
+}
diff --git a/gcc/testsuite/c-c++-common/musttail7.c b/gcc/testsuite/c-c++-common/musttail7.c
new file mode 100644
index 000000000000..5e4eb1bfbacc
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/musttail7.c
@@ -0,0 +1,14 @@
+/* { dg-do compile { target { tail_call && { c || c++11 } } } } */
+/* { dg-additional-options "-fdelayed-branch" { target sparc*-*-* } } */
+
+extern void f();
+
+void f2()
+{
+  [[gnu::musttail]] return f2();
+}
+
+void f3()
+{
+  [[gnu::musttail]] return f();
+}
diff --git a/gcc/testsuite/c-c++-common/musttail8.c b/gcc/testsuite/c-c++-common/musttail8.c
new file mode 100644
index 000000000000..9fa10e0b54c4
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/musttail8.c
@@ -0,0 +1,17 @@
+/* { dg-do compile { target { tail_call && { c || c++11 } } } } */
+
+float f1(void);
+
+int f2(void)
+{
+  [[gnu::musttail]] return f1 (); /* { dg-error "changed after call" } */
+}
+
+
+int f3(int *);
+
+int f4(void)
+{
+  int x;
+  [[gnu::musttail]] return f3(&x); /* { dg-error "\(refers to locals|other reasons\)" } */
+}
diff --git a/gcc/testsuite/g++.dg/musttail10.C b/gcc/testsuite/g++.dg/musttail10.C
new file mode 100644
index 000000000000..6da7e021f826
--- /dev/null
+++ b/gcc/testsuite/g++.dg/musttail10.C
@@ -0,0 +1,20 @@
+/* { dg-do compile { target { tail_call } } } */
+/* { dg-options "-std=gnu++11" } */
+/* { dg-additional-options "-fdelayed-branch" { target sparc*-*-* } } */
+
+int f();
+
+double h() { [[gnu::musttail]] return f(); } /* { dg-error "cannot tail-call" } */
+
+/* The error check cannot distinguish between the (valid) int and the invalid
+   double conversion case because the template context is lost when tree-tailcall
+   finally runs.  */
+
+template <class T>
+T g() { [[gnu::musttail]] return f(); } /* { dg-error "cannot tail-call" } */
+
+int main()
+{
+  g<int>();
+  g<double>();
+}
diff --git a/gcc/testsuite/g++.dg/musttail6.C b/gcc/testsuite/g++.dg/musttail6.C
new file mode 100644
index 000000000000..e0e478e08d58
--- /dev/null
+++ b/gcc/testsuite/g++.dg/musttail6.C
@@ -0,0 +1,58 @@
+/* { dg-do compile { target { tail_call } } } */
+/* { dg-options "-std=gnu++11" } */
+/* { dg-additional-options "-fdelayed-branch" { target sparc*-*-* } } */
+
+class Foo {
+public:
+  int a, b;
+  Foo(int a, int b) : a(a), b(b) {}
+};
+
+Foo __attribute__((noinline,noclone,noipa))
+callee (int i)
+{
+  return Foo(i, i+1);
+}
+
+Foo __attribute__((noinline,noclone,noipa))
+caller (int i)
+{
+  [[gnu::musttail]] return callee (i + 1);
+}
+
+template<typename T>
+T __attribute__((noinline,noclone,noipa)) foo (T i)
+{
+  return i + 1;
+}
+
+int
+caller2 (int k)
+{
+  [[gnu::musttail]] return foo<int>(1);
+}
+
+template<typename T>
+T caller3 (T v)
+{
+  [[gnu::musttail]] return foo<T>(v);
+}
+
+int call3(int i)
+{
+  [[gnu::musttail]] return caller3<int>(i + 1);
+}
+
+struct Bar {
+  int a;
+  Bar(int a) : a(a) {}
+  Bar operator+(Bar o) { return Bar(a + o.a); } 
+};
+
+#if __OPTIMIZE__ >= 1
+Bar
+caller4 (Bar k)
+{
+  [[gnu::musttail]] return caller3<Bar>(Bar(99));
+}
+#endif
diff --git a/gcc/testsuite/g++.dg/musttail9.C b/gcc/testsuite/g++.dg/musttail9.C
new file mode 100644
index 000000000000..8095bdf6fe62
--- /dev/null
+++ b/gcc/testsuite/g++.dg/musttail9.C
@@ -0,0 +1,10 @@
+/* { dg-do compile { target { tail_call } } } */
+/* { dg-options "-std=gnu++11 -Wno-deprecated" } */
+/* { dg-additional-options "-fdelayed-branch" { target sparc*-*-* } } */
+
+extern void foo() throw(const char *);
+
+void f() throw()
+{
+  [[gnu::musttail]] return foo(); /* { dg-error "call may throw exception that does not propagate" } */
+}
-- 
2.44.0


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

* [PATCH v7 7/9] Enable musttail tail conversion even when not optimizing
  2024-06-02 17:16 Updated musttail patchkit Andi Kleen
                   ` (5 preceding siblings ...)
  2024-06-02 17:16 ` [PATCH v7 6/9] Add tests for C/C++ musttail attributes Andi Kleen
@ 2024-06-02 17:16 ` Andi Kleen
  2024-06-02 17:16 ` [PATCH v7 8/9] Give better error messages for musttail Andi Kleen
  2024-06-02 17:16 ` [PATCH v7 9/9] Add documentation for musttail attribute Andi Kleen
  8 siblings, 0 replies; 23+ messages in thread
From: Andi Kleen @ 2024-06-02 17:16 UTC (permalink / raw)
  To: gcc-patches
  Cc: richard.guenther, nathan, josmyers, richard.sandiford, jason, Andi Kleen

Enable the tailcall optimization for non optimizing builds,
but in this case only checks calls that have the musttail attribute set.
This makes musttail work without optimization.

This is done with a new late musttail pass that is only active when
not optimizing. The pass must be after ehcleanup.

gcc/ChangeLog:

	* passes.def (pass_musttail): Add.
	* tree-pass.h (make_pass_musttail): Add.
	* tree-tailcall.cc (find_tail_calls): Handle only_musttail
	  argument.
	(tree_optimize_tail_calls_1): Pass on only_musttail.
	(execute_tail_calls): Pass only_musttail as false.
	(class pass_musttail): Add.
	(make_pass_musttail): Add.
---
 gcc/passes.def       |  1 +
 gcc/tree-pass.h      |  1 +
 gcc/tree-tailcall.cc | 64 +++++++++++++++++++++++++++++++++++++-------
 3 files changed, 57 insertions(+), 9 deletions(-)

diff --git a/gcc/passes.def b/gcc/passes.def
index 1cbbd4130970..3e83cc327fd2 100644
--- a/gcc/passes.def
+++ b/gcc/passes.def
@@ -443,6 +443,7 @@ along with GCC; see the file COPYING3.  If not see
   NEXT_PASS (pass_tsan_O0);
   NEXT_PASS (pass_sanopt);
   NEXT_PASS (pass_cleanup_eh);
+  NEXT_PASS (pass_musttail);
   NEXT_PASS (pass_lower_resx);
   NEXT_PASS (pass_nrv);
   NEXT_PASS (pass_gimple_isel);
diff --git a/gcc/tree-pass.h b/gcc/tree-pass.h
index 29267589eeb3..0668cea0a48e 100644
--- a/gcc/tree-pass.h
+++ b/gcc/tree-pass.h
@@ -368,6 +368,7 @@ extern gimple_opt_pass *make_pass_sra (gcc::context *ctxt);
 extern gimple_opt_pass *make_pass_sra_early (gcc::context *ctxt);
 extern gimple_opt_pass *make_pass_tail_recursion (gcc::context *ctxt);
 extern gimple_opt_pass *make_pass_tail_calls (gcc::context *ctxt);
+extern gimple_opt_pass *make_pass_musttail (gcc::context *ctxt);
 extern gimple_opt_pass *make_pass_fix_loops (gcc::context *ctxt);
 extern gimple_opt_pass *make_pass_tree_loop (gcc::context *ctxt);
 extern gimple_opt_pass *make_pass_tree_no_loop (gcc::context *ctxt);
diff --git a/gcc/tree-tailcall.cc b/gcc/tree-tailcall.cc
index e9f7f8a12b3a..094856de22ef 100644
--- a/gcc/tree-tailcall.cc
+++ b/gcc/tree-tailcall.cc
@@ -408,10 +408,10 @@ static live_vars_map *live_vars;
 static vec<bitmap_head> live_vars_vec;
 
 /* Finds tailcalls falling into basic block BB. The list of found tailcalls is
-   added to the start of RET.  */
+   added to the start of RET. When ONLY_MUSTTAIL is set only handle musttail.  */
 
 static void
-find_tail_calls (basic_block bb, struct tailcall **ret)
+find_tail_calls (basic_block bb, struct tailcall **ret, bool only_musttail)
 {
   tree ass_var = NULL_TREE, ret_var, func, param;
   gimple *stmt;
@@ -445,6 +445,9 @@ find_tail_calls (basic_block bb, struct tailcall **ret)
       if (is_gimple_call (stmt))
 	{
 	  call = as_a <gcall *> (stmt);
+	  /* Handle only musttail calls when not optimizing.  */
+	  if (only_musttail && !gimple_call_must_tail_p (call))
+	    return;
 	  ass_var = gimple_call_lhs (call);
 	  break;
 	}
@@ -467,7 +470,7 @@ find_tail_calls (basic_block bb, struct tailcall **ret)
       edge_iterator ei;
       /* Recurse to the predecessors.  */
       FOR_EACH_EDGE (e, ei, bb->preds)
-	find_tail_calls (e->src, ret);
+	find_tail_calls (e->src, ret, only_musttail);
 
       return;
     }
@@ -528,7 +531,8 @@ find_tail_calls (basic_block bb, struct tailcall **ret)
   func = gimple_call_fndecl (call);
   if (func
       && !fndecl_built_in_p (func)
-      && recursive_call_p (current_function_decl, func))
+      && recursive_call_p (current_function_decl, func)
+      && !only_musttail)
     {
       tree arg;
 
@@ -1094,10 +1098,11 @@ create_tailcall_accumulator (const char *label, basic_block bb, tree init)
 }
 
 /* Optimizes tail calls in the function, turning the tail recursion
-   into iteration.  */
+   into iteration. When ONLY_MUSTCALL is true only optimize mustcall
+   marked calls.  */
 
 static unsigned int
-tree_optimize_tail_calls_1 (bool opt_tailcalls)
+tree_optimize_tail_calls_1 (bool opt_tailcalls, bool only_mustcall)
 {
   edge e;
   bool phis_constructed = false;
@@ -1117,7 +1122,7 @@ tree_optimize_tail_calls_1 (bool opt_tailcalls)
       /* Only traverse the normal exits, i.e. those that end with return
 	 statement.  */
       if (safe_is_a <greturn *> (*gsi_last_bb (e->src)))
-	find_tail_calls (e->src, &tailcalls);
+	find_tail_calls (e->src, &tailcalls, only_mustcall);
     }
 
   if (live_vars)
@@ -1228,7 +1233,7 @@ gate_tail_calls (void)
 static unsigned int
 execute_tail_calls (void)
 {
-  return tree_optimize_tail_calls_1 (true);
+  return tree_optimize_tail_calls_1 (true, false);
 }
 
 namespace {
@@ -1261,7 +1266,7 @@ public:
   bool gate (function *) final override { return gate_tail_calls (); }
   unsigned int execute (function *) final override
     {
-      return tree_optimize_tail_calls_1 (false);
+      return tree_optimize_tail_calls_1 (false, false);
     }
 
 }; // class pass_tail_recursion
@@ -1312,3 +1317,44 @@ make_pass_tail_calls (gcc::context *ctxt)
 {
   return new pass_tail_calls (ctxt);
 }
+
+namespace {
+
+const pass_data pass_data_musttail =
+{
+  GIMPLE_PASS, /* type */
+  "musttail", /* name */
+  OPTGROUP_NONE, /* optinfo_flags */
+  TV_NONE, /* tv_id */
+  ( PROP_cfg | PROP_ssa ), /* properties_required */
+  0, /* properties_provided */
+  0, /* properties_destroyed */
+  0, /* todo_flags_start */
+  0, /* todo_flags_finish */
+};
+
+class pass_musttail : public gimple_opt_pass
+{
+public:
+  pass_musttail (gcc::context *ctxt)
+    : gimple_opt_pass (pass_data_musttail, ctxt)
+  {}
+
+  /* opt_pass methods: */
+  /* This pass is only used when not optimizing to make [[musttail]] still
+     work.  */
+  bool gate (function *) final override { return !flag_optimize_sibling_calls; }
+  unsigned int execute (function *) final override
+  {
+    return tree_optimize_tail_calls_1 (true, true);
+  }
+
+}; // class pass_musttail
+
+} // anon namespace
+
+gimple_opt_pass *
+make_pass_musttail (gcc::context *ctxt)
+{
+  return new pass_musttail (ctxt);
+}
-- 
2.44.0


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

* [PATCH v7 8/9] Give better error messages for musttail
  2024-06-02 17:16 Updated musttail patchkit Andi Kleen
                   ` (6 preceding siblings ...)
  2024-06-02 17:16 ` [PATCH v7 7/9] Enable musttail tail conversion even when not optimizing Andi Kleen
@ 2024-06-02 17:16 ` Andi Kleen
  2024-06-02 17:16 ` [PATCH v7 9/9] Add documentation for musttail attribute Andi Kleen
  8 siblings, 0 replies; 23+ messages in thread
From: Andi Kleen @ 2024-06-02 17:16 UTC (permalink / raw)
  To: gcc-patches
  Cc: richard.guenther, nathan, josmyers, richard.sandiford, jason, Andi Kleen

When musttail is set, make tree-tailcall to give error messages
when it cannot handle a call. This avoids vague "other reasons"
error messages later at expand time.

This doesn't always work, for example when find_tail_call
walking gives up because the control flow is too complicated
then it won't find the tail call and can't give a suitable
error message.

gcc/ChangeLog:

	* tree-tailcall.cc (maybe_error_musttail): Add.
        (bb_get_succ_edge_count): Add.
	(find_tail_calls): Add error messages. Keep searching
        for basic blocks with multiple BBs if all but one is EH
        only.
---
 gcc/tree-tailcall.cc | 70 +++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 62 insertions(+), 8 deletions(-)

diff --git a/gcc/tree-tailcall.cc b/gcc/tree-tailcall.cc
index 094856de22ef..d315be554418 100644
--- a/gcc/tree-tailcall.cc
+++ b/gcc/tree-tailcall.cc
@@ -43,6 +43,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "common/common-target.h"
 #include "ipa-utils.h"
 #include "tree-ssa-live.h"
+#include "diagnostic-core.h"
 
 /* The file implements the tail recursion elimination.  It is also used to
    analyze the tail calls in general, passing the results to the rtl level
@@ -402,6 +403,36 @@ propagate_through_phis (tree var, edge e)
   return var;
 }
 
+/* Report an error for failing to tail convert must call CALL
+   with error message ERR.  */
+
+static void
+maybe_error_musttail (gcall *call, const char *err)
+{
+  if (gimple_call_must_tail_p (call))
+    {
+      error_at (call->location, "cannot tail-call: %s", err);
+      gimple_call_set_must_tail (call, false); /* Avoid another error.  */
+      gimple_call_set_tail (call, false);
+    }
+}
+
+/* Count succ edges for BB and return in NUM_OTHER and NUM_EH.  */
+
+static void
+bb_get_succ_edge_count (basic_block bb, int &num_other, int &num_eh)
+{
+  edge e;
+  edge_iterator ei;
+  num_eh = 0;
+  num_other = 0;
+  FOR_EACH_EDGE (e, ei, bb->succs)
+    if (e->flags & EDGE_EH)
+      num_eh++;
+    else
+      num_other++;
+}
+
 /* Argument for compute_live_vars/live_vars_at_stmt and what compute_live_vars
    returns.  Computed lazily, but just once for the function.  */
 static live_vars_map *live_vars;
@@ -426,7 +457,14 @@ find_tail_calls (basic_block bb, struct tailcall **ret, bool only_musttail)
   tree var;
 
   if (!single_succ_p (bb))
-    return;
+    {
+      int num_eh, num_other;
+      bb_get_succ_edge_count (bb, num_eh, num_other);
+      /* Allow a single EH edge so that we can give a better
+	 error message later.  */
+      if (!(num_eh == 1 && num_other == 1))
+	return;
+    }
 
   for (gsi = gsi_last_bb (bb); !gsi_end_p (gsi); gsi_prev (&gsi))
     {
@@ -489,13 +527,20 @@ find_tail_calls (basic_block bb, struct tailcall **ret, bool only_musttail)
   if (ass_var
       && !is_gimple_reg (ass_var)
       && !auto_var_in_fn_p (ass_var, cfun->decl))
-    return;
+    {
+      maybe_error_musttail (call, "complex return value");
+      return;
+    }
 
   /* If the call might throw an exception that wouldn't propagate out of
      cfun, we can't transform to a tail or sibling call (82081).  */
   if (stmt_could_throw_p (cfun, stmt)
       && !stmt_can_throw_external (cfun, stmt))
+  {
+    maybe_error_musttail (call,
+			  "call may throw exception that does not propagate");
     return;
+  }
 
   /* If the function returns a value, then at present, the tail call
      must return the same type of value.  There is conceptually a copy
@@ -524,7 +569,10 @@ find_tail_calls (basic_block bb, struct tailcall **ret, bool only_musttail)
   if (result_decl
       && may_be_aliased (result_decl)
       && ref_maybe_used_by_stmt_p (call, result_decl, false))
-    return;
+    {
+      maybe_error_musttail (call, "tail call must be same type");
+      return;
+    }
 
   /* We found the call, check whether it is suitable.  */
   tail_recursion = false;
@@ -605,6 +653,7 @@ find_tail_calls (basic_block bb, struct tailcall **ret, bool only_musttail)
 	    {
 	      if (local_live_vars)
 		BITMAP_FREE (local_live_vars);
+	      maybe_error_musttail (call, "call invocation refers to locals");
 	      return;
 	    }
 	  else
@@ -613,6 +662,7 @@ find_tail_calls (basic_block bb, struct tailcall **ret, bool only_musttail)
 	      if (bitmap_bit_p (local_live_vars, *v))
 		{
 		  BITMAP_FREE (local_live_vars);
+		  maybe_error_musttail (call, "call invocation refers to locals");
 		  return;
 		}
 	    }
@@ -663,12 +713,13 @@ find_tail_calls (basic_block bb, struct tailcall **ret, bool only_musttail)
       /* This is a gimple assign. */
       par ret = process_assignment (as_a <gassign *> (stmt), gsi,
 				    &tmp_m, &tmp_a, &ass_var, to_move_defs);
-      if (ret == FAIL)
-	return;
+      if (ret == FAIL || (ret == TRY_MOVE && !tail_recursion))
+	{
+	  maybe_error_musttail (call, "return value changed after call");
+	  return;
+	}
       else if (ret == TRY_MOVE)
 	{
-	  if (! tail_recursion)
-	    return;
 	  /* Do not deal with checking dominance, the real fix is to
 	     do path isolation for the transform phase anyway, removing
 	     the need to compute the accumulators with new stmts.  */
@@ -716,7 +767,10 @@ find_tail_calls (basic_block bb, struct tailcall **ret, bool only_musttail)
   if (ret_var
       && (ret_var != ass_var
 	  && !(is_empty_type (TREE_TYPE (ret_var)) && !ass_var)))
-    return;
+    {
+      maybe_error_musttail (call, "call must be the same type");
+      return;
+    }
 
   /* If this is not a tail recursive call, we cannot handle addends or
      multiplicands.  */
-- 
2.44.0


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

* [PATCH v7 9/9] Add documentation for musttail attribute
  2024-06-02 17:16 Updated musttail patchkit Andi Kleen
                   ` (7 preceding siblings ...)
  2024-06-02 17:16 ` [PATCH v7 8/9] Give better error messages for musttail Andi Kleen
@ 2024-06-02 17:16 ` Andi Kleen
  8 siblings, 0 replies; 23+ messages in thread
From: Andi Kleen @ 2024-06-02 17:16 UTC (permalink / raw)
  To: gcc-patches
  Cc: richard.guenther, nathan, josmyers, richard.sandiford, jason, Andi Kleen

gcc/ChangeLog:

	* doc/extend.texi: Document [[musttail]]
---
 gcc/doc/extend.texi | 23 +++++++++++++++++++++--
 1 file changed, 21 insertions(+), 2 deletions(-)

diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi
index 799a36586dc9..1984e8c1beec 100644
--- a/gcc/doc/extend.texi
+++ b/gcc/doc/extend.texi
@@ -9921,7 +9921,7 @@ same manner as the @code{deprecated} attribute.
 @section Statement Attributes
 @cindex Statement Attributes
 
-GCC allows attributes to be set on null statements.  @xref{Attribute Syntax},
+GCC allows attributes to be set on statements.  @xref{Attribute Syntax},
 for details of the exact syntax for using attributes.  Other attributes are
 available for functions (@pxref{Function Attributes}), variables
 (@pxref{Variable Attributes}), labels (@pxref{Label Attributes}), enumerators
@@ -9978,6 +9978,23 @@ foo (int x, int y)
 @code{y} is not actually incremented and the compiler can but does not
 have to optimize it to just @code{return 42 + 42;}.
 
+@cindex @code{musttail} statement attribute
+@item musttail
+
+The @code{gnu::musttail} or @code{clang::musttail} attribute
+can be applied to a @code{return} statement with a return-value expression
+that is a function call.  It asserts that the call must be a tail call that
+does not allocate extra stack space.
+
+@smallexample
+[[gnu::musttail]] return foo();
+@end smallexample
+
+If the compiler cannot generate a tail call it generates
+an error. On some targets tail calls may not be supported.
+Tail calls cannot reference locals in memory, which may affect
+builds without optimization when passing small structures, or passing
+or returning large structures.
 @end table
 
 @node Attribute Syntax
@@ -10101,7 +10118,9 @@ the constant expression, if present.
 
 @subsubheading Statement Attributes
 In GNU C, an attribute specifier list may appear as part of a null
-statement.  The attribute goes before the semicolon.
+statement. The attribute goes before the semicolon.
+Some attributes in new style syntax are also supported
+on non-null statements.
 
 @subsubheading Type Attributes
 
-- 
2.44.0


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

* Re: [PATCH v7 2/9] Fix pro_and_epilogue for sibcalls at -O0
  2024-06-02 17:16 ` [PATCH v7 2/9] Fix pro_and_epilogue for sibcalls at -O0 Andi Kleen
@ 2024-06-02 17:30   ` Andrew Pinski
  2024-06-02 20:27     ` Andi Kleen
  0 siblings, 1 reply; 23+ messages in thread
From: Andrew Pinski @ 2024-06-02 17:30 UTC (permalink / raw)
  To: Andi Kleen
  Cc: GCC Patches, Richard Guenther, Nathan Sidwell, josmyers,
	Richard Sandiford, Jason Merrill

[-- Attachment #1: Type: text/plain, Size: 2199 bytes --]

On Sun, Jun 2, 2024, 10:24 AM Andi Kleen <ak@linux.intel.com> wrote:

> Some of the cfg fixups in pro_and_epilogue for sibcalls were dependent on
> "optimize".
> Make them check cfun->tail_call_marked instead to handle the -O0 musttail
> case. This fixes the musttail test cases on arm targets.
>
>         PR115255
>
> gcc/ChangeLog:
>
>         * function.cc (thread_prologue_and_epilogue_insns): Check
>           cfun->tail_call_marked for sibcalls too.
>         (rest_of_handle_thread_prologue_and_epilogue): Dito.
> ---
>  gcc/function.cc | 5 +++--
>  1 file changed, 3 insertions(+), 2 deletions(-)
>
> diff --git a/gcc/function.cc b/gcc/function.cc
> index 4edd4da12474..f949c38b916c 100644
> --- a/gcc/function.cc
> +++ b/gcc/function.cc
> @@ -6261,6 +6261,7 @@ thread_prologue_and_epilogue_insns (void)
>    /* Threading the prologue and epilogue changes the artificial refs in
> the
>       entry and exit blocks, and may invalidate DF info for tail calls.  */
>    if (optimize
> +      || cfun->tail_call_marked
>        || flag_optimize_sibling_calls
>        || flag_ipa_icf_functions
>        || in_lto_p)
> @@ -6557,7 +6558,7 @@ rest_of_handle_thread_prologue_and_epilogue
> (function *fun)
>  {
>    /* prepare_shrink_wrap is sensitive to the block structure of the
> control
>       flow graph, so clean it up first.  */
> -  if (optimize)
> +  if (cfun->tail_call_marked || optimize)
>      cleanup_cfg (0);
>
>    /* On some machines, the prologue and epilogue code, or parts thereof,
> @@ -6579,7 +6580,7 @@ rest_of_handle_thread_prologue_and_epilogue
> (function *fun)
>
>    /* Shrink-wrapping can result in unreachable edges in the epilogue,
>       see PR57320.  */
> -  cleanup_cfg (optimize ? CLEANUP_EXPENSIVE : 0);
> +  cleanup_cfg ((cfun->tail_call_marked || optimize) ? CLEANUP_EXPENSIVE :
> 0);
>

I think this makes -g -O0 with musttail useless. Make sure if you have a
musttail function call in the ir that it does not change the cfg for -O0
case which adds extra jumps specifically for debugging.

Thanks,
Andrew



>    /* The stack usage info is finalized during prologue expansion.  */
>    if (flag_stack_usage_info || flag_callgraph_info)
> --
> 2.44.0
>
>

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

* Re: [PATCH v7 2/9] Fix pro_and_epilogue for sibcalls at -O0
  2024-06-02 17:30   ` Andrew Pinski
@ 2024-06-02 20:27     ` Andi Kleen
  0 siblings, 0 replies; 23+ messages in thread
From: Andi Kleen @ 2024-06-02 20:27 UTC (permalink / raw)
  To: Andrew Pinski
  Cc: GCC Patches, Richard Guenther, Nathan Sidwell, josmyers,
	Richard Sandiford, Jason Merrill

>      @@ -6557,7 +6558,7 @@ rest_of_handle_thread_prologue_and_epilogue
>      (function *fun)
>       {
>         /* prepare_shrink_wrap is sensitive to the block structure of the
>      control
>            flow graph, so clean it up first.  */
>      -  if (optimize)
>      +  if (cfun->tail_call_marked || optimize)
>           cleanup_cfg (0);
> 
>         /* On some machines, the prologue and epilogue code, or parts
>      thereof,
>      @@ -6579,7 +6580,7 @@ rest_of_handle_thread_prologue_and_epilogue
>      (function *fun)
> 
>         /* Shrink-wrapping can result in unreachable edges in the epilogue,
>            see PR57320.  */
>      -  cleanup_cfg (optimize ? CLEANUP_EXPENSIVE : 0);
>      +  cleanup_cfg ((cfun->tail_call_marked || optimize) ? CLEANUP_EXPENSIVE
>      : 0);
> 
>    I think this makes -g -O0 with musttail useless. Make sure if you have a
>    musttail function call in the ir that it does not change the cfg for -O0
>    case which adds extra jumps specifically for debugging. 

It looks like the second hunk (CLEANUP_EXPENSIVE) is not actually
needed. I just tested and at least ARM works without it. I will drop it.

But the first one is needed.

Do you still have the concern only with the first? AFAIK without
CLEANUP_EXPENSIVE there will be much less changes to branches.

-Andi

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

* Re: [PATCH v7 4/9] C++: Support clang compatible [[musttail]] (PR83324)
  2024-06-02 17:16 ` [PATCH v7 4/9] C++: Support clang compatible [[musttail]] (PR83324) Andi Kleen
@ 2024-06-03 14:42   ` Jason Merrill
  2024-06-03 15:33     ` Andi Kleen
  0 siblings, 1 reply; 23+ messages in thread
From: Jason Merrill @ 2024-06-03 14:42 UTC (permalink / raw)
  To: Andi Kleen, gcc-patches
  Cc: richard.guenther, nathan, josmyers, richard.sandiford

On 6/2/24 13:16, Andi Kleen wrote:
> This patch implements a clang compatible [[musttail]] attribute for
> returns.
> 
> musttail is useful as an alternative to computed goto for interpreters.
> With computed goto the interpreter function usually ends up very big
> which causes problems with register allocation and other per function
> optimizations not scaling. With musttail the interpreter can be instead
> written as a sequence of smaller functions that call each other. To
> avoid unbounded stack growth this requires forcing a sibling call, which
> this attribute does. It guarantees an error if the call cannot be tail
> called which allows the programmer to fix it instead of risking a stack
> overflow. Unlike computed goto it is also type-safe.
> 
> It turns out that David Malcolm had already implemented middle/backend
> support for a musttail attribute back in 2016, but it wasn't exposed
> to any frontend other than a special plugin.
> 
> This patch adds a [[gnu::musttail]] attribute for C++ that can be added
> to return statements. The return statement must be a direct call
> (it does not follow dependencies), which is similar to what clang
> implements. It then uses the existing must tail infrastructure.
> 
> For compatibility it also detects clang::musttail
> 
> One problem is that tree-tailcall usually fails when optimization
> is disabled, which implies the attribute only really works with
> optimization on. But that seems to be a reasonable limitation.
> 
> Passes bootstrap and full test
> 
> 	PR83324
> 
> gcc/cp/ChangeLog:
> 
> 	* parser.cc (cp_parser_statement): Handle musttail.
> 	(cp_parser_jump_statement): Dito.
> 	(cp_parser_std_attribute): Dito.
> 	* pt.cc (tsubst_expr): Copy CALL_EXPR_MUST_TAIL_CALL.
> ---
>   gcc/cp/parser.cc | 42 +++++++++++++++++++++++++++++++++++++-----
>   gcc/cp/pt.cc     |  4 +++-
>   2 files changed, 40 insertions(+), 6 deletions(-)
> 
> diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc
> index 779625144db4..c2cb304bac7d 100644
> --- a/gcc/cp/parser.cc
> +++ b/gcc/cp/parser.cc
> @@ -2467,7 +2467,7 @@ static tree cp_parser_perform_range_for_lookup
>   static tree cp_parser_range_for_member_function
>     (tree, tree);
>   static tree cp_parser_jump_statement
> -  (cp_parser *);
> +  (cp_parser *, tree &);
>   static void cp_parser_declaration_statement
>     (cp_parser *);
>   
> @@ -12747,13 +12747,17 @@ cp_parser_statement (cp_parser* parser, tree in_statement_expr,
>   						     NULL_TREE, false);
>   	  break;
>   
> +	case RID_RETURN:
> +	  std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
> +	  statement = cp_parser_jump_statement (parser, std_attrs);
> +	  break;
> +
>   	case RID_BREAK:
>   	case RID_CONTINUE:
> -	case RID_RETURN:
>   	case RID_CO_RETURN:
>   	case RID_GOTO:
>   	  std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
> -	  statement = cp_parser_jump_statement (parser);
> +	  statement = cp_parser_jump_statement (parser, std_attrs);
>   	  break;
>   
>   	  /* Objective-C++ exception-handling constructs.  */
> @@ -14813,10 +14817,11 @@ cp_parser_init_statement (cp_parser *parser, tree *decl)
>      jump-statement:
>        goto * expression ;
>   
> +   STD_ATTRS are the statement attributes. They can be modified.
>      Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
>   
>   static tree
> -cp_parser_jump_statement (cp_parser* parser)
> +cp_parser_jump_statement (cp_parser* parser, tree &std_attrs)
>   {
>     tree statement = error_mark_node;
>     cp_token *token;
> @@ -14893,6 +14898,33 @@ cp_parser_jump_statement (cp_parser* parser)
>   	  /* If the next token is a `;', then there is no
>   	     expression.  */
>   	  expr = NULL_TREE;
> +
> +	if (keyword == RID_RETURN && expr)
> +	  {
> +	    bool musttail_p = false;
> +	    if (lookup_attribute ("gnu", "musttail", std_attrs))
> +	      {
> +		musttail_p = true;
> +		std_attrs = remove_attribute ("gnu", "musttail", std_attrs);
> +	      }
> +	    // support this for compatibility

This should follow the usual comment conventions, i.e.

/* Support this for compatibility.  */

> +	    if (lookup_attribute ("clang", "musttail", std_attrs))
> +	      {
> +		musttail_p = true;
> +		std_attrs = remove_attribute ("clang", "musttail", std_attrs);
> +	      }
> +	    if (musttail_p)
> +	      {
> +		tree t = expr;
> +		if (t && TREE_CODE (t) == TARGET_EXPR)
> +		  t = TARGET_EXPR_INITIAL (t);
> +		if (t && TREE_CODE (t) != CALL_EXPR)
> +		  error_at (token->location, "cannot tail-call: return value must be a call");
> +		else
> +		  CALL_EXPR_MUST_TAIL_CALL (t) = 1;
> +	      }
> +	  }
> +
>   	/* Build the return-statement, check co-return first, since type
>   	   deduction is not valid there.  */
>   	if (keyword == RID_CO_RETURN)
> @@ -30316,7 +30348,7 @@ cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
>       /* Maybe we don't expect to see any arguments for this attribute.  */
>       const attribute_spec *as
>         = lookup_attribute_spec (TREE_PURPOSE (attribute));
> -    if (as && as->max_length == 0)
> +    if ((as && as->max_length == 0) || is_attribute_p ("musttail", attr_id))

This shouldn't be necessary with the attribute in the c-attribs table, 
right?  This patch is OK without this hunk and with the comment tweak above.

Jason


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

* Re: [PATCH v7 6/9] Add tests for C/C++ musttail attributes
  2024-06-02 17:16 ` [PATCH v7 6/9] Add tests for C/C++ musttail attributes Andi Kleen
@ 2024-06-03 14:46   ` Jason Merrill
  0 siblings, 0 replies; 23+ messages in thread
From: Jason Merrill @ 2024-06-03 14:46 UTC (permalink / raw)
  To: Andi Kleen, gcc-patches
  Cc: richard.guenther, nathan, josmyers, richard.sandiford

On 6/2/24 13:16, Andi Kleen wrote:
> Mostly adopted from the existing C musttail plugin tests.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* c-c++-common/musttail1.c: New test.
> 	* c-c++-common/musttail2.c: New test.
> 	* c-c++-common/musttail3.c: New test.
> 	* c-c++-common/musttail4.c: New test.
> 	* c-c++-common/musttail7.c: New test.
> 	* c-c++-common/musttail8.c: New test.
> 	* g++.dg/musttail6.C: New test.
> 	* g++.dg/musttail9.C: New test.
> 	* g++.dg/musttail10.C: New test.
> ---
>   gcc/testsuite/c-c++-common/musttail1.c | 14 +++++++
>   gcc/testsuite/c-c++-common/musttail2.c | 33 +++++++++++++++
>   gcc/testsuite/c-c++-common/musttail3.c | 29 +++++++++++++
>   gcc/testsuite/c-c++-common/musttail4.c | 17 ++++++++
>   gcc/testsuite/c-c++-common/musttail5.c | 28 +++++++++++++
>   gcc/testsuite/c-c++-common/musttail7.c | 14 +++++++
>   gcc/testsuite/c-c++-common/musttail8.c | 17 ++++++++
>   gcc/testsuite/g++.dg/musttail10.C      | 20 +++++++++
>   gcc/testsuite/g++.dg/musttail6.C       | 58 ++++++++++++++++++++++++++
>   gcc/testsuite/g++.dg/musttail9.C       | 10 +++++
>   10 files changed, 240 insertions(+)
>   create mode 100644 gcc/testsuite/c-c++-common/musttail1.c
>   create mode 100644 gcc/testsuite/c-c++-common/musttail2.c
>   create mode 100644 gcc/testsuite/c-c++-common/musttail3.c
>   create mode 100644 gcc/testsuite/c-c++-common/musttail4.c
>   create mode 100644 gcc/testsuite/c-c++-common/musttail5.c
>   create mode 100644 gcc/testsuite/c-c++-common/musttail7.c
>   create mode 100644 gcc/testsuite/c-c++-common/musttail8.c
>   create mode 100644 gcc/testsuite/g++.dg/musttail10.C
>   create mode 100644 gcc/testsuite/g++.dg/musttail6.C
>   create mode 100644 gcc/testsuite/g++.dg/musttail9.C
> 
> diff --git a/gcc/testsuite/c-c++-common/musttail1.c b/gcc/testsuite/c-c++-common/musttail1.c
> new file mode 100644
> index 000000000000..74efcc2a0bc6
> --- /dev/null
> +++ b/gcc/testsuite/c-c++-common/musttail1.c
> @@ -0,0 +1,14 @@
> +/* { dg-do compile { target { tail_call && { c || c++11 } } } } */
> +/* { dg-additional-options "-fdelayed-branch" { target sparc*-*-* } } */
> +
> +int __attribute__((noinline,noclone,noipa))
> +callee (int i)
> +{
> +  return i * i;
> +}
> +
> +int __attribute__((noinline,noclone,noipa))
> +caller (int i)
> +{
> +  [[gnu::musttail]] return callee (i + 1);
> +}
> diff --git a/gcc/testsuite/c-c++-common/musttail2.c b/gcc/testsuite/c-c++-common/musttail2.c
> new file mode 100644
> index 000000000000..86f2c3d77404
> --- /dev/null
> +++ b/gcc/testsuite/c-c++-common/musttail2.c
> @@ -0,0 +1,33 @@
> +/* { dg-do compile { target { tail_call && { c || c++11 } } } } */
> +
> +struct box { char field[256]; int i; };
> +
> +int __attribute__((noinline,noclone,noipa))
> +test_2_callee (int i, struct box b)
> +{
> +  if (b.field[0])
> +    return 5;
> +  return i * i;
> +}
> +
> +int __attribute__((noinline,noclone,noipa))
> +test_2_caller (int i)
> +{
> +  struct box b;
> +  [[gnu::musttail]] return test_2_callee (i + 1, b); /* { dg-error "cannot tail-call: " } */
> +}
> +
> +extern void setjmp (void);
> +void
> +test_3 (void)
> +{
> +  [[gnu::musttail]] return setjmp (); /* { dg-error "cannot tail-call: " } */
> +}
> +
> +extern float f7(void);
> +
> +int
> +test_6 (void)
> +{
> +  [[gnu::musttail]] return f7(); /* { dg-error "cannot tail-call: " } */
> +}
> diff --git a/gcc/testsuite/c-c++-common/musttail3.c b/gcc/testsuite/c-c++-common/musttail3.c
> new file mode 100644
> index 000000000000..ea9589c59ef2
> --- /dev/null
> +++ b/gcc/testsuite/c-c++-common/musttail3.c
> @@ -0,0 +1,29 @@
> +/* { dg-do compile { target { tail_call && { c || c++11 } } } } */
> +
> +extern int foo2 (int x, ...);
> +
> +struct str
> +{
> +  int a, b;
> +};
> +
> +struct str
> +cstruct (int x)
> +{
> +  if (x < 10)
> +    [[clang::musttail]] return cstruct (x + 1);
> +  return ((struct str){ x, 0 });
> +}
> +
> +int
> +foo (int x)
> +{
> +  if (x < 10)
> +    [[clang::musttail]] return foo2 (x, 29);
> +  if (x < 100)
> +    {
> +      int k = foo (x + 1);
> +      [[clang::musttail]] return k;	/* { dg-error "cannot tail-call: " } */
> +    }
> +  return x;
> +}
> diff --git a/gcc/testsuite/c-c++-common/musttail4.c b/gcc/testsuite/c-c++-common/musttail4.c
> new file mode 100644
> index 000000000000..23f4b5e1cd68
> --- /dev/null
> +++ b/gcc/testsuite/c-c++-common/musttail4.c
> @@ -0,0 +1,17 @@
> +/* { dg-do compile { target { tail_call && { c || c++11 } } } } */
> +
> +struct box { char field[64]; int i; };
> +
> +struct box __attribute__((noinline,noclone,noipa))
> +returns_struct (int i)
> +{
> +  struct box b;
> +  b.i = i * i;
> +  return b;
> +}
> +
> +int __attribute__((noinline,noclone))
> +test_1 (int i)
> +{
> +  [[gnu::musttail]] return returns_struct (i * 5).i; /* { dg-error "cannot tail-call: " } */
> +}
> diff --git a/gcc/testsuite/c-c++-common/musttail5.c b/gcc/testsuite/c-c++-common/musttail5.c
> new file mode 100644
> index 000000000000..234da0d3f2a9
> --- /dev/null
> +++ b/gcc/testsuite/c-c++-common/musttail5.c
> @@ -0,0 +1,28 @@
> +/* { dg-do compile } */
> +/* { dg-options "-std=c23" { target c } } */
> +/* { dg-options "-std=gnu++11" { target c++ } } */
> +
> +[[musttail]] int j; /* { dg-warning "attribute" } */
> +__attribute__((musttail)) int k; /* { dg-warning "attribute" } */
> +
> +void foo(void)
> +{
> +  [[gnu::musttail]] j++; /* { dg-warning "attribute" } */
> +  [[gnu::musttail]] if (k > 0) /* { dg-warning "attribute" } */
> +    [[gnu::musttail]] k++; /* { dg-warning "attribute" } */
> +}
> +
> +int foo2(int p)
> +{
> +  [[gnu::musttail(1)]] return foo2(p + 1); /* { dg-error "\(before numeric constant|attribute\)" } */
> +}
> +
> +int i;
> +
> +int foo3(void)
> +{
> +  [[musttail]] i++; /* { dg-warning "attribute" } */
> +  [[musttail]] if (i > 10) /* { dg-warning "attribute" } */
> +    [[musttail]] return foo2(i); /* { dg-warning "attribute" } */
> +  return 0;
> +}
> diff --git a/gcc/testsuite/c-c++-common/musttail7.c b/gcc/testsuite/c-c++-common/musttail7.c
> new file mode 100644
> index 000000000000..5e4eb1bfbacc
> --- /dev/null
> +++ b/gcc/testsuite/c-c++-common/musttail7.c
> @@ -0,0 +1,14 @@
> +/* { dg-do compile { target { tail_call && { c || c++11 } } } } */
> +/* { dg-additional-options "-fdelayed-branch" { target sparc*-*-* } } */
> +
> +extern void f();
> +
> +void f2()
> +{
> +  [[gnu::musttail]] return f2();
> +}
> +
> +void f3()
> +{
> +  [[gnu::musttail]] return f();
> +}
> diff --git a/gcc/testsuite/c-c++-common/musttail8.c b/gcc/testsuite/c-c++-common/musttail8.c
> new file mode 100644
> index 000000000000..9fa10e0b54c4
> --- /dev/null
> +++ b/gcc/testsuite/c-c++-common/musttail8.c
> @@ -0,0 +1,17 @@
> +/* { dg-do compile { target { tail_call && { c || c++11 } } } } */
> +
> +float f1(void);
> +
> +int f2(void)
> +{
> +  [[gnu::musttail]] return f1 (); /* { dg-error "changed after call" } */
> +}
> +
> +
> +int f3(int *);
> +
> +int f4(void)
> +{
> +  int x;
> +  [[gnu::musttail]] return f3(&x); /* { dg-error "\(refers to locals|other reasons\)" } */
> +}
> diff --git a/gcc/testsuite/g++.dg/musttail10.C b/gcc/testsuite/g++.dg/musttail10.C
> new file mode 100644
> index 000000000000..6da7e021f826
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/musttail10.C
> @@ -0,0 +1,20 @@
> +/* { dg-do compile { target { tail_call } } } */
> +/* { dg-options "-std=gnu++11" } */
> +/* { dg-additional-options "-fdelayed-branch" { target sparc*-*-* } } */
> +
> +int f();
> +
> +double h() { [[gnu::musttail]] return f(); } /* { dg-error "cannot tail-call" } */
> +
> +/* The error check cannot distinguish between the (valid) int and the invalid
> +   double conversion case because the template context is lost when tree-tailcall
> +   finally runs.  */

Maybe use identical g1 and g2 to distinguish?

> +
> +template <class T>
> +T g() { [[gnu::musttail]] return f(); } /* { dg-error "cannot tail-call" } */
> +
> +int main()
> +{
> +  g<int>();
> +  g<double>();
> +}
> diff --git a/gcc/testsuite/g++.dg/musttail6.C b/gcc/testsuite/g++.dg/musttail6.C
> new file mode 100644
> index 000000000000..e0e478e08d58
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/musttail6.C
> @@ -0,0 +1,58 @@
> +/* { dg-do compile { target { tail_call } } } */
> +/* { dg-options "-std=gnu++11" } */
> +/* { dg-additional-options "-fdelayed-branch" { target sparc*-*-* } } */
> +
> +class Foo {
> +public:
> +  int a, b;
> +  Foo(int a, int b) : a(a), b(b) {}
> +};
> +
> +Foo __attribute__((noinline,noclone,noipa))
> +callee (int i)
> +{
> +  return Foo(i, i+1);
> +}
> +
> +Foo __attribute__((noinline,noclone,noipa))
> +caller (int i)
> +{
> +  [[gnu::musttail]] return callee (i + 1);
> +}
> +
> +template<typename T>
> +T __attribute__((noinline,noclone,noipa)) foo (T i)
> +{
> +  return i + 1;
> +}
> +
> +int
> +caller2 (int k)
> +{
> +  [[gnu::musttail]] return foo<int>(1);
> +}
> +
> +template<typename T>
> +T caller3 (T v)
> +{
> +  [[gnu::musttail]] return foo<T>(v);
> +}
> +
> +int call3(int i)
> +{
> +  [[gnu::musttail]] return caller3<int>(i + 1);
> +}
> +
> +struct Bar {
> +  int a;
> +  Bar(int a) : a(a) {}
> +  Bar operator+(Bar o) { return Bar(a + o.a); }
> +};
> +
> +#if __OPTIMIZE__ >= 1
> +Bar
> +caller4 (Bar k)
> +{
> +  [[gnu::musttail]] return caller3<Bar>(Bar(99));
> +}
> +#endif
> diff --git a/gcc/testsuite/g++.dg/musttail9.C b/gcc/testsuite/g++.dg/musttail9.C
> new file mode 100644
> index 000000000000..8095bdf6fe62
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/musttail9.C
> @@ -0,0 +1,10 @@
> +/* { dg-do compile { target { tail_call } } } */
> +/* { dg-options "-std=gnu++11 -Wno-deprecated" } */
> +/* { dg-additional-options "-fdelayed-branch" { target sparc*-*-* } } */
> +
> +extern void foo() throw(const char *);
> +
> +void f() throw()
> +{
> +  [[gnu::musttail]] return foo(); /* { dg-error "call may throw exception that does not propagate" } */
> +}

Instead of -Wno-deprecated let's use C++11 exception-specifications; you 
can leave out the exception specification on foo and change f's to 
"noexcept".

Jason


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

* Re: [PATCH v7 4/9] C++: Support clang compatible [[musttail]] (PR83324)
  2024-06-03 14:42   ` Jason Merrill
@ 2024-06-03 15:33     ` Andi Kleen
  2024-06-03 15:44       ` Jakub Jelinek
  0 siblings, 1 reply; 23+ messages in thread
From: Andi Kleen @ 2024-06-03 15:33 UTC (permalink / raw)
  To: Jason Merrill
  Cc: gcc-patches, richard.guenther, nathan, josmyers, richard.sandiford

On Mon, Jun 03, 2024 at 10:42:20AM -0400, Jason Merrill wrote:
> > @@ -30316,7 +30348,7 @@ cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
> >       /* Maybe we don't expect to see any arguments for this attribute.  */
> >       const attribute_spec *as
> >         = lookup_attribute_spec (TREE_PURPOSE (attribute));
> > -    if (as && as->max_length == 0)
> > +    if ((as && as->max_length == 0) || is_attribute_p ("musttail", attr_id))
> 
> This shouldn't be necessary with the attribute in the c-attribs table,
> right?  This patch is OK without this hunk and with the comment tweak above.

Yes I will remove it. Also the hunk above can be simplified, we don't
need the extra case anymore.

But unfortunately there's another problem (sorry I missed that earlier
but the Linaro bot pointed it out again):

This hunk:

@@ -21085,12 +21085,14 @@ tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
 	    bool op = CALL_EXPR_OPERATOR_SYNTAX (t);
 	    bool ord = CALL_EXPR_ORDERED_ARGS (t);
 	    bool rev = CALL_EXPR_REVERSE_ARGS (t);
-	    if (op || ord || rev)
+	    bool mtc = CALL_EXPR_MUST_TAIL_CALL (t);
+	    if (op || ord || rev || mtc)
 	      if (tree call = extract_call_expr (ret))
 		{
 		  CALL_EXPR_OPERATOR_SYNTAX (call) = op;
 		  CALL_EXPR_ORDERED_ARGS (call) = ord;
 		  CALL_EXPR_REVERSE_ARGS (call) = rev;
+		  CALL_EXPR_MUST_TAIL_CALL (call) = mtc;
 		}
 	    if (warning_suppressed_p (t, OPT_Wpessimizing_move))
 	      /* This also suppresses -Wredundant-move.  */


causes 

/home/ak/gcc/gcc/gcc/testsuite/g++.dg/ipa/devirt-52.C:49:8: internal
compiler error: tree check: expected call_expr, have aggr_init_expr in
tsubst_expr, at cp/pt.cc:21095
0x910b66 tree_check_failed(tree_node const*, char const*, int, char
const*, ...)
        ../../gcc/gcc/tree.cc:8995


I suspect that's because CALL_EXPR_MUST_TAIL_CALL uses
tree_core->static_flag, but that means something else during
template expansion?

Any suggestions how to fix that?

-Andi

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

* Re: [PATCH v7 4/9] C++: Support clang compatible [[musttail]] (PR83324)
  2024-06-03 15:33     ` Andi Kleen
@ 2024-06-03 15:44       ` Jakub Jelinek
  2024-06-03 16:29         ` Jason Merrill
  0 siblings, 1 reply; 23+ messages in thread
From: Jakub Jelinek @ 2024-06-03 15:44 UTC (permalink / raw)
  To: Andi Kleen
  Cc: Jason Merrill, gcc-patches, richard.guenther, nathan, josmyers,
	richard.sandiford

On Mon, Jun 03, 2024 at 08:33:52AM -0700, Andi Kleen wrote:
> On Mon, Jun 03, 2024 at 10:42:20AM -0400, Jason Merrill wrote:
> > > @@ -30316,7 +30348,7 @@ cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
> > >       /* Maybe we don't expect to see any arguments for this attribute.  */
> > >       const attribute_spec *as
> > >         = lookup_attribute_spec (TREE_PURPOSE (attribute));
> > > -    if (as && as->max_length == 0)
> > > +    if ((as && as->max_length == 0) || is_attribute_p ("musttail", attr_id))
> > 
> > This shouldn't be necessary with the attribute in the c-attribs table,
> > right?  This patch is OK without this hunk and with the comment tweak above.
> 
> Yes I will remove it. Also the hunk above can be simplified, we don't
> need the extra case anymore.
> 
> But unfortunately there's another problem (sorry I missed that earlier
> but the Linaro bot pointed it out again):
> 
> This hunk:
> 
> @@ -21085,12 +21085,14 @@ tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
>  	    bool op = CALL_EXPR_OPERATOR_SYNTAX (t);
>  	    bool ord = CALL_EXPR_ORDERED_ARGS (t);
>  	    bool rev = CALL_EXPR_REVERSE_ARGS (t);
> -	    if (op || ord || rev)
> +	    bool mtc = CALL_EXPR_MUST_TAIL_CALL (t);
> +	    if (op || ord || rev || mtc)
>  	      if (tree call = extract_call_expr (ret))
>  		{
>  		  CALL_EXPR_OPERATOR_SYNTAX (call) = op;
>  		  CALL_EXPR_ORDERED_ARGS (call) = ord;
>  		  CALL_EXPR_REVERSE_ARGS (call) = rev;
> +		  CALL_EXPR_MUST_TAIL_CALL (call) = mtc;
>  		}

The difference is that CALL_EXPR_MUST_TAIL_CALL is defined as:
#define CALL_EXPR_MUST_TAIL_CALL(NODE) \
  (CALL_EXPR_CHECK (NODE)->base.static_flag)
while the others like:
#define CALL_EXPR_ORDERED_ARGS(NODE) \
  TREE_LANG_FLAG_3 (CALL_OR_AGGR_INIT_CHECK (NODE))
where
#define CALL_OR_AGGR_INIT_CHECK(NODE) \
  TREE_CHECK2 ((NODE), CALL_EXPR, AGGR_INIT_EXPR)
while
#define CALL_EXPR_CHECK(t)	TREE_CHECK (t, CALL_EXPR)
(this one is defined in generated tree-check.h).
So, while the CALL_EXPR_REVERSE_ARGS etc. can be used on either
CALL_EXPR or AGGR_INIT_EXPR (the latter is a C++ specific tree code),
CALL_EXPR_MUST_TAIL_CALL is allowed only on CALL_EXPR.
AGGR_INIT_EXPR is used for C++ constructor calls, so I think
they really don't need such a flag, so you could do:
	    bool mtc = (TREE_CODE (t) == CALL_EXPR
			? CALL_EXPR_MUST_TAIL_CALL (t) : false);
	    if (op || ord || rev || mtc)
...
	      if (mtc)
		CALL_EXPR_MUST_TAIL_CALL (call) = 1;
or something similar.
Or you'd need to define a variant of the CALL_EXPR_MUST_TAIL_CALL
macro for the C++ FE (as CALL_OR_AGGR_INIT_CHECK is C++ FE too)
and use that in the FE and somehow assert it means the same thing
as the middle-end flag except that it can be also used on AGGR_INIT_EXPR.

	Jakub


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

* Re: [PATCH v7 4/9] C++: Support clang compatible [[musttail]] (PR83324)
  2024-06-03 15:44       ` Jakub Jelinek
@ 2024-06-03 16:29         ` Jason Merrill
  2024-06-03 19:35           ` Andi Kleen
  0 siblings, 1 reply; 23+ messages in thread
From: Jason Merrill @ 2024-06-03 16:29 UTC (permalink / raw)
  To: Jakub Jelinek, Andi Kleen
  Cc: gcc-patches, richard.guenther, nathan, josmyers, richard.sandiford

On 6/3/24 11:44, Jakub Jelinek wrote:
> On Mon, Jun 03, 2024 at 08:33:52AM -0700, Andi Kleen wrote:
>> On Mon, Jun 03, 2024 at 10:42:20AM -0400, Jason Merrill wrote:
>>>> @@ -30316,7 +30348,7 @@ cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
>>>>        /* Maybe we don't expect to see any arguments for this attribute.  */
>>>>        const attribute_spec *as
>>>>          = lookup_attribute_spec (TREE_PURPOSE (attribute));
>>>> -    if (as && as->max_length == 0)
>>>> +    if ((as && as->max_length == 0) || is_attribute_p ("musttail", attr_id))
>>>
>>> This shouldn't be necessary with the attribute in the c-attribs table,
>>> right?  This patch is OK without this hunk and with the comment tweak above.
>>
>> Yes I will remove it. Also the hunk above can be simplified, we don't
>> need the extra case anymore.
>>
>> But unfortunately there's another problem (sorry I missed that earlier
>> but the Linaro bot pointed it out again):
>>
>> This hunk:
>>
>> @@ -21085,12 +21085,14 @@ tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
>>   	    bool op = CALL_EXPR_OPERATOR_SYNTAX (t);
>>   	    bool ord = CALL_EXPR_ORDERED_ARGS (t);
>>   	    bool rev = CALL_EXPR_REVERSE_ARGS (t);
>> -	    if (op || ord || rev)
>> +	    bool mtc = CALL_EXPR_MUST_TAIL_CALL (t);
>> +	    if (op || ord || rev || mtc)
>>   	      if (tree call = extract_call_expr (ret))
>>   		{
>>   		  CALL_EXPR_OPERATOR_SYNTAX (call) = op;
>>   		  CALL_EXPR_ORDERED_ARGS (call) = ord;
>>   		  CALL_EXPR_REVERSE_ARGS (call) = rev;
>> +		  CALL_EXPR_MUST_TAIL_CALL (call) = mtc;
>>   		}
> 
> The difference is that CALL_EXPR_MUST_TAIL_CALL is defined as:
> #define CALL_EXPR_MUST_TAIL_CALL(NODE) \
>    (CALL_EXPR_CHECK (NODE)->base.static_flag)
> while the others like:
> #define CALL_EXPR_ORDERED_ARGS(NODE) \
>    TREE_LANG_FLAG_3 (CALL_OR_AGGR_INIT_CHECK (NODE))
> where
> #define CALL_OR_AGGR_INIT_CHECK(NODE) \
>    TREE_CHECK2 ((NODE), CALL_EXPR, AGGR_INIT_EXPR)
> while
> #define CALL_EXPR_CHECK(t)	TREE_CHECK (t, CALL_EXPR)
> (this one is defined in generated tree-check.h).
> So, while the CALL_EXPR_REVERSE_ARGS etc. can be used on either
> CALL_EXPR or AGGR_INIT_EXPR (the latter is a C++ specific tree code),
> CALL_EXPR_MUST_TAIL_CALL is allowed only on CALL_EXPR.
> AGGR_INIT_EXPR is used for C++ constructor calls, so I think
> they really don't need such a flag

AGGR_INIT_EXPR is also used for functions returning a class, so I think 
it is needed.

Jason


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

* Re: [PATCH v7 4/9] C++: Support clang compatible [[musttail]] (PR83324)
  2024-06-03 16:29         ` Jason Merrill
@ 2024-06-03 19:35           ` Andi Kleen
  2024-06-03 20:27             ` Jason Merrill
  0 siblings, 1 reply; 23+ messages in thread
From: Andi Kleen @ 2024-06-03 19:35 UTC (permalink / raw)
  To: Jason Merrill
  Cc: Jakub Jelinek, gcc-patches, richard.guenther, nathan, josmyers,
	richard.sandiford

On Mon, Jun 03, 2024 at 12:29:28PM -0400, Jason Merrill wrote:
> On 6/3/24 11:44, Jakub Jelinek wrote:
> > On Mon, Jun 03, 2024 at 08:33:52AM -0700, Andi Kleen wrote:
> > > On Mon, Jun 03, 2024 at 10:42:20AM -0400, Jason Merrill wrote:
> > > > > @@ -30316,7 +30348,7 @@ cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
> > > > >        /* Maybe we don't expect to see any arguments for this attribute.  */
> > > > >        const attribute_spec *as
> > > > >          = lookup_attribute_spec (TREE_PURPOSE (attribute));
> > > > > -    if (as && as->max_length == 0)
> > > > > +    if ((as && as->max_length == 0) || is_attribute_p ("musttail", attr_id))
> > > > 
> > > > This shouldn't be necessary with the attribute in the c-attribs table,
> > > > right?  This patch is OK without this hunk and with the comment tweak above.
> > > 
> > > Yes I will remove it. Also the hunk above can be simplified, we don't
> > > need the extra case anymore.
> > > 
> > > But unfortunately there's another problem (sorry I missed that earlier
> > > but the Linaro bot pointed it out again):
> > > 
> > > This hunk:
> > > 
> > > @@ -21085,12 +21085,14 @@ tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
> > >   	    bool op = CALL_EXPR_OPERATOR_SYNTAX (t);
> > >   	    bool ord = CALL_EXPR_ORDERED_ARGS (t);
> > >   	    bool rev = CALL_EXPR_REVERSE_ARGS (t);
> > > -	    if (op || ord || rev)
> > > +	    bool mtc = CALL_EXPR_MUST_TAIL_CALL (t);
> > > +	    if (op || ord || rev || mtc)
> > >   	      if (tree call = extract_call_expr (ret))
> > >   		{
> > >   		  CALL_EXPR_OPERATOR_SYNTAX (call) = op;
> > >   		  CALL_EXPR_ORDERED_ARGS (call) = ord;
> > >   		  CALL_EXPR_REVERSE_ARGS (call) = rev;
> > > +		  CALL_EXPR_MUST_TAIL_CALL (call) = mtc;
> > >   		}
> > 
> > The difference is that CALL_EXPR_MUST_TAIL_CALL is defined as:
> > #define CALL_EXPR_MUST_TAIL_CALL(NODE) \
> >    (CALL_EXPR_CHECK (NODE)->base.static_flag)
> > while the others like:
> > #define CALL_EXPR_ORDERED_ARGS(NODE) \
> >    TREE_LANG_FLAG_3 (CALL_OR_AGGR_INIT_CHECK (NODE))
> > where
> > #define CALL_OR_AGGR_INIT_CHECK(NODE) \
> >    TREE_CHECK2 ((NODE), CALL_EXPR, AGGR_INIT_EXPR)
> > while
> > #define CALL_EXPR_CHECK(t)	TREE_CHECK (t, CALL_EXPR)
> > (this one is defined in generated tree-check.h).
> > So, while the CALL_EXPR_REVERSE_ARGS etc. can be used on either
> > CALL_EXPR or AGGR_INIT_EXPR (the latter is a C++ specific tree code),
> > CALL_EXPR_MUST_TAIL_CALL is allowed only on CALL_EXPR.
> > AGGR_INIT_EXPR is used for C++ constructor calls, so I think
> > they really don't need such a flag
> 
> AGGR_INIT_EXPR is also used for functions returning a class, so I think it
> is needed.

I used this variant which passes tests. It assumes that there are no
wrapped calls with this flag, but I assume that's ok.

diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
index f6af8e1a81e4..c50fea4282e4 100644
--- a/gcc/cp/pt.cc
+++ b/gcc/cp/pt.cc
@@ -21085,14 +21085,17 @@ tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
 	    bool op = CALL_EXPR_OPERATOR_SYNTAX (t);
 	    bool ord = CALL_EXPR_ORDERED_ARGS (t);
 	    bool rev = CALL_EXPR_REVERSE_ARGS (t);
-	    bool mtc = CALL_EXPR_MUST_TAIL_CALL (t);
+	    bool mtc = false;
+	    if (TREE_CODE (t) == CALL_EXPR)
+	      mtc = CALL_EXPR_MUST_TAIL_CALL (t);
 	    if (op || ord || rev || mtc)
 	      if (tree call = extract_call_expr (ret))
 		{
 		  CALL_EXPR_OPERATOR_SYNTAX (call) = op;
 		  CALL_EXPR_ORDERED_ARGS (call) = ord;
 		  CALL_EXPR_REVERSE_ARGS (call) = rev;
-		  CALL_EXPR_MUST_TAIL_CALL (call) = mtc;
+		  if (TREE_CODE (call) == CALL_EXPR)
+		    CALL_EXPR_MUST_TAIL_CALL (call) = mtc;
 		}
 	    if (warning_suppressed_p (t, OPT_Wpessimizing_move))
 	      /* This also suppresses -Wredundant-move.  */


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

* Re: [PATCH v7 4/9] C++: Support clang compatible [[musttail]] (PR83324)
  2024-06-03 19:35           ` Andi Kleen
@ 2024-06-03 20:27             ` Jason Merrill
  2024-06-04  0:17               ` Andi Kleen
  0 siblings, 1 reply; 23+ messages in thread
From: Jason Merrill @ 2024-06-03 20:27 UTC (permalink / raw)
  To: Andi Kleen
  Cc: Jakub Jelinek, gcc-patches, richard.guenther, nathan, josmyers,
	richard.sandiford

On 6/3/24 15:35, Andi Kleen wrote:
> On Mon, Jun 03, 2024 at 12:29:28PM -0400, Jason Merrill wrote:
>> On 6/3/24 11:44, Jakub Jelinek wrote:
>>> On Mon, Jun 03, 2024 at 08:33:52AM -0700, Andi Kleen wrote:
>>>> On Mon, Jun 03, 2024 at 10:42:20AM -0400, Jason Merrill wrote:
>>>>>> @@ -30316,7 +30348,7 @@ cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
>>>>>>         /* Maybe we don't expect to see any arguments for this attribute.  */
>>>>>>         const attribute_spec *as
>>>>>>           = lookup_attribute_spec (TREE_PURPOSE (attribute));
>>>>>> -    if (as && as->max_length == 0)
>>>>>> +    if ((as && as->max_length == 0) || is_attribute_p ("musttail", attr_id))
>>>>>
>>>>> This shouldn't be necessary with the attribute in the c-attribs table,
>>>>> right?  This patch is OK without this hunk and with the comment tweak above.
>>>>
>>>> Yes I will remove it. Also the hunk above can be simplified, we don't
>>>> need the extra case anymore.
>>>>
>>>> But unfortunately there's another problem (sorry I missed that earlier
>>>> but the Linaro bot pointed it out again):
>>>>
>>>> This hunk:
>>>>
>>>> @@ -21085,12 +21085,14 @@ tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
>>>>    	    bool op = CALL_EXPR_OPERATOR_SYNTAX (t);
>>>>    	    bool ord = CALL_EXPR_ORDERED_ARGS (t);
>>>>    	    bool rev = CALL_EXPR_REVERSE_ARGS (t);
>>>> -	    if (op || ord || rev)
>>>> +	    bool mtc = CALL_EXPR_MUST_TAIL_CALL (t);
>>>> +	    if (op || ord || rev || mtc)
>>>>    	      if (tree call = extract_call_expr (ret))
>>>>    		{
>>>>    		  CALL_EXPR_OPERATOR_SYNTAX (call) = op;
>>>>    		  CALL_EXPR_ORDERED_ARGS (call) = ord;
>>>>    		  CALL_EXPR_REVERSE_ARGS (call) = rev;
>>>> +		  CALL_EXPR_MUST_TAIL_CALL (call) = mtc;
>>>>    		}
>>>
>>> The difference is that CALL_EXPR_MUST_TAIL_CALL is defined as:
>>> #define CALL_EXPR_MUST_TAIL_CALL(NODE) \
>>>     (CALL_EXPR_CHECK (NODE)->base.static_flag)
>>> while the others like:
>>> #define CALL_EXPR_ORDERED_ARGS(NODE) \
>>>     TREE_LANG_FLAG_3 (CALL_OR_AGGR_INIT_CHECK (NODE))
>>> where
>>> #define CALL_OR_AGGR_INIT_CHECK(NODE) \
>>>     TREE_CHECK2 ((NODE), CALL_EXPR, AGGR_INIT_EXPR)
>>> while
>>> #define CALL_EXPR_CHECK(t)	TREE_CHECK (t, CALL_EXPR)
>>> (this one is defined in generated tree-check.h).
>>> So, while the CALL_EXPR_REVERSE_ARGS etc. can be used on either
>>> CALL_EXPR or AGGR_INIT_EXPR (the latter is a C++ specific tree code),
>>> CALL_EXPR_MUST_TAIL_CALL is allowed only on CALL_EXPR.
>>> AGGR_INIT_EXPR is used for C++ constructor calls, so I think
>>> they really don't need such a flag
>>
>> AGGR_INIT_EXPR is also used for functions returning a class, so I think it
>> is needed.
> 
> I used this variant which passes tests. It assumes that there are no
> wrapped calls with this flag, but I assume that's ok.

musttail10.C should also check the case where the function called with 
[[musttail]] returns a non-trivially-copyable class (e.g. that has a 
destructor).

> diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
> index f6af8e1a81e4..c50fea4282e4 100644
> --- a/gcc/cp/pt.cc
> +++ b/gcc/cp/pt.cc
> @@ -21085,14 +21085,17 @@ tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
>   	    bool op = CALL_EXPR_OPERATOR_SYNTAX (t);
>   	    bool ord = CALL_EXPR_ORDERED_ARGS (t);
>   	    bool rev = CALL_EXPR_REVERSE_ARGS (t);
> -	    bool mtc = CALL_EXPR_MUST_TAIL_CALL (t);
> +	    bool mtc = false;
> +	    if (TREE_CODE (t) == CALL_EXPR)
> +	      mtc = CALL_EXPR_MUST_TAIL_CALL (t);
>   	    if (op || ord || rev || mtc)
>   	      if (tree call = extract_call_expr (ret))
>   		{
>   		  CALL_EXPR_OPERATOR_SYNTAX (call) = op;
>   		  CALL_EXPR_ORDERED_ARGS (call) = ord;
>   		  CALL_EXPR_REVERSE_ARGS (call) = rev;
> -		  CALL_EXPR_MUST_TAIL_CALL (call) = mtc;
> +		  if (TREE_CODE (call) == CALL_EXPR)
> +		    CALL_EXPR_MUST_TAIL_CALL (call) = mtc;
>   		}
>   	    if (warning_suppressed_p (t, OPT_Wpessimizing_move))
>   	      /* This also suppresses -Wredundant-move.  */
> 


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

* Re: [PATCH v7 4/9] C++: Support clang compatible [[musttail]] (PR83324)
  2024-06-03 20:27             ` Jason Merrill
@ 2024-06-04  0:17               ` Andi Kleen
  0 siblings, 0 replies; 23+ messages in thread
From: Andi Kleen @ 2024-06-04  0:17 UTC (permalink / raw)
  To: Jason Merrill
  Cc: Jakub Jelinek, gcc-patches, richard.guenther, nathan, josmyers,
	richard.sandiford

> > I used this variant which passes tests. It assumes that there are no
> > wrapped calls with this flag, but I assume that's ok.
> 
> musttail10.C should also check the case where the function called with
> [[musttail]] returns a non-trivially-copyable class (e.g. that has a
> destructor).

It fails as expected, however the resulting tree code confuses
tree-tailcall, so the end result is a not very helpful
"cannot tail-call: other reasons" error when only expand
catches it.

I may fix that later, but I don't think it's critical for the
initial feature. There are some other cases where this happens too.

I also improved the test case by running it in "torture" to cover
more cases

-Andi

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

* [PING] Re: [PATCH v7 1/9] Improve must tail in RTL backend
  2024-06-02 17:16 ` [PATCH v7 1/9] Improve must tail in RTL backend Andi Kleen
@ 2024-06-10  0:55   ` Andi Kleen
  2024-06-14 13:43     ` [PING^2] " Andi Kleen
  0 siblings, 1 reply; 23+ messages in thread
From: Andi Kleen @ 2024-06-10  0:55 UTC (permalink / raw)
  To: gcc-patches; +Cc: richard.guenther, nathan, josmyers, richard.sandiford, jason


Need reviewers for the tree and middle-end parts, as well as the C frontend.

Thanks!

-Andi

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

* Re: [PING^2] Re: [PATCH v7 1/9] Improve must tail in RTL backend
  2024-06-10  0:55   ` [PING] " Andi Kleen
@ 2024-06-14 13:43     ` Andi Kleen
  2024-06-20 18:13       ` [PING^3] " Andi Kleen
  0 siblings, 1 reply; 23+ messages in thread
From: Andi Kleen @ 2024-06-14 13:43 UTC (permalink / raw)
  To: gcc-patches; +Cc: richard.guenther, nathan, josmyers, richard.sandiford, jason

Andi Kleen <ak@linux.intel.com> writes:

PING^2

> Need reviewers for the tree and middle-end parts, as well as the C frontend.
>
> Thanks!
>
> -Andi

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

* [PING^3] Re: [PATCH v7 1/9] Improve must tail in RTL backend
  2024-06-14 13:43     ` [PING^2] " Andi Kleen
@ 2024-06-20 18:13       ` Andi Kleen
  0 siblings, 0 replies; 23+ messages in thread
From: Andi Kleen @ 2024-06-20 18:13 UTC (permalink / raw)
  To: gcc-patches; +Cc: richard.guenther, nathan, josmyers, richard.sandiford, jason

Andi Kleen <ak@linux.intel.com> writes:

PING^3 for the musttail patchkit at
https://gcc.gnu.org/pipermail/gcc-patches/2024-June/653319.html
(except the C++ patch which got approved) 

Thanks!
-Andi


> Andi Kleen <ak@linux.intel.com> writes:
>
> PING^2
>
>> Need reviewers for the tree and middle-end parts, as well as the C frontend.
>>
>> Thanks!
>>
>> -Andi

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

end of thread, other threads:[~2024-06-20 18:13 UTC | newest]

Thread overview: 23+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-06-02 17:16 Updated musttail patchkit Andi Kleen
2024-06-02 17:16 ` [PATCH v7 1/9] Improve must tail in RTL backend Andi Kleen
2024-06-10  0:55   ` [PING] " Andi Kleen
2024-06-14 13:43     ` [PING^2] " Andi Kleen
2024-06-20 18:13       ` [PING^3] " Andi Kleen
2024-06-02 17:16 ` [PATCH v7 2/9] Fix pro_and_epilogue for sibcalls at -O0 Andi Kleen
2024-06-02 17:30   ` Andrew Pinski
2024-06-02 20:27     ` Andi Kleen
2024-06-02 17:16 ` [PATCH v7 3/9] Add a musttail generic attribute to the c-attribs table Andi Kleen
2024-06-02 17:16 ` [PATCH v7 4/9] C++: Support clang compatible [[musttail]] (PR83324) Andi Kleen
2024-06-03 14:42   ` Jason Merrill
2024-06-03 15:33     ` Andi Kleen
2024-06-03 15:44       ` Jakub Jelinek
2024-06-03 16:29         ` Jason Merrill
2024-06-03 19:35           ` Andi Kleen
2024-06-03 20:27             ` Jason Merrill
2024-06-04  0:17               ` Andi Kleen
2024-06-02 17:16 ` [PATCH v7 5/9] C: Implement musttail attribute for returns Andi Kleen
2024-06-02 17:16 ` [PATCH v7 6/9] Add tests for C/C++ musttail attributes Andi Kleen
2024-06-03 14:46   ` Jason Merrill
2024-06-02 17:16 ` [PATCH v7 7/9] Enable musttail tail conversion even when not optimizing Andi Kleen
2024-06-02 17:16 ` [PATCH v7 8/9] Give better error messages for musttail Andi Kleen
2024-06-02 17:16 ` [PATCH v7 9/9] Add documentation for musttail attribute Andi Kleen

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).