We are going to create Module Pass. In the same way it is
possible to create Function, BasicBlock, etc. Instead of Module use Function,
BasiBlock, etc
Step 1: Got to the llvm/lib/Transformas/MyLLVMPasses directory.
cd llvm/lib/Transformas/MyLLVMPasses
|
Step 2: Create NewLlvmPass.cpp file (you can give any name) in
MyLLVMPasses directory and
write there this code
#include "llvm/Transforms/MyLLVMPasses.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Transforms/Scalar.h"
#include "llvm/Pass.h"
using namespace llvm;
namespace {
class NewLlvmPass : public ModulePass {
public:
static char ID;
NewLlvmPass() : ModulePass(ID) {
initializeNewLlvmPassPass(*PassRegistry::getPassRegistry());
}
bool runOnModule(Module &M);
};
} // end namespace
char NewLlvmPass::ID = 0;
INITIALIZE_PASS_BEGIN(NewLlvmPass,
"myoption", "New LLVM Pass", false, false)
INITIALIZE_PASS_END(NewLlvmPass,
"myoption", "New LLVM Pass", false, false)
ModulePass
*llvm::createNewLlvmPassPass() {
return new NewLlvmPass();
}
// runOnModule(Module
&M) is like main()
bool NewLlvmPass::runOnModule(Module &M) {
bool Changed = false;
errs() << "New LLVM Pass" << "\n";
return Changed;
}
|
Step 3: Open llvm/include/llvm/Transforms/MyLLVMPasses.h file. After class decelerations add this code
ModulePass
*createNewLlvmPassPass();
|
Step 4: Open llvm/include/llvm/LinkAllPasses.h file. Add this code.
(void) llvm::createNewLlvmPassPass();
|
Step 5: Open llvm/include/llvm/InitializePasses.h file and in end of file add this code
void initializeNewLlvmPassPass(PassRegistry&);
|
Step 6: Open llvm/include/llvm-c/Transforms/MyLLVMPasses.h file and add this line
void LLVMAddNewLlvmPassPass(LLVMPassManagerRef PM);
see below
extern "C" {
#endif
/** See
llvm::createNewLlvmPassPass function. */
void LLVMAddNewLlvmPassPass(LLVMPassManagerRef PM);
#ifdef __cplusplus
}
|
Step 7: Open llvm/lib/Transforms/MyLLVMPasses/CmakeLists.txt file. Add in file pass' file name.
add_llvm_library(LLVMMyLLVMPasses
NewLlvmPass.cpp
)
add_dependencies(LLVMMyLLVMPasses
intrinsics_gen)
|
Step 8: Open llvm/lib/Transforms/MyLLVMPasses/MyLLVMPasses.cpp file. Add lines which are highlighted.
//
initializeMyLLVMPasses - Initialize all passes linked into the
// MyLLVMPasses
library.
void llvm::initializeMyLLVMPasses(PassRegistry &Registry) {
initializeNewLlvmPassPass(Registry);
}
void LLVMInitializeMyLLVMPasses(LLVMPassRegistryRef R) {
initializeMyLLVMPasses(*unwrap(R));
}
void LLVMAddNewLlvmPassPass(LLVMPassManagerRef PM) {
unwrap(PM)->add(createNewLlvmPassPass());
}
|
Step 9: Open file llvm/tools/clang/lib/Frontend/CompilerInvocation.cpp
file. Add line
Opts.NewLlvmPass =
Args.hasArg(OPT_enable_myoption);
in function ParseCodeGenArgs,
after those lines (see below).
Opts.VectorizeBB =
Args.hasArg(OPT_vectorize_slp_aggressive);
Opts.VectorizeLoop =
Args.hasArg(OPT_vectorize_loops);
Opts.VectorizeSLP =
Args.hasArg(OPT_vectorize_slp);
Opts.NewLlvmPass =
Args.hasArg(OPT_enable_myoption);
|
Step 10: Open llvm/tools/clang/lib/CodeGen/BackendUtil.cpp file. In end of
EmitAssemblyHelper::CreatePasses() functio add this code
if (CodeGenOpts.NewLlvmPass) {
MPM->add(createNewLlvmPassPass());
}
|
Step 11: Open llvm/tools/clang/include/clang/Frontend/CodeGenOptions.def file. Add this line
CODEGENOPT(NewLlvmPass
, 1, 0) ///< Run My New LLVM
pass
you can add it after Vectorize, like this
CODEGENOPT(VectorizeSLP ,
1, 0) /// < Run SLP vectorizer.
CODEGENOPT(NewLlvmPass , 1, 0)
/// < Run My New LLVM pass
|
Now we must add our Clang option, which we named "-myoption"
Step 12: Open llvm/tools/clang/lib/Driver/Tools.cpp file and
in Clang::ConstructJob function add this code
if (Args.hasArg(options::OPT_enable_myoption))
CmdArgs.push_back("-myoption");
|
Step 13: Open llvm/tools/clang/include/clang/Driver/Options.td file. Add these lines
def enable_myoption :
Flag<["-"], "myoption">, Flags<[CC1Option]>,
HelpText<"New LLVM Pass">;
|
you can add it before // standard options comment.
Step 14: Now go to check LLVM pass with Clang option.
make
make install
|
Step 15: Run (path_to_install_dir is absolute path where installed clang/clang++ )
path_to_install_dir/clang++
-mayoption test.cpp
|
Output during compilation.
New LLVM Pass
|