-
Notifications
You must be signed in to change notification settings - Fork 1
/
abc.cc
172 lines (152 loc) · 4 KB
/
abc.cc
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
162
163
164
165
166
167
168
169
170
171
172
/*************************************************************************
*
* Copyright (c) 2024 Rajit Manohar
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
**************************************************************************
*/
#include "expropt.h"
#include <string.h>
#include <stdio.h>
#include "abc_api.h"
extern "C"
bool abc_run (act_syn_info *s)
{
AbcApi *api;
char *sdc_file;
if (config_get_int("expropt.verbose") == 2) {
printf("running: built-in abc \n");
}
else if (config_get_int("expropt.verbose") == 1) {
printf(".");
fflush(stdout);
}
int len = strlen (s->v_in);
MALLOC (sdc_file, char, len+3);
snprintf (sdc_file, len + 3, "%s", s->v_in);
snprintf (sdc_file + len - 2, 5, ".sdc");
FILE *fp = fopen (sdc_file, "w");
if (!fp) {
fatal_error ("Could not open `%s' file!", sdc_file);
}
fprintf (fp, "set_load %g\n", config_get_real ("expropt.default_load"));
fclose (fp);
FREE (sdc_file);
api = (AbcApi *) s->space;
if (!api->startSession (s->v_in, s->v_out, s->toplevel)) {
fatal_error ("Unable to start ABC session!");
}
if (!api->stdSynthesis ()) {
fatal_error ("Unable to run logic synthesis using ABC api");
}
if (config_exists ("expropt.abc_use_constraints")) {
if (config_get_int ("expropt.abc_use_constraints") == 1) {
if (!api->runTiming()) {
fatal_error ("Unable to run timing");
}
}
}
if (!api->endSession ()) {
fatal_error ("Unable to end session with ABC");
}
return true;
}
static double parse_abc_info (const char *file, double *area)
{
char buf[10240];
FILE *fp;
double ret;
snprintf (buf, 10240, "%s.log", file);
fp = fopen (buf, "r");
if (!fp) {
return -1;
}
ret = -1;
if (area) {
*area = 0;
}
while (fgets (buf, 10240, fp)) {
char *tmp = strstr (buf, "Delay =");
if (tmp) {
if (sscanf (tmp, "Delay = %lf ps", &ret) == 1) {
ret = ret*1e-12;
}
}
// we need to parse cells for area!
else {
tmp = strstr (buf, "Fanin =");
if (tmp) {
char cellname[1000];
int inst;
double tot_area;
if (sscanf (buf, "%s Fanin = %*d Instance = %d Area = %lg",
cellname, &inst, &tot_area) == 3) {
// um^2
tot_area *= 1e-12;
if (area) {
*area += tot_area;
}
}
}
}
}
fclose (fp);
return ret;
}
extern "C"
double abc_get_metric (act_syn_info *s, expropt_metadata type)
{
if (type == metadata_area || type == metadata_delay_typ) {
double res, area;
res = parse_abc_info (s->v_out, &area);
if (type == metadata_area) {
if (!config_exists ("expropt.abc_use_constraints") ||
!(config_get_int ("expropt.abc_use_constraints") == 1)) {
return -1.0;
}
return area;
}
else {
return res;
}
}
else {
return 0.0;
}
}
extern "C"
void abc_cleanup (act_syn_info *s)
{
char *sdc_file;
int len;
char cmd[4096];
len = strlen (s->v_in);
MALLOC (sdc_file, char, len+3);
snprintf (sdc_file, len + 3, "%s", s->v_in);
snprintf (sdc_file + len - 2, 5, ".sdc");
snprintf(cmd, 4096, "rm %s && rm %s && rm %s && rm %s.* ",
s->v_out, s->v_in, sdc_file, s->v_out);
if (config_get_int("expropt.verbose") == 2) {
printf("running: %s \n", cmd);
}
else if (config_get_int("expropt.verbose") == 1) {
printf(".");
fflush(stdout);
}
system(cmd);
FREE (sdc_file);
}