From patchwork Tue May 21 14:28:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andi Kleen X-Patchwork-Id: 1937460 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=TGpiZGNc; 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 4VkH4301YCz20KF for ; Wed, 22 May 2024 00:33:38 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 3E3CB3849AE6 for ; Tue, 21 May 2024 14:33:37 +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 [198.175.65.14]) by sourceware.org (Postfix) with ESMTPS id CD03E3858D33 for ; Tue, 21 May 2024 14:32:22 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org CD03E3858D33 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 CD03E3858D33 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=198.175.65.14 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1716301944; cv=none; b=a0vz1b7k/Ar2WgO73c5cstnWoSeiQLSD53FYnLuTAtcaHyDNbtsUPfW64i9mRNJZ0f76UhQU4T+A2L0cCapxeoFB83t42YbDg8Wgpw1Q+OBEubrhBfOZqsVMHh0fCbByR3EW9+tIINo1LTEjyawGZpD+ZhiPxao0cCZrt1IwR1k= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1716301944; c=relaxed/simple; bh=uWipM2UVpo/1WhIKouXpTUD4vAkY3YIth7sIbvyAqQA=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=w5CfdJEe5RdDYPc+Lferez6zyVoUPRVJCV3+QRSQCZtFhcwiAnmpySnoK2IPgmSuVJcoqR3rt00Jki8e5PkzohpN7NEooED5atbzwiiXknDGpWmxhEObIaZxnjtALRrROS9TqBffrPAt8w54+m+WRI7eWdTpKJXxi/RhU8tbx9A= 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=1716301943; x=1747837943; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=uWipM2UVpo/1WhIKouXpTUD4vAkY3YIth7sIbvyAqQA=; b=TGpiZGNc6wi0I/CVJgZROlJHA0cW4WC+3IQlbdbyHT/rP8osvyQGWW2L 6DhdHEEvIGy01O0bJus944FL1+SXt2H17Dd7hYWphVl7vY7TkF9ecKODr PcElQHmaCVW+sN2Mw7bwff4fTcUM0z1dunrKoWtS5nD8EwGb+o2N1fjV8 3FDRbRaCv++7tvTStmUbYM+afd1egaH0Na2rArLfGUFOMEuny7I3qy6jR lCNiovecA9cby8yCvvfEiTGdb7BpcTbo1Z14COqgU+ODIvK3nnK5FyIPf UiuzhX2P/dtltZ1j1Dil05VkjOIj+XUl3R164U8HpDiJBPwDZvJMeFrFO w==; X-CSE-ConnectionGUID: 3ZPWTS8sTkuLjZarAmdcYA== X-CSE-MsgGUID: A0xubsbaQVGW5dnbDx0CKA== X-IronPort-AV: E=McAfee;i="6600,9927,11078"; a="16328249" X-IronPort-AV: E=Sophos;i="6.08,178,1712646000"; d="scan'208";a="16328249" Received: from fmviesa001.fm.intel.com ([10.60.135.141]) by orvoesa106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 May 2024 07:32:19 -0700 X-CSE-ConnectionGUID: f97IIEaGS0eddqHp99bEdQ== X-CSE-MsgGUID: hoIwAVvnTNqpWzfnddSYag== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.08,178,1712646000"; d="scan'208";a="64166840" Received: from tassilo.jf.intel.com ([10.54.38.190]) by smtpauth.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 May 2024 07:32:19 -0700 From: Andi Kleen To: gcc-patches@gcc.gnu.org Cc: richard.guenther@gmail.com, nathan@acm.org, josmyers@redhat.com, richard.sandiford@arm.com, jason@redhat.com, Andi Kleen Subject: [PATCH v6 1/8] Improve must tail in RTL backend Date: Tue, 21 May 2024 07:28:34 -0700 Message-ID: <20240521143203.2893096-2-ak@linux.intel.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240521143203.2893096-1-ak@linux.intel.com> References: <20240521143203.2893096-1-ak@linux.intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-10.1 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 - 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. */ From patchwork Tue May 21 14:28:35 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andi Kleen X-Patchwork-Id: 1937461 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=YAIo2fUe; 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 4VkH4h34sBz20KF for ; Wed, 22 May 2024 00:34:12 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A927B384AB7C for ; Tue, 21 May 2024 14:34:10 +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 [198.175.65.14]) by sourceware.org (Postfix) with ESMTPS id 6C4EB384B06A for ; Tue, 21 May 2024 14:32:23 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 6C4EB384B06A 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 6C4EB384B06A Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=198.175.65.14 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1716301945; cv=none; b=gAhfnDAwKlRWjCWSRqWDSlUvxohO0qlibxVXKv4OhagjIFCpZ7TPUNX0SI84aXBUDOeOR/kexMhgeLckW+oLBOLltvdXbXtck6i6uXI2gAauiOL572SybukbzMIGzlT/xfSEtBnDmjwA2Xlecvw1PSUZQIaEiujIttP8EV45DaU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1716301945; c=relaxed/simple; bh=piVVOF4b/Dp0brV/CtdroV0gJMtjlQ+rp2EqSTiz2mc=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=CBNXuwJzA1cZoPPIROnk5hD3l45dK6MuYOpVFqVtivQEHbAQq/9shbkI5v1KBgp4HMJDW+UnhYlbIOj78PPhjnXq1pSoxC6BmQY+UNBIAG8UOaB+/TFpfH+1tWg5HCKdpdzOQh4ca4oWyI6vF8o7CBpEgc6OG13zXF2gny75SuI= 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=1716301944; x=1747837944; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=piVVOF4b/Dp0brV/CtdroV0gJMtjlQ+rp2EqSTiz2mc=; b=YAIo2fUeKhYojdv1DoXX7AUUYrxa6gP0jy4rdLxb1Pv1QKy6q+y+rN59 v+AIGCKHbb8M+0S6wcfYNogOaJTt1kgBDKd6REWw14/d4KUFaYh3t+vs7 wAX5+yY0RTlZVdF9/kgpA14DTje+HpS8/EEGwjiSEYQ3xtlN/cGtwcf+/ GWyUcr7YHLMNJMPG7SB3vteDXLZ76HOtueXgzVXmR9UVRorGFwJU5B256 TlvsbboNoslXxDF0I1kTDdFkDK4PHW22WNiUVVEWDk3pAYfR8pIf+Vfgi TQBGpf+vqu8kOrHR9NlFpjBgBcFRursgHgQ/bHKQN3PcV77a9Y+BH5839 A==; X-CSE-ConnectionGUID: YyiNF2ZVQ92M3AcndcVxvw== X-CSE-MsgGUID: H9EM+j7ER1umW0nTAK7MyA== X-IronPort-AV: E=McAfee;i="6600,9927,11078"; a="16328256" X-IronPort-AV: E=Sophos;i="6.08,178,1712646000"; d="scan'208";a="16328256" Received: from fmviesa001.fm.intel.com ([10.60.135.141]) by orvoesa106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 May 2024 07:32:19 -0700 X-CSE-ConnectionGUID: fDTkK0CvRgOfEYBycUt4qw== X-CSE-MsgGUID: wWBtvbWASFe8em2uUhUsbw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.08,178,1712646000"; d="scan'208";a="64166843" Received: from tassilo.jf.intel.com ([10.54.38.190]) by smtpauth.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 May 2024 07:32:19 -0700 From: Andi Kleen To: gcc-patches@gcc.gnu.org Cc: richard.guenther@gmail.com, nathan@acm.org, josmyers@redhat.com, richard.sandiford@arm.com, jason@redhat.com, Andi Kleen Subject: [PATCH v6 2/8] Add a musttail generic attribute to the c-attribs table Date: Tue, 21 May 2024 07:28:35 -0700 Message-ID: <20240521143203.2893096-3-ak@linux.intel.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240521143203.2893096-1-ak@linux.intel.com> References: <20240521143203.2893096-1-ak@linux.intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-10.9 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 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 04e39b41bdf3..7110d56c8ca0 100644 --- a/gcc/c-family/c-attribs.cc +++ b/gcc/c-family/c-attribs.cc @@ -338,6 +338,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 @@ -1216,6 +1218,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 2d5f53998855..2707405e8def 100644 --- a/gcc/c-family/c-common.h +++ b/gcc/c-family/c-common.h @@ -1637,6 +1637,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); From patchwork Tue May 21 14:28:36 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andi Kleen X-Patchwork-Id: 1937458 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=Oo4QXWsU; 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 4VkH3f3Sp6z20KF for ; Wed, 22 May 2024 00:33:18 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C77E9384AB79 for ; Tue, 21 May 2024 14:33:16 +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 [198.175.65.14]) by sourceware.org (Postfix) with ESMTPS id F1E38384AB43 for ; Tue, 21 May 2024 14:32:24 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org F1E38384AB43 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 F1E38384AB43 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=198.175.65.14 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1716301949; cv=none; b=URzWi7smszZXLQ6gLP3BXQE+j9wU/l8PwfA5d1Mo5WToKNdsP4zWACwapK4tsW/G2bhClowCl9GL+fIqsAHuwmzRlokT/DGibks7AzUuw34ftiK0ytiTxueLgAqdDejM+HguIqALtrCuXNpPZ37GcK1h0UuTnWjod/PHutK+6G0= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1716301949; c=relaxed/simple; bh=MstMhyMhwtif5HoBZsEzHyW5sv2HlsH98uqgFNCqpOk=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=RJOgSV1W2/lK85MJtgdESu8IFJvcuLnkWPpCa1dAzGLdwOKW02cwQj1ryzJ/fkCfqCUHlZKPJIZZeV9PJZzDEbbU6c2HcNnBltNqLmoZX+JkKJVG5+4R8VkXzrN25VWA6eU14Exd2+02QOqcenpmonZn8NiYLXiekMxQ4+h24KE= 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=1716301945; x=1747837945; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=MstMhyMhwtif5HoBZsEzHyW5sv2HlsH98uqgFNCqpOk=; b=Oo4QXWsUN2Ltz9LlNxXQGMlf8UML3ygx5yWHAzOj4z6d0hxTSdsHwSxX QWZkEDrFJFoe94M7g55QZTFQpo5H/VsbJ7goLFNHa6queLr3xbllXM4KN Bg7b8M07SApEOWPSB9HA/VcyzNE5wa+WE1Y/+zzxDMnBGJYShz9BKt1ds /MUVk2BdhXburRNqz7KcdkK3tHQgJDBVzsCkkzHZHfzyB4NSU9sU42WXi /qbUh2nNjGcLVduh2ZNIXZIgfOQtWWsq0iAwu3rr2U2LdD8194VS9Gvuq AmkG3G+ByRsuqcILmEbdQama8hOBlCbKU7VECxAjBBiL6pNS7iSmOm/tC w==; X-CSE-ConnectionGUID: EZVTrXgPSeOHcSg9KvypBg== X-CSE-MsgGUID: w82hR2IjTWCQpA0CWTy4+w== X-IronPort-AV: E=McAfee;i="6600,9927,11078"; a="16328262" X-IronPort-AV: E=Sophos;i="6.08,178,1712646000"; d="scan'208";a="16328262" Received: from fmviesa001.fm.intel.com ([10.60.135.141]) by orvoesa106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 May 2024 07:32:19 -0700 X-CSE-ConnectionGUID: Imfu6S58QjqUhp1pqsQBow== X-CSE-MsgGUID: 9/28xlQWS/aOLf5Zr57VVg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.08,178,1712646000"; d="scan'208";a="64166848" Received: from tassilo.jf.intel.com ([10.54.38.190]) by smtpauth.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 May 2024 07:32:19 -0700 From: Andi Kleen To: gcc-patches@gcc.gnu.org Cc: richard.guenther@gmail.com, nathan@acm.org, josmyers@redhat.com, richard.sandiford@arm.com, jason@redhat.com, Andi Kleen Subject: [PATCH v6 3/8] C++: Support clang compatible [[musttail]] (PR83324) Date: Tue, 21 May 2024 07:28:36 -0700 Message-ID: <20240521143203.2893096-4-ak@linux.intel.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240521143203.2893096-1-ak@linux.intel.com> References: <20240521143203.2893096-1-ak@linux.intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-11.0 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: * parser.cc (cp_parser_statement): Handle musttail. (cp_parser_jump_statement): Dito. (cp_parser_std_attribute): Dito. --- gcc/cp/parser.cc | 42 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 37 insertions(+), 5 deletions(-) diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc index 66ce161252c7..d25c1a1ac6a5 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 *); @@ -12734,13 +12734,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. */ @@ -14797,10 +14801,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; @@ -14877,6 +14882,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) @@ -30189,7 +30221,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); From patchwork Tue May 21 14:28:37 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andi Kleen X-Patchwork-Id: 1937459 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=XP44iLTl; 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 4VkH3g1r2lz20dK for ; Wed, 22 May 2024 00:33:19 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 6A922384AB58 for ; Tue, 21 May 2024 14:33:17 +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 [198.175.65.14]) by sourceware.org (Postfix) with ESMTPS id 73D71384AB64 for ; Tue, 21 May 2024 14:32:25 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 73D71384AB64 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 73D71384AB64 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=198.175.65.14 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1716301949; cv=none; b=MpCGpC94+GX5c5badmy75J0omR87U4cVHVgZ9NvOQa1XUuI7G7JTcggLXyuYy9bazuQb8Yo1/12Dco8tYtUJaeX1n69rvTmMwRr1gUsQPD3HE4yS9qhfrDsL+AgnMvjS8K+/o/Vc9+nfmJe0a5QHN0vzdg2/qmynm0AEMPgmjF0= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1716301949; c=relaxed/simple; bh=K8LRpn7LAsMF8xNOf+FbJs8s+Tcn2K/OZjtJgnczO9Y=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=cCRVgVDJ9VRBeF6pYTZpshaAIQww5EQZ2CyzjDnRtrgx1/eC4GAMHmdBRYGeVdZ9s8E+SlIQn3MJ+FLC0FkpWIqjy16T54kWg6F+M8j3pX6PQll+pn5onWAKkgDb+z2nJ82Fzv6mDJ8xmM655v69d1hW1/I7VVBlTrGm5svrArg= 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=1716301946; x=1747837946; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=K8LRpn7LAsMF8xNOf+FbJs8s+Tcn2K/OZjtJgnczO9Y=; b=XP44iLTl2Q4ife72uT4S+3UDw9Y0mA7WeB0nrcgxWdqqe+UpdqHAn08W Wffc877IJaeDjk/6PliTLJdBOVNDqP1YL5zCajZHBwmJNWVZQMm0eAgOI KX8zdZ/1XrnxA4C04uZZ5HtCrob2SaTyVb8Hqnl4y+BQnHpCCmhvhJA/S 4pBWHvGWu7F6o2PhSATTGtiEtauZVMufSx0bsVIfT+s8Sp8yP+bDox4kT 7kIB8geMk6d1yYjqH3A+h5hT7mTExYow2GlWqnhdURMzvB2XBuGVdgAbV zopiTExqYqPw9j97XO4zoh9pVeqHKtxAJXnIfSkslWYeRpGSNbtvcnBV2 Q==; X-CSE-ConnectionGUID: jUoz8oOKStqKJsI0GQRzlA== X-CSE-MsgGUID: okMD8TmYRByAHcfcuKBzKA== X-IronPort-AV: E=McAfee;i="6600,9927,11078"; a="16328268" X-IronPort-AV: E=Sophos;i="6.08,178,1712646000"; d="scan'208";a="16328268" Received: from fmviesa001.fm.intel.com ([10.60.135.141]) by orvoesa106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 May 2024 07:32:19 -0700 X-CSE-ConnectionGUID: R714qOMoTHWE8xq+g3rNUQ== X-CSE-MsgGUID: ZbPEdi8QTTCA6yt8FSWb4w== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.08,178,1712646000"; d="scan'208";a="64166850" Received: from tassilo.jf.intel.com ([10.54.38.190]) by smtpauth.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 May 2024 07:32:19 -0700 From: Andi Kleen To: gcc-patches@gcc.gnu.org Cc: richard.guenther@gmail.com, nathan@acm.org, josmyers@redhat.com, richard.sandiford@arm.com, jason@redhat.com, Andi Kleen Subject: [PATCH v6 4/8] C: Implement musttail attribute for returns Date: Tue, 21 May 2024 07:28:37 -0700 Message-ID: <20240521143203.2893096-5-ak@linux.intel.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240521143203.2893096-1-ak@linux.intel.com> References: <20240521143203.2893096-1-ak@linux.intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-11.0 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 Tue May 21 14:28:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andi Kleen X-Patchwork-Id: 1937462 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=hfQ5fl70; 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 4VkH550ysJz20KF for ; Wed, 22 May 2024 00:34:33 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 5C513384AB4F for ; Tue, 21 May 2024 14:34:31 +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 [198.175.65.14]) by sourceware.org (Postfix) with ESMTPS id 947573858D3C for ; Tue, 21 May 2024 14:32:25 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 947573858D3C 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 947573858D3C Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=198.175.65.14 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1716301949; cv=none; b=fNaOBCd5wRRXubz1oefr8SM4dsbmKhyp+tJXppf9XjtdkojHtqj8jEXEWuWR8rb4amVzLq9kxHDV3yR872mtz0ihWkk2vOyH8qowqn590s1Ek3bEHqktjPlSYDxQiA44jQMPLTrwENxC97LDxvZlgX8+63B34tgjAf1TvT9T/g8= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1716301949; c=relaxed/simple; bh=jX/fsPnf59mnwMlwCQE0FWV8A4x7Nf6qNEIgjCUZI7A=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=wMRwq1/RRapQlRpuiaqX3Awtau8IJOFXw4G7sSvi6jdomVg/Q2xiCWxS8gP8AmcNVDwklP/xD+8BGzoJUYn/3iDxTefIxHFL/5g0rqhcKzoA9fiag3T484xnKfgFDCHhrRblfy7ZiIbqJUWK0rfQoIcTZXJ8LYOIGD1O8dNCwWE= 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=1716301946; x=1747837946; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=jX/fsPnf59mnwMlwCQE0FWV8A4x7Nf6qNEIgjCUZI7A=; b=hfQ5fl703/dpRnMSSmiEo0A/rhfILkHDAcUbP1n3EKbz5FAVRhHAR03G ZNT1Nqb9cox6O92FjMJ+ODMssPsJfo4xlakxOz7xCoydRbzYqwrLuPrcf r7kzebs8ZRKuPJqZde/RPRzyjICC/dDNjO8B9bOAGBFVyQs+1jQbnQtQe 4TUa1jXzcf165+Z1x8M4HHUgtYtAw5xiE87i+DSKZV7+Ijmm4XZk23fkL vt99+Zlk/jeQwtFLHnNQTSAWTd424FaOONl6XYirRM7mSkpvMKbgBBe9U oF9z4CdluNmy9TlhC1eKqvcfssMPziAMIQ6/Gu0x+qoxSqgpHSQaVuAYr Q==; X-CSE-ConnectionGUID: ILd9ti/GStqTHeTekfnu3g== X-CSE-MsgGUID: rIriNjeaSxW8fUFn0PMC6Q== X-IronPort-AV: E=McAfee;i="6600,9927,11078"; a="16328276" X-IronPort-AV: E=Sophos;i="6.08,178,1712646000"; d="scan'208";a="16328276" Received: from fmviesa001.fm.intel.com ([10.60.135.141]) by orvoesa106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 May 2024 07:32:19 -0700 X-CSE-ConnectionGUID: YDKpVTFGRWKSJjvUDvLLiQ== X-CSE-MsgGUID: N1SH2H6pSsWI6YtzGlSmjg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.08,178,1712646000"; d="scan'208";a="64166851" Received: from tassilo.jf.intel.com ([10.54.38.190]) by smtpauth.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 May 2024 07:32:19 -0700 From: Andi Kleen To: gcc-patches@gcc.gnu.org Cc: richard.guenther@gmail.com, nathan@acm.org, josmyers@redhat.com, richard.sandiford@arm.com, jason@redhat.com, Andi Kleen Subject: [PATCH v6 5/8] Add tests for C/C++ musttail attributes Date: Tue, 21 May 2024 07:28:38 -0700 Message-ID: <20240521143203.2893096-6-ak@linux.intel.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240521143203.2893096-1-ak@linux.intel.com> References: <20240521143203.2893096-1-ak@linux.intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-1.0 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, UNWANTED_LANGUAGE_BODY 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/musttail7.c: New test. * c-c++-common/musttail8.c: New test. * g++.dg/musttail6.C: New test. * g++.dg/musttail9.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/musttail6.C | 58 ++++++++++++++++++++++++++ gcc/testsuite/g++.dg/musttail9.C | 10 +++++ 9 files changed, 220 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/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..676d0e8f381c --- /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 "\(must be the same type|other reasons|target is not\)" } */ +} + + +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/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 +T __attribute__((noinline,noclone,noipa)) foo (T i) +{ + return i + 1; +} + +int +caller2 (int k) +{ + [[gnu::musttail]] return foo(1); +} + +template +T caller3 (T v) +{ + [[gnu::musttail]] return foo(v); +} + +int call3(int i) +{ + [[gnu::musttail]] return caller3(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(99)); +} +#endif diff --git a/gcc/testsuite/g++.dg/musttail9.C b/gcc/testsuite/g++.dg/musttail9.C new file mode 100644 index 000000000000..78e0c50db6e0 --- /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 "cannot tail-call" } */ +} From patchwork Tue May 21 14:28:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andi Kleen X-Patchwork-Id: 1937465 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=ez0uCZ8/; 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 4VkH6R4Qp7z1ynR for ; Wed, 22 May 2024 00:35:43 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id DB1703858D1E for ; Tue, 21 May 2024 14:35:41 +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 [198.175.65.14]) by sourceware.org (Postfix) with ESMTPS id B6E1E384AB55 for ; Tue, 21 May 2024 14:32:29 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B6E1E384AB55 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 B6E1E384AB55 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=198.175.65.14 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1716301952; cv=none; b=YI4xiP7cmOXJs+VRWrI8qhIvTfAX9t3nqNmRa610p8Wgj6CozXMcaAuRB3EcPYGqvwQhjglloLMoAOWNOeNtAgsYdvZw2PuQ0Ic7mU2l6cTC5s33UZ+Pl5uZ9Y1QSAiT1sCgRKjAL+WZYR2s26TyhtQpxY4AeQCQFFmP+m8WXTw= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1716301952; c=relaxed/simple; bh=l/qU6rQnzEnIledCINoxQXm7qusUO3h9o6VSPCAtbL8=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=LVQAmvgXb+bA9SGnQ/021Y/IKjyp+Md4vQjKAWBAxs6OyVAlKQMNia76fcoF7IvRXd15rtUf9Au/lSPhGVz2eTFeqj/JMe67yequiDmZUn2FR8AiyQS/f9a644QCx7Lv6NXC+GHKpYC/YKumFLqa+lnj8zgnAhs2rYDyCErINY8= 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=1716301950; x=1747837950; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=l/qU6rQnzEnIledCINoxQXm7qusUO3h9o6VSPCAtbL8=; b=ez0uCZ8/nto33NKXdRiU8mDbtWXDvupeJeQpzOW6c+1chbzchvtaF+DN h62ziWs1GpW59QJu8r9famyXhBln+5XNLYcHeRylduKXn9Fi2v3dV6LA0 IA1h2KF5T+5WrX6OUJPb4YWDtg6cgAP9sEF2jLmC3U6vM9WXqJyK3Ogbn zlEyGZO+3rfKGjLv28DHqbMDlQh5TypDhKHB1ZMReZ+fjIb15C3MpUU5Q AgfiM1CMJ1xpVpgUrBlDDM0Qs6En+VW0VCRn4/gtvPAJV5NTzb7adVb0l XM5VGb5/FICkWtFNYXvqw73WvIA14IigE8wA496omeMkC1o5oP8MfZ9et w==; X-CSE-ConnectionGUID: VVPk69fYRziNg1pqa0dkQA== X-CSE-MsgGUID: vuOLh1IhQQ6Z4GQ4tPwyhg== X-IronPort-AV: E=McAfee;i="6600,9927,11078"; a="16328283" X-IronPort-AV: E=Sophos;i="6.08,178,1712646000"; d="scan'208";a="16328283" Received: from fmviesa001.fm.intel.com ([10.60.135.141]) by orvoesa106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 May 2024 07:32:19 -0700 X-CSE-ConnectionGUID: hbFNE5tvS3WSpN0nDbN6gA== X-CSE-MsgGUID: n9KUXhKdT3KiW4Asc6ePGg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.08,178,1712646000"; d="scan'208";a="64166854" Received: from tassilo.jf.intel.com ([10.54.38.190]) by smtpauth.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 May 2024 07:32:20 -0700 From: Andi Kleen To: gcc-patches@gcc.gnu.org Cc: richard.guenther@gmail.com, nathan@acm.org, josmyers@redhat.com, richard.sandiford@arm.com, jason@redhat.com, Andi Kleen Subject: [PATCH v6 6/8] Enable musttail tail conversion even when not optimizing Date: Tue, 21 May 2024 07:28:39 -0700 Message-ID: <20240521143203.2893096-7-ak@linux.intel.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240521143203.2893096-1-ak@linux.intel.com> References: <20240521143203.2893096-1-ak@linux.intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-11.1 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 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 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 (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 (*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); +} From patchwork Tue May 21 14:28:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andi Kleen X-Patchwork-Id: 1937463 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=mmp4aR2z; 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 4VkH5G2fjmz20KF for ; Wed, 22 May 2024 00:34:42 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 9E55D384AB72 for ; Tue, 21 May 2024 14:34:40 +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 [198.175.65.14]) by sourceware.org (Postfix) with ESMTPS id B7F98384AB56 for ; Tue, 21 May 2024 14:32:29 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B7F98384AB56 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 B7F98384AB56 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=198.175.65.14 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1716301951; cv=none; b=wfKqIU0UyjLpk0kDT8v1zmZXc+xZanqz09ydR0a5LY8gX0ZyRNlvlr2ejIVal8J6SRhUDM1bmYHhumziKHkSGRpjU0nDSFN0y+XOpCzBU66akp737KtewPYSYjFPDx9AOBBmZyE1uQnEHtnSQFqpA8tIj2Fjkas4/RwApUrk/7s= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1716301951; c=relaxed/simple; bh=d0imsXXugI5GAFGe4n3p5QVWKt/zYENqrqaNTXF8eRs=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=jZhfIYQBCw37fQX5g0TPW4L7PZ9UbJqVNcs2LEg0q5bAQNwUCGcHKluPikdeUyEXaQXu8HluQF0YoVSfAoNdZV+kj8zdO9Epp91Z9nU1zjg7RuFROaC+dG+JiHOhGcfMhDgUJWjy36+49IhY9swXCG1aBgpHJ5oGzcTba43Lcew= 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=1716301950; x=1747837950; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=d0imsXXugI5GAFGe4n3p5QVWKt/zYENqrqaNTXF8eRs=; b=mmp4aR2zq2IeQZHHWfpmDX9POjE4RaSufgvB84iOa+RdBWAxPoy0ytOf qaZnmzEaGd9B8z9Rv17BiP1w+6HJXpumKk1yP7Yk+xTqSJhVRzlqlMMlg k0Vt2wAUWfYshNwJUhZfbLM6mvtHAy8QHYLIOOEm+jX1OKFjsDN+q5yHF wMKNZIEGDPftagVHjnWcaiKMMOBvJTgzzkI5oBrnRAWwNr7bwvH0vwHat iWbiG9jnxJ8PjmcIlWiQ+oUjJvt14RLtmOAkDTDA9BDkhnHyigsnQkYbL K0YUC7nbVRJcl54h0FNypHAROxbXYZIgMvJuH9ZrVGKAJIKTAEP22azJR Q==; X-CSE-ConnectionGUID: Y2U1YiBFTtaEef/ivIsL/A== X-CSE-MsgGUID: KhoqjYprR/eT/fmlaucZNA== X-IronPort-AV: E=McAfee;i="6600,9927,11078"; a="16328287" X-IronPort-AV: E=Sophos;i="6.08,178,1712646000"; d="scan'208";a="16328287" Received: from fmviesa001.fm.intel.com ([10.60.135.141]) by orvoesa106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 May 2024 07:32:20 -0700 X-CSE-ConnectionGUID: 1zkNrf1ISLKOzPy2L31VBA== X-CSE-MsgGUID: 8EyynaqYRlqkh4kiiEeCGA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.08,178,1712646000"; d="scan'208";a="64166857" Received: from tassilo.jf.intel.com ([10.54.38.190]) by smtpauth.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 May 2024 07:32:20 -0700 From: Andi Kleen To: gcc-patches@gcc.gnu.org Cc: richard.guenther@gmail.com, nathan@acm.org, josmyers@redhat.com, richard.sandiford@arm.com, jason@redhat.com, Andi Kleen Subject: [PATCH v6 7/8] Give better error messages for musttail Date: Tue, 21 May 2024 07:28:40 -0700 Message-ID: <20240521143203.2893096-8-ak@linux.intel.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240521143203.2893096-1-ak@linux.intel.com> References: <20240521143203.2893096-1-ak@linux.intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-11.1 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 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. (find_tail_calls): Add error messages. --- gcc/tree-tailcall.cc | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/gcc/tree-tailcall.cc b/gcc/tree-tailcall.cc index 094856de22ef..1217435c3f90 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,20 @@ 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); + } +} + /* 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; @@ -489,8 +504,14 @@ 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; + } + /* This doesn't really work because the BB would have multiple succ edges + and is rejected above. Instead just gives a "other reasons" error + at expand time. */ /* 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) @@ -524,7 +545,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 +629,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 +638,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; } } @@ -631,6 +657,8 @@ find_tail_calls (basic_block bb, struct tailcall **ret, bool only_musttail) auto_bitmap to_move_defs; auto_vec to_move_stmts; + /* Does not give musttail errors because the frontend won't generate + such musttails. */ abb = bb; agsi = gsi; while (1) @@ -716,7 +744,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. */ From patchwork Tue May 21 14:28:41 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andi Kleen X-Patchwork-Id: 1937464 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=J++ieftZ; 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 4VkH6D5PsMz1ynR for ; Wed, 22 May 2024 00:35:32 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 067AE384CBA1 for ; Tue, 21 May 2024 14:35:31 +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 [198.175.65.14]) by sourceware.org (Postfix) with ESMTPS id 1B5E53858000 for ; Tue, 21 May 2024 14:32:30 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 1B5E53858000 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 1B5E53858000 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=198.175.65.14 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1716301952; cv=none; b=cv4Cqi7DcBVL++y6WmYxeKUpe6pnjIfVsjNVlLs1e3u6Djgkf5WUwpFxVp4Re9btVyuvg5qiLZPIpd/lEpyYMdp/QMsVYYpLwxLBC1EEJVaAwQZwYuvjm7AUIeyuj8+quROxd8bHnV3xzPyCtYOFhSYD715F9sDT4s+PUJ8/wwc= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1716301952; c=relaxed/simple; bh=dGPiVj4B88toym1iD1ANZ0v6CS1LpSpzgVyNfG49LrU=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=qKgAoaA5h3JpI8M3nNYVzplqpKskW8vT/UUWtqY3W1dr7GDHfS1eYgSjyj5QDwbiyYLaUBB5ao+VCAIWIAJ2932BwdHBGEaSKSyNalXT/7PfehehtwSoPNCOXi2kcDqyT0w6rGOtJjWBCR/ZmmmfjhhVTC0R2aPjliU9SBFxUCM= 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=1716301950; x=1747837950; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=dGPiVj4B88toym1iD1ANZ0v6CS1LpSpzgVyNfG49LrU=; b=J++ieftZQBT8s+Ba7ElCMwBQ/fQMlGIW+RYC4HySIu+qehEJbckVFgOW j7XLzTvH0cvWStOj1tQsg56iJuHwjDUE6EWkdP+3tSYuPY11Dtx6lAqDt PcO4MC+MQ/2fDVW0q1l5WfYx87kNFzTUhmG+tAcIJ7VNunzd4IB3uDQXn eGgm4Ww0v+HYAAFriyVk7t/HXOXogjGTHFgxPP/ugbdo4aXrSFpNeqWCt XRPD1XvdQUuj1DeMzmVYdgy4ux8R+XCvrknBROtvq26z2kn1aHB2r5Mpq 3074B2/P8t9vKgO9qWxsyteZX73InUOFLF5T+fT9JT4cuGHdOo7WqslIo A==; X-CSE-ConnectionGUID: qT07PlB7SjSUEFq87Jnlig== X-CSE-MsgGUID: Z6XB/I0FQ0CU/itU7Tz7iw== X-IronPort-AV: E=McAfee;i="6600,9927,11078"; a="16328297" X-IronPort-AV: E=Sophos;i="6.08,178,1712646000"; d="scan'208";a="16328297" Received: from fmviesa001.fm.intel.com ([10.60.135.141]) by orvoesa106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 May 2024 07:32:20 -0700 X-CSE-ConnectionGUID: odmnYI+NRaCD7oRvlqibew== X-CSE-MsgGUID: 6D8Gi40yQdaNVvnnxqBzqQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.08,178,1712646000"; d="scan'208";a="64166858" Received: from tassilo.jf.intel.com ([10.54.38.190]) by smtpauth.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 May 2024 07:32:20 -0700 From: Andi Kleen To: gcc-patches@gcc.gnu.org Cc: richard.guenther@gmail.com, nathan@acm.org, josmyers@redhat.com, richard.sandiford@arm.com, jason@redhat.com, Andi Kleen Subject: [PATCH v6 8/8] Add documentation for musttail attribute Date: Tue, 21 May 2024 07:28:41 -0700 Message-ID: <20240521143203.2893096-9-ak@linux.intel.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240521143203.2893096-1-ak@linux.intel.com> References: <20240521143203.2893096-1-ak@linux.intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-11.1 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 | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi index e290265d68d3..f3df7688edf1 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,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 they may not be supported. +Tail calls cannot reference locals in memory, which may affect +builds without optimization when passing structures that +would fit into registers. @end table @node Attribute Syntax @@ -10019,7 +10036,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