mirror of
https://github.com/YosysHQ/yosys
synced 2026-07-23 23:52:32 +00:00
Merge remote-tracking branch 'upstream' into merge3
This commit is contained in:
commit
3783a820ee
655 changed files with 11031 additions and 9437 deletions
|
|
@ -217,7 +217,7 @@ struct CheckPass : public Pass {
|
|||
|
||||
const int threshold = 1024;
|
||||
|
||||
// if the multiplication may overflow we will catch it here
|
||||
// if the multiplication may overflow we will catch it here
|
||||
if (in_widths + out_widths >= threshold)
|
||||
return true;
|
||||
|
||||
|
|
@ -400,7 +400,7 @@ struct CheckPass : public Pass {
|
|||
|
||||
message += stringf(" cell %s (%s)%s\n", driver, driver->type.unescape(), driver_src);
|
||||
|
||||
if (!coarsened_cells.count(driver)) {
|
||||
if (!coarsened_cells.count(driver)) {
|
||||
MatchingEdgePrinter printer(message, sigmap, prev, bit);
|
||||
printer.add_edges_from_cell(driver);
|
||||
} else {
|
||||
|
|
@ -414,7 +414,7 @@ struct CheckPass : public Pass {
|
|||
std::string src_attr = wire->get_src_attribute();
|
||||
wire_src = stringf(" source: %s", src_attr);
|
||||
}
|
||||
message += stringf(" wire %s%s\n", log_signal(SigBit(wire, pair.second)), wire_src);
|
||||
message += stringf(" wire %s%s\n", log_signal(SigBit(wire, pair.second)), wire_src);
|
||||
}
|
||||
|
||||
prev = bit;
|
||||
|
|
@ -502,12 +502,12 @@ struct CheckMemPass : public Pass {
|
|||
for (auto &init : mem.inits) {
|
||||
int start = init.addr.as_int();
|
||||
if (start < min_addr) {
|
||||
log_warning("Mem %s.%s starts at %d but initializes address %d.\n", log_id(module), log_id(mem.mem), min_addr, start);
|
||||
log_warning("Mem %s.%s starts at %d but initializes address %d.\n", module, mem.mem, min_addr, start);
|
||||
counter++;
|
||||
}
|
||||
int end = start + (GetSize(init.data) / mem.width) - 1;
|
||||
if (end > max_addr) {
|
||||
log_warning("Mem %s.%s ends at %d but initializes address %d.\n", log_id(module), log_id(mem.mem), max_addr, end);
|
||||
log_warning("Mem %s.%s ends at %d but initializes address %d.\n", module, mem.mem, max_addr, end);
|
||||
counter++;
|
||||
}
|
||||
}
|
||||
|
|
@ -516,7 +516,7 @@ struct CheckMemPass : public Pass {
|
|||
if (addr_sig.is_fully_const()) {
|
||||
auto addr = addr_sig.as_int();
|
||||
if (addr < min_addr || addr > max_addr) {
|
||||
log_warning("Mem %s.%s contains entries for addresses %d..%d but %s address %d.\n", log_id(module), log_id(mem.mem), min_addr, max_addr, access, addr);
|
||||
log_warning("Mem %s.%s contains entries for addresses %d..%d but %s address %d.\n", module, mem.mem, min_addr, max_addr, access, addr);
|
||||
counter++;
|
||||
}
|
||||
} else if (nonconst_mode) {
|
||||
|
|
@ -525,7 +525,7 @@ struct CheckMemPass : public Pass {
|
|||
int addr_sig_min = 0;
|
||||
int addr_sig_max = (1 << addr_sig.size()) - 1;
|
||||
if (min_addr > addr_sig_min || max_addr < addr_sig_max) {
|
||||
log_warning("Mem %s.%s contains entries for addresses %d..%d but has a potentially dangerous non-const input %s\n", log_id(module), log_id(mem.mem), min_addr, max_addr, log_signal(addr_sig));
|
||||
log_warning("Mem %s.%s contains entries for addresses %d..%d but has a potentially dangerous non-const input %s\n", module, mem.mem, min_addr, max_addr, log_signal(addr_sig));
|
||||
counter++;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -401,4 +401,3 @@ struct DesignPass : public Pass {
|
|||
} DesignPass;
|
||||
|
||||
YOSYS_NAMESPACE_END
|
||||
|
||||
|
|
|
|||
|
|
@ -158,7 +158,6 @@ struct ExecPass : public Pass {
|
|||
int status = 0;
|
||||
int retval = 0;
|
||||
|
||||
#ifndef EMSCRIPTEN
|
||||
FILE *f = popen(cmd.c_str(), "r");
|
||||
if (f == nullptr)
|
||||
log_cmd_error("errno %d after popen() returned NULL.\n", errno);
|
||||
|
|
@ -183,7 +182,6 @@ struct ExecPass : public Pass {
|
|||
}
|
||||
}
|
||||
status = pclose(f);
|
||||
#endif
|
||||
|
||||
if(WIFEXITED(status)) {
|
||||
retval = WEXITSTATUS(status);
|
||||
|
|
|
|||
|
|
@ -90,7 +90,7 @@ struct CoveragePass : public Pass {
|
|||
|
||||
std::map<std::string, std::set<int>> uncovered_lines;
|
||||
std::map<std::string, std::set<int>> all_lines;
|
||||
|
||||
|
||||
for (auto module : design->modules())
|
||||
{
|
||||
log_debug("Module %s:\n", module);
|
||||
|
|
@ -136,7 +136,7 @@ struct CoveragePass : public Pass {
|
|||
fout << "DA:" << l << ",";
|
||||
if (uncovered_lines.count(file_entry.first) && uncovered_lines[file_entry.first].count(l))
|
||||
fout << "0";
|
||||
else
|
||||
else
|
||||
fout << "1";
|
||||
fout << "\n";
|
||||
}
|
||||
|
|
|
|||
|
|
@ -36,7 +36,7 @@ struct LinuxPerf : public Pass {
|
|||
bool formatted_help() override
|
||||
{
|
||||
auto *help = PrettyHelp::get_current();
|
||||
|
||||
|
||||
auto content_root = help->get_root();
|
||||
|
||||
content_root->usage("linux_perf [on|off]");
|
||||
|
|
|
|||
|
|
@ -106,7 +106,7 @@ struct LoggerPass : public Pass {
|
|||
}
|
||||
if (args[argidx] == "-warn" && argidx+1 < args.size()) {
|
||||
std::string pattern = args[++argidx];
|
||||
if (pattern.front() == '\"' && pattern.back() == '\"') pattern = pattern.substr(1, pattern.size() - 2);
|
||||
if (pattern.front() == '\"' && pattern.back() == '\"') pattern = pattern.substr(1, pattern.size() - 2);
|
||||
try {
|
||||
log("Added regex '%s' for warnings to warn list.\n", pattern);
|
||||
log_warn_regexes.push_back(YS_REGEX_COMPILE(pattern));
|
||||
|
|
@ -118,7 +118,7 @@ struct LoggerPass : public Pass {
|
|||
}
|
||||
if (args[argidx] == "-nowarn" && argidx+1 < args.size()) {
|
||||
std::string pattern = args[++argidx];
|
||||
if (pattern.front() == '\"' && pattern.back() == '\"') pattern = pattern.substr(1, pattern.size() - 2);
|
||||
if (pattern.front() == '\"' && pattern.back() == '\"') pattern = pattern.substr(1, pattern.size() - 2);
|
||||
try {
|
||||
log("Added regex '%s' for warnings to nowarn list.\n", pattern);
|
||||
log_nowarn_regexes.push_back(YS_REGEX_COMPILE(pattern));
|
||||
|
|
@ -130,7 +130,7 @@ struct LoggerPass : public Pass {
|
|||
}
|
||||
if (args[argidx] == "-werror" && argidx+1 < args.size()) {
|
||||
std::string pattern = args[++argidx];
|
||||
if (pattern.front() == '\"' && pattern.back() == '\"') pattern = pattern.substr(1, pattern.size() - 2);
|
||||
if (pattern.front() == '\"' && pattern.back() == '\"') pattern = pattern.substr(1, pattern.size() - 2);
|
||||
try {
|
||||
log("Added regex '%s' for warnings to werror list.\n", pattern);
|
||||
log_werror_regexes.push_back(YS_REGEX_COMPILE(pattern));
|
||||
|
|
|
|||
|
|
@ -1472,7 +1472,7 @@ struct SelectPass : public Pass {
|
|||
const char *common_flagset = "-add, -del, -assert-none, -assert-any, -assert-mod-count, -assert-count, -assert-max, or -assert-min";
|
||||
|
||||
if (common_flagset_tally > 1)
|
||||
log_cmd_error("Options %s can not be combined.\n", common_flagset);
|
||||
log_cmd_error("Options %s can not be combined.\n", common_flagset);
|
||||
|
||||
if ((list_mode || !write_file.empty() || count_mode) && common_flagset_tally)
|
||||
log_cmd_error("Options -list, -list-mod, -write and -count can not be combined with %s.\n", common_flagset);
|
||||
|
|
|
|||
|
|
@ -41,6 +41,7 @@ struct setunset_t
|
|||
if (!RTLIL::SigSpec::parse(sig_value, nullptr, set_value))
|
||||
log_cmd_error("Can't decode value '%s'!\n", set_value);
|
||||
value = sig_value.as_const();
|
||||
value.tag_bare_integer_const(set_value);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
|
|||
|
|
@ -47,14 +47,14 @@ struct SetenvPass : public Pass {
|
|||
std::string name = args[1];
|
||||
std::string value = args[2];
|
||||
if (value.front() == '\"' && value.back() == '\"') value = value.substr(1, value.size() - 2);
|
||||
|
||||
|
||||
#if defined(_WIN32)
|
||||
_putenv_s(name.c_str(), value.c_str());
|
||||
#else
|
||||
if (setenv(name.c_str(), value.c_str(), 1))
|
||||
log_cmd_error("Invalid name \"%s\".\n", name);
|
||||
#endif
|
||||
|
||||
|
||||
}
|
||||
} SetenvPass;
|
||||
|
||||
|
|
|
|||
|
|
@ -122,7 +122,7 @@ struct EstimateSta {
|
|||
if (aigs.at(fingerprint).name.empty()) {
|
||||
log_error("Unsupported cell '%s' in module '%s'",
|
||||
cell->type.unescape(), m);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
combinational.push_back(cell);
|
||||
|
|
@ -217,9 +217,9 @@ struct EstimateSta {
|
|||
|
||||
if (!topo.sort())
|
||||
log_error("Module '%s' contains combinational loops", m);
|
||||
|
||||
|
||||
// now we determine how long it takes for signals to stabilize
|
||||
|
||||
|
||||
// `levels` records the time after a clock edge after which a signal is stable
|
||||
dict<std::tuple<SigBit, Cell *, AigNode *>, arrivalint> levels;
|
||||
|
||||
|
|
|
|||
|
|
@ -60,6 +60,10 @@ struct EquivOptPass:public ScriptPass
|
|||
log(" -undef\n");
|
||||
log(" enable modelling of undef states during equiv_induct.\n");
|
||||
log("\n");
|
||||
log(" -ignore-unknown-cells\n");
|
||||
log(" do not check the internals of unknown (blackbox) cells during\n");
|
||||
log(" equiv_induct.\n");
|
||||
log("\n");
|
||||
log(" -nocells\n");
|
||||
log(" Do not check for equivalent cells, just wires.\n");
|
||||
log("\n");
|
||||
|
|
@ -74,16 +78,17 @@ struct EquivOptPass:public ScriptPass
|
|||
log("\n");
|
||||
}
|
||||
|
||||
std::string command, techmap_opts, make_opts, induct_opts;
|
||||
bool assert, multiclock, async2sync, nocheck, post;
|
||||
std::string command, techmap_opts, make_opts;
|
||||
bool assert, undef, ignore_unknown_cells, multiclock, async2sync, nocheck, post;
|
||||
|
||||
void clear_flags() override
|
||||
{
|
||||
command = "";
|
||||
techmap_opts = "";
|
||||
make_opts = "";
|
||||
induct_opts = "";
|
||||
assert = false;
|
||||
undef = false;
|
||||
ignore_unknown_cells = false;
|
||||
multiclock = false;
|
||||
async2sync = false;
|
||||
nocheck = false;
|
||||
|
|
@ -118,11 +123,11 @@ struct EquivOptPass:public ScriptPass
|
|||
continue;
|
||||
}
|
||||
if (args[argidx] == "-undef") {
|
||||
induct_opts += " -undef";
|
||||
undef = true;
|
||||
continue;
|
||||
}
|
||||
if (args[argidx] == "-ignore-unknown-cells") {
|
||||
induct_opts += " -ignore-unknown-cells";
|
||||
ignore_unknown_cells = true;
|
||||
continue;
|
||||
}
|
||||
if (args[argidx] == "-nocheck") {
|
||||
|
|
@ -216,11 +221,16 @@ struct EquivOptPass:public ScriptPass
|
|||
else
|
||||
opts = make_opts;
|
||||
run("equiv_make" + opts + " gold gate equiv");
|
||||
string induct_opts;
|
||||
if (help_mode)
|
||||
opts = " [-undef] [-ignore-unknown-cells]";
|
||||
else
|
||||
opts = induct_opts;
|
||||
run("equiv_induct" + opts + " equiv");
|
||||
induct_opts = " [-undef] [-ignore-unknown-cells]";
|
||||
else {
|
||||
if (undef)
|
||||
induct_opts += " -undef";
|
||||
if (ignore_unknown_cells)
|
||||
induct_opts += " -ignore-unknown-cells";
|
||||
}
|
||||
run("equiv_induct" + induct_opts + " equiv");
|
||||
if (help_mode)
|
||||
run("equiv_status [-assert] equiv");
|
||||
else if (assert)
|
||||
|
|
|
|||
|
|
@ -985,7 +985,9 @@ struct HierarchyPass : public Pass {
|
|||
SigSpec sig_value;
|
||||
if (!RTLIL::SigSpec::parse(sig_value, NULL, para.second))
|
||||
log_cmd_error("Can't decode value '%s'!\n", para.second);
|
||||
top_parameters[RTLIL::escape_id(para.first)] = sig_value.as_const();
|
||||
RTLIL::Const c = sig_value.as_const();
|
||||
c.tag_bare_integer_const(para.second);
|
||||
top_parameters[RTLIL::escape_id(para.first)] = c;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -1073,7 +1075,9 @@ struct HierarchyPass : public Pass {
|
|||
SigSpec sig_value;
|
||||
if (!RTLIL::SigSpec::parse(sig_value, NULL, para.second))
|
||||
log_cmd_error("Can't decode value '%s'!\n", para.second);
|
||||
top_parameters[RTLIL::escape_id(para.first)] = sig_value.as_const();
|
||||
RTLIL::Const c = sig_value.as_const();
|
||||
c.tag_bare_integer_const(para.second);
|
||||
top_parameters[RTLIL::escape_id(para.first)] = c;
|
||||
}
|
||||
|
||||
top_mod = design->module(top_mod->derive(design, top_parameters));
|
||||
|
|
|
|||
|
|
@ -343,7 +343,7 @@ struct MemMapping {
|
|||
rejected_cfg_debug_msgs += "\n";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void log_reject(const Ram &ram, const PortGroup &pg, int pvi, std::string message) {
|
||||
if(ys_debug(1)) {
|
||||
rejected_cfg_debug_msgs += stringf("can't map to option selection [");
|
||||
|
|
@ -516,7 +516,7 @@ std::pair<bool, Const> search_for_attribute(Mem mem, IdString attr) {
|
|||
for (SigBit bit: port.addr)
|
||||
if (bit.is_wire() && bit.wire->has_attribute(attr))
|
||||
return std::make_pair(true, bit.wire->attributes.at(attr));
|
||||
|
||||
|
||||
return std::make_pair(false, Const());
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -159,7 +159,7 @@ struct MuxpackWorker
|
|||
if (cell->type == ID($mux))
|
||||
b_sig = sigmap(cell->getPort(ID::B));
|
||||
SigSpec y_sig = sigmap(cell->getPort(ID::Y));
|
||||
|
||||
|
||||
if (sig_chain_next.count(a_sig))
|
||||
for (auto a_bit : a_sig)
|
||||
sigbit_with_non_chain_users.insert(a_bit);
|
||||
|
|
|
|||
|
|
@ -1993,6 +1993,56 @@ skip_identity:
|
|||
}
|
||||
skip_alu_split:
|
||||
|
||||
// replace (2^k-1)-x with ~x when x is known to be smaller than 2^k
|
||||
if (do_fine && cell->type == ID($sub))
|
||||
{
|
||||
int y_width = GetSize(cell->getPort(ID::Y));
|
||||
bool a_signed = cell->getParam(ID::A_SIGNED).as_bool();
|
||||
|
||||
RTLIL::SigSpec sig_a = assign_map(cell->getPort(ID::A));
|
||||
sig_a.extend_u0(y_width, a_signed);
|
||||
|
||||
if (y_width > 0 && sig_a.is_fully_const())
|
||||
{
|
||||
RTLIL::Const a_val = sig_a.as_const();
|
||||
|
||||
int k = 0;
|
||||
while (k < y_width && a_val[k] == State::S1)
|
||||
k++;
|
||||
|
||||
bool a_is_mask = k > 0;
|
||||
for (int i = k; a_is_mask && i < y_width; i++)
|
||||
if (a_val[i] != State::S0)
|
||||
a_is_mask = false;
|
||||
|
||||
if (a_is_mask)
|
||||
{
|
||||
bool b_signed = cell->getParam(ID::B_SIGNED).as_bool();
|
||||
RTLIL::SigSpec sig_b = assign_map(cell->getPort(ID::B));
|
||||
sig_b.extend_u0(y_width, b_signed);
|
||||
|
||||
bool b_fits = true;
|
||||
for (int i = k; b_fits && i < y_width; i++)
|
||||
if (sig_b[i] != State::S0)
|
||||
b_fits = false;
|
||||
|
||||
if (b_fits)
|
||||
{
|
||||
RTLIL::SigSpec sig_y = module->Not(NEW_ID, sig_b.extract(0, k));
|
||||
if (y_width > k)
|
||||
sig_y.append(RTLIL::SigSpec(State::S0, y_width - k));
|
||||
|
||||
log_debug("Replacing `(2^%d-1) - B` $sub cell `%s' in module `%s' with $not.\n",
|
||||
k, cell->name.c_str(), module->name.c_str());
|
||||
module->connect(cell->getPort(ID::Y), sig_y);
|
||||
module->remove(cell);
|
||||
did_something = true;
|
||||
goto next_cell;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// remove redundant pairs of bits in ==, ===, !=, and !==
|
||||
// replace cell with const driver if inputs can't be equal
|
||||
if (do_fine && cell->type.in(ID($eq), ID($ne), ID($eqx), ID($nex)))
|
||||
|
|
|
|||
|
|
@ -296,7 +296,7 @@ struct OptLutWorker
|
|||
luts_dlogic_inputs.erase(lut);
|
||||
|
||||
module->remove(lut);
|
||||
|
||||
|
||||
eliminated_count++;
|
||||
if (limit > 0)
|
||||
limit--;
|
||||
|
|
|
|||
|
|
@ -282,4 +282,3 @@ struct OptLutInsPass : public Pass {
|
|||
} OptLutInsPass;
|
||||
|
||||
PRIVATE_NAMESPACE_END
|
||||
|
||||
|
|
|
|||
|
|
@ -347,4 +347,3 @@ struct OptMemFeedbackPass : public Pass {
|
|||
} OptMemFeedbackPass;
|
||||
|
||||
PRIVATE_NAMESPACE_END
|
||||
|
||||
|
|
|
|||
|
|
@ -106,4 +106,3 @@ struct OptMemPriorityPass : public Pass {
|
|||
} OptMemPriorityPass;
|
||||
|
||||
PRIVATE_NAMESPACE_END
|
||||
|
||||
|
|
|
|||
|
|
@ -68,7 +68,7 @@ struct OptMuxtreeWorker
|
|||
// Is bit directly used by non-mux cells or ports?
|
||||
bool seen_non_mux;
|
||||
pool<int> mux_users;
|
||||
pool<int> mux_drivers;
|
||||
std::optional<int> mux_driver;
|
||||
};
|
||||
|
||||
idict<SigBit> bit2num;
|
||||
|
|
@ -107,7 +107,7 @@ struct OptMuxtreeWorker
|
|||
// Populate bit2info[]:
|
||||
// .seen_non_mux
|
||||
// .mux_users
|
||||
// .mux_drivers
|
||||
// .mux_driver
|
||||
// Populate mux2info[].ports[]:
|
||||
// .ctrl_sig
|
||||
// .input_sigs
|
||||
|
|
@ -137,8 +137,11 @@ struct OptMuxtreeWorker
|
|||
// Analyze port A
|
||||
muxinfo.ports.push_back(used_port_bit(sig_a, this_mux_idx));
|
||||
|
||||
for (int idx : sig2bits(sig_y))
|
||||
bit2info[idx].mux_drivers.insert(this_mux_idx);
|
||||
for (int idx : sig2bits(sig_y)) {
|
||||
if (bit2info[idx].mux_driver)
|
||||
log_cmd_error("Cell %s Y port signal %s already driven by %s\n", cell->name, log_signal(sig_y), mux2info[*bit2info[idx].mux_driver].cell->name);
|
||||
bit2info[idx].mux_driver = this_mux_idx;
|
||||
}
|
||||
|
||||
for (int idx : sig2bits(sig_s))
|
||||
bit2info[idx].seen_non_mux = true;
|
||||
|
|
@ -170,8 +173,8 @@ struct OptMuxtreeWorker
|
|||
for (int j : bit2info[i].mux_users)
|
||||
for (auto &p : mux2info[j].ports) {
|
||||
if (p.input_sigs.count(i))
|
||||
for (int k : bit2info[i].mux_drivers)
|
||||
p.input_muxes.insert(k);
|
||||
if (bit2info[i].mux_driver)
|
||||
p.input_muxes.insert(*bit2info[i].mux_driver);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -184,14 +187,14 @@ struct OptMuxtreeWorker
|
|||
root_muxes.resize(GetSize(mux2info));
|
||||
|
||||
for (auto &bi : bit2info) {
|
||||
for (int i : bi.mux_drivers)
|
||||
if (bi.mux_driver)
|
||||
for (int j : bi.mux_users)
|
||||
mux_to_users[i].insert(j);
|
||||
mux_to_users[*bi.mux_driver].insert(j);
|
||||
if (!bi.seen_non_mux)
|
||||
continue;
|
||||
for (int mux_idx : bi.mux_drivers) {
|
||||
root_muxes.at(mux_idx) = true;
|
||||
root_enable_muxes.at(mux_idx) = true;
|
||||
if (bi.mux_driver) {
|
||||
root_muxes.at(*bi.mux_driver) = true;
|
||||
root_enable_muxes.at(*bi.mux_driver) = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -55,19 +55,19 @@ code
|
|||
int c_const_int = c_const.as_int(c_const_signed);
|
||||
int b_const_int_shifted = b_const_int << offset;
|
||||
|
||||
// Helper lambdas for two's complement math
|
||||
// Helper lambdas for two's complement math
|
||||
auto sign2sComplement = [](auto value, int numBits) {
|
||||
if (value & (1 << (numBits - 1))) {
|
||||
return -1;
|
||||
return -1;
|
||||
} else {
|
||||
return 1;
|
||||
return 1;
|
||||
}
|
||||
};
|
||||
auto twosComplement = [](auto value, int numBits) {
|
||||
if (value & (1 << (numBits - 1))) {
|
||||
return (~value) + 1; // invert bits before adding 1
|
||||
} else {
|
||||
return value;
|
||||
return value;
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -104,8 +104,8 @@ code
|
|||
std::string location = shift->get_src_attribute();
|
||||
|
||||
if(shiftadd_max_ratio>0 && offset<0 && -offset*shiftadd_max_ratio > old_a.size()) {
|
||||
log_warning("at %s: candiate for shiftadd optimization (shifting '%s' by '%s - %d' bits) "
|
||||
"was ignored to avoid high resource usage, see help peepopt\n",
|
||||
log_warning("at %s: candiate for shiftadd optimization (shifting '%s' by '%s - %d' bits) "
|
||||
"was ignored to avoid high resource usage, see help peepopt\n",
|
||||
location.c_str(), log_signal(old_a), log_signal(var_signal), -offset);
|
||||
reject;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -684,4 +684,3 @@ struct WreducePass : public Pass {
|
|||
} WreducePass;
|
||||
|
||||
PRIVATE_NAMESPACE_END
|
||||
|
||||
|
|
|
|||
|
|
@ -201,6 +201,77 @@ struct proc_dlatch_db_t
|
|||
sig[index] = State::Sx;
|
||||
cell->setPort(ID::A, sig);
|
||||
}
|
||||
bool sibling_undef = true;
|
||||
for (int i = 0; i < (is_bwmux ? 1 : GetSize(sig_s)); i++)
|
||||
if (sig_b[i*width + index] != State::Sx)
|
||||
sibling_undef = false;
|
||||
if (!sibling_undef) {
|
||||
if (!is_bwmux) {
|
||||
for (int i = 0; i < GetSize(sig_s); i++)
|
||||
n = make_inner(sig_s[i], State::S0, n);
|
||||
} else {
|
||||
n = make_inner(sig_s[index], State::S0, n);
|
||||
}
|
||||
}
|
||||
children.insert(n);
|
||||
}
|
||||
|
||||
for (int i = 0; i < (is_bwmux ? 1 : GetSize(sig_s)); i++) {
|
||||
n = find_mux_feedback(sig_b[i*width + index], needle, set_undef);
|
||||
if (n != false_node) {
|
||||
if (set_undef && sig_b[i*width + index] == needle) {
|
||||
SigSpec sig = cell->getPort(ID::B);
|
||||
sig[i*width + index] = State::Sx;
|
||||
cell->setPort(ID::B, sig);
|
||||
}
|
||||
bool sibling_undef = (sig_a[index] == State::Sx);
|
||||
if (!is_bwmux)
|
||||
for (int j = 0; j < GetSize(sig_s); j++)
|
||||
if (j != i && sig_b[j*width + index] != State::Sx)
|
||||
sibling_undef = false;
|
||||
if (!sibling_undef)
|
||||
n = make_inner(sig_s[is_bwmux ? index : i], State::S1, n);
|
||||
children.insert(n);
|
||||
}
|
||||
}
|
||||
|
||||
if (children.empty())
|
||||
return false_node;
|
||||
|
||||
return make_inner(children);
|
||||
}
|
||||
|
||||
int find_mux_constant(SigBit haystack, State needle, bool set_undef)
|
||||
{
|
||||
if (sigusers[haystack] > 1)
|
||||
set_undef = false;
|
||||
|
||||
if (haystack == SigBit(needle))
|
||||
return true_node;
|
||||
|
||||
auto it = mux_drivers.find(haystack);
|
||||
if (it == mux_drivers.end())
|
||||
return false_node;
|
||||
|
||||
Cell *cell = it->second.first;
|
||||
int index = it->second.second;
|
||||
|
||||
log_assert(cell->type.in(ID($mux), ID($pmux), ID($bwmux)));
|
||||
bool is_bwmux = (cell->type == ID($bwmux));
|
||||
SigSpec sig_a = sigmap(cell->getPort(ID::A));
|
||||
SigSpec sig_b = sigmap(cell->getPort(ID::B));
|
||||
SigSpec sig_s = sigmap(cell->getPort(ID::S));
|
||||
int width = GetSize(sig_a);
|
||||
|
||||
pool<int> children;
|
||||
|
||||
int n = find_mux_constant(sig_a[index], needle, set_undef);
|
||||
if (n != false_node) {
|
||||
if (set_undef && sig_a[index] == SigBit(needle)) {
|
||||
SigSpec sig = cell->getPort(ID::A);
|
||||
sig[index] = State::Sx;
|
||||
cell->setPort(ID::A, sig);
|
||||
}
|
||||
if (!is_bwmux) {
|
||||
for (int i = 0; i < GetSize(sig_s); i++)
|
||||
n = make_inner(sig_s[i], State::S0, n);
|
||||
|
|
@ -211,9 +282,9 @@ struct proc_dlatch_db_t
|
|||
}
|
||||
|
||||
for (int i = 0; i < (is_bwmux ? 1 : GetSize(sig_s)); i++) {
|
||||
n = find_mux_feedback(sig_b[i*width + index], needle, set_undef);
|
||||
n = find_mux_constant(sig_b[i*width + index], needle, set_undef);
|
||||
if (n != false_node) {
|
||||
if (set_undef && sig_b[i*width + index] == needle) {
|
||||
if (set_undef && sig_b[i*width + index] == SigBit(needle)) {
|
||||
SigSpec sig = cell->getPort(ID::B);
|
||||
sig[i*width + index] = State::Sx;
|
||||
cell->setPort(ID::B, sig);
|
||||
|
|
@ -349,7 +420,7 @@ void proc_dlatch(proc_dlatch_db_t &db, RTLIL::Process *proc)
|
|||
{
|
||||
RTLIL::SigSig latches_bits, nolatches_bits;
|
||||
dict<SigBit, SigBit> latches_out_in;
|
||||
dict<SigBit, int> latches_hold;
|
||||
dict<SigBit, int> latches_hold, latches_rst, latches_set;
|
||||
std::string src = proc->get_src_attribute();
|
||||
|
||||
for (auto sr : proc->syncs)
|
||||
|
|
@ -381,15 +452,31 @@ void proc_dlatch(proc_dlatch_db_t &db, RTLIL::Process *proc)
|
|||
|
||||
latches_out_in.sort();
|
||||
for (auto &it : latches_out_in) {
|
||||
int n = db.find_mux_feedback(it.second, it.first, true);
|
||||
if (n == db.false_node) {
|
||||
if (db.find_mux_feedback(it.second, it.first, false) == db.false_node) {
|
||||
nolatches_bits.first.append(it.first);
|
||||
nolatches_bits.second.append(it.second);
|
||||
} else {
|
||||
latches_bits.first.append(it.first);
|
||||
latches_bits.second.append(it.second);
|
||||
latches_hold[it.first] = n;
|
||||
continue;
|
||||
}
|
||||
|
||||
latches_bits.first.append(it.first);
|
||||
latches_bits.second.append(it.second);
|
||||
int nrst = db.find_mux_constant(it.second, State::S0, false);
|
||||
int nset = db.find_mux_constant(it.second, State::S1, false);
|
||||
bool has_rst = (nrst != db.false_node);
|
||||
bool has_set = (nset != db.false_node);
|
||||
|
||||
if (has_rst && !has_set)
|
||||
nrst = db.find_mux_constant(it.second, State::S0, true);
|
||||
else if (has_set && !has_rst)
|
||||
nset = db.find_mux_constant(it.second, State::S1, true);
|
||||
else
|
||||
nrst = nset = db.false_node;
|
||||
|
||||
int n = db.find_mux_feedback(it.second, it.first, true);
|
||||
log_assert(n != db.false_node);
|
||||
latches_hold[it.first] = n;
|
||||
latches_rst[it.first] = nrst;
|
||||
latches_set[it.first] = nset;
|
||||
}
|
||||
|
||||
int offset = 0;
|
||||
|
|
@ -423,20 +510,35 @@ void proc_dlatch(proc_dlatch_db_t &db, RTLIL::Process *proc)
|
|||
while (offset < GetSize(latches_bits.first))
|
||||
{
|
||||
int width = 1;
|
||||
int n = latches_hold[latches_bits.first[offset]];
|
||||
Wire *w = latches_bits.first[offset].wire;
|
||||
SigBit obit = latches_bits.first[offset];
|
||||
int n = latches_hold[obit];
|
||||
int nrst = latches_rst[obit];
|
||||
int nset = latches_set[obit];
|
||||
Wire *w = obit.wire;
|
||||
|
||||
if (w != nullptr)
|
||||
{
|
||||
while (offset+width < GetSize(latches_bits.first) &&
|
||||
n == latches_hold[latches_bits.first[offset+width]] &&
|
||||
nrst == latches_rst[latches_bits.first[offset+width]] &&
|
||||
nset == latches_set[latches_bits.first[offset+width]] &&
|
||||
w == latches_bits.first[offset+width].wire)
|
||||
width++;
|
||||
|
||||
SigSpec lhs = latches_bits.first.extract(offset, width);
|
||||
SigSpec rhs = latches_bits.second.extract(offset, width);
|
||||
|
||||
Cell *cell = db.module->addDlatch(NEW_ID, db.module->Not(NEW_ID, db.make_hold(n, src)), rhs, lhs);
|
||||
SigBit en = db.module->Not(NEW_ID, db.make_hold(n, src));
|
||||
bool has_rst = (nrst != db.false_node);
|
||||
bool has_set = (nset != db.false_node);
|
||||
|
||||
Cell *cell;
|
||||
if (has_rst)
|
||||
cell = db.module->addAdlatch(NEW_ID, en, db.make_hold(nrst, src), rhs, lhs, RTLIL::Const(State::S0, width));
|
||||
else if (has_set)
|
||||
cell = db.module->addAdlatch(NEW_ID, en, db.make_hold(nset, src), rhs, lhs, RTLIL::Const(State::S1, width));
|
||||
else
|
||||
cell = db.module->addDlatch(NEW_ID, en, rhs, lhs);
|
||||
cell->set_src_attribute(src);
|
||||
db.generated_dlatches.insert(cell);
|
||||
|
||||
|
|
|
|||
|
|
@ -117,4 +117,3 @@ struct ProcMemWrPass : public Pass {
|
|||
} ProcMemWrPass;
|
||||
|
||||
PRIVATE_NAMESPACE_END
|
||||
|
||||
|
|
|
|||
|
|
@ -116,7 +116,7 @@ struct CutpointPass : public Pass {
|
|||
for (auto bit : sigmap(conn.second))
|
||||
if (bit.wire)
|
||||
wire_drivers.insert(bit);
|
||||
|
||||
|
||||
for (auto wire : module->wires())
|
||||
if (wire->port_input)
|
||||
for (auto bit : sigmap(wire))
|
||||
|
|
|
|||
|
|
@ -82,4 +82,3 @@ always @(posedge clk)
|
|||
assign y = counter == 12;
|
||||
|
||||
endmodule
|
||||
|
||||
|
|
|
|||
|
|
@ -11,4 +11,3 @@ sat -show rst,counter -set-at 3 y 1'b1 -seq 4 example004
|
|||
|
||||
sat -prove y 1'b0 -show rst,counter,y -ignore_unknown_cells example004
|
||||
sat -prove y 1'b0 -tempinduct -show rst,counter,y -set-at 1 rst 1'b1 -seq 1 example004
|
||||
|
||||
|
|
|
|||
|
|
@ -132,7 +132,7 @@ struct QbfSolutionType {
|
|||
//More importantly, we want to have the ability to port hole assignments to other modules with compatible
|
||||
//hole names and widths. Obviously in those cases source locations of the $anyconst cells will not match.
|
||||
//
|
||||
//Option 2 has the benefits previously described, but wire names can be changed automatically by
|
||||
//Option 2 has the benefits previously described, but wire names can be changed automatically by
|
||||
//optimization or techmapping passes, especially when (ex/im)porting from BLIF for optimization with ABC.
|
||||
//
|
||||
//The approach taken here is to allow both options. We write the assignment information for each bit of
|
||||
|
|
|
|||
|
|
@ -27,6 +27,7 @@
|
|||
#include "kernel/yw.h"
|
||||
#include "kernel/json.h"
|
||||
#include "kernel/fmt.h"
|
||||
#include "kernel/drivertools.h"
|
||||
#include "passes/silimate/reg_rename.h"
|
||||
|
||||
#include <ctime>
|
||||
|
|
@ -143,6 +144,8 @@ struct SimShared
|
|||
bool serious_asserts = false;
|
||||
bool fst_noinit = false;
|
||||
bool initstate = true;
|
||||
bool undriven_check = true;
|
||||
bool undriven_warning = false;
|
||||
bool blackbox_children = false;
|
||||
pool<IdString> instance_root_modules;
|
||||
double clk_period_override = 0.0;
|
||||
|
|
@ -506,7 +509,7 @@ struct SimInstance
|
|||
{
|
||||
Const value = get_state_mapped(sigmap(sig));
|
||||
if (shared->debug)
|
||||
log("[%s] get %s: %s\n", hiername(), log_signal(sig), log_signal(value));
|
||||
log("[%s] get %s: %s\n", hiername(), log_signal(sig), log_signal(value, true));
|
||||
return value;
|
||||
}
|
||||
|
||||
|
|
@ -519,7 +522,7 @@ struct SimInstance
|
|||
log("GetSize(sig) %s: %d, GetSize(value) %s: %d\n",
|
||||
log_signal(sig),
|
||||
GetSize(sig),
|
||||
log_signal(value),
|
||||
log_const(value),
|
||||
GetSize(value));
|
||||
}
|
||||
log_assert(GetSize(sig) <= GetSize(value));
|
||||
|
|
@ -532,7 +535,7 @@ struct SimInstance
|
|||
}
|
||||
|
||||
if (shared->debug)
|
||||
log("[%s] set %s: %s\n", hiername(), log_signal(sig), log_signal(value));
|
||||
log("[%s] set %s: %s\n", hiername(), log_signal(sig), log_signal(value, true));
|
||||
return did_something;
|
||||
}
|
||||
|
||||
|
|
@ -1311,7 +1314,7 @@ struct SimInstance
|
|||
(unsigned long)time,
|
||||
shared->fst->getTimescaleString(),
|
||||
scope.c_str(), log_id(wire->name),
|
||||
log_signal(sim_val), log_signal(vcd_val));
|
||||
log_const(sim_val), log_const(vcd_val));
|
||||
}
|
||||
// Overwrite simulation register state with the ground truth
|
||||
did_something |= set_state(wire, vcd_val);
|
||||
|
|
@ -1347,6 +1350,54 @@ struct SimInstance
|
|||
child.second->addAdditionalInputs();
|
||||
}
|
||||
|
||||
// Preconditions / assumptions:
|
||||
// 1) fst_handles is populated for this instance (0 handle means not in trace).
|
||||
// 2) fst_inputs is finalized (top-level inputs + addAdditionalInputs() for $anyseq).
|
||||
// 3) module has no processes (sim enforces proc-lowered input before this point).
|
||||
// 4) sigmap is valid for per-bit queries on this instance.
|
||||
// 5) shared->fst is active, i.e. this is called from FST/VCD replay flow.
|
||||
int checkUndrivenReplaySignals(bool &any_undriven_found)
|
||||
{
|
||||
int issue_count = 0;
|
||||
bool has_replay_candidates = false;
|
||||
|
||||
for (auto &item : fst_handles)
|
||||
if (item.second != 0 && !fst_inputs.count(item.first)) {
|
||||
has_replay_candidates = true;
|
||||
break;
|
||||
}
|
||||
|
||||
if (has_replay_candidates) {
|
||||
DriverMap drivermap(module->design);
|
||||
drivermap.add(module);
|
||||
|
||||
for (auto &item : fst_handles) {
|
||||
Wire *wire = item.first;
|
||||
if (item.second == 0 || fst_inputs.count(wire))
|
||||
continue;
|
||||
|
||||
SigSpec undriven;
|
||||
for (auto bit : sigmap(wire))
|
||||
if (bit.wire != nullptr && drivermap(DriveBit(bit)).is_none())
|
||||
undriven.append(bit);
|
||||
|
||||
undriven.sort_and_unify();
|
||||
if (undriven.empty())
|
||||
continue;
|
||||
|
||||
issue_count++;
|
||||
any_undriven_found = true;
|
||||
std::string wire_name = scope + "." + RTLIL::unescape_id(wire->name);
|
||||
log_warning("Input trace contains undriven signal `%s` (%s).\n", wire_name.c_str(), log_signal(undriven));
|
||||
}
|
||||
}
|
||||
|
||||
for (auto child : children)
|
||||
issue_count += child.second->checkUndrivenReplaySignals(any_undriven_found);
|
||||
|
||||
return issue_count;
|
||||
}
|
||||
|
||||
bool setInputs()
|
||||
{
|
||||
bool did_something = false;
|
||||
|
|
@ -1404,7 +1455,7 @@ struct SimInstance
|
|||
if (shared->sim_mode == SimulationMode::gate && !fst_val.is_fully_def()) { // FST data contains X
|
||||
for(int i=0;i<fst_val.size();i++) {
|
||||
if (fst_val[i]!=State::Sx && fst_val[i]!=sim_val[i]) {
|
||||
log_warning("Signal '%s.%s' in file %s in simulation %s\n", scope, item.first, log_signal(fst_val), log_signal(sim_val));
|
||||
log_warning("Signal '%s.%s' in file %s in simulation %s\n", scope, item.first, log_signal(fst_val, true), log_signal(sim_val, true));
|
||||
retVal = true;
|
||||
break;
|
||||
}
|
||||
|
|
@ -1412,14 +1463,14 @@ struct SimInstance
|
|||
} else if (shared->sim_mode == SimulationMode::gold && !sim_val.is_fully_def()) { // sim data contains X
|
||||
for(int i=0;i<sim_val.size();i++) {
|
||||
if (sim_val[i]!=State::Sx && fst_val[i]!=sim_val[i]) {
|
||||
log_warning("Signal '%s.%s' in file %s in simulation %s\n", scope, item.first, log_signal(fst_val), log_signal(sim_val));
|
||||
log_warning("Signal '%s.%s' in file %s in simulation %s\n", scope, item.first, log_signal(fst_val, true), log_signal(sim_val, true));
|
||||
retVal = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (fst_val!=sim_val) {
|
||||
log_warning("Signal '%s.%s' in file %s in simulation '%s'\n", scope, item.first, log_signal(fst_val), log_signal(sim_val));
|
||||
log_warning("Signal '%s.%s' in file %s in simulation '%s'\n", scope, item.first, log_signal(fst_val, true), log_signal(sim_val, true));
|
||||
retVal = true;
|
||||
}
|
||||
}
|
||||
|
|
@ -1710,6 +1761,16 @@ struct SimWorker : SimShared
|
|||
}
|
||||
|
||||
register_signals();
|
||||
top->addAdditionalInputs();
|
||||
if (undriven_check) {
|
||||
bool any_undriven_found = false;
|
||||
int issue_count = top->checkUndrivenReplaySignals(any_undriven_found);
|
||||
if (any_undriven_found)
|
||||
log_warning("Values for the undriven signal(s) listed above are not replayed from FST/VCD input.\n");
|
||||
if (issue_count > 0 && !undriven_warning)
|
||||
log_cmd_error("Found %d undriven signal%s in the replay trace. Use -undriven-warn to continue or -no-undriven-check to disable this check.\n",
|
||||
issue_count, issue_count == 1 ? "" : "s");
|
||||
}
|
||||
|
||||
uint64_t startCount = 0;
|
||||
uint64_t stopCount = 0;
|
||||
|
|
@ -3107,8 +3168,15 @@ struct SimPass : public Pass {
|
|||
log(" -r <filename>\n");
|
||||
log(" read simulation or formal results file\n");
|
||||
log(" File formats supported: FST, VCD, AIW, WIT and .yw\n");
|
||||
log(" Yosys witness (.yw) replay is preferred when possible.\n");
|
||||
log(" VCD support requires vcd2fst external tool to be present\n");
|
||||
log("\n");
|
||||
log(" -no-undriven-check\n");
|
||||
log(" skip undriven-signal checks for FST/VCD replay (can be expensive for large designs)\n");
|
||||
log("\n");
|
||||
log(" -undriven-warn\n");
|
||||
log(" downgrade undriven-signal replay errors to warnings\n");
|
||||
log("\n");
|
||||
log(" -width <integer>\n");
|
||||
log(" cycle width in generated simulation output (must be divisible by 2).\n");
|
||||
log("\n");
|
||||
|
|
@ -3350,6 +3418,14 @@ struct SimPass : public Pass {
|
|||
worker.fst_noinit = true;
|
||||
continue;
|
||||
}
|
||||
if (args[argidx] == "-no-undriven-check") {
|
||||
worker.undriven_check = false;
|
||||
continue;
|
||||
}
|
||||
if (args[argidx] == "-undriven-warn") {
|
||||
worker.undriven_warning = true;
|
||||
continue;
|
||||
}
|
||||
if (args[argidx] == "-x") {
|
||||
worker.ignore_x = true;
|
||||
continue;
|
||||
|
|
|
|||
|
|
@ -159,7 +159,7 @@ void SynthPropWorker::run()
|
|||
if (tracing_data[module].names.size() == 0) return;
|
||||
|
||||
if (!reset_name.empty()) {
|
||||
int width = tracing_data[module].names.size();
|
||||
int width = tracing_data[module].names.size();
|
||||
SigSpec reset = module->wire(reset_name);
|
||||
reset.extend_u0(width, true);
|
||||
|
||||
|
|
|
|||
|
|
@ -67,11 +67,15 @@ yosys_pass(abc9_ops
|
|||
REQUIRES
|
||||
proc
|
||||
)
|
||||
yosys_pass(abc_ops_reintegrate
|
||||
abc_ops_reintegrate.cc
|
||||
)
|
||||
yosys_pass(abc9
|
||||
abc9.cc
|
||||
DEFINITIONS
|
||||
${abc_definitions}
|
||||
REQUIRES
|
||||
abc_ops_reintegrate
|
||||
abc9_exe
|
||||
abc9_ops
|
||||
aigmap
|
||||
|
|
|
|||
|
|
@ -378,7 +378,7 @@ struct Abc9Pass : public ScriptPass
|
|||
run(" write_xaiger -map <abc-temp-dir>/input.sym [-dff] <abc-temp-dir>/input.xaig");
|
||||
run(" abc9_exe [options] -cwd <abc-temp-dir> -lut [<abc-temp-dir>/input.lut] -box [<abc-temp-dir>/input.box]");
|
||||
run(" read_aiger -xaiger -wideports -module_name <module-name>$abc9 -map <abc-temp-dir>/input.sym <abc-temp-dir>/output.aig");
|
||||
run(" abc9_ops -reintegrate [-dff]");
|
||||
run(" abc_ops_reintegrate [-dff]");
|
||||
}
|
||||
else {
|
||||
auto selected_modules = active_design->selected_modules();
|
||||
|
|
@ -398,7 +398,7 @@ struct Abc9Pass : public ScriptPass
|
|||
log_error("Can't handle partially selected module %s!\n", mod);
|
||||
|
||||
std::string tempdir_name;
|
||||
if (cleanup)
|
||||
if (cleanup)
|
||||
tempdir_name = get_base_tmpdir() + "/";
|
||||
else
|
||||
tempdir_name = "_tmp_";
|
||||
|
|
@ -430,7 +430,7 @@ struct Abc9Pass : public ScriptPass
|
|||
abc9_exe_cmd += stringf(" -box %s", box_file);
|
||||
run_nocheck(abc9_exe_cmd);
|
||||
run_nocheck(stringf("read_aiger -xaiger -wideports -module_name %s$abc9 -map %s/input.sym %s/output.aig", mod, tempdir_name, tempdir_name));
|
||||
run_nocheck(stringf("abc9_ops -reintegrate %s", dff_mode ? "-dff" : ""));
|
||||
run_nocheck(stringf("abc_ops_reintegrate %s", dff_mode ? "-dff" : ""));
|
||||
}
|
||||
else
|
||||
log("Don't call ABC as there is nothing to map.\n");
|
||||
|
|
|
|||
|
|
@ -28,13 +28,6 @@
|
|||
USING_YOSYS_NAMESPACE
|
||||
PRIVATE_NAMESPACE_BEGIN
|
||||
|
||||
int map_autoidx;
|
||||
|
||||
inline std::string remap_name(RTLIL::IdString abc9_name)
|
||||
{
|
||||
return stringf("$abc$%d$%s", map_autoidx, abc9_name.c_str()+1);
|
||||
}
|
||||
|
||||
void check(RTLIL::Design *design, bool dff_mode)
|
||||
{
|
||||
dict<IdString,IdString> box_lookup;
|
||||
|
|
@ -1196,435 +1189,6 @@ void write_box(RTLIL::Module *module, const std::string &dst) {
|
|||
ofs.close();
|
||||
}
|
||||
|
||||
void reintegrate(RTLIL::Module *module, bool dff_mode)
|
||||
{
|
||||
auto design = module->design;
|
||||
log_assert(design);
|
||||
|
||||
map_autoidx = autoidx++;
|
||||
|
||||
RTLIL::Module *mapped_mod = design->module(stringf("%s$abc9", module->name));
|
||||
if (mapped_mod == NULL)
|
||||
log_error("ABC output file does not contain a module `%s$abc'.\n", module);
|
||||
|
||||
for (auto w : mapped_mod->wires()) {
|
||||
auto nw = module->addWire(remap_name(w->name), GetSize(w));
|
||||
nw->start_offset = w->start_offset;
|
||||
// Remove all (* init *) since they only exist on $_DFF_[NP]_
|
||||
w->attributes.erase(ID::init);
|
||||
}
|
||||
|
||||
dict<IdString,std::vector<IdString>> box_ports;
|
||||
|
||||
for (auto m : design->modules()) {
|
||||
if (!m->attributes.count(ID::abc9_box_id))
|
||||
continue;
|
||||
|
||||
auto r = box_ports.insert(m->name);
|
||||
if (!r.second)
|
||||
continue;
|
||||
|
||||
// Make carry in the last PI, and carry out the last PO
|
||||
// since ABC requires it this way
|
||||
IdString carry_in, carry_out;
|
||||
for (const auto &port_name : m->ports) {
|
||||
auto w = m->wire(port_name);
|
||||
log_assert(w);
|
||||
if (w->get_bool_attribute(ID::abc9_carry)) {
|
||||
log_assert(w->port_input != w->port_output);
|
||||
if (w->port_input)
|
||||
carry_in = port_name;
|
||||
else if (w->port_output)
|
||||
carry_out = port_name;
|
||||
}
|
||||
else
|
||||
r.first->second.push_back(port_name);
|
||||
}
|
||||
|
||||
if (carry_in != IdString()) {
|
||||
r.first->second.push_back(carry_in);
|
||||
r.first->second.push_back(carry_out);
|
||||
}
|
||||
}
|
||||
|
||||
SigMap initmap;
|
||||
if (dff_mode) {
|
||||
// Build a sigmap prioritising bits with (* init *)
|
||||
initmap.set(module);
|
||||
for (auto w : module->wires()) {
|
||||
auto it = w->attributes.find(ID::init);
|
||||
if (it == w->attributes.end())
|
||||
continue;
|
||||
for (auto i = 0; i < GetSize(w); i++)
|
||||
if (it->second[i] == State::S0 || it->second[i] == State::S1)
|
||||
initmap.add(w);
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<Cell*> boxes;
|
||||
for (auto cell : module->cells().to_vector()) {
|
||||
if (cell->has_keep_attr())
|
||||
continue;
|
||||
|
||||
// Short out (so that existing name can be preserved) and remove
|
||||
// $_DFF_[NP]_ cells since flop box already has all the information
|
||||
// we need to reconstruct them
|
||||
if (dff_mode && cell->type.in(ID($_DFF_N_), ID($_DFF_P_)) && !cell->get_bool_attribute(ID::abc9_keep)) {
|
||||
SigBit Q = cell->getPort(ID::Q);
|
||||
module->connect(Q, cell->getPort(ID::D));
|
||||
module->remove(cell);
|
||||
auto Qi = initmap(Q);
|
||||
auto it = Qi.wire->attributes.find(ID::init);
|
||||
if (it != Qi.wire->attributes.end())
|
||||
it->second.set(Qi.offset, State::Sx);
|
||||
}
|
||||
else if (cell->type.in(ID($_AND_), ID($_NOT_)))
|
||||
module->remove(cell);
|
||||
else if (cell->attributes.erase(ID::abc9_box_seq))
|
||||
boxes.emplace_back(cell);
|
||||
}
|
||||
|
||||
dict<SigBit, pool<IdString>> bit_drivers, bit_users;
|
||||
TopoSort<IdString, RTLIL::sort_by_id_str> toposort;
|
||||
dict<RTLIL::Cell*,RTLIL::Cell*> not2drivers;
|
||||
dict<SigBit, std::vector<RTLIL::Cell*>> bit2sinks;
|
||||
|
||||
std::map<IdString, int> cell_stats;
|
||||
for (auto mapped_cell : mapped_mod->cells())
|
||||
{
|
||||
// Short out $_FF_ cells since the flop box already has
|
||||
// all the information we need to reconstruct cell
|
||||
if (dff_mode && mapped_cell->type == ID($_FF_)) {
|
||||
SigBit D = mapped_cell->getPort(ID::D);
|
||||
SigBit Q = mapped_cell->getPort(ID::Q);
|
||||
if (D.wire)
|
||||
D.wire = module->wires_.at(remap_name(D.wire->name));
|
||||
Q.wire = module->wires_.at(remap_name(Q.wire->name));
|
||||
module->connect(Q, D);
|
||||
continue;
|
||||
}
|
||||
|
||||
// TODO: Speed up toposort -- we care about NOT ordering only
|
||||
toposort.node(mapped_cell->name);
|
||||
|
||||
if (mapped_cell->type == ID($_NOT_)) {
|
||||
RTLIL::SigBit a_bit = mapped_cell->getPort(ID::A);
|
||||
RTLIL::SigBit y_bit = mapped_cell->getPort(ID::Y);
|
||||
bit_users[a_bit].insert(mapped_cell->name);
|
||||
// Ignore inouts for topo ordering
|
||||
if (y_bit.wire && !(y_bit.wire->port_input && y_bit.wire->port_output))
|
||||
bit_drivers[y_bit].insert(mapped_cell->name);
|
||||
|
||||
if (!a_bit.wire) {
|
||||
mapped_cell->setPort(ID::Y, module->addWire(NEW_ID));
|
||||
RTLIL::Wire *wire = module->wire(remap_name(y_bit.wire->name));
|
||||
log_assert(wire);
|
||||
module->connect(RTLIL::SigBit(wire, y_bit.offset), State::S1);
|
||||
}
|
||||
else {
|
||||
RTLIL::Cell* driver_lut = nullptr;
|
||||
// ABC can return NOT gates that drive POs
|
||||
if (!a_bit.wire->port_input) {
|
||||
// If it's not a NOT gate that that comes from a PI directly,
|
||||
// find the driver LUT and clone that to guarantee that we won't
|
||||
// increase the max logic depth
|
||||
// (TODO: Optimise by not cloning unless will increase depth)
|
||||
RTLIL::IdString driver_name;
|
||||
if (GetSize(a_bit.wire) == 1)
|
||||
driver_name = stringf("$lut%s", a_bit.wire->name);
|
||||
else
|
||||
driver_name = stringf("$lut%s[%d]", a_bit.wire->name, a_bit.offset);
|
||||
driver_lut = mapped_mod->cell(driver_name);
|
||||
}
|
||||
|
||||
if (!driver_lut) {
|
||||
// If a driver couldn't be found (could be from PI or box CI)
|
||||
// then implement using a LUT
|
||||
RTLIL::Cell *cell = module->addLut(remap_name(stringf("$lut%s", mapped_cell->name)),
|
||||
RTLIL::SigBit(module->wires_.at(remap_name(a_bit.wire->name)), a_bit.offset),
|
||||
RTLIL::SigBit(module->wires_.at(remap_name(y_bit.wire->name)), y_bit.offset),
|
||||
RTLIL::Const::from_string("01"));
|
||||
bit2sinks[cell->getPort(ID::A)].push_back(cell);
|
||||
cell_stats[ID($lut)]++;
|
||||
}
|
||||
else
|
||||
not2drivers[mapped_cell] = driver_lut;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (mapped_cell->type == ID($lut)) {
|
||||
RTLIL::Cell *cell = module->addCell(remap_name(mapped_cell->name), mapped_cell->type);
|
||||
cell->parameters = mapped_cell->parameters;
|
||||
cell->attributes = mapped_cell->attributes;
|
||||
|
||||
for (auto &mapped_conn : mapped_cell->connections()) {
|
||||
RTLIL::SigSpec newsig;
|
||||
for (auto c : mapped_conn.second.chunks()) {
|
||||
if (c.width == 0)
|
||||
continue;
|
||||
//log_assert(c.width == 1);
|
||||
if (c.wire)
|
||||
c.wire = module->wires_.at(remap_name(c.wire->name));
|
||||
newsig.append(c);
|
||||
}
|
||||
cell->setPort(mapped_conn.first, newsig);
|
||||
|
||||
if (cell->input(mapped_conn.first)) {
|
||||
for (auto i : newsig)
|
||||
bit2sinks[i].push_back(cell);
|
||||
for (auto i : mapped_conn.second)
|
||||
bit_users[i].insert(mapped_cell->name);
|
||||
}
|
||||
if (cell->output(mapped_conn.first))
|
||||
for (auto i : mapped_conn.second)
|
||||
// Ignore inouts for topo ordering
|
||||
if (i.wire && !(i.wire->port_input && i.wire->port_output))
|
||||
bit_drivers[i].insert(mapped_cell->name);
|
||||
}
|
||||
}
|
||||
else {
|
||||
RTLIL::Cell *existing_cell = module->cell(mapped_cell->name);
|
||||
if (!existing_cell)
|
||||
log_error("Cannot find existing box cell with name '%s' in original design.\n", mapped_cell);
|
||||
|
||||
if (existing_cell->type.begins_with("$paramod$__ABC9_DELAY\\DELAY=")) {
|
||||
SigBit I = mapped_cell->getPort(ID(i));
|
||||
SigBit O = mapped_cell->getPort(ID(o));
|
||||
if (I.wire)
|
||||
I.wire = module->wires_.at(remap_name(I.wire->name));
|
||||
log_assert(O.wire);
|
||||
O.wire = module->wires_.at(remap_name(O.wire->name));
|
||||
module->connect(O, I);
|
||||
continue;
|
||||
}
|
||||
|
||||
RTLIL::Module* box_module = design->module(existing_cell->type);
|
||||
log_assert(existing_cell->parameters.empty());
|
||||
log_assert(mapped_cell->type == stringf("$__boxid%d", box_module->attributes.at(ID::abc9_box_id).as_int()));
|
||||
mapped_cell->type = existing_cell->type;
|
||||
|
||||
RTLIL::Cell *cell = module->addCell(remap_name(mapped_cell->name), mapped_cell->type);
|
||||
cell->parameters = existing_cell->parameters;
|
||||
cell->attributes = existing_cell->attributes;
|
||||
module->swap_names(cell, existing_cell);
|
||||
|
||||
auto jt = mapped_cell->connections_.find(ID(i));
|
||||
log_assert(jt != mapped_cell->connections_.end());
|
||||
SigSpec inputs = std::move(jt->second);
|
||||
mapped_cell->connections_.erase(jt);
|
||||
jt = mapped_cell->connections_.find(ID(o));
|
||||
log_assert(jt != mapped_cell->connections_.end());
|
||||
SigSpec outputs = std::move(jt->second);
|
||||
mapped_cell->connections_.erase(jt);
|
||||
|
||||
auto abc9_flop = box_module->get_bool_attribute(ID::abc9_flop);
|
||||
if (abc9_flop) {
|
||||
// Link this sole flop box output to the output of the existing
|
||||
// flop box, so that any (public) signal it drives will be
|
||||
// preserved
|
||||
SigBit old_q;
|
||||
for (const auto &port_name : box_ports.at(existing_cell->type)) {
|
||||
RTLIL::Wire *w = box_module->wire(port_name);
|
||||
log_assert(w);
|
||||
if (!w->port_output)
|
||||
continue;
|
||||
log_assert(old_q == SigBit());
|
||||
log_assert(GetSize(w) == 1);
|
||||
old_q = existing_cell->getPort(port_name);
|
||||
}
|
||||
auto new_q = outputs[0];
|
||||
new_q.wire = module->wires_.at(remap_name(new_q.wire->name));
|
||||
module->connect(old_q, new_q);
|
||||
}
|
||||
else {
|
||||
for (const auto &i : inputs)
|
||||
bit_users[i].insert(mapped_cell->name);
|
||||
for (const auto &i : outputs)
|
||||
// Ignore inouts for topo ordering
|
||||
if (i.wire && !(i.wire->port_input && i.wire->port_output))
|
||||
bit_drivers[i].insert(mapped_cell->name);
|
||||
}
|
||||
|
||||
int input_count = 0, output_count = 0;
|
||||
for (const auto &port_name : box_ports.at(existing_cell->type)) {
|
||||
RTLIL::Wire *w = box_module->wire(port_name);
|
||||
log_assert(w);
|
||||
|
||||
SigSpec sig;
|
||||
if (w->port_input) {
|
||||
sig = inputs.extract(input_count, GetSize(w));
|
||||
input_count += GetSize(w);
|
||||
}
|
||||
if (w->port_output) {
|
||||
sig = outputs.extract(output_count, GetSize(w));
|
||||
output_count += GetSize(w);
|
||||
}
|
||||
|
||||
SigSpec newsig;
|
||||
for (auto c : sig.chunks()) {
|
||||
if (c.width == 0)
|
||||
continue;
|
||||
//log_assert(c.width == 1);
|
||||
if (c.wire)
|
||||
c.wire = module->wires_.at(remap_name(c.wire->name));
|
||||
newsig.append(c);
|
||||
}
|
||||
|
||||
if (w->port_input && !abc9_flop)
|
||||
for (const auto &i : newsig)
|
||||
bit2sinks[i].push_back(cell);
|
||||
|
||||
cell->setPort(port_name, std::move(newsig));
|
||||
}
|
||||
}
|
||||
|
||||
cell_stats[mapped_cell->type]++;
|
||||
}
|
||||
|
||||
for (auto cell : boxes)
|
||||
module->remove(cell);
|
||||
|
||||
// Copy connections (and rename) from mapped_mod to module
|
||||
for (auto conn : mapped_mod->connections()) {
|
||||
if (!conn.first.is_fully_const()) {
|
||||
std::vector<RTLIL::SigChunk> chunks = conn.first.chunks();
|
||||
for (auto &c : chunks)
|
||||
c.wire = module->wires_.at(remap_name(c.wire->name));
|
||||
conn.first = std::move(chunks);
|
||||
}
|
||||
if (!conn.second.is_fully_const()) {
|
||||
std::vector<RTLIL::SigChunk> chunks = conn.second.chunks();
|
||||
for (auto &c : chunks)
|
||||
if (c.wire)
|
||||
c.wire = module->wires_.at(remap_name(c.wire->name));
|
||||
conn.second = std::move(chunks);
|
||||
}
|
||||
module->connect(conn);
|
||||
}
|
||||
|
||||
for (auto &it : cell_stats)
|
||||
log("ABC RESULTS: %15s cells: %8d\n", it.first, it.second);
|
||||
int in_wires = 0, out_wires = 0;
|
||||
|
||||
// Stitch in mapped_mod's inputs/outputs into module
|
||||
for (auto port : mapped_mod->ports) {
|
||||
RTLIL::Wire *mapped_wire = mapped_mod->wire(port);
|
||||
RTLIL::Wire *wire = module->wire(port);
|
||||
log_assert(wire);
|
||||
|
||||
RTLIL::Wire *remap_wire = module->wire(remap_name(port));
|
||||
RTLIL::SigSpec signal(wire, remap_wire->start_offset-wire->start_offset, GetSize(remap_wire));
|
||||
log_assert(GetSize(signal) >= GetSize(remap_wire));
|
||||
|
||||
RTLIL::SigSig conn;
|
||||
if (mapped_wire->port_output) {
|
||||
conn.first = signal;
|
||||
conn.second = remap_wire;
|
||||
out_wires++;
|
||||
module->connect(conn);
|
||||
}
|
||||
else if (mapped_wire->port_input) {
|
||||
conn.first = remap_wire;
|
||||
conn.second = signal;
|
||||
in_wires++;
|
||||
module->connect(conn);
|
||||
}
|
||||
}
|
||||
|
||||
// ABC9 will return $_NOT_ gates in its mapping (since they are
|
||||
// treated as being "free"), in particular driving primary
|
||||
// outputs (real primary outputs, or cells treated as blackboxes)
|
||||
// or driving box inputs.
|
||||
// Instead of just mapping those $_NOT_ gates into 1-input $lut-s
|
||||
// at an area and delay cost, see if it is possible to push
|
||||
// this $_NOT_ into the driving LUT, or into all sink LUTs.
|
||||
// When this is not possible, (i.e. this signal drives two primary
|
||||
// outputs, only one of which is complemented) and when the driver
|
||||
// is a LUT, then clone the LUT so that it can be inverted without
|
||||
// increasing depth/delay.
|
||||
for (auto &it : bit_users)
|
||||
if (bit_drivers.count(it.first))
|
||||
for (auto driver_cell : bit_drivers.at(it.first))
|
||||
for (auto user_cell : it.second)
|
||||
toposort.edge(driver_cell, user_cell);
|
||||
bool no_loops = toposort.sort();
|
||||
log_assert(no_loops);
|
||||
|
||||
for (auto ii = toposort.sorted.rbegin(); ii != toposort.sorted.rend(); ii++) {
|
||||
RTLIL::Cell *not_cell = mapped_mod->cell(*ii);
|
||||
log_assert(not_cell);
|
||||
if (not_cell->type != ID($_NOT_))
|
||||
continue;
|
||||
auto it = not2drivers.find(not_cell);
|
||||
if (it == not2drivers.end())
|
||||
continue;
|
||||
RTLIL::Cell *driver_lut = it->second;
|
||||
RTLIL::SigBit a_bit = not_cell->getPort(ID::A);
|
||||
RTLIL::SigBit y_bit = not_cell->getPort(ID::Y);
|
||||
RTLIL::Const driver_mask;
|
||||
|
||||
a_bit.wire = module->wires_.at(remap_name(a_bit.wire->name));
|
||||
y_bit.wire = module->wires_.at(remap_name(y_bit.wire->name));
|
||||
|
||||
auto jt = bit2sinks.find(a_bit);
|
||||
if (jt == bit2sinks.end())
|
||||
goto clone_lut;
|
||||
|
||||
for (auto sink_cell : jt->second)
|
||||
if (sink_cell->type != ID($lut))
|
||||
goto clone_lut;
|
||||
|
||||
// Push downstream LUTs past inverter
|
||||
for (auto sink_cell : jt->second) {
|
||||
SigSpec A = sink_cell->getPort(ID::A);
|
||||
RTLIL::Const mask = sink_cell->getParam(ID::LUT);
|
||||
int index = 0;
|
||||
for (; index < GetSize(A); index++)
|
||||
if (A[index] == a_bit)
|
||||
break;
|
||||
log_assert(index < GetSize(A));
|
||||
int i = 0;
|
||||
while (i < GetSize(mask)) {
|
||||
for (int j = 0; j < (1 << index); j++) {
|
||||
State bit = mask[i+j];
|
||||
mask.set(i+j, mask[i+j+(1 << index)]);
|
||||
mask.set(i+j+(1 << index), bit);
|
||||
}
|
||||
i += 1 << (index+1);
|
||||
}
|
||||
A[index] = y_bit;
|
||||
sink_cell->setPort(ID::A, A);
|
||||
sink_cell->setParam(ID::LUT, mask);
|
||||
}
|
||||
|
||||
// Since we have rewritten all sinks (which we know
|
||||
// to be only LUTs) to be after the inverter, we can
|
||||
// go ahead and clone the LUT with the expectation
|
||||
// that the original driving LUT will become dangling
|
||||
// and get cleaned away
|
||||
clone_lut:
|
||||
driver_mask = driver_lut->getParam(ID::LUT);
|
||||
for (auto b : driver_mask) {
|
||||
if (b == RTLIL::State::S0) b = RTLIL::State::S1;
|
||||
else if (b == RTLIL::State::S1) b = RTLIL::State::S0;
|
||||
}
|
||||
auto cell = module->addLut(NEW_ID,
|
||||
driver_lut->getPort(ID::A),
|
||||
y_bit,
|
||||
driver_mask);
|
||||
for (auto &bit : cell->connections_.at(ID::A)) {
|
||||
bit.wire = module->wires_.at(remap_name(bit.wire->name));
|
||||
bit2sinks[bit].push_back(cell);
|
||||
}
|
||||
}
|
||||
|
||||
log("ABC RESULTS: input signals: %8d\n", in_wires);
|
||||
log("ABC RESULTS: output signals: %8d\n", out_wires);
|
||||
|
||||
design->remove(mapped_mod);
|
||||
}
|
||||
|
||||
|
||||
static void replace_zbufs(Design *design)
|
||||
{
|
||||
|
|
@ -1770,11 +1334,6 @@ struct Abc9OpsPass : public Pass {
|
|||
log(" -write_box <dst>\n");
|
||||
log(" write the pre-computed box library to <dst>.\n");
|
||||
log("\n");
|
||||
log(" -reintegrate\n");
|
||||
log(" for each selected module, re-intergrate the module '<module-name>$abc9'\n");
|
||||
log(" by first recovering ABC9 boxes, and then stitching in the remaining\n");
|
||||
log(" primary inputs and outputs.\n");
|
||||
log("\n");
|
||||
}
|
||||
void execute(std::vector<std::string> args, RTLIL::Design *design) override
|
||||
{
|
||||
|
|
@ -1789,7 +1348,6 @@ struct Abc9OpsPass : public Pass {
|
|||
bool prep_xaiger_mode = false;
|
||||
bool prep_lut_mode = false;
|
||||
bool prep_box_mode = false;
|
||||
bool reintegrate_mode = false;
|
||||
bool replace_zbufs_mode = false;
|
||||
bool restore_zbufs_mode = false;
|
||||
bool dff_mode = false;
|
||||
|
|
@ -1869,11 +1427,6 @@ struct Abc9OpsPass : public Pass {
|
|||
valid = true;
|
||||
continue;
|
||||
}
|
||||
if (arg == "-reintegrate") {
|
||||
reintegrate_mode = true;
|
||||
valid = true;
|
||||
continue;
|
||||
}
|
||||
if (arg == "-dff") {
|
||||
dff_mode = true;
|
||||
continue;
|
||||
|
|
@ -1895,8 +1448,8 @@ struct Abc9OpsPass : public Pass {
|
|||
if (!valid)
|
||||
log_cmd_error("At least one of -check, -break_scc, -prep_{delays,xaiger,dff[123],lut,box}, -write_{lut,box}, -reintegrate, -{replace,restore}_zbufs must be specified.\n");
|
||||
|
||||
if (dff_mode && !check_mode && !prep_hier_mode && !prep_delays_mode && !prep_xaiger_mode && !reintegrate_mode)
|
||||
log_cmd_error("'-dff' option is only relevant for -prep_{hier,delay,xaiger} or -reintegrate.\n");
|
||||
if (dff_mode && !check_mode && !prep_hier_mode && !prep_delays_mode && !prep_xaiger_mode)
|
||||
log_cmd_error("'-dff' option is only relevant for -prep_{hier,delay,xaiger}.\n");
|
||||
|
||||
if (replace_zbufs_mode)
|
||||
replace_zbufs(design);
|
||||
|
|
@ -1938,8 +1491,6 @@ struct Abc9OpsPass : public Pass {
|
|||
break_scc(mod);
|
||||
if (prep_xaiger_mode)
|
||||
prep_xaiger(mod, dff_mode);
|
||||
if (reintegrate_mode)
|
||||
reintegrate(mod, dff_mode);
|
||||
}
|
||||
}
|
||||
} Abc9OpsPass;
|
||||
|
|
|
|||
|
|
@ -135,7 +135,7 @@ struct AbcNewPass : public ScriptPass {
|
|||
void script() override
|
||||
{
|
||||
if (check_label("check")) {
|
||||
run("abc9_ops -check");
|
||||
run("abc9_ops -check");
|
||||
}
|
||||
|
||||
if (check_label("prep_boxes")) {
|
||||
|
|
|
|||
509
passes/techmap/abc_ops_reintegrate.cc
Normal file
509
passes/techmap/abc_ops_reintegrate.cc
Normal file
|
|
@ -0,0 +1,509 @@
|
|||
/*
|
||||
* yosys -- Yosys Open SYnthesis Suite
|
||||
*
|
||||
* Copyright (C) 2012 Claire Xenia Wolf <claire@yosyshq.com>
|
||||
* 2019 Eddie Hung <eddie@fpgeh.com>
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* 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/utils.h"
|
||||
#include "kernel/newcelltypes.h"
|
||||
#include "kernel/timinginfo.h"
|
||||
|
||||
USING_YOSYS_NAMESPACE
|
||||
PRIVATE_NAMESPACE_BEGIN
|
||||
|
||||
int map_autoidx;
|
||||
|
||||
inline std::string remap_name(RTLIL::IdString abc9_name)
|
||||
{
|
||||
return stringf("$abc$%d$%s", map_autoidx, abc9_name.c_str()+1);
|
||||
}
|
||||
|
||||
void reintegrate(RTLIL::Module *module, bool dff_mode)
|
||||
{
|
||||
auto design = module->design;
|
||||
log_assert(design);
|
||||
|
||||
map_autoidx = autoidx++;
|
||||
|
||||
RTLIL::Module *mapped_mod = design->module(stringf("%s$abc9", module->name));
|
||||
if (mapped_mod == NULL)
|
||||
log_error("ABC output file does not contain a module `%s$abc'.\n", module);
|
||||
|
||||
for (auto w : mapped_mod->wires()) {
|
||||
auto nw = module->addWire(remap_name(w->name), GetSize(w));
|
||||
nw->start_offset = w->start_offset;
|
||||
// Remove all (* init *) since they only exist on $_DFF_[NP]_
|
||||
w->attributes.erase(ID::init);
|
||||
}
|
||||
|
||||
dict<IdString,std::vector<IdString>> box_ports;
|
||||
|
||||
for (auto m : design->modules()) {
|
||||
if (!m->attributes.count(ID::abc9_box_id))
|
||||
continue;
|
||||
|
||||
auto r = box_ports.insert(m->name);
|
||||
if (!r.second)
|
||||
continue;
|
||||
|
||||
// Make carry in the last PI, and carry out the last PO
|
||||
// since ABC requires it this way
|
||||
IdString carry_in, carry_out;
|
||||
for (const auto &port_name : m->ports) {
|
||||
auto w = m->wire(port_name);
|
||||
log_assert(w);
|
||||
if (w->get_bool_attribute(ID::abc9_carry)) {
|
||||
log_assert(w->port_input != w->port_output);
|
||||
if (w->port_input)
|
||||
carry_in = port_name;
|
||||
else if (w->port_output)
|
||||
carry_out = port_name;
|
||||
}
|
||||
else
|
||||
r.first->second.push_back(port_name);
|
||||
}
|
||||
|
||||
if (carry_in != IdString()) {
|
||||
r.first->second.push_back(carry_in);
|
||||
r.first->second.push_back(carry_out);
|
||||
}
|
||||
}
|
||||
|
||||
SigMap initmap;
|
||||
if (dff_mode) {
|
||||
// Build a sigmap prioritising bits with (* init *)
|
||||
initmap.set(module);
|
||||
for (auto w : module->wires()) {
|
||||
auto it = w->attributes.find(ID::init);
|
||||
if (it == w->attributes.end())
|
||||
continue;
|
||||
for (auto i = 0; i < GetSize(w); i++)
|
||||
if (it->second[i] == State::S0 || it->second[i] == State::S1)
|
||||
initmap.add(w);
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<Cell*> boxes;
|
||||
for (auto cell : module->cells().to_vector()) {
|
||||
if (cell->has_keep_attr())
|
||||
continue;
|
||||
|
||||
// Short out (so that existing name can be preserved) and remove
|
||||
// $_DFF_[NP]_ cells since flop box already has all the information
|
||||
// we need to reconstruct them
|
||||
if (dff_mode && cell->type.in(ID($_DFF_N_), ID($_DFF_P_)) && !cell->get_bool_attribute(ID::abc9_keep)) {
|
||||
SigBit Q = cell->getPort(ID::Q);
|
||||
module->connect(Q, cell->getPort(ID::D));
|
||||
module->remove(cell);
|
||||
auto Qi = initmap(Q);
|
||||
auto it = Qi.wire->attributes.find(ID::init);
|
||||
if (it != Qi.wire->attributes.end())
|
||||
it->second.set(Qi.offset, State::Sx);
|
||||
}
|
||||
else if (cell->type.in(ID($_AND_), ID($_NOT_)))
|
||||
module->remove(cell);
|
||||
else if (cell->attributes.erase(ID::abc9_box_seq))
|
||||
boxes.emplace_back(cell);
|
||||
}
|
||||
|
||||
dict<SigBit, pool<IdString>> bit_drivers, bit_users;
|
||||
TopoSort<IdString, RTLIL::sort_by_id_str> toposort;
|
||||
dict<RTLIL::Cell*,RTLIL::Cell*> not2drivers;
|
||||
dict<SigBit, std::vector<RTLIL::Cell*>> bit2sinks;
|
||||
|
||||
std::map<IdString, int> cell_stats;
|
||||
for (auto mapped_cell : mapped_mod->cells())
|
||||
{
|
||||
// Short out $_FF_ cells since the flop box already has
|
||||
// all the information we need to reconstruct cell
|
||||
if (dff_mode && mapped_cell->type == ID($_FF_)) {
|
||||
SigBit D = mapped_cell->getPort(ID::D);
|
||||
SigBit Q = mapped_cell->getPort(ID::Q);
|
||||
if (D.wire)
|
||||
D.wire = module->wires_.at(remap_name(D.wire->name));
|
||||
Q.wire = module->wires_.at(remap_name(Q.wire->name));
|
||||
module->connect(Q, D);
|
||||
continue;
|
||||
}
|
||||
|
||||
// TODO: Speed up toposort -- we care about NOT ordering only
|
||||
toposort.node(mapped_cell->name);
|
||||
|
||||
if (mapped_cell->type == ID($_NOT_)) {
|
||||
RTLIL::SigBit a_bit = mapped_cell->getPort(ID::A);
|
||||
RTLIL::SigBit y_bit = mapped_cell->getPort(ID::Y);
|
||||
bit_users[a_bit].insert(mapped_cell->name);
|
||||
// Ignore inouts for topo ordering
|
||||
if (y_bit.wire && !(y_bit.wire->port_input && y_bit.wire->port_output))
|
||||
bit_drivers[y_bit].insert(mapped_cell->name);
|
||||
|
||||
if (!a_bit.wire) {
|
||||
mapped_cell->setPort(ID::Y, module->addWire(NEW_ID));
|
||||
RTLIL::Wire *wire = module->wire(remap_name(y_bit.wire->name));
|
||||
log_assert(wire);
|
||||
module->connect(RTLIL::SigBit(wire, y_bit.offset), State::S1);
|
||||
}
|
||||
else {
|
||||
RTLIL::Cell* driver_lut = nullptr;
|
||||
// ABC can return NOT gates that drive POs
|
||||
if (!a_bit.wire->port_input) {
|
||||
// If it's not a NOT gate that that comes from a PI directly,
|
||||
// find the driver LUT and clone that to guarantee that we won't
|
||||
// increase the max logic depth
|
||||
// (TODO: Optimise by not cloning unless will increase depth)
|
||||
RTLIL::IdString driver_name;
|
||||
if (GetSize(a_bit.wire) == 1)
|
||||
driver_name = stringf("$lut%s", a_bit.wire->name);
|
||||
else
|
||||
driver_name = stringf("$lut%s[%d]", a_bit.wire->name, a_bit.offset);
|
||||
driver_lut = mapped_mod->cell(driver_name);
|
||||
}
|
||||
|
||||
if (!driver_lut) {
|
||||
// If a driver couldn't be found (could be from PI or box CI)
|
||||
// then implement using a LUT
|
||||
RTLIL::Cell *cell = module->addLut(remap_name(stringf("$lut%s", mapped_cell->name)),
|
||||
RTLIL::SigBit(module->wires_.at(remap_name(a_bit.wire->name)), a_bit.offset),
|
||||
RTLIL::SigBit(module->wires_.at(remap_name(y_bit.wire->name)), y_bit.offset),
|
||||
RTLIL::Const::from_string("01"));
|
||||
bit2sinks[cell->getPort(ID::A)].push_back(cell);
|
||||
cell_stats[ID($lut)]++;
|
||||
}
|
||||
else
|
||||
not2drivers[mapped_cell] = driver_lut;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (mapped_cell->type == ID($lut)) {
|
||||
RTLIL::Cell *cell = module->addCell(remap_name(mapped_cell->name), mapped_cell->type);
|
||||
cell->parameters = mapped_cell->parameters;
|
||||
cell->attributes = mapped_cell->attributes;
|
||||
|
||||
for (auto &mapped_conn : mapped_cell->connections()) {
|
||||
RTLIL::SigSpec newsig;
|
||||
for (auto c : mapped_conn.second.chunks()) {
|
||||
if (c.width == 0)
|
||||
continue;
|
||||
//log_assert(c.width == 1);
|
||||
if (c.wire)
|
||||
c.wire = module->wires_.at(remap_name(c.wire->name));
|
||||
newsig.append(c);
|
||||
}
|
||||
cell->setPort(mapped_conn.first, newsig);
|
||||
|
||||
if (cell->input(mapped_conn.first)) {
|
||||
for (auto i : newsig)
|
||||
bit2sinks[i].push_back(cell);
|
||||
for (auto i : mapped_conn.second)
|
||||
bit_users[i].insert(mapped_cell->name);
|
||||
}
|
||||
if (cell->output(mapped_conn.first))
|
||||
for (auto i : mapped_conn.second)
|
||||
// Ignore inouts for topo ordering
|
||||
if (i.wire && !(i.wire->port_input && i.wire->port_output))
|
||||
bit_drivers[i].insert(mapped_cell->name);
|
||||
}
|
||||
}
|
||||
else {
|
||||
RTLIL::Cell *existing_cell = module->cell(mapped_cell->name);
|
||||
if (!existing_cell)
|
||||
log_error("Cannot find existing box cell with name '%s' in original design.\n", mapped_cell);
|
||||
|
||||
if (existing_cell->type.begins_with("$paramod$__ABC9_DELAY\\DELAY=")) {
|
||||
SigBit I = mapped_cell->getPort(ID(i));
|
||||
SigBit O = mapped_cell->getPort(ID(o));
|
||||
if (I.wire)
|
||||
I.wire = module->wires_.at(remap_name(I.wire->name));
|
||||
log_assert(O.wire);
|
||||
O.wire = module->wires_.at(remap_name(O.wire->name));
|
||||
module->connect(O, I);
|
||||
continue;
|
||||
}
|
||||
|
||||
RTLIL::Module* box_module = design->module(existing_cell->type);
|
||||
log_assert(existing_cell->parameters.empty());
|
||||
log_assert(mapped_cell->type == stringf("$__boxid%d", box_module->attributes.at(ID::abc9_box_id).as_int()));
|
||||
mapped_cell->type = existing_cell->type;
|
||||
|
||||
RTLIL::Cell *cell = module->addCell(remap_name(mapped_cell->name), mapped_cell->type);
|
||||
cell->parameters = existing_cell->parameters;
|
||||
cell->attributes = existing_cell->attributes;
|
||||
module->swap_names(cell, existing_cell);
|
||||
|
||||
auto jt = mapped_cell->connections_.find(ID(i));
|
||||
log_assert(jt != mapped_cell->connections_.end());
|
||||
SigSpec inputs = std::move(jt->second);
|
||||
mapped_cell->connections_.erase(jt);
|
||||
jt = mapped_cell->connections_.find(ID(o));
|
||||
log_assert(jt != mapped_cell->connections_.end());
|
||||
SigSpec outputs = std::move(jt->second);
|
||||
mapped_cell->connections_.erase(jt);
|
||||
|
||||
auto abc9_flop = box_module->get_bool_attribute(ID::abc9_flop);
|
||||
if (abc9_flop) {
|
||||
// Link this sole flop box output to the output of the existing
|
||||
// flop box, so that any (public) signal it drives will be
|
||||
// preserved
|
||||
SigBit old_q;
|
||||
for (const auto &port_name : box_ports.at(existing_cell->type)) {
|
||||
RTLIL::Wire *w = box_module->wire(port_name);
|
||||
log_assert(w);
|
||||
if (!w->port_output)
|
||||
continue;
|
||||
log_assert(old_q == SigBit());
|
||||
log_assert(GetSize(w) == 1);
|
||||
old_q = existing_cell->getPort(port_name);
|
||||
}
|
||||
auto new_q = outputs[0];
|
||||
new_q.wire = module->wires_.at(remap_name(new_q.wire->name));
|
||||
module->connect(old_q, new_q);
|
||||
}
|
||||
else {
|
||||
for (const auto &i : inputs)
|
||||
bit_users[i].insert(mapped_cell->name);
|
||||
for (const auto &i : outputs)
|
||||
// Ignore inouts for topo ordering
|
||||
if (i.wire && !(i.wire->port_input && i.wire->port_output))
|
||||
bit_drivers[i].insert(mapped_cell->name);
|
||||
}
|
||||
|
||||
int input_count = 0, output_count = 0;
|
||||
for (const auto &port_name : box_ports.at(existing_cell->type)) {
|
||||
RTLIL::Wire *w = box_module->wire(port_name);
|
||||
log_assert(w);
|
||||
|
||||
SigSpec sig;
|
||||
if (w->port_input) {
|
||||
sig = inputs.extract(input_count, GetSize(w));
|
||||
input_count += GetSize(w);
|
||||
}
|
||||
if (w->port_output) {
|
||||
sig = outputs.extract(output_count, GetSize(w));
|
||||
output_count += GetSize(w);
|
||||
}
|
||||
|
||||
SigSpec newsig;
|
||||
for (auto c : sig.chunks()) {
|
||||
if (c.width == 0)
|
||||
continue;
|
||||
//log_assert(c.width == 1);
|
||||
if (c.wire)
|
||||
c.wire = module->wires_.at(remap_name(c.wire->name));
|
||||
newsig.append(c);
|
||||
}
|
||||
|
||||
if (w->port_input && !abc9_flop)
|
||||
for (const auto &i : newsig)
|
||||
bit2sinks[i].push_back(cell);
|
||||
|
||||
cell->setPort(port_name, std::move(newsig));
|
||||
}
|
||||
}
|
||||
|
||||
cell_stats[mapped_cell->type]++;
|
||||
}
|
||||
|
||||
for (auto cell : boxes)
|
||||
module->remove(cell);
|
||||
|
||||
// Copy connections (and rename) from mapped_mod to module
|
||||
for (auto conn : mapped_mod->connections()) {
|
||||
if (!conn.first.is_fully_const()) {
|
||||
std::vector<RTLIL::SigChunk> chunks = conn.first.chunks();
|
||||
for (auto &c : chunks)
|
||||
c.wire = module->wires_.at(remap_name(c.wire->name));
|
||||
conn.first = std::move(chunks);
|
||||
}
|
||||
if (!conn.second.is_fully_const()) {
|
||||
std::vector<RTLIL::SigChunk> chunks = conn.second.chunks();
|
||||
for (auto &c : chunks)
|
||||
if (c.wire)
|
||||
c.wire = module->wires_.at(remap_name(c.wire->name));
|
||||
conn.second = std::move(chunks);
|
||||
}
|
||||
module->connect(conn);
|
||||
}
|
||||
|
||||
for (auto &it : cell_stats)
|
||||
log("ABC RESULTS: %15s cells: %8d\n", it.first, it.second);
|
||||
int in_wires = 0, out_wires = 0;
|
||||
|
||||
// Stitch in mapped_mod's inputs/outputs into module
|
||||
for (auto port : mapped_mod->ports) {
|
||||
RTLIL::Wire *mapped_wire = mapped_mod->wire(port);
|
||||
RTLIL::Wire *wire = module->wire(port);
|
||||
log_assert(wire);
|
||||
|
||||
RTLIL::Wire *remap_wire = module->wire(remap_name(port));
|
||||
RTLIL::SigSpec signal(wire, remap_wire->start_offset-wire->start_offset, GetSize(remap_wire));
|
||||
log_assert(GetSize(signal) >= GetSize(remap_wire));
|
||||
|
||||
RTLIL::SigSig conn;
|
||||
if (mapped_wire->port_output) {
|
||||
conn.first = signal;
|
||||
conn.second = remap_wire;
|
||||
out_wires++;
|
||||
module->connect(conn);
|
||||
}
|
||||
else if (mapped_wire->port_input) {
|
||||
conn.first = remap_wire;
|
||||
conn.second = signal;
|
||||
in_wires++;
|
||||
module->connect(conn);
|
||||
}
|
||||
}
|
||||
|
||||
// ABC9 will return $_NOT_ gates in its mapping (since they are
|
||||
// treated as being "free"), in particular driving primary
|
||||
// outputs (real primary outputs, or cells treated as blackboxes)
|
||||
// or driving box inputs.
|
||||
// Instead of just mapping those $_NOT_ gates into 1-input $lut-s
|
||||
// at an area and delay cost, see if it is possible to push
|
||||
// this $_NOT_ into the driving LUT, or into all sink LUTs.
|
||||
// When this is not possible, (i.e. this signal drives two primary
|
||||
// outputs, only one of which is complemented) and when the driver
|
||||
// is a LUT, then clone the LUT so that it can be inverted without
|
||||
// increasing depth/delay.
|
||||
for (auto &it : bit_users)
|
||||
if (bit_drivers.count(it.first))
|
||||
for (auto driver_cell : bit_drivers.at(it.first))
|
||||
for (auto user_cell : it.second)
|
||||
toposort.edge(driver_cell, user_cell);
|
||||
bool no_loops = toposort.sort();
|
||||
log_assert(no_loops);
|
||||
|
||||
for (auto ii = toposort.sorted.rbegin(); ii != toposort.sorted.rend(); ii++) {
|
||||
RTLIL::Cell *not_cell = mapped_mod->cell(*ii);
|
||||
log_assert(not_cell);
|
||||
if (not_cell->type != ID($_NOT_))
|
||||
continue;
|
||||
auto it = not2drivers.find(not_cell);
|
||||
if (it == not2drivers.end())
|
||||
continue;
|
||||
RTLIL::Cell *driver_lut = it->second;
|
||||
RTLIL::SigBit a_bit = not_cell->getPort(ID::A);
|
||||
RTLIL::SigBit y_bit = not_cell->getPort(ID::Y);
|
||||
RTLIL::Const driver_mask;
|
||||
|
||||
a_bit.wire = module->wires_.at(remap_name(a_bit.wire->name));
|
||||
y_bit.wire = module->wires_.at(remap_name(y_bit.wire->name));
|
||||
|
||||
auto jt = bit2sinks.find(a_bit);
|
||||
if (jt == bit2sinks.end())
|
||||
goto clone_lut;
|
||||
|
||||
for (auto sink_cell : jt->second)
|
||||
if (sink_cell->type != ID($lut))
|
||||
goto clone_lut;
|
||||
|
||||
// Push downstream LUTs past inverter
|
||||
for (auto sink_cell : jt->second) {
|
||||
SigSpec A = sink_cell->getPort(ID::A);
|
||||
RTLIL::Const mask = sink_cell->getParam(ID::LUT);
|
||||
int index = 0;
|
||||
for (; index < GetSize(A); index++)
|
||||
if (A[index] == a_bit)
|
||||
break;
|
||||
log_assert(index < GetSize(A));
|
||||
int i = 0;
|
||||
while (i < GetSize(mask)) {
|
||||
for (int j = 0; j < (1 << index); j++) {
|
||||
State bit = mask[i+j];
|
||||
mask.set(i+j, mask[i+j+(1 << index)]);
|
||||
mask.set(i+j+(1 << index), bit);
|
||||
}
|
||||
i += 1 << (index+1);
|
||||
}
|
||||
A[index] = y_bit;
|
||||
sink_cell->setPort(ID::A, A);
|
||||
sink_cell->setParam(ID::LUT, mask);
|
||||
}
|
||||
|
||||
// Since we have rewritten all sinks (which we know
|
||||
// to be only LUTs) to be after the inverter, we can
|
||||
// go ahead and clone the LUT with the expectation
|
||||
// that the original driving LUT will become dangling
|
||||
// and get cleaned away
|
||||
clone_lut:
|
||||
driver_mask = driver_lut->getParam(ID::LUT);
|
||||
for (auto b : driver_mask) {
|
||||
if (b == RTLIL::State::S0) b = RTLIL::State::S1;
|
||||
else if (b == RTLIL::State::S1) b = RTLIL::State::S0;
|
||||
}
|
||||
auto cell = module->addLut(NEW_ID,
|
||||
driver_lut->getPort(ID::A),
|
||||
y_bit,
|
||||
driver_mask);
|
||||
for (auto &bit : cell->connections_.at(ID::A)) {
|
||||
bit.wire = module->wires_.at(remap_name(bit.wire->name));
|
||||
bit2sinks[bit].push_back(cell);
|
||||
}
|
||||
}
|
||||
|
||||
log("ABC RESULTS: input signals: %8d\n", in_wires);
|
||||
log("ABC RESULTS: output signals: %8d\n", out_wires);
|
||||
|
||||
design->remove(mapped_mod);
|
||||
}
|
||||
|
||||
struct AbcOpsReintegratePass : public Pass {
|
||||
AbcOpsReintegratePass() : Pass("abc_ops_reintegrate", "reintegrate ABC mapped design into module") { }
|
||||
void help() override
|
||||
{
|
||||
// |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
|
||||
log("\n");
|
||||
log(" abc_ops_reintegrate [options] [selection]\n");
|
||||
log("\n");
|
||||
log("For each selected module, re-integrate the module '<module-name>$abc9'\n");
|
||||
log("by first recovering ABC9 boxes, and then stitching in the remaining\n");
|
||||
log("primary inputs and outputs.\n");
|
||||
log("\n");
|
||||
}
|
||||
void execute(std::vector<std::string> args, RTLIL::Design *design) override
|
||||
{
|
||||
log_header(design, "Executing ABC_OPS_REINTEGRATE pass (reintegrate ABC mapped design into module).\n");
|
||||
|
||||
bool dff_mode = false;
|
||||
|
||||
size_t argidx;
|
||||
for (argidx = 1; argidx < args.size(); argidx++) {
|
||||
std::string arg = args[argidx];
|
||||
if (arg == "-dff") {
|
||||
dff_mode = true;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
extra_args(args, argidx, design);
|
||||
|
||||
for (auto mod : design->selected_modules()) {
|
||||
if (mod->processes.size() > 0) {
|
||||
log("Skipping module %s as it contains processes.\n", mod);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!design->selected_whole_module(mod))
|
||||
log_error("Can't handle partially selected module %s!\n", mod);
|
||||
|
||||
reintegrate(mod, dff_mode);
|
||||
}
|
||||
}
|
||||
} AbcOpsReintegratePass;
|
||||
|
||||
PRIVATE_NAMESPACE_END
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
/**
|
||||
* Replaces chains of $add/$sub and $macc cells with carry-save adder trees
|
||||
* Replaces chains of $add/$sub/$alu and $macc cells with carry-save compression trees
|
||||
*
|
||||
* Terminology:
|
||||
* - parent: Cells that consume another cell's output
|
||||
|
|
@ -7,9 +7,9 @@
|
|||
* - chain: Connected path of chainable cells
|
||||
*/
|
||||
|
||||
#include "kernel/compressor_tree.h"
|
||||
#include "kernel/macc.h"
|
||||
#include "kernel/sigtools.h"
|
||||
#include "kernel/wallace_tree.h"
|
||||
#include "kernel/yosys.h"
|
||||
|
||||
#include <queue>
|
||||
|
|
@ -17,49 +17,57 @@
|
|||
USING_YOSYS_NAMESPACE
|
||||
PRIVATE_NAMESPACE_BEGIN
|
||||
|
||||
struct Operand {
|
||||
SigSpec sig;
|
||||
bool is_signed;
|
||||
bool negate;
|
||||
struct ArithTreeOptions {
|
||||
CompressorTree::Strategy strategy = CompressorTree::Strategy::PREFER_42;
|
||||
CompressorTree::FinalMode final_mode = CompressorTree::FinalMode::RIPPLE;
|
||||
bool fma_fusion = true;
|
||||
};
|
||||
|
||||
struct Traversal {
|
||||
struct ArithTreeWorker {
|
||||
const ArithTreeOptions &opt;
|
||||
Module *module;
|
||||
SigMap sigmap;
|
||||
|
||||
dict<SigBit, pool<Cell *>> bit_consumers;
|
||||
dict<SigBit, int> fanout;
|
||||
Traversal(Module *module) : sigmap(module)
|
||||
{
|
||||
for (auto cell : module->cells())
|
||||
for (auto &conn : cell->connections())
|
||||
if (cell->input(conn.first))
|
||||
for (auto bit : sigmap(conn.second))
|
||||
bit_consumers[bit].insert(cell);
|
||||
|
||||
for (auto &pair : bit_consumers)
|
||||
fanout[pair.first] = pair.second.size();
|
||||
pool<Cell *> addsub;
|
||||
pool<Cell *> alu;
|
||||
pool<Cell *> macc;
|
||||
|
||||
struct Operand {
|
||||
SigSpec sig;
|
||||
bool is_signed;
|
||||
bool negate;
|
||||
// With FMA, when both factors are set, the operand represents a product to
|
||||
// be expanded into partial products at extraction time, is_signed then
|
||||
// applies to factor_a, and factor_b carries its own signedness
|
||||
SigSpec factor_b; // empty for regular operands
|
||||
bool factor_b_signed = false;
|
||||
};
|
||||
|
||||
ArithTreeWorker(const ArithTreeOptions &opt, Module *module) : opt(opt), module(module), sigmap(module)
|
||||
{
|
||||
// Build traversal data
|
||||
for (auto cell : module->cells()) {
|
||||
for (auto &[name, sig] : cell->connections()) {
|
||||
if (cell->input(name)) {
|
||||
for (auto bit : sigmap(sig)) {
|
||||
bit_consumers[bit].insert(cell);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (auto &[sig, consumers] : bit_consumers)
|
||||
fanout[sig] = consumers.size();
|
||||
|
||||
for (auto wire : module->wires())
|
||||
if (wire->port_output)
|
||||
for (auto bit : sigmap(SigSpec(wire)))
|
||||
fanout[bit]++;
|
||||
}
|
||||
};
|
||||
|
||||
struct Cells {
|
||||
pool<Cell *> addsub;
|
||||
pool<Cell *> alu;
|
||||
pool<Cell *> macc;
|
||||
|
||||
static bool is_addsub(Cell *cell) { return cell->type == ID($add) || cell->type == ID($sub); }
|
||||
|
||||
static bool is_alu(Cell *cell) { return cell->type == ID($alu); }
|
||||
|
||||
static bool is_macc(Cell *cell) { return cell->type == ID($macc) || cell->type == ID($macc_v2); }
|
||||
|
||||
bool empty() { return addsub.empty() && alu.empty() && macc.empty(); }
|
||||
|
||||
Cells(Module *module)
|
||||
{
|
||||
// Collect cell data
|
||||
for (auto cell : module->cells()) {
|
||||
if (is_addsub(cell))
|
||||
addsub.insert(cell);
|
||||
|
|
@ -69,59 +77,55 @@ struct Cells {
|
|||
macc.insert(cell);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
struct AluInfo {
|
||||
Cells &cells;
|
||||
Traversal &traversal;
|
||||
bool is_subtract(Cell *cell)
|
||||
{
|
||||
SigSpec bi = traversal.sigmap(cell->getPort(ID::BI));
|
||||
SigSpec ci = traversal.sigmap(cell->getPort(ID::CI));
|
||||
bool is_addsub(Cell *cell) {
|
||||
return cell->type == ID($add) || cell->type == ID($sub);
|
||||
}
|
||||
|
||||
bool is_alu(Cell *cell) {
|
||||
return cell->type == ID($alu);
|
||||
}
|
||||
|
||||
bool is_macc(Cell *cell) {
|
||||
return cell->type == ID($macc) || cell->type == ID($macc_v2);
|
||||
}
|
||||
|
||||
bool is_sub(Cell *cell) {
|
||||
SigSpec bi = sigmap(cell->getPort(ID::BI));
|
||||
SigSpec ci = sigmap(cell->getPort(ID::CI));
|
||||
return GetSize(bi) == 1 && bi[0] == State::S1 && GetSize(ci) == 1 && ci[0] == State::S1;
|
||||
}
|
||||
|
||||
bool is_add(Cell *cell)
|
||||
{
|
||||
SigSpec bi = traversal.sigmap(cell->getPort(ID::BI));
|
||||
SigSpec ci = traversal.sigmap(cell->getPort(ID::CI));
|
||||
SigSpec bi = sigmap(cell->getPort(ID::BI));
|
||||
SigSpec ci = sigmap(cell->getPort(ID::CI));
|
||||
return GetSize(bi) == 1 && bi[0] == State::S0 && GetSize(ci) == 1 && ci[0] == State::S0;
|
||||
}
|
||||
|
||||
bool is_chainable(Cell *cell)
|
||||
{
|
||||
if (!(is_add(cell) || is_subtract(cell)))
|
||||
if (!(is_add(cell) || is_sub(cell)))
|
||||
return false;
|
||||
|
||||
for (auto bit : traversal.sigmap(cell->getPort(ID::X)))
|
||||
if (traversal.fanout.count(bit) && traversal.fanout[bit] > 0)
|
||||
for (auto bit : sigmap(cell->getPort(ID::X)))
|
||||
if (fanout.count(bit) && fanout[bit] > 0)
|
||||
return false;
|
||||
for (auto bit : traversal.sigmap(cell->getPort(ID::CO)))
|
||||
if (traversal.fanout.count(bit) && traversal.fanout[bit] > 0)
|
||||
for (auto bit : sigmap(cell->getPort(ID::CO)))
|
||||
if (fanout.count(bit) && fanout[bit] > 0)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
struct Rewriter {
|
||||
Module *module;
|
||||
Cells &cells;
|
||||
Traversal traversal;
|
||||
AluInfo alu_info;
|
||||
|
||||
Rewriter(Module *module, Cells &cells) : module(module), cells(cells), traversal(module), alu_info{cells, traversal} {}
|
||||
|
||||
Cell *sole_chainable_consumer(SigSpec sig, const pool<Cell *> &candidates)
|
||||
{
|
||||
Cell *consumer = nullptr;
|
||||
for (auto bit : sig) {
|
||||
if (!traversal.fanout.count(bit) || traversal.fanout[bit] != 1)
|
||||
if (!fanout.count(bit) || fanout[bit] != 1)
|
||||
return nullptr;
|
||||
if (!traversal.bit_consumers.count(bit) || traversal.bit_consumers[bit].size() != 1)
|
||||
if (!bit_consumers.count(bit) || bit_consumers[bit].size() != 1)
|
||||
return nullptr;
|
||||
|
||||
Cell *c = *traversal.bit_consumers[bit].begin();
|
||||
Cell *c = *bit_consumers[bit].begin();
|
||||
if (!candidates.count(c))
|
||||
return nullptr;
|
||||
|
||||
|
|
@ -137,7 +141,7 @@ struct Rewriter {
|
|||
{
|
||||
dict<Cell *, Cell *> parent_of;
|
||||
for (auto cell : candidates) {
|
||||
Cell *consumer = sole_chainable_consumer(traversal.sigmap(cell->getPort(ID::Y)), candidates);
|
||||
Cell *consumer = sole_chainable_consumer(sigmap(cell->getPort(ID::Y)), candidates);
|
||||
if (consumer && consumer != cell)
|
||||
parent_of[cell] = consumer;
|
||||
}
|
||||
|
|
@ -177,12 +181,12 @@ struct Rewriter {
|
|||
{
|
||||
pool<SigBit> bits;
|
||||
for (auto cell : chain)
|
||||
for (auto bit : traversal.sigmap(cell->getPort(ID::Y)))
|
||||
for (auto bit : sigmap(cell->getPort(ID::Y)))
|
||||
bits.insert(bit);
|
||||
return bits;
|
||||
}
|
||||
|
||||
static bool overlaps(SigSpec sig, const pool<SigBit> &bits)
|
||||
bool overlaps(SigSpec sig, const pool<SigBit> &bits)
|
||||
{
|
||||
for (auto bit : sig)
|
||||
if (bits.count(bit))
|
||||
|
|
@ -195,17 +199,16 @@ struct Rewriter {
|
|||
bool parent_subtracts;
|
||||
if (parent->type == ID($sub))
|
||||
parent_subtracts = true;
|
||||
else if (cells.is_alu(parent))
|
||||
parent_subtracts = alu_info.is_subtract(parent);
|
||||
else if (is_alu(parent))
|
||||
parent_subtracts = is_sub(parent);
|
||||
else
|
||||
return false;
|
||||
|
||||
if (!parent_subtracts)
|
||||
return false;
|
||||
|
||||
// Check if any bit of child's Y connects to parent's B
|
||||
SigSpec child_y = traversal.sigmap(child->getPort(ID::Y));
|
||||
SigSpec parent_b = traversal.sigmap(parent->getPort(ID::B));
|
||||
SigSpec child_y = sigmap(child->getPort(ID::Y));
|
||||
SigSpec parent_b = sigmap(parent->getPort(ID::B));
|
||||
for (auto bit : child_y)
|
||||
for (auto pbit : parent_b)
|
||||
if (bit == pbit)
|
||||
|
|
@ -244,21 +247,20 @@ struct Rewriter {
|
|||
for (auto cell : chain) {
|
||||
bool cell_neg = negated.count(cell) ? negated[cell] : false;
|
||||
|
||||
SigSpec a = traversal.sigmap(cell->getPort(ID::A));
|
||||
SigSpec b = traversal.sigmap(cell->getPort(ID::B));
|
||||
SigSpec a = sigmap(cell->getPort(ID::A));
|
||||
SigSpec b = sigmap(cell->getPort(ID::B));
|
||||
bool a_signed = cell->getParam(ID::A_SIGNED).as_bool();
|
||||
bool b_signed = cell->getParam(ID::B_SIGNED).as_bool();
|
||||
bool b_sub = (cell->type == ID($sub)) || (cells.is_alu(cell) && alu_info.is_subtract(cell));
|
||||
bool b_sub = (cell->type == ID($sub)) || (is_alu(cell) && is_sub(cell));
|
||||
|
||||
// Only add operands not produced by other chain cells
|
||||
if (!overlaps(a, chain_bits)) {
|
||||
operands.push_back({a, a_signed, cell_neg});
|
||||
operands.push_back({a, a_signed, cell_neg, SigSpec(), false});
|
||||
if (cell_neg)
|
||||
neg_compensation++;
|
||||
}
|
||||
if (!overlaps(b, chain_bits)) {
|
||||
bool neg = cell_neg ^ b_sub;
|
||||
operands.push_back({b, b_signed, neg});
|
||||
operands.push_back({b, b_signed, neg, SigSpec(), false});
|
||||
if (neg)
|
||||
neg_compensation++;
|
||||
}
|
||||
|
|
@ -272,63 +274,83 @@ struct Rewriter {
|
|||
neg_compensation = 0;
|
||||
|
||||
for (auto &term : macc.terms) {
|
||||
// Bail on multiplication
|
||||
if (GetSize(term.in_b) != 0)
|
||||
return false;
|
||||
operands.push_back({term.in_a, term.is_signed, term.do_subtract});
|
||||
if (GetSize(term.in_b) != 0) {
|
||||
if (!opt.fma_fusion)
|
||||
return false;
|
||||
|
||||
// Preserve term as a multiplicative operand which is expanded into partial products
|
||||
Operand op;
|
||||
op.sig = term.in_a;
|
||||
op.is_signed = term.is_signed;
|
||||
op.negate = term.do_subtract;
|
||||
op.factor_b = term.in_b;
|
||||
op.factor_b_signed = term.is_signed;
|
||||
operands.push_back(op);
|
||||
continue;
|
||||
}
|
||||
operands.push_back({term.in_a, term.is_signed, term.do_subtract, SigSpec(), false});
|
||||
if (term.do_subtract)
|
||||
neg_compensation++;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
SigSpec extend_operand(SigSpec sig, bool is_signed, int width)
|
||||
std::vector<CompressorTree::DepthSig> build_operand_pool(std::vector<Operand> &operands, int width, int &neg_compensation)
|
||||
{
|
||||
if (GetSize(sig) < width) {
|
||||
SigBit pad;
|
||||
if (is_signed && GetSize(sig) > 0)
|
||||
pad = sig[GetSize(sig) - 1];
|
||||
else
|
||||
pad = State::S0;
|
||||
sig.append(SigSpec(pad, width - GetSize(sig)));
|
||||
}
|
||||
if (GetSize(sig) > width)
|
||||
sig = sig.extract(0, width);
|
||||
return sig;
|
||||
}
|
||||
|
||||
void replace_with_carry_save_tree(std::vector<Operand> &operands, SigSpec result_y, int neg_compensation, const char *desc)
|
||||
{
|
||||
int width = GetSize(result_y);
|
||||
std::vector<SigSpec> extended;
|
||||
extended.reserve(operands.size() + 1);
|
||||
// Expand operands into a flat list of signals for reduction
|
||||
std::vector<CompressorTree::DepthSig> pool;
|
||||
pool.reserve(operands.size() * 2);
|
||||
|
||||
for (auto &op : operands) {
|
||||
SigSpec s = extend_operand(op.sig, op.is_signed, width);
|
||||
if (op.negate)
|
||||
s = module->Not(NEW_ID, s);
|
||||
extended.push_back(s);
|
||||
if (GetSize(op.factor_b) == 0) {
|
||||
// Additive operand
|
||||
op.sig.extend_u0(width, op.is_signed);
|
||||
if (op.negate)
|
||||
op.sig = module->Not(NEW_ID, op.sig);
|
||||
pool.push_back({op.sig, 0});
|
||||
} else {
|
||||
// Multiplicative operand
|
||||
auto pps = CompressorTree::generate_partial_products(module, op.sig, op.factor_b, op.is_signed, op.factor_b_signed, width);
|
||||
|
||||
if (!op.negate) {
|
||||
for (auto &pp : pps)
|
||||
pool.push_back(pp);
|
||||
continue;
|
||||
}
|
||||
|
||||
auto [pa, pb] = CompressorTree::reduce_scheduled(module, pps, width, opt.strategy);
|
||||
SigSpec p = module->addWire(NEW_ID, width);
|
||||
module->addAdd(NEW_ID, pa, pb, p, false);
|
||||
SigSpec np = module->addWire(NEW_ID, width);
|
||||
module->addNot(NEW_ID, p, np);
|
||||
pool.push_back({np, 0});
|
||||
neg_compensation++;
|
||||
}
|
||||
}
|
||||
|
||||
// Add correction for negated operands (-x = ~x + 1 so 1 per negation)
|
||||
if (neg_compensation > 0)
|
||||
extended.push_back(SigSpec(neg_compensation, width));
|
||||
pool.push_back({SigSpec(neg_compensation, width), 0});
|
||||
|
||||
int compressor_count;
|
||||
auto [a, b] = wallace_reduce_scheduled(module, extended, width, &compressor_count);
|
||||
log(" %s -> %d $fa + 1 $add (%d operands, module %s)\n", desc, compressor_count, (int)operands.size(), module);
|
||||
return pool;
|
||||
}
|
||||
|
||||
// Emit final add
|
||||
module->addAdd(NEW_ID, a, b, result_y, false);
|
||||
void emit_tree(std::vector<Operand> &operands, SigSpec result_y, int neg_compensation)
|
||||
{
|
||||
int width = GetSize(result_y);
|
||||
auto pool = build_operand_pool(operands, width, neg_compensation);
|
||||
int final_depth = 0;
|
||||
auto [a, b] = CompressorTree::reduce_scheduled(module, std::move(pool), width, opt.strategy, nullptr, &final_depth);
|
||||
auto final_choice = CompressorTree::pick_final_adder(width, final_depth, opt.final_mode);
|
||||
CompressorTree::emit_final_adder(module, a, b, result_y, final_choice);
|
||||
}
|
||||
|
||||
void process_chains()
|
||||
{
|
||||
pool<Cell *> candidates;
|
||||
for (auto cell : cells.addsub)
|
||||
for (auto cell : addsub)
|
||||
candidates.insert(cell);
|
||||
for (auto cell : cells.alu)
|
||||
if (alu_info.is_chainable(cell))
|
||||
for (auto cell : alu)
|
||||
if (is_chainable(cell))
|
||||
candidates.insert(cell);
|
||||
|
||||
if (candidates.empty())
|
||||
|
|
@ -354,7 +376,7 @@ struct Rewriter {
|
|||
for (auto c : chain)
|
||||
to_remove.insert(c);
|
||||
|
||||
replace_with_carry_save_tree(operands, root->getPort(ID::Y), neg_compensation, "Replaced add/sub chain");
|
||||
emit_tree(operands, root->getPort(ID::Y), neg_compensation);
|
||||
}
|
||||
|
||||
for (auto cell : to_remove)
|
||||
|
|
@ -363,48 +385,66 @@ struct Rewriter {
|
|||
|
||||
void process_maccs()
|
||||
{
|
||||
for (auto cell : cells.macc) {
|
||||
pool<Cell *> to_remove;
|
||||
for (auto cell : macc) {
|
||||
std::vector<Operand> operands;
|
||||
int neg_compensation;
|
||||
if (!extract_macc_operands(cell, operands, neg_compensation))
|
||||
continue;
|
||||
if (operands.size() < 3)
|
||||
if (operands.size() < 1)
|
||||
continue;
|
||||
|
||||
replace_with_carry_save_tree(operands, cell->getPort(ID::Y), neg_compensation, "Replaced $macc");
|
||||
module->remove(cell);
|
||||
int mul_terms = 0;
|
||||
for (auto &op : operands)
|
||||
if (GetSize(op.factor_b) > 0)
|
||||
mul_terms++;
|
||||
bool has_mul = (mul_terms > 0);
|
||||
if (mul_terms == 1 && operands.size() == 1)
|
||||
continue;
|
||||
if (!has_mul && operands.size() < 3)
|
||||
continue;
|
||||
emit_tree(operands, cell->getPort(ID::Y), neg_compensation);
|
||||
to_remove.insert(cell);
|
||||
}
|
||||
for (auto cell : to_remove)
|
||||
module->remove(cell);
|
||||
}
|
||||
|
||||
void run()
|
||||
{
|
||||
if (addsub.empty() && alu.empty() && macc.empty())
|
||||
return;
|
||||
|
||||
process_chains();
|
||||
process_maccs();
|
||||
}
|
||||
};
|
||||
|
||||
void run(Module *module)
|
||||
{
|
||||
Cells cells(module);
|
||||
|
||||
if (cells.empty())
|
||||
return;
|
||||
|
||||
Rewriter rewriter{module, cells};
|
||||
rewriter.process_chains();
|
||||
rewriter.process_maccs();
|
||||
}
|
||||
|
||||
struct ArithTreePass : public Pass {
|
||||
ArithTreePass() : Pass("arith_tree", "convert add/sub/macc chains to carry-save adder trees") {}
|
||||
ArithTreePass() : Pass("arith_tree", "convert add/sub/macc/alu chains to carry-save adder trees") {}
|
||||
|
||||
void help() override
|
||||
{
|
||||
// |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
|
||||
log("\n");
|
||||
log(" arith_tree [selection]\n");
|
||||
log(" arith_tree [options] [selection]\n");
|
||||
log("\n");
|
||||
log("This pass replaces chains of $add/$sub cells, $alu cells (with constant\n");
|
||||
log("BI/CI), and $macc/$macc_v2 cells (without multiplications) with carry-save\n");
|
||||
log("adder trees using $fa cells and a single final $add.\n");
|
||||
log("BI/CI), and $macc/$macc_v2 cells with carry-save adder trees \n");
|
||||
log("using $fa cells and a single final adder.\n");
|
||||
log("\n");
|
||||
log("The tree uses Wallace-tree scheduling: at each level, ready operands are\n");
|
||||
log("grouped into triplets and compressed via full adders, giving\n");
|
||||
log("O(log_{1.5} N) depth for N input operands.\n");
|
||||
log(" -strategy <fa|42>\n");
|
||||
log(" Compressor strategy. 'fa' uses only 3:2 full-adder groupings\n");
|
||||
log(" '42' (the default) prefers 4:2 compressor groupings, with\n");
|
||||
log(" fallback to 3:2 compressors for residuals\n");
|
||||
log("\n");
|
||||
log(" -final <auto|ripple|prefix>\n");
|
||||
log(" Selects the architecture used for the final two-vector add.\n");
|
||||
log("\n");
|
||||
log(" -no-fma\n");
|
||||
log(" Disable fused multiply-add expansion in $macc cells\n");
|
||||
log("\n");
|
||||
log("The default behaviour delivers 4:2 compression, FMA fusion, and a\n");
|
||||
log("final standard adder\n");
|
||||
log("\n");
|
||||
}
|
||||
|
||||
|
|
@ -412,13 +452,37 @@ struct ArithTreePass : public Pass {
|
|||
{
|
||||
log_header(design, "Executing ARITH_TREE pass.\n");
|
||||
|
||||
ArithTreeOptions opt;
|
||||
|
||||
size_t argidx;
|
||||
for (argidx = 1; argidx < args.size(); argidx++)
|
||||
for (argidx = 1; argidx < args.size(); argidx++) {
|
||||
const std::string &arg = args[argidx];
|
||||
if (arg == "-strategy" && argidx + 1 < args.size()) {
|
||||
const std::string &v = args[++argidx];
|
||||
if (v == "fa") { opt.strategy = CompressorTree::Strategy::FA_ONLY; }
|
||||
else if (v == "42") { opt.strategy = CompressorTree::Strategy::PREFER_42; }
|
||||
else { log_cmd_error("arith_tree: unknown -strategy '%s'\n", v.c_str()); }
|
||||
continue;
|
||||
}
|
||||
if (arg == "-final" && argidx + 1 < args.size()) {
|
||||
const std::string &v = args[++argidx];
|
||||
if (v == "auto") { opt.final_mode = CompressorTree::FinalMode::AUTO; }
|
||||
else if (v == "ripple") { opt.final_mode = CompressorTree::FinalMode::RIPPLE; }
|
||||
else if (v == "prefix") { opt.final_mode = CompressorTree::FinalMode::PREFIX; }
|
||||
else { log_cmd_error("arith_tree: unknown -final '%s'\n", v.c_str()); }
|
||||
continue;
|
||||
}
|
||||
if (arg == "-no-fma") {
|
||||
opt.fma_fusion = false;
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
}
|
||||
extra_args(args, argidx, design);
|
||||
|
||||
for (auto module : design->selected_modules()) {
|
||||
run(module);
|
||||
for (auto mod : design->selected_modules()) {
|
||||
ArithTreeWorker worker(opt, mod);
|
||||
worker.run();
|
||||
}
|
||||
}
|
||||
} ArithTreePass;
|
||||
|
|
|
|||
|
|
@ -58,7 +58,7 @@ synth -top my_design -booth
|
|||
#include "kernel/sigtools.h"
|
||||
#include "kernel/yosys.h"
|
||||
#include "kernel/macc.h"
|
||||
#include "kernel/wallace_tree.h"
|
||||
#include "kernel/compressor_tree.h"
|
||||
|
||||
USING_YOSYS_NAMESPACE
|
||||
PRIVATE_NAMESPACE_BEGIN
|
||||
|
|
@ -260,7 +260,7 @@ struct BoothPassWorker {
|
|||
y_sz_revised = y_sz + 1;
|
||||
} else {
|
||||
x_sz_revised = y_sz;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (x_sz % 2 != 0) {
|
||||
y_sz_revised = x_sz + 1;
|
||||
|
|
@ -386,7 +386,11 @@ struct BoothPassWorker {
|
|||
// Later on yosys will clean up unused constants
|
||||
// DebugDumpAlignPP(aligned_pp);
|
||||
|
||||
auto [wtree_a, wtree_b] = wallace_reduce_scheduled(module, aligned_pp, z_sz);
|
||||
std::vector<CompressorTree::DepthSig> operands;
|
||||
operands.reserve(aligned_pp.size());
|
||||
for (auto &s : aligned_pp)
|
||||
operands.push_back({s, 0});
|
||||
auto [wtree_a, wtree_b] = CompressorTree::reduce_scheduled(module, std::move(operands), z_sz, CompressorTree::Strategy::FA_ONLY);
|
||||
|
||||
// Debug code: Dump out the csa trees
|
||||
// DumpCSATrees(debug_csa_trees);
|
||||
|
|
@ -800,7 +804,7 @@ struct BoothPassWorker {
|
|||
c_result = c_wire;
|
||||
|
||||
debug_csa_trees[column_ix].push_back(csa);
|
||||
csa_ix++;
|
||||
csa_ix++;
|
||||
|
||||
if (var_ix <= column_bits.size() - 1)
|
||||
carry_bits_to_sum.append(c_wire);
|
||||
|
|
|
|||
|
|
@ -139,7 +139,7 @@ struct DffLegalizePass : public Pass {
|
|||
}
|
||||
|
||||
// Table of all supported cell types.
|
||||
// First index in the array is one of the FF_* values, second
|
||||
// First index in the array is one of the FF_* values, second
|
||||
// index is the set of negative-polarity inputs (OR of NEG_*
|
||||
// values), and the value is the set of supported init values
|
||||
// (OR of INIT_* values).
|
||||
|
|
@ -370,10 +370,16 @@ struct DffLegalizePass : public Pass {
|
|||
else
|
||||
fail_ff(ff, "initialized dffs with async set and reset are not supported");
|
||||
} else {
|
||||
if (!supported_cells[FF_DLATCHSR])
|
||||
fail_ff(ff, "dlatch with async set and reset are not supported");
|
||||
else
|
||||
fail_ff(ff, "initialized dlatch with async set and reset are not supported");
|
||||
if (!supported_dlatch) {
|
||||
if (!supported_cells[FF_DLATCHSR])
|
||||
fail_ff(ff, "dlatch with async set and reset are not supported");
|
||||
else
|
||||
fail_ff(ff, "initialized dlatch with async set and reset are not supported");
|
||||
}
|
||||
if (ff.cell)
|
||||
log_warning("Emulating async set + reset latch with a plain D latch and logic for %s.%s\n", ff.module->name.unescape(), ff.cell->name.unescape());
|
||||
emulate_dlatch(ff);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -449,6 +455,51 @@ struct DffLegalizePass : public Pass {
|
|||
legalize_ff(ff_sel);
|
||||
}
|
||||
|
||||
void emulate_dlatch(FfData &ff) {
|
||||
// emulate adlatch or dlatchsr
|
||||
log_assert(!ff.has_clk);
|
||||
log_assert(ff.has_aload);
|
||||
log_assert(ff.width == 1);
|
||||
|
||||
auto active_high = [&](SigBit sig, bool pol) -> SigBit {
|
||||
if (pol)
|
||||
return sig;
|
||||
return ff.is_fine ? ff.module->NotGate(NEW_ID, sig) : ff.module->Not(NEW_ID, sig)[0];
|
||||
};
|
||||
|
||||
auto do_mux = [&](SigBit a, SigBit b, SigBit s) -> SigBit {
|
||||
return ff.is_fine ? ff.module->MuxGate(NEW_ID, a, b, s) : ff.module->Mux(NEW_ID, a, b, s)[0];
|
||||
};
|
||||
|
||||
auto do_or = [&](SigBit a, SigBit b) -> SigBit {
|
||||
return ff.is_fine ? ff.module->OrGate(NEW_ID, a, b) : ff.module->Or(NEW_ID, a, b)[0];
|
||||
};
|
||||
|
||||
SigBit en = active_high(ff.sig_aload, ff.pol_aload);
|
||||
SigBit d = ff.sig_ad;
|
||||
|
||||
if (ff.has_sr) {
|
||||
SigBit set = active_high(ff.sig_set[0], ff.pol_set);
|
||||
SigBit clr = active_high(ff.sig_clr[0], ff.pol_clr);
|
||||
// clr > set > load > hold
|
||||
d = do_mux(d, State::S1, set);
|
||||
d = do_mux(d, State::S0, clr);
|
||||
en = do_or(en, do_or(set, clr));
|
||||
ff.has_sr = false;
|
||||
}
|
||||
if (ff.has_arst) {
|
||||
SigBit arst = active_high(ff.sig_arst[0], ff.pol_arst);
|
||||
d = do_mux(d, ff.val_arst[0], arst);
|
||||
en = do_or(en, arst);
|
||||
ff.has_arst = false;
|
||||
}
|
||||
|
||||
ff.sig_ad = d;
|
||||
ff.sig_aload = en;
|
||||
ff.pol_aload = true;
|
||||
legalize_dlatch(ff);
|
||||
}
|
||||
|
||||
void legalize_dff(FfData &ff) {
|
||||
if (!try_flip(ff, supported_dff)) {
|
||||
if (!supported_dff)
|
||||
|
|
@ -742,8 +793,14 @@ struct DffLegalizePass : public Pass {
|
|||
|
||||
void legalize_adlatch(FfData &ff) {
|
||||
if (!try_flip(ff, supported_adlatch)) {
|
||||
if (!supported_adlatch)
|
||||
fail_ff(ff, "D latches with async set or reset are not supported");
|
||||
if (!supported_adlatch) {
|
||||
if (!supported_dlatch)
|
||||
fail_ff(ff, "D latches with async set or reset are not supported");
|
||||
if (ff.cell)
|
||||
log_warning("Emulating async reset latch with a plain D latch and logic for %s.%s\n", ff.module->name.unescape(), ff.cell->name.unescape());
|
||||
emulate_dlatch(ff);
|
||||
return;
|
||||
}
|
||||
if (!(supported_dlatch & (INIT_0 | INIT_1)))
|
||||
fail_ff(ff, "initialized D latches are not supported");
|
||||
|
||||
|
|
|
|||
|
|
@ -326,12 +326,12 @@ int counter_tryextract(
|
|||
return 24;
|
||||
//Mux should have A driven by count Q, and B by muxy
|
||||
//if A and B are swapped, CE polarity is inverted
|
||||
if(sigmap(cemux->getPort(ID::B)) == muxy &&
|
||||
if(sigmap(cemux->getPort(ID::B)) == muxy &&
|
||||
sigmap(cemux->getPort(ID::A)) == sigmap(count_reg->getPort(ID::Q)))
|
||||
{
|
||||
extract.ce_inverted = false;
|
||||
}
|
||||
else if(sigmap(cemux->getPort(ID::A)) == muxy &&
|
||||
else if(sigmap(cemux->getPort(ID::A)) == muxy &&
|
||||
sigmap(cemux->getPort(ID::B)) == sigmap(count_reg->getPort(ID::Q)))
|
||||
{
|
||||
extract.ce_inverted = true;
|
||||
|
|
|
|||
|
|
@ -1,4 +1,3 @@
|
|||
|
||||
#define FILTERLIB
|
||||
#include "libparse.cc"
|
||||
|
||||
|
|
|
|||
|
|
@ -142,4 +142,4 @@ inline std::string convert_liberty_files_to_merged_scl(const std::vector<std::st
|
|||
|
||||
YOSYS_NAMESPACE_END
|
||||
|
||||
#endif // LIBERTY_CACHE_H
|
||||
#endif // LIBERTY_CACHE_H
|
||||
|
|
|
|||
|
|
@ -652,7 +652,7 @@ LibertyAst *LibertyParser::parse(bool top_level)
|
|||
return NULL;
|
||||
|
||||
if (tok != 'v') {
|
||||
report_unexpected_token(tok);
|
||||
report_unexpected_token(tok);
|
||||
}
|
||||
|
||||
LibertyAst *ast = new LibertyAst;
|
||||
|
|
@ -662,7 +662,7 @@ LibertyAst *LibertyParser::parse(bool top_level)
|
|||
{
|
||||
tok = lexer(str);
|
||||
|
||||
// allow both ';' and new lines to
|
||||
// allow both ';' and new lines to
|
||||
// terminate a statement.
|
||||
if ((tok == ';') || (tok == 'n'))
|
||||
break;
|
||||
|
|
@ -1286,4 +1286,3 @@ int main(int argc, char **argv)
|
|||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
|
|
|||
|
|
@ -338,7 +338,7 @@ struct TechmapWorker
|
|||
|
||||
RTLIL::Cell *c = module->addCell(c_name, tpl_cell);
|
||||
design->select(module, c);
|
||||
|
||||
|
||||
if (c->type == ID::_TECHMAP_PLACEHOLDER_ && tpl_cell->has_attribute(ID::techmap_chtype)) {
|
||||
c->type = RTLIL::escape_id(tpl_cell->get_string_attribute(ID::techmap_chtype));
|
||||
c->attributes.erase(ID::techmap_chtype);
|
||||
|
|
|
|||
|
|
@ -7,6 +7,9 @@ yosys_test_pass(cell
|
|||
yosys_test_pass(abcloop
|
||||
test_abcloop.cc
|
||||
)
|
||||
yosys_test_pass(kogge_stone
|
||||
test_kogge_stone.cc
|
||||
)
|
||||
|
||||
yosys_pass(raise_error
|
||||
raise_error.cc
|
||||
|
|
|
|||
|
|
@ -389,4 +389,3 @@ struct TestAutotbBackend : public Backend {
|
|||
} TestAutotbBackend;
|
||||
|
||||
PRIVATE_NAMESPACE_END
|
||||
|
||||
|
|
|
|||
101
passes/tests/test_kogge_stone.cc
Normal file
101
passes/tests/test_kogge_stone.cc
Normal file
|
|
@ -0,0 +1,101 @@
|
|||
#include "kernel/compressor_tree.h"
|
||||
#include "kernel/yosys.h"
|
||||
|
||||
USING_YOSYS_NAMESPACE
|
||||
PRIVATE_NAMESPACE_BEGIN
|
||||
|
||||
static void build_lcu_adder(Module *module, SigSpec a, SigSpec b, SigSpec y)
|
||||
{
|
||||
int width = GetSize(y);
|
||||
|
||||
SigSpec p = module->Xor(NEW_ID, a, b);
|
||||
SigSpec g = module->And(NEW_ID, a, b);
|
||||
|
||||
SigSpec co = module->addWire(NEW_ID, width);
|
||||
Cell *lcu = module->addCell(NEW_ID, ID($lcu));
|
||||
lcu->setParam(ID::WIDTH, width);
|
||||
lcu->setPort(ID::P, p);
|
||||
lcu->setPort(ID::G, g);
|
||||
lcu->setPort(ID::CI, State::S0);
|
||||
lcu->setPort(ID::CO, co);
|
||||
|
||||
SigSpec carry_in;
|
||||
carry_in.append(State::S0);
|
||||
carry_in.append(co.extract(0, width - 1));
|
||||
module->addXor(NEW_ID, p, carry_in, y);
|
||||
}
|
||||
|
||||
static Module *make_module(Design *design, IdString name, int width)
|
||||
{
|
||||
Module *module = design->addModule(name);
|
||||
|
||||
Wire *a = module->addWire(ID(a), width);
|
||||
a->port_input = true;
|
||||
Wire *b = module->addWire(ID(b), width);
|
||||
b->port_input = true;
|
||||
Wire *y = module->addWire(ID(y), width);
|
||||
y->port_output = true;
|
||||
module->fixup_ports();
|
||||
|
||||
return module;
|
||||
}
|
||||
|
||||
struct TestKoggeStonePass : public Pass {
|
||||
TestKoggeStonePass() : Pass("test_kogge_stone", "build adders for Kogge-Stone equivalence testing") {}
|
||||
void help() override
|
||||
{
|
||||
// |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
|
||||
log("\n");
|
||||
log(" test_kogge_stone [options]\n");
|
||||
log("\n");
|
||||
log("Build two modules implementing an unsigned 'y = a + b' adder of a given width,\n");
|
||||
log("and compare various internal Kogge-Stone adders.\n");
|
||||
log("\n");
|
||||
log(" -width N\n");
|
||||
log(" width of the operands and result (default = 16)\n");
|
||||
log("\n");
|
||||
log(" -gold name\n");
|
||||
log(" name of the $lcu-based reference module (default = gold)\n");
|
||||
log("\n");
|
||||
log(" -gate name\n");
|
||||
log(" name of the emit_kogge_stone() module (default = gate)\n");
|
||||
log("\n");
|
||||
}
|
||||
void execute(std::vector<std::string> args, Design *design) override
|
||||
{
|
||||
int width = 16;
|
||||
IdString gold_name = ID(gold);
|
||||
IdString gate_name = ID(gate);
|
||||
|
||||
size_t argidx;
|
||||
for (argidx = 1; argidx < args.size(); argidx++) {
|
||||
if (args[argidx] == "-width" && argidx + 1 < args.size()) {
|
||||
width = atoi(args[++argidx].c_str());
|
||||
continue;
|
||||
}
|
||||
if (args[argidx] == "-gold" && argidx + 1 < args.size()) {
|
||||
gold_name = RTLIL::escape_id(args[++argidx]);
|
||||
continue;
|
||||
}
|
||||
if (args[argidx] == "-gate" && argidx + 1 < args.size()) {
|
||||
gate_name = RTLIL::escape_id(args[++argidx]);
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
}
|
||||
extra_args(args, argidx, design, false);
|
||||
|
||||
if (width < 1)
|
||||
log_cmd_error("Width must be at least 1.\n");
|
||||
|
||||
log_header(design, "Executing TEST_KOGGE_STONE pass (width=%d).\n", width);
|
||||
|
||||
Module *gold = make_module(design, gold_name, width);
|
||||
build_lcu_adder(gold, gold->wire(ID(a)), gold->wire(ID(b)), gold->wire(ID(y)));
|
||||
|
||||
Module *gate = make_module(design, gate_name, width);
|
||||
CompressorTree::emit_kogge_stone(gate, gate->wire(ID(a)), gate->wire(ID(b)), gate->wire(ID(y)));
|
||||
}
|
||||
} TestKoggeStonePass;
|
||||
|
||||
PRIVATE_NAMESPACE_END
|
||||
Loading…
Add table
Add a link
Reference in a new issue