5

I want to know which all llvm IR statements correspond to the code inside a particular pragma in clang. My pragma is having the following structure.

#pragma markme
{
   stmt1;
   stmt2;
}

I need to know which all stmts were present between the opening braces and closing braces of mark me pragma.

Can we attach some metadata to these stmts? If yes could anyone point me to some reference.

I have searched on Google and found this

Add a pragma handler, which has a callback on the actions interface. Add a sema implementation of the callback, which sets some internal bit in the Sema object. Add a new bit to the 'for' statement, to specify whether this it had #pragma optimize set. Modify codegin to emit the metadata based on that bit.

Could any one give more details on this.

I am using the latest llvm (llvm 3.4)

Note: Any help in any direction is appreciated. I know llvm may do optimizations that moves the statements around. But this is fine with me

simpleuser
  • 479
  • 5
  • 16
  • also it would be helpful if someone can tell where I should do this step "Add a new bit to the 'for' statement, to specify whether this it had #pragma optimize set" – simpleuser Jul 24 '13 at 13:02
  • I have looked at the existing code and modified some code. The problem is that its compiling properly, but still I cant find metadata attached – simpleuser Jul 25 '13 at 09:32

1 Answers1

7

Note that this has to be done in Clang, which knows about #pragma. LLVM itself knows nothing about them - #pragma are not part of the LLVM IR.

There are plenty of examples of generating metadata in Clang's lib/CodeGen directory. It all depends on where you want this metadata to appear - on instructions? On functions?

For attaching metadata to instructions look for setMetadata. For example, in lib/CodeGen/CGExpr.cpp, some profiling metadata is attached to branches. For placing module-level metadata, see lib/CodeGen/CodeGenModule.cpp.

Eli Bendersky
  • 263,248
  • 89
  • 350
  • 412
  • Thank you for the reply, I have checked this. I will recheck this once more to make sure I have not missed anything. – simpleuser Jul 24 '13 at 13:24
  • I have checked this. Actually, I have written something in CGStmt.cpp. May be I am missing something. It is all compiling properly. But when I use clang to generate the *.ll file, I can see the meta data. If you dont mind can you start a chat. It would really help me. The function which I have modified in that file is CodeGenFunction::EmitCompoundStmt – simpleuser Jul 24 '13 at 13:28
  • In the ll file generated I cannot see any metadata (generated by clang -S -emit-llvm) – simpleuser Jul 24 '13 at 13:35