LUND UNIVERSITY
Department of Computer Science

LLVM Project, EDAN75 Optimizing Compilers, 2023

The purpose of this project is to learn how to make a simple optimization in LLVM.
  1. Find a UNIX machine and either (1) make sure you have write permission to /opt, or (2) change from /opt to something else in the install script in tresorit (llvm.sh). It may take several hours.

    When extending LLVM, it is essential not to have to type just make in the build directory since it will be far too time consuming. Instead of making your code part of clang directly, you will create a shared library which can be loaded when running the optimizer for testing.

  2. Save the following source code in a.c in the build directory:
    int f(int a, int b)
    {
            int     c;
            int     d;
    
    	c = a + b;
    	d = a + b;
    
            return c * d;
    }
    
    and produce a file with textual LLVM IR:

    bin/clang -emit-llvm -S a.c

    Study the produced a.ll to see that you have a rough idea of its contents.

  3. Now open the file src/lib/Transforms/Hello/Hello.cpp

    The runOnFunction method is called for each function and it returns true if the function was modified.

    Save the file to trigger recompilation and in the build directory, type:

    make -f lib/Transforms/Hello/Makefile

    You should get a shared library file lib/LLVMHello.so

    Use this file by typing:

    bin/opt -load lib/LLVMHello.so -hello a.ll -o b.ll

  4. At the last line of Hello.cpp the pass is registered. The first string "hello" on line 40 is the name of the command line switch and the second string is output when typing help. Type:

    bin/opt -load lib/LLVMHello.so -help | grep Hello

    You should see the second string.

  5. LLVM tries to find the most likely switch at a typo. Try:

    bin/opt -load lib/LLVMHello.so -Hello a.ll -o b.ll

  6. Make a new directory EDAN75 by copying Hello in src/lib/Transforms directory.

    Edit all files and replace Hello with EDAN75, but replace LLVMHello with only EDAN75!

  7. Add EDAN75 to the file src/lib/Transforms/CMakeLists.txt

  8. Run cmake in build as:

    cmake ../src

  9. You will not rebuild all of LLVM but only your new file EDAN75.so. Again in the build directory, type:

    make -f lib/Transforms/EDAN75/Makefile

    When you have implemented your optimization and want use it, type:

    bin/opt -load lib/EDAN75.so -EDAN75 a.ll -S

    This will print the LLVM code on stdout (or save it in a file such as above with -o b.ll)

  10. Your task is to implement an optimization which removes redundant instructions in each basic block. It only needs to work for a.c.

    To do this optimization, you need to iterate through each basic block of the function (there is only one basic block in this input). The easiest way to figure out how to do various things in LLVM is to look for a file which does something similar. The most interesting directory for inspiration files is src/lib/Transforms/Scalar.

    Go there and type:

    grep F.begin *.cpp

    to get started.


Fri Feb 24 11:20:12 AM CET 2023