mirror of
https://github.com/YosysHQ/yosys
synced 2025-04-13 04:28:18 +00:00
Improved init spec handling in opt_rmdff, modernized the code a bit
This commit is contained in:
parent
c417421495
commit
4ea7054b56
|
@ -60,7 +60,7 @@ bool handle_dlatch(RTLIL::Module *mod, RTLIL::Cell *dlatch)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
delete_dlatch:
|
delete_dlatch:
|
||||||
log("Removing %s (%s) from module %s.\n", dlatch->name.c_str(), dlatch->type.c_str(), mod->name.c_str());
|
log("Removing %s (%s) from module %s.\n", log_id(dlatch), log_id(dlatch->type), log_id(mod));
|
||||||
remove_init_attr(dlatch->getPort("\\Q"));
|
remove_init_attr(dlatch->getPort("\\Q"));
|
||||||
mod->remove(dlatch);
|
mod->remove(dlatch);
|
||||||
return true;
|
return true;
|
||||||
|
@ -170,7 +170,7 @@ bool handle_dff(RTLIL::Module *mod, RTLIL::Cell *dff)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
delete_dff:
|
delete_dff:
|
||||||
log("Removing %s (%s) from module %s.\n", dff->name.c_str(), dff->type.c_str(), mod->name.c_str());
|
log("Removing %s (%s) from module %s.\n", log_id(dff), log_id(dff->type), log_id(mod));
|
||||||
remove_init_attr(dff->getPort("\\Q"));
|
remove_init_attr(dff->getPort("\\Q"));
|
||||||
mod->remove(dff);
|
mod->remove(dff);
|
||||||
return true;
|
return true;
|
||||||
|
@ -190,73 +190,116 @@ struct OptRmdffPass : public Pass {
|
||||||
}
|
}
|
||||||
virtual void execute(std::vector<std::string> args, RTLIL::Design *design)
|
virtual void execute(std::vector<std::string> args, RTLIL::Design *design)
|
||||||
{
|
{
|
||||||
int total_count = 0;
|
int total_count = 0, total_initdrv = 0;
|
||||||
log_header(design, "Executing OPT_RMDFF pass (remove dff with constant values).\n");
|
log_header(design, "Executing OPT_RMDFF pass (remove dff with constant values).\n");
|
||||||
|
|
||||||
extra_args(args, 1, design);
|
extra_args(args, 1, design);
|
||||||
|
|
||||||
for (auto &mod_it : design->modules_)
|
for (auto module : design->selected_modules())
|
||||||
{
|
{
|
||||||
if (!design->selected(mod_it.second))
|
pool<SigBit> driven_bits;
|
||||||
continue;
|
dict<SigBit, State> init_bits;
|
||||||
|
|
||||||
assign_map.set(mod_it.second);
|
assign_map.set(module);
|
||||||
dff_init_map.set(mod_it.second);
|
dff_init_map.set(module);
|
||||||
for (auto &it : mod_it.second->wires_)
|
|
||||||
if (it.second->attributes.count("\\init") != 0) {
|
for (auto wire : module->wires())
|
||||||
dff_init_map.add(it.second, it.second->attributes.at("\\init"));
|
{
|
||||||
for (int i = 0; i < GetSize(it.second); i++) {
|
if (wire->attributes.count("\\init") != 0) {
|
||||||
SigBit wire_bit(it.second, i), mapped_bit = assign_map(wire_bit);
|
Const initval = wire->attributes.at("\\init");
|
||||||
if (mapped_bit.wire)
|
dff_init_map.add(wire, initval);
|
||||||
|
for (int i = 0; i < GetSize(wire); i++) {
|
||||||
|
SigBit wire_bit(wire, i), mapped_bit = assign_map(wire_bit);
|
||||||
|
if (mapped_bit.wire) {
|
||||||
init_attributes[mapped_bit].insert(wire_bit);
|
init_attributes[mapped_bit].insert(wire_bit);
|
||||||
|
if (i < GetSize(initval))
|
||||||
|
init_bits[mapped_bit] = initval[i];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (wire->port_input) {
|
||||||
|
for (auto bit : assign_map(wire))
|
||||||
|
driven_bits.insert(bit);
|
||||||
|
}
|
||||||
|
}
|
||||||
mux_drivers.clear();
|
mux_drivers.clear();
|
||||||
|
|
||||||
std::vector<RTLIL::IdString> dff_list;
|
std::vector<RTLIL::IdString> dff_list;
|
||||||
std::vector<RTLIL::IdString> dlatch_list;
|
std::vector<RTLIL::IdString> dlatch_list;
|
||||||
for (auto &it : mod_it.second->cells_) {
|
for (auto cell : module->cells())
|
||||||
if (it.second->type == "$mux" || it.second->type == "$pmux") {
|
{
|
||||||
if (it.second->getPort("\\A").size() == it.second->getPort("\\B").size())
|
for (auto &conn : cell->connections())
|
||||||
mux_drivers.insert(assign_map(it.second->getPort("\\Y")), it.second);
|
if (cell->output(conn.first) || !cell->known())
|
||||||
|
for (auto bit : assign_map(conn.second))
|
||||||
|
driven_bits.insert(bit);
|
||||||
|
|
||||||
|
if (cell->type == "$mux" || cell->type == "$pmux") {
|
||||||
|
if (cell->getPort("\\A").size() == cell->getPort("\\B").size())
|
||||||
|
mux_drivers.insert(assign_map(cell->getPort("\\Y")), cell);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (!design->selected(mod_it.second, it.second))
|
|
||||||
|
if (!design->selected(module, cell))
|
||||||
continue;
|
continue;
|
||||||
if (it.second->type == "$_DFF_N_") dff_list.push_back(it.first);
|
|
||||||
if (it.second->type == "$_DFF_P_") dff_list.push_back(it.first);
|
if (cell->type.in("$_DFF_N_", "$_DFF_P_",
|
||||||
if (it.second->type == "$_DFF_NN0_") dff_list.push_back(it.first);
|
"$_DFF_NN0_", "$_DFF_NN1_", "$_DFF_NP0_", "$_DFF_NP1_",
|
||||||
if (it.second->type == "$_DFF_NN1_") dff_list.push_back(it.first);
|
"$_DFF_PN0_", "$_DFF_PN1_", "$_DFF_PP0_", "$_DFF_PP1_",
|
||||||
if (it.second->type == "$_DFF_NP0_") dff_list.push_back(it.first);
|
"$dff", "$adff"))
|
||||||
if (it.second->type == "$_DFF_NP1_") dff_list.push_back(it.first);
|
dff_list.push_back(cell->name);
|
||||||
if (it.second->type == "$_DFF_PN0_") dff_list.push_back(it.first);
|
|
||||||
if (it.second->type == "$_DFF_PN1_") dff_list.push_back(it.first);
|
if (cell->type == "$dlatch")
|
||||||
if (it.second->type == "$_DFF_PP0_") dff_list.push_back(it.first);
|
dlatch_list.push_back(cell->name);
|
||||||
if (it.second->type == "$_DFF_PP1_") dff_list.push_back(it.first);
|
|
||||||
if (it.second->type == "$dff") dff_list.push_back(it.first);
|
|
||||||
if (it.second->type == "$adff") dff_list.push_back(it.first);
|
|
||||||
if (it.second->type == "$dlatch") dlatch_list.push_back(it.first);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for (auto &id : dff_list) {
|
for (auto &id : dff_list) {
|
||||||
if (mod_it.second->cells_.count(id) > 0 &&
|
if (module->cell(id) != nullptr &&
|
||||||
handle_dff(mod_it.second, mod_it.second->cells_[id]))
|
handle_dff(module, module->cells_[id]))
|
||||||
total_count++;
|
total_count++;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (auto &id : dlatch_list) {
|
for (auto &id : dlatch_list) {
|
||||||
if (mod_it.second->cells_.count(id) > 0 &&
|
if (module->cell(id) != nullptr &&
|
||||||
handle_dlatch(mod_it.second, mod_it.second->cells_[id]))
|
handle_dlatch(module, module->cells_[id]))
|
||||||
total_count++;
|
total_count++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SigSpec const_init_sigs;
|
||||||
|
|
||||||
|
for (auto bit : init_bits)
|
||||||
|
if (!driven_bits.count(bit.first))
|
||||||
|
const_init_sigs.append(bit.first);
|
||||||
|
|
||||||
|
const_init_sigs.sort_and_unify();
|
||||||
|
|
||||||
|
for (SigSpec sig : const_init_sigs.chunks())
|
||||||
|
{
|
||||||
|
Const val;
|
||||||
|
|
||||||
|
for (auto bit : sig)
|
||||||
|
val.bits.push_back(init_bits.at(bit));
|
||||||
|
|
||||||
|
log("Promoting init spec %s = %s to constant driver in module %s.\n",
|
||||||
|
log_signal(sig), log_signal(val), log_id(module));
|
||||||
|
|
||||||
|
module->connect(sig, val);
|
||||||
|
remove_init_attr(sig);
|
||||||
|
total_initdrv++;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
assign_map.clear();
|
assign_map.clear();
|
||||||
mux_drivers.clear();
|
mux_drivers.clear();
|
||||||
|
|
||||||
if (total_count)
|
if (total_count || total_initdrv)
|
||||||
design->scratchpad_set_bool("opt.did_something", true);
|
design->scratchpad_set_bool("opt.did_something", true);
|
||||||
log("Replaced %d DFF cells.\n", total_count);
|
|
||||||
|
if (total_initdrv)
|
||||||
|
log("Promoted %d init specs to constant drivers.\n", total_initdrv);
|
||||||
|
|
||||||
|
if (total_count)
|
||||||
|
log("Replaced %d DFF cells.\n", total_count);
|
||||||
}
|
}
|
||||||
} OptRmdffPass;
|
} OptRmdffPass;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue