Progress in pmgen

Signed-off-by: Clifford Wolf <clifford@clifford.at>
This commit is contained in:
Clifford Wolf 2019-01-13 12:53:13 +01:00
parent b9545aa0e1
commit 1f8e76f993
3 changed files with 158 additions and 22 deletions

View File

@ -24,8 +24,15 @@
USING_YOSYS_NAMESPACE
PRIVATE_NAMESPACE_BEGIN
void ice40_dsp_accept(ice40_dsp_pm * /* pm */)
void ice40_dsp_accept(ice40_dsp_pm *pm)
{
log("\n");
log("mul: %s\n", pm->st.mul ? log_id(pm->st.mul) : "--");
log("ffA: %s\n", pm->st.ffA ? log_id(pm->st.ffA) : "--");
log("ffB: %s\n", pm->st.ffB ? log_id(pm->st.ffB) : "--");
log("ffY: %s\n", pm->st.ffY ? log_id(pm->st.ffY) : "--");
pm->blacklist(pm->st.mul);
}
struct Ice40DspPass : public Pass {
@ -56,7 +63,7 @@ struct Ice40DspPass : public Pass {
for (auto module : design->selected_modules())
{
ice40_dsp_pm pm(module);
ice40_dsp_pm pm(module, module->cells());
pm.run(ice40_dsp_accept);
}
}

View File

@ -10,7 +10,7 @@ endmatch
match ffA
select ffA->type.in($dff)
select nusers(port(ffA, \Q)) == 2
// select nusers(port(ffA, \Q)) == 2
filter <SigSpec> port(ffA, \Q) === port(mul, \A)
optional
endmatch
@ -29,7 +29,7 @@ endcode
match ffB
select ffB->type.in($dff)
select nusers(port(ffA, \Q)) == 2
// select nusers(port(ffB, \Q)) == 2
filter <SigSpec> port(ffB, \Q) === port(mul, \B)
optional
endmatch
@ -73,6 +73,4 @@ code sigY clock clock_pol clock_vld
clock_pol = cp;
clock_vld = true;
}
accept;
endcode

View File

@ -79,7 +79,7 @@ with open("%s.pmg" % prefix, "r") as f:
line = line.strip()
cmd = line.split()
if len(cmd) == 0: continue
if len(cmd) == 0 or cmd[0].startswith("//"): continue
cmd = cmd[0]
if cmd == "state":
@ -110,7 +110,7 @@ with open("%s.pmg" % prefix, "r") as f:
l = f.readline()
assert l != ""
a = l.split()
if len(a) == 0: continue
if len(a) == 0 or a[0].startswith("//"): continue
if a[0] == "endmatch": break
if a[0] == "select":
@ -153,13 +153,17 @@ with open("%s.pmg" % prefix, "r") as f:
blocks.append(block)
# pp.pprint(blocks)
with open("%s_pm.h" % prefix, "w") as f:
print("// Generated by pmgen.py from {}.pgm".format(prefix), file=f)
print("", file=f)
print("#include \"kernel/yosys.h\"", file=f)
print("#include \"kernel/sigtools.h\"", file=f)
print("", file=f)
print("YOSYS_NAMESPACE_BEGIN", file=f)
print("", file=f)
print("struct {}_pm {{".format(prefix), file=f)
print(" Module *module;", file=f)
print(" SigMap sigmap;", file=f)
@ -174,7 +178,9 @@ with open("%s_pm.h" % prefix, "w") as f:
index_types.append(filt[0])
print(" typedef std::tuple<{}> index_{}_key_type;".format(", ".join(index_types), index), file=f)
print(" dict<index_{}_key_type, vector<Cell*>> index_{};".format(index, index), file=f)
print(" pool<Cell*> blacklist;", file=f)
print(" dict<SigBit, pool<Cell*>> sigusers;", file=f)
print(" pool<Cell*> blacklist_cells;", file=f)
print(" int rollback;", file=f)
print("", file=f)
print(" struct state_t {", file=f)
@ -191,21 +197,89 @@ with open("%s_pm.h" % prefix, "w") as f:
print(" IdString {}{{\"{}\"}};".format(v, n), file=f)
print("", file=f)
print(" {}_pm(Module *module) :".format(prefix), file=f)
print(" module(module), sigmap(module) {", file=f)
print(" void add_siguser(const SigSpec &sig, Cell *cell) {", file=f)
print(" for (auto bit : sigmap(sig)) {", file=f)
print(" if (bit.wire == nullptr) continue;", file=f)
print(" if (sigusers.count(bit) == 0 && bit.wire->port_id)", file=f)
print(" sigusers[bit].insert(nullptr);", file=f)
print(" sigusers[bit].insert(cell);", file=f)
print(" }", file=f)
print(" }", file=f)
print("", file=f)
print(" void blacklist(Cell *cell) {", file=f)
print(" blacklist_cells.insert(cell);", file=f)
print(" }", file=f)
print("", file=f)
print(" void check_blacklist() {", file=f)
for index in range(len(blocks)):
block = blocks[index]
if block["type"] == "match":
print(" if (st.{} != nullptr && blacklist_cells.count(st.{})) {{".format(block["cell"], block["cell"]), file=f)
print(" rollback = {};".format(index+1), file=f)
print(" return;", file=f)
print(" }", file=f)
print(" rollback = 0;", file=f)
print(" }", file=f)
print("", file=f)
print(" SigSpec port(Cell *cell, IdString portname) {", file=f)
print(" return sigmap(cell->getPort(portname));", file=f)
print(" }", file=f)
print("", file=f)
print(" Const param(Cell *cell, IdString paramname) {", file=f)
print(" return cell->getParam(paramname);", file=f)
print(" }", file=f)
print("", file=f)
print(" int nusers(const SigSpec &sig) {", file=f)
print(" pool<Cell*> users;", file=f)
print(" for (auto bit : sigmap(sig))", file=f)
print(" for (auto user : sigusers[bit])", file=f)
print(" users.insert(user);", file=f)
print(" return GetSize(users);", file=f)
print(" }", file=f)
print("", file=f)
print(" {}_pm(Module *module, const vector<Cell*> &cells) :".format(prefix), file=f)
print(" module(module), sigmap(module) {", file=f)
print(" for (auto cell : cells) {", file=f)
print(" for (auto &conn : cell->connections())", file=f)
print(" add_siguser(conn.second, cell);", file=f)
for index in range(len(blocks)):
block = blocks[index]
if block["type"] == "match":
print(" do {", file=f)
print(" Cell *{} = cell;".format(block["cell"]), file=f)
for expr in block["select"]:
print(" if (!({})) break;".format(expr), file=f)
print(" index_{}_key_type key;".format(index), file=f)
for field, filt in enumerate(block["filter"]):
print(" std::get<{}>(key) = {};".format(field, filt[1]), file=f)
print(" index_{}[key].push_back(cell);".format(index), file=f)
print(" } while (0);", file=f)
print(" }", file=f)
print(" }", file=f)
print("", file=f)
print(" void run(std::function<void(struct {}_pm*)> on_accept_f) {{".format(prefix), file=f)
print(" on_accept = on_accept_f;", file=f)
if len(blocks):
print(" block_0();", file=f)
print(" rollback = 0;", file=f)
print(" block_0();", file=f)
print(" }", file=f)
print("", file=f)
print("#define reject break", file=f)
print("#define accept do { on_accept(this); check_blacklist(); if (rollback) goto rollback_label; } while(0)", file=f)
print("", file=f)
for index in range(len(blocks)):
block = blocks[index]
print("", file=f)
print(" void block_{}() {{".format(index), file=f)
const_st = set()
@ -251,12 +325,69 @@ with open("%s_pm.h" % prefix, "w") as f:
t = state_types[s]
print(" {} backup_{} = st.{};".format(t, s, s), file=f)
if len(restore_st):
if block["type"] == "code":
print("", file=f)
for s in sorted(restore_st):
t = state_types[s]
print(" st.{} = backup_{};".format(s, s), file=f)
print(" do {", file=f)
for line in block["code"]:
print(" " + line, file=f)
print("", file=f)
print(" block_{}();".format(index+1), file=f)
print("rollback_label: YS_ATTRIBUTE(unused);", file=f)
print(" } while (0);", file=f)
if len(restore_st):
print("", file=f)
for s in sorted(restore_st):
t = state_types[s]
print(" st.{} = backup_{};".format(s, s), file=f)
elif block["type"] == "match":
assert len(restore_st) == 0
print("", file=f)
print(" index_{}_key_type key;".format(index), file=f)
for field, filt in enumerate(block["filter"]):
print(" std::get<{}>(key) = {};".format(field, filt[2]), file=f)
print(" const vector<Cell*> &cells = index_{}[key];".format(index), file=f)
print("", file=f)
print(" for (int idx = 0; idx < GetSize(cells); idx++) {", file=f)
print(" {} = cells[idx];".format(block["cell"]), file=f)
print(" block_{}();".format(index+1), file=f)
print(" if (rollback) {", file=f)
print(" if (rollback != {}) {{".format(index+1), file=f)
print(" {} = nullptr;".format(block["cell"]), file=f)
print(" return;", file=f)
print(" }", file=f)
print(" rollback = 0;", file=f)
print(" }", file=f)
print(" }", file=f)
print("", file=f)
print(" {} = nullptr;".format(block["cell"]), file=f)
if block["optional"]:
print(" block_{}();".format(index+1), file=f)
else:
assert False
print(" }", file=f)
print("", file=f)
print("};\nYOSYS_NAMESPACE_END", file=f)
print("#undef reject", file=f)
print("#undef accept", file=f)
print("", file=f)
print(" void block_{}() {{".format(len(blocks)), file=f)
print(" on_accept(this);", file=f)
print(" check_blacklist();", file=f)
print(" }", file=f)
print("};", file=f)
print("", file=f)
print("YOSYS_NAMESPACE_END", file=f)
# pp.pprint(blocks)