From patchwork Sun May 5 18:14:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andi Kleen X-Patchwork-Id: 1931590 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=KEeavk/4; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4VXXm20T17z1xnS for ; Mon, 6 May 2024 04:15:59 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A7CEF385B50B for ; Sun, 5 May 2024 18:15:57 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.9]) by sourceware.org (Postfix) with ESMTPS id 1C1E23858D28 for ; Sun, 5 May 2024 18:15:27 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 1C1E23858D28 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=linux.intel.com Authentication-Results: sourceware.org; spf=none smtp.mailfrom=linux.intel.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 1C1E23858D28 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=192.198.163.9 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1714932929; cv=none; b=LBGz5ezX7aHX5Vt5tpG9LYSA81CQjFe4k+JE/d5TCho1qhfAFMVVSXWVSv6KOw+NsyLLYaW0y+sdOrtBk0YjbXjzZ+KWoe7kvCMR4Dw1h3IO2iKI0yQn0BaKCEt/In1QBVtCnw/LmFwvl6zRrkIKSEh/rzF57T3FKCwDy+jiaTM= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1714932929; c=relaxed/simple; bh=wCCSEEet2oPiCbaSeWucj7kBpVN7FnMyZBQ5zpzGxmk=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=Z48KyKStXwXnVmzlfACzNSrI0qkRJcL2B9HczDQFT3MZ8PROCdrOVJtuoRckRdqtd5Z96Yuh/eja7zhQi2M/cIUwgggnPzqhccnLWa57tqhMsH8COm1Q037yN14phMESbj5vE5955d/XHMjlcowqca8DMhwtVSO2T2ULB6pI8Fg= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1714932927; x=1746468927; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=wCCSEEet2oPiCbaSeWucj7kBpVN7FnMyZBQ5zpzGxmk=; b=KEeavk/4Mu45i9WlDkcvQmc3rQWpvt483kQWRU5Lyzm6NeZ+D5F2TK4C O8JwXafncyv1DFdXHG0iwZRxtJNzRQ1gY1zE6QkvDddbJhUE/cOw37vIe pL+rCB6tVbu23TOW6vAJXBWa71TvUPYl4ZV0Rhju4JupCqFGWmcE4fdoI MKjSy9miuAj95r7q8cNpw4fPHiUTeXjguP+8H0QCDu0XmnSBQ6uXmSuCK 8FzCWmamCcklHe0rLf/Gs7lvwPfucLomMIYNdgElYiNsX3ckoEjsxz9Pq l8NjWErw80ma0HCxai2smwqE648zqkEgxCzv2FAalDHyNFJ8kZkAzG8jN g==; X-CSE-ConnectionGUID: MJIGwv7dT1ORRP8FT2AupA== X-CSE-MsgGUID: aqFuj4z8TWyU1gssSepHew== X-IronPort-AV: E=McAfee;i="6600,9927,11064"; a="21356316" X-IronPort-AV: E=Sophos;i="6.07,256,1708416000"; d="scan'208";a="21356316" Received: from orviesa007.jf.intel.com ([10.64.159.147]) by fmvoesa103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 May 2024 11:15:26 -0700 X-CSE-ConnectionGUID: +W/frTD3TEy6zTvpU750lQ== X-CSE-MsgGUID: 9tI2NG1lRIGoqqN/zkXLIA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,256,1708416000"; d="scan'208";a="28457578" Received: from tassilo.jf.intel.com ([10.54.38.190]) by orviesa007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 May 2024 11:15:25 -0700 From: Andi Kleen To: gcc-patches@gcc.gnu.org Cc: nathan@acm.org, josmyers@redhat.com, richard.sandiford@arm.com, jason@redhat.com, Andi Kleen Subject: [PATCH v5 1/5] Improve must tail in RTL backend Date: Sun, 5 May 2024 11:14:54 -0700 Message-ID: <20240505181458.2903045-1-ak@linux.intel.com> X-Mailer: git-send-email 2.44.0 MIME-Version: 1.0 X-Spam-Status: No, score=-10.5 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_EF, GIT_PATCH_0, SPF_HELO_NONE, SPF_NONE, TXREP, URIBL_BLACK autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org - Give error messages for all causes of non sibling call generation - Don't override choices of other non sibling call checks with must tail. This causes ICEs. The must tail attribute now only overrides flag_optimize_sibling_calls locally. - 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 (this could be improved, but it's already useful without that) tree-tailcall usually fails without optimization, so must adjust the existing must-tail plugin test to specify -O2. PR83324 gcc/ChangeLog: * calls.cc (expand_call): Fix mustcall implementation. gcc/testsuite/ChangeLog: * gcc.dg/plugin/must-tail-call-1.c: Adjust. --- gcc/calls.cc | 30 ++++++++++++------- .../gcc.dg/plugin/must-tail-call-1.c | 1 + 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/gcc/calls.cc b/gcc/calls.cc index 21d78f9779fe..a6b8ee44cc29 100644 --- a/gcc/calls.cc +++ b/gcc/calls.cc @@ -2650,7 +2650,9 @@ 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. */ + 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 +3024,22 @@ 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) + { + /* ??? correct message? */ + maybe_complain_about_tail_call (exp, "stack space needed"); + try_tail_call = 0; + } + if (dbg_cnt (tail_call) == false) try_tail_call = 0; /* Workaround buggy C/C++ wrappers around Fortran routines with @@ -3046,15 +3060,11 @@ 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"); break; } } - /* If the user has marked the function as requiring tail-call - optimization, attempt it. */ - if (must_tail_call) - try_tail_call = 1; - /* Rest of purposes for tail call optimizations to fail. */ if (try_tail_call) try_tail_call = can_implement_as_sibling_call_p (exp, diff --git a/gcc/testsuite/gcc.dg/plugin/must-tail-call-1.c b/gcc/testsuite/gcc.dg/plugin/must-tail-call-1.c index 3a6d4cceaba7..44af361e2925 100644 --- a/gcc/testsuite/gcc.dg/plugin/must-tail-call-1.c +++ b/gcc/testsuite/gcc.dg/plugin/must-tail-call-1.c @@ -1,4 +1,5 @@ /* { dg-do compile { target tail_call } } */ +/* { dg-options "-O2" } */ /* { dg-options "-fdelayed-branch" { target sparc*-*-* } } */ extern void abort (void); From patchwork Sun May 5 18:14:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andi Kleen X-Patchwork-Id: 1931592 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=g2nonCCa; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4VXXm91V2Jz1xnS for ; Mon, 6 May 2024 04:16:09 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 549483858435 for ; Sun, 5 May 2024 18:16:07 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.9]) by sourceware.org (Postfix) with ESMTPS id 532CD3858D35 for ; Sun, 5 May 2024 18:15:30 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 532CD3858D35 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=linux.intel.com Authentication-Results: sourceware.org; spf=none smtp.mailfrom=linux.intel.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 532CD3858D35 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=192.198.163.9 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1714932933; cv=none; b=FogapYf1xucPuDvbtiFu1KS8TX9nBPshnkx+65tGdo6fOvn2RmRsx0kKx3O+83XJ16K5/tRivlZuzc66seYOEeOU7fFFfKtj0kgHfvJ0bfW+3fSNMoXqck0kygFmIno7hbjbfcoe9zhbFlDZrn3S5VY+0YzvxPyfTnz4YgS5MpE= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1714932933; c=relaxed/simple; bh=9J9sbtv8A6LHNs0ilXvbSjKAkPTTRw/1lhn8CnwGQ84=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=TGJzzu1jo/tsG/Z++HGz0v3JkdrTiQPg2cyOgMsXYjq74j8Z6WJ37hKSwoyxqOf5R2GMNpJOvQ6gd268mp2Fomt3qSnBYQ4h+eGjpNVzKrzshT2FqMJ87jZ6Lzm9YxIZUeWO8jQk17U8CMNYWQJd0R9FcTVyew9u3+oTuswkddc= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1714932930; x=1746468930; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=9J9sbtv8A6LHNs0ilXvbSjKAkPTTRw/1lhn8CnwGQ84=; b=g2nonCCaqD4AvA2Ceukw3CuvBa4EoMrSxValQr5D3nq4ZosnGUK5k7xv Bbov8uyFchDiJhA8w+bitd7MVMErzj/ByJR4f1WVUOvf0c2a597fcTtSh W54CshSWCNoy82BXo9ovstvimor7auW7UEZMl8Dxiw80rb/fpWS7g2ynF 7zaTYUvgGv5xHUtXIQ9kLDBO6yNnItoCmByejfYdEzgPSwWgTEFTuoPnW Q3Gaf684TBt14ZxoGH4SL3VXcswVHBsbUI+7J1MwzfjXvGSuMojnFuQz2 bPxRR9KFgCsNrIEvzSBoWDPr7b2XgIksoKShCcRfFyLMdj7pENgnRy6SA A==; X-CSE-ConnectionGUID: dc0Mn9AzQo+RYVG/why+Dw== X-CSE-MsgGUID: jf5/jfJrQ4W/mvs99Rmmag== X-IronPort-AV: E=McAfee;i="6600,9927,11064"; a="21356319" X-IronPort-AV: E=Sophos;i="6.07,256,1708416000"; d="scan'208";a="21356319" Received: from orviesa007.jf.intel.com ([10.64.159.147]) by fmvoesa103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 May 2024 11:15:26 -0700 X-CSE-ConnectionGUID: mZlHPADWRLSt8VR3NFO4Og== X-CSE-MsgGUID: MtosvtTPSFGodR+STJEouA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,256,1708416000"; d="scan'208";a="28457579" Received: from tassilo.jf.intel.com ([10.54.38.190]) by orviesa007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 May 2024 11:15:25 -0700 From: Andi Kleen To: gcc-patches@gcc.gnu.org Cc: nathan@acm.org, josmyers@redhat.com, richard.sandiford@arm.com, jason@redhat.com, Andi Kleen Subject: [PATCH v5 2/5] C++: Support clang compatible [[musttail]] (PR83324) Date: Sun, 5 May 2024 11:14:55 -0700 Message-ID: <20240505181458.2903045-2-ak@linux.intel.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240505181458.2903045-1-ak@linux.intel.com> References: <20240505181458.2903045-1-ak@linux.intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-11.3 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_EF, GIT_PATCH_0, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org 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: * cp-tree.h (finish_return_stmt): Add musttail_p. (check_return_expr): Dito. * parser.cc (cp_parser_statement): Handle [[musttail]]. (cp_parser_std_attribute): Dito. (cp_parser_init_statement): Dito. (cp_parser_jump_statement): Dito. * semantics.cc (finish_return_stmt): Dito. * typeck.cc (check_return_expr): Handle musttail_p flag. --- gcc/cp/cp-tree.h | 4 ++-- gcc/cp/parser.cc | 30 ++++++++++++++++++++++++------ gcc/cp/semantics.cc | 6 +++--- gcc/cp/typeck.cc | 20 ++++++++++++++++++-- 4 files changed, 47 insertions(+), 13 deletions(-) diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h index 52d6841559ca..ef5f0039ece2 100644 --- a/gcc/cp/cp-tree.h +++ b/gcc/cp/cp-tree.h @@ -7782,7 +7782,7 @@ extern void finish_while_stmt (tree); extern tree begin_do_stmt (void); extern void finish_do_body (tree); extern void finish_do_stmt (tree, tree, bool, tree, bool); -extern tree finish_return_stmt (tree); +extern tree finish_return_stmt (tree, bool = false); extern tree begin_for_scope (tree *); extern tree begin_for_stmt (tree, tree); extern void finish_init_stmt (tree); @@ -8294,7 +8294,7 @@ extern tree composite_pointer_type (const op_location_t &, tsubst_flags_t); extern tree merge_types (tree, tree); extern tree strip_array_domain (tree); -extern tree check_return_expr (tree, bool *, bool *); +extern tree check_return_expr (tree, bool *, bool *, bool); extern tree spaceship_type (tree, tsubst_flags_t = tf_warning_or_error); extern tree genericize_spaceship (location_t, tree, tree, tree); extern tree cp_build_binary_op (const op_location_t &, diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc index 66ce161252c7..e1bf92628ac3 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 *, bool = false); static void cp_parser_declaration_statement (cp_parser *); @@ -12734,9 +12734,27 @@ cp_parser_statement (cp_parser* parser, tree in_statement_expr, NULL_TREE, false); break; + case RID_RETURN: + { + bool musttail_p = false; + std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc); + 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); + } + statement = cp_parser_jump_statement (parser, musttail_p); + } + 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); @@ -14782,7 +14800,7 @@ cp_parser_init_statement (cp_parser *parser, tree *decl) return false; } -/* Parse a jump-statement. +/* Parse a jump-statement. MUSTTAIL_P indicates a musttail attribute. jump-statement: break ; @@ -14800,7 +14818,7 @@ cp_parser_init_statement (cp_parser *parser, tree *decl) 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, bool musttail_p) { tree statement = error_mark_node; cp_token *token; @@ -14884,7 +14902,7 @@ cp_parser_jump_statement (cp_parser* parser) else if (FNDECL_USED_AUTO (current_function_decl) && in_discarded_stmt) /* Don't deduce from a discarded return statement. */; else - statement = finish_return_stmt (expr); + statement = finish_return_stmt (expr, musttail_p); /* Look for the final `;'. */ cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON); } @@ -30189,7 +30207,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/semantics.cc b/gcc/cp/semantics.cc index b8c2bf8771fc..cac1a949c7d3 100644 --- a/gcc/cp/semantics.cc +++ b/gcc/cp/semantics.cc @@ -1387,16 +1387,16 @@ finish_do_stmt (tree cond, tree do_stmt, bool ivdep, tree unroll, } /* Finish a return-statement. The EXPRESSION returned, if any, is as - indicated. */ + indicated. MUSTTAIL_P indicates a mustcall attribute. */ tree -finish_return_stmt (tree expr) +finish_return_stmt (tree expr, bool musttail_p) { tree r; bool no_warning; bool dangling; - expr = check_return_expr (expr, &no_warning, &dangling); + expr = check_return_expr (expr, &no_warning, &dangling, musttail_p); if (error_operand_p (expr) || (flag_openmp && !check_omp_return ())) diff --git a/gcc/cp/typeck.cc b/gcc/cp/typeck.cc index a25f8622651d..9a5668796d2e 100644 --- a/gcc/cp/typeck.cc +++ b/gcc/cp/typeck.cc @@ -11036,10 +11036,12 @@ maybe_warn_pessimizing_move (tree expr, tree type, bool return_p) the DECL_RESULT for the function. Set *NO_WARNING to true if code reaches end of non-void function warning shouldn't be issued on this RETURN_EXPR. Set *DANGLING to true if code returns the - address of a local variable. */ + address of a local variable. MUSTTAIL_P indicates a musttail + return. */ tree -check_return_expr (tree retval, bool *no_warning, bool *dangling) +check_return_expr (tree retval, bool *no_warning, bool *dangling, + bool musttail_p) { tree result; /* The type actually returned by the function. */ @@ -11053,6 +11055,20 @@ check_return_expr (tree retval, bool *no_warning, bool *dangling) *no_warning = false; *dangling = false; + if (musttail_p) + { + tree t = retval; + if (TREE_CODE (t) == TARGET_EXPR) + t = TARGET_EXPR_INITIAL (retval); + if (TREE_CODE (t) != CALL_EXPR) + { + error_at (loc, "cannot tail-call: return value must be a call"); + return error_mark_node; + } + else + CALL_EXPR_MUST_TAIL_CALL (t) = 1; + } + /* A `volatile' function is one that isn't supposed to return, ever. (This is a G++ extension, used to get better code for functions that call the `volatile' function.) */ From patchwork Sun May 5 18:14:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andi Kleen X-Patchwork-Id: 1931589 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=fAQBJnZj; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4VXXm20yymz1ydV for ; Mon, 6 May 2024 04:16:01 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 545BF385EC44 for ; Sun, 5 May 2024 18:15:59 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.9]) by sourceware.org (Postfix) with ESMTPS id EB2F33858D33 for ; Sun, 5 May 2024 18:15:30 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org EB2F33858D33 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=linux.intel.com Authentication-Results: sourceware.org; spf=none smtp.mailfrom=linux.intel.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org EB2F33858D33 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=192.198.163.9 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1714932933; cv=none; b=FuyROEtG2NNuA8MOG3/q0mVlHEmVs8wDbLIGfJ/iEFe1Aer+L1ZUdM5tYc38LWN3z0Gpynnk0IEVFPEXMUb1hl/wVynmNI1fOgNrW3e8RZqhKkxKgp8j11HcfSwqkvTegUIXnqbynRh36AauP0AOWaU1Q+7AKl3Bn3wa9uJMsPk= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1714932933; c=relaxed/simple; bh=K8LRpn7LAsMF8xNOf+FbJs8s+Tcn2K/OZjtJgnczO9Y=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=fWhbwgZcmhHVZvq9zb3WevJ1PKnPpcvKrAnX0m1Fri2gy6EtuiiF8raU6T/NGVwfuOHc36AXn+PkLwHunNMZ4ga8ZjbasAyvz7BE/GmbnRdXdhhd/T3A67Smb2VBw9Y1NXFxwLZ2Al7wlGDtOEDi7OzFzVGLPrIW74mwa2OQsdA= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1714932931; x=1746468931; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=K8LRpn7LAsMF8xNOf+FbJs8s+Tcn2K/OZjtJgnczO9Y=; b=fAQBJnZjUx2tEOjF1GxW62jLfmgrY0jEj1KZjaYNtdLSkPLdiey4WgSF SUUFeKTr1CC1tEWfL/nJFZ7KfKT3SqSdKIAge9MYLpEaULXd3tQcOOf+Y DO5rIGP4ZpRetibiu5nYi3E9rJnYHOGx3IQJ9uzjXPnErcTCfeDN8uLFw BiwZj7/mCuHHTGLUz1x/o4MBbUi2+3P1h+5z5b5j2Qxmw0nueP6Xb5BsV h75EaXx8uU/UkNcpqdZhVxm17OFjMGBNAi78X24fPtdDXxokqoSGJdp5D k8sMSbjH8xIGw7osJkzjgyTPklkKOtpz0ZedFXtWmtoIZESw+nFGJy1kf w==; X-CSE-ConnectionGUID: GI9ddjb9SvqOvGIhc00rYg== X-CSE-MsgGUID: p0pBewd6QhaRTLn1ID6x2w== X-IronPort-AV: E=McAfee;i="6600,9927,11064"; a="21356323" X-IronPort-AV: E=Sophos;i="6.07,256,1708416000"; d="scan'208";a="21356323" Received: from orviesa007.jf.intel.com ([10.64.159.147]) by fmvoesa103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 May 2024 11:15:26 -0700 X-CSE-ConnectionGUID: qWrv4Z7bS2ioHpmLqe8szw== X-CSE-MsgGUID: FVhBEtXPTR6iy9ZGl8zKnw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,256,1708416000"; d="scan'208";a="28457580" Received: from tassilo.jf.intel.com ([10.54.38.190]) by orviesa007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 May 2024 11:15:25 -0700 From: Andi Kleen To: gcc-patches@gcc.gnu.org Cc: nathan@acm.org, josmyers@redhat.com, richard.sandiford@arm.com, jason@redhat.com, Andi Kleen Subject: [PATCH v5 3/5] C: Implement musttail attribute for returns Date: Sun, 5 May 2024 11:14:56 -0700 Message-ID: <20240505181458.2903045-3-ak@linux.intel.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240505181458.2903045-1-ak@linux.intel.com> References: <20240505181458.2903045-1-ak@linux.intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-11.3 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_EF, GIT_PATCH_0, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org 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 00f8bf4376e5..9edadb0fee96 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 * = NULL); + vec * = 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 *); @@ -5756,6 +5761,8 @@ c_parser_std_attribute (c_parser *parser, bool for_tm) } goto out; } + else if (is_attribute_p ("musttail", name)) + error ("% 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 *chain) + vec *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 22b0009874b5..002351bb9934 100644 --- a/gcc/c/c-tree.h +++ b/gcc/c/c-tree.h @@ -826,7 +826,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 4567b114734b..174348e96d7f 100644 --- a/gcc/c/c-typeck.cc +++ b/gcc/c/c-typeck.cc @@ -11445,10 +11445,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; @@ -11462,6 +11462,17 @@ c_finish_return (location_t loc, tree retval, tree origtype) warning_at (xloc, 0, "function declared % has a % 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; From patchwork Sun May 5 18:14:57 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andi Kleen X-Patchwork-Id: 1931593 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=HwoNW8eh; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4VXXmJ2FRwz1xnS for ; Mon, 6 May 2024 04:16:16 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 818B23846405 for ; Sun, 5 May 2024 18:16:14 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.9]) by sourceware.org (Postfix) with ESMTPS id 867483858429 for ; Sun, 5 May 2024 18:15:33 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 867483858429 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=linux.intel.com Authentication-Results: sourceware.org; spf=none smtp.mailfrom=linux.intel.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 867483858429 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=192.198.163.9 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1714932936; cv=none; b=MxMcyEK+TAbwthDEGs9jXeDG7dXCPF2raxf2wH8R5oCYYryAxvv7ZHKOlj4BsSO52QSzVp7KhoSCwHRBQOC+334I0hInTIk0HOUcYr9gmNIs1L6IAnej6T2Z618cV8rCXxLZRQznpZIGCw+BzgaYqms+f2E+bW5u6ajkjh3SXek= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1714932936; c=relaxed/simple; bh=6QUFda8jNA/1tmpfSZ0dv+GQya5S//uHkmiNa6P2qSE=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=HKy0zeEJpVpDeFD3yf1HP+zANNwg3K5+nrQ/pIuXmjuiKrz0SnOTcTg6z3uhwSjNZS+VGWmo28anAoEUa3EvpTgB2HEDs9yXOzCrLysw/0HnO9SjeSVfrYRQwqPUITFKdDC7Z9HlDVO08UH6pPTlvFg6mLCBdbA5teASaK5eJi8= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1714932934; x=1746468934; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=6QUFda8jNA/1tmpfSZ0dv+GQya5S//uHkmiNa6P2qSE=; b=HwoNW8ehLkN3wxhdp+ZYQCcEqKAovQsXrtdhcxiOqOWJ2uMlYnUHT7hq 6Ir/HONT9MT1HCgEh9ncjqehaBKut5tQIz+LEXu0GSeQ3c2MT/uZR8Se6 fzvAmr/jpXJgQ7ka14Jd7dNMdzfcrvlE/3p6y48NpZ3IbB/Od9gbSsLuZ sjxHtd3nZvYEm9Qv9NxqQk8WMeVb5+OYeRmABtZa6ZoLlvXNT/acrqmGP T4DVy09JA5MNJJ90aghgekksuKDb5ewcRXRUnJBTzqhTrp3C1D+pgAO2J PJ6KbnFZSSGe90Lf01dl+1vxQUP3zC9Ea+n1OgyW8n/aVOhINB41c0qa9 Q==; X-CSE-ConnectionGUID: jfnW3uqaQyq1z+1sSWLeVw== X-CSE-MsgGUID: QWPjOgz6Soe/y0zrAuqOGg== X-IronPort-AV: E=McAfee;i="6600,9927,11064"; a="21356327" X-IronPort-AV: E=Sophos;i="6.07,256,1708416000"; d="scan'208";a="21356327" Received: from orviesa007.jf.intel.com ([10.64.159.147]) by fmvoesa103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 May 2024 11:15:26 -0700 X-CSE-ConnectionGUID: Y2R1ioT8TnWarfhm5zIEiQ== X-CSE-MsgGUID: xqCq3h6vRZ+Uodf3HBqRVw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,256,1708416000"; d="scan'208";a="28457582" Received: from tassilo.jf.intel.com ([10.54.38.190]) by orviesa007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 May 2024 11:15:25 -0700 From: Andi Kleen To: gcc-patches@gcc.gnu.org Cc: nathan@acm.org, josmyers@redhat.com, richard.sandiford@arm.com, jason@redhat.com, Andi Kleen Subject: [PATCH v5 4/5] Add tests for C/C++ musttail attributes Date: Sun, 5 May 2024 11:14:57 -0700 Message-ID: <20240505181458.2903045-4-ak@linux.intel.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240505181458.2903045-1-ak@linux.intel.com> References: <20240505181458.2903045-1-ak@linux.intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-11.4 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_EF, GIT_PATCH_0, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org 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/musttail5.c: New test. --- gcc/testsuite/c-c++-common/musttail1.c | 15 ++++++++++++ gcc/testsuite/c-c++-common/musttail2.c | 34 ++++++++++++++++++++++++++ gcc/testsuite/c-c++-common/musttail3.c | 29 ++++++++++++++++++++++ gcc/testsuite/c-c++-common/musttail4.c | 17 +++++++++++++ gcc/testsuite/c-c++-common/musttail5.c | 28 +++++++++++++++++++++ 5 files changed, 123 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 diff --git a/gcc/testsuite/c-c++-common/musttail1.c b/gcc/testsuite/c-c++-common/musttail1.c new file mode 100644 index 000000000000..ac92f9f74616 --- /dev/null +++ b/gcc/testsuite/c-c++-common/musttail1.c @@ -0,0 +1,15 @@ +/* { dg-do compile { target { tail_call && { c || c++11 } } } } */ +/* { dg-options "-O2" } */ +/* { 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..058329b69cc2 --- /dev/null +++ b/gcc/testsuite/c-c++-common/musttail2.c @@ -0,0 +1,34 @@ +/* { 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: " } */ +} + +typedef void (fn_ptr_t) (void); +volatile fn_ptr_t fn_ptr; + +void +test_5 (void) +{ + [[gnu::musttail]] return fn_ptr (); /* { 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..7938e7ff80e4 --- /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 "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; +} From patchwork Sun May 5 18:14:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andi Kleen X-Patchwork-Id: 1931591 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=POgyFMmM; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4VXXm34zkqz213x for ; Mon, 6 May 2024 04:16:03 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id D13423844042 for ; Sun, 5 May 2024 18:16:01 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.9]) by sourceware.org (Postfix) with ESMTPS id B75CF385843B for ; Sun, 5 May 2024 18:15:33 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B75CF385843B Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=linux.intel.com Authentication-Results: sourceware.org; spf=none smtp.mailfrom=linux.intel.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org B75CF385843B Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=192.198.163.9 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1714932935; cv=none; b=QasUxnzdSA+O5ORVuSMYn/vR/vl/d7kc5dP8zUDDD51W9HgiAwdz5pm5OGtVbqvOxrx8poV56xMpFhKCK47tVmX+UCYtUGa/hyrNFak1glivyMKLj5q07JNT8jrrY2sX7ibTsCEASxZoxWl2VbrADu8nrXx6ePmBHZn+Arjo4cE= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1714932935; c=relaxed/simple; bh=F23XnbzfWiFMcm6O9iCI3HZF3waNYMTl+yGy/xwsKg4=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=pVqwwiYu2emJCiAteTHxgjFtil/s4SXkwTdM7RwEp2ITb/Wy7/duOxjVpfqXxI6qWa/gBqRLAYHZaKPgJunBm874gc+dodY4gMhpwdU6LEyXlA8X5ATChmb3ny6R947nxU6g72d3LmtahcfqLqUcMb76SXVludHTAJvaC32FnQI= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1714932934; x=1746468934; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=F23XnbzfWiFMcm6O9iCI3HZF3waNYMTl+yGy/xwsKg4=; b=POgyFMmMIRtsRptxlSIJXMJdksKkcTfh1nxm/F9Ca3HqrNlKQP8qRCk3 60zasqNP8h8mJH52hU7lV9ENkkMFHLIAIqDqVSVffd8lc+lF/qdG8rKuu I0Pk+MJNvdkTYlb8CpMjkw/cx6cxSdzd4esUdispSV1hssBVWhTrQW0wM oSU38KNFPHvIHVDDQ67y77ChUoRNhTNOm96R7TgXdDOBjX2cK/Uu5wNZg waWjt3iGb5jg3d/T4sFUyfMJ29fvPO81lmi8KstQNw1UKLoFIEKYt/ZoH kmTYh8CnN3pYuee2f5oa3FLsHbQePRHphvnDz8Dedqobnplo98v5Uecoq g==; X-CSE-ConnectionGUID: MHyWEZOwR3GyVjqIETUW9w== X-CSE-MsgGUID: sYxZ1cZFT1WL0yRkQz09sQ== X-IronPort-AV: E=McAfee;i="6600,9927,11064"; a="21356331" X-IronPort-AV: E=Sophos;i="6.07,256,1708416000"; d="scan'208";a="21356331" Received: from orviesa007.jf.intel.com ([10.64.159.147]) by fmvoesa103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 May 2024 11:15:26 -0700 X-CSE-ConnectionGUID: bmSUdwefQF6bns6R54N5+w== X-CSE-MsgGUID: 4ZSrR21eQL2uhrH1G3Xx3g== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,256,1708416000"; d="scan'208";a="28457583" Received: from tassilo.jf.intel.com ([10.54.38.190]) by orviesa007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 May 2024 11:15:26 -0700 From: Andi Kleen To: gcc-patches@gcc.gnu.org Cc: nathan@acm.org, josmyers@redhat.com, richard.sandiford@arm.com, jason@redhat.com, Andi Kleen Subject: [PATCH v5 5/5] Add documentation for musttail attribute Date: Sun, 5 May 2024 11:14:58 -0700 Message-ID: <20240505181458.2903045-5-ak@linux.intel.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240505181458.2903045-1-ak@linux.intel.com> References: <20240505181458.2903045-1-ak@linux.intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-11.4 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_EF, GIT_PATCH_0, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org gcc/ChangeLog: * doc/extend.texi: Document [[musttail]] --- gcc/doc/extend.texi | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi index e290265d68d3..deb100ad93b6 100644 --- a/gcc/doc/extend.texi +++ b/gcc/doc/extend.texi @@ -9839,7 +9839,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 @@ -9896,6 +9896,22 @@ 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. Tail calls generally require enabling optimization. +On some targets they may not be supported. + @end table @node Attribute Syntax @@ -10019,7 +10035,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