2013-01-05 04:13:26 -06:00
|
|
|
/*
|
|
|
|
* yosys -- Yosys Open SYnthesis Suite
|
|
|
|
*
|
2021-06-07 17:39:36 -05:00
|
|
|
* Copyright (C) 2012 Claire Xenia Wolf <claire@yosyshq.com>
|
2015-07-02 04:14:30 -05:00
|
|
|
*
|
2013-01-05 04:13:26 -06:00
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
2015-07-02 04:14:30 -05:00
|
|
|
*
|
2013-01-05 04:13:26 -06:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "kernel/register.h"
|
|
|
|
#include "kernel/sigtools.h"
|
|
|
|
#include "kernel/log.h"
|
|
|
|
#include "kernel/celltypes.h"
|
2021-03-09 14:32:16 -06:00
|
|
|
#include "kernel/ffinit.h"
|
2013-01-05 04:13:26 -06:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <set>
|
|
|
|
|
2014-09-27 09:17:53 -05:00
|
|
|
USING_YOSYS_NAMESPACE
|
|
|
|
PRIVATE_NAMESPACE_BEGIN
|
|
|
|
|
2013-03-03 06:18:37 -06:00
|
|
|
using RTLIL::id2cstr;
|
|
|
|
|
2015-08-12 07:10:14 -05:00
|
|
|
struct keep_cache_t
|
|
|
|
{
|
|
|
|
Design *design;
|
|
|
|
dict<Module*, bool> cache;
|
2023-12-19 09:23:38 -06:00
|
|
|
bool purge_mode = false;
|
2015-08-12 07:10:14 -05:00
|
|
|
|
2023-12-19 09:23:38 -06:00
|
|
|
void reset(Design *design = nullptr, bool purge_mode = false)
|
2015-08-12 07:10:14 -05:00
|
|
|
{
|
|
|
|
this->design = design;
|
2023-12-19 09:23:38 -06:00
|
|
|
this->purge_mode = purge_mode;
|
2015-08-12 07:10:14 -05:00
|
|
|
cache.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool query(Module *module)
|
|
|
|
{
|
|
|
|
log_assert(design != nullptr);
|
|
|
|
|
|
|
|
if (module == nullptr)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (cache.count(module))
|
|
|
|
return cache.at(module);
|
|
|
|
|
2020-02-28 14:33:55 -06:00
|
|
|
cache[module] = true;
|
|
|
|
if (!module->get_bool_attribute(ID::keep)) {
|
|
|
|
bool found_keep = false;
|
|
|
|
for (auto cell : module->cells())
|
2020-10-08 06:33:47 -05:00
|
|
|
if (query(cell, true /* ignore_specify */)) {
|
2020-08-09 06:53:01 -05:00
|
|
|
found_keep = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
for (auto wire : module->wires())
|
|
|
|
if (wire->get_bool_attribute(ID::keep)) {
|
2020-02-28 14:33:55 -06:00
|
|
|
found_keep = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
cache[module] = found_keep;
|
|
|
|
}
|
|
|
|
|
|
|
|
return cache[module];
|
2015-08-12 07:10:14 -05:00
|
|
|
}
|
|
|
|
|
2020-10-08 06:33:47 -05:00
|
|
|
bool query(Cell *cell, bool ignore_specify = false)
|
2015-08-12 07:10:14 -05:00
|
|
|
{
|
2020-08-09 06:53:01 -05:00
|
|
|
if (cell->type.in(ID($assert), ID($assume), ID($live), ID($fair), ID($cover)))
|
2020-02-19 12:45:10 -06:00
|
|
|
return true;
|
|
|
|
|
2023-08-29 10:40:51 -05:00
|
|
|
if (cell->type.in(ID($overwrite_tag)))
|
|
|
|
return true;
|
|
|
|
|
2020-10-08 06:33:47 -05:00
|
|
|
if (!ignore_specify && cell->type.in(ID($specify2), ID($specify3), ID($specrule)))
|
2015-08-12 07:10:14 -05:00
|
|
|
return true;
|
|
|
|
|
2024-01-11 03:39:28 -06:00
|
|
|
if (cell->type == ID($print) || cell->type == ID($check))
|
2020-11-29 10:33:45 -06:00
|
|
|
return true;
|
|
|
|
|
2015-08-12 07:10:14 -05:00
|
|
|
if (cell->has_keep_attr())
|
|
|
|
return true;
|
|
|
|
|
2023-12-19 09:23:38 -06:00
|
|
|
if (!purge_mode && cell->type == ID($scopeinfo))
|
|
|
|
return true;
|
|
|
|
|
2015-08-12 07:10:14 -05:00
|
|
|
if (cell->module && cell->module->design)
|
|
|
|
return query(cell->module->design->module(cell->type));
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
keep_cache_t keep_cache;
|
|
|
|
CellTypes ct_reg, ct_all;
|
2014-09-27 09:17:53 -05:00
|
|
|
int count_rm_cells, count_rm_wires;
|
2013-01-05 04:13:26 -06:00
|
|
|
|
2015-02-04 09:34:06 -06:00
|
|
|
void rmunused_module_cells(Module *module, bool verbose)
|
2013-01-05 04:13:26 -06:00
|
|
|
{
|
2014-10-16 04:46:57 -05:00
|
|
|
SigMap sigmap(module);
|
2020-10-08 06:33:47 -05:00
|
|
|
dict<IdString, pool<Cell*>> mem2cells;
|
|
|
|
pool<IdString> mem_unused;
|
2015-02-04 09:34:06 -06:00
|
|
|
pool<Cell*> queue, unused;
|
2019-05-01 02:29:34 -05:00
|
|
|
pool<SigBit> used_raw_bits;
|
2015-02-04 09:34:06 -06:00
|
|
|
dict<SigBit, pool<Cell*>> wire2driver;
|
2019-05-01 02:29:34 -05:00
|
|
|
dict<SigBit, vector<string>> driver_driver_logs;
|
2021-03-09 14:32:16 -06:00
|
|
|
FfInitVals ffinit(&sigmap, module);
|
2019-05-01 02:29:34 -05:00
|
|
|
|
2019-05-03 02:22:26 -05:00
|
|
|
SigMap raw_sigmap;
|
2019-05-01 02:29:34 -05:00
|
|
|
for (auto &it : module->connections_) {
|
2019-05-03 02:22:26 -05:00
|
|
|
for (int i = 0; i < GetSize(it.second); i++) {
|
|
|
|
if (it.second[i].wire != nullptr)
|
|
|
|
raw_sigmap.add(it.first[i], it.second[i]);
|
|
|
|
}
|
2019-05-01 02:29:34 -05:00
|
|
|
}
|
2013-01-05 04:13:26 -06:00
|
|
|
|
2020-10-08 06:33:47 -05:00
|
|
|
for (auto &it : module->memories) {
|
|
|
|
mem_unused.insert(it.first);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (Cell *cell : module->cells()) {
|
2021-05-27 13:54:29 -05:00
|
|
|
if (cell->type.in(ID($memwr), ID($memwr_v2), ID($meminit), ID($meminit_v2))) {
|
2020-10-08 06:33:47 -05:00
|
|
|
IdString mem_id = cell->getParam(ID::MEMID).decode_string();
|
|
|
|
mem2cells[mem_id].insert(cell);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-26 18:51:45 -05:00
|
|
|
for (auto &it : module->cells_) {
|
2015-02-04 09:34:06 -06:00
|
|
|
Cell *cell = it.second;
|
2014-07-26 07:32:50 -05:00
|
|
|
for (auto &it2 : cell->connections()) {
|
2019-05-03 02:12:10 -05:00
|
|
|
if (ct_all.cell_known(cell->type) && !ct_all.cell_output(cell->type, it2.first))
|
|
|
|
continue;
|
|
|
|
for (auto raw_bit : it2.second) {
|
|
|
|
if (raw_bit.wire == nullptr)
|
|
|
|
continue;
|
|
|
|
auto bit = sigmap(raw_bit);
|
2019-06-05 02:14:12 -05:00
|
|
|
if (bit.wire == nullptr && ct_all.cell_known(cell->type))
|
2019-05-03 02:22:26 -05:00
|
|
|
driver_driver_logs[raw_sigmap(raw_bit)].push_back(stringf("Driver-driver conflict "
|
2019-05-03 02:12:10 -05:00
|
|
|
"for %s between cell %s.%s and constant %s in %s: Resolved using constant.",
|
|
|
|
log_signal(raw_bit), log_id(cell), log_id(it2.first), log_signal(bit), log_id(module)));
|
|
|
|
if (bit.wire != nullptr)
|
|
|
|
wire2driver[bit].insert(cell);
|
|
|
|
}
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
2015-08-12 07:10:14 -05:00
|
|
|
if (keep_cache.query(cell))
|
2013-01-05 04:13:26 -06:00
|
|
|
queue.insert(cell);
|
2015-02-04 09:34:06 -06:00
|
|
|
else
|
|
|
|
unused.insert(cell);
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
|
|
|
|
2014-07-26 18:49:51 -05:00
|
|
|
for (auto &it : module->wires_) {
|
2015-02-04 09:34:06 -06:00
|
|
|
Wire *wire = it.second;
|
2019-08-15 16:51:12 -05:00
|
|
|
if (wire->port_output || wire->get_bool_attribute(ID::keep)) {
|
2015-02-04 09:34:06 -06:00
|
|
|
for (auto bit : sigmap(wire))
|
|
|
|
for (auto c : wire2driver[bit])
|
|
|
|
queue.insert(c), unused.erase(c);
|
2019-05-01 02:29:34 -05:00
|
|
|
for (auto raw_bit : SigSpec(wire))
|
2019-05-03 02:22:26 -05:00
|
|
|
used_raw_bits.insert(raw_sigmap(raw_bit));
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-16 04:46:57 -05:00
|
|
|
while (!queue.empty())
|
2013-01-05 04:13:26 -06:00
|
|
|
{
|
2015-02-04 09:34:06 -06:00
|
|
|
pool<SigBit> bits;
|
2020-10-08 06:33:47 -05:00
|
|
|
pool<IdString> mems;
|
|
|
|
for (auto cell : queue) {
|
|
|
|
for (auto &it : cell->connections())
|
|
|
|
if (!ct_all.cell_known(cell->type) || ct_all.cell_input(cell->type, it.first))
|
|
|
|
for (auto bit : sigmap(it.second))
|
|
|
|
bits.insert(bit);
|
|
|
|
|
2021-05-27 13:54:29 -05:00
|
|
|
if (cell->type.in(ID($memrd), ID($memrd_v2))) {
|
2020-10-08 06:33:47 -05:00
|
|
|
IdString mem_id = cell->getParam(ID::MEMID).decode_string();
|
|
|
|
if (mem_unused.count(mem_id)) {
|
|
|
|
mem_unused.erase(mem_id);
|
|
|
|
mems.insert(mem_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-02-04 09:34:06 -06:00
|
|
|
|
|
|
|
queue.clear();
|
2020-10-08 06:33:47 -05:00
|
|
|
|
2015-02-04 09:34:06 -06:00
|
|
|
for (auto bit : bits)
|
|
|
|
for (auto c : wire2driver[bit])
|
|
|
|
if (unused.count(c))
|
|
|
|
queue.insert(c), unused.erase(c);
|
2020-10-08 06:33:47 -05:00
|
|
|
|
|
|
|
for (auto mem : mems)
|
|
|
|
for (auto c : mem2cells[mem])
|
|
|
|
if (unused.count(c))
|
|
|
|
queue.insert(c), unused.erase(c);
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
|
|
|
|
2015-02-04 09:34:06 -06:00
|
|
|
unused.sort(RTLIL::sort_by_name_id<RTLIL::Cell>());
|
|
|
|
|
2013-01-05 04:13:26 -06:00
|
|
|
for (auto cell : unused) {
|
2013-08-08 03:53:37 -05:00
|
|
|
if (verbose)
|
2019-04-22 10:25:52 -05:00
|
|
|
log_debug(" removing unused `%s' cell `%s'.\n", cell->type.c_str(), cell->name.c_str());
|
2014-08-30 12:37:12 -05:00
|
|
|
module->design->scratchpad_set_bool("opt.did_something", true);
|
2021-03-09 14:32:16 -06:00
|
|
|
if (RTLIL::builtin_ff_cell_types().count(cell->type))
|
|
|
|
ffinit.remove_init(cell->getPort(ID::Q));
|
2014-07-25 08:05:18 -05:00
|
|
|
module->remove(cell);
|
2013-08-08 03:53:37 -05:00
|
|
|
count_rm_cells++;
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
2019-05-03 02:12:10 -05:00
|
|
|
|
2020-10-08 06:33:47 -05:00
|
|
|
for (auto it : mem_unused)
|
|
|
|
{
|
|
|
|
if (verbose)
|
|
|
|
log_debug(" removing unused memory `%s'.\n", it.c_str());
|
|
|
|
delete module->memories.at(it);
|
|
|
|
module->memories.erase(it);
|
|
|
|
}
|
|
|
|
|
2019-05-03 02:12:10 -05:00
|
|
|
for (auto &it : module->cells_) {
|
|
|
|
Cell *cell = it.second;
|
|
|
|
for (auto &it2 : cell->connections()) {
|
|
|
|
if (ct_all.cell_known(cell->type) && !ct_all.cell_input(cell->type, it2.first))
|
|
|
|
continue;
|
2019-05-03 02:22:26 -05:00
|
|
|
for (auto raw_bit : raw_sigmap(it2.second))
|
2019-05-03 02:12:10 -05:00
|
|
|
used_raw_bits.insert(raw_bit);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto it : driver_driver_logs) {
|
|
|
|
if (used_raw_bits.count(it.first))
|
|
|
|
for (auto msg : it.second)
|
|
|
|
log_warning("%s\n", msg.c_str());
|
|
|
|
}
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
|
|
|
|
2014-09-27 09:17:53 -05:00
|
|
|
int count_nontrivial_wire_attrs(RTLIL::Wire *w)
|
2014-02-08 07:21:04 -06:00
|
|
|
{
|
|
|
|
int count = w->attributes.size();
|
2020-04-02 11:51:32 -05:00
|
|
|
count -= w->attributes.count(ID::src);
|
2023-12-19 09:23:38 -06:00
|
|
|
count -= w->attributes.count(ID::hdlname);
|
|
|
|
count -= w->attributes.count(ID(scopename));
|
2020-04-02 11:51:32 -05:00
|
|
|
count -= w->attributes.count(ID::unused_bits);
|
2014-02-08 07:21:04 -06:00
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2024-01-09 04:52:41 -06:00
|
|
|
// Should we pick `s2` over `s1` to represent a signal?
|
2014-12-28 21:06:52 -06:00
|
|
|
bool compare_signals(RTLIL::SigBit &s1, RTLIL::SigBit &s2, SigPool ®s, SigPool &conns, pool<RTLIL::Wire*> &direct_wires)
|
2013-01-05 04:13:26 -06:00
|
|
|
{
|
2014-07-24 15:47:57 -05:00
|
|
|
RTLIL::Wire *w1 = s1.wire;
|
|
|
|
RTLIL::Wire *w2 = s2.wire;
|
2013-01-05 04:13:26 -06:00
|
|
|
|
|
|
|
if (w1 == NULL || w2 == NULL)
|
|
|
|
return w2 == NULL;
|
|
|
|
|
|
|
|
if (w1->port_input != w2->port_input)
|
|
|
|
return w2->port_input;
|
|
|
|
|
2016-04-26 12:49:05 -05:00
|
|
|
if ((w1->port_input && w1->port_output) != (w2->port_input && w2->port_output))
|
|
|
|
return !(w2->port_input && w2->port_output);
|
|
|
|
|
2020-09-14 05:43:18 -05:00
|
|
|
if (w1->name.isPublic() && w2->name.isPublic()) {
|
2020-03-13 10:17:39 -05:00
|
|
|
if (regs.check(s1) != regs.check(s2))
|
|
|
|
return regs.check(s2);
|
2014-02-07 16:50:17 -06:00
|
|
|
if (direct_wires.count(w1) != direct_wires.count(w2))
|
2014-10-18 08:20:38 -05:00
|
|
|
return direct_wires.count(w2) != 0;
|
2013-10-17 15:10:55 -05:00
|
|
|
if (conns.check_any(s1) != conns.check_any(s2))
|
|
|
|
return conns.check_any(s2);
|
|
|
|
}
|
2013-10-17 13:48:40 -05:00
|
|
|
|
2013-10-16 19:41:59 -05:00
|
|
|
if (w1->port_output != w2->port_output)
|
|
|
|
return w2->port_output;
|
|
|
|
|
2013-01-05 04:13:26 -06:00
|
|
|
if (w1->name[0] != w2->name[0])
|
2020-09-14 05:43:18 -05:00
|
|
|
return w2->name.isPublic();
|
2013-01-05 04:13:26 -06:00
|
|
|
|
2014-02-08 07:21:04 -06:00
|
|
|
int attrs1 = count_nontrivial_wire_attrs(w1);
|
|
|
|
int attrs2 = count_nontrivial_wire_attrs(w2);
|
|
|
|
|
|
|
|
if (attrs1 != attrs2)
|
|
|
|
return attrs2 > attrs1;
|
2013-01-05 04:13:26 -06:00
|
|
|
|
2016-02-02 02:16:18 -06:00
|
|
|
return strcmp(w2->name.c_str(), w1->name.c_str()) < 0;
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
|
|
|
|
2014-09-27 09:17:53 -05:00
|
|
|
bool check_public_name(RTLIL::IdString id)
|
2013-03-08 02:16:25 -06:00
|
|
|
{
|
2019-08-07 14:20:08 -05:00
|
|
|
if (id.begins_with("$"))
|
2013-03-08 02:16:25 -06:00
|
|
|
return false;
|
2019-08-07 14:20:08 -05:00
|
|
|
const std::string &id_str = id.str();
|
|
|
|
if (id.begins_with("\\_") && (id.ends_with("_") || id_str.find("_[") != std::string::npos))
|
2013-08-07 11:39:49 -05:00
|
|
|
return false;
|
2014-08-02 06:11:01 -05:00
|
|
|
if (id_str.find(".$") != std::string::npos)
|
2013-03-10 08:20:03 -05:00
|
|
|
return false;
|
|
|
|
return true;
|
2013-03-08 02:16:25 -06:00
|
|
|
}
|
|
|
|
|
2019-05-15 09:01:28 -05:00
|
|
|
bool rmunused_module_signals(RTLIL::Module *module, bool purge_mode, bool verbose)
|
2013-01-05 04:13:26 -06:00
|
|
|
{
|
2024-01-09 04:52:41 -06:00
|
|
|
// `register_signals` and `connected_signals` will help us decide later on
|
|
|
|
// on picking representatives out of groups of connected signals
|
2013-10-17 13:48:40 -05:00
|
|
|
SigPool register_signals;
|
|
|
|
SigPool connected_signals;
|
|
|
|
if (!purge_mode)
|
2014-07-26 18:51:45 -05:00
|
|
|
for (auto &it : module->cells_) {
|
2013-10-17 13:48:40 -05:00
|
|
|
RTLIL::Cell *cell = it.second;
|
Use clk2fflogic attr on cells to track original FF names in witnesses
This makes clk2fflogic add an attr to $ff cells that carry the state of
the emulated async FF. The $ff output doesn't have any async updates
that happened in the current cycle, but the $ff input does, so the $ff
input corresponds to the async FF's output in the original design.
Hence this patch also makes the following changes to passes besides
clk2fflogic (but only for FFs with the clk2fflogic attr set):
* opt_clean treats the input as a register name (instead of the
output)
* rename -witness ensures that the input has a public name
* the formal backends (smt2, btor, aiger) will use the input's
name for the initial state of the FF in witness files
* when sim reads a yw witness that assigns an initial value to the
input signal, the state update is redirected to the output
This ensures that yosys witness files for clk2fflogic designs have
useful and stable public signal names. It also makes it possible to
simulate a clk2fflogic witness on the original design (with some
limitations when the original design is already using $ff cells).
It might seem like setting the output of a clk2fflogic FF to update the
input's initial value might not work in general, but it works fine for
these reasons:
* Witnesses for FFs are only present in the initial cycle, so we do
not care about any later cycles.
* The logic that clk2fflogic generates loops the output of the
genreated FF back to the input, with muxes in between to apply any
edge or level sensitive updates. So when there are no active updates
in the current gclk cycle, there is a combinational path from the
output back to the input.
* The logic clk2fflogic generates makes sure that an edge sensitive
update cannot be active in the first cycle (i.e. the past initial
value is assumed to be whatever it needs to be to avoid an edge).
* When a level sensitive update is active in the first gclk cycle, it
is actively driving the output for the whole gclk cycle, so ignoring
any witness initialization is the correct behavior.
2023-05-25 05:48:02 -05:00
|
|
|
if (ct_reg.cell_known(cell->type)) {
|
|
|
|
bool clk2fflogic = cell->get_bool_attribute(ID(clk2fflogic));
|
2014-07-26 07:32:50 -05:00
|
|
|
for (auto &it2 : cell->connections())
|
Use clk2fflogic attr on cells to track original FF names in witnesses
This makes clk2fflogic add an attr to $ff cells that carry the state of
the emulated async FF. The $ff output doesn't have any async updates
that happened in the current cycle, but the $ff input does, so the $ff
input corresponds to the async FF's output in the original design.
Hence this patch also makes the following changes to passes besides
clk2fflogic (but only for FFs with the clk2fflogic attr set):
* opt_clean treats the input as a register name (instead of the
output)
* rename -witness ensures that the input has a public name
* the formal backends (smt2, btor, aiger) will use the input's
name for the initial state of the FF in witness files
* when sim reads a yw witness that assigns an initial value to the
input signal, the state update is redirected to the output
This ensures that yosys witness files for clk2fflogic designs have
useful and stable public signal names. It also makes it possible to
simulate a clk2fflogic witness on the original design (with some
limitations when the original design is already using $ff cells).
It might seem like setting the output of a clk2fflogic FF to update the
input's initial value might not work in general, but it works fine for
these reasons:
* Witnesses for FFs are only present in the initial cycle, so we do
not care about any later cycles.
* The logic that clk2fflogic generates loops the output of the
genreated FF back to the input, with muxes in between to apply any
edge or level sensitive updates. So when there are no active updates
in the current gclk cycle, there is a combinational path from the
output back to the input.
* The logic clk2fflogic generates makes sure that an edge sensitive
update cannot be active in the first cycle (i.e. the past initial
value is assumed to be whatever it needs to be to avoid an edge).
* When a level sensitive update is active in the first gclk cycle, it
is actively driving the output for the whole gclk cycle, so ignoring
any witness initialization is the correct behavior.
2023-05-25 05:48:02 -05:00
|
|
|
if (clk2fflogic ? it2.first == ID::D : ct_reg.cell_output(cell->type, it2.first))
|
2013-10-17 13:48:40 -05:00
|
|
|
register_signals.add(it2.second);
|
Use clk2fflogic attr on cells to track original FF names in witnesses
This makes clk2fflogic add an attr to $ff cells that carry the state of
the emulated async FF. The $ff output doesn't have any async updates
that happened in the current cycle, but the $ff input does, so the $ff
input corresponds to the async FF's output in the original design.
Hence this patch also makes the following changes to passes besides
clk2fflogic (but only for FFs with the clk2fflogic attr set):
* opt_clean treats the input as a register name (instead of the
output)
* rename -witness ensures that the input has a public name
* the formal backends (smt2, btor, aiger) will use the input's
name for the initial state of the FF in witness files
* when sim reads a yw witness that assigns an initial value to the
input signal, the state update is redirected to the output
This ensures that yosys witness files for clk2fflogic designs have
useful and stable public signal names. It also makes it possible to
simulate a clk2fflogic witness on the original design (with some
limitations when the original design is already using $ff cells).
It might seem like setting the output of a clk2fflogic FF to update the
input's initial value might not work in general, but it works fine for
these reasons:
* Witnesses for FFs are only present in the initial cycle, so we do
not care about any later cycles.
* The logic that clk2fflogic generates loops the output of the
genreated FF back to the input, with muxes in between to apply any
edge or level sensitive updates. So when there are no active updates
in the current gclk cycle, there is a combinational path from the
output back to the input.
* The logic clk2fflogic generates makes sure that an edge sensitive
update cannot be active in the first cycle (i.e. the past initial
value is assumed to be whatever it needs to be to avoid an edge).
* When a level sensitive update is active in the first gclk cycle, it
is actively driving the output for the whole gclk cycle, so ignoring
any witness initialization is the correct behavior.
2023-05-25 05:48:02 -05:00
|
|
|
}
|
2014-07-26 07:32:50 -05:00
|
|
|
for (auto &it2 : cell->connections())
|
2013-10-17 13:48:40 -05:00
|
|
|
connected_signals.add(it2.second);
|
|
|
|
}
|
2015-07-02 04:14:30 -05:00
|
|
|
|
2013-01-05 04:13:26 -06:00
|
|
|
SigMap assign_map(module);
|
2024-01-09 04:52:41 -06:00
|
|
|
|
|
|
|
// construct a pool of wires which are directly driven by a known celltype,
|
|
|
|
// this will influence our choice of representatives
|
2014-12-28 21:06:52 -06:00
|
|
|
pool<RTLIL::Wire*> direct_wires;
|
2024-01-09 04:52:41 -06:00
|
|
|
{
|
|
|
|
pool<RTLIL::SigSpec> direct_sigs;
|
|
|
|
for (auto &it : module->cells_) {
|
|
|
|
RTLIL::Cell *cell = it.second;
|
|
|
|
if (ct_all.cell_known(cell->type))
|
|
|
|
for (auto &it2 : cell->connections())
|
|
|
|
if (ct_all.cell_output(cell->type, it2.first))
|
|
|
|
direct_sigs.insert(assign_map(it2.second));
|
|
|
|
}
|
|
|
|
for (auto &it : module->wires_) {
|
|
|
|
if (direct_sigs.count(assign_map(it.second)) || it.second->port_input)
|
|
|
|
direct_wires.insert(it.second);
|
|
|
|
}
|
2014-02-07 16:50:17 -06:00
|
|
|
}
|
|
|
|
|
2024-01-09 04:52:41 -06:00
|
|
|
// weight all options for representatives with `compare_signals`,
|
|
|
|
// the one that wins will be what `assign_map` maps to
|
2014-07-26 18:49:51 -05:00
|
|
|
for (auto &it : module->wires_) {
|
2013-01-05 04:13:26 -06:00
|
|
|
RTLIL::Wire *wire = it.second;
|
|
|
|
for (int i = 0; i < wire->width; i++) {
|
2014-07-24 15:47:57 -05:00
|
|
|
RTLIL::SigBit s1 = RTLIL::SigBit(wire, i), s2 = assign_map(s1);
|
2014-02-07 16:50:17 -06:00
|
|
|
if (!compare_signals(s1, s2, register_signals, connected_signals, direct_wires))
|
2013-01-05 04:13:26 -06:00
|
|
|
assign_map.add(s1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-09 04:52:41 -06:00
|
|
|
// we are removing all connections
|
2014-07-26 08:57:57 -05:00
|
|
|
module->connections_.clear();
|
2013-01-05 04:13:26 -06:00
|
|
|
|
2024-01-09 04:52:41 -06:00
|
|
|
// used signals sigmapped
|
2013-01-05 04:13:26 -06:00
|
|
|
SigPool used_signals;
|
2024-01-09 04:52:41 -06:00
|
|
|
// used signals pre-sigmapped
|
2019-05-15 09:01:28 -05:00
|
|
|
SigPool raw_used_signals;
|
2024-01-09 04:52:41 -06:00
|
|
|
// used signals sigmapped, ignoring drivers (we keep track of this to set `unused_bits`)
|
2013-01-05 04:13:26 -06:00
|
|
|
SigPool used_signals_nodrivers;
|
2024-01-09 04:52:41 -06:00
|
|
|
|
|
|
|
// gather the usage information for cells
|
2014-07-26 18:51:45 -05:00
|
|
|
for (auto &it : module->cells_) {
|
2013-01-05 04:13:26 -06:00
|
|
|
RTLIL::Cell *cell = it.second;
|
2014-07-26 08:57:57 -05:00
|
|
|
for (auto &it2 : cell->connections_) {
|
2024-01-09 04:52:41 -06:00
|
|
|
assign_map.apply(it2.second); // modify the cell connection in place
|
2019-05-15 09:01:28 -05:00
|
|
|
raw_used_signals.add(it2.second);
|
2013-01-05 04:13:26 -06:00
|
|
|
used_signals.add(it2.second);
|
2015-08-12 07:10:14 -05:00
|
|
|
if (!ct_all.cell_output(cell->type, it2.first))
|
2013-01-05 04:13:26 -06:00
|
|
|
used_signals_nodrivers.add(it2.second);
|
|
|
|
}
|
|
|
|
}
|
2024-01-09 04:52:41 -06:00
|
|
|
|
|
|
|
// gather the usage information for ports, wires with `keep`,
|
|
|
|
// also gather init bits
|
2021-08-21 16:36:00 -05:00
|
|
|
dict<RTLIL::SigBit, RTLIL::State> init_bits;
|
2014-07-26 18:49:51 -05:00
|
|
|
for (auto &it : module->wires_) {
|
2013-02-27 09:27:20 -06:00
|
|
|
RTLIL::Wire *wire = it.second;
|
|
|
|
if (wire->port_id > 0) {
|
|
|
|
RTLIL::SigSpec sig = RTLIL::SigSpec(wire);
|
2019-05-15 09:01:28 -05:00
|
|
|
raw_used_signals.add(sig);
|
2013-02-27 09:27:20 -06:00
|
|
|
assign_map.apply(sig);
|
|
|
|
used_signals.add(sig);
|
|
|
|
if (!wire->port_input)
|
|
|
|
used_signals_nodrivers.add(sig);
|
|
|
|
}
|
2019-08-15 16:51:12 -05:00
|
|
|
if (wire->get_bool_attribute(ID::keep)) {
|
2013-11-05 08:52:29 -06:00
|
|
|
RTLIL::SigSpec sig = RTLIL::SigSpec(wire);
|
|
|
|
assign_map.apply(sig);
|
|
|
|
used_signals.add(sig);
|
|
|
|
}
|
2021-08-21 16:36:00 -05:00
|
|
|
auto it2 = wire->attributes.find(ID::init);
|
|
|
|
if (it2 != wire->attributes.end()) {
|
|
|
|
RTLIL::Const &val = it2->second;
|
|
|
|
SigSpec sig = assign_map(wire);
|
|
|
|
for (int i = 0; i < GetSize(val) && i < GetSize(sig); i++)
|
2024-10-09 12:39:45 -05:00
|
|
|
if (val[i] != State::Sx)
|
|
|
|
init_bits[sig[i]] = val[i];
|
2021-08-21 16:36:00 -05:00
|
|
|
wire->attributes.erase(it2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-09 04:52:41 -06:00
|
|
|
// set init attributes on all wires of a connected group
|
2021-08-21 16:36:00 -05:00
|
|
|
for (auto wire : module->wires()) {
|
|
|
|
bool found = false;
|
|
|
|
Const val(State::Sx, wire->width);
|
|
|
|
for (int i = 0; i < wire->width; i++) {
|
|
|
|
auto it = init_bits.find(RTLIL::SigBit(wire, i));
|
|
|
|
if (it != init_bits.end()) {
|
2024-10-09 12:39:45 -05:00
|
|
|
val.bits()[i] = it->second;
|
2021-08-21 16:36:00 -05:00
|
|
|
found = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (found)
|
|
|
|
wire->attributes[ID::init] = val;
|
2013-02-27 09:27:20 -06:00
|
|
|
}
|
2013-01-05 04:13:26 -06:00
|
|
|
|
2024-01-09 04:52:41 -06:00
|
|
|
// now decide for each wire if we should be deleting it
|
2019-05-06 05:45:22 -05:00
|
|
|
pool<RTLIL::Wire*> del_wires_queue;
|
2014-07-27 06:19:05 -05:00
|
|
|
for (auto wire : module->wires())
|
|
|
|
{
|
2019-05-03 07:24:53 -05:00
|
|
|
SigSpec s1 = SigSpec(wire), s2 = assign_map(s1);
|
|
|
|
log_assert(GetSize(s1) == GetSize(s2));
|
|
|
|
|
2019-05-04 02:47:16 -05:00
|
|
|
Const initval;
|
2020-04-02 11:51:32 -05:00
|
|
|
if (wire->attributes.count(ID::init))
|
|
|
|
initval = wire->attributes.at(ID::init);
|
2019-05-04 02:47:16 -05:00
|
|
|
if (GetSize(initval) != GetSize(wire))
|
2024-10-09 12:39:45 -05:00
|
|
|
initval.bits().resize(GetSize(wire), State::Sx);
|
2019-05-04 02:47:16 -05:00
|
|
|
if (initval.is_fully_undef())
|
2020-04-02 11:51:32 -05:00
|
|
|
wire->attributes.erase(ID::init);
|
2019-05-04 02:47:16 -05:00
|
|
|
|
2019-05-07 07:41:58 -05:00
|
|
|
if (GetSize(wire) == 0) {
|
2019-05-22 06:56:56 -05:00
|
|
|
// delete zero-width wires, unless they are module ports
|
|
|
|
if (wire->port_id == 0)
|
|
|
|
goto delete_this_wire;
|
2019-05-07 07:41:58 -05:00
|
|
|
} else
|
2019-08-15 16:51:12 -05:00
|
|
|
if (wire->port_id != 0 || wire->get_bool_attribute(ID::keep) || !initval.is_fully_undef()) {
|
2019-05-07 07:41:58 -05:00
|
|
|
// do not delete anything with "keep" or module ports or initialized wires
|
2019-05-04 02:47:16 -05:00
|
|
|
} else
|
2019-06-26 10:54:17 -05:00
|
|
|
if (!purge_mode && check_public_name(wire->name) && (raw_used_signals.check_any(s1) || used_signals.check_any(s2) || s1 != s2)) {
|
|
|
|
// do not get rid of public names unless in purge mode or if the wire is entirely unused, not even aliased
|
2019-05-07 07:41:58 -05:00
|
|
|
} else
|
2019-05-15 09:01:28 -05:00
|
|
|
if (!raw_used_signals.check_any(s1)) {
|
2019-05-07 07:41:58 -05:00
|
|
|
// delete wires that aren't used by anything directly
|
|
|
|
goto delete_this_wire;
|
2019-05-03 07:24:53 -05:00
|
|
|
}
|
|
|
|
|
2019-05-07 07:41:58 -05:00
|
|
|
if (0)
|
|
|
|
{
|
|
|
|
delete_this_wire:
|
2019-05-06 05:45:22 -05:00
|
|
|
del_wires_queue.insert(wire);
|
2019-05-07 07:41:58 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-05-03 07:24:53 -05:00
|
|
|
RTLIL::SigSig new_conn;
|
|
|
|
for (int i = 0; i < GetSize(s1); i++)
|
|
|
|
if (s1[i] != s2[i]) {
|
|
|
|
if (s2[i] == State::Sx && (initval[i] == State::S0 || initval[i] == State::S1)) {
|
|
|
|
s2[i] = initval[i];
|
2024-10-09 12:39:45 -05:00
|
|
|
initval.bits()[i] = State::Sx;
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
2020-03-13 10:17:39 -05:00
|
|
|
new_conn.first.append(s1[i]);
|
|
|
|
new_conn.second.append(s2[i]);
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
2019-05-03 07:24:53 -05:00
|
|
|
if (new_conn.first.size() > 0) {
|
|
|
|
if (initval.is_fully_undef())
|
2020-04-02 11:51:32 -05:00
|
|
|
wire->attributes.erase(ID::init);
|
2019-05-03 07:24:53 -05:00
|
|
|
else
|
2020-04-02 11:51:32 -05:00
|
|
|
wire->attributes.at(ID::init) = initval;
|
2019-05-03 07:24:53 -05:00
|
|
|
used_signals.add(new_conn.first);
|
|
|
|
used_signals.add(new_conn.second);
|
|
|
|
module->connect(new_conn);
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
2014-07-27 06:19:05 -05:00
|
|
|
|
2019-05-07 07:41:58 -05:00
|
|
|
if (!used_signals_nodrivers.check_all(s2)) {
|
|
|
|
std::string unused_bits;
|
|
|
|
for (int i = 0; i < GetSize(s2); i++) {
|
|
|
|
if (s2[i].wire == NULL)
|
|
|
|
continue;
|
|
|
|
if (!used_signals_nodrivers.check(s2[i])) {
|
|
|
|
if (!unused_bits.empty())
|
|
|
|
unused_bits += " ";
|
|
|
|
unused_bits += stringf("%d", i);
|
|
|
|
}
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
2019-05-07 07:41:58 -05:00
|
|
|
if (unused_bits.empty() || wire->port_id != 0)
|
2020-04-02 11:51:32 -05:00
|
|
|
wire->attributes.erase(ID::unused_bits);
|
2019-05-07 07:41:58 -05:00
|
|
|
else
|
2020-04-02 11:51:32 -05:00
|
|
|
wire->attributes[ID::unused_bits] = RTLIL::Const(unused_bits);
|
2019-05-07 07:41:58 -05:00
|
|
|
} else {
|
2020-04-02 11:51:32 -05:00
|
|
|
wire->attributes.erase(ID::unused_bits);
|
2019-05-07 07:41:58 -05:00
|
|
|
}
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-06 05:45:22 -05:00
|
|
|
int del_temp_wires_count = 0;
|
|
|
|
for (auto wire : del_wires_queue) {
|
|
|
|
if (ys_debug() || (check_public_name(wire->name) && verbose))
|
|
|
|
log_debug(" removing unused non-port wire %s.\n", wire->name.c_str());
|
|
|
|
else
|
|
|
|
del_temp_wires_count++;
|
2019-05-03 07:24:53 -05:00
|
|
|
}
|
2014-07-26 13:12:50 -05:00
|
|
|
|
2019-05-06 05:45:22 -05:00
|
|
|
module->remove(del_wires_queue);
|
|
|
|
count_rm_wires += GetSize(del_wires_queue);
|
2014-07-26 13:12:50 -05:00
|
|
|
|
2019-05-06 05:45:22 -05:00
|
|
|
if (verbose && del_temp_wires_count)
|
|
|
|
log_debug(" removed %d unused temporary wires.\n", del_temp_wires_count);
|
2019-05-15 09:01:28 -05:00
|
|
|
|
2020-04-11 09:59:10 -05:00
|
|
|
if (!del_wires_queue.empty())
|
|
|
|
module->design->scratchpad_set_bool("opt.did_something", true);
|
|
|
|
|
2019-05-15 09:01:28 -05:00
|
|
|
return !del_wires_queue.empty();
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
|
|
|
|
2020-05-14 02:24:23 -05:00
|
|
|
bool rmunused_module_init(RTLIL::Module *module, bool verbose)
|
2017-07-28 16:11:52 -05:00
|
|
|
{
|
|
|
|
bool did_something = false;
|
|
|
|
CellTypes fftypes;
|
|
|
|
fftypes.setup_internals_mem();
|
|
|
|
|
|
|
|
SigMap sigmap(module);
|
|
|
|
dict<SigBit, State> qbits;
|
|
|
|
|
|
|
|
for (auto cell : module->cells())
|
2020-04-02 11:51:32 -05:00
|
|
|
if (fftypes.cell_known(cell->type) && cell->hasPort(ID::Q))
|
2017-07-28 16:11:52 -05:00
|
|
|
{
|
2020-04-02 11:51:32 -05:00
|
|
|
SigSpec sig = cell->getPort(ID::Q);
|
2017-07-28 16:11:52 -05:00
|
|
|
|
|
|
|
for (int i = 0; i < GetSize(sig); i++)
|
|
|
|
{
|
|
|
|
SigBit bit = sig[i];
|
|
|
|
|
2020-04-02 11:51:32 -05:00
|
|
|
if (bit.wire == nullptr || bit.wire->attributes.count(ID::init) == 0)
|
2017-07-28 16:11:52 -05:00
|
|
|
continue;
|
|
|
|
|
2020-04-02 11:51:32 -05:00
|
|
|
Const init = bit.wire->attributes.at(ID::init);
|
2017-07-28 16:11:52 -05:00
|
|
|
|
|
|
|
if (i >= GetSize(init) || init[i] == State::Sx || init[i] == State::Sz)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
sigmap.add(bit);
|
|
|
|
qbits[bit] = init[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto wire : module->wires())
|
|
|
|
{
|
2020-04-02 11:51:32 -05:00
|
|
|
if (wire->attributes.count(ID::init) == 0)
|
2017-07-28 16:11:52 -05:00
|
|
|
continue;
|
|
|
|
|
2020-04-02 11:51:32 -05:00
|
|
|
Const init = wire->attributes.at(ID::init);
|
2017-07-28 16:11:52 -05:00
|
|
|
|
|
|
|
for (int i = 0; i < GetSize(wire) && i < GetSize(init); i++)
|
|
|
|
{
|
|
|
|
if (init[i] == State::Sx || init[i] == State::Sz)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
SigBit wire_bit = SigBit(wire, i);
|
|
|
|
SigBit mapped_wire_bit = sigmap(wire_bit);
|
|
|
|
|
|
|
|
if (wire_bit == mapped_wire_bit)
|
|
|
|
goto next_wire;
|
|
|
|
|
2020-05-14 02:24:23 -05:00
|
|
|
if (mapped_wire_bit.wire) {
|
|
|
|
if (qbits.count(mapped_wire_bit) == 0)
|
|
|
|
goto next_wire;
|
2017-07-28 16:11:52 -05:00
|
|
|
|
2020-05-14 02:24:23 -05:00
|
|
|
if (qbits.at(mapped_wire_bit) != init[i])
|
|
|
|
goto next_wire;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (mapped_wire_bit == State::Sx || mapped_wire_bit == State::Sz)
|
|
|
|
goto next_wire;
|
|
|
|
|
|
|
|
if (mapped_wire_bit != init[i]) {
|
2020-05-14 02:59:38 -05:00
|
|
|
log_warning("Initial value conflict for %s resolving to %s but with init %s.\n", log_signal(wire_bit), log_signal(mapped_wire_bit), log_signal(init[i]));
|
2020-05-14 02:24:23 -05:00
|
|
|
goto next_wire;
|
|
|
|
}
|
|
|
|
}
|
2017-07-28 16:11:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (verbose)
|
2019-04-22 10:25:52 -05:00
|
|
|
log_debug(" removing redundant init attribute on %s.\n", log_id(wire));
|
2017-07-28 16:11:52 -05:00
|
|
|
|
2020-04-02 11:51:32 -05:00
|
|
|
wire->attributes.erase(ID::init);
|
2017-07-28 16:11:52 -05:00
|
|
|
did_something = true;
|
|
|
|
next_wire:;
|
|
|
|
}
|
|
|
|
|
2020-04-11 09:59:10 -05:00
|
|
|
if (did_something)
|
|
|
|
module->design->scratchpad_set_bool("opt.did_something", true);
|
|
|
|
|
2017-07-28 16:11:52 -05:00
|
|
|
return did_something;
|
|
|
|
}
|
|
|
|
|
|
|
|
void rmunused_module(RTLIL::Module *module, bool purge_mode, bool verbose, bool rminit)
|
2013-01-05 04:13:26 -06:00
|
|
|
{
|
2013-08-08 03:53:37 -05:00
|
|
|
if (verbose)
|
|
|
|
log("Finding unused cells or wires in module %s..\n", module->name.c_str());
|
2013-01-05 04:13:26 -06:00
|
|
|
|
2014-10-03 03:04:15 -05:00
|
|
|
std::vector<RTLIL::Cell*> delcells;
|
|
|
|
for (auto cell : module->cells())
|
2019-08-09 11:58:14 -05:00
|
|
|
if (cell->type.in(ID($pos), ID($_BUF_)) && !cell->has_keep_attr()) {
|
2020-04-02 11:51:32 -05:00
|
|
|
bool is_signed = cell->type == ID($pos) && cell->getParam(ID::A_SIGNED).as_bool();
|
2019-08-15 16:50:10 -05:00
|
|
|
RTLIL::SigSpec a = cell->getPort(ID::A);
|
|
|
|
RTLIL::SigSpec y = cell->getPort(ID::Y);
|
2014-10-10 09:59:44 -05:00
|
|
|
a.extend_u0(GetSize(y), is_signed);
|
2014-10-03 03:04:15 -05:00
|
|
|
module->connect(y, a);
|
|
|
|
delcells.push_back(cell);
|
|
|
|
}
|
2015-02-24 15:31:30 -06:00
|
|
|
for (auto cell : delcells) {
|
|
|
|
if (verbose)
|
2019-04-22 10:25:52 -05:00
|
|
|
log_debug(" removing buffer cell `%s': %s = %s\n", cell->name.c_str(),
|
2019-08-15 16:50:10 -05:00
|
|
|
log_signal(cell->getPort(ID::Y)), log_signal(cell->getPort(ID::A)));
|
2014-10-03 03:04:15 -05:00
|
|
|
module->remove(cell);
|
2015-02-24 15:31:30 -06:00
|
|
|
}
|
|
|
|
if (!delcells.empty())
|
|
|
|
module->design->scratchpad_set_bool("opt.did_something", true);
|
2014-10-03 03:04:15 -05:00
|
|
|
|
2013-08-08 03:53:37 -05:00
|
|
|
rmunused_module_cells(module, verbose);
|
2019-05-15 09:01:28 -05:00
|
|
|
while (rmunused_module_signals(module, purge_mode, verbose)) { }
|
2017-07-28 16:11:52 -05:00
|
|
|
|
2020-05-14 02:24:23 -05:00
|
|
|
if (rminit && rmunused_module_init(module, verbose))
|
2019-05-15 09:01:28 -05:00
|
|
|
while (rmunused_module_signals(module, purge_mode, verbose)) { }
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
|
|
|
|
2013-06-05 00:07:31 -05:00
|
|
|
struct OptCleanPass : public Pass {
|
|
|
|
OptCleanPass() : Pass("opt_clean", "remove unused cells and wires") { }
|
2020-06-18 18:34:52 -05:00
|
|
|
void help() override
|
2013-03-01 01:58:55 -06:00
|
|
|
{
|
|
|
|
// |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
|
|
|
|
log("\n");
|
2013-07-07 05:59:30 -05:00
|
|
|
log(" opt_clean [options] [selection]\n");
|
2013-03-01 01:58:55 -06:00
|
|
|
log("\n");
|
|
|
|
log("This pass identifies wires and cells that are unused and removes them. Other\n");
|
2013-03-17 16:02:30 -05:00
|
|
|
log("passes often remove cells but leave the wires in the design or reconnect the\n");
|
|
|
|
log("wires but leave the old cells in the design. This pass can be used to clean up\n");
|
|
|
|
log("after the passes that do the actual work.\n");
|
2013-03-01 01:58:55 -06:00
|
|
|
log("\n");
|
|
|
|
log("This pass only operates on completely selected modules without processes.\n");
|
|
|
|
log("\n");
|
2013-07-07 05:59:30 -05:00
|
|
|
log(" -purge\n");
|
|
|
|
log(" also remove internal nets if they have a public name\n");
|
|
|
|
log("\n");
|
2013-03-01 01:58:55 -06:00
|
|
|
}
|
2020-06-18 18:34:52 -05:00
|
|
|
void execute(std::vector<std::string> args, RTLIL::Design *design) override
|
2013-01-05 04:13:26 -06:00
|
|
|
{
|
2013-07-07 05:59:30 -05:00
|
|
|
bool purge_mode = false;
|
|
|
|
|
2016-04-21 16:28:37 -05:00
|
|
|
log_header(design, "Executing OPT_CLEAN pass (remove unused cells and wires).\n");
|
2013-01-05 04:13:26 -06:00
|
|
|
log_push();
|
|
|
|
|
2013-07-07 05:59:30 -05:00
|
|
|
size_t argidx;
|
|
|
|
for (argidx = 1; argidx < args.size(); argidx++) {
|
|
|
|
if (args[argidx] == "-purge") {
|
|
|
|
purge_mode = true;
|
|
|
|
continue;
|
|
|
|
}
|
2013-08-11 06:59:14 -05:00
|
|
|
break;
|
2013-07-07 05:59:30 -05:00
|
|
|
}
|
|
|
|
extra_args(args, argidx, design);
|
2013-01-05 04:13:26 -06:00
|
|
|
|
2023-12-19 09:23:38 -06:00
|
|
|
keep_cache.reset(design, purge_mode);
|
2015-08-10 15:14:21 -05:00
|
|
|
|
2013-10-17 13:48:40 -05:00
|
|
|
ct_reg.setup_internals_mem();
|
2022-07-21 07:22:15 -05:00
|
|
|
ct_reg.setup_internals_anyinit();
|
2013-10-17 13:48:40 -05:00
|
|
|
ct_reg.setup_stdcells_mem();
|
|
|
|
|
2015-08-11 00:54:32 -05:00
|
|
|
ct_all.setup(design);
|
|
|
|
|
2019-05-03 07:24:53 -05:00
|
|
|
count_rm_cells = 0;
|
|
|
|
count_rm_wires = 0;
|
|
|
|
|
2015-02-03 16:45:01 -06:00
|
|
|
for (auto module : design->selected_whole_modules_warn()) {
|
|
|
|
if (module->has_processes_warn())
|
2013-03-01 01:58:55 -06:00
|
|
|
continue;
|
2017-07-28 16:11:52 -05:00
|
|
|
rmunused_module(module, purge_mode, true, true);
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
|
|
|
|
2017-07-28 16:11:52 -05:00
|
|
|
if (count_rm_cells > 0 || count_rm_wires > 0)
|
|
|
|
log("Removed %d unused cells and %d unused wires.\n", count_rm_cells, count_rm_wires);
|
|
|
|
|
2015-02-24 15:31:30 -06:00
|
|
|
design->optimize();
|
|
|
|
design->sort();
|
|
|
|
design->check();
|
|
|
|
|
2015-08-12 07:10:14 -05:00
|
|
|
keep_cache.reset();
|
2013-10-17 13:48:40 -05:00
|
|
|
ct_reg.clear();
|
2015-08-11 00:54:32 -05:00
|
|
|
ct_all.clear();
|
2013-01-05 04:13:26 -06:00
|
|
|
log_pop();
|
|
|
|
}
|
2013-06-05 00:07:31 -05:00
|
|
|
} OptCleanPass;
|
2015-07-02 04:14:30 -05:00
|
|
|
|
2013-08-08 03:53:37 -05:00
|
|
|
struct CleanPass : public Pass {
|
|
|
|
CleanPass() : Pass("clean", "remove unused cells and wires") { }
|
2020-06-18 18:34:52 -05:00
|
|
|
void help() override
|
2013-08-08 03:53:37 -05:00
|
|
|
{
|
|
|
|
// |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
|
|
|
|
log("\n");
|
2013-08-11 06:59:14 -05:00
|
|
|
log(" clean [options] [selection]\n");
|
2013-08-08 03:53:37 -05:00
|
|
|
log("\n");
|
2013-08-11 06:59:14 -05:00
|
|
|
log("This is identical to 'opt_clean', but less verbose.\n");
|
2013-08-08 03:53:37 -05:00
|
|
|
log("\n");
|
2014-09-06 01:47:06 -05:00
|
|
|
log("When commands are separated using the ';;' token, this command will be executed\n");
|
2013-08-11 06:33:38 -05:00
|
|
|
log("between the commands.\n");
|
|
|
|
log("\n");
|
2014-09-06 01:47:06 -05:00
|
|
|
log("When commands are separated using the ';;;' token, this command will be executed\n");
|
2013-08-11 06:59:14 -05:00
|
|
|
log("in -purge mode between the commands.\n");
|
|
|
|
log("\n");
|
2013-08-08 03:53:37 -05:00
|
|
|
}
|
2020-06-18 18:34:52 -05:00
|
|
|
void execute(std::vector<std::string> args, RTLIL::Design *design) override
|
2013-08-08 03:53:37 -05:00
|
|
|
{
|
2013-08-11 06:59:14 -05:00
|
|
|
bool purge_mode = false;
|
|
|
|
|
|
|
|
size_t argidx;
|
|
|
|
for (argidx = 1; argidx < args.size(); argidx++) {
|
|
|
|
if (args[argidx] == "-purge") {
|
|
|
|
purge_mode = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2020-05-14 02:19:58 -05:00
|
|
|
extra_args(args, argidx, design);
|
2013-08-08 03:53:37 -05:00
|
|
|
|
2015-08-12 07:10:14 -05:00
|
|
|
keep_cache.reset(design);
|
2015-08-10 15:14:21 -05:00
|
|
|
|
2013-10-17 13:48:40 -05:00
|
|
|
ct_reg.setup_internals_mem();
|
2022-07-21 07:22:15 -05:00
|
|
|
ct_reg.setup_internals_anyinit();
|
2013-10-17 13:48:40 -05:00
|
|
|
ct_reg.setup_stdcells_mem();
|
|
|
|
|
2014-02-07 16:50:17 -06:00
|
|
|
ct_all.setup(design);
|
|
|
|
|
2013-08-08 03:53:37 -05:00
|
|
|
count_rm_cells = 0;
|
|
|
|
count_rm_wires = 0;
|
|
|
|
|
2015-02-24 15:31:30 -06:00
|
|
|
for (auto module : design->selected_whole_modules()) {
|
|
|
|
if (module->has_processes())
|
2015-02-03 16:45:01 -06:00
|
|
|
continue;
|
2020-05-14 02:19:58 -05:00
|
|
|
rmunused_module(module, purge_mode, ys_debug(), true);
|
2013-08-08 03:53:37 -05:00
|
|
|
}
|
|
|
|
|
2019-04-22 10:25:52 -05:00
|
|
|
log_suppressed();
|
2013-08-08 03:53:37 -05:00
|
|
|
if (count_rm_cells > 0 || count_rm_wires > 0)
|
|
|
|
log("Removed %d unused cells and %d unused wires.\n", count_rm_cells, count_rm_wires);
|
|
|
|
|
2015-01-23 17:13:27 -06:00
|
|
|
design->optimize();
|
|
|
|
design->sort();
|
|
|
|
design->check();
|
|
|
|
|
2015-08-12 07:10:14 -05:00
|
|
|
keep_cache.reset();
|
2013-10-17 13:48:40 -05:00
|
|
|
ct_reg.clear();
|
2014-02-07 16:50:17 -06:00
|
|
|
ct_all.clear();
|
2013-08-08 03:53:37 -05:00
|
|
|
}
|
|
|
|
} CleanPass;
|
2015-07-02 04:14:30 -05:00
|
|
|
|
2014-09-27 09:17:53 -05:00
|
|
|
PRIVATE_NAMESPACE_END
|