CS380C: Assignment 4

Loop analysis

 

 

Project description

The goal of this assignment is to familiarize you with the LLVM compiler by implementing a simple LLVM pass to analyze loops in programs.

Properties of loops

A compiler analyzes and optimizes programs in passes; each pass scans or traverses the intermediate code to analyze or transform it. Analysis passes are typically used to collect information about a program that helps in ensuring that an optimization pass is safe (not alter the meaning of the program) or determining whether an optimization pass could be beneficial. In this assignment, you will implement an analysis pass to learn interesting properties about the loops in a program. Your pass should report the following information about all loops that are present in a program:

  1. Function: name of the function containing this loop.
  2. Loop depth: 0 if it is not nested in any loop; otherwise, it is 1 more than that of the loop in which it is nested in.
  3. Contains nested loops: determine whether it has any loop nested within it.
  4. The number of top-level basic blocks: count all basic blocks in it but not in any of its nested loops.
  5. The number of instructions: count all instructions in it, including those in its nested loops.
  6. The number of atomic operations: count atomic instructions in it, including those in its nested loops.
  7. The number of top-level branch instructions: count branch instructions in it but not in any of its nested loops. In particular, you need to count the following instructions: llvm::BranchInst, llvm::IndirectBrInst, llvm::SwitchInst.

For each loop, print a line to standard error in the following format (case and space sensitive):

<ID>: func=<str>, depth=<no>, subLoops=<str>, BBs=<no>, instrs=<no>, atomics=<no>, branches=<no>

<ID> represents a global counter for each loop encountered, starting at 0. <str> represents a string for function name or boolean value - “true” or “false”. <no> represents a number.

Getting Started with LLVM

Getting started with LLVM

Please go through the above guide thoroughly. It has all the details to setup LLVM and write/run your own LLVM passes.

Test cases

You are responsible for writing test cases to test your pass. Write test cases such that you can test various aspects of your pass; start with simple ones and then add more complex ones. Feel free to use LLVM test programs. We can use any program for testing your pass. Your program must be robust (no crashes or undefined behavior) and precise (correct output). So, please test your code thoroughly.

Implementation guidelines

IMPORTANT: DO NOT READ ~/llvm/llvm/lib/Transforms/Scalar/LICM.cpp
You are not allowed to copy source code from anywhere, including other LLVM source files. You can include the header files and call those functions if you think it does precisely what you want. If you are not sure whether you can use something or if you think some LLVM source code is making the assignment trivial, then please let us know on Piazza (you can use a private note).

Evaluation

The following is performed during evaluation:

  1. Unzip the archive you submit into the ~/llvm/llvm/lib/Transforms folder, which will contain a CMakeLists.txt that adds your directory as a sub-directory.
  2. Run make inside your sub-directory in the ~/llvm/llvm/lib/Transforms folder.
  3. Use opt as mentioned in the LLVM guide to test your pass on a few input programs. Your program should not crash.
  4. Compare (diff) the output to standard error (stderr) against the expected output. It should match. Note that this will be done through scripts. So, please follow the LLVM guide precisely.

Code quality

Please note that 10 points out of 100 points are assigned for code quality. Code quality will include the following: 1. making sure that your submission conforms to the submission guidelines mentioned below, 2. proper code formatting to make your code readable, and 3. proper code documentation to ensure ease of navigation. Ensuring good code quality is always appreciated throughout the software industry. You can use this opportunity to learn more about the good coding practices and incorporating them into your submission.

Submission guidelines

  1. llvmpass.zip: LLVM pass directory containing ONLY your pass should be uploaded. For instance, ~/llvm/llvm/lib/Transforms/SG12345 directory. The name of this directory should be your EID in the upper case. Please ensure that the naming is correct. Do not just rename the directory. The name of the pass is used in multiple places including CMakeLists and inside source files. Refer to the LLVM guide to know the exact places where renaming is necessary.
  2. README.md: The README file should mention all the materials that you have read or used for this assignment, including LLVM documentation and source files.