Add custom pragma to clang

Hi all,
I’m trying to add a custom pragma to the my latest llvm version (18) for my project. What I need is just to mark a loop with my pragma and then add some metadata such that I can use my custom llvm pass only on the marked loop.

e.g

for(...){
  #pragma my_pragma
  for(int i = 0; i < 10; i++){
  //some code
  }
  for(...){
  }
}

Up to this point, I’ve implemented a pragma handler within Lex/Pragma.cpp and made modifications to the ForStmt class in AST/Stmt.h and CodeGen/CGStmt.cpp.

I added a new boolean variable to the ForStmt class. My plan is to change its value in the pragma handler I made. Then, in CGStmt.cpp, I’ll check that flag, and if it’s set to true, I’ll go ahead and add some metadata.

Everything seems reasonable to me, but I want to be sure I’m on the right track.
So far am I approaching the problem on the right way?

Also at this point I’m a little bit lost, since I’m not sure on what to do in the handler to mark change the variable and in the CGStmt to add the metadata.

While writing the question I’ve also notice that maybe my idea of changing the value of the variable in the “ForStmt” class is not correct, since if I parse and generate the ForStmt in the pragma handler the changes are not propagated to the CGStmt.cpp file.

Any hint on what I should do?

1 Like