This assignment will help you get acquainted with the Low Level Virtual Machine (LLVM)
compiler. LLVM is a modular compiler framework and intermediate representation (IR) that uses
a static-single-assignment (SSA) form to represent programs at a level suitable for both
target-specific and target-agnostic program optimizations. Our work is all built on top of the
LLVM compiler framework, so it will be critical that you familiarize yourself with LLVM. In order to
learn the internals of LLVM, we have provided a list of useful resources below. Start with the
Getting Started guide and the Programmer’s manual, while also looking at the Writing an LLVM
Pass guide. Adrian Sampson’s LLVM tutorial linked below will also be useful for this assignment.
The other links are optional useful resources for you to learn more about LLVM:
● LLVM Getting Started: https://llvm.org/docs/GettingStarted.html
● LLVM Installation Guide: https://llvm.org/docs/CMake.html
● LLVM Programmers Manual: https://llvm.org/docs/ProgrammersManual.html
● Writing an LLVM Pass: https://llvm.org/docs/WritingAnLLVMPass.html
● Adrian Sampson’s LLVM Tutorial: https://www.cs.cornell.edu/~asampson/blog/llvm.html
● LLVM Loops: https://llvm.org/docs/LoopTerminology.html
● A list of LLVM’s built-in passes: https://llvm.org/docs/Passes.html
● LLVM Language Reference: https://llvm.org/docs/LangRef.html
● Another third-party tutorial: https://github.com/banach-space/llvm-tutor
Once you have gone over the tutorials and gotten yourself familiar with LLVM, your first
assignment will be to write an LLVM pass as described:
Our goal is to write an LLVM pass that inspects the body of a function (function pass) and prints
out all the different types of binary operations found (see the Language Reference for the
definition of binary operations), and the number of occurrences of each (e.g. 4 mults, 3 adds, 2
subtracts, …). Make sure you write at least 3 appropriate test cases to test your pass (refer to
the guides for more information).
Bonus: As an extra challenge, identify the operations that are located inside loops, and if the
loop bounds are constant, provide the dynamic count of those operations. (i.e. the number of
operations taking into account all the loop iterations). Note that this should be implemented as a
modification to the original pass, not a new one. Use comments to mark your code appropriately
to identify where the bonus code begins/ends. Provide one additional test case for the bonus.
Bonus 2: As an additional challenge, modify your pass to be a transformation that, after
counting the binary ops and displaying them, goes through and replaces all additions with
subtractions. Note that the actual application code will no longer be functionally equivalent to
your input application; so this can only be tested by inspecting the generated bitcode.
The deadline for this assignment is Sunday March 13, 11:59 PM. Please make sure to upload
your assignment to the box folder that I shared with you. You are required to upload the new
pass as well as any test cases you created along with the Makefiles you used to invoke your
pass on the test cases. Make sure you upload the pass folder as a whole, not just the C++
source file. Please DO NOT upload the entire LLVM source tree!
● The purpose of this assignment is to test your skills and ability to learn. As such, please
refrain from sending questions unless you are truly stuck on something and can’t figure it
out. Questions about clarity of the assignment are allowed.
● For the same reason as above, please refrain from working on the assignment together.
However, I will leave it to your discretion if you want to help each other out when you get
stuck on something.
● Make sure you document your code and thought process. Even if you are not successful
in getting the pass to work within the given time limit, our goal is to judge your problem
solving skills, so make sure you submit your assignment anyway!
● Regarding code organization, note that the LLVM’s tutorial on writing a pass uses an
in-tree approach, whereas Adiran Sampson’s approach uses an out-of-tree approach.
Both are acceptable solutions, so feel free to choose whichever you prefer.