Skip to content

SRO-SA/HelloPass-LLVM

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 

Repository files navigation

HelloPass-LLVM

Getting started for LLVM pass writing

Setup and Run

  1. Clone this repo, see the file details below:
  1. For Mac OSX users, uncomment the following line in Pass/Transforms/ValueNumbering/CMakeLists.txt
SET(CMAKE_MODULE_LINKER_FLAGS "-undefined dynamic_lookup")
  1. Move to Pass/build/ directory using cd command on your local system. Next, execute the following command. If it executes successfully, proceed to next step.
cmake -DCMAKE_BUILD_TYPE=Release ../Transforms/ValueNumbering
  1. Next execute make and it will generate *.so files under build directory.
make -j4
  1. Move to test/ directory and generate test.ll file for test.c using following command.
clang -S -fno-discard-value-names -emit-llvm test.c -o test.ll
  1. After generating test.ll, run the following command to test the LLVM Pass.
opt -load-pass-plugin ../Pass/build/libLLVMValueNumberingPass.so  -passes=value-numbering test.ll
  1. If you want to see debug information, use -debug-pass-manager flag.
opt -load-pass-plugin ../Pass/build/libLLVMValueNumberingPass.so  -passes=value-numbering test.ll -debug-pass-manager

Code Explanation

  • The implemented Pass extends from FunctionPass class and overrides run(Function &F, FunctionAnalysisManager &) function.
  • run(Function &F, FunctionAnalysisManager &) function gets called for each function in the test code. Name of the function being analyzed is accessible using the following code snippet.
PreservedAnalyses run(Function &F, FunctionAnalysisManager &) {
	F.getName();
}
  • To print out to the screen, you need to redirect strings to errs(), as in:
PreservedAnalyses run(Function &F, FunctionAnalysisManager &) {
	errs() << "function name: " << F.getName() << "\n";
}
  • We can iterate over basic blocks of the given function as:
PreservedAnalyses run(Function &F, FunctionAnalysisManager &) {
	for (auto& basic_block : F)
	{
		...
	}
}
  • Next, we can iterate over the instructions in a basic block (BB). Note: instructions are in LLVM IR.
PreservedAnalyses run(Function &F, FunctionAnalysisManager &) {
    for (auto& basic_block : F)
    {
        for (auto& inst : basic_block)
        {
            ...
        }
    }
}
  • Once we get an instruction, then we can cast it as User and iterate over operands of that instruction.
auto* ptr = dyn_cast<User>(&inst);
for (auto it = ptr->op_begin(); it != ptr->op_end(); ++it) 
{
    ...
}
  • You can also access the operands using getOperand(operand_index) as in:
for (auto& inst : basic_block)
{
    ...
    errs() << "operand: " << inst.getOperand(0) << "\n";
    ...
}
  • You can check whether instruction is a binary operation (like a = b + c) with isBinaryOp()
if (inst.isBinaryOp())
{
    ...
}
  • You can find operator types with getOpcode() and predefined opcodes
if (inst.isBinaryOp())
{
    inst.getOpcodeName(); //prints OpCode by name such as add, mul etc.
    if(inst.getOpcode() == Instruction::Add)
    {
        errs() << "This is Addition"<<"\n";
    }
    if(inst.getOpcode() == Instruction::Mul)
    {
        errs() << "This is Multiplication"<<"\n";
    }
    // See Other classes Instruction::Sub, Instruction::UDiv, Instruction::SDiv
}
  • A sample implementation of run(Function &F, FunctionAnalysisManager &):
string func_name = "test";
PreservedAnalyses run(Function &F, FunctionAnalysisManager &) {

    errs() << "ValueNumbering: ";
    errs() << F.getName() << "\n";
    if (F.getName() != func_name) 
        return PreservedAnalyses::all();
	
    for (auto& basic_block : F)
    {
        for (auto& inst : basic_block)
        {
            errs() << inst << "\n";
            auto* ptr = dyn_cast<User>(&inst);
            for (auto it = ptr->op_begin(); it != ptr->op_end(); ++it) 
            {
                errs() << "\t" << *(*it) << "\n";
            }
        }
    }
    return PreservedAnalyses::all();
}

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages