Skip to content

Commit d541ded

Browse files
bors[bot]liushuyu
andauthored
Merge #1148
1148: backend: handle cold attribute r=philberty a=liushuyu - handle the `cold` attribute Co-authored-by: liushuyu <liushuyu011@gmail.com>
2 parents 75ac2f6 + 1673005 commit d541ded

File tree

4 files changed

+48
-5
lines changed

4 files changed

+48
-5
lines changed

gcc/rust/backend/rust-compile-base.cc

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ HIRCompileBase::setup_attributes_on_fndecl (
5555
bool is_inline = attr.get_path ().as_string ().compare ("inline") == 0;
5656
bool is_must_use
5757
= attr.get_path ().as_string ().compare ("must_use") == 0;
58+
bool is_cold = attr.get_path ().as_string ().compare ("cold") == 0;
5859
if (is_inline)
5960
{
6061
handle_inline_attribute_on_fndecl (fndecl, attr);
@@ -63,7 +64,29 @@ HIRCompileBase::setup_attributes_on_fndecl (
6364
{
6465
handle_must_use_attribute_on_fndecl (fndecl, attr);
6566
}
67+
else if (is_cold)
68+
{
69+
handle_cold_attribute_on_fndecl (fndecl, attr);
70+
}
71+
}
72+
}
73+
74+
void
75+
HIRCompileBase::handle_cold_attribute_on_fndecl (tree fndecl,
76+
const AST::Attribute &attr)
77+
{
78+
// simple #[cold]
79+
if (!attr.has_attr_input ())
80+
{
81+
tree cold = get_identifier ("cold");
82+
// this will get handled by the GCC backend later
83+
DECL_ATTRIBUTES (fndecl)
84+
= tree_cons (cold, NULL_TREE, DECL_ATTRIBUTES (fndecl));
85+
return;
6686
}
87+
88+
rust_error_at (attr.get_locus (),
89+
"attribute %<cold%> does not accept any arguments");
6790
}
6891

6992
void

gcc/rust/backend/rust-compile-base.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ class HIRCompileBase
8282
static void handle_inline_attribute_on_fndecl (tree fndecl,
8383
const AST::Attribute &attr);
8484

85+
static void handle_cold_attribute_on_fndecl (tree fndecl,
86+
const AST::Attribute &attr);
87+
8588
static void handle_must_use_attribute_on_fndecl (tree fndecl,
8689
const AST::Attribute &attr);
8790

gcc/rust/util/rust-attributes.cc

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,16 @@ namespace Rust {
2222
namespace Analysis {
2323

2424
// https://doc.rust-lang.org/stable/nightly-rustc/src/rustc_feature/builtin_attrs.rs.html#248
25-
static const BuiltinAttrDefinition __definitions[]
26-
= {{"inline", CODE_GENERATION}, {"cfg", EXPANSION},
27-
{"cfg_attr", EXPANSION}, {"allow", STATIC_ANALYSIS},
28-
{"doc", HIR_LOWERING}, {"lang", HIR_LOWERING},
29-
{"must_use", STATIC_ANALYSIS}};
25+
static const BuiltinAttrDefinition __definitions[] = {
26+
{"inline", CODE_GENERATION},
27+
{"cold", CODE_GENERATION},
28+
{"cfg", EXPANSION},
29+
{"cfg_attr", EXPANSION},
30+
{"allow", STATIC_ANALYSIS},
31+
{"doc", HIR_LOWERING},
32+
{"must_use", STATIC_ANALYSIS},
33+
{"lang", HIR_LOWERING},
34+
};
3035

3136
BuiltinAttributeMappings *
3237
BuiltinAttributeMappings::get ()
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// { dg-additional-options "-fdump-tree-gimple }
2+
#[cold]
3+
fn cold_function() -> i32 {
4+
42
5+
}
6+
7+
fn main() -> i32 {
8+
// { dg-final { scan-tree-dump-times {__attribute__((cdecl, cold))} 1 gimple } }
9+
cold_function();
10+
11+
0
12+
}

0 commit comments

Comments
 (0)