Integrating LLVM passes

2019-03-21 10:50发布

问题:

This maybe a rookie question but is there a way to integrate my LLVM modulepass to be called by default during the transformation phase?

Right now I am using this syntax to load my pass and register it

 ~/llvm/llvm/build/Debug+Asserts/bin/clang -Xclang -load -Xclang ~/llvm/llvm/build/Debug+Asserts/lib/SOMEPASSLIB.so

(The problem is when I want to build some package with this pass, the compiler accepts it when I say, pass the loading part as CFLAGS env variable, but some makefiles use CFLAGS for linking too, and the linker has no idea what it can do with this information and fails the build :\ )

回答1:

There are couple of files you need to modify in order to define your pass inside LLVM core:

i) inside your pass: loadable pass is registered like this (assuming your pass name is FunctionInfo):

char FunctionInfo::ID = 0;
RegisterPass<FunctionInfo> X("function-info", "Functions Information"); 

you need to change it to be like this:

char FunctionInfo::ID = 0;
INITIALIZE_PASS_BEGIN(FunctionInfo, "function-info", "Gathering Function info",  false, false)
INITIALIZE_PASS_DEPENDENCY(DominatorTree)
INITIALIZE_PASS_DEPENDENCY(LoopInfo)
.... // initialize all passes which your pass needs
INITIALIZE_PASS_END(FunctionInfo, "function-info", "gathering function info", false, false)

ModulePass *llvm::createFunctionInfoPass() { return new FunctionInfo(); }

ii) you need to register your pass inside llvm as well, at least in InitializePasses.h and LinkAllPasses.h. in LinkAllPasses.h you should add :

(void)llvm::createFunctionInfoPass();

and in InitializePasses.h add :

void initializeFunctionInfoPass(PassRegistry &);

iii) beside this modifications you might need to change another file depend on where you are going to add your pass. for instance if you are going to add it in lib/Analysis/ you also need to add one line to Analysis.cpp as below :

 initializeFunctionInfoPass(Registry);

or if you are going to add it as new Scalar Transform you need to modify both Scalar.h and Scalar.cpp likewise.