Build LLVM with CMake

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.

Using the pre-built distribution

In most cases, we can just directly download and use the pre-compiled version of LLVM and Clang from llvm-project/releases . For example, clang+llvm-16.0.0-x86_64-linux-gnu-ubuntu-18.04.tar.xz is a pre-built distribution for Ubuntu 18.04 and for the x86-64 platforms.

Build LLVM from source

In cases where there’s no pre-built version that suffice our needs, we can download and build LLVM from scratch. For example, openai/triton Python library requires a LLVM compiler to build the wheel.

The Getting Started with the LLVM System documentation page of LLVM contains some instruction on how to build it. I recorded my steps below.

  1. Install CMake :
pip install cmake

here we use the Python wheel which is easier to install.

  1. Download LLVM and Clang source
curl -LO
tar -xvf llvm-11.0.1.src.tar.xz
  1. Create a new directory for the build files and enter it:
mkdir build
cd build
  1. Configure the build with CMake
cmake -G "Unix Makefiles" -DLLVM_TARGETS_TO_BUILD="X86;NVPTX;AMDGPU" -DCMAKE_BUILD_TYPE="Release" ../

We tell CMake to build Release version which performs best optimization and disables debug information. For more detailed information see CMAKE_BUILD_TYPE . List of Targets available. We may also include -DLLVM_INCLUDE_TESTS=Off -DLLVM_INCLUDE_EXAMPLES=Off -DLLVM_INCLUDE_BENCHMARKS=Off to skip the unnecessary build.

  1. Compile and install
make -j4 install

By default, the built bin, lib and include will be installed to /usr/local/. We can now use the compiled LLVM libraries in other project, e.g. Embedding LLVM in your project .