BZU CS 6340 - Introduction to LLVM

Unformatted text preview:

Lab 0 Introduction to LLVM Fall Semester 2023 Due 28 Aug 8 00 a m Eastern Time Objective This lab involves running and extending LLVM a popular compiler framework for a large class of programming languages that will be used to implement many labs in this course You will use the LLVM framework in the provided course VM and implement an LLVM pass that will perform simple analytics on an input program Specifically your pass will compute the number of functions instructions and signed division instructions in the input C program There is an LLVM primer under Syllabus Additional Resources on Canvas for more details on the LLVM framework Resources Some past students have requested that we produce a lesson specifically on LLVM Instead we are offering you several resources that come from people closely connected to the LLVM project Introduction to LLVM talk from 2019 LLVM Developers Meeting 1 01 19 Introduction to LLVM Program Analysis 1 21 07 Basic Overview of LLVM components 20 27 Setup The LLVM framework is pre installed on the course VM and the skeleton code for Lab 0 can be found in Canvas Navigate to Assignments on the left hand module click on Lab 0 and download the llvmintro zip file Transfer this file to the VM and unzip it in the VM home directory This will create a folder in the directory called llvmintro We will refer to this top level directory for Lab 0 simply as llvmintro when describing file locations for the lab Throughout the LLVM labs we will use CMake a modern tool for managing the build process If you are unfamiliar with CMake you are strongly advised to read the CMake tutorial first please pay attention to Step 1 and Step 2 in the tutorial link Running cmake produces a Makefile that you might be more familiar with If not read the Makefile tutorial before proceeding further Once a Makefile is generated you need only run make to rebuild your project after editing the source files The following commands set up the lab Copyright 2022 Georgia Institute of Technology All Rights Reserved 1 cd llvmintro mkdir build cd build cmake make Among the files generated you should now see PrereqPass so in the current directory PrereqPass so is a shared object file built from llvmintro src Prereqs cpp which is the source file you will modify in this lab and submit as a deliverable You will implement the functionality of this lab as an LLVM pass called PrereqPass LLVM passes are subprocesses of the LLVM framework They usually perform transformations optimizations or analyses on programs Each pass operates on an intermediate representation of the input program called the LLVM IR So to exercise this lab on an input C program we must first compile the program s source code to LLVM IR cd llvmintro test clang emit llvm S fno discard value names c simple0 c clang is a C language compiler that uses LLVM and serves as a drop in replacement for the gcc compiler If you know how to use gcc you should be fine with clang Otherwise scan the user manual to familiarize yourself with some of clang s command line options Briefly S instructs clang to perform preprocessing and compilation steps only emit llvm instructs the compiler to generate LLVM IR which will be saved to simple0 ll and fno discard value names preserves names of values in the generated LLVM to improve readability opt is a tool from LLVM that performs analyses and optimizations on LLVM IR We will use opt to run our custom LLVM pass on the compiled C code opt load llvmintro build PrereqPass so Prereqs disable output llvmintro test simple0 ll The option load loads our LLVM pass library while Prereqs instructs opt to run the pass on simple0 ll the documentation for opt to understand the potential ways to use the tool it may help you build and debug your solutions Libraries can and often do contain multiple LLVM passes Consult Copyright 2022 Georgia Institute of Technology All Rights Reserved 2 You should see the following output from the above command Analytics of Module llvmintro test simple0 ll 0 Functions Instructions 0 Signed Division Instructions 0 After implementing your pass logic in Prereqs cpp these numbers will reflect the actual counts of various properties of the C program under analysis for now the output only shows the default integer values provided to you as boilerplate Lab Instructions We have provided a pass skeleton for you to modify in particular you will need to edit the runOnModule function in the pass file llvmintro src Prereqs cpp so that it correctly prints the number of functions instructions and signed division instructions for an input C program compiled to LLVM IR LLVM IR Primer Consider the following example of an LLVM IR instruction This instruction performs the add operation on 32 bit integers i32 It adds the number in register 2 2 and the constant number 4 and places its result in register 1 1 Now consider a more complicated example We show the LLVM IR of the code highlighted in green in the right column I1 1 call i32 input I2 2 icmp sgt i32 1 0 I3 br i1 2 label 3 label 6 1 add i32 2 4 int x input if x 0 if x 0 int y x 1 else int y x 1 In I1 variable 1 refers to the integer value from the user input In I2 the return value of icmp integer comparison is stored to a temporary variable 2 Lastly in I3 the program execution jumps to either label 3 or 6 depending on the value of 2 Copyright 2022 Georgia Institute of Technology All Rights Reserved 3 The following figure shows the control flow graph of the full LLVM IR code for our preceding example In it we show the LLVM IR for each basic block You will learn how to utilize many classes in the LLVM API throughout the labs in this course Some of the more general ones that you will frequently use are Module Function BasicBlock in the habit of scanning some of the LLVM class and Instruction You should get documentation as each contains many useful methods that you might in implementing some of the labs We recommend following the LLVM coding standards for code names of variables types classes etc at the very least you should consult the relevant section but the standard serves as a good programming guideline find helpful think of returning to the task at hand Now the runOnModule function as the main entry point to your compiler pass Inside a Module you can find all program Functions In LLVM a function consists of one or more BasicBlocks that contain Instructions The hierarchy is summarized in the figure Traversing over these program elements is common when working

View Full Document

BZU CS 6340 - Introduction to LLVM

Documents in this Course
Load more
Download Introduction to LLVM
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...

Join to view Introduction to LLVM and access 3M+ class-specific study document.

We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Introduction to LLVM 2 2 and access 3M+ class-specific study document.


By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?