SILICON
Loading...
Searching...
No Matches
silicon_structure.cpp
Go to the documentation of this file.
1/* Recover graphical gate and mux structures while the design is still RTLIL. */
2#include "kernel/rtlil.h"
3#include "kernel/sigtools.h"
4#include "kernel/yosys.h"
5
6#include <algorithm>
7#include <optional>
8#include <vector>
9
10USING_YOSYS_NAMESPACE
12
13namespace {
14IdString id(const char* name)
15{
16 return RTLIL::escape_id(name);
17}
18
19struct Topology {
20 SigMap signals;
21 dict<SigSpec, Cell*> producers;
22 dict<SigBit, int> references;
23 pool<SigBit> kept;
24
25 explicit Topology(Module* module) : signals(module)
26 {
27 for (auto* wire : module->wires())
28 if (wire->get_bool_attribute(ID::keep))
29 for (auto bit : signals(SigSpec(wire)))
30 kept.insert(bit);
31 for (auto name : module->ports)
32 for (auto bit : signals(SigSpec(module->wire(name))))
33 ++references[bit];
34 for (auto* cell : module->cells()) {
35 if (cell->hasPort(ID::Y))
36 producers[signals(cell->getPort(ID::Y))] = cell;
37 for (const auto& connection : cell->connections())
38 for (auto bit : signals(connection.second))
39 ++references[bit];
40 }
41 }
42
43 bool privateOutput(Cell* cell, const SigSpec& output)
44 {
45 if (cell->get_bool_attribute(ID::keep))
46 return false;
47 for (auto bit : signals(output))
48 // One driver reference and exactly one input reference. Ports, bit taps
49 // and repeated uses all prevent absorption, including unselected users.
50 if (!bit.wire || references[bit] != 2 || kept.count(bit))
51 return false;
52 return true;
53 }
54};
55
56struct Gate {
57 int mode, width;
58 bool bitwise;
59 std::vector<SigSpec> inputs;
60 SigSpec output;
61};
62
63std::optional<Gate> gate(Cell* cell, SigMap& signals)
64{
65 Gate result;
66 if (cell->type == ID($and) || cell->type == ID($or)) {
67 result.mode = cell->type == ID($and) ? 0 : 1;
68 result.width = cell->getParam(ID::Y_WIDTH).as_int();
69 result.bitwise = true;
70 if (cell->getParam(ID::A_WIDTH).as_int() != result.width
71 || cell->getParam(ID::B_WIDTH).as_int() != result.width)
72 return std::nullopt;
73 result.inputs = {signals(cell->getPort(ID::A)), signals(cell->getPort(ID::B))};
74 } else if (cell->type == id("SILICON_LOGIC")) {
75 result.mode = cell->getParam(id("MODE")).as_int();
76 if (result.mode != 0 && result.mode != 1)
77 return std::nullopt;
78 result.width = 1;
79 result.bitwise = false;
80 result.inputs = {signals(cell->getPort(ID::A)), signals(cell->getPort(ID::B))};
81 } else if (cell->type == id("SILICON_GATE")) {
82 result.mode = cell->getParam(id("MODE")).as_int();
83 result.width = cell->getParam(ID::WIDTH).as_int();
84 result.bitwise = cell->getParam(id("BITWISE")).as_bool();
85 const int count = cell->getParam(id("INPUTS")).as_int();
86 if (count < 2 || count > 65535 || result.mode < 0 || result.mode > 1)
87 return std::nullopt;
88 for (int i = 0; i < count; ++i)
89 result.inputs.push_back(
90 signals(cell->getPort(RTLIL::escape_id("A" + std::to_string(i)))));
91 } else
92 return std::nullopt;
93 result.output = signals(cell->getPort(ID::Y));
94 if (result.width < 1 || result.width > 65535 || GetSize(result.output) != result.width
95 || (!result.bitwise && result.width != 1))
96 return std::nullopt;
97 for (const auto& input : result.inputs)
98 if (input.empty() || (result.bitwise && GetSize(input) != result.width))
99 return std::nullopt;
100 return result;
101}
102
103void foldGates(Module* module)
104{
105 // Process reads are not included in the reference count.
106 if (!module->processes.empty()) {
107 log_warning("SILICON: run proc before silicon_gates on module %s.\n", log_id(module));
108 return;
109 }
110 Topology topology(module);
111 dict<Cell*, Gate> shapes;
112 pool<Cell*> removed, hasParent;
113 std::vector<Cell*> roots;
114 // Decode the original graph once. Never build a gate for a private prefix:
115 // only sinks of compatible private edges can own a maximal folding group.
116 for (auto* cell : module->selected_cells()) {
117 if (cell->get_bool_attribute(ID::keep))
118 continue;
119 auto shape = gate(cell, topology.signals);
120 if (shape)
121 shapes.emplace(cell, std::move(*shape));
122 }
123 const auto compatible = [](const Gate& parent, const Gate& child) {
124 return parent.mode == child.mode && parent.width == child.width
125 && parent.bitwise == child.bitwise;
126 };
127 for (const auto& entry : shapes) {
128 const auto& parent = entry.second;
129 for (const auto& input : parent.inputs) {
130 auto found = topology.producers.find(input);
131 if (found == topology.producers.end()
132 || !topology.privateOutput(found->second, input))
133 continue;
134 auto child = shapes.find(found->second);
135 if (child != shapes.end() && compatible(parent, child->second))
136 hasParent.insert(child->first);
137 }
138 }
139 for (const auto& entry : shapes)
140 if (!hasParent.count(entry.first))
141 roots.push_back(entry.first);
142 std::sort(roots.begin(), roots.end(),
143 [](Cell* a, Cell* b) { return a->name < b->name; });
144 // Private edges give each child at most one parent. Thus these traversals
145 // are disjoint and linear in the original graph plus final gate inputs.
146 // Compatible cycles have no root and are deliberately left untouched.
147 for (auto* root : roots) {
148 const Gate* shape = &shapes.at(root);
149 std::vector<SigSpec> pending(shape->inputs.rbegin(), shape->inputs.rend()), inputs;
150 pool<Cell*> absorbed;
151 pool<SigBit> rootOutputs;
152 for (auto bit : shape->output)
153 rootOutputs.insert(bit);
154 bool acyclic = true;
155 while (!pending.empty()) {
156 auto input = pending.back();
157 pending.pop_back();
158 for (auto bit : input)
159 if (rootOutputs.count(bit))
160 acyclic = false;
161 if (!acyclic)
162 break;
163 auto found = topology.producers.find(input);
164 Cell* child = found == topology.producers.end() ? nullptr : found->second;
165 if (child == root) {
166 acyclic = false;
167 break;
168 }
169 const auto childEntry = shapes.find(child);
170 const Gate* childShape =
171 childEntry != shapes.end() && topology.privateOutput(child, input)
172 ? &childEntry->second
173 : nullptr;
174 if (!childShape || !compatible(*shape, *childShape)) {
175 inputs.push_back(input);
176 continue;
177 }
178 if (!absorbed.insert(child).second) {
179 acyclic = false;
180 break;
181 }
182 pending.insert(pending.end(), childShape->inputs.rbegin(),
183 childShape->inputs.rend());
184 }
185 if (!acyclic || absorbed.empty() || GetSize(inputs) > 65535)
186 continue;
187 root->type = id("SILICON_GATE");
188 root->parameters.clear();
189 root->connections_.clear();
190 root->setParam(id("MODE"), shape->mode);
191 root->setParam(ID::WIDTH, shape->width);
192 root->setParam(id("BITWISE"), shape->bitwise ? 1 : 0);
193 root->setParam(id("INPUTS"), GetSize(inputs));
194 for (int i = 0; i < GetSize(inputs); ++i)
195 root->setPort(RTLIL::escape_id("A" + std::to_string(i)), inputs[i]);
196 root->setPort(ID::Y, shape->output);
197 for (auto* child : absorbed)
198 removed.insert(child);
199 }
200 for (auto* cell : removed)
201 module->remove(cell);
202}
203
204struct Mux {
205 int width;
207 std::vector<SigSpec> lanes;
208};
209
210std::optional<Mux> mux(Cell* cell, SigMap& signals)
211{
212 if (cell->type != ID($mux) && cell->type != ID($bmux))
213 return std::nullopt;
214 Mux result;
215 result.width = cell->getParam(ID::WIDTH).as_int();
216 result.selection = signals(cell->getPort(ID::S));
217 result.output = signals(cell->getPort(ID::Y));
218 const int bits = GetSize(result.selection);
219 if (result.width < 1 || result.width > 65535 || bits < 1 || bits > 15
220 || GetSize(result.output) != result.width)
221 return std::nullopt;
222 if (cell->type == ID($mux)) {
223 if (bits != 1)
224 return std::nullopt;
225 result.lanes = {signals(cell->getPort(ID::A)), signals(cell->getPort(ID::B))};
226 } else {
227 auto data = signals(cell->getPort(ID::A));
228 if (GetSize(data) != result.width * (1 << bits))
229 return std::nullopt;
230 for (int i = 0; i < 1 << bits; ++i)
231 result.lanes.push_back(data.extract(i * result.width, result.width));
232 }
233 for (const auto& lane : result.lanes)
234 if (GetSize(lane) != result.width)
235 return std::nullopt;
236 return result;
237}
238
239void foldMuxes(Module* module)
240{
241 if (!module->processes.empty()) {
242 log_warning("SILICON: run proc before silicon_mux on module %s.\n",
243 log_id(module));
244 return;
245 }
246 bool changed;
247 do {
248 changed = false;
249 Topology topology(module);
250 pool<Cell*> selected, removed;
251 std::vector<Cell*> roots;
252 for (auto* cell : module->selected_cells()) {
253 selected.insert(cell);
254 roots.push_back(cell);
255 }
256 for (auto* root : roots) {
257 if (removed.count(root) || root->get_bool_attribute(ID::keep))
258 continue;
259 auto shape = mux(root, topology.signals);
260 if (!shape)
261 continue;
262 std::vector<Mux> children;
263 pool<Cell*> absorbed;
264 for (const auto& lane : shape->lanes) {
265 auto found = topology.producers.find(lane);
266 Cell* child = found == topology.producers.end() ? nullptr : found->second;
267 auto childShape =
268 child && selected.count(child) && !removed.count(child)
269 && topology.privateOutput(child, lane)
270 ? mux(child, topology.signals)
271 : std::nullopt;
272 if (!childShape || child == root || childShape->width != shape->width
273 || !absorbed.insert(child).second
274 || (!children.empty() && childShape->selection != children.front().selection))
275 break;
276 children.push_back(*childShape);
277 }
278 if (children.size() != shape->lanes.size()
279 || GetSize(shape->selection) + GetSize(children.front().selection) > 8)
280 continue;
281 SigSpec data, selection = shape->selection;
282 selection.append(
283 children.front().selection); // root bits first, including X/E precedence
284 pool<SigBit> output;
285 for (auto bit : shape->output)
286 output.insert(bit);
287 bool feedback = false;
288 for (auto bit : selection)
289 feedback |= output.count(bit);
290 for (int lane = 0; lane < GetSize(children.front().lanes); ++lane)
291 for (const auto& child : children) {
292 data.append(child.lanes[lane]);
293 for (auto bit : child.lanes[lane])
294 feedback |= output.count(bit);
295 }
296 if (feedback)
297 continue;
298 root->type = ID($bmux);
299 root->parameters.clear();
300 root->connections_.clear();
301 root->setParam(ID::WIDTH, shape->width);
302 root->setParam(ID::S_WIDTH, GetSize(selection));
303 root->setPort(ID::A, data);
304 root->setPort(ID::S, selection);
305 root->setPort(ID::Y, shape->output);
306 for (auto* child : absorbed)
307 removed.insert(child);
308 changed = true;
309 }
310 for (auto* cell : removed)
311 module->remove(cell);
312 } while (changed);
313}
314
315void compactMuxes(Module* module)
316{
317 if (!module->processes.empty())
318 return;
319 SigMap signals(module);
320 for (auto* cell : module->selected_cells()) {
321 if (cell->get_bool_attribute(ID::keep))
322 continue;
323 const auto shape = mux(cell, signals);
324 if (!shape)
325 continue;
326 dict<SigSpec, int> indices;
327 SigSpec data;
328 std::vector<RTLIL::State> table;
329 for (const auto& lane : shape->lanes) {
330 auto entry = indices.find(lane);
331 int index;
332 if (entry == indices.end()) {
333 index = GetSize(indices);
334 indices[lane] = index;
335 data.append(lane);
336 } else
337 index = entry->second;
338 for (int bit = 0; bit < GetSize(shape->selection); ++bit)
339 table.push_back(index & (1 << bit) ? RTLIL::State::S1 : RTLIL::State::S0);
340 }
341 if (GetSize(indices) == GetSize(shape->lanes))
342 continue;
343 cell->type = id("SILICON_MUX");
344 cell->parameters.clear();
345 cell->connections_.clear();
346 cell->setParam(ID::WIDTH, shape->width);
347 cell->setParam(ID::S_WIDTH, GetSize(shape->selection));
348 cell->setParam(id("INPUTS"), GetSize(indices));
349 cell->setParam(id("TABLE"), RTLIL::Const(std::move(table)));
350 cell->setPort(ID::A, data);
351 cell->setPort(ID::S, shape->selection);
352 cell->setPort(ID::Y, shape->output);
353 }
354}
355} // namespace
356
357struct SiliconGatesPass : Pass {
358 SiliconGatesPass() : Pass("silicon_gates", "recover gates with multiple inputs") {}
359 void execute(std::vector<std::string> args, RTLIL::Design* design) override
360 {
361 extra_args(args, 1, design);
362 for (auto* module : design->selected_modules())
363 foldGates(module);
364 }
366
367struct SiliconMuxPass : Pass {
368 SiliconMuxPass() : Pass("silicon_mux", "recover mux trees and compact repeated choices")
369 {
370 }
371 void execute(std::vector<std::string> args, RTLIL::Design* design) override
372 {
373 extra_args(args, 1, design);
374 for (auto* module : design->selected_modules()) {
375 foldMuxes(module);
376 compactMuxes(module);
377 }
378 }
380
381PRIVATE_NAMESPACE_END
std::optional< Gate > gate(Cell *cell, SigMap &signals)
std::optional< Mux > mux(Cell *cell, SigMap &signals)
void compactMuxes(Module *module)
RTLIL::IdString id(const char *name)
void foldGates(Module *module)
void foldMuxes(Module *module)
bool privateOutput(Cell *cell, const SigSpec &output)
void execute(std::vector< std::string > args, RTLIL::Design *design) override
void execute(std::vector< std::string > args, RTLIL::Design *design) override