-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmeasure-error.py
140 lines (130 loc) · 5.45 KB
/
measure-error.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
import os.path
import subprocess
import sys
from typing import List, Tuple, Union
def execute(cmd: List[str], i: Union[None, str] = None) -> Tuple[int, str, str]:
result = subprocess.run(
cmd,
text = True,
input = i,
capture_output = True
)
return (result.returncode, result.stdout, result.stderr)
def call_preprocessor(clang_path: str, fpath: str, ipaths: List[str]) -> str:
cmd = [clang_path, "-std=c++20", "-E", "-P", fpath]
for ipath in ipaths:
cmd.append("-I" + ipath)
result = execute(cmd)
if result[0] != 0:
sys.exit(f"Error: {cmd} failed\n{result[2]}")
return result[1]
def call_synthesizer(synthesizer_path: str, fpath: str) -> str:
cmd = [synthesizer_path, fpath, "--", "-std=c++20"]
result = execute(cmd)
if result[0] != 0:
sys.exit(f"Error: {cmd} failed\n{result[2]}")
return result[1]
def cut_synthesizer_output_section(output: str, section: str) -> str:
section_header = f'[-[{section}]-]'
start = output.find(section_header) + len(section_header)
end = output.find('[-[]-]', start)
return output[start:end].strip()
def compose_invalid_code(main: str, call: str) -> str:
lines = main.splitlines()
lines.insert(-2, "struct S {};") # insert before
lines.insert(-1, "S s;")
lines.insert(-1, call)
return "\n".join(lines)
def get_error_message(clang_path: str, code: str) -> str:
cmd = [clang_path, "-x", "c++", "-std=c++20", "-w", "-"]
result = execute(cmd, code)
if result[0] == 0:
sys.exit(f"Error: {cmd} didn't fail as expected\n{result[1]}")
return result[2]
def get_callee_name(call: str) -> str:
return call.split("(")[0]
def classify(lst: List[int]) -> List[Tuple[int, int, int]]:
results = []
lst.sort()
n = len(lst)
i = 0
start = 0
step = 50
cnt = 0
while i < n:
if lst[i] < start + step:
cnt += 1
i += 1
else:
results.append((start, start + step, cnt))
start = start + step
cnt = 0
if cnt > 0:
results.append((start, start + step, cnt))
return results
def run_benchmark(fpath: str, includes: List[str], name_prefix: str) -> None:
print(f"### started error message measurement for {fpath}")
paths = {
"clang": "clang++",
"synthesizer": "./llvm-project-llvmorg-19.1.1/build/bin/concept-synthesizer",
"source": fpath,
"preprocessed": fpath + ".preprocessed.cc",
}
preprocessed_code = call_preprocessor(paths["clang"], paths["source"], includes)
with open(paths["preprocessed"], "w") as f:
f.write(preprocessed_code)
synthesizer_output = call_synthesizer(paths["synthesizer"], paths["preprocessed"])
constrained_code = cut_synthesizer_output_section(synthesizer_output, "Constrained code")
invalid_calls = [
ic
for ic in cut_synthesizer_output_section(synthesizer_output, "Invalid calls").splitlines()
if ic and get_callee_name(ic).startswith(name_prefix)
]
print("[statistics]")
print(cut_synthesizer_output_section(synthesizer_output, "Statistics"))
print("[resource consumption]")
print(cut_synthesizer_output_section(synthesizer_output, "Resource consumption"))
print("[error reduction measurement]")
n = len(invalid_calls)
original_error_lengths = []
constrained_error_lengths = []
constraint_not_satisfied_count = 0
for (i, call) in enumerate(invalid_calls):
error1 = get_error_message(paths["clang"], compose_invalid_code(preprocessed_code, call))
error2 = get_error_message(paths["clang"], compose_invalid_code(constrained_code, call))
if "constraints not satisfied" in error2:
constraint_not_satisfied_count += 1
if i % 10 == 0:
print("* sample {{{")
print(error1)
print("* ===")
print(error2)
print("* }}}")
original_error_length = len(error1.splitlines())
constrained_error_length = len(error2.splitlines())
print(f"{i + 1}/{n}: original = {original_error_length}\t"
f"constrained = {constrained_error_length}\t"
f"name = {get_callee_name(call)}")
original_error_lengths.append(original_error_length)
constrained_error_lengths.append(constrained_error_length)
if n > 0:
print(f"total count = {n}, constraint not satisfied = {constraint_not_satisfied_count}")
print(f"original average = {round(sum(original_error_lengths) / n, 3)}")
print(f"constrained average = {round(sum(constrained_error_lengths) / n, 3)}")
print("original distribution:")
original_intervals = classify(original_error_lengths)
for begin, end, count in original_intervals:
print(f"[{begin}, {end}) = {count}")
print("constrained distribution:")
constrained_intervals = classify(constrained_error_lengths)
for begin, end, count in constrained_intervals:
print(f"[{begin}, {end}) = {count}")
if __name__ == "__main__":
if not os.path.exists('./boost_1_84_0/'):
print("### downloading boost...")
execute(["wget", "https://archives.boost.io/release/1.84.0/source/boost_1_84_0.zip"])
print("### unzipping boost...")
execute(["unzip", "-q", "boost_1_84_0.zip"])
execute(["rm", "boost_1_84_0.zip"])
run_benchmark("./examples/stl-algo.cpp", [], "")
run_benchmark("./examples/boost-sf.cpp", ["./boost_1_84_0/"], "boost")