2019-08-15 11:35:00 -05:00
|
|
|
/*
|
|
|
|
* yosys -- Yosys Open SYnthesis Suite
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012 Clifford Wolf <clifford@clifford.at>
|
|
|
|
*
|
|
|
|
* 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/yosys.h"
|
|
|
|
#include "kernel/sigtools.h"
|
|
|
|
|
|
|
|
USING_YOSYS_NAMESPACE
|
|
|
|
PRIVATE_NAMESPACE_BEGIN
|
|
|
|
|
2019-08-16 06:26:36 -05:00
|
|
|
// for peepopt_pm
|
|
|
|
bool did_something;
|
|
|
|
|
2019-08-16 04:47:51 -05:00
|
|
|
#include "passes/pmgen/test_pmgen_pm.h"
|
2019-08-16 06:26:36 -05:00
|
|
|
#include "passes/pmgen/ice40_dsp_pm.h"
|
|
|
|
#include "passes/pmgen/peepopt_pm.h"
|
2019-08-15 11:35:00 -05:00
|
|
|
|
2019-08-16 04:47:51 -05:00
|
|
|
void reduce_chain(test_pmgen_pm &pm)
|
2019-08-15 11:35:00 -05:00
|
|
|
{
|
|
|
|
auto &st = pm.st_reduce;
|
|
|
|
auto &ud = pm.ud_reduce;
|
|
|
|
|
|
|
|
if (ud.longest_chain.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
log("Found chain of length %d (%s):\n", GetSize(ud.longest_chain), log_id(st.first->type));
|
|
|
|
|
|
|
|
SigSpec A;
|
|
|
|
SigSpec Y = ud.longest_chain.front().first->getPort(ID(Y));
|
|
|
|
auto last_cell = ud.longest_chain.back().first;
|
|
|
|
|
|
|
|
for (auto it : ud.longest_chain) {
|
|
|
|
auto cell = it.first;
|
|
|
|
if (cell == last_cell) {
|
|
|
|
A.append(cell->getPort(ID(A)));
|
|
|
|
A.append(cell->getPort(ID(B)));
|
|
|
|
} else {
|
|
|
|
A.append(cell->getPort(it.second == ID(A) ? ID(B) : ID(A)));
|
|
|
|
}
|
|
|
|
log(" %s\n", log_id(cell));
|
|
|
|
pm.autoremove(cell);
|
|
|
|
}
|
|
|
|
|
|
|
|
Cell *c;
|
|
|
|
|
|
|
|
if (last_cell->type == ID($_AND_))
|
|
|
|
c = pm.module->addReduceAnd(NEW_ID, A, Y);
|
|
|
|
else if (last_cell->type == ID($_OR_))
|
|
|
|
c = pm.module->addReduceOr(NEW_ID, A, Y);
|
|
|
|
else if (last_cell->type == ID($_XOR_))
|
|
|
|
c = pm.module->addReduceXor(NEW_ID, A, Y);
|
|
|
|
else
|
|
|
|
log_abort();
|
|
|
|
|
|
|
|
log(" -> %s (%s)\n", log_id(c), log_id(c->type));
|
|
|
|
}
|
|
|
|
|
2019-08-16 04:47:51 -05:00
|
|
|
void reduce_tree(test_pmgen_pm &pm)
|
|
|
|
{
|
|
|
|
auto &st = pm.st_reduce;
|
|
|
|
auto &ud = pm.ud_reduce;
|
|
|
|
|
|
|
|
if (ud.longest_chain.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
SigSpec A = ud.leaves;
|
|
|
|
SigSpec Y = st.first->getPort(ID(Y));
|
|
|
|
pm.autoremove(st.first);
|
|
|
|
|
|
|
|
log("Found %s tree with %d leaves for %s (%s).\n", log_id(st.first->type),
|
|
|
|
GetSize(A), log_signal(Y), log_id(st.first));
|
|
|
|
|
|
|
|
Cell *c;
|
|
|
|
|
|
|
|
if (st.first->type == ID($_AND_))
|
|
|
|
c = pm.module->addReduceAnd(NEW_ID, A, Y);
|
|
|
|
else if (st.first->type == ID($_OR_))
|
|
|
|
c = pm.module->addReduceOr(NEW_ID, A, Y);
|
|
|
|
else if (st.first->type == ID($_XOR_))
|
|
|
|
c = pm.module->addReduceXor(NEW_ID, A, Y);
|
|
|
|
else
|
|
|
|
log_abort();
|
|
|
|
|
|
|
|
log(" -> %s (%s)\n", log_id(c), log_id(c->type));
|
|
|
|
}
|
|
|
|
|
2019-08-16 06:26:36 -05:00
|
|
|
#define GENERATE_PATTERN(pmclass, pattern) \
|
|
|
|
generate_pattern<pmclass>([](pmclass &pm, std::function<void()> f){ return pm.run_ ## pattern(f); }, #pmclass, #pattern, design)
|
|
|
|
|
|
|
|
void pmtest_addports(Module *module)
|
|
|
|
{
|
|
|
|
pool<SigBit> driven_bits, used_bits;
|
|
|
|
SigMap sigmap(module);
|
|
|
|
int icnt = 0, ocnt = 0;
|
|
|
|
|
|
|
|
for (auto cell : module->cells())
|
|
|
|
for (auto conn : cell->connections())
|
|
|
|
{
|
|
|
|
if (cell->input(conn.first))
|
|
|
|
for (auto bit : sigmap(conn.second))
|
|
|
|
used_bits.insert(bit);
|
|
|
|
if (cell->output(conn.first))
|
|
|
|
for (auto bit : sigmap(conn.second))
|
|
|
|
driven_bits.insert(bit);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto wire : vector<Wire*>(module->wires()))
|
|
|
|
{
|
|
|
|
SigSpec ibits, obits;
|
|
|
|
for (auto bit : sigmap(wire)) {
|
|
|
|
if (!used_bits.count(bit))
|
|
|
|
obits.append(bit);
|
|
|
|
if (!driven_bits.count(bit))
|
|
|
|
ibits.append(bit);
|
|
|
|
}
|
|
|
|
if (!ibits.empty()) {
|
|
|
|
Wire *w = module->addWire(stringf("\\i%d", icnt++), GetSize(ibits));
|
|
|
|
w->port_input = true;
|
|
|
|
module->connect(ibits, w);
|
|
|
|
}
|
|
|
|
if (!obits.empty()) {
|
|
|
|
Wire *w = module->addWire(stringf("\\o%d", ocnt++), GetSize(obits));
|
|
|
|
w->port_output = true;
|
|
|
|
module->connect(w, obits);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module->fixup_ports();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class pm>
|
|
|
|
void generate_pattern(std::function<void(pm&,std::function<void()>)> run, const char *pmclass, const char *pattern, Design *design)
|
|
|
|
{
|
|
|
|
log("Generating \"%s\" patterns for pattern matcher \"%s\".\n", pattern, pmclass);
|
|
|
|
|
|
|
|
int modcnt = 0;
|
2019-08-16 06:47:50 -05:00
|
|
|
int maxsubcnt = 4;
|
2019-08-17 06:53:55 -05:00
|
|
|
int timeout = 0;
|
|
|
|
vector<Module*> mods;
|
2019-08-16 06:26:36 -05:00
|
|
|
|
|
|
|
while (modcnt < 100)
|
|
|
|
{
|
|
|
|
int submodcnt = 0, itercnt = 0, cellcnt = 0;
|
|
|
|
Module *mod = design->addModule(NEW_ID);
|
|
|
|
|
2019-08-16 07:35:13 -05:00
|
|
|
while (modcnt < 100 && submodcnt < maxsubcnt && itercnt++ < 1000)
|
2019-08-16 06:26:36 -05:00
|
|
|
{
|
2019-08-17 06:53:55 -05:00
|
|
|
if (timeout++ > 10000)
|
|
|
|
log_error("pmgen generator is stuck: 10000 iterations an no matching module generated.\n");
|
|
|
|
|
2019-08-16 06:26:36 -05:00
|
|
|
pm matcher(mod, mod->cells());
|
|
|
|
|
|
|
|
matcher.rng(1);
|
|
|
|
matcher.rngseed += modcnt;
|
|
|
|
matcher.rng(1);
|
|
|
|
matcher.rngseed += submodcnt;
|
|
|
|
matcher.rng(1);
|
|
|
|
matcher.rngseed += itercnt;
|
|
|
|
matcher.rng(1);
|
|
|
|
matcher.rngseed += cellcnt;
|
|
|
|
matcher.rng(1);
|
|
|
|
|
|
|
|
if (GetSize(mod->cells()) != cellcnt)
|
|
|
|
{
|
|
|
|
bool found_match = false;
|
|
|
|
run(matcher, [&](){ found_match = true; });
|
2019-08-17 06:53:55 -05:00
|
|
|
cellcnt = GetSize(mod->cells());
|
2019-08-16 06:26:36 -05:00
|
|
|
|
|
|
|
if (found_match) {
|
|
|
|
Module *m = design->addModule(stringf("\\pmtest_%s_%s_%05d",
|
|
|
|
pmclass, pattern, modcnt++));
|
2019-08-17 06:53:55 -05:00
|
|
|
log("Creating module %s with %d cells.\n", log_id(m), cellcnt);
|
2019-08-16 06:26:36 -05:00
|
|
|
mod->cloneInto(m);
|
|
|
|
pmtest_addports(m);
|
2019-08-17 06:53:55 -05:00
|
|
|
mods.push_back(m);
|
2019-08-16 06:26:36 -05:00
|
|
|
submodcnt++;
|
2019-08-17 06:53:55 -05:00
|
|
|
timeout = 0;
|
2019-08-16 06:26:36 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
matcher.generate_mode = true;
|
|
|
|
run(matcher, [](){});
|
|
|
|
}
|
|
|
|
|
2019-08-17 06:53:55 -05:00
|
|
|
if (submodcnt)
|
|
|
|
maxsubcnt *= 2;
|
|
|
|
|
2019-08-16 06:26:36 -05:00
|
|
|
design->remove(mod);
|
|
|
|
}
|
2019-08-17 06:53:55 -05:00
|
|
|
|
|
|
|
Module *m = design->addModule(stringf("\\pmtest_%s_%s", pmclass, pattern));
|
|
|
|
log("Creating module %s with %d cells.\n", log_id(m), GetSize(mods));
|
|
|
|
for (auto mod : mods) {
|
|
|
|
Cell *c = m->addCell(mod->name, mod->name);
|
|
|
|
for (auto port : mod->ports) {
|
|
|
|
Wire *w = m->addWire(NEW_ID, GetSize(mod->wire(port)));
|
|
|
|
c->setPort(port, w);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pmtest_addports(m);
|
2019-08-16 06:26:36 -05:00
|
|
|
}
|
|
|
|
|
2019-08-16 04:47:51 -05:00
|
|
|
struct TestPmgenPass : public Pass {
|
|
|
|
TestPmgenPass() : Pass("test_pmgen", "test pass for pmgen") { }
|
2019-08-15 11:35:00 -05:00
|
|
|
void help() YS_OVERRIDE
|
|
|
|
{
|
|
|
|
// |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
|
|
|
|
log("\n");
|
2019-08-16 04:47:51 -05:00
|
|
|
log(" test_pmgen -reduce_chain [options] [selection]\n");
|
2019-08-15 11:35:00 -05:00
|
|
|
log("\n");
|
|
|
|
log("Demo for recursive pmgen patterns. Map chains of AND/OR/XOR to $reduce_*.\n");
|
|
|
|
log("\n");
|
2019-08-16 06:26:36 -05:00
|
|
|
|
2019-08-16 04:47:51 -05:00
|
|
|
log("\n");
|
|
|
|
log(" test_pmgen -reduce_tree [options] [selection]\n");
|
|
|
|
log("\n");
|
|
|
|
log("Demo for recursive pmgen patterns. Map trees of AND/OR/XOR to $reduce_*.\n");
|
|
|
|
log("\n");
|
2019-08-16 06:26:36 -05:00
|
|
|
|
|
|
|
log("\n");
|
|
|
|
log(" test_pmgen -generate [options] <pattern_name>\n");
|
|
|
|
log("\n");
|
|
|
|
log("Create modules that match the specified pattern.\n");
|
|
|
|
log("\n");
|
2019-08-15 11:35:00 -05:00
|
|
|
}
|
2019-08-16 04:47:51 -05:00
|
|
|
|
|
|
|
void execute_reduce_chain(std::vector<std::string> args, RTLIL::Design *design)
|
2019-08-15 11:35:00 -05:00
|
|
|
{
|
2019-08-16 04:47:51 -05:00
|
|
|
log_header(design, "Executing TEST_PMGEN pass (-reduce_chain).\n");
|
2019-08-15 11:35:00 -05:00
|
|
|
|
|
|
|
size_t argidx;
|
2019-08-16 04:47:51 -05:00
|
|
|
for (argidx = 2; argidx < args.size(); argidx++)
|
2019-08-15 11:35:00 -05:00
|
|
|
{
|
|
|
|
// if (args[argidx] == "-singleton") {
|
|
|
|
// singleton_mode = true;
|
|
|
|
// continue;
|
|
|
|
// }
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
extra_args(args, argidx, design);
|
|
|
|
|
|
|
|
for (auto module : design->selected_modules())
|
2019-08-16 07:16:35 -05:00
|
|
|
while (test_pmgen_pm(module, module->selected_cells()).run_reduce(reduce_chain)) {}
|
2019-08-16 04:47:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void execute_reduce_tree(std::vector<std::string> args, RTLIL::Design *design)
|
|
|
|
{
|
|
|
|
log_header(design, "Executing TEST_PMGEN pass (-reduce_tree).\n");
|
|
|
|
|
|
|
|
size_t argidx;
|
|
|
|
for (argidx = 2; argidx < args.size(); argidx++)
|
|
|
|
{
|
|
|
|
// if (args[argidx] == "-singleton") {
|
|
|
|
// singleton_mode = true;
|
|
|
|
// continue;
|
|
|
|
// }
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
extra_args(args, argidx, design);
|
|
|
|
|
|
|
|
for (auto module : design->selected_modules())
|
|
|
|
test_pmgen_pm(module, module->selected_cells()).run_reduce(reduce_tree);
|
|
|
|
}
|
|
|
|
|
2019-08-16 06:26:36 -05:00
|
|
|
void execute_generate(std::vector<std::string> args, RTLIL::Design *design)
|
|
|
|
{
|
|
|
|
log_header(design, "Executing TEST_PMGEN pass (-generate).\n");
|
|
|
|
|
|
|
|
size_t argidx;
|
|
|
|
for (argidx = 2; argidx < args.size(); argidx++)
|
|
|
|
{
|
|
|
|
// if (args[argidx] == "-singleton") {
|
|
|
|
// singleton_mode = true;
|
|
|
|
// continue;
|
|
|
|
// }
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argidx+1 != args.size())
|
|
|
|
log_cmd_error("Expected exactly one pattern.\n");
|
|
|
|
|
|
|
|
string pattern = args[argidx];
|
|
|
|
|
|
|
|
if (pattern == "reduce")
|
|
|
|
return GENERATE_PATTERN(test_pmgen_pm, reduce);
|
|
|
|
|
|
|
|
if (pattern == "ice40_dsp")
|
|
|
|
return GENERATE_PATTERN(ice40_dsp_pm, ice40_dsp);
|
|
|
|
|
|
|
|
if (pattern == "peepopt-muldiv")
|
|
|
|
return GENERATE_PATTERN(peepopt_pm, muldiv);
|
|
|
|
|
|
|
|
if (pattern == "peepopt-shiftmul")
|
|
|
|
return GENERATE_PATTERN(peepopt_pm, shiftmul);
|
|
|
|
|
|
|
|
log_cmd_error("Unkown pattern: %s\n", pattern.c_str());
|
|
|
|
}
|
|
|
|
|
2019-08-16 04:47:51 -05:00
|
|
|
void execute(std::vector<std::string> args, RTLIL::Design *design) YS_OVERRIDE
|
|
|
|
{
|
|
|
|
if (GetSize(args) > 1)
|
|
|
|
{
|
|
|
|
if (args[1] == "-reduce_chain")
|
|
|
|
return execute_reduce_chain(args, design);
|
|
|
|
if (args[1] == "-reduce_tree")
|
|
|
|
return execute_reduce_tree(args, design);
|
2019-08-16 06:26:36 -05:00
|
|
|
if (args[1] == "-generate")
|
|
|
|
return execute_generate(args, design);
|
2019-08-16 04:47:51 -05:00
|
|
|
}
|
2019-08-16 15:00:12 -05:00
|
|
|
help();
|
2019-08-16 04:47:51 -05:00
|
|
|
log_cmd_error("Missing or unsupported mode parameter.\n");
|
2019-08-15 11:35:00 -05:00
|
|
|
}
|
2019-08-16 04:47:51 -05:00
|
|
|
} TestPmgenPass;
|
2019-08-15 11:35:00 -05:00
|
|
|
|
|
|
|
PRIVATE_NAMESPACE_END
|