-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dc13965
commit 131d37c
Showing
8 changed files
with
231 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
llvm/ | ||
compiler-rt/ | ||
clang/ | ||
build/ | ||
# General | ||
.DS_Store | ||
.AppleDouble | ||
.LSOverride | ||
|
||
# Icon must end with two \r | ||
Icon | ||
|
||
# Thumbnails | ||
._* | ||
|
||
# Files that might appear in the root of a volume | ||
.DocumentRevisions-V100 | ||
.fseventsd | ||
.Spotlight-V100 | ||
.TemporaryItems | ||
.Trashes | ||
.VolumeIcon.icns | ||
.com.apple.timemachine.donotpresent | ||
|
||
# Directories potentially created on remote AFP share | ||
.AppleDB | ||
.AppleDesktop | ||
Network Trash Folder | ||
Temporary Items | ||
.apdisk | ||
|
||
.vscode/* | ||
!.vscode/settings.json | ||
!.vscode/tasks.json | ||
!.vscode/launch.json | ||
!.vscode/extensions.json | ||
|
||
test/*.bc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,14 @@ | ||
# llvm-pass-practice | ||
the road to write llvm pass | ||
|
||
|
||
## notice | ||
|
||
* path should not include Chinese character | ||
|
||
## install llvm | ||
`./install.sh` | ||
|
||
## practices | ||
|
||
* my first pass:show function name |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#get LLVM | ||
git clone --depth=1 http://llvm.org/git/llvm.git | ||
|
||
|
||
#get clang | ||
git clone --depth=1 http://llvm.org/git/clang.git | ||
|
||
|
||
#get compiler-rt | ||
git clone --depth=1 http://llvm.org/git/compiler-rt.git | ||
|
||
|
||
#Set up clang, compiler-rt | ||
pushd llvm/tools | ||
ln -s ../../clang . | ||
popd | ||
|
||
pushd llvm/projects | ||
ln -s ../../compiler-rt . | ||
popd | ||
|
||
mkdir build | ||
pushd build | ||
cmake -G "Unix Makefiles" ../llvm | ||
make -j4 | ||
popd |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
add_llvm_library( LLVMFuncName MODULE | ||
FuncName.cpp | ||
|
||
PLUGIN_TOOL | ||
opt | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#include "llvm/Pass.h" | ||
#include "llvm/IR/Function.h" | ||
#include "llvm/Support/raw_ostream.h" | ||
|
||
using namespace llvm; | ||
|
||
namespace { | ||
struct FuncName:public FunctionPass { | ||
static char ID; | ||
FuncName():FunctionPass(ID) {} | ||
bool runOnFunction(Function &F) { | ||
errs()<< F.getName() << "\n"; | ||
return false; | ||
} | ||
}; | ||
} | ||
|
||
char FuncName::ID = 0; | ||
static RegisterPass<FuncName> X("funcName","display function name"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
## setup env | ||
1. add library | ||
add | ||
``` | ||
add_llvm_library( LLVMFuncName MODULE | ||
YOUR_PASS.cpp | ||
|
||
PLUGIN_TOOL | ||
opt | ||
) | ||
``` | ||
into `PASS_DIR_PATH/CMakeLists.txt` | ||
|
||
2. add subdirectory | ||
add `add_subdirectory(PASS_DIR_PATH)` into `lib/Transforms/CMakeLists.txt` | ||
|
||
## coding | ||
|
||
1. the necessary header | ||
```cpp | ||
#include "llvm/Pass.h" | ||
#include "llvm/IR/Function.h" | ||
#include "llvm/Support/raw_ostream.h" | ||
``` | ||
|
||
2. add namespace | ||
```cpp | ||
using namespace llvm; | ||
``` | ||
|
||
3. write your own pass in an anonymous namespace | ||
```cpp | ||
namespace { | ||
} | ||
``` | ||
|
||
## build | ||
|
||
```sh | ||
⇒ doublemice@DoubleMice-MBP:~/Documents/graduate/llvm-pass-practice|master⚡ pwd | ||
/Users/doublemice/Documents/graduate/llvm-pass-practice | ||
pushd llvm/lib/Transforms | ||
ln ../../../my\ first\ pass/FuncName . | ||
popd | ||
pushd build | ||
make | ||
popd | ||
``` | ||
|
||
## load the pass | ||
|
||
### the output lib | ||
```sh | ||
doublemice@DoubleMice-MBP:~/Documents/graduate/llvm-pass-practice|master⚡ | ||
⇒ ls build/lib/LLVMFuncName.dylib | ||
build/lib/LLVMFuncName.dylib | ||
``` | ||
|
||
as we see,the output dylib name is exact the name we define in `PASS_DIR_PATH/CMakeLists.txt`(see: setup env -> 1) | ||
|
||
### load it with opt | ||
1. prepare a test file in test/FuncName_test.c | ||
```cpp | ||
#include <stdio.h> | ||
void sayHello() { | ||
printf("hello\n"); | ||
} | ||
void sayGoodbye() { | ||
printf("goodbye\n"); | ||
} | ||
int main() { | ||
sayHello(); | ||
sayGoodbye(); | ||
return 0; | ||
} | ||
``` | ||
|
||
2. compile it into a llvm bitcode | ||
```sh | ||
$LLVM_BIN/clang -O3 -emit-llvm test/FuncName_test.c -c -o test/FuncName_test.bc | ||
``` | ||
3. run the pass with opt | ||
```sh | ||
doublemice@DoubleMice-MBP:~/Documents/graduate/llvm-pass-practice|master⚡ | ||
⇒ $LLVM_BIN/opt -load build/lib/LLVMFuncName.dylib -funcName < test/FuncName_test.bc > /dev/null | ||
sayHello | ||
sayGoodbye | ||
main | ||
``` | ||
|
||
## a skeleton | ||
```cpp | ||
#include "llvm/Pass.h" | ||
#include "llvm/IR/Function.h" | ||
#include "llvm/Support/raw_ostream.h" | ||
using namespace llvm; | ||
namespace { | ||
struct Example:public FunctionPass { | ||
static char ID; | ||
Example():FunctionPass(ID) {} | ||
//tells llvm which other pass we need | ||
void getAnalysisUsage(AnalysisUsage &AU) const { | ||
... | ||
} | ||
virtual bool runOnFunction(Function &F) { | ||
... | ||
//true:this pass will change our program | ||
//false:won't change | ||
return false; | ||
} | ||
}; | ||
} | ||
char Example::ID = 0; | ||
static RegisterPass<Example> X("PASS_NAME","PASS_DESCRIPTION") | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#include <stdio.h> | ||
void sayHello() { | ||
printf("hello\n"); | ||
} | ||
void sayGoodbye() { | ||
printf("goodbye\n"); | ||
} | ||
int main() { | ||
sayHello(); | ||
sayGoodbye(); | ||
return 0; | ||
} |