-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfuzz_existing.py
161 lines (140 loc) · 4.63 KB
/
fuzz_existing.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import os
import sys
import subprocess
import shutil
from multiprocessing import Pool
from check import check_once_impl
import random
import tqdm
alive2_tv = sys.argv[1]
llvm_bin = sys.argv[2]
llvm_opt = os.path.join(llvm_bin, "opt")
tool_bin = sys.argv[3]
mutate_bin = os.path.join(tool_bin, "mutate")
merge_bin = os.path.join(tool_bin, "merge")
cost_bin = os.path.join(tool_bin, "cost")
work_dir = "fuzz"
pass_name = "instcombine<no-verify-fixpoint>"
test_dir = sys.argv[4]
test_count = int(sys.argv[5])
processes = 16
if os.path.exists(work_dir):
shutil.rmtree(work_dir)
os.makedirs(work_dir)
seed_dir = os.path.join(work_dir, "seed")
os.makedirs(seed_dir)
block_list = [
"minmax-fold.ll", # Known FP issue
"clamp-to-minmax.ll",
"loadstore-metadata.ll", # noalias.addrspace
"fabs.ll",
"fpcast.ll",
"fcmp.ll",
"sign-test-and-or.ll", # https://alive2.llvm.org/ce/z/vbzktq
"2008-01-13-AndCmpCmp.ll", # https://alive2.llvm.org/ce/z/-eysFm
"icmp-equality-test.ll", # https://github.com/llvm/llvm-project/issues/121702
"preserve-sminmax.ll", # https://github.com/llvm/llvm-project/issues/121772
"select-imm-canon.ll", # https://github.com/llvm/llvm-project/issues/121774
"select_meta.ll",
"sadd_sat.ll",
"unsigned_saturated_sub.ll",
"matching-binops.ll",
"minmax-fp.ll", # https://github.com/llvm/llvm-project/issues/121786
"fast-basictest.ll", # https://github.com/llvm/llvm-project/issues/121790
"fneg-fabs.ll", # https://github.com/AliveToolkit/alive2/pull/1155
"simplify-demanded-fpclass.ll", # https://github.com/AliveToolkit/alive2/pull/1155
"unordered-fcmp-select.ll", # https://alive2.llvm.org/ce/z/xz-He7
]
def preprocess(pack):
id, file = pack
if file in block_list:
return None
if file.endswith(".ll"):
try:
orig = os.path.join(test_dir, file)
tmp = os.path.join(seed_dir, f"{id}")
os.makedirs(tmp)
shutil.copyfile(orig, os.path.join(tmp, "orig.ll"))
seed = os.path.join(tmp, "seed.ll")
merge_ops = []
# merge_ops = ['-ignore-fp']
subprocess.check_call(
[merge_bin, tmp, seed] + merge_ops, stderr=subprocess.DEVNULL
)
ref_out = os.path.join(tmp, "ref.ll")
subprocess.check_call(
[llvm_opt, "-passes=" + pass_name, seed, "-o", ref_out, "-S"],
stderr=subprocess.DEVNULL,
)
return (seed, ref_out)
except Exception:
pass
return None
tests = []
with Pool(processes) as pool:
for res in pool.imap_unordered(preprocess, enumerate(os.listdir(test_dir))):
if res is not None:
tests.append(res)
print(f"Valid tests: {len(tests)}")
def parse_cost(output: str):
res = dict()
for line in output.splitlines():
k, v = line.strip().split(" ")
res[k] = int(v)
return res
ref_cost = dict()
for k, v in tests:
ref_cost[v] = parse_cost(subprocess.check_output([cost_bin, v]).decode())
def compare(before, after, precond):
if before in ref_cost:
before_cost = ref_cost[before]
else:
before_cost = parse_cost(subprocess.check_output([cost_bin, before]).decode())
after_cost = parse_cost(subprocess.check_output([cost_bin, after]).decode())
if precond is not None:
if precond in ref_cost:
precond_cost = ref_cost[precond]
else:
precond_cost = parse_cost(
subprocess.check_output([cost_bin, precond]).decode()
)
for k in after_cost.keys():
if k not in before_cost:
continue
# print(k, before_cost[k], after_cost[k])
if before_cost[k] < after_cost[k]:
if precond is not None:
if before_cost[k] < precond_cost[k]:
continue
print(k)
return True
return False
recipes = ["correctness", "commutative", "multi-use", "canonical-form"]
def check(id):
# recipe = random.choice(recipes)
recipe = recipes[0]
seed, seed_ref = random.choice(tests)
if check_once_impl(
id,
work_dir,
recipe,
seed,
seed_ref,
mutate_bin,
llvm_opt,
alive2_tv,
pass_name,
compare,
):
return (id, recipe, seed)
return None
progress = tqdm.tqdm(range(test_count))
with Pool(processes) as pool:
for res in pool.imap_unordered(check, range(test_count)):
progress.update()
if res is None:
continue
id, recipe, seed = res
progress.write(f"{id} {recipe} {seed}")
# exit(1)
progress.close()