Skip to content

Commit 2166f07

Browse files
eomisoML2jinmyoung
authored andcommitted
chore: Add a script to quickly run the project.
1 parent 323e736 commit 2166f07

File tree

2 files changed

+100
-1
lines changed

2 files changed

+100
-1
lines changed

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ sudo ./llvm.sh <version number>
2121
cmake .. -DLLVM_ROOT=<path to llvm source root>
2222
make llvm-block
2323
./llvm-block/llvm-block <before> <after>
24-
24+
2525
### Quick Commands
2626
clang -O0 -g -Xclang -disable-O0-optnone -emit-llvm -S *.c
2727
llvm-link *.ll -S -o beforeg.ll
@@ -37,3 +37,10 @@ sudo ./llvm.sh <version number>
3737
mv .*.dot before
3838
opt -dot-cfg after.ll
3939
mv .*.dot after
40+
41+
### Short script
42+
If you are too bothered to run the commands above, simply run `run-llvm-block.sh` with the following instructions.
43+
44+
1. First add a `*.c` file to any directory, say you have `foo/a.c`.
45+
46+
2. Then simply run `bash run-llvm-block.sh foo` (checkout the code for more options if you are on MacOS).

run-llvm-block.sh

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#!/bin/bash
2+
3+
# Check if $LLVM_BIN_PATH is set
4+
if [ -z "$LLVM_BIN_PATH" ]; then
5+
echo "LLVM_BIN_PATH is not set"
6+
exit 1
7+
fi
8+
9+
MACOS_FLAG=false
10+
OPT_LEVEL=0
11+
LLVM_PASS="mem2reg" # Default pass
12+
13+
# Parse command line arguments
14+
15+
while [[ $# -gt 0 ]]; do
16+
case "$1" in
17+
--macos)
18+
MACOS_FLAG=true
19+
shift
20+
;;
21+
--opt-level=*)
22+
OPT_LEVEL="${1#*=}"
23+
shift
24+
;;
25+
--pass=*)
26+
LLVM_PASS="${1#*=}"
27+
shift
28+
;;
29+
*)
30+
if [ -z "$TARGET_DIR" ]; then
31+
TARGET_DIR="$1"
32+
else
33+
echo "Error: Unexpected argument: $1"
34+
echo "Usage: $0 [--macos] [--opt-level=N] [--pass=<llvm-pass>] <target_directory>"
35+
exit 1
36+
fi
37+
shift
38+
;;
39+
esac
40+
done
41+
42+
# Check if target directory is provided
43+
if [ -z "$TARGET_DIR" ]; then
44+
echo "Usage: $0 [--macos] [--opt-level=<opt-level>] [--pass=<llvm-pass>] <target_directory>"
45+
exit 1
46+
fi
47+
48+
LLVM_BLOCK_PATH="$(pwd)/build/llvm-block"
49+
50+
# Get macOS SDK path
51+
if [ "$MACOS_FLAG" = true ]; then
52+
MACOS_SDK_PATH=$(xcrun --sdk macosx --show-sdk-path)
53+
else
54+
MACOS_SDK_PATH=""
55+
fi
56+
57+
# Change to target directory
58+
echo "Changing to target directory... $TARGET_DIR"
59+
cd "$TARGET_DIR"
60+
61+
# Compile C files to LLVM IR
62+
echo "Compiling C files to LLVM IR..."
63+
64+
if [ "$MACOS_FLAG" = true ]; then
65+
"$LLVM_BIN_PATH/clang" -O$OPT_LEVEL -g -Xclang -disable-O0-optnone -emit-llvm -S *.c \
66+
-isysroot "$MACOS_SDK_PATH"
67+
else
68+
"$LLVM_BIN_PATH/clang" -O$OPT_LEVEL -g -Xclang -disable-O0-optnone -emit-llvm -S *.c
69+
fi
70+
71+
# Link LLVM IR files
72+
"$LLVM_BIN_PATH/llvm-link" *.ll -S -o beforeg.ll
73+
74+
# Apply transformation pass
75+
"$LLVM_BIN_PATH/opt" beforeg.ll -S -$LLVM_PASS -o afterg.ll
76+
77+
# Run llvm-block
78+
"$LLVM_BLOCK_PATH/llvm-block" beforeg.ll afterg.ll 2> output
79+
80+
# Strip debug info
81+
"$LLVM_BIN_PATH/opt" --strip-debug -S beforeg.ll -o before.ll
82+
"$LLVM_BIN_PATH/opt" --strip-debug -S afterg.ll -o after.ll
83+
84+
# print log
85+
echo "Generating CFGs..."
86+
87+
# Generate CFGs
88+
mkdir -p before after
89+
"$LLVM_BIN_PATH/opt" -dot-cfg -f before.ll
90+
mv .*.dot before/
91+
"$LLVM_BIN_PATH/opt" -dot-cfg -f after.ll
92+
mv .*.dot after/

0 commit comments

Comments
 (0)