From patchwork Thu Aug 3 11:07:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Javier Martinez X-Patchwork-Id: 130478 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9f41:0:b0:3e4:2afc:c1 with SMTP id v1csp1067995vqx; Thu, 3 Aug 2023 04:08:47 -0700 (PDT) X-Google-Smtp-Source: APBJJlHHnzT8bvPHquUF/j3kmDcPMmdYZc6DmhbMuG4YAWkXAeAoyTaY8Pvz6iaeXmWwBSH9ZKGv X-Received: by 2002:a17:906:8a64:b0:99c:1c8e:ac20 with SMTP id hy4-20020a1709068a6400b0099c1c8eac20mr7209458ejc.22.1691060926805; Thu, 03 Aug 2023 04:08:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691060926; cv=none; d=google.com; s=arc-20160816; b=JOIUsszjCacB9S/5iZoo7xTJGJFrEisb+PtkOcDVOevbli4whXlpxlaitUoXniEp3r yc9Koju2M6DE1BipyumiBaLNuZmYJFC0DFeNWtTbDy35r3GHLIb4+j3nM58D+pqg65Y1 Gz8+4GkdMXKhHdmvaybQb7msetGq2FvWQ2vtt1+sels2eqNWCKfEv9II+SAyVWOpjM4o kRGUL0+ukS8nAXpZ4bjX+DHX9ZBeV7YZUGqxiyljKpT2kqcU0GXY8W0kBAif6u0GW7nq RPV1fii1RNpFPsaK5kqJgZS84NvrVlLUP2wJ2i5cperxxKt1QL2w7R5nPzEFc9ptMJ1a R0Pw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence:to:subject :message-id:date:mime-version:dmarc-filter:delivered-to :dkim-signature:dkim-filter; bh=zeXnFPaEiyOKI1E+UW8txUZPXBoXp5XN+YDjklP6uHI=; fh=k8u0S0BkZBmBAuWvcl9OPOUaCLI5YqsUgqU4cl155Ug=; b=HVEjomlj5pre9wlxTuwPJAMp8CcaQ+qt0ojdyBbYvXW/e0MBeuI9TMs0NhAFI9wiKI Z1WrlWy/Q92JWu4tbPoN2ACW6y47tw8qmtPpXtJslk0U+dWQAS1nsfJ9ivvYDp3D9D1p xxe17qqrWYp2p0Sn2aEBCBt7cyG6wNIOAEx+UTWc12m2wKazQC9R1pp7lgeiLPcaPQzy AeDZeCeta7qUWndhC65CnktZYkYfL2QjpyCs9gj0RAb8X8kTjFfxDQRQPutpio5+EJsn YWweV6VaIFtQUj4JV2rCG4+e9hK9cxvBGrbBigl5zxCQiSew0jahvlNd8yqPfIzH7OwV 9Yqg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=qG5oLrs4; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id n16-20020a170906841000b0099bd00f8f73si8731307ejx.742.2023.08.03.04.08.46 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Aug 2023 04:08:46 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=qG5oLrs4; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id BD0003858C2A for ; Thu, 3 Aug 2023 11:08:45 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org BD0003858C2A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1691060925; bh=zeXnFPaEiyOKI1E+UW8txUZPXBoXp5XN+YDjklP6uHI=; h=Date:Subject:To:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=qG5oLrs48lXYHrWw2iHS8ok+m1ZHlWWnm6l2BXtKHC7sL3BMBmQJLtyiHjtW3RG11 //JyeuCgGePziBvZKccRCm7lN/BM7PXXBRinmU3fwUhqkVKaWoyHsJXehqwFtRIPuy AGzqKa7BDyU7mw5baY9w4TnNhbGIRyOwvIlFZFpw= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-lj1-x232.google.com (mail-lj1-x232.google.com [IPv6:2a00:1450:4864:20::232]) by sourceware.org (Postfix) with ESMTPS id 51DD93858D35 for ; Thu, 3 Aug 2023 11:08:01 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 51DD93858D35 Received: by mail-lj1-x232.google.com with SMTP id 38308e7fff4ca-2b9ab1725bbso12662531fa.0 for ; Thu, 03 Aug 2023 04:08:01 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691060880; x=1691665680; h=to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=DDsPGWn0PPLFqNpEPe39Y3/QDjOGjKEHHZOr22YN2zs=; b=FW3xNxuKPsAdWKmzSciNvlTrxM+WgGi/Lh0edHde9LTa5QNzIvGu8gbXOJyZxWtWZn icTQd1IEsOPOoUuECPOOkoc92AqUPEtLAvfBD3+aRCHQZWeTsBq8FIJxS9tsQvYMVO00 DXx64X2k1NLd2GIoWFaz6YsnCG0SXwG2U/2fXHxtwjNpG0EeiG1cHlpZnqylXslrF/Eu n9DNMwVCBD9zcvinoiPHodAR3xlkY4LKjznF4OYtq78TFgJny5qgh10s7Ft64Q9VvDf2 T851ZnPpOGDIyeJf6ED4CjxOvou1tXCqWSiPVbYbJW7y6Tv6MmZmf2aDyYSE9IQhKvQy Z5Ug== X-Gm-Message-State: ABy/qLatH1A2fRS39Y7HPTyeFbs4ePWnchAbX0mjYLo/JPTCxMrWA7rv 7V1wa0qduX8MoFH1bHyFlmPq0d7rBH+tTzkubue9ciEySNe17TKgEN4= X-Received: by 2002:a2e:8813:0:b0:2b9:575e:5298 with SMTP id x19-20020a2e8813000000b002b9575e5298mr7404662ljh.13.1691060879488; Thu, 03 Aug 2023 04:07:59 -0700 (PDT) MIME-Version: 1.0 Date: Thu, 3 Aug 2023 13:07:48 +0200 Message-ID: Subject: [RFC] c++: extend cold, hot attributes to classes To: gcc-patches@gcc.gnu.org X-Spam-Status: No, score=-7.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, HTML_MESSAGE, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, 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-Content-Filtered-By: Mailman/MimeDel 2.1.29 X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Javier Martinez via Gcc-patches From: Javier Martinez Reply-To: Javier Martinez Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1773205902260613524 X-GMAIL-MSGID: 1773205902260613524 Most code is cold. This patch extends support for attribute ((cold)) to C++ Classes, Unions, and Structs (RECORD_TYPES and UNION_TYPES) to benefit from encapsulation - reducing the verbosity of using the attribute where deserved. The ((hot)) attribute is also extended for its semantic relation. What is the sentiment on this use-case? for gcc/c-family/ChangeLog * c-attribs.c (attribute_spec): Remove decl_required field for "hot" and "cold" attributes. (handle_cold_attribute): remove warning on RECORD_TYPE and UNION_TYPE when c_dialect_cxx. (handle_cold_attribute): remove warning on RECORD_TYPE and UNION_TYPE when c_dialect_cxx. for gcc/cp/ChangeLog * class.c (finish_struct) propagate hot and cold attributes to all FUNCTION_DECL when the class itself is marked hot or cold. for gcc/testsuite/ChangeLog * g++.dg/ext/attr-hotness.C: New. Signed-off-by: Javier Martinez foo(void) { } }; /* { dg-warning "ignoring attribute .hot. because it conflicts with attribute .cold." } */ + +/* { dg-final { scan-tree-dump-times "cold" 2 "gimple" } } */ +/* { dg-final { scan-tree-dump-times "hot" 2 "gimple" } } */ + diff --git a/gcc/c-family/c-attribs.c b/gcc/c-family/c-attribs.c index dc9579c..815df66 100644 --- a/gcc/c-family/c-attribs.c +++ b/gcc/c-family/c-attribs.c @@ -398,10 +398,10 @@ const struct attribute_spec c_common_attribute_table[] = { "alloc_size", 1, 2, false, true, true, false, handle_alloc_size_attribute, attr_alloc_exclusions }, - { "cold", 0, 0, true, false, false, false, + { "cold", 0, 0, false, false, false, false, handle_cold_attribute, attr_cold_hot_exclusions }, - { "hot", 0, 0, true, false, false, false, + { "hot", 0, 0, false, false, false, false, handle_hot_attribute, attr_cold_hot_exclusions }, { "no_address_safety_analysis", @@ -837,22 +837,23 @@ handle_noreturn_attribute (tree *node, tree name, tree ARG_UNUSED (args), static tree handle_hot_attribute (tree *node, tree name, tree ARG_UNUSED (args), - int ARG_UNUSED (flags), bool *no_add_attrs) + int ARG_UNUSED (flags), bool *no_add_attrs) { - if (TREE_CODE (*node) == FUNCTION_DECL - || TREE_CODE (*node) == LABEL_DECL) + if ( (TREE_CODE (*node) == FUNCTION_DECL || + TREE_CODE (*node) == LABEL_DECL) + || ((TREE_CODE(*node) == RECORD_TYPE || + TREE_CODE(*node) == UNION_TYPE) && c_dialect_cxx())) { /* Attribute hot processing is done later with lookup_attribute. */ } else { warning (OPT_Wattributes, "%qE attribute ignored", name); - *no_add_attrs = true; + *no_add_attrs = true; } return NULL_TREE; } - /* Handle a "cold" and attribute; arguments as in struct attribute_spec.handler. */ @@ -860,15 +861,17 @@ static tree handle_cold_attribute (tree *node, tree name, tree ARG_UNUSED (args), int ARG_UNUSED (flags), bool *no_add_attrs) { - if (TREE_CODE (*node) == FUNCTION_DECL - || TREE_CODE (*node) == LABEL_DECL) + if ( (TREE_CODE (*node) == FUNCTION_DECL || + TREE_CODE (*node) == LABEL_DECL) + || ((TREE_CODE(*node) == RECORD_TYPE || + TREE_CODE(*node) == UNION_TYPE) && c_dialect_cxx())) { /* Attribute cold processing is done later with lookup_attribute. */ } else { warning (OPT_Wattributes, "%qE attribute ignored", name); - *no_add_attrs = true; + *no_add_attrs = true; } return NULL_TREE; diff --git a/gcc/cp/class.c b/gcc/cp/class.c index 07abe52..70f734f 100644 --- a/gcc/cp/class.c +++ b/gcc/cp/class.c @@ -7540,6 +7540,35 @@ finish_struct (tree t, tree attributes) && !LAMBDA_TYPE_P (t)) add_stmt (build_min (TAG_DEFN, t)); + + /* classes marked with hotness attributes propagate the attribute to + all methods. We propagate these here as there is a guarantee that + TYPE_FIELDS is populated, as opposed from within decl_attributes. */ + + tree has_cold_attr = lookup_attribute("cold", TYPE_ATTRIBUTES(t)); + tree has_hot_attr = lookup_attribute("hot", TYPE_ATTRIBUTES(t)); + + if ( has_cold_attr || has_hot_attr ) { + + /* hoisted out of the loop */ + tree attr_cold_id = get_identifier("cold"); + tree attr_hot_id = get_identifier("hot"); + + for (tree f = TYPE_FIELDS (t); f; f = DECL_CHAIN (f)) + { + if (TREE_CODE (f) == FUNCTION_DECL) { + /* decl_attributes will take care of conflicts, + also prioritizing attributes explicitly marked in methods */ + + if (has_cold_attr) { + decl_attributes (&f, tree_cons (attr_cold_id, NULL, NULL), 0); + } else if (has_hot_attr) { + decl_attributes (&f, tree_cons (attr_hot_id, NULL, NULL), 0); + } + } + } + } + return t; } diff --git a/gcc/testsuite/g++.dg/ext/attr-hotness.C b/gcc/testsuite/g++.dg/ext/attr-hotness.C new file mode 100644 index 0000000..075c624 --- /dev/null +++ b/gcc/testsuite/g++.dg/ext/attr-hotness.C @@ -0,0 +1,15 @@ +/* { dg-do compile } */ +/* { dg-options "-O0 -Wattributes -fdump-tree-gimple" } */ + + +struct __attribute((cold)) A { __attribute((noinline, used)) void foo(void) { } }; + +struct __attribute((cold)) B { __attribute((noinline, used, hot)) void foo(void) { } }; /* { dg-warning "ignoring attribute .cold. because it conflicts with attribute .hot." } */ + +struct __attribute((hot)) D { __attribute((noinline, used)) void foo(void) { } }; + +struct __attribute((hot)) E { __attribute((noinline, used, cold)) void