mirror of https://github.com/YosysHQ/yosys.git
extract_reduce: Refactor and fix input signal construction.
Fixes #3047.
This commit is contained in:
parent
a0e9d9fef9
commit
e64456f920
|
@ -224,89 +224,60 @@ struct ExtractReducePass : public Pass
|
||||||
if(consumed_cells.count(head_cell))
|
if(consumed_cells.count(head_cell))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
pool<Cell*> cur_supercell;
|
dict<SigBit, int> sources;
|
||||||
|
int inner_cells = 0;
|
||||||
std::deque<Cell*> bfs_queue = {head_cell};
|
std::deque<Cell*> bfs_queue = {head_cell};
|
||||||
while (bfs_queue.size())
|
while (bfs_queue.size())
|
||||||
{
|
{
|
||||||
Cell* x = bfs_queue.front();
|
Cell* x = bfs_queue.front();
|
||||||
bfs_queue.pop_front();
|
bfs_queue.pop_front();
|
||||||
|
|
||||||
cur_supercell.insert(x);
|
for (auto port: {ID::A, ID::B}) {
|
||||||
|
auto bit = sigmap(x->getPort(port)[0]);
|
||||||
|
|
||||||
auto a = sigmap(x->getPort(ID::A));
|
bool sink_single = sig_to_sink[bit].size() == 1 && !port_sigs.count(bit);
|
||||||
log_assert(a.size() == 1);
|
|
||||||
|
|
||||||
// Must have only one sink unless we're going off chain
|
Cell* drv = sig_to_driver[bit];
|
||||||
// XXX: Check that it is indeed this node?
|
bool drv_ok = drv && drv->type == head_cell->type;
|
||||||
if( allow_off_chain || (sig_to_sink[a[0]].size() + port_sigs.count(a[0]) == 1) )
|
|
||||||
{
|
|
||||||
Cell* cell_a = sig_to_driver[a[0]];
|
|
||||||
if(cell_a && IsRightType(cell_a, gt))
|
|
||||||
{
|
|
||||||
// The cell here is the correct type, and it's definitely driving
|
|
||||||
// this current cell.
|
|
||||||
bfs_queue.push_back(cell_a);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
auto b = sigmap(x->getPort(ID::B));
|
if (drv_ok && (allow_off_chain || sink_single)) {
|
||||||
log_assert(b.size() == 1);
|
inner_cells++;
|
||||||
|
bfs_queue.push_back(drv);
|
||||||
// Must have only one sink
|
} else {
|
||||||
// XXX: Check that it is indeed this node?
|
sources[bit]++;
|
||||||
if( allow_off_chain || (sig_to_sink[b[0]].size() + port_sigs.count(b[0]) == 1) )
|
|
||||||
{
|
|
||||||
Cell* cell_b = sig_to_driver[b[0]];
|
|
||||||
if(cell_b && IsRightType(cell_b, gt))
|
|
||||||
{
|
|
||||||
// The cell here is the correct type, and it's definitely driving only
|
|
||||||
// this current cell.
|
|
||||||
bfs_queue.push_back(cell_b);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
log(" Cells:\n");
|
if (inner_cells)
|
||||||
for (auto x : cur_supercell)
|
|
||||||
log(" %s\n", x->name.c_str());
|
|
||||||
|
|
||||||
if (cur_supercell.size() > 1)
|
|
||||||
{
|
{
|
||||||
// Worth it to create reduce cell
|
// Worth it to create reduce cell
|
||||||
log(" Creating $reduce_* cell!\n");
|
log(" Creating $reduce_* cell!\n");
|
||||||
|
|
||||||
pool<SigBit> input_pool;
|
|
||||||
pool<SigBit> input_pool_intermed;
|
|
||||||
for (auto x : cur_supercell)
|
|
||||||
{
|
|
||||||
input_pool.insert(sigmap(x->getPort(ID::A))[0]);
|
|
||||||
input_pool.insert(sigmap(x->getPort(ID::B))[0]);
|
|
||||||
input_pool_intermed.insert(sigmap(x->getPort(ID::Y))[0]);
|
|
||||||
}
|
|
||||||
SigSpec input;
|
|
||||||
for (auto b : input_pool)
|
|
||||||
if (input_pool_intermed.count(b) == 0)
|
|
||||||
input.append(b);
|
|
||||||
|
|
||||||
SigBit output = sigmap(head_cell->getPort(ID::Y)[0]);
|
SigBit output = sigmap(head_cell->getPort(ID::Y)[0]);
|
||||||
|
|
||||||
auto new_reduce_cell = module->addCell(NEW_ID,
|
SigSpec input;
|
||||||
gt == GateType::And ? ID($reduce_and) :
|
for (auto it : sources) {
|
||||||
gt == GateType::Or ? ID($reduce_or) :
|
bool cond;
|
||||||
gt == GateType::Xor ? ID($reduce_xor) : "");
|
if (head_cell->type == ID($_XOR_))
|
||||||
new_reduce_cell->setParam(ID::A_SIGNED, 0);
|
cond = it.second & 1;
|
||||||
new_reduce_cell->setParam(ID::A_WIDTH, input.size());
|
else
|
||||||
new_reduce_cell->setParam(ID::Y_WIDTH, 1);
|
cond = it.second != 0;
|
||||||
new_reduce_cell->setPort(ID::A, input);
|
if (cond)
|
||||||
new_reduce_cell->setPort(ID::Y, output);
|
input.append(it.first);
|
||||||
|
|
||||||
if(allow_off_chain)
|
|
||||||
consumed_cells.insert(head_cell);
|
|
||||||
else
|
|
||||||
{
|
|
||||||
for (auto x : cur_supercell)
|
|
||||||
consumed_cells.insert(x);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (head_cell->type == ID($_AND_)) {
|
||||||
|
module->addReduceAnd(NEW_ID, input, output);
|
||||||
|
} else if (head_cell->type == ID($_OR_)) {
|
||||||
|
module->addReduceOr(NEW_ID, input, output);
|
||||||
|
} else if (head_cell->type == ID($_XOR_)) {
|
||||||
|
module->addReduceXor(NEW_ID, input, output);
|
||||||
|
} else {
|
||||||
|
log_assert(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
consumed_cells.insert(head_cell);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,12 @@
|
||||||
|
read_verilog << EOT
|
||||||
|
|
||||||
|
module test (A, B, C, D, Y);
|
||||||
|
input A, B, C, D;
|
||||||
|
output Y;
|
||||||
|
assign Y = A^B^C^D^A;
|
||||||
|
endmodule
|
||||||
|
|
||||||
|
EOT
|
||||||
|
|
||||||
|
techmap
|
||||||
|
equiv_opt -assert extract_reduce
|
Loading…
Reference in New Issue