backend: handle cold attribute

This commit is contained in:
liushuyu 2022-04-21 17:30:52 -06:00
parent 243ef0dfe7
commit 16730054de
No known key found for this signature in database
GPG Key ID: 23D1CE4534419437
4 changed files with 48 additions and 5 deletions

View File

@ -55,6 +55,7 @@ HIRCompileBase::setup_attributes_on_fndecl (
bool is_inline = attr.get_path ().as_string ().compare ("inline") == 0; bool is_inline = attr.get_path ().as_string ().compare ("inline") == 0;
bool is_must_use bool is_must_use
= attr.get_path ().as_string ().compare ("must_use") == 0; = attr.get_path ().as_string ().compare ("must_use") == 0;
bool is_cold = attr.get_path ().as_string ().compare ("cold") == 0;
if (is_inline) if (is_inline)
{ {
handle_inline_attribute_on_fndecl (fndecl, attr); handle_inline_attribute_on_fndecl (fndecl, attr);
@ -63,9 +64,31 @@ HIRCompileBase::setup_attributes_on_fndecl (
{ {
handle_must_use_attribute_on_fndecl (fndecl, attr); 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 %<cold%> does not accept any arguments");
}
void void
HIRCompileBase::handle_inline_attribute_on_fndecl (tree fndecl, HIRCompileBase::handle_inline_attribute_on_fndecl (tree fndecl,
const AST::Attribute &attr) const AST::Attribute &attr)

View File

@ -82,6 +82,9 @@ protected:
static void handle_inline_attribute_on_fndecl (tree fndecl, static void handle_inline_attribute_on_fndecl (tree fndecl,
const AST::Attribute &attr); 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, static void handle_must_use_attribute_on_fndecl (tree fndecl,
const AST::Attribute &attr); const AST::Attribute &attr);

View File

@ -22,11 +22,16 @@ namespace Rust {
namespace Analysis { namespace Analysis {
// https://doc.rust-lang.org/stable/nightly-rustc/src/rustc_feature/builtin_attrs.rs.html#248 // https://doc.rust-lang.org/stable/nightly-rustc/src/rustc_feature/builtin_attrs.rs.html#248
static const BuiltinAttrDefinition __definitions[] static const BuiltinAttrDefinition __definitions[] = {
= {{"inline", CODE_GENERATION}, {"cfg", EXPANSION}, {"inline", CODE_GENERATION},
{"cfg_attr", EXPANSION}, {"allow", STATIC_ANALYSIS}, {"cold", CODE_GENERATION},
{"doc", HIR_LOWERING}, {"lang", HIR_LOWERING}, {"cfg", EXPANSION},
{"must_use", STATIC_ANALYSIS}}; {"cfg_attr", EXPANSION},
{"allow", STATIC_ANALYSIS},
{"doc", HIR_LOWERING},
{"must_use", STATIC_ANALYSIS},
{"lang", HIR_LOWERING},
};
BuiltinAttributeMappings * BuiltinAttributeMappings *
BuiltinAttributeMappings::get () BuiltinAttributeMappings::get ()

View File

@ -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
}