[cfe-commits] r156723 - in /cfe/trunk: include/clang/Basic/Attr.td lib/CodeGen/CodeGenModule.cpp lib/Sema/SemaDeclAttr.cpp test/CodeGen/attr-coldhot.c test/Sema/attr-coldhot.c

Chandler Carruth chandlerc at google.com
Sat May 12 20:00:25 PDT 2012


Flatten isn't just an optimization issue, so clearly we shouldn't just
ignore it.  Hot and cold should have essentially no user detectable effect,
so I think ignoring them is fine until we can implement optimization passes
that use them.
On May 12, 2012 6:21 PM, "Nico Weber" <thakis at chromium.org> wrote:

> Should other unimplemented optimization attributes (e.g. "flatten") be
> silently ignored instead of issuing a warning too? How do you decide which
> attributes to warn on and which to ignore silently?
>
> Nico
>
> On Sat, May 12, 2012 at 2:10 PM, Benjamin Kramer <benny.kra at googlemail.com
> > wrote:
>
>> Author: d0k
>> Date: Sat May 12 16:10:52 2012
>> New Revision: 156723
>>
>> URL: http://llvm.org/viewvc/llvm-project?rev=156723&view=rev
>> Log:
>> 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.
>>
>> Added:
>>    cfe/trunk/test/CodeGen/attr-coldhot.c
>>    cfe/trunk/test/Sema/attr-coldhot.c
>> Modified:
>>    cfe/trunk/include/clang/Basic/Attr.td
>>    cfe/trunk/lib/CodeGen/CodeGenModule.cpp
>>    cfe/trunk/lib/Sema/SemaDeclAttr.cpp
>>
>> Modified: cfe/trunk/include/clang/Basic/Attr.td
>> URL:
>> http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/Attr.td?rev=156723&r1=156722&r2=156723&view=diff
>>
>> ==============================================================================
>> --- cfe/trunk/include/clang/Basic/Attr.td (original)
>> +++ cfe/trunk/include/clang/Basic/Attr.td Sat May 12 16:10:52 2012
>> @@ -243,6 +243,10 @@
>>   let Args = [FunctionArgument<"FunctionDecl">];
>>  }
>>
>> +def Cold : InheritableAttr {
>> +  let Spellings = ["cold"];
>> +}
>> +
>>  def Common : InheritableAttr {
>>   let Spellings = ["common"];
>>  }
>> @@ -348,6 +352,10 @@
>>   let Spellings = ["gnu_inline"];
>>  }
>>
>> +def Hot : InheritableAttr {
>> +  let Spellings = ["hot"];
>> +}
>> +
>>  def IBAction : InheritableAttr {
>>   let Spellings = ["ibaction"];
>>  }
>>
>> Modified: cfe/trunk/lib/CodeGen/CodeGenModule.cpp
>> URL:
>> http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenModule.cpp?rev=156723&r1=156722&r2=156723&view=diff
>>
>> ==============================================================================
>> --- cfe/trunk/lib/CodeGen/CodeGenModule.cpp (original)
>> +++ cfe/trunk/lib/CodeGen/CodeGenModule.cpp Sat May 12 16:10:52 2012
>> @@ -523,6 +523,10 @@
>>       !F->hasFnAttr(llvm::Attribute::NoInline))
>>     F->addFnAttr(llvm::Attribute::AlwaysInline);
>>
>> +  // FIXME: Communicate hot and cold attributes to LLVM more directly.
>> +  if (D->hasAttr<ColdAttr>())
>> +    F->addFnAttr(llvm::Attribute::OptimizeForSize);
>> +
>>   if (isa<CXXConstructorDecl>(D) || isa<CXXDestructorDecl>(D))
>>     F->setUnnamedAddr(true);
>>
>>
>> Modified: cfe/trunk/lib/Sema/SemaDeclAttr.cpp
>> URL:
>> http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDeclAttr.cpp?rev=156723&r1=156722&r2=156723&view=diff
>>
>> ==============================================================================
>> --- cfe/trunk/lib/Sema/SemaDeclAttr.cpp (original)
>> +++ cfe/trunk/lib/Sema/SemaDeclAttr.cpp Sat May 12 16:10:52 2012
>> @@ -1294,6 +1294,46 @@
>>                                          Str->getString()));
>>  }
>>
>> +static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) {
>> +  // Check the attribute arguments.
>> +  if (!checkAttributeNumArgs(S, Attr, 0))
>> +    return;
>> +
>> +  if (!isa<FunctionDecl>(D)) {
>> +    S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
>> +      << Attr.getName() << ExpectedFunction;
>> +    return;
>> +  }
>> +
>> +  if (D->hasAttr<HotAttr>()) {
>> +    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<FunctionDecl>(D)) {
>> +    S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
>> +      << Attr.getName() << ExpectedFunction;
>> +    return;
>> +  }
>> +
>> +  if (D->hasAttr<ColdAttr>()) {
>> +    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 @@
>>   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;
>>
>> Added: cfe/trunk/test/CodeGen/attr-coldhot.c
>> URL:
>> http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/attr-coldhot.c?rev=156723&view=auto
>>
>> ==============================================================================
>> --- cfe/trunk/test/CodeGen/attr-coldhot.c (added)
>> +++ cfe/trunk/test/CodeGen/attr-coldhot.c Sat May 12 16:10:52 2012
>> @@ -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
>> +}
>>
>> Added: cfe/trunk/test/Sema/attr-coldhot.c
>> URL:
>> http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/attr-coldhot.c?rev=156723&view=auto
>>
>> ==============================================================================
>> --- cfe/trunk/test/Sema/attr-coldhot.c (added)
>> +++ cfe/trunk/test/Sema/attr-coldhot.c Sat May 12 16:10:52 2012
>> @@ -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}}
>>
>>
>> _______________________________________________
>> cfe-commits mailing list
>> cfe-commits at cs.uiuc.edu
>> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits
>>
>
>
> _______________________________________________
> cfe-commits mailing list
> cfe-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20120512/c1ebbda2/attachment.html>


More information about the cfe-commits mailing list