A. Download CLion from the Homepage, and install it.

B. Create a new project:

Since LLVM Passes are built as shared libraries, we configure the new project as followed:

C. Open the Preferences (Command + ,), and add the $LLVM_HOME to the Path Variables:

D. Clion has prepared files of CMakeLists.txt, library.cpp, and library.h. Rename the .cpp file as hello.cpp, and remove .h file which is not necessary in this project.

E. Copy the source code from the llvm-project’s Hello pass to hello.cpp:

cmake_minimum_required(VERSION 3.4.3)

project(Hello)

set(CMAKE_CXX_STANDARD 20)

 

# provide the dependencies from LLVM

set(ENV{LLVM_HOME} /usr/local/Cellar/llvm/9.0.0_1)

if(NOT DEFINED ENV{LLVM_DIR})

set(ENV{LLVM_DIR} $ENV{LLVM_HOME}/lib/cmake/llvm)

endif()

 

find_package(LLVM REQUIRED CONFIG)

add_definitions(${LLVM_DEFINITIONS})

include_directories(${LLVM_INCLUDE_DIRS}) # include

link_directories(${LLVM_LIBRARY_DIRS}) # libs

 

add_library(Hello MODULE

# List your source files here.

Hello.cpp

)

 

# LLVM is (typically) built with no C++ RTTI. We need to match that;

# otherwise, we'll get linker errors about missing RTTI data.

# it could be omitted if LLVM is compiled from source code and RTTI is attached

set_target_properties(Hello PROPERTIES

COMPILE_FLAGS "-fno-rtti"

)

 

# Get proper shared-library behavior (where symbols are not necessarily

# resolved when the shared library is linked) on OS X.

if(APPLE)

set_target_properties(Hello PROPERTIES

LINK_FLAGS "-undefined dynamic_lookup"

)

endif(APPLE)

F. Modify the CMakeLists.txt as:

G. Build the code (Command + F9), and the information:

H. Run the new pass

1) In the path of the project, the built pass is in the directory of cmake-build-debug (or release), named as libHello.so:

2) Take the 188.ammp from SPEC 2000 as the object program, and run the pass with the command opt, obtaining a list of the function names: