[llvm-dev] Adding a function attribute with an argument

591 views
Skip to first unread message

Tehila Mayzels via llvm-dev

unread,
Oct 8, 2015, 3:02:23 AM10/8/15
to llvm...@lists.llvm.org

Hi,

 

I'm trying to add a function attribute to clang/llvm.

I need an attribute that will have an additional information of a variable name.

e.g. I would like to add support for something like:

 

void foo() __attribute__ ((xyz(v)) {…}

 

such that on the IR stage I can realize that the function foo has the attribute xyz and an argument v that  marks variable v (which is a variable inside foo) for my needs.

 

I was able to add a function attribute without an argument, but I don't understand how to parse/save/analyze the argument.

 

I would appreciate any ideas/examples/references.

 

Thanks a lot,

Tehila.

 

 

Gaël Jobin via llvm-dev

unread,
Oct 8, 2015, 4:08:17 AM10/8/15
to llvm...@lists.llvm.org
Hi Tehila,

I recently added new function attributes to clang/llvm with arguments.

What I did is the following:

1. Add your attributes to tools/clang/include/clang/Basic/Attr.td

def MYATTR : InheritableAttr {
  let Spellings = [GNU<"myattr">, CXX11<"gnu", "myattr">];
  let Args = [DefaultIntArgument<"myargument", 5>];
  let Subjects = SubjectList<[Function]>;
  let Documentation = [Undocumented];
}

2. Manage it in tools/clang/lib/CodeGen/CGCall.cpp...

void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI,
                                           const Decl *TargetDecl,
                                           AttributeListType &PAL,
                                           unsigned &CallingConv,
                                           bool AttrOnCallSite) {
//other code...
if (TargetDecl) {
if (TargetDecl->hasAttr<MYEXAMPLEAttr>())
{
auto myexample = TargetDecl->getAttr<MYEXAMPLEAttr>();
FuncAttrs.addAttribute("myexample");
unsigned myargument = myexample->getMyargument();


FuncAttrs.addAttribute("myargument", llvm::utostr(myargument));
}
//other code...
}
//other code...
}


3. ...and in tools/clang/lib/Sema/SemaDeclAttr.cpp

static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
                                 const AttributeList &Attr,
                                 bool IncludeCXX11Attributes) {
//other code...
switch (Attr.getKind()) {
case AttributeList::AT_MYEXAMPLE:
handleMYEXAMPLEAttr(S, D, Attr);
break;
//other code...
}


static void handleMYEXAMPLEAttr(Sema &S, Decl *D, const AttributeList &Attr) {
uint32_t myargument = AVMAttr::DefaultMyargument;

if (Attr.getNumArgs() && !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), myargument))
return;

D->addAttr(::new (S.Context)
AVMAttr(Attr.getRange(), S.Context, myargument,
Attr.getAttributeSpellingListIndex()));
}

Then you should have access to it in LLVM IR.

I may have forgotten some stuff but it should help you get started.

Regards,
Gael
_______________________________________________
LLVM Developers mailing list
llvm...@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev

Tehila Mayzels via llvm-dev

unread,
Oct 13, 2015, 8:55:34 AM10/13/15
to gael....@switzerlandmail.ch, llvm...@lists.llvm.org

Hi Gael,

 

Thanks a lot for your response, it was very helpful!

How did you manage to access the argument value at the IR stage?

 

Thanks again,

Tehila.

 

Gaël Jobin via llvm-dev llvm-dev at lists.llvm.org 
Thu Oct 8 01:08:05 PDT 2015

Gaël Jobin via llvm-dev

unread,
Oct 13, 2015, 10:18:46 AM10/13/15
to llvm...@lists.llvm.org
Hi Tehila,

Le mardi 13 octobre 2015 à 15:55 +0300, Tehila Mayzels a écrit :

How did you manage to access the argument value at the IR stage?


Something like this if I remember correctly:

Function *f = ...;
int myargument;

if(f->hasFnAttribute("myexample"))
{
if(f->hasFnAttribute("myargument"))
{
myargument = f->getFnAttribute("myargument").getValueAsInt();
}
}

//...

Regards,
Gaël
Reply all
Reply to author
Forward
0 new messages