Skip to content

Commit d9af465

Browse files
committed
opt_expand peepopt (still needs testing)
1 parent dc75774 commit d9af465

File tree

4 files changed

+162
-0
lines changed

4 files changed

+162
-0
lines changed

passes/silimate/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/peepopt*.h

passes/silimate/Makefile.inc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,13 @@ OBJS += passes/silimate/segv.o
1212
OBJS += passes/silimate/selectconst.o
1313
OBJS += passes/silimate/splitfanout.o
1414
OBJS += passes/silimate/splitnetlist.o
15+
16+
OBJS += passes/silimate/opt_expand.o
17+
GENFILES += passes/silimate/peepopt_expand.h
18+
passes/silimate/opt_expand.o: passes/silimate/peepopt_expand.h
19+
$(eval $(call add_extra_objs,passes/silimate/peepopt_expand.h))
20+
21+
PEEPOPT_PATTERN = passes/silimate/peepopt_expand.pmg
22+
23+
passes/silimate/peepopt_expand.h: passes/pmgen/pmgen.py $(PEEPOPT_PATTERN)
24+
$(P) mkdir -p $(dir $@) && $(PYTHON_EXECUTABLE) $< -o $@ -p peepopt $(filter-out $<,$^)

passes/silimate/opt_expand.cc

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* yosys -- Yosys Open SYnthesis Suite
3+
*
4+
* Copyright (C) 2012 Claire Xenia Wolf <[email protected]>
5+
* Akash Levy <[email protected]>
6+
*
7+
* Permission to use, copy, modify, and/or distribute this software for any
8+
* purpose with or without fee is hereby granted, provided that the above
9+
* copyright notice and this permission notice appear in all copies.
10+
*
11+
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12+
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13+
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14+
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15+
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16+
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17+
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18+
*
19+
*/
20+
#include "kernel/yosys.h"
21+
#include "kernel/sigtools.h"
22+
23+
USING_YOSYS_NAMESPACE
24+
PRIVATE_NAMESPACE_BEGIN
25+
26+
bool did_something;
27+
28+
#include "passes/silimate/peepopt_expand.h"
29+
30+
struct OptExpandPass : public Pass {
31+
OptExpandPass() : Pass("opt_expand", "expand conjunction") { }
32+
void help() override
33+
{
34+
// |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
35+
log("\n");
36+
log(" opt_expand [selection]\n");
37+
log("\n");
38+
log("This pass expands conjunction (AND) operations into disjunction (OR).\n");
39+
log("\n");
40+
log("y = (a | b) & c ===> y = (a & c) | (b & c)\n");
41+
log("\n");
42+
log(" -max_iters n\n");
43+
log(" max number of pass iterations to run.\n");
44+
log("\n");
45+
}
46+
void execute(std::vector<std::string> args, RTLIL::Design *design) override
47+
{
48+
log_header(design, "Executing OPT_EXPAND pass (expand conjunction into disjunction).\n");
49+
50+
size_t argidx;
51+
int max_iters = 10000;
52+
for (argidx = 1; argidx < args.size(); argidx++) {
53+
// No extra arguments
54+
if (args[argidx] == "-max_iters" && argidx + 1 < args.size()) {
55+
max_iters = std::stoi(args[++argidx]);
56+
continue;
57+
}
58+
break;
59+
}
60+
extra_args(args, argidx, design);
61+
for (auto module : design->selected_modules())
62+
{
63+
did_something = true;
64+
for (int i = 0; did_something && i < max_iters; i++)
65+
{
66+
log("ITERATION OF OPT_EXPAND\n");
67+
did_something = false;
68+
peepopt_pm pm(module);
69+
pm.setup(module->selected_cells());
70+
pm.run_expand();
71+
}
72+
}
73+
}
74+
} PeepoptPass;
75+
76+
PRIVATE_NAMESPACE_END

passes/silimate/peepopt_expand.pmg

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
pattern expand
2+
//
3+
// Authored by Akash Levy of Silimate, Inc. under ISC license.
4+
//
5+
// Expand logical conjunction (&) across (|)
6+
//
7+
// y = (a | b) & c ===> y = (a & c) | (b & c)
8+
//
9+
10+
state <SigSpec> and_a and_b and_y or_a or_b or_y
11+
12+
match or_gate
13+
// Select OR gate
14+
select or_gate->type.in($or, $_OR_)
15+
set or_a port(or_gate, \A)
16+
set or_b port(or_gate, \B)
17+
set or_y port(or_gate, \Y)
18+
endmatch
19+
20+
code
21+
// Fanout of each OR gate Y bit should be 1 (no bit-split)
22+
if (nusers(or_y) != 2)
23+
reject;
24+
endcode
25+
26+
match and_gate
27+
// Select AND gate
28+
select and_gate->type.in($and, $_AND_)
29+
30+
// Set ports, allowing A and B to be swapped
31+
choice <IdString> A {\A, \B}
32+
define <IdString> B (A == \A ? \B : \A)
33+
set and_a port(and_gate, A)
34+
set and_b port(and_gate, B)
35+
set and_y port(and_gate, \Y)
36+
37+
// Connection
38+
index <SigSpec> port(and_gate, A) === or_y
39+
endmatch
40+
41+
code and_a and_b and_y or_a or_b or_y
42+
// Unset all ports
43+
and_gate->unsetPort(\A);
44+
and_gate->unsetPort(\B);
45+
and_gate->unsetPort(\Y);
46+
or_gate->unsetPort(\A);
47+
or_gate->unsetPort(\B);
48+
or_gate->unsetPort(\Y);
49+
50+
// Create new intermediate wires
51+
Cell *cell = and_gate;
52+
Wire *new_or_a = module->addWire(NEW_ID2, GetSize(and_y));
53+
Wire *new_or_b = module->addWire(NEW_ID2, GetSize(and_y));
54+
55+
// Create new AND gates connected to the OR gate
56+
module->addAnd(NEW_ID2, or_a, and_b, new_or_a, false, cell->get_src_attribute());
57+
module->addAnd(NEW_ID2, or_b, and_b, new_or_b, false, cell->get_src_attribute());
58+
59+
// Update OR gate ports
60+
or_gate->setPort(\A, new_or_a);
61+
or_gate->setPort(\B, new_or_b);
62+
or_gate->setPort(\Y, and_y);
63+
64+
// Rename OR gate for formal
65+
cell = or_gate;
66+
module->rename(or_gate, NEW_ID2);
67+
68+
// Remove AND gate
69+
autoremove(and_gate);
70+
71+
// Log, fixup, accept
72+
log("expand pattern in %s: and=%s, or=%s\n", log_id(module), log_id(and_gate), log_id(or_gate));
73+
did_something = true;
74+
accept;
75+
endcode

0 commit comments

Comments
 (0)