[llvm-dev] [Help] Add custom pragma

Sunghyun Park via llvm-dev llvm-dev at lists.llvm.org
Tue Oct 25 14:54:14 PDT 2016


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:
    ~
  case AttributeList::AT_FallThrough:
    return ~
  case AttributeList::AT_LoopHint:
    return handleLoopHintAttr( ~ )
  case AttributeList::AT_OpenCLUnrollHint:
  ~
  ...
}

As I want to define new attribute, I've tried to find where the definitions
for AT_LoopHint, AT_FallThrough are done. This is what I found.

class AttributeList @ tools/clang/include/clang/Sema/AttributeList.h

enum Kind{
   #define PARSED ATTR(NAME) AT_##NAME,
   #include "clang/Sema/AttrParsedAttrList.inc"
   #undef PARSED_ATTR
   IgnoredAttribute,
   UnkownAttribute
};

It looks like those are defined at this enum, but there's no such file,
"clang/Sema/AttrParsedAttrList.inc".

Anyone knows how those definitions are defined and how the kind of
attributes are decided?
Thank you for your valuable time!

-- 
Best, Sung
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20161025/cdea8c9c/attachment.html>


More information about the llvm-dev mailing list