3
0
Fork 0
mirror of https://github.com/YosysHQ/yosys synced 2025-04-24 01:25:33 +00:00

Merge branch 'YosysHQ:main' into main

This commit is contained in:
Akash Levy 2024-12-11 12:00:34 -08:00 committed by GitHub
commit caaef5ac14
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
21 changed files with 432 additions and 137 deletions

View file

@ -18,12 +18,37 @@
*/
#include "kernel/yosys.h"
#include "kernel/celltypes.h"
#include "kernel/sigtools.h"
#include "backends/rtlil/rtlil_backend.h"
USING_YOSYS_NAMESPACE
PRIVATE_NAMESPACE_BEGIN
std::optional<std::string> format(std::string fmt, const dict<IdString, Const> &parameters)
bool has_fmt_field(std::string fmt, std::string field_name)
{
auto it = fmt.begin();
while (it != fmt.end()) {
if (*it == '{') {
it++;
auto beg = it;
while (it != fmt.end() && *it != '}') it++;
if (it == fmt.end())
return false;
if (std::string(beg, it) == field_name)
return true;
}
it++;
}
return false;
}
struct ContextData {
std::string unused_outputs;
};
std::optional<std::string> format(std::string fmt, const dict<IdString, Const> &parameters,
const ContextData &context)
{
std::stringstream result;
@ -38,13 +63,19 @@ std::optional<std::string> format(std::string fmt, const dict<IdString, Const> &
return {};
}
auto id = RTLIL::escape_id(std::string(beg, it));
if (!parameters.count(id)) {
log("Parameter %s referenced in format string '%s' not found\n", log_id(id), fmt.c_str());
return {};
}
std::string param_name = {beg, it};
RTLIL_BACKEND::dump_const(result, parameters.at(id));
if (param_name == "%unused") {
result << context.unused_outputs;
} else {
auto id = RTLIL::escape_id(std::string(beg, it));
if (!parameters.count(id)) {
log("Parameter %s referenced in format string '%s' not found\n", log_id(id), fmt.c_str());
return {};
}
RTLIL_BACKEND::dump_const(result, parameters.at(id));
}
} else {
result << *it;
}
@ -54,6 +85,45 @@ std::optional<std::string> format(std::string fmt, const dict<IdString, Const> &
return {result.str()};
}
struct Chunk {
IdString port;
int base, len;
Chunk(IdString id, int base, int len)
: port(id), base(base), len(len) {}
IdString format(Cell *cell)
{
if (len == cell->getPort(port).size())
return port;
else if (len == 1)
return stringf("%s[%d]", port.c_str(), base);
else
return stringf("%s[%d:%d]", port.c_str(), base + len - 1, base);
}
SigSpec sample(Cell *cell)
{
return cell->getPort(port).extract(base, len);
}
};
// Joins contiguous runs of bits into a 'Chunk'
std::vector<Chunk> collect_chunks(std::vector<std::pair<IdString, int>> bits)
{
std::vector<Chunk> ret;
std::sort(bits.begin(), bits.end());
for (auto it = bits.begin(); it != bits.end();) {
auto sep = it + 1;
for (; sep != bits.end() &&
sep->first == it->first &&
sep->second == (sep - 1)->second + 1; sep++);
ret.emplace_back(it->first, it->second, sep - it);
it = sep;
}
return ret;
}
struct WrapcellPass : Pass {
WrapcellPass() : Pass("wrapcell", "wrap individual cells into new modules") {}
@ -68,6 +138,10 @@ struct WrapcellPass : Pass {
log("parameter values as specified in curly brackets. If the named module already\n");
log("exists, it is reused.\n");
log("\n");
log("If the template contains the special string '{%%unused}', the command tracks\n");
log("unused output ports -- specialized wrapper modules will be generated per every\n");
log("distinct set of unused port bits as appearing on any selected cell.\n");
log("\n");
log(" -setattr <attribute-name>\n");
log(" set the given boolean attribute on each created wrapper module\n");
log("\n");
@ -114,35 +188,81 @@ struct WrapcellPass : Pass {
CellTypes ct;
ct.setup();
bool tracking_unused = has_fmt_field(name_fmt, "%unused");
for (auto module : d->selected_modules()) {
for (auto cell : module->selected_cells()) {
std::optional<std::string> unescaped_name = format(name_fmt, cell->parameters);
if (!unescaped_name)
log_error("Formatting error when processing cell '%s' in module '%s'\n",
log_id(cell), log_id(module));
SigPool unused;
IdString name = RTLIL::escape_id(unescaped_name.value());
if (d->module(name)) {
cell->type = name;
cell->parameters.clear();
continue;
for (auto wire : module->wires())
if (wire->has_attribute(ID::unused_bits)) {
std::string str = wire->get_string_attribute(ID::unused_bits);
for (auto it = str.begin(); it != str.end();) {
auto sep = it;
for (; sep != str.end() && *sep != ' '; sep++);
unused.add(SigBit(wire, std::stoi(std::string(it, sep))));
for (it = sep; it != str.end() && *it == ' '; it++);
}
}
for (auto cell : module->selected_cells()) {
Module *subm;
Cell *subcell;
if (!ct.cell_known(cell->type))
log_error("Non-internal cell type '%s' on cell '%s' in module '%s' unsupported\n",
log_id(cell->type), log_id(cell), log_id(module));
Module *subm = d->addModule(name);
Cell *subcell = subm->addCell("$1", cell->type);
std::vector<std::pair<IdString, int>> unused_outputs, used_outputs;
for (auto conn : cell->connections()) {
Wire *w = subm->addWire(conn.first, conn.second.size());
if (ct.cell_output(cell->type, w->name))
w->port_output = true;
else
w->port_input = true;
subcell->setPort(conn.first, w);
if (ct.cell_output(cell->type, conn.first))
for (int i = 0; i < conn.second.size(); i++) {
if (tracking_unused && unused.check(conn.second[i]))
unused_outputs.emplace_back(conn.first, i);
else
used_outputs.emplace_back(conn.first, i);
}
}
ContextData context;
if (!unused_outputs.empty()) {
context.unused_outputs += "_unused";
for (auto chunk : collect_chunks(unused_outputs))
context.unused_outputs += "_" + RTLIL::unescape_id(chunk.format(cell));
}
std::optional<std::string> unescaped_name = format(name_fmt, cell->parameters, context);
if (!unescaped_name)
log_error("Formatting error when processing cell '%s' in module '%s'\n",
log_id(cell), log_id(module));
IdString name = RTLIL::escape_id(unescaped_name.value());
if (d->module(name))
goto replace_cell;
subm = d->addModule(name);
subcell = subm->addCell("$1", cell->type);
for (auto conn : cell->connections()) {
if (ct.cell_output(cell->type, conn.first)) {
// Insert marker bits as placehodlers which need to be replaced
subcell->setPort(conn.first, SigSpec(RTLIL::Sm, conn.second.size()));
} else {
Wire *w = subm->addWire(conn.first, conn.second.size());
w->port_input = true;
subcell->setPort(conn.first, w);
}
}
for (auto chunk : collect_chunks(used_outputs)) {
Wire *w = subm->addWire(chunk.format(cell), chunk.len);
w->port_output = true;
subcell->connections_[chunk.port].replace(chunk.base, w);
}
for (auto chunk : collect_chunks(unused_outputs)) {
Wire *w = subm->addWire(chunk.format(cell), chunk.len);
subcell->connections_[chunk.port].replace(chunk.base, w);
}
subcell->parameters = cell->parameters;
subm->fixup_ports();
@ -150,7 +270,7 @@ struct WrapcellPass : Pass {
if (rule.value_fmt.empty()) {
subm->set_bool_attribute(rule.name);
} else {
std::optional<std::string> value = format(rule.value_fmt, cell->parameters);
std::optional<std::string> value = format(rule.value_fmt, cell->parameters, context);
if (!value)
log_error("Formatting error when processing cell '%s' in module '%s'\n",
@ -160,8 +280,20 @@ struct WrapcellPass : Pass {
}
}
cell->type = name;
replace_cell:
cell->parameters.clear();
dict<IdString, SigSpec> new_connections;
for (auto conn : cell->connections())
if (!ct.cell_output(cell->type, conn.first))
new_connections[conn.first] = conn.second;
for (auto chunk : collect_chunks(used_outputs))
new_connections[chunk.format(cell)] = chunk.sample(cell);
cell->type = name;
cell->connections_ = new_connections;
}
}
}

View file

@ -969,13 +969,10 @@ void prep_box(RTLIL::Design *design)
if (it == module->attributes.end())
continue;
bool box = it->second.as_bool();
module->attributes.erase(it);
if (!box)
continue;
auto r = module->attributes.insert(ID::abc9_box_id);
if (!r.second)
continue;
r.first->second = abc9_box_id++;
if (module->get_bool_attribute(ID::abc9_flop)) {
@ -1078,7 +1075,8 @@ void prep_box(RTLIL::Design *design)
}
ss << log_id(module) << " " << module->attributes.at(ID::abc9_box_id).as_int();
ss << " " << (module->get_bool_attribute(ID::whitebox) ? "1" : "0");
bool has_model = module->get_bool_attribute(ID::whitebox) || !module->get_bool_attribute(ID::blackbox);
ss << " " << (has_model ? "1" : "0");
ss << " " << GetSize(inputs) << " " << GetSize(outputs) << std::endl;
bool first = true;
@ -1096,8 +1094,9 @@ void prep_box(RTLIL::Design *design)
ss << std::endl;
auto &t = timing.setup_module(module);
if (t.comb.empty())
if (t.comb.empty() && !outputs.empty() && !inputs.empty()) {
log_error("Module '%s' with (* abc9_box *) has no timing (and thus no connectivity) information.\n", log_id(module));
}
for (const auto &o : outputs) {
first = true;

View file

@ -19,10 +19,29 @@
#include "kernel/register.h"
#include "kernel/rtlil.h"
#include "kernel/utils.h"
USING_YOSYS_NAMESPACE
PRIVATE_NAMESPACE_BEGIN
std::vector<Module*> order_modules(Design *design, std::vector<Module *> modules)
{
std::set<Module *> modules_set(modules.begin(), modules.end());
TopoSort<Module*> sort;
for (auto m : modules) {
sort.node(m);
for (auto cell : m->cells()) {
Module *submodule = design->module(cell->type);
if (modules_set.count(submodule))
sort.edge(submodule, m);
}
}
log_assert(sort.sort());
return sort.sorted;
}
struct AbcNewPass : public ScriptPass {
AbcNewPass() : ScriptPass("abc_new", "(experimental) use ABC for SC technology mapping (new)")
{
@ -101,6 +120,15 @@ struct AbcNewPass : public ScriptPass {
}
if (check_label("prep_boxes")) {
if (!help_mode) {
for (auto mod : active_design->selected_whole_modules_warn()) {
if (mod->get_bool_attribute(ID::abc9_box)) {
mod->set_bool_attribute(ID::abc9_box, false);
mod->set_bool_attribute(ID(abc9_deferred_box), true);
}
}
}
run("box_derive");
run("abc9_ops -prep_box");
}
@ -109,7 +137,8 @@ struct AbcNewPass : public ScriptPass {
std::vector<Module *> selected_modules;
if (!help_mode) {
selected_modules = active_design->selected_whole_modules_warn();
selected_modules = order_modules(active_design,
active_design->selected_whole_modules_warn());
active_design->selection_stack.emplace_back(false);
} else {
selected_modules = {nullptr};
@ -131,15 +160,36 @@ struct AbcNewPass : public ScriptPass {
active_design->selection().select(mod);
}
std::string script_save;
if (!help_mode && mod->has_attribute(ID(abc9_script))) {
script_save = active_design->scratchpad_get_string("abc9.script");
active_design->scratchpad_set_string("abc9.script",
mod->get_string_attribute(ID(abc9_script)));
}
run(stringf(" abc9_ops -write_box %s/input.box", tmpdir.c_str()));
run(stringf(" write_xaiger2 -mapping_prep -map2 %s/input.map2 %s/input.xaig", tmpdir.c_str(), tmpdir.c_str()));
run(stringf(" abc9_exe %s -cwd %s -box %s/input.box", exe_options.c_str(), tmpdir.c_str(), tmpdir.c_str()));
run(stringf(" read_xaiger2 -sc_mapping -module_name %s -map2 %s/input.map2 %s/output.aig",
modname.c_str(), tmpdir.c_str(), tmpdir.c_str()));
if (!help_mode && mod->has_attribute(ID(abc9_script))) {
if (script_save.empty())
active_design->scratchpad_unset("abc9.script");
else
active_design->scratchpad_set_string("abc9.script", script_save);
}
if (!help_mode) {
active_design->selection().selected_modules.clear();
log_pop();
if (mod->get_bool_attribute(ID(abc9_deferred_box))) {
mod->set_bool_attribute(ID(abc9_deferred_box), false);
mod->set_bool_attribute(ID::abc9_box, true);
Pass::call_on_module(active_design, mod, "portarcs -draw -write");
run("abc9_ops -prep_box");
}
}
}