-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathabcsynth.py
98 lines (83 loc) · 3.15 KB
/
abcsynth.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
"""
Goal: synthesize all circuit by ABC to get #LUTs and delay
Output: circ.abc.pkl.gz
"""
import pandas as pd
import subprocess
import re
from tqdm import tqdm
import gzip
import os
import json
class ABCsynth:
def __init__(self):
pass
def runSynth(self, verilog, fname=None, raise_on_error = False):
# print(verilog)
if not fname:
fname = "tmp/tmp_abc{}.v".format(os.getpid())
# hack to make ABC working
verilog = re.sub(r"(\S)=(\S)", r"\1 = \2", verilog)
open(fname, "w").write(verilog)
#r = subprocess.run(["berkeley-abc", "-c", "read_verilog tmp.v; sweep; resyn2; ps -p"], stdout=subprocess.PIPE)
r = subprocess.run(
["berkeley-abc", "-c", f"read_verilog {fname}; if -K 6; ps -p"], stdout=subprocess.PIPE)
res = r.stdout.decode("utf-8").split("\n")[-2]
#print(res)
m = re.match(r"""
(\W*\d+;\d+)? # unprintable character
([^ ]+).*:.* # name
i\/o\s*=\s*(\d+)\/\s*(\d+) # i/o
\s*lat\s*=\s*(\d+) # lat
\s*and\s*=\s*(\d+) # and
\s*lev\s*=\s*(\d+) # lev
\s*power\s*=\s*(\d+\.?\d*) # power
""", res, re.X) # / 14 lat = 0 and = 20 lev = 6 power = 24.26
if m:
_, g_name, g_in, g_out, g_lat, g_and, g_lev, g_power = m.groups()
return {
"abc_name": g_name,
"abc_in": int(g_in),
"abc_out": int(g_out),
"abc_lat": int(g_lat),
"abc_and": int(g_and),
"abc_lev": int(g_lev),
"abc_power": float(g_power),
}
m = re.match(r"""
(\W*\d+;\d+)? # unprintable character
([^ ]+).*:.* # name
i\/o\s*=\s*(\d+)\/\s*(\d+) # i/o
\s*lat\s*=\s*(\d+) # lat
\s*nd\s*=\s*(\d+) # nd
\s*edge\s*=\s*(\d+) # nd
\s*aig\s*=\s*(\d+) # nd
\s*lev\s*=\s*(\d+) # lev
\s*power\s*=\s*(\d+\.?\d*) # power
""", res, re.X) # / 14 lat = 0 and = 20 lev = 6 power = 24.26
if m:
_, g_name, g_in, g_out, g_lat, g_nd, g_edge, g_aig, g_lev, g_power = m.groups()
return {
"abc_name": g_name,
"abc_in": int(g_in),
"abc_out": int(g_out),
"abc_lat": int(g_lat),
"abc_nd": int(g_nd),
"abc_edge": int(g_edge),
"abc_aig": int(g_aig),
"abc_lev": int(g_lev),
"abc_power": float(g_power),
}
if raise_on_error:
raise Exception("Cannot parse ABC output: " + r.stdout.decode("utf-8") + "\n" +
r.stderr.decode("utf-8") +
"\nTry run command: " + f"berkeley-abc -c read_verilog {fname};")
return {
"abc_name": "ERROR",
"abc_in": None,
"abc_out": None,
"abc_lat": None,
"abc_and": None,
"abc_lev": None,
"abc_power": None,
}