Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integrating LLVM passes

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 :\ )

like image 454
Abhishek Vasisht Avatar asked Apr 28 '15 03:04

Abhishek Vasisht


People also ask

What are LLVM passes?

All LLVM passes are subclasses of the Pass class, which implement functionality by overriding virtual methods inherited from Pass .

What is LLVM optimization?

LLVM divides the entire compilation process into three steps: Frontend: Convert the high-level language to IR. Middle-End: Perform optimization in the IR layer. Backend: Convert the IR into the assembly language of the corresponding hardware platform.

What is LLVM IR code?

LLVM IR is a low-level intermediate representation used by the LLVM compiler framework. You can think of LLVM IR as a platform-independent assembly language with an infinite number of function local registers.


1 Answers

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.

like image 62
hadi sadeghi Avatar answered Sep 19 '22 19:09

hadi sadeghi