From: Benjamin Kramer Date: Sat, 12 May 2012 21:10:52 +0000 (+0000) Subject: Add support for __attribute__((hot)) and __attribute__((cold)). X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=ee409a916e9b97ed6bc2f48d8d6aac6b8b773d7b;p=clang Add support for __attribute__((hot)) and __attribute__((cold)). Currently cold functions are marked with the "optsize" attribute in CodeGen so they are always optimized for size. The hot attribute is just ignored, LLVM doesn't have a way to express hotness at the moment. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@156723 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Basic/Attr.td b/include/clang/Basic/Attr.td index fc886faab1..0b3b3cb33f 100644 --- a/include/clang/Basic/Attr.td +++ b/include/clang/Basic/Attr.td @@ -243,6 +243,10 @@ def Cleanup : InheritableAttr { let Args = [FunctionArgument<"FunctionDecl">]; } +def Cold : InheritableAttr { + let Spellings = ["cold"]; +} + def Common : InheritableAttr { let Spellings = ["common"]; } @@ -348,6 +352,10 @@ def GNUInline : InheritableAttr { let Spellings = ["gnu_inline"]; } +def Hot : InheritableAttr { + let Spellings = ["hot"]; +} + def IBAction : InheritableAttr { let Spellings = ["ibaction"]; } diff --git a/lib/CodeGen/CodeGenModule.cpp b/lib/CodeGen/CodeGenModule.cpp index 775f500fdb..f2dda5d658 100644 --- a/lib/CodeGen/CodeGenModule.cpp +++ b/lib/CodeGen/CodeGenModule.cpp @@ -523,6 +523,10 @@ void CodeGenModule::SetLLVMFunctionAttributesForDefinition(const Decl *D, !F->hasFnAttr(llvm::Attribute::NoInline)) F->addFnAttr(llvm::Attribute::AlwaysInline); + // FIXME: Communicate hot and cold attributes to LLVM more directly. + if (D->hasAttr()) + F->addFnAttr(llvm::Attribute::OptimizeForSize); + if (isa(D) || isa(D)) F->setUnnamedAddr(true); diff --git a/lib/Sema/SemaDeclAttr.cpp b/lib/Sema/SemaDeclAttr.cpp index de0e2d97b4..7c290f76ee 100644 --- a/lib/Sema/SemaDeclAttr.cpp +++ b/lib/Sema/SemaDeclAttr.cpp @@ -1294,6 +1294,46 @@ static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) { Str->getString())); } +static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) { + // Check the attribute arguments. + if (!checkAttributeNumArgs(S, Attr, 0)) + return; + + if (!isa(D)) { + S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) + << Attr.getName() << ExpectedFunction; + return; + } + + if (D->hasAttr()) { + S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible) + << Attr.getName() << "hot"; + return; + } + + D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context)); +} + +static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) { + // Check the attribute arguments. + if (!checkAttributeNumArgs(S, Attr, 0)) + return; + + if (!isa(D)) { + S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) + << Attr.getName() << ExpectedFunction; + return; + } + + if (D->hasAttr()) { + S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible) + << Attr.getName() << "cold"; + return; + } + + D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context)); +} + static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) { // Check the attribute arguments. if (!checkAttributeNumArgs(S, Attr, 0)) @@ -3825,6 +3865,8 @@ static void ProcessInheritableDeclAttr(Sema &S, Scope *scope, Decl *D, case AttributeList::AT_ownership_takes: case AttributeList::AT_ownership_holds: handleOwnershipAttr (S, D, Attr); break; + case AttributeList::AT_cold: handleColdAttr (S, D, Attr); break; + case AttributeList::AT_hot: handleHotAttr (S, D, Attr); break; case AttributeList::AT_naked: handleNakedAttr (S, D, Attr); break; case AttributeList::AT_noreturn: handleNoReturnAttr (S, D, Attr); break; case AttributeList::AT_nothrow: handleNothrowAttr (S, D, Attr); break; diff --git a/test/CodeGen/attr-coldhot.c b/test/CodeGen/attr-coldhot.c new file mode 100644 index 0000000000..b9bb299b5e --- /dev/null +++ b/test/CodeGen/attr-coldhot.c @@ -0,0 +1,9 @@ +// RUN: %clang_cc1 -emit-llvm %s -o - | FileCheck %s + +int test1() __attribute__((__cold__)) { + return 42; + +// Check that we set the optsize attribute on the function. +// CHECK: @test1{{.*}}optsize +// CHECK: ret +} diff --git a/test/Sema/attr-coldhot.c b/test/Sema/attr-coldhot.c new file mode 100644 index 0000000000..253b1898c0 --- /dev/null +++ b/test/Sema/attr-coldhot.c @@ -0,0 +1,10 @@ +// RUN: %clang_cc1 -fsyntax-only -verify %s + +int foo() __attribute__((__hot__)); +int bar() __attribute__((__cold__)); + +int var1 __attribute__((__cold__)); // expected-warning{{'__cold__' attribute only applies to functions}} +int var2 __attribute__((__hot__)); // expected-warning{{'__hot__' attribute only applies to functions}} + +int qux() __attribute__((__hot__)) __attribute__((__cold__)); // expected-error{{'__hot__' and cold attributes are not compatible}} +int baz() __attribute__((__cold__)) __attribute__((__hot__)); // expected-error{{'__cold__' and hot attributes are not compatible}}