SILICON
Loading...
Searching...
No Matches
silicon_legalize.cpp
Go to the documentation of this file.
1/* SILICON's word-level RTLIL legalization and capability boundary. */
2#include "kernel/rtlil.h"
3#include "kernel/yosys.h"
4
5#include <algorithm>
6#include <limits>
7#include <string>
8#include <vector>
9
10USING_YOSYS_NAMESPACE
12
13namespace {
14RTLIL::IdString id(const char* name) { return RTLIL::escape_id(name); }
15
16int width(RTLIL::Module* module, RTLIL::Cell* cell, RTLIL::IdString key)
17{
18 if (!cell->hasParam(key))
19 log_error("SILICON: missing %s in module %s cell %s (%s).\n", log_id(key),
20 log_id(module), log_id(cell), log_id(cell->type));
21 int result = cell->getParam(key).as_int();
22 if (result <= 0 || result > std::numeric_limits<unsigned short>::max())
23 log_error("SILICON: invalid %s in module %s cell %s (%s): %d.\n", log_id(key),
24 log_id(module), log_id(cell), log_id(cell->type), result);
25 return result;
26}
27
28bool flag(RTLIL::Module* module, RTLIL::Cell* cell, RTLIL::IdString key)
29{
30 if (!cell->hasParam(key))
31 log_error("SILICON: missing %s in module %s cell %s (%s).\n", log_id(key),
32 log_id(module), log_id(cell), log_id(cell->type));
33 auto value = cell->getParam(key);
34 if (!value.is_fully_def() || (value.as_int() != 0 && value.as_int() != 1))
35 log_error("SILICON: invalid %s in module %s cell %s (%s).\n", log_id(key),
36 log_id(module), log_id(cell), log_id(cell->type));
37 return value.as_int() == 1;
38}
39
40RTLIL::SigSpec port(RTLIL::Module* module, RTLIL::Cell* cell, RTLIL::IdString key,
41 int expected)
42{
43 auto value = cell->getPort(key);
44 if (GetSize(value) != expected)
45 log_error("SILICON: invalid %s width in module %s cell %s (%s): expected %d, got %d.\n",
46 log_id(key), log_id(module), log_id(cell), log_id(cell->type), expected,
47 GetSize(value));
48 return value;
49}
50
51void narrowTruth(RTLIL::Module* module, RTLIL::Cell* cell, int yWidth)
52{
53 auto y = port(module, cell, ID::Y, yWidth);
54 if (yWidth > 1) {
55 module->connect(y.extract(1, yWidth - 1), RTLIL::SigSpec(RTLIL::State::S0, yWidth - 1));
56 cell->setPort(ID::Y, y.extract(0, 1));
57 }
58}
59
60void normalizeOperand(RTLIL::Module* module, RTLIL::Cell* cell, RTLIL::IdString key,
61 int originalWidth, int targetWidth, bool signExtend)
62{
63 auto signal = port(module, cell, key, originalWidth);
64 signal.extend_u0(targetWidth, signExtend);
65 cell->setPort(key, signal);
66}
67
68void runLogic(RTLIL::Design* design)
69{
70 for (auto* module : design->selected_modules())
71 for (auto* cell : module->selected_cells()) {
72 int mode = cell->type == ID($logic_and) ? 0 : cell->type == ID($logic_or) ? 1
73 : cell->type == ID($logic_not) ? 2 : -1;
74 if (mode < 0) continue;
75 int aWidth = width(module, cell, ID::A_WIDTH);
76 int yWidth = width(module, cell, ID::Y_WIDTH);
77 (void)flag(module, cell, ID::A_SIGNED);
78 port(module, cell, ID::A, aWidth);
79 if (mode != 2) {
80 int bWidth = width(module, cell, ID::B_WIDTH);
81 (void)flag(module, cell, ID::B_SIGNED);
82 port(module, cell, ID::B, bWidth);
83 cell->setParam(ID::B_WIDTH, bWidth);
84 }
85 narrowTruth(module, cell, yWidth);
86 cell->parameters.clear();
87 cell->setParam(id("MODE"), mode);
88 cell->setParam(ID::A_WIDTH, aWidth);
89 if (mode != 2) cell->setParam(ID::B_WIDTH, GetSize(cell->getPort(ID::B)));
90 cell->type = id("SILICON_LOGIC");
91 }
92}
93
94void runCompare(RTLIL::Design* design)
95{
96 for (auto* module : design->selected_modules())
97 for (auto* cell : module->selected_cells()) {
98 bool invert = cell->type == ID($ne);
99 int mode = (cell->type == ID($eq) || invert) ? 0 : cell->type == ID($lt) ? 1
100 : cell->type == ID($le) ? 2 : cell->type == ID($gt) ? 3
101 : cell->type == ID($ge) ? 4 : -1;
102 if (mode < 0) continue;
103 int aWidth = width(module, cell, ID::A_WIDTH);
104 int bWidth = width(module, cell, ID::B_WIDTH);
105 int yWidth = width(module, cell, ID::Y_WIDTH);
106 bool aSigned = flag(module, cell, ID::A_SIGNED);
107 bool bSigned = flag(module, cell, ID::B_SIGNED);
108 bool sign = aSigned && bSigned;
109 int targetWidth = std::max(aWidth, bWidth);
110 normalizeOperand(module, cell, ID::A, aWidth, targetWidth, sign);
111 normalizeOperand(module, cell, ID::B, bWidth, targetWidth, sign);
112 narrowTruth(module, cell, yWidth);
113 if (invert) {
114 RTLIL::SigSpec equal(module->addWire(NEW_ID));
115 module->addNot(NEW_ID, equal, cell->getPort(ID::Y));
116 cell->setPort(ID::Y, equal);
117 }
118 cell->parameters.clear();
119 cell->setParam(ID::WIDTH, targetWidth);
120 cell->setParam(id("MODE"), mode);
121 cell->setParam(id("SIGNED"), sign ? 1 : 0);
122 cell->type = id("SILICON_COMPARE");
123 }
124}
125
126// Unlike logical shifts, dynamic selections fill out-of-range bits with X.
127// Keep the muxes word-wide and collapse all excessive shift bits into one test.
128RTLIL::SigSpec selectionShift(RTLIL::Module* module, RTLIL::SigSpec value,
129 const RTLIL::SigSpec& amount, bool left,
130 RTLIL::Cell*& finalMux)
131{
132 const int dataWidth = GetSize(value);
133 int bit = 0;
134 for (int distance = 1; bit < GetSize(amount) && distance < dataWidth;
135 ++bit, distance *= 2) {
136 RTLIL::SigSpec shifted;
137 if (left) {
138 shifted = RTLIL::SigSpec(RTLIL::State::Sx, distance);
139 shifted.append(value.extract(0, dataWidth - distance));
140 } else {
141 shifted = value.extract(distance, dataWidth - distance);
142 shifted.append(RTLIL::SigSpec(RTLIL::State::Sx, distance));
143 }
144 RTLIL::SigSpec next(module->addWire(NEW_ID, dataWidth));
145 finalMux = module->addMux(NEW_ID, value, shifted, amount[bit], next);
146 value = next;
147 }
148 if (bit < GetSize(amount)) {
149 RTLIL::SigSpec overflow(module->addWire(NEW_ID));
150 module->addReduceOr(NEW_ID, amount.extract(bit, GetSize(amount) - bit), overflow);
151 RTLIL::SigSpec next(module->addWire(NEW_ID, dataWidth));
152 finalMux = module->addMux(NEW_ID, value, RTLIL::SigSpec(RTLIL::State::Sx, dataWidth),
153 overflow, next);
154 value = next;
155 }
156 return value;
157}
158
159void lowerSelection(RTLIL::Module* module, RTLIL::Cell* cell)
160{
161 const int aWidth = width(module, cell, ID::A_WIDTH);
162 const int bWidth = width(module, cell, ID::B_WIDTH);
163 const int yWidth = width(module, cell, ID::Y_WIDTH);
164 (void)flag(module, cell, ID::A_SIGNED);
165 const bool signedOffset = flag(module, cell, ID::B_SIGNED);
166 auto a = port(module, cell, ID::A, aWidth);
167 const auto b = port(module, cell, ID::B, bWidth);
168 const auto y = port(module, cell, ID::Y, yWidth);
169 // Pad before shifting so negative offsets can move input bits into a wider Y.
170 if (yWidth > aWidth)
171 a.append(RTLIL::SigSpec(RTLIL::State::Sx, yWidth - aWidth));
172 RTLIL::Cell* finalMux = nullptr;
173 auto result = selectionShift(module, a, b, false, finalMux);
174 if (signedOffset) {
175 RTLIL::SigSpec magnitude(module->addWire(NEW_ID, bWidth));
176 module->addNeg(NEW_ID, b, magnitude);
177 const auto negative = selectionShift(module, a, magnitude, true, finalMux);
178 RTLIL::SigSpec selected(module->addWire(NEW_ID, GetSize(a)));
179 finalMux = module->addMux(NEW_ID, result, negative, b[bWidth - 1], selected);
180 result = selected;
181 }
182 // Retain the final computation and its dependency cone, without protecting
183 // every intermediate or spreading unrelated source attributes.
184 if (cell->get_bool_attribute(ID::keep)) {
185 log_assert(finalMux != nullptr);
186 finalMux->attributes[ID::keep] = cell->attributes.at(ID::keep);
187 }
188 module->connect(y, result.extract(0, yWidth));
189 module->remove(cell);
190}
191
192void runShift(RTLIL::Design* design)
193{
194 for (auto* module : design->selected_modules()) {
195 // Selection lowering adds cells, so iterate a snapshot of the selection.
196 const auto selected = module->selected_cells();
197 const std::vector<RTLIL::Cell*> cells(selected.begin(), selected.end());
198 for (auto* cell : cells) {
199 if (cell->type == ID($shiftx)) {
200 lowerSelection(module, cell);
201 continue;
202 }
203 int direction = cell->type == ID($shl) ? 0 :
204 (cell->type == ID($shr) || cell->type == ID($sshr)) ? 1 : -1;
205 if (direction < 0) continue;
206 bool arithmeticCell = cell->type == ID($sshr);
207 int aWidth = width(module, cell, ID::A_WIDTH);
208 int bWidth = width(module, cell, ID::B_WIDTH);
209 int yWidth = width(module, cell, ID::Y_WIDTH);
210 bool signedInput = flag(module, cell, ID::A_SIGNED);
211 if (flag(module, cell, ID::B_SIGNED))
212 log_error("SILICON: unsupported signed shift amount in module %s cell %s (%s).\n",
213 log_id(module), log_id(cell), log_id(cell->type));
214 normalizeOperand(module, cell, ID::A, aWidth, yWidth, signedInput);
215 port(module, cell, ID::B, bWidth);
216 port(module, cell, ID::Y, yWidth);
217 cell->parameters.clear();
218 cell->setParam(ID::WIDTH, yWidth);
219 cell->setParam(id("B_WIDTH"), bWidth);
220 cell->setParam(id("DIRECTION"), direction);
221 cell->setParam(id("ARITHMETIC"), arithmeticCell && signedInput ? 1 : 0);
222 cell->type = id("SILICON_SHIFT");
223 }
224 }
225}
226
227bool supported(RTLIL::IdString type)
228{
229 // clang-format off
230 static const pool<RTLIL::IdString> types = {
231 ID($and), ID($or), ID($xor), ID($not), ID($_NAND_), ID($_NOR_),
232 ID($pos), ID($add), ID($sub), ID($neg), ID($mux), ID($bmux), ID($demux),
233 ID($adffe),
234 id("SILICON_ROM"), id("SILICON_LOGIC"), id("SILICON_COMPARE"),
235 id("SILICON_SHIFT"), id("SILICON_DFF"), id("SILICON_DFFE"),
236 id("SILICON_GATE"), id("SILICON_MUX"),
237 id("SILICON_DLATCH"), id("SILICON_DFFSR"), id("SILICON_DFFSRE"),
238 id("SILICON_JKFF"), id("SILICON_HALF_ADDER"), id("SILICON_FULL_ADDER"),
239 id("SILICON_ADDER"), id("SILICON_MULTIPLIER"), id("SILICON_DIVIDER"),
240 id("SILICON_PIPO"), id("SILICON_PISO"),
241 id("SILICON_SIPO"), id("SILICON_SISO")};
242 // clang-format on
243 return types.count(type);
244}
245
246void runValidate(RTLIL::Design* design)
247{
248 for (auto* module : design->selected_modules())
249 for (auto* cell : module->selected_cells()) {
250 if (supported(cell->type) || design->module(cell->type)) continue;
251 log_error("SILICON: unsupported cell type %s in module %s cell %s.\n",
252 log_id(cell->type), log_id(module), log_id(cell));
253 }
254}
255}
256
257struct SiliconLogicPass : Pass {
258 SiliconLogicPass() : Pass("silicon_logic", "legalize word-level logical operators") {}
259 void execute(std::vector<std::string> args, RTLIL::Design* design) override {
260 extra_args(args, 1, design); runLogic(design);
261 }
263
264struct SiliconComparePass : Pass {
265 SiliconComparePass() : Pass("silicon_compare", "normalize comparison widths and signedness") {}
266 void execute(std::vector<std::string> args, RTLIL::Design* design) override {
267 extra_args(args, 1, design); runCompare(design);
268 }
270
271struct SiliconShiftPass : Pass {
272 SiliconShiftPass() : Pass("silicon_shift", "normalize shift widths and modes") {}
273 void execute(std::vector<std::string> args, RTLIL::Design* design) override {
274 extra_args(args, 1, design); runShift(design);
275 }
277
278struct SiliconValidatePass : Pass {
279 SiliconValidatePass() : Pass("silicon_validate", "reject cells outside SILICON's canonical subset") {}
280 void execute(std::vector<std::string> args, RTLIL::Design* design) override {
281 extra_args(args, 1, design); runValidate(design);
282 }
284
285struct SiliconLegalizePass : Pass {
286 SiliconLegalizePass() : Pass("silicon_legalize", "run SILICON's ordered legalization pipeline") {}
287 void execute(std::vector<std::string> args, RTLIL::Design* design) override {
288 if (args.size() != 2)
289 log_error("Usage: silicon_legalize <technology-map-path>\n");
290 Pass::call(design, "silicon_pmux_bmux");
291 Pass::call(design, "silicon_eq_decoder");
292 Pass::call(design, "pmuxtree");
293 Pass::call(design, "delete t:$scopeinfo");
294 Pass::call(design, "opt -nosdff");
295 Pass::call(design, "memory_dff");
296 Pass::call(design, "silicon_memrd_address");
297 Pass::call(design, "silicon_memory_lower");
298 Pass::call(design, "silicon_logic");
299 Pass::call(design, "silicon_compare");
300 Pass::call(design, "silicon_shift");
301 Pass::call(design, "simplemap t:$and r:Y_WIDTH=1 %i t:$or r:Y_WIDTH=1 %i "
302 "t:$xor r:Y_WIDTH=1 %i t:$not r:Y_WIDTH=1 %i "
303 "t:$reduce_and t:$reduce_or t:$reduce_xor t:$reduce_bool");
304 Pass::call(design, "extract_fa");
305 // The vector overload preserves a parsed path as one argument, including spaces.
306 Pass::call(design, std::vector<std::string>{"techmap", "-map", args[1]});
307 Pass::call(design, "opt_clean");
308 Pass::call(design, "silicon_gates");
309 Pass::call(design, "silicon_mux");
310 Pass::call(design, "silicon_validate");
311 }
313
314PRIVATE_NAMESPACE_END
int width(RTLIL::Module *module, RTLIL::Cell *cell, RTLIL::IdString key)
RTLIL::SigSpec port(RTLIL::Module *module, RTLIL::Cell *cell, RTLIL::IdString key, int expected)
void narrowTruth(RTLIL::Module *module, RTLIL::Cell *cell, int yWidth)
void runLogic(RTLIL::Design *design)
void normalizeOperand(RTLIL::Module *module, RTLIL::Cell *cell, RTLIL::IdString key, int originalWidth, int targetWidth, bool signExtend)
RTLIL::SigSpec selectionShift(RTLIL::Module *module, RTLIL::SigSpec value, const RTLIL::SigSpec &amount, bool left, RTLIL::Cell *&finalMux)
void runValidate(RTLIL::Design *design)
bool supported(RTLIL::IdString type)
void runCompare(RTLIL::Design *design)
void lowerSelection(RTLIL::Module *module, RTLIL::Cell *cell)
bool flag(RTLIL::Module *module, RTLIL::Cell *cell, RTLIL::IdString key)
void runShift(RTLIL::Design *design)
RTLIL::IdString id(const char *name)
void execute(std::vector< std::string > args, RTLIL::Design *design) override
void execute(std::vector< std::string > args, RTLIL::Design *design) override
void execute(std::vector< std::string > args, RTLIL::Design *design) override
void execute(std::vector< std::string > args, RTLIL::Design *design) override
void execute(std::vector< std::string > args, RTLIL::Design *design) override