2 * yosys -- Yosys Open SYnthesis Suite
4 * Copyright (C) 2012 Clifford Wolf <clifford@clifford.at>
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.
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.
20 #include "kernel/yosys.h"
21 #include "kernel/utils.h"
22 #include "kernel/sigtools.h"
23 #include "libs/sha1/sha1.h"
29 #include "simplemap.h"
34 extern void maccmap(RTLIL::Module
*module
, RTLIL::Cell
*cell
, bool unmap
= false);
39 PRIVATE_NAMESPACE_BEGIN
41 void apply_prefix(IdString prefix
, IdString
&id
)
44 id
= stringf("%s.%s", prefix
.c_str(), id
.c_str()+1);
46 id
= stringf("$techmap%s.%s", prefix
.c_str(), id
.c_str());
49 void apply_prefix(IdString prefix
, RTLIL::SigSpec
&sig
, RTLIL::Module
*module
)
51 vector
<SigChunk
> chunks
= sig
;
52 for (auto &chunk
: chunks
)
53 if (chunk
.wire
!= nullptr) {
54 IdString wire_name
= chunk
.wire
->name
;
55 apply_prefix(prefix
, wire_name
);
56 log_assert(module
->wire(wire_name
) != nullptr);
57 chunk
.wire
= module
->wire(wire_name
);
64 dict
<IdString
, void(*)(RTLIL::Module
*, RTLIL::Cell
*)> simplemap_mappers
;
65 dict
<std::pair
<IdString
, dict
<IdString
, RTLIL::Const
>>, RTLIL::Module
*> techmap_cache
;
66 dict
<RTLIL::Module
*, bool> techmap_do_cache
;
67 pool
<RTLIL::Module
*> module_queue
;
68 dict
<Module
*, SigMap
> sigmaps
;
70 pool
<IdString
> flatten_do_list
;
71 pool
<IdString
> flatten_done_list
;
72 pool
<Cell
*> flatten_keep_list
;
74 pool
<string
> log_msg_cache
;
76 struct TechmapWireData
{
81 typedef dict
<IdString
, std::vector
<TechmapWireData
>> TechmapWires
;
95 recursive_mode
= false;
96 autoproc_mode
= false;
100 std::string
constmap_tpl_name(SigMap
&sigmap
, RTLIL::Module
*tpl
, RTLIL::Cell
*cell
, bool verbose
)
102 std::string constmap_info
;
103 dict
<RTLIL::SigBit
, std::pair
<IdString
, int>> connbits_map
;
105 for (auto &conn
: cell
->connections())
106 for (int i
= 0; i
< GetSize(conn
.second
); i
++) {
107 RTLIL::SigBit bit
= sigmap(conn
.second
[i
]);
108 if (bit
.wire
== nullptr) {
110 log(" Constant input on bit %d of port %s: %s\n", i
, log_id(conn
.first
), log_signal(bit
));
111 constmap_info
+= stringf("|%s %d %d", log_id(conn
.first
), i
, bit
.data
);
112 } else if (connbits_map
.count(bit
)) {
114 log(" Bit %d of port %s and bit %d of port %s are connected.\n", i
, log_id(conn
.first
),
115 connbits_map
.at(bit
).second
, log_id(connbits_map
.at(bit
).first
));
116 constmap_info
+= stringf("|%s %d %s %d", log_id(conn
.first
), i
,
117 log_id(connbits_map
.at(bit
).first
), connbits_map
.at(bit
).second
);
119 connbits_map
.emplace(bit
, std::make_pair(conn
.first
, i
));
120 constmap_info
+= stringf("|%s %d", log_id(conn
.first
), i
);
124 return stringf("$paramod$constmap:%s%s", sha1(constmap_info
).c_str(), tpl
->name
.c_str());
127 TechmapWires
techmap_find_special_wires(RTLIL::Module
*module
)
131 if (module
== nullptr)
134 for (auto w
: module
->wires()) {
135 const char *p
= w
->name
.c_str();
139 const char *q
= strrchr(p
+1, '.');
143 if (!strncmp(p
, "\\_TECHMAP_", 10)) {
144 TechmapWireData record
;
147 result
[p
].push_back(record
);
148 w
->set_bool_attribute(ID::keep
);
149 w
->set_bool_attribute(ID::_techmap_special_
);
153 if (!result
.empty()) {
154 SigMap
sigmap(module
);
155 for (auto &it1
: result
)
156 for (auto &it2
: it1
.second
)
157 sigmap
.apply(it2
.value
);
163 void techmap_module_worker(RTLIL::Design
*design
, RTLIL::Module
*module
, RTLIL::Cell
*cell
, RTLIL::Module
*tpl
)
165 if (tpl
->processes
.size() != 0) {
166 log("Technology map yielded processes:");
167 for (auto &it
: tpl
->processes
)
168 log(" %s",log_id(it
.first
));
171 Pass::call_on_module(tpl
->design
, tpl
, "proc");
172 log_assert(GetSize(tpl
->processes
) == 0);
174 log_error("Technology map yielded processes -> this is not supported (use -autoproc to run 'proc' automatically).\n");
177 std::string orig_cell_name
;
178 pool
<string
> extra_src_attrs
= cell
->get_strpool_attribute(ID::src
);
180 orig_cell_name
= cell
->name
.str();
182 for (auto tpl_cell
: tpl
->cells())
183 if (tpl_cell
->name
== ID::_TECHMAP_REPLACE_
) {
184 module
->rename(cell
, stringf("$techmap%d", autoidx
++) + cell
->name
.str());
189 dict
<IdString
, IdString
> memory_renames
;
191 for (auto &it
: tpl
->memories
) {
192 IdString m_name
= it
.first
;
193 apply_prefix(cell
->name
, m_name
);
194 RTLIL::Memory
*m
= new RTLIL::Memory
;
196 m
->width
= it
.second
->width
;
197 m
->start_offset
= it
.second
->start_offset
;
198 m
->size
= it
.second
->size
;
199 m
->attributes
= it
.second
->attributes
;
200 if (m
->attributes
.count(ID::src
))
201 m
->add_strpool_attribute(ID::src
, extra_src_attrs
);
202 module
->memories
[m
->name
] = m
;
203 memory_renames
[it
.first
] = m
->name
;
204 design
->select(module
, m
);
207 dict
<IdString
, IdString
> positional_ports
;
208 dict
<Wire
*, IdString
> temp_renamed_wires
;
209 pool
<SigBit
> autopurge_tpl_bits
;
211 for (auto tpl_w
: tpl
->wires())
213 if (tpl_w
->port_id
> 0)
215 IdString posportname
= stringf("$%d", tpl_w
->port_id
);
216 positional_ports
.emplace(posportname
, tpl_w
->name
);
218 if (!flatten_mode
&& tpl_w
->get_bool_attribute(ID::techmap_autopurge
) &&
219 (!cell
->hasPort(tpl_w
->name
) || !GetSize(cell
->getPort(tpl_w
->name
))) &&
220 (!cell
->hasPort(posportname
) || !GetSize(cell
->getPort(posportname
))))
222 if (sigmaps
.count(tpl
) == 0)
223 sigmaps
[tpl
].set(tpl
);
225 for (auto bit
: sigmaps
.at(tpl
)(tpl_w
))
226 if (bit
.wire
!= nullptr)
227 autopurge_tpl_bits
.insert(bit
);
230 IdString w_name
= tpl_w
->name
;
231 apply_prefix(cell
->name
, w_name
);
232 RTLIL::Wire
*w
= module
->wire(w_name
);
234 if (!flatten_mode
|| !w
->get_bool_attribute(ID::hierconn
)) {
235 temp_renamed_wires
[w
] = w
->name
;
236 module
->rename(w
, NEW_ID
);
239 w
->attributes
.erase(ID::hierconn
);
240 if (GetSize(w
) < GetSize(tpl_w
)) {
241 log_warning("Widening signal %s.%s to match size of %s.%s (via %s.%s).\n", log_id(module
), log_id(w
),
242 log_id(tpl
), log_id(tpl_w
), log_id(module
), log_id(cell
));
243 w
->width
= GetSize(tpl_w
);
248 w
= module
->addWire(w_name
, tpl_w
);
249 w
->port_input
= false;
250 w
->port_output
= false;
253 w
->attributes
.erase(ID::techmap_autopurge
);
254 if (tpl_w
->get_bool_attribute(ID::_techmap_special_
))
255 w
->attributes
.clear();
256 if (w
->attributes
.count(ID::src
))
257 w
->add_strpool_attribute(ID::src
, extra_src_attrs
);
259 design
->select(module
, w
);
261 if (tpl_w
->name
.begins_with("\\_TECHMAP_REPLACE_.")) {
262 IdString replace_name
= stringf("%s%s", orig_cell_name
.c_str(), tpl_w
->name
.c_str() + strlen("\\_TECHMAP_REPLACE_"));
263 Wire
*replace_w
= module
->addWire(replace_name
, tpl_w
);
264 module
->connect(replace_w
, w
);
268 SigMap
tpl_sigmap(tpl
);
269 pool
<SigBit
> tpl_written_bits
;
271 for (auto tpl_cell
: tpl
->cells())
272 for (auto &conn
: tpl_cell
->connections())
273 if (tpl_cell
->output(conn
.first
))
274 for (auto bit
: tpl_sigmap(conn
.second
))
275 tpl_written_bits
.insert(bit
);
276 for (auto &conn
: tpl
->connections())
277 for (auto bit
: tpl_sigmap(conn
.first
))
278 tpl_written_bits
.insert(bit
);
280 SigMap port_signal_map
;
281 SigSig port_signal_assign
;
283 for (auto &it
: cell
->connections())
285 IdString portname
= it
.first
;
286 if (positional_ports
.count(portname
) > 0)
287 portname
= positional_ports
.at(portname
);
288 if (tpl
->wire(portname
) == nullptr || tpl
->wire(portname
)->port_id
== 0) {
289 if (portname
.begins_with("$"))
290 log_error("Can't map port `%s' of cell `%s' to template `%s'!\n", portname
.c_str(), cell
->name
.c_str(), tpl
->name
.c_str());
294 if (GetSize(it
.second
) == 0)
297 RTLIL::Wire
*w
= tpl
->wire(portname
);
298 RTLIL::SigSig c
, extra_connect
;
300 if (w
->port_output
&& !w
->port_input
) {
302 c
.second
= RTLIL::SigSpec(w
);
303 apply_prefix(cell
->name
, c
.second
, module
);
304 extra_connect
.first
= c
.second
;
305 extra_connect
.second
= c
.first
;
306 } else if (!w
->port_output
&& w
->port_input
) {
307 c
.first
= RTLIL::SigSpec(w
);
308 c
.second
= it
.second
;
309 apply_prefix(cell
->name
, c
.first
, module
);
310 extra_connect
.first
= c
.first
;
311 extra_connect
.second
= c
.second
;
313 SigSpec sig_tpl
= w
, sig_tpl_pf
= w
, sig_mod
= it
.second
;
314 apply_prefix(cell
->name
, sig_tpl_pf
, module
);
315 for (int i
= 0; i
< GetSize(sig_tpl
) && i
< GetSize(sig_mod
); i
++) {
316 if (tpl_written_bits
.count(tpl_sigmap(sig_tpl
[i
]))) {
317 c
.first
.append(sig_mod
[i
]);
318 c
.second
.append(sig_tpl_pf
[i
]);
320 c
.first
.append(sig_tpl_pf
[i
]);
321 c
.second
.append(sig_mod
[i
]);
324 extra_connect
.first
= sig_tpl_pf
;
325 extra_connect
.second
= sig_mod
;
328 if (c
.second
.size() > c
.first
.size())
329 c
.second
.remove(c
.first
.size(), c
.second
.size() - c
.first
.size());
331 if (c
.second
.size() < c
.first
.size())
332 c
.second
.append(RTLIL::SigSpec(RTLIL::State::S0
, c
.first
.size() - c
.second
.size()));
334 log_assert(c
.first
.size() == c
.second
.size());
338 // more conservative approach:
339 // connect internal and external wires
341 if (sigmaps
.count(module
) == 0)
342 sigmaps
[module
].set(module
);
344 if (sigmaps
.at(module
)(c
.first
).has_const())
345 log_error("Mismatch in directionality for cell port %s.%s.%s: %s <= %s\n",
346 log_id(module
), log_id(cell
), log_id(it
.first
), log_signal(c
.first
), log_signal(c
.second
));
352 // approach that yields nicer outputs:
353 // replace internal wires that are connected to external wires
355 if (w
->port_output
&& !w
->port_input
) {
356 port_signal_map
.add(c
.second
, c
.first
);
358 if (!w
->port_output
&& w
->port_input
) {
359 port_signal_map
.add(c
.first
, c
.second
);
362 extra_connect
= SigSig();
365 for (auto &attr
: w
->attributes
) {
366 if (attr
.first
== ID::src
)
368 auto lhs
= GetSize(extra_connect
.first
);
369 auto rhs
= GetSize(extra_connect
.second
);
371 extra_connect
.first
.remove(rhs
, lhs
-rhs
);
373 extra_connect
.second
.remove(lhs
, rhs
-lhs
);
374 module
->connect(extra_connect
);
380 for (auto tpl_cell
: tpl
->cells())
382 IdString c_name
= tpl_cell
->name
;
383 bool techmap_replace_cell
= (!flatten_mode
) && (c_name
== ID::_TECHMAP_REPLACE_
);
385 if (techmap_replace_cell
)
386 c_name
= orig_cell_name
;
387 else if (tpl_cell
->name
.begins_with("\\_TECHMAP_REPLACE_."))
388 c_name
= stringf("%s%s", orig_cell_name
.c_str(), c_name
.c_str() + strlen("\\_TECHMAP_REPLACE_"));
390 apply_prefix(cell
->name
, c_name
);
392 RTLIL::Cell
*c
= module
->addCell(c_name
, tpl_cell
);
393 design
->select(module
, c
);
395 if (!flatten_mode
&& c
->type
.begins_with("\\$"))
396 c
->type
= c
->type
.substr(1);
398 vector
<IdString
> autopurge_ports
;
400 for (auto &conn
: c
->connections())
402 bool autopurge
= false;
403 if (!autopurge_tpl_bits
.empty()) {
404 autopurge
= GetSize(conn
.second
) != 0;
405 for (auto &bit
: sigmaps
.at(tpl
)(conn
.second
))
406 if (!autopurge_tpl_bits
.count(bit
)) {
413 autopurge_ports
.push_back(conn
.first
);
415 RTLIL::SigSpec new_conn
= conn
.second
;
416 apply_prefix(cell
->name
, new_conn
, module
);
417 port_signal_map
.apply(new_conn
);
418 c
->setPort(conn
.first
, std::move(new_conn
));
422 for (auto &it2
: autopurge_ports
)
425 if (c
->type
.in(ID($memrd
), ID($memwr
), ID($meminit
))) {
426 IdString memid
= c
->getParam(ID::MEMID
).decode_string();
427 log_assert(memory_renames
.count(memid
) != 0);
428 c
->setParam(ID::MEMID
, Const(memory_renames
[memid
].str()));
431 if (c
->type
== ID($mem
)) {
432 IdString memid
= c
->getParam(ID::MEMID
).decode_string();
433 apply_prefix(cell
->name
, memid
);
434 c
->setParam(ID::MEMID
, Const(memid
.c_str()));
437 if (c
->attributes
.count(ID::src
))
438 c
->add_strpool_attribute(ID::src
, extra_src_attrs
);
440 if (techmap_replace_cell
)
441 for (auto attr
: cell
->attributes
)
442 if (!c
->attributes
.count(attr
.first
))
443 c
->attributes
[attr
.first
] = attr
.second
;
446 for (auto &it
: tpl
->connections()) {
447 RTLIL::SigSig c
= it
;
448 apply_prefix(cell
->name
.str(), c
.first
, module
);
449 apply_prefix(cell
->name
.str(), c
.second
, module
);
450 port_signal_map
.apply(c
.first
);
451 port_signal_map
.apply(c
.second
);
455 module
->remove(cell
);
457 for (auto &it
: temp_renamed_wires
)
460 IdString name
= it
.second
;
461 IdString altname
= module
->uniquify(name
);
462 Wire
*other_w
= module
->wire(name
);
463 module
->rename(other_w
, altname
);
464 module
->rename(w
, name
);
468 bool techmap_module(RTLIL::Design
*design
, RTLIL::Module
*module
, RTLIL::Design
*map
, pool
<RTLIL::Cell
*> &handled_cells
,
469 const dict
<IdString
, pool
<IdString
>> &celltypeMap
, bool in_recursion
)
471 std::string mapmsg_prefix
= in_recursion
? "Recursively mapping" : "Mapping";
473 if (!design
->selected(module
) || module
->get_blackbox_attribute(ignore_wb
))
476 bool log_continue
= false;
477 bool did_something
= false;
478 LogMakeDebugHdl mkdebug
;
480 SigMap
sigmap(module
);
482 dict
<SigBit
, State
> init_bits
;
483 pool
<SigBit
> remove_init_bits
;
485 for (auto wire
: module
->wires()) {
486 if (wire
->attributes
.count(ID::init
)) {
487 Const value
= wire
->attributes
.at(ID::init
);
488 for (int i
= 0; i
< min(GetSize(value
), GetSize(wire
)); i
++)
489 if (value
[i
] != State::Sx
)
490 init_bits
[sigmap(SigBit(wire
, i
))] = value
[i
];
494 TopoSort
<RTLIL::Cell
*, IdString::compare_ptr_by_name
<RTLIL::Cell
>> cells
;
495 dict
<RTLIL::Cell
*, pool
<RTLIL::SigBit
>> cell_to_inbit
;
496 dict
<RTLIL::SigBit
, pool
<RTLIL::Cell
*>> outbit_to_cell
;
498 for (auto cell
: module
->selected_cells())
500 if (handled_cells
.count(cell
) > 0)
503 std::string cell_type
= cell
->type
.str();
504 if (in_recursion
&& cell
->type
.begins_with("\\$"))
505 cell_type
= cell_type
.substr(1);
507 if (celltypeMap
.count(cell_type
) == 0) {
508 if (assert_mode
&& cell_type
.back() != '_')
509 log_error("(ASSERT MODE) No matching template cell for type %s found.\n", log_id(cell_type
));
514 bool keepit
= cell
->get_bool_attribute(ID::keep_hierarchy
);
515 for (auto &tpl_name
: celltypeMap
.at(cell_type
))
516 if (map
->module(tpl_name
)->get_bool_attribute(ID::keep_hierarchy
))
519 if (!flatten_keep_list
[cell
]) {
520 log("Keeping %s.%s (found keep_hierarchy property).\n", log_id(module
), log_id(cell
));
521 flatten_keep_list
.insert(cell
);
523 if (!flatten_done_list
[cell
->type
])
524 flatten_do_list
.insert(cell
->type
);
529 for (auto &conn
: cell
->connections())
531 RTLIL::SigSpec sig
= sigmap(conn
.second
);
534 if (GetSize(sig
) == 0)
537 for (auto &tpl_name
: celltypeMap
.at(cell_type
)) {
538 RTLIL::Module
*tpl
= map
->module(tpl_name
);
539 RTLIL::Wire
*port
= tpl
->wire(conn
.first
);
540 if (port
&& port
->port_input
)
541 cell_to_inbit
[cell
].insert(sig
.begin(), sig
.end());
542 if (port
&& port
->port_output
)
543 for (auto &bit
: sig
)
544 outbit_to_cell
[bit
].insert(cell
);
551 for (auto &it_right
: cell_to_inbit
)
552 for (auto &it_sigbit
: it_right
.second
)
553 for (auto &it_left
: outbit_to_cell
[it_sigbit
])
554 cells
.edge(it_left
, it_right
.first
);
558 for (auto cell
: cells
.sorted
)
560 log_assert(handled_cells
.count(cell
) == 0);
561 log_assert(cell
== module
->cell(cell
->name
));
562 bool mapped_cell
= false;
564 std::string cell_type
= cell
->type
.str();
566 if (in_recursion
&& cell
->type
.begins_with("\\$"))
567 cell_type
= cell_type
.substr(1);
569 for (auto &tpl_name
: celltypeMap
.at(cell_type
))
571 IdString derived_name
= tpl_name
;
572 RTLIL::Module
*tpl
= map
->module(tpl_name
);
573 dict
<IdString
, RTLIL::Const
> parameters(cell
->parameters
);
575 if (tpl
->get_blackbox_attribute(ignore_wb
))
580 std::string extmapper_name
;
582 if (tpl
->get_bool_attribute(ID::techmap_simplemap
))
583 extmapper_name
= "simplemap";
585 if (tpl
->get_bool_attribute(ID::techmap_maccmap
))
586 extmapper_name
= "maccmap";
588 if (tpl
->attributes
.count(ID::techmap_wrap
))
589 extmapper_name
= "wrap";
591 if (!extmapper_name
.empty())
593 cell
->type
= cell_type
;
595 if ((extern_mode
&& !in_recursion
) || extmapper_name
== "wrap")
597 std::string m_name
= stringf("$extern:%s:%s", extmapper_name
.c_str(), log_id(cell
->type
));
599 for (auto &c
: cell
->parameters
)
600 m_name
+= stringf(":%s=%s", log_id(c
.first
), log_signal(c
.second
));
602 if (extmapper_name
== "wrap")
603 m_name
+= ":" + sha1(tpl
->attributes
.at(ID::techmap_wrap
).decode_string());
605 RTLIL::Design
*extmapper_design
= extern_mode
&& !in_recursion
? design
: tpl
->design
;
606 RTLIL::Module
*extmapper_module
= extmapper_design
->module(m_name
);
608 if (extmapper_module
== nullptr)
610 extmapper_module
= extmapper_design
->addModule(m_name
);
611 RTLIL::Cell
*extmapper_cell
= extmapper_module
->addCell(cell
->type
, cell
);
613 extmapper_cell
->set_src_attribute(cell
->get_src_attribute());
615 int port_counter
= 1;
616 for (auto &c
: extmapper_cell
->connections_
) {
617 RTLIL::Wire
*w
= extmapper_module
->addWire(c
.first
, GetSize(c
.second
));
618 if (w
->name
.in(ID::Y
, ID::Q
))
619 w
->port_output
= true;
621 w
->port_input
= true;
622 w
->port_id
= port_counter
++;
626 extmapper_module
->fixup_ports();
627 extmapper_module
->check();
629 if (extmapper_name
== "simplemap") {
630 log("Creating %s with simplemap.\n", log_id(extmapper_module
));
631 if (simplemap_mappers
.count(extmapper_cell
->type
) == 0)
632 log_error("No simplemap mapper for cell type %s found!\n", log_id(extmapper_cell
->type
));
633 simplemap_mappers
.at(extmapper_cell
->type
)(extmapper_module
, extmapper_cell
);
634 extmapper_module
->remove(extmapper_cell
);
637 if (extmapper_name
== "maccmap") {
638 log("Creating %s with maccmap.\n", log_id(extmapper_module
));
639 if (extmapper_cell
->type
!= ID($macc
))
640 log_error("The maccmap mapper can only map $macc (not %s) cells!\n", log_id(extmapper_cell
->type
));
641 maccmap(extmapper_module
, extmapper_cell
);
642 extmapper_module
->remove(extmapper_cell
);
645 if (extmapper_name
== "wrap") {
646 std::string cmd_string
= tpl
->attributes
.at(ID::techmap_wrap
).decode_string();
647 log("Running \"%s\" on wrapper %s.\n", cmd_string
.c_str(), log_id(extmapper_module
));
649 Pass::call_on_module(extmapper_design
, extmapper_module
, cmd_string
);
654 cell
->type
= extmapper_module
->name
;
655 cell
->parameters
.clear();
657 if (!extern_mode
|| in_recursion
) {
658 tpl
= extmapper_module
;
659 goto use_wrapper_tpl
;
662 auto msg
= stringf("Using extmapper %s for cells of type %s.", log_id(extmapper_module
), log_id(cell
->type
));
663 if (!log_msg_cache
.count(msg
)) {
664 log_msg_cache
.insert(msg
);
665 log("%s\n", msg
.c_str());
667 log_debug("%s %s.%s (%s) to %s.\n", mapmsg_prefix
.c_str(), log_id(module
), log_id(cell
), log_id(cell
->type
), log_id(extmapper_module
));
671 auto msg
= stringf("Using extmapper %s for cells of type %s.", extmapper_name
.c_str(), log_id(cell
->type
));
672 if (!log_msg_cache
.count(msg
)) {
673 log_msg_cache
.insert(msg
);
674 log("%s\n", msg
.c_str());
676 log_debug("%s %s.%s (%s) with %s.\n", mapmsg_prefix
.c_str(), log_id(module
), log_id(cell
), log_id(cell
->type
), extmapper_name
.c_str());
678 if (extmapper_name
== "simplemap") {
679 if (simplemap_mappers
.count(cell
->type
) == 0)
680 log_error("No simplemap mapper for cell type %s found!\n", log_id(cell
->type
));
681 simplemap_mappers
.at(cell
->type
)(module
, cell
);
684 if (extmapper_name
== "maccmap") {
685 if (cell
->type
!= ID($macc
))
686 log_error("The maccmap mapper can only map $macc (not %s) cells!\n", log_id(cell
->type
));
687 maccmap(module
, cell
);
690 module
->remove(cell
);
694 did_something
= true;
699 for (auto &conn
: cell
->connections()) {
700 if (conn
.first
.begins_with("$"))
702 if (tpl
->wire(conn
.first
) != nullptr && tpl
->wire(conn
.first
)->port_id
> 0)
704 if (!conn
.second
.is_fully_const() || parameters
.count(conn
.first
) > 0 || tpl
->avail_parameters
.count(conn
.first
) == 0)
706 parameters
[conn
.first
] = conn
.second
.as_const();
714 if (tpl
->avail_parameters
.count(ID::_TECHMAP_CELLTYPE_
) != 0)
715 parameters
.emplace(ID::_TECHMAP_CELLTYPE_
, RTLIL::unescape_id(cell
->type
));
717 for (auto &conn
: cell
->connections()) {
718 if (tpl
->avail_parameters
.count(stringf("\\_TECHMAP_CONSTMSK_%s_", log_id(conn
.first
))) != 0) {
719 std::vector
<RTLIL::SigBit
> v
= sigmap(conn
.second
).to_sigbit_vector();
721 bit
= RTLIL::SigBit(bit
.wire
== nullptr ? RTLIL::State::S1
: RTLIL::State::S0
);
722 parameters
.emplace(stringf("\\_TECHMAP_CONSTMSK_%s_", log_id(conn
.first
)), RTLIL::SigSpec(v
).as_const());
724 if (tpl
->avail_parameters
.count(stringf("\\_TECHMAP_CONSTVAL_%s_", log_id(conn
.first
))) != 0) {
725 std::vector
<RTLIL::SigBit
> v
= sigmap(conn
.second
).to_sigbit_vector();
727 if (bit
.wire
!= nullptr)
728 bit
= RTLIL::SigBit(RTLIL::State::Sx
);
729 parameters
.emplace(stringf("\\_TECHMAP_CONSTVAL_%s_", log_id(conn
.first
)), RTLIL::SigSpec(v
).as_const());
731 if (tpl
->avail_parameters
.count(stringf("\\_TECHMAP_WIREINIT_%s_", log_id(conn
.first
))) != 0) {
732 auto sig
= sigmap(conn
.second
);
733 RTLIL::Const
value(State::Sx
, sig
.size());
734 for (int i
= 0; i
< sig
.size(); i
++) {
735 auto it
= init_bits
.find(sig
[i
]);
736 if (it
!= init_bits
.end()) {
737 value
[i
] = it
->second
;
740 parameters
.emplace(stringf("\\_TECHMAP_WIREINIT_%s_", log_id(conn
.first
)), value
);
744 int unique_bit_id_counter
= 0;
745 dict
<RTLIL::SigBit
, int> unique_bit_id
;
746 unique_bit_id
[RTLIL::State::S0
] = unique_bit_id_counter
++;
747 unique_bit_id
[RTLIL::State::S1
] = unique_bit_id_counter
++;
748 unique_bit_id
[RTLIL::State::Sx
] = unique_bit_id_counter
++;
749 unique_bit_id
[RTLIL::State::Sz
] = unique_bit_id_counter
++;
751 for (auto &conn
: cell
->connections())
752 if (tpl
->avail_parameters
.count(stringf("\\_TECHMAP_CONNMAP_%s_", log_id(conn
.first
))) != 0) {
753 for (auto &bit
: sigmap(conn
.second
))
754 if (unique_bit_id
.count(bit
) == 0)
755 unique_bit_id
[bit
] = unique_bit_id_counter
++;
758 // Find highest bit set
760 for (int i
= 0; i
< 32; i
++)
761 if (((unique_bit_id_counter
-1) & (1 << i
)) != 0)
763 // Increment index by one to get number of bits
765 if (tpl
->avail_parameters
.count(ID::_TECHMAP_BITS_CONNMAP_
))
766 parameters
[ID::_TECHMAP_BITS_CONNMAP_
] = bits
;
768 for (auto &conn
: cell
->connections())
769 if (tpl
->avail_parameters
.count(stringf("\\_TECHMAP_CONNMAP_%s_", log_id(conn
.first
))) != 0) {
771 for (auto &bit
: sigmap(conn
.second
)) {
772 int val
= unique_bit_id
.at(bit
);
773 for (int i
= 0; i
< bits
; i
++) {
774 value
.bits
.push_back((val
& 1) != 0 ? State::S1
: State::S0
);
778 parameters
.emplace(stringf("\\_TECHMAP_CONNMAP_%s_", log_id(conn
.first
)), value
);
784 // do not register techmap_wrap modules with techmap_cache
786 std::pair
<IdString
, dict
<IdString
, RTLIL::Const
>> key(tpl_name
, parameters
);
787 auto it
= techmap_cache
.find(key
);
788 if (it
!= techmap_cache
.end()) {
791 if (parameters
.size() != 0) {
793 derived_name
= tpl
->derive(map
, parameters
);
794 tpl
= map
->module(derived_name
);
797 techmap_cache
.emplace(std::move(key
), tpl
);
802 techmap_do_cache
[tpl
] = true;
804 RTLIL::Module
*constmapped_tpl
= map
->module(constmap_tpl_name(sigmap
, tpl
, cell
, false));
805 if (constmapped_tpl
!= nullptr)
806 tpl
= constmapped_tpl
;
809 if (techmap_do_cache
.count(tpl
) == 0)
811 bool keep_running
= true;
812 techmap_do_cache
[tpl
] = true;
814 pool
<IdString
> techmap_wire_names
;
818 TechmapWires twd
= techmap_find_special_wires(tpl
);
819 keep_running
= false;
822 techmap_wire_names
.insert(it
.first
);
824 for (auto &it
: twd
[ID::_TECHMAP_FAIL_
]) {
825 RTLIL::SigSpec value
= it
.value
;
826 if (value
.is_fully_const() && value
.as_bool()) {
827 log("Not using module `%s' from techmap as it contains a %s marker wire with non-zero value %s.\n",
828 derived_name
.c_str(), log_id(it
.wire
->name
), log_signal(value
));
829 techmap_do_cache
[tpl
] = false;
833 if (!techmap_do_cache
[tpl
])
838 if (!it
.first
.begins_with("\\_TECHMAP_DO_") || it
.second
.empty())
841 auto &data
= it
.second
.front();
843 if (!data
.value
.is_fully_const())
844 log_error("Techmap yielded config wire %s with non-const value %s.\n", log_id(data
.wire
->name
), log_signal(data
.value
));
846 techmap_wire_names
.erase(it
.first
);
848 const char *p
= data
.wire
->name
.c_str();
849 const char *q
= strrchr(p
+1, '.');
852 std::string cmd_string
= data
.value
.as_const().decode_string();
854 restart_eval_cmd_string
:
855 if (cmd_string
.rfind("CONSTMAP; ", 0) == 0)
857 cmd_string
= cmd_string
.substr(strlen("CONSTMAP; "));
859 log("Analyzing pattern of constant bits for this cell:\n");
860 IdString new_tpl_name
= constmap_tpl_name(sigmap
, tpl
, cell
, true);
861 log("Creating constmapped module `%s'.\n", log_id(new_tpl_name
));
862 log_assert(map
->module(new_tpl_name
) == nullptr);
864 RTLIL::Module
*new_tpl
= map
->addModule(new_tpl_name
);
865 tpl
->cloneInto(new_tpl
);
867 techmap_do_cache
.erase(tpl
);
868 techmap_do_cache
[new_tpl
] = true;
871 dict
<RTLIL::SigBit
, RTLIL::SigBit
> port_new2old_map
;
872 dict
<RTLIL::SigBit
, RTLIL::SigBit
> port_connmap
;
873 dict
<RTLIL::SigBit
, RTLIL::SigBit
> cellbits_to_tplbits
;
875 for (auto wire
: tpl
->wires().to_vector())
877 if (!wire
->port_input
|| wire
->port_output
)
880 IdString port_name
= wire
->name
;
881 tpl
->rename(wire
, NEW_ID
);
883 RTLIL::Wire
*new_wire
= tpl
->addWire(port_name
, wire
);
884 wire
->port_input
= false;
887 for (int i
= 0; i
< wire
->width
; i
++) {
888 port_new2old_map
.emplace(RTLIL::SigBit(new_wire
, i
), RTLIL::SigBit(wire
, i
));
889 port_connmap
.emplace(RTLIL::SigBit(wire
, i
), RTLIL::SigBit(new_wire
, i
));
893 for (auto &conn
: cell
->connections())
894 for (int i
= 0; i
< GetSize(conn
.second
); i
++)
896 RTLIL::SigBit bit
= sigmap(conn
.second
[i
]);
897 RTLIL::SigBit
tplbit(tpl
->wire(conn
.first
), i
);
899 if (bit
.wire
== nullptr)
901 RTLIL::SigBit oldbit
= port_new2old_map
.at(tplbit
);
902 port_connmap
.at(oldbit
) = bit
;
904 else if (cellbits_to_tplbits
.count(bit
))
906 RTLIL::SigBit oldbit
= port_new2old_map
.at(tplbit
);
907 port_connmap
.at(oldbit
) = cellbits_to_tplbits
[bit
];
910 cellbits_to_tplbits
[bit
] = tplbit
;
913 RTLIL::SigSig port_conn
;
914 for (auto &it
: port_connmap
) {
915 port_conn
.first
.append(it
.first
);
916 port_conn
.second
.append(it
.second
);
918 tpl
->connect(port_conn
);
921 goto restart_eval_cmd_string
;
924 if (cmd_string
.rfind("RECURSION; ", 0) == 0)
926 cmd_string
= cmd_string
.substr(strlen("RECURSION; "));
927 while (techmap_module(map
, tpl
, map
, handled_cells
, celltypeMap
, true)) { }
928 goto restart_eval_cmd_string
;
931 Pass::call_on_module(map
, tpl
, cmd_string
);
933 log_assert(!strncmp(q
, "_TECHMAP_DO_", 12));
934 std::string new_name
= data
.wire
->name
.substr(0, q
-p
) + "_TECHMAP_DONE_" + data
.wire
->name
.substr(q
-p
+12);
935 while (tpl
->wire(new_name
) != nullptr)
937 tpl
->rename(data
.wire
->name
, new_name
);
944 TechmapWires twd
= techmap_find_special_wires(tpl
);
945 for (auto &it
: twd
) {
946 if (it
.first
!= ID::_TECHMAP_FAIL_
&& (!it
.first
.begins_with("\\_TECHMAP_REMOVEINIT_") || !it
.first
.ends_with("_")) && !it
.first
.begins_with("\\_TECHMAP_DO_") && !it
.first
.begins_with("\\_TECHMAP_DONE_"))
947 log_error("Techmap yielded unknown config wire %s.\n", log_id(it
.first
));
948 if (techmap_do_cache
[tpl
])
949 for (auto &it2
: it
.second
)
950 if (!it2
.value
.is_fully_const())
951 log_error("Techmap yielded config wire %s with non-const value %s.\n", log_id(it2
.wire
->name
), log_signal(it2
.value
));
952 techmap_wire_names
.erase(it
.first
);
955 for (auto &it
: techmap_wire_names
)
956 log_error("Techmap special wire %s disappeared. This is considered a fatal error.\n", log_id(it
));
958 if (recursive_mode
) {
960 log_header(design
, "Continuing TECHMAP pass.\n");
961 log_continue
= false;
964 while (techmap_module(map
, tpl
, map
, handled_cells
, celltypeMap
, true)) { }
968 if (techmap_do_cache
.at(tpl
) == false)
972 log_header(design
, "Continuing TECHMAP pass.\n");
973 log_continue
= false;
977 TechmapWires twd
= techmap_find_special_wires(tpl
);
978 for (auto &it
: twd
) {
979 if (it
.first
.begins_with("\\_TECHMAP_REMOVEINIT_")) {
980 for (auto &it2
: it
.second
) {
981 auto val
= it2
.value
.as_const();
982 auto wirename
= RTLIL::escape_id(it
.first
.substr(21, it
.first
.size() - 21 - 1));
983 auto it
= cell
->connections().find(wirename
);
984 if (it
!= cell
->connections().end()) {
985 auto sig
= sigmap(it
->second
);
986 for (int i
= 0; i
< sig
.size(); i
++)
987 if (val
[i
] == State::S1
)
988 remove_init_bits
.insert(sig
[i
]);
994 if (extern_mode
&& !in_recursion
)
996 std::string m_name
= stringf("$extern:%s", log_id(tpl
));
998 if (!design
->module(m_name
))
1000 RTLIL::Module
*m
= design
->addModule(m_name
);
1003 for (auto cell
: m
->cells()) {
1004 if (cell
->type
.begins_with("\\$"))
1005 cell
->type
= cell
->type
.substr(1);
1008 module_queue
.insert(m
);
1011 log_debug("%s %s.%s to imported %s.\n", mapmsg_prefix
.c_str(), log_id(module
), log_id(cell
), log_id(m_name
));
1012 cell
->type
= m_name
;
1013 cell
->parameters
.clear();
1017 auto msg
= stringf("Using template %s for cells of type %s.", log_id(tpl
), log_id(cell
->type
));
1018 if (!log_msg_cache
.count(msg
)) {
1019 log_msg_cache
.insert(msg
);
1020 log("%s\n", msg
.c_str());
1022 log_debug("%s %s.%s (%s) using %s.\n", mapmsg_prefix
.c_str(), log_id(module
), log_id(cell
), log_id(cell
->type
), log_id(tpl
));
1023 techmap_module_worker(design
, module
, cell
, tpl
);
1026 did_something
= true;
1031 if (assert_mode
&& !mapped_cell
)
1032 log_error("(ASSERT MODE) Failed to map cell %s.%s (%s).\n", log_id(module
), log_id(cell
), log_id(cell
->type
));
1034 handled_cells
.insert(cell
);
1037 if (!remove_init_bits
.empty()) {
1038 for (auto wire
: module
->wires())
1039 if (wire
->attributes
.count(ID::init
)) {
1040 Const
&value
= wire
->attributes
.at(ID::init
);
1041 bool do_cleanup
= true;
1042 for (int i
= 0; i
< min(GetSize(value
), GetSize(wire
)); i
++) {
1043 SigBit bit
= sigmap(SigBit(wire
, i
));
1044 if (remove_init_bits
.count(bit
))
1045 value
[i
] = State::Sx
;
1046 else if (value
[i
] != State::Sx
)
1050 log("Removing init attribute from wire %s.%s.\n", log_id(module
), log_id(wire
));
1051 wire
->attributes
.erase(ID::init
);
1057 log_header(design
, "Continuing TECHMAP pass.\n");
1058 log_continue
= false;
1062 return did_something
;
1066 struct TechmapPass
: public Pass
{
1067 TechmapPass() : Pass("techmap", "generic technology mapper") { }
1068 void help() YS_OVERRIDE
1070 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
1072 log(" techmap [-map filename] [selection]\n");
1074 log("This pass implements a very simple technology mapper that replaces cells in\n");
1075 log("the design with implementations given in form of a Verilog or ilang source\n");
1078 log(" -map filename\n");
1079 log(" the library of cell implementations to be used.\n");
1080 log(" without this parameter a builtin library is used that\n");
1081 log(" transforms the internal RTL cells to the internal gate\n");
1084 log(" -map %%<design-name>\n");
1085 log(" like -map above, but with an in-memory design instead of a file.\n");
1088 log(" load the cell implementations as separate modules into the design\n");
1089 log(" instead of inlining them.\n");
1091 log(" -max_iter <number>\n");
1092 log(" only run the specified number of iterations on each module.\n");
1093 log(" default: unlimited\n");
1095 log(" -recursive\n");
1096 log(" instead of the iterative breadth-first algorithm use a recursive\n");
1097 log(" depth-first algorithm. both methods should yield equivalent results,\n");
1098 log(" but may differ in performance.\n");
1100 log(" -autoproc\n");
1101 log(" Automatically call \"proc\" on implementations that contain processes.\n");
1104 log(" Ignore the 'whitebox' attribute on cell implementations.\n");
1107 log(" this option will cause techmap to exit with an error if it can't map\n");
1108 log(" a selected cell. only cell types that end on an underscore are accepted\n");
1109 log(" as final cell types by this mode.\n");
1111 log(" -D <define>, -I <incdir>\n");
1112 log(" this options are passed as-is to the Verilog frontend for loading the\n");
1113 log(" map file. Note that the Verilog frontend is also called with the\n");
1114 log(" '-nooverwrite' option set.\n");
1116 log("When a module in the map file has the 'techmap_celltype' attribute set, it will\n");
1117 log("match cells with a type that match the text value of this attribute. Otherwise\n");
1118 log("the module name will be used to match the cell.\n");
1120 log("When a module in the map file has the 'techmap_simplemap' attribute set, techmap\n");
1121 log("will use 'simplemap' (see 'help simplemap') to map cells matching the module.\n");
1123 log("When a module in the map file has the 'techmap_maccmap' attribute set, techmap\n");
1124 log("will use 'maccmap' (see 'help maccmap') to map cells matching the module.\n");
1126 log("When a module in the map file has the 'techmap_wrap' attribute set, techmap\n");
1127 log("will create a wrapper for the cell and then run the command string that the\n");
1128 log("attribute is set to on the wrapper module.\n");
1130 log("When a port on a module in the map file has the 'techmap_autopurge' attribute\n");
1131 log("set, and that port is not connected in the instantiation that is mapped, then\n");
1132 log("then a cell port connected only to such wires will be omitted in the mapped\n");
1133 log("version of the circuit.\n");
1135 log("All wires in the modules from the map file matching the pattern _TECHMAP_*\n");
1136 log("or *._TECHMAP_* are special wires that are used to pass instructions from\n");
1137 log("the mapping module to the techmap command. At the moment the following special\n");
1138 log("wires are supported:\n");
1140 log(" _TECHMAP_FAIL_\n");
1141 log(" When this wire is set to a non-zero constant value, techmap will not\n");
1142 log(" use this module and instead try the next module with a matching\n");
1143 log(" 'techmap_celltype' attribute.\n");
1145 log(" When such a wire exists but does not have a constant value after all\n");
1146 log(" _TECHMAP_DO_* commands have been executed, an error is generated.\n");
1148 log(" _TECHMAP_DO_*\n");
1149 log(" This wires are evaluated in alphabetical order. The constant text value\n");
1150 log(" of this wire is a yosys command (or sequence of commands) that is run\n");
1151 log(" by techmap on the module. A common use case is to run 'proc' on modules\n");
1152 log(" that are written using always-statements.\n");
1154 log(" When such a wire has a non-constant value at the time it is to be\n");
1155 log(" evaluated, an error is produced. That means it is possible for such a\n");
1156 log(" wire to start out as non-constant and evaluate to a constant value\n");
1157 log(" during processing of other _TECHMAP_DO_* commands.\n");
1159 log(" A _TECHMAP_DO_* command may start with the special token 'CONSTMAP; '.\n");
1160 log(" in this case techmap will create a copy for each distinct configuration\n");
1161 log(" of constant inputs and shorted inputs at this point and import the\n");
1162 log(" constant and connected bits into the map module. All further commands\n");
1163 log(" are executed in this copy. This is a very convenient way of creating\n");
1164 log(" optimized specializations of techmap modules without using the special\n");
1165 log(" parameters described below.\n");
1167 log(" A _TECHMAP_DO_* command may start with the special token 'RECURSION; '.\n");
1168 log(" then techmap will recursively replace the cells in the module with their\n");
1169 log(" implementation. This is not affected by the -max_iter option.\n");
1171 log(" It is possible to combine both prefixes to 'RECURSION; CONSTMAP; '.\n");
1173 log(" _TECHMAP_REMOVEINIT_<port-name>_\n");
1174 log(" When this wire is set to a constant value, the init attribute of the wire(s)\n");
1175 log(" connected to this port will be consumed. This wire must have the same\n");
1176 log(" width as the given port, and for every bit that is set to 1 in the value,\n");
1177 log(" the corresponding init attribute bit will be changed to 1'bx. If all\n");
1178 log(" bits of an init attribute are left as x, it will be removed.\n");
1180 log("In addition to this special wires, techmap also supports special parameters in\n");
1181 log("modules in the map file:\n");
1183 log(" _TECHMAP_CELLTYPE_\n");
1184 log(" When a parameter with this name exists, it will be set to the type name\n");
1185 log(" of the cell that matches the module.\n");
1187 log(" _TECHMAP_CONSTMSK_<port-name>_\n");
1188 log(" _TECHMAP_CONSTVAL_<port-name>_\n");
1189 log(" When this pair of parameters is available in a module for a port, then\n");
1190 log(" former has a 1-bit for each constant input bit and the latter has the\n");
1191 log(" value for this bit. The unused bits of the latter are set to undef (x).\n");
1193 log(" _TECHMAP_WIREINIT_<port-name>_\n");
1194 log(" When a parameter with this name exists, it will be set to the initial\n");
1195 log(" value of the wire(s) connected to the given port, as specified by the init\n");
1196 log(" attribute. If the attribute doesn't exist, x will be filled for the\n");
1197 log(" missing bits. To remove the init attribute bits used, use the\n");
1198 log(" _TECHMAP_REMOVEINIT_*_ wires.\n");
1200 log(" _TECHMAP_BITS_CONNMAP_\n");
1201 log(" _TECHMAP_CONNMAP_<port-name>_\n");
1202 log(" For an N-bit port, the _TECHMAP_CONNMAP_<port-name>_ parameter, if it\n");
1203 log(" exists, will be set to an N*_TECHMAP_BITS_CONNMAP_ bit vector containing\n");
1204 log(" N words (of _TECHMAP_BITS_CONNMAP_ bits each) that assign each single\n");
1205 log(" bit driver a unique id. The values 0-3 are reserved for 0, 1, x, and z.\n");
1206 log(" This can be used to detect shorted inputs.\n");
1208 log("When a module in the map file has a parameter where the according cell in the\n");
1209 log("design has a port, the module from the map file is only used if the port in\n");
1210 log("the design is connected to a constant value. The parameter is then set to the\n");
1211 log("constant value.\n");
1213 log("A cell with the name _TECHMAP_REPLACE_ in the map file will inherit the name\n");
1214 log("and attributes of the cell that is being replaced.\n");
1215 log("A cell with a name of the form `_TECHMAP_REPLACE_.<suffix>` in the map file will\n");
1216 log("be named thus but with the `_TECHMAP_REPLACE_' prefix substituted with the name\n");
1217 log("of the cell being replaced.\n");
1218 log("Similarly, a wire named in the form `_TECHMAP_REPLACE_.<suffix>` will cause a\n");
1219 log("new wire alias to be created and named as above but with the `_TECHMAP_REPLACE_'\n");
1220 log("prefix also substituted.\n");
1222 log("See 'help extract' for a pass that does the opposite thing.\n");
1224 log("See 'help flatten' for a pass that does flatten the design (which is\n");
1225 log("essentially techmap but using the design itself as map library).\n");
1228 void execute(std::vector
<std::string
> args
, RTLIL::Design
*design
) YS_OVERRIDE
1230 log_header(design
, "Executing TECHMAP pass (map to technology primitives).\n");
1233 TechmapWorker worker
;
1234 simplemap_get_mappers(worker
.simplemap_mappers
);
1236 std::vector
<std::string
> map_files
;
1237 std::string verilog_frontend
= "verilog -nooverwrite -noblackbox";
1241 for (argidx
= 1; argidx
< args
.size(); argidx
++) {
1242 if (args
[argidx
] == "-map" && argidx
+1 < args
.size()) {
1243 map_files
.push_back(args
[++argidx
]);
1246 if (args
[argidx
] == "-max_iter" && argidx
+1 < args
.size()) {
1247 max_iter
= atoi(args
[++argidx
].c_str());
1250 if (args
[argidx
] == "-D" && argidx
+1 < args
.size()) {
1251 verilog_frontend
+= " -D " + args
[++argidx
];
1254 if (args
[argidx
] == "-I" && argidx
+1 < args
.size()) {
1255 verilog_frontend
+= " -I " + args
[++argidx
];
1258 if (args
[argidx
] == "-assert") {
1259 worker
.assert_mode
= true;
1262 if (args
[argidx
] == "-extern") {
1263 worker
.extern_mode
= true;
1266 if (args
[argidx
] == "-recursive") {
1267 worker
.recursive_mode
= true;
1270 if (args
[argidx
] == "-autoproc") {
1271 worker
.autoproc_mode
= true;
1274 if (args
[argidx
] == "-wb") {
1275 worker
.ignore_wb
= true;
1280 extra_args(args
, argidx
, design
);
1282 RTLIL::Design
*map
= new RTLIL::Design
;
1283 if (map_files
.empty()) {
1284 Frontend::frontend_call(map
, nullptr, "+/techmap.v", verilog_frontend
);
1286 for (auto &fn
: map_files
)
1287 if (fn
.compare(0, 1, "%") == 0) {
1288 if (!saved_designs
.count(fn
.substr(1))) {
1290 log_cmd_error("Can't open saved design `%s'.\n", fn
.c_str()+1);
1292 for (auto mod
: saved_designs
.at(fn
.substr(1))->modules())
1293 if (!map
->module(mod
->name
))
1294 map
->add(mod
->clone());
1296 Frontend::frontend_call(map
, nullptr, fn
, (fn
.size() > 3 && fn
.compare(fn
.size()-3, std::string::npos
, ".il") == 0 ? "ilang" : verilog_frontend
));
1300 log_header(design
, "Continuing TECHMAP pass.\n");
1302 dict
<IdString
, pool
<IdString
>> celltypeMap
;
1303 for (auto module
: map
->modules()) {
1304 if (module
->attributes
.count(ID::techmap_celltype
) && !module
->attributes
.at(ID::techmap_celltype
).bits
.empty()) {
1305 char *p
= strdup(module
->attributes
.at(ID::techmap_celltype
).decode_string().c_str());
1306 for (char *q
= strtok(p
, " \t\r\n"); q
; q
= strtok(nullptr, " \t\r\n"))
1307 celltypeMap
[RTLIL::escape_id(q
)].insert(module
->name
);
1310 IdString module_name
= module
->name
.begins_with("\\$") ?
1311 module
->name
.substr(1) : module
->name
.str();
1312 celltypeMap
[module_name
].insert(module
->name
);
1315 for (auto &i
: celltypeMap
)
1316 i
.second
.sort(RTLIL::sort_by_id_str());
1318 for (auto module
: design
->modules())
1319 worker
.module_queue
.insert(module
);
1321 while (!worker
.module_queue
.empty())
1323 RTLIL::Module
*module
= *worker
.module_queue
.begin();
1324 worker
.module_queue
.erase(module
);
1326 int module_max_iter
= max_iter
;
1327 bool did_something
= true;
1328 pool
<RTLIL::Cell
*> handled_cells
;
1329 while (did_something
) {
1330 did_something
= false;
1331 if (worker
.techmap_module(design
, module
, map
, handled_cells
, celltypeMap
, false))
1332 did_something
= true;
1335 if (module_max_iter
> 0 && --module_max_iter
== 0)
1340 log("No more expansions possible.\n");
1347 struct FlattenPass
: public Pass
{
1348 FlattenPass() : Pass("flatten", "flatten design") { }
1349 void help() YS_OVERRIDE
1351 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
1353 log(" flatten [options] [selection]\n");
1355 log("This pass flattens the design by replacing cells by their implementation. This\n");
1356 log("pass is very similar to the 'techmap' pass. The only difference is that this\n");
1357 log("pass is using the current design as mapping library.\n");
1359 log("Cells and/or modules with the 'keep_hierarchy' attribute set will not be\n");
1360 log("flattened by this command.\n");
1363 log(" Ignore the 'whitebox' attribute on cell implementations.\n");
1366 void execute(std::vector
<std::string
> args
, RTLIL::Design
*design
) YS_OVERRIDE
1368 log_header(design
, "Executing FLATTEN pass (flatten design).\n");
1371 TechmapWorker worker
;
1372 worker
.flatten_mode
= true;
1375 for (argidx
= 1; argidx
< args
.size(); argidx
++) {
1376 if (args
[argidx
] == "-wb") {
1377 worker
.ignore_wb
= true;
1382 extra_args(args
, argidx
, design
);
1385 dict
<IdString
, pool
<IdString
>> celltypeMap
;
1386 for (auto module
: design
->modules())
1387 celltypeMap
[module
->name
].insert(module
->name
);
1388 for (auto &i
: celltypeMap
)
1389 i
.second
.sort(RTLIL::sort_by_id_str());
1391 RTLIL::Module
*top_mod
= nullptr;
1392 if (design
->full_selection())
1393 for (auto mod
: design
->modules())
1394 if (mod
->get_bool_attribute(ID::top
))
1397 pool
<RTLIL::Cell
*> handled_cells
;
1398 if (top_mod
!= nullptr) {
1399 worker
.flatten_do_list
.insert(top_mod
->name
);
1400 while (!worker
.flatten_do_list
.empty()) {
1401 auto mod
= design
->module(*worker
.flatten_do_list
.begin());
1402 while (worker
.techmap_module(design
, mod
, design
, handled_cells
, celltypeMap
, false)) { }
1403 worker
.flatten_done_list
.insert(mod
->name
);
1404 worker
.flatten_do_list
.erase(mod
->name
);
1407 for (auto mod
: design
->modules().to_vector())
1408 while (worker
.techmap_module(design
, mod
, design
, handled_cells
, celltypeMap
, false)) { }
1412 log("No more expansions possible.\n");
1414 if (top_mod
!= nullptr)
1416 pool
<IdString
> used_modules
, new_used_modules
;
1417 new_used_modules
.insert(top_mod
->name
);
1418 while (!new_used_modules
.empty()) {
1419 pool
<IdString
> queue
;
1420 queue
.swap(new_used_modules
);
1421 for (auto modname
: queue
)
1422 used_modules
.insert(modname
);
1423 for (auto modname
: queue
)
1424 for (auto cell
: design
->module(modname
)->cells())
1425 if (design
->module(cell
->type
) && !used_modules
[cell
->type
])
1426 new_used_modules
.insert(cell
->type
);
1429 for (auto mod
: design
->modules().to_vector())
1430 if (!used_modules
[mod
->name
] && !mod
->get_blackbox_attribute(worker
.ignore_wb
)) {
1431 log("Deleting now unused module %s.\n", log_id(mod
));
1432 design
->remove(mod
);
1440 PRIVATE_NAMESPACE_END