Skip to content

Whole Program Analysis (using WLLVM)

Fabian Schiebel edited this page Sep 17, 2023 · 5 revisions

Analyzing a complex project

Within PhASAR, analyses are done on LLVM IR that comes either as human-readable .ll files or binary .bc files. In order to produce LLVM IR, firstly the project should be compiled. For more complex C/C++ projects, you may want to use WLLVM, which provides tools for building whole-program LLVM bitcode files. You can use the wllvm compiler wrapper as drop-in replacement for clang or clang++. After compiling, you can use the WLLVM tool extract-bc to produce a LLVM bitcode file for each object file, executable, or library. WLLVM takes care that all bitcode parts of the individual object files are correctly linked together.

Install WLLVM

WLLVM is a pip package. You can just install it by either of these commands:

pip install wllvm
sudo pip install wllvm

Then set LLVM_COMPILER environment variable to clang by the following command:

export LLVM_COMPILER=clang

Two following examples show how WLLVM can be used to produce bitcode file for C++ projects:

First, download two example projects from PhASAR website. Then, do the following.

In the Makefile of simple_makefile_project, change:

CXX = clang++

into

CXX = wllvm++

In CMakeLists.txt file of simple_cmake_project, change:

set(CMAKE_C_COMPILER clang)
set(CMAKE_CXX_COMPILER clang++)

into

set(CMAKE_C_COMPILER wllvm)
set(CMAKE_CXX_COMPILER wllvm++)

or set these variables when configuring the project with the cmake command.

After compiling each project, run the extract-bc command on each executable binary:

extract-bc main for simple_makefile_project and extract-bc myprogram for simple_cmakefile_project.

Above commands produce LLVM bitcode files of the whole project. To create human readable .ll file of the produced .bc file, run the llvm-dis main.bc and llvm-dis mypogram.bc for simple_makefile_project and simple_cmakefile_project respectively. This step is optional as PhASAR can deal with both LLVM IR and LLVM Bitcode; however, using the bitcode directly may improve loading times.

You can read more about WLLM on WLLVM git and WLLVM Project description.

If you feel that WLLVM is too slow for your use-cases, you may consider using GLLVM instead.

Clone this wiki locally