yosys-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
AlumaccWorker Struct Reference
+ Collaboration diagram for AlumaccWorker:

Data Structures

struct  alunode_t
 
struct  maccnode_t
 

Public Member Functions

 AlumaccWorker (RTLIL::Module *module)
 
void count_bit_users ()
 
void extract_macc ()
 
void merge_macc ()
 
void macc_to_alu ()
 
void replace_macc ()
 
void extract_cmp_alu ()
 
void replace_alu ()
 
void run ()
 

Static Public Member Functions

static bool macc_may_overflow (Macc &macc, int width, bool is_signed)
 

Data Fields

RTLIL::Modulemodule
 
SigMap sigmap
 
std::map< RTLIL::SigBit, int > bit_users
 
std::map< RTLIL::SigSpec,
maccnode_t * > 
sig_macc
 
std::map< RTLIL::SigSig,
std::set< alunode_t * > > 
sig_alu
 
int macc_counter
 
int alu_counter
 

Detailed Description

Definition at line 27 of file alumacc.cc.

Constructor & Destructor Documentation

AlumaccWorker::AlumaccWorker ( RTLIL::Module module)
inline

Definition at line 106 of file alumacc.cc.

106  : module(module), sigmap(module)
107  {
108  macc_counter = 0;
109  alu_counter = 0;
110  }
RTLIL::Module * module
Definition: alumacc.cc:29
int alu_counter
Definition: alumacc.cc:104
int macc_counter
Definition: alumacc.cc:104
SigMap sigmap
Definition: alumacc.cc:30

Member Function Documentation

void AlumaccWorker::count_bit_users ( )
inline

Definition at line 112 of file alumacc.cc.

113  {
114  for (auto port : module->ports)
115  for (auto bit : sigmap(module->wire(port)))
116  bit_users[bit]++;
117 
118  for (auto cell : module->cells())
119  for (auto &conn : cell->connections())
120  for (auto bit : sigmap(conn.second))
121  bit_users[bit]++;
122  }
RTLIL::Module * module
Definition: alumacc.cc:29
RTLIL::Wire * wire(RTLIL::IdString id)
Definition: rtlil.h:637
SigMap sigmap
Definition: alumacc.cc:30
std::vector< RTLIL::IdString > ports
Definition: rtlil.h:617
std::map< RTLIL::SigBit, int > bit_users
Definition: alumacc.cc:101
RTLIL::ObjRange< RTLIL::Cell * > cells()
Definition: rtlil.h:641

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void AlumaccWorker::extract_cmp_alu ( )
inline

Definition at line 370 of file alumacc.cc.

371  {
372  std::vector<RTLIL::Cell*> lge_cells, eq_cells;
373 
374  for (auto cell : module->selected_cells())
375  {
376  if (cell->type.in("$lt", "$le", "$ge", "$gt"))
377  lge_cells.push_back(cell);
378  if (cell->type.in("$eq", "$eqx", "$ne", "$nex"))
379  eq_cells.push_back(cell);
380  }
381 
382  for (auto cell : lge_cells)
383  {
384  log(" creating $alu model for %s (%s):", log_id(cell), log_id(cell->type));
385 
386  bool cmp_less = cell->type.in("$lt", "$le");
387  bool cmp_equal = cell->type.in("$le", "$ge");
388  bool is_signed = cell->getParam("\\A_SIGNED").as_bool();
389 
390  RTLIL::SigSpec A = sigmap(cell->getPort("\\A"));
391  RTLIL::SigSpec B = sigmap(cell->getPort("\\B"));
392  RTLIL::SigSpec Y = sigmap(cell->getPort("\\Y"));
393 
394  if (B < A && GetSize(B)) {
395  cmp_less = !cmp_less;
396  std::swap(A, B);
397  }
398 
399  alunode_t *n = nullptr;
400 
401  for (auto node : sig_alu[RTLIL::SigSig(A, B)])
402  if (node->is_signed == is_signed && node->invert_b && node->c == RTLIL::S1) {
403  n = node;
404  break;
405  }
406 
407  if (n == nullptr) {
408  n = new alunode_t;
409  n->a = A;
410  n->b = B;
411  n->c = RTLIL::S1;
412  n->y = module->addWire(NEW_ID, std::max(GetSize(A), GetSize(B)));
413  n->is_signed = is_signed;
414  n->invert_b = true;
415  sig_alu[RTLIL::SigSig(A, B)].insert(n);
416  log(" new $alu\n");
417  } else {
418  log(" merged with %s.\n", log_id(n->cells.front()));
419  }
420 
421  n->cells.push_back(cell);
422  n->cmp.push_back(std::make_tuple(cmp_less, !cmp_less, cmp_equal, false, Y));
423  }
424 
425  for (auto cell : eq_cells)
426  {
427  bool cmp_equal = cell->type.in("$eq", "$eqx");
428  bool is_signed = cell->getParam("\\A_SIGNED").as_bool();
429 
430  RTLIL::SigSpec A = sigmap(cell->getPort("\\A"));
431  RTLIL::SigSpec B = sigmap(cell->getPort("\\B"));
432  RTLIL::SigSpec Y = sigmap(cell->getPort("\\Y"));
433 
434  if (B < A && GetSize(B))
435  std::swap(A, B);
436 
437  alunode_t *n = nullptr;
438 
439  for (auto node : sig_alu[RTLIL::SigSig(A, B)])
440  if (node->is_signed == is_signed && node->invert_b && node->c == RTLIL::S1) {
441  n = node;
442  break;
443  }
444 
445  if (n != nullptr) {
446  log(" creating $alu model for %s (%s): merged with %s.\n", log_id(cell), log_id(cell->type), log_id(n->cells.front()));
447  n->cells.push_back(cell);
448  n->cmp.push_back(std::make_tuple(false, false, cmp_equal, !cmp_equal, Y));
449  }
450  }
451  }
RTLIL::Module * module
Definition: alumacc.cc:29
tuple n
Definition: fsm/generate.py:59
SigMap sigmap
Definition: alumacc.cc:30
int GetSize(RTLIL::Wire *wire)
Definition: yosys.cc:334
RTLIL::Wire * addWire(RTLIL::IdString name, int width=1)
Definition: rtlil.cc:1331
#define NEW_ID
Definition: yosys.h:166
std::vector< RTLIL::Cell * > selected_cells() const
Definition: rtlil.cc:1103
std::map< RTLIL::SigSig, std::set< alunode_t * > > sig_alu
Definition: alumacc.cc:103
void log(const char *format,...)
Definition: log.cc:180
std::pair< SigSpec, SigSpec > SigSig
Definition: rtlil.h:71
const char * log_id(RTLIL::IdString str)
Definition: log.cc:283

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void AlumaccWorker::extract_macc ( )
inline

Definition at line 124 of file alumacc.cc.

125  {
126  for (auto cell : module->selected_cells())
127  {
128  if (!cell->type.in("$pos", "$neg", "$add", "$sub", "$mul"))
129  continue;
130 
131  log(" creating $macc model for %s (%s).\n", log_id(cell), log_id(cell->type));
132 
133  maccnode_t *n = new maccnode_t;
134  Macc::port_t new_port;
135 
136  n->cell = cell;
137  n->y = sigmap(cell->getPort("\\Y"));
138  n->users = 0;
139 
140  for (auto bit : n->y)
141  n->users = std::max(n->users, bit_users.at(bit) - 1);
142 
143  if (cell->type.in("$pos", "$neg"))
144  {
145  new_port.in_a = sigmap(cell->getPort("\\A"));
146  new_port.is_signed = cell->getParam("\\A_SIGNED").as_bool();
147  new_port.do_subtract = cell->type == "$neg";
148  n->macc.ports.push_back(new_port);
149  }
150 
151  if (cell->type.in("$add", "$sub"))
152  {
153  new_port.in_a = sigmap(cell->getPort("\\A"));
154  new_port.is_signed = cell->getParam("\\A_SIGNED").as_bool();
155  new_port.do_subtract = false;
156  n->macc.ports.push_back(new_port);
157 
158  new_port.in_a = sigmap(cell->getPort("\\B"));
159  new_port.is_signed = cell->getParam("\\B_SIGNED").as_bool();
160  new_port.do_subtract = cell->type == "$sub";
161  n->macc.ports.push_back(new_port);
162  }
163 
164  if (cell->type.in("$mul"))
165  {
166  new_port.in_a = sigmap(cell->getPort("\\A"));
167  new_port.in_b = sigmap(cell->getPort("\\B"));
168  new_port.is_signed = cell->getParam("\\A_SIGNED").as_bool();
169  new_port.do_subtract = false;
170  n->macc.ports.push_back(new_port);
171  }
172 
173  log_assert(sig_macc.count(n->y) == 0);
174  sig_macc[n->y] = n;
175  }
176  }
RTLIL::Module * module
Definition: alumacc.cc:29
tuple n
Definition: fsm/generate.py:59
std::map< RTLIL::SigSpec, maccnode_t * > sig_macc
Definition: alumacc.cc:102
RTLIL::SigSpec in_b
Definition: macc.h:30
SigMap sigmap
Definition: alumacc.cc:30
std::map< RTLIL::SigBit, int > bit_users
Definition: alumacc.cc:101
#define log_assert(_assert_expr_)
Definition: log.h:85
std::vector< RTLIL::Cell * > selected_cells() const
Definition: rtlil.cc:1103
bool is_signed
Definition: macc.h:31
RTLIL::SigSpec in_a
Definition: macc.h:30
void log(const char *format,...)
Definition: log.cc:180
bool do_subtract
Definition: macc.h:31
const char * log_id(RTLIL::IdString str)
Definition: log.cc:283

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static bool AlumaccWorker::macc_may_overflow ( Macc macc,
int  width,
bool  is_signed 
)
inlinestatic

Definition at line 178 of file alumacc.cc.

179  {
180  std::vector<int> port_sizes;
181 
182  for (auto &port : macc.ports) {
183  if (port.is_signed != is_signed)
184  return true;
185  if (!port.is_signed && port.do_subtract)
186  return true;
187  if (GetSize(port.in_b))
188  port_sizes.push_back(GetSize(port.in_a) + GetSize(port.in_b));
189  else
190  port_sizes.push_back(GetSize(port.in_a));
191  }
192 
193  std::sort(port_sizes.begin(), port_sizes.end());
194 
195  int acc_sum = 0, acc_shift = 0;
196  for (int sz : port_sizes) {
197  while ((sz - acc_shift) > 20) {
198  if (acc_sum & 1)
199  acc_sum++;
200  acc_sum = acc_sum >> 1;
201  acc_shift++;
202  }
203  acc_sum += (1 << (sz - acc_shift)) - 1;
204  }
205 
206  while (acc_sum) {
207  acc_sum = acc_sum >> 1;
208  acc_shift++;
209  }
210 
211  return acc_shift > width;
212  }
void sort(T *array, int size, LessThan lt)
Definition: Sort.h:57
std::vector< port_t > ports
Definition: macc.h:34
int GetSize(RTLIL::Wire *wire)
Definition: yosys.cc:334

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void AlumaccWorker::macc_to_alu ( )
inline

Definition at line 268 of file alumacc.cc.

269  {
270  std::set<maccnode_t*> delete_nodes;
271 
272  for (auto &it : sig_macc)
273  {
274  auto n = it.second;
275  RTLIL::SigSpec A, B, C = n->macc.bit_ports;
276  bool a_signed = false, b_signed = false;
277  bool subtract_b = false;
278  alunode_t *alunode;
279 
280  for (auto &port : n->macc.ports)
281  if (GetSize(port.in_b) > 0) {
282  goto next_macc;
283  } else if (GetSize(port.in_a) == 1 && !port.is_signed && !port.do_subtract) {
284  C.append(port.in_a);
285  } else if (GetSize(A) || port.do_subtract) {
286  if (GetSize(B))
287  goto next_macc;
288  B = port.in_a;
289  b_signed = port.is_signed;
290  subtract_b = port.do_subtract;
291  } else {
292  if (GetSize(A))
293  goto next_macc;
294  A = port.in_a;
295  a_signed = port.is_signed;
296  }
297 
298  if (!a_signed || !b_signed) {
299  if (GetSize(A) == GetSize(n->y))
300  a_signed = false;
301  if (GetSize(B) == GetSize(n->y))
302  b_signed = false;
303  if (a_signed != b_signed)
304  goto next_macc;
305  }
306 
307  if (GetSize(A) == 0 && GetSize(C) > 0) {
308  A = C[0];
309  C.remove(0);
310  }
311 
312  if (GetSize(B) == 0 && GetSize(C) > 0) {
313  B = C[0];
314  C.remove(0);
315  }
316 
317  if (subtract_b)
318  C.append(RTLIL::S1);
319 
320  if (GetSize(C) > 1)
321  goto next_macc;
322 
323  if (!subtract_b && B < A && GetSize(B))
324  std::swap(A, B);
325 
326  log(" creating $alu model for $macc %s.\n", log_id(n->cell));
327 
328  alunode = new alunode_t;
329  alunode->cells.push_back(n->cell);
330  alunode->is_signed = a_signed;
331  alunode->invert_b = subtract_b;
332 
333  alunode->a = A;
334  alunode->b = B;
335  alunode->c = C;
336  alunode->y = n->y;
337 
338  sig_alu[RTLIL::SigSig(A, B)].insert(alunode);
339  delete_nodes.insert(n);
340  next_macc:;
341  }
342 
343  for (auto n : delete_nodes) {
344  sig_macc.erase(n->y);
345  delete n;
346  }
347  }
void remove(const RTLIL::SigSpec &pattern)
Definition: rtlil.cc:2342
tuple n
Definition: fsm/generate.py:59
std::map< RTLIL::SigSpec, maccnode_t * > sig_macc
Definition: alumacc.cc:102
int GetSize(RTLIL::Wire *wire)
Definition: yosys.cc:334
std::map< RTLIL::SigSig, std::set< alunode_t * > > sig_alu
Definition: alumacc.cc:103
void log(const char *format,...)
Definition: log.cc:180
void append(const RTLIL::SigSpec &signal)
Definition: rtlil.cc:2523
std::pair< SigSpec, SigSpec > SigSig
Definition: rtlil.h:71
const char * log_id(RTLIL::IdString str)
Definition: log.cc:283

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void AlumaccWorker::merge_macc ( )
inline

Definition at line 214 of file alumacc.cc.

215  {
216  while (1)
217  {
218  std::set<maccnode_t*> delete_nodes;
219 
220  for (auto &it : sig_macc)
221  {
222  auto n = it.second;
223 
224  if (delete_nodes.count(n))
225  continue;
226 
227  for (int i = 0; i < GetSize(n->macc.ports); i++)
228  {
229  auto &port = n->macc.ports[i];
230 
231  if (GetSize(port.in_b) > 0 || sig_macc.count(port.in_a) == 0)
232  continue;
233 
234  auto other_n = sig_macc.at(port.in_a);
235 
236  if (other_n->users > 1)
237  continue;
238 
239  if (GetSize(other_n->y) != GetSize(n->y) && macc_may_overflow(other_n->macc, GetSize(other_n->y), port.is_signed))
240  continue;
241 
242  log(" merging $macc model for %s into %s.\n", log_id(other_n->cell), log_id(n->cell));
243 
244  bool do_subtract = port.do_subtract;
245  for (int j = 0; j < GetSize(other_n->macc.ports); j++) {
246  if (do_subtract)
247  other_n->macc.ports[j].do_subtract = !other_n->macc.ports[j].do_subtract;
248  if (j == 0)
249  n->macc.ports[i--] = other_n->macc.ports[j];
250  else
251  n->macc.ports.push_back(other_n->macc.ports[j]);
252  }
253 
254  delete_nodes.insert(other_n);
255  }
256  }
257 
258  if (delete_nodes.empty())
259  break;
260 
261  for (auto n : delete_nodes) {
262  sig_macc.erase(n->y);
263  delete n;
264  }
265  }
266  }
static bool macc_may_overflow(Macc &macc, int width, bool is_signed)
Definition: alumacc.cc:178
tuple n
Definition: fsm/generate.py:59
std::map< RTLIL::SigSpec, maccnode_t * > sig_macc
Definition: alumacc.cc:102
int GetSize(RTLIL::Wire *wire)
Definition: yosys.cc:334
void log(const char *format,...)
Definition: log.cc:180
const char * log_id(RTLIL::IdString str)
Definition: log.cc:283

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void AlumaccWorker::replace_alu ( )
inline

Definition at line 453 of file alumacc.cc.

454  {
455  for (auto &it1 : sig_alu)
456  for (auto n : it1.second)
457  {
458  if (GetSize(n->b) == 0 && GetSize(n->c) == 0 && GetSize(n->cmp) == 0)
459  {
460  n->alu_cell = module->addPos(NEW_ID, n->a, n->y, n->is_signed);
461 
462  log(" creating $pos cell for ");
463  for (int i = 0; i < GetSize(n->cells); i++)
464  log("%s%s", i ? ", ": "", log_id(n->cells[i]));
465  log(": %s\n", log_id(n->alu_cell));
466 
467  goto delete_node;
468  }
469 
470  n->alu_cell = module->addCell(NEW_ID, "$alu");
471  alu_counter++;
472 
473  log(" creating $alu cell for ");
474  for (int i = 0; i < GetSize(n->cells); i++)
475  log("%s%s", i ? ", ": "", log_id(n->cells[i]));
476  log(": %s\n", log_id(n->alu_cell));
477 
478  n->alu_cell->setPort("\\A", n->a);
479  n->alu_cell->setPort("\\B", n->b);
480  n->alu_cell->setPort("\\CI", GetSize(n->c) ? n->c : RTLIL::S0);
481  n->alu_cell->setPort("\\BI", n->invert_b ? RTLIL::S1 : RTLIL::S0);
482  n->alu_cell->setPort("\\Y", n->y);
483  n->alu_cell->setPort("\\X", module->addWire(NEW_ID, GetSize(n->y)));
484  n->alu_cell->setPort("\\CO", module->addWire(NEW_ID, GetSize(n->y)));
485  n->alu_cell->fixup_parameters(n->is_signed, n->is_signed);
486 
487  for (auto &it : n->cmp)
488  {
489  bool cmp_lt = std::get<0>(it);
490  bool cmp_gt = std::get<1>(it);
491  bool cmp_eq = std::get<2>(it);
492  bool cmp_ne = std::get<3>(it);
493  RTLIL::SigSpec cmp_y = std::get<4>(it);
494 
495  RTLIL::SigSpec sig;
496  if (cmp_lt) sig.append(n->get_lt());
497  if (cmp_gt) sig.append(n->get_gt());
498  if (cmp_eq) sig.append(n->get_eq());
499  if (cmp_ne) sig.append(n->get_ne());
500 
501  if (GetSize(sig) > 1)
502  sig = module->ReduceOr(NEW_ID, sig);
503 
504  sig.extend(GetSize(cmp_y));
505  module->connect(cmp_y, sig);
506  }
507 
508  delete_node:
509  for (auto c : n->cells)
510  module->remove(c);
511  delete n;
512  }
513 
514  sig_alu.clear();
515  }
RTLIL::Module * module
Definition: alumacc.cc:29
int alu_counter
Definition: alumacc.cc:104
RTLIL::Cell * addCell(RTLIL::IdString name, RTLIL::IdString type)
Definition: rtlil.cc:1353
tuple n
Definition: fsm/generate.py:59
void connect(const RTLIL::SigSig &conn)
Definition: rtlil.cc:1278
int GetSize(RTLIL::Wire *wire)
Definition: yosys.cc:334
RTLIL::Wire * addWire(RTLIL::IdString name, int width=1)
Definition: rtlil.cc:1331
#define NEW_ID
Definition: yosys.h:166
std::map< RTLIL::SigSig, std::set< alunode_t * > > sig_alu
Definition: alumacc.cc:103
void remove(const std::set< RTLIL::Wire * > &wires)
Definition: rtlil.cc:1158
RTLIL::SigSpec ReduceOr(RTLIL::IdString name, RTLIL::SigSpec sig_a, bool is_signed=false)
void log(const char *format,...)
Definition: log.cc:180
void append(const RTLIL::SigSpec &signal)
Definition: rtlil.cc:2523
void extend(int width, bool is_signed=false)
Definition: rtlil.cc:2593
RTLIL::Cell * addPos(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_y, bool is_signed=false)
const char * log_id(RTLIL::IdString str)
Definition: log.cc:283

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void AlumaccWorker::replace_macc ( )
inline

Definition at line 349 of file alumacc.cc.

350  {
351  for (auto &it : sig_macc)
352  {
353  auto n = it.second;
354  auto cell = module->addCell(NEW_ID, "$macc");
355  macc_counter++;
356 
357  log(" creating $macc cell for %s: %s\n", log_id(n->cell), log_id(cell));
358 
359  n->macc.optimize(GetSize(n->y));
360  n->macc.to_cell(cell);
361  cell->setPort("\\Y", n->y);
362  cell->fixup_parameters();
363  module->remove(n->cell);
364  delete n;
365  }
366 
367  sig_macc.clear();
368  }
RTLIL::Module * module
Definition: alumacc.cc:29
RTLIL::Cell * addCell(RTLIL::IdString name, RTLIL::IdString type)
Definition: rtlil.cc:1353
tuple n
Definition: fsm/generate.py:59
int macc_counter
Definition: alumacc.cc:104
std::map< RTLIL::SigSpec, maccnode_t * > sig_macc
Definition: alumacc.cc:102
int GetSize(RTLIL::Wire *wire)
Definition: yosys.cc:334
#define NEW_ID
Definition: yosys.h:166
void remove(const std::set< RTLIL::Wire * > &wires)
Definition: rtlil.cc:1158
void log(const char *format,...)
Definition: log.cc:180
const char * log_id(RTLIL::IdString str)
Definition: log.cc:283

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void AlumaccWorker::run ( )
inline

Definition at line 517 of file alumacc.cc.

518  {
519  log("Extracting $alu and $macc cells in module %s:\n", log_id(module));
520 
521  count_bit_users();
522  extract_macc();
523  merge_macc();
524  macc_to_alu();
525  replace_macc();
526  extract_cmp_alu();
527  replace_alu();
528 
529  log(" created %d $alu and %d $macc cells.\n", alu_counter, macc_counter);
530  }
RTLIL::Module * module
Definition: alumacc.cc:29
int alu_counter
Definition: alumacc.cc:104
void merge_macc()
Definition: alumacc.cc:214
void macc_to_alu()
Definition: alumacc.cc:268
int macc_counter
Definition: alumacc.cc:104
void count_bit_users()
Definition: alumacc.cc:112
void replace_macc()
Definition: alumacc.cc:349
void extract_macc()
Definition: alumacc.cc:124
void replace_alu()
Definition: alumacc.cc:453
void log(const char *format,...)
Definition: log.cc:180
void extract_cmp_alu()
Definition: alumacc.cc:370
const char * log_id(RTLIL::IdString str)
Definition: log.cc:283

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Field Documentation

int AlumaccWorker::alu_counter

Definition at line 104 of file alumacc.cc.

std::map<RTLIL::SigBit, int> AlumaccWorker::bit_users

Definition at line 101 of file alumacc.cc.

int AlumaccWorker::macc_counter

Definition at line 104 of file alumacc.cc.

RTLIL::Module* AlumaccWorker::module

Definition at line 29 of file alumacc.cc.

std::map<RTLIL::SigSig, std::set<alunode_t*> > AlumaccWorker::sig_alu

Definition at line 103 of file alumacc.cc.

std::map<RTLIL::SigSpec, maccnode_t*> AlumaccWorker::sig_macc

Definition at line 102 of file alumacc.cc.

SigMap AlumaccWorker::sigmap

Definition at line 30 of file alumacc.cc.


The documentation for this struct was generated from the following file: