Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying LLVM Cookbook
  • Table Of Contents Toc
LLVM Cookbook

LLVM Cookbook

By : Mayur Pandey, Suyog Sarda
2 (9)
close
close
LLVM Cookbook

LLVM Cookbook

2 (9)
By: Mayur Pandey, Suyog Sarda

Overview of this book

The book is for compiler programmers who are familiar with concepts of compilers and want to indulge in understanding, exploring, and using LLVM infrastructure in a meaningful way in their work. This book is also for programmers who are not directly involved in compiler projects but are often involved in development phases where they write thousands of lines of code. With knowledge of how compilers work, they will be able to code in an optimal way and improve performance with clean code.
Table of Contents (11 chapters)
close
close
10
Index

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

CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
LLVM Cookbook
notes
bookmark Notes and Bookmarks search Search in title playlist Add to playlist font-size Font size

Change the font size

margin-width Margin width

Change margin width

day-mode Day/Sepia/Night Modes

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY

Submit Your Feedback

Modal Close icon
Modal Close icon
Modal Close icon