[Help] Add custom pragma

Hi, all.

I want to give programmer ability to tell LLVM that certain region of code is expected to get specialized optimization.

So, I’m trying to make custom pragma to mark certain region of code and pass this information to LLVM, in the similar way that ‘#pragma clang loop unroll_count(N)’ works.

By tracking the framework of loop unroll pragma, I found out it works in the way below.

(1) Detect pragma at lexer, parser.

(2) Create AttributeList and push it into AST.

(3) Once AST is built, consume AST and generate LLVM IR at CodeGeneration

(4) If attribute for loop unroll is found, put tag on LLVM IR with Meta data which indicates whether to unroll loop.

Now I can detect my own pragma and figured it out how to create and tag custom metadata.

The problem is that creating custom AttributeList is somewhat hard to follow.

As far as I know, the attribute for loop unroll pragma gets serviced at ProcessSmtAttrbute Function @ tools/clang/lib/Sema/SemaStmtAttr.cpp.

It contains switch statement like below.

ProcessSmtAttrbute @ tools/clang/lib/Sema/SemaStmtAttr.cpp

// A : AttributeList - passed by argument

switch( A.getKind() ) {

case AttributeList::UnknownAttribute:

That file is generated by the clang-tblgen tool:

  COMMAND = cd /Users/vk/Desktop/llvm/DA-NoModules/tools/clang/include/clang/Sema && /Users/vk/Desktop/llvm/DA-NoModules/bin/clang-tblgen -gen-clang-attr-parsed-attr-list -I /Users/vk/Desktop/llvm/tools/clang/include/clang/Sema/../../ -I /Users/vk/Desktop/llvm/tools/clang/include/clang/Sema -I /Users/vk/Desktop/llvm/lib/Target -I /Users/vk/Desktop/llvm/include /Users/vk/Desktop/llvm/tools/clang/include/clang/Sema/../Basic/Attr.td -o /Users/vk/Desktop/llvm/DA-NoModules/tools/clang/include/clang/Sema/AttrParsedAttrList.inc.tmp

Try looking include/clang/Basic/Attr.td?

best,
vedant

Yes. I checked that file, but I wasn’t sure whether that is right one to look at.
What is *.td extension btw? When I google it, it says this is for Windows Debug Simulator.
Why clang use this extension, which is limited to Windows environment?

Yes. I checked that file, but I wasn't sure whether that is right one to look at.

It seems promising. I imagine you'd want something like the LoopHint attr.

What is *.td extension btw?

The llvm project uses that extension to identify TableGen inputs
(http://llvm.org/docs/TableGen/).

vedant

Yes. Exactly.
So, I add my definition for custom attribute in ‘include/clang/Basic/Attr.td’, something like below.

def MyHint : Attr {
let Spellings = ~
let Args = ~
let AdditionalMembers = [{
void printPrettyPragma( ~ )
}];
let Documentation = [Undocumented];
}

This is basically similar with “def LoopHint : Attr”, which is attribute of loop unroll.

But somehow it seems it doesn’t work properly. It can not emit ‘MyHintAttr’ class as well as its elements like ‘MyHintAttr::Spelling’, ‘MyHintAttr::CreateImplicit(~)’.
My guess is there should be more things to do other than adding definition on Attr.td, so I’m trying to follow how those of LoopHIntAttr are emitted.
Is there any reference, or advice where to look at? Instruction on clang is somewhat simpler than my need.
It’s my first time to hack this large program, so I’m stalling for hours at same status…

Thanks, in advance!