File: sizer.cc

package info (click to toggle)
iverilog 12.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 30,148 kB
  • sloc: cpp: 109,972; ansic: 62,713; yacc: 10,216; sh: 3,470; vhdl: 3,246; perl: 1,814; makefile: 1,774; python: 78; csh: 2
file content (244 lines) | stat: -rw-r--r-- 8,483 bytes parent folder | download | duplicates (4)
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*
 * Copyright (c) 2014-2020 Stephen Williams (steve@icarus.com)
 *
 *    This source code is free software; you can redistribute it
 *    and/or modify it in source code form 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 "version_base.h"
# include "version_tag.h"
# include "config.h"
# include "sizer_priv.h"
# include <cstdio>
# include <cstring>
# include <cassert>

using namespace std;

/*
 * This is a null target module. It does nothing.
 */


static const char*version_string =
"Icarus Verilog SIZER Statistics Generator " VERSION " (" VERSION_TAG ")\n\n"
"Copyright (c) 2014-2020 Stephen Williams (steve@icarus.com)\n\n"
"  This program is free software; you can redistribute it and/or modify\n"
"  it under the terms of the GNU General Public License as published by\n"
"  the Free Software Foundation; either version 2 of the License, or\n"
"  (at your option) any later version.\n"
"\n"
"  This program is distributed in the hope that it will be useful,\n"
"  but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
"  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n"
"  GNU General Public License for more details.\n"
"\n"
"  You should have received a copy of the GNU General Public License along\n"
"  with this program; if not, write to the Free Software Foundation, Inc.,\n"
"  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.\n"
;

int sizer_errors = 0;

FILE*sizer_out = 0;

static int process_scan_fun(ivl_process_t net, void*raw);
static void emit_sizer_scope(ivl_design_t des, ivl_scope_t model, struct sizer_statistics&stats);
static void show_stats(struct sizer_statistics&stats);

/*
 * This is called by the ivl core to get version information from the
 * loadable code generator.
 */
const char* target_query(const char*key)
{
      if (strcmp(key,"version") == 0)
	    return version_string;

      return 0;
}

/*
 * This is the main entry point from the IVL core.
 */
int target_design(ivl_design_t des)
{
      const char*sizer_path = ivl_design_flag(des, "-o");

      sizer_out = fopen(sizer_path, "wt");
      assert(sizer_out);

	// Detect processes and dispatch them.
      ivl_design_process(des, &process_scan_fun, 0);

	// Locate the root scopes for the design.
      ivl_scope_t*roots;
      unsigned nroots;
      ivl_design_roots(des, &roots, &nroots);

	// Process all the root scopes. It is possible that there are
	// multiple root scopes, we will give isolated numbers for
	// each and keep then separate.
      for (unsigned idx = 0 ; idx < nroots ; idx += 1) {
	    if (ivl_scope_type(roots[idx]) != IVL_SCT_MODULE) {
		  fprintf(stderr, "SIZER: The root scope %s must be a module.\n", ivl_scope_basename(roots[idx]));
		  sizer_errors += 1;
		  continue;
	    }

	    struct sizer_statistics stats;
	    emit_sizer_scope(des, roots[idx], stats);

	    fprintf(sizer_out, "**** TOTALS\n");
	    show_stats(stats);
      }

      return sizer_errors;
}


/*
 * Processes are not collected into scopes, but we should not have any
 * left anyhow. Give error messages for all the processes that we find
 * to be remaining.
 */
static int process_scan_fun(ivl_process_t net, void* /*raw*/)
{
      for (unsigned idx = 0 ; idx < ivl_process_attr_cnt(net) ; idx += 1) {
	    ivl_attribute_t att = ivl_process_attr_val(net, idx);

	      // If synthesis is explicitly turned off for this
	      // process, then we just ignore it.
	    if (strcmp(att->key, "ivl_synthesis_off") == 0)
		  return 0;
      }

      fprintf(stderr, "%s:%u: SIZER: Processes not synthesized for statistics.\n",
	      ivl_process_file(net), ivl_process_lineno(net));
      sizer_errors += 1;
      return 0;
}

static void emit_sizer_scope(ivl_design_t des, ivl_scope_t scope, struct sizer_statistics&stats)
{

      fprintf(sizer_out, "**** module/scope: %s\n", ivl_scope_name(scope));

      scan_logs(scope, stats);
      scan_lpms(scope, stats);

      show_stats(stats);

      for (size_t idx = 0 ; idx < ivl_scope_childs(scope) ; idx += 1) {
	    ivl_scope_t child = ivl_scope_child(scope,idx);
	    struct sizer_statistics child_stats;
	    emit_sizer_scope(des, child, child_stats);
	    stats += child_stats;
      }
}

static void show_stats(struct sizer_statistics&stats)
{
      fprintf(sizer_out, "     Flip-Flops   : %u\n", stats.flop_count);
      fprintf(sizer_out, "     Logic Gates  : %u\n", stats.gate_count);

      for (map<unsigned,unsigned>::const_iterator cur = stats.adder_count.begin()
		 ; cur != stats.adder_count.end() ; ++ cur) {
	    fprintf(sizer_out, "     ADDER[%u]: %u units\n", cur->first, cur->second);
      }

      for (map<unsigned,unsigned>::const_iterator cur = stats.equality_count.begin()
		 ; cur != stats.equality_count.end() ; ++ cur) {
	    fprintf(sizer_out, "     EQUALITY[%u]: %u units\n", cur->first, cur->second);
      }

      for (map<unsigned,unsigned>::const_iterator cur = stats.equality_wc_count.begin()
		 ; cur != stats.equality_wc_count.end() ; ++ cur) {
	    fprintf(sizer_out, "     EQUALITY_WC[%u]: %u units\n", cur->first, cur->second);
      }

      for (map<unsigned,unsigned>::const_iterator cur = stats.magnitude_count.begin()
		 ; cur != stats.magnitude_count.end() ; ++ cur) {
	    fprintf(sizer_out, "     MAGNITUDE[%u]: %u units\n", cur->first, cur->second);
      }

      for (map<unsigned,unsigned>::const_iterator cur = stats.mux_count.begin()
		 ; cur != stats.mux_count.end() ; ++ cur) {
	    fprintf(sizer_out, "     MUX[%u]: %u slices\n", cur->first, cur->second);
      }

	// These are diagnostic outputs for when more detail is needed.
      for (map<ivl_lpm_type_t,unsigned>::const_iterator cur = stats.lpm_bytype.begin()
		 ; cur != stats.lpm_bytype.end() ; ++ cur) {
	    fprintf(sizer_out, "     LPM[%d]: %u unaccounted\n", cur->first, cur->second);
      }

      for (map<ivl_logic_t,unsigned>::const_iterator cur = stats.log_bytype.begin()
		 ; cur != stats.log_bytype.end() ; ++ cur) {
	    fprintf(sizer_out, "     LOG[%d]: %u unaccounted\n", cur->first, cur->second);
      }
}

unsigned get_nexus_width(ivl_nexus_t nex)
{
      for (unsigned idx = 0 ; idx < ivl_nexus_ptrs(nex) ; idx += 1) {
	    ivl_nexus_ptr_t ptr = ivl_nexus_ptr(nex,idx);
	    ivl_signal_t sig = ivl_nexus_ptr_sig(ptr);
	    if (sig) return ivl_signal_width(sig);
      }

      fprintf(stderr, "SIZER: Unable to find width of nexus?!\n");
      sizer_errors += 1;
      return 0;
}

struct sizer_statistics& sizer_statistics::operator += (const sizer_statistics&that)
{
      flop_count += that.flop_count;
      gate_count += that.gate_count;

      for (map<unsigned,unsigned>::const_iterator cur = that.adder_count.begin()
		 ; cur != that.adder_count.end() ; ++ cur)
	    adder_count[cur->first] += cur->second;

      for (map<unsigned,unsigned>::const_iterator cur = that.equality_count.begin()
		 ; cur != that.equality_count.end() ; ++ cur)
	    equality_count[cur->first] += cur->second;

      for (map<unsigned,unsigned>::const_iterator cur = that.equality_wc_count.begin()
		 ; cur != that.equality_wc_count.end() ; ++ cur)
	    equality_wc_count[cur->first] += cur->second;

      for (map<unsigned,unsigned>::const_iterator cur = that.magnitude_count.begin()
		 ; cur != that.magnitude_count.end() ; ++ cur)
	    magnitude_count[cur->first] += cur->second;


      for (map<unsigned,unsigned>::const_iterator cur = that.mux_count.begin()
		 ; cur != that.mux_count.end() ; ++ cur)
	    mux_count[cur->first] += cur->second;


      for (map<ivl_lpm_type_t,unsigned>::const_iterator cur = that.lpm_bytype.begin()
		 ; cur != that.lpm_bytype.end() ; ++ cur)
	    lpm_bytype[cur->first] += cur->second;


      for (map<ivl_logic_t,unsigned>::const_iterator cur = that.log_bytype.begin()
		 ; cur != that.log_bytype.end() ; ++ cur)
	    log_bytype[cur->first] += cur->second;

      return *this;
}