36f2fafc35abc9cd3ccceaa868e2a4483b7d1852
[yosys.git] / passes / techmap / abc9.cc
1 /*
2 * yosys -- Yosys Open SYnthesis Suite
3 *
4 * Copyright (C) 2012 Clifford Wolf <clifford@clifford.at>
5 * 2019 Eddie Hung <eddie@fpgeh.com>
6 *
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 *
19 */
20
21 // [[CITE]] ABC
22 // Berkeley Logic Synthesis and Verification Group, ABC: A System for Sequential Synthesis and Verification
23 // http://www.eecs.berkeley.edu/~alanmi/abc/
24
25 #if 0
26 // Based on &flow3 - better QoR but more experimental
27 #define ABC_COMMAND_LUT "&st; &ps -l; &sweep -v; &scorr; " \
28 "&st; &if {W}; &save; &st; &syn2; &if {W} -v; &save; &load; "\
29 "&st; &if -g -K 6; &dch -f; &if {W} -v; &save; &load; "\
30 "&st; &if -g -K 6; &synch2; &if {W} -v; &save; &load; "\
31 "&mfs; &ps -l"
32 #else
33 #define ABC_COMMAND_LUT "&st; &scorr; &sweep; &dc2; &st; &dch -f; &ps; &if {W} {D} -v; &mfs; &ps -l"
34 #endif
35
36
37 #define ABC_FAST_COMMAND_LUT "&st; &if {W} {D}"
38
39 #include "kernel/register.h"
40 #include "kernel/sigtools.h"
41 #include "kernel/celltypes.h"
42 #include "kernel/cost.h"
43 #include "kernel/log.h"
44 #include <stdlib.h>
45 #include <stdio.h>
46 #include <string.h>
47 #include <cerrno>
48 #include <sstream>
49 #include <climits>
50
51 #ifndef _WIN32
52 # include <unistd.h>
53 # include <dirent.h>
54 #endif
55
56 #include "frontends/aiger/aigerparse.h"
57 #include "kernel/utils.h"
58
59 #ifdef YOSYS_LINK_ABC
60 extern "C" int Abc_RealMain(int argc, char *argv[]);
61 #endif
62
63 USING_YOSYS_NAMESPACE
64 PRIVATE_NAMESPACE_BEGIN
65
66 bool markgroups;
67 int map_autoidx;
68 SigMap assign_map;
69 RTLIL::Module *module;
70
71 bool clk_polarity, en_polarity;
72 RTLIL::SigSpec clk_sig, en_sig;
73
74 std::string remap_name(RTLIL::IdString abc_name)
75 {
76 std::stringstream sstr;
77 sstr << "$abc$" << map_autoidx << "$" << abc_name.substr(1);
78 return sstr.str();
79 }
80
81 void handle_loops(RTLIL::Design *design)
82 {
83 Pass::call(design, "scc -set_attr abc_scc_id {}");
84
85 dict<IdString, vector<IdString>> abc_scc_break;
86
87 // For every unique SCC found, (arbitrarily) find the first
88 // cell in the component, and select (and mark) all its output
89 // wires
90 pool<RTLIL::Const> ids_seen;
91 for (auto cell : module->cells()) {
92 auto it = cell->attributes.find("\\abc_scc_id");
93 if (it != cell->attributes.end()) {
94 auto r = ids_seen.insert(it->second);
95 if (r.second) {
96 for (auto &c : cell->connections_) {
97 if (c.second.is_fully_const()) continue;
98 if (cell->output(c.first)) {
99 SigBit b = c.second.as_bit();
100 Wire *w = b.wire;
101 log_assert(!w->port_input);
102 w->port_input = true;
103 w = module->wire(stringf("%s.abci", w->name.c_str()));
104 if (!w) {
105 w = module->addWire(stringf("%s.abci", b.wire->name.c_str()), GetSize(b.wire));
106 w->port_output = true;
107 }
108 else {
109 log_assert(w->port_input);
110 log_assert(b.offset < GetSize(w));
111 }
112 w->set_bool_attribute("\\abc_scc_break");
113 module->swap_names(b.wire, w);
114 c.second = RTLIL::SigBit(w, b.offset);
115 }
116 }
117 }
118 cell->attributes.erase(it);
119 }
120
121 auto jt = abc_scc_break.find(cell->type);
122 if (jt == abc_scc_break.end()) {
123 std::vector<IdString> ports;
124 RTLIL::Module* box_module = design->module(cell->type);
125 if (box_module) {
126 auto ports_csv = box_module->attributes.at("\\abc_scc_break", RTLIL::Const::from_string("")).decode_string();
127 for (const auto &port_name : split_tokens(ports_csv, ",")) {
128 auto port_id = RTLIL::escape_id(port_name);
129 auto kt = cell->connections_.find(port_id);
130 if (kt == cell->connections_.end())
131 log_error("abc_scc_break attribute value '%s' does not exist as port on module '%s'\n", port_name.c_str(), log_id(box_module));
132 ports.push_back(port_id);
133 }
134 }
135 jt = abc_scc_break.insert(std::make_pair(cell->type, std::move(ports))).first;
136 }
137
138 for (auto port_name : jt->second) {
139 RTLIL::SigSpec sig;
140 auto &rhs = cell->connections_.at(port_name);
141 for (auto b : rhs) {
142 Wire *w = b.wire;
143 if (!w) continue;
144 w->port_output = true;
145 w->set_bool_attribute("\\abc_scc_break");
146 w = module->wire(stringf("%s.abci", w->name.c_str()));
147 if (!w) {
148 w = module->addWire(stringf("%s.abci", b.wire->name.c_str()), GetSize(b.wire));
149 w->port_input = true;
150 }
151 else {
152 log_assert(b.offset < GetSize(w));
153 log_assert(w->port_input);
154 }
155 sig.append(RTLIL::SigBit(w, b.offset));
156 }
157 rhs = sig;
158 }
159 }
160
161 module->fixup_ports();
162 }
163
164 std::string add_echos_to_abc_cmd(std::string str)
165 {
166 std::string new_str, token;
167 for (size_t i = 0; i < str.size(); i++) {
168 token += str[i];
169 if (str[i] == ';') {
170 while (i+1 < str.size() && str[i+1] == ' ')
171 i++;
172 new_str += "echo + " + token + " " + token + " ";
173 token.clear();
174 }
175 }
176
177 if (!token.empty()) {
178 if (!new_str.empty())
179 new_str += "echo + " + token + "; ";
180 new_str += token;
181 }
182
183 return new_str;
184 }
185
186 std::string fold_abc_cmd(std::string str)
187 {
188 std::string token, new_str = " ";
189 int char_counter = 10;
190
191 for (size_t i = 0; i <= str.size(); i++) {
192 if (i < str.size())
193 token += str[i];
194 if (i == str.size() || str[i] == ';') {
195 if (char_counter + token.size() > 75)
196 new_str += "\n ", char_counter = 14;
197 new_str += token, char_counter += token.size();
198 token.clear();
199 }
200 }
201
202 return new_str;
203 }
204
205 std::string replace_tempdir(std::string text, std::string tempdir_name, bool show_tempdir)
206 {
207 if (show_tempdir)
208 return text;
209
210 while (1) {
211 size_t pos = text.find(tempdir_name);
212 if (pos == std::string::npos)
213 break;
214 text = text.substr(0, pos) + "<abc-temp-dir>" + text.substr(pos + GetSize(tempdir_name));
215 }
216
217 std::string selfdir_name = proc_self_dirname();
218 if (selfdir_name != "/") {
219 while (1) {
220 size_t pos = text.find(selfdir_name);
221 if (pos == std::string::npos)
222 break;
223 text = text.substr(0, pos) + "<yosys-exe-dir>/" + text.substr(pos + GetSize(selfdir_name));
224 }
225 }
226
227 return text;
228 }
229
230 struct abc_output_filter
231 {
232 bool got_cr;
233 int escape_seq_state;
234 std::string linebuf;
235 std::string tempdir_name;
236 bool show_tempdir;
237
238 abc_output_filter(std::string tempdir_name, bool show_tempdir) : tempdir_name(tempdir_name), show_tempdir(show_tempdir)
239 {
240 got_cr = false;
241 escape_seq_state = 0;
242 }
243
244 void next_char(char ch)
245 {
246 if (escape_seq_state == 0 && ch == '\033') {
247 escape_seq_state = 1;
248 return;
249 }
250 if (escape_seq_state == 1) {
251 escape_seq_state = ch == '[' ? 2 : 0;
252 return;
253 }
254 if (escape_seq_state == 2) {
255 if ((ch < '0' || '9' < ch) && ch != ';')
256 escape_seq_state = 0;
257 return;
258 }
259 escape_seq_state = 0;
260 if (ch == '\r') {
261 got_cr = true;
262 return;
263 }
264 if (ch == '\n') {
265 log("ABC: %s\n", replace_tempdir(linebuf, tempdir_name, show_tempdir).c_str());
266 got_cr = false, linebuf.clear();
267 return;
268 }
269 if (got_cr)
270 got_cr = false, linebuf.clear();
271 linebuf += ch;
272 }
273
274 void next_line(const std::string &line)
275 {
276 //int pi, po;
277 //if (sscanf(line.c_str(), "Start-point = pi%d. End-point = po%d.", &pi, &po) == 2) {
278 // log("ABC: Start-point = pi%d (%s). End-point = po%d (%s).\n",
279 // pi, pi_map.count(pi) ? pi_map.at(pi).c_str() : "???",
280 // po, po_map.count(po) ? po_map.at(po).c_str() : "???");
281 // return;
282 //}
283
284 for (char ch : line)
285 next_char(ch);
286 }
287 };
288
289 void abc9_module(RTLIL::Design *design, RTLIL::Module *current_module, std::string script_file, std::string exe_file,
290 bool cleanup, vector<int> lut_costs, bool dff_mode, std::string clk_str,
291 bool /*keepff*/, std::string delay_target, std::string /*lutin_shared*/, bool fast_mode,
292 bool show_tempdir, std::string box_file, std::string lut_file,
293 std::string wire_delay)
294 {
295 module = current_module;
296 map_autoidx = autoidx++;
297
298 if (clk_str != "$")
299 {
300 clk_polarity = true;
301 clk_sig = RTLIL::SigSpec();
302
303 en_polarity = true;
304 en_sig = RTLIL::SigSpec();
305 }
306
307 if (!clk_str.empty() && clk_str != "$")
308 {
309 if (clk_str.find(',') != std::string::npos) {
310 int pos = clk_str.find(',');
311 std::string en_str = clk_str.substr(pos+1);
312 clk_str = clk_str.substr(0, pos);
313 if (en_str[0] == '!') {
314 en_polarity = false;
315 en_str = en_str.substr(1);
316 }
317 if (module->wires_.count(RTLIL::escape_id(en_str)) != 0)
318 en_sig = assign_map(RTLIL::SigSpec(module->wires_.at(RTLIL::escape_id(en_str)), 0));
319 }
320 if (clk_str[0] == '!') {
321 clk_polarity = false;
322 clk_str = clk_str.substr(1);
323 }
324 if (module->wires_.count(RTLIL::escape_id(clk_str)) != 0)
325 clk_sig = assign_map(RTLIL::SigSpec(module->wires_.at(RTLIL::escape_id(clk_str)), 0));
326 }
327
328 if (dff_mode && clk_sig.empty())
329 log_cmd_error("Clock domain %s not found.\n", clk_str.c_str());
330
331 std::string tempdir_name = "/tmp/yosys-abc-XXXXXX";
332 if (!cleanup)
333 tempdir_name[0] = tempdir_name[4] = '_';
334 tempdir_name = make_temp_dir(tempdir_name);
335 log_header(design, "Extracting gate netlist of module `%s' to `%s/input.xaig'..\n",
336 module->name.c_str(), replace_tempdir(tempdir_name, tempdir_name, show_tempdir).c_str());
337
338 std::string abc_script;
339
340 if (!lut_costs.empty()) {
341 abc_script += stringf("read_lut %s/lutdefs.txt; ", tempdir_name.c_str());
342 if (!box_file.empty())
343 abc_script += stringf("read_box -v %s; ", box_file.c_str());
344 }
345 else
346 if (!lut_file.empty()) {
347 abc_script += stringf("read_lut %s; ", lut_file.c_str());
348 if (!box_file.empty())
349 abc_script += stringf("read_box -v %s; ", box_file.c_str());
350 }
351 else
352 log_abort();
353
354 abc_script += stringf("&read %s/input.xaig; &ps; ", tempdir_name.c_str());
355
356 if (!script_file.empty()) {
357 if (script_file[0] == '+') {
358 for (size_t i = 1; i < script_file.size(); i++)
359 if (script_file[i] == '\'')
360 abc_script += "'\\''";
361 else if (script_file[i] == ',')
362 abc_script += " ";
363 else
364 abc_script += script_file[i];
365 } else
366 abc_script += stringf("source %s", script_file.c_str());
367 } else if (!lut_costs.empty() || !lut_file.empty()) {
368 //bool all_luts_cost_same = true;
369 //for (int this_cost : lut_costs)
370 // if (this_cost != lut_costs.front())
371 // all_luts_cost_same = false;
372 abc_script += fast_mode ? ABC_FAST_COMMAND_LUT : ABC_COMMAND_LUT;
373 //if (all_luts_cost_same && !fast_mode)
374 // abc_script += "; lutpack {S}";
375 } else
376 log_abort();
377
378 //if (script_file.empty() && !delay_target.empty())
379 // for (size_t pos = abc_script.find("dretime;"); pos != std::string::npos; pos = abc_script.find("dretime;", pos+1))
380 // abc_script = abc_script.substr(0, pos) + "dretime; retime -o {D};" + abc_script.substr(pos+8);
381
382 for (size_t pos = abc_script.find("{D}"); pos != std::string::npos; pos = abc_script.find("{D}", pos))
383 abc_script = abc_script.substr(0, pos) + delay_target + abc_script.substr(pos+3);
384
385 //for (size_t pos = abc_script.find("{S}"); pos != std::string::npos; pos = abc_script.find("{S}", pos))
386 // abc_script = abc_script.substr(0, pos) + lutin_shared + abc_script.substr(pos+3);
387
388 for (size_t pos = abc_script.find("{W}"); pos != std::string::npos; pos = abc_script.find("{W}", pos))
389 abc_script = abc_script.substr(0, pos) + wire_delay + abc_script.substr(pos+3);
390
391 abc_script += stringf("; &write %s/output.aig", tempdir_name.c_str());
392 abc_script = add_echos_to_abc_cmd(abc_script);
393
394 for (size_t i = 0; i+1 < abc_script.size(); i++)
395 if (abc_script[i] == ';' && abc_script[i+1] == ' ')
396 abc_script[i+1] = '\n';
397
398 FILE *f = fopen(stringf("%s/abc.script", tempdir_name.c_str()).c_str(), "wt");
399 fprintf(f, "%s\n", abc_script.c_str());
400 fclose(f);
401
402 if (dff_mode || !clk_str.empty())
403 {
404 if (clk_sig.size() == 0)
405 log("No%s clock domain found. Not extracting any FF cells.\n", clk_str.empty() ? "" : " matching");
406 else {
407 log("Found%s %s clock domain: %s", clk_str.empty() ? "" : " matching", clk_polarity ? "posedge" : "negedge", log_signal(clk_sig));
408 if (en_sig.size() != 0)
409 log(", enabled by %s%s", en_polarity ? "" : "!", log_signal(en_sig));
410 log("\n");
411 }
412 }
413
414 bool count_output = false;
415 for (auto port_name : module->ports) {
416 RTLIL::Wire *port_wire = module->wire(port_name);
417 log_assert(port_wire);
418 if (port_wire->port_output) {
419 count_output = true;
420 break;
421 }
422 }
423
424 log_push();
425
426 if (count_output)
427 {
428 design->selection_stack.emplace_back(false);
429 RTLIL::Selection& sel = design->selection_stack.back();
430 sel.select(module);
431
432 Pass::call(design, "aigmap");
433
434 handle_loops(design);
435
436 //log("Extracted %d gates and %d wires to a netlist network with %d inputs and %d outputs.\n",
437 // count_gates, GetSize(signal_list), count_input, count_output);
438
439 Pass::call(design, stringf("write_xaiger -map %s/input.sym %s/input.xaig", tempdir_name.c_str(), tempdir_name.c_str()));
440
441 std::string buffer;
442 std::ifstream ifs;
443 #if 0
444 buffer = stringf("%s/%s", tempdir_name.c_str(), "input.xaig");
445 ifs.open(buffer);
446 if (ifs.fail())
447 log_error("Can't open ABC output file `%s'.\n", buffer.c_str());
448 buffer = stringf("%s/%s", tempdir_name.c_str(), "input.sym");
449 log_assert(!design->module("$__abc9__"));
450 {
451 AigerReader reader(design, ifs, "$__abc9__", "" /* clk_name */, buffer.c_str() /* map_filename */, true /* wideports */);
452 reader.parse_xaiger();
453 }
454 ifs.close();
455 Pass::call(design, stringf("write_verilog -noexpr -norename"));
456 design->remove(design->module("$__abc9__"));
457 #endif
458
459 design->selection_stack.pop_back();
460
461 // Now 'unexpose' those wires by undoing
462 // the expose operation -- remove them from PO/PI
463 // and re-connecting them back together
464 for (auto wire : module->wires()) {
465 auto it = wire->attributes.find("\\abc_scc_break");
466 if (it != wire->attributes.end()) {
467 wire->attributes.erase(it);
468 log_assert(wire->port_output);
469 wire->port_output = false;
470 RTLIL::Wire *i_wire = module->wire(wire->name.str() + ".abci");
471 log_assert(i_wire);
472 log_assert(i_wire->port_input);
473 i_wire->port_input = false;
474 module->connect(i_wire, wire);
475 }
476 }
477 module->fixup_ports();
478
479
480 log_header(design, "Executing ABC9.\n");
481
482 if (!lut_costs.empty()) {
483 buffer = stringf("%s/lutdefs.txt", tempdir_name.c_str());
484 f = fopen(buffer.c_str(), "wt");
485 if (f == NULL)
486 log_error("Opening %s for writing failed: %s\n", buffer.c_str(), strerror(errno));
487 for (int i = 0; i < GetSize(lut_costs); i++)
488 fprintf(f, "%d %d.00 1.00\n", i+1, lut_costs.at(i));
489 fclose(f);
490 }
491
492 buffer = stringf("%s -s -f %s/abc.script 2>&1", exe_file.c_str(), tempdir_name.c_str());
493 log("Running ABC command: %s\n", replace_tempdir(buffer, tempdir_name, show_tempdir).c_str());
494
495 #ifndef YOSYS_LINK_ABC
496 abc_output_filter filt(tempdir_name, show_tempdir);
497 int ret = run_command(buffer, std::bind(&abc_output_filter::next_line, filt, std::placeholders::_1));
498 #else
499 // These needs to be mutable, supposedly due to getopt
500 char *abc_argv[5];
501 string tmp_script_name = stringf("%s/abc.script", tempdir_name.c_str());
502 abc_argv[0] = strdup(exe_file.c_str());
503 abc_argv[1] = strdup("-s");
504 abc_argv[2] = strdup("-f");
505 abc_argv[3] = strdup(tmp_script_name.c_str());
506 abc_argv[4] = 0;
507 int ret = Abc_RealMain(4, abc_argv);
508 free(abc_argv[0]);
509 free(abc_argv[1]);
510 free(abc_argv[2]);
511 free(abc_argv[3]);
512 #endif
513 if (ret != 0)
514 log_error("ABC: execution of command \"%s\" failed: return code %d.\n", buffer.c_str(), ret);
515
516 buffer = stringf("%s/%s", tempdir_name.c_str(), "output.aig");
517 ifs.open(buffer);
518 if (ifs.fail())
519 log_error("Can't open ABC output file `%s'.\n", buffer.c_str());
520
521 buffer = stringf("%s/%s", tempdir_name.c_str(), "input.sym");
522 log_assert(!design->module("$__abc9__"));
523 AigerReader reader(design, ifs, "$__abc9__", "" /* clk_name */, buffer.c_str() /* map_filename */, true /* wideports */);
524 reader.parse_xaiger();
525 ifs.close();
526
527 #if 0
528 Pass::call(design, stringf("write_verilog -noexpr -norename"));
529 #endif
530
531 log_header(design, "Re-integrating ABC9 results.\n");
532 RTLIL::Module *mapped_mod = design->module("$__abc9__");
533 if (mapped_mod == NULL)
534 log_error("ABC output file does not contain a module `$__abc9__'.\n");
535
536 pool<RTLIL::SigBit> output_bits;
537 for (auto &it : mapped_mod->wires_) {
538 RTLIL::Wire *w = it.second;
539 RTLIL::Wire *remap_wire = module->addWire(remap_name(w->name), GetSize(w));
540 if (markgroups) remap_wire->attributes["\\abcgroup"] = map_autoidx;
541 if (w->port_output) {
542 RTLIL::Wire *wire = module->wire(w->name);
543 log_assert(wire);
544 for (int i = 0; i < GetSize(w); i++)
545 output_bits.insert({wire, i});
546 }
547 }
548
549 for (auto &it : module->connections_) {
550 auto &signal = it.first;
551 auto bits = signal.bits();
552 for (auto &b : bits)
553 if (output_bits.count(b))
554 b = module->addWire(NEW_ID);
555 signal = std::move(bits);
556 }
557
558 dict<IdString, bool> abc_box;
559 vector<RTLIL::Cell*> boxes;
560 for (const auto &it : module->cells_) {
561 auto cell = it.second;
562 if (cell->type.in("$_AND_", "$_NOT_")) {
563 module->remove(cell);
564 continue;
565 }
566 auto jt = abc_box.find(cell->type);
567 if (jt == abc_box.end()) {
568 RTLIL::Module* box_module = design->module(cell->type);
569 jt = abc_box.insert(std::make_pair(cell->type, box_module && box_module->attributes.count("\\abc_box_id"))).first;
570 }
571 if (jt->second)
572 boxes.emplace_back(cell);
573 }
574
575 dict<SigBit, pool<IdString>> bit_drivers, bit_users;
576 TopoSort<IdString, RTLIL::sort_by_id_str> toposort;
577 dict<RTLIL::Cell*,RTLIL::Cell*> not2drivers;
578 dict<SigBit, std::vector<RTLIL::Cell*>> bit2sinks;
579
580 std::map<std::string, int> cell_stats;
581 for (auto c : mapped_mod->cells())
582 {
583 toposort.node(c->name);
584
585 RTLIL::Cell *cell = nullptr;
586 if (c->type == "$_NOT_") {
587 RTLIL::SigBit a_bit = c->getPort("\\A");
588 RTLIL::SigBit y_bit = c->getPort("\\Y");
589 bit_users[a_bit].insert(c->name);
590 bit_drivers[y_bit].insert(c->name);
591
592 if (!a_bit.wire) {
593 c->setPort("\\Y", module->addWire(NEW_ID));
594 RTLIL::Wire *wire = module->wire(remap_name(y_bit.wire->name));
595 log_assert(wire);
596 module->connect(RTLIL::SigBit(wire, y_bit.offset), RTLIL::S1);
597 }
598 else if (!lut_costs.empty() || !lut_file.empty()) {
599 RTLIL::Cell* driver_lut = nullptr;
600 // ABC can return NOT gates that drive POs
601 if (!a_bit.wire->port_input) {
602 // If it's not a NOT gate that that comes from a PI directly,
603 // find the driver LUT and clone that to guarantee that we won't
604 // increase the max logic depth
605 // (TODO: Optimise by not cloning unless will increase depth)
606 RTLIL::IdString driver_name;
607 if (GetSize(a_bit.wire) == 1)
608 driver_name = stringf("%s$lut", a_bit.wire->name.c_str());
609 else
610 driver_name = stringf("%s[%d]$lut", a_bit.wire->name.c_str(), a_bit.offset);
611 driver_lut = mapped_mod->cell(driver_name);
612 }
613
614 if (!driver_lut) {
615 // If a driver couldn't be found (could be from PI or box CI)
616 // then implement using a LUT
617 cell = module->addLut(remap_name(stringf("%s$lut", c->name.c_str())),
618 RTLIL::SigBit(module->wires_.at(remap_name(a_bit.wire->name)), a_bit.offset),
619 RTLIL::SigBit(module->wires_.at(remap_name(y_bit.wire->name)), y_bit.offset),
620 RTLIL::Const::from_string("01"));
621 bit2sinks[cell->getPort("\\A")].push_back(cell);
622 cell_stats["$lut"]++;
623 }
624 else
625 not2drivers[c] = driver_lut;
626 continue;
627 }
628 else
629 log_abort();
630 if (cell && markgroups) cell->attributes["\\abcgroup"] = map_autoidx;
631 continue;
632 }
633 cell_stats[RTLIL::unescape_id(c->type)]++;
634
635 RTLIL::Cell *existing_cell = nullptr;
636 if (c->type == "$lut") {
637 if (GetSize(c->getPort("\\A")) == 1 && c->getParam("\\LUT") == RTLIL::Const::from_string("01")) {
638 SigSpec my_a = module->wires_.at(remap_name(c->getPort("\\A").as_wire()->name));
639 SigSpec my_y = module->wires_.at(remap_name(c->getPort("\\Y").as_wire()->name));
640 module->connect(my_y, my_a);
641 if (markgroups) c->attributes["\\abcgroup"] = map_autoidx;
642 log_abort();
643 continue;
644 }
645 cell = module->addCell(remap_name(c->name), c->type);
646 }
647 else {
648 existing_cell = module->cell(c->name);
649 cell = module->addCell(remap_name(c->name), c->type);
650 module->swap_names(cell, existing_cell);
651 }
652
653 if (markgroups) cell->attributes["\\abcgroup"] = map_autoidx;
654 if (existing_cell) {
655 cell->parameters = existing_cell->parameters;
656 cell->attributes = existing_cell->attributes;
657 }
658 else {
659 cell->parameters = c->parameters;
660 cell->attributes = c->attributes;
661 }
662 for (auto &conn : c->connections()) {
663 RTLIL::SigSpec newsig;
664 for (auto c : conn.second.chunks()) {
665 if (c.width == 0)
666 continue;
667 //log_assert(c.width == 1);
668 if (c.wire)
669 c.wire = module->wires_.at(remap_name(c.wire->name));
670 newsig.append(c);
671 }
672 cell->setPort(conn.first, newsig);
673
674 if (cell->input(conn.first)) {
675 for (auto i : newsig)
676 bit2sinks[i].push_back(cell);
677 for (auto i : conn.second)
678 bit_users[i].insert(c->name);
679 }
680 if (cell->output(conn.first))
681 for (auto i : conn.second)
682 bit_drivers[i].insert(c->name);
683 }
684 }
685
686 for (auto cell : boxes)
687 module->remove(cell);
688
689 // Copy connections (and rename) from mapped_mod to module
690 for (auto conn : mapped_mod->connections()) {
691 if (!conn.first.is_fully_const()) {
692 auto chunks = conn.first.chunks();
693 for (auto &c : chunks)
694 c.wire = module->wires_.at(remap_name(c.wire->name));
695 conn.first = std::move(chunks);
696 }
697 if (!conn.second.is_fully_const()) {
698 auto chunks = conn.second.chunks();
699 for (auto &c : chunks)
700 if (c.wire)
701 c.wire = module->wires_.at(remap_name(c.wire->name));
702 conn.second = std::move(chunks);
703 }
704 module->connect(conn);
705 }
706
707 for (auto &it : cell_stats)
708 log("ABC RESULTS: %15s cells: %8d\n", it.first.c_str(), it.second);
709 int in_wires = 0, out_wires = 0;
710
711 // Stitch in mapped_mod's inputs/outputs into module
712 for (auto &it : mapped_mod->wires_) {
713 RTLIL::Wire *w = it.second;
714 if (!w->port_input && !w->port_output)
715 continue;
716 RTLIL::Wire *wire = module->wire(w->name);
717 log_assert(wire);
718 RTLIL::Wire *remap_wire = module->wire(remap_name(w->name));
719 RTLIL::SigSpec signal = RTLIL::SigSpec(wire, 0, GetSize(remap_wire));
720 log_assert(GetSize(signal) >= GetSize(remap_wire));
721
722 log_assert(w->port_input || w->port_output);
723 RTLIL::SigSig conn;
724 if (w->port_input) {
725 conn.first = remap_wire;
726 conn.second = signal;
727 in_wires++;
728 module->connect(conn);
729 }
730 if (w->port_output) {
731 conn.first = signal;
732 conn.second = remap_wire;
733 out_wires++;
734 module->connect(conn);
735 }
736 }
737
738 for (auto &it : bit_users)
739 if (bit_drivers.count(it.first))
740 for (auto driver_cell : bit_drivers.at(it.first))
741 for (auto user_cell : it.second)
742 toposort.edge(driver_cell, user_cell);
743 bool no_loops = toposort.sort();
744 log_assert(no_loops);
745
746 for (auto ii = toposort.sorted.rbegin(); ii != toposort.sorted.rend(); ii++) {
747 RTLIL::Cell *not_cell = mapped_mod->cell(*ii);
748 log_assert(not_cell);
749 if (not_cell->type != "$_NOT_")
750 continue;
751 auto it = not2drivers.find(not_cell);
752 if (it == not2drivers.end())
753 continue;
754 RTLIL::Cell *driver_lut = it->second;
755 RTLIL::SigBit a_bit = not_cell->getPort("\\A");
756 RTLIL::SigBit y_bit = not_cell->getPort("\\Y");
757 RTLIL::Const driver_mask;
758
759 a_bit.wire = module->wires_.at(remap_name(a_bit.wire->name));
760 y_bit.wire = module->wires_.at(remap_name(y_bit.wire->name));
761
762 auto jt = bit2sinks.find(a_bit);
763 if (jt == bit2sinks.end())
764 goto duplicate_lut;
765
766 for (auto sink_cell : jt->second)
767 if (sink_cell->type != "$lut")
768 goto duplicate_lut;
769
770 // Push downstream LUTs past inverter
771 for (auto sink_cell : jt->second) {
772 SigSpec A = sink_cell->getPort("\\A");
773 RTLIL::Const mask = sink_cell->getParam("\\LUT");
774 int index = 0;
775 for (; index < GetSize(A); index++)
776 if (A[index] == a_bit)
777 break;
778 log_assert(index < GetSize(A));
779 int i = 0;
780 while (i < GetSize(mask)) {
781 for (int j = 0; j < (1 << index); j++)
782 std::swap(mask[i+j], mask[i+j+(1 << index)]);
783 i += 1 << (index+1);
784 }
785 A[index] = y_bit;
786 sink_cell->setPort("\\A", A);
787 sink_cell->setParam("\\LUT", mask);
788 }
789
790 duplicate_lut:
791 driver_mask = driver_lut->getParam("\\LUT");
792 for (auto &b : driver_mask.bits) {
793 if (b == RTLIL::State::S0) b = RTLIL::State::S1;
794 else if (b == RTLIL::State::S1) b = RTLIL::State::S0;
795 }
796 auto cell = module->addLut(NEW_ID,
797 driver_lut->getPort("\\A"),
798 y_bit,
799 driver_mask);
800 for (auto &bit : cell->connections_.at("\\A")) {
801 bit.wire = module->wires_.at(remap_name(bit.wire->name));
802 bit2sinks[bit].push_back(cell);
803 }
804 }
805
806 //log("ABC RESULTS: internal signals: %8d\n", int(signal_list.size()) - in_wires - out_wires);
807 log("ABC RESULTS: input signals: %8d\n", in_wires);
808 log("ABC RESULTS: output signals: %8d\n", out_wires);
809
810 design->remove(mapped_mod);
811 }
812 else
813 {
814 log("Don't call ABC as there is nothing to map.\n");
815 }
816
817 if (cleanup)
818 {
819 log("Removing temp directory.\n");
820 remove_directory(tempdir_name);
821 }
822
823 log_pop();
824 }
825
826 struct Abc9Pass : public Pass {
827 Abc9Pass() : Pass("abc9", "use ABC9 for technology mapping") { }
828 void help() YS_OVERRIDE
829 {
830 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
831 log("\n");
832 log(" abc9 [options] [selection]\n");
833 log("\n");
834 log("This pass uses the ABC tool [1] for technology mapping of yosys's internal gate\n");
835 log("library to a target architecture.\n");
836 log("\n");
837 log(" -exe <command>\n");
838 #ifdef ABCEXTERNAL
839 log(" use the specified command instead of \"" ABCEXTERNAL "\" to execute ABC.\n");
840 #else
841 log(" use the specified command instead of \"<yosys-bindir>/yosys-abc\" to execute ABC.\n");
842 #endif
843 log(" This can e.g. be used to call a specific version of ABC or a wrapper.\n");
844 log("\n");
845 log(" -script <file>\n");
846 log(" use the specified ABC script file instead of the default script.\n");
847 log("\n");
848 log(" if <file> starts with a plus sign (+), then the rest of the filename\n");
849 log(" string is interpreted as the command string to be passed to ABC. The\n");
850 log(" leading plus sign is removed and all commas (,) in the string are\n");
851 log(" replaced with blanks before the string is passed to ABC.\n");
852 log("\n");
853 log(" if no -script parameter is given, the following scripts are used:\n");
854 log("\n");
855 log(" for -lut/-luts (only one LUT size):\n");
856 log("%s\n", fold_abc_cmd(ABC_COMMAND_LUT /*"; lutpack {S}"*/).c_str());
857 log("\n");
858 log(" for -lut/-luts (different LUT sizes):\n");
859 log("%s\n", fold_abc_cmd(ABC_COMMAND_LUT).c_str());
860 log("\n");
861 log(" -fast\n");
862 log(" use different default scripts that are slightly faster (at the cost\n");
863 log(" of output quality):\n");
864 log("\n");
865 log(" for -lut/-luts:\n");
866 log("%s\n", fold_abc_cmd(ABC_FAST_COMMAND_LUT).c_str());
867 log("\n");
868 log(" -D <picoseconds>\n");
869 log(" set delay target. the string {D} in the default scripts above is\n");
870 log(" replaced by this option when used, and an empty string otherwise\n");
871 log(" (indicating best possible delay).\n");
872 // log(" This also replaces 'dretime' with 'dretime; retime -o {D}' in the\n");
873 // log(" default scripts above.\n");
874 log("\n");
875 // log(" -S <num>\n");
876 // log(" maximum number of LUT inputs shared.\n");
877 // log(" (replaces {S} in the default scripts above, default: -S 1)\n");
878 // log("\n");
879 log(" -lut <width>\n");
880 log(" generate netlist using luts of (max) the specified width.\n");
881 log("\n");
882 log(" -lut <w1>:<w2>\n");
883 log(" generate netlist using luts of (max) the specified width <w2>. All\n");
884 log(" luts with width <= <w1> have constant cost. for luts larger than <w1>\n");
885 log(" the area cost doubles with each additional input bit. the delay cost\n");
886 log(" is still constant for all lut widths.\n");
887 log("\n");
888 log(" -lut <file>\n");
889 log(" pass this file with lut library to ABC.\n");
890 log("\n");
891 log(" -luts <cost1>,<cost2>,<cost3>,<sizeN>:<cost4-N>,..\n");
892 log(" generate netlist using luts. Use the specified costs for luts with 1,\n");
893 log(" 2, 3, .. inputs.\n");
894 log("\n");
895 // log(" -dff\n");
896 // log(" also pass $_DFF_?_ and $_DFFE_??_ cells through ABC. modules with many\n");
897 // log(" clock domains are automatically partitioned in clock domains and each\n");
898 // log(" domain is passed through ABC independently.\n");
899 // log("\n");
900 // log(" -clk [!]<clock-signal-name>[,[!]<enable-signal-name>]\n");
901 // log(" use only the specified clock domain. this is like -dff, but only FF\n");
902 // log(" cells that belong to the specified clock domain are used.\n");
903 // log("\n");
904 // log(" -keepff\n");
905 // log(" set the \"keep\" attribute on flip-flop output wires. (and thus preserve\n");
906 // log(" them, for example for equivalence checking.)\n");
907 // log("\n");
908 log(" -nocleanup\n");
909 log(" when this option is used, the temporary files created by this pass\n");
910 log(" are not removed. this is useful for debugging.\n");
911 log("\n");
912 log(" -showtmp\n");
913 log(" print the temp dir name in log. usually this is suppressed so that the\n");
914 log(" command output is identical across runs.\n");
915 log("\n");
916 log(" -markgroups\n");
917 log(" set a 'abcgroup' attribute on all objects created by ABC. The value of\n");
918 log(" this attribute is a unique integer for each ABC process started. This\n");
919 log(" is useful for debugging the partitioning of clock domains.\n");
920 log("\n");
921 log(" -box <file>\n");
922 log(" pass this file with box library to ABC. Use with -lut.\n");
923 log("\n");
924 log("Note that this is a logic optimization pass within Yosys that is calling ABC\n");
925 log("internally. This is not going to \"run ABC on your design\". It will instead run\n");
926 log("ABC on logic snippets extracted from your design. You will not get any useful\n");
927 log("output when passing an ABC script that writes a file. Instead write your full\n");
928 log("design as BLIF file with write_blif and then load that into ABC externally if\n");
929 log("you want to use ABC to convert your design into another format.\n");
930 log("\n");
931 log("[1] http://www.eecs.berkeley.edu/~alanmi/abc/\n");
932 log("\n");
933 }
934 void execute(std::vector<std::string> args, RTLIL::Design *design) YS_OVERRIDE
935 {
936 log_header(design, "Executing ABC9 pass (technology mapping using ABC9).\n");
937 log_push();
938
939 assign_map.clear();
940
941 #ifdef ABCEXTERNAL
942 std::string exe_file = ABCEXTERNAL;
943 #else
944 std::string exe_file = proc_self_dirname() + "yosys-abc";
945 #endif
946 std::string script_file, clk_str, box_file, lut_file;
947 std::string delay_target, lutin_shared = "-S 1", wire_delay;
948 bool fast_mode = false, dff_mode = false, keepff = false, cleanup = true;
949 bool show_tempdir = false;
950 vector<int> lut_costs;
951 markgroups = false;
952
953 #if 0
954 cleanup = false;
955 show_tempdir = true;
956 #endif
957
958 #ifdef _WIN32
959 #ifndef ABCEXTERNAL
960 if (!check_file_exists(exe_file + ".exe") && check_file_exists(proc_self_dirname() + "..\\yosys-abc.exe"))
961 exe_file = proc_self_dirname() + "..\\yosys-abc";
962 #endif
963 #endif
964
965 size_t argidx;
966 char pwd [PATH_MAX];
967 if (!getcwd(pwd, sizeof(pwd))) {
968 log_cmd_error("getcwd failed: %s\n", strerror(errno));
969 log_abort();
970 }
971 for (argidx = 1; argidx < args.size(); argidx++) {
972 std::string arg = args[argidx];
973 if (arg == "-exe" && argidx+1 < args.size()) {
974 exe_file = args[++argidx];
975 continue;
976 }
977 if (arg == "-script" && argidx+1 < args.size()) {
978 script_file = args[++argidx];
979 rewrite_filename(script_file);
980 if (!script_file.empty() && !is_absolute_path(script_file) && script_file[0] != '+')
981 script_file = std::string(pwd) + "/" + script_file;
982 continue;
983 }
984 if (arg == "-D" && argidx+1 < args.size()) {
985 delay_target = "-D " + args[++argidx];
986 continue;
987 }
988 //if (arg == "-S" && argidx+1 < args.size()) {
989 // lutin_shared = "-S " + args[++argidx];
990 // continue;
991 //}
992 if (arg == "-lut" && argidx+1 < args.size()) {
993 string arg = args[++argidx];
994 size_t pos = arg.find_first_of(':');
995 int lut_mode = 0, lut_mode2 = 0;
996 if (pos != string::npos) {
997 lut_mode = atoi(arg.substr(0, pos).c_str());
998 lut_mode2 = atoi(arg.substr(pos+1).c_str());
999 } else {
1000 pos = arg.find_first_of('.');
1001 if (pos != string::npos) {
1002 lut_file = arg;
1003 rewrite_filename(lut_file);
1004 if (!lut_file.empty() && !is_absolute_path(lut_file))
1005 lut_file = std::string(pwd) + "/" + lut_file;
1006 }
1007 else {
1008 lut_mode = atoi(arg.c_str());
1009 lut_mode2 = lut_mode;
1010 }
1011 }
1012 lut_costs.clear();
1013 for (int i = 0; i < lut_mode; i++)
1014 lut_costs.push_back(1);
1015 for (int i = lut_mode; i < lut_mode2; i++)
1016 lut_costs.push_back(2 << (i - lut_mode));
1017 continue;
1018 }
1019 if (arg == "-luts" && argidx+1 < args.size()) {
1020 lut_costs.clear();
1021 for (auto &tok : split_tokens(args[++argidx], ",")) {
1022 auto parts = split_tokens(tok, ":");
1023 if (GetSize(parts) == 0 && !lut_costs.empty())
1024 lut_costs.push_back(lut_costs.back());
1025 else if (GetSize(parts) == 1)
1026 lut_costs.push_back(atoi(parts.at(0).c_str()));
1027 else if (GetSize(parts) == 2)
1028 while (GetSize(lut_costs) < atoi(parts.at(0).c_str()))
1029 lut_costs.push_back(atoi(parts.at(1).c_str()));
1030 else
1031 log_cmd_error("Invalid -luts syntax.\n");
1032 }
1033 continue;
1034 }
1035 if (arg == "-fast") {
1036 fast_mode = true;
1037 continue;
1038 }
1039 //if (arg == "-dff") {
1040 // dff_mode = true;
1041 // continue;
1042 //}
1043 //if (arg == "-clk" && argidx+1 < args.size()) {
1044 // clk_str = args[++argidx];
1045 // dff_mode = true;
1046 // continue;
1047 //}
1048 //if (arg == "-keepff") {
1049 // keepff = true;
1050 // continue;
1051 //}
1052 if (arg == "-nocleanup") {
1053 cleanup = false;
1054 continue;
1055 }
1056 if (arg == "-showtmp") {
1057 show_tempdir = true;
1058 continue;
1059 }
1060 if (arg == "-markgroups") {
1061 markgroups = true;
1062 continue;
1063 }
1064 if (arg == "-box" && argidx+1 < args.size()) {
1065 box_file = args[++argidx];
1066 rewrite_filename(box_file);
1067 if (!box_file.empty() && !is_absolute_path(box_file))
1068 box_file = std::string(pwd) + "/" + box_file;
1069 continue;
1070 }
1071 if (arg == "-W" && argidx+1 < args.size()) {
1072 wire_delay = "-W " + args[++argidx];
1073 continue;
1074 }
1075 break;
1076 }
1077 extra_args(args, argidx, design);
1078
1079 for (auto mod : design->selected_modules())
1080 {
1081 if (mod->attributes.count("\\abc_box_id"))
1082 continue;
1083
1084 if (mod->processes.size() > 0) {
1085 log("Skipping module %s as it contains processes.\n", log_id(mod));
1086 continue;
1087 }
1088
1089 assign_map.set(mod);
1090
1091 if (!dff_mode || !clk_str.empty()) {
1092 abc9_module(design, mod, script_file, exe_file, cleanup, lut_costs, dff_mode, clk_str, keepff,
1093 delay_target, lutin_shared, fast_mode, show_tempdir,
1094 box_file, lut_file, wire_delay);
1095 continue;
1096 }
1097
1098 CellTypes ct(design);
1099
1100 std::vector<RTLIL::Cell*> all_cells = mod->selected_cells();
1101 std::set<RTLIL::Cell*> unassigned_cells(all_cells.begin(), all_cells.end());
1102
1103 std::set<RTLIL::Cell*> expand_queue, next_expand_queue;
1104 std::set<RTLIL::Cell*> expand_queue_up, next_expand_queue_up;
1105 std::set<RTLIL::Cell*> expand_queue_down, next_expand_queue_down;
1106
1107 typedef tuple<bool, RTLIL::SigSpec, bool, RTLIL::SigSpec> clkdomain_t;
1108 std::map<clkdomain_t, std::vector<RTLIL::Cell*>> assigned_cells;
1109 std::map<RTLIL::Cell*, clkdomain_t> assigned_cells_reverse;
1110
1111 std::map<RTLIL::Cell*, std::set<RTLIL::SigBit>> cell_to_bit, cell_to_bit_up, cell_to_bit_down;
1112 std::map<RTLIL::SigBit, std::set<RTLIL::Cell*>> bit_to_cell, bit_to_cell_up, bit_to_cell_down;
1113
1114 for (auto cell : all_cells)
1115 {
1116 clkdomain_t key;
1117
1118 for (auto &conn : cell->connections())
1119 for (auto bit : conn.second) {
1120 bit = assign_map(bit);
1121 if (bit.wire != nullptr) {
1122 cell_to_bit[cell].insert(bit);
1123 bit_to_cell[bit].insert(cell);
1124 if (ct.cell_input(cell->type, conn.first)) {
1125 cell_to_bit_up[cell].insert(bit);
1126 bit_to_cell_down[bit].insert(cell);
1127 }
1128 if (ct.cell_output(cell->type, conn.first)) {
1129 cell_to_bit_down[cell].insert(bit);
1130 bit_to_cell_up[bit].insert(cell);
1131 }
1132 }
1133 }
1134
1135 if (cell->type == "$_DFF_N_" || cell->type == "$_DFF_P_")
1136 {
1137 key = clkdomain_t(cell->type == "$_DFF_P_", assign_map(cell->getPort("\\C")), true, RTLIL::SigSpec());
1138 }
1139 else
1140 if (cell->type == "$_DFFE_NN_" || cell->type == "$_DFFE_NP_" || cell->type == "$_DFFE_PN_" || cell->type == "$_DFFE_PP_")
1141 {
1142 bool this_clk_pol = cell->type == "$_DFFE_PN_" || cell->type == "$_DFFE_PP_";
1143 bool this_en_pol = cell->type == "$_DFFE_NP_" || cell->type == "$_DFFE_PP_";
1144 key = clkdomain_t(this_clk_pol, assign_map(cell->getPort("\\C")), this_en_pol, assign_map(cell->getPort("\\E")));
1145 }
1146 else
1147 continue;
1148
1149 unassigned_cells.erase(cell);
1150 expand_queue.insert(cell);
1151 expand_queue_up.insert(cell);
1152 expand_queue_down.insert(cell);
1153
1154 assigned_cells[key].push_back(cell);
1155 assigned_cells_reverse[cell] = key;
1156 }
1157
1158 while (!expand_queue_up.empty() || !expand_queue_down.empty())
1159 {
1160 if (!expand_queue_up.empty())
1161 {
1162 RTLIL::Cell *cell = *expand_queue_up.begin();
1163 clkdomain_t key = assigned_cells_reverse.at(cell);
1164 expand_queue_up.erase(cell);
1165
1166 for (auto bit : cell_to_bit_up[cell])
1167 for (auto c : bit_to_cell_up[bit])
1168 if (unassigned_cells.count(c)) {
1169 unassigned_cells.erase(c);
1170 next_expand_queue_up.insert(c);
1171 assigned_cells[key].push_back(c);
1172 assigned_cells_reverse[c] = key;
1173 expand_queue.insert(c);
1174 }
1175 }
1176
1177 if (!expand_queue_down.empty())
1178 {
1179 RTLIL::Cell *cell = *expand_queue_down.begin();
1180 clkdomain_t key = assigned_cells_reverse.at(cell);
1181 expand_queue_down.erase(cell);
1182
1183 for (auto bit : cell_to_bit_down[cell])
1184 for (auto c : bit_to_cell_down[bit])
1185 if (unassigned_cells.count(c)) {
1186 unassigned_cells.erase(c);
1187 next_expand_queue_up.insert(c);
1188 assigned_cells[key].push_back(c);
1189 assigned_cells_reverse[c] = key;
1190 expand_queue.insert(c);
1191 }
1192 }
1193
1194 if (expand_queue_up.empty() && expand_queue_down.empty()) {
1195 expand_queue_up.swap(next_expand_queue_up);
1196 expand_queue_down.swap(next_expand_queue_down);
1197 }
1198 }
1199
1200 while (!expand_queue.empty())
1201 {
1202 RTLIL::Cell *cell = *expand_queue.begin();
1203 clkdomain_t key = assigned_cells_reverse.at(cell);
1204 expand_queue.erase(cell);
1205
1206 for (auto bit : cell_to_bit.at(cell)) {
1207 for (auto c : bit_to_cell[bit])
1208 if (unassigned_cells.count(c)) {
1209 unassigned_cells.erase(c);
1210 next_expand_queue.insert(c);
1211 assigned_cells[key].push_back(c);
1212 assigned_cells_reverse[c] = key;
1213 }
1214 bit_to_cell[bit].clear();
1215 }
1216
1217 if (expand_queue.empty())
1218 expand_queue.swap(next_expand_queue);
1219 }
1220
1221 clkdomain_t key(true, RTLIL::SigSpec(), true, RTLIL::SigSpec());
1222 for (auto cell : unassigned_cells) {
1223 assigned_cells[key].push_back(cell);
1224 assigned_cells_reverse[cell] = key;
1225 }
1226
1227 log_header(design, "Summary of detected clock domains:\n");
1228 for (auto &it : assigned_cells)
1229 log(" %d cells in clk=%s%s, en=%s%s\n", GetSize(it.second),
1230 std::get<0>(it.first) ? "" : "!", log_signal(std::get<1>(it.first)),
1231 std::get<2>(it.first) ? "" : "!", log_signal(std::get<3>(it.first)));
1232
1233 for (auto &it : assigned_cells) {
1234 clk_polarity = std::get<0>(it.first);
1235 clk_sig = assign_map(std::get<1>(it.first));
1236 en_polarity = std::get<2>(it.first);
1237 en_sig = assign_map(std::get<3>(it.first));
1238 abc9_module(design, mod, script_file, exe_file, cleanup, lut_costs, !clk_sig.empty(), "$",
1239 keepff, delay_target, lutin_shared, fast_mode, show_tempdir,
1240 box_file, lut_file, wire_delay);
1241 assign_map.set(mod);
1242 }
1243 }
1244
1245 Pass::call(design, "clean");
1246
1247 assign_map.clear();
1248
1249 log_pop();
1250 }
1251 } Abc9Pass;
1252
1253 PRIVATE_NAMESPACE_END