From 985a29ff3b1f9ce19b091aa2541dc07dbc669341 Mon Sep 17 00:00:00 2001 From: Alberto Gonzalez Date: Sun, 5 Apr 2020 07:07:50 +0000 Subject: [PATCH 01/28] Clean up pseudo-private member usage, superfluous `std::vector` instantiation, and `RTLIL::id2cstr()` usage in `passes/techmap/techmap.cc`. --- passes/techmap/techmap.cc | 146 ++++++++++++++++++-------------------- 1 file changed, 70 insertions(+), 76 deletions(-) diff --git a/passes/techmap/techmap.cc b/passes/techmap/techmap.cc index a554be257..f300c2f72 100644 --- a/passes/techmap/techmap.cc +++ b/passes/techmap/techmap.cc @@ -54,8 +54,8 @@ void apply_prefix(IdString prefix, RTLIL::SigSpec &sig, RTLIL::Module *module) if (chunk.wire != NULL) { IdString wire_name = chunk.wire->name; apply_prefix(prefix, wire_name); - log_assert(module->wires_.count(wire_name) > 0); - chunk.wire = module->wires_[wire_name]; + log_assert(module->wire(wire_name) != nullptr); + chunk.wire = module->wire(wire_name); } sig = chunks; } @@ -132,8 +132,8 @@ struct TechmapWorker if (module == NULL) return result; - for (auto &it : module->wires_) { - const char *p = it.first.c_str(); + for (auto w : module->wires()) { + const char *p = w->name.c_str(); if (*p == '$') continue; @@ -142,11 +142,11 @@ struct TechmapWorker if (!strncmp(p, "_TECHMAP_", 9)) { TechmapWireData record; - record.wire = it.second; - record.value = it.second; + record.wire = w; + record.value = w; result[p].push_back(record); - it.second->attributes[ID::keep] = RTLIL::Const(1); - it.second->attributes[ID::_techmap_special_] = RTLIL::Const(1); + w->attributes[ID::keep] = RTLIL::Const(1); + w->attributes[ID::_techmap_special_] = RTLIL::Const(1); } } @@ -165,7 +165,7 @@ struct TechmapWorker if (tpl->processes.size() != 0) { log("Technology map yielded processes:"); for (auto &it : tpl->processes) - log(" %s",RTLIL::id2cstr(it.first)); + log(" %s",log_id(it.first)); log("\n"); if (autoproc_mode) { Pass::call_on_module(tpl->design, tpl, "proc"); @@ -179,8 +179,8 @@ struct TechmapWorker orig_cell_name = cell->name.str(); if (!flatten_mode) { - for (auto &it : tpl->cells_) - if (it.first == ID::_TECHMAP_REPLACE_) { + for (auto tpl_cell : tpl->cells()) + if (tpl_cell->name == ID::_TECHMAP_REPLACE_) { module->rename(cell, stringf("$techmap%d", autoidx++) + cell->name.str()); break; } @@ -208,26 +208,26 @@ struct TechmapWorker dict temp_renamed_wires; pool autopurge_tpl_bits; - for (auto &it : tpl->wires_) + for (auto tpl_w : tpl->wires()) { - if (it.second->port_id > 0) + if (tpl_w->port_id > 0) { - IdString posportname = stringf("$%d", it.second->port_id); - positional_ports[posportname] = it.first; + IdString posportname = stringf("$%d", tpl_w->port_id); + positional_ports[posportname] = tpl_w->name; - if (!flatten_mode && it.second->get_bool_attribute(ID::techmap_autopurge) && - (!cell->hasPort(it.second->name) || !GetSize(cell->getPort(it.second->name))) && + if (!flatten_mode && tpl_w->get_bool_attribute(ID::techmap_autopurge) && + (!cell->hasPort(tpl_w->name) || !GetSize(cell->getPort(tpl_w->name))) && (!cell->hasPort(posportname) || !GetSize(cell->getPort(posportname)))) { if (sigmaps.count(tpl) == 0) sigmaps[tpl].set(tpl); - for (auto bit : sigmaps.at(tpl)(it.second)) + for (auto bit : sigmaps.at(tpl)(tpl_w)) if (bit.wire != nullptr) autopurge_tpl_bits.insert(bit); } } - IdString w_name = it.second->name; + IdString w_name = tpl_w->name; apply_prefix(cell->name, w_name); RTLIL::Wire *w = module->wire(w_name); if (w != nullptr) { @@ -237,30 +237,30 @@ struct TechmapWorker w = nullptr; } else { w->attributes.erase(ID::hierconn); - if (GetSize(w) < GetSize(it.second)) { + if (GetSize(w) < GetSize(tpl_w)) { log_warning("Widening signal %s.%s to match size of %s.%s (via %s.%s).\n", log_id(module), log_id(w), - log_id(tpl), log_id(it.second), log_id(module), log_id(cell)); - w->width = GetSize(it.second); + log_id(tpl), log_id(tpl_w), log_id(module), log_id(cell)); + w->width = GetSize(tpl_w); } } } if (w == nullptr) { - w = module->addWire(w_name, it.second); + w = module->addWire(w_name, tpl_w); w->port_input = false; w->port_output = false; w->port_id = 0; if (!flatten_mode) w->attributes.erase(ID::techmap_autopurge); - if (it.second->get_bool_attribute(ID::_techmap_special_)) + if (tpl_w->get_bool_attribute(ID::_techmap_special_)) w->attributes.clear(); if (w->attributes.count(ID::src)) w->add_strpool_attribute(ID::src, extra_src_attrs); } design->select(module, w); - if (it.second->name.begins_with("\\_TECHMAP_REPLACE_.")) { - IdString replace_name = stringf("%s%s", orig_cell_name.c_str(), it.second->name.c_str() + strlen("\\_TECHMAP_REPLACE_")); - Wire *replace_w = module->addWire(replace_name, it.second); + if (tpl_w->name.begins_with("\\_TECHMAP_REPLACE_.")) { + IdString replace_name = stringf("%s%s", orig_cell_name.c_str(), tpl_w->name.c_str() + strlen("\\_TECHMAP_REPLACE_")); + Wire *replace_w = module->addWire(replace_name, tpl_w); module->connect(replace_w, w); } } @@ -268,9 +268,9 @@ struct TechmapWorker SigMap tpl_sigmap(tpl); pool tpl_written_bits; - for (auto &it1 : tpl->cells_) - for (auto &it2 : it1.second->connections_) - if (it1.second->output(it2.first)) + for (auto tpl_cell : tpl->cells()) + for (auto &it2 : tpl_cell->connections_) + if (tpl_cell->output(it2.first)) for (auto bit : tpl_sigmap(it2.second)) tpl_written_bits.insert(bit); for (auto &it1 : tpl->connections_) @@ -285,7 +285,7 @@ struct TechmapWorker RTLIL::IdString portname = it.first; if (positional_ports.count(portname) > 0) portname = positional_ports.at(portname); - if (tpl->wires_.count(portname) == 0 || tpl->wires_.at(portname)->port_id == 0) { + if (tpl->wire(portname) == nullptr || tpl->wire(portname)->port_id == 0) { if (portname.begins_with("$")) log_error("Can't map port `%s' of cell `%s' to template `%s'!\n", portname.c_str(), cell->name.c_str(), tpl->name.c_str()); continue; @@ -294,7 +294,7 @@ struct TechmapWorker if (GetSize(it.second) == 0) continue; - RTLIL::Wire *w = tpl->wires_.at(portname); + RTLIL::Wire *w = tpl->wire(portname); RTLIL::SigSig c, extra_connect; if (w->port_output && !w->port_input) { @@ -377,19 +377,19 @@ struct TechmapWorker } } - for (auto &it : tpl->cells_) + for (auto tpl_cell : tpl->cells()) { - IdString c_name = it.second->name.str(); + IdString c_name = tpl_cell->name; bool techmap_replace_cell = (!flatten_mode) && (c_name == ID::_TECHMAP_REPLACE_); if (techmap_replace_cell) c_name = orig_cell_name; - else if (it.second->name.begins_with("\\_TECHMAP_REPLACE_.")) + else if (tpl_cell->name.begins_with("\\_TECHMAP_REPLACE_.")) c_name = stringf("%s%s", orig_cell_name.c_str(), c_name.c_str() + strlen("\\_TECHMAP_REPLACE_")); else apply_prefix(cell->name, c_name); - RTLIL::Cell *c = module->addCell(c_name, it.second); + RTLIL::Cell *c = module->addCell(c_name, tpl_cell); design->select(module, c); if (!flatten_mode && c->type.begins_with("\\$")) @@ -493,9 +493,9 @@ struct TechmapWorker std::map> cell_to_inbit; std::map> outbit_to_cell; - for (auto cell : module->cells()) + for (auto cell : module->selected_cells()) { - if (!design->selected(module, cell) || handled_cells.count(cell) > 0) + if (handled_cells.count(cell) > 0) continue; std::string cell_type = cell->type.str(); @@ -511,7 +511,7 @@ struct TechmapWorker if (flatten_mode) { bool keepit = cell->get_bool_attribute(ID::keep_hierarchy); for (auto &tpl_name : celltypeMap.at(cell_type)) - if (map->modules_[tpl_name]->get_bool_attribute(ID::keep_hierarchy)) + if (map->module(tpl_name)->get_bool_attribute(ID::keep_hierarchy)) keepit = true; if (keepit) { if (!flatten_keep_list[cell]) { @@ -533,7 +533,7 @@ struct TechmapWorker continue; for (auto &tpl_name : celltypeMap.at(cell_type)) { - RTLIL::Module *tpl = map->modules_[tpl_name]; + RTLIL::Module *tpl = map->module(tpl_name); RTLIL::Wire *port = tpl->wire(conn.first); if (port && port->port_input) cell_to_inbit[cell].insert(sig.begin(), sig.end()); @@ -567,7 +567,7 @@ struct TechmapWorker for (auto &tpl_name : celltypeMap.at(cell_type)) { RTLIL::IdString derived_name = tpl_name; - RTLIL::Module *tpl = map->modules_[tpl_name]; + RTLIL::Module *tpl = map->module(tpl_name); std::map parameters(cell->parameters.begin(), cell->parameters.end()); if (tpl->get_blackbox_attribute(ignore_wb)) @@ -675,7 +675,7 @@ struct TechmapWorker if (extmapper_name == "simplemap") { if (simplemap_mappers.count(cell->type) == 0) - log_error("No simplemap mapper for cell type %s found!\n", RTLIL::id2cstr(cell->type)); + log_error("No simplemap mapper for cell type %s found!\n", log_id(cell->type)); simplemap_mappers.at(cell->type)(module, cell); } @@ -697,7 +697,7 @@ struct TechmapWorker for (auto conn : cell->connections()) { if (conn.first.begins_with("$")) continue; - if (tpl->wires_.count(conn.first) > 0 && tpl->wires_.at(conn.first)->port_id > 0) + if (tpl->wire(conn.first) != nullptr && tpl->wire(conn.first)->port_id > 0) continue; if (!conn.second.is_fully_const() || parameters.count(conn.first) > 0 || tpl->avail_parameters.count(conn.first) == 0) goto next_tpl; @@ -713,20 +713,20 @@ struct TechmapWorker parameters[ID::_TECHMAP_CELLTYPE_] = RTLIL::unescape_id(cell->type); for (auto conn : cell->connections()) { - if (tpl->avail_parameters.count(stringf("\\_TECHMAP_CONSTMSK_%s_", RTLIL::id2cstr(conn.first))) != 0) { + if (tpl->avail_parameters.count(stringf("\\_TECHMAP_CONSTMSK_%s_", log_id(conn.first))) != 0) { std::vector v = sigmap(conn.second).to_sigbit_vector(); for (auto &bit : v) bit = RTLIL::SigBit(bit.wire == NULL ? RTLIL::State::S1 : RTLIL::State::S0); - parameters[stringf("\\_TECHMAP_CONSTMSK_%s_", RTLIL::id2cstr(conn.first))] = RTLIL::SigSpec(v).as_const(); + parameters[stringf("\\_TECHMAP_CONSTMSK_%s_", log_id(conn.first))] = RTLIL::SigSpec(v).as_const(); } - if (tpl->avail_parameters.count(stringf("\\_TECHMAP_CONSTVAL_%s_", RTLIL::id2cstr(conn.first))) != 0) { + if (tpl->avail_parameters.count(stringf("\\_TECHMAP_CONSTVAL_%s_", log_id(conn.first))) != 0) { std::vector v = sigmap(conn.second).to_sigbit_vector(); for (auto &bit : v) if (bit.wire != NULL) bit = RTLIL::SigBit(RTLIL::State::Sx); - parameters[stringf("\\_TECHMAP_CONSTVAL_%s_", RTLIL::id2cstr(conn.first))] = RTLIL::SigSpec(v).as_const(); + parameters[stringf("\\_TECHMAP_CONSTVAL_%s_", log_id(conn.first))] = RTLIL::SigSpec(v).as_const(); } - if (tpl->avail_parameters.count(stringf("\\_TECHMAP_WIREINIT_%s_", RTLIL::id2cstr(conn.first))) != 0) { + if (tpl->avail_parameters.count(stringf("\\_TECHMAP_WIREINIT_%s_", log_id(conn.first))) != 0) { auto sig = sigmap(conn.second); RTLIL::Const value(State::Sx, sig.size()); for (int i = 0; i < sig.size(); i++) { @@ -735,7 +735,7 @@ struct TechmapWorker value[i] = it->second; } } - parameters[stringf("\\_TECHMAP_WIREINIT_%s_", RTLIL::id2cstr(conn.first))] = value; + parameters[stringf("\\_TECHMAP_WIREINIT_%s_", log_id(conn.first))] = value; } } @@ -747,8 +747,8 @@ struct TechmapWorker unique_bit_id[RTLIL::State::Sz] = unique_bit_id_counter++; for (auto conn : cell->connections()) - if (tpl->avail_parameters.count(stringf("\\_TECHMAP_CONNMAP_%s_", RTLIL::id2cstr(conn.first))) != 0) { - for (auto &bit : sigmap(conn.second).to_sigbit_vector()) + if (tpl->avail_parameters.count(stringf("\\_TECHMAP_CONNMAP_%s_", log_id(conn.first))) != 0) { + for (auto &bit : sigmap(conn.second)) if (unique_bit_id.count(bit) == 0) unique_bit_id[bit] = unique_bit_id_counter++; } @@ -764,13 +764,13 @@ struct TechmapWorker parameters[ID::_TECHMAP_BITS_CONNMAP_] = bits; for (auto conn : cell->connections()) - if (tpl->avail_parameters.count(stringf("\\_TECHMAP_CONNMAP_%s_", RTLIL::id2cstr(conn.first))) != 0) { + if (tpl->avail_parameters.count(stringf("\\_TECHMAP_CONNMAP_%s_", log_id(conn.first))) != 0) { RTLIL::Const value; - for (auto &bit : sigmap(conn.second).to_sigbit_vector()) { + for (auto &bit : sigmap(conn.second)) { RTLIL::Const chunk(unique_bit_id.at(bit), bits); value.bits.insert(value.bits.end(), chunk.bits.begin(), chunk.bits.end()); } - parameters[stringf("\\_TECHMAP_CONNMAP_%s_", RTLIL::id2cstr(conn.first))] = value; + parameters[stringf("\\_TECHMAP_CONNMAP_%s_", log_id(conn.first))] = value; } } @@ -819,7 +819,7 @@ struct TechmapWorker RTLIL::SigSpec value = it.value; if (value.is_fully_const() && value.as_bool()) { log("Not using module `%s' from techmap as it contains a %s marker wire with non-zero value %s.\n", - derived_name.c_str(), RTLIL::id2cstr(it.wire->name), log_signal(value)); + derived_name.c_str(), log_id(it.wire->name), log_signal(value)); techmap_do_cache[tpl] = false; } } @@ -835,7 +835,7 @@ struct TechmapWorker auto &data = it.second.front(); if (!data.value.is_fully_const()) - log_error("Techmap yielded config wire %s with non-const value %s.\n", RTLIL::id2cstr(data.wire->name), log_signal(data.value)); + log_error("Techmap yielded config wire %s with non-const value %s.\n", log_id(data.wire->name), log_signal(data.value)); techmap_wire_names.erase(it.first); @@ -926,7 +926,7 @@ struct TechmapWorker log_assert(!strncmp(q, "_TECHMAP_DO_", 12)); std::string new_name = data.wire->name.substr(0, q-p) + "_TECHMAP_DONE_" + data.wire->name.substr(q-p+12); - while (tpl->wires_.count(new_name)) + while (tpl->wire(new_name) != nullptr) new_name += "_"; tpl->rename(data.wire->name, new_name); @@ -942,12 +942,12 @@ struct TechmapWorker if (techmap_do_cache[tpl]) for (auto &it2 : it.second) if (!it2.value.is_fully_const()) - log_error("Techmap yielded config wire %s with non-const value %s.\n", RTLIL::id2cstr(it2.wire->name), log_signal(it2.value)); + log_error("Techmap yielded config wire %s with non-const value %s.\n", log_id(it2.wire->name), log_signal(it2.value)); techmap_wire_names.erase(it.first); } for (auto &it : techmap_wire_names) - log_error("Techmap special wire %s disappeared. This is considered a fatal error.\n", RTLIL::id2cstr(it)); + log_error("Techmap special wire %s disappeared. This is considered a fatal error.\n", log_id(it)); if (recursive_mode) { if (log_continue) { @@ -1301,17 +1301,16 @@ struct TechmapPass : public Pass { log_header(design, "Continuing TECHMAP pass.\n"); std::map> celltypeMap; - for (auto &it : map->modules_) { - if (it.second->attributes.count(ID::techmap_celltype) && !it.second->attributes.at(ID::techmap_celltype).bits.empty()) { - char *p = strdup(it.second->attributes.at(ID::techmap_celltype).decode_string().c_str()); + for (auto module : map->modules()) { + if (module->attributes.count(ID::techmap_celltype) && !module->attributes.at(ID::techmap_celltype).bits.empty()) { + char *p = strdup(module->attributes.at(ID::techmap_celltype).decode_string().c_str()); for (char *q = strtok(p, " \t\r\n"); q; q = strtok(NULL, " \t\r\n")) - celltypeMap[RTLIL::escape_id(q)].insert(it.first); + celltypeMap[RTLIL::escape_id(q)].insert(module->name); free(p); } else { - string module_name = it.first.str(); - if (it.first.begins_with("\\$")) - module_name = module_name.substr(1); - celltypeMap[module_name].insert(it.first); + std::string module_name = module->name.begins_with("\\$") ? + module->name.substr(1) : module->name.str(); + celltypeMap[module_name].insert(module->name); } } @@ -1402,9 +1401,8 @@ struct FlattenPass : public Pass { worker.flatten_do_list.erase(mod->name); } } else { - for (auto mod : vector(design->modules())) { + for (auto mod : design->modules().to_vector()) while (worker.techmap_module(design, mod, design, handled_cells, celltypeMap, false)) { } - } } log_suppressed(); @@ -1425,15 +1423,11 @@ struct FlattenPass : public Pass { new_used_modules.insert(cell->type); } - dict new_modules; - for (auto mod : vector(design->modules())) - if (used_modules[mod->name] || mod->get_blackbox_attribute(worker.ignore_wb)) { - new_modules[mod->name] = mod; - } else { + for (auto mod : design->modules()) + if (!used_modules[mod->name] && !mod->get_blackbox_attribute(worker.ignore_wb)) { log("Deleting now unused module %s.\n", log_id(mod)); - delete mod; + design->remove(mod); } - design->modules_.swap(new_modules); } log_pop(); From e49fdee40441cb4b1892007560d56467fd75a798 Mon Sep 17 00:00:00 2001 From: Alberto Gonzalez Date: Fri, 10 Apr 2020 07:19:05 +0000 Subject: [PATCH 02/28] Do not modify design modules while iterating over `modules()`. Co-Authored-By: Eddie Hung --- passes/techmap/techmap.cc | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/passes/techmap/techmap.cc b/passes/techmap/techmap.cc index f300c2f72..930b4c416 100644 --- a/passes/techmap/techmap.cc +++ b/passes/techmap/techmap.cc @@ -1423,11 +1423,14 @@ struct FlattenPass : public Pass { new_used_modules.insert(cell->type); } + std::set to_remove; for (auto mod : design->modules()) if (!used_modules[mod->name] && !mod->get_blackbox_attribute(worker.ignore_wb)) { log("Deleting now unused module %s.\n", log_id(mod)); - design->remove(mod); + to_remove.insert(mod); } + for (auto mod : to_remove) + design->remove(mod); } log_pop(); From 5f7f213c7f3182169c5efe12c9c8e9ceec3ef81f Mon Sep 17 00:00:00 2001 From: Alberto Gonzalez Date: Tue, 14 Apr 2020 17:38:43 +0000 Subject: [PATCH 03/28] Replace `std::string` and `RTLIL::IdString` with `IdString` in `passes/techmap/techmap.cc`. Co-Authored-By: Eddie Hung --- passes/techmap/techmap.cc | 42 +++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/passes/techmap/techmap.cc b/passes/techmap/techmap.cc index 930b4c416..dd5369ed0 100644 --- a/passes/techmap/techmap.cc +++ b/passes/techmap/techmap.cc @@ -62,10 +62,10 @@ void apply_prefix(IdString prefix, RTLIL::SigSpec &sig, RTLIL::Module *module) struct TechmapWorker { - std::map simplemap_mappers; - std::map>, RTLIL::Module*> techmap_cache; + std::map simplemap_mappers; + std::map>, RTLIL::Module*> techmap_cache; std::map techmap_do_cache; - std::set> module_queue; + std::set> module_queue; dict sigmaps; pool flatten_do_list; @@ -79,7 +79,7 @@ struct TechmapWorker RTLIL::SigSpec value; }; - typedef std::map> TechmapWires; + typedef std::map> TechmapWires; bool extern_mode; bool assert_mode; @@ -101,7 +101,7 @@ struct TechmapWorker std::string constmap_tpl_name(SigMap &sigmap, RTLIL::Module *tpl, RTLIL::Cell *cell, bool verbose) { std::string constmap_info; - std::map> connbits_map; + std::map> connbits_map; for (auto conn : cell->connections()) for (int i = 0; i < GetSize(conn.second); i++) { @@ -117,7 +117,7 @@ struct TechmapWorker constmap_info += stringf("|%s %d %s %d", log_id(conn.first), i, log_id(connbits_map.at(bit).first), connbits_map.at(bit).second); } else { - connbits_map[bit] = std::pair(conn.first, i); + connbits_map[bit] = std::pair(conn.first, i); constmap_info += stringf("|%s %d", log_id(conn.first), i); } } @@ -204,7 +204,7 @@ struct TechmapWorker design->select(module, m); } - std::map positional_ports; + std::map positional_ports; dict temp_renamed_wires; pool autopurge_tpl_bits; @@ -282,7 +282,7 @@ struct TechmapWorker for (auto &it : cell->connections()) { - RTLIL::IdString portname = it.first; + IdString portname = it.first; if (positional_ports.count(portname) > 0) portname = positional_ports.at(portname); if (tpl->wire(portname) == nullptr || tpl->wire(portname)->port_id == 0) { @@ -464,7 +464,7 @@ struct TechmapWorker } bool techmap_module(RTLIL::Design *design, RTLIL::Module *module, RTLIL::Design *map, std::set &handled_cells, - const std::map> &celltypeMap, bool in_recursion) + const std::map> &celltypeMap, bool in_recursion) { std::string mapmsg_prefix = in_recursion ? "Recursively mapping" : "Mapping"; @@ -489,7 +489,7 @@ struct TechmapWorker } } - TopoSort> cells; + TopoSort> cells; std::map> cell_to_inbit; std::map> outbit_to_cell; @@ -566,9 +566,9 @@ struct TechmapWorker for (auto &tpl_name : celltypeMap.at(cell_type)) { - RTLIL::IdString derived_name = tpl_name; + IdString derived_name = tpl_name; RTLIL::Module *tpl = map->module(tpl_name); - std::map parameters(cell->parameters.begin(), cell->parameters.end()); + std::map parameters(cell->parameters.begin(), cell->parameters.end()); if (tpl->get_blackbox_attribute(ignore_wb)) continue; @@ -778,13 +778,13 @@ struct TechmapWorker use_wrapper_tpl:; // do not register techmap_wrap modules with techmap_cache } else { - std::pair> key(tpl_name, parameters); + std::pair> key(tpl_name, parameters); if (techmap_cache.count(key) > 0) { tpl = techmap_cache[key]; } else { if (parameters.size() != 0) { mkdebug.on(); - derived_name = tpl->derive(map, dict(parameters.begin(), parameters.end())); + derived_name = tpl->derive(map, dict(parameters.begin(), parameters.end())); tpl = map->module(derived_name); log_continue = true; } @@ -805,7 +805,7 @@ struct TechmapWorker bool keep_running = true; techmap_do_cache[tpl] = true; - std::set techmap_wire_names; + std::set techmap_wire_names; while (keep_running) { @@ -851,7 +851,7 @@ struct TechmapWorker cmd_string = cmd_string.substr(strlen("CONSTMAP; ")); log("Analyzing pattern of constant bits for this cell:\n"); - RTLIL::IdString new_tpl_name = constmap_tpl_name(sigmap, tpl, cell, true); + IdString new_tpl_name = constmap_tpl_name(sigmap, tpl, cell, true); log("Creating constmapped module `%s'.\n", log_id(new_tpl_name)); log_assert(map->module(new_tpl_name) == nullptr); @@ -871,7 +871,7 @@ struct TechmapWorker if (!wire->port_input || wire->port_output) continue; - RTLIL::IdString port_name = wire->name; + IdString port_name = wire->name; tpl->rename(wire, NEW_ID); RTLIL::Wire *new_wire = tpl->addWire(port_name, wire); @@ -1300,7 +1300,7 @@ struct TechmapPass : public Pass { log_header(design, "Continuing TECHMAP pass.\n"); - std::map> celltypeMap; + std::map> celltypeMap; for (auto module : map->modules()) { if (module->attributes.count(ID::techmap_celltype) && !module->attributes.at(ID::techmap_celltype).bits.empty()) { char *p = strdup(module->attributes.at(ID::techmap_celltype).decode_string().c_str()); @@ -1381,7 +1381,7 @@ struct FlattenPass : public Pass { extra_args(args, argidx, design); - std::map> celltypeMap; + std::map> celltypeMap; for (auto module : design->modules()) celltypeMap[module->name].insert(module->name); @@ -1410,10 +1410,10 @@ struct FlattenPass : public Pass { if (top_mod != NULL) { - pool used_modules, new_used_modules; + pool used_modules, new_used_modules; new_used_modules.insert(top_mod->name); while (!new_used_modules.empty()) { - pool queue; + pool queue; queue.swap(new_used_modules); for (auto modname : queue) used_modules.insert(modname); From 99b586b28367e1aea8c9e75a8132079c4a3687f9 Mon Sep 17 00:00:00 2001 From: Alberto Gonzalez Date: Tue, 14 Apr 2020 17:44:33 +0000 Subject: [PATCH 04/28] Use `nullptr` instead of `NULL` in `passes/techmap/techmap.cc`. --- passes/techmap/techmap.cc | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/passes/techmap/techmap.cc b/passes/techmap/techmap.cc index dd5369ed0..99ec92f33 100644 --- a/passes/techmap/techmap.cc +++ b/passes/techmap/techmap.cc @@ -51,7 +51,7 @@ void apply_prefix(IdString prefix, RTLIL::SigSpec &sig, RTLIL::Module *module) { vector chunks = sig; for (auto &chunk : chunks) - if (chunk.wire != NULL) { + if (chunk.wire != nullptr) { IdString wire_name = chunk.wire->name; apply_prefix(prefix, wire_name); log_assert(module->wire(wire_name) != nullptr); @@ -129,7 +129,7 @@ struct TechmapWorker { TechmapWires result; - if (module == NULL) + if (module == nullptr) return result; for (auto w : module->wires()) { @@ -686,7 +686,7 @@ struct TechmapWorker } module->remove(cell); - cell = NULL; + cell = nullptr; } did_something = true; @@ -716,13 +716,13 @@ struct TechmapWorker if (tpl->avail_parameters.count(stringf("\\_TECHMAP_CONSTMSK_%s_", log_id(conn.first))) != 0) { std::vector v = sigmap(conn.second).to_sigbit_vector(); for (auto &bit : v) - bit = RTLIL::SigBit(bit.wire == NULL ? RTLIL::State::S1 : RTLIL::State::S0); + bit = RTLIL::SigBit(bit.wire == nullptr ? RTLIL::State::S1 : RTLIL::State::S0); parameters[stringf("\\_TECHMAP_CONSTMSK_%s_", log_id(conn.first))] = RTLIL::SigSpec(v).as_const(); } if (tpl->avail_parameters.count(stringf("\\_TECHMAP_CONSTVAL_%s_", log_id(conn.first))) != 0) { std::vector v = sigmap(conn.second).to_sigbit_vector(); for (auto &bit : v) - if (bit.wire != NULL) + if (bit.wire != nullptr) bit = RTLIL::SigBit(RTLIL::State::Sx); parameters[stringf("\\_TECHMAP_CONSTVAL_%s_", log_id(conn.first))] = RTLIL::SigSpec(v).as_const(); } @@ -1015,7 +1015,7 @@ struct TechmapWorker } log_debug("%s %s.%s (%s) using %s.\n", mapmsg_prefix.c_str(), log_id(module), log_id(cell), log_id(cell->type), log_id(tpl)); techmap_module_worker(design, module, cell, tpl); - cell = NULL; + cell = nullptr; } did_something = true; mapped_cell = true; @@ -1304,7 +1304,7 @@ struct TechmapPass : public Pass { for (auto module : map->modules()) { if (module->attributes.count(ID::techmap_celltype) && !module->attributes.at(ID::techmap_celltype).bits.empty()) { char *p = strdup(module->attributes.at(ID::techmap_celltype).decode_string().c_str()); - for (char *q = strtok(p, " \t\r\n"); q; q = strtok(NULL, " \t\r\n")) + for (char *q = strtok(p, " \t\r\n"); q; q = strtok(nullptr, " \t\r\n")) celltypeMap[RTLIL::escape_id(q)].insert(module->name); free(p); } else { @@ -1385,14 +1385,14 @@ struct FlattenPass : public Pass { for (auto module : design->modules()) celltypeMap[module->name].insert(module->name); - RTLIL::Module *top_mod = NULL; + RTLIL::Module *top_mod = nullptr; if (design->full_selection()) for (auto mod : design->modules()) if (mod->get_bool_attribute(ID::top)) top_mod = mod; std::set handled_cells; - if (top_mod != NULL) { + if (top_mod != nullptr) { worker.flatten_do_list.insert(top_mod->name); while (!worker.flatten_do_list.empty()) { auto mod = design->module(*worker.flatten_do_list.begin()); @@ -1408,7 +1408,7 @@ struct FlattenPass : public Pass { log_suppressed(); log("No more expansions possible.\n"); - if (top_mod != NULL) + if (top_mod != nullptr) { pool used_modules, new_used_modules; new_used_modules.insert(top_mod->name); From 437f3fb342f91d0c1a1a923c92312301a03cb07d Mon Sep 17 00:00:00 2001 From: Alberto Gonzalez Date: Tue, 14 Apr 2020 17:50:23 +0000 Subject: [PATCH 05/28] Replace `std::map` with `dict` for `simplemap_mappers`. --- passes/techmap/simplemap.cc | 6 +++--- passes/techmap/simplemap.h | 2 +- passes/techmap/techmap.cc | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/passes/techmap/simplemap.cc b/passes/techmap/simplemap.cc index b65b3e972..214157a64 100644 --- a/passes/techmap/simplemap.cc +++ b/passes/techmap/simplemap.cc @@ -522,7 +522,7 @@ void simplemap_dlatch(RTLIL::Module *module, RTLIL::Cell *cell) } } -void simplemap_get_mappers(std::map &mappers) +void simplemap_get_mappers(dict &mappers) { mappers[ID($not)] = simplemap_not; mappers[ID($pos)] = simplemap_pos; @@ -559,7 +559,7 @@ void simplemap_get_mappers(std::map mappers; + static dict mappers; static bool initialized_mappers = false; if (!initialized_mappers) { @@ -595,7 +595,7 @@ struct SimplemapPass : public Pass { log_header(design, "Executing SIMPLEMAP pass (map simple cells to gate primitives).\n"); extra_args(args, 1, design); - std::map mappers; + dict mappers; simplemap_get_mappers(mappers); for (auto mod : design->modules()) { diff --git a/passes/techmap/simplemap.h b/passes/techmap/simplemap.h index c2d73ea79..5091050a1 100644 --- a/passes/techmap/simplemap.h +++ b/passes/techmap/simplemap.h @@ -42,7 +42,7 @@ extern void simplemap_adff(RTLIL::Module *module, RTLIL::Cell *cell); extern void simplemap_dlatch(RTLIL::Module *module, RTLIL::Cell *cell); extern void simplemap(RTLIL::Module *module, RTLIL::Cell *cell); -extern void simplemap_get_mappers(std::map &mappers); +extern void simplemap_get_mappers(dict &mappers); YOSYS_NAMESPACE_END diff --git a/passes/techmap/techmap.cc b/passes/techmap/techmap.cc index 99ec92f33..bd7cc14a9 100644 --- a/passes/techmap/techmap.cc +++ b/passes/techmap/techmap.cc @@ -62,7 +62,7 @@ void apply_prefix(IdString prefix, RTLIL::SigSpec &sig, RTLIL::Module *module) struct TechmapWorker { - std::map simplemap_mappers; + dict simplemap_mappers; std::map>, RTLIL::Module*> techmap_cache; std::map techmap_do_cache; std::set> module_queue; From 2fb4931e5be2c2a0c80ffbca73ad74ebb8c9032f Mon Sep 17 00:00:00 2001 From: Alberto Gonzalez Date: Tue, 14 Apr 2020 18:09:05 +0000 Subject: [PATCH 06/28] Add specialized `hash()` for type `dict` and use a `dict` instead of a `std::map` for `techmap_cache` and `techmap_do_cache`. --- kernel/hashlib.h | 25 ++++++++++++++++++++----- kernel/rtlil.h | 2 +- passes/techmap/techmap.cc | 8 ++++---- 3 files changed, 25 insertions(+), 10 deletions(-) diff --git a/kernel/hashlib.h b/kernel/hashlib.h index 592d6e577..cdbad87c4 100644 --- a/kernel/hashlib.h +++ b/kernel/hashlib.h @@ -19,6 +19,12 @@ namespace hashlib { +template struct hash_ops; +template> class dict; +template> class idict; +template> class pool; +template> class mfp; + const int hashtable_size_trigger = 2; const int hashtable_size_factor = 3; @@ -100,6 +106,20 @@ template struct hash_ops> { } }; +template struct hash_ops> { + static inline bool cmp(dict a, dict b) { + return a == b; + } + static inline unsigned int hash(dict a) { + unsigned int h = mkhash_init; + for (auto &it : a) { + h = mkhash(h, hash_ops

::hash(it.first)); + h = mkhash(h, hash_ops::hash(it.second)); + } + return h; + } +}; + template struct hash_ops> { static inline bool cmp(std::tuple a, std::tuple b) { return a == b; @@ -191,11 +211,6 @@ inline int hashtable_size(int min_size) throw std::length_error("hash table exceeded maximum size."); } -template> class dict; -template> class idict; -template> class pool; -template> class mfp; - template class dict { diff --git a/kernel/rtlil.h b/kernel/rtlil.h index 11c45bbec..dad8508c3 100644 --- a/kernel/rtlil.h +++ b/kernel/rtlil.h @@ -150,7 +150,7 @@ namespace RTLIL if (!p[0]) return 0; - log_assert(p[0] == '$' || p[0] == '\\'); + log_assert(p[0] == '$' || p[0] == '\\' || strncmp(p, "_TECHMAP_", strlen("_TECHMAP_")) == 0); log_assert(p[1] != 0); auto it = global_id_index_.find((char*)p); diff --git a/passes/techmap/techmap.cc b/passes/techmap/techmap.cc index bd7cc14a9..9779ecae9 100644 --- a/passes/techmap/techmap.cc +++ b/passes/techmap/techmap.cc @@ -63,8 +63,8 @@ void apply_prefix(IdString prefix, RTLIL::SigSpec &sig, RTLIL::Module *module) struct TechmapWorker { dict simplemap_mappers; - std::map>, RTLIL::Module*> techmap_cache; - std::map techmap_do_cache; + dict>, RTLIL::Module*> techmap_cache; + dict techmap_do_cache; std::set> module_queue; dict sigmaps; @@ -568,7 +568,7 @@ struct TechmapWorker { IdString derived_name = tpl_name; RTLIL::Module *tpl = map->module(tpl_name); - std::map parameters(cell->parameters.begin(), cell->parameters.end()); + dict parameters(cell->parameters.begin(), cell->parameters.end()); if (tpl->get_blackbox_attribute(ignore_wb)) continue; @@ -778,7 +778,7 @@ struct TechmapWorker use_wrapper_tpl:; // do not register techmap_wrap modules with techmap_cache } else { - std::pair> key(tpl_name, parameters); + std::pair> key(tpl_name, parameters); if (techmap_cache.count(key) > 0) { tpl = techmap_cache[key]; } else { From 64c16f8c1364bbe45bc17a54c29d70990efb45ad Mon Sep 17 00:00:00 2001 From: Alberto Gonzalez Date: Sun, 19 Apr 2020 22:21:52 +0000 Subject: [PATCH 07/28] Replace `std::map` with `dict` for `positional_ports`. --- passes/techmap/techmap.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/passes/techmap/techmap.cc b/passes/techmap/techmap.cc index 9779ecae9..651f772c9 100644 --- a/passes/techmap/techmap.cc +++ b/passes/techmap/techmap.cc @@ -204,7 +204,7 @@ struct TechmapWorker design->select(module, m); } - std::map positional_ports; + dict positional_ports; dict temp_renamed_wires; pool autopurge_tpl_bits; From 67f4046c05622140d9d2ae93d8cc44bb680b3b20 Mon Sep 17 00:00:00 2001 From: Alberto Gonzalez Date: Sun, 19 Apr 2020 22:24:20 +0000 Subject: [PATCH 08/28] Replace `std::set` with `pool` for `handled_cells` and `techmap_wire_names`. --- passes/techmap/techmap.cc | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/passes/techmap/techmap.cc b/passes/techmap/techmap.cc index 651f772c9..55d80bad1 100644 --- a/passes/techmap/techmap.cc +++ b/passes/techmap/techmap.cc @@ -463,7 +463,7 @@ struct TechmapWorker } } - bool techmap_module(RTLIL::Design *design, RTLIL::Module *module, RTLIL::Design *map, std::set &handled_cells, + bool techmap_module(RTLIL::Design *design, RTLIL::Module *module, RTLIL::Design *map, pool &handled_cells, const std::map> &celltypeMap, bool in_recursion) { std::string mapmsg_prefix = in_recursion ? "Recursively mapping" : "Mapping"; @@ -805,7 +805,7 @@ struct TechmapWorker bool keep_running = true; techmap_do_cache[tpl] = true; - std::set techmap_wire_names; + pool techmap_wire_names; while (keep_running) { @@ -1324,7 +1324,7 @@ struct TechmapPass : public Pass { int module_max_iter = max_iter; bool did_something = true; - std::set handled_cells; + pool handled_cells; while (did_something) { did_something = false; if (worker.techmap_module(design, module, map, handled_cells, celltypeMap, false)) @@ -1391,7 +1391,7 @@ struct FlattenPass : public Pass { if (mod->get_bool_attribute(ID::top)) top_mod = mod; - std::set handled_cells; + pool handled_cells; if (top_mod != nullptr) { worker.flatten_do_list.insert(top_mod->name); while (!worker.flatten_do_list.empty()) { From 644e55b3d30159b3dc033e5cfbfe07bde2604ea7 Mon Sep 17 00:00:00 2001 From: Alberto Gonzalez Date: Sun, 19 Apr 2020 22:32:28 +0000 Subject: [PATCH 09/28] Replace `std::map` with `dict` for `celltypeMap`. --- passes/techmap/techmap.cc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/passes/techmap/techmap.cc b/passes/techmap/techmap.cc index 55d80bad1..416d96862 100644 --- a/passes/techmap/techmap.cc +++ b/passes/techmap/techmap.cc @@ -464,7 +464,7 @@ struct TechmapWorker } bool techmap_module(RTLIL::Design *design, RTLIL::Module *module, RTLIL::Design *map, pool &handled_cells, - const std::map> &celltypeMap, bool in_recursion) + const dict> &celltypeMap, bool in_recursion) { std::string mapmsg_prefix = in_recursion ? "Recursively mapping" : "Mapping"; @@ -1300,7 +1300,7 @@ struct TechmapPass : public Pass { log_header(design, "Continuing TECHMAP pass.\n"); - std::map> celltypeMap; + dict> celltypeMap; for (auto module : map->modules()) { if (module->attributes.count(ID::techmap_celltype) && !module->attributes.at(ID::techmap_celltype).bits.empty()) { char *p = strdup(module->attributes.at(ID::techmap_celltype).decode_string().c_str()); @@ -1381,7 +1381,7 @@ struct FlattenPass : public Pass { extra_args(args, argidx, design); - std::map> celltypeMap; + dict> celltypeMap; for (auto module : design->modules()) celltypeMap[module->name].insert(module->name); From c43017fc082ecf860283c860e8f9de18c3ae09c3 Mon Sep 17 00:00:00 2001 From: Alberto Gonzalez Date: Sun, 19 Apr 2020 22:34:17 +0000 Subject: [PATCH 10/28] Replace `std::map` with `dict` for `TechmapWires` type. --- passes/techmap/techmap.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/passes/techmap/techmap.cc b/passes/techmap/techmap.cc index 416d96862..3a68d3cb9 100644 --- a/passes/techmap/techmap.cc +++ b/passes/techmap/techmap.cc @@ -79,7 +79,7 @@ struct TechmapWorker RTLIL::SigSpec value; }; - typedef std::map> TechmapWires; + typedef dict> TechmapWires; bool extern_mode; bool assert_mode; From 5cb4ae46665574e721161d4eb38552f0938b9948 Mon Sep 17 00:00:00 2001 From: Alberto Gonzalez Date: Sun, 19 Apr 2020 22:38:10 +0000 Subject: [PATCH 11/28] Replace `std::map` with `dict` for `connbits_map`, `cell_to_inbit`, and `outbit_to_cell`. --- passes/techmap/techmap.cc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/passes/techmap/techmap.cc b/passes/techmap/techmap.cc index 3a68d3cb9..96508845d 100644 --- a/passes/techmap/techmap.cc +++ b/passes/techmap/techmap.cc @@ -101,7 +101,7 @@ struct TechmapWorker std::string constmap_tpl_name(SigMap &sigmap, RTLIL::Module *tpl, RTLIL::Cell *cell, bool verbose) { std::string constmap_info; - std::map> connbits_map; + dict> connbits_map; for (auto conn : cell->connections()) for (int i = 0; i < GetSize(conn.second); i++) { @@ -490,8 +490,8 @@ struct TechmapWorker } TopoSort> cells; - std::map> cell_to_inbit; - std::map> outbit_to_cell; + dict> cell_to_inbit; + dict> outbit_to_cell; for (auto cell : module->selected_cells()) { From 6d64d768b07a027fff42515dbba4cdb6f060d17a Mon Sep 17 00:00:00 2001 From: Alberto Gonzalez Date: Sun, 19 Apr 2020 22:41:21 +0000 Subject: [PATCH 12/28] Replace `std::map` with `dict` for `port_new2old_map`, `port_connmap`, and `cellbits_to_tplbits`. --- passes/techmap/techmap.cc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/passes/techmap/techmap.cc b/passes/techmap/techmap.cc index 96508845d..b4df6142e 100644 --- a/passes/techmap/techmap.cc +++ b/passes/techmap/techmap.cc @@ -862,9 +862,9 @@ struct TechmapWorker techmap_do_cache[new_tpl] = true; tpl = new_tpl; - std::map port_new2old_map; - std::map port_connmap; - std::map cellbits_to_tplbits; + dict port_new2old_map; + dict port_connmap; + dict cellbits_to_tplbits; for (auto wire : tpl->wires().to_vector()) { From 7857782575bded685bdff55da942670343d54b20 Mon Sep 17 00:00:00 2001 From: Alberto Gonzalez Date: Sun, 19 Apr 2020 22:44:38 +0000 Subject: [PATCH 13/28] Replace `std::map` with `dict` for `unique_bit_id`. --- passes/techmap/techmap.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/passes/techmap/techmap.cc b/passes/techmap/techmap.cc index b4df6142e..a915f207d 100644 --- a/passes/techmap/techmap.cc +++ b/passes/techmap/techmap.cc @@ -740,7 +740,7 @@ struct TechmapWorker } int unique_bit_id_counter = 0; - std::map unique_bit_id; + dict unique_bit_id; unique_bit_id[RTLIL::State::S0] = unique_bit_id_counter++; unique_bit_id[RTLIL::State::S1] = unique_bit_id_counter++; unique_bit_id[RTLIL::State::Sx] = unique_bit_id_counter++; From a4755c50c37636db1f39097e607592487d4eed29 Mon Sep 17 00:00:00 2001 From: Alberto Gonzalez Date: Sun, 19 Apr 2020 23:52:21 +0000 Subject: [PATCH 14/28] Clean up extraneous buffer. --- passes/techmap/techmap.cc | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/passes/techmap/techmap.cc b/passes/techmap/techmap.cc index a915f207d..6b0957ad7 100644 --- a/passes/techmap/techmap.cc +++ b/passes/techmap/techmap.cc @@ -1423,14 +1423,11 @@ struct FlattenPass : public Pass { new_used_modules.insert(cell->type); } - std::set to_remove; - for (auto mod : design->modules()) + for (auto mod : design->modules().to_vector()) if (!used_modules[mod->name] && !mod->get_blackbox_attribute(worker.ignore_wb)) { log("Deleting now unused module %s.\n", log_id(mod)); - to_remove.insert(mod); + design->remove(mod); } - for (auto mod : to_remove) - design->remove(mod); } log_pop(); From dfcb936cd565411eb488152213945c7896a980db Mon Sep 17 00:00:00 2001 From: Alberto Gonzalez Date: Mon, 20 Apr 2020 00:01:14 +0000 Subject: [PATCH 15/28] Clean up pseudo-private member usage and ensure range iteration uses references where possible to avoid unnecessary copies. --- passes/techmap/techmap.cc | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/passes/techmap/techmap.cc b/passes/techmap/techmap.cc index 6b0957ad7..548a80b01 100644 --- a/passes/techmap/techmap.cc +++ b/passes/techmap/techmap.cc @@ -103,7 +103,7 @@ struct TechmapWorker std::string constmap_info; dict> connbits_map; - for (auto conn : cell->connections()) + for (auto &conn : cell->connections()) for (int i = 0; i < GetSize(conn.second); i++) { RTLIL::SigBit bit = sigmap(conn.second[i]); if (bit.wire == nullptr) { @@ -269,12 +269,12 @@ struct TechmapWorker pool tpl_written_bits; for (auto tpl_cell : tpl->cells()) - for (auto &it2 : tpl_cell->connections_) - if (tpl_cell->output(it2.first)) - for (auto bit : tpl_sigmap(it2.second)) + for (auto &conn : tpl_cell->connections()) + if (tpl_cell->output(conn.first)) + for (auto bit : tpl_sigmap(conn.second)) tpl_written_bits.insert(bit); - for (auto &it1 : tpl->connections_) - for (auto bit : tpl_sigmap(it1.first)) + for (auto &conn : tpl->connections()) + for (auto bit : tpl_sigmap(conn.first)) tpl_written_bits.insert(bit); SigMap port_signal_map; @@ -397,12 +397,12 @@ struct TechmapWorker vector autopurge_ports; - for (auto &it2 : c->connections_) + for (auto &conn : c->connections_) { bool autopurge = false; if (!autopurge_tpl_bits.empty()) { - autopurge = GetSize(it2.second) != 0; - for (auto &bit : sigmaps.at(tpl)(it2.second)) + autopurge = GetSize(conn.second) != 0; + for (auto &bit : sigmaps.at(tpl)(conn.second)) if (!autopurge_tpl_bits.count(bit)) { autopurge = false; break; @@ -410,10 +410,10 @@ struct TechmapWorker } if (autopurge) { - autopurge_ports.push_back(it2.first); + autopurge_ports.push_back(conn.first); } else { - apply_prefix(cell->name, it2.second, module); - port_signal_map.apply(it2.second); + apply_prefix(cell->name, conn.second, module); + port_signal_map.apply(conn.second); } } @@ -694,7 +694,7 @@ struct TechmapWorker break; } - for (auto conn : cell->connections()) { + for (auto &conn : cell->connections()) { if (conn.first.begins_with("$")) continue; if (tpl->wire(conn.first) != nullptr && tpl->wire(conn.first)->port_id > 0) @@ -712,7 +712,7 @@ struct TechmapWorker if (tpl->avail_parameters.count(ID::_TECHMAP_CELLTYPE_) != 0) parameters[ID::_TECHMAP_CELLTYPE_] = RTLIL::unescape_id(cell->type); - for (auto conn : cell->connections()) { + for (auto &conn : cell->connections()) { if (tpl->avail_parameters.count(stringf("\\_TECHMAP_CONSTMSK_%s_", log_id(conn.first))) != 0) { std::vector v = sigmap(conn.second).to_sigbit_vector(); for (auto &bit : v) @@ -746,7 +746,7 @@ struct TechmapWorker unique_bit_id[RTLIL::State::Sx] = unique_bit_id_counter++; unique_bit_id[RTLIL::State::Sz] = unique_bit_id_counter++; - for (auto conn : cell->connections()) + for (auto &conn : cell->connections()) if (tpl->avail_parameters.count(stringf("\\_TECHMAP_CONNMAP_%s_", log_id(conn.first))) != 0) { for (auto &bit : sigmap(conn.second)) if (unique_bit_id.count(bit) == 0) @@ -763,7 +763,7 @@ struct TechmapWorker if (tpl->avail_parameters.count(ID::_TECHMAP_BITS_CONNMAP_)) parameters[ID::_TECHMAP_BITS_CONNMAP_] = bits; - for (auto conn : cell->connections()) + for (auto &conn : cell->connections()) if (tpl->avail_parameters.count(stringf("\\_TECHMAP_CONNMAP_%s_", log_id(conn.first))) != 0) { RTLIL::Const value; for (auto &bit : sigmap(conn.second)) { @@ -884,7 +884,7 @@ struct TechmapWorker } } - for (auto conn : cell->connections()) + for (auto &conn : cell->connections()) for (int i = 0; i < GetSize(conn.second); i++) { RTLIL::SigBit bit = sigmap(conn.second[i]); From 62946218255eaea385fc62802c881bff256157fb Mon Sep 17 00:00:00 2001 From: Alberto Gonzalez Date: Mon, 20 Apr 2020 04:42:19 +0000 Subject: [PATCH 16/28] Use `emplace()` rather than `insert()`. --- passes/techmap/techmap.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/passes/techmap/techmap.cc b/passes/techmap/techmap.cc index 548a80b01..17d98847e 100644 --- a/passes/techmap/techmap.cc +++ b/passes/techmap/techmap.cc @@ -213,7 +213,7 @@ struct TechmapWorker if (tpl_w->port_id > 0) { IdString posportname = stringf("$%d", tpl_w->port_id); - positional_ports[posportname] = tpl_w->name; + positional_ports.emplace(posportname, tpl_w->name); if (!flatten_mode && tpl_w->get_bool_attribute(ID::techmap_autopurge) && (!cell->hasPort(tpl_w->name) || !GetSize(cell->getPort(tpl_w->name))) && From f235f212ea242d765e38f4b317390657c1291f77 Mon Sep 17 00:00:00 2001 From: Alberto Gonzalez Date: Mon, 20 Apr 2020 04:46:09 +0000 Subject: [PATCH 17/28] Replace `std::set` with `pool` for `cell_to_inbit` and `outbit_to_cell`. --- passes/techmap/techmap.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/passes/techmap/techmap.cc b/passes/techmap/techmap.cc index 17d98847e..ab719cbaa 100644 --- a/passes/techmap/techmap.cc +++ b/passes/techmap/techmap.cc @@ -490,8 +490,8 @@ struct TechmapWorker } TopoSort> cells; - dict> cell_to_inbit; - dict> outbit_to_cell; + dict> cell_to_inbit; + dict> outbit_to_cell; for (auto cell : module->selected_cells()) { From c658d9d59d98063fec463e17978a3e2f449f38de Mon Sep 17 00:00:00 2001 From: Alberto Gonzalez Date: Mon, 20 Apr 2020 05:10:08 +0000 Subject: [PATCH 18/28] Build constant bits directly rather than constructing an object and copying its bits. --- passes/techmap/techmap.cc | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/passes/techmap/techmap.cc b/passes/techmap/techmap.cc index ab719cbaa..da53e8448 100644 --- a/passes/techmap/techmap.cc +++ b/passes/techmap/techmap.cc @@ -767,8 +767,11 @@ struct TechmapWorker if (tpl->avail_parameters.count(stringf("\\_TECHMAP_CONNMAP_%s_", log_id(conn.first))) != 0) { RTLIL::Const value; for (auto &bit : sigmap(conn.second)) { - RTLIL::Const chunk(unique_bit_id.at(bit), bits); - value.bits.insert(value.bits.end(), chunk.bits.begin(), chunk.bits.end()); + int val = unique_bit_id.at(bit); + for (int i = 0; i < bits; i++) { + value.bits.push_back((val & 1) != 0 ? State::S1 : State::S0); + val = val >> 1; + } } parameters[stringf("\\_TECHMAP_CONNMAP_%s_", log_id(conn.first))] = value; } From 982562ff13b475ec96af6e12e0ad8fdec42f91b6 Mon Sep 17 00:00:00 2001 From: Alberto Gonzalez Date: Mon, 20 Apr 2020 05:59:00 +0000 Subject: [PATCH 19/28] Use `emplace()` for more efficient insertion into various `dict`s. --- passes/techmap/techmap.cc | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/passes/techmap/techmap.cc b/passes/techmap/techmap.cc index da53e8448..d137365c2 100644 --- a/passes/techmap/techmap.cc +++ b/passes/techmap/techmap.cc @@ -117,7 +117,7 @@ struct TechmapWorker constmap_info += stringf("|%s %d %s %d", log_id(conn.first), i, log_id(connbits_map.at(bit).first), connbits_map.at(bit).second); } else { - connbits_map[bit] = std::pair(conn.first, i); + connbits_map.emplace(bit, std::make_pair(conn.first, i)); constmap_info += stringf("|%s %d", log_id(conn.first), i); } } @@ -710,21 +710,21 @@ struct TechmapWorker } if (tpl->avail_parameters.count(ID::_TECHMAP_CELLTYPE_) != 0) - parameters[ID::_TECHMAP_CELLTYPE_] = RTLIL::unescape_id(cell->type); + parameters.emplace(ID::_TECHMAP_CELLTYPE_, RTLIL::unescape_id(cell->type)); for (auto &conn : cell->connections()) { if (tpl->avail_parameters.count(stringf("\\_TECHMAP_CONSTMSK_%s_", log_id(conn.first))) != 0) { std::vector v = sigmap(conn.second).to_sigbit_vector(); for (auto &bit : v) bit = RTLIL::SigBit(bit.wire == nullptr ? RTLIL::State::S1 : RTLIL::State::S0); - parameters[stringf("\\_TECHMAP_CONSTMSK_%s_", log_id(conn.first))] = RTLIL::SigSpec(v).as_const(); + parameters.emplace(stringf("\\_TECHMAP_CONSTMSK_%s_", log_id(conn.first)), RTLIL::SigSpec(v).as_const()); } if (tpl->avail_parameters.count(stringf("\\_TECHMAP_CONSTVAL_%s_", log_id(conn.first))) != 0) { std::vector v = sigmap(conn.second).to_sigbit_vector(); for (auto &bit : v) if (bit.wire != nullptr) bit = RTLIL::SigBit(RTLIL::State::Sx); - parameters[stringf("\\_TECHMAP_CONSTVAL_%s_", log_id(conn.first))] = RTLIL::SigSpec(v).as_const(); + parameters.emplace(stringf("\\_TECHMAP_CONSTVAL_%s_", log_id(conn.first)), RTLIL::SigSpec(v).as_const()); } if (tpl->avail_parameters.count(stringf("\\_TECHMAP_WIREINIT_%s_", log_id(conn.first))) != 0) { auto sig = sigmap(conn.second); @@ -735,7 +735,7 @@ struct TechmapWorker value[i] = it->second; } } - parameters[stringf("\\_TECHMAP_WIREINIT_%s_", log_id(conn.first))] = value; + parameters.emplace(stringf("\\_TECHMAP_WIREINIT_%s_", log_id(conn.first)), value); } } @@ -773,7 +773,7 @@ struct TechmapWorker val = val >> 1; } } - parameters[stringf("\\_TECHMAP_CONNMAP_%s_", log_id(conn.first))] = value; + parameters.emplace(stringf("\\_TECHMAP_CONNMAP_%s_", log_id(conn.first)), value); } } @@ -882,8 +882,8 @@ struct TechmapWorker wire->port_id = 0; for (int i = 0; i < wire->width; i++) { - port_new2old_map[RTLIL::SigBit(new_wire, i)] = RTLIL::SigBit(wire, i); - port_connmap[RTLIL::SigBit(wire, i)] = RTLIL::SigBit(new_wire, i); + port_new2old_map.emplace(RTLIL::SigBit(new_wire, i), RTLIL::SigBit(wire, i)); + port_connmap.emplace(RTLIL::SigBit(wire, i), RTLIL::SigBit(new_wire, i)); } } From bd54d67ad4a24992c4161f12b2c8e42df2a65569 Mon Sep 17 00:00:00 2001 From: Alberto Gonzalez Date: Mon, 20 Apr 2020 21:57:23 +0000 Subject: [PATCH 20/28] Further clean up `passes/techmap/techmap.cc`. Co-Authored-By: Eddie Hung --- passes/techmap/techmap.cc | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/passes/techmap/techmap.cc b/passes/techmap/techmap.cc index d137365c2..898aceccd 100644 --- a/passes/techmap/techmap.cc +++ b/passes/techmap/techmap.cc @@ -568,7 +568,7 @@ struct TechmapWorker { IdString derived_name = tpl_name; RTLIL::Module *tpl = map->module(tpl_name); - dict parameters(cell->parameters.begin(), cell->parameters.end()); + dict parameters(cell->parameters); if (tpl->get_blackbox_attribute(ignore_wb)) continue; @@ -782,16 +782,17 @@ struct TechmapWorker // do not register techmap_wrap modules with techmap_cache } else { std::pair> key(tpl_name, parameters); - if (techmap_cache.count(key) > 0) { - tpl = techmap_cache[key]; + auto it = techmap_cache.find(key); + if (it != techmap_cache.end()) { + tpl = it->second; } else { if (parameters.size() != 0) { mkdebug.on(); - derived_name = tpl->derive(map, dict(parameters.begin(), parameters.end())); + derived_name = tpl->derive(map, parameters); tpl = map->module(derived_name); log_continue = true; } - techmap_cache[key] = tpl; + techmap_cache.emplace(std::move(key), tpl); } } From dabeb1e8a136730536d17fd79e6c348d8cdca271 Mon Sep 17 00:00:00 2001 From: Eddie Hung Date: Mon, 20 Apr 2020 15:50:12 -0700 Subject: [PATCH 21/28] techmap: prefix special wires with backslash for use as IdString --- kernel/constids.inc | 1 + kernel/rtlil.h | 2 +- passes/techmap/techmap.cc | 23 ++++++++++++----------- 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/kernel/constids.inc b/kernel/constids.inc index aa75a9c09..5c8373513 100644 --- a/kernel/constids.inc +++ b/kernel/constids.inc @@ -169,6 +169,7 @@ X(techmap_autopurge) X(_TECHMAP_BITS_CONNMAP_) X(_TECHMAP_CELLTYPE_) X(techmap_celltype) +X(_TECHMAP_FAIL_) X(techmap_maccmap) X(_TECHMAP_REPLACE_) X(techmap_simplemap) diff --git a/kernel/rtlil.h b/kernel/rtlil.h index dad8508c3..11c45bbec 100644 --- a/kernel/rtlil.h +++ b/kernel/rtlil.h @@ -150,7 +150,7 @@ namespace RTLIL if (!p[0]) return 0; - log_assert(p[0] == '$' || p[0] == '\\' || strncmp(p, "_TECHMAP_", strlen("_TECHMAP_")) == 0); + log_assert(p[0] == '$' || p[0] == '\\'); log_assert(p[1] != 0); auto it = global_id_index_.find((char*)p); diff --git a/passes/techmap/techmap.cc b/passes/techmap/techmap.cc index 898aceccd..6513ab827 100644 --- a/passes/techmap/techmap.cc +++ b/passes/techmap/techmap.cc @@ -138,15 +138,16 @@ struct TechmapWorker continue; const char *q = strrchr(p+1, '.'); - p = q ? q+1 : p+1; + if (q) + p = q; - if (!strncmp(p, "_TECHMAP_", 9)) { + if (!strncmp(p, "\\_TECHMAP_", 10)) { TechmapWireData record; record.wire = w; record.value = w; result[p].push_back(record); - w->attributes[ID::keep] = RTLIL::Const(1); - w->attributes[ID::_techmap_special_] = RTLIL::Const(1); + w->set_bool_attribute(ID::keep); + w->set_bool_attribute(ID::_techmap_special_); } } @@ -819,7 +820,7 @@ struct TechmapWorker for (auto &it : twd) techmap_wire_names.insert(it.first); - for (auto &it : twd["_TECHMAP_FAIL_"]) { + for (auto &it : twd[ID::_TECHMAP_FAIL_]) { RTLIL::SigSpec value = it.value; if (value.is_fully_const() && value.as_bool()) { log("Not using module `%s' from techmap as it contains a %s marker wire with non-zero value %s.\n", @@ -833,7 +834,7 @@ struct TechmapWorker for (auto &it : twd) { - if (it.first.compare(0, 12, "_TECHMAP_DO_") != 0 || it.second.empty()) + if (!it.first.begins_with("\\_TECHMAP_DO_") || it.second.empty()) continue; auto &data = it.second.front(); @@ -941,8 +942,8 @@ struct TechmapWorker TechmapWires twd = techmap_find_special_wires(tpl); for (auto &it : twd) { - if (it.first != "_TECHMAP_FAIL_" && (it.first.substr(0, 20) != "_TECHMAP_REMOVEINIT_" || it.first[it.first.size()-1] != '_') && it.first.substr(0, 12) != "_TECHMAP_DO_" && it.first.substr(0, 14) != "_TECHMAP_DONE_") - log_error("Techmap yielded unknown config wire %s.\n", it.first.c_str()); + if (it.first != ID::_TECHMAP_FAIL_ && (!it.first.begins_with("\\_TECHMAP_REMOVEINIT_") || !it.first.ends_with("_")) && !it.first.begins_with("\\_TECHMAP_DO_") && !it.first.begins_with("\\_TECHMAP_DONE_")) + log_error("Techmap yielded unknown config wire %s.\n", log_id(it.first)); if (techmap_do_cache[tpl]) for (auto &it2 : it.second) if (!it2.value.is_fully_const()) @@ -974,10 +975,10 @@ struct TechmapWorker TechmapWires twd = techmap_find_special_wires(tpl); for (auto &it : twd) { - if (it.first.substr(0, 20) == "_TECHMAP_REMOVEINIT_") { + if (it.first.begins_with("\\_TECHMAP_REMOVEINIT_")) { for (auto &it2 : it.second) { auto val = it2.value.as_const(); - auto wirename = RTLIL::escape_id(it.first.substr(20, it.first.size() - 20 - 1)); + auto wirename = RTLIL::escape_id(it.first.substr(21, it.first.size() - 21 - 1)); auto it = cell->connections().find(wirename); if (it != cell->connections().end()) { auto sig = sigmap(it->second); @@ -1289,7 +1290,7 @@ struct TechmapPass : public Pass { log_cmd_error("Can't open saved design `%s'.\n", fn.c_str()+1); } for (auto mod : saved_designs.at(fn.substr(1))->modules()) - if (!map->has(mod->name)) + if (!map->module(mod->name)) map->add(mod->clone()); } else { std::ifstream f; From ce62d0751ac25211cd468ae7026805e6933e0ce6 Mon Sep 17 00:00:00 2001 From: Alberto Gonzalez Date: Mon, 20 Apr 2020 22:55:11 +0000 Subject: [PATCH 22/28] Replace `std::set`s using custom comparators with `pool`. Co-Authored-By: Eddie Hung --- passes/techmap/techmap.cc | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/passes/techmap/techmap.cc b/passes/techmap/techmap.cc index 6513ab827..16bc9c803 100644 --- a/passes/techmap/techmap.cc +++ b/passes/techmap/techmap.cc @@ -65,7 +65,7 @@ struct TechmapWorker dict simplemap_mappers; dict>, RTLIL::Module*> techmap_cache; dict techmap_do_cache; - std::set> module_queue; + pool module_queue; dict sigmaps; pool flatten_do_list; @@ -465,7 +465,7 @@ struct TechmapWorker } bool techmap_module(RTLIL::Design *design, RTLIL::Module *module, RTLIL::Design *map, pool &handled_cells, - const dict> &celltypeMap, bool in_recursion) + const dict> &celltypeMap, bool in_recursion) { std::string mapmsg_prefix = in_recursion ? "Recursively mapping" : "Mapping"; @@ -1305,7 +1305,7 @@ struct TechmapPass : public Pass { log_header(design, "Continuing TECHMAP pass.\n"); - dict> celltypeMap; + dict> celltypeMap; for (auto module : map->modules()) { if (module->attributes.count(ID::techmap_celltype) && !module->attributes.at(ID::techmap_celltype).bits.empty()) { char *p = strdup(module->attributes.at(ID::techmap_celltype).decode_string().c_str()); @@ -1386,7 +1386,7 @@ struct FlattenPass : public Pass { extra_args(args, argidx, design); - dict> celltypeMap; + dict> celltypeMap; for (auto module : design->modules()) celltypeMap[module->name].insert(module->name); From 36bb201dd9572f71c74d0987d8f42b19a6feaa9c Mon Sep 17 00:00:00 2001 From: Eddie Hung Date: Mon, 20 Apr 2020 20:56:38 -0700 Subject: [PATCH 23/28] techmap: sort celltypeMap as it determines techmap order --- passes/techmap/techmap.cc | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/passes/techmap/techmap.cc b/passes/techmap/techmap.cc index 16bc9c803..8a8756757 100644 --- a/passes/techmap/techmap.cc +++ b/passes/techmap/techmap.cc @@ -1313,11 +1313,13 @@ struct TechmapPass : public Pass { celltypeMap[RTLIL::escape_id(q)].insert(module->name); free(p); } else { - std::string module_name = module->name.begins_with("\\$") ? + IdString module_name = module->name.begins_with("\\$") ? module->name.substr(1) : module->name.str(); celltypeMap[module_name].insert(module->name); } } + for (auto &i : celltypeMap) + i.second.sort(RTLIL::sort_by_id_str()); for (auto module : design->modules()) worker.module_queue.insert(module); @@ -1389,6 +1391,8 @@ struct FlattenPass : public Pass { dict> celltypeMap; for (auto module : design->modules()) celltypeMap[module->name].insert(module->name); + for (auto &i : celltypeMap) + i.second.sort(RTLIL::sort_by_id_str()); RTLIL::Module *top_mod = nullptr; if (design->full_selection()) From 97fd304cbec4b760bb2ea454668c4957ddb99624 Mon Sep 17 00:00:00 2001 From: Alberto Gonzalez Date: Tue, 21 Apr 2020 07:51:29 +0000 Subject: [PATCH 24/28] techmap: Replace pseudo-private member usage with the range accessor function and some naughty `const_cast<>()`s. --- passes/techmap/techmap.cc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/passes/techmap/techmap.cc b/passes/techmap/techmap.cc index 8a8756757..0b88e5910 100644 --- a/passes/techmap/techmap.cc +++ b/passes/techmap/techmap.cc @@ -398,7 +398,7 @@ struct TechmapWorker vector autopurge_ports; - for (auto &conn : c->connections_) + for (auto &conn : c->connections()) { bool autopurge = false; if (!autopurge_tpl_bits.empty()) { @@ -413,8 +413,8 @@ struct TechmapWorker if (autopurge) { autopurge_ports.push_back(conn.first); } else { - apply_prefix(cell->name, conn.second, module); - port_signal_map.apply(conn.second); + apply_prefix(cell->name, const_cast(conn.second), module); + port_signal_map.apply(const_cast(conn.second)); } } From e17329164904f05efa48be63fbb73ec8afdda8e4 Mon Sep 17 00:00:00 2001 From: Alberto Gonzalez Date: Tue, 21 Apr 2020 17:47:00 +0000 Subject: [PATCH 25/28] techmap: Replace naughty `const_cast<>()`s. Co-Authored-By: Eddie Hung --- passes/techmap/techmap.cc | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/passes/techmap/techmap.cc b/passes/techmap/techmap.cc index 0b88e5910..6ac1d1113 100644 --- a/passes/techmap/techmap.cc +++ b/passes/techmap/techmap.cc @@ -413,8 +413,10 @@ struct TechmapWorker if (autopurge) { autopurge_ports.push_back(conn.first); } else { - apply_prefix(cell->name, const_cast(conn.second), module); - port_signal_map.apply(const_cast(conn.second)); + RTLIL::SigSpec new_conn = conn.second; + apply_prefix(cell->name, new_conn, module); + port_signal_map.apply(new_conn); + c->setPort(conn.first, std::move(new_conn)); } } From 35b94d1f664928dcf8476a9a6f35e2bb7f647ee1 Mon Sep 17 00:00:00 2001 From: Alberto Gonzalez Date: Wed, 22 Apr 2020 22:04:22 +0000 Subject: [PATCH 26/28] kernel: Re-implement `dict` hash code as a `dict` member function instead of a specialized template for `hash_ops`. --- kernel/hashlib.h | 34 ++++++++++++++-------------------- 1 file changed, 14 insertions(+), 20 deletions(-) diff --git a/kernel/hashlib.h b/kernel/hashlib.h index cdbad87c4..18114b6ad 100644 --- a/kernel/hashlib.h +++ b/kernel/hashlib.h @@ -19,12 +19,6 @@ namespace hashlib { -template struct hash_ops; -template> class dict; -template> class idict; -template> class pool; -template> class mfp; - const int hashtable_size_trigger = 2; const int hashtable_size_factor = 3; @@ -106,20 +100,6 @@ template struct hash_ops> { } }; -template struct hash_ops> { - static inline bool cmp(dict a, dict b) { - return a == b; - } - static inline unsigned int hash(dict a) { - unsigned int h = mkhash_init; - for (auto &it : a) { - h = mkhash(h, hash_ops

::hash(it.first)); - h = mkhash(h, hash_ops::hash(it.second)); - } - return h; - } -}; - template struct hash_ops> { static inline bool cmp(std::tuple a, std::tuple b) { return a == b; @@ -211,6 +191,11 @@ inline int hashtable_size(int min_size) throw std::length_error("hash table exceeded maximum size."); } +template> class dict; +template> class idict; +template> class pool; +template> class mfp; + template class dict { @@ -630,6 +615,15 @@ public: return !operator==(other); } + unsigned int hash() const { + unsigned int h = mkhash_init; + for (auto &it : entries) { + h = mkhash(h, hash_ops::hash(it.udata.first)); + h = mkhash(h, hash_ops::hash(it.udata.second)); + } + return h; + } + void reserve(size_t n) { entries.reserve(n); } size_t size() const { return entries.size(); } bool empty() const { return entries.empty(); } From 976edb7597692ac04111b3c51b13e18105c14f42 Mon Sep 17 00:00:00 2001 From: Alberto Gonzalez Date: Fri, 24 Apr 2020 08:37:16 +0000 Subject: [PATCH 27/28] kernel: Ensure `dict` always hashes to the same value given the same contents. --- kernel/hashlib.h | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/kernel/hashlib.h b/kernel/hashlib.h index 18114b6ad..1284f3f8d 100644 --- a/kernel/hashlib.h +++ b/kernel/hashlib.h @@ -207,6 +207,7 @@ class dict entry_t() { } entry_t(const std::pair &udata, int next) : udata(udata), next(next) { } entry_t(std::pair &&udata, int next) : udata(std::move(udata)), next(next) { } + bool operator<(const entry_t &other) const { return udata.first < other.udata.first; } }; std::vector hashtable; @@ -616,10 +617,12 @@ public: } unsigned int hash() const { + std::vector entries_(entries); //make a copy to preserve const-ness + std::sort(entries_.begin(), entries_.end()); unsigned int h = mkhash_init; - for (auto &it : entries) { - h = mkhash(h, hash_ops::hash(it.udata.first)); - h = mkhash(h, hash_ops::hash(it.udata.second)); + for (unsigned int i = 0; i < entries_.size(); ++i) { + h = mkhash(h, hash_ops::hash(entries_[i].udata.first)); + h = mkhash(h, hash_ops::hash(entries_[i].udata.second)); } return h; } From 6eea4b3d79590f874caa3ec740785781f3926666 Mon Sep 17 00:00:00 2001 From: Alberto Gonzalez Date: Mon, 18 May 2020 17:10:01 +0000 Subject: [PATCH 28/28] kernel: Try an order-independent approach to hashing `dict`. Co-Authored-By: David Shah Co-Authored-By: Eddie Hung --- kernel/hashlib.h | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/kernel/hashlib.h b/kernel/hashlib.h index 1284f3f8d..5c87b55f5 100644 --- a/kernel/hashlib.h +++ b/kernel/hashlib.h @@ -617,12 +617,10 @@ public: } unsigned int hash() const { - std::vector entries_(entries); //make a copy to preserve const-ness - std::sort(entries_.begin(), entries_.end()); unsigned int h = mkhash_init; - for (unsigned int i = 0; i < entries_.size(); ++i) { - h = mkhash(h, hash_ops::hash(entries_[i].udata.first)); - h = mkhash(h, hash_ops::hash(entries_[i].udata.second)); + for (auto &entry : entries) { + h ^= hash_ops::hash(entry.udata.first); + h ^= hash_ops::hash(entry.udata.second); } return h; }