compor / llvm-ir-cmake-utils

LLVM IR CMake utils for bitcode file manipulation by opt and friends
GNU Lesser General Public License v2.1
68 stars 14 forks source link
bitcode cmake intermediate-representation llvm

LLVM IR cmake utilities

Introduction

A collection of helper cmake functions/macros that eases the generation of LLVM IR and the application of various LLVM opt passes while obtaining and preserving the separate IR files that are generated by each user-defined step.

Requirements

Installation

Quick overview

The provided cmake commands are expected to work in a parasitic way to targets created via add_executable() and add_library. The "gateway" command is llvmir_attach_bc_target() which generates the required bitcode files. Currently, C/C++ are supported via clang/clang++, but in theory any compiler which produces LLVM bitcode should be easily supported (depending how nice it plays with cmake too).

The cmake calls currently provided are:

Influential properties

CAUTION

If you require to get raw unoptimized LLVM IR, but with the ability to further optimize it later on and you are compiling with LLVM 5 or later, you need to add the following compile options, either:

-O1 -Xclang -disable-llvm-passes

or

-O0 -Xclang -disable-O0-optnone

This is because, since LLVM 5, using -O0 add the optnone attribute to all functions.

Basic Usage

Have a look and toy around with the included examples in this repo. The easiest way to start is:

  1. git clone this repo.
  2. Create a directory for an out-of-source build and cd into it.
  3. CC=clang CXX=clang++ cmake [path to example source dir]
  4. cmake --build .
  5. cmake --build . --target help to see available target and use them for bitcode generation.