How to write an LLVM passport

LLVM is a free software project, a compiler infrastructure written in C ++. Its development arose from studying Vikram Adve from the University of Illinois at Urbana-Champaign (UIUC) and his first PhD student Chris Lattner in 2000. At the time they were you want to create dynamic compilation technology for all static and dynamic languages.

The name LLVM was originally derived from the acronym Low Level Virtual Machine, but now the scope of this project has far exceeded its original meaning. Currently, LLVM has evolved into a "Collection of Modular and Reusable Compiler and Toolchain Technologies" ("Collection of Modular and Reusable Compiler and Toolchain Technologies") used for front-to-back development of the compiler.

In 2005, Apple Computer hired Chris Ratner and his team to develop application systems for Apple computers. LLVM is part of the current Mac OS X and iOS development tools. The Xcode development environment uses LLVM as the kernel. Because of LLVM's contribution to the industry, ACM recognized Adve, Lattner and Evan Cheng with the 2012 ACM Software System Award.

----------------------------------------------------------------------------------------------------------------------------------------

In the previous article we demonstrated the basic method of writing (front-end) pass in LLVM through a simple Hello World program. This article describes how to iterate over a function by writing PassBasic Blocks methods.

First of all, we need to know that in LLVM, the basic unit of a program is a module (module). (The ModulePass class is the most general of all superclasses you can use. If you derive from ModulePass, your pass uses the entire program as a unit.)


The function is the basic unit of the module, i.e. aModuleConsists of one or more functions.Basic BlockIs the basic unit of a function, i.e. a function of one or moreBasic BlockComposition. Instructions (instructions) JaBasic BlockIs the basic unit, so aBasic Block of one or more InstructionsComposition.

Same as before, suppose your LLVM installation directory is ... ... / llvm, then you first ... ... / llvm create a subfolder in / lib / Transforms. For example, the name is IterInsideBB.


Then create the following three files in this folder: CMakeLists.txt, IterInsideBB.exports, IterInsideBB.cpp. As an example of a passport in LLVM, another pre-made passport is provided; H. InTransforms In the Hello folder you can enter three names: CMakeLists.txt 、 Hello.exports 、 Hello.cpp files copied to IterInsideBB folder and change the appropriate file name.

Then change the top directory (ieTransforms), add the following to the end of CMakeLists.txt: add_subdirectory (IterInsideBB). Then change ... ... / llvm / lib / Transforms / IterInsideBB MitteCMakeLists.txt. Note that this file is from ... ... / llvm / lib / Transforms / Hello Copied from to. You'll need to set up a build script that will compile the source code for the new pass. Specifically, you need to set up a build script that will show 3 times Hello, Changed to IterInsideBB, the contents of the file after the change is as follows:

Next, you need to write the specific content of the passport file. This pass file is actually a CPP file. For the current example we just need to edit IterInsideBB the contents of .cpp. The contents of the file are as follows:

The next thing to do is re-bulid LLVM. Enter ... ... / llvm In the / build folder, use make directly. The entire process takes a few minutes. Then try the pass written above, you need to create a new test file in your desired location (ex: desktop). For example, the contents of a file named test.c are as follows:


Then use the following command on the command line to compile and generate the .ll file :

clang test.c -O0 -S -emit-llvm -o test.ll

Then take our passport. The specific method is to run the following command on the command line:

opt -load /Users/fzuo/llvm/build/lib/LLVMIterInsideBB.dylib -IterInsideBB test.ll

Then you will get the following output:


The above output iterates through the two functions add and main in the source code and then iterates through all of the basic blocks contained therein. The output of the BasicBlock size is always Basic BlockThe number of commands in.

【List of articles in this series】

  • A detailed tutorial on writing passport in LLVM (1): a Hello World Pass
  • A detailed tutorial on writing passport in LLVM (2): Going through basic blocks in a function
  • A detailed tutorial on writing passport in LLVM (3)
  • A detailed tutorial on how to write the backend pass in LLVM (1)
  • A detailed tutorial on how to write the backend pass in LLVM (2)

(End of this article)