Registering a pass with pass manager

Until now, a new pass was a dynamic object that was run independently. The opt tool consists of a pipeline of such passes that are registered with the pass manager, and a part of LLVM. Let's see how to register our pass with the Pass Manager.

Getting ready

The PassManager class takes a list of passes, ensures that their prerequisites are set up correctly, and then schedules the passes to run efficiently. The Pass Manager does two main tasks to try to reduce the execution time of a series of passes:

  • Shares the analysis results to avoid recomputing analysis results as much as possible
  • Pipelines the execution of passes to the program to get better cache and memory usage behavior out of a series of passes by pipelining the passes together

How to do it…

Follow the given steps to register a pass with Pass Manager:

  1. Define a DEBUG_TYPE macro, specifying the debugging name in the FuncBlockCount.cpp file:
    #define DEBUG_TYPE "func-block-count"
  2. In the FuncBlockCount struct, specify the getAnalysisUsage syntax as follows:
    void getAnalysisUsage(AnalysisUsage &AU) const override {
        AU.addRequired<LoopInfoWrapperPass>();
      }
  3. Now initialize the macros for initialization of the new pass:
    INITIALIZE_PASS_BEGIN(FuncBlockCount, " funcblockcount ",
                         "Function Block Count", false, false)
    INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
    
    INITIALIZE_PASS_END(FuncBlockCount, "funcblockcount",
                       "Function Block Count", false, false)
    
    Pass *llvm::createFuncBlockCountPass() { return new FuncBlockCount(); }
  4. Add the createFuncBlockCount Pass function in the LinkAllPasses.h file, located at include/llvm/:
    (void) llvm:: createFuncBlockCountPass ();
    
  5. Add the declaration to the Scalar.h file, located at include/llvm/Transforms:
    Pass * createFuncBlockCountPass ();
    
  6. Also modify the constructor of the pass:
    FuncBlockCount() : FunctionPass(ID) {initializeFuncBlockCount Pass (*PassRegistry::getPassRegistry());}
  7. In the Scalar.cpp file, located at lib/Transforms/Scalar/, add the initialization pass entry:
    initializeFuncBlockCountPass (Registry);
  8. Add this initialization declaration to the InitializePasses.h file, which is located at include/llvm/:
    void initializeFuncBlockCountPass (Registry);
  9. Finally, add the FuncBlockCount.cpp filename to the CMakeLists.txt file, located at lib/Transforms/Scalar/:
    FuncBlockCount.cpp

How it works…

Compile the LLVM with the cmake command as specified in Chapter 1, LLVM Design and Use. The Pass Manager will include this pass in the pass pipeline of the opt command-line tool. Also, this pass can be run in isolation from the command line:

$ opt –funcblockcount sample.ll

See Also

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.40.189