Merge remote-tracking branch 'origin/master' into xaig_dff
[yosys.git] / passes / techmap / abc9.cc
index 777ec6ac8fecf8198c4a036df0d7976a2e67ea2b..def347c210d3b676763dbe66e4eae9b5b66a0262 100644 (file)
@@ -66,22 +66,21 @@ PRIVATE_NAMESPACE_BEGIN
 bool markgroups;
 int map_autoidx;
 
-inline std::string remap_name(RTLIL::IdString abc_name)
+inline std::string remap_name(RTLIL::IdString abc9_name)
 {
-       return stringf("$abc$%d$%s", map_autoidx, abc_name.c_str()+1);
+       return stringf("$abc$%d$%s", map_autoidx, abc9_name.c_str()+1);
 }
 
 void handle_loops(RTLIL::Design *design, RTLIL::Module *module)
 {
-       // FIXME: Do not run on all modules in design!?!
-       Pass::call(design, "scc -set_attr abc_scc_id {} % w:*");
+       Pass::call(design, "scc -set_attr abc9_scc_id {} % w:*");
 
        // For every unique SCC found, (arbitrarily) find the first
        // cell in the component, and select (and mark) all its output
        // wires
        pool<RTLIL::Const> ids_seen;
        for (auto cell : module->cells()) {
-               auto it = cell->attributes.find(ID(abc_scc_id));
+               auto it = cell->attributes.find(ID(abc9_scc_id));
                if (it != cell->attributes.end()) {
                        auto r = ids_seen.insert(it->second);
                        if (r.second) {
@@ -90,20 +89,30 @@ void handle_loops(RTLIL::Design *design, RTLIL::Module *module)
                                        if (cell->output(c.first)) {
                                                SigBit b = c.second.as_bit();
                                                Wire *w = b.wire;
-                                               log_assert(!w->port_input);
-                                               w->port_input = true;
-                                               w = module->wire(stringf("%s.abci", w->name.c_str()));
-                                               if (!w) {
-                                                       w = module->addWire(stringf("%s.abci", b.wire->name.c_str()), GetSize(b.wire));
-                                                       w->port_output = true;
+                                               if (w->port_input) {
+                                                       // In this case, hopefully the loop break has been already created
+                                                       // Get the non-prefixed wire
+                                                       Wire *wo = module->wire(stringf("%s.abco", b.wire->name.c_str()));
+                                                       log_assert(wo != nullptr);
+                                                       log_assert(wo->port_output);
+                                                       log_assert(b.offset < GetSize(wo));
+                                                       c.second = RTLIL::SigBit(wo, b.offset);
                                                }
                                                else {
-                                                       log_assert(w->port_input);
-                                                       log_assert(b.offset < GetSize(w));
+                                                       // Create a new output/input loop break
+                                                       w->port_input = true;
+                                                       w = module->wire(stringf("%s.abco", w->name.c_str()));
+                                                       if (!w) {
+                                                               w = module->addWire(stringf("%s.abco", b.wire->name.c_str()), GetSize(b.wire));
+                                                               w->port_output = true;
+                                                       }
+                                                       else {
+                                                               log_assert(w->port_input);
+                                                               log_assert(b.offset < GetSize(w));
+                                                       }
+                                                       w->set_bool_attribute(ID(abc9_scc_break));
+                                                       c.second = RTLIL::SigBit(w, b.offset);
                                                }
-                                               w->set_bool_attribute(ID(abc_scc_break));
-                                               module->swap_names(b.wire, w);
-                                               c.second = RTLIL::SigBit(w, b.offset);
                                        }
                                }
                        }
@@ -114,7 +123,7 @@ void handle_loops(RTLIL::Design *design, RTLIL::Module *module)
        module->fixup_ports();
 }
 
-std::string add_echos_to_abc_cmd(std::string str)
+std::string add_echos_to_abc9_cmd(std::string str)
 {
        std::string new_str, token;
        for (size_t i = 0; i < str.size(); i++) {
@@ -136,7 +145,7 @@ std::string add_echos_to_abc_cmd(std::string str)
        return new_str;
 }
 
-std::string fold_abc_cmd(std::string str)
+std::string fold_abc9_cmd(std::string str)
 {
        std::string token, new_str = "          ";
        int char_counter = 10;
@@ -180,7 +189,7 @@ std::string replace_tempdir(std::string text, std::string tempdir_name, bool sho
        return text;
 }
 
-struct abc_output_filter
+struct abc9_output_filter
 {
        bool got_cr;
        int escape_seq_state;
@@ -188,7 +197,7 @@ struct abc_output_filter
        std::string tempdir_name;
        bool show_tempdir;
 
-       abc_output_filter(std::string tempdir_name, bool show_tempdir) : tempdir_name(tempdir_name), show_tempdir(show_tempdir)
+       abc9_output_filter(std::string tempdir_name, bool show_tempdir) : tempdir_name(tempdir_name), show_tempdir(show_tempdir)
        {
                got_cr = false;
                escape_seq_state = 0;
@@ -243,7 +252,7 @@ void abc9_module(RTLIL::Design *design, RTLIL::Module *module, std::string scrip
                bool cleanup, vector<int> lut_costs, bool /*dff_mode*/, std::string /*clk_str*/,
                bool /*keepff*/, std::string delay_target, std::string /*lutin_shared*/, bool fast_mode,
                bool show_tempdir, std::string box_file, std::string lut_file,
-               std::string wire_delay, const dict<int,IdString> &box_lookup
+               std::string wire_delay, const dict<int,IdString> &box_lookup, bool nomfs
 )
 {
        map_autoidx = autoidx++;
@@ -255,83 +264,78 @@ void abc9_module(RTLIL::Design *design, RTLIL::Module *module, std::string scrip
        log_header(design, "Extracting gate netlist of module `%s' to `%s/input.xaig'..\n",
                        module->name.c_str(), replace_tempdir(tempdir_name, tempdir_name, show_tempdir).c_str());
 
-       std::string abc_script;
+       std::string abc9_script;
 
        if (!lut_costs.empty()) {
-               abc_script += stringf("read_lut %s/lutdefs.txt; ", tempdir_name.c_str());
+               abc9_script += stringf("read_lut %s/lutdefs.txt; ", tempdir_name.c_str());
                if (!box_file.empty())
-                       abc_script += stringf("read_box -v %s; ", box_file.c_str());
+                       abc9_script += stringf("read_box -v %s; ", box_file.c_str());
        }
        else
        if (!lut_file.empty()) {
-               abc_script += stringf("read_lut %s; ", lut_file.c_str());
+               abc9_script += stringf("read_lut %s; ", lut_file.c_str());
                if (!box_file.empty())
-                       abc_script += stringf("read_box -v %s; ", box_file.c_str());
+                       abc9_script += stringf("read_box -v %s; ", box_file.c_str());
        }
        else
                log_abort();
 
-       abc_script += stringf("&read %s/input.xaig; &ps; ", tempdir_name.c_str());
+       abc9_script += stringf("&read %s/input.xaig; &ps; ", tempdir_name.c_str());
 
        if (!script_file.empty()) {
                if (script_file[0] == '+') {
                        for (size_t i = 1; i < script_file.size(); i++)
                                if (script_file[i] == '\'')
-                                       abc_script += "'\\''";
+                                       abc9_script += "'\\''";
                                else if (script_file[i] == ',')
-                                       abc_script += " ";
+                                       abc9_script += " ";
                                else
-                                       abc_script += script_file[i];
+                                       abc9_script += script_file[i];
                } else
-                       abc_script += stringf("source %s", script_file.c_str());
+                       abc9_script += stringf("source %s", script_file.c_str());
        } else if (!lut_costs.empty() || !lut_file.empty()) {
                //bool all_luts_cost_same = true;
                //for (int this_cost : lut_costs)
                //      if (this_cost != lut_costs.front())
                //              all_luts_cost_same = false;
-               abc_script += fast_mode ? ABC_FAST_COMMAND_LUT : ABC_COMMAND_LUT;
+               abc9_script += fast_mode ? ABC_FAST_COMMAND_LUT : ABC_COMMAND_LUT;
                //if (all_luts_cost_same && !fast_mode)
-               //      abc_script += "; lutpack {S}";
+               //      abc9_script += "; lutpack {S}";
        } else
                log_abort();
 
        //if (script_file.empty() && !delay_target.empty())
-       //      for (size_t pos = abc_script.find("dretime;"); pos != std::string::npos; pos = abc_script.find("dretime;", pos+1))
-       //              abc_script = abc_script.substr(0, pos) + "dretime; retime -o {D};" + abc_script.substr(pos+8);
+       //      for (size_t pos = abc9_script.find("dretime;"); pos != std::string::npos; pos = abc9_script.find("dretime;", pos+1))
+       //              abc9_script = abc9_script.substr(0, pos) + "dretime; retime -o {D};" + abc9_script.substr(pos+8);
 
-       for (size_t pos = abc_script.find("{D}"); pos != std::string::npos; pos = abc_script.find("{D}", pos))
-               abc_script = abc_script.substr(0, pos) + delay_target + abc_script.substr(pos+3);
+       for (size_t pos = abc9_script.find("{D}"); pos != std::string::npos; pos = abc9_script.find("{D}", pos))
+               abc9_script = abc9_script.substr(0, pos) + delay_target + abc9_script.substr(pos+3);
 
-       //for (size_t pos = abc_script.find("{S}"); pos != std::string::npos; pos = abc_script.find("{S}", pos))
-       //      abc_script = abc_script.substr(0, pos) + lutin_shared + abc_script.substr(pos+3);
+       //for (size_t pos = abc9_script.find("{S}"); pos != std::string::npos; pos = abc9_script.find("{S}", pos))
+       //      abc9_script = abc9_script.substr(0, pos) + lutin_shared + abc9_script.substr(pos+3);
 
-       for (size_t pos = abc_script.find("{W}"); pos != std::string::npos; pos = abc_script.find("{W}", pos))
-               abc_script = abc_script.substr(0, pos) + wire_delay + abc_script.substr(pos+3);
+       for (size_t pos = abc9_script.find("{W}"); pos != std::string::npos; pos = abc9_script.find("{W}", pos))
+               abc9_script = abc9_script.substr(0, pos) + wire_delay + abc9_script.substr(pos+3);
 
-       abc_script += stringf("; &write %s/output.aig", tempdir_name.c_str());
-       abc_script = add_echos_to_abc_cmd(abc_script);
+       if (nomfs)
+               for (size_t pos = abc9_script.find("&mfs"); pos != std::string::npos; pos = abc9_script.find("&mfs", pos))
+                       abc9_script = abc9_script.erase(pos, strlen("&mfs"));
 
-       for (size_t i = 0; i+1 < abc_script.size(); i++)
-               if (abc_script[i] == ';' && abc_script[i+1] == ' ')
-                       abc_script[i+1] = '\n';
+       abc9_script += stringf("; &write -n %s/output.aig", tempdir_name.c_str());
+       abc9_script = add_echos_to_abc9_cmd(abc9_script);
+
+       for (size_t i = 0; i+1 < abc9_script.size(); i++)
+               if (abc9_script[i] == ';' && abc9_script[i+1] == ' ')
+                       abc9_script[i+1] = '\n';
 
        FILE *f = fopen(stringf("%s/abc.script", tempdir_name.c_str()).c_str(), "wt");
-       fprintf(f, "%s\n", abc_script.c_str());
+       fprintf(f, "%s\n", abc9_script.c_str());
        fclose(f);
 
-       bool count_output = false;
-       for (auto port_name : module->ports) {
-               RTLIL::Wire *port_wire = module->wire(port_name);
-               log_assert(port_wire);
-               if (port_wire->port_output) {
-                       count_output = true;
-                       break;
-               }
-       }
-
+       //bool count_output = false;
        log_push();
 
-       if (count_output)
+       //if (count_output)
        {
                handle_loops(design, module);
 
@@ -353,31 +357,13 @@ void abc9_module(RTLIL::Design *design, RTLIL::Module *module, std::string scrip
                log_assert(!design->module(ID($__abc9__)));
                {
                        AigerReader reader(design, ifs, ID($__abc9__), "" /* clk_name */, buffer.c_str() /* map_filename */, true /* wideports */);
-                       reader.parse_xaiger();
+                       reader.parse_xaiger(box_lookup);
                }
                ifs.close();
-               Pass::call(design, stringf("write_verilog -noexpr -norename"));
+               Pass::call_on_module(design, design->module(ID($__abc9__)), stringf("write_verilog -noexpr -norename -selected"));
                design->remove(design->module(ID($__abc9__)));
 #endif
 
-               // Now 'unexpose' those wires by undoing
-               // the expose operation -- remove them from PO/PI
-               // and re-connecting them back together
-               for (auto wire : module->wires()) {
-                       auto it = wire->attributes.find(ID(abc_scc_break));
-                       if (it != wire->attributes.end()) {
-                               wire->attributes.erase(it);
-                               log_assert(wire->port_output);
-                               wire->port_output = false;
-                               RTLIL::Wire *i_wire = module->wire(wire->name.str() + ".abci");
-                               log_assert(i_wire);
-                               log_assert(i_wire->port_input);
-                               i_wire->port_input = false;
-                               module->connect(i_wire, wire);
-                       }
-               }
-               module->fixup_ports();
-
                log_header(design, "Executing ABC9.\n");
 
                if (!lut_costs.empty()) {
@@ -394,22 +380,22 @@ void abc9_module(RTLIL::Design *design, RTLIL::Module *module, std::string scrip
                log("Running ABC command: %s\n", replace_tempdir(buffer, tempdir_name, show_tempdir).c_str());
 
 #ifndef YOSYS_LINK_ABC
-               abc_output_filter filt(tempdir_name, show_tempdir);
-               int ret = run_command(buffer, std::bind(&abc_output_filter::next_line, filt, std::placeholders::_1));
+               abc9_output_filter filt(tempdir_name, show_tempdir);
+               int ret = run_command(buffer, std::bind(&abc9_output_filter::next_line, filt, std::placeholders::_1));
 #else
                // These needs to be mutable, supposedly due to getopt
-               char *abc_argv[5];
+               char *abc9_argv[5];
                string tmp_script_name = stringf("%s/abc.script", tempdir_name.c_str());
-               abc_argv[0] = strdup(exe_file.c_str());
-               abc_argv[1] = strdup("-s");
-               abc_argv[2] = strdup("-f");
-               abc_argv[3] = strdup(tmp_script_name.c_str());
-               abc_argv[4] = 0;
-               int ret = Abc_RealMain(4, abc_argv);
-               free(abc_argv[0]);
-               free(abc_argv[1]);
-               free(abc_argv[2]);
-               free(abc_argv[3]);
+               abc9_argv[0] = strdup(exe_file.c_str());
+               abc9_argv[1] = strdup("-s");
+               abc9_argv[2] = strdup("-f");
+               abc9_argv[3] = strdup(tmp_script_name.c_str());
+               abc9_argv[4] = 0;
+               int ret = Abc_RealMain(4, abc9_argv);
+               free(abc9_argv[0]);
+               free(abc9_argv[1]);
+               free(abc9_argv[2]);
+               free(abc9_argv[3]);
 #endif
                if (ret != 0)
                        log_error("ABC: execution of command \"%s\" failed: return code %d.\n", buffer.c_str(), ret);
@@ -427,7 +413,7 @@ void abc9_module(RTLIL::Design *design, RTLIL::Module *module, std::string scrip
                ifs.close();
 
 #if 0
-               Pass::call(design, stringf("write_verilog -noexpr -norename"));
+               Pass::call_on_module(design, design->module(ID($__abc9__)), stringf("write_verilog -noexpr -norename -selected"));
 #endif
 
                log_header(design, "Re-integrating ABC9 results.\n");
@@ -435,39 +421,23 @@ void abc9_module(RTLIL::Design *design, RTLIL::Module *module, std::string scrip
                if (mapped_mod == NULL)
                        log_error("ABC output file does not contain a module `$__abc9__'.\n");
 
-               pool<RTLIL::SigBit> output_bits;
                for (auto &it : mapped_mod->wires_) {
                        RTLIL::Wire *w = it.second;
                        RTLIL::Wire *remap_wire = module->addWire(remap_name(w->name), GetSize(w));
                        if (markgroups) remap_wire->attributes[ID(abcgroup)] = map_autoidx;
-                       if (w->port_output) {
-                               RTLIL::Wire *wire = module->wire(w->name);
-                               log_assert(wire);
-                               for (int i = 0; i < GetSize(w); i++)
-                                       output_bits.insert({wire, i});
-                       }
                }
 
-               for (auto &it : module->connections_) {
-                       auto &signal = it.first;
-                       auto bits = signal.bits();
-                       for (auto &b : bits)
-                               if (output_bits.count(b))
-                                       b = module->addWire(NEW_ID);
-                       signal = std::move(bits);
-               }
-
-               dict<IdString, bool> abc_box;
+               dict<IdString, bool> abc9_box;
                vector<RTLIL::Cell*> boxes;
                for (auto cell : module->selected_cells()) {
-                       if (cell->type.in(ID($_AND_), ID($_NOT_), ID($__ABC_FF_))) {
+                       if (cell->type.in(ID($_AND_), ID($_NOT_), ID($__ABC9_FF_))) {
                                module->remove(cell);
                                continue;
                        }
-                       auto jt = abc_box.find(cell->type);
-                       if (jt == abc_box.end()) {
+                       auto jt = abc9_box.find(cell->type);
+                       if (jt == abc9_box.end()) {
                                RTLIL::Module* box_module = design->module(cell->type);
-                               jt = abc_box.insert(std::make_pair(cell->type, box_module && box_module->attributes.count(ID(abc_box_id)))).first;
+                               jt = abc9_box.insert(std::make_pair(cell->type, box_module && box_module->attributes.count(ID(abc9_box_id)))).first;
                        }
                        if (jt->second)
                                boxes.emplace_back(cell);
@@ -534,7 +504,7 @@ void abc9_module(RTLIL::Design *design, RTLIL::Module *module, std::string scrip
                        cell_stats[mapped_cell->type]++;
 
                        RTLIL::Cell *existing_cell = nullptr;
-                       if (mapped_cell->type.in(ID($lut), ID($__ABC_FF_))) {
+                       if (mapped_cell->type.in(ID($lut), ID($__ABC9_FF_))) {
                                if (mapped_cell->type == ID($lut) &&
                                                GetSize(mapped_cell->getPort(ID::A)) == 1 &&
                                                mapped_cell->getParam(ID(LUT)) == RTLIL::Const::from_string("01")) {
@@ -564,7 +534,7 @@ void abc9_module(RTLIL::Design *design, RTLIL::Module *module, std::string scrip
                        }
 
                        RTLIL::Module* box_module = design->module(mapped_cell->type);
-                       auto abc_flop = box_module && box_module->attributes.count("\\abc9_flop");
+                       auto abc9_flop = box_module && box_module->attributes.count("\\abc9_flop");
                        for (auto &conn : mapped_cell->connections()) {
                                RTLIL::SigSpec newsig;
                                for (auto c : conn.second.chunks()) {
@@ -577,7 +547,7 @@ void abc9_module(RTLIL::Design *design, RTLIL::Module *module, std::string scrip
                                }
                                cell->setPort(conn.first, newsig);
 
-                               if (!abc_flop) {
+                               if (!abc9_flop) {
                                        if (cell->input(conn.first)) {
                                                for (auto i : newsig)
                                                        bit2sinks[i].push_back(cell);
@@ -598,7 +568,7 @@ void abc9_module(RTLIL::Design *design, RTLIL::Module *module, std::string scrip
                                        if (!conn.second.is_wire())
                                                continue;
                                        Wire *wire = conn.second.as_wire();
-                                       if (!wire->get_bool_attribute(ID(abc_padding)))
+                                       if (!wire->get_bool_attribute(ID(abc9_padding)))
                                                continue;
                                        cell->unsetPort(conn.first);
                                        log_debug("Dropping padded port connection for %s (%s) .%s (%s )\n", log_id(cell), cell->type.c_str(), log_id(conn.first), log_signal(conn.second));
@@ -727,16 +697,35 @@ clone_lut:
                        }
                }
 
+               // Now 'unexpose' those wires by undoing
+               // the expose operation -- remove them from PO/PI
+               // and re-connecting them back together
+               for (auto wire : module->wires()) {
+                       auto it = wire->attributes.find(ID(abc9_scc_break));
+                       if (it != wire->attributes.end()) {
+                               wire->attributes.erase(it);
+                               log_assert(wire->port_output);
+                               wire->port_output = false;
+                               std::string name = wire->name.str();
+                               RTLIL::Wire *i_wire = module->wire(name.substr(0, GetSize(name) - 5));
+                               log_assert(i_wire);
+                               log_assert(i_wire->port_input);
+                               i_wire->port_input = false;
+                               module->connect(i_wire, wire);
+                       }
+               }
+               module->fixup_ports();
+
                //log("ABC RESULTS:        internal signals: %8d\n", int(signal_list.size()) - in_wires - out_wires);
                log("ABC RESULTS:           input signals: %8d\n", in_wires);
                log("ABC RESULTS:          output signals: %8d\n", out_wires);
 
                design->remove(mapped_mod);
        }
-       else
-       {
-               log("Don't call ABC as there is nothing to map.\n");
-       }
+       //else
+       //{
+       //      log("Don't call ABC as there is nothing to map.\n");
+       //}
 
        if (cleanup)
        {
@@ -777,17 +766,17 @@ struct Abc9Pass : public Pass {
                log("        if no -script parameter is given, the following scripts are used:\n");
                log("\n");
                log("        for -lut/-luts (only one LUT size):\n");
-               log("%s\n", fold_abc_cmd(ABC_COMMAND_LUT /*"; lutpack {S}"*/).c_str());
+               log("%s\n", fold_abc9_cmd(ABC_COMMAND_LUT /*"; lutpack {S}"*/).c_str());
                log("\n");
                log("        for -lut/-luts (different LUT sizes):\n");
-               log("%s\n", fold_abc_cmd(ABC_COMMAND_LUT).c_str());
+               log("%s\n", fold_abc9_cmd(ABC_COMMAND_LUT).c_str());
                log("\n");
                log("    -fast\n");
                log("        use different default scripts that are slightly faster (at the cost\n");
                log("        of output quality):\n");
                log("\n");
                log("        for -lut/-luts:\n");
-               log("%s\n", fold_abc_cmd(ABC_FAST_COMMAND_LUT).c_str());
+               log("%s\n", fold_abc9_cmd(ABC_FAST_COMMAND_LUT).c_str());
                log("\n");
                log("    -D <picoseconds>\n");
                log("        set delay target. the string {D} in the default scripts above is\n");
@@ -849,8 +838,17 @@ struct Abc9Pass : public Pass {
                log("internally. This is not going to \"run ABC on your design\". It will instead run\n");
                log("ABC on logic snippets extracted from your design. You will not get any useful\n");
                log("output when passing an ABC script that writes a file. Instead write your full\n");
-               log("design as BLIF file with write_blif and then load that into ABC externally if\n");
-               log("you want to use ABC to convert your design into another format.\n");
+               log("design as an XAIGER file with write_xaiger and then load that into ABC externally\n");
+               log("if you want to use ABC to convert your design into another format.\n");
+               log("\n");
+               //   |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
+               log("Delay targets can also be specified on a per clock basis by attaching a\n");
+               log("'(* abc9_period = <int> *)' attribute onto clock wires (specifically, onto wires\n");
+               log("that appear inside any special '$abc9_clock' wires inserted by abc9_map.v). This\n");
+               log("can be achieved by modifying the source directly, or through a `setattr`\n");
+               log("invocation. Since such attributes cannot yet be propagated through a\n");
+               log("hierarchical design (whether or not it has been uniquified) it is recommended\n");
+               log("that the design be flattened when using this feature.\n");
                log("\n");
                log("[1] http://www.eecs.berkeley.edu/~alanmi/abc/\n");
                log("\n");
@@ -869,6 +867,7 @@ struct Abc9Pass : public Pass {
                std::string delay_target, lutin_shared = "-S 1", wire_delay;
                bool fast_mode = false, /*dff_mode = false,*/ keepff = false, cleanup = true;
                bool show_tempdir = false;
+               bool nomfs = false;
                vector<int> lut_costs;
                markgroups = false;
 
@@ -913,29 +912,28 @@ struct Abc9Pass : public Pass {
                        //}
                        if (arg == "-lut" && argidx+1 < args.size()) {
                                string arg = args[++argidx];
-                               size_t pos = arg.find_first_of(':');
-                               int lut_mode = 0, lut_mode2 = 0;
-                               if (pos != string::npos) {
-                                       lut_mode = atoi(arg.substr(0, pos).c_str());
-                                       lut_mode2 = atoi(arg.substr(pos+1).c_str());
-                               } else {
-                                       pos = arg.find_first_of('.');
+                               if (arg.find_first_not_of("0123456789:") == std::string::npos) {
+                                       size_t pos = arg.find_first_of(':');
+                                       int lut_mode = 0, lut_mode2 = 0;
                                        if (pos != string::npos) {
-                                               lut_file = arg;
-                                               rewrite_filename(lut_file);
-                                               if (!lut_file.empty() && !is_absolute_path(lut_file))
-                                                       lut_file = std::string(pwd) + "/" + lut_file;
-                                       }
-                                       else {
+                                               lut_mode = atoi(arg.substr(0, pos).c_str());
+                                               lut_mode2 = atoi(arg.substr(pos+1).c_str());
+                                       } else {
                                                lut_mode = atoi(arg.c_str());
                                                lut_mode2 = lut_mode;
                                        }
+                                       lut_costs.clear();
+                                       for (int i = 0; i < lut_mode; i++)
+                                               lut_costs.push_back(1);
+                                       for (int i = lut_mode; i < lut_mode2; i++)
+                                               lut_costs.push_back(2 << (i - lut_mode));
+                               }
+                               else {
+                                       lut_file = arg;
+                                       rewrite_filename(lut_file);
+                                       if (!lut_file.empty() && !is_absolute_path(lut_file) && lut_file[0] != '+')
+                                               lut_file = std::string(pwd) + "/" + lut_file;
                                }
-                               lut_costs.clear();
-                               for (int i = 0; i < lut_mode; i++)
-                                       lut_costs.push_back(1);
-                               for (int i = lut_mode; i < lut_mode2; i++)
-                                       lut_costs.push_back(2 << (i - lut_mode));
                                continue;
                        }
                        if (arg == "-luts" && argidx+1 < args.size()) {
@@ -991,6 +989,10 @@ struct Abc9Pass : public Pass {
                                wire_delay = "-W " + args[++argidx];
                                continue;
                        }
+                       if (arg == "-nomfs") {
+                               nomfs = true;
+                               continue;
+                       }
                        break;
                }
                extra_args(args, argidx, design);
@@ -1000,12 +1002,12 @@ struct Abc9Pass : public Pass {
                    box_file = "+/dummy.box";
 
                rewrite_filename(box_file);
-               if (!box_file.empty() && !is_absolute_path(box_file))
+               if (!box_file.empty() && !is_absolute_path(box_file) && box_file[0] != '+')
                    box_file = std::string(pwd) + "/" + box_file;
 
                dict<int,IdString> box_lookup;
                for (auto m : design->modules()) {
-                       auto it = m->attributes.find(ID(abc_box_id));
+                       auto it = m->attributes.find(ID(abc9_box_id));
                        if (it == m->attributes.end())
                                continue;
                        if (m->name.begins_with("$paramod"))
@@ -1013,7 +1015,7 @@ struct Abc9Pass : public Pass {
                        auto id = it->second.as_int();
                        auto r = box_lookup.insert(std::make_pair(id, m->name));
                        if (!r.second)
-                               log_error("Module '%s' has the same abc_box_id = %d value as '%s'.\n",
+                               log_error("Module '%s' has the same abc9_box_id = %d value as '%s'.\n",
                                                log_id(m), id, log_id(r.first->second));
                        log_assert(r.second);
 
@@ -1021,24 +1023,24 @@ struct Abc9Pass : public Pass {
                        for (auto p : m->ports) {
                                auto w = m->wire(p);
                                log_assert(w);
-                               if (w->attributes.count(ID(abc_carry))) {
+                               if (w->attributes.count(ID(abc9_carry))) {
                                        if (w->port_input) {
                                                if (carry_in)
-                                                       log_error("Module '%s' contains more than one 'abc_carry' input port.\n", log_id(m));
+                                                       log_error("Module '%s' contains more than one 'abc9_carry' input port.\n", log_id(m));
                                                carry_in = w;
                                        }
                                        else if (w->port_output) {
                                                if (carry_out)
-                                                       log_error("Module '%s' contains more than one 'abc_carry' input port.\n", log_id(m));
+                                                       log_error("Module '%s' contains more than one 'abc9_carry' input port.\n", log_id(m));
                                                carry_out = w;
                                        }
                                }
                        }
                        if (carry_in || carry_out) {
                                if (carry_in && !carry_out)
-                                       log_error("Module '%s' contains an 'abc_carry' input port but no output port.\n", log_id(m));
+                                       log_error("Module '%s' contains an 'abc9_carry' input port but no output port.\n", log_id(m));
                                if (!carry_in && carry_out)
-                                       log_error("Module '%s' contains an 'abc_carry' output port but no input port.\n", log_id(m));
+                                       log_error("Module '%s' contains an 'abc9_carry' output port but no input port.\n", log_id(m));
                                // Make carry_in the last PI, and carry_out the last PO
                                //   since ABC requires it this way
                                auto &ports = m->ports;
@@ -1064,9 +1066,11 @@ struct Abc9Pass : public Pass {
                        }
                }
 
+               SigMap assign_map;
+               CellTypes ct(design);
                for (auto module : design->selected_modules())
                {
-                       if (module->attributes.count(ID(abc_box_id)))
+                       if (module->attributes.count(ID(abc9_box_id)))
                                continue;
 
                        if (module->processes.size() > 0) {
@@ -1074,27 +1078,23 @@ struct Abc9Pass : public Pass {
                                continue;
                        }
 
-                       SigMap assign_map(module);
-
-                       CellTypes ct(design);
+                       assign_map.set(module);
 
                        std::vector<RTLIL::Cell*> all_cells = module->selected_cells();
-                       std::set<RTLIL::Cell*> unassigned_cells(all_cells.begin(), all_cells.end());
+                       pool<RTLIL::Cell*> unassigned_cells(all_cells.begin(), all_cells.end());
 
-                       std::set<RTLIL::Cell*> expand_queue, next_expand_queue;
-                       std::set<RTLIL::Cell*> expand_queue_up, next_expand_queue_up;
-                       std::set<RTLIL::Cell*> expand_queue_down, next_expand_queue_down;
+                       pool<RTLIL::Cell*> expand_queue, next_expand_queue;
+                       pool<RTLIL::Cell*> expand_queue_up, next_expand_queue_up;
+                       pool<RTLIL::Cell*> expand_queue_down, next_expand_queue_down;
 
-                       std::map<SigSpec, pool<RTLIL::IdString>> assigned_cells;
-                       std::map<RTLIL::Cell*, SigSpec> assigned_cells_reverse;
+                       typedef SigSpec clkdomain_t;
+                       std::map<clkdomain_t, pool<RTLIL::IdString>> assigned_cells;
+                       std::map<RTLIL::Cell*, clkdomain_t> assigned_cells_reverse;
 
-                       std::map<RTLIL::Cell*, std::set<RTLIL::SigBit>> cell_to_bit, cell_to_bit_up, cell_to_bit_down;
-                       std::map<RTLIL::SigBit, std::set<RTLIL::Cell*>> bit_to_cell, bit_to_cell_up, bit_to_cell_down;
+                       std::map<RTLIL::Cell*, pool<RTLIL::SigBit>> cell_to_bit, cell_to_bit_up, cell_to_bit_down;
+                       std::map<RTLIL::SigBit, pool<RTLIL::Cell*>> bit_to_cell, bit_to_cell_up, bit_to_cell_down;
 
-                       typedef std::pair<IdString, SigSpec> endomain_t;
-                       std::map<endomain_t, int> mergeability_class;
-
-                       for (auto cell : all_cells) {
+                       for (auto cell : all_cells)
                                for (auto &conn : cell->connections())
                                for (auto bit : assign_map(conn.second))
                                        if (bit.wire != nullptr) {
@@ -1110,56 +1110,58 @@ struct Abc9Pass : public Pass {
                                                }
                                        }
 
+                       for (auto cell : all_cells) {
                                auto inst_module = design->module(cell->type);
                                if (!inst_module || !inst_module->attributes.count("\\abc9_flop"))
                                        continue;
 
-                               auto derived_name = inst_module->derive(design, cell->parameters);
-                               auto derived_module = design->module(derived_name);
-                               log_assert(derived_module);
-                               if (derived_module->has_processes())
-                                       Pass::call_on_module(design, derived_module, "proc");
-                               SigMap derived_sigmap(derived_module);
-
-                               SigSpec pattern;
-                               SigSpec with;
-                               for (auto &conn : cell->connections()) {
-                                       Wire *first = derived_module->wire(conn.first);
-                                       log_assert(first);
-                                       SigSpec second = assign_map(conn.second);
-                                       log_assert(GetSize(first) == GetSize(second));
-                                       pattern.append(first);
-                                       with.append(second);
-                               }
-
-                               Wire *abc9_clock_wire = derived_module->wire("\\$abc9_clock");
+                               Wire *abc9_clock_wire = module->wire(stringf("%s.$abc9_clock", cell->name.c_str()));
                                if (abc9_clock_wire == NULL)
-                                       log_error("'\\$abc9_clock' is not a wire present in module '%s'.\n", log_id(cell->type));
-                               SigSpec abc9_clock = derived_sigmap(abc9_clock_wire);
-                               abc9_clock.replace(pattern, with);
-                               for (const auto &c : abc9_clock.chunks())
-                                       log_assert(!c.wire || c.wire->module == module);
-
-                               Wire *abc9_control_wire = derived_module->wire("\\$abc9_control");
-                               if (abc9_control_wire == NULL)
-                                       log_error("'\\$abc9_control' is not a wire present in module '%s'.\n", log_id(cell->type));
-                               SigSpec abc9_control = derived_sigmap(abc9_control_wire);
-                               abc9_control.replace(pattern, with);
-                               for (const auto &c : abc9_control.chunks())
-                                       log_assert(!c.wire || c.wire->module == module);
+                                       log_error("'%s$abc9_clock' is not a wire present in module '%s'.\n", cell->name.c_str(), log_id(module));
+                               SigSpec abc9_clock = assign_map(abc9_clock_wire);
 
                                unassigned_cells.erase(cell);
-                               expand_queue.insert(cell);
                                expand_queue_up.insert(cell);
-                               expand_queue_down.insert(cell);
+                               clkdomain_t key(abc9_clock);
+                               assigned_cells[key].insert(cell->name);
+                               assigned_cells_reverse[cell] = key;
 
-                               assigned_cells[abc9_clock].insert(cell->name);
-                               assigned_cells_reverse[cell] = abc9_clock;
+                               auto YS_ATTRIBUTE(unused) r2 = cell->attributes.insert(std::make_pair(ID(abc9_mergeability), 1));
+                               log_assert(r2.second);
 
-                               endomain_t key(cell->type, abc9_control);
-                               auto r = mergeability_class.emplace(key, mergeability_class.size() + 1);
-                               auto YS_ATTRIBUTE(unused) r2 = cell->attributes.insert(std::make_pair(ID(abc9_mergeability),  r.first->second));
+                               Wire *abc9_init_wire = module->wire(stringf("%s.$abc9_init", cell->name.c_str()));
+                               if (abc9_init_wire == NULL)
+                                   log_error("'%s.$abc9_init' is not a wire present in module '%s'.\n", cell->name.c_str(), log_id(module));
+                               log_assert(GetSize(abc9_init_wire) == 1);
+                               SigSpec abc9_init = assign_map(abc9_init_wire);
+                               if (!abc9_init.is_fully_const())
+                                   log_error("'%s.$abc9_init' is not a constant wire present in module '%s'.\n", cell->name.c_str(), log_id(module));
+                               r2 = cell->attributes.insert(std::make_pair(ID(abc9_init), abc9_init.as_const()));
                                log_assert(r2.second);
+
+                               // Also assign these special ABC9 cells to the
+                               //   same clock domain
+                               for (auto b : cell_to_bit_down[cell])
+                               for (auto c : bit_to_cell_down[b])
+                                       if (c->type == "$__ABC9_FF_") {
+                                               cell = c;
+                                               unassigned_cells.erase(cell);
+                                               assigned_cells[key].insert(cell->name);
+                                               assigned_cells_reverse[cell] = key;
+                                               break;
+                                       }
+                               for (auto b : cell_to_bit_down[cell])
+                               for (auto c : bit_to_cell_down[b])
+                                       if (c->type == "$__ABC9_ASYNC") {
+                                               cell = c;
+                                               unassigned_cells.erase(cell);
+                                               assigned_cells[key].insert(cell->name);
+                                               assigned_cells_reverse[cell] = key;
+                                               break;
+                                       }
+
+                               expand_queue.insert(cell);
+                               expand_queue_down.insert(cell);
                        }
 
                        while (!expand_queue_up.empty() || !expand_queue_down.empty())
@@ -1167,7 +1169,7 @@ struct Abc9Pass : public Pass {
                                if (!expand_queue_up.empty())
                                {
                                        RTLIL::Cell *cell = *expand_queue_up.begin();
-                                       SigSpec key = assigned_cells_reverse.at(cell);
+                                       auto key = assigned_cells_reverse.at(cell);
                                        expand_queue_up.erase(cell);
 
                                        for (auto bit : cell_to_bit_up[cell])
@@ -1184,7 +1186,7 @@ struct Abc9Pass : public Pass {
                                if (!expand_queue_down.empty())
                                {
                                        RTLIL::Cell *cell = *expand_queue_down.begin();
-                                       SigSpec key = assigned_cells_reverse.at(cell);
+                                       auto key = assigned_cells_reverse.at(cell);
                                        expand_queue_down.erase(cell);
 
                                        for (auto bit : cell_to_bit_down[cell])
@@ -1207,7 +1209,7 @@ struct Abc9Pass : public Pass {
                        while (!expand_queue.empty())
                        {
                                RTLIL::Cell *cell = *expand_queue.begin();
-                               SigSpec key = assigned_cells_reverse.at(cell);
+                               auto key = assigned_cells_reverse.at(cell);
                                expand_queue.erase(cell);
 
                                for (auto bit : cell_to_bit.at(cell)) {
@@ -1225,7 +1227,7 @@ struct Abc9Pass : public Pass {
                                        expand_queue.swap(next_expand_queue);
                        }
 
-                       SigSpec key;
+                       clkdomain_t key;
                        for (auto cell : unassigned_cells) {
                                assigned_cells[key].insert(cell->name);
                                assigned_cells_reverse[cell] = key;
@@ -1238,11 +1240,24 @@ struct Abc9Pass : public Pass {
                        design->selection_stack.emplace_back(false);
                        design->selected_active_module = module->name.str();
                        for (auto &it : assigned_cells) {
+                               std::string target = delay_target;
+                               if (target.empty()) {
+                                       for (auto b : assign_map(it.first))
+                                               if (b.wire) {
+                                                       auto jt = b.wire->attributes.find("\\abc9_period");
+                                                       if (jt != b.wire->attributes.end()) {
+                                                               target = stringf("-D %d", jt->second.as_int());
+                                                               log("Target period = %s ps for clock domain %s\n", target.c_str(), log_signal(it.first));
+                                                               break;
+                                                       }
+                                               }
+                               }
                                RTLIL::Selection& sel = design->selection_stack.back();
                                sel.selected_members[module->name] = std::move(it.second);
                                abc9_module(design, module, script_file, exe_file, cleanup, lut_costs, false, "$",
-                                               keepff, delay_target, lutin_shared, fast_mode, show_tempdir,
-                                               box_file, lut_file, wire_delay, box_lookup);
+                                               keepff, target, lutin_shared, fast_mode, show_tempdir,
+                                               box_file, lut_file, wire_delay, box_lookup, nomfs);
+                               assign_map.set(module);
                        }
                        design->selection_stack.pop_back();
                        design->selected_active_module.clear();