From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1643) id E1E4C3AA9C69; Wed, 8 Jun 2022 12:34:39 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E1E4C3AA9C69 Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Thomas Schwinge To: gcc-cvs@gcc.gnu.org Subject: [gcc/devel/rust/master] backend: handle cold attribute X-Act-Checkin: gcc X-Git-Author: liushuyu X-Git-Refname: refs/heads/devel/rust/master X-Git-Oldrev: 243ef0dfe713a9fc8d4d80feb488a58b2639f39f X-Git-Newrev: 16730054de50fa613b86addd9cc4d737cfb7caf8 Message-Id: <20220608123439.E1E4C3AA9C69@sourceware.org> Date: Wed, 8 Jun 2022 12:34:39 +0000 (GMT) X-BeenThere: gcc-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 08 Jun 2022 12:34:40 -0000 https://gcc.gnu.org/g:16730054de50fa613b86addd9cc4d737cfb7caf8 commit 16730054de50fa613b86addd9cc4d737cfb7caf8 Author: liushuyu Date: Thu Apr 21 17:30:52 2022 -0600 backend: handle cold attribute Diff: --- gcc/rust/backend/rust-compile-base.cc | 23 +++++++++++++++++++++++ gcc/rust/backend/rust-compile-base.h | 3 +++ gcc/rust/util/rust-attributes.cc | 15 ++++++++++----- gcc/testsuite/rust/compile/attr_cold.rs | 12 ++++++++++++ 4 files changed, 48 insertions(+), 5 deletions(-) diff --git a/gcc/rust/backend/rust-compile-base.cc b/gcc/rust/backend/rust-compile-base.cc index 602fc56c353..f138cb12478 100644 --- a/gcc/rust/backend/rust-compile-base.cc +++ b/gcc/rust/backend/rust-compile-base.cc @@ -55,6 +55,7 @@ HIRCompileBase::setup_attributes_on_fndecl ( bool is_inline = attr.get_path ().as_string ().compare ("inline") == 0; bool is_must_use = attr.get_path ().as_string ().compare ("must_use") == 0; + bool is_cold = attr.get_path ().as_string ().compare ("cold") == 0; if (is_inline) { handle_inline_attribute_on_fndecl (fndecl, attr); @@ -63,7 +64,29 @@ HIRCompileBase::setup_attributes_on_fndecl ( { handle_must_use_attribute_on_fndecl (fndecl, attr); } + else if (is_cold) + { + handle_cold_attribute_on_fndecl (fndecl, attr); + } + } +} + +void +HIRCompileBase::handle_cold_attribute_on_fndecl (tree fndecl, + const AST::Attribute &attr) +{ + // simple #[cold] + if (!attr.has_attr_input ()) + { + tree cold = get_identifier ("cold"); + // this will get handled by the GCC backend later + DECL_ATTRIBUTES (fndecl) + = tree_cons (cold, NULL_TREE, DECL_ATTRIBUTES (fndecl)); + return; } + + rust_error_at (attr.get_locus (), + "attribute % does not accept any arguments"); } void diff --git a/gcc/rust/backend/rust-compile-base.h b/gcc/rust/backend/rust-compile-base.h index c7f7f406ebd..68a3994450b 100644 --- a/gcc/rust/backend/rust-compile-base.h +++ b/gcc/rust/backend/rust-compile-base.h @@ -82,6 +82,9 @@ protected: static void handle_inline_attribute_on_fndecl (tree fndecl, const AST::Attribute &attr); + static void handle_cold_attribute_on_fndecl (tree fndecl, + const AST::Attribute &attr); + static void handle_must_use_attribute_on_fndecl (tree fndecl, const AST::Attribute &attr); diff --git a/gcc/rust/util/rust-attributes.cc b/gcc/rust/util/rust-attributes.cc index b1211fe792c..106500d87e6 100644 --- a/gcc/rust/util/rust-attributes.cc +++ b/gcc/rust/util/rust-attributes.cc @@ -22,11 +22,16 @@ namespace Rust { namespace Analysis { // https://doc.rust-lang.org/stable/nightly-rustc/src/rustc_feature/builtin_attrs.rs.html#248 -static const BuiltinAttrDefinition __definitions[] - = {{"inline", CODE_GENERATION}, {"cfg", EXPANSION}, - {"cfg_attr", EXPANSION}, {"allow", STATIC_ANALYSIS}, - {"doc", HIR_LOWERING}, {"lang", HIR_LOWERING}, - {"must_use", STATIC_ANALYSIS}}; +static const BuiltinAttrDefinition __definitions[] = { + {"inline", CODE_GENERATION}, + {"cold", CODE_GENERATION}, + {"cfg", EXPANSION}, + {"cfg_attr", EXPANSION}, + {"allow", STATIC_ANALYSIS}, + {"doc", HIR_LOWERING}, + {"must_use", STATIC_ANALYSIS}, + {"lang", HIR_LOWERING}, +}; BuiltinAttributeMappings * BuiltinAttributeMappings::get () diff --git a/gcc/testsuite/rust/compile/attr_cold.rs b/gcc/testsuite/rust/compile/attr_cold.rs new file mode 100644 index 00000000000..883d2f81932 --- /dev/null +++ b/gcc/testsuite/rust/compile/attr_cold.rs @@ -0,0 +1,12 @@ +// { dg-additional-options "-fdump-tree-gimple } +#[cold] +fn cold_function() -> i32 { + 42 +} + +fn main() -> i32 { + // { dg-final { scan-tree-dump-times {__attribute__((cdecl, cold))} 1 gimple } } + cold_function(); + + 0 +}