Running your own pass with the opt tool

The pass written in the previous recipe, Writing your own LLVM pass, is ready to be run on the LLVM IR. This pass needs to be loaded dynamically for the opt tool to recognize and execute it.

How to do it…

Do the following steps:

  1. Write the C test code in the sample.c file, which we will convert into an .ll file in the next step:
    $ vi sample.c
    
    int foo(int n, int m) {
      int sum = 0;
      int c0;
      for (c0 = n; c0 > 0; c0--) {
        int c1 = m;
        for (; c1 > 0; c1--) {
          sum += c0 > c1 ? 1 : 0;
        }
      }
      return sum;
    }
  2. Convert the C test code into LLVM IR using the following command:
    $ clang –O0 –S –emit-llvm sample.c –o sample.ll
    

    This will generate a sample.ll file.

  3. Run the new pass with the opt tool, as follows:
    $ opt  -load (path_to_.so_file)/FuncBlockCount.so  -funcblockcount sample.ll

    The output will look something like this:

    Function foo
    

How it works…

As seen in the preceding code, the shared object loads dynamically into the opt command-line tool and runs the pass. It goes over the function and displays its name. It does not modify the IR. Further enhancement in the new pass is demonstrated in the next recipe.

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.139.97.40