2011年2月26日 星期六

LLVM 2.8 env set && pass manager set

1. LLVM 2.8 environment build 這邊我比較偷懶用 git clone 的方式
git clone http://llvm.org/git/llvm.git
2. clang: a C language family front end for LLVM
cd llvm/tools
svn co http://llvm.org/svn/llvm-project/cfe/trunk clang
3. Build LLVM and Clang:
cd build
../llvm/configure
make
如果還要加入 C++ 的 compiler 請參考 Clang C++ support ps: 如果你懶的話.其實用 apt-get install 就可以找到相對應的套件.
apt-get install llvm-gcc-4.2
ps: 不過這只適用於 LLVM 2.7, 因為 GCC 4.3 之後是 GPL3 授權,所以 LLVM 2.8 把llvm-gcc 拿掉,變成要用外卦的 clang 當 front end 來解析 c/c++ 的語法.或者是自行把 GCC 4.2 include 進去. 假設 env path 跟 clang 都已經set好, 底下就用 LLVM 2.8 + pass manager 建立起一個基於 LLVM env 的 back end project.
export PATH=$PATH:/your_llvm_loc/llvm/obj_root/Debug+Asserts/bin
4. project build 可參考 Writing an LLVM PassCreating an LLVM Project, 裡面有詳細的說明. 請記得在 makefile 中加入 LOADABLE_MODULE =1 來產生 share lib.
# Make the shared library become a loadable module so the tools can
# dlopen/dlsym on the resulting library.
LOADABLE_MODULE = 1
不過 Writing an LLVM Pass 的 example 是乎有些 bug, 可以用下面的 patch update.
--- oldHello.cpp        2011-02-26 13:16:52.872094173 +0800
+++ PassHello.cpp       2011-02-26 11:31:27.639147310 +0800
@@ -1,6 +1,7 @@
#include "llvm/Pass.h"
#include "llvm/Function.h"
#include "llvm/Support/raw_ostream.h"
+#include "llvm/ADT/Statistic.h"

using namespace llvm;

@@ -17,6 +18,6 @@
};

char Hello::ID = 0;
-  static RegisterPass X("hello", "Hello World Pass", false, false);
+  static RegisterPass<Hello> X("hello", "Hello World Pass", false, false);
}
後續就請參考 Writing an LLVM Pass 有詳細的說明. ps: 可以參考 llvm/lib/Transform/Hello/ 有比較完整的說明,不然弄半天還弄不出來還真的很XXX.最後記得在 opt -load 時候要把 *.so 的路徑指到所在的 project 下的 Debug+Asserts/lib.不然直接 load 會造成底下的 Error
Error opening 'LLVMHellocc.so': LLVMHellocc.so: cannot open shared object file: No such file or directory
  -load request ignored.
感覺 Debug+Asserts 被 LLVM 的 key word tied 住.要用底下的 command 才會 work
opt -load Debug+Asserts/lib/LLVMHellocc.so -help | grep hello
Ref http://article.gmane.org/gmane.comp.compilers.llvm.devel/31132/match=hello+so ref: http://dcreager.net/2010/02/17/llvm-lto-karmic/ http://llvm.org/docs/doxygen/html/Hello_8cpp.html

沒有留言:

張貼留言