harvard-acc / LLVM-Tracer

An LLVM pass to profile dynamic LLVM IR instructions and runtime values
Other
135 stars 35 forks source link

Write CMake script & port to LLVM 3.5 #2

Closed rgly closed 9 years ago

rgly commented 9 years ago

I write CMake scripts which are smart enough to judge the LLVM version and upgrade the required LLVM version from 3.4 to 3.5. I have checked that Opcodes of LLVM 3.5 is compatiable with 3.4. The reason using CMake is that it supports out-of-source build. Simply remove the build directory when object files and executables are not needed.

ysshao commented 9 years ago

very nice, thanks chia-wei!

i really like the cmake scripts, will try it out. I'm a bit concerned with the LLVM 3.4 -> LLVM 3.5 change since we have quite a few users using Aladdin with LLVM 3.4 setup. Can we make the script compatible for both 3.4 and 3.5?

rgly commented 9 years ago

Hi, I do agree your concern. I am a aladdin user too! I write some macro in fulltrace.cpp to judge which LLVM interface to use, and make CMake to pass macro LLVM_VERSION=34 or 35 to all it's C/C++ code.

ysshao commented 9 years ago

oh i don't seem to see the LLVM macros in full_trace.cpp...am i missing something? the current code doesn't compile with my llvm 3.4 setup.

ysshao commented 9 years ago

If I just do cmake, it shows

CMake Error at scripts/LLVMFinder/findLLVM.cmake:68 (message): LLVM version is recommanded to be : 3.5

Your current version is 3.4

and exit compilation.

ysshao commented 9 years ago

oh didn't see your new commit...ok, just pulled your new commit, i got this when i build full-trace:

[100%] Building CXX object CMakeFiles/full_trace.dir/full_trace.o /group/vlsiarch/shao/Projects/GitHub/LLVM-Tracer/full-trace/full_trace.cpp:4:31: fatal error: llvm/IR/Constants.h: No such file or directory

include "llvm/IR/Constants.h"

                           ^

compilation terminated. make[2]: * [CMakeFiles/full_trace.dir/full_trace.o] Error 1 make[1]: * [CMakeFiles/full_trace.dir/all] Error 2 make: *\ [all] Error 2

ysshao commented 9 years ago

and if i do cmake at the top level, i got this:

[shao@rb22]LLVM-Tracer $ cmake . -- use LLVM_ROOT : /group/brooks/shao/llvm-3.4 -- find LLVM-Config : /group/brooks/shao/llvm-3.4/bin/llvm-config -- LLVM version : 3.4 CMake Error at scripts/LLVMFinder/findLLVM.cmake:91 (get_filename_component): get_filename_component unknown component DIRECTORY Call Stack (most recent call first): scripts/LLVMFinder/findLLVM.cmake:135 (LOAD_LLVM_SETTINGS) scripts/findAndSetLLVM.cmake:13 (FIND_LLVM) CMakeLists.txt:7 (include)

-- LLVM_LINK found : /group/brooks/shao/llvm-3.4/bin/llvm-link -- LLVM_OPT found : /group/brooks/shao/llvm-3.4/bin/opt -- LLVM_LLC found : /group/brooks/shao/llvm-3.4/bin/llc -- LLVM_COMPILER found : /group/brooks/shao/llvm-3.4/bin/clang CMake Error at scripts/buildBitcode.cmake:38 (get_filename_component): get_filename_component unknown component DIRECTORY Call Stack (most recent call first): scripts/buildBitcode.cmake:65 (build_llvm_bc_object) profile-func/CMakeLists.txt:5 (build_llvm_bitcode)

CMake Error at scripts/buildBitcode.cmake:38 (get_filename_component): get_filename_component unknown component DIRECTORY Call Stack (most recent call first): scripts/buildBitcode.cmake:65 (build_llvm_bc_object) profile-func/CMakeLists.txt:5 (build_llvm_bitcode)

CMake Error at scripts/buildBitcode.cmake:38 (get_filename_component): get_filename_component unknown component DIRECTORY Call Stack (most recent call first): scripts/buildBitcode.cmake:65 (build_llvm_bc_object) scripts/buildTracerBitcode.cmake:24 (build_llvm_bitcode) example/triad/CMakeLists.txt:4 (build_tracer_bitcode)

-- Configuring incomplete, errors occurred!

rgly commented 9 years ago

Sorry for misusing newer version of CMake API. I have pushed two more patches. one for pass -DLLVM_VERSION=34 while compiling fulltrace.cpp to fix Makefile build error. The other patch uses legacy API of CMake to fix the CMake build error.

ysshao commented 9 years ago

thanks! full-trace builds fine now, still have some issue with profile-func:

[shao@rb22]profile-func $ make [ 0%] Generating trace_logger.unopt.llvm llvm-link: Not enough positional command line arguments specified! Must specify at least 1 positional arguments: See: /group/brooks/shao/llvm-3.4/bin/llvm-link -help make[2]: * [profile-func/trace_logger.unopt.llvm] Error 1 make[1]: * [profile-func/CMakeFiles/trace_logger.dir/all] Error 2 make: *\ [all] Error 2

rgly commented 9 years ago

Wired, it looks like CMake does not get the source file name correctly. The script here tries to collect *.c files and it gets a empty source list in profile-func/CMakeLists.txt. Can you add some print-out code at the bottom of profile-func/CMakeLists.txt and re-cmake again?

# print-out code at bottom of profile-func/CMakeLists.txt
string(REPLACE ";" " "  TO_PRINT "${SRC}")
message("find trace_logger source :  ${TO_PRINT}")

I expect CMake will produce something like :

rgly@rgly-Linux-mint ~/Source/aladdin/build $ cmake ../original_tracer/ -DLLVM_RECOMMAND_VERSION="3.4" -DLLVM_ROOT=$HOME/lib/llvm-3.4
-- The CXX compiler identification is GNU 4.8.2
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- use LLVM_ROOT : /home/rgly/lib/llvm-3.4
-- find LLVM-Config : /home/rgly/lib/llvm-3.4/bin/llvm-config
-- LLVM version : 3.4.2
-- LLVM_LINK found : /home/rgly/lib/llvm-3.4/bin/llvm-link
-- LLVM_OPT found : /home/rgly/lib/llvm-3.4/bin/opt
-- LLVM_LLC found : /home/rgly/lib/llvm-3.4/bin/llc
-- LLVM_COMPILER found : /home/rgly/lib/llvm-3.4/bin/clang-3.4
find trace_logger source : /home/rgly/Source/aladdin/original_tracer/profile-func/trace_logger.c
-- Configuring done
-- Generating done
-- Build files have been written to: /home/rgly/Source/aladdin/build
ysshao commented 9 years ago

hmm, what I see is this:

[shao@rb22]LLVM-Tracer $ cmake . -DLLVM_RECOMMAND_VERSION="3.4" -- use LLVM_ROOT : /group/brooks/shao/llvm-3.4 -- find LLVM-Config : /group/brooks/shao/llvm-3.4/bin/llvm-config -- LLVM version : 3.4 -- LLVM_LINK found : /group/brooks/shao/llvm-3.4/bin/llvm-link -- LLVM_OPT found : /group/brooks/shao/llvm-3.4/bin/opt -- LLVM_LLC found : /group/brooks/shao/llvm-3.4/bin/llc -- LLVM_COMPILER found : /group/brooks/shao/llvm-3.4/bin/clang find trace_logger source : -- Configuring done -- Generating done -- Build files have been written to: /group/vlsiarch/shao/Projects/GitHub/LLVM-Tracer

ysshao commented 9 years ago

the reason that it cannot find trace_logger.c is that if I do a make clean, it deletes trace_logger.c as well as example/triad/triad.c.

[shao@rb22]LLVM-Tracer $ make clean [shao@rb22]LLVM-Tracer $ git status On branch rgly-master Your branch is ahead of 'rgly/master' by 2 commits.

Changed but not updated: (use "git add/rm ..." to update what will be committed) (use "git checkout -- ..." to discard changes in working directory)

modified:   Makefile
deleted:    example/triad/triad.c
modified:   full-trace/Makefile
modified:   profile-func/Makefile
deleted:    profile-func/trace_logger.c
rgly commented 9 years ago

Thanks for your feedback! I push two more patches to fix them.

I find out the reason of CMake failure whlie in-source-build. In order to get LLVM bitcode file path for later use, CMake scripts replace all of .cpp file names to .obj.llvm file names. Since all test cases pass .c files, CMake scripts find no .cpp to replace and just use the .c file name as LLVM bitcode file path. While building LLVM bitcodes, all the .c files are gone(clang -o a.c a.c).

ysshao commented 9 years ago

hmm, i still get this:

[shao@rb22]LLVM-Tracer $ cmake . -- use LLVM_ROOT : /group/brooks/shao/llvm-3.4 -- find LLVM-Config : /group/brooks/shao/llvm-3.4/bin/llvm-config -- LLVM version : 3.4 -- LLVM_LINK found : /group/brooks/shao/llvm-3.4/bin/llvm-link -- LLVM_OPT found : /group/brooks/shao/llvm-3.4/bin/opt -- LLVM_LLC found : /group/brooks/shao/llvm-3.4/bin/llc -- LLVM_COMPILER found : /group/brooks/shao/llvm-3.4/bin/clang CMake Error at scripts/buildBitcode.cmake:42 (get_filename_component): get_filename_component unknown component DIRECTORY Call Stack (most recent call first): scripts/buildBitcode.cmake:69 (build_llvm_bc_object) profile-func/CMakeLists.txt:5 (build_llvm_bitcode)

CMake Error at scripts/buildBitcode.cmake:42 (get_filename_component): get_filename_component unknown component DIRECTORY Call Stack (most recent call first): scripts/buildBitcode.cmake:69 (build_llvm_bc_object) scripts/buildTracerBitcode.cmake:24 (build_llvm_bitcode) example/triad/CMakeLists.txt:4 (build_tracer_bitcode)

-- Configuring incomplete, errors occurred!

rgly commented 9 years ago

Oh, sorry. I lost one line in previous patch. I push a patch and I think it's working now.

rgly commented 9 years ago

push another patch for use the same CFLAG and LDFLAG settings with llvm_compile.py while building bitcode

ysshao commented 9 years ago

Great! That seems to work now!

I'm testing the autoinstall script, seems there are some issues there:

[shao@rb22]build $ cmake .. -DAUTOINSTALL=True -- The CXX compiler identification is GNU -- Check for working CXX compiler: /usr/local/bin/g++ -- Check for working CXX compiler: /usr/local/bin/g++ -- works -- Detecting CXX compiler ABI info -- Detecting CXX compiler ABI info - done -- use LLVM_ROOT : /group/vlsiarch/shao/Projects/GitHub/LLVM-Tracer/build/lib/llvm-3.4 CMake Error at scripts/AutoInstaller/installLLVM.cmake:13 (include): include could not find load file:

ProcessorCount

Call Stack (most recent call first): scripts/AutoInstaller/installLLVM.cmake:259 (configure_processor_count) scripts/LLVMFinder/findLLVM.cmake:29 (autoinstall_llvm) scripts/LLVMFinder/findLLVM.cmake:130 (FIND_LLVM_CONFIG_EXE_OR_AUTOINSTALL) scripts/findAndSetLLVM.cmake:13 (FIND_LLVM) CMakeLists.txt:7 (include)

-- find LLVM-Config : LLVM_CONFIG_EXE-NOTFOUND CMake Error at scripts/AutoInstaller/installLLVM.cmake:13 (include): include could not find load file:

ProcessorCount

Call Stack (most recent call first): scripts/AutoInstaller/installLLVM.cmake:259 (configure_processor_count) scripts/LLVMFinder/findLLVM.cmake:63 (autoinstall_llvm) scripts/LLVMFinder/findLLVM.cmake:133 (CHECK_LLVM_CONFIG_VERSION_OR_AUTOINSTALL) scripts/findAndSetLLVM.cmake:13 (FIND_LLVM) CMakeLists.txt:7 (include)

CMake Error at scripts/LLVMFinder/findLLVM.cmake:92 (get_filename_component): get_filename_component called with incorrect number of arguments Call Stack (most recent call first): scripts/LLVMFinder/findLLVM.cmake:135 (LOAD_LLVM_SETTINGS) scripts/findAndSetLLVM.cmake:13 (FIND_LLVM) CMakeLists.txt:7 (include)

CMake Error at scripts/LLVMFinder/findLLVMTools.cmake:8 (message): finds no LLVM_LINK Call Stack (most recent call first): scripts/LLVMFinder/findLLVMTools.cmake:17 (findLLVMTool_Fct) scripts/findAndSetLLVM.cmake:14 (findLLVMTool) CMakeLists.txt:7 (include)

-- Configuring incomplete, errors occurred!

rgly commented 9 years ago

ProcessorCount function is a API of CMake 2.8.12, and the LLVM AutoInstaller use it to estimate the job number for building LLVM(make -j N). Building LLVM takes 4 hours on my Sandy Bridge i5 CPU in sequential. It is better to run it in parallel, and using merely "make -j" may consume all the memories. How about raising the required version of CMake from 2.8.8 to 2.8.12 or any newer?

I also push a patch to give a more detailed document about LLVM AutoInstaller.

ysshao commented 9 years ago

Overall it looks good. One comment:

I'd like to keep both the original Makefile and the cmake flows. The original makefiles gives users a clean makefile to compile the tracer after they have installed the LLVM/Clang flow; the cmake flow provides users a comprehensive way to install LLVM/Clang behind the scene.

So for users that have already installed LLVM on their own, they can just to a make at where they put LLVM-Tracer.

For the cmake users, can we suggest that they

  1. Create a separate director, let's say at /where/they/put/LLVM-Tracer/build.
  2. Do cmake .. -DLLVM_ROOT=/where/they/want/to/install/LLVM -DAUTOINSTALL=True

One thing I notice here is that if I do a make inside the build directory, the full_trace.so will be built in build/full-trace/full_trace.so instead of $TRACER_HOME/full-trace/full_trace.so. Can we change here so that the library still resides in $TRACER_HOME/full-trace even we build in a separate build directory?

rgly commented 9 years ago

I updated Readme.md, hoping they're clear.

If users create a build directory, I think users may expect that the source code directory keeps clean after binary building. In my own flow, I set $TRACER_HOME to the LLVM-Tracer/build, so that Aladdin can still use them by python scripts.

How about offering a CMake option to generate fulltrace.so & trace_logger.llvm under source directory? I just want that users are aware about dirty-source-directory while using CMake.

 -DBUILD_ON_SOURCE=TRUE
   By assign this option, CMake will build fulltrace.so &
   trace_logger.llvm under the source directory.
   Other llvm bitcode and object files still remain in the build directory.
ysshao commented 9 years ago

Looks pretty good! A couple minor things:

1) Can we change the folder name "scripts" to "cmake-scripts"? Just to be a bit more specific.

2) In the README file, a) Before "Build with cmake", can we add a couple sentence there describing that there are two ways of building LLVM-Tracer: one is the CMake flow which is recommended if users are familiar with CMake flow and/or want to automatically install LLVM and Clang; the other is the standalone Makefile which is recommended if users just want to a simple Make.

b) In the "Build with CMake" session on "Available CMake Settings", what do you think if we clarify what the default options are? For example, for -DLLVM_ROOT, we can do

-DLLVM_ROOT=/where/your/llvm/install (default: $LLVM_HOME) -DLLVM_RECOMMEND_VERSION="3.4","3.5" (default: 3.4) btw, is this 3.4 or 34? I thought it's 34 at some point? and do users need to include quotation marks? -DAUTOINSTAL=TRUE,FALSE (default: FALSE) -DCMAKE_BUILD_TYPE=None,Debug,Release (default: None) -DBUILD_ON_SOURCE=TRUE,FALSE (default: ??) what do you think of making the default TRUE? I'm just concerned that the legacy users may get confused of where the library is.

rgly commented 9 years ago

Finished ! Thanks for your feedback.

ysshao commented 9 years ago

Awesome! Thanks!