From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id DB5243858D1E for ; Tue, 7 May 2024 03:02:28 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org DB5243858D1E Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org DB5243858D1E Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1715050950; cv=none; b=PVuMUcU3gcneMB2QoqxqybBQO+i0PUuUv3pK0ag+YOOJZKhYUfaEKuEgy9yPbbXM192V4kj+17XVjJT8/LFzsU37CYwLSuYEr37K7e5xCpgF8YP/AYnVRUJah8zcv7moDbpFhZ6GcmsOz/zY8bhPY1EX4asDOkicJ1FqMzHjt3E= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1715050950; c=relaxed/simple; bh=MUqgQPCw8bc0RDFNIMK8wpixzOXifxoNOR748NIbJRQ=; h=DKIM-Signature:Message-ID:Date:MIME-Version:Subject:To:From; b=OGMpLxPY4U488DzvVWhLrpkulU26hrlTLuX9b2OrGC/PNRiuaq+E7o+9WjJfBp82EoBpHkAMrPUocfkrf89bvqpLL0GQbeJUkk8+cdH3oR7CjxQ0KncpVdo4SMeASF3aPkb9jO7m77UHjzqowkas4YnA3zEHRff5FJdVo14uLMw= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1715050948; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=vT4ZYAzOK+aTAeHpFxhTzdnA8k4ZBdOoPp3TGbla4xs=; b=YL9aENdkDCnNj8H4hAOR5hNsRv1DMAMSuf6opVx8oHR49LAD1dEkhPpch/ifC8NLDxX9A8 hioKI4Cq1qowcmqrunGgK0aPeI/MGFhMVReh+iMaxxVYN21BrB7qkdG5GfBj6Am0WIzs1O bxqFaeIItm/aZlH12zaOiV3YEdNxQxg= Received: from mail-pf1-f198.google.com (mail-pf1-f198.google.com [209.85.210.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-632-ZP8SzZ7aMmKOq6ONxEcPHA-1; Mon, 06 May 2024 23:02:24 -0400 X-MC-Unique: ZP8SzZ7aMmKOq6ONxEcPHA-1 Received: by mail-pf1-f198.google.com with SMTP id d2e1a72fcca58-6f441afba80so3346346b3a.3 for ; Mon, 06 May 2024 20:02:24 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715050944; x=1715655744; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=vT4ZYAzOK+aTAeHpFxhTzdnA8k4ZBdOoPp3TGbla4xs=; b=t/+sZQ7AcQagUt2l0MB4chFzSJbD71Lu+oqryE5e1nRg0CpidL5ERPnK65S0znIpTp QFCqVvg3B4m/qKMhneUXeztx7+IPx3A0WGxz9Fi5pnTWQ1GQp2uHxyhjYHcXAFFLEhXD 6MdmaUJgrjSfUnR41csdO9T+vx8xyALyX59ZO7cOb+9AImhYaAbWLZF3jLeJIK5bB24T rYb4mi2zVJ99be0t8zJRxtNr2utXAvlRkdYjFIRrTVcwq4wWFNBP/llm2D2AJdCR0kim r4VIVEVfZBvTiZMwPoSt8bKwqTnxhRln6wc3MC4iGysYRG/qKGwYmMNSrMfYOEO/YIyv lB7A== X-Forwarded-Encrypted: i=1; AJvYcCWfBtPdKzmTueYezFKlLdwKDj0cu3mxJHqpzR+MxLiAYIt11jKK6KbvOCgHjQ6la04JLAx182yn2m4GwvNFyXXHpjPQn8caIw== X-Gm-Message-State: AOJu0Yzu9lvJVkolQxlm5jxX6VUX7bEPDvM1JDhzB1r+NzCELpVhRQkY pD8WjnjQa34DOQv/6jkjoZ6YyPSwIDVQur3PKysre90PI6gx8IP/nNl8HlnWi1Pj7LWMMNiv0ft qtAwhzNLKFT+zqojiY+o7nSgPWE1z0/xUTQMJxFZC8+ES9yDK6DATzBI= X-Received: by 2002:a05:6a21:2d88:b0:1af:b1c0:c9eb with SMTP id ty8-20020a056a212d8800b001afb1c0c9ebmr5331827pzb.45.1715050943720; Mon, 06 May 2024 20:02:23 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFiSD7uf6zTr3Mcu5AB0M3KIsmGg8PWzC5UbQsYGjkmfBQoQ+uuiV5JdGJAtA8yFuFedsM/vA== X-Received: by 2002:a05:6a21:2d88:b0:1af:b1c0:c9eb with SMTP id ty8-20020a056a212d8800b001afb1c0c9ebmr5331803pzb.45.1715050943231; Mon, 06 May 2024 20:02:23 -0700 (PDT) Received: from [192.168.1.130] (130-44-146-16.s12558.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.146.16]) by smtp.gmail.com with ESMTPSA id d15-20020a170902654f00b001e89e7b2b74sm8962034pln.235.2024.05.06.20.02.21 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 06 May 2024 20:02:22 -0700 (PDT) Message-ID: <13a46de1-6551-4ff4-abac-2269e587d857@redhat.com> Date: Mon, 6 May 2024 23:02:20 -0400 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v5 2/5] C++: Support clang compatible [[musttail]] (PR83324) To: Andi Kleen , gcc-patches@gcc.gnu.org Cc: nathan@acm.org, josmyers@redhat.com, richard.sandiford@arm.com References: <20240505181458.2903045-1-ak@linux.intel.com> <20240505181458.2903045-2-ak@linux.intel.com> From: Jason Merrill In-Reply-To: <20240505181458.2903045-2-ak@linux.intel.com> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-6.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,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 List-Id: On 5/5/24 14:14, Andi Kleen wrote: > This patch implements a clang compatible [[musttail]] attribute for > returns. Thanks. > musttail is useful as an alternative to computed goto for interpreters. > With computed goto the interpreter function usually ends up very big > which causes problems with register allocation and other per function > optimizations not scaling. With musttail the interpreter can be instead > written as a sequence of smaller functions that call each other. To > avoid unbounded stack growth this requires forcing a sibling call, which > this attribute does. It guarantees an error if the call cannot be tail > called which allows the programmer to fix it instead of risking a stack > overflow. Unlike computed goto it is also type-safe. > > It turns out that David Malcolm had already implemented middle/backend > support for a musttail attribute back in 2016, but it wasn't exposed > to any frontend other than a special plugin. > > This patch adds a [[gnu::musttail]] attribute for C++ that can be added > to return statements. The return statement must be a direct call > (it does not follow dependencies), which is similar to what clang > implements. It then uses the existing must tail infrastructure. > > For compatibility it also detects clang::musttail > > One problem is that tree-tailcall usually fails when optimization > is disabled, which implies the attribute only really works with > optimization on. But that seems to be a reasonable limitation. > > Passes bootstrap and full test > > PR83324 > > gcc/cp/ChangeLog: > > * cp-tree.h (finish_return_stmt): Add musttail_p. > (check_return_expr): Dito. > * parser.cc (cp_parser_statement): Handle [[musttail]]. > (cp_parser_std_attribute): Dito. > (cp_parser_init_statement): Dito. > (cp_parser_jump_statement): Dito. > * semantics.cc (finish_return_stmt): Dito. > * typeck.cc (check_return_expr): Handle musttail_p flag. > --- > gcc/cp/cp-tree.h | 4 ++-- > gcc/cp/parser.cc | 30 ++++++++++++++++++++++++------ > gcc/cp/semantics.cc | 6 +++--- > gcc/cp/typeck.cc | 20 ++++++++++++++++++-- > 4 files changed, 47 insertions(+), 13 deletions(-) > > @@ -12734,9 +12734,27 @@ cp_parser_statement (cp_parser* parser, tree in_statement_expr, > NULL_TREE, false); > break; > > + case RID_RETURN: > + { > + bool musttail_p = false; > + std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc); > + if (lookup_attribute ("gnu", "musttail", std_attrs)) > + { > + musttail_p = true; > + std_attrs = remove_attribute ("gnu", "musttail", std_attrs); > + } > + // support this for compatibility > + if (lookup_attribute ("clang", "musttail", std_attrs)) > + { > + musttail_p = true; > + std_attrs = remove_attribute ("clang", "musttail", std_attrs); > + } > + statement = cp_parser_jump_statement (parser, musttail_p); It seems to me that if we were to pass &std_attrs to cp_parser_jump_statement, we could handle this entirely in that function rather than adding a flag to finish_return_stmt and check_return_stmt. > @@ -30189,7 +30207,7 @@ cp_parser_std_attribute (cp_parser *parser, tree attr_ns) > /* Maybe we don't expect to see any arguments for this attribute. */ > const attribute_spec *as > = lookup_attribute_spec (TREE_PURPOSE (attribute)); > - if (as && as->max_length == 0) > + if ((as && as->max_length == 0) || is_attribute_p ("musttail", attr_id)) I'd prefer to add an attribute to the table, rather than special-case it here; apart from consistency, it seems likely that someone will later want to apply it to a function. You need a template testcase; I expect it doesn't work in templates with the current patch. It's probably enough to copy it in tsubst_expr where we currently propagate CALL_EXPR_OPERATOR_SYNTAX. You also need a testcase where the function returns a class; in that case the call will often appear as AGGR_INIT_EXPR rather than CALL_EXPR, so you'll need to handle that as well. And see the places that copy flags like CALL_EXPR_OPERATOR_SYNTAX between CALL_EXPR and AGGR_INIT_EXPR. Jason