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.
cmake3.0.0 or laterLLVMtools:- Currently used:
clang/clang++optllvm-dis/llvm-asllvm-link
- Tested with:
- 3.7 and later
- Currently used:
- Clone this repo (or even add it as a submodule to your project).
- In your
CMakeLists.txtfileinclude(LLVMIRUtil). - You are good to go!
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:
-
llvmir_attach_bc_target()Attaches to an existing target that can be compiled down toLLVM IRand does just that, using all the related flags and options from the main target. The existing supported targets make use ofclang/clang++, so currently this means that theC/C++language is supported. It usesadd_custom_library()cmakecommand under the hood. This creates a target of typeLLVMIR. -
llvmir_attach_opt_pass_target()Attaches to a target of typeLLVMIRand applies variousoptpasses to its bitcode files, specified as arguments. It usesadd_custom_library()cmakecommand under the hood. This creates a target of typeLLVMIR. -
llvmir_attach_disassemble_target()Attaches to a target of typeLLVMIRand usesllvm-disto disassemble its bitcode files. It usesadd_custom_library()cmakecommand under the hood. This creates a target of typeLLVMIR. -
llvmir_attach_assemble_target()Attaches to a target of typeLLVMIRand usesllvm-asto assemble its bitcode files. It usesadd_custom_library()cmakecommand under the hood. This creates a target of typeLLVMIR. -
llvmir_attach_link_target()Attaches to a target of typeLLVMIRand usesllvm-linkto link its bitcode files to a single bitcode file. The output bitcode file is names after the target name. It usesadd_custom_library()cmakecommand under the hood. This creates a target of typeLLVMIR. -
llvmir_attach_library()Attaches to a target of typeLLVMIRand uses the appropriate compiler to compile its bitcode files to a native library. The output library name uses the target name according to platform rules. It usesadd_library()cmakecommand under the hood. This creates a target of typeLLVMIR. -
llvmir_attach_executable()Attaches to a target of typeLLVMIRand uses the appropriate compiler to compile its bitcode files to a native executable. The output library name uses the target name according to platform rules. It usesadd_executable()cmakecommand under the hood. This creates a target of typeLLVMIR.
LLVMIR_SHORT_NAMEThis property, if present, controls the output name for the calls that produce a single object (e.g. archive, library, etc.):llvmir_attach_link_target()llvmir_attach_library()llvmir_attach_executable()
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.
Have a look and toy around with the included examples in this repo. The easiest way to start is:
git clonethis repo.- Create a directory for an out-of-source build and
cdinto it. CC=clang CXX=clang++ cmake [path to example source dir]cmake --build .cmake --build . --target helpto see available target and use them for bitcode generation.