[LLVMdev] Propagate clang attribute to IR

Aaron Ballman aaron at aaronballman.com
Tue Mar 24 07:46:27 PDT 2015

On Tue, Mar 24, 2015 at 10:40 AM, Rinaldini Julien
<julien.rinaldini at heig-vd.ch> wrote:
>> On 24 Mar 2015, at 14:55, Aaron Ballman <aaron at aaronballman.com> wrote:
>> On Tue, Mar 24, 2015 at 9:48 AM, Rinaldini Julien
>> <julien.rinaldini at heig-vd.ch> wrote:
>>> Hi,
>>> I want to *tag* some functions with some *flags*. I was using annotate((“myFlag”)) and everything was working fine until I tried on ObjC method. It seems that clang just ignore it.
>>> So, to be able to *flag* my functions I’m trying to add a *real* attribute to clang.
>>> I’ve added a new attribute to clang in tools/clang/include/clang/Basic/Attr.td:
>>> def NoFLA : Attr {
>>>  let Spellings = [GNU<"nofla">, CXX11<"gnu", "nofla">];
>>> }
>>> In tools/clang/lib/Sema/SemaDeclAttr.cpp I added my new attribute to the switch that handle it and created my own handler function.
>>> I’m trying to simply pass my attribute to the IR attribute list, but, sadly, I have absolutely no idea how to do that. I looked at others handler but none seems to do that. I also tried to use handleSimpleAttribute, but it doesn’t work either.
>>> Any idea how to do that (or enable annotate on ObjC method)?
>> handleSimpleAttribute takes the parsed attribute (AttributeList entry)
>> and turns it into a semantic attribute (Attr subclass). You have to
>> manually handle the semantic attribute in whatever way makes sense for
>> your attribute. Check out CodeGenModule::ConstructAttributeList for an
>> example of how function semantic attributes translate into IR
>> attributes to be passed along to LLVM.
>> HTH!
>> ~Aaron
> Hi,
> Thx for your answer...
> I tried that without any luck:
>   static void handleOBF(Sema &S, Decl *D, const AttributeList &Attr) {
>     D->addAttr(::new (S.Context)NoFLAAttr(Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
>     // ???????????
>     llvm::AttrBuilder FuncAttrs;
>     FuncAttrs.addAttribute();
> }
> In CodeGenModule::ConstructAttributeList it uses a AttrBuilder, I tried there to add a random attribute and it works.
> But I’m not understanding how to do that in SemaDeclAttr.cpp.

You can't do it from SemaDeclAttr.cpp. You have to do it from the
CodeGen layer. If you were using handleSimpleAttr in SemaDeclAttr.cpp,
that's the part which hooks the attribute to the function declaration
itself. When functions get code genned, you'd have to lower that
attribute information to the IR layer, and an AttrBuilder is the way
that happens.


More information about the llvm-dev mailing list