From patchwork Tue Aug 15 13:41:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Javier Martinez X-Patchwork-Id: 135661 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:a888:0:b0:3f2:4152:657d with SMTP id x8csp438182vqo; Tue, 15 Aug 2023 06:43:00 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHtexnMbLxOdNL6N/mlAp9m6ENqflTwSBb0naRssZOkMCXP3gJfK5fkBhayewxWTQqfiSPQ X-Received: by 2002:a2e:9059:0:b0:2b6:a057:8098 with SMTP id n25-20020a2e9059000000b002b6a0578098mr9004696ljg.0.1692106979565; Tue, 15 Aug 2023 06:42:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1692106979; cv=none; d=google.com; s=arc-20160816; b=wIFbfJW+ZDfpaiKZfg7Z7g9x30HvlvdUEL/sowmcAHZZo9rECI29+mx+BNTXexAS1q lPJX6M/pTwSpQvLQJ2GTL7tFMG71TQGZpuxZ0/xU+nONVtK3/laBEUy7waFhTqJWN3wK 26oGcYAOeoCXm2k+H48NJdWYTNmpyxyscR6sm1VoDB0BNF6amGeGIcHLWHqrJo4pSqqd hiNjZy0jdtaz08HMsKsx8uzItAN3FU3K9fxp0kX6ENqbMba8sAjCBgA1Xd0fMQ4Glwod NLxTGRSbCxUPhcREi9n29J3UHgkC/fqrflynZJUAH5KwD352FKTAzji7opaNikI/Vwib rGMw== 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:cc:to:subject :message-id:date:mime-version:dmarc-filter:delivered-to :dkim-signature:dkim-filter; bh=tQx2Aa/BJMRvjm1qFd0QhyAj8qibCnvjS1HJAMnmFaY=; fh=d5QGVRk2N5a5/4r89dmOt0RH0wgWbIxx7/c9RbuhBI0=; b=k6M6wkZ/wvwRahkLt74nTdlTGNcZK7U4fWUKEZddkztIUqfPJyrtEEodUbsE/nDJLn anEqzC2m4tcZS28EFXkRmMAPZzLnoGlK/EU4bWh8ExdIQv39075xK24bNbggJTm8zcwr I4/5ibxJ8z2lh+aUIZ5BjRczvOKQqHS+VI1XY30PGZ269SolSiIyxosSBIGbPXQZ5/ZO G3DGXV9H1rdwlWXVl9+B3jhOmhvJZfAOnSmpj+74arPQpiFnI5iqDRUmpq5Kid3u29t/ 08pbOx+528VLWZoFdUeMPVGcovqmZmOztgGxfFKpd9YjIkAli/HMrISeb6MY/xOyvlHI IaUA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=FghEW8zc; 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=gnu.org Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id sa27-20020a170906edbb00b00993ebae9929si9172955ejb.708.2023.08.15.06.42.59 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Aug 2023 06:42:59 -0700 (PDT) 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=@gcc.gnu.org header.s=default header.b=FghEW8zc; 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=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 18B3C3856964 for ; Tue, 15 Aug 2023 13:42:58 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 18B3C3856964 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1692106978; bh=tQx2Aa/BJMRvjm1qFd0QhyAj8qibCnvjS1HJAMnmFaY=; h=Date:Subject:To:Cc:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=FghEW8zcpvZjQa8jm7MftiWKAqxMWKlh4Ml5OLQ83mERIvcsHwCKB3IOKFSsQEnTX 1QvOg3lt55+PzCcpflD1wS1DKBs5Zr5pY8tB6uES95LB7mQvMgGA1cVF5ZL4gIhNi0 nvflGDH6BHspxdzxEf9AfYb+84FgT/tYuvTESYaA= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-lj1-x241.google.com (mail-lj1-x241.google.com [IPv6:2a00:1450:4864:20::241]) by sourceware.org (Postfix) with ESMTPS id 42CD23857357 for ; Tue, 15 Aug 2023 13:42:11 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 42CD23857357 Received: by mail-lj1-x241.google.com with SMTP id 38308e7fff4ca-2b95d5ee18dso81149761fa.1 for ; Tue, 15 Aug 2023 06:42:11 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692106929; x=1692711729; h=cc:to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=KVtktNVRekSSK2OboMRzLm39v73h6VTWgQ1jwJhdg6c=; b=hS5eoXgWhXZS8ETYW4bliq4JWK3vgM61u0/6mTiV6BRRf55uFxAfhkCFL9U3axAxYx eJDuTb3hMClWMQamK18K18l6uIpTm196/kix7WEC8wC/jjYXkdUYwlmAUnLNuaTeqV48 Xm/4gcporsmSwWH95YDNjPWfZmIlUqZCh6Ded5Dcqibq4pfzTJPvQwPv7IIJQ6bracjo tmGBwoXaLyHGbNLdr0AMdoKbLXVHg0z19G2jPYA09hmbtTvhvDlfpJaprWqReN4VhF+B suJok0PB2BSHLutyjfLcP0UPmN47DLRKjJy2/dU/EpqSmPL51QaMR4Wr0MGw+qhMHvbK S4ww== X-Gm-Message-State: AOJu0YxTz9U+NbLrRz7GAsZ9bRsZe0Nxc9ajIzqGc68vTg00L2ulfIU4 DhzqqL6m55/YDTwmXzUSUfxJtCe8UMLnveX4WpU5Ut3V3h0q/qH+ X-Received: by 2002:a2e:3518:0:b0:2b6:cca1:9760 with SMTP id z24-20020a2e3518000000b002b6cca19760mr9036225ljz.27.1692106929308; Tue, 15 Aug 2023 06:42:09 -0700 (PDT) MIME-Version: 1.0 Date: Tue, 15 Aug 2023 15:41:58 +0200 Message-ID: Subject: [PATCH v4] c++: extend cold, hot attributes to classes To: gcc-patches@gcc.gnu.org Cc: jason@redhat.com X-Spam-Status: No, score=-9.0 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 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: 1774302768423069183 X-GMAIL-MSGID: 1774302768423069183 On Mon, Aug 14, 2023 at 8:32 PM Jason Merrill wrote: > I think you also want to check for ATTR_FLAG_TYPE_IN_PLACE. > [...] > > + propagate_class_warmth_attribute (t); > Maybe call this in check_bases_and_members instead? Yes, that is sensible. Done. Thanks, Javier Signed-off-by: Javier Martinez Signed-off-by: Javier Martinez gcc/c-family/ChangeLog: * c-attribs.cc (handle_hot_attribute): remove warning on RECORD_TYPE and UNION_TYPE when in c_dialect_xx. (handle_cold_attribute): Likewise. gcc/cp/ChangeLog: * class.cc (propagate_class_warmth_attribute): New function. (check_bases_and_members): propagate hot and cold attributes to all FUNCTION_DECL when the record is marked hot or cold. * cp-tree.h (maybe_propagate_warmth_attributes): New function. * decl2.cc (maybe_propagate_warmth_attributes): New function. * method.cc (lazily_declare_fn): propagate hot and cold attributes to lazily declared functions when the record is marked hot or cold. gcc/testsuite/ChangeLog: * g++.dg/ext/attr-hotness.C: New test. --- gcc/c-family/c-attribs.cc | 50 ++++++++++++++++++++++++- gcc/cp/class.cc | 31 +++++++++++++++ gcc/cp/cp-tree.h | 1 + gcc/cp/decl2.cc | 37 ++++++++++++++++++ gcc/cp/method.cc | 6 +++ gcc/testsuite/g++.dg/ext/attr-hotness.C | 16 ++++++++ 6 files changed, 139 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/g++.dg/ext/attr-hotness.C diff --git a/gcc/c-family/c-attribs.cc b/gcc/c-family/c-attribs.cc index e2792ca6898..25083d597c0 100644 --- a/gcc/c-family/c-attribs.cc +++ b/gcc/c-family/c-attribs.cc @@ -452,10 +452,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", @@ -1110,6 +1110,29 @@ handle_hot_attribute (tree *node, tree name, tree ARG_UNUSED (args), { /* Attribute hot processing is done later with lookup_attribute. */ } + else if ((TREE_CODE (*node) == RECORD_TYPE + || TREE_CODE (*node) == UNION_TYPE) + && c_dialect_cxx () + && (flags & (int) ATTR_FLAG_TYPE_IN_PLACE)) + { + /* Check conflict here as decl_attributes will otherwise only catch + it late at the function when the attribute is used on a class. */ + tree cold_attr = lookup_attribute ("cold", TYPE_ATTRIBUTES (*node)); + if (cold_attr) + { + warning (OPT_Wattributes, "ignoring attribute %qE because it " + "conflicts with attribute %qs", name, "cold"); + *no_add_attrs = true; + } + } + else if (flags & ((int) ATTR_FLAG_FUNCTION_NEXT + | (int) ATTR_FLAG_DECL_NEXT)) + { + /* Avoid applying the attribute to a function return type when + used as: void __attribute ((hot)) foo (void). It will be + passed to the function. */ + *no_add_attrs = true; + } else { warning (OPT_Wattributes, "%qE attribute ignored", name); @@ -1131,6 +1154,29 @@ handle_cold_attribute (tree *node, tree name, tree ARG_UNUSED (args), { /* Attribute cold processing is done later with lookup_attribute. */ } + else if ((TREE_CODE (*node) == RECORD_TYPE + || TREE_CODE (*node) == UNION_TYPE) + && c_dialect_cxx () + && (flags & (int) ATTR_FLAG_TYPE_IN_PLACE)) + { + /* Check conflict here as decl_attributes will otherwise only catch + it late at the function when the attribute is used on a class. */ + tree hot_attr = lookup_attribute ("hot", TYPE_ATTRIBUTES (*node)); + if (hot_attr) + { + warning (OPT_Wattributes, "ignoring attribute %qE because it " + "conflicts with attribute %qs", name, "hot"); + *no_add_attrs = true; + } + } + else if (flags & ((int) ATTR_FLAG_FUNCTION_NEXT + | (int) ATTR_FLAG_DECL_NEXT)) + { + /* Avoid applying the attribute to a function return type when + used as: void __attribute ((cold)) foo (void). It will be + passed to the function. */ + *no_add_attrs = true; + } else { warning (OPT_Wattributes, "%qE attribute ignored", name); diff --git a/gcc/cp/class.cc b/gcc/cp/class.cc index 778759237dc..bf0b558967f 100644 --- a/gcc/cp/class.cc +++ b/gcc/cp/class.cc @@ -205,6 +205,7 @@ static tree get_vcall_index (tree, tree); static bool type_maybe_constexpr_default_constructor (tree); static bool type_maybe_constexpr_destructor (tree); static bool field_poverlapping_p (tree); +static void propagate_class_warmth_attribute (tree); /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */ @@ -6253,6 +6254,12 @@ check_bases_and_members (tree t) allocating an array of this type. */ LANG_TYPE_CLASS_CHECK (t)->vec_new_uses_cookie = type_requires_array_cookie (t); + + /* Classes marked hot or cold propagate the attribute to all members. We + may do this now that methods are declared. This does miss some lazily + declared special member functions (CLASSTYPE_LAZY_*), which are handled + in lazily_declare_fn later on. */ + propagate_class_warmth_attribute (t); } /* If T needs a pointer to its virtual function table, set TYPE_VFIELD @@ -7733,6 +7740,30 @@ unreverse_member_declarations (tree t) } } +/* Classes, structs or unions T marked with hotness attributes propagate + the attribute to all methods. */ + +void +propagate_class_warmth_attribute (tree t) +{ + + if (t == NULL_TREE + || !(TREE_CODE (t) == RECORD_TYPE + || TREE_CODE (t) == UNION_TYPE)) + return; + + tree class_has_cold_attr = lookup_attribute ("cold", + TYPE_ATTRIBUTES (t)); + tree class_has_hot_attr = lookup_attribute ("hot", + TYPE_ATTRIBUTES (t)); + + if (class_has_cold_attr || class_has_hot_attr) + for (tree f = TYPE_FIELDS (t); f; f = DECL_CHAIN (f)) + if (TREE_CODE (f) == FUNCTION_DECL) + maybe_propagate_warmth_attributes (f, t); + +} + tree finish_struct (tree t, tree attributes) { diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h index d051ee85f70..4931a45607e 100644 --- a/gcc/cp/cp-tree.h +++ b/gcc/cp/cp-tree.h @@ -7033,6 +7033,7 @@ extern int parm_index (tree); extern tree vtv_start_verification_constructor_init_function (void); extern tree vtv_finish_verification_constructor_init_function (tree); extern void cp_check_const_attributes (tree); +extern void maybe_propagate_warmth_attributes (tree, tree); /* in error.cc */ extern const char *type_as_string (tree, int); diff --git a/gcc/cp/decl2.cc b/gcc/cp/decl2.cc index b402befba6d..b7c43e10a9d 100644 --- a/gcc/cp/decl2.cc +++ b/gcc/cp/decl2.cc @@ -1604,6 +1604,43 @@ cp_check_const_attributes (tree attributes) } } +/* Copies hot or cold attributes to a function FN if present on the + encapsulating class, struct, or union TYPE. */ + +void +maybe_propagate_warmth_attributes (tree fn, tree type) +{ + if (fn == NULL_TREE || type == NULL_TREE + || !(TREE_CODE (type) == RECORD_TYPE + || TREE_CODE (type) == UNION_TYPE)) + return; + + tree has_cold_attr = lookup_attribute ("cold", TYPE_ATTRIBUTES (type)); + tree has_hot_attr = lookup_attribute ("hot", TYPE_ATTRIBUTES (type)); + + if (has_cold_attr || has_hot_attr) + { + + /* Transparently ignore the new warmth attribute if it + conflicts with a present function attribute. Otherwise + decl_attribute would still honour the present attribute, + but producing an undesired warning in the process. */ + + if (has_cold_attr) + { + if (lookup_attribute ("hot", DECL_ATTRIBUTES (fn)) == NULL) + decl_attributes (&fn, + tree_cons (get_identifier ("cold"), NULL, NULL), 0); + } + else if (has_hot_attr) + { + if (lookup_attribute ("cold", DECL_ATTRIBUTES (fn)) == NULL) + decl_attributes (&fn, + tree_cons (get_identifier ("hot"), NULL, NULL), 0); + } + } +} + /* Return the last pushed declaration for the symbol DECL or NULL when no such declaration exists. */ diff --git a/gcc/cp/method.cc b/gcc/cp/method.cc index 4ba00175048..a70dd5d6adc 100644 --- a/gcc/cp/method.cc +++ b/gcc/cp/method.cc @@ -3592,6 +3592,12 @@ lazily_declare_fn (special_function_kind sfk, tree type) /* Create appropriate clones. */ clone_cdtor (fn, /*update_methods=*/true); + /* Classes, structs or unions TYPE marked with hotness attributes propagate + the attribute to all methods. This is typically done in + check_bases_and_members, but we must also inject them here for deferred + lazily-declared functions. */ + maybe_propagate_warmth_attributes (fn, type); + return fn; } 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 00000000000..f9a6930304d --- /dev/null +++ b/gcc/testsuite/g++.dg/ext/attr-hotness.C @@ -0,0 +1,16 @@ +/* { dg-do compile } */ +/* { dg-options "-O0 -Wattributes -fdump-tree-gimple" } */ + + +struct __attribute((cold)) A { __attribute((noinline, used)) void foo(void) { } }; + +struct __attribute((hot)) B { __attribute((noinline, used)) void foo(void) { } }; + +struct __attribute((hot, cold)) C { __attribute((noinline, used)) void foo(void) { } }; /* { dg-warning "ignoring attribute .cold. because it conflicts with attribute .hot." } */ + +struct __attribute((cold, hot)) D { __attribute((noinline, used)) void 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" } } */ + -- 2.34.1