From patchwork Wed Jan 31 02:17:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andi Kleen X-Patchwork-Id: 194442 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:2087:b0:106:209c:c626 with SMTP id gs7csp1626423dyb; Tue, 30 Jan 2024 18:20:50 -0800 (PST) X-Google-Smtp-Source: AGHT+IHiv9l7boa12DxAI1EhHVsuAE4dZz26IAQZUZcoxYejbNSvK4MWCBK66VVTD39qbaQrlGeD X-Received: by 2002:ac8:7c50:0:b0:42a:9b63:7828 with SMTP id o16-20020ac87c50000000b0042a9b637828mr499956qtv.68.1706667650203; Tue, 30 Jan 2024 18:20:50 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706667650; cv=pass; d=google.com; s=arc-20160816; b=r+Qe2hsGWHLiddfIImanWq0xFBdu3WjaTE1QyVXo0AhPHcyJHnk8D66CBnShgfLDS/ jN/MOM8aFJ+7ITzKaPfg6pMMAW06teV/1qc4FESlkSqlSNNYQBOkeGilkAg8Qb57Ma4l nQu6cZhYpyyJz07NAUC4FwLglEuhIeRVrlgYFpJEPmeuKZhrZIzN1RPV+EWgM9V4W2CJ Yw+KcqDr42xTGK5yL12RktfySW15c8DJv1Ptv8lU/Ze919O9u5CGQgVhF37xD12RH0AO 1Jj12JQhiNFUrlUrHKvukufQCq9F9oQPpyN+tnhxtcx4yxtELkFipPebzC/TWmHjw/+n +dUg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature:arc-filter:dmarc-filter:delivered-to; bh=xZrz8wpQA42brzwGYeXWpok+VmPY06EmxA5C2kkQd7Y=; fh=YyzCi7CJHfvE6iOMwG+4sn2lVIIVjlqb0ClzooejE54=; b=cP9TNazWvYRX4kSrnmYqL0GIvqtrMPXi9RXcr9R8tVC8mhs3vQZJ8vakItWVp6kmi7 6G48c1Aa4afqm+bttRLg+mkwmfNpRhzCJNm5GvOYtCLEzk4wGncCQKe4TC9EpZ7lycAs jUMWRtgshkbwKgwv64lfgyVUvlBz+P2w7huXiGK+bBAkPpj0viXb9BVWOxO2rbdtdhH7 UCoJsyNZnkQ0MLzC3m3XkExYqwxHFoeeRDnmkgRbHML7pZKQR2v50Gw7eebm6qxGyEX0 0VrO1wDutc8vTtv/OaKc2J8OoHKowOzK23U4XTc5pRqrse9XDBQrWN2qtsulHa0ceaCS n60A==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=jl1MmxRA; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com X-Forwarded-Encrypted: i=1; AJvYcCXwmhfyeMxmisnI+K8r1TWkYrmSF37PNSb4limXUdvh8Rl8r1KIlsWpEIFyAZOpyyPZ1XQLTNTyf01VS7Uym4FfQPD8HA== Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id d10-20020a05622a15ca00b0042a0b35c01esi7566678qty.231.2024.01.30.18.20.50 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 Jan 2024 18:20:50 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=jl1MmxRA; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id DF9AC38582B5 for ; Wed, 31 Jan 2024 02:20:49 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from eggs.gnu.org (eggs.gnu.org [IPv6:2001:470:142:3::10]) by sourceware.org (Postfix) with ESMTPS id 06D64385828A for ; Wed, 31 Jan 2024 02:18:38 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 06D64385828A 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 06D64385828A Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2001:470:142:3::10 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1706667522; cv=none; b=kuEHhRmnYfIOyCT6xuAAdh9LSdLhoLyOEgIgeHPbDQ5ZEQvIHyNbcWcLH//e25phvIfwBEgLOqMGXqk5TzvOAEj4docVOxNt89rQkRzHDRbBT4N4Bm10NjBj01nuBlsLC6lfPqx63m6tA7s4XOQVljd34IPVI0UPl7pDTqozY4I= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1706667522; c=relaxed/simple; bh=j3kbVqsgAznZ98FCM1bQe6L31BmviIjsVBeWQjuQdGk=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=qEdkh6zF4RGp13SkWgmLfDl0Msz8q/yVTkPmAlXvMhvWg+kP227h2q2rUkyeCTTf/Wp0j7VK0s8MzVX3tRZY8Vc8bj39tYKOyGL0Zru+Rn0oRxKu44do7dY2iug02GOLcF12XQ3OHxGkQkymzNHyZrZwvkKlADAIufuP4kN5K6k= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from mgamail.intel.com ([198.175.65.13]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rV0BQ-0002Tg-R0 for gcc-patches@gnu.org; Tue, 30 Jan 2024 21:18:38 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1706667517; x=1738203517; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=j3kbVqsgAznZ98FCM1bQe6L31BmviIjsVBeWQjuQdGk=; b=jl1MmxRAvfdnRBLtoU0JAV2WtYktv7giDplrZkVle1gruerWmLu3QUff Xc24CTl5SjLCqs55WI6o/3PPlPx4dQcySFohWfOxJBLYw4mwTZei9vSZY /A1FVzdZkPqhF7xiXJJwFgbD+kj/iTXGKLZZOM6gWlv3NpyMWpV5LvG/L vrp9eoi6WlStjsvaz8hr3cNNDZaLF+LsIQMcjfJh3rivrk5BJoW/4ynPh uo6AVsM5XJE+yqPl1WkButmdmOvRNTy7IfniYcHPiFkwBwBdubrkoeJgW gIIQPl1IEsOLU4qFfQoQW6OK9gY8l8Kuwxbxgl305hoR62Y20NT5W0wk+ A==; X-IronPort-AV: E=McAfee;i="6600,9927,10969"; a="10561933" X-IronPort-AV: E=Sophos;i="6.05,231,1701158400"; d="scan'208";a="10561933" Received: from orsmga003.jf.intel.com ([10.7.209.27]) by orvoesa105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Jan 2024 18:18:16 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10969"; a="737946819" X-IronPort-AV: E=Sophos;i="6.05,231,1701158400"; d="scan'208";a="737946819" Received: from tassilo.jf.intel.com ([10.54.38.190]) by orsmga003-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Jan 2024 18:18:15 -0800 From: Andi Kleen To: gcc-patches@gnu.org Cc: Andi Kleen Subject: [PATCH v3 3/5] C: Implement musttail attribute for returns Date: Tue, 30 Jan 2024 18:17:16 -0800 Message-ID: <20240131021808.151575-4-ak@linux.intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240131021808.151575-1-ak@linux.intel.com> References: <20240131021808.151575-1-ak@linux.intel.com> MIME-Version: 1.0 Received-SPF: none client-ip=198.175.65.13; envelope-from=ak@linux.intel.com; helo=mgamail.intel.com X-Spam_score_int: -32 X-Spam_score: -3.3 X-Spam_bar: --- X-Spam_report: (-3.3 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-1.292, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_NONE=0.001, SPF_NONE=0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-Spam-Status: No, score=-12.4 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_EF, GIT_PATCH_0, SPF_HELO_PASS, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE 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+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1789570738029616551 X-GMAIL-MSGID: 1789570738029616551 Implement a C23 clang compatible musttail attribute similar to the earlier C++ implementation in the C parser. --- 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 c31349dae2ff..76931931e270 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 *); @@ -5757,6 +5762,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; @@ -6943,6 +6950,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). */ @@ -6959,6 +6988,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)) { @@ -7097,7 +7127,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) @@ -7245,7 +7278,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 @@ -7287,15 +7320,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) @@ -7317,6 +7353,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). @@ -7560,11 +7597,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 @@ -7573,11 +7610,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; @@ -7645,7 +7682,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 @@ -7654,7 +7691,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 cf29534c0915..902cc8f6aa49 100644 --- a/gcc/c/c-tree.h +++ b/gcc/c/c-tree.h @@ -824,7 +824,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 66c6abc9f076..144b001e3a6f 100644 --- a/gcc/c/c-typeck.cc +++ b/gcc/c/c-typeck.cc @@ -11422,10 +11422,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; @@ -11439,6 +11439,17 @@ c_finish_return (location_t loc, tree retval, tree origtype) warning_at (xloc, 0, "function declared % has a % statement"); + if (retval && musttail_p) + { + if (TREE_CODE (retval) == CALL_EXPR) + CALL_EXPR_MUST_TAIL_CALL (retval) = 1; + else if (TREE_CODE (retval) == TARGET_EXPR + && TREE_CODE (TARGET_EXPR_INITIAL (retval)) == CALL_EXPR) + CALL_EXPR_MUST_TAIL_CALL (TARGET_EXPR_INITIAL (retval)) = 1; + else + error_at (xloc, "cannot tail-call: return value must be call"); + } + if (retval) { tree semantic_type = NULL_TREE;