Debian Codegraph @

generic/viaduct/fabulous/pack.cc

raw
1 /*
2 * nextpnr -- Next Generation Place and Route
3 *
4 * Copyright (C) 2021-22 gatecat <[email protected]>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 *
18 */
19
20 #include "pack.h"
21 #include "log.h"
22 #include "util.h"
23
24 #define VIADUCT_CONSTIDS "viaduct/fabulous/constids.inc"
25 #include "viaduct_constids.h"
26 #include "viaduct_helpers.h"
27
28 #include "validity_check.h"
29
30 NEXTPNR_NAMESPACE_BEGIN
31
32 namespace {
33 struct FabulousPacker
34 {
35 Context *ctx;
36 const FabricConfig &cfg;
37 ViaductHelpers h;
38
39 dict<IdString, unsigned> lut_types;
40 std::vector<IdString> lut_inputs;
41 CellTagger cell_tags;
42
43 FabulousPacker(Context *ctx, const FabricConfig &cfg) : ctx(ctx), cfg(cfg)
44 {
45 // Set up some structures for faster lookups
46 for (unsigned i = 0; i < cfg.clb.lut_k; i++) {
47 lut_types[ctx->idf("LUT%d", i + 1)] = i + 1;
48 lut_inputs.push_back(ctx->idf("I%d", i));
49 }
50 if (cfg.clb.lut_k == 4)
51 lut_types[id_LUT4_HA] = 4; // special case for now
52 h.init(ctx);
53 }
54
55 void pack_luts()
56 {
57 // Pack LUTs into FABULOUS_COMB (split-LUTFF mode) or FABULOUS_LC (packed-LUTFF mode)
58 // TODO: fracturable LUT handling
59 for (auto &cell : ctx->cells) {
60 CellInfo *ci = cell.second.get();
61 auto fnd_lut = lut_types.find(ci->type);
62 if (fnd_lut == lut_types.end())
63 continue;
64 unsigned lut_n = fnd_lut->second;
65 // convert to the necessary type
66 ci->type = cfg.clb.split_lc ? id_FABULOUS_COMB : id_FABULOUS_LC;
67 // add disconnected unused inputs
68 for (unsigned i = 0; i < cfg.clb.lut_k; i++)
69 if (!ci->ports.count(lut_inputs.at(i)))
70 ci->addInput(lut_inputs.at(i));
71 // replicate the INIT value so the unused MSBs become don't-cares
72 auto inst_init = get_or_default(ci->params, id_INIT, Property(0));
73 unsigned orig_init_len = 1U << lut_n, prim_len = 1U << cfg.clb.lut_k;
74 Property new_init(0, prim_len);
75 for (unsigned i = 0; i < prim_len; i += orig_init_len) {
76 auto chunk = inst_init.extract(0, orig_init_len);
77 for (unsigned j = 0; j < orig_init_len; j++)
78 new_init.str.at(i + j) = chunk.str.at(j);
79 }
80 new_init.update_intval();
81 ci->params[id_INIT] = new_init;
82 }
83 }
84
85 void assign_lc_info()
86 {
87 int flat_index = 0;
88 for (auto &cell : ctx->cells) {
89 cell.second->flat_index = flat_index++;
90 if (cell.second->type == id_FABULOUS_LC)
91 cell_tags.assign_for(ctx, cfg, cell.second.get());
92 }
93 }
94
95 // Two-stage flipflop packing. First convert all the random primitives into a much easier-to-handle FABULOUS_FF
96 // Then for split-LC mode, cluster it to connected LUTs; separate-LC mode, pack it into a connected or new LC
97
98 void prepare_ffs()
99 {
100 for (auto &cell : ctx->cells) {
101 CellInfo *ci = cell.second.get();
102 const std::string &type_str = ci->type.str(ctx);
103 if (type_str.size() < 5 || type_str.substr(0, 5) != "LUTFF")
104 continue;
105 ci->type = id_FABULOUS_FF;
106 // parse config string and unify
107 size_t idx = 5;
108 if (idx < type_str.size() && type_str.at(idx) == '_')
109 ++idx;
110 // clock inversion
111 if (idx < type_str.size() && type_str.at(idx) == 'N') {
112 ci->params[id_NEG_CLK] = 1;
113 ++idx;
114 } else {
115 ci->params[id_NEG_CLK] = 0;
116 }
117 // clock enable
118 if (idx < type_str.size() && type_str.at(idx) == 'E')
119 ++idx;
120 if (ci->ports.count(id_E))
121 ci->renamePort(id_E, id_EN);
122 else
123 ci->addInput(id_EN); // autocreate emtpy enable port if enable missing or unused
124 // sr presence and type
125 std::string srt = type_str.substr(idx);
126 if (srt == "S") {
127 ci->params[id_SET_NORESET] = 1;
128 ci->params[id_ASYNC_SR] = 1;
129 } else if (srt == "R") {
130 ci->params[id_SET_NORESET] = 0;
131 ci->params[id_ASYNC_SR] = 1;
132 } else if (srt == "SS") {
133 ci->params[id_SET_NORESET] = 1;
134 ci->params[id_ASYNC_SR] = 0;
135 } else if (srt == "SR" || srt == "") {
136 ci->params[id_SET_NORESET] = 0;
137 ci->params[id_ASYNC_SR] = 0;
138 } else {
139 NPNR_ASSERT_FALSE("unhandled FF type");
140 }
141 if (ci->ports.count(id_S))
142 ci->renamePort(id_S, id_SR);
143 else if (ci->ports.count(id_R))
144 ci->renamePort(id_R, id_SR);
145 if (!ci->ports.count(id_SR))
146 ci->addInput(id_SR); // autocreate emtpy enable port if enable missing or unused
147 }
148 }
149
150 void pack_muxes()
151 {
152 // TODO: don't hardcode z-offset -- we should come up with our own constraint structure
153 int lut_muxes_dz = 9;
154 int lut_lut_dz = 1;
155 for (auto &cell : ctx->cells) {
156 CellInfo *ci = cell.second.get();
157 unsigned k = 0;
158 if (ci->type == id_FABULOUS_MUX2)
159 k = 1;
160 else if (ci->type == id_FABULOUS_MUX4)
161 k = 2;
162 else if (ci->type == id_FABULOUS_MUX8)
163 k = 3;
164 else
165 continue;
166 unsigned m = (1U << k);
167 std::vector<CellInfo *> luts;
168 for (unsigned i = 0; i < m; i++) {
169 NetInfo *ii = ci->getPort(ctx->idf("I%d", i));
170 if (!ii || !ii->driver.cell || !ii->driver.cell->type.in(id_FABULOUS_LC, id_FABULOUS_COMB) ||
171 ii->driver.port != id_O)
172 log_error("mux %s input I%d net %s is not driven by a LUT!\n", ctx->nameOf(ci), i, ctx->nameOf(ii));
173 CellInfo *lut = ii->driver.cell;
174 NPNR_ASSERT(lut->cluster == ClusterId());
175 luts.push_back(lut);
176 }
177 luts.at(0)->cluster = luts.at(0)->name;
178 for (unsigned i = 0; i < m; i++) {
179 luts.at(i)->cluster = luts.at(0)->name;
180 luts.at(i)->constr_x = 0;
181 luts.at(i)->constr_y = 0;
182 luts.at(i)->constr_z = i * lut_lut_dz;
183 luts.at(i)->constr_abs_z = false;
184 if (i > 0)
185 luts.at(0)->constr_children.push_back(luts.at(i));
186 }
187 int extra_mux_dz = (m == 8) ? 7 : (m == 4) ? 1 : 0;
188 ci->cluster = luts.at(0)->name;
189 ci->constr_x = 0;
190 ci->constr_y = 0;
191 ci->constr_z = lut_muxes_dz + extra_mux_dz;
192 ci->constr_abs_z = false;
193 luts.at(0)->constr_children.push_back(ci);
194 }
195 }
196
197 bool check_cluster_legality(CellInfo *lc)
198 {
199 if (lc->cluster == ClusterId())
200 return true;
201 CLBState test_clb(cfg.clb);
202 auto process_cluster_cell = [&](CellInfo *ci) {
203 if (ci->constr_y != lc->constr_y)
204 return;
205 if (ci->type == id_FABULOUS_LC) {
206 NPNR_ASSERT(ci->constr_z >= 0 && ci->constr_z < int(cfg.clb.lc_per_clb));
207 test_clb.lc_comb[ci->constr_z] = ci;
208 } else if (ci->type.in(id_FABULOUS_MUX2, id_FABULOUS_MUX4, id_FABULOUS_MUX8)) {
209 int mux_z = (ci->constr_z - cfg.clb.lc_per_clb - 1);
210 NPNR_ASSERT(mux_z >= 0 && mux_z < int(cfg.clb.lc_per_clb));
211 test_clb.mux[mux_z] = ci;
212 }
213 // TODO: non-split MODE
214 };
215 CellInfo *root = ctx->getClusterRootCell(lc->cluster);
216 process_cluster_cell(root);
217 for (auto child : root->constr_children)
218 process_cluster_cell(child);
219 return test_clb.check_validity(cfg.clb, cell_tags);
220 }
221
222 void pack_ffs()
223 {
224 pool<IdString> to_delete;
225 assign_lc_info();
226 for (auto &cell : ctx->cells) {
227 CellInfo *ci = cell.second.get();
228 if (ci->type != id_FABULOUS_FF)
229 continue;
230 NetInfo *d = ci->getPort(id_D);
231 if (!d || !d->driver.cell)
232 continue;
233 CellInfo *drv = d->driver.cell;
234 if (drv->type != (cfg.clb.split_lc ? id_FABULOUS_COMB : id_FABULOUS_LC) || d->driver.port != id_O)
235 continue;
236 if (!cfg.clb.split_lc && d->users.entries() > 1)
237 continue; // TODO: could also resolve by duplicating LUT
238 // we can pack them together
239 if (cfg.clb.split_lc) {
240 // create/modify cluster and add constraints. copy from an arch where we do this already...
241 NPNR_ASSERT_FALSE("unimplemented");
242 } else {
243 // move config ports/params, these affect control set for the legality set
244 ci->movePortTo(id_CLK, drv, id_CLK);
245 ci->movePortTo(id_SR, drv, id_SR);
246 ci->movePortTo(id_EN, drv, id_EN);
247 drv->params[id_NEG_CLK] = ci->params[id_NEG_CLK];
248 drv->params[id_ASYNC_SR] = ci->params[id_ASYNC_SR];
249 drv->params[id_SET_NORESET] = ci->params[id_SET_NORESET];
250 drv->params[id_FF] = 1;
251 // update tags for this cluster checks
252 cell_tags.assign_for(ctx, cfg, drv);
253 if (drv->cluster != ClusterId()) {
254 if (!check_cluster_legality(drv)) {
255 // If packing wasn't legal; revert half-finished move
256 for (auto p : {id_NEG_CLK, id_SR, id_EN, id_FF})
257 drv->params.erase(p);
258 drv->movePortTo(id_CLK, ci, id_CLK);
259 drv->movePortTo(id_SR, ci, id_SR);
260 drv->movePortTo(id_EN, ci, id_EN);
261 // revert tag changes too for this cluster checks
262 cell_tags.assign_for(ctx, cfg, drv);
263 continue;
264 }
265 }
266 // this connection is packed inside the LC
267 to_delete.insert(ci->name);
268 ci->movePortTo(id_O, drv, id_Q);
269 ci->disconnectPort(id_D);
270 drv->disconnectPort(id_O);
271 for (auto &attr : ci->attrs)
272 drv->attrs[attr.first] = attr.second;
273 // update tags for future cluster checks
274 cell_tags.assign_for(ctx, cfg, drv);
275 }
276 }
277 for (auto del : to_delete)
278 ctx->cells.erase(del);
279 if (!cfg.clb.split_lc) {
280 // convert remaining ffs to their own lc
281 for (auto &cell : ctx->cells) {
282 CellInfo *ci = cell.second.get();
283 if (ci->type != id_FABULOUS_FF)
284 continue;
285 ci->type = id_FABULOUS_LC;
286 ci->renamePort(id_D, lut_inputs.at(0));
287 ci->renamePort(id_O, id_Q);
288 // configure LUT as a thru
289 Property init(1U << cfg.clb.lut_k);
290 for (unsigned i = 0; i < (1U << cfg.clb.lut_k); i += 2) {
291 init.str[i] = Property::S0;
292 init.str[i + 1] = Property::S1;
293 }
294 init.update_intval();
295 ci->params[id_INIT] = init;
296 ci->params[id_FF] = 1;
297 }
298 }
299 }
300
301 void update_bel_attrs()
302 {
303 // This new arch uses the new IdStringList system with a / separator
304 // old fabulous arches used a dot separator in bel names
305 // update old attributes for maximum cross-compat
306 for (auto &cell : ctx->cells) {
307 CellInfo *ci = cell.second.get();
308 if (!ci->attrs.count(id_BEL))
309 continue;
310 std::string &bel = ci->attrs.at(id_BEL).str;
311 if (bel.find('/') != std::string::npos) // new style
312 continue;
313 size_t dot_pos = bel.find('.');
314 if (dot_pos != std::string::npos)
315 bel[dot_pos] = '/';
316 }
317 }
318
319 void handle_constants()
320 {
321 h.replace_constants(CellTypePort(id__CONST1_DRV, id_O), CellTypePort(id__CONST0_DRV, id_O), {}, {});
322 }
323
324 void handle_io()
325 {
326 // Any remaining $nextpnr_*buf cells were not constrained via PCF.
327 // Verify each connects to an IO cell via PAD, then remove it.
328 IdString ibuf = ctx->id("$nextpnr_ibuf");
329 IdString obuf = ctx->id("$nextpnr_obuf");
330 IdString iobuf = ctx->id("$nextpnr_iobuf");
331
332 std::vector<IdString> to_remove;
333 for (auto &cell : ctx->cells) {
334 auto &ci = *cell.second;
335 if (!ci.type.in(ibuf, obuf, iobuf))
336 continue;
337 bool found_pad = false;
338 bool constrained = false;
339 for (auto &port : ci.ports) {
340 NetInfo *net = port.second.net;
341 if (!net)
342 continue;
343
344 for (auto &usr : net->users) {
345 if (usr.cell == &ci)
346 continue;
347 if (usr.port != id_PAD)
348 log_error("Top-level port '%s' connected to illegal port %s.%s (must be PAD)\n",
349 ctx->nameOf(&ci), ctx->nameOf(usr.cell), ctx->nameOf(usr.port));
350 if (usr.cell->attrs.count(id_BEL))
351 constrained = true;
352 if (found_pad)
353 log_error("Top-level port '%s' connected to multiple PAD ports (at least %s.%s and %s.%s)\n",
354 ctx->nameOf(&ci), ctx->nameOf(usr.cell), ctx->nameOf(usr.port),
355 ctx->nameOf(net->driver.cell), ctx->nameOf(net->driver.port));
356 found_pad = true;
357 }
358
359 if (found_pad)
360 continue;
361
362 auto &drv = net->driver;
363 if (drv.cell && drv.cell != &ci) {
364 if (drv.port != id_PAD)
365 log_error("Top-level port '%s' connected to illegal port %s.%s (must be PAD)\n",
366 ctx->nameOf(&ci), ctx->nameOf(drv.cell), ctx->nameOf(drv.port));
367 if (drv.cell->attrs.count(id_BEL))
368 constrained = true;
369 if (found_pad)
370 log_error("Top-level port '%s' connected to multiple PAD ports (at least %s.%s and %s.%s)\n",
371 ctx->nameOf(&ci), ctx->nameOf(drv.cell), ctx->nameOf(drv.port),
372 ctx->nameOf(net->driver.cell), ctx->nameOf(net->driver.port));
373 found_pad = true;
374 }
375 }
376 if (!found_pad)
377 log_error("No IO cell found connected to '%s' via PAD port. Was iopadmap run in Yosys?\n",
378 ctx->nameOf(&ci));
379 if (!constrained)
380 log_info("port is unconstrained: %s This pin will be assigned randomly\n", ctx->nameOf(&ci));
381 ci.disconnectPort(id_I);
382 ci.disconnectPort(id_O);
383 to_remove.push_back(ci.name);
384 }
385 for (IdString cell_name : to_remove)
386 ctx->cells.erase(cell_name);
387 }
388
389 void constrain_carries()
390 {
391 std::vector<CellInfo *> carry_roots;
392 for (auto &cell : ctx->cells) {
393 CellInfo *ci = cell.second.get();
394 if (!ci->type.in(id_FABULOUS_LC, id_FABULOUS_COMB))
395 continue;
396 if (!ci->getPort(id_Ci) && ci->getPort(id_Co))
397 carry_roots.push_back(ci);
398 else if (ci->getPort(id_Ci) && ci->getPort(id_Ci)->driver.cell) {
399 auto &drv = ci->getPort(id_Ci)->driver;
400 if (drv.port != id_Co || !drv.cell->type.in(id_FABULOUS_LC, id_FABULOUS_COMB))
401 log_error("Carry cell '%s' has Ci driven by illegal port '%s.%s'\n", ctx->nameOf(ci),
402 ctx->nameOf(drv.cell), ctx->nameOf(drv.port));
403 }
404 }
405 for (auto root : carry_roots) {
406 CellInfo *cursor = root;
407 int dy = 0, dz = 0;
408 while (true) {
409 // create cluster
410 cursor->cluster = root->name;
411 cursor->constr_z = dz;
412 cursor->constr_abs_z = true;
413 if (cursor != root) {
414 cursor->constr_x = 0;
415 cursor->constr_y = -dy;
416 root->constr_children.push_back(cursor);
417 }
418 NetInfo *co = cursor->getPort(id_Co);
419 if (co && !co->users.empty()) {
420 if (co->users.entries() > 1)
421 log_error("Carry cell '%s' has illegal multiple fanout on Co net '%s'\n", ctx->nameOf(cursor),
422 ctx->nameOf(co));
423 auto &usr = *(co->users.begin());
424 if (usr.port != id_Ci || !usr.cell->type.in(id_FABULOUS_LC, id_FABULOUS_COMB))
425 log_error("Carry cell '%s' has illegal fanout '%s.%s' on Co net '%s'\n", ctx->nameOf(cursor),
426 ctx->nameOf(usr.cell), ctx->nameOf(usr.port), ctx->nameOf(co));
427 cursor = usr.cell;
428 } else {
429 break;
430 }
431 ++dz;
432 if (dz == int(cfg.clb.lc_per_clb)) {
433 dz = 0;
434 ++dy;
435 }
436 }
437 }
438 }
439
440 void run()
441 {
442 update_bel_attrs();
443 handle_constants();
444 handle_io();
445 pack_luts();
446 pack_muxes();
447 prepare_ffs();
448 constrain_carries();
449 pack_ffs();
450 }
451 };
452 } // namespace
453
454 void fabulous_pack(Context *ctx, const FabricConfig &cfg)
455 {
456 FabulousPacker packer(ctx, cfg);
457 packer.run();
458 }
459
460 NEXTPNR_NAMESPACE_END