forked from sampsyo/llvm-pass-skeleton
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbenchmark.py
70 lines (59 loc) · 2.68 KB
/
benchmark.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import os
import subprocess
import sys
PASS_NAME = 'function-inlining'
PASS_PATH = os.path.expanduser(
'~/llvm-pass-skeleton/build/function-inlining/libFunctionInliningPass.so')
POLYBENCH_CODE_PATH = os.path.expanduser(
'~/Downloads/Misc/')
RUNS_PER_BENCHMARK = 5
BINARY_FILE = './a.out'
def emit_llvm_command(c_file, lib):
return f'clang -S -emit-llvm -Xclang -disable-O0-optnone {c_file}'
# return f'clang -S -emit-llvm -Xclang -disable-O0-optnone -I {lib} {c_file}'
def opt_command(name):
return f'opt -load {PASS_PATH} -{PASS_NAME} -S {name}.ll -o {name}_opt.ll'
def clang_command(name, polybench_c):
return f'clang {name} -o {BINARY_FILE}'
# return f'clang {name} {polybench_c} -o {BINARY_FILE}'
def benchmark(ll_file, polybench_c):
result = subprocess.run(clang_command(ll_file, polybench_c).split())
assert result.returncode == 0, 'compiling program failed'
result = subprocess.run(['time', BINARY_FILE], capture_output=True)
assert result.returncode == 0, 'running program failed' + str(result)
return float(result.stderr.strip().split()[-4])
if __name__ == '__main__':
if len(sys.argv) != 2:
print("Usage: python3 benchmark.py [output_csv_path]")
sys.exit(1)
out_file = sys.argv[1]
polybench = POLYBENCH_CODE_PATH
c_files = []
lib = os.path.join(polybench, 'utilities')
polybench_c = os.path.join(lib, 'polybench.c')
for root, subdirs, files in os.walk(polybench):
if 'utilities' in os.path.splitext(root):
continue
c_files += [os.path.join(root, f) for f in files if f.endswith('.c')]
print(f'Collected \033[1m{len(c_files)}\033[0m files to benchmark.')
print(f'\u001b[35mOutput will be saved to {out_file}\u001b[0m')
output = open(out_file, 'w')
output.write('Name, Original, Optimized\n')
for f in c_files:
name = os.path.splitext(os.path.basename(f))[-2]
print('----------------')
print(f'\033[1mBenchmarking {name}\033[0m')
result = subprocess.run(emit_llvm_command(f, lib).split())
assert result.returncode == 0, 'll generation failed'
result = subprocess.run(opt_command(name).split())
assert result.returncode == 0, 'running opt failed'
original = f'{name}.ll'
optimized = f'{name}_opt.ll'
for i in range(RUNS_PER_BENCHMARK):
print(f'\033[4mRun {i + 1} of {RUNS_PER_BENCHMARK}\033[0m')
orig = benchmark(original, polybench_c)
print(f'\033[36mOriginal: {orig}\033[0m')
opt = benchmark(optimized, polybench_c)
print(f'\033[92mOptimized: {opt}\033[0m')
output.write(f'{name}, {str(orig)}, {str(opt)}\n')
output.close()