3
0
Fork 0
mirror of https://github.com/YosysHQ/yosys synced 2026-05-25 11:26:22 +00:00

patch: source transfer

This commit is contained in:
Emil J. Tywoniak 2026-05-19 19:31:16 +02:00
parent db1c1d4359
commit 9f22b9d2a0
5 changed files with 68 additions and 6 deletions

View file

@ -54,7 +54,54 @@ RTLIL::Wire *RTLIL::Patch::addWire(RTLIL::IdString name, const RTLIL::Wire *othe
return wire;
}
void Patch::collect_src(Cell* old_cell) {
src.insert(old_cell->get_src_attribute());
log("collect %s\n", old_cell->name);
std::vector<Cell*> inputs = {};
for (auto [port_name, sig] : old_cell->connections()) {
auto dir = old_cell->port_dir(port_name);
log_assert(dir != PD_UNKNOWN);
log_assert(!sig.size() || sig.is_wire());
if (dir == PD_INPUT || dir == PD_INOUT) {
Wire* in_wire = sig.as_wire();
if (!leaves.count(in_wire))
inputs.push_back(in_wire->driverCell());
}
}
for (auto input : inputs)
collect_src(input);
}
void Patch::gc(Cell* old_cell) {
log("gc %s\n", old_cell->name);
std::vector<Cell*> inputs = {};
for (auto [port_name, sig] : old_cell->connections()) {
auto dir = old_cell->port_dir(port_name);
log_assert(dir != PD_UNKNOWN);
log_assert(!sig.size() || sig.is_wire());
if (dir == PD_OUTPUT || dir == PD_INOUT) {
if (sig.size()) {
for (auto bit : sig) {
// Reject GC if used
if (!mod->fanout(bit).empty())
return;
}
}
}
if (dir == PD_INPUT || dir == PD_INOUT) {
Wire* in_wire = sig.as_wire();
if (!leaves.count(in_wire))
inputs.push_back(in_wire->driverCell());
}
}
for (auto input : inputs)
gc(input);
}
void Patch::patch(Cell* old_cell, Cell* new_cell) {
log_assert(!leaves.empty());
collect_src(old_cell);
std::string src_str = AttrObject::strpool_attribute_to_str(src);
for (auto& wire: wires_) {
wire->module = mod;
Wire* raw = wire.release();
@ -64,6 +111,7 @@ void Patch::patch(Cell* old_cell, Cell* new_cell) {
log_cell(old_cell);
for (auto& cell: cells_) {
log_cell(cell.get());
cell->set_src_attribute(src_str);
Cell* raw = cell.release();
mod->cells_[raw->name] = raw;
for (auto [port_name, sig] : raw->connections()) {
@ -91,6 +139,7 @@ void Patch::patch(Cell* old_cell, Cell* new_cell) {
raw->fixup_parameters();
}
log_module(mod, "");
gc(old_cell);
}

View file

@ -11,6 +11,10 @@ struct RTLIL::Patch final : public CellAdderMixin<RTLIL::Patch>
Hasher::hash_t hashidx_;
[[nodiscard]] Hasher hash_into(Hasher h) const { h.eat(hashidx_); return h; }
private:
void collect_src(Cell* old_cell);
void gc(Cell* old_cell);
protected:
void add(RTLIL::Wire *wire);
void add(RTLIL::Cell *cell);
@ -18,12 +22,14 @@ protected:
public:
Module *mod;
SigMap map;
// SigMap map;
vector<std::unique_ptr<Wire>> wires_;
vector<std::unique_ptr<Cell>> cells_;
Cell* root;
pool<Wire*> leaves;
vector<RTLIL::SigSig> connections_;
// vector<RTLIL::SigSig> connections_;
pool<string> src;
void connect(const RTLIL::SigSig &conn);
void connect(const RTLIL::SigSpec &lhs, const RTLIL::SigSpec &rhs);